Skip to content

Commit 10e43f6

Browse files
author
Yveaux
committed
Support parent search without node ID
To not break existing functionality parent search can be started when node ID is still unknown. In this case node ID is set to broadcast ID, which can cause funky stuff to happen...
1 parent 0f34bb3 commit 10e43f6

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

libraries/MySensors/MySensor.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
8181
requestNodeId();
8282
}
8383

84-
debug(PSTR("%s started, id %d\n"), repeaterMode?"repeater":"sensor", nc.nodeId);
84+
debug(PSTR("%s started, id=%d, parent=%d, distance=%d\n"), repeaterMode?"repeater":"sensor", nc.nodeId, nc.parentNodeId, nc.distance);
8585

8686
// Open reading pipe for messages directed to this node (set write pipe to same)
8787
RF24::openReadingPipe(WRITE_PIPE, TO_ADDR(nc.nodeId));
@@ -148,7 +148,8 @@ void MySensor::findParentNode() {
148148
debug(PSTR("find parent\n"));
149149

150150
build(msg, nc.nodeId, BROADCAST_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT, false).set("");
151-
sendWrite(BROADCAST_ADDRESS, msg);
151+
// Write msg, but suppress recursive parent search
152+
sendWrite(BROADCAST_ADDRESS, msg, false);
152153

153154
// Wait for ping response.
154155
waitForReply();
@@ -195,12 +196,16 @@ boolean MySensor::sendRoute(MyMessage &message) {
195196
return false;
196197
}
197198

198-
boolean MySensor::sendWrite(uint8_t next, MyMessage &message) {
199+
boolean MySensor::sendWrite(uint8_t next, MyMessage &message, const bool allowFindParent) {
199200
bool ok = true;
200201
const bool broadcast = next == BROADCAST_ADDRESS;
201-
const bool toParent = !broadcast && (next == nc.parentNodeId);
202+
const bool toParent = next == nc.parentNodeId;
203+
// With current implementation parent node Id can equal the broadcast address when
204+
// starting with empty eeprom and AUTO node Id is active.
205+
// This behavior is undesired, as possible parents will report back to broadcast address.
206+
// debug(PSTR("sendWrite next=%d, parent=%d, distance=%d\n"), next, nc.parentNodeId, nc.distance);
202207
// If sending directly to parent node and distance is not set, then try to find parent now.
203-
if ( toParent && !isValidDistance(nc.distance) ) {
208+
if ( allowFindParent && toParent && !isValidDistance(nc.distance) ) {
204209
findParentNode();
205210
// Known distance indicates parent has been found
206211
ok = isValidDistance(nc.distance);
@@ -219,7 +224,8 @@ boolean MySensor::sendWrite(uint8_t next, MyMessage &message) {
219224
RF24::startListening();
220225

221226
debug(PSTR("send: %d-%d-%d-%d s=%d,c=%d,t=%d,pt=%d,l=%d,st=%s:%s\n"),
222-
message.sender,message.last, next, message.destination, message.sensor, mGetCommand(message), message.type, mGetPayloadType(message), mGetLength(message), ok?"ok":"fail", message.getString(convBuf));
227+
message.sender,message.last, next, message.destination, message.sensor, mGetCommand(message), message.type,
228+
mGetPayloadType(message), mGetLength(message), broadcast ? "bc" : (ok ? "ok":"fail"), message.getString(convBuf));
223229

224230
// If many successive transmissions to parent failed, the parent node might be down and we
225231
// need to find another route to gateway.

libraries/MySensors/MySensor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class MySensor : public RF24
254254
void setupRepeaterMode();
255255
void setupRadio(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate);
256256
boolean sendRoute(MyMessage &message);
257-
boolean sendWrite(uint8_t dest, MyMessage &message);
257+
boolean sendWrite(uint8_t dest, MyMessage &message, const bool allowFindParent = true );
258258

259259
private:
260260
#ifdef DEBUG

0 commit comments

Comments
 (0)