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
0 commit comments