Skip to content

Set random NTP port by default #198

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

Merged
merged 10 commits into from
Sep 3, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ String str_property_6;
String str_property_7;
String str_property_8;


#if defined(BOARD_HAS_WIFI)
WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_PASS);
#elif defined(BOARD_HAS_GSM)
Expand Down
4 changes: 4 additions & 0 deletions src/AIoTC_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion src/utility/time/NTPUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@
#include "NTPUtils.h"

#include <Arduino.h>
#ifdef BOARD_HAS_ECCX08
#include <ArduinoECCX08.h>
#endif

/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/

unsigned long NTPUtils::getTime(UDP & udp)
{
#ifdef NTP_USE_RANDOM_PORT
udp.begin(NTPUtils::getRandomPort(MIN_NTP_PORT, MAX_NTP_PORT));
#else
udp.begin(NTP_LOCAL_PORT);

#endif

sendNTPpacket(udp);

bool is_timeout = false;
Expand Down Expand Up @@ -83,4 +90,14 @@ void NTPUtils::sendNTPpacket(UDP & udp)
udp.endPacket();
}

int NTPUtils::getRandomPort(int const min_port, int const max_port)
{
#ifdef BOARD_HAS_ECCX08
return ECCX08.random(min_port, max_port);
#else
randomSeed(analogRead(0));
return random(min_port, max_port);
#endif
}

#endif /* #ifndef HAS_LORA */
6 changes: 5 additions & 1 deletion src/utility/time/NTPUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,21 @@ class NTPUtils
public:

static unsigned long getTime(UDP & udp);
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 = 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";

static void sendNTPpacket(UDP & udp);

};

#endif /* #ifndef HAS_LORA */
Expand Down