BaseChatMesh.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. #include <Arduino.h> // needed for PlatformIO
  3. #include <Mesh.h>
  4. #include <helpers/AdvertDataHelpers.h>
  5. #include <helpers/TxtDataHelpers.h>
  6. #define MAX_TEXT_LEN (10*CIPHER_BLOCK_SIZE) // must be LESS than (MAX_PACKET_PAYLOAD - 4 - CIPHER_MAC_SIZE - 1)
  7. #include "ContactInfo.h"
  8. #define MAX_SEARCH_RESULTS 8
  9. #define MSG_SEND_FAILED 0
  10. #define MSG_SEND_SENT_FLOOD 1
  11. #define MSG_SEND_SENT_DIRECT 2
  12. #define REQ_TYPE_GET_STATUS 0x01 // same as _GET_STATS
  13. #define REQ_TYPE_KEEP_ALIVE 0x02
  14. #define RESP_SERVER_LOGIN_OK 0 // response to ANON_REQ
  15. class ContactVisitor {
  16. public:
  17. virtual void onContactVisit(const ContactInfo& contact) = 0;
  18. };
  19. class BaseChatMesh;
  20. class ContactsIterator {
  21. int next_idx = 0;
  22. public:
  23. bool hasNext(const BaseChatMesh* mesh, ContactInfo& dest);
  24. };
  25. #ifndef MAX_CONTACTS
  26. #define MAX_CONTACTS 32
  27. #endif
  28. #ifndef MAX_CONNECTIONS
  29. #define MAX_CONNECTIONS 16
  30. #endif
  31. struct ConnectionInfo {
  32. mesh::Identity server_id;
  33. unsigned long next_ping;
  34. uint32_t last_activity;
  35. uint32_t keep_alive_millis;
  36. uint32_t expected_ack;
  37. };
  38. #include "ChannelDetails.h"
  39. /**
  40. * \brief abstract Mesh class for common 'chat' client
  41. */
  42. class BaseChatMesh : public mesh::Mesh {
  43. friend class ContactsIterator;
  44. ContactInfo contacts[MAX_CONTACTS];
  45. int num_contacts;
  46. int sort_array[MAX_CONTACTS];
  47. int matching_peer_indexes[MAX_SEARCH_RESULTS];
  48. unsigned long txt_send_timeout;
  49. #ifdef MAX_GROUP_CHANNELS
  50. ChannelDetails channels[MAX_GROUP_CHANNELS];
  51. int num_channels; // only for addChannel()
  52. #endif
  53. mesh::Packet* _pendingLoopback;
  54. uint8_t temp_buf[MAX_TRANS_UNIT];
  55. ConnectionInfo connections[MAX_CONNECTIONS];
  56. mesh::Packet* composeMsgPacket(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char *text, uint32_t& expected_ack);
  57. void sendAckTo(const ContactInfo& dest, uint32_t ack_hash);
  58. protected:
  59. BaseChatMesh(mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::PacketManager& mgr, mesh::MeshTables& tables)
  60. : mesh::Mesh(radio, ms, rng, rtc, mgr, tables)
  61. {
  62. num_contacts = 0;
  63. #ifdef MAX_GROUP_CHANNELS
  64. memset(channels, 0, sizeof(channels));
  65. num_channels = 0;
  66. #endif
  67. txt_send_timeout = 0;
  68. _pendingLoopback = NULL;
  69. memset(connections, 0, sizeof(connections));
  70. }
  71. void resetContacts() { num_contacts = 0; }
  72. // 'UI' concepts, for sub-classes to implement
  73. virtual bool isAutoAddEnabled() const { return true; }
  74. virtual void onDiscoveredContact(ContactInfo& contact, bool is_new, uint8_t path_len, const uint8_t* path) = 0;
  75. virtual ContactInfo* processAck(const uint8_t *data) = 0;
  76. virtual void onContactPathUpdated(const ContactInfo& contact) = 0;
  77. virtual bool onContactPathRecv(ContactInfo& from, uint8_t* in_path, uint8_t in_path_len, uint8_t* out_path, uint8_t out_path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len);
  78. virtual void onMessageRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const char *text) = 0;
  79. virtual void onCommandDataRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const char *text) = 0;
  80. virtual void onSignedMessageRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const uint8_t *sender_prefix, const char *text) = 0;
  81. virtual uint32_t calcFloodTimeoutMillisFor(uint32_t pkt_airtime_millis) const = 0;
  82. virtual uint32_t calcDirectTimeoutMillisFor(uint32_t pkt_airtime_millis, uint8_t path_len) const = 0;
  83. virtual void onSendTimeout() = 0;
  84. virtual void onChannelMessageRecv(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t timestamp, const char *text) = 0;
  85. virtual uint8_t onContactRequest(const ContactInfo& contact, uint32_t sender_timestamp, const uint8_t* data, uint8_t len, uint8_t* reply) = 0;
  86. virtual void onContactResponse(const ContactInfo& contact, const uint8_t* data, uint8_t len) = 0;
  87. virtual void handleReturnPathRetry(const ContactInfo& contact, const uint8_t* path, uint8_t path_len);
  88. virtual void sendFloodScoped(const ContactInfo& recipient, mesh::Packet* pkt, uint32_t delay_millis=0);
  89. virtual void sendFloodScoped(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t delay_millis=0);
  90. // storage concepts, for sub-classes to override/implement
  91. virtual int getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) { return 0; } // not implemented
  92. virtual bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], int len) { return false; }
  93. // Mesh overrides
  94. void onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len) override;
  95. int searchPeersByHash(const uint8_t* hash) override;
  96. void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override;
  97. void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override;
  98. bool 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;
  99. void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override;
  100. #ifdef MAX_GROUP_CHANNELS
  101. int searchChannelsByHash(const uint8_t* hash, mesh::GroupChannel channels[], int max_matches) override;
  102. #endif
  103. void onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::GroupChannel& channel, uint8_t* data, size_t len) override;
  104. // Connections
  105. bool startConnection(const ContactInfo& contact, uint16_t keep_alive_secs);
  106. void stopConnection(const uint8_t* pub_key);
  107. bool hasConnectionTo(const uint8_t* pub_key);
  108. void markConnectionActive(const ContactInfo& contact);
  109. ContactInfo* checkConnectionsAck(const uint8_t* data);
  110. void checkConnections();
  111. public:
  112. mesh::Packet* createSelfAdvert(const char* name);
  113. mesh::Packet* createSelfAdvert(const char* name, double lat, double lon);
  114. int sendMessage(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char* text, uint32_t& expected_ack, uint32_t& est_timeout);
  115. int sendCommandData(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char* text, uint32_t& est_timeout);
  116. bool sendGroupMessage(uint32_t timestamp, mesh::GroupChannel& channel, const char* sender_name, const char* text, int text_len);
  117. int sendLogin(const ContactInfo& recipient, const char* password, uint32_t& est_timeout);
  118. int sendRequest(const ContactInfo& recipient, uint8_t req_type, uint32_t& tag, uint32_t& est_timeout);
  119. int sendRequest(const ContactInfo& recipient, const uint8_t* req_data, uint8_t data_len, uint32_t& tag, uint32_t& est_timeout);
  120. bool shareContactZeroHop(const ContactInfo& contact);
  121. uint8_t exportContact(const ContactInfo& contact, uint8_t dest_buf[]);
  122. bool importContact(const uint8_t src_buf[], uint8_t len);
  123. void resetPathTo(ContactInfo& recipient);
  124. void scanRecentContacts(int last_n, ContactVisitor* visitor);
  125. ContactInfo* searchContactsByPrefix(const char* name_prefix);
  126. ContactInfo* lookupContactByPubKey(const uint8_t* pub_key, int prefix_len);
  127. bool removeContact(ContactInfo& contact);
  128. bool addContact(const ContactInfo& contact);
  129. int getNumContacts() const { return num_contacts; }
  130. bool getContactByIdx(uint32_t idx, ContactInfo& contact);
  131. ContactsIterator startContactsIterator();
  132. ChannelDetails* addChannel(const char* name, const char* psk_base64);
  133. bool getChannel(int idx, ChannelDetails& dest);
  134. bool setChannel(int idx, const ChannelDetails& src);
  135. int findChannelIdx(const mesh::GroupChannel& ch);
  136. void loop();
  137. };