Skip to content

Commit 1e55bdd

Browse files
committed
1) Converted ping status codes to enum;
2) Changed ping_cb to reflect new status code enum; 3) Removed pingGateway() method; 4) Corrected bug where ping(const char* hostname, uint8_t ttl) returned WL_PING_ERROR when host was unresolved instead of WL_PING_UNKNOWN_HOST; 5) Updated WiFiPing example sketch.
1 parent b2f7245 commit 1e55bdd

File tree

3 files changed

+44
-34
lines changed

3 files changed

+44
-34
lines changed

examples/WiFiPing/WiFiPing.ino

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
created 13 July 2010
1212
by dlf (Metodo2 srl)
13-
modified 13 May 2016
13+
modified 09 June 2016
1414
by Petar Georgiev
1515
*/
1616
#include <SPI.h>
@@ -64,14 +64,10 @@ void loop() {
6464
pingResult = WiFi.ping(hostName);
6565

6666
if (pingResult == WL_PING_SUCCESS){
67-
Serial.println("SUCCESS");
67+
Serial.println("SUCCESS!");
6868
} else {
69-
switch (pingResult){
70-
case WL_PING_DEST_UNREACHABLE: { Serial.println("Destination host unreachable"); }; break;
71-
case WL_PING_TIMEOUT: { Serial.println("Request Timed Out"); }; break;
72-
case WL_PING_UNKNOWN_HOST: { Serial.println("Unable to resolve hostname to IP Address"); }; break;
73-
case WL_PING_ERROR: { Serial.println("Internal error"); }; break;
74-
};
69+
Serial.print("FAILED! Error code: ");
70+
Serial.println(pingResult);
7571
};
7672

7773
delay(3000);

src/WiFi.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,31 @@ static void resolve_cb(uint8_t * /* hostName */, uint32_t hostIp)
148148
}
149149

150150
static void ping_cb(uint32 u32IPAddr, uint32 u32RTT, uint8 u8ErrorCode) {
151-
if (u8ErrorCode == 0)
152-
if (WiFi._resolve == u32IPAddr)
153-
WiFi._resolve = WL_PING_SUCCESS;
154-
else
155-
WiFi._resolve = WL_PING_DEST_UNREACHABLE;
156-
else
157-
WiFi._resolve = u8ErrorCode;
151+
switch(u8ErrorCode){
152+
153+
// PING_ERR_SUCCESS
154+
case 0: {
155+
// Ensure that reply IP address matches requested IP address
156+
if (WiFi._resolve == u32IPAddr)
157+
WiFi._resolve = WL_PING_SUCCESS;
158+
else
159+
// Another network device replied to the our ICMP request
160+
WiFi._resolve = WL_PING_DEST_UNREACHABLE;
161+
}; break;
162+
163+
// PING_ERR_DEST_UNREACH
164+
case 1: {
165+
WiFi._resolve = WL_PING_DEST_UNREACHABLE;
166+
}; break;
167+
168+
// PING_ERR_TIMEOUT
169+
case 2: {
170+
WiFi._resolve = WL_PING_TIMEOUT;
171+
}; break;
172+
173+
// Unknown error codes
174+
default: WiFi._resolve = WL_PING_ERROR;
175+
};
158176
};
159177

160178
WiFiClass::WiFiClass()
@@ -733,19 +751,19 @@ void WiFiClass::refresh(void)
733751
m2m_wifi_handle_events(NULL);
734752
}
735753

736-
uint8_t WiFiClass::ping(const char* hostname, uint8_t ttl){
754+
wl_ping_result_t WiFiClass::ping(const char* hostname, uint8_t ttl){
737755
IPAddress ip;
738756
if (hostByName(hostname, ip) > 0)
739757
return ping(ip, ttl);
740758
else
741-
return WL_PING_ERROR;
759+
return WL_PING_UNKNOWN_HOST;
742760
};
743761

744-
uint8_t WiFiClass::ping(const String &hostname, uint8_t ttl){
762+
wl_ping_result_t WiFiClass::ping(const String &hostname, uint8_t ttl){
745763
return ping(hostname.c_str(), ttl);
746764
};
747765

748-
uint8_t WiFiClass::ping(IPAddress host, uint8_t ttl) {
766+
wl_ping_result_t WiFiClass::ping(IPAddress host, uint8_t ttl) {
749767

750768
// Network led ON (rev A then rev B).
751769
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0);
@@ -775,12 +793,7 @@ uint8_t WiFiClass::ping(IPAddress host, uint8_t ttl) {
775793
if (_resolve == dstHost)
776794
return WL_PING_TIMEOUT;
777795
else
778-
return _resolve;
779-
};
780-
781-
uint8_t WiFiClass::pingGateway(){
782-
return ping(_gateway);
783-
796+
return (wl_ping_result_t)_resolve;
784797
};
785798

786799
WiFiClass WiFi;

src/WiFi101.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ typedef enum {
6161
WL_AP_MODE
6262
} wl_mode_t;
6363

64-
#define WL_PING_SUCCESS 0
65-
#define WL_PING_DEST_UNREACHABLE 1
66-
#define WL_PING_TIMEOUT 2
67-
#define WL_PING_UNKNOWN_HOST 3
68-
#define WL_PING_ERROR 4
64+
typedef enum {
65+
WL_PING_SUCCESS = 0,
66+
WL_PING_DEST_UNREACHABLE,
67+
WL_PING_TIMEOUT,
68+
WL_PING_UNKNOWN_HOST,
69+
WL_PING_ERROR
70+
} wl_ping_result_t;
6971

7072
class WiFiClass
7173
{
@@ -144,10 +146,9 @@ class WiFiClass
144146
int hostByName(const char* hostname, IPAddress& result);
145147
int hostByName(const String &hostname, IPAddress& result) { return hostByName(hostname.c_str(), result); }
146148

147-
uint8_t ping(const char* hostname, uint8_t ttl = 128);
148-
uint8_t ping(const String &hostname, uint8_t ttl = 128);
149-
uint8_t ping(IPAddress host, uint8_t ttl = 128);
150-
uint8_t pingGateway();
149+
wl_ping_result_t ping(const char* hostname, uint8_t ttl = 128);
150+
wl_ping_result_t ping(const String &hostname, uint8_t ttl = 128);
151+
wl_ping_result_t ping(IPAddress host, uint8_t ttl = 128);
151152

152153
void refresh(void);
153154

0 commit comments

Comments
 (0)