@@ -299,6 +299,12 @@ void MySensor::setupNode() {
299299}
300300
301301void MySensor::findParentNode () {
302+ static boolean findingParentNode = false ;
303+
304+ if (findingParentNode)
305+ return ;
306+ findingParentNode = true ;
307+
302308 failedTransmissions = 0 ;
303309
304310 // Set distance to max
@@ -313,6 +319,7 @@ void MySensor::findParentNode() {
313319
314320 // Wait for ping response.
315321 wait (2000 );
322+ findingParentNode = false ;
316323}
317324
318325boolean MySensor::sendRoute (MyMessage &message) {
@@ -786,14 +793,17 @@ boolean MySensor::process() {
786793 // If this node have an id, relay the message
787794
788795 if (command == C_INTERNAL && type == I_FIND_PARENT) {
789- if (nc.distance == DISTANCE_INVALID) {
790- findParentNode ();
791- } else if (sender != nc.parentNodeId ) {
792- // Relaying nodes should always answer ping messages
793- // Wait a random delay of 0-2 seconds to minimize collision
794- // between ping ack messages from other relaying nodes
795- wait (hw_millis () & 0x3ff );
796- sendWrite (sender, build (msg, nc.nodeId , sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE, false ).set (nc.distance ));
796+ if (sender != nc.parentNodeId ) {
797+ if (nc.distance == DISTANCE_INVALID)
798+ findParentNode ();
799+
800+ if (nc.distance != DISTANCE_INVALID) {
801+ // Relaying nodes should always answer ping messages
802+ // Wait a random delay of 0-2 seconds to minimize collision
803+ // between ping ack messages from other relaying nodes
804+ wait (hw_millis () & 0x3ff );
805+ sendWrite (sender, build (msg, nc.nodeId , sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE, false ).set (nc.distance ));
806+ }
797807 }
798808 } else if (to == nc.nodeId ) {
799809 // We should try to relay this message to another node
0 commit comments