Skip to content

Commit f3c60a0

Browse files
author
Marcelo Aquino
committed
process() uses sendRoute() for routing decisions
To keep routing decisions in one place, and avoid rewriting the same code to add failedTransmissions count to process()
1 parent 50c8c44 commit f3c60a0

File tree

1 file changed

+63
-64
lines changed

1 file changed

+63
-64
lines changed

libraries/MySensors/MySensor.cpp

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ ControllerConfig MySensor::getConfig() {
125125
void MySensor::requestNodeId() {
126126
debug(PSTR("req node id\n"));
127127
radio->setAddress(nc.nodeId);
128-
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_ID_REQUEST, false).set(""));
128+
build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_ID_REQUEST, false).set("");
129+
sendWrite(nc.parentNodeId, msg);
129130
wait(2000);
130131
}
131132

@@ -160,49 +161,82 @@ void MySensor::findParentNode() {
160161
}
161162

162163
boolean MySensor::sendRoute(MyMessage &message) {
163-
// Make sure to process any incoming messages before sending (could this end up in recursive loop?)
164-
// process();
165-
bool isInternal = mGetCommand(message) == C_INTERNAL;
164+
uint8_t sender = message.sender;
165+
uint8_t dest = message.destination;
166+
uint8_t last = message.last;
167+
bool ok;
168+
169+
// If we still don't have any parent id, re-request and skip this message.
170+
if (nc.parentNodeId == AUTO) {
171+
findParentNode();
172+
return false;
173+
}
166174

167175
// If we still don't have any node id, re-request and skip this message.
168-
if (nc.nodeId == AUTO && !(isInternal && message.type == I_ID_REQUEST)) {
176+
if (nc.nodeId == AUTO) {
169177
requestNodeId();
170178
return false;
171179
}
172180

173-
if (repeaterMode) {
174-
uint8_t dest = message.destination;
181+
if (dest == GATEWAY_ADDRESS || !repeaterMode) {
182+
// If destination is the gateway or if we aren't a repeater, let
183+
// our parent take care of the message
184+
ok = sendWrite(nc.parentNodeId, message);
185+
} else {
186+
// Relay the message
175187
uint8_t route = getChildRoute(dest);
176-
if (route>GATEWAY_ADDRESS && route<BROADCAST_ADDRESS && dest != GATEWAY_ADDRESS) {
177-
// --- debug(PSTR("route %d.\n"), route);
188+
if (route > GATEWAY_ADDRESS && route < BROADCAST_ADDRESS) {
189+
// This message should be forwarded to a child node. If we send message
190+
// to this nodes pipe then all children will receive it because the are
191+
// all listening to this nodes pipe.
192+
//
193+
// +----B
194+
// -A
195+
// +----C------D
196+
//
197+
// We're node C, Message comes from A and has destination D
198+
//
178199
// Message destination is not gateway and is in routing table for this node.
179200
// Send it downstream
180201
return sendWrite(route, message);
181-
} else if (isInternal && message.type == I_ID_RESPONSE && dest==BROADCAST_ADDRESS) {
202+
} else if (sender == GATEWAY_ADDRESS && dest == BROADCAST_ADDRESS) {
182203
// Node has not yet received any id. We need to send it
183204
// by doing a broadcast sending,
184205
return sendWrite(BROADCAST_ADDRESS, message);
206+
} else if (isGateway) {
207+
// Destination isn't in our routing table and isn't a broadcast address
208+
// Nothing to do here
209+
return false;
210+
} else {
211+
// A message comes from a child node and we have no
212+
// route for it.
213+
//
214+
// +----B
215+
// -A
216+
// +----C------D <-- Message comes from D
217+
//
218+
// We're node C
219+
//
220+
// Message should be passed to node A (this nodes relay)
221+
222+
// This message should be routed back towards sensor net gateway
223+
ok = sendWrite(nc.parentNodeId, message);
224+
// Add this child to our "routing table" if it not already exist
225+
addChildRoute(sender, last);
185226
}
186227
}
187228

188-
if (!isGateway) {
189-
// Should be routed back to gateway.
190-
bool ok = sendWrite(nc.parentNodeId, message);
191-
192-
if (!ok) {
193-
// Failure when sending to parent node. The parent node might be down and we
194-
// need to find another route to gateway.
195-
if (autoFindParent && failedTransmissions > SEARCH_FAILURES) {
196-
findParentNode();
197-
} else {
198-
failedTransmissions++;
199-
}
200-
} else {
201-
failedTransmissions = 0;
229+
if (!ok) {
230+
// Failure when sending to parent node. The parent node might be down and we
231+
// need to find another route to gateway.
232+
failedTransmissions++;
233+
if (autoFindParent && failedTransmissions > SEARCH_FAILURES) {
234+
findParentNode();
202235
}
203-
return ok;
236+
} else {
237+
failedTransmissions = 0;
204238
}
205-
return false;
239+
return ok;
206240
}
207241

208242
boolean MySensor::sendWrite(uint8_t to, MyMessage &message) {
@@ -369,10 +403,10 @@ boolean MySensor::process() {
369403
// Return true if message was addressed for this node...
370404
return true;
371405
} else if (repeaterMode && nc.nodeId != AUTO) {
372-
// Relaying nodes should answer only after set an id
406+
// If this node have an id, relay the message
373407

374408
if (command == C_INTERNAL && type == I_FIND_PARENT) {
375-
if (nc.distance == 255) {
409+
if (nc.distance == DISTANCE_INVALID) {
376410
findParentNode();
377411
} else if (sender != nc.parentNodeId) {
378412
// Relaying nodes should always answer ping messages
@@ -383,42 +417,7 @@ boolean MySensor::process() {
383417
}
384418
} else if (to == nc.nodeId) {
385419
// We should try to relay this message to another node
386-
387-
uint8_t route = getChildRoute(msg.destination);
388-
if (route>0 && route<255) {
389-
// This message should be forwarded to a child node. If we send message
390-
// to this nodes pipe then all children will receive it because the are
391-
// all listening to this nodes pipe.
392-
//
393-
// +----B
394-
// -A
395-
// +----C------D
396-
//
397-
// We're node C, Message comes from A and has destination D
398-
//
399-
// lookup route in table and send message there
400-
sendWrite(route, msg);
401-
} else if (sender == GATEWAY_ADDRESS && destination == BROADCAST_ADDRESS) {
402-
// A net gateway reply to a message previously sent by us from a 255 node
403-
// We should broadcast this back to the node
404-
sendWrite(BROADCAST_ADDRESS, msg);
405-
} else {
406-
// A message comes from a child node and we have no
407-
// route for it.
408-
//
409-
// +----B
410-
// -A
411-
// +----C------D <-- Message comes from D
412-
//
413-
// We're node C
414-
//
415-
// Message should be passed to node A (this nodes relay)
416-
417-
// This message should be routed back towards sensor net gateway
418-
sendWrite(nc.parentNodeId, msg);
419-
// Add this child to our "routing table" if it not already exist
420-
addChildRoute(sender, last);
421-
}
420+
sendRoute(msg);
422421
}
423422
}
424423
return false;

0 commit comments

Comments
 (0)