/* ITEAD RF BRIDGE MODULE Copyright (C) 2017 by Xose PĂ©rez */ #ifdef ITEAD_SONOFF_RFBRIDGE #include #include // ----------------------------------------------------------------------------- // DEFINITIONS // ----------------------------------------------------------------------------- #define RF_MESSAGE_SIZE 9 #define RF_MAX_MESSAGE_SIZE (112+4) #define RF_CODE_START 0xAA #define RF_CODE_ACK 0xA0 #define RF_CODE_LEARN 0xA1 #define RF_CODE_LEARN_KO 0xA2 #define RF_CODE_LEARN_OK 0xA3 #define RF_CODE_RFIN 0xA4 #define RF_CODE_RFOUT 0xA5 #define RF_CODE_SNIFFING_ON 0xA6 #define RF_CODE_SNIFFING_OFF 0xA7 #define RF_CODE_RFOUT_NEW 0xA8 #define RF_CODE_LEARN_NEW 0xA9 #define RF_CODE_LEARN_KO_NEW 0xAA #define RF_CODE_LEARN_OK_NEW 0xAB #define RF_CODE_RFOUT_BUCKET 0xB0 #define RF_CODE_STOP 0x55 // ----------------------------------------------------------------------------- // GLOBALS TO THE MODULE // ----------------------------------------------------------------------------- unsigned char _uartbuf[RF_MESSAGE_SIZE+3] = {0}; unsigned char _uartpos = 0; unsigned char _learnId = 0; bool _learnStatus = true; bool _rfbin = false; typedef struct { byte code[RF_MESSAGE_SIZE]; byte times; } rfb_message_t; std::vector _rfb_message_queue; Ticker _rfbTicker; // ----------------------------------------------------------------------------- // PRIVATES // ----------------------------------------------------------------------------- /* From an hexa char array ("A220EE...") to a byte array (half the size) */ static int _rfbToArray(const char * in, byte * out, int length = RF_MESSAGE_SIZE * 2) { int n = strlen(in); if (n > RF_MAX_MESSAGE_SIZE*2 || (length > 0 && n != length)) return 0; char tmp[3] = {0,0,0}; n /= 2; for (unsigned char p = 0; p()); } void _rfbAck() { DEBUG_MSG_P(PSTR("[RFBRIDGE] Sending ACK\n")); Serial.println(); Serial.write(RF_CODE_START); Serial.write(RF_CODE_ACK); Serial.write(RF_CODE_STOP); Serial.flush(); Serial.println(); } void _rfbLearn() { DEBUG_MSG_P(PSTR("[RFBRIDGE] Sending LEARN\n")); Serial.println(); Serial.write(RF_CODE_START); Serial.write(RF_CODE_LEARN); Serial.write(RF_CODE_STOP); Serial.flush(); Serial.println(); #if WEB_SUPPORT char buffer[100]; snprintf_P(buffer, sizeof(buffer), PSTR("{\"action\": \"rfbLearn\", \"data\":{\"id\": %d, \"status\": %d}}"), _learnId, _learnStatus ? 1 : 0); wsSend(buffer); #endif } void _rfbSendRaw(const byte *message, const unsigned char n = RF_MESSAGE_SIZE) { for (unsigned char j=0; j 1) { message.times = message.times - 1; _rfb_message_queue.push_back(message); } // if there are still messages in the queue... if (_rfb_message_queue.size() > 0) { _rfbTicker.once_ms(RF_SEND_DELAY, _rfbSend); } } void _rfbSend(byte * code, int times) { char buffer[RF_MESSAGE_SIZE]; _rfbToChar(code, buffer); DEBUG_MSG_P(PSTR("[RFBRIDGE] Sending MESSAGE '%s' %d time(s)\n"), buffer, times); rfb_message_t message; memcpy(message.code, code, RF_MESSAGE_SIZE); message.times = times; _rfb_message_queue.push_back(message); _rfbSend(); } #ifdef RF_RAW_SUPPORT void _rfbSendRawOnce(byte *code, int length) { char buffer[length*2]; _rfbToChar(code, buffer, length); DEBUG_MSG_P(PSTR("[RFBRIDGE] Sending raw MESSAGE '%s'\n"), buffer); _rfbSendRaw(code, length); } #endif bool _rfbMatch(char * code, unsigned char& relayID, unsigned char& value) { if (strlen(code) != 18) return false; bool found = false; String compareto = String(&code[12]); compareto.toUpperCase(); DEBUG_MSG_P(PSTR("[RFBRIDGE] Trying to match code %s\n"), compareto.c_str()); for (unsigned char i=0; i= relayCount()) { DEBUG_MSG_P(PSTR("[RFBRIDGE] Wrong learnID (%d)\n"), _learnId); return; } _learnStatus = (char)payload[0] != '0'; _rfbLearn(); } #ifdef RF_RAW_SUPPORT bool isRFOut = t.equals(MQTT_TOPIC_RFOUT); bool isRFRaw = !isRFOut && t.equals(MQTT_TOPIC_RFRAW); if (isRFOut || isRFRaw) { byte message[RF_MAX_MESSAGE_SIZE]; #else if (t.equals(MQTT_TOPIC_RFOUT)) { byte message[RF_MESSAGE_SIZE]; #endif // The payload may be a code in HEX format ([0-9A-Z]{18}) or // the code comma the number of times to transmit it. char * tok = strtok((char *) payload, ","); // Check if a switch is linked to that message unsigned char id; unsigned char status = 0; if (_rfbMatch(tok, id, status)) { if (status == 2) { relayToggle(id); } else { relayStatus(id, status == 1); } return; } #ifdef RF_RAW_SUPPORT int len = _rfbToArray(tok, message, 0); if (len > 0 && (isRFRaw || len != RF_MESSAGE_SIZE)) { _rfbSendRawOnce(message, len); } else { #else if (_rfbToArray(tok, message)) { #endif tok = strtok(NULL, ","); byte times = (tok != NULL) ? atoi(tok) : 1; _rfbSend(message, times); } } } } #endif // ----------------------------------------------------------------------------- // PUBLIC // ----------------------------------------------------------------------------- void rfbStore(unsigned char id, bool status, const char * code) { DEBUG_MSG_P(PSTR("[RFBRIDGE] Storing %d-%s => '%s'\n"), id, status ? "ON" : "OFF", code); char key[8] = {0}; snprintf_P(key, sizeof(key), PSTR("rfb%s%d"), status ? "ON" : "OFF", id); setSetting(key, code); } String rfbRetrieve(unsigned char id, bool status) { char key[8] = {0}; snprintf_P(key, sizeof(key), PSTR("rfb%s%d"), status ? "ON" : "OFF", id); return getSetting(key); } void rfbStatus(unsigned char id, bool status) { String value = rfbRetrieve(id, status); if (value.length() > 0) { bool same = _rfbSameOnOff(id); #ifdef RF_RAW_SUPPORT byte message[RF_MAX_MESSAGE_SIZE]; int len = _rfbToArray(value.c_str(), message, 0); if (len == RF_MESSAGE_SIZE && // probably a standard msg (message[0] != RF_CODE_START || // raw would start with 0xAA message[1] != RF_CODE_RFOUT_BUCKET || // followed by 0xB0, message[2] + 4 != len || // needs a valid length, message[len-1] != RF_CODE_STOP)) { // and finish with 0x55 #else byte message[RF_MESSAGE_SIZE]; _rfbToArray(value.c_str(), message); #endif unsigned char times = RF_SEND_TIMES; if (same) times = _rfbin ? 0 : 1; _rfbSend(message, times); #ifdef RF_RAW_SUPPORT } else { _rfbSendRawOnce(message, len); // send a raw message } #endif } } void rfbLearn(unsigned char id, bool status) { _learnId = id; _learnStatus = status; _rfbLearn(); } void rfbForget(unsigned char id, bool status) { char key[8] = {0}; snprintf_P(key, sizeof(key), PSTR("rfb%s%d"), status ? "ON" : "OFF", id); delSetting(key); // Websocket update #if WEB_SUPPORT char wsb[100]; snprintf_P(wsb, sizeof(wsb), PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"\"}]}"), id, status ? 1 : 0); wsSend(wsb); #endif } // ----------------------------------------------------------------------------- // SETUP & LOOP // ----------------------------------------------------------------------------- void rfbSetup() { #if MQTT_SUPPORT mqttRegister(_rfbMqttCallback); #endif #if WEB_SUPPORT wsOnSendRegister(_rfbWebSocketOnSend); wsOnActionRegister(_rfbWebSocketOnAction); #endif } void rfbLoop() { _rfbReceive(); } #endif