Save an opt-in off/no/yes answer to private Am I lost questions from known chat contacts. Keep reply traffic and ACK state bounded, preserve pending user delivery, and expose the setting through local CLI and ESP32 WebConfig. Retain compact STM32 application/filesystem boundaries, qualify the Tag BLE queue allowance, and exercise saved-image compatibility and browser controls. Fix stale room mailbox protocol dependencies in the ACL/route test fixtures. Validation: ten Companion builds, sanitized production-path regressions, preference fault/compatibility tests, browser tests, and 86 capacity/build recipe checks passed. No live radio was flashed for this feature.
270 lines
13 KiB
C++
270 lines
13 KiB
C++
#pragma once
|
|
|
|
#include <Arduino.h> // needed for PlatformIO
|
|
#include <Mesh.h>
|
|
#include <helpers/AdvertDataHelpers.h>
|
|
#include <helpers/RemoteCliRequest.h>
|
|
#include <helpers/TxtDataHelpers.h>
|
|
|
|
#define MAX_TEXT_LEN (10*CIPHER_BLOCK_SIZE) // must be LESS than (MAX_PACKET_PAYLOAD - 4 - CIPHER_MAC_SIZE - 1)
|
|
|
|
static constexpr size_t MAX_CORRELATED_CLI_TEXT_LEN =
|
|
MAX_PACKET_PAYLOAD - CIPHER_MAC_SIZE - (CIPHER_BLOCK_SIZE - 1)
|
|
- 5 - mesh::RemoteCliRequest::EXTENSION_SIZE;
|
|
static_assert(MAX_CORRELATED_CLI_TEXT_LEN <= MAX_TEXT_LEN,
|
|
"correlated CLI text must fit the encrypted datagram");
|
|
|
|
#include "ContactInfo.h"
|
|
|
|
#define MAX_SEARCH_RESULTS 24
|
|
|
|
#define MSG_SEND_FAILED 0
|
|
#define MSG_SEND_SENT_FLOOD 1
|
|
#define MSG_SEND_SENT_DIRECT 2
|
|
|
|
#define REQ_TYPE_GET_STATUS 0x01 // same as _GET_STATS
|
|
#define REQ_TYPE_KEEP_ALIVE 0x02
|
|
|
|
#define RESP_SERVER_LOGIN_OK 0 // response to ANON_REQ
|
|
|
|
class ContactVisitor {
|
|
public:
|
|
virtual void onContactVisit(const ContactInfo& contact) = 0;
|
|
};
|
|
|
|
class BaseChatMesh;
|
|
|
|
class ContactsIterator {
|
|
int next_idx;
|
|
public:
|
|
ContactsIterator(int start) { next_idx = start; }
|
|
bool hasNext(const BaseChatMesh* mesh, ContactInfo& dest);
|
|
};
|
|
|
|
#ifndef MAX_CONTACTS
|
|
#define MAX_CONTACTS 32
|
|
#endif
|
|
|
|
#define MAX_ANON_CONTACTS 8
|
|
|
|
#ifndef MAX_CONNECTIONS
|
|
#define MAX_CONNECTIONS 16
|
|
#endif
|
|
|
|
struct ConnectionInfo {
|
|
mesh::Identity server_id;
|
|
unsigned long next_ping;
|
|
uint32_t last_activity;
|
|
uint32_t keep_alive_millis;
|
|
uint32_t expected_ack;
|
|
};
|
|
|
|
#include "ChannelDetails.h"
|
|
|
|
/**
|
|
* \brief abstract Mesh class for common 'chat' client
|
|
*/
|
|
class BaseChatMesh : public mesh::Mesh {
|
|
|
|
friend class ContactsIterator;
|
|
|
|
#if defined(ESP32_PLATFORM) && defined(BOARD_HAS_PSRAM)
|
|
enum {
|
|
CONTACT_PSRAM_FALLBACK_REGULAR_SLOTS =
|
|
MAX_CONTACTS < 16 ? MAX_CONTACTS : 16,
|
|
CONTACT_PSRAM_FALLBACK_TOTAL_SLOTS =
|
|
MAX_ANON_CONTACTS + CONTACT_PSRAM_FALLBACK_REGULAR_SLOTS
|
|
};
|
|
ContactInfo* contacts;
|
|
ContactInfo contacts_fallback[CONTACT_PSRAM_FALLBACK_TOTAL_SLOTS];
|
|
int* sort_array;
|
|
int sort_array_fallback[CONTACT_PSRAM_FALLBACK_TOTAL_SLOTS];
|
|
int contact_capacity;
|
|
#else
|
|
ContactInfo contacts[MAX_CONTACTS+MAX_ANON_CONTACTS];
|
|
int sort_array[MAX_CONTACTS+MAX_ANON_CONTACTS];
|
|
#endif
|
|
int num_contacts = 0;
|
|
uint32_t contact_table_revision;
|
|
int matching_peer_indexes[MAX_SEARCH_RESULTS];
|
|
unsigned long txt_send_timeout;
|
|
#ifdef MAX_GROUP_CHANNELS
|
|
ChannelDetails channels[MAX_GROUP_CHANNELS];
|
|
int num_channels; // only for addChannel()
|
|
#endif
|
|
mesh::Packet* _pendingLoopback;
|
|
uint8_t temp_buf[MAX_TRANS_UNIT];
|
|
ConnectionInfo connections[MAX_CONNECTIONS];
|
|
|
|
mesh::Packet* composeMsgPacket(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char *text, uint32_t& expected_ack);
|
|
|
|
protected:
|
|
void sendAckTo(const ContactInfo& dest, const uint8_t* ack_hash, uint8_t ack_len=4);
|
|
// Autonomous replies have independent ACK tracking and must preserve the
|
|
// application/terminal send's shared timeout.
|
|
int sendMessageDetached(const ContactInfo& recipient, uint32_t timestamp,
|
|
const char* text, uint32_t& expected_ack,
|
|
uint32_t& est_timeout, uint8_t* packet_hash);
|
|
BaseChatMesh(mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::PacketManager& mgr, mesh::MeshTables& tables)
|
|
: mesh::Mesh(radio, ms, rng, rtc, mgr, tables)
|
|
{
|
|
#if defined(ESP32_PLATFORM) && defined(BOARD_HAS_PSRAM)
|
|
contacts = contacts_fallback;
|
|
sort_array = sort_array_fallback;
|
|
contact_capacity = CONTACT_PSRAM_FALLBACK_TOTAL_SLOTS;
|
|
#endif
|
|
// No live references exist during base construction. Seed the initially
|
|
// empty prefix directly; runtime resets also notify reference owners.
|
|
#if defined(NRF52_PLATFORM)
|
|
for (int i = 0; i < MAX_ANON_CONTACTS; i++) {
|
|
contacts[i].storage_slot = mesh::storage::CONTACT_SLOT_NONE;
|
|
}
|
|
#endif
|
|
num_contacts = MAX_ANON_CONTACTS;
|
|
contact_table_revision = 1;
|
|
|
|
#ifdef MAX_GROUP_CHANNELS
|
|
memset(channels, 0, sizeof(channels));
|
|
num_channels = 0;
|
|
#endif
|
|
txt_send_timeout = 0;
|
|
_pendingLoopback = NULL;
|
|
memset(connections, 0, sizeof(connections));
|
|
}
|
|
|
|
void bootstrapRTCfromContacts();
|
|
bool initializeContactStorage();
|
|
static void resetContactValue(ContactInfo& contact);
|
|
|
|
void resetContacts() {
|
|
for (int i = 0; i < num_contacts; ++i) {
|
|
onContactReferenceChanged(&contacts[i], NULL);
|
|
resetContactValue(contacts[i]);
|
|
}
|
|
#if defined(NRF52_PLATFORM)
|
|
for (int i = 0; i < MAX_ANON_CONTACTS; i++) {
|
|
contacts[i].storage_slot = mesh::storage::CONTACT_SLOT_NONE;
|
|
}
|
|
#endif
|
|
num_contacts = MAX_ANON_CONTACTS; // seed the first contacts for anon requests
|
|
contact_table_revision++;
|
|
}
|
|
void populateContactFromAdvert(ContactInfo& ci, const mesh::Identity& id, const AdvertDataParser& parser, uint32_t timestamp);
|
|
ContactInfo* allocateContactSlot(bool transient_only=false); // helper to find slot for new contact
|
|
|
|
// 'UI' concepts, for sub-classes to implement
|
|
virtual bool isAutoAddEnabled() const { return true; }
|
|
virtual bool shouldAutoAddContactType(uint8_t type) const { return true; }
|
|
virtual void onContactsFull() {};
|
|
virtual bool shouldOverwriteWhenFull() const { return false; }
|
|
virtual uint8_t getAutoAddMaxHops() const { return 0; } // 0 = no limit, 1 = direct (0 hops), N = up to N-1 hops
|
|
virtual bool canMutateContacts() const { return true; }
|
|
// Return false to preserve the exact selected contact when a subclass cannot
|
|
// safely retire its associated persistent state. A refusal must not mutate
|
|
// the contact or its backing state.
|
|
virtual bool onContactOverwrite(const ContactInfo&) { return true; };
|
|
// A replacement is already initialized with the same identity. Notify after
|
|
// copying it, before the old slot is cleared/reused; NULL retires an identity.
|
|
// Subclasses that retain no contact pointers need no relocation work.
|
|
virtual void onContactReferenceChanged(const ContactInfo*, ContactInfo*) {}
|
|
virtual void onDiscoveredContact(ContactInfo& contact, bool is_new, uint8_t path_len, const uint8_t* path) = 0;
|
|
// Ownership does not require a surviving contact. Set peer to NULL when the
|
|
// expected ACK belongs to us but its recipient was removed or overwritten.
|
|
virtual bool processAck(const uint8_t *data, ContactInfo*& peer) = 0;
|
|
virtual void onContactPathUpdated(const ContactInfo& contact) = 0;
|
|
virtual bool onContactPathRecv(ContactInfo& from, uint8_t* in_path, uint8_t in_path_len, uint8_t* out_path, uint8_t out_path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len);
|
|
virtual void onMessageRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const char *text) = 0;
|
|
virtual void onCommandDataRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const char *text) = 0;
|
|
virtual void onCLICommandRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const char *text, char* reply) = 0;
|
|
// Return true only after retaining/delivering the room post. A false result
|
|
// leaves the catch-up cursor unchanged and suppresses the delivery ACK.
|
|
virtual bool onSignedMessageRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const uint8_t *sender_prefix, const char *text) = 0;
|
|
virtual uint32_t calcFloodTimeoutMillisFor(uint32_t pkt_airtime_millis) const = 0;
|
|
virtual uint32_t calcDirectTimeoutMillisFor(uint32_t pkt_airtime_millis, uint8_t path_len) const = 0;
|
|
virtual void onSendTimeout() = 0;
|
|
virtual void onChannelMessageRecv(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t timestamp, const char *text) = 0;
|
|
virtual void onChannelDataRecv(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint16_t data_type,
|
|
const uint8_t* data, size_t data_len) {}
|
|
virtual uint8_t onContactRequest(const ContactInfo& contact, uint32_t sender_timestamp, const uint8_t* data, uint8_t len, uint8_t* reply) = 0;
|
|
virtual void onContactResponse(const ContactInfo& contact, const uint8_t* data, uint8_t len) = 0;
|
|
// Reject a recently retired reflected tag before allocating/transmitting.
|
|
virtual bool allowRequestTag(uint32_t tag) { return true; }
|
|
virtual bool allocateRequestTag(uint32_t& tag);
|
|
virtual void handleReturnPathRetry(const ContactInfo& contact, const uint8_t* path, uint8_t path_len);
|
|
|
|
virtual bool sendFloodScoped(const ContactInfo& recipient, mesh::Packet* pkt, uint32_t delay_millis=0);
|
|
virtual bool sendFloodScoped(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t delay_millis=0);
|
|
|
|
// storage concepts, for sub-classes to override/implement
|
|
virtual int getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) { return 0; } // not implemented
|
|
virtual bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], int len) { return false; }
|
|
|
|
// Mesh overrides
|
|
uint8_t getContactTxRadio(const mesh::Identity& peer) const override;
|
|
void onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len) override;
|
|
int searchPeersByHash(const uint8_t* hash) override;
|
|
void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override;
|
|
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override;
|
|
bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;
|
|
void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override;
|
|
#ifdef MAX_GROUP_CHANNELS
|
|
int searchChannelsByHash(const uint8_t* hash, mesh::GroupChannel channels[], int max_matches) override;
|
|
#endif
|
|
void onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::GroupChannel& channel, uint8_t* data, size_t len) override;
|
|
|
|
// Connections
|
|
bool startConnection(const ContactInfo& contact, uint16_t keep_alive_secs);
|
|
void stopConnection(const uint8_t* pub_key);
|
|
bool hasConnectionTo(const uint8_t* pub_key);
|
|
void markConnectionActive(const ContactInfo& contact);
|
|
bool checkConnectionsAck(const uint8_t* data, ContactInfo*& peer);
|
|
void checkConnections();
|
|
|
|
public:
|
|
mesh::Packet* createSelfAdvert(const char* name);
|
|
mesh::Packet* createSelfAdvert(const char* name, double lat, double lon);
|
|
int sendMessage(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char* text,
|
|
uint32_t& expected_ack, uint32_t& est_timeout, uint8_t* packet_hash = NULL,
|
|
const uint8_t* replace_retry_key = NULL,
|
|
const uint8_t* message_retry_key = NULL,
|
|
uint32_t delay_millis = 0);
|
|
int sendCommandData(const ContactInfo& recipient, uint32_t timestamp,
|
|
uint8_t attempt, uint8_t txt_type, const char* text,
|
|
uint32_t& est_timeout,
|
|
uint32_t logical_request_id = 0);
|
|
bool sendGroupMessage(uint32_t timestamp, mesh::GroupChannel& channel, const char* sender_name, const char* text, int text_len);
|
|
bool sendGroupData(mesh::GroupChannel& channel, uint8_t* path, uint8_t path_len, uint16_t data_type, const uint8_t* data, int data_len);
|
|
int sendLogin(const ContactInfo& recipient, const char* password, uint32_t& est_timeout);
|
|
int sendAnonReq(const ContactInfo& recipient, const uint8_t* data, uint8_t len, uint32_t& tag, uint32_t& est_timeout);
|
|
int sendRequest(const ContactInfo& recipient, uint8_t req_type, uint32_t& tag, uint32_t& est_timeout);
|
|
int sendRequest(const ContactInfo& recipient, const uint8_t* req_data, uint8_t data_len, uint32_t& tag, uint32_t& est_timeout);
|
|
bool shareContactZeroHop(const ContactInfo& contact);
|
|
uint8_t exportContact(const ContactInfo& contact, uint8_t dest_buf[]);
|
|
bool importContact(const uint8_t src_buf[], uint8_t len);
|
|
void resetPathTo(ContactInfo& recipient);
|
|
void scanRecentContacts(int last_n, ContactVisitor* visitor);
|
|
ContactInfo* searchContactsByPrefix(const char* name_prefix);
|
|
ContactInfo* lookupContactByPubKey(const uint8_t* pub_key, int prefix_len);
|
|
ContactInfo* lookupTransientContactByPubKey(const uint8_t* pub_key, int prefix_len);
|
|
ContactInfo* lookupPersistentContactByPubKey(const uint8_t* pub_key, int prefix_len);
|
|
bool isTransientContact(const ContactInfo& contact) const;
|
|
// Supply a live same-identity replacement when promoting a transient peer.
|
|
bool clearTransientContact(ContactInfo& contact, ContactInfo* replacement = NULL);
|
|
bool removeContact(ContactInfo& contact);
|
|
bool addContact(const ContactInfo& contact);
|
|
int getTotalContactSlots() const { return num_contacts; }
|
|
int getNumContacts() const { return num_contacts - MAX_ANON_CONTACTS; } // don't include the reserved slots at start
|
|
uint32_t getContactTableRevision() const { return contact_table_revision; }
|
|
bool getContactByIdx(uint32_t idx, ContactInfo& contact);
|
|
ContactInfo* getContactPtrByIdx(uint32_t idx) {
|
|
return idx < (uint32_t)num_contacts ? &contacts[idx] : NULL;
|
|
}
|
|
ContactsIterator startContactsIterator();
|
|
ChannelDetails* addChannel(const char* name, const char* psk_base64);
|
|
bool getChannel(int idx, ChannelDetails& dest);
|
|
bool setChannel(int idx, const ChannelDetails& src);
|
|
int findChannelIdx(const mesh::GroupChannel& ch);
|
|
|
|
void loop();
|
|
};
|