Skip to content

Commit 0b45114

Browse files
authored
Merge pull request #198 from luigigubello/improving_ntputils
Set random NTP port by default
2 parents f4d07c8 + 11054bd commit 0b45114

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ String str_property_6;
5454
String str_property_7;
5555
String str_property_8;
5656

57-
5857
#if defined(BOARD_HAS_WIFI)
5958
WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_PASS);
6059
#elif defined(BOARD_HAS_GSM)

src/AIoTC_Config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#define OTA_STORAGE_SFU (0)
2727
#endif
2828

29+
#ifndef NTP_USE_RANDOM_PORT
30+
#define NTP_USE_RANDOM_PORT (1)
31+
#endif
32+
2933
#ifndef DBG_ERROR
3034
#define DBG_ERROR(fmt, ...) Debug.print(DBG_ERROR, fmt, ## __VA_ARGS__)
3135
#endif

src/utility/time/NTPUtils.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,22 @@
2525
#include "NTPUtils.h"
2626

2727
#include <Arduino.h>
28+
#ifdef BOARD_HAS_ECCX08
29+
#include <ArduinoECCX08.h>
30+
#endif
2831

2932
/**************************************************************************************
3033
* PUBLIC MEMBER FUNCTIONS
3134
**************************************************************************************/
3235

3336
unsigned long NTPUtils::getTime(UDP & udp)
3437
{
38+
#ifdef NTP_USE_RANDOM_PORT
39+
udp.begin(NTPUtils::getRandomPort(MIN_NTP_PORT, MAX_NTP_PORT));
40+
#else
3541
udp.begin(NTP_LOCAL_PORT);
36-
42+
#endif
43+
3744
sendNTPpacket(udp);
3845

3946
bool is_timeout = false;
@@ -83,4 +90,14 @@ void NTPUtils::sendNTPpacket(UDP & udp)
8390
udp.endPacket();
8491
}
8592

93+
int NTPUtils::getRandomPort(int const min_port, int const max_port)
94+
{
95+
#ifdef BOARD_HAS_ECCX08
96+
return ECCX08.random(min_port, max_port);
97+
#else
98+
randomSeed(analogRead(0));
99+
return random(min_port, max_port);
100+
#endif
101+
}
102+
86103
#endif /* #ifndef HAS_LORA */

src/utility/time/NTPUtils.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,21 @@ class NTPUtils
4242
public:
4343

4444
static unsigned long getTime(UDP & udp);
45+
static int getRandomPort(int const min_port, int const max_port);
4546

4647
private:
4748

4849
static size_t const NTP_PACKET_SIZE = 48;
4950
static int const NTP_TIME_SERVER_PORT = 123;
5051
static int const NTP_LOCAL_PORT = 8888;
52+
#if NTP_USE_RANDOM_PORT
53+
static int const MIN_NTP_PORT = 49152;
54+
static int const MAX_NTP_PORT = 65535;
55+
#endif
5156
static unsigned long const NTP_TIMEOUT_MS = 1000;
5257
static char constexpr * NTP_TIME_SERVER = "time.arduino.cc";
5358

5459
static void sendNTPpacket(UDP & udp);
55-
5660
};
5761

5862
#endif /* #ifndef HAS_LORA */

0 commit comments

Comments
 (0)