Skip to content

Commit a5bcc23

Browse files
committed
Ping functions now return Round Trip Time (RTT) on success or negative error codes on failure
As proposed in #68 (comment) all ping functions return: 1) on success: RTT (values >= 0) 2) on failure: negative values wl_ping_result_t was updated to: typedef enum { WL_PING_DEST_UNREACHABLE = -1, WL_PING_TIMEOUT = -2, WL_PING_UNKNOWN_HOST = -3, WL_PING_ERROR = -4 } wl_ping_result_t; WiFiPing example was updated also.
1 parent 663d9f8 commit a5bcc23

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

examples/WiFiPing/WiFiPing.ino

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,29 @@ void loop() {
6363

6464
pingResult = WiFi.ping(hostName);
6565

66-
if (pingResult == WL_PING_SUCCESS) {
67-
Serial.println("SUCCESS!");
66+
if (pingResult >= 0) {
67+
Serial.print("SUCCESS! RTT = ");
68+
Serial.println(pingResult);
6869
} else {
6970
Serial.print("FAILED! Error code: ");
7071
Serial.println(pingResult);
7172
}
7273

73-
delay(3000);
74+
delay(1000);
7475
}
7576

7677
void printWifiData() {
7778
// print your WiFi shield's IP address:
7879
IPAddress ip = WiFi.localIP();
79-
Serial.print("IP Address: ");
80-
Serial.println(ip);
80+
Serial.print("IP address : ");
8181
Serial.println(ip);
8282

83+
Serial.print("Subnet mask: ");
84+
Serial.println((IPAddress)WiFi.subnetMask());
85+
86+
Serial.print("Gateway IP : ");
87+
Serial.println((IPAddress)WiFi.gatewayIP());
88+
8389
// print your MAC address:
8490
byte mac[6];
8591
WiFi.macAddress(mac);
@@ -121,12 +127,12 @@ void printCurrentNet() {
121127

122128
// print the received signal strength:
123129
long rssi = WiFi.RSSI();
124-
Serial.print("signal strength (RSSI):");
130+
Serial.print("signal strength (RSSI): ");
125131
Serial.println(rssi);
126132

127133
// print the encryption type:
128134
byte encryption = WiFi.encryptionType();
129-
Serial.print("Encryption Type:");
135+
Serial.print("Encryption Type: ");
130136
Serial.println(encryption, HEX);
131137
Serial.println();
132138
}

src/WiFi.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,22 +159,22 @@ static void resolve_cb(uint8_t * /* hostName */, uint32_t hostIp)
159159
WiFi._resolve = hostIp;
160160
}
161161

162-
static void ping_cb(uint32 u32IPAddr, uint32 /*u32RTT*/, uint8 u8ErrorCode)
162+
static void ping_cb(uint32 u32IPAddr, uint32 u32RTT, uint8 u8ErrorCode)
163163
{
164164
if (PING_ERR_SUCCESS == u8ErrorCode) {
165165
// Ensure this ICMP reply comes from requested IP address
166166
if (WiFi._resolve == u32IPAddr) {
167-
WiFi._resolve = WL_PING_SUCCESS;
167+
WiFi._resolve = (uint32_t)u32RTT;
168168
} else {
169169
// Another network device replied to the our ICMP request
170-
WiFi._resolve = WL_PING_DEST_UNREACHABLE;
170+
WiFi._resolve = (uint32_t)WL_PING_DEST_UNREACHABLE;
171171
}
172172
} else if (PING_ERR_DEST_UNREACH == u8ErrorCode) {
173-
WiFi._resolve = WL_PING_DEST_UNREACHABLE;
173+
WiFi._resolve = (uint32_t)WL_PING_DEST_UNREACHABLE;
174174
} else if (PING_ERR_TIMEOUT == u8ErrorCode) {
175-
WiFi._resolve = WL_PING_TIMEOUT;
175+
WiFi._resolve = (uint32_t)WL_PING_TIMEOUT;
176176
} else {
177-
WiFi._resolve = WL_PING_ERROR;
177+
WiFi._resolve = (uint32_t)WL_PING_ERROR;
178178
}
179179
}
180180

@@ -797,7 +797,7 @@ void WiFiClass::noLowPowerMode(void)
797797
m2m_wifi_set_sleep_mode(M2M_NO_PS, false);
798798
}
799799

800-
uint8_t WiFiClass::ping(const char* hostname, uint8_t ttl)
800+
int WiFiClass::ping(const char* hostname, uint8_t ttl)
801801
{
802802
IPAddress ip;
803803

@@ -808,12 +808,12 @@ uint8_t WiFiClass::ping(const char* hostname, uint8_t ttl)
808808
}
809809
}
810810

811-
uint8_t WiFiClass::ping(const String &hostname, uint8_t ttl)
811+
int WiFiClass::ping(const String &hostname, uint8_t ttl)
812812
{
813813
return ping(hostname.c_str(), ttl);
814814
}
815815

816-
uint8_t WiFiClass::ping(IPAddress host, uint8_t ttl)
816+
int WiFiClass::ping(IPAddress host, uint8_t ttl)
817817
{
818818
// Network led ON (rev A then rev B).
819819
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0);
@@ -843,7 +843,7 @@ uint8_t WiFiClass::ping(IPAddress host, uint8_t ttl)
843843
if (_resolve == dstHost) {
844844
return WL_PING_TIMEOUT;
845845
} else {
846-
return _resolve;
846+
return (int)_resolve;
847847
}
848848
}
849849

src/WiFi101.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ typedef enum {
6565
} wl_mode_t;
6666

6767
typedef enum {
68-
WL_PING_SUCCESS = 0,
69-
WL_PING_DEST_UNREACHABLE,
70-
WL_PING_TIMEOUT,
71-
WL_PING_UNKNOWN_HOST,
72-
WL_PING_ERROR
68+
WL_PING_DEST_UNREACHABLE = -1,
69+
WL_PING_TIMEOUT = -2,
70+
WL_PING_UNKNOWN_HOST = -3,
71+
WL_PING_ERROR = -4
7372
} wl_ping_result_t;
7473

7574
class WiFiClass
@@ -152,9 +151,9 @@ class WiFiClass
152151
int hostByName(const char* hostname, IPAddress& result);
153152
int hostByName(const String &hostname, IPAddress& result) { return hostByName(hostname.c_str(), result); }
154153

155-
uint8_t ping(const char* hostname, uint8_t ttl = 128);
156-
uint8_t ping(const String &hostname, uint8_t ttl = 128);
157-
uint8_t ping(IPAddress host, uint8_t ttl = 128);
154+
int ping(const char* hostname, uint8_t ttl = 128);
155+
int ping(const String &hostname, uint8_t ttl = 128);
156+
int ping(IPAddress host, uint8_t ttl = 128);
158157

159158
void refresh(void);
160159

0 commit comments

Comments
 (0)