From 33bee48d3898b4e48cbd7dadec7f44ecb4017a57 Mon Sep 17 00:00:00 2001 From: Scott Powell Date: Sat, 1 Feb 2025 21:45:34 +1100 Subject: [PATCH] * Terminal Chat: new commands "card" and "import" --- examples/simple_secure_chat/main.cpp | 45 ++++++++++++++++++++++++++-- src/Dispatcher.cpp | 7 +++-- src/Mesh.cpp | 2 ++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/examples/simple_secure_chat/main.cpp b/examples/simple_secure_chat/main.cpp index 89e1e0e7..375c889d 100644 --- a/examples/simple_secure_chat/main.cpp +++ b/examples/simple_secure_chat/main.cpp @@ -86,7 +86,9 @@ class MyMesh : public BaseChatMesh, ContactVisitor { mesh::GroupChannel* _public; unsigned long last_msg_sent; ContactInfo* curr_recipient; - char command[MAX_TEXT_LEN+1]; + char command[512+10]; + uint8_t tmp_buf[256]; + char hex_buf[512]; const char* getTypeName(uint8_t type) const { if (type == ADV_TYPE_CHAT) return "Chat"; @@ -168,6 +170,29 @@ class MyMesh : public BaseChatMesh, ContactVisitor { } } + void importCard(const char* command) { + while (*command == ' ') command++; // skip leading spaces + if (memcmp(command, "meshcore://", 11) == 0) { + command += 11; // skip the prefix + int len = strlen(command); + 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; + } + } + } + } + } + Serial.println(" error: invalid format"); + } + protected: void onDiscoveredContact(ContactInfo& contact, bool is_new) override { // TODO: if not in favs, prompt to add as fav(?) @@ -351,9 +376,25 @@ public: saveContacts(); Serial.println(" Done."); } + } else if (memcmp(command, "card", 4) == 0) { + Serial.printf("Hello %s\n", self_name); + auto pkt = createSelfAdvert(self_name); + if (pkt) { + uint8_t len = pkt->writeTo(tmp_buf); + mesh::Utils::toHex(hex_buf, tmp_buf, len); + Serial.println("Your MeshCore biz card:"); + Serial.print("meshcore://"); Serial.println(hex_buf); + Serial.println(); + } else { + Serial.println(" Error"); + } + } else if (memcmp(command, "import ", 7) == 0) { + importCard(&command[7]); } else if (memcmp(command, "help", 4) == 0) { - Serial.printf("Hello %s, Commands:\n", self_name); + Serial.println("Commands:"); Serial.println(" name "); + Serial.println(" card"); + Serial.println(" import {biz card}"); Serial.println(" clock"); Serial.println(" time "); Serial.println(" list {n}"); diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index d1fdcfbc..ab3fa356 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -157,8 +157,11 @@ void Dispatcher::checkSend() { Packet* Dispatcher::obtainNewPacket() { auto pkt = _mgr->allocNew(); // TODO: zero out all fields - if (pkt == NULL) n_full_events++; - + if (pkt == NULL) { + n_full_events++; + } else { + pkt->payload_len = pkt->path_len = 0; + } return pkt; } diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 35af0f54..96092f30 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -185,6 +185,8 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) { if (i > pkt->payload_len) { MESH_DEBUG_PRINTLN("Mesh::onRecvPacket(): incomplete advertisement packet"); + } else if (self_id.matches(id.pub_key)) { + MESH_DEBUG_PRINTLN("Mesh::onRecvPacket(): receiving SELF advert packet"); } else if (!_tables->hasSeen(pkt)) { uint8_t* app_data = &pkt->payload[i]; int app_data_len = pkt->payload_len - i;