@@ -64,64 +64,8 @@ GSMConnectionManager::GSMConnectionManager(const char *pin, const char *apn, con
6464 connectionTickTimeInterval(CHECK_INTERVAL_IDLE) {
6565}
6666
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-
11467unsigned 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 ();
12569}
12670
12771void GSMConnectionManager::init () {
@@ -169,25 +113,22 @@ void GSMConnectionManager::check() {
169113 int networkStatus = 0 ;
170114 if (now - lastConnectionTickTime > connectionTickTimeInterval) {
171115 switch (netConnectionState) {
172- case CONNECTION_STATE_IDLE :
116+ case CONNECTION_STATE_INIT :
173117 init ();
174- changeConnectionState (CONNECTION_STATE_INIT );
118+ changeConnectionState (CONNECTION_STATE_CONNECTING );
175119 break ;
176- case CONNECTION_STATE_INIT :
120+ case CONNECTION_STATE_CONNECTING :
177121 // blocking call with 4th parameter == true
178122 networkStatus = gprs.attachGPRS (apn, login, pass, true );
179123 *msgBuffer = 0 ;
180124 sprintf (msgBuffer, " GPRS.attachGPRS(): %d" , networkStatus);
181125 debugMessage (msgBuffer, 2 );
182126 if (networkStatus == ERROR) {
183127 debugMessage (" GPRS attach failed\n Make sure the antenna is connected" , 0 );
184- changeConnectionState (CONNECTION_STATE_INIT );
128+ changeConnectionState (CONNECTION_STATE_CONNECTING );
185129 lastConnectionTickTime = now;
186130 return ;
187131 }
188- changeConnectionState (CONNECTION_STATE_CONNECTING);
189- break ;
190- case CONNECTION_STATE_CONNECTING:
191132 *msgBuffer = 0 ;
192133 sprintf (msgBuffer, " Trying to ping external world" );
193134 debugMessage (msgBuffer, 2 );
@@ -204,7 +145,7 @@ void GSMConnectionManager::check() {
204145 *msgBuffer = 0 ;
205146 sprintf (msgBuffer, " Retrying in \" %d\" milliseconds" , connectionTickTimeInterval);
206147 debugMessage (msgBuffer, 2 );
207- changeConnectionState (CONNECTION_STATE_INIT );
148+ changeConnectionState (CONNECTION_STATE_CONNECTING );
208149 return ;
209150 } else {
210151 *msgBuffer = 0 ;
@@ -231,14 +172,14 @@ void GSMConnectionManager::check() {
231172 networkStatus = gsmAccess.isAccessAlive ();
232173 *msgBuffer = 0 ;
233174 sprintf (msgBuffer, " GPRS.isAccessAlive(): %d" , networkStatus);
234- debugMessage (msgBuffer, 2 );
175+ debugMessage (msgBuffer, 4 );
235176 if (networkStatus != 1 ) {
236177 changeConnectionState (CONNECTION_STATE_DISCONNECTED);
237178 return ;
238179 }
239180 *msgBuffer = 0 ;
240181 sprintf (msgBuffer, " Still connected" );
241- debugMessage (msgBuffer, 2 );
182+ debugMessage (msgBuffer, 4 );
242183 break ;
243184 case CONNECTION_STATE_DISCONNECTED:
244185 gprs.detachGPRS ();
0 commit comments