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.

322 lines
8.6 KiB

  1. /*
  2. ITEAD RF BRIDGE MODULE
  3. Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #ifdef ITEAD_SONOFF_RFBRIDGE
  6. // -----------------------------------------------------------------------------
  7. // DEFINITIONS
  8. // -----------------------------------------------------------------------------
  9. #define RF_MESSAGE_SIZE 9
  10. #define RF_CODE_START 0xAA
  11. #define RF_CODE_ACK 0xA0
  12. #define RF_CODE_LEARN 0xA1
  13. #define RF_CODE_LEARN_KO 0xA2
  14. #define RF_CODE_LEARN_OK 0xA3
  15. #define RF_CODE_RFIN 0xA4
  16. #define RF_CODE_RFOUT 0xA5
  17. #define RF_CODE_STOP 0x55
  18. // -----------------------------------------------------------------------------
  19. // GLOBALS TO THE MODULE
  20. // -----------------------------------------------------------------------------
  21. unsigned char _uartbuf[RF_MESSAGE_SIZE+3] = {0};
  22. unsigned char _uartpos = 0;
  23. unsigned char _learnId = 0;
  24. bool _learnStatus = true;
  25. bool _rfbin = false;
  26. // -----------------------------------------------------------------------------
  27. // PRIVATES
  28. // -----------------------------------------------------------------------------
  29. void _rfbAck() {
  30. DEBUG_MSG_P(PSTR("[RFBRIDGE] Sending ACK\n"));
  31. Serial.println();
  32. Serial.write(RF_CODE_START);
  33. Serial.write(RF_CODE_ACK);
  34. Serial.write(RF_CODE_STOP);
  35. Serial.flush();
  36. Serial.println();
  37. }
  38. void _rfbLearn() {
  39. DEBUG_MSG_P(PSTR("[RFBRIDGE] Sending LEARN\n"));
  40. Serial.println();
  41. Serial.write(RF_CODE_START);
  42. Serial.write(RF_CODE_LEARN);
  43. Serial.write(RF_CODE_STOP);
  44. Serial.flush();
  45. Serial.println();
  46. #if WEB_SUPPORT
  47. char buffer[100];
  48. snprintf_P(buffer, sizeof(buffer), PSTR("{\"action\": \"rfbLearn\", \"data\":{\"id\": %d, \"status\": %d}}"), _learnId, _learnStatus ? 1 : 0);
  49. wsSend(buffer);
  50. #endif
  51. }
  52. void _rfbSend(byte * message) {
  53. Serial.println();
  54. Serial.write(RF_CODE_START);
  55. Serial.write(RF_CODE_RFOUT);
  56. for (unsigned char j=0; j<RF_MESSAGE_SIZE; j++) {
  57. Serial.write(message[j]);
  58. }
  59. Serial.write(RF_CODE_STOP);
  60. Serial.flush();
  61. Serial.println();
  62. }
  63. void _rfbSend(byte * message, int times) {
  64. char buffer[RF_MESSAGE_SIZE];
  65. _rfbToChar(message, buffer);
  66. DEBUG_MSG_P(PSTR("[RFBRIDGE] Sending MESSAGE '%s' %d time(s)\n"), buffer, times);
  67. for (int i=0; i<times; i++) {
  68. if (i>0) {
  69. unsigned long start = millis();
  70. while (millis() - start < RF_SEND_DELAY) delay(1);
  71. }
  72. _rfbSend(message);
  73. }
  74. }
  75. void _rfbDecode() {
  76. byte action = _uartbuf[0];
  77. char buffer[RF_MESSAGE_SIZE * 2 + 1] = {0};
  78. DEBUG_MSG_P(PSTR("[RFBRIDGE] Action 0x%02X\n"), action);
  79. if (action == RF_CODE_LEARN_KO) {
  80. _rfbAck();
  81. DEBUG_MSG_P(PSTR("[RFBRIDGE] Learn timeout\n"));
  82. #if WEB_SUPPORT
  83. wsSend_P(PSTR("{\"action\": \"rfbTimeout\"}"));
  84. #endif
  85. }
  86. if (action == RF_CODE_LEARN_OK || action == RF_CODE_RFIN) {
  87. _rfbToChar(&_uartbuf[1], buffer);
  88. mqttSend(MQTT_TOPIC_RFIN, buffer);
  89. _rfbAck();
  90. }
  91. if (action == RF_CODE_LEARN_OK) {
  92. DEBUG_MSG_P(PSTR("[RFBRIDGE] Learn success\n"));
  93. rfbStore(_learnId, _learnStatus, buffer);
  94. // Websocket update
  95. #if WEB_SUPPORT
  96. char wsb[100];
  97. snprintf_P(wsb, sizeof(wsb), PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"%s\"}]}"), _learnId, _learnStatus ? 1 : 0, buffer);
  98. wsSend(wsb);
  99. #endif
  100. }
  101. if (action == RF_CODE_RFIN) {
  102. DEBUG_MSG_P(PSTR("[RFBRIDGE] Forward message '%s'\n"), buffer);
  103. // Look for the code
  104. unsigned char id, status;
  105. bool found = false;
  106. for (id=0; id<relayCount(); id++) {
  107. for (status=0; status<2; status++) {
  108. String code = rfbRetrieve(id, status == 1);
  109. if (code.length()) {
  110. if (code.endsWith(&buffer[12])) {
  111. found = true;
  112. break;
  113. }
  114. }
  115. }
  116. if (found) break;
  117. }
  118. if (found) {
  119. _rfbin = true;
  120. relayStatus(id, status == 1);
  121. }
  122. }
  123. }
  124. void _rfbReceive() {
  125. static bool receiving = false;
  126. while (Serial.available()) {
  127. yield();
  128. byte c = Serial.read();
  129. //DEBUG_MSG_P(PSTR("[RFBRIDGE] Received 0x%02X\n"), c);
  130. if (receiving) {
  131. if (c == RF_CODE_STOP) {
  132. _rfbDecode();
  133. receiving = false;
  134. } else {
  135. _uartbuf[_uartpos++] = c;
  136. }
  137. } else if (c == RF_CODE_START) {
  138. _uartpos = 0;
  139. receiving = true;
  140. }
  141. }
  142. }
  143. bool _rfbCompare(const char * code1, const char * code2) {
  144. return strcmp(&code1[12], &code2[12]) == 0;
  145. }
  146. bool _rfbSameOnOff(unsigned char id) {
  147. return _rfbCompare(rfbRetrieve(id, true).c_str(), rfbRetrieve(id, false).c_str());
  148. }
  149. /*
  150. From an hexa char array ("A220EE...") to a byte array (half the size)
  151. */
  152. bool _rfbToArray(const char * in, byte * out) {
  153. if (strlen(in) != RF_MESSAGE_SIZE * 2) return false;
  154. char tmp[3] = {0};
  155. for (unsigned char p = 0; p<RF_MESSAGE_SIZE; p++) {
  156. memcpy(tmp, &in[p*2], 2);
  157. out[p] = strtol(tmp, NULL, 16);
  158. }
  159. return true;
  160. }
  161. /*
  162. From a byte array to an hexa char array ("A220EE...", double the size)
  163. */
  164. bool _rfbToChar(byte * in, char * out) {
  165. for (unsigned char p = 0; p<RF_MESSAGE_SIZE; p++) {
  166. sprintf_P(&out[p*2], PSTR("%02X"), in[p]);
  167. }
  168. return true;
  169. }
  170. void _rfbMqttCallback(unsigned int type, const char * topic, const char * payload) {
  171. if (type == MQTT_CONNECT_EVENT) {
  172. char buffer[strlen(MQTT_TOPIC_RFLEARN) + 3];
  173. snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_RFLEARN);
  174. mqttSubscribe(buffer);
  175. mqttSubscribe(MQTT_TOPIC_RFOUT);
  176. }
  177. if (type == MQTT_MESSAGE_EVENT) {
  178. // Match topic
  179. String t = mqttSubtopic((char *) topic);
  180. // Check if should go into learn mode
  181. if (t.startsWith(MQTT_TOPIC_RFLEARN)) {
  182. _learnId = t.substring(strlen(MQTT_TOPIC_RFLEARN)+1).toInt();
  183. if (_learnId >= relayCount()) {
  184. DEBUG_MSG_P(PSTR("[RFBRIDGE] Wrong learnID (%d)\n"), _learnId);
  185. return;
  186. }
  187. _learnStatus = (char)payload[0] != '0';
  188. _rfbLearn();
  189. }
  190. if (t.equals(MQTT_TOPIC_RFOUT)) {
  191. // The payload may be a code in HEX format ([0-9A-Z]{18}) or
  192. // the code comma the number of times to transmit it.
  193. byte message[RF_MESSAGE_SIZE];
  194. char * tok = strtok((char *) payload, ",");
  195. if (_rfbToArray(tok, message)) {
  196. tok = strtok(NULL, ",");
  197. byte times = (tok != NULL) ? atoi(tok) : 1;
  198. _rfbSend(message, times);
  199. }
  200. }
  201. }
  202. }
  203. // -----------------------------------------------------------------------------
  204. // PUBLIC
  205. // -----------------------------------------------------------------------------
  206. void rfbStore(unsigned char id, bool status, const char * code) {
  207. DEBUG_MSG_P(PSTR("[RFBRIDGE] Storing %d-%s => '%s'\n"), id, status ? "ON" : "OFF", code);
  208. char key[8] = {0};
  209. snprintf_P(key, sizeof(key), PSTR("rfb%s%d"), status ? "ON" : "OFF", id);
  210. setSetting(key, code);
  211. }
  212. String rfbRetrieve(unsigned char id, bool status) {
  213. char key[8] = {0};
  214. snprintf_P(key, sizeof(key), PSTR("rfb%s%d"), status ? "ON" : "OFF", id);
  215. return getSetting(key);
  216. }
  217. void rfbStatus(unsigned char id, bool status) {
  218. String value = rfbRetrieve(id, status);
  219. if (value.length() > 0) {
  220. bool same = _rfbSameOnOff(id);
  221. byte message[RF_MESSAGE_SIZE];
  222. _rfbToArray(value.c_str(), message);
  223. unsigned char times = RF_SEND_TIMES;
  224. if (same) times = _rfbin ? 0 : 1;
  225. _rfbSend(message, times);
  226. }
  227. }
  228. void rfbLearn(unsigned char id, bool status) {
  229. _learnId = id;
  230. _learnStatus = status;
  231. _rfbLearn();
  232. }
  233. void rfbForget(unsigned char id, bool status) {
  234. char key[8] = {0};
  235. snprintf_P(key, sizeof(key), PSTR("rfb%s%d"), status ? "ON" : "OFF", id);
  236. delSetting(key);
  237. // Websocket update
  238. #if WEB_SUPPORT
  239. char wsb[100];
  240. snprintf_P(wsb, sizeof(wsb), PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"\"}]}"), id, status ? 1 : 0);
  241. wsSend(wsb);
  242. #endif
  243. }
  244. // -----------------------------------------------------------------------------
  245. // SETUP & LOOP
  246. // -----------------------------------------------------------------------------
  247. void rfbSetup() {
  248. mqttRegister(_rfbMqttCallback);
  249. }
  250. void rfbLoop() {
  251. _rfbReceive();
  252. }
  253. #endif