Skip to content

Commit 379adfb

Browse files
committed
set connected state after checking internet availability
1 parent 6fbe6b6 commit 379adfb

5 files changed

+158
-26
lines changed

Diff for: src/CatM1ConnectionHandler.cpp

+19-5
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleInit()
6464
pinMode(ON_MKR2, OUTPUT);
6565
digitalWrite(ON_MKR2, HIGH);
6666
#endif
67-
return NetworkConnectionState::CONNECTING;
68-
}
6967

70-
NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
71-
{
7268
if(!GSM.begin(
7369
_settings.catm1.pin,
7470
_settings.catm1.apn,
@@ -80,7 +76,25 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
8076
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
8177
return NetworkConnectionState::ERROR;
8278
}
83-
Debug.print(DBG_INFO, F("Connected to Network"));
79+
80+
81+
return NetworkConnectionState::CONNECTING;
82+
}
83+
84+
NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
85+
{
86+
if (!GSM.isConnected())
87+
{
88+
return NetworkConnectionState::INIT;
89+
}
90+
91+
if(getTime() == 0){
92+
Debug.print(DBG_ERROR, F("Internet check failed"));
93+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
94+
return NetworkConnectionState::CONNECTING;
95+
}
96+
97+
Debug.print(DBG_INFO, F("Connected to Internet"));
8498
return NetworkConnectionState::CONNECTED;
8599
}
86100

Diff for: src/CellularConnectionHandler.cpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,27 @@ NetworkConnectionState CellularConnectionHandler::update_handleInit()
6262
Debug.print(DBG_ERROR, F("SIM not present or wrong PIN"));
6363
return NetworkConnectionState::ERROR;
6464
}
65-
return NetworkConnectionState::CONNECTING;
66-
}
6765

68-
NetworkConnectionState CellularConnectionHandler::update_handleConnecting()
69-
{
7066
if (!_cellular.connect(String(_settings.cell.apn), String(_settings.cell.login), String(_settings.cell.pass))) {
7167
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
7268
return NetworkConnectionState::ERROR;
7369
}
7470
Debug.print(DBG_INFO, F("Connected to Network"));
71+
return NetworkConnectionState::CONNECTING;
72+
}
73+
74+
NetworkConnectionState CellularConnectionHandler::update_handleConnecting()
75+
{
76+
if (!_cellular.isConnectedToInternet()) {
77+
return NetworkConnectionState::INIT;
78+
}
79+
80+
if(getTime() == 0){
81+
Debug.print(DBG_ERROR, F("Internet check failed"));
82+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
83+
return NetworkConnectionState::CONNECTING;
84+
}
85+
7586
return NetworkConnectionState::CONNECTED;
7687
}
7788

Diff for: src/EthernetConnectionHandler.cpp

+46-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */
2222
#include "EthernetConnectionHandler.h"
23+
#include <Udp.h>
2324

