20
20
21
21
#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */
22
22
#include " EthernetConnectionHandler.h"
23
+ #include < Udp.h>
23
24
24
25
/* *****************************************************************************
25
26
CTOR/DTOR
@@ -72,11 +73,6 @@ NetworkConnectionState EthernetConnectionHandler::update_handleInit()
72
73
Debug.print (DBG_ERROR, F (" Error, ethernet shield was not found." ));
73
74
return NetworkConnectionState::ERROR;
74
75
}
75
- return NetworkConnectionState::CONNECTING;
76
- }
77
-
78
- NetworkConnectionState EthernetConnectionHandler::update_handleConnecting ()
79
- {
80
76
IPAddress ip (_settings.eth .ip .type , _settings.eth .ip .bytes );
81
77
82
78
// An ip address is provided -> static ip configuration
@@ -91,7 +87,7 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
91
87
Debug.print (DBG_ERROR, F (" Failed to configure Ethernet, check cable connection" ));
92
88
Debug.print (DBG_VERBOSE, " timeout: %d, response timeout: %d" ,
93
89
_settings.eth .timeout , _settings.eth .response_timeout );
94
- return NetworkConnectionState::CONNECTING ;
90
+ return NetworkConnectionState::INIT ;
95
91
}
96
92
// An ip address is not provided -> dhcp configuration
97
93
} else {
@@ -100,10 +96,53 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
100
96
Debug.print (DBG_VERBOSE, " timeout: %d, response timeout: %d" ,
101
97
_settings.eth .timeout , _settings.eth .response_timeout );
102
98
103
- return NetworkConnectionState::CONNECTING ;
99
+ return NetworkConnectionState::INIT ;
104
100
}
105
101
}
106
102
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" ));
107
146
return NetworkConnectionState::CONNECTED;
108
147
}
109
148
0 commit comments