Add (S)SerialSend features

* Fix mDNS addService (#4938, #4951)
 * Add (S)SerialSend3 escape sequence \x to allow hexadecimal byte value (#3560, #4947)
 * Add SerialBridge command SSerialSend5 <hexdata>
This commit is contained in:
Theo Arends 2019-01-17 17:48:34 +01:00
parent be23f905ae
commit e7e378f463
12 changed files with 53 additions and 22 deletions

View File

@ -1,7 +1,9 @@
/* 6.4.1.9 20190115 /* 6.4.1.9 20190115
* Add support for Mi LED Desk Lamp with rotary switch (#4887) * 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) * 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 <hexdata>
* *
* 6.4.1.8 20190107 * 6.4.1.8 20190107
* Change sonoff_template.h layout regarding optional module flags like ADC0 * Change sonoff_template.h layout regarding optional module flags like ADC0

View File

@ -1051,7 +1051,7 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len)
Serial.printf("%s", Unescape(dataBuf, &dat_len)); // "Hello\f" Serial.printf("%s", Unescape(dataBuf, &dat_len)); // "Hello\f"
} }
else if (5 == index) { 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); 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; 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; interlock_mutex = 1;
for (byte i = 0; i < devices_present; i++) { for (byte i = 0; i < devices_present; i++) {
power_t imask = 1 << i; power_t imask = 1 << i;

View File

@ -210,10 +210,12 @@ char* Unescape(char* buffer, uint16_t* size)
{ {
uint8_t* read = (uint8_t*)buffer; uint8_t* read = (uint8_t*)buffer;
uint8_t* write = (uint8_t*)buffer; uint8_t* write = (uint8_t*)buffer;
uint16_t start_size = *size; int16_t start_size = *size;
uint16_t end_size = *size; int16_t end_size = *size;
uint8_t che = 0; uint8_t che = 0;
// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)buffer, *size);
while (start_size > 0) { while (start_size > 0) {
uint8_t ch = *read++; uint8_t ch = *read++;
start_size--; start_size--;
@ -235,6 +237,14 @@ char* Unescape(char* buffer, uint16_t* size)
case 's': che = ' '; break; // 20 Space case 's': che = ' '; break; // 20 Space
case 't': che = '\t'; break; // 09 Horizontal tab case 't': che = '\t'; break; // 09 Horizontal tab
case 'v': che = '\v'; break; // 0B Vertical 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 // case '?': che = '\?'; break; // 3F Question mark
default : { default : {
che = chi; che = chi;
@ -247,6 +257,9 @@ char* Unescape(char* buffer, uint16_t* size)
} }
} }
*size = end_size; *size = end_size;
// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)buffer, *size);
return buffer; return buffer;
} }
@ -511,7 +524,7 @@ void ModuleGpios(myio *gp)
memcpy_P(&src, &kModules[Settings.module].gp, sizeof(mycfgio)); memcpy_P(&src, &kModules[Settings.module].gp, sizeof(mycfgio));
// 11 85 00 85 85 00 00 00 15 38 85 00 00 81 // 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++) { for (uint8_t i = 0; i < sizeof(mycfgio); i++) {
if (i < 6) { 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 // 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() gpio_flag ModuleFlag()
@ -1197,9 +1210,9 @@ void AddLog_P(byte loglevel, const char *formatP, const char *formatP2)
AddLog(loglevel); 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++) { for (int i = 0; i < count; i++) {
snprintf_P(log_data, sizeof(log_data), PSTR("%s %02X"), log_data, *(buffer++)); 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) 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) void AddLogMissed(char *sensor, uint8_t misses)

View File

@ -99,18 +99,34 @@ boolean SerialBridgeCommand(void)
if (-1 == command_code) { if (-1 == command_code) {
serviced = false; // Unknown command serviced = false; // Unknown command
} }
else if ((CMND_SSERIALSEND == command_code) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= 3)) { else if ((CMND_SSERIALSEND == command_code) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= 5)) {
if (XdrvMailbox.data_len > 0) { if (XdrvMailbox.data_len > 0) { // "Hello Tiger\n"
if (1 == XdrvMailbox.index) { if (1 == XdrvMailbox.index) {
SerialBridgeSerial->write(XdrvMailbox.data, XdrvMailbox.data_len); SerialBridgeSerial->write(XdrvMailbox.data, XdrvMailbox.data_len);
SerialBridgeSerial->write("\n"); 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); 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); 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); snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_JSON_DONE);
} }
} }

View File

@ -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)) { if (len != sizeof(PZEMCommand)) {
// AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "Pzem comms timeout")); // AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "Pzem comms timeout"));

View File

@ -143,7 +143,7 @@ void McpSend(uint8_t *data)
data[0] = MCP_START_FRAME; data[0] = MCP_START_FRAME;
data[data[1] -1] = McpChecksum(data); 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++) { for (byte i = 0; i < data[1]; i++) {
Serial.write(data[i]); Serial.write(data[i]);

View File

@ -46,7 +46,7 @@ void PzemAcEverySecond(void)
uint8_t buffer[26]; uint8_t buffer[26];
uint8_t error = PzemAcModbus->ReceiveBuffer(buffer, 10); 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) { if (error) {
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "PzemAc response error %d"), error); snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "PzemAc response error %d"), error);

View File

@ -46,7 +46,7 @@ void PzemDcEverySecond(void)
uint8_t buffer[22]; uint8_t buffer[22];
uint8_t error = PzemDcModbus->ReceiveBuffer(buffer, 8); 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) { if (error) {
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "PzemDc response error %d"), error); snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "PzemDc response error %d"), error);

View File

@ -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) { if (counter < 9) {
// AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "MH-Z19 comms timeout")); // AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "MH-Z19 comms timeout"));

View File

@ -62,7 +62,7 @@ boolean PmsReadData(void)
PmsSerial->readBytes(buffer, 32); PmsSerial->readBytes(buffer, 32);
PmsSerial->flush(); // Make room for another burst PmsSerial->flush(); // Make room for another burst
AddLogSerial(LOG_LEVEL_DEBUG_MORE, buffer, 32); AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, 32);
// get checksum ready // get checksum ready
for (uint8_t i = 0; i < 30; i++) { for (uint8_t i = 0; i < 30; i++) {

View File

@ -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 // read rest (9 of 10 bytes) of message
NovaSdsSerial->readBytes(&recbuf[1], 9); NovaSdsSerial->readBytes(&recbuf[1], 9);
AddLogSerial(LOG_LEVEL_DEBUG_MORE, recbuf, sizeof(recbuf)); AddLogBuffer(LOG_LEVEL_DEBUG_MORE, recbuf, sizeof(recbuf));
if ( NULL != buffer ) { if ( NULL != buffer ) {
// return data to buffer // return data to buffer

View File

@ -158,7 +158,7 @@ void AzEverySecond(void)
} }
} while(((millis() - start) < AZ_READ_TIMEOUT) && (counter < sizeof(az_response)) && !az_received); } 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) { if (!az_received) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "AZ7798 comms timeout")); AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "AZ7798 comms timeout"));