diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 27b47a02f..2d77e925f 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,7 +1,9 @@ /* 6.4.1.9 20190115 * Add support for Mi LED Desk Lamp with rotary switch (#4887) - * Fix mDNS addService (#4938) + * Fix mDNS addService (#4938, #4951) * Fix allowable MAX_RULE_VARS to 16 (#4933) + * Add (S)SerialSend3 escape sequence \x to allow hexadecimal byte value (#3560, #4947) + * Add SerialBridge command SSerialSend5 * * 6.4.1.8 20190107 * Change sonoff_template.h layout regarding optional module flags like ADC0 diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index f16a3ba10..a4fe3bf38 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -1051,7 +1051,7 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len) Serial.printf("%s", Unescape(dataBuf, &dat_len)); // "Hello\f" } else if (5 == index) { - SerialSendRaw(RemoveSpace(dataBuf)); // "AA004566" + SerialSendRaw(RemoveSpace(dataBuf)); // "AA004566" as hex values } snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_JSON_DONE); } @@ -1427,7 +1427,7 @@ void ExecuteCommandPower(byte device, byte state, int source) interlock_mutex = 0; } } - if ( Settings.flag.interlock && !interlock_mutex && !Settings.flag3.split_interlock) { //execute regular interlock-mode as interlock-split is off + if ( Settings.flag.interlock && !interlock_mutex && !Settings.flag3.split_interlock) { //execute regular interlock-mode as interlock-split is off interlock_mutex = 1; for (byte i = 0; i < devices_present; i++) { power_t imask = 1 << i; diff --git a/sonoff/support.ino b/sonoff/support.ino index 55be05df7..148ae8783 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -210,10 +210,12 @@ char* Unescape(char* buffer, uint16_t* size) { uint8_t* read = (uint8_t*)buffer; uint8_t* write = (uint8_t*)buffer; - uint16_t start_size = *size; - uint16_t end_size = *size; + int16_t start_size = *size; + int16_t end_size = *size; uint8_t che = 0; +// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)buffer, *size); + while (start_size > 0) { uint8_t ch = *read++; start_size--; @@ -235,6 +237,14 @@ char* Unescape(char* buffer, uint16_t* size) case 's': che = ' '; break; // 20 Space case 't': che = '\t'; break; // 09 Horizontal tab case 'v': che = '\v'; break; // 0B Vertical tab + case 'x': { + uint8_t* start = read; + che = (uint8_t)strtol((const char*)read, (char**)&read, 16); + start_size -= (uint16_t)(read - start); + end_size -= (uint16_t)(read - start); + break; + } + case '"': che = '\"'; break; // 22 Quotation mark // case '?': che = '\?'; break; // 3F Question mark default : { che = chi; @@ -247,6 +257,9 @@ char* Unescape(char* buffer, uint16_t* size) } } *size = end_size; + +// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)buffer, *size); + return buffer; } @@ -511,7 +524,7 @@ void ModuleGpios(myio *gp) memcpy_P(&src, &kModules[Settings.module].gp, sizeof(mycfgio)); // 11 85 00 85 85 00 00 00 15 38 85 00 00 81 -// AddLogSerial(LOG_LEVEL_DEBUG, (uint8_t *)&src, sizeof(mycfgio)); +// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t *)&src, sizeof(mycfgio)); for (uint8_t i = 0; i < sizeof(mycfgio); i++) { if (i < 6) { @@ -526,7 +539,7 @@ void ModuleGpios(myio *gp) } // 11 85 00 85 85 00 00 00 00 00 00 00 15 38 85 00 00 81 -// AddLogSerial(LOG_LEVEL_DEBUG, (uint8_t *)gp, sizeof(myio)); +// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t *)gp, sizeof(myio)); } gpio_flag ModuleFlag() @@ -1197,9 +1210,9 @@ void AddLog_P(byte loglevel, const char *formatP, const char *formatP2) AddLog(loglevel); } -void AddLogSerial(byte loglevel, uint8_t *buffer, int count) +void AddLogBuffer(byte loglevel, uint8_t *buffer, int count) { - snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SERIAL D_RECEIVED)); + snprintf_P(log_data, sizeof(log_data), PSTR("DMP:")); for (int i = 0; i < count; i++) { snprintf_P(log_data, sizeof(log_data), PSTR("%s %02X"), log_data, *(buffer++)); } @@ -1208,7 +1221,7 @@ void AddLogSerial(byte loglevel, uint8_t *buffer, int count) void AddLogSerial(byte loglevel) { - AddLogSerial(loglevel, (uint8_t*)serial_in_buffer, serial_in_byte_counter); + AddLogBuffer(loglevel, (uint8_t*)serial_in_buffer, serial_in_byte_counter); } void AddLogMissed(char *sensor, uint8_t misses) diff --git a/sonoff/xdrv_08_serial_bridge.ino b/sonoff/xdrv_08_serial_bridge.ino index 08a4f7211..80ec37178 100644 --- a/sonoff/xdrv_08_serial_bridge.ino +++ b/sonoff/xdrv_08_serial_bridge.ino @@ -99,18 +99,34 @@ boolean SerialBridgeCommand(void) if (-1 == command_code) { serviced = false; // Unknown command } - else if ((CMND_SSERIALSEND == command_code) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= 3)) { - if (XdrvMailbox.data_len > 0) { + else if ((CMND_SSERIALSEND == command_code) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= 5)) { + if (XdrvMailbox.data_len > 0) { // "Hello Tiger\n" if (1 == XdrvMailbox.index) { SerialBridgeSerial->write(XdrvMailbox.data, XdrvMailbox.data_len); SerialBridgeSerial->write("\n"); } - else if (2 == XdrvMailbox.index) { + else if ((2 == XdrvMailbox.index) || (4 == XdrvMailbox.index)) { // "Hello Tiger" or "A0" SerialBridgeSerial->write(XdrvMailbox.data, XdrvMailbox.data_len); } - else if (3 == XdrvMailbox.index) { + else if (3 == XdrvMailbox.index) { // "Hello\f" SerialBridgeSerial->write(Unescape(XdrvMailbox.data, &XdrvMailbox.data_len), XdrvMailbox.data_len); } + else if (5 == XdrvMailbox.index) { // "AA004566" as hex values + char *p; + char stemp[3]; + uint8_t code; + + char *codes = RemoveSpace(XdrvMailbox.data); + int size = strlen(XdrvMailbox.data); + + while (size > 0) { + snprintf(stemp, sizeof(stemp), codes); + code = strtol(stemp, &p, 16); + SerialBridgeSerial->write(code); + size -= 2; + codes += 2; + } + } snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_JSON_DONE); } } diff --git a/sonoff/xnrg_03_pzem004t.ino b/sonoff/xnrg_03_pzem004t.ino index 846c14431..34bff43fe 100644 --- a/sonoff/xnrg_03_pzem004t.ino +++ b/sonoff/xnrg_03_pzem004t.ino @@ -122,7 +122,7 @@ bool PzemRecieve(uint8_t resp, float *data) } } - AddLogSerial(LOG_LEVEL_DEBUG_MORE, buffer, len); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, len); if (len != sizeof(PZEMCommand)) { // AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "Pzem comms timeout")); diff --git a/sonoff/xnrg_04_mcp39f501.ino b/sonoff/xnrg_04_mcp39f501.ino index 0301bf040..1ff04fb7d 100644 --- a/sonoff/xnrg_04_mcp39f501.ino +++ b/sonoff/xnrg_04_mcp39f501.ino @@ -143,7 +143,7 @@ void McpSend(uint8_t *data) data[0] = MCP_START_FRAME; data[data[1] -1] = McpChecksum(data); -// AddLogSerial(LOG_LEVEL_DEBUG_MORE, data, data[1]); +// AddLogBuffer(LOG_LEVEL_DEBUG_MORE, data, data[1]); for (byte i = 0; i < data[1]; i++) { Serial.write(data[i]); diff --git a/sonoff/xnrg_05_pzem_ac.ino b/sonoff/xnrg_05_pzem_ac.ino index a171b4079..f01ebad3e 100644 --- a/sonoff/xnrg_05_pzem_ac.ino +++ b/sonoff/xnrg_05_pzem_ac.ino @@ -46,7 +46,7 @@ void PzemAcEverySecond(void) uint8_t buffer[26]; uint8_t error = PzemAcModbus->ReceiveBuffer(buffer, 10); - AddLogSerial(LOG_LEVEL_DEBUG_MORE, buffer, (buffer[2]) ? buffer[2] +5 : sizeof(buffer)); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, (buffer[2]) ? buffer[2] +5 : sizeof(buffer)); if (error) { snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "PzemAc response error %d"), error); diff --git a/sonoff/xnrg_06_pzem_dc.ino b/sonoff/xnrg_06_pzem_dc.ino index 2adda5092..536cb4b97 100644 --- a/sonoff/xnrg_06_pzem_dc.ino +++ b/sonoff/xnrg_06_pzem_dc.ino @@ -46,7 +46,7 @@ void PzemDcEverySecond(void) uint8_t buffer[22]; uint8_t error = PzemDcModbus->ReceiveBuffer(buffer, 8); - AddLogSerial(LOG_LEVEL_DEBUG_MORE, buffer, (buffer[2]) ? buffer[2] +5 : sizeof(buffer)); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, (buffer[2]) ? buffer[2] +5 : sizeof(buffer)); if (error) { snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "PzemDc response error %d"), error); diff --git a/sonoff/xsns_15_mhz19.ino b/sonoff/xsns_15_mhz19.ino index d04d5a0c5..bc8e46ada 100644 --- a/sonoff/xsns_15_mhz19.ino +++ b/sonoff/xsns_15_mhz19.ino @@ -198,7 +198,7 @@ void MhzEverySecond(void) } } - AddLogSerial(LOG_LEVEL_DEBUG_MORE, mhz_response, counter); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, mhz_response, counter); if (counter < 9) { // AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "MH-Z19 comms timeout")); diff --git a/sonoff/xsns_18_pms5003.ino b/sonoff/xsns_18_pms5003.ino index 50ad9f7af..1f048134a 100644 --- a/sonoff/xsns_18_pms5003.ino +++ b/sonoff/xsns_18_pms5003.ino @@ -62,7 +62,7 @@ boolean PmsReadData(void) PmsSerial->readBytes(buffer, 32); PmsSerial->flush(); // Make room for another burst - AddLogSerial(LOG_LEVEL_DEBUG_MORE, buffer, 32); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, 32); // get checksum ready for (uint8_t i = 0; i < 30; i++) { diff --git a/sonoff/xsns_20_novasds.ino b/sonoff/xsns_20_novasds.ino index e3e1e48e9..1f94dea6a 100644 --- a/sonoff/xsns_20_novasds.ino +++ b/sonoff/xsns_20_novasds.ino @@ -108,7 +108,7 @@ bool NovaSdsCommand(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint16_t sensor // read rest (9 of 10 bytes) of message NovaSdsSerial->readBytes(&recbuf[1], 9); - AddLogSerial(LOG_LEVEL_DEBUG_MORE, recbuf, sizeof(recbuf)); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, recbuf, sizeof(recbuf)); if ( NULL != buffer ) { // return data to buffer diff --git a/sonoff/xsns_38_az7798.ino b/sonoff/xsns_38_az7798.ino index d53b657c7..433b380b0 100644 --- a/sonoff/xsns_38_az7798.ino +++ b/sonoff/xsns_38_az7798.ino @@ -158,7 +158,7 @@ void AzEverySecond(void) } } while(((millis() - start) < AZ_READ_TIMEOUT) && (counter < sizeof(az_response)) && !az_received); - AddLogSerial(LOG_LEVEL_DEBUG_MORE, az_response, counter); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, az_response, counter); if (!az_received) { AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "AZ7798 comms timeout"));