Merge pull request #54 from mapnull/hotfix-r255
Relay messages only after receiving an id
This commit is contained in:
@@ -27,11 +27,9 @@ inline MyMessage& build (MyMessage &msg, uint8_t sender, uint8_t destination, ui
|
|||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MySensor::MySensor(uint8_t _cepin, uint8_t _cspin) : RF24(_cepin, _cspin) {
|
MySensor::MySensor(uint8_t _cepin, uint8_t _cspin) : RF24(_cepin, _cspin) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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) {
|
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) {
|
||||||
Serial.begin(BAUD_RATE);
|
Serial.begin(BAUD_RATE);
|
||||||
isGateway = false;
|
isGateway = false;
|
||||||
@@ -125,7 +123,6 @@ void MySensor::setupRepeaterMode(){
|
|||||||
eeprom_read_block((void*)childNodeTable, (void*)EEPROM_ROUTES_ADDRESS, 256);
|
eeprom_read_block((void*)childNodeTable, (void*)EEPROM_ROUTES_ADDRESS, 256);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint8_t MySensor::getNodeId() {
|
uint8_t MySensor::getNodeId() {
|
||||||
return nc.nodeId;
|
return nc.nodeId;
|
||||||
}
|
}
|
||||||
@@ -141,7 +138,6 @@ void MySensor::requestNodeId() {
|
|||||||
waitForReply();
|
waitForReply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MySensor::findParentNode() {
|
void MySensor::findParentNode() {
|
||||||
failedTransmissions = 0;
|
failedTransmissions = 0;
|
||||||
|
|
||||||
@@ -228,7 +224,6 @@ boolean MySensor::sendWrite(uint8_t next, MyMessage &message, bool broadcast) {
|
|||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool MySensor::send(MyMessage &message, bool enableAck) {
|
bool MySensor::send(MyMessage &message, bool enableAck) {
|
||||||
message.sender = nc.nodeId;
|
message.sender = nc.nodeId;
|
||||||
mSetCommand(message,C_SET);
|
mSetCommand(message,C_SET);
|
||||||
@@ -262,7 +257,6 @@ void MySensor::requestTime(void (* _timeCallback)(unsigned long)) {
|
|||||||
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_TIME, false).set(""));
|
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_TIME, false).set(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
boolean MySensor::process() {
|
boolean MySensor::process() {
|
||||||
uint8_t pipe;
|
uint8_t pipe;
|
||||||
boolean available = RF24::available(&pipe);
|
boolean available = RF24::available(&pipe);
|
||||||
@@ -289,18 +283,7 @@ boolean MySensor::process() {
|
|||||||
uint8_t last = msg.last;
|
uint8_t last = msg.last;
|
||||||
uint8_t destination = msg.destination;
|
uint8_t destination = msg.destination;
|
||||||
|
|
||||||
if (repeaterMode && command == C_INTERNAL && type == I_FIND_PARENT) {
|
if (destination == nc.nodeId) {
|
||||||
if (nc.distance == 255) {
|
|
||||||
findParentNode();
|
|
||||||
} else if (sender != nc.parentNodeId) {
|
|
||||||
// Relaying nodes should always answer ping messages
|
|
||||||
// Wait a random delay of 0-2 seconds to minimize collision
|
|
||||||
// between ping ack messages from other relaying nodes
|
|
||||||
delay(millis() & 0x3ff);
|
|
||||||
sendWrite(sender, build(msg, nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE, false).set(nc.distance), true);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} else if (destination == nc.nodeId) {
|
|
||||||
// Check if sender requests an ack back.
|
// Check if sender requests an ack back.
|
||||||
if (mGetRequestAck(msg)) {
|
if (mGetRequestAck(msg)) {
|
||||||
// Copy message
|
// Copy message
|
||||||
@@ -390,54 +373,66 @@ boolean MySensor::process() {
|
|||||||
}
|
}
|
||||||
// Return true if message was addressed for this node...
|
// Return true if message was addressed for this node...
|
||||||
return true;
|
return true;
|
||||||
} else if (repeaterMode && pipe == CURRENT_NODE_PIPE) {
|
} else if (repeaterMode && nc.nodeId != AUTO) {
|
||||||
// We should try to relay this message to another node
|
// Relaying nodes should answer only after set an id
|
||||||
|
|
||||||
uint8_t route = getChildRoute(msg.destination);
|
if (command == C_INTERNAL && type == I_FIND_PARENT) {
|
||||||
if (route>0 && route<255) {
|
if (nc.distance == 255) {
|
||||||
// This message should be forwarded to a child node. If we send message
|
findParentNode();
|
||||||
// to this nodes pipe then all children will receive it because the are
|
} else if (sender != nc.parentNodeId) {
|
||||||
// all listening to this nodes pipe.
|
// Relaying nodes should always answer ping messages
|
||||||
//
|
// Wait a random delay of 0-2 seconds to minimize collision
|
||||||
// +----B
|
// between ping ack messages from other relaying nodes
|
||||||
// -A
|
delay(millis() & 0x3ff);
|
||||||
// +----C------D
|
sendWrite(sender, build(msg, nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE, false).set(nc.distance), true);
|
||||||
//
|
}
|
||||||
// We're node C, Message comes from A and has destination D
|
} else if (pipe == CURRENT_NODE_PIPE) {
|
||||||
//
|
// We should try to relay this message to another node
|
||||||
// lookup route in table and send message there
|
|
||||||
sendWrite(route, msg);
|
|
||||||
} else if (sender == GATEWAY_ADDRESS && destination == BROADCAST_ADDRESS) {
|
|
||||||
// A net gateway reply to a message previously sent by us from a 255 node
|
|
||||||
// We should broadcast this back to the node
|
|
||||||
sendWrite(destination, msg, true);
|
|
||||||
} else {
|
|
||||||
// A message comes from a child node and we have no
|
|
||||||
// route for it.
|
|
||||||
//
|
|
||||||
// +----B
|
|
||||||
// -A
|
|
||||||
// +----C------D <-- Message comes from D
|
|
||||||
//
|
|
||||||
// We're node C
|
|
||||||
//
|
|
||||||
// Message should be passed to node A (this nodes relay)
|
|
||||||
|
|
||||||
// This message should be routed back towards sensor net gateway
|
uint8_t route = getChildRoute(msg.destination);
|
||||||
sendWrite(nc.parentNodeId, msg);
|
if (route>0 && route<255) {
|
||||||
// Add this child to our "routing table" if it not already exist
|
// This message should be forwarded to a child node. If we send message
|
||||||
addChildRoute(sender, last);
|
// to this nodes pipe then all children will receive it because the are
|
||||||
|
// all listening to this nodes pipe.
|
||||||
|
//
|
||||||
|
// +----B
|
||||||
|
// -A
|
||||||
|
// +----C------D
|
||||||
|
//
|
||||||
|
// We're node C, Message comes from A and has destination D
|
||||||
|
//
|
||||||
|
// lookup route in table and send message there
|
||||||
|
sendWrite(route, msg);
|
||||||
|
} else if (sender == GATEWAY_ADDRESS && destination == BROADCAST_ADDRESS) {
|
||||||
|
// A net gateway reply to a message previously sent by us from a 255 node
|
||||||
|
// We should broadcast this back to the node
|
||||||
|
sendWrite(destination, msg, true);
|
||||||
|
} else {
|
||||||
|
// A message comes from a child node and we have no
|
||||||
|
// route for it.
|
||||||
|
//
|
||||||
|
// +----B
|
||||||
|
// -A
|
||||||
|
// +----C------D <-- Message comes from D
|
||||||
|
//
|
||||||
|
// We're node C
|
||||||
|
//
|
||||||
|
// Message should be passed to node A (this nodes relay)
|
||||||
|
|
||||||
|
// This message should be routed back towards sensor net gateway
|
||||||
|
sendWrite(nc.parentNodeId, msg);
|
||||||
|
// Add this child to our "routing table" if it not already exist
|
||||||
|
addChildRoute(sender, last);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MyMessage& MySensor::getLastMessage() {
|
MyMessage& MySensor::getLastMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MySensor::saveState(uint8_t pos, uint8_t value) {
|
void MySensor::saveState(uint8_t pos, uint8_t value) {
|
||||||
if (loadState(pos) != value) {
|
if (loadState(pos) != value) {
|
||||||
eeprom_write_byte((uint8_t*)(EEPROM_LOCAL_CONFIG_ADDRESS+pos), value);
|
eeprom_write_byte((uint8_t*)(EEPROM_LOCAL_CONFIG_ADDRESS+pos), value);
|
||||||
@@ -447,7 +442,6 @@ uint8_t MySensor::loadState(uint8_t pos) {
|
|||||||
return eeprom_read_byte((uint8_t*)(EEPROM_LOCAL_CONFIG_ADDRESS+pos));
|
return eeprom_read_byte((uint8_t*)(EEPROM_LOCAL_CONFIG_ADDRESS+pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MySensor::addChildRoute(uint8_t childId, uint8_t route) {
|
void MySensor::addChildRoute(uint8_t childId, uint8_t route) {
|
||||||
if (childNodeTable[childId] != route) {
|
if (childNodeTable[childId] != route) {
|
||||||
childNodeTable[childId] = route;
|
childNodeTable[childId] = route;
|
||||||
@@ -466,7 +460,6 @@ uint8_t MySensor::getChildRoute(uint8_t childId) {
|
|||||||
return childNodeTable[childId];
|
return childNodeTable[childId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int8_t pinIntTrigger = 0;
|
int8_t pinIntTrigger = 0;
|
||||||
void wakeUp() //place to send the interrupts
|
void wakeUp() //place to send the interrupts
|
||||||
{
|
{
|
||||||
@@ -589,7 +582,6 @@ void MySensor::debugPrint(const char *fmt, ... ) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
int MySensor::freeRam (void) {
|
int MySensor::freeRam (void) {
|
||||||
extern int __heap_start, *__brkval;
|
extern int __heap_start, *__brkval;
|
||||||
|
|||||||
Reference in New Issue
Block a user