Skip to content

Commit d254a35

Browse files
committed
Multitransport implementation
1 parent 8103c6c commit d254a35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3908
-857
lines changed

MyConfig.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@
570570
* @brief Declare the amount of incoming messages that can be buffered at driver level.
571571
*/
572572
#ifndef MY_NRF5_ESB_RX_BUFFER_SIZE
573-
#define MY_NRF5_ESB_RX_BUFFER_SIZE (20)
573+
#define MY_NRF5_ESB_RX_BUFFER_SIZE (5)
574574
#endif
575575

576576
/**
@@ -2190,7 +2190,7 @@
21902190
#define MY_DEBUG_VERBOSE_OTA_UPDATE //!< MY_DEBUG_VERBOSE_OTA_UPDATE
21912191
#endif
21922192

2193-
#if defined(MY_DEBUG) || defined(MY_DEBUG_VERBOSE_CORE) || defined(MY_DEBUG_VERBOSE_TRANSPORT) || defined(MY_DEBUG_VERBOSE_GATEWAY) || defined(MY_DEBUG_VERBOSE_SIGNING) || defined(MY_DEBUG_VERBOSE_OTA_UPDATE) || defined(MY_DEBUG_VERBOSE_RF24) || defined(MY_DEBUG_VERBOSE_NRF5_ESB) || defined(MY_DEBUG_VERBOSE_RFM69) || defined(MY_DEBUG_VERBOSE_RFM95) || defined(MY_DEBUG_VERBOSE_TRANSPORT_HAL)
2193+
#if defined(MY_DEBUG) || defined(MY_DEBUG_VERBOSE_CORE) || defined(MY_DEBUG_VERBOSE_TRANSPORT) || defined(MY_DEBUG_VERBOSE_GATEWAY) || defined(MY_DEBUG_VERBOSE_SIGNING) || defined(MY_DEBUG_VERBOSE_OTA_UPDATE) || defined(MY_DEBUG_VERBOSE_RF24) || defined(MY_DEBUG_VERBOSE_NRF5_ESB) || defined(MY_DEBUG_VERBOSE_RFM69) || defined(MY_DEBUG_VERBOSE_RFM95) || defined(MY_DEBUG_VERBOSE_TRANSPORT_HAL) || defined(MY_DEBUG_VERBOSE_TRANSPORT_ENCRYPTION)
21942194
#define DEBUG_OUTPUT_ENABLED //!< DEBUG_OUTPUT_ENABLED
21952195
#ifndef MY_DEBUG_OTA
21962196
#define DEBUG_OUTPUT(x,...) hwDebugPrint(x, ##__VA_ARGS__) //!< debug

MySensors.h

+29-10
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,18 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
286286
#define __RS485CNT 0 //!< __RS485CNT
287287
#endif
288288

289-
#if (__RF24CNT + __NRF5ESBCNT + __RFM69CNT + __RFM95CNT + __RS485CNT > 1)
290-
#error Only one forward link driver can be activated
289+
#define MY_TRANSPORT_COUNT (__RF24CNT + __NRF5ESBCNT + __RFM69CNT + __RFM95CNT + __RS485CNT)
290+
291+
#if (MY_TRANSPORT_COUNT > 1)
292+
// more than 1 transport requires RX queue
293+
#define MY_TRANSPORT_RX_QUEUE
294+
#else
295+
// RF24 + IRQ requires RX queue
296+
#if defined(MY_RADIO_RF24) && defined(MY_RF24_USE_INTERRUPTS)
297+
#define MY_TRANSPORT_RX_QUEUE
298+
#endif
291299
#endif
300+
292301
#endif //DOXYGEN
293302

294303
// SANITY CHECK
@@ -297,7 +306,7 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
297306
#endif
298307

299308
// TRANSPORT INCLUDES
300-
#if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB) || defined(MY_RADIO_RFM69) || defined(MY_RADIO_RFM95) || defined(MY_RS485)
309+
#if (MY_TRANSPORT_COUNT > 0)
301310
#include "hal/transport/MyTransportHAL.h"
302311
#include "core/MyTransport.h"
303312

@@ -353,39 +362,49 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
353362
#endif
354363
#endif
355364

365+
#if (defined(MY_RF24_ENABLE_ENCRYPTION) && defined(MY_RADIO_RF24)) || (defined(MY_NRF5_ESB_ENABLE_ENCRYPTION) && defined(MY_RADIO_NRF5_ESB)) || (defined(MY_RFM69_ENABLE_ENCRYPTION) && defined(MY_RADIO_RFM69)) || (defined(MY_RFM95_ENABLE_ENCRYPTION) && defined(MY_RADIO_RFM95))
366+
#define MY_TRANSPORT_ENCRYPTION //!< internal flag
367+
#include "hal/transport/MyTransportEncryption.cpp"
368+
#endif
369+
356370
// Transport drivers
357371
#if defined(MY_RADIO_RF24)
358372
#include "hal/transport/RF24/driver/RF24.cpp"
359373
#include "hal/transport/RF24/MyTransportRF24.cpp"
360-
#elif defined(MY_RADIO_NRF5_ESB)
374+
#endif
375+
#if defined(MY_RADIO_NRF5_ESB)
361376
#if !defined(ARDUINO_ARCH_NRF5)
362377
#error No support for nRF5 radio on this platform
363378
#endif
364379
#include "hal/transport/NRF5_ESB/driver/Radio.cpp"
365380
#include "hal/transport/NRF5_ESB/driver/Radio_ESB.cpp"
366381
#include "hal/transport/NRF5_ESB/MyTransportNRF5_ESB.cpp"
367-
#elif defined(MY_RS485)
382+
#endif
383+
#if defined(MY_RS485)
368384
#if !defined(MY_RS485_HWSERIAL)
369385
#if defined(__linux__)
370386
#error You must specify MY_RS485_HWSERIAL for RS485 transport
371387
#endif
372388
#include "drivers/AltSoftSerial/AltSoftSerial.cpp"
373389
#endif
374390
#include "hal/transport/RS485/MyTransportRS485.cpp"
375-
#elif defined(MY_RADIO_RFM69)
391+
#endif
392+
#if defined(MY_RADIO_RFM69)
376393
#if defined(MY_RFM69_NEW_DRIVER)
377394
#include "hal/transport/RFM69/driver/new/RFM69_new.cpp"
378395
#else
379396
#include "hal/transport/RFM69/driver/old/RFM69_old.cpp"
380397
#endif
381398
#include "hal/transport/RFM69/MyTransportRFM69.cpp"
382-
#elif defined(MY_RADIO_RFM95)
399+
#endif
400+
#if defined(MY_RADIO_RFM95)
401+
#if defined(MY_RFM95_RFM69_COMPATIBILITY)
402+
#include "hal/transport/RFM95/driver/RFM95_RFM69.cpp"
403+
#include "hal/transport/RFM95/MyTransportRFM95_RFM69.cpp"
404+
#else
383405
#include "hal/transport/RFM95/driver/RFM95.cpp"
384406
#include "hal/transport/RFM95/MyTransportRFM95.cpp"
385407
#endif
386-
387-
#if (defined(MY_RF24_ENABLE_ENCRYPTION) && defined(MY_RADIO_RF24)) || (defined(MY_NRF5_ESB_ENABLE_ENCRYPTION) && defined(MY_RADIO_NRF5_ESB)) || (defined(MY_RFM69_ENABLE_ENCRYPTION) && defined(MY_RADIO_RFM69)) || (defined(MY_RFM95_ENABLE_ENCRYPTION) && defined(MY_RADIO_RFM95))
388-
#define MY_TRANSPORT_ENCRYPTION //!< ïnternal flag
389408
#endif
390409

391410
#include "hal/transport/MyTransportHAL.cpp"

core/MyCapabilities.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,17 @@
8787
*
8888
* | Radio | Indicator
8989
* |--------------|----------
90+
* | Multiple | M
9091
* | nRF24/nRF5 | N
9192
* | %RFM69 (old) | R
9293
* | %RFM69 (new) | P
9394
* | RFM95 | L
9495
* | RS485 | S
9596
* | None | -
9697
*/
98+
#if (MY_TRANSPORT_COUNT > 1)
99+
#define MY_CAP_RADIO "M"
100+
#else
97101
#if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB)
98102
#define MY_CAP_RADIO "N"
99103
#elif defined(MY_RADIO_RFM69)
@@ -111,7 +115,7 @@
111115
#else
112116
#define MY_CAP_RADIO "-"
113117
#endif
114-
118+
#endif
115119
// Node type
116120
/**
117121
* @def MY_CAP_TYPE

core/MyHelperFunctions.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,33 @@ static char convertI2H(const uint8_t i)
3939
return 'A' + k - 10;
4040
}
4141
}
42+
43+
static int timingneutralMemcmp(const void* a, const void* b, size_t sz)
44+
{
45+
int retVal;
46+
size_t i;
47+
int done = 0;
48+
const uint8_t* ptrA = (const uint8_t*)a;
49+
const uint8_t* ptrB = (const uint8_t*)b;
50+
for (i = 0; i < sz; i++) {
51+
if (ptrA[i] == ptrB[i]) {
52+
if (done > 0) {
53+
done = 1;
54+
} else {
55+
done = 0;
56+
}
57+
} else {
58+
if (done > 0) {
59+
done = 2;
60+
} else {
61+
done = 3;
62+
}
63+
}
64+
}
65+
if (done > 0) {
66+
retVal = -1;
67+
} else {
68+
retVal = 0;
69+
}
70+
return retVal;
71+
}

core/MyHelperFunctions.h

+13
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,18 @@ static uint8_t convertH2I(const char c) __attribute__((unused));
3434
*/
3535
static char convertI2H(const uint8_t i) __attribute__((unused));
3636

