Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multitransport implementation #1414

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions MyConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@
* @brief Declare the amount of incoming messages that can be buffered at driver level.
*/
#ifndef MY_NRF5_ESB_RX_BUFFER_SIZE
#define MY_NRF5_ESB_RX_BUFFER_SIZE (20)
#define MY_NRF5_ESB_RX_BUFFER_SIZE (5)
#endif

/**
Expand Down Expand Up @@ -2191,7 +2191,7 @@
#define MY_DEBUG_VERBOSE_OTA_UPDATE //!< MY_DEBUG_VERBOSE_OTA_UPDATE
#endif

#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)
#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)
#define DEBUG_OUTPUT_ENABLED //!< DEBUG_OUTPUT_ENABLED
#ifndef MY_DEBUG_OTA
#define DEBUG_OUTPUT(x,...) hwDebugPrint(x, ##__VA_ARGS__) //!< debug
Expand Down
39 changes: 29 additions & 10 deletions MySensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,18 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#define __RS485CNT 0 //!< __RS485CNT
#endif

#if (__RF24CNT + __NRF5ESBCNT + __RFM69CNT + __RFM95CNT + __RS485CNT > 1)
#error Only one forward link driver can be activated
#define MY_TRANSPORT_COUNT (__RF24CNT + __NRF5ESBCNT + __RFM69CNT + __RFM95CNT + __RS485CNT)

#if (MY_TRANSPORT_COUNT > 1)
// more than 1 transport requires RX queue
#define MY_TRANSPORT_RX_QUEUE
#else
// RF24 + IRQ requires RX queue
#if defined(MY_RADIO_RF24) && defined(MY_RF24_USE_INTERRUPTS)
#define MY_TRANSPORT_RX_QUEUE
#endif
#endif

#endif //DOXYGEN

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

// TRANSPORT INCLUDES
#if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB) || defined(MY_RADIO_RFM69) || defined(MY_RADIO_RFM95) || defined(MY_RS485)
#if (MY_TRANSPORT_COUNT > 0)
#include "hal/transport/MyTransportHAL.h"
#include "core/MyTransport.h"

Expand Down Expand Up @@ -352,39 +361,49 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#endif
#endif

#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))
#define MY_TRANSPORT_ENCRYPTION //!< internal flag
#include "hal/transport/MyTransportEncryption.cpp"
#endif

// Transport drivers
#if defined(MY_RADIO_RF24)
#include "hal/transport/RF24/driver/RF24.cpp"
#include "hal/transport/RF24/MyTransportRF24.cpp"
#elif defined(MY_RADIO_NRF5_ESB)
#endif
#if defined(MY_RADIO_NRF5_ESB)
#if !defined(ARDUINO_ARCH_NRF5)
#error No support for nRF5 radio on this platform
#endif
#include "hal/transport/NRF5_ESB/driver/Radio.cpp"
#include "hal/transport/NRF5_ESB/driver/Radio_ESB.cpp"
#include "hal/transport/NRF5_ESB/MyTransportNRF5_ESB.cpp"
#elif defined(MY_RS485)
#endif
#if defined(MY_RS485)
#if !defined(MY_RS485_HWSERIAL)
#if defined(__linux__)
#error You must specify MY_RS485_HWSERIAL for RS485 transport
#endif
#include "drivers/AltSoftSerial/AltSoftSerial.cpp"
#endif
#include "hal/transport/RS485/MyTransportRS485.cpp"
#elif defined(MY_RADIO_RFM69)
#endif
#if defined(MY_RADIO_RFM69)
#if defined(MY_RFM69_NEW_DRIVER)
#include "hal/transport/RFM69/driver/new/RFM69_new.cpp"
#else
#include "hal/transport/RFM69/driver/old/RFM69_old.cpp"
#endif
#include "hal/transport/RFM69/MyTransportRFM69.cpp"
#elif defined(MY_RADIO_RFM95)
#endif
#if defined(MY_RADIO_RFM95)
#if defined(MY_RFM95_RFM69_COMPATIBILITY)
#include "hal/transport/RFM95/driver/RFM95_RFM69.cpp"
#include "hal/transport/RFM95/MyTransportRFM95_RFM69.cpp"
#else
#include "hal/transport/RFM95/driver/RFM95.cpp"
#include "hal/transport/RFM95/MyTransportRFM95.cpp"
#endif

#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))
#define MY_TRANSPORT_ENCRYPTION //!< ïnternal flag
#endif

#include "hal/transport/MyTransportHAL.cpp"
Expand Down
6 changes: 5 additions & 1 deletion core/MyCapabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@
*
* | Radio | Indicator
* |--------------|----------
* | Multiple | M
* | nRF24/nRF5 | N
* | %RFM69 (old) | R
* | %RFM69 (new) | P
* | RFM95 | L
* | RS485 | S
* | None | -
*/
#if (MY_TRANSPORT_COUNT > 1)
#define MY_CAP_RADIO "M"
#else
#if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB)
#define MY_CAP_RADIO "N"
#elif defined(MY_RADIO_RFM69)
Expand All @@ -111,7 +115,7 @@
#else
#define MY_CAP_RADIO "-"
#endif

#endif
// Node type
/**
* @def MY_CAP_TYPE
Expand Down
30 changes: 30 additions & 0 deletions core/MyHelperFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,33 @@ static char convertI2H(const uint8_t i)
return 'A' + k - 10;
}
}

static int timingneutralMemcmp(const void* a, const void* b, size_t sz)
{
int retVal;
size_t i;
int done = 0;
const uint8_t* ptrA = (const uint8_t*)a;
const uint8_t* ptrB = (const uint8_t*)b;
for (i = 0; i < sz; i++) {
if (ptrA[i] == ptrB[i]) {
if (done > 0) {
done = 1;
} else {
done = 0;
}
} else {
if (done > 0) {
done = 2;
} else {
done = 3;
}
}
}
if (done > 0) {
retVal = -1;
} else {
retVal = 0;
}
return retVal;
}
13 changes: 13 additions & 0 deletions core/MyHelperFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,18 @@ static uint8_t convertH2I(const char c) __attribute__((unused));
*/
static char convertI2H(const uint8_t i) __attribute__((unused));

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

#endif
3 changes: 0 additions & 3 deletions core/MyOTALogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
#ifndef MyOTALogging_h
#define MyOTALogging_h

#include "MySensorsCore.h"
#include "MyTransport.h"

/**
* @brief Send a log message to a node
*
Expand Down
2 changes: 1 addition & 1 deletion core/MySensorsCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ void _nodeLock(const char *str)
doYield();
(void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID,C_INTERNAL, I_LOCKED).set(str));
#if defined(MY_SENSOR_NETWORK)
transportSleep();
transportHALSleep();
CORE_DEBUG(PSTR("MCO:NLK:TSL\n")); // sleep transport
#endif
setIndication(INDICATION_SLEEP);
Expand Down
1 change: 1 addition & 0 deletions core/MySensorsCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include <stdarg.h>

#define GATEWAY_ADDRESS ((uint8_t)0) //!< Node ID for GW sketch
#define BROADCAST_ADDRESS ((uint8_t)255) //!< broadcasts are addressed to ID 255
#define NODE_SENSOR_ID ((uint8_t)255) //!< Node child is always created/presented when a node is started
#define MY_CORE_VERSION ((uint8_t)2) //!< core version
#define MY_CORE_MIN_VERSION ((uint8_t)2) //!< min core version required for compatibility
Expand Down
35 changes: 5 additions & 30 deletions core/MySigning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,36 +321,6 @@ bool signerVerifyMsg(MyMessage &msg)
return verificationResult;
}

int signerMemcmp(const void* a, const void* b, size_t sz)
{
int retVal;
size_t i;
int done = 0;
const uint8_t* ptrA = (const uint8_t*)a;
const uint8_t* ptrB = (const uint8_t*)b;
for (i=0; i < sz; i++) {
if (ptrA[i] == ptrB[i]) {
if (done > 0) {
done = 1;
} else {
done = 0;
}
} else {
if (done > 0) {
done = 2;
} else {
done = 3;
}
}
}
if (done > 0) {
retVal = -1;
} else {
retVal = 0;
}
return retVal;
}

#if defined(MY_SIGNING_FEATURE)
// Helper function to centralize signing/verification exceptions
// cppcheck-suppress constParameter
Expand Down Expand Up @@ -401,6 +371,11 @@ static void prepareSigningPresentation(MyMessage &msg, uint8_t destination)
static bool signerInternalProcessPresentation(MyMessage &msg)
{
const uint8_t sender = msg.getSender();
#if defined(MY_GATEWAY_FEATURE) && (F_CPU > 16*1000000ul)
// let's slow down things a bit - on fast GWs we may send messages before slow node's radio switches from TX->RX
delay(50);
#endif

#if defined(MY_SIGNING_FEATURE)
if (msg.data[0] != SIGNING_PRESENTATION_VERSION_1) {
SIGN_DEBUG(PSTR("!SGN:PRE:VER=%" PRIu8 "\n"),
Expand Down
14 changes: 0 additions & 14 deletions core/MySigning.h
Original file line number Diff line number Diff line change
Expand Up @@ -711,20 +711,6 @@ bool signerSignMsg(MyMessage &msg);
*/
bool signerVerifyMsg(MyMessage &msg);

/**
* @brief Do a timing neutral memory comparison.
*
* The function behaves similar to memcmp with the difference that it will
* always use the same number of instructions for a given number of bytes,
* no matter how the two buffers differ and the response is either 0 or -1.
*
* @param a First buffer for comparison.
* @param b Second buffer for comparison.
* @param sz The number of bytes to compare.
* @returns 0 if buffers match, -1 if they do not.
*/
int signerMemcmp(const void* a, const void* b, size_t sz);

#endif
/** @}*/

Expand Down
4 changes: 2 additions & 2 deletions core/MySigningAtsha204.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ bool signerAtsha204VerifyMsg(MyMessage &msg)
_signing_hmac[0] = SIGNING_IDENTIFIER;

// Compare the calculated signature with the provided signature
if (signerMemcmp(&msg.data[msg.getLength()], _signing_hmac,
min(MAX_PAYLOAD_SIZE - msg.getLength(), 32))) {
if (timingneutralMemcmp(&msg.data[msg.getLength()], _signing_hmac,
min(MAX_PAYLOAD_SIZE - msg.getLength(), 32))) {
return false;
} else {
return true;
Expand Down
4 changes: 2 additions & 2 deletions core/MySigningAtsha204Soft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ bool signerAtsha204SoftVerifyMsg(MyMessage &msg)
_signing_hmac[0] = SIGNING_IDENTIFIER;

// Compare the calculated signature with the provided signature
if (signerMemcmp(&msg.data[msg.getLength()], _signing_hmac,
MIN((uint8_t)(MAX_PAYLOAD_SIZE - msg.getLength()), (uint8_t)32))) {
if (timingneutralMemcmp(&msg.data[msg.getLength()], _signing_hmac,
MIN((uint8_t)(MAX_PAYLOAD_SIZE - msg.getLength()), (uint8_t)32))) {
return false;
} else {
return true;
Expand Down
Loading