@@ -125,7 +125,8 @@ ControllerConfig MySensor::getConfig() {
125
125
void MySensor::requestNodeId () {
126
126
debug (PSTR (" req node id\n " ));
127
127
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);
129
130
wait (2000 );
130
131
}
131
132
@@ -160,49 +161,82 @@ void MySensor::findParentNode() {
160
161
}
161
162
162
163
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
+ }
166
174
167
175
// 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) {
169
177
requestNodeId ();
170
178
return false ;
171
179
}
172
180
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
175
187
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
+ //
178
199
// Message destination is not gateway and is in routing table for this node.
179
200
// Send it downstream
180
201
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) {
182
203
// Node has not yet received any id. We need to send it
183
204
// by doing a broadcast sending,
184
205
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);
185
226
}
186
227
}
187
228
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 ();
202
235
}
203
- return ok;
236
+ } else {
237
+ failedTransmissions = 0 ;
204
238
}
205
- return false ;
239
+ return ok ;
206
240
}
207
241
208
242
boolean MySensor::sendWrite (uint8_t to, MyMessage &message) {
@@ -369,10 +403,10 @@ boolean MySensor::process() {
369
403
// Return true if message was addressed for this node...
370
404
return true ;
371
405
} 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
373
407
374
408
if (command == C_INTERNAL && type == I_FIND_PARENT) {
375
- if (nc.distance == 255 ) {
409
+ if (nc.distance == DISTANCE_INVALID ) {
376
410
findParentNode ();
377
411
} else if (sender != nc.parentNodeId ) {
378
412
// Relaying nodes should always answer ping messages
@@ -383,42 +417,7 @@ boolean MySensor::process() {
383
417
}
384
418
} else if (to == nc.nodeId ) {
385
419
// 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);
422
421
}
423
422
}
424
423
return false ;
0 commit comments