@@ -64,64 +64,8 @@ GSMConnectionManager::GSMConnectionManager(const char *pin, const char *apn, con
64
64
connectionTickTimeInterval(CHECK_INTERVAL_IDLE) {
65
65
}
66
66
67
- void GSMConnectionManager::sendNTPpacket (const char * address, uint8_t * packetBuffer) {
68
- const int NTP_PACKET_SIZE = 48 ;
69
- memset (packetBuffer, 0 , NTP_PACKET_SIZE);
70
- packetBuffer[0 ] = 0b11100011 ;
71
- packetBuffer[1 ] = 0 ;
72
- packetBuffer[2 ] = 6 ;
73
- packetBuffer[3 ] = 0xEC ;
74
- packetBuffer[12 ] = 49 ;
75
- packetBuffer[13 ] = 0x4E ;
76
- packetBuffer[14 ] = 49 ;
77
- packetBuffer[15 ] = 52 ;
78
- Udp.beginPacket (address, 123 );
79
- Udp.write (packetBuffer, NTP_PACKET_SIZE);
80
- Udp.endPacket ();
81
- }
82
-
83
- unsigned long GSMConnectionManager::getNTPTime () {
84
-
85
- unsigned int localPort = 8888 ;
86
- const char timeServer[] = " time.apple.com" ;
87
- const int NTP_PACKET_SIZE = 48 ;
88
- uint8_t packetBuffer[NTP_PACKET_SIZE];
89
-
90
- Udp.begin (localPort);
91
- sendNTPpacket (timeServer, packetBuffer);
92
- long start = millis ();
93
- while (!Udp.parsePacket () && (millis () - start < 10000 )) {
94
-
95
- }
96
- if (millis () - start >= 1000 ) {
97
- // timeout reached
98
- return 0 ;
99
- }
100
- Udp.read (packetBuffer, NTP_PACKET_SIZE);
101
-
102
- unsigned long highWord = word (packetBuffer[40 ], packetBuffer[41 ]);
103
- unsigned long lowWord = word (packetBuffer[42 ], packetBuffer[43 ]);
104
- unsigned long secsSince1900 = highWord << 16 | lowWord;
105
- const unsigned long seventyYears = 2208988800UL ;
106
- unsigned long epoch = secsSince1900 - seventyYears;
107
-
108
- Udp.stop ();
109
-
110
- return epoch;
111
- }
112
-
113
-
114
67
unsigned long GSMConnectionManager::getTime () {
115
- unsigned long time = gsmAccess.getTime ();
116
- // some simcard can return bogus time; in this case request it directly to an ntp server
117
- if (time < 1549967573 ) {
118
- Serial.println (" Ask time to apple.com" );
119
- return getNTPTime ();
120
- } else {
121
- Serial.print (" Using network provided time: " );
122
- Serial.println (time);
123
- return time;
124
- }
68
+ return gsmAccess.getTime ();
125
69
}
126
70
127
71
void GSMConnectionManager::init () {
@@ -169,25 +113,22 @@ void GSMConnectionManager::check() {
169
113
int networkStatus = 0 ;
170
114
if (now - lastConnectionTickTime > connectionTickTimeInterval) {
171
115
switch (netConnectionState) {
172
- case CONNECTION_STATE_IDLE :
116
+ case CONNECTION_STATE_INIT :
173
117
init ();
174
- changeConnectionState (CONNECTION_STATE_INIT );
118
+ changeConnectionState (CONNECTION_STATE_CONNECTING );
175
119
break ;
176
- case CONNECTION_STATE_INIT :
120
+ case CONNECTION_STATE_CONNECTING :
177
121
// blocking call with 4th parameter == true
178
122
networkStatus = gprs.attachGPRS (apn, login, pass, true );
179
123
*msgBuffer = 0 ;
180
124
sprintf (msgBuffer, " GPRS.attachGPRS(): %d" , networkStatus);
181
125
debugMessage (msgBuffer, 2 );
182
126
if (networkStatus == ERROR) {
183
127
debugMessage (" GPRS attach failed\n Make sure the antenna is connected" , 0 );
184
- changeConnectionState (CONNECTION_STATE_INIT );
128
+ changeConnectionState (CONNECTION_STATE_CONNECTING );
185
129
lastConnectionTickTime = now;
186
130
return ;
187
131
}
188
- changeConnectionState (CONNECTION_STATE_CONNECTING);
189
- break ;
190
- case CONNECTION_STATE_CONNECTING:
191
132
*msgBuffer = 0 ;
192
133
sprintf (msgBuffer, " Trying to ping external world" );
193
134
debugMessage (msgBuffer, 2 );
@@ -204,7 +145,7 @@ void GSMConnectionManager::check() {
204
145
*msgBuffer = 0 ;
205
146
sprintf (msgBuffer, " Retrying in \" %d\" milliseconds" , connectionTickTimeInterval);
206
147
debugMessage (msgBuffer, 2 );
207
- changeConnectionState (CONNECTION_STATE_INIT );
148
+ changeConnectionState (CONNECTION_STATE_CONNECTING );
208
149
return ;
209
150
} else {
210
151
*msgBuffer = 0 ;
@@ -231,14 +172,14 @@ void GSMConnectionManager::check() {
231
172
networkStatus = gsmAccess.isAccessAlive ();
232
173
*msgBuffer = 0 ;
233
174
sprintf (msgBuffer, " GPRS.isAccessAlive(): %d" , networkStatus);
234
- debugMessage (msgBuffer, 2 );
175
+ debugMessage (msgBuffer, 4 );
235
176
if (networkStatus != 1 ) {
236
177
changeConnectionState (CONNECTION_STATE_DISCONNECTED);
237
178
return ;
238
179
}
239
180
*msgBuffer = 0 ;
240
181
sprintf (msgBuffer, " Still connected" );
241
- debugMessage (msgBuffer, 2 );
182
+ debugMessage (msgBuffer, 4 );
242
183
break ;
243
184
case CONNECTION_STATE_DISCONNECTED:
244
185
gprs.detachGPRS ();
0 commit comments