Skip to content

Commit 6fdb52e

Browse files
committed
Add bridge transport feature
1 parent 2a82738 commit 6fdb52e

File tree

5 files changed

+460
-23
lines changed

5 files changed

+460
-23
lines changed

core/MyEepromAddresses.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* The MySensors Arduino library handles the wireless radio link and protocol
33
* between your home built sensors/actuators and HA controller of choice.
44
* The sensors forms a self healing radio network with optional repeaters. Each
@@ -35,7 +35,7 @@
3535
#define SIZE_NODE_ID (1u) //!< Size node ID
3636
#define SIZE_PARENT_NODE_ID (1u) //!< Size parent node ID
3737
#define SIZE_DISTANCE (1u) //!< Size GW distance
38-
#define SIZE_ROUTES (256u) //!< Size routing table
38+
#define SIZE_ROUTES (256u) //!< Size routing table and bridged addresses
3939
#define SIZE_CONTROLLER_CONFIG (23u) //!< Size controller config
4040
#define SIZE_PERSONALIZATION_CHECKSUM (1u) //!< Size personalization checksum
4141
#define SIZE_FIRMWARE_TYPE (2u) //!< Size firmware type
@@ -84,8 +84,10 @@
8484
#define EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS (EEPROM_SIGNING_SOFT_SERIAL_ADDRESS + SIZE_SIGNING_SOFT_SERIAL)
8585
/** @brief Address node lock counter. This is set with @ref SecurityPersonalizer.ino */
8686
#define EEPROM_NODE_LOCK_COUNTER_ADDRESS (EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS + SIZE_RF_ENCRYPTION_AES_KEY)
87+
/** @brief Address routing table from and to bridge media */
88+
#define EEPROM_BRIDGED_ADDRESS (EEPROM_NODE_LOCK_COUNTER_ADDRESS + SIZE_NODE_LOCK_COUNTER)
8789
/** @brief First free address for sketch static configuration */
88-
#define EEPROM_LOCAL_CONFIG_ADDRESS (EEPROM_NODE_LOCK_COUNTER_ADDRESS + SIZE_NODE_LOCK_COUNTER)
90+
#define EEPROM_LOCAL_CONFIG_ADDRESS (EEPROM_BRIDGED_ADDRESS + SIZE_ROUTES)
8991

9092
#endif // MyEepromAddresses_h
9193

