Skip to content

Commit b108178

Browse files
authored
Release v1.5.1
* Decreased the number of items of the _sensors array #117 * Fixed gateway crashing on node check-in
1 parent 00946ad commit b108178

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

NodeManager.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,7 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {
20932093

20942094
// attach a built-in or custom sensor to this manager
20952095
int NodeManager::registerSensor(Sensor* sensor) {
2096+
if (sensor->getChildId() > MAX_SENSORS) return;
20962097
#if DEBUG == 1
20972098
Serial.print(F("REG I="));
20982099
Serial.print(sensor->getChildId());
@@ -2115,12 +2116,14 @@ int NodeManager::registerSensor(Sensor* sensor) {
21152116

21162117
// un-register a sensor to this manager
21172118
void NodeManager::unRegisterSensor(int sensor_index) {
2119+
if (sensor_index > MAX_SENSORS) return;
21182120
// unlink the pointer to this sensor
21192121
_sensors[sensor_index] == 0;
21202122
}
21212123

21222124
// return a sensor given its index
21232125
Sensor* NodeManager::get(int child_id) {
2126+
if (child_id > MAX_SENSORS) return 0;
21242127
// return a pointer to the sensor from the given child_id
21252128
return _sensors[child_id];
21262129
}
@@ -2130,6 +2133,7 @@ Sensor* NodeManager::getSensor(int child_id) {
21302133

21312134
// assign a different child id to a sensor'
21322135
bool NodeManager::renameSensor(int old_child_id, int new_child_id) {
2136+
if (old_child_id > MAX_SENSORS || new_child_id > MAX_SENSORS) return;
21332137
// ensure the old id exists and the new is available
21342138
if (_sensors[old_child_id] == 0 || _sensors[new_child_id] != 0) return false;
21352139
// assign the sensor to new id
@@ -2193,7 +2197,7 @@ void NodeManager::before() {
21932197
if (! _battery_internal_vcc && _battery_pin > -1) analogReference(INTERNAL);
21942198
#endif
21952199
// setup individual sensors
2196-
for (int i = 0; i < 255; i++) {
2200+
for (int i = 1; i <= MAX_SENSORS; i++) {
21972201
if (_sensors[i] == 0) continue;
21982202
// call each sensor's setup()
21992203
_sensors[i]->before();
@@ -2217,7 +2221,7 @@ void NodeManager::presentation() {
22172221
_process("BATTERY");
22182222
#endif
22192223
// present each sensor
2220-
for (int i = 0; i < 255; i++) {
2224+
for (int i = 1; i <= MAX_SENSORS; i++) {
22212225
if (_sensors[i] == 0) continue;
22222226
// call each sensor's presentation()
22232227
if (_sleep_between_send > 0) sleep(_sleep_between_send);
@@ -2242,7 +2246,7 @@ void NodeManager::setup() {
22422246
_send(_msg.set("STARTED"));
22432247
#endif
22442248
// run setup for all the registered sensors
2245-
for (int i = 0; i < 255; i++) {
2249+
for (int i = 1; i <= MAX_SENSORS; i++) {
22462250
if (_sensors[i] == 0) continue;
22472251
// call each sensor's setup()
22482252
_sensors[i]->setup();
@@ -2261,7 +2265,7 @@ void NodeManager::loop() {
22612265
if (_auto_power_pins) powerOn();
22622266
#endif
22632267
// run loop for all the registered sensors
2264-
for (int i = 0; i < 255; i++) {
2268+
for (int i = 1; i <= MAX_SENSORS; i++) {
22652269
// skip not configured sensors
22662270
if (_sensors[i] == 0) continue;
22672271
// if waking up from an interrupt skip all the sensor without that interrupt configured
@@ -2296,7 +2300,7 @@ void NodeManager::receive(const MyMessage &message) {
22962300
_process(message.getString());
22972301
}
22982302
// dispatch the message to the registered sensor
2299-
else if (_sensors[message.sensor] != 0) {
2303+
else if (message.sensor <= MAX_SENSORS && _sensors[message.sensor] != 0) {
23002304
#if POWER_MANAGER == 1
23012305
// turn on the pin powering all the sensors
23022306
if (_auto_power_pins) powerOn();
@@ -2609,12 +2613,13 @@ void NodeManager::_present(int child_id, int type) {
26092613

26102614
// return the next available child_id
26112615
int NodeManager::_getAvailableChildId() {
2612-
for (int i = 1; i < 255; i++) {
2616+
for (int i = 1; i <= MAX_SENSORS; i++) {
26132617
if (i == CONFIGURATION_CHILD_ID) continue;
26142618
if (i == BATTERY_CHILD_ID) continue;
26152619
// empty place, return it
26162620
if (_sensors[i] == 0) return i;
26172621
}
2622+
return MAX_SENSORS;
26182623
}
26192624

26202625
// guess the initial value of a digital output based on the configured interrupt mode

NodeManager.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <Arduino.h>
88

99
// define NodeManager version
10-
#define VERSION "1.5"
10+
#define VERSION "1.5.1"
1111

1212
/***********************************
1313
Constants
@@ -51,6 +51,10 @@
5151
/***********************************
5252
Default configuration settings
5353
*/
54+
// if enabled, enable debug messages on serial port
55+
#ifndef DEBUG
56+
#define DEBUG 1
57+
#endif
5458

5559
// if enabled, enable the capability to power on sensors with the arduino's pins to save battery while sleeping
5660
#ifndef POWER_MANAGER
@@ -69,29 +73,15 @@
6973
#define PERSIST 0
7074
#endif
7175

72-
// if enabled, enable debug messages on serial port
73-
#ifndef DEBUG
74-
#define DEBUG 1
75-
#endif
76-
7776
// if enabled, send a SLEEPING and AWAKE service messages just before entering and just after leaving a sleep cycle
7877
#ifndef SERVICE_MESSAGES
79-
#define SERVICE_MESSAGES 1
78+
#define SERVICE_MESSAGES 0
8079
#endif
8180
// if enabled, a battery sensor will be created at BATTERY_CHILD_ID and will report vcc voltage together with the battery level percentage
8281
#ifndef BATTERY_SENSOR
8382
#define BATTERY_SENSOR 1
8483
#endif
8584

86-
// the child id used to allow remote configuration
87-
#ifndef CONFIGURATION_CHILD_ID
88-
#define CONFIGURATION_CHILD_ID 200
89-
#endif
90-
// the child id used to report the battery voltage to the controller
91-
#ifndef BATTERY_CHILD_ID
92-
#define BATTERY_CHILD_ID 201
93-
#endif
94-
9585
// Enable this module to use one of the following sensors: SENSOR_ANALOG_INPUT, SENSOR_LDR, SENSOR_THERMISTOR, SENSOR_MQ, SENSOR_ACS712
9686
#ifndef MODULE_ANALOG_INPUT
9787
#define MODULE_ANALOG_INPUT 0
@@ -149,6 +139,19 @@
149139
#define MODULE_MCP9808 0
150140
#endif
151141

142+
// the child id used to allow remote configuration
143+
#ifndef CONFIGURATION_CHILD_ID
144+
#define CONFIGURATION_CHILD_ID 200
145+
#endif
146+
// the child id used to report the battery voltage to the controller
147+
#ifndef BATTERY_CHILD_ID
148+
#define BATTERY_CHILD_ID 201
149+
#endif
150+
// define the maximum number of sensors that can be managed
151+
#ifndef MAX_SENSORS
152+
#define MAX_SENSORS 10
153+
#endif
154+
152155
/***********************************
153156
Sensors types
154157
*/
@@ -1063,7 +1066,7 @@ class NodeManager {
10631066
int _interrupt_2_pull = -1;
10641067
int _last_interrupt_pin = -1;
10651068
long _timestamp = -1;
1066-
Sensor* _sensors[255] = {0};
1069+
Sensor* _sensors[MAX_SENSORS+1] = {0};
10671070
bool _ack = false;
10681071
void _process(const char * message);
10691072
void _sleep();

0 commit comments

Comments
 (0)