Skip to content

Added method to set random local port #116

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 3 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 9 additions & 3 deletions NTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void NTPClient::begin() {
this->begin(NTP_DEFAULT_LOCAL_PORT);
}

void NTPClient::begin(int port) {
void NTPClient::begin(long port) {
this->_port = port;

this->_udp->begin(this->_port);
Expand Down Expand Up @@ -120,7 +120,7 @@ bool NTPClient::forceUpdate() {
bool NTPClient::update() {
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
|| this->_lastUpdate == 0) { // Update if there was no update yet.
if (!this->_udpSetup) this->begin(); // setup the UDP client if needed
if (!this->_udpSetup || this->_port != NTP_DEFAULT_LOCAL_PORT) this->begin(this->_port); // setup the UDP client if needed
return this->forceUpdate();
}
return false; // return false if update does not occur
Expand Down Expand Up @@ -181,7 +181,8 @@ void NTPClient::sendNTPPacket() {
// set all bytes in the buffer to 0
memset(this->_packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
// (see URL above for details on the packets) Serial.println(this->_port);

this->_packetBuffer[0] = 0b11100011; // LI, Version, Mode
this->_packetBuffer[1] = 0; // Stratum, or type of clock
this->_packetBuffer[2] = 6; // Polling Interval
Expand All @@ -202,3 +203,8 @@ void NTPClient::sendNTPPacket() {
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
this->_udp->endPacket();
}

void NTPClient::setRandomPort(long minValue, long maxValue) {
randomSeed(analogRead(0));
this->_port = random(minValue, maxValue);
}
9 changes: 7 additions & 2 deletions NTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class NTPClient {

const char* _poolServerName = "pool.ntp.org"; // Default time server
IPAddress _poolServerIP;
int _port = NTP_DEFAULT_LOCAL_PORT;
long _port = NTP_DEFAULT_LOCAL_PORT;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @luigigubello 👋 ☕ Is there any special reason why you change this data type from int to long? Certainly int is capable of holding values up 2^16-1 even on AVR 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly int is capable of holding values up 2^16-1 even on AVR

Reading the documentation I have understood it's not, but your suggestion to use unsigned int is a good idea. So, yes, I have chosen long instead int to be sure to maintain the AVR compatibility. It seems unsigned int is the best choice 🙂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked again - so yes, int 2 byte on AVR which covers from -2^15 to 2^15-1. unsigned int covers 0 to 2^16-1 which is enough.

long _timeOffset = 0;

unsigned long _updateInterval = 60000; // In ms
Expand Down Expand Up @@ -44,6 +44,11 @@ class NTPClient {
*/
void setPoolServerName(const char* poolServerName);

/**
* Set random local port
*/
void setRandomPort(long minValue, long maxValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here ... I think int or even better unsigned int (as it excludes negative values) would do the job just nicely here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are right


/**
* Starts the underlying UDP client with the default local port
*/
Expand All @@ -52,7 +57,7 @@ class NTPClient {
/**
* Starts the underlying UDP client with the specified local port
*/
void begin(int port);
void begin(long port);

/**
* This should be called in the main loop of your application. By default an update from the NTP Server is only
Expand Down