Skip to content

Commit 34471df

Browse files
committed
add ping command
1 parent f7fee48 commit 34471df

File tree

13 files changed

+224
-7
lines changed

13 files changed

+224
-7
lines changed

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

+85
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#include "SocketHelpers.h"
2+
#include "lwip/prot/icmp.h"
3+
#include "lwip/inet_chksum.h"
4+
#include "lwip/prot/ip4.h"
5+
#include <ICMPSocket.h>
26

37
uint8_t* arduino::MbedSocketClass::macAddress(uint8_t* mac) {
48
const char* mac_str = getNetwork()->get_mac_address();
@@ -65,6 +69,24 @@ arduino::IPAddress arduino::MbedSocketClass::dnsIP(int n) {
6569
return ipAddressFromSocketAddress(ip);
6670
}
6771

72+
int arduino::MbedSocketClass::ping(const char *hostname, uint8_t ttl)
73+
{
74+
SocketAddress socketAddress;
75+
gethostbyname(getNetwork(),hostname, &socketAddress);
76+
return ping(socketAddress, ttl);
77+
}
78+
79+
int arduino::MbedSocketClass::ping(const String &hostname, uint8_t ttl)
80+
{
81+
return ping(hostname.c_str(), ttl);
82+
}
83+
84+
int arduino::MbedSocketClass::ping(IPAddress host, uint8_t ttl)
85+
{
86+
SocketAddress socketAddress = socketAddressFromIpAddress(host, 0);
87+
return ping(socketAddress, ttl);
88+
}
89+
6890
void arduino::MbedSocketClass::config(arduino::IPAddress local_ip) {
6991
IPAddress dns = local_ip;
7092
dns[3] = 1;
@@ -110,6 +132,69 @@ void arduino::MbedSocketClass::setDNS(IPAddress dns_server1, IPAddress dns_serve
110132
_dnsServer2 = SocketAddress(convertedDNSServer2);
111133
}
112134

135+
int arduino::MbedSocketClass::ping(SocketAddress &socketAddress, uint8_t ttl)
136+
{
137+
uint32_t timeout = 5000;
138+
ICMPSocket s;
139+
s.set_timeout(timeout);
140+
s.open(getNetwork());
141+
142+
struct __attribute__((__packed__)) {
143+
struct icmp_echo_hdr header;
144+
uint8_t data[32];
145+
} request;
146+
147+
ICMPH_TYPE_SET(&request.header, ICMP_ECHO);
148+
ICMPH_CODE_SET(&request.header, 0);
149+
request.header.chksum = 0;
150+
request.header.id = 0xAFAF;
151+
request.header.seqno = random(0xffff);
152+
153+
for (size_t i = 0; i < sizeof(request.data); i++) {
154+
request.data[i] = i;
155+
}
156+
157+
request.header.chksum = inet_chksum(&request, sizeof(request));
158+
unsigned long recvTime = 0;
159+
unsigned long sendTime = millis();
160+
161+
int res = s.sendto(socketAddress,&request, sizeof(request));
162+
if(res <= 0){
163+
return -1;
164+
}
165+
166+
uint32_t startRec = millis();
167+
do {
168+
struct __attribute__((__packed__)) {
169+
struct ip_hdr ipHeader;
170+
struct icmp_echo_hdr header;
171+
} response;
172+
173+
int rxSize = s.recvfrom(&socketAddress, &response, sizeof(response));
174+
if (rxSize < 0) {
175+
// time out
176+
break;
177+
}
178+
179+
if (rxSize < sizeof(response)) {
180+
// too short
181+
continue;
182+
}
183+
184+
if ((response.header.id == request.header.id) && (response.header.seqno == request.header.seqno)) {
185+
recvTime = millis();
186+
}
187+
} while (recvTime == 0 && (millis() - startRec) < timeout);
188+
189+
s.close();
190+
191+
if (recvTime == 0) {
192+
return -1;
193+
} else {
194+
return (recvTime - sendTime);
195+
}
196+
}
197+
113198
arduino::IPAddress arduino::MbedSocketClass::ipAddressFromSocketAddress(SocketAddress socketAddress) {
114199
nsapi_addr_t address = socketAddress.get_addr();
115200
return IPAddress(address.bytes[0], address.bytes[1], address.bytes[2], address.bytes[3]);

libraries/SocketWrapper/src/SocketHelpers.h

+10
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ class MbedSocketClass {
108108

109109
virtual NetworkInterface* getNetwork() = 0;
110110

111+
/*
112+
* Ping the specified target.
113+
*
114+
* return: RTT in milliseconds or -1 on error
115+
*/
116+
int ping(const char* hostname, uint8_t ttl = 128);
117+
int ping(const String &hostname, uint8_t ttl = 128);
118+
int ping(IPAddress host, uint8_t ttl = 128);
119+
111120
/*
112121
* Download a file from an HTTP endpoint and save it in the provided `target` location on the fs
113122
* The parameter cbk can be used to perform actions on the buffer before saving on the fs
@@ -152,6 +161,7 @@ class MbedSocketClass {
152161

153162
void body_callback(const char* data, uint32_t data_len);
154163

164+
int ping(SocketAddress &socketAddress, uint8_t ttl);
155165
static arduino::IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress);
156166
static SocketAddress socketAddressFromIpAddress(arduino::IPAddress ip, uint16_t port);
157167
static nsapi_error_t gethostbyname(NetworkInterface* interface, const char* aHostname, SocketAddress* socketAddress);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
Serial.println("Make sure your WiFi firmware version is at least 0.5.0");
76+
}
77+
78+
// Ping Host
79+
const char* remote_host = "www.google.com";
80+
Serial.print("Trying to ping host: ");
81+
Serial.println(remote_host);
82+
83+
int res1 = WiFi.ping(remote_host);
84+
85+
if (res1 > 0) {
86+
Serial.print("Ping average response time: ");
87+
Serial.print(res1);
88+
Serial.println(" ms");
89+
}
90+
else {
91+
Serial.println("Timeout on host!");
92+
Serial.println("Make sure your WiFi firmware version is at least 0.5.0");
93+
}
94+
95+
Serial.println();
96+
delay(5000);
97+
}
98+
99+
/* -------------------------------------------------------------------------- */
100+
void printWifiStatus() {
101+
/* -------------------------------------------------------------------------- */
102+
// print the SSID of the network you're attached to:
103+
Serial.print("SSID: ");
104+
Serial.println(WiFi.SSID());
105+
106+
// print your board's IP address:
107+
IPAddress ip = WiFi.localIP();
108+
Serial.print("IP Address: ");
109+
Serial.println(ip);
110+
111+
// print the received signal strength:
112+
long rssi = WiFi.RSSI();
113+
Serial.print("signal strength (RSSI):");
114+
Serial.print(rssi);
115+
Serial.println(" dBm");
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

mbed-os-to-arduino

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ generate_includes () {
164164

165165
find ./BUILD/"$BOARDNAME"/GCC_ARM${PROFILE}/ -type f -name '.include*' -print0 | xargs -0 cat \
166166
| tr ' ' '\n' | tr -d '"' | sed -e 's#-I./mbed-os#-iwithprefixbefore/mbed#g' \
167-
| sed '/^-I./d' | sed '/lwipstack/d' | cat \
167+
| sed '/^-I./d' | cat \
168168
> "$ARDUINOVARIANT"/includes.txt
169169

170170
echo -n " copying to destination... "

variants/EDGE_CONTROL/conf/mbed_app.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"macros": [
33
"MBED_HEAP_STATS_ENABLED=1",
44
"MBED_STACK_STATS_ENABLED=1",
@@ -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/GENERIC_STM32H747_M4/conf/mbed_app.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"rtos.main-thread-stack-size": 32768,
1212
"cordio.max-connections": 5,
1313
"target.lse_available": 0,
14+
"lwip.raw-socket-enabled": true,
1415
"target.device_has_remove": [
1516
"USBDEVICE",
1617
"EMAC"

variants/GIGA/conf/mbed_app.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"rtos.main-thread-stack-size": 32768,
1212
"cordio.max-connections": 5,
1313
"target.mbed_app_start": "0x8040000",
14+
"lwip.raw-socket-enabled": true,
1415
"target.macros_add": [
1516
"METAL_INTERNAL",
1617
"VIRTIO_DRIVER_ONLY",

variants/NANO_RP2040_CONNECT/conf/mbed_app.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"platform.callback-nontrivial": true,
1010
"platform.all-stats-enabled": true,
1111
"platform.memory-tracing-enabled": true,
12-
"rtos.main-thread-stack-size": 32768
12+
"rtos.main-thread-stack-size": 32768,
13+
"lwip.raw-socket-enabled": true
1314
}
1415
}
1516
}

variants/NICLA_VISION/conf/mbed_app.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"rtos.main-thread-stack-size": 32768,
1313
"cordio.max-connections": 5,
1414
"target.mbed_app_start": "0x8040000",
15+
"lwip.raw-socket-enabled": true,
1516
"target.macros_add": [
1617
"METAL_INTERNAL",
1718
"VIRTIO_DRIVER_ONLY",

variants/OPTA/conf/mbed_app.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"rtos.main-thread-stack-size": 32768,
1212
"cordio.max-connections": 5,
1313
"target.mbed_app_start": "0x8040000",
14+
"lwip.raw-socket-enabled": true,
1415
"target.macros_add": [
1516
"METAL_INTERNAL",
1617
"VIRTIO_DRIVER_ONLY",

variants/PORTENTA_H7_M7/conf/mbed_app.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"cellular.at-handler-buffer-size": 512,
1616
"mbed-trace.enable": true,
1717
"target.mbed_app_start": "0x8040000",
18+
"lwip.raw-socket-enabled": true,
1819
"target.macros_add": [
1920
"BT_UART_NO_3M_SUPPORT",
2021
"USB_DYNAMIC_CONFIGURATION",

0 commit comments

Comments
 (0)