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.

315 lines
8.3 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. byte message[RF_MESSAGE_SIZE];
  192. if (_rfbToArray(payload, message)) {
  193. _rfbSend(message, 1);
  194. }
  195. }
  196. }
  197. }
  198. // -----------------------------------------------------------------------------
  199. // PUBLIC
  200. // -----------------------------------------------------------------------------
  201. void rfbStore(unsigned char id, bool status, const char * code) {
  202. DEBUG_MSG_P(PSTR("[RFBRIDGE] Storing %d-%s => '%s'\n"), id, status ? "ON" : "OFF", code);
  203. char key[8] = {0};
  204. snprintf_P(key, sizeof(key), PSTR("rfb%d%s"), id, status ? "on" : "off");
  205. setSetting(key, code);
  206. }
  207. String rfbRetrieve(unsigned char id, bool status) {
  208. char key[8] = {0};
  209. snprintf_P(key, sizeof(key), PSTR("rfb%d%s"), id, status ? "on" : "off");
  210. return getSetting(key);
  211. }
  212. void rfbStatus(unsigned char id, bool status) {
  213. String value = rfbRetrieve(id, status);
  214. if (value.length() > 0) {
  215. bool same = _rfbSameOnOff(id);
  216. byte message[RF_MESSAGE_SIZE];
  217. _rfbToArray(value.c_str(), message);
  218. unsigned char times = RF_SEND_TIMES;
  219. if (same) times = _rfbin ? 0 : 1;
  220. _rfbSend(message, times);
  221. }
  222. }
  223. void rfbLearn(unsigned char id, bool status) {
  224. _learnId = id;
  225. _learnStatus = status;
  226. _rfbLearn();
  227. }
  228. void rfbForget(unsigned char id, bool status) {
  229. char key[8] = {0};
  230. snprintf_P(key, sizeof(key), PSTR("rfb%d%s"), id, status ? "on" : "off");
  231. delSetting(key);
  232. // Websocket update
  233. #if WEB_SUPPORT
  234. char wsb[100];
  235. snprintf_P(wsb, sizeof(wsb), PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"\"}]}"), id, status ? 1 : 0);
  236. wsSend(wsb);
  237. #endif
  238. }
  239. // -----------------------------------------------------------------------------
  240. // SETUP & LOOP
  241. // -----------------------------------------------------------------------------
  242. void rfbSetup() {
  243. mqttRegister(_rfbMqttCallback);
  244. }
  245. void rfbLoop() {
  246. _rfbReceive();
  247. }
  248. #endif