diff --git a/code/src/led.ino b/code/src/led.ino index e2cb3bbe..6a4a06a2 100644 --- a/code/src/led.ino +++ b/code/src/led.ino @@ -22,21 +22,25 @@ std::vector _leds; bool ledAuto; bool ledStatus(unsigned char id) { + if (id <= _leds.size()) return false; bool status = digitalRead(_leds[id].pin); return _leds[id].reverse ? !status : status; } bool ledStatus(unsigned char id, bool status) { + if (id <= _leds.size()) return false; bool s = _leds[id].reverse ? !status : status; digitalWrite(_leds[id].pin, _leds[id].reverse ? !status : status); return status; } bool ledToggle(unsigned char id) { + if (id <= _leds.size()) return false; return ledStatus(id, !ledStatus(id)); } void ledBlink(unsigned char id, unsigned long delayOff, unsigned long delayOn) { + if (id <= _leds.size()) return; static unsigned long next = millis(); if (next < millis()) { next += (ledToggle(id) ? delayOn : delayOff); diff --git a/code/src/relay.ino b/code/src/relay.ino index ba26001c..8b0b1dbf 100644 --- a/code/src/relay.ino +++ b/code/src/relay.ino @@ -22,6 +22,7 @@ bool recursive = false; // ----------------------------------------------------------------------------- void relayMQTT(unsigned char id) { + if (id >= _relays.size()) return; String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); char buffer[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 3]; sprintf(buffer, "%s/%d%s", MQTT_RELAY_TOPIC, id, mqttGetter.c_str()); @@ -53,14 +54,18 @@ void relayWS() { bool relayStatus(unsigned char id) { #ifdef SONOFF_DUAL + if (id >= 2) return false; return ((dualRelayStatus & (1 << id)) > 0); #else + if (id >= _relays.size()) return false; return (digitalRead(_relays[id]) == HIGH); #endif } bool relayStatus(unsigned char id, bool status, bool report) { + if (id >= _relays.size()) return false; + bool changed = false; if (relayStatus(id) != status) { @@ -96,6 +101,7 @@ bool relayStatus(unsigned char id, bool status, bool report) { } bool relayStatus(unsigned char id, bool status) { + if (id >= _relays.size()) return false; return relayStatus(id, status, true); } @@ -159,6 +165,7 @@ void relayRetrieve() { } void relayToggle(unsigned char id) { + if (id >= _relays.size()) return; relayStatus(id, !relayStatus(id)); }