Skip to content

Commit ff1ddc3

Browse files
author
Marcelo Aquino
committed
Relay messages only after receiving an id
1 parent 08221e6 commit ff1ddc3

File tree

1 file changed

+52
-60
lines changed

1 file changed

+52
-60
lines changed

libraries/MySensors/MySensor.cpp

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ inline MyMessage& build (MyMessage &msg, uint8_t sender, uint8_t destination, ui
2727
return msg;
2828
}
2929

30-
3130
MySensor::MySensor(uint8_t _cepin, uint8_t _cspin) : RF24(_cepin, _cspin) {
3231
}
3332

34-
3533
void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, uint8_t _parentNodeId, rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate) {
3634
Serial.begin(BAUD_RATE);
3735
isGateway = false;
@@ -125,7 +123,6 @@ void MySensor::setupRepeaterMode(){
125123
eeprom_read_block((void*)childNodeTable, (void*)EEPROM_ROUTES_ADDRESS, 256);
126124
}
127125

128-
129126
uint8_t MySensor::getNodeId() {
130127
return nc.nodeId;
131128
}
@@ -141,7 +138,6 @@ void MySensor::requestNodeId() {
141138
waitForReply();
142139
}
143140

144-
145141
void MySensor::findParentNode() {
146142
failedTransmissions = 0;
147143

@@ -228,7 +224,6 @@ boolean MySensor::sendWrite(uint8_t next, MyMessage &message, bool broadcast) {
228224
return ok;
229225
}
230226

231-
232227
bool MySensor::send(MyMessage &message, bool enableAck) {
233228
message.sender = nc.nodeId;
234229
mSetCommand(message,C_SET);
@@ -262,7 +257,6 @@ void MySensor::requestTime(void (* _timeCallback)(unsigned long)) {
262257
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_TIME, false).set(""));
263258
}
264259

265-
266260
boolean MySensor::process() {
267261
uint8_t pipe;
268262
boolean available = RF24::available(&pipe);
@@ -289,18 +283,7 @@ boolean MySensor::process() {
289283
uint8_t last = msg.last;
290284
uint8_t destination = msg.destination;
291285

292-
if (repeaterMode && command == C_INTERNAL && type == I_FIND_PARENT) {
293-
if (nc.distance == 255) {
294-
findParentNode();
295-
} else if (sender != nc.parentNodeId) {
296-
// Relaying nodes should always answer ping messages
297-
// Wait a random delay of 0-2 seconds to minimize collision
298-
// between ping ack messages from other relaying nodes
299-
delay(millis() & 0x3ff);
300-
sendWrite(sender, build(msg, nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE, false).set(nc.distance), true);
301-
}
302-
return false;
303-
} else if (destination == nc.nodeId) {
286+
if (destination == nc.nodeId) {
304287
// Check if sender requests an ack back.
305288
if (mGetRequestAck(msg)) {
306289
// Copy message
@@ -390,54 +373,66 @@ boolean MySensor::process() {
390373
}
391374
// Return true if message was addressed for this node...
392375
return true;
393-
} else if (repeaterMode && pipe == CURRENT_NODE_PIPE) {
394-
// We should try to relay this message to another node
395-
396-
uint8_t route = getChildRoute(msg.destination);
397-
if (route>0 && route<255) {
398-
// This message should be forwarded to a child node. If we send message
399-
// to this nodes pipe then all children will receive it because the are
400-
// all listening to this nodes pipe.
401-
//
402-
// +----B
403-
// -A
404-
// +----C------D
405-
//
406-
// We're node C, Message comes from A and has destination D
407-
//
408-
// lookup route in table and send message there
409-
sendWrite(route, msg);
410-
} else if (sender == GATEWAY_ADDRESS && destination == BROADCAST_ADDRESS) {
411-
// A net gateway reply to a message previously sent by us from a 255 node
412-
// We should broadcast this back to the node
413-
sendWrite(destination, msg, true);
414-
} else {
415-
// A message comes from a child node and we have no
416-
// route for it.
417-
//
418-
// +----B
419-
// -A
420-
// +----C------D <-- Message comes from D
421-
//
422-
// We're node C
423-
//
424-
// Message should be passed to node A (this nodes relay)
425-
426-
// This message should be routed back towards sensor net gateway
427-
sendWrite(nc.parentNodeId, msg);
428-
// Add this child to our "routing table" if it not already exist
429-
addChildRoute(sender, last);
376+
} else if (repeaterMode && nc.nodeId != AUTO) {
377+
// Relaying nodes should answer only after set an id
378+
379+
if (command == C_INTERNAL && type == I_FIND_PARENT) {
380+
if (nc.distance == 255) {
381+
findParentNode();
382+
} else if (sender != nc.parentNodeId) {
383+
// Relaying nodes should always answer ping messages
384+
// Wait a random delay of 0-2 seconds to minimize collision
385+
// between ping ack messages from other relaying nodes
386+
delay(millis() & 0x3ff);
387+
sendWrite(sender, build(msg, nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE, false).set(nc.distance), true);
388+
}
389+
} else if (pipe == CURRENT_NODE_PIPE) {
390+
// We should try to relay this message to another node
391+
392+
uint8_t route = getChildRoute(msg.destination);
393+
if (route>0 && route<255) {
394+
// This message should be forwarded to a child node. If we send message
395+
// to this nodes pipe then all children will receive it because the are
396+
// all listening to this nodes pipe.
397+
//
398+
// +----B
399+
// -A
400+
// +----C------D
401+
//
402+
// We're node C, Message comes from A and has destination D
403+
//
404+
// lookup route in table and send message there
405+
sendWrite(route, msg);
406+
} else if (sender == GATEWAY_ADDRESS && destination == BROADCAST_ADDRESS) {
407+
// A net gateway reply to a message previously sent by us from a 255 node
408+
// We should broadcast this back to the node
409+
sendWrite(destination, msg, true);
410+
} else {
411+
// A message comes from a child node and we have no
412+
// route for it.
413+
//
414+
// +----B
415+
// -A
416+
// +----C------D <-- Message comes from D
417+
//
418+
// We're node C
419+
//
420+
// Message should be passed to node A (this nodes relay)
421+
422+
// This message should be routed back towards sensor net gateway
423+
sendWrite(nc.parentNodeId, msg);
424+
// Add this child to our "routing table" if it not already exist
425+
addChildRoute(sender, last);
426+
}
430427
}
431428
}
432429
return false;
433430
}
434431

435-
436432
MyMessage& MySensor::getLastMessage() {
437433
return msg;
438434
}
439435

440-
441436
void MySensor::saveState(uint8_t pos, uint8_t value) {
442437
if (loadState(pos) != value) {
443438
eeprom_write_byte((uint8_t*)(EEPROM_LOCAL_CONFIG_ADDRESS+pos), value);
@@ -447,7 +442,6 @@ uint8_t MySensor::loadState(uint8_t pos) {
447442
return eeprom_read_byte((uint8_t*)(EEPROM_LOCAL_CONFIG_ADDRESS+pos));
448443
}
449444

450-
451445
void MySensor::addChildRoute(uint8_t childId, uint8_t route) {
452446
if (childNodeTable[childId] != route) {
453447
childNodeTable[childId] = route;
@@ -466,7 +460,6 @@ uint8_t MySensor::getChildRoute(uint8_t childId) {
466460
return childNodeTable[childId];
467461
}
468462

469-
470463
int8_t pinIntTrigger = 0;
471464
void wakeUp() //place to send the interrupts
472465
{
@@ -589,7 +582,6 @@ void MySensor::debugPrint(const char *fmt, ... ) {
589582
}
590583
#endif
591584

592-
593585
#ifdef DEBUG
594586
int MySensor::freeRam (void) {
595587
extern int __heap_start, *__brkval;

0 commit comments

Comments
 (0)