Skip to content

Commit 4cd3c25

Browse files
fabik111pennam
authored andcommitted
Add ping command
1 parent 6175e39 commit 4cd3c25

File tree

11 files changed

+223
-5
lines changed

11 files changed

+223
-5
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

+88
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();
@@ -74,6 +78,24 @@ arduino::IPAddress arduino::MbedSocketClass::dnsIP(int n) {
7478
return ipAddressFromSocketAddress(ip);
7579
}
7680

81+
int arduino::MbedSocketClass::ping(const char *hostname, uint8_t ttl)
82+
{
83+
SocketAddress socketAddress;
84+
gethostbyname(getNetwork(),hostname, &socketAddress);
85+
return ping(socketAddress, ttl);
86+
}
87+
88+
int arduino::MbedSocketClass::ping(const String &hostname, uint8_t ttl)
89+
{
90+
return ping(hostname.c_str(), ttl);
91+
}
92+
93+
int arduino::MbedSocketClass::ping(IPAddress host, uint8_t ttl)
94+
{
95+
SocketAddress socketAddress = socketAddressFromIpAddress(host, 0);
96+
return ping(socketAddress, ttl);
97+
}
98+
7799
void arduino::MbedSocketClass::config(arduino::IPAddress local_ip) {
78100
IPAddress dns = local_ip;
79101
dns[3] = 1;
@@ -119,6 +141,72 @@ void arduino::MbedSocketClass::setDNS(IPAddress dns_server1, IPAddress dns_serve
119141
_dnsServer2 = SocketAddress(convertedDNSServer2);
120142
}
121143

144+
int arduino::MbedSocketClass::ping(SocketAddress &socketAddress, uint8_t ttl)
145+
{
146+
const uint32_t timeout = 5000;
147+
148+
/* ttl is not supported by mbed ICMPSocket. Default value used is 255 */
149+
(void)ttl;
150+
ICMPSocket s;
151+
s.set_timeout(timeout);
152+
s.open(getNetwork());
153+
154+
struct __attribute__((__packed__)) {
155+
struct icmp_echo_hdr header;
156+
uint8_t data[32];
157+
} request;
158+
159+
ICMPH_TYPE_SET(&request.header, ICMP_ECHO);
160+
ICMPH_CODE_SET(&request.header, 0);
161+
request.header.chksum = 0;
162+
request.header.id = 0xAFAF;
163+
request.header.seqno = random(0xffff);
164+
165+
for (size_t i = 0; i < sizeof(request.data); i++) {
166+
request.data[i] = i;
167+
}
168+
169+
request.header.chksum = inet_chksum(&request, sizeof(request));
170+
unsigned long recvTime = 0;
171+
unsigned long sendTime = millis();
172+
173+
int res = s.sendto(socketAddress,&request, sizeof(request));
174+
if(res <= 0){
175+
return -1;
176+
}
177+
178+
uint32_t startRec = millis();
179+
do {
180+
struct __attribute__((__packed__)) {
181+
struct ip_hdr ipHeader;
182+
struct icmp_echo_hdr header;
183+
} response;
184+
185+
int rxSize = s.recvfrom(&socketAddress, &response, sizeof(response));
186+
if (rxSize < 0) {
187+
// time out
188+
break;
189+
}
190+
191+
if (rxSize < sizeof(response)) {
192+
// too short
193+
continue;
194+
}
195+
196+
if ((response.header.id == request.header.id) && (response.header.seqno == request.header.seqno)) {
197+
recvTime = millis();
198+
}
199+
} while (recvTime == 0 && (millis() - startRec) < timeout);
200+
201+
s.close();
202+
203+
if (recvTime == 0) {
204+
return -1;
205+
} else {
206+
return (recvTime - sendTime);
207+
}
208+
}
209+
122210
arduino::IPAddress arduino::MbedSocketClass::ipAddressFromSocketAddress(SocketAddress socketAddress) {
123211
nsapi_addr_t address = socketAddress.get_addr();
124212
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);
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 ""

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

+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/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",

variants/NICLA_VISION/conf/mbed_app.json

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

variants/OPTA/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",

variants/PORTENTA_H7_M7/conf/mbed_app.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"target.mbed_app_start": "0x8040000",
1818
"nsapi.dns-response-wait-time": 5000,
1919
"nsapi.dns-total-attempts": 3,
20+
"lwip.raw-socket-enabled": true,
2021
"target.macros_add": [
2122
"BT_UART_NO_3M_SUPPORT",
2223
"USB_DYNAMIC_CONFIGURATION",

0 commit comments

Comments
 (0)