Skip to content

Commit b5847e4

Browse files
committed
Fixing up NTP random port usage code (enabled by default, disable via AIoTC_Config.h)
1 parent 99731e9 commit b5847e4

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535

3636
unsigned long NTPUtils::getTime(UDP & udp)
3737
{
38-
NTPUtils randomPort;
39-
int _randomPort = randomPort.setRandomPort(MIN_NTP_PORT, MAX_NTP_PORT);
40-
udp.begin(_randomPort);
38+
#ifdef NTP_USE_RANDOM_PORT
39+
udp.begin(NTPUtils::getRandomPort(MIN_NTP_PORT, MAX_NTP_PORT));
40+
#else
41+
udp.begin(NTP_LOCAL_PORT);
42+
#endif
4143

4244
sendNTPpacket(udp);
4345

@@ -88,13 +90,13 @@ void NTPUtils::sendNTPpacket(UDP & udp)
8890
udp.endPacket();
8991
}
9092

91-
int NTPUtils::setRandomPort(int minValue, int maxValue)
93+
int NTPUtils::getRandomPort(int const min_port, int const max_port)
9294
{
9395
#ifdef BOARD_HAS_ECCX08
94-
return ECCX08.random(minValue, maxValue);
96+
return ECCX08.random(min_port, max_port);
9597
#else
9698
randomSeed(analogRead(0));
97-
return random(minValue, maxValue);
99+
return random(min_port, max_port);
98100
#endif
99101
}
100102

src/utility/time/NTPUtils.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,22 @@
3737
* CLASS DECLARATION
3838
**************************************************************************************/
3939

40-
#define NTP_DEFAULT_LOCAL_PORT 8888
41-
extern const int MIN_NTP_PORT;
42-
extern const int MAX_NTP_PORT;
43-
4440
class NTPUtils
4541
{
4642
public:
4743

4844
static unsigned long getTime(UDP & udp);
49-
int setRandomPort(int minValue, int maxValue);
45+
static int getRandomPort(int const min_port, int const max_port);
5046

5147
private:
5248

5349
static size_t const NTP_PACKET_SIZE = 48;
5450
static int const NTP_TIME_SERVER_PORT = 123;
55-
static int const NTP_LOCAL_PORT = NTP_DEFAULT_LOCAL_PORT;
51+
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
5656
static unsigned long const NTP_TIMEOUT_MS = 1000;
5757
static char constexpr * NTP_TIME_SERVER = "time.arduino.cc";
5858

0 commit comments

Comments
 (0)