Skip to content

Commit d00befa

Browse files
authored
Merge pull request #1014 from fabik111/add-ping-command
Ping command
2 parents 540c1cd + 17ff057 commit d00befa

File tree

41 files changed

+303
-29
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+303
-29
lines changed

cores/arduino/mbed/connectivity/drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.h

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class WhdSTAInterface : public WiFiInterface, public EMACInterface {
122122
nsapi_error_t set_timeout(uint32_t timeout)
123123
{
124124
_timeout = timeout;
125+
return NSAPI_ERROR_OK;
125126
}
126127

127128
/** Set blocking status of interface.

cores/arduino/mbed/connectivity/drivers/wifi/COMPONENT_WHD/wifi-host-driver/inc/whd_version.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
#define WHD_VERSION "v1.94.0"
18+
/* This define is used by arduino::WiFiClass::firmwareVersion() do not prepend v */
19+
#define WHD_VERSION "1.94.0"
1920
#define WHD_BRANCH "v1.94.0"
2021
#define WHD_DATE "2021-04-27 16:54:34 +0800"

cores/arduino/mbed/connectivity/netsocket/include/netsocket/ICMPSocket.h

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class ICMPSocket : public InternetDatagramSocket {
3737
*/
3838
ICMPSocket();
3939

40+
#if MBED_CONF_LWIP_RAW_SOCKET_ENABLED
41+
int ping(SocketAddress &socketAddress, uint32_t timeout);
42+
#endif
43+
4044
#if !defined(DOXYGEN_ONLY)
4145

4246
protected:

libraries/GSM/src/GSM.h

-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ class GSMClass : public MbedSocketClass {
108108
void trace(Stream& stream);
109109
void setTraceLevel(int trace_level, bool timestamp = false, bool at_trace = false);
110110
#endif
111-
int ping(const char* hostname, uint8_t ttl = 128);
112-
int ping(const String& hostname, uint8_t ttl = 128);
113-
int ping(IPAddress host, uint8_t ttl = 128);
114111
bool isConnected();
115112

116113
friend class GSMClient;

libraries/SocketWrapper/src/SocketHelpers.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "SocketHelpers.h"
2+
#include <ICMPSocket.h>
23

34
uint8_t* arduino::MbedSocketClass::macAddress(uint8_t* mac) {
45
const char* mac_str = getNetwork()->get_mac_address();
@@ -74,6 +75,24 @@ arduino::IPAddress arduino::MbedSocketClass::dnsIP(int n) {
7475
return ipAddressFromSocketAddress(ip);
7576
}
7677

78+
int arduino::MbedSocketClass::ping(const char *hostname, uint8_t ttl)
79+
{
80+
SocketAddress socketAddress;
81+
gethostbyname(getNetwork(),hostname, &socketAddress);
82+
return ping(socketAddress, ttl);
83+
}
84+
85+
int arduino::MbedSocketClass::ping(const String &hostname, uint8_t ttl)
86+
{
87+
return ping(hostname.c_str(), ttl);
88+
}
89+
90+
int arduino::MbedSocketClass::ping(IPAddress host, uint8_t ttl)
91+
{
92+
SocketAddress socketAddress = socketAddressFromIpAddress(host, 0);
93+
return ping(socketAddress, ttl);
94+
}
95+
7796
void arduino::MbedSocketClass::config(arduino::IPAddress local_ip) {
7897
IPAddress dns = local_ip;
7998
dns[3] = 1;
@@ -119,6 +138,19 @@ void arduino::MbedSocketClass::setDNS(IPAddress dns_server1, IPAddress dns_serve
119138
_dnsServer2 = SocketAddress(convertedDNSServer2);
120139
}
121140

141+
int arduino::MbedSocketClass::ping(SocketAddress &socketAddress, uint8_t ttl, uint32_t timeout)
142+
{
143+
/* ttl is not supported by mbed ICMPSocket. Default value used is 255 */
144+
(void)ttl;
145+
ICMPSocket s;
146+
s.set_timeout(timeout);
147+
s.open(getNetwork());
148+
int response = s.ping(socketAddress, timeout);
149+
s.close();
150+
151+
return response;
152+
}
153+
122154
arduino::IPAddress arduino::MbedSocketClass::ipAddressFromSocketAddress(SocketAddress socketAddress) {
123155
nsapi_addr_t address = socketAddress.get_addr();
124156
return IPAddress(address.bytes[0], address.bytes[1], address.bytes[2], address.bytes[3]);

libraries/SocketWrapper/src/SocketHelpers.h

+12
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ class MbedSocketClass {
111111

112112
virtual NetworkInterface* getNetwork() = 0;
113113

114+
/*
115+
* Ping the specified target.
116+
*
117+
* ttl value is unused, but kept for API compatibility
118+
*
119+
* return: RTT in milliseconds or -1 on error
120+
*/
121+
int ping(const char* hostname, uint8_t ttl = 255);
122+
int ping(const String &hostname, uint8_t ttl = 255);
123+
int ping(IPAddress host, uint8_t ttl = 255);
124+
114125
/*
115126
* Download a file from an HTTP endpoint and save it in the provided `target` location on the fs
116127
* The parameter cbk can be used to perform actions on the buffer before saving on the fs
@@ -174,6 +185,7 @@ class MbedSocketClass {
174185

175186
void body_callback(const char* data, uint32_t data_len);
176187

188+
int ping(SocketAddress &socketAddress, uint8_t ttl, uint32_t timeout = 5000);
177189
static arduino::IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress);
178190
static SocketAddress socketAddressFromIpAddress(arduino::IPAddress ip, uint16_t port);
179191
static nsapi_error_t gethostbyname(NetworkInterface* interface, const char* aHostname, SocketAddress* socketAddress);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
Web ICMP Ping
3+
4+
This sketch pings a device based on the IP address or the hostname
5+
using the WiFi module.
6+
7+
This example is written for a network using WPA encryption. For
8+
WEP or WPA, change the WiFi.begin() call accordingly.
9+
10+
created 14 February 2024
11+
by paulvha
12+
modified 8 Jenuary 2025
13+
by fabik111
14+
15+
*/
16+
17+
#include <WiFi.h>
18+
#include "arduino_secrets.h"
19+
20+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
21+
char ssid[] = SECRET_SSID; // your network SSID (name)
22+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
23+
24+
int status = WL_IDLE_STATUS;
25+
26+
/* -------------------------------------------------------------------------- */
27+
void setup() {
28+
/* -------------------------------------------------------------------------- */
29+
//Initialize serial and wait for port to open:
30+
Serial.begin(9600);
31+
while (!Serial) {
32+
; // wait for serial port to connect. Needed for native USB port only
33+
}
34+
35+
// check for the WiFi module:
36+
if (WiFi.status() == WL_NO_MODULE) {
37+
Serial.println("Communication with WiFi module failed.");
38+
// don't continue
39+
while (true);
40+
}
41+
42+
// attempt to connect to WiFi network:
43+
while (status != WL_CONNECTED) {
44+
Serial.print("Attempting to connect to SSID: ");
45+
Serial.println(ssid);
46+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
47+
status = WiFi.begin(ssid, pass);
48+
49+
// wait 3 seconds for connection:
50+
delay(3000);
51+
}
52+
53+
printWifiStatus();
54+
}
55+
56+
/* -------------------------------------------------------------------------- */
57+
void loop() {
58+
/* -------------------------------------------------------------------------- */
59+
60+
// Ping IP
61+
const IPAddress remote_ip(140,82,121,4);
62+
Serial.print("Trying to ping github.com on IP: ");
63+
Serial.println(remote_ip);
64+
65+
// using default ping count of 1
66+
int res = WiFi.ping(remote_ip);
67+
68+
if (res > 0) {
69+
Serial.print("Ping response time: ");
70+
Serial.print(res);
71+
Serial.println(" ms");
72+
}
73+
else {
74+
Serial.println("Timeout on IP!");
75+
}
76+
77+
// Ping Host
78+
const char* remote_host = "www.google.com";
79+
Serial.print("Trying to ping host: ");
80+
Serial.println(remote_host);
81+
82+
int res1 = WiFi.ping(remote_host);
83+
84+
if (res1 > 0) {
85+
Serial.print("Ping response time: ");
86+
Serial.print(res1);
87+
Serial.println(" ms");
88+
}
89+
else {
90+
Serial.println("Timeout on host!");
91+
}
92+
93+
Serial.println();
94+
delay(5000);
95+
}
96+
97+
/* -------------------------------------------------------------------------- */
98+
void printWifiStatus() {
99+
/* -------------------------------------------------------------------------- */
100+
// print the SSID of the network you're attached to:
101+
Serial.print("SSID: ");
102+
Serial.println(WiFi.SSID());
103+
104+
// print your board's IP address:
105+
IPAddress ip = WiFi.localIP();
106+
Serial.print("IP Address: ");
107+
Serial.println(ip);
108+
109+
// print the received signal strength:
110+
long rssi = WiFi.RSSI();
111+
Serial.print("signal strength (RSSI):");
112+
Serial.print(rssi);
113+
Serial.println(" dBm");
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
From 933694e0f35451d21eed77a93fa346570de20878 Mon Sep 17 00:00:00 2001
2+
From: pennam <m.pennasilico@arduino.cc>
3+
Date: Tue, 4 Feb 2025 14:31:59 +0100
4+
Subject: [PATCH] ICMPSocket: add ping
5+
6+
---
7+
.../netsocket/include/netsocket/ICMPSocket.h | 4 ++
8+
connectivity/netsocket/source/ICMPSocket.cpp | 61 +++++++++++++++++++
9+
2 files changed, 65 insertions(+)
10+
11+
diff --git a/connectivity/netsocket/include/netsocket/ICMPSocket.h b/connectivity/netsocket/include/netsocket/ICMPSocket.h
12+
index 1837bc8e09..5e1ee8fb03 100644
13+
--- a/connectivity/netsocket/include/netsocket/ICMPSocket.h
14+
+++ b/connectivity/netsocket/include/netsocket/ICMPSocket.h
15+
@@ -37,6 +37,10 @@ public:
16+
*/
17+
ICMPSocket();
18+
19+
+#if MBED_CONF_LWIP_RAW_SOCKET_ENABLED
20+
+ int ping(SocketAddress &socketAddress, uint32_t timeout);
21+
+#endif
22+
+
23+
#if !defined(DOXYGEN_ONLY)
24+
25+
protected:
26+
diff --git a/connectivity/netsocket/source/ICMPSocket.cpp b/connectivity/netsocket/source/ICMPSocket.cpp
27+
index f6c9b98de1..d8ea954835 100644
28+
--- a/connectivity/netsocket/source/ICMPSocket.cpp
29+
+++ b/connectivity/netsocket/source/ICMPSocket.cpp
30+
@@ -16,12 +16,73 @@
31+
*/
32+
33+
#include "ICMPSocket.h"
34+
+#if MBED_CONF_LWIP_RAW_SOCKET_ENABLED
35+
+#include "drivers/Timer.h"
36+
+#include "lwip/prot/icmp.h"
37+
+#include "lwip/inet_chksum.h"
38+
+#include "lwip/prot/ip4.h"
39+
+#endif
40+
41+
ICMPSocket::ICMPSocket()
42+
{
43+
_socket_stats.stats_update_proto(this, NSAPI_ICMP);
44+
}
45+
46+
+#if MBED_CONF_LWIP_RAW_SOCKET_ENABLED
47+
+int ICMPSocket::ping(SocketAddress &socketAddress, uint32_t timeout)
48+
+{
49+
+ struct __attribute__((__packed__)) {
50+
+ struct icmp_echo_hdr header;
51+
+ uint8_t data[32];
52+
+ } request;
53+
+
54+
+ ICMPH_TYPE_SET(&request.header, ICMP_ECHO);
55+
+ ICMPH_CODE_SET(&request.header, 0);
56+
+ request.header.chksum = 0;
57+
+ request.header.id = 0xAFAF;
58+
+ request.header.seqno = random();
59+
+
60+
+ for (size_t i = 0; i < sizeof(request.data); i++) {
61+
+ request.data[i] = i;
62+
+ }
63+
+
64+
+ request.header.chksum = inet_chksum(&request, sizeof(request));
65+
+
66+
+ int res = sendto(socketAddress, &request, sizeof(request));
67+
+ if (res <= 0){
68+
+ return -1;
69+
+ }
70+
+
71+
+ mbed::Timer timer;
72+
+ timer.start();
73+
+ int elapsed = -1;
74+
+ do {
75+
+ struct __attribute__((__packed__)) {
76+
+ struct ip_hdr ipHeader;
77+
+ struct icmp_echo_hdr header;
78+
+ } response;
79+
+
80+
+ int rxSize = recvfrom(&socketAddress, &response, sizeof(response));
81+
+ if (rxSize < 0) {
82+
+ // time out
83+
+ break;
84+
+ }
85+
+
86+
+ if (rxSize < sizeof(response)) {
87+
+ // too short
88+
+ continue;
89+
+ }
90+
+
91+
+ if ((response.header.id == request.header.id) && (response.header.seqno == request.header.seqno)) {
92+
+ elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(timer.elapsed_time()).count();
93+
+ timer.stop();
94+
+ }
95+
+ } while (elapsed == -1 && std::chrono::duration_cast<std::chrono::milliseconds>(timer.elapsed_time()).count() < timeout);
96+
+
97+
+ return elapsed;
98+
+}
99+
+#endif
100+
+
101+
nsapi_protocol_t ICMPSocket::get_proto()
102+
{
103+
return NSAPI_ICMP;
104+
--
105+
2.47.2
106+

variants/ARDUINO_NANO33BLE/defines.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
-DFEATURE_STORAGE=1
3535
-D__FPU_PRESENT=1
3636
-D__MBED__=1
37-
-DMBED_BUILD_TIMESTAMP=1730202709.4767566
37+
-DMBED_BUILD_TIMESTAMP=1738678457.278008
3838
-D__MBED_CMSIS_RTOS_CM
3939
-DMBED_MPU_CUSTOM
4040
-DMBED_TICKLESS
0 Bytes
Binary file not shown.

variants/EDGE_CONTROL/conf/mbed_app.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"cellular.offload-dns-queries": true,
2020
"cellular.at-handler-buffer-size": 1024,
2121
"mbed-trace.enable": true,
22-
"target.mbed_app_start": "0x10000"
22+
"target.mbed_app_start": "0x10000",
23+
"lwip.raw-socket-enabled": true
2324
},
2425
"EDGE_CONTROL": {
2526
"sd.SPI_MOSI": "P0_20",

variants/EDGE_CONTROL/defines.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
-DFEATURE_STORAGE=1
3939
-D__FPU_PRESENT=1
4040
-D__MBED__=1
41-
-DMBED_BUILD_TIMESTAMP=1730202880.502858
41+
-DMBED_BUILD_TIMESTAMP=1738678638.4581091
4242
-D__MBED_CMSIS_RTOS_CM
4343
-DMBED_MPU_CUSTOM
4444
-DMBED_TICKLESS

variants/EDGE_CONTROL/libs/libmbed.a

5.65 KB
Binary file not shown.

variants/EDGE_CONTROL/mbed_config.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@
239239
#define MBED_CONF_LWIP_PPP_IPV6_ENABLED 0 // set by library:lwip
240240
#define MBED_CONF_LWIP_PPP_THREAD_STACKSIZE 768 // set by library:lwip
241241
#define MBED_CONF_LWIP_PRESENT 1 // set by library:lwip
242-
#define MBED_CONF_LWIP_RAW_SOCKET_ENABLED 0 // set by library:lwip
242+
#define MBED_CONF_LWIP_RAW_SOCKET_ENABLED 1 // set by application[*]
243243
#define MBED_CONF_LWIP_SOCKET_MAX 4 // set by library:lwip
244244
#define MBED_CONF_LWIP_TCPIP_THREAD_PRIORITY osPriorityNormal // set by library:lwip
245245
#define MBED_CONF_LWIP_TCPIP_THREAD_STACKSIZE 1200 // set by library:lwip

variants/GENERIC_STM32H747_M4/defines.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
-DFEATURE_BLE=1
4343
-D__FPU_PRESENT=1
4444
-D__MBED__=1
45-
-DMBED_BUILD_TIMESTAMP=1730202826.649384
45+
-DMBED_BUILD_TIMESTAMP=1738678579.8515525
4646
-D__MBED_CMSIS_RTOS_CM
4747
-DMBED_MPU_CUSTOM
4848
-DMBED_TICKLESS
-4 Bytes
Binary file not shown.

variants/GIGA/conf/mbed_app.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"target.mbed_app_start": "0x8040000",
1414
"nsapi.dns-response-wait-time": 5000,
1515
"nsapi.dns-total-attempts": 3,
16+
"lwip.raw-socket-enabled": true,
1617
"target.macros_add": [
1718
"METAL_INTERNAL",
1819
"VIRTIO_DRIVER_ONLY",

0 commit comments

Comments
 (0)