Skip to content

Commit 19c688e

Browse files
committed
Create NTP util class to handle getTime() fallback
1 parent 1324ac6 commit 19c688e

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

src/ConnectionManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#endif
2424

2525
#include <Client.h>
26+
#include <Udp.h>
27+
#include "utility/NTPUtils.h"
2628

2729
enum NetworkConnectionState {
2830
CONNECTION_STATE_INIT,
@@ -40,6 +42,7 @@ class ConnectionManager {
4042
virtual void check() = 0;
4143
virtual unsigned long getTime() = 0;
4244
virtual Client &getClient();
45+
virtual UDP &getUDP();
4346

4447
virtual NetworkConnectionState getStatus() { return netConnectionState; }
4548

src/GSMConnectionManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ class GSMConnectionManager : public ConnectionManager {
2626
virtual void init();
2727
virtual void check();
2828
virtual Client &getClient() { return networkClient; };
29+
virtual UDP &getUDP() { return udp; };
2930

3031
GSMClient networkClient;
3132
GSM gsmAccess;
3233
GPRS gprs;
34+
GSMUDP udp;
3335
private:
3436

3537
void changeConnectionState(NetworkConnectionState _newState);

src/WiFiConnectionManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class WiFiConnectionManager : public ConnectionManager {
2525
virtual void init();
2626
virtual void check();
2727
virtual Client &getClient() { return wifiClient; };
28-
28+
virtual UDP &getUDP() { return udp; };
2929

3030
private:
3131

src/utility/NTPUtils.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "NTPUtils.h"
2+
#include "Arduino.h"
3+
4+
// could be a constexpr in C++14
5+
static time_t cvt_TIME(char const *time) {
6+
char s_month[5];
7+
int month, day, year;
8+
struct tm t = {0};
9+
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
10+
11+
sscanf(time, "%s %d %d", s_month, &day, &year);
12+
13+
month = (strstr(month_names, s_month)-month_names)/3;
14+
15+
t.tm_mon = month;
16+
t.tm_mday = day;
17+
t.tm_year = year - 1900;
18+
t.tm_isdst = -1;
19+
20+
return mktime(&t);
21+
}
22+
23+
24+
NTPUtils::NTPUtils(UDP& Udp) : Udp(Udp) {}
25+
26+
bool NTPUtils::isTimeValid(unsigned long time) {
27+
Serial.println("Compile time: " + String(cvt_TIME(__DATE__)));
28+
return (time > cvt_TIME(__DATE__));
29+
}
30+
31+
void NTPUtils::sendNTPpacket(uint8_t* packetBuffer) {
32+
memset(packetBuffer, 0, NTP_PACKET_SIZE);
33+
packetBuffer[0] = 0b11100011;
34+
packetBuffer[1] = 0;
35+
packetBuffer[2] = 6;
36+
packetBuffer[3] = 0xEC;
37+
packetBuffer[12] = 49;
38+
packetBuffer[13] = 0x4E;
39+
packetBuffer[14] = 49;
40+
packetBuffer[15] = 52;
41+
Udp.beginPacket(timeServer, 123);
42+
Udp.write(packetBuffer, NTP_PACKET_SIZE);
43+
Udp.endPacket();
44+
}
45+
46+
unsigned long NTPUtils::getTime() {
47+
48+
unsigned int localPort = 8888;
49+
uint8_t packetBuffer[NTP_PACKET_SIZE];
50+
51+
Udp.begin(localPort);
52+
sendNTPpacket(packetBuffer);
53+
long start = millis();
54+
while (!Udp.parsePacket() && (millis() - start < 10000)) {
55+
56+
}
57+
if (millis() - start >= 1000) {
58+
//timeout reached
59+
return 0;
60+
}
61+
Udp.read(packetBuffer, NTP_PACKET_SIZE);
62+
63+
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
64+
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
65+
unsigned long secsSince1900 = highWord << 16 | lowWord;
66+
const unsigned long seventyYears = 2208988800UL;
67+
unsigned long epoch = secsSince1900 - seventyYears;
68+
69+
Udp.stop();
70+
71+
return epoch;
72+
}

src/utility/NTPUtils.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef __NTP_UTILS__
2+
#define __NTP_UTILS__
3+
4+
#include "Udp.h"
5+
#include <time.h>
6+
7+
class NTPUtils {
8+
public:
9+
NTPUtils(UDP& Udp);
10+
void sendNTPpacket(uint8_t* packetBuffer);
11+
unsigned long getTime();
12+
static bool isTimeValid(unsigned long time);
13+
private:
14+
const char* timeServer = "time.apple.com";
15+
const int NTP_PACKET_SIZE = 48;
16+
UDP& Udp;
17+
};
18+
19+
#endif

0 commit comments

Comments
 (0)