main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 10
  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. int searchPeersByHash(const uint8_t* hash) override {
  39. if (got_adv && server_id.isHashMatch(hash)) {
  40. return 1;
  41. }
  42. return 0; // not found
  43. }
  44. void onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len) override {
  45. if (memcmp(app_data, "PING", 4) == 0) {
  46. Serial.println("Received advertisement from a PING server");
  47. // check for replay attacks
  48. if (timestamp > last_advert_timestamp) {
  49. last_advert_timestamp = timestamp;
  50. server_id = id;
  51. self_id.calcSharedSecret(server_secret, id); // calc ECDH shared secret
  52. got_adv = true;
  53. }
  54. }
  55. }
  56. void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, uint8_t* data, size_t len) override {
  57. if (type == PAYLOAD_TYPE_RESPONSE) {
  58. Serial.println("Received PING Reply!");
  59. if (packet->isRouteFlood()) {
  60. // let server know path TO here, so they can use sendDirect() for future ping responses
  61. mesh::Packet* path = createPathReturn(server_id, server_secret, packet->path, packet->path_len, 0, NULL, 0);
  62. if (path) sendFlood(path);
  63. }
  64. }
  65. }
  66. 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 {
  67. // must be from server_id
  68. Serial.printf("PATH to server, path_len=%d\n", (uint32_t) path_len);
  69. memcpy(server_path, path, server_path_len = path_len); // store a copy of path, for sendDirect()
  70. if (packet->isRouteFlood()) {
  71. // send a reciprocal return path to sender, but send DIRECTLY!
  72. mesh::Packet* rpath = createPathReturn(server_id, server_secret, packet->path, packet->path_len, 0, NULL, 0);
  73. if (rpath) sendDirect(rpath, path, path_len);
  74. }
  75. if (extra_type == PAYLOAD_TYPE_RESPONSE) {
  76. Serial.println("Received PING Reply!");
  77. }
  78. }
  79. public:
  80. MyMesh(mesh::Radio& radio, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables)
  81. : mesh::Mesh(radio, *new ArduinoMillis(), rng, rtc, *new StaticPoolPacketManager(16), tables)
  82. {
  83. }
  84. void sendPingPacket() {
  85. if (!got_adv) return; // have not received Advert yet
  86. uint32_t now = getRTCClock()->getCurrentTime(); // important, need timestamp in packet, so that packet_hash will be unique
  87. mesh::Packet* ping = createAnonDatagram(PAYLOAD_TYPE_ANON_REQ, self_id, server_id, server_secret, (uint8_t *) &now, sizeof(now));
  88. if (ping) {
  89. if (server_path_len < 0) {
  90. sendFlood(ping);
  91. } else {
  92. sendDirect(ping, server_path, server_path_len);
  93. }
  94. }
  95. }
  96. };
  97. SPIClass spi;
  98. StdRNG fast_rng;
  99. SimpleMeshTables tables;
  100. SX1262 radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, spi);
  101. MyMesh the_mesh(*new RadioLibWrapper(radio, board), fast_rng, *new VolatileRTCClock(), tables);
  102. unsigned long nextPing;
  103. void halt() {
  104. while (1) ;
  105. }
  106. void setup() {
  107. Serial.begin(115200);
  108. board.begin();
  109. #if defined(P_LORA_SCLK)
  110. spi.begin(P_LORA_SCLK, P_LORA_MISO, P_LORA_MOSI);
  111. int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, 22, 8);
  112. #else
  113. int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, 22, 8);
  114. #endif
  115. if (status != RADIOLIB_ERR_NONE) {
  116. Serial.print("ERROR: radio init failed: ");
  117. Serial.println(status);
  118. halt();
  119. }
  120. fast_rng.begin(radio.random(0x7FFFFFFF));
  121. the_mesh.begin();
  122. RadioNoiseListener true_rng(radio);
  123. the_mesh.self_id = mesh::LocalIdentity(&true_rng); // create new random identity
  124. nextPing = 0;
  125. }
  126. void loop() {
  127. if (the_mesh.millisHasNowPassed(nextPing)) {
  128. the_mesh.sendPingPacket();
  129. nextPing = the_mesh.futureMillis(10000); // attempt ping every 10 seconds
  130. }
  131. the_mesh.loop();
  132. }