Skip to content

Commit

Permalink
Add (S)SerialSend features
Browse files Browse the repository at this point in the history
 * Fix mDNS addService (arendst#4938, arendst#4951)
 * Add (S)SerialSend3 escape sequence \x to allow hexadecimal byte value (arendst#3560, arendst#4947)
 * Add SerialBridge command SSerialSend5 <hexdata>
  • Loading branch information
arendst committed Jan 17, 2019
1 parent b1fc4c8 commit c6be3b1
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 22 deletions.
4 changes: 3 additions & 1 deletion sonoff/_changelog.ino
Original file line number Diff line number Diff line change
@@ -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 <hexdata>
*
* 6.4.1.8 20190107
* Change sonoff_template.h layout regarding optional module flags like ADC0
Expand Down
4 changes: 2 additions & 2 deletions sonoff/sonoff.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 20 additions & 7 deletions sonoff/support.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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--;
Expand All @@ -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;
Expand All @@ -247,6 +257,9 @@ char* Unescape(char* buffer, uint16_t* size)
}
}
*size = end_size;

// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)buffer, *size);

return buffer;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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()
Expand Down Expand Up @@ -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++));
}
Expand All @@ -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)
Expand Down
24 changes: 20 additions & 4 deletions sonoff/xdrv_08_serial_bridge.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion sonoff/xnrg_03_pzem004t.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
2 changes: 1 addition & 1 deletion sonoff/xnrg_04_mcp39f501.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion sonoff/xnrg_05_pzem_ac.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion sonoff/xnrg_06_pzem_dc.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion sonoff/xsns_15_mhz19.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
2 changes: 1 addition & 1 deletion sonoff/xsns_18_pms5003.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
2 changes: 1 addition & 1 deletion sonoff/xsns_20_novasds.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sonoff/xsns_38_az7798.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit c6be3b1

Please sign in to comment.