37+
/**
38+
* @brief Do a timing neutral memory comparison.
39+
*
40+
* The function behaves similar to memcmp with the difference that it will
41+
* always use the same number of instructions for a given number of bytes,
42+
* no matter how the two buffers differ and the response is either 0 or -1.
43+
*
44+
* @param a First buffer for comparison.
45+
* @param b Second buffer for comparison.
46+
* @param sz The number of bytes to compare.
47+
* @returns 0 if buffers match, -1 if they do not.
48+
*/
49+
static int timingneutralMemcmp(const void* a, const void* b, size_t sz) __attribute__((unused));
3750

3851
#endif

core/MyOTALogging.h

-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
#ifndef MyOTALogging_h
3131
#define MyOTALogging_h
3232

33-
#include "MySensorsCore.h"
34-
#include "MyTransport.h"
35-
3633
/**
3734
* @brief Send a log message to a node
3835
*

core/MySensorsCore.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ void _nodeLock(const char *str)
788788
doYield();
789789
(void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID,C_INTERNAL, I_LOCKED).set(str));
790790
#if defined(MY_SENSOR_NETWORK)
791-
transportSleep();
791+
transportHALSleep();
792792
CORE_DEBUG(PSTR("MCO:NLK:TSL\n")); // sleep transport
793793
#endif
794794
setIndication(INDICATION_SLEEP);

core/MySensorsCore.h

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include <stdarg.h>
7676

7777
#define GATEWAY_ADDRESS ((uint8_t)0) //!< Node ID for GW sketch
78+
#define BROADCAST_ADDRESS ((uint8_t)255) //!< broadcasts are addressed to ID 255
7879
#define NODE_SENSOR_ID ((uint8_t)255) //!< Node child is always created/presented when a node is started
7980
#define MY_CORE_VERSION ((uint8_t)2) //!< core version
8081
#define MY_CORE_MIN_VERSION ((uint8_t)2) //!< min core version required for compatibility

core/MySigning.cpp

+5-30
Original file line numberDiff line numberDiff line change
@@ -321,36 +321,6 @@ bool signerVerifyMsg(MyMessage &msg)
321321
return verificationResult;
322322
}
323323

324-
int signerMemcmp(const void* a, const void* b, size_t sz)
325-
{
326-
int retVal;
327-
size_t i;
328-
int done = 0;
329-
const uint8_t* ptrA = (const uint8_t*)a;
330-
const uint8_t* ptrB = (const uint8_t*)b;
331-
for (i=0; i < sz; i++) {
332-
if (ptrA[i] == ptrB[i]) {
333-
if (done > 0) {
334-
done = 1;
335-
} else {
336-
done = 0;
337-
}
338-
} else {
339-
if (done > 0) {
340-
done = 2;
341-
} else {
342-
done = 3;
343-
}
344-
}
345-
}
346-
if (done > 0) {
347-
retVal = -1;
348-
} else {
349-
retVal = 0;
350-
}
351-
return retVal;
352-
}
353-
354324
#if defined(MY_SIGNING_FEATURE)
355325
// Helper function to centralize signing/verification exceptions
356326
// cppcheck-suppress constParameter
@@ -401,6 +371,11 @@ static void prepareSigningPresentation(MyMessage &msg, uint8_t destination)
401371
static bool signerInternalProcessPresentation(MyMessage &msg)
402372
{
403373
const uint8_t sender = msg.getSender();
374+
#if defined(MY_GATEWAY_FEATURE) && (F_CPU > 16*1000000ul)
375+
// let's slow down things a bit - on fast GWs we may send messages before slow node's radio switches from TX->RX
376+
delay(50);
377+
#endif
378+
404379
#if defined(MY_SIGNING_FEATURE)
405380
if (msg.data[0] != SIGNING_PRESENTATION_VERSION_1) {
406381
SIGN_DEBUG(PSTR("!SGN:PRE:VER=%" PRIu8 "\n"),

core/MySigning.h

-14
Original file line numberDiff line numberDiff line change
@@ -711,20 +711,6 @@ bool signerSignMsg(MyMessage &msg);
711711
*/
712712
bool signerVerifyMsg(MyMessage &msg);
713713

