| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- #include <Arduino.h> // needed for PlatformIO
- #include <Mesh.h>
- #include <SPIFFS.h>
- #define RADIOLIB_STATIC_ONLY 1
- #include <RadioLib.h>
- #include <helpers/RadioLibWrappers.h>
- #include <helpers/ArduinoHelpers.h>
- #include <helpers/StaticPoolPacketManager.h>
- #include <helpers/SimpleSeenTable.h>
- /* ---------------------------------- CONFIGURATION ------------------------------------- */
- //#define RUN_AS_ALICE true
- #if RUN_AS_ALICE
- const char* alice_private = "B8830658388B2DDF22C3A508F4386975970CDE1E2A2A495C8F3B5727957A97629255A1392F8BA4C26A023A0DAB78BFC64D261C8E51507496DD39AFE3707E7B42";
- #else
- const char *bob_private = "30BAA23CCB825D8020A59C936D0AB7773B07356020360FC77192813640BAD375E43BBF9A9A7537E4B9614610F1F2EF874AAB390BA9B0C2F01006B01FDDFEFF0C";
- #endif
- const char *alice_public = "106A5136EC0DD797650AD204C065CF9B66095F6ED772B0822187785D65E11B1F";
- const char *bob_public = "020BCEDAC07D709BD8507EC316EB5A7FF2F0939AF5057353DCE7E4436A1B9681";
- #ifdef HELTEC_LORA_V3
- #include <helpers/HeltecV3Board.h>
- static HeltecV3Board board;
- #else
- #error "need to provide a 'board' object"
- #endif
- #define FLOOD_SEND_TIMEOUT_MILLIS 4000
- #define DIRECT_SEND_TIMEOUT_MILLIS 2000
- /* -------------------------------------------------------------------------------------- */
- static unsigned long txt_send_timeout;
- #define MAX_CONTACTS 1
- #define MAX_SEARCH_RESULTS 1
- #define MAX_TEXT_LEN (10*CIPHER_BLOCK_SIZE) // must be LESS than (MAX_PACKET_PAYLOAD - 4 - CIPHER_MAC_SIZE - 1)
- struct ContactInfo {
- mesh::Identity id;
- const char* name;
- int out_path_len;
- uint8_t out_path[MAX_PATH_SIZE];
- uint32_t last_advert_timestamp;
- uint8_t shared_secret[PUB_KEY_SIZE];
- };
- class MyMesh : public mesh::Mesh {
- public:
- SimpleSeenTable* _table;
- mesh::LocalIdentity self_id;
- ContactInfo contacts[MAX_CONTACTS];
- int num_contacts;
- void addContact(const char* name, const mesh::Identity& id) {
- if (num_contacts < MAX_CONTACTS) {
- contacts[num_contacts].id = id;
- contacts[num_contacts].name = name;
- contacts[num_contacts].last_advert_timestamp = 0;
- contacts[num_contacts].out_path_len = -1;
- // only need to calculate the shared_secret once, for better performance
- self_id.calcSharedSecret(contacts[num_contacts].shared_secret, id);
- num_contacts++;
- }
- }
- protected:
- int matching_peer_indexes[MAX_SEARCH_RESULTS];
- int searchPeersByHash(const uint8_t* hash) override {
- int n = 0;
- for (int i = 0; i < num_contacts && n < MAX_SEARCH_RESULTS; i++) {
- if (contacts[i].id.isHashMatch(hash)) {
- matching_peer_indexes[n++] = i; // store the INDEXES of matching contacts (for subsequent 'peer' methods)
- }
- }
- return n;
- }
- void onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len) override {
- Serial.print("Valid Advertisement -> ");
- mesh::Utils::printHex(Serial, id.pub_key, PUB_KEY_SIZE);
- Serial.println();
- for (int i = 0; i < num_contacts; i++) {
- ContactInfo& from = contacts[i];
- // check for replay attacks
- if (id.matches(from.id) && timestamp > from.last_advert_timestamp) { // is from one of our contacts
- from.last_advert_timestamp = timestamp;
- Serial.printf(" From contact: %s\n", from.name);
- }
- }
- }
- void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override {
- int i = matching_peer_indexes[peer_idx];
- if (i >= 0 && i < num_contacts) {
- // lookup pre-calculated shared_secret
- memcpy(dest_secret, contacts[i].shared_secret, PUB_KEY_SIZE);
- } else {
- MESH_DEBUG_PRINTLN("getPeerSHharedSecret: Invalid peer idx: %d", i);
- }
- }
- void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, uint8_t* data, size_t len) override {
- if (type == PAYLOAD_TYPE_TXT_MSG) {
- if (_table->hasSeenPacket(packet)) return;
- int i = matching_peer_indexes[sender_idx];
- if (i < 0 && i >= num_contacts) {
- MESH_DEBUG_PRINTLN("onPeerDataRecv: Invalid sender idx: %d", i);
- return;
- }
- ContactInfo& from = contacts[i];
- uint32_t timestamp;
- memcpy(×tamp, data, 4); // timestamp (by sender's RTC clock - which could be wrong)
- // len can be > original length, but 'text' will be padded with zeroes
- data[len] = 0; // need to make a C string again, with null terminator
- Serial.print("MSG -> from ");
- Serial.print(from.name);
- Serial.print(": ");
- Serial.println((const char *) &data[4]);
- uint32_t ack_hash; // calc truncated hash of the message timestamp + text + sender pub_key, to prove to sender that we got it
- mesh::Utils::sha256((uint8_t *) &ack_hash, 4, data, len, from.id.pub_key, PUB_KEY_SIZE);
- if (packet->isRouteFlood()) {
- // let this sender know path TO here, so they can use sendDirect(), and ALSO encode the ACK
- mesh::Packet* path = createPathReturn(from.id, from.shared_secret, packet->path, packet->path_len,
- PAYLOAD_TYPE_ACK, (uint8_t *) &ack_hash, 4);
- if (path) sendFlood(path);
- } else {
- mesh::Packet* ack = createAck(ack_hash);
- if (ack) {
- if (from.out_path_len < 0) {
- sendFlood(ack);
- } else {
- sendDirect(ack, from.out_path, from.out_path_len);
- }
- }
- }
- }
- }
- void onPeerPathRecv(mesh::Packet* packet, int sender_idx, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override {
- if (_table->hasSeenPacket(packet)) return;
- int i = matching_peer_indexes[sender_idx];
- if (i < 0 && i >= num_contacts) {
- MESH_DEBUG_PRINTLN("onPeerPathRecv: Invalid sender idx: %d", i);
- return;
- }
- ContactInfo& from = contacts[i];
- Serial.printf("PATH to: %s, path_len=%d\n", from.name, (uint32_t) path_len);
- memcpy(from.out_path, path, from.out_path_len = path_len); // store a copy of path, for sendDirect()
- if (packet->isRouteFlood()) {
- // send a reciprocal return path to sender, but send DIRECTLY!
- mesh::Packet* rpath = createPathReturn(from.id, from.shared_secret, packet->path, packet->path_len, 0, NULL, 0);
- if (rpath) sendDirect(rpath, path, path_len);
- }
- if (extra_type == PAYLOAD_TYPE_ACK && extra_len >= 4) {
- // also got an encoded ACK!
- processAck(extra);
- }
- }
- void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override {
- processAck((uint8_t *)&ack_crc);
- }
- void processAck(const uint8_t *data) {
- if (memcmp(data, &expected_ack_crc, 4) == 0) { // got an ACK from recipient
- Serial.println("Got ACK!");
- // NOTE: the same ACK can be received multiple times!
- expected_ack_crc = 0; // reset our expected hash, now that we have received ACK
- txt_send_timeout = 0;
- }
- }
- public:
- uint32_t expected_ack_crc;
- MyMesh(mesh::Radio& radio, mesh::RNG& rng, mesh::RTCClock& rtc, SimpleSeenTable& table)
- : mesh::Mesh(radio, *new ArduinoMillis(), rng, rtc, *new StaticPoolPacketManager(16)), _table(&table)
- {
- num_contacts = 0;
- }
- mesh::Packet* composeMsgPacket(ContactInfo& recipient, const char *text) {
- int text_len = strlen(text);
- if (text_len > MAX_TEXT_LEN) return NULL;
- uint8_t temp[4+MAX_TEXT_LEN+1];
- uint32_t timestamp = getRTCClock()->getCurrentTime();
- memcpy(temp, ×tamp, 4); // mostly an extra blob to help make packet_hash unique
- memcpy(&temp[4], text, text_len);
- // calc expected ACK reply
- mesh::Utils::sha256((uint8_t *)&expected_ack_crc, 4, (const uint8_t *) temp, 4 + text_len, self_id.pub_key, PUB_KEY_SIZE);
- return createDatagram(PAYLOAD_TYPE_TXT_MSG, recipient.id, recipient.shared_secret, temp, 4 + text_len);
- }
- void sendSelfAnnounce() {
- mesh::Packet* announce = createAdvert(self_id);
- if (announce) {
- sendFlood(announce);
- Serial.println(" (advert sent).");
- } else {
- Serial.println(" ERROR: unable to create packet.");
- }
- }
- };
- SPIClass spi;
- StdRNG fast_rng;
- SimpleSeenTable table;
- SX1262 radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, spi);
- MyMesh the_mesh(*new RadioLibWrapper(radio, board), fast_rng, *new VolatileRTCClock(), table);
- void halt() {
- while (1) ;
- }
- static char command[MAX_TEXT_LEN+1];
- void setup() {
- Serial.begin(115200);
- board.begin();
- spi.begin(P_LORA_SCLK, P_LORA_MISO, P_LORA_MOSI);
- int status = radio.begin(915.0, 250, 9, 5, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, 22);
- if (status != RADIOLIB_ERR_NONE) {
- Serial.print("ERROR: radio init failed: ");
- Serial.println(status);
- halt();
- }
- fast_rng.begin(radio.random(0x7FFFFFFF));
- #if RUN_AS_ALICE
- Serial.println(" --- user: Alice ---");
- the_mesh.self_id = mesh::LocalIdentity(alice_private, alice_public);
- the_mesh.addContact("Bob", mesh::Identity(bob_public));
- #else
- Serial.println(" --- user: Bob ---");
- the_mesh.self_id = mesh::LocalIdentity(bob_private, bob_public);
- the_mesh.addContact("Alice", mesh::Identity(alice_public));
- #endif
- Serial.println("Help:");
- Serial.println(" enter 'ann' to announce presence to mesh");
- Serial.println(" enter 'send {message text}' to send a message");
- the_mesh.begin();
- command[0] = 0;
- txt_send_timeout = 0;
- // send out initial Announce to the mesh
- the_mesh.sendSelfAnnounce();
- }
- void loop() {
- int len = strlen(command);
- while (Serial.available() && len < sizeof(command)-1) {
- char c = Serial.read();
- if (c != '\n') {
- command[len++] = c;
- command[len] = 0;
- }
- Serial.print(c);
- }
- if (len == sizeof(command)-1) { // command buffer full
- command[sizeof(command)-1] = '\r';
- }
- if (len > 0 && command[len - 1] == '\r') { // received complete line
- command[len - 1] = 0; // replace newline with C string null terminator
- if (memcmp(command, "send ", 5) == 0) {
- // TODO: some way to select recipient??
- ContactInfo& recipient = the_mesh.contacts[0]; // just send to first contact for now
- const char *text = &command[5];
- mesh::Packet* pkt = the_mesh.composeMsgPacket(recipient, text);
- if (pkt) {
- if (recipient.out_path_len < 0) {
- the_mesh.sendFlood(pkt);
- txt_send_timeout = the_mesh.futureMillis(FLOOD_SEND_TIMEOUT_MILLIS);
- } else {
- the_mesh.sendDirect(pkt, recipient.out_path, recipient.out_path_len);
- txt_send_timeout = the_mesh.futureMillis(DIRECT_SEND_TIMEOUT_MILLIS);
- }
- Serial.println(" (message sent)");
- } else {
- Serial.println(" ERROR: unable to create packet.");
- }
- } else if (strcmp(command, "ann") == 0) {
- the_mesh.sendSelfAnnounce();
- } else if (strcmp(command, "key") == 0) {
- mesh::LocalIdentity new_id(the_mesh.getRNG());
- new_id.printTo(Serial);
- } else {
- Serial.print(" ERROR: unknown command: "); Serial.println(command);
- }
- command[0] = 0; // reset command buffer
- }
- if (txt_send_timeout && the_mesh.millisHasNowPassed(txt_send_timeout)) {
- // failed to get an ACK
- ContactInfo& recipient = the_mesh.contacts[0]; // just the one contact for now
- Serial.println(" ERROR: timed out, no ACK.");
- // path to our contact is now possibly broken, fallback to Flood mode
- recipient.out_path_len = -1;
- txt_send_timeout = 0;
- }
- the_mesh.loop();
- }
|