BaseChatMesh.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. ContactInfo* allocateContactSlot(); // helper to find slot for new contact
  73. // 'UI' concepts, for sub-classes to implement
  74. virtual bool isAutoAddEnabled() const { return true; }
  75. virtual bool shouldAutoAddContactType(uint8_t type) const { return true; }
  76. virtual void onContactsFull() {};
  77. virtual bool shouldOverwriteWhenFull() const { return false; }
  78. virtual void onContactOverwrite(const uint8_t* pub_key) {};
  79. virtual void onDiscoveredContact(ContactInfo& contact, bool is_new, uint8_t path_len, const uint8_t* path) = 0;
  80. virtual ContactInfo* processAck(const uint8_t *data) = 0;
  81. virtual void onContactPathUpdated(const ContactInfo& contact) = 0;
  82. 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);
  83. virtual void onMessageRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const char *text) = 0;
  84. virtual void onCommandDataRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const char *text) = 0;
  85. virtual void onSignedMessageRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const uint8_t *sender_prefix, const char *text) = 0;
  86. virtual uint32_t calcFloodTimeoutMillisFor(uint32_t pkt_airtime_millis) const = 0;
  87. virtual uint32_t calcDirectTimeoutMillisFor(uint32_t pkt_airtime_millis, uint8_t path_len) const = 0;
  88. virtual void onSendTimeout() = 0;
  89. virtual void onChannelMessageRecv(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t timestamp, const char *text) = 0;
  90. virtual uint8_t onContactRequest(const ContactInfo& contact, uint32_t sender_timestamp, const uint8_t* data, uint8_t len, uint8_t* reply) = 0;
  91. virtual void onContactResponse(const ContactInfo& contact, const uint8_t* data, uint8_t len) = 0;
  92. virtual void handleReturnPathRetry(const ContactInfo& contact, const uint8_t* path, uint8_t path_len);
  93. virtual void sendFloodScoped(const ContactInfo& recipient, mesh::Packet* pkt, uint32_t delay_millis=0);
  94. virtual void sendFloodScoped(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t delay_millis=0);
  95. // storage concepts, for sub-classes to override/implement
  96. virtual int getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) { return 0; } // not implemented
  97. virtual bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], int len) { return false; }
  98. // Mesh overrides
  99. void onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len) override;
  100. int searchPeersByHash(const uint8_t* hash) override;
  101. void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override;
  102. void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override;
  103. 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;
  104. void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override;
  105. #ifdef MAX_GROUP_CHANNELS
  106. int searchChannelsByHash(const uint8_t* hash, mesh::GroupChannel channels[], int max_matches) override;
  107. #endif
  108. void onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::GroupChannel& channel, uint8_t* data, size_t len) override;
  109. // Connections
  110. bool startConnection(const ContactInfo& contact, uint16_t keep_alive_secs);
  111. void stopConnection(const uint8_t* pub_key);
  112. bool hasConnectionTo(const uint8_t* pub_key);
  113. void markConnectionActive(const ContactInfo& contact);
  114. ContactInfo* checkConnectionsAck(const uint8_t* data);
  115. void checkConnections();
  116. public:
  117. mesh::Packet* createSelfAdvert(const char* name);
  118. mesh::Packet* createSelfAdvert(const char* name, double lat, double lon);
  119. int sendMessage(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char* text, uint32_t& expected_ack, uint32_t& est_timeout);
  120. int sendCommandData(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char* text, uint32_t& est_timeout);
  121. bool sendGroupMessage(uint32_t timestamp, mesh::GroupChannel& channel, const char* sender_name, const char* text, int text_len);
  122. int sendLogin(const ContactInfo& recipient, const char* password, uint32_t& est_timeout);
  123. int sendAnonReq(const ContactInfo& recipient, const uint8_t* data, uint8_t len, uint32_t& tag, uint32_t& est_timeout);
  124. int sendRequest(const ContactInfo& recipient, uint8_t req_type, uint32_t& tag, uint32_t& est_timeout);
  125. int sendRequest(const ContactInfo& recipient, const uint8_t* req_data, uint8_t data_len, uint32_t& tag, uint32_t& est_timeout);
  126. bool shareContactZeroHop(const ContactInfo& contact);
  127. uint8_t exportContact(const ContactInfo& contact, uint8_t dest_buf[]);
  128. bool importContact(const uint8_t src_buf[], uint8_t len);
  129. void resetPathTo(ContactInfo& recipient);
  130. void scanRecentContacts(int last_n, ContactVisitor* visitor);
  131. ContactInfo* searchContactsByPrefix(const char* name_prefix);
  132. ContactInfo* lookupContactByPubKey(const uint8_t* pub_key, int prefix_len);
  133. bool removeContact(ContactInfo& contact);
  134. bool addContact(const ContactInfo& contact);
  135. int getNumContacts() const { return num_contacts; }
  136. bool getContactByIdx(uint32_t idx, ContactInfo& contact);
  137. ContactsIterator startContactsIterator();
  138. ChannelDetails* addChannel(const char* name, const char* psk_base64);
  139. bool getChannel(int idx, ChannelDetails& dest);
  140. bool setChannel(int idx, const ChannelDetails& src);
  141. int findChannelIdx(const mesh::GroupChannel& ch);
  142. void loop();
  143. };