Skip to content

Commit 68d2c3c

Browse files
committed
GSM: add fallback getTime retrieve
Some operators return bogus getTime; if reported value is too low (generating it at compile time would be awesome) use fallback mode.
1 parent db14c67 commit 68d2c3c

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

src/GSMConnectionManager.h

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class GSMConnectionManager : public ConnectionManager {
4646
const char *pin, *apn, *login, *pass;
4747
unsigned long lastConnectionTickTime, lastNetworkStep;
4848
int connectionTickTimeInterval;
49+
GSMUDP Udp;
50+
void sendNTPpacket(const char * address, uint8_t* packetBuffer);
51+
unsigned long getNTPTime();
4952
};
5053

5154
static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000;
@@ -59,8 +62,64 @@ GSMConnectionManager::GSMConnectionManager(const char *pin, const char *apn, con
5962
connectionTickTimeInterval(CHECK_INTERVAL_IDLE) {
6063
}
6164

65+
void GSMConnectionManager::sendNTPpacket(const char * address, uint8_t* packetBuffer) {
66+
const int NTP_PACKET_SIZE = 48;
67+
memset(packetBuffer, 0, NTP_PACKET_SIZE);
68+
packetBuffer[0] = 0b11100011;
69+
packetBuffer[1] = 0;
70+
packetBuffer[2] = 6;
71+
packetBuffer[3] = 0xEC;
72+
packetBuffer[12] = 49;
73+
packetBuffer[13] = 0x4E;
74+
packetBuffer[14] = 49;
75+
packetBuffer[15] = 52;
76+
Udp.beginPacket(address, 123);
77+
Udp.write(packetBuffer, NTP_PACKET_SIZE);
78+
Udp.endPacket();
79+
}
80+
81+
unsigned long GSMConnectionManager::getNTPTime() {
82+
83+
unsigned int localPort = 8888;
84+
const char timeServer[] = "time.apple.com";
85+
const int NTP_PACKET_SIZE = 48;
86+
uint8_t packetBuffer[NTP_PACKET_SIZE];
87+
88+
Udp.begin(localPort);
89+
sendNTPpacket(timeServer, packetBuffer);
90+
long start = millis();
91+
while (!Udp.parsePacket() && (millis() - start < 10000)) {
92+
93+
}
94+
if (millis() - start >= 1000) {
95+
//timeout reached
96+
return 0;
97+
}
98+
Udp.read(packetBuffer, NTP_PACKET_SIZE);
99+
100+
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
101+
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
102+
unsigned long secsSince1900 = highWord << 16 | lowWord;
103+
const unsigned long seventyYears = 2208988800UL;
104+
unsigned long epoch = secsSince1900 - seventyYears;
105+
106+
Udp.stop();
107+
108+
return epoch;
109+
}
110+
111+
62112
unsigned long GSMConnectionManager::getTime() {
63-
return gsmAccess.getTime();
113+
unsigned long time = gsmAccess.getTime();
114+
// some simcard can return bogus time; in this case request it directly to an ntp server
115+
if (time < 1549967573) {
116+
Serial.println("Ask time to apple.com");
117+
return getNTPTime();
118+
} else {
119+
Serial.print("Using network provided time: ");
120+
Serial.println(time);
121+
return time;
122+
}
64123
}
65124

66125
void GSMConnectionManager::init() {

0 commit comments

Comments
 (0)