core/MyTransport.cpp

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ void stInitTransition(void)
9898
void stInitUpdate(void)
9999
{
100100
// initialise radio
101-
if (!transportHALInit()) {
101+
#if defined(MY_BRIDGE_FEATURE)
102+
if (!transportInit() || !bridgeTransportHALInit()) {
103+
#else
104+
if (!transportInit()) {
105+
#endif
102106
TRANSPORT_DEBUG(PSTR("!TSM:INIT:TSP FAIL\n"));
103107
setIndication(INDICATION_ERR_INIT_TRANSPORT);
104108
transportSwitchSM(stFailure);
@@ -438,6 +442,9 @@ void transportReInitialise(void)
438442
TRANSPORT_DEBUG(PSTR("TSF:TRI:TPU\n")); // transport power up
439443
transportHALPowerUp();
440444
transportHALSetAddress(_transportConfig.nodeId);
445+
#if defined(MY_BRIDGE_FEATURE)
446+
bridgeTransportHALSetAddress(_transportConfig.nodeId);
447+
#endif
441448
} else {
442449
TRANSPORT_DEBUG(PSTR("TSF:TRI:TSB\n")); // transport standby
443450
transportHALStandBy();
@@ -499,6 +506,9 @@ bool transportAssignNodeID(const uint8_t newNodeId)
499506
if (newNodeId != GATEWAY_ADDRESS && newNodeId != AUTO) {
500507
_transportConfig.nodeId = newNodeId;
501508
transportHALSetAddress(newNodeId);
509+
#if defined(MY_BRIDGE_FEATURE)
510+
bridgeTransportHALSetAddress(newNodeId);
511+
#endif
502512
// Write ID to EEPROM
503513
hwWriteConfig(EEPROM_NODE_ID_ADDRESS, newNodeId);
504514
TRANSPORT_DEBUG(PSTR("TSF:SID:OK,ID=%" PRIu8 "\n"),newNodeId); // Node ID assigned
@@ -513,6 +523,7 @@ bool transportAssignNodeID(const uint8_t newNodeId)
513523
bool transportRouteMessage(MyMessage &message)
514524
{
515525
const uint8_t destination = message.getDestination();
526+
bool routeToBridge = false;
516527

517528
if (_transportSM.findingParentNode && destination != BROADCAST_ADDRESS) {
518529
TRANSPORT_DEBUG(PSTR("!TSF:RTE:FPAR ACTIVE\n")); // find parent active, message not sent
@@ -529,7 +540,7 @@ bool transportRouteMessage(MyMessage &message)
529540
} else {
530541
#if defined(MY_REPEATER_FEATURE)
531542
// destination not GW & not BC, get route
532-
route = transportGetRoute(destination);
543+
route = transportGetRoute(destination, &routeToBridge);
533544
if (route == AUTO) {
534545
TRANSPORT_DEBUG(PSTR("!TSF:RTE:%" PRIu8 " UNKNOWN\n"), destination); // route unknown
535546
#if !defined(MY_GATEWAY_FEATURE)
@@ -562,7 +573,7 @@ bool transportRouteMessage(MyMessage &message)
562573
#endif
563574
}
564575
// send message
565-
const bool result = transportSendWrite(route, message);
576+
const bool result = transportSendWrite(route, message, routeToBridge);
566577
#if !defined(MY_GATEWAY_FEATURE)
567578
// update counter
568579
if (route == _transportConfig.parentNodeId) {
@@ -647,16 +658,23 @@ uint32_t transportGetHeartbeat(void)
647658
return transportTimeInState();
648659
}
649660

650-
void transportProcessMessage(void)
661+
void transportProcessMessage(bool fromBridge)
651662
{
652663
// Manage signing timeout
653664
(void)signerCheckTimer();
654665
// receive message
655666
setIndication(INDICATION_RX);
656667
uint8_t payloadLength;
657-
// last is the first byte of the payload buffer
658-
if (!transportHALReceive(&_msg, &payloadLength)) {
659-
return;
668+
if (fromBridge) {
669+
#if defined(MY_BRIDGE_FEATURE)
670+
if (!bridgeTransportHALReceive(&_msg, &payloadLength)) {
671+
return;
672+
}
673+
#endif
674+
} else {
675+
if (!transportHALReceive(&_msg, &payloadLength)) {
676+
return;
677+
}
660678
}
661679
// get message length and limit size
662680
const uint8_t msgLength = _msg.getLength();
@@ -692,7 +710,7 @@ void transportProcessMessage(void)
692710
// Message is from one of the child nodes and not sent from this node. Add it to routing table.
693711
if (sender != _transportConfig.nodeId)
694712
{
695-
transportSetRoute(sender, last);
713+
transportSetRoute(sender, last, fromBridge);
696714
}
697715
}
698716
#endif // MY_REPEATER_FEATURE
@@ -950,7 +968,11 @@ void transportInvokeSanityCheck(void)
950968
{
951969
// Suppress this because the function may return a variable value in some configurations
952970
// cppcheck-suppress knownConditionTrueFalse
971+
#if defined(MY_BRIDGE_FEATURE)
972+
if (!transportHALSanityCheck() || !bridgeTransportHALSanityCheck()) {
973+
#else
953974
if (!transportHALSanityCheck()) {
975+
#endif
954976
TRANSPORT_DEBUG(PSTR("!TSF:SAN:FAIL\n")); // sanity check fail
955977
transportSwitchSM(stFailure);
956978
} else {
@@ -975,8 +997,15 @@ void transportProcessFIFO(void)
975997
uint8_t _processedMessages = MAX_SUBSEQ_MSGS;
976998
// process all msgs in FIFO or counter exit
977999
while (transportHALDataAvailable() && _processedMessages--) {
978-
transportProcessMessage();
1000+
transportProcessMessage(false);
1001+
}
1002+
#if defined(MY_BRIDGE_FEATURE)
1003+
_processedMessages = MAX_SUBSEQ_MSGS;
1004+
// process all msgs in FIFO or counter exit
1005+
while (bridgeTransportHALDataAvailable() && _processedMessages--) {
1006+
transportProcessMessage(true);
9791007
}
1008+
#endif
9801009
#if defined(MY_OTA_FIRMWARE_FEATURE)
9811010
if (isTransportReady()) {
9821011
// only process if transport ok
@@ -985,7 +1014,7 @@ void transportProcessFIFO(void)
9851014
#endif
9861015
}
9871016

988-
bool transportSendWrite(const uint8_t to, MyMessage &message)
1017+
bool transportSendWrite(const uint8_t to, MyMessage &message, const bool routeToBridge)
9891018
{
9901019
message.setLast(_transportConfig.nodeId); // Update last
9911020

@@ -1002,8 +1031,16 @@ bool transportSendWrite(const uint8_t to, MyMessage &message)
10021031
const bool noACK = _transportConfig.passiveMode || (to == BROADCAST_ADDRESS);
10031032
// send
10041033
setIndication(INDICATION_TX);
1005-
const bool result = transportHALSend(to, &message, totalMsgLength,
1006-
noACK);
1034+
bool result = false;
1035+
// if to is BC (AUTO) then send to both transport and bridge transport media
1036+
#if defined(MY_BRIDGE_FEATURE)
1037+
if (routeToBridge || to == AUTO) {
1038+
result = bridgeTransportHALSend(to, &message, totalMsgLength, noACK);
1039+
}
1040+
#endif
1041+
if (!routeToBridge || to == AUTO) {
1042+
result = transportHALSend(to, &message, totalMsgLength, noACK);
1043+
}
10071044

10081045
TRANSPORT_DEBUG(PSTR("%sTSF:MSG:SEND,%" PRIu8 "-%" PRIu8 "-%" PRIu8 "-%" PRIu8 ",s=%" PRIu8 ",c=%"
10091046
PRIu8 ",t=%" PRIu8 ",pt=%" PRIu8 ",l=%" PRIu8 ",sg=%" PRIu8 ",ft=%" PRIu8 ",st=%s:%s\n"),
@@ -1043,7 +1080,7 @@ uint8_t transportGetDistanceGW(void)
10431080
void transportClearRoutingTable(void)
10441081
{
10451082
for (uint16_t i = 0; i < SIZE_ROUTES; i++) {
1046-
transportSetRoute((uint8_t)i, BROADCAST_ADDRESS);
1083+
transportSetRoute((uint8_t)i, BROADCAST_ADDRESS, false);
10471084
}
10481085
transportSaveRoutingTable(); // save cleared routing table to EEPROM (if feature enabled)
10491086
TRANSPORT_DEBUG(PSTR("TSF:CRT:OK\n")); // clear routing table
@@ -1053,6 +1090,8 @@ void transportLoadRoutingTable(void)
10531090
{
10541091
#if defined(MY_RAM_ROUTING_TABLE_ENABLED)
10551092
hwReadConfigBlock((void*)&_transportRoutingTable.route, (void*)EEPROM_ROUTES_ADDRESS, SIZE_ROUTES);
1093+
hwReadConfigBlock((void*)&_transportRoutingTable.bridged, (void*)EEPROM_BRIDGED_ADDRESS,
1094+
SIZE_ROUTES);
10561095
TRANSPORT_DEBUG(PSTR("TSF:LRT:OK\n")); // load routing table
10571096
#endif
10581097
}
@@ -1061,24 +1100,32 @@ void transportSaveRoutingTable(void)
10611100
{
10621101
#if defined(MY_RAM_ROUTING_TABLE_ENABLED)
10631102
hwWriteConfigBlock((void*)&_transportRoutingTable.route, (void*)EEPROM_ROUTES_ADDRESS, SIZE_ROUTES);
1103+
hwWriteConfigBlock((void*)&_transportRoutingTable.bridged, (void*)EEPROM_BRIDGED_ADDRESS,
1104+
SIZE_ROUTES);
10641105
TRANSPORT_DEBUG(PSTR("TSF:SRT:OK\n")); // save routing table
10651106
#endif
10661107
}
10671108

1068-
void transportSetRoute(const uint8_t node, const uint8_t route)
1109+
void transportSetRoute(const uint8_t node, const uint8_t route, const bool fromBridge)
10691110
{
10701111
#if defined(MY_RAM_ROUTING_TABLE_ENABLED)
10711112
_transportRoutingTable.route[node] = route;
1113+
_transportRoutingTable.bridged[node] = fromBridge;
10721114
#else
10731115
hwWriteConfig(EEPROM_ROUTES_ADDRESS + node, route);
10741116
#endif
10751117
}
10761118

1077-
uint8_t transportGetRoute(const uint8_t node)
1119+
uint8_t transportGetRoute(const uint8_t node, bool* toBridge)
10781120
{
10791121
uint8_t result;
10801122
#if defined(MY_RAM_ROUTING_TABLE_ENABLED)
10811123
result = _transportRoutingTable.route[node];
1124+
#if defined(MY_BRIDGE_FEATURE)
1125+
*toBridge = _transportRoutingTable.bridged[node];
1126+
#else
1127+
*toBridge = false;
1128+
#endif
10821129
#else
10831130
result = hwReadConfig(EEPROM_ROUTES_ADDRESS + node);
10841131
#endif
@@ -1089,7 +1136,8 @@ void transportReportRoutingTable(void)
10891136
{
10901137
#if defined(MY_REPEATER_FEATURE)
10911138
for (uint16_t cnt = 0; cnt < SIZE_ROUTES; cnt++) {
1092-
const uint8_t route = transportGetRoute(cnt);
1139+
bool toBridge;
1140+
const uint8_t route = transportGetRoute((uint8_t)cnt, &toBridge);
10931141
if (route != BROADCAST_ADDRESS) {
10941142
TRANSPORT_DEBUG(PSTR("TSF:RRT:ROUTE N=%" PRIu8 ",R=%" PRIu8 "\n"), cnt, route);
10951143
uint8_t outBuf[2] = { (uint8_t)cnt,route };

core/MyTransport.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ typedef struct {
317317
*/
318318
typedef struct {
319319
uint8_t route[SIZE_ROUTES]; //!< route for node
320+
bool bridged[SIZE_ROUTES]; //!< route via bridge
320321
} routingTable_t;
321322

322323
// PRIVATE functions
@@ -394,7 +395,7 @@ void transportProcessFIFO(void);
394395
/**
395396
* @brief Receive message from RX FIFO and process
396397
*/
397-
void transportProcessMessage(void);
398+
void transportProcessMessage(bool fromBridge = false);
398399
/**
399400
* @brief Assign node ID
400401
* @param newNodeId New node ID
@@ -436,7 +437,7 @@ bool transportSendRoute(MyMessage &message);
436437
* @param message
437438
* @return true if message sent successfully
438439
*/
439-
bool transportSendWrite(const uint8_t to, MyMessage &message);
440+
bool transportSendWrite(const uint8_t to, MyMessage &message, const bool routeToBridge);
440441
/**
441442
* @brief Check uplink to GW, includes flooding control
442443
* @param force to override flood control timer
@@ -508,13 +509,13 @@ void transportSaveRoutingTable(void);
508509
* @param node
509510
* @param route
510511
*/
511-
void transportSetRoute(const uint8_t node, const uint8_t route);
512+
void transportSetRoute(const uint8_t node, const uint8_t route, const bool fromBridge);
512513
/**
513514
* @brief Load route to node
514515
* @param node
515516
* @return route to node
516517
*/
517-
uint8_t transportGetRoute(const uint8_t node);
518+
uint8_t transportGetRoute(const uint8_t node, bool* toBridge);
518519
/**
519520
* @brief Reports content of routing table
520521
*/

0 commit comments

Comments
 (0)