* companion radio: new CMD_EXPORT_CONTACT, CMD_IMPORT_CONTACT

This commit is contained in:
Scott Powell 2025-02-15 15:57:02 +11:00
parent 65580c76d0
commit a2fa4caa3f
4 changed files with 64 additions and 12 deletions

View file

@ -108,6 +108,8 @@ static uint32_t _atoi(const char* sp) {
#define CMD_SET_ADVERT_LATLON 14
#define CMD_REMOVE_CONTACT 15
#define CMD_SHARE_CONTACT 16
#define CMD_EXPORT_CONTACT 17
#define CMD_IMPORT_CONTACT 18
#define RESP_CODE_OK 0
#define RESP_CODE_ERR 1
@ -120,6 +122,7 @@ static uint32_t _atoi(const char* sp) {
#define RESP_CODE_CHANNEL_MSG_RECV 8 // a reply to CMD_SYNC_NEXT_MESSAGE
#define RESP_CODE_CURR_TIME 9 // a reply to CMD_GET_DEVICE_TIME
#define RESP_CODE_NO_MORE_MESSAGES 10 // a reply to CMD_SYNC_NEXT_MESSAGE
#define RESP_CODE_EXPORT_CONTACT 11
// these are _pushed_ to client app at any time
#define PUSH_CODE_ADVERT 0x80
@ -712,6 +715,35 @@ public:
} else {
writeErrFrame(); // not found, or unable to send
}
} else if (cmd_frame[0] == CMD_EXPORT_CONTACT) {
if (len < 1 + PUB_KEY_SIZE) {
// export SELF
auto pkt = createSelfAdvert(_prefs.node_name, _prefs.node_lat, _prefs.node_lon);
if (pkt) {
out_frame[0] = RESP_CODE_EXPORT_CONTACT;
uint8_t out_len = pkt->writeTo(&out_frame[1]);
releasePacket(pkt); // undo the obtainNewPacket()
_serial->writeFrame(out_frame, out_len + 1);
} else {
writeErrFrame(); // Error
}
} else {
uint8_t* pub_key = &cmd_frame[1];
ContactInfo* recipient = lookupContactByPubKey(pub_key, PUB_KEY_SIZE);
uint8_t out_len;
if (recipient && (out_len = exportContact(*recipient, &out_frame[1])) > 0) {
out_frame[0] = RESP_CODE_EXPORT_CONTACT;
_serial->writeFrame(out_frame, out_len + 1);
} else {
writeErrFrame(); // not found
}
}
} else if (cmd_frame[0] == CMD_IMPORT_CONTACT && len > 2+32+64) {
if (importContact(&cmd_frame[1], len - 1)) {
writeOKFrame();
} else {
writeErrFrame();
}
} else if (cmd_frame[0] == CMD_SYNC_NEXT_MESSAGE) {
int out_len;
if ((out_len = getFromOfflineQueue(out_frame)) > 0) {
@ -764,6 +796,7 @@ public:
void loop() {
BaseChatMesh::loop();
size_t len = _serial->checkRecvFrame(cmd_frame);
if (len > 0) {
handleCmdFrame(len);

View file

@ -200,17 +200,8 @@ class MyMesh : public BaseChatMesh, ContactVisitor {
if (len % 2 == 0) {
len >>= 1; // halve, for num bytes
if (mesh::Utils::fromHex(tmp_buf, len, command)) {
auto pkt = obtainNewPacket();
if (pkt) {
if (pkt->readFrom(tmp_buf, len) && pkt->getPayloadType() == PAYLOAD_TYPE_ADVERT) {
pkt->header |= ROUTE_TYPE_FLOOD; // simulate it being received flood-mode
onRecvPacket(pkt); // loop-back, as if received over radio
releasePacket(pkt); // undo the obtainNewPacket()
return;
} else {
releasePacket(pkt); // undo the obtainNewPacket()
}
}
importContact(tmp_buf, len);
return;
}
}
}