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.

307 lines
8.0 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 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. char wsb[100];
  47. sprintf_P(wsb, PSTR("{\"action\": \"rfbLearn\", \"data\":{\"id\": %d, \"status\": %d}}"), _learnId, _learnStatus ? 1 : 0);
  48. wsSend(wsb);
  49. }
  50. void _rfbSend(byte * message) {
  51. Serial.println();
  52. Serial.write(RF_CODE_START);
  53. Serial.write(RF_CODE_RFOUT);
  54. for (unsigned char j=0; j<RF_MESSAGE_SIZE; j++) {
  55. Serial.write(message[j]);
  56. }
  57. Serial.write(RF_CODE_STOP);
  58. Serial.flush();
  59. Serial.println();
  60. }
  61. void _rfbSend(byte * message, int times) {
  62. char buffer[RF_MESSAGE_SIZE];
  63. _rfbToChar(message, buffer);
  64. DEBUG_MSG_P(PSTR("[RFBRIDGE] Sending MESSAGE '%s' %d time(s)\n"), buffer, times);
  65. for (int i=0; i<times; i++) {
  66. if (i>0) {
  67. unsigned long start = millis();
  68. while (millis() - start < RF_SEND_DELAY) delay(1);
  69. }
  70. _rfbSend(message);
  71. }
  72. }
  73. void _rfbDecode() {
  74. byte action = _uartbuf[0];
  75. char buffer[RF_MESSAGE_SIZE * 2 + 1] = {0};
  76. DEBUG_MSG_P(PSTR("[RFBRIDGE] Action 0x%02X\n"), action);
  77. if (action == RF_CODE_LEARN_KO) {
  78. _rfbAck();
  79. DEBUG_MSG_P(PSTR("[RFBRIDGE] Learn timeout\n"));
  80. wsSend("{\"action\": \"rfbTimeout\"}");
  81. }
  82. if (action == RF_CODE_LEARN_OK || action == RF_CODE_RFIN) {
  83. _rfbToChar(&_uartbuf[1], buffer);
  84. mqttSend(MQTT_TOPIC_RFIN, buffer);
  85. _rfbAck();
  86. }
  87. if (action == RF_CODE_LEARN_OK) {
  88. DEBUG_MSG_P(PSTR("[RFBRIDGE] Learn success\n"));
  89. rfbStore(_learnId, _learnStatus, buffer);
  90. // Websocket update
  91. char wsb[100];
  92. sprintf_P(wsb, PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"%s\"}]}"), _learnId, _learnStatus ? 1 : 0, buffer);
  93. wsSend(wsb);
  94. }
  95. if (action == RF_CODE_RFIN) {
  96. DEBUG_MSG_P(PSTR("[RFBRIDGE] Forward message '%s'\n"), buffer);
  97. // Look for the code
  98. unsigned char id, status;
  99. bool found = false;
  100. for (id=0; id<relayCount(); id++) {
  101. for (status=0; status<2; status++) {
  102. String code = rfbRetrieve(id, status == 1);
  103. if (code.length()) {
  104. if (code.endsWith(&buffer[12])) {
  105. found = true;
  106. break;
  107. }
  108. }
  109. }
  110. if (found) break;
  111. }
  112. if (found) {
  113. _rfbin = true;
  114. relayStatus(id, status == 1);
  115. }
  116. }
  117. }
  118. void _rfbReceive() {
  119. static bool receiving = false;
  120. while (Serial.available()) {
  121. yield();
  122. byte c = Serial.read();
  123. //DEBUG_MSG_P(PSTR("[RFBRIDGE] Received 0x%02X\n"), c);
  124. if (receiving) {
  125. if (c == RF_CODE_STOP) {
  126. _rfbDecode();
  127. receiving = false;
  128. } else {
  129. _uartbuf[_uartpos++] = c;
  130. }
  131. } else if (c == RF_CODE_START) {
  132. _uartpos = 0;
  133. receiving = true;
  134. }
  135. }
  136. }
  137. bool _rfbCompare(const char * code1, const char * code2) {
  138. return strcmp(&code1[12], &code2[12]) == 0;
  139. }
  140. bool _rfbSameOnOff(unsigned char id) {
  141. return _rfbCompare(rfbRetrieve(id, true).c_str(), rfbRetrieve(id, false).c_str());
  142. }
  143. /*
  144. From an hexa char array ("A220EE...") to a byte array (half the size)
  145. */
  146. bool _rfbToArray(const char * in, byte * out) {
  147. if (strlen(in) != RF_MESSAGE_SIZE * 2) return false;
  148. char tmp[3] = {0};
  149. for (unsigned char p = 0; p<RF_MESSAGE_SIZE; p++) {
  150. memcpy(tmp, &in[p*2], 2);
  151. out[p] = strtol(tmp, NULL, 16);
  152. }
  153. return true;
  154. }
  155. /*
  156. From a byte array to an hexa char array ("A220EE...", double the size)
  157. */
  158. bool _rfbToChar(byte * in, char * out) {
  159. for (unsigned char p = 0; p<RF_MESSAGE_SIZE; p++) {
  160. sprintf(&out[p*2], "%02X", in[p]);
  161. }
  162. return true;
  163. }
  164. void _rfbMqttCallback(unsigned int type, const char * topic, const char * payload) {
  165. if (type == MQTT_CONNECT_EVENT) {
  166. char buffer[strlen(MQTT_TOPIC_RFLEARN) + 3];
  167. sprintf(buffer, "%s/+", MQTT_TOPIC_RFLEARN);
  168. mqttSubscribe(buffer);
  169. mqttSubscribe(MQTT_TOPIC_RFOUT);
  170. }
  171. if (type == MQTT_MESSAGE_EVENT) {
  172. // Match topic
  173. String t = mqttSubtopic((char *) topic);
  174. // Check if should go into learn mode
  175. if (t.startsWith(MQTT_TOPIC_RFLEARN)) {
  176. _learnId = t.substring(strlen(MQTT_TOPIC_RFLEARN)+1).toInt();
  177. if (_learnId >= relayCount()) {
  178. DEBUG_MSG_P(PSTR("[RFBRIDGE] Wrong learnID (%d)\n"), _learnId);
  179. return;
  180. }
  181. _learnStatus = (char)payload[0] != '0';
  182. _rfbLearn();
  183. }
  184. if (t.equals(MQTT_TOPIC_RFOUT)) {
  185. byte message[RF_MESSAGE_SIZE];
  186. if (_rfbToArray(payload, message)) {
  187. _rfbSend(message, 1);
  188. }
  189. }
  190. }
  191. }
  192. // -----------------------------------------------------------------------------
  193. // PUBLIC
  194. // -----------------------------------------------------------------------------
  195. void rfbStore(unsigned char id, bool status, const char * code) {
  196. DEBUG_MSG_P(PSTR("[RFBRIDGE] Storing %d-%s => '%s'\n"), id, status ? "ON" : "OFF", code);
  197. char key[8] = {0};
  198. sprintf(key, "rfb%d%s", id, status ? "on" : "off");
  199. setSetting(key, code);
  200. }
  201. String rfbRetrieve(unsigned char id, bool status) {
  202. char key[8] = {0};
  203. sprintf(key, "rfb%d%s", id, status ? "on" : "off");
  204. return getSetting(key);
  205. }
  206. void rfbStatus(unsigned char id, bool status) {
  207. String value = rfbRetrieve(id, status);
  208. if (value.length() > 0) {
  209. bool same = _rfbSameOnOff(id);
  210. byte message[RF_MESSAGE_SIZE];
  211. _rfbToArray(value.c_str(), message);
  212. unsigned char times = RF_SEND_TIMES;
  213. if (same) times = _rfbin ? 0 : 1;
  214. _rfbSend(message, times);
  215. }
  216. }
  217. void rfbLearn(unsigned char id, bool status) {
  218. _learnId = id;
  219. _learnStatus = status;
  220. _rfbLearn();
  221. }
  222. void rfbForget(unsigned char id, bool status) {
  223. char key[8] = {0};
  224. sprintf(key, "rfb%d%s", id, status ? "on" : "off");
  225. delSetting(key);
  226. // Websocket update
  227. char wsb[100];
  228. sprintf_P(wsb, PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"\"}]}"), id, status ? 1 : 0);
  229. wsSend(wsb);
  230. }
  231. // -----------------------------------------------------------------------------
  232. // SETUP & LOOP
  233. // -----------------------------------------------------------------------------
  234. void rfbSetup() {
  235. mqttRegister(_rfbMqttCallback);
  236. }
  237. void rfbLoop() {
  238. _rfbReceive();
  239. }
  240. #endif