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,7 +373,20 @@ 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) {
|
||||||
|
// Relaying nodes should answer only after set an id
|
||||||
|
|
||||||
|
if (command == C_INTERNAL && type == I_FIND_PARENT) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
} else if (pipe == CURRENT_NODE_PIPE) {
|
||||||
// We should try to relay this message to another node
|
// We should try to relay this message to another node
|
||||||
|
|
||||||
uint8_t route = getChildRoute(msg.destination);
|
uint8_t route = getChildRoute(msg.destination);
|
||||||
@@ -429,15 +425,14 @@ boolean MySensor::process() {
|
|||||||
addChildRoute(sender, last);
|
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