* 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
This commit is contained in:
Scott Powell
2025-01-29 13:56:39 +11:00
parent 2e3d2d13b2
commit f46263a263
4 changed files with 26 additions and 7 deletions

View File

@@ -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();

View File

@@ -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

View File

@@ -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();

View File

@@ -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