From b10817880dbe88d994babdc307276cf9f1463875 Mon Sep 17 00:00:00 2001 From: user2684 Date: Sun, 2 Jul 2017 19:04:49 +0200 Subject: [PATCH] Release v1.5.1 * Decreased the number of items of the _sensors array #117 * Fixed gateway crashing on node check-in --- NodeManager.cpp | 17 +++++++++++------ NodeManager.h | 37 ++++++++++++++++++++----------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/NodeManager.cpp b/NodeManager.cpp index 0624c7a0..6471fd0e 100644 --- a/NodeManager.cpp +++ b/NodeManager.cpp @@ -2093,6 +2093,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()); @@ -2115,12 +2116,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]; } @@ -2130,6 +2133,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 @@ -2193,7 +2197,7 @@ void NodeManager::before() { if (! _battery_internal_vcc && _battery_pin > -1) analogReference(INTERNAL); #endif // setup individual sensors - for (int i = 0; i < 255; i++) { + for (int i = 1; i <= MAX_SENSORS; i++) { if (_sensors[i] == 0) continue; // call each sensor's setup() _sensors[i]->before(); @@ -2217,7 +2221,7 @@ void NodeManager::presentation() { _process("BATTERY"); #endif // present each sensor - for (int i = 0; i < 255; 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); @@ -2242,7 +2246,7 @@ void NodeManager::setup() { _send(_msg.set("STARTED")); #endif // run setup for all the registered sensors - for (int i = 0; i < 255; i++) { + for (int i = 1; i <= MAX_SENSORS; i++) { if (_sensors[i] == 0) continue; // call each sensor's setup() _sensors[i]->setup(); @@ -2261,7 +2265,7 @@ void NodeManager::loop() { if (_auto_power_pins) powerOn(); #endif // run loop for all the registered sensors - for (int i = 0; i < 255; 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 @@ -2296,7 +2300,7 @@ void NodeManager::receive(const MyMessage &message) { _process(message.getString()); } // 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(); @@ -2609,12 +2613,13 @@ void NodeManager::_present(int child_id, int type) { // return the next available child_id int NodeManager::_getAvailableChildId() { - for (int i = 1; i < 255; 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 6e3ed5ed..c56a30fe 100644 --- a/NodeManager.h +++ b/NodeManager.h @@ -7,7 +7,7 @@ #include // define NodeManager version -#define VERSION "1.5" +#define VERSION "1.5.1" /*********************************** Constants @@ -51,6 +51,10 @@ /*********************************** Default configuration settings */ +// if enabled, enable debug messages on serial port +#ifndef DEBUG + #define DEBUG 1 +#endif // if enabled, enable the capability to power on sensors with the arduino's pins to save battery while sleeping #ifndef POWER_MANAGER @@ -69,29 +73,15 @@ #define PERSIST 0 #endif -// if enabled, enable debug messages on serial port -#ifndef DEBUG - #define DEBUG 1 -#endif - // if enabled, send a SLEEPING and AWAKE service messages just before entering and just after leaving a sleep cycle #ifndef SERVICE_MESSAGES - #define SERVICE_MESSAGES 1 + #define SERVICE_MESSAGES 0 #endif // if enabled, a battery sensor will be created at BATTERY_CHILD_ID and will report vcc voltage together with the battery level percentage #ifndef BATTERY_SENSOR #define BATTERY_SENSOR 1 #endif -// the child id used to allow remote configuration -#ifndef CONFIGURATION_CHILD_ID - #define CONFIGURATION_CHILD_ID 200 -#endif -// the child id used to report the battery voltage to the controller -#ifndef BATTERY_CHILD_ID - #define BATTERY_CHILD_ID 201 -#endif - // Enable this module to use one of the following sensors: SENSOR_ANALOG_INPUT, SENSOR_LDR, SENSOR_THERMISTOR, SENSOR_MQ, SENSOR_ACS712 #ifndef MODULE_ANALOG_INPUT #define MODULE_ANALOG_INPUT 0 @@ -149,6 +139,19 @@ #define MODULE_MCP9808 0 #endif +// the child id used to allow remote configuration +#ifndef CONFIGURATION_CHILD_ID + #define CONFIGURATION_CHILD_ID 200 +#endif +// the child id used to report the battery voltage to the controller +#ifndef BATTERY_CHILD_ID + #define BATTERY_CHILD_ID 201 +#endif +// define the maximum number of sensors that can be managed +#ifndef MAX_SENSORS + #define MAX_SENSORS 10 +#endif + /*********************************** Sensors types */ @@ -1063,7 +1066,7 @@ class NodeManager { int _interrupt_2_pull = -1; int _last_interrupt_pin = -1; long _timestamp = -1; - Sensor* _sensors[255] = {0}; + Sensor* _sensors[MAX_SENSORS+1] = {0}; bool _ack = false; void _process(const char * message); void _sleep();