main.cpp 4.5 KB

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