* companion: saveContacts() now deferred for 5 secs (lazy writes)

This commit is contained in:
Scott Powell 2025-05-27 18:45:06 +10:00
parent 0e90b73110
commit b3fc6bedf9

View file

@ -57,6 +57,7 @@
#define FLOOD_SEND_TIMEOUT_FACTOR 16.0f #define FLOOD_SEND_TIMEOUT_FACTOR 16.0f
#define DIRECT_SEND_PERHOP_FACTOR 6.0f #define DIRECT_SEND_PERHOP_FACTOR 6.0f
#define DIRECT_SEND_PERHOP_EXTRA_MILLIS 250 #define DIRECT_SEND_PERHOP_EXTRA_MILLIS 250
#define LAZY_CONTACTS_WRITE_DELAY 5000
#define PUBLIC_GROUP_PSK "izOH6cXN6mrJ5e26oRXNcg==" #define PUBLIC_GROUP_PSK "izOH6cXN6mrJ5e26oRXNcg=="
@ -198,6 +199,7 @@ class MyMesh : public BaseChatMesh {
uint8_t app_target_ver; uint8_t app_target_ver;
uint8_t* sign_data; uint8_t* sign_data;
uint32_t sign_data_len; uint32_t sign_data_len;
unsigned long dirty_contacts_expiry;
uint8_t cmd_frame[MAX_FRAME_SIZE+1]; uint8_t cmd_frame[MAX_FRAME_SIZE+1];
uint8_t out_frame[MAX_FRAME_SIZE+1]; uint8_t out_frame[MAX_FRAME_SIZE+1];
CayenneLPP telemetry; CayenneLPP telemetry;
@ -524,7 +526,7 @@ protected:
#endif #endif
} }
saveContacts(); dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
} }
void onContactPathUpdated(const ContactInfo& contact) override { void onContactPathUpdated(const ContactInfo& contact) override {
@ -532,7 +534,7 @@ protected:
memcpy(&out_frame[1], contact.id.pub_key, PUB_KEY_SIZE); memcpy(&out_frame[1], contact.id.pub_key, PUB_KEY_SIZE);
_serial->writeFrame(out_frame, 1 + PUB_KEY_SIZE); // NOTE: app may not be connected _serial->writeFrame(out_frame, 1 + PUB_KEY_SIZE); // NOTE: app may not be connected
saveContacts(); dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
} }
bool processAck(const uint8_t *data) override { bool processAck(const uint8_t *data) override {
@ -603,7 +605,8 @@ protected:
void onSignedMessageRecv(const ContactInfo& from, mesh::Packet* pkt, uint32_t sender_timestamp, const uint8_t *sender_prefix, const char *text) override { void onSignedMessageRecv(const ContactInfo& from, mesh::Packet* pkt, uint32_t sender_timestamp, const uint8_t *sender_prefix, const char *text) override {
markConnectionActive(from); markConnectionActive(from);
saveContacts(); // from.sync_since change needs to be persisted // from.sync_since change needs to be persisted
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
queueMessage(from, TXT_TYPE_SIGNED_PLAIN, pkt, sender_timestamp, sender_prefix, 4, text); queueMessage(from, TXT_TYPE_SIGNED_PLAIN, pkt, sender_timestamp, sender_prefix, 4, text);
} }
@ -797,6 +800,7 @@ public:
pending_login = pending_status = pending_telemetry = 0; pending_login = pending_status = pending_telemetry = 0;
next_ack_idx = 0; next_ack_idx = 0;
sign_data = NULL; sign_data = NULL;
dirty_contacts_expiry = 0;
// defaults // defaults
memset(&_prefs, 0, sizeof(_prefs)); memset(&_prefs, 0, sizeof(_prefs));
@ -1148,7 +1152,7 @@ public:
if (recipient) { if (recipient) {
recipient->out_path_len = -1; recipient->out_path_len = -1;
//recipient->lastmod = ?? shouldn't be needed, app already has this version of contact //recipient->lastmod = ?? shouldn't be needed, app already has this version of contact
saveContacts(); dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
writeOKFrame(); writeOKFrame();
} else { } else {
writeErrFrame(ERR_CODE_NOT_FOUND); // unknown contact writeErrFrame(ERR_CODE_NOT_FOUND); // unknown contact
@ -1159,7 +1163,7 @@ public:
if (recipient) { if (recipient) {
updateContactFromFrame(*recipient, cmd_frame, len); updateContactFromFrame(*recipient, cmd_frame, len);
//recipient->lastmod = ?? shouldn't be needed, app already has this version of contact //recipient->lastmod = ?? shouldn't be needed, app already has this version of contact
saveContacts(); dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
writeOKFrame(); writeOKFrame();
} else { } else {
ContactInfo contact; ContactInfo contact;
@ -1167,7 +1171,7 @@ public:
contact.lastmod = getRTCClock()->getCurrentTime(); contact.lastmod = getRTCClock()->getCurrentTime();
contact.sync_since = 0; contact.sync_since = 0;
if (addContact(contact)) { if (addContact(contact)) {
saveContacts(); dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
writeOKFrame(); writeOKFrame();
} else { } else {
writeErrFrame(ERR_CODE_TABLE_FULL); writeErrFrame(ERR_CODE_TABLE_FULL);
@ -1177,7 +1181,7 @@ public:
uint8_t* pub_key = &cmd_frame[1]; uint8_t* pub_key = &cmd_frame[1];
ContactInfo* recipient = lookupContactByPubKey(pub_key, PUB_KEY_SIZE); ContactInfo* recipient = lookupContactByPubKey(pub_key, PUB_KEY_SIZE);
if (recipient && removeContact(*recipient)) { if (recipient && removeContact(*recipient)) {
saveContacts(); dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
writeOKFrame(); writeOKFrame();
} else { } else {
writeErrFrame(ERR_CODE_NOT_FOUND); // not found, or unable to remove writeErrFrame(ERR_CODE_NOT_FOUND); // not found, or unable to remove
@ -1296,6 +1300,9 @@ public:
savePrefs(); savePrefs();
writeOKFrame(); writeOKFrame();
} else if (cmd_frame[0] == CMD_REBOOT && memcmp(&cmd_frame[1], "reboot", 6) == 0) { } else if (cmd_frame[0] == CMD_REBOOT && memcmp(&cmd_frame[1], "reboot", 6) == 0) {
if (dirty_contacts_expiry) { // is there are pending dirty contacts write needed?
saveContacts();
}
board.reboot(); board.reboot();
} else if (cmd_frame[0] == CMD_GET_BATTERY_VOLTAGE) { } else if (cmd_frame[0] == CMD_GET_BATTERY_VOLTAGE) {
uint8_t reply[3]; uint8_t reply[3];
@ -1566,6 +1573,12 @@ public:
checkConnections(); checkConnections();
} }
// is there are pending dirty contacts write needed?
if (dirty_contacts_expiry && millisHasNowPassed(dirty_contacts_expiry)) {
saveContacts();
dirty_contacts_expiry = 0;
}
#ifdef DISPLAY_CLASS #ifdef DISPLAY_CLASS
ui_task.setHasConnection(_serial->isConnected()); ui_task.setHasConnection(_serial->isConnected());
ui_task.loop(); ui_task.loop();