Mesh.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #pragma once
  2. #include <Dispatcher.h>
  3. namespace mesh {
  4. class GroupChannel {
  5. public:
  6. uint8_t hash[PATH_HASH_SIZE];
  7. uint8_t secret[PUB_KEY_SIZE];
  8. };
  9. /**
  10. * An abstraction of the data tables needed to be maintained
  11. */
  12. class MeshTables {
  13. public:
  14. virtual bool hasSeen(const Packet* packet) = 0;
  15. virtual void clear(const Packet* packet) = 0; // remove this packet hash from table
  16. };
  17. /**
  18. * \brief The next layer in the basic Dispatcher task, Mesh recognises the particular Payload TYPES,
  19. * and provides virtual methods for sub-classes on handling incoming, and also preparing outbound Packets.
  20. */
  21. class Mesh : public Dispatcher {
  22. RTCClock* _rtc;
  23. RNG* _rng;
  24. MeshTables* _tables;
  25. void removeSelfFromPath(Packet* packet);
  26. void routeDirectRecvAcks(Packet* packet, uint32_t delay_millis);
  27. //void routeRecvAcks(Packet* packet, uint32_t delay_millis);
  28. DispatcherAction forwardMultipartDirect(Packet* pkt);
  29. protected:
  30. DispatcherAction onRecvPacket(Packet* pkt) override;
  31. virtual uint32_t getCADFailRetryDelay() const override;
  32. /**
  33. * \brief Decide what to do with received packet, ie. discard, forward, or hold
  34. */
  35. DispatcherAction routeRecvPacket(Packet* packet);
  36. /**
  37. * \brief Called _before_ the packet is dispatched to the on..Recv() methods.
  38. * \returns true, if given packet should NOT be processed.
  39. */
  40. virtual bool filterRecvFloodPacket(Packet* packet) { return false; }
  41. /**
  42. * \brief Check whether this packet should be forwarded (re-transmitted) or not.
  43. * Is sub-classes responsibility to make sure given packet is only transmitted ONCE (by this node)
  44. */
  45. virtual bool allowPacketForward(const Packet* packet);
  46. /**
  47. * \returns number of milliseconds delay to apply to retransmitting the given packet.
  48. */
  49. virtual uint32_t getRetransmitDelay(const Packet* packet);
  50. /**
  51. * \returns number of milliseconds delay to apply to retransmitting the given packet, for DIRECT mode.
  52. */
  53. virtual uint32_t getDirectRetransmitDelay(const Packet* packet);
  54. /**
  55. * \returns number of extra (Direct) ACK transmissions wanted.
  56. */
  57. virtual uint8_t getExtraAckTransmitCount() const;
  58. /**
  59. * \brief Perform search of local DB of peers/contacts.
  60. * \returns Number of peers with matching hash
  61. */
  62. virtual int searchPeersByHash(const uint8_t* hash);
  63. /**
  64. * \brief lookup the ECDH shared-secret between this node and peer by idx (calculate if necessary)
  65. * \param dest_secret destination array to copy the secret (must be PUB_KEY_SIZE bytes)
  66. * \param peer_idx index of peer, [0..n) where n is what searchPeersByHash() returned
  67. */
  68. virtual void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) { }
  69. /**
  70. * \brief A (now decrypted) data packet has been received (by a known peer).
  71. * NOTE: these can be received multiple times (per sender/msg-id), via different routes
  72. * \param type one of: PAYLOAD_TYPE_TXT_MSG, PAYLOAD_TYPE_REQ, PAYLOAD_TYPE_RESPONSE
  73. * \param sender_idx index of peer, [0..n) where n is what searchPeersByHash() returned
  74. * \param secret the pre-calculated shared-secret (handy for sending response packet)
  75. * \param data decrypted data from payload
  76. */
  77. virtual void onPeerDataRecv(Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) { }
  78. /**
  79. * \brief A TRACE packet has been received. (and has reached the end of its given path)
  80. * NOTE: this may have been initiated by another node.
  81. * \param tag a random (unique-ish) tag set by initiator
  82. * \param auth_code a code to authenticate the packet
  83. * \param flags zero for now
  84. * \param path_snrs single byte SNR*4 for each hop in the path
  85. * \param path_hashes hashes if each repeater in the path
  86. * \param path_len length of the path_snrs[] and path_hashes[] arrays
  87. */
  88. virtual void onTraceRecv(Packet* packet, uint32_t tag, uint32_t auth_code, uint8_t flags, const uint8_t* path_snrs, const uint8_t* path_hashes, uint8_t path_len) { }
  89. /**
  90. * \brief A path TO peer (sender_idx) has been received. (also with optional 'extra' data encoded)
  91. * NOTE: these can be received multiple times (per sender), via different routes
  92. * \param sender_idx index of peer, [0..n) where n is what searchPeersByHash() returned
  93. * \param secret the pre-calculated shared-secret (handy for sending response packet)
  94. * \returns true, if path was accepted and that reciprocal path should be sent
  95. */
  96. virtual bool onPeerPathRecv(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) { return false; }
  97. /**
  98. * \brief A new incoming Advertisement has been received.
  99. * NOTE: these can be received multiple times (per id/timestamp), via different routes
  100. */
  101. virtual void onAdvertRecv(Packet* packet, const Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len) { }
  102. /**
  103. * \brief A (now decrypted) data packet has been received.
  104. * NOTE: these can be received multiple times (per sender/contents), via different routes
  105. * \param secret ECDH shared secret
  106. * \param sender public key provided by sender
  107. */
  108. virtual void onAnonDataRecv(Packet* packet, const uint8_t* secret, const Identity& sender, uint8_t* data, size_t len) { }
  109. /**
  110. * \brief A path TO 'sender' has been received. (also with optional 'extra' data encoded)
  111. * NOTE: these can be received multiple times (per sender), via different routes
  112. */
  113. virtual void onPathRecv(Packet* packet, Identity& sender, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) { }
  114. /**
  115. * \brief A control packet has been received.
  116. */
  117. virtual void onControlDataRecv(Packet* packet) { }
  118. /**
  119. * \brief A packet with PAYLOAD_TYPE_RAW_CUSTOM has been received.
  120. */
  121. virtual void onRawDataRecv(Packet* packet) { }
  122. /**
  123. * \brief Perform search of local DB of matching GroupChannels.
  124. * \param channels OUT - store matching channels in this array, up to max_matches
  125. * \returns Number of channels with matching hash
  126. */
  127. virtual int searchChannelsByHash(const uint8_t* hash, GroupChannel channels[], int max_matches);
  128. /**
  129. * \brief An encrypted group data packet has been received.
  130. * NOTE: the same payload can be received multiple times, via different routes
  131. * \param type one of: PAYLOAD_TYPE_GRP_TXT, PAYLOAD_TYPE_GRP_DATA
  132. * \param channel the matching GroupChannel
  133. */
  134. virtual void onGroupDataRecv(Packet* packet, uint8_t type, const GroupChannel& channel, uint8_t* data, size_t len) { }
  135. /**
  136. * \brief A simple ACK packet has been received.
  137. * NOTE: same ACK can be received multiple times, via different routes
  138. */
  139. virtual void onAckRecv(Packet* packet, uint32_t ack_crc) { }
  140. Mesh(Radio& radio, MillisecondClock& ms, RNG& rng, RTCClock& rtc, PacketManager& mgr, MeshTables& tables)
  141. : Dispatcher(radio, ms, mgr), _rng(&rng), _rtc(&rtc), _tables(&tables)
  142. {
  143. }
  144. MeshTables* getTables() const { return _tables; }
  145. public:
  146. void begin();
  147. void loop();
  148. LocalIdentity self_id;
  149. RNG* getRNG() const { return _rng; }
  150. RTCClock* getRTCClock() const { return _rtc; }
  151. Packet* createAdvert(const LocalIdentity& id, const uint8_t* app_data=NULL, size_t app_data_len=0);
  152. Packet* createDatagram(uint8_t type, const Identity& dest, const uint8_t* secret, const uint8_t* data, size_t len);
  153. Packet* createAnonDatagram(uint8_t type, const LocalIdentity& sender, const Identity& dest, const uint8_t* secret, const uint8_t* data, size_t data_len);
  154. Packet* createGroupDatagram(uint8_t type, const GroupChannel& channel, const uint8_t* data, size_t data_len);
  155. Packet* createAck(const uint8_t* ack, uint8_t len);
  156. Packet* createAck(uint32_t ack_crc) { return createAck((uint8_t *) &ack_crc, 4); }
  157. Packet* createMultiAck(const uint8_t* ack, uint8_t len, uint8_t remaining);
  158. Packet* createMultiAck(uint32_t ack_crc, uint8_t remaining) { return createMultiAck((uint8_t *)&ack_crc, 4, remaining); }
  159. Packet* createPathReturn(const uint8_t* dest_hash, const uint8_t* secret, const uint8_t* path, uint8_t path_len, uint8_t extra_type, const uint8_t*extra, size_t extra_len);
  160. Packet* createPathReturn(const Identity& dest, const uint8_t* secret, const uint8_t* path, uint8_t path_len, uint8_t extra_type, const uint8_t*extra, size_t extra_len);
  161. Packet* createRawData(const uint8_t* data, size_t len);
  162. Packet* createTrace(uint32_t tag, uint32_t auth_code, uint8_t flags = 0);
  163. Packet* createControlData(const uint8_t* data, size_t len);
  164. /**
  165. * \brief send a locally-generated Packet with flood routing
  166. */
  167. void sendFlood(Packet* packet, uint32_t delay_millis=0, uint8_t path_hash_size=1);
  168. /**
  169. * \brief send a locally-generated Packet with flood routing
  170. * \param transport_codes array of 2 codes to attach to packet
  171. */
  172. void sendFlood(Packet* packet, uint16_t* transport_codes, uint32_t delay_millis=0, uint8_t path_hash_size=1);
  173. /**
  174. * \brief send a locally-generated Packet with Direct routing
  175. */
  176. void sendDirect(Packet* packet, const uint8_t* path, uint8_t path_len, uint32_t delay_millis=0);
  177. /**
  178. * \brief send a locally-generated Packet to just neighbor nodes (zero hops)
  179. */
  180. void sendZeroHop(Packet* packet, uint32_t delay_millis=0);
  181. /**
  182. * \brief send a locally-generated Packet to just neighbor nodes (zero hops), with specific transport codes
  183. * \param transport_codes array of 2 codes to attach to packet
  184. */
  185. void sendZeroHop(Packet* packet, uint16_t* transport_codes, uint32_t delay_millis=0);
  186. };
  187. }