2425
/******************************************************************************
2526
CTOR/DTOR
@@ -72,11 +73,6 @@ NetworkConnectionState EthernetConnectionHandler::update_handleInit()
7273
Debug.print(DBG_ERROR, F("Error, ethernet shield was not found."));
7374
return NetworkConnectionState::ERROR;
7475
}
75-
return NetworkConnectionState::CONNECTING;
76-
}
77-
78-
NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
79-
{
8076
IPAddress ip(_settings.eth.ip.type, _settings.eth.ip.bytes);
8177

8278
// An ip address is provided -> static ip configuration
@@ -91,7 +87,7 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
9187
Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection"));
9288
Debug.print(DBG_VERBOSE, "timeout: %d, response timeout: %d",
9389
_settings.eth.timeout, _settings.eth.response_timeout);
94-
return NetworkConnectionState::CONNECTING;
90+
return NetworkConnectionState::INIT;
9591
}
9692
// An ip address is not provided -> dhcp configuration
9793
} else {
@@ -100,10 +96,53 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
10096
Debug.print(DBG_VERBOSE, "timeout: %d, response timeout: %d",
10197
_settings.eth.timeout, _settings.eth.response_timeout);
10298

103-
return NetworkConnectionState::CONNECTING;
99+
return NetworkConnectionState::INIT;
104100
}
105101
}
106102

103+
return NetworkConnectionState::CONNECTING;
104+
}
105+
106+
NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
107+
{
108+
if (Ethernet.linkStatus() == LinkOFF) {
109+
return NetworkConnectionState::INIT;
110+
}
111+
// Request time from NTP server for testing internet connection
112+
UDP &udp = getUDP();
113+
udp.begin(4001);
114+
uint8_t ntp_packet_buf[48] = {0};
115+
116+
ntp_packet_buf[0] = 0b11100011;
117+
ntp_packet_buf[1] = 0;
118+
ntp_packet_buf[2] = 6;
119+
ntp_packet_buf[3] = 0xEC;
120+
ntp_packet_buf[12] = 49;
121+
ntp_packet_buf[13] = 0x4E;
122+
ntp_packet_buf[14] = 49;
123+
ntp_packet_buf[15] = 52;
124+
125+
udp.beginPacket("time.arduino.cc", 123);
126+
udp.write(ntp_packet_buf, 48);
127+
udp.endPacket();
128+
129+
bool is_timeout = false;
130+
unsigned long const start = millis();
131+
do
132+
{
133+
is_timeout = (millis() - start) >= 1000;
134+
} while(!is_timeout && !udp.parsePacket());
135+
136+
if(is_timeout) {
137+
udp.stop();
138+
Debug.print(DBG_ERROR, F("Internet check failed"));
139+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
140+
return NetworkConnectionState::CONNECTING;
141+
}
142+
143+
udp.read(ntp_packet_buf, 48);
144+
udp.stop();
145+
Debug.print(DBG_INFO, F("Connected to Internet"));
107146
return NetworkConnectionState::CONNECTED;
108147
}
109148

Diff for: src/NBConnectionHandler.cpp

+17-5
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ NetworkConnectionState NBConnectionHandler::update_handleInit()
102102
Debug.print(DBG_ERROR, F("SIM not present or wrong PIN"));
103103
return NetworkConnectionState::ERROR;
104104
}
105-
}
106-
107-
NetworkConnectionState NBConnectionHandler::update_handleConnecting()
108-
{
109105
NB_NetworkStatus_t const network_status = _nb_gprs.attachGPRS(true);
110106
Debug.print(DBG_DEBUG, F("GPRS.attachGPRS(): %d"), network_status);
111107
if (network_status == NB_NetworkStatus_t::NB_ERROR)
@@ -116,8 +112,24 @@ NetworkConnectionState NBConnectionHandler::update_handleConnecting()
116112
else
117113
{
118114
Debug.print(DBG_INFO, F("Connected to GPRS Network"));
119-
return NetworkConnectionState::CONNECTED;
115+
return NetworkConnectionState::CONNECTING;
120116
}
117+
118+
}
119+
120+
NetworkConnectionState NBConnectionHandler::update_handleConnecting()
121+
{
122+
if(_nb.isAccessAlive() != 1){
123+
return NetworkConnectionState::INIT;
124+
}
125+
126+
if(getTime() == 0){
127+
Debug.print(DBG_ERROR, F("Internet check failed"));
128+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
129+
return NetworkConnectionState::CONNECTING;
130+
}
131+
132+
return NetworkConnectionState::CONNECTED;
121133
}
122134

123135
NetworkConnectionState NBConnectionHandler::update_handleConnected()

Diff for: src/WiFiConnectionHandler.cpp

+61-5
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit()
9696
#else
9797
WiFi.mode(WIFI_STA);
9898
#endif /* #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) */
99-
return NetworkConnectionState::CONNECTING;
100-
}
10199

102-
NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
103-
{
104100
if (WiFi.status() != WL_CONNECTED)
105101
{
106102
WiFi.begin(_settings.wifi.ssid, _settings.wifi.pwd);
@@ -120,7 +116,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
120116
Debug.print(DBG_ERROR, F("Connection to \"%s\" failed"), _settings.wifi.ssid);
121117
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
122118
#endif
123-
return NetworkConnectionState::CONNECTING;
119+
return NetworkConnectionState::INIT;
124120
}
125121
else
126122
{
@@ -130,6 +126,66 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
130126
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
131127
configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov");
132128
#endif
129+
return NetworkConnectionState::CONNECTING;
130+
}
131+
}
132+
133+
NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
134+
{
135+
if (WiFi.status() != NETWORK_CONNECTED){
136+
return NetworkConnectionState::INIT;
137+
}
138+
bool checkSuccess = false;
139+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M7) || \
140+
defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA)
141+
// Request time from NTP server for testing internet connection
142+
UDP &udp = getUDP();
143+
udp.begin(4001);
144+
uint8_t ntp_packet_buf[48] = {0};
145+
146+
ntp_packet_buf[0] = 0b11100011;
147+
ntp_packet_buf[1] = 0;
148+
ntp_packet_buf[2] = 6;
149+
ntp_packet_buf[3] = 0xEC;
150+
ntp_packet_buf[12] = 49;
151+
ntp_packet_buf[13] = 0x4E;
152+
ntp_packet_buf[14] = 49;
153+
ntp_packet_buf[15] = 52;
154+
udp.beginPacket("time.arduino.cc", 123);
155+
udp.write(ntp_packet_buf, 48);
156+
udp.endPacket();
157+
158+
bool is_timeout = false;
159+
unsigned long const start = millis();
160+
do
161+
{
162+
is_timeout = (millis() - start) >= 1000;
163+
} while(!is_timeout && !udp.parsePacket());
164+
if(is_timeout) {
165+
udp.stop();
166+
}
167+
else
168+
{
169+
udp.read(ntp_packet_buf, 48);
170+
udp.stop();
171+
checkSuccess = true;
172+
}
173+
#else
174+
int const ping_result = WiFi.ping("time.arduino.cc");
175+
Debug.print(DBG_INFO, F("WiFi.ping(): %d"), ping_result);
176+
if (ping_result > 0)
177+
{
178+
checkSuccess = true;
179+
}
180+
#endif
181+
if(!checkSuccess){
182+
Debug.print(DBG_ERROR, F("Internet check failed"));
183+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
184+
return NetworkConnectionState::CONNECTING;
185+
}
186+
else
187+
{
188+
Debug.print(DBG_INFO, F("Connected to Internet"));
133189
return NetworkConnectionState::CONNECTED;
134190
}
135191
}

0 commit comments

Comments
 (0)