Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
8.6 KiB

  1. /*
  2. RFM69 MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if RFM69_SUPPORT
  6. #include "libs/RFM69Wrap.h"
  7. #include "ws.h"
  8. #define RFM69_PACKET_SEPARATOR ':'
  9. // -----------------------------------------------------------------------------
  10. // Locals
  11. // -----------------------------------------------------------------------------
  12. RFM69Wrap * _rfm69_radio;
  13. struct _node_t {
  14. unsigned long count = 0;
  15. unsigned long missing = 0;
  16. unsigned long duplicates = 0;
  17. unsigned char lastPacketID = 0;
  18. };
  19. _node_t _rfm69_node_info[RFM69_MAX_NODES];
  20. unsigned char _rfm69_node_count;
  21. unsigned long _rfm69_packet_count;
  22. // -----------------------------------------------------------------------------
  23. // WEB
  24. // -----------------------------------------------------------------------------
  25. #if WEB_SUPPORT
  26. void _rfm69WebSocketOnConnected(JsonObject& root) {
  27. root["rfm69Visible"] = 1;
  28. root["rfm69Topic"] = getSetting("rfm69Topic", RFM69_DEFAULT_TOPIC);
  29. root["packetCount"] = _rfm69_packet_count;
  30. root["nodeCount"] = _rfm69_node_count;
  31. JsonArray& mappings = root.createNestedArray("mapping");
  32. for (unsigned char i=0; i<RFM69_MAX_TOPICS; i++) {
  33. auto node = getSetting({"node", i}, 0);
  34. if (0 == node) break;
  35. JsonObject& mapping = mappings.createNestedObject();
  36. mapping["node"] = node;
  37. mapping["key"] = getSetting({"key", i});
  38. mapping["topic"] = getSetting({"topic", i});
  39. }
  40. }
  41. bool _rfm69WebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  42. if (strncmp(key, "rfm69", 5) == 0) return true;
  43. if (strncmp(key, "node", 4) == 0) return true;
  44. if (strncmp(key, "key", 3) == 0) return true;
  45. if (strncmp(key, "topic", 5) == 0) return true;
  46. return false;
  47. }
  48. void _rfm69WebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  49. if (strcmp(action, "clear-counts") == 0) _rfm69Clear();
  50. }
  51. #endif // WEB_SUPPORT
  52. void _rfm69CleanNodes(unsigned char num) {
  53. // Look for the last defined node
  54. int i = 0;
  55. while (i < num) {
  56. if (0 == getSetting({"node", i}, 0)) break;
  57. if (!getSetting({"key", i}).length()) break;
  58. if (!getSetting({"topic", i}).length()) break;
  59. ++i;
  60. }
  61. // Delete all other settings
  62. while (i < WIFI_MAX_NETWORKS) {
  63. delSetting("node", i);
  64. delSetting("key", i);
  65. delSetting("topic", i);
  66. ++i;
  67. }
  68. }
  69. void _rfm69Configure() {
  70. _rfm69CleanNodes(RFM69_MAX_TOPICS);
  71. }
  72. // -----------------------------------------------------------------------------
  73. // Radio
  74. // -----------------------------------------------------------------------------
  75. void _rfm69Debug(const char * level, packet_t * data) {
  76. DEBUG_MSG_P(
  77. PSTR("[RFM69] %s: messageID:%05d senderID:%03d targetID:%03d packetID:%03d rssi:%-04d key:%s value:%s\n"),
  78. level,
  79. data->messageID,
  80. data->senderID,
  81. data->targetID,
  82. data->packetID,
  83. data->rssi,
  84. data->key,
  85. data->value
  86. );
  87. }
  88. void _rfm69Process(packet_t * data) {
  89. // Is node beyond RFM69_MAX_NODES?
  90. if (data->senderID >= RFM69_MAX_NODES) return;
  91. // Count seen nodes
  92. if (_rfm69_node_info[data->senderID].count == 0) ++_rfm69_node_count;
  93. // Detect duplicates and missing packets
  94. // packetID==0 means device is not sending packetID info
  95. if (data->packetID > 0) {
  96. if (_rfm69_node_info[data->senderID].count > 0) {
  97. unsigned char gap = data->packetID - _rfm69_node_info[data->senderID].lastPacketID;
  98. if (gap == 0) {
  99. _rfm69_node_info[data->senderID].duplicates = _rfm69_node_info[data->senderID].duplicates + 1;
  100. //_rfm69Debug("DUP", data);
  101. return;
  102. }
  103. if ((gap > 1) && (data->packetID > 1)) {
  104. _rfm69_node_info[data->senderID].missing = _rfm69_node_info[data->senderID].missing + gap - 1;
  105. DEBUG_MSG_P(PSTR("[RFM69] %u missing packets detected\n"), gap - 1);
  106. }
  107. }
  108. }
  109. _rfm69Debug("OK ", data);
  110. _rfm69_node_info[data->senderID].lastPacketID = data->packetID;
  111. _rfm69_node_info[data->senderID].count = _rfm69_node_info[data->senderID].count + 1;
  112. // Send info to websocket clients
  113. {
  114. char buffer[200];
  115. snprintf_P(
  116. buffer,
  117. sizeof(buffer) - 1,
  118. PSTR("{\"nodeCount\": %d, \"packetCount\": %lu, \"packet\": {\"senderID\": %u, \"targetID\": %u, \"packetID\": %u, \"key\": \"%s\", \"value\": \"%s\", \"rssi\": %d, \"duplicates\": %d, \"missing\": %d}}"),
  119. _rfm69_node_count, _rfm69_packet_count,
  120. data->senderID, data->targetID, data->packetID, data->key, data->value, data->rssi,
  121. _rfm69_node_info[data->senderID].duplicates , _rfm69_node_info[data->senderID].missing);
  122. wsSend(buffer);
  123. }
  124. // If we are the target of the message, forward it via MQTT, otherwise quit
  125. if (!RFM69_PROMISCUOUS_SENDS && (RFM69_GATEWAY_ID != data->targetID)) return;
  126. // Try to find a matching mapping
  127. for (unsigned int i=0; i<RFM69_MAX_TOPICS; i++) {
  128. auto node = getSetting({"node", i}, 0);
  129. if (0 == node) break;
  130. if ((node == data->senderID) && (getSetting({"key", i}).equals(data->key))) {
  131. mqttSendRaw((char *) getSetting({"topic", i}).c_str(), (char *) String(data->value).c_str());
  132. return;
  133. }
  134. }
  135. // Mapping not found, use default topic
  136. String topic = getSetting("rfm69Topic", RFM69_DEFAULT_TOPIC);
  137. if (topic.length() > 0) {
  138. topic.replace("{node}", String(data->senderID));
  139. topic.replace("{key}", String(data->key));
  140. mqttSendRaw((char *) topic.c_str(), (char *) String(data->value).c_str());
  141. }
  142. }
  143. void _rfm69Loop() {
  144. if (_rfm69_radio->receiveDone()) {
  145. uint8_t senderID = _rfm69_radio->SENDERID;
  146. uint8_t targetID = _rfm69_radio->TARGETID;
  147. int16_t rssi = _rfm69_radio->RSSI;
  148. uint8_t length = _rfm69_radio->DATALEN;
  149. char buffer[length + 1];
  150. strncpy(buffer, (const char *) _rfm69_radio->DATA, length);
  151. buffer[length] = 0;
  152. // Do not send ACKs in promiscuous mode,
  153. // we want to listen without being heard
  154. if (!RFM69_PROMISCUOUS) {
  155. if (_rfm69_radio->ACKRequested()) _rfm69_radio->sendACK();
  156. }
  157. uint8_t parts = 1;
  158. for (uint8_t i=0; i<length; i++) {
  159. if (buffer[i] == RFM69_PACKET_SEPARATOR) ++parts;
  160. }
  161. if (parts > 1) {
  162. char sep[2] = {RFM69_PACKET_SEPARATOR, 0};
  163. uint8_t packetID = 0;
  164. char * key = strtok(buffer, sep);
  165. char * value = strtok(NULL, sep);
  166. if (parts > 2) {
  167. char * packet = strtok(NULL, sep);
  168. packetID = atoi(packet);
  169. }
  170. packet_t message;
  171. message.messageID = ++_rfm69_packet_count;
  172. message.packetID = packetID;
  173. message.senderID = senderID;
  174. message.targetID = targetID;
  175. message.key = key;
  176. message.value = value;
  177. message.rssi = rssi;
  178. _rfm69Process(&message);
  179. }
  180. }
  181. }
  182. void _rfm69Clear() {
  183. for(unsigned int i=0; i<RFM69_MAX_NODES; i++) {
  184. _rfm69_node_info[i].duplicates = 0;
  185. _rfm69_node_info[i].missing = 0;
  186. _rfm69_node_info[i].count = 0;
  187. }
  188. _rfm69_node_count = 0;
  189. _rfm69_packet_count = 0;
  190. }
  191. // -----------------------------------------------------------------------------
  192. // RFM69
  193. // -----------------------------------------------------------------------------
  194. void rfm69Setup() {
  195. delay(10);
  196. _rfm69Configure();
  197. _rfm69_radio = new RFM69Wrap(RFM69_CS_PIN, RFM69_IRQ_PIN, RFM69_IS_RFM69HW, digitalPinToInterrupt(RFM69_IRQ_PIN));
  198. _rfm69_radio->initialize(RFM69_FREQUENCY, RFM69_NODE_ID, RFM69_NETWORK_ID);
  199. _rfm69_radio->encrypt(RFM69_ENCRYPTKEY);
  200. _rfm69_radio->promiscuous(RFM69_PROMISCUOUS);
  201. _rfm69_radio->enableAutoPower(0);
  202. if (RFM69_IS_RFM69HW) _rfm69_radio->setHighPower();
  203. DEBUG_MSG_P(PSTR("[RFM69] Worning at %u MHz\n"), RFM69_FREQUENCY == RF69_433MHZ ? 433 : RFM69_FREQUENCY == RF69_868MHZ ? 868 : 915);
  204. DEBUG_MSG_P(PSTR("[RFM69] Node %u\n"), RFM69_NODE_ID);
  205. DEBUG_MSG_P(PSTR("[RFM69] Network %u\n"), RFM69_NETWORK_ID);
  206. DEBUG_MSG_P(PSTR("[RFM69] Promiscuous mode %s\n"), RFM69_PROMISCUOUS ? "ON" : "OFF");
  207. #if WEB_SUPPORT
  208. wsRegister()
  209. .onConnected(_rfm69WebSocketOnConnected)
  210. .onAction(_rfm69WebSocketOnAction)
  211. .onKeyCheck(_rfm69WebSocketOnKeyCheck);
  212. #endif
  213. // Main callbacks
  214. espurnaRegisterLoop(_rfm69Loop);
  215. espurnaRegisterReload(_rfm69Configure);
  216. }
  217. #endif // RFM69_SUPPORT