format check in ntp client

pull/293/head
Peter Buchegger 2023-05-17 21:34:25 +02:00
rodzic 9d6ce8dfac
commit 74e01a76a7
3 zmienionych plików z 124 dodań i 122 usunięć

Wyświetl plik

@ -44,7 +44,7 @@ jobs:
- 'lib/BoardFinder' - 'lib/BoardFinder'
- 'lib/ConfigurationManagement' - 'lib/ConfigurationManagement'
#- 'lib/Display' #- 'lib/Display'
#- 'lib/NTPClient' - 'lib/NTPClient'
- 'lib/PowerManagement' - 'lib/PowerManagement'
- 'lib/System' - 'lib/System'
#- 'lib/TimeLib' #- 'lib/TimeLib'

Wyświetl plik

@ -25,10 +25,10 @@ NTPClient::NTPClient() {
} }
NTPClient::NTPClient(long timeOffset) { NTPClient::NTPClient(long timeOffset) {
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
} }
NTPClient::NTPClient(const char* poolServerName) { NTPClient::NTPClient(const char *poolServerName) {
this->_poolServerName = poolServerName; this->_poolServerName = poolServerName;
} }
@ -37,18 +37,18 @@ NTPClient::NTPClient(IPAddress poolServerIP) {
this->_poolServerName = NULL; this->_poolServerName = NULL;
} }
NTPClient::NTPClient(const char* poolServerName, long timeOffset) { NTPClient::NTPClient(const char *poolServerName, long timeOffset) {
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
this->_poolServerName = poolServerName; this->_poolServerName = poolServerName;
} }
NTPClient::NTPClient(IPAddress poolServerIP, long timeOffset){ NTPClient::NTPClient(IPAddress poolServerIP, long timeOffset) {
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
this->_poolServerIP = poolServerIP; this->_poolServerIP = poolServerIP;
this->_poolServerName = NULL; this->_poolServerName = NULL;
} }
NTPClient::NTPClient(const char* poolServerName, long timeOffset, unsigned long updateInterval) { NTPClient::NTPClient(const char *poolServerName, long timeOffset, unsigned long updateInterval) {
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
this->_poolServerName = poolServerName; this->_poolServerName = poolServerName;
this->_updateInterval = updateInterval; this->_updateInterval = updateInterval;
@ -74,23 +74,24 @@ void NTPClient::begin(unsigned int port) {
} }
bool NTPClient::forceUpdate() { bool NTPClient::forceUpdate() {
#ifdef DEBUG_NTPClient #ifdef DEBUG_NTPClient
Serial.println("Update from NTP Server"); Serial.println("Update from NTP Server");
#endif #endif
// flush any existing packets // flush any existing packets
while(this->_udp.parsePacket() != 0) while (this->_udp.parsePacket() != 0)
this->_udp.flush(); this->_udp.flush();
this->sendNTPPacket(); this->sendNTPPacket();
// Wait till data is there or timeout... // Wait till data is there or timeout...
byte timeout = 0; byte timeout = 0;
int cb = 0; int cb = 0;
do { do {
delay ( 10 ); delay(10);
cb = this->_udp.parsePacket(); cb = this->_udp.parsePacket();
if (timeout > 100) return false; // timeout after 1000 ms if (timeout > 100)
return false; // timeout after 1000 ms
timeout++; timeout++;
} while (cb == 0); } while (cb == 0);
@ -99,36 +100,37 @@ bool NTPClient::forceUpdate() {
this->_udp.read(this->_packetBuffer, NTP_PACKET_SIZE); this->_udp.read(this->_packetBuffer, NTP_PACKET_SIZE);
unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]); unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]); unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
// combine the four bytes (two words) into a long integer // combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900): // this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord; unsigned long secsSince1900 = highWord << 16 | lowWord;
this->_currentEpoc = secsSince1900 - SEVENZYYEARS; this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
return true; // return true after successful update return true; // return true after successful update
} }
bool NTPClient::update() { bool NTPClient::update() {
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
|| this->_lastUpdate == 0) { // Update if there was no update yet. || this->_lastUpdate == 0) { // Update if there was no update yet.
if (!this->_udpSetup || this->_port != NTP_DEFAULT_LOCAL_PORT) this->begin(this->_port); // 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 this->forceUpdate();
} }
return false; // return false if update does not occur return false; // return false if update does not occur
} }
unsigned long NTPClient::getEpochTime() const { unsigned long NTPClient::getEpochTime() const {
return this->_timeOffset + // User offset return this->_timeOffset + // User offset
this->_currentEpoc + // Epoc returned by the NTP server this->_currentEpoc + // Epoc returned by the NTP server
((millis() - this->_lastUpdate) / 1000); // Time since last update ((millis() - this->_lastUpdate) / 1000); // Time since last update
} }
int NTPClient::getDay() const { int NTPClient::getDay() const {
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday return (((this->getEpochTime() / 86400L) + 4) % 7); // 0 is Sunday
} }
int NTPClient::getHours() const { int NTPClient::getHours() const {
return ((this->getEpochTime() % 86400L) / 3600); return ((this->getEpochTime() % 86400L) / 3600);
} }
int NTPClient::getMinutes() const { int NTPClient::getMinutes() const {
return ((this->getEpochTime() % 3600) / 60); return ((this->getEpochTime() % 3600) / 60);
@ -138,15 +140,15 @@ int NTPClient::getSeconds() const {
} }
String NTPClient::getFormattedTime() const { String NTPClient::getFormattedTime() const {
unsigned long rawTime = this->getEpochTime(); unsigned long rawTime = this->getEpochTime();
unsigned long hours = (rawTime % 86400L) / 3600; unsigned long hours = (rawTime % 86400L) / 3600;
String hoursStr = hours < 10 ? "0" + String(hours) : String(hours); String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
unsigned long minutes = (rawTime % 3600) / 60; unsigned long minutes = (rawTime % 3600) / 60;
String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes); String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes);
unsigned long seconds = rawTime % 60; unsigned long seconds = rawTime % 60;
String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds); String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);
return hoursStr + ":" + minuteStr + ":" + secondStr; return hoursStr + ":" + minuteStr + ":" + secondStr;
} }
@ -158,15 +160,15 @@ void NTPClient::end() {
} }
void NTPClient::setTimeOffset(int timeOffset) { void NTPClient::setTimeOffset(int timeOffset) {
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
} }
void NTPClient::setUpdateInterval(unsigned long updateInterval) { void NTPClient::setUpdateInterval(unsigned long updateInterval) {
this->_updateInterval = updateInterval; this->_updateInterval = updateInterval;
} }
void NTPClient::setPoolServerName(const char* poolServerName) { void NTPClient::setPoolServerName(const char *poolServerName) {
this->_poolServerName = poolServerName; this->_poolServerName = poolServerName;
} }
void NTPClient::sendNTPPacket() { void NTPClient::sendNTPPacket() {
@ -175,19 +177,19 @@ void NTPClient::sendNTPPacket() {
// Initialize values needed to form NTP request // Initialize values needed to form NTP request
// (see URL above for details on the packets) // (see URL above for details on the packets)
this->_packetBuffer[0] = 0b11100011; // LI, Version, Mode this->_packetBuffer[0] = 0b11100011; // LI, Version, Mode
this->_packetBuffer[1] = 0; // Stratum, or type of clock this->_packetBuffer[1] = 0; // Stratum, or type of clock
this->_packetBuffer[2] = 6; // Polling Interval this->_packetBuffer[2] = 6; // Polling Interval
this->_packetBuffer[3] = 0xEC; // Peer Clock Precision this->_packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion // 8 bytes of zero for Root Delay & Root Dispersion
this->_packetBuffer[12] = 49; this->_packetBuffer[12] = 49;
this->_packetBuffer[13] = 0x4E; this->_packetBuffer[13] = 0x4E;
this->_packetBuffer[14] = 49; this->_packetBuffer[14] = 49;
this->_packetBuffer[15] = 52; this->_packetBuffer[15] = 52;
// all NTP fields have been given values, now // all NTP fields have been given values, now
// you can send a packet requesting a timestamp: // you can send a packet requesting a timestamp:
if (this->_poolServerName) { if (this->_poolServerName) {
this->_udp.beginPacket(this->_poolServerName, 123); this->_udp.beginPacket(this->_poolServerName, 123);
} else { } else {
this->_udp.beginPacket(this->_poolServerIP, 123); this->_udp.beginPacket(this->_poolServerIP, 123);
@ -199,4 +201,4 @@ void NTPClient::sendNTPPacket() {
void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) { void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) {
randomSeed(analogRead(0)); randomSeed(analogRead(0));
this->_port = random(minValue, maxValue); this->_port = random(minValue, maxValue);
} }

Wyświetl plik

@ -3,104 +3,104 @@
#include <Arduino.h> #include <Arduino.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>
#define SEVENZYYEARS 2208988800UL #define SEVENZYYEARS 2208988800UL
#define NTP_PACKET_SIZE 48 #define NTP_PACKET_SIZE 48
#define NTP_DEFAULT_LOCAL_PORT 1337 #define NTP_DEFAULT_LOCAL_PORT 1337
class NTPClient { class NTPClient {
private: private:
WiFiUDP _udp; WiFiUDP _udp;
bool _udpSetup = false; bool _udpSetup = false;
const char* _poolServerName = "pool.ntp.org"; // Default time server const char *_poolServerName = "pool.ntp.org"; // Default time server
IPAddress _poolServerIP; IPAddress _poolServerIP;
unsigned int _port = NTP_DEFAULT_LOCAL_PORT; unsigned int _port = NTP_DEFAULT_LOCAL_PORT;
long _timeOffset = 0; long _timeOffset = 0;
unsigned long _updateInterval = 3600000; // In ms unsigned long _updateInterval = 3600000; // In ms
unsigned long _currentEpoc = 0; // In s unsigned long _currentEpoc = 0; // In s
unsigned long _lastUpdate = 0; // In ms unsigned long _lastUpdate = 0; // In ms
byte _packetBuffer[NTP_PACKET_SIZE]; byte _packetBuffer[NTP_PACKET_SIZE];
void sendNTPPacket(); void sendNTPPacket();
public: public:
NTPClient(); NTPClient();
explicit NTPClient(long timeOffset); explicit NTPClient(long timeOffset);
explicit NTPClient(const char* poolServerName); explicit NTPClient(const char *poolServerName);
NTPClient(const char* poolServerName, long timeOffset); NTPClient(const char *poolServerName, long timeOffset);
NTPClient(const char* poolServerName, long timeOffset, unsigned long updateInterval); NTPClient(const char *poolServerName, long timeOffset, unsigned long updateInterval);
explicit NTPClient(IPAddress poolServerIP); explicit NTPClient(IPAddress poolServerIP);
NTPClient(IPAddress poolServerIP, long timeOffset); NTPClient(IPAddress poolServerIP, long timeOffset);
NTPClient(IPAddress poolServerIP, long timeOffset, unsigned long updateInterval); NTPClient(IPAddress poolServerIP, long timeOffset, unsigned long updateInterval);
/** /**
* Set time server name * Set time server name
* *
* @param poolServerName * @param poolServerName
*/ */
void setPoolServerName(const char* poolServerName); void setPoolServerName(const char *poolServerName);
/** /**
* Set random local port * Set random local port
*/ */
void setRandomPort(unsigned int minValue = 49152, unsigned int maxValue = 65535); void setRandomPort(unsigned int minValue = 49152, unsigned int maxValue = 65535);
/** /**
* Starts the underlying UDP client with the default local port * Starts the underlying UDP client with the default local port
*/ */
void begin(); void begin();
/** /**
* Starts the underlying UDP client with the specified local port * Starts the underlying UDP client with the specified local port
*/ */
void begin(unsigned int port); void begin(unsigned int port);
/** /**
* This should be called in the main loop of your application. By default an update from the NTP Server is only * This should be called in the main loop of your application. By default an update from the NTP Server is only
* made every 60 seconds. This can be configured in the NTPClient constructor. * made every 60 seconds. This can be configured in the NTPClient constructor.
* *
* @return true on success, false on failure * @return true on success, false on failure
*/ */
bool update(); bool update();
/** /**
* This will force the update from the NTP Server. * This will force the update from the NTP Server.
* *
* @return true on success, false on failure * @return true on success, false on failure
*/ */
bool forceUpdate(); bool forceUpdate();
int getDay() const; int getDay() const;
int getHours() const; int getHours() const;
int getMinutes() const; int getMinutes() const;
int getSeconds() const; int getSeconds() const;
/** /**
* Changes the time offset. Useful for changing timezones dynamically * Changes the time offset. Useful for changing timezones dynamically
*/ */
void setTimeOffset(int timeOffset); void setTimeOffset(int timeOffset);
/** /**
* Set the update interval to another frequency. E.g. useful when the * Set the update interval to another frequency. E.g. useful when the
* timeOffset should not be set in the constructor * timeOffset should not be set in the constructor
*/ */
void setUpdateInterval(unsigned long updateInterval); void setUpdateInterval(unsigned long updateInterval);
/** /**
* @return time formatted like `hh:mm:ss` * @return time formatted like `hh:mm:ss`
*/ */
String getFormattedTime() const; String getFormattedTime() const;
/** /**
* @return time in seconds since Jan. 1, 1970 * @return time in seconds since Jan. 1, 1970
*/ */
unsigned long getEpochTime() const; unsigned long getEpochTime() const;
/** /**
* Stops the underlying UDP client * Stops the underlying UDP client
*/ */
void end(); void end();
}; };