714-
/**
715-
* @brief Do a timing neutral memory comparison.
716-
*
717-
* The function behaves similar to memcmp with the difference that it will
718-
* always use the same number of instructions for a given number of bytes,
719-
* no matter how the two buffers differ and the response is either 0 or -1.
720-
*
721-
* @param a First buffer for comparison.
722-
* @param b Second buffer for comparison.
723-
* @param sz The number of bytes to compare.
724-
* @returns 0 if buffers match, -1 if they do not.
725-
*/
726-
int signerMemcmp(const void* a, const void* b, size_t sz);
727-
728714
#endif
729715
/** @}*/
730716

core/MySigningAtsha204.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ bool signerAtsha204VerifyMsg(MyMessage &msg)
256256
_signing_hmac[0] = SIGNING_IDENTIFIER;
257257

258258
// Compare the calculated signature with the provided signature
259-
if (signerMemcmp(&msg.data[msg.getLength()], _signing_hmac,
260-
min(MAX_PAYLOAD_SIZE - msg.getLength(), 32))) {
259+
if (timingneutralMemcmp(&msg.data[msg.getLength()], _signing_hmac,
260+
min(MAX_PAYLOAD_SIZE - msg.getLength(), 32))) {
261261
return false;
262262
} else {
263263
return true;

core/MySigningAtsha204Soft.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ bool signerAtsha204SoftVerifyMsg(MyMessage &msg)
261261
_signing_hmac[0] = SIGNING_IDENTIFIER;
262262

263263
// Compare the calculated signature with the provided signature
264-
if (signerMemcmp(&msg.data[msg.getLength()], _signing_hmac,
265-
MIN((uint8_t)(MAX_PAYLOAD_SIZE - msg.getLength()), (uint8_t)32))) {
264+
if (timingneutralMemcmp(&msg.data[msg.getLength()], _signing_hmac,
265+
MIN((uint8_t)(MAX_PAYLOAD_SIZE - msg.getLength()), (uint8_t)32))) {
266266
return false;
267267
} else {
268268
return true;

0 commit comments

Comments
 (0)