main.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <Arduino.h> // needed for PlatformIO
  2. #include <Mesh.h>
  3. #include <SPIFFS.h>
  4. #define RADIOLIB_STATIC_ONLY 1
  5. #include <RadioLib.h>
  6. #include <helpers/RadioLibWrappers.h>
  7. #include <helpers/ArduinoHelpers.h>
  8. #include <helpers/StaticPoolPacketManager.h>
  9. #include <helpers/SimpleMeshTables.h>
  10. /* ------------------------------ Config -------------------------------- */
  11. #ifndef LORA_FREQ
  12. #define LORA_FREQ 915.0
  13. #endif
  14. #ifndef LORA_BW
  15. #define LORA_BW 125
  16. #endif
  17. #ifndef LORA_SF
  18. #define LORA_SF 9
  19. #endif
  20. #ifndef LORA_CR
  21. #define LORA_CR 5
  22. #endif
  23. #ifdef HELTEC_LORA_V3
  24. #include <helpers/HeltecV3Board.h>
  25. static HeltecV3Board board;
  26. #else
  27. #error "need to provide a 'board' object"
  28. #endif
  29. /* ------------------------------ Code -------------------------------- */
  30. class MyMesh : public mesh::Mesh {
  31. uint32_t last_advert_timestamp = 0;
  32. mesh::Identity server_id;
  33. uint8_t server_secret[PUB_KEY_SIZE];
  34. int server_path_len = -1;
  35. uint8_t server_path[MAX_PATH_SIZE];
  36. bool got_adv = false;
  37. protected:
  38. void onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len) override {
  39. if (memcmp(app_data, "PING", 4) == 0) {
  40. Serial.println("Received advertisement from a PING server");
  41. // check for replay attacks
  42. if (timestamp > last_advert_timestamp) {
  43. last_advert_timestamp = timestamp;
  44. server_id = id;
  45. self_id.calcSharedSecret(server_secret, id); // calc ECDH shared secret
  46. got_adv = true;
  47. }
  48. }
  49. }
  50. int searchPeersByHash(const uint8_t* hash) override {
  51. if (got_adv && server_id.isHashMatch(hash)) {
  52. return 1;
  53. }
  54. return 0; // not found
  55. }
  56. void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override {
  57. // lookup pre-calculated shared_secret
  58. memcpy(dest_secret, server_secret, PUB_KEY_SIZE);
  59. }
  60. void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override {
  61. if (type == PAYLOAD_TYPE_RESPONSE) {
  62. Serial.println("Received PING Reply!");
  63. if (packet->isRouteFlood()) {
  64. // let server know path TO here, so they can use sendDirect() for future ping responses
  65. mesh::Packet* path = createPathReturn(server_id, secret, packet->path, packet->path_len, 0, NULL, 0);
  66. if (path) sendFlood(path);
  67. }
  68. }
  69. }
  70. void onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override {
  71. // must be from server_id
  72. Serial.printf("PATH to server, path_len=%d\n", (uint32_t) path_len);
  73. memcpy(server_path, path, server_path_len = path_len); // store a copy of path, for sendDirect()
  74. if (packet->isRouteFlood()) {
  75. // send a reciprocal return path to sender, but send DIRECTLY!
  76. mesh::Packet* rpath = createPathReturn(server_id, secret, packet->path, packet->path_len, 0, NULL, 0);
  77. if (rpath) sendDirect(rpath, path, path_len);
  78. }
  79. if (extra_type == PAYLOAD_TYPE_RESPONSE) {
  80. Serial.println("Received PING Reply!");
  81. }
  82. }
  83. public:
  84. MyMesh(mesh::Radio& radio, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables)
  85. : mesh::Mesh(radio, *new ArduinoMillis(), rng, rtc, *new StaticPoolPacketManager(16), tables)
  86. {
  87. }
  88. void sendPingPacket() {
  89. if (!got_adv) return; // have not received Advert yet
  90. uint32_t now = getRTCClock()->getCurrentTime(); // important, need timestamp in packet, so that packet_hash will be unique
  91. mesh::Packet* ping = createAnonDatagram(PAYLOAD_TYPE_ANON_REQ, self_id, server_id, server_secret, (uint8_t *) &now, sizeof(now));
  92. if (ping) {
  93. if (server_path_len < 0) {
  94. sendFlood(ping);
  95. } else {
  96. sendDirect(ping, server_path, server_path_len);
  97. }
  98. }
  99. }
  100. };
  101. SPIClass spi;
  102. StdRNG fast_rng;
  103. SimpleMeshTables tables;
  104. SX1262 radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, spi);
  105. MyMesh the_mesh(*new RadioLibWrapper(radio, board), fast_rng, *new VolatileRTCClock(), tables);
  106. unsigned long nextPing;
  107. void halt() {
  108. while (1) ;
  109. }
  110. void setup() {
  111. Serial.begin(115200);
  112. board.begin();
  113. #if defined(P_LORA_SCLK)
  114. spi.begin(P_LORA_SCLK, P_LORA_MISO, P_LORA_MOSI);
  115. int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, 22, 8);
  116. #else
  117. int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, 22, 8);
  118. #endif
  119. if (status != RADIOLIB_ERR_NONE) {
  120. Serial.print("ERROR: radio init failed: ");
  121. Serial.println(status);
  122. halt();
  123. }
  124. fast_rng.begin(radio.random(0x7FFFFFFF));
  125. the_mesh.begin();
  126. RadioNoiseListener true_rng(radio);
  127. the_mesh.self_id = mesh::LocalIdentity(&true_rng); // create new random identity
  128. nextPing = 0;
  129. }
  130. void loop() {
  131. if (the_mesh.millisHasNowPassed(nextPing)) {
  132. the_mesh.sendPingPacket();
  133. nextPing = the_mesh.futureMillis(10000); // attempt ping every 10 seconds
  134. }
  135. the_mesh.loop();
  136. }