Browse Source

Fix overflow in relayParsePayload with messages bigger than 255 chars

i18n
Xose Pérez 6 years ago
parent
commit
fd0223698b
1 changed files with 40 additions and 34 deletions
  1. +40
    -34
      code/espurna/relay.ino

+ 40
- 34
code/espurna/relay.ino View File

@ -276,16 +276,21 @@ unsigned char relayParsePayload(const char * payload) {
// Payload could be "OFF", "ON", "TOGGLE"
// or its number equivalents: 0, 1 or 2
if (payload[0] == '0') return 0;
if (payload[0] == '1') return 1;
if (payload[0] == '2') return 2;
// trim payload
char * p = ltrim((char *)payload);
// to lower
for (unsigned char i=0; i<strlen(p); i++) {
unsigned int l = strlen(p);
if (l>6) l=6;
for (unsigned char i=0; i<l; i++) {
p[i] = tolower(p[i]);
}
unsigned int value;
unsigned int value = 0xFF;
if (strcmp(p, "off") == 0) {
value = 0;
} else if (strcmp(p, "on") == 0) {
@ -294,12 +299,9 @@ unsigned char relayParsePayload(const char * payload) {
value = 2;
} else if (strcmp(p, "query") == 0) {
value = 3;
} else {
value = p[0] - '0';
}
if (0 <= value && value <=3) return value;
return 0xFF;
return value;
}
@ -583,43 +585,47 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
if (type == MQTT_MESSAGE_EVENT) {
// Get value
unsigned char value = relayParsePayload(payload);
if (value == 0xFF) {
DEBUG_MSG_P(PSTR("[RELAY] Wrong payload (%s)\n"), payload);
// Check relay topic
String t = mqttSubtopic((char *) topic);
if (t.startsWith(MQTT_TOPIC_RELAY)) {
// Get value
unsigned char value = relayParsePayload(payload);
if (value == 0xFF) return;
// Get relay ID
unsigned int id = t.substring(strlen(MQTT_TOPIC_RELAY)+1).toInt();
if (id >= relayCount()) {
DEBUG_MSG_P(PSTR("[RELAY] Wrong relayID (%d)\n"), id);
} else {
relayStatusWrap(id, value, false);
}
return;
}
// Check group topics
bool found = false;
for (unsigned int i=0; i < _relays.size(); i++) {
String t = getSetting("mqttGroup", i, "");
if (t.equals(topic)) {
unsigned char local_value = value;
if (getSetting("mqttGroupInv", i, 0).toInt() == 1) {
if (local_value < 2) local_value = 1 - local_value;
}
found = true;
DEBUG_MSG_P(PSTR("[RELAY] Matched group topic for relayID %d\n"), i);
relayStatusWrap(i, local_value, true);
}
}
// If found as group topic quit
if (found) return;
if ((t.length() > 0) && t.equals(topic)) {
// Else, try to match topic
String t = mqttSubtopic((char *) topic);
if (!t.startsWith(MQTT_TOPIC_RELAY)) return;
unsigned char value = relayParsePayload(payload);
if (value == 0xFF) return;
// Get relay ID
unsigned int id = t.substring(strlen(MQTT_TOPIC_RELAY)+1).toInt();
if (id >= relayCount()) {
DEBUG_MSG_P(PSTR("[RELAY] Wrong relayID (%d)\n"), id);
return;
}
if (value < 2) {
if (getSetting("mqttGroupInv", i, 0).toInt() == 1) {
value = 1 - value;
}
}
DEBUG_MSG_P(PSTR("[RELAY] Matched group topic for relayID %d\n"), i);
relayStatusWrap(i, value, true);
relayStatusWrap(id, value, false);
}
}
}


Loading…
Cancel
Save