Skip to content

Commit ec51ae9

Browse files
committed
1) Changed return type to uint8_t;
2) Changed switch statement to if/else statement in example sketch and ping_cb; 3) Included socket/include/m2m_socket_host_if.h in WiFi101.cpp to use already defined constants like PING_ERR_SUCCESS; 4) Fixed spacing and added comments.
1 parent 1e55bdd commit ec51ae9

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

examples/WiFiPing/WiFiPing.ino

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This example connects to a encrypted Wifi network (WPA/WPA2).
44
Then it prints the MAC address of the Wifi shield,
55
the IP address obtained, and other network details.
6-
Then it continuously pings given IP Address.
6+
Then it continuously pings given host specified by IP Address or name.
77
88
Circuit:
99
* WiFi shield attached / MKR1000
@@ -20,11 +20,12 @@ char ssid[] = "yourNetwork"; // your network SSID (name)
2020
char pass[] = "secretPassword"; // your network password
2121
int status = WL_IDLE_STATUS; // the Wifi radio's status
2222

23+
// Specify IP address or hostname
2324
String hostName = "www.google.com";
2425
byte pingResult;
2526

2627
void setup() {
27-
//Initialize serial and wait for port to open:
28+
// Initialize serial and wait for port to open:
2829
Serial.begin(9600);
2930
while (!Serial) {
3031
; // wait for serial port to connect. Needed for native USB port only
@@ -63,11 +64,11 @@ void loop() {
6364

6465
pingResult = WiFi.ping(hostName);
6566

66-
if (pingResult == WL_PING_SUCCESS){
67+
if (pingResult == WL_PING_SUCCESS) {
6768
Serial.println("SUCCESS!");
6869
} else {
69-
Serial.print("FAILED! Error code: ");
70-
Serial.println(pingResult);
70+
Serial.print("FAILED! Error code: ");
71+
Serial.println(pingResult);
7172
};
7273

7374
delay(3000);

src/WiFi.cpp

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
extern "C" {
2323
#include "bsp/include/nm_bsp.h"
2424
#include "socket/include/socket_buffer.h"
25+
#include "socket/include/m2m_socket_host_if.h"
2526
#include "driver/source/nmasic.h"
26-
#include "driver/include/m2m_periph.h"
27+
#include "driver/include/m2m_periph.h"
2728
}
2829

2930
static void wifi_cb(uint8_t u8MsgType, void *pvMsg)
@@ -148,31 +149,23 @@ static void resolve_cb(uint8_t * /* hostName */, uint32_t hostIp)
148149
}
149150

150151
static void ping_cb(uint32 u32IPAddr, uint32 u32RTT, uint8 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: {
152+
if (PING_ERR_SUCCESS == u8ErrorCode){
153+
// Ensure this ICMP reply comes from requested IP address
154+
if (WiFi._resolve == u32IPAddr)
155+
WiFi._resolve = WL_PING_SUCCESS;
156+
else
157+
// Another network device replied to the our ICMP request
158+
WiFi._resolve = WL_PING_DEST_UNREACHABLE;
159+
}
160+
else if (PING_ERR_DEST_UNREACH == u8ErrorCode) {
165161
WiFi._resolve = WL_PING_DEST_UNREACHABLE;
166-
}; break;
167-
168-
// PING_ERR_TIMEOUT
169-
case 2: {
162+
}
163+
else if (PING_ERR_TIMEOUT == u8ErrorCode) {
170164
WiFi._resolve = WL_PING_TIMEOUT;
171-
}; break;
172-
173-
// Unknown error codes
174-
default: WiFi._resolve = WL_PING_ERROR;
175-
};
165+
}
166+
else {
167+
WiFi._resolve = WL_PING_ERROR;
168+
};
176169
};
177170

178171
WiFiClass::WiFiClass()
@@ -751,19 +744,19 @@ void WiFiClass::refresh(void)
751744
m2m_wifi_handle_events(NULL);
752745
}
753746

754-
wl_ping_result_t WiFiClass::ping(const char* hostname, uint8_t ttl){
747+
uint8_t WiFiClass::ping(const char* hostname, uint8_t ttl){
755748
IPAddress ip;
756749
if (hostByName(hostname, ip) > 0)
757750
return ping(ip, ttl);
758751
else
759752
return WL_PING_UNKNOWN_HOST;
760753
};
761754

762-
wl_ping_result_t WiFiClass::ping(const String &hostname, uint8_t ttl){
755+
uint8_t WiFiClass::ping(const String &hostname, uint8_t ttl){
763756
return ping(hostname.c_str(), ttl);
764757
};
765758

766-
wl_ping_result_t WiFiClass::ping(IPAddress host, uint8_t ttl) {
759+
uint8_t WiFiClass::ping(IPAddress host, uint8_t ttl) {
767760

768761
// Network led ON (rev A then rev B).
769762
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0);

src/WiFi101.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ class WiFiClass
146146
int hostByName(const char* hostname, IPAddress& result);
147147
int hostByName(const String &hostname, IPAddress& result) { return hostByName(hostname.c_str(), result); }
148148

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);
149+
uint8_t ping(const char* hostname, uint8_t ttl = 128);
150+
uint8_t ping(const String &hostname, uint8_t ttl = 128);
151+
uint8_t ping(IPAddress host, uint8_t ttl = 128);
152152

153153
void refresh(void);
154154

0 commit comments

Comments
 (0)