From 722e25dc530feaa40e43a1af50e56512d403dfcb Mon Sep 17 00:00:00 2001 From: Luigi Gubello Date: Wed, 2 Sep 2020 12:55:08 +0200 Subject: [PATCH 1/8] set random NTP port by default --- src/utility/time/NTPUtils.cpp | 21 +++++++++++++++++++-- src/utility/time/NTPUtils.h | 8 ++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/utility/time/NTPUtils.cpp b/src/utility/time/NTPUtils.cpp index 76d6862d7..d24c00364 100644 --- a/src/utility/time/NTPUtils.cpp +++ b/src/utility/time/NTPUtils.cpp @@ -25,6 +25,12 @@ #include "NTPUtils.h" #include +#ifdef BOARD_HAS_ECCX08 + #include + bool has_crypto = 1; +#else + bool has_crypto = 0; +#endif /************************************************************************************** * PUBLIC MEMBER FUNCTIONS @@ -32,8 +38,10 @@ unsigned long NTPUtils::getTime(UDP & udp) { - udp.begin(NTP_LOCAL_PORT); - + NTPUtils randomPort; + int _randomPort = randomPort.setRandomPort(MIN_NTP_PORT, MAX_NTP_PORT); + udp.begin(_randomPort); + sendNTPpacket(udp); bool is_timeout = false; @@ -83,4 +91,13 @@ void NTPUtils::sendNTPpacket(UDP & udp) udp.endPacket(); } +int NTPUtils::setRandomPort(int minValue, int maxValue) { + if (has_crypto) { + return ECCX08.random(minValue, maxValue); + } else { + randomSeed(analogRead(0)); + return random(minValue, maxValue); + } +} + #endif /* #ifndef HAS_LORA */ diff --git a/src/utility/time/NTPUtils.h b/src/utility/time/NTPUtils.h index 0162078a5..bba316e6c 100644 --- a/src/utility/time/NTPUtils.h +++ b/src/utility/time/NTPUtils.h @@ -37,22 +37,26 @@ * CLASS DECLARATION **************************************************************************************/ +#define NTP_DEFAULT_LOCAL_PORT 8888 +extern const int MIN_NTP_PORT; +extern const int MAX_NTP_PORT; + class NTPUtils { public: static unsigned long getTime(UDP & udp); + int setRandomPort(int minValue, int maxValue); private: static size_t const NTP_PACKET_SIZE = 48; static int const NTP_TIME_SERVER_PORT = 123; - static int const NTP_LOCAL_PORT = 8888; + static int const NTP_LOCAL_PORT = NTP_DEFAULT_LOCAL_PORT; static unsigned long const NTP_TIMEOUT_MS = 1000; static char constexpr * NTP_TIME_SERVER = "time.arduino.cc"; static void sendNTPpacket(UDP & udp); - }; #endif /* #ifndef HAS_LORA */ From 2dfc4c33ca6a363f9ac7afbfdc5f2e38fa0be5b4 Mon Sep 17 00:00:00 2001 From: Luigi Gubello Date: Wed, 2 Sep 2020 16:22:48 +0200 Subject: [PATCH 2/8] Fix example --- examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h | 2 ++ examples/utility/Provisioning/Provisioning.ino | 3 +++ 2 files changed, 5 insertions(+) diff --git a/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h b/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h index 02ce29c2f..22de0595d 100644 --- a/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h +++ b/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h @@ -54,6 +54,8 @@ String str_property_6; String str_property_7; String str_property_8; +const int MIN_NTP_PORT = 49152; +const int MAX_NTP_PORT = 65535; #if defined(BOARD_HAS_WIFI) WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_PASS); diff --git a/examples/utility/Provisioning/Provisioning.ino b/examples/utility/Provisioning/Provisioning.ino index e4ea8ce1b..f5d6c554e 100644 --- a/examples/utility/Provisioning/Provisioning.ino +++ b/examples/utility/Provisioning/Provisioning.ino @@ -9,6 +9,9 @@ const int compressedCertSlot = 10; const int serialNumberAndAuthorityKeyIdentifierSlot = 11; const int deviceIdSlot = 12; +const int MIN_NTP_PORT = 49152; +const int MAX_NTP_PORT = 65535; + ECCX08CertClass ECCX08Cert; void setup() { From 86e812bb427d478b4ca95e952d4925ae2d1002e9 Mon Sep 17 00:00:00 2001 From: Luigi Gubello Date: Wed, 2 Sep 2020 16:25:34 +0200 Subject: [PATCH 3/8] Fix examples again --- examples/ArduinoIoTCloud-Advanced/thingProperties.h | 3 +++ examples/ArduinoIoTCloud-Basic/thingProperties.h | 3 +++ 2 files changed, 6 insertions(+) diff --git a/examples/ArduinoIoTCloud-Advanced/thingProperties.h b/examples/ArduinoIoTCloud-Advanced/thingProperties.h index 467e2e5e4..5b32622b7 100644 --- a/examples/ArduinoIoTCloud-Advanced/thingProperties.h +++ b/examples/ArduinoIoTCloud-Advanced/thingProperties.h @@ -4,6 +4,9 @@ #define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" +const int MIN_NTP_PORT = 49152; +const int MAX_NTP_PORT = 65535; + void onSwitchButtonChange(); void onColorChange(); diff --git a/examples/ArduinoIoTCloud-Basic/thingProperties.h b/examples/ArduinoIoTCloud-Basic/thingProperties.h index b2bfb5d67..7107671f1 100644 --- a/examples/ArduinoIoTCloud-Basic/thingProperties.h +++ b/examples/ArduinoIoTCloud-Basic/thingProperties.h @@ -12,6 +12,9 @@ #define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" +const int MIN_NTP_PORT = 49152; +const int MAX_NTP_PORT = 65535; + void onLedChange(); bool led; From 68c425653bab0a4d206b7e73162d5b7e1ae6de2d Mon Sep 17 00:00:00 2001 From: Luigi Gubello Date: Wed, 2 Sep 2020 16:50:01 +0200 Subject: [PATCH 4/8] Fix SelfProvisioning --- examples/utility/SelfProvisioning/SelfProvisioning.ino | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/utility/SelfProvisioning/SelfProvisioning.ino b/examples/utility/SelfProvisioning/SelfProvisioning.ino index a0c13724b..1b5e1edb6 100644 --- a/examples/utility/SelfProvisioning/SelfProvisioning.ino +++ b/examples/utility/SelfProvisioning/SelfProvisioning.ino @@ -34,6 +34,9 @@ const int compressedCertSlot = 10; const int serialNumberAndAuthorityKeyIdentifierSlot = 11; const int deviceIdSlot = 12; +const int MIN_NTP_PORT = 49152; +const int MAX_NTP_PORT = 65535; + char ssid[] = SECRET_SSID; char pass[] = SECRET_PASS; char client_id[] = SECRET_CLIENT_ID; From c8359771eac5aa19eb0383d6acf2394ee0b81d90 Mon Sep 17 00:00:00 2001 From: Luigi Gubello Date: Wed, 2 Sep 2020 18:26:06 +0200 Subject: [PATCH 5/8] Fix examples --- examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h | 4 ++-- examples/utility/Provisioning/Provisioning.ino | 4 ++-- examples/utility/SelfProvisioning/SelfProvisioning.ino | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h b/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h index 22de0595d..47f20a8bb 100644 --- a/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h +++ b/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h @@ -54,8 +54,8 @@ String str_property_6; String str_property_7; String str_property_8; -const int MIN_NTP_PORT = 49152; -const int MAX_NTP_PORT = 65535; +int MIN_NTP_PORT = 49152; +int MAX_NTP_PORT = 65535; #if defined(BOARD_HAS_WIFI) WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_PASS); diff --git a/examples/utility/Provisioning/Provisioning.ino b/examples/utility/Provisioning/Provisioning.ino index f5d6c554e..86ca61b6f 100644 --- a/examples/utility/Provisioning/Provisioning.ino +++ b/examples/utility/Provisioning/Provisioning.ino @@ -9,8 +9,8 @@ const int compressedCertSlot = 10; const int serialNumberAndAuthorityKeyIdentifierSlot = 11; const int deviceIdSlot = 12; -const int MIN_NTP_PORT = 49152; -const int MAX_NTP_PORT = 65535; +int MIN_NTP_PORT = 49152; +int MAX_NTP_PORT = 65535; ECCX08CertClass ECCX08Cert; diff --git a/examples/utility/SelfProvisioning/SelfProvisioning.ino b/examples/utility/SelfProvisioning/SelfProvisioning.ino index 1b5e1edb6..3ccefcfae 100644 --- a/examples/utility/SelfProvisioning/SelfProvisioning.ino +++ b/examples/utility/SelfProvisioning/SelfProvisioning.ino @@ -34,8 +34,8 @@ const int compressedCertSlot = 10; const int serialNumberAndAuthorityKeyIdentifierSlot = 11; const int deviceIdSlot = 12; -const int MIN_NTP_PORT = 49152; -const int MAX_NTP_PORT = 65535; +int MIN_NTP_PORT = 49152; +int MAX_NTP_PORT = 65535; char ssid[] = SECRET_SSID; char pass[] = SECRET_PASS; From 99731e9b15cae3b26c75539ddf2e56125a97249a Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 3 Sep 2020 08:23:56 +0200 Subject: [PATCH 6/8] Replace global bool variable with preprocessor directives --- src/utility/time/NTPUtils.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/utility/time/NTPUtils.cpp b/src/utility/time/NTPUtils.cpp index d24c00364..627521fa6 100644 --- a/src/utility/time/NTPUtils.cpp +++ b/src/utility/time/NTPUtils.cpp @@ -27,9 +27,6 @@ #include #ifdef BOARD_HAS_ECCX08 #include - bool has_crypto = 1; -#else - bool has_crypto = 0; #endif /************************************************************************************** @@ -91,13 +88,14 @@ void NTPUtils::sendNTPpacket(UDP & udp) udp.endPacket(); } -int NTPUtils::setRandomPort(int minValue, int maxValue) { - if (has_crypto) { - return ECCX08.random(minValue, maxValue); - } else { - randomSeed(analogRead(0)); - return random(minValue, maxValue); - } +int NTPUtils::setRandomPort(int minValue, int maxValue) +{ +#ifdef BOARD_HAS_ECCX08 + return ECCX08.random(minValue, maxValue); +#else + randomSeed(analogRead(0)); + return random(minValue, maxValue); +#endif } #endif /* #ifndef HAS_LORA */ From b5847e48b0e2aa0373737f8326128363f0a7bd1b Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 3 Sep 2020 08:30:35 +0200 Subject: [PATCH 7/8] Fixing up NTP random port usage code (enabled by default, disable via AIoTC_Config.h) --- src/AIoTC_Config.h | 4 ++++ src/utility/time/NTPUtils.cpp | 14 ++++++++------ src/utility/time/NTPUtils.h | 12 ++++++------ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index 7fd515395..2515575c9 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -26,6 +26,10 @@ #define OTA_STORAGE_SFU (0) #endif +#ifndef NTP_USE_RANDOM_PORT + #define NTP_USE_RANDOM_PORT (1) +#endif + #ifndef DBG_ERROR #define DBG_ERROR(fmt, ...) Debug.print(DBG_ERROR, fmt, ## __VA_ARGS__) #endif diff --git a/src/utility/time/NTPUtils.cpp b/src/utility/time/NTPUtils.cpp index 627521fa6..9ce276c8c 100644 --- a/src/utility/time/NTPUtils.cpp +++ b/src/utility/time/NTPUtils.cpp @@ -35,9 +35,11 @@ unsigned long NTPUtils::getTime(UDP & udp) { - NTPUtils randomPort; - int _randomPort = randomPort.setRandomPort(MIN_NTP_PORT, MAX_NTP_PORT); - udp.begin(_randomPort); +#ifdef NTP_USE_RANDOM_PORT + udp.begin(NTPUtils::getRandomPort(MIN_NTP_PORT, MAX_NTP_PORT)); +#else + udp.begin(NTP_LOCAL_PORT); +#endif sendNTPpacket(udp); @@ -88,13 +90,13 @@ void NTPUtils::sendNTPpacket(UDP & udp) udp.endPacket(); } -int NTPUtils::setRandomPort(int minValue, int maxValue) +int NTPUtils::getRandomPort(int const min_port, int const max_port) { #ifdef BOARD_HAS_ECCX08 - return ECCX08.random(minValue, maxValue); + return ECCX08.random(min_port, max_port); #else randomSeed(analogRead(0)); - return random(minValue, maxValue); + return random(min_port, max_port); #endif } diff --git a/src/utility/time/NTPUtils.h b/src/utility/time/NTPUtils.h index bba316e6c..91cec89af 100644 --- a/src/utility/time/NTPUtils.h +++ b/src/utility/time/NTPUtils.h @@ -37,22 +37,22 @@ * CLASS DECLARATION **************************************************************************************/ -#define NTP_DEFAULT_LOCAL_PORT 8888 -extern const int MIN_NTP_PORT; -extern const int MAX_NTP_PORT; - class NTPUtils { public: static unsigned long getTime(UDP & udp); - int setRandomPort(int minValue, int maxValue); + static int getRandomPort(int const min_port, int const max_port); private: static size_t const NTP_PACKET_SIZE = 48; static int const NTP_TIME_SERVER_PORT = 123; - static int const NTP_LOCAL_PORT = NTP_DEFAULT_LOCAL_PORT; + static int const NTP_LOCAL_PORT = 8888; +#if NTP_USE_RANDOM_PORT + static int const MIN_NTP_PORT = 49152; + static int const MAX_NTP_PORT = 65535; +#endif static unsigned long const NTP_TIMEOUT_MS = 1000; static char constexpr * NTP_TIME_SERVER = "time.arduino.cc"; From 11054bdb732f3d758770a8205676b6fda663ebb1 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 3 Sep 2020 08:32:08 +0200 Subject: [PATCH 8/8] Removing unnecessary extern declaration of NTP ports --- examples/ArduinoIoTCloud-Advanced/thingProperties.h | 3 --- examples/ArduinoIoTCloud-Basic/thingProperties.h | 3 --- examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h | 3 --- examples/utility/Provisioning/Provisioning.ino | 3 --- examples/utility/SelfProvisioning/SelfProvisioning.ino | 3 --- 5 files changed, 15 deletions(-) diff --git a/examples/ArduinoIoTCloud-Advanced/thingProperties.h b/examples/ArduinoIoTCloud-Advanced/thingProperties.h index 5b32622b7..467e2e5e4 100644 --- a/examples/ArduinoIoTCloud-Advanced/thingProperties.h +++ b/examples/ArduinoIoTCloud-Advanced/thingProperties.h @@ -4,9 +4,6 @@ #define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -const int MIN_NTP_PORT = 49152; -const int MAX_NTP_PORT = 65535; - void onSwitchButtonChange(); void onColorChange(); diff --git a/examples/ArduinoIoTCloud-Basic/thingProperties.h b/examples/ArduinoIoTCloud-Basic/thingProperties.h index 7107671f1..b2bfb5d67 100644 --- a/examples/ArduinoIoTCloud-Basic/thingProperties.h +++ b/examples/ArduinoIoTCloud-Basic/thingProperties.h @@ -12,9 +12,6 @@ #define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -const int MIN_NTP_PORT = 49152; -const int MAX_NTP_PORT = 65535; - void onLedChange(); bool led; diff --git a/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h b/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h index 47f20a8bb..c0dded7a5 100644 --- a/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h +++ b/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h @@ -54,9 +54,6 @@ String str_property_6; String str_property_7; String str_property_8; -int MIN_NTP_PORT = 49152; -int MAX_NTP_PORT = 65535; - #if defined(BOARD_HAS_WIFI) WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_PASS); #elif defined(BOARD_HAS_GSM) diff --git a/examples/utility/Provisioning/Provisioning.ino b/examples/utility/Provisioning/Provisioning.ino index 86ca61b6f..e4ea8ce1b 100644 --- a/examples/utility/Provisioning/Provisioning.ino +++ b/examples/utility/Provisioning/Provisioning.ino @@ -9,9 +9,6 @@ const int compressedCertSlot = 10; const int serialNumberAndAuthorityKeyIdentifierSlot = 11; const int deviceIdSlot = 12; -int MIN_NTP_PORT = 49152; -int MAX_NTP_PORT = 65535; - ECCX08CertClass ECCX08Cert; void setup() { diff --git a/examples/utility/SelfProvisioning/SelfProvisioning.ino b/examples/utility/SelfProvisioning/SelfProvisioning.ino index 3ccefcfae..a0c13724b 100644 --- a/examples/utility/SelfProvisioning/SelfProvisioning.ino +++ b/examples/utility/SelfProvisioning/SelfProvisioning.ino @@ -34,9 +34,6 @@ const int compressedCertSlot = 10; const int serialNumberAndAuthorityKeyIdentifierSlot = 11; const int deviceIdSlot = 12; -int MIN_NTP_PORT = 49152; -int MAX_NTP_PORT = 65535; - char ssid[] = SECRET_SSID; char pass[] = SECRET_PASS; char client_id[] = SECRET_CLIENT_ID;