From f46263a263c9fcdc5fe81cf920da803bcc133ef8 Mon Sep 17 00:00:00 2001 From: Scott Powell Date: Wed, 29 Jan 2025 13:56:39 +1100 Subject: [PATCH] * ContactInfo, added gps_lat, gps_lon * companion_radio: now also save gps_lat, gps_lon * BaseChatMesh: now parses gps_lat, gps_lon out of Advertisement --- examples/companion_radio/main.cpp | 24 +++++++++++++++++------- examples/simple_secure_chat/main.cpp | 1 + src/helpers/BaseChatMesh.cpp | 7 +++++++ src/helpers/BaseChatMesh.h | 1 + 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index 573cb0ea..821b2e52 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -123,8 +123,8 @@ class MyMesh : public BaseChatMesh { uint8_t out_frame[MAX_FRAME_SIZE+1]; void loadContacts() { - if (_fs->exists("/contacts2")) { - File file = _fs->open("/contacts2"); + if (_fs->exists("/contacts3")) { + File file = _fs->open("/contacts3"); if (file) { bool full = false; while (!full) { @@ -143,6 +143,8 @@ class MyMesh : public BaseChatMesh { success = success && (file.read((uint8_t *) &c.last_advert_timestamp, 4) == 4); success = success && (file.read(c.out_path, 64) == 64); success = success && (file.read((uint8_t *) c.lastmod, 4) == 4); + success = success && (file.read((uint8_t *) c.gps_lat, 4) == 4); + success = success && (file.read((uint8_t *) c.gps_lon, 4) == 4); if (!success) break; // EOF @@ -156,10 +158,10 @@ class MyMesh : public BaseChatMesh { void saveContacts() { #if defined(NRF52_PLATFORM) - File file = _fs->open("/contacts2", FILE_O_WRITE); + File file = _fs->open("/contacts3", FILE_O_WRITE); if (file) { file.seek(0); file.truncate(); } #else - File file = _fs->open("/contacts2", "w", true); + File file = _fs->open("/contacts3", "w", true); #endif if (file) { ContactsIterator iter; @@ -178,6 +180,8 @@ class MyMesh : public BaseChatMesh { success = success && (file.write((uint8_t *) &c.last_advert_timestamp, 4) == 4); success = success && (file.write(c.out_path, 64) == 64); success = success && (file.write((uint8_t *) &c.lastmod, 4) == 4); + success = success && (file.write((uint8_t *) &c.gps_lat, 4) == 4); + success = success && (file.write((uint8_t *) &c.gps_lon, 4) == 4); if (!success) break; // write failed } @@ -206,11 +210,13 @@ class MyMesh : public BaseChatMesh { memcpy(&out_frame[i], contact.out_path, MAX_PATH_SIZE); i += MAX_PATH_SIZE; memcpy(&out_frame[i], contact.name, 32); i += 32; memcpy(&out_frame[i], &contact.last_advert_timestamp, 4); i += 4; + memcpy(&out_frame[i], &contact.gps_lat, 4); i += 4; + memcpy(&out_frame[i], &contact.gps_lon, 4); i += 4; memcpy(&out_frame[i], &contact.lastmod, 4); i += 4; _serial->writeFrame(out_frame, i); } - void updateContactFromFrame(ContactInfo& contact, const uint8_t* frame) { + void updateContactFromFrame(ContactInfo& contact, const uint8_t* frame, int len) { int i = 0; uint8_t code = frame[i++]; // eg. CMD_ADD_UPDATE_CONTACT memcpy(contact.id.pub_key, &frame[i], PUB_KEY_SIZE); i += PUB_KEY_SIZE; @@ -220,6 +226,10 @@ class MyMesh : public BaseChatMesh { memcpy(contact.out_path, &frame[i], MAX_PATH_SIZE); i += MAX_PATH_SIZE; memcpy(contact.name, &frame[i], 32); i += 32; memcpy(&contact.last_advert_timestamp, &frame[i], 4); i += 4; + if (i + 8 >= len) { // optional fields + memcpy(&contact.gps_lat, &frame[i], 4); i += 4; + memcpy(&contact.gps_lon, &frame[i], 4); i += 4; + } } void addToOfflineQueue(const uint8_t frame[], int len) { @@ -482,13 +492,13 @@ public: uint8_t* pub_key = &cmd_frame[1]; ContactInfo* recipient = lookupContactByPubKey(pub_key, PUB_KEY_SIZE); if (recipient) { - updateContactFromFrame(*recipient, cmd_frame); + updateContactFromFrame(*recipient, cmd_frame, len); //recipient->lastmod = ?? shouldn't be needed, app already has this version of contact saveContacts(); writeOKFrame(); } else { ContactInfo contact; - updateContactFromFrame(contact, cmd_frame); + updateContactFromFrame(contact, cmd_frame, len); contact.lastmod = getRTCClock()->getCurrentTime(); if (addContact(contact)) { saveContacts(); diff --git a/examples/simple_secure_chat/main.cpp b/examples/simple_secure_chat/main.cpp index 8df6183a..abe564ed 100644 --- a/examples/simple_secure_chat/main.cpp +++ b/examples/simple_secure_chat/main.cpp @@ -115,6 +115,7 @@ class MyMesh : public BaseChatMesh, ContactVisitor { success = success && (file.read((uint8_t *) &c.out_path_len, 1) == 1); success = success && (file.read((uint8_t *) &c.last_advert_timestamp, 4) == 4); success = success && (file.read(c.out_path, 64) == 64); + c.gps_lat = c.gps_lon = 0; // not yet supported if (!success) break; // EOF diff --git a/src/helpers/BaseChatMesh.cpp b/src/helpers/BaseChatMesh.cpp index a3b34cd1..9b231a1e 100644 --- a/src/helpers/BaseChatMesh.cpp +++ b/src/helpers/BaseChatMesh.cpp @@ -38,6 +38,9 @@ void BaseChatMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, from = &contacts[num_contacts++]; from->id = id; from->out_path_len = -1; // initially out_path is unknown + from->gps_lat = 0; // initially unknown GPS loc + from->gps_lon = 0; + // only need to calculate the shared_secret once, for better performance self_id.calcSharedSecret(from->shared_secret, id); } else { @@ -50,6 +53,10 @@ void BaseChatMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, strncpy(from->name, parser.getName(), sizeof(from->name)-1); from->name[sizeof(from->name)-1] = 0; from->type = parser.getType(); + if (parser.hasLatLon()) { + from->gps_lat = parser.getIntLat(); + from->gps_lon = parser.getIntLon(); + } from->last_advert_timestamp = timestamp; from->lastmod = getRTCClock()->getCurrentTime(); diff --git a/src/helpers/BaseChatMesh.h b/src/helpers/BaseChatMesh.h index 366cb2bd..9a7b2627 100644 --- a/src/helpers/BaseChatMesh.h +++ b/src/helpers/BaseChatMesh.h @@ -17,6 +17,7 @@ struct ContactInfo { uint32_t last_advert_timestamp; // by THEIR clock uint8_t shared_secret[PUB_KEY_SIZE]; uint32_t lastmod; // by OUR clock + int32_t gps_lat, gps_lon; // 6 dec places }; #define MAX_SEARCH_RESULTS 8