Core optimizations
This commit is contained in:
+54
-54
@@ -31,7 +31,7 @@ static transportState_t stFailure = { stFailureTransition, stFailureUpdate };
|
||||
static transportSM_t _transportSM;
|
||||
|
||||
// transport config
|
||||
transportConfig_t transportConfig;
|
||||
static transportConfig_t _transportConfig;
|
||||
|
||||
// callbacks
|
||||
transportCallback_t transportOk_cb = NULL;
|
||||
@@ -41,7 +41,7 @@ extern MyMessage _msg; // incoming message
|
||||
extern MyMessage _msgTmp; // outgoing message
|
||||
|
||||
#if defined(MY_RAM_ROUTING_TABLE_ENABLED)
|
||||
static routingTable _transportRoutingTable; //!< routing table
|
||||
static routingTable_t _transportRoutingTable; //!< routing table
|
||||
static uint32_t _lastRoutingTableSave; //!< last routing table dump
|
||||
#endif
|
||||
|
||||
@@ -75,7 +75,7 @@ void stInitTransition(void) {
|
||||
#endif
|
||||
|
||||
// Read node settings (ID, parent ID, GW distance) from EEPROM
|
||||
hwReadConfigBlock((void*)&transportConfig, (void*)EEPROM_NODE_ID_ADDRESS, sizeof(transportConfig_t));
|
||||
hwReadConfigBlock((void*)&_transportConfig, (void*)EEPROM_NODE_ID_ADDRESS, sizeof(transportConfig_t));
|
||||
}
|
||||
|
||||
void stInitUpdate(void) {
|
||||
@@ -91,9 +91,9 @@ void stInitUpdate(void) {
|
||||
#if defined(MY_GATEWAY_FEATURE)
|
||||
// Set configuration for gateway
|
||||
TRANSPORT_DEBUG(PSTR("TSM:INIT:GW MODE\n"));
|
||||
transportConfig.parentNodeId = GATEWAY_ADDRESS;
|
||||
transportConfig.distanceGW = 0u;
|
||||
transportConfig.nodeId = GATEWAY_ADDRESS;
|
||||
_transportConfig.parentNodeId = GATEWAY_ADDRESS;
|
||||
_transportConfig.distanceGW = 0u;
|
||||
_transportConfig.nodeId = GATEWAY_ADDRESS;
|
||||
transportSetAddress(GATEWAY_ADDRESS);
|
||||
// GW mode: skip FPAR,ID,UPL states
|
||||
transportSwitchSM(stReady);
|
||||
@@ -101,12 +101,12 @@ void stInitUpdate(void) {
|
||||
if ((uint8_t)MY_NODE_ID != AUTO) {
|
||||
TRANSPORT_DEBUG(PSTR("TSM:INIT:STATID=%d\n"),(uint8_t)MY_NODE_ID);
|
||||
// Set static ID
|
||||
transportConfig.nodeId = (uint8_t)MY_NODE_ID;
|
||||
_transportConfig.nodeId = (uint8_t)MY_NODE_ID;
|
||||
// Save static ID to eeprom (for bootloader)
|
||||
hwWriteConfig(EEPROM_NODE_ID_ADDRESS, (uint8_t)MY_NODE_ID);
|
||||
}
|
||||
// assign ID if set
|
||||
if (transportConfig.nodeId == AUTO || transportAssignNodeID(transportConfig.nodeId)) {
|
||||
if (_transportConfig.nodeId == AUTO || transportAssignNodeID(_transportConfig.nodeId)) {
|
||||
// if node ID valid (>0 and <255), proceed to next state
|
||||
transportSwitchSM(stParent);
|
||||
}
|
||||
@@ -127,14 +127,14 @@ void stParentTransition(void) {
|
||||
#if defined(MY_PARENT_NODE_IS_STATIC)
|
||||
TRANSPORT_DEBUG(PSTR("TSM:FPAR:STATP=%d\n"), (uint8_t)MY_PARENT_NODE_ID); // static parent
|
||||
_transportSM.findingParentNode = false;
|
||||
transportConfig.distanceGW = 1u; // assumption, CHKUPL:GWDC will update this variable
|
||||
transportConfig.parentNodeId = (uint8_t)MY_PARENT_NODE_ID;
|
||||
_transportConfig.distanceGW = 1u; // assumption, CHKUPL:GWDC will update this variable
|
||||
_transportConfig.parentNodeId = (uint8_t)MY_PARENT_NODE_ID;
|
||||
// save parent ID to eeprom (for bootloader)
|
||||
hwWriteConfig(EEPROM_PARENT_NODE_ID_ADDRESS, (uint8_t)MY_PARENT_NODE_ID);
|
||||
#else
|
||||
_transportSM.findingParentNode = true;
|
||||
transportConfig.distanceGW = DISTANCE_INVALID; // Set distance to max and invalidate parent node ID
|
||||
transportConfig.parentNodeId = AUTO;
|
||||
_transportConfig.distanceGW = DISTANCE_INVALID; // Set distance to max and invalidate parent node ID
|
||||
_transportConfig.parentNodeId = AUTO;
|
||||
// Broadcast find parent request
|
||||
(void)transportRouteMessage(build(_msgTmp, BROADCAST_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_REQUEST).set(""));
|
||||
#endif
|
||||
@@ -149,7 +149,7 @@ void stParentUpdate(void) {
|
||||
#else
|
||||
if (transportTimeInState() > MY_TRANSPORT_STATE_TIMEOUT_MS || _transportSM.preferredParentFound) {
|
||||
// timeout or preferred parent found
|
||||
if (transportConfig.parentNodeId != AUTO) {
|
||||
if (_transportConfig.parentNodeId != AUTO) {
|
||||
// parent assigned
|
||||
TRANSPORT_DEBUG(PSTR("TSM:FPAR:OK\n")); // find parent ok
|
||||
_transportSM.findingParentNode = false;
|
||||
@@ -179,7 +179,7 @@ void stParentUpdate(void) {
|
||||
// stID: verify and request ID if necessary
|
||||
void stIDTransition(void) {
|
||||
TRANSPORT_DEBUG(PSTR("TSM:ID\n")); // verify/request node ID
|
||||
if (transportConfig.nodeId == AUTO) {
|
||||
if (_transportConfig.nodeId == AUTO) {
|
||||
// send ID request
|
||||
setIndication(INDICATION_REQ_NODEID);
|
||||
TRANSPORT_DEBUG(PSTR("TSM:ID:REQ\n")); // request node ID
|
||||
@@ -188,7 +188,7 @@ void stIDTransition(void) {
|
||||
}
|
||||
|
||||
void stIDUpdate(void) {
|
||||
if (transportConfig.nodeId != AUTO) {
|
||||
if (_transportConfig.nodeId != AUTO) {
|
||||
// current node ID is valid
|
||||
TRANSPORT_DEBUG(PSTR("TSM:ID:OK\n"));
|
||||
setIndication(INDICATION_GOT_NODEID);
|
||||
@@ -227,9 +227,9 @@ void stUplinkUpdate(void) {
|
||||
// uplink ok, i.e. GW replied
|
||||
TRANSPORT_DEBUG(PSTR("TSM:UPL:OK\n")); // uplink ok
|
||||
//_transportSM.pingActive = false; ==> set false upon receiving I_PONG
|
||||
if (_transportSM.pingResponse != transportConfig.distanceGW) {
|
||||
TRANSPORT_DEBUG(PSTR("TSM:UPL:DGWC,O=%d,N=%d\n"), transportConfig.distanceGW, _transportSM.pingResponse); // distance to GW changed
|
||||
transportConfig.distanceGW = _transportSM.pingResponse;
|
||||
if (_transportSM.pingResponse != _transportConfig.distanceGW) {
|
||||
TRANSPORT_DEBUG(PSTR("TSM:UPL:DGWC,O=%d,N=%d\n"), _transportConfig.distanceGW, _transportSM.pingResponse); // distance to GW changed
|
||||
_transportConfig.distanceGW = _transportSM.pingResponse;
|
||||
}
|
||||
transportSwitchSM(stReady); // proceed to next state
|
||||
}
|
||||
@@ -263,7 +263,7 @@ void stReadyTransition(void) {
|
||||
if (transportOk_cb) {
|
||||
transportOk_cb();
|
||||
}
|
||||
TRANSPORT_DEBUG(PSTR("TSM:READY:PARAM,ID=%d,PAR=%d,DIS=%d\n"), transportConfig.nodeId, transportConfig.parentNodeId, transportConfig.distanceGW);
|
||||
TRANSPORT_DEBUG(PSTR("TSM:READY:PARAM,ID=%d,PAR=%d,DIS=%d\n"), _transportConfig.nodeId, _transportConfig.parentNodeId, _transportConfig.distanceGW);
|
||||
}
|
||||
|
||||
// stReadyUpdate: monitors link
|
||||
@@ -393,9 +393,9 @@ bool transportCheckUplink(const bool force) {
|
||||
_transportSM.lastUplinkCheck = hwMillis();
|
||||
TRANSPORT_DEBUG(PSTR("TSF:CKU:OK\n"));
|
||||
// did distance to GW change upstream, eg. re-routing of uplink nodes
|
||||
if (hopsCount != transportConfig.distanceGW) {
|
||||
TRANSPORT_DEBUG(PSTR("TSF:CKU:DGWC,O=%d,N=%d\n"), transportConfig.distanceGW, hopsCount); // distance to GW changed
|
||||
transportConfig.distanceGW = hopsCount;
|
||||
if (hopsCount != _transportConfig.distanceGW) {
|
||||
TRANSPORT_DEBUG(PSTR("TSF:CKU:DGWC,O=%d,N=%d\n"), _transportConfig.distanceGW, hopsCount); // distance to GW changed
|
||||
_transportConfig.distanceGW = hopsCount;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -408,7 +408,7 @@ bool transportCheckUplink(const bool force) {
|
||||
bool transportAssignNodeID(const uint8_t newNodeId) {
|
||||
// verify if ID valid
|
||||
if (newNodeId != GATEWAY_ADDRESS && newNodeId != AUTO) {
|
||||
transportConfig.nodeId = newNodeId;
|
||||
_transportConfig.nodeId = newNodeId;
|
||||
transportSetAddress(newNodeId);
|
||||
// Write ID to EEPROM
|
||||
hwWriteConfig(EEPROM_NODE_ID_ADDRESS, newNodeId);
|
||||
@@ -418,14 +418,14 @@ bool transportAssignNodeID(const uint8_t newNodeId) {
|
||||
else {
|
||||
TRANSPORT_DEBUG(PSTR("!TSF:SID:FAIL,ID=%d\n"),newNodeId); // ID is invalid, cannot assign ID
|
||||
setIndication(INDICATION_ERR_NET_FULL);
|
||||
transportConfig.nodeId = AUTO;
|
||||
_transportConfig.nodeId = AUTO;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool transportRouteMessage(MyMessage &message) {
|
||||
const uint8_t destination = message.destination;
|
||||
uint8_t route = transportConfig.parentNodeId; // by default, all traffic is routed via parent node
|
||||
uint8_t route = _transportConfig.parentNodeId; // by default, all traffic is routed via parent node
|
||||
|
||||
if (_transportSM.findingParentNode && destination != BROADCAST_ADDRESS) {
|
||||
TRANSPORT_DEBUG(PSTR("!TSF:RTE:FPAR ACTIVE\n")); // find parent active, message not sent
|
||||
@@ -434,7 +434,7 @@ bool transportRouteMessage(MyMessage &message) {
|
||||
}
|
||||
|
||||
if (destination == GATEWAY_ADDRESS) {
|
||||
route = transportConfig.parentNodeId; // message to GW always routes via parent
|
||||
route = _transportConfig.parentNodeId; // message to GW always routes via parent
|
||||
}
|
||||
else if (destination == BROADCAST_ADDRESS) {
|
||||
route = BROADCAST_ADDRESS; // message to BC does not require routing
|
||||
@@ -446,9 +446,9 @@ bool transportRouteMessage(MyMessage &message) {
|
||||
if (route == AUTO) {
|
||||
TRANSPORT_DEBUG(PSTR("!TSF:RTE:%d UNKNOWN\n"), destination); // route unknown
|
||||
#if !defined(MY_GATEWAY_FEATURE)
|
||||
if (message.last != transportConfig.parentNodeId) {
|
||||
if (message.last != _transportConfig.parentNodeId) {
|
||||
// message not from parent, i.e. child node - route it to parent
|
||||
route = transportConfig.parentNodeId;
|
||||
route = _transportConfig.parentNodeId;
|
||||
}
|
||||
else {
|
||||
// route unknown and msg received from parent, send it to destination assuming in rx radius
|
||||
@@ -460,14 +460,14 @@ bool transportRouteMessage(MyMessage &message) {
|
||||
#endif
|
||||
}
|
||||
#else
|
||||
route = transportConfig.parentNodeId; // not a repeater, all traffic routed via parent
|
||||
route = _transportConfig.parentNodeId; // not a repeater, all traffic routed via parent
|
||||
#endif
|
||||
}
|
||||
// send message
|
||||
const bool result = transportSendWrite(route, message);
|
||||
#if !defined(MY_GATEWAY_FEATURE)
|
||||
// update counter
|
||||
if (route == transportConfig.parentNodeId) {
|
||||
if (route == _transportConfig.parentNodeId) {
|
||||
if (!result) {
|
||||
setIndication(INDICATION_ERR_TX);
|
||||
_transportSM.failedUplinkTransmissions++;
|
||||
@@ -496,16 +496,16 @@ bool transportSendRoute(MyMessage &message) {
|
||||
}
|
||||
|
||||
// only be used inside transport
|
||||
bool transportWait(const uint32_t ms, const uint8_t cmd, const uint8_t msgtype){
|
||||
const uint32_t enter = hwMillis();
|
||||
bool transportWait(const uint32_t waitingMS, const uint8_t cmd, const uint8_t msgType){
|
||||
const uint32_t enterMS = hwMillis();
|
||||
// invalidate msg type
|
||||
_msg.type = !msgtype;
|
||||
_msg.type = !msgType;
|
||||
bool expectedResponse = false;
|
||||
while ((hwMillis() - enter < ms) && !expectedResponse) {
|
||||
while ((hwMillis() - enterMS < waitingMS) && !expectedResponse) {
|
||||
// process incoming messages
|
||||
transportProcessFIFO();
|
||||
doYield();
|
||||
expectedResponse = (mGetCommand(_msg) == cmd && _msg.type == msgtype);
|
||||
expectedResponse = (mGetCommand(_msg) == cmd && _msg.type == msgType);
|
||||
}
|
||||
return expectedResponse;
|
||||
}
|
||||
@@ -513,7 +513,7 @@ bool transportWait(const uint32_t ms, const uint8_t cmd, const uint8_t msgtype){
|
||||
uint8_t transportPingNode(const uint8_t targetId) {
|
||||
if(!_transportSM.pingActive){
|
||||
TRANSPORT_DEBUG(PSTR("TSF:PNG:SEND,TO=%d\n"), targetId);
|
||||
if(targetId == transportConfig.nodeId) {
|
||||
if(targetId == _transportConfig.nodeId) {
|
||||
// pinging self
|
||||
_transportSM.pingResponse = 0u;
|
||||
}
|
||||
@@ -581,7 +581,7 @@ void transportProcessMessage(void) {
|
||||
// update routing table if msg not from parent
|
||||
#if defined(MY_REPEATER_FEATURE)
|
||||
#if !defined(MY_GATEWAY_FEATURE)
|
||||
if (last != transportConfig.parentNodeId) {
|
||||
if (last != _transportConfig.parentNodeId) {
|
||||
#else
|
||||
// GW doesn't have parent
|
||||
{
|
||||
@@ -595,7 +595,7 @@ void transportProcessMessage(void) {
|
||||
_transportSM.msgReceived = true;
|
||||
|
||||
// Is message addressed to this node?
|
||||
if (destination == transportConfig.nodeId) {
|
||||
if (destination == _transportConfig.nodeId) {
|
||||
// prevent buffer overflow by limiting max. possible message length (5 bits=31 bytes max) to MAX_PAYLOAD (25 bytes)
|
||||
mSetLength(_msg, min(mGetLength(_msg),(uint8_t)MAX_PAYLOAD));
|
||||
// null terminate data
|
||||
@@ -606,7 +606,7 @@ void transportProcessMessage(void) {
|
||||
_msgTmp = _msg; // Copy message
|
||||
mSetRequestAck(_msgTmp, false); // Reply without ack flag (otherwise we would end up in an eternal loop)
|
||||
mSetAck(_msgTmp, true); // set ACK flag
|
||||
_msgTmp.sender = transportConfig.nodeId;
|
||||
_msgTmp.sender = _transportConfig.nodeId;
|
||||
_msgTmp.destination = sender;
|
||||
// send ACK, use transportSendRoute since ACK reply is not internal, i.e. if !transportOK do not reply
|
||||
(void)transportSendRoute(_msgTmp);
|
||||
@@ -634,15 +634,15 @@ void transportProcessMessage(void) {
|
||||
if (isValidDistance(distance)) {
|
||||
distance++; // Distance to gateway is one more for us w.r.t. parent
|
||||
// update settings if distance shorter or preferred parent found
|
||||
if (((isValidDistance(distance) && distance < transportConfig.distanceGW) || (!_autoFindParent && sender == (uint8_t)MY_PARENT_NODE_ID)) && !_transportSM.preferredParentFound) {
|
||||
if (((isValidDistance(distance) && distance < _transportConfig.distanceGW) || (!_autoFindParent && sender == (uint8_t)MY_PARENT_NODE_ID)) && !_transportSM.preferredParentFound) {
|
||||
// Found a neighbor closer to GW than previously found
|
||||
if (!_autoFindParent && sender == (uint8_t)MY_PARENT_NODE_ID) {
|
||||
_transportSM.preferredParentFound = true;
|
||||
TRANSPORT_DEBUG(PSTR("TSF:MSG:FPAR PREF\n")); // find parent, preferred parent found
|
||||
}
|
||||
transportConfig.distanceGW = distance;
|
||||
transportConfig.parentNodeId = sender;
|
||||
TRANSPORT_DEBUG(PSTR("TSF:MSG:FPAR OK,ID=%d,D=%d\n"), transportConfig.parentNodeId, transportConfig.distanceGW);
|
||||
_transportConfig.distanceGW = distance;
|
||||
_transportConfig.parentNodeId = sender;
|
||||
TRANSPORT_DEBUG(PSTR("TSF:MSG:FPAR OK,ID=%d,D=%d\n"), _transportConfig.parentNodeId, _transportConfig.distanceGW);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -704,7 +704,7 @@ void transportProcessMessage(void) {
|
||||
// only reply if node is fully operational
|
||||
if (type == I_FIND_PARENT_REQUEST) {
|
||||
#if defined(MY_REPEATER_FEATURE)
|
||||
if (sender != transportConfig.parentNodeId) { // no circular reference
|
||||
if (sender != _transportConfig.parentNodeId) { // no circular reference
|
||||
TRANSPORT_DEBUG(PSTR("TSF:MSG:FPAR REQ,ID=%d\n"), sender); // FPAR: find parent request
|
||||
// check if uplink functional - node can only be parent node if link to GW functional
|
||||
// this also prevents circular references in case GW ooo
|
||||
@@ -713,7 +713,7 @@ void transportProcessMessage(void) {
|
||||
TRANSPORT_DEBUG(PSTR("TSF:MSG:GWL OK\n")); // GW uplink ok
|
||||
// random delay minimizes collisions
|
||||
delay(hwMillis() & 0x3ff);
|
||||
(void)transportRouteMessage(build(_msgTmp, sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE).set(transportConfig.distanceGW));
|
||||
(void)transportRouteMessage(build(_msgTmp, sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE).set(_transportConfig.distanceGW));
|
||||
}
|
||||
else {
|
||||
TRANSPORT_DEBUG(PSTR("!TSF:MSG:GWL FAIL\n")); // GW uplink fail, do not respond to parent request
|
||||
@@ -728,10 +728,10 @@ void transportProcessMessage(void) {
|
||||
}
|
||||
#if !defined(MY_GATEWAY_FEATURE)
|
||||
if (type == I_DISCOVER_REQUEST) {
|
||||
if (last == transportConfig.parentNodeId) {
|
||||
if (last == _transportConfig.parentNodeId) {
|
||||
// random wait to minimize collisions
|
||||
delay(hwMillis() & 0x3ff);
|
||||
(void)transportRouteMessage(build(_msgTmp, sender, NODE_SENSOR_ID, C_INTERNAL, I_DISCOVER_RESPONSE).set(transportConfig.parentNodeId));
|
||||
(void)transportRouteMessage(build(_msgTmp, sender, NODE_SENSOR_ID, C_INTERNAL, I_DISCOVER_RESPONSE).set(_transportConfig.parentNodeId));
|
||||
// no return here (for fwd if repeater)
|
||||
}
|
||||
}
|
||||
@@ -740,7 +740,7 @@ void transportProcessMessage(void) {
|
||||
// controlled BC relay
|
||||
#if defined(MY_REPEATER_FEATURE)
|
||||
// controlled BC repeating: forward only if message received from parent and sender not self to prevent circular fwds
|
||||
if(last == transportConfig.parentNodeId && sender != transportConfig.nodeId && isTransportReady()){
|
||||
if(last == _transportConfig.parentNodeId && sender != _transportConfig.nodeId && isTransportReady()){
|
||||
TRANSPORT_DEBUG(PSTR("TSF:MSG:FWD BC MSG\n")); // controlled broadcast msg forwarding
|
||||
(void)transportRouteMessage(_msg);
|
||||
}
|
||||
@@ -750,7 +750,7 @@ void transportProcessMessage(void) {
|
||||
if (command != C_INTERNAL) {
|
||||
#if !defined(MY_GATEWAY_FEATURE)
|
||||
// only proceed if message received from parent
|
||||
if (last != transportConfig.parentNodeId) {
|
||||
if (last != _transportConfig.parentNodeId) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
@@ -825,7 +825,7 @@ void transportProcessFIFO(void) {
|
||||
}
|
||||
|
||||
bool transportSendWrite(const uint8_t to, MyMessage &message) {
|
||||
message.last = transportConfig.nodeId; // Update last
|
||||
message.last = _transportConfig.nodeId; // Update last
|
||||
// sign message if required
|
||||
if (!signerSignMsg(message)) {
|
||||
TRANSPORT_DEBUG(PSTR("!TSF:MSG:SIGN FAIL\n"));
|
||||
@@ -849,22 +849,22 @@ bool transportSendWrite(const uint8_t to, MyMessage &message) {
|
||||
return result;
|
||||
}
|
||||
|
||||
void transportRegisterTransportOkCallback(transportCallback_t cb)
|
||||
void transportRegisterOkCallback(transportCallback_t cb)
|
||||
{
|
||||
transportOk_cb = cb;
|
||||
}
|
||||
|
||||
uint8_t transportGetNodeId(void)
|
||||
{
|
||||
return transportConfig.nodeId;
|
||||
return _transportConfig.nodeId;
|
||||
}
|
||||
uint8_t transportGetParentNodeId(void)
|
||||
{
|
||||
return transportConfig.parentNodeId;
|
||||
return _transportConfig.parentNodeId;
|
||||
}
|
||||
uint8_t transportGetDistanceGW(void)
|
||||
{
|
||||
return transportConfig.distanceGW;
|
||||
return _transportConfig.distanceGW;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user