diff --git a/NodeManager.cpp b/NodeManager.cpp index 06ead41a0059aefcc4f67ef98c6e1e860e70948c..1e32a47825f9610fd53b2338a25ce456b9f5a10c 100644 --- a/NodeManager.cpp +++ b/NodeManager.cpp @@ -2747,6 +2747,7 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) { // attach a built-in or custom sensor to this manager int NodeManager::registerSensor(Sensor* sensor) { + if (sensor->getChildId() > MAX_SENSORS) return; #if DEBUG == 1 Serial.print(F("REG I=")); Serial.print(sensor->getChildId()); @@ -2769,12 +2770,14 @@ int NodeManager::registerSensor(Sensor* sensor) { // un-register a sensor to this manager void NodeManager::unRegisterSensor(int sensor_index) { + if (sensor_index > MAX_SENSORS) return; // unlink the pointer to this sensor _sensors[sensor_index] == 0; } // return a sensor given its index Sensor* NodeManager::get(int child_id) { + if (child_id > MAX_SENSORS) return 0; // return a pointer to the sensor from the given child_id return _sensors[child_id]; } @@ -2784,6 +2787,7 @@ Sensor* NodeManager::getSensor(int child_id) { // assign a different child id to a sensor' bool NodeManager::renameSensor(int old_child_id, int new_child_id) { + if (old_child_id > MAX_SENSORS || new_child_id > MAX_SENSORS) return; // ensure the old id exists and the new is available if (_sensors[old_child_id] == 0 || _sensors[new_child_id] != 0) return false; // assign the sensor to new id @@ -2851,7 +2855,7 @@ void NodeManager::before() { _battery_report_timer.start(); #endif // setup individual sensors - for (int i = 0; i < MAX_SENSORS; i++) { + for (int i = 1; i <= MAX_SENSORS; i++) { if (_sensors[i] == 0) continue; // call each sensor's setup() _sensors[i]->before(); @@ -2875,7 +2879,7 @@ void NodeManager::presentation() { batteryReport(); #endif // present each sensor - for (int i = 0; i < MAX_SENSORS; i++) { + for (int i = 1; i <= MAX_SENSORS; i++) { if (_sensors[i] == 0) continue; // call each sensor's presentation() if (_sleep_between_send > 0) sleep(_sleep_between_send); @@ -2902,7 +2906,7 @@ void NodeManager::setup() { _send(_msg.set("STARTED")); #endif // run setup for all the registered sensors - for (int i = 0; i < MAX_SENSORS; i++) { + for (int i = 1; i <= MAX_SENSORS; i++) { if (_sensors[i] == 0) continue; // call each sensor's setup() _sensors[i]->setup(); @@ -2935,7 +2939,7 @@ void NodeManager::loop() { if (_auto_power_pins) powerOn(); #endif // run loop for all the registered sensors - for (int i = 0; i < MAX_SENSORS; i++) { + for (int i = 1; i <= MAX_SENSORS; i++) { // skip not configured sensors if (_sensors[i] == 0) continue; // if waking up from an interrupt skip all the sensor without that interrupt configured @@ -2975,7 +2979,7 @@ void NodeManager::receive(const MyMessage &message) { #endif } // dispatch the message to the registered sensor - else if (_sensors[message.sensor] != 0) { + else if (message.sensor <= MAX_SENSORS && _sensors[message.sensor] != 0) { #if POWER_MANAGER == 1 // turn on the pin powering all the sensors if (_auto_power_pins) powerOn(); @@ -3285,12 +3289,13 @@ void NodeManager::_present(int child_id, int type) { // return the next available child_id int NodeManager::_getAvailableChildId() { - for (int i = 1; i < MAX_SENSORS; i++) { + for (int i = 1; i <= MAX_SENSORS; i++) { if (i == CONFIGURATION_CHILD_ID) continue; if (i == BATTERY_CHILD_ID) continue; // empty place, return it if (_sensors[i] == 0) return i; } + return MAX_SENSORS; } // guess the initial value of a digital output based on the configured interrupt mode diff --git a/NodeManager.h b/NodeManager.h index 7e752f69a0a138f13718bc1667742346025d8b3f..ab3f84166980eeb7c6d3f6fb81e359b1a926aac8 100644 --- a/NodeManager.h +++ b/NodeManager.h @@ -1295,7 +1295,7 @@ class NodeManager { int _interrupt_2_pull = -1; int _last_interrupt_pin = -1; long _timestamp = -1; - Sensor* _sensors[MAX_SENSORS] = {0}; + Sensor* _sensors[MAX_SENSORS+1] = {0}; bool _ack = false; void _sleep(); void _present(int child_id, int type);