Jelajahi Sumber

bootstrap RTC from contact.lastmod and improve slot overwrite logic

slot overwrite logic can now safely use contact.lastmod to find oldest contact for overwrite
taco 7 bulan lalu
induk
melakukan
df6687034a

+ 1 - 0
examples/companion_radio/MyMesh.cpp

@@ -862,6 +862,7 @@ void MyMesh::begin(bool has_display) {
 
   resetContacts();
   _store->loadContacts(this);
+  bootstrapRTCfromContacts();
   addChannel("Public", PUBLIC_GROUP_PSK); // pre-configure Andy's public channel
   _store->loadChannels(this);
 

+ 16 - 7
src/helpers/BaseChatMesh.cpp

@@ -55,17 +55,29 @@ void BaseChatMesh::sendAckTo(const ContactInfo& dest, uint32_t ack_hash) {
   }
 }
 
+void BaseChatMesh::bootstrapRTCfromContacts() {
+  uint32_t latest = 0;
+  for (int i = 0; i < num_contacts; i++) {
+    if (contacts[i].lastmod > latest) {
+      latest = contacts[i].lastmod;
+    }
+  }
+  if (latest != 0) {
+    getRTCClock()->setCurrentTime(latest + 1);
+  }
+}
+
 ContactInfo* BaseChatMesh::allocateContactSlot() {
   if (num_contacts < MAX_CONTACTS) {
     return &contacts[num_contacts++];
   } else if (shouldOverwriteWhenFull()) {
-    // Find oldest non-favourite contact by last_advert_timestamp
+    // Find oldest non-favourite contact by oldest lastmod timestamp
     int oldest_idx = -1;
-    uint32_t oldest_timestamp = 0xFFFFFFFF;
+    uint32_t oldest_lastmod = 0xFFFFFFFF;
     for (int i = 0; i < num_contacts; i++) {
       bool is_favourite = (contacts[i].flags & 0x01) != 0;
-      if (!is_favourite && contacts[i].last_advert_timestamp < oldest_timestamp) {
-        oldest_timestamp = contacts[i].last_advert_timestamp;
+      if (!is_favourite && contacts[i].lastmod < oldest_lastmod) {
+        oldest_lastmod = contacts[i].lastmod;
         oldest_idx = i;
       }
     }
@@ -750,9 +762,6 @@ bool BaseChatMesh::addContact(const ContactInfo& contact) {
   ContactInfo* dest = allocateContactSlot();
   if (dest) {
     *dest = contact;
-    if (dest->last_advert_timestamp == 0) {  // ensure non-zero timestamp to prevent contacts added from discover list being considered 'oldest'
-      dest->last_advert_timestamp = getRTCClock()->getCurrentTimeUnique();
-    }
     dest->shared_secret_valid = false; // mark shared_secret as needing calculation
     return true;  // success
   }

+ 1 - 0
src/helpers/BaseChatMesh.h

@@ -88,6 +88,7 @@ protected:
     memset(connections, 0, sizeof(connections));
   }
 
+  void bootstrapRTCfromContacts();
   void resetContacts() { num_contacts = 0; }
   void populateContactFromAdvert(ContactInfo& ci, const mesh::Identity& id, const AdvertDataParser& parser, uint32_t timestamp);
   ContactInfo* allocateContactSlot(); // helper to find slot for new contact