-
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1014 from fabik111/add-ping-command
Ping command
- Loading branch information
Showing
41 changed files
with
303 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
Web ICMP Ping | ||
This sketch pings a device based on the IP address or the hostname | ||
using the WiFi module. | ||
This example is written for a network using WPA encryption. For | ||
WEP or WPA, change the WiFi.begin() call accordingly. | ||
created 14 February 2024 | ||
by paulvha | ||
modified 8 Jenuary 2025 | ||
by fabik111 | ||
*/ | ||
|
||
#include <WiFi.h> | ||
#include "arduino_secrets.h" | ||
|
||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h | ||
char ssid[] = SECRET_SSID; // your network SSID (name) | ||
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) | ||
|
||
int status = WL_IDLE_STATUS; | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
void setup() { | ||
/* -------------------------------------------------------------------------- */ | ||
//Initialize serial and wait for port to open: | ||
Serial.begin(9600); | ||
while (!Serial) { | ||
; // wait for serial port to connect. Needed for native USB port only | ||
} | ||
|
||
// check for the WiFi module: | ||
if (WiFi.status() == WL_NO_MODULE) { | ||
Serial.println("Communication with WiFi module failed."); | ||
// don't continue | ||
while (true); | ||
} | ||
|
||
// attempt to connect to WiFi network: | ||
while (status != WL_CONNECTED) { | ||
Serial.print("Attempting to connect to SSID: "); | ||
Serial.println(ssid); | ||
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: | ||
status = WiFi.begin(ssid, pass); | ||
|
||
// wait 3 seconds for connection: | ||
delay(3000); | ||
} | ||
|
||
printWifiStatus(); | ||
} | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
void loop() { | ||
/* -------------------------------------------------------------------------- */ | ||
|
||
// Ping IP | ||
const IPAddress remote_ip(140,82,121,4); | ||
Serial.print("Trying to ping github.com on IP: "); | ||
Serial.println(remote_ip); | ||
|
||
// using default ping count of 1 | ||
int res = WiFi.ping(remote_ip); | ||
|
||
if (res > 0) { | ||
Serial.print("Ping response time: "); | ||
Serial.print(res); | ||
Serial.println(" ms"); | ||
} | ||
else { | ||
Serial.println("Timeout on IP!"); | ||
} | ||
|
||
// Ping Host | ||
const char* remote_host = "www.google.com"; | ||
Serial.print("Trying to ping host: "); | ||
Serial.println(remote_host); | ||
|
||
int res1 = WiFi.ping(remote_host); | ||
|
||
if (res1 > 0) { | ||
Serial.print("Ping response time: "); | ||
Serial.print(res1); | ||
Serial.println(" ms"); | ||
} | ||
else { | ||
Serial.println("Timeout on host!"); | ||
} | ||
|
||
Serial.println(); | ||
delay(5000); | ||
} | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
void printWifiStatus() { | ||
/* -------------------------------------------------------------------------- */ | ||
// print the SSID of the network you're attached to: | ||
Serial.print("SSID: "); | ||
Serial.println(WiFi.SSID()); | ||
|
||
// print your board's IP address: | ||
IPAddress ip = WiFi.localIP(); | ||
Serial.print("IP Address: "); | ||
Serial.println(ip); | ||
|
||
// print the received signal strength: | ||
long rssi = WiFi.RSSI(); | ||
Serial.print("signal strength (RSSI):"); | ||
Serial.print(rssi); | ||
Serial.println(" dBm"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#define SECRET_SSID "" | ||
#define SECRET_PASS "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
From 933694e0f35451d21eed77a93fa346570de20878 Mon Sep 17 00:00:00 2001 | ||
From: pennam <[email protected]> | ||
Date: Tue, 4 Feb 2025 14:31:59 +0100 | ||
Subject: [PATCH] ICMPSocket: add ping | ||
|
||
--- | ||
.../netsocket/include/netsocket/ICMPSocket.h | 4 ++ | ||
connectivity/netsocket/source/ICMPSocket.cpp | 61 +++++++++++++++++++ | ||
2 files changed, 65 insertions(+) | ||
|
||
diff --git a/connectivity/netsocket/include/netsocket/ICMPSocket.h b/connectivity/netsocket/include/netsocket/ICMPSocket.h | ||
index 1837bc8e09..5e1ee8fb03 100644 | ||
--- a/connectivity/netsocket/include/netsocket/ICMPSocket.h | ||
+++ b/connectivity/netsocket/include/netsocket/ICMPSocket.h | ||
@@ -37,6 +37,10 @@ public: | ||
*/ | ||
ICMPSocket(); | ||
|
||
+#if MBED_CONF_LWIP_RAW_SOCKET_ENABLED | ||
+ int ping(SocketAddress &socketAddress, uint32_t timeout); | ||
+#endif | ||
+ | ||
#if !defined(DOXYGEN_ONLY) | ||
|
||
protected: | ||
diff --git a/connectivity/netsocket/source/ICMPSocket.cpp b/connectivity/netsocket/source/ICMPSocket.cpp | ||
index f6c9b98de1..d8ea954835 100644 | ||
--- a/connectivity/netsocket/source/ICMPSocket.cpp | ||
+++ b/connectivity/netsocket/source/ICMPSocket.cpp | ||
@@ -16,12 +16,73 @@ | ||
*/ | ||
|
||
#include "ICMPSocket.h" | ||
+#if MBED_CONF_LWIP_RAW_SOCKET_ENABLED | ||
+#include "drivers/Timer.h" | ||
+#include "lwip/prot/icmp.h" | ||
+#include "lwip/inet_chksum.h" | ||
+#include "lwip/prot/ip4.h" | ||
+#endif | ||
|
||
ICMPSocket::ICMPSocket() | ||
{ | ||
_socket_stats.stats_update_proto(this, NSAPI_ICMP); | ||
} | ||
|
||
+#if MBED_CONF_LWIP_RAW_SOCKET_ENABLED | ||
+int ICMPSocket::ping(SocketAddress &socketAddress, uint32_t timeout) | ||
+{ | ||
+ struct __attribute__((__packed__)) { | ||
+ struct icmp_echo_hdr header; | ||
+ uint8_t data[32]; | ||
+ } request; | ||
+ | ||
+ ICMPH_TYPE_SET(&request.header, ICMP_ECHO); | ||
+ ICMPH_CODE_SET(&request.header, 0); | ||
+ request.header.chksum = 0; | ||
+ request.header.id = 0xAFAF; | ||
+ request.header.seqno = random(); | ||
+ | ||
+ for (size_t i = 0; i < sizeof(request.data); i++) { | ||
+ request.data[i] = i; | ||
+ } | ||
+ | ||
+ request.header.chksum = inet_chksum(&request, sizeof(request)); | ||
+ | ||
+ int res = sendto(socketAddress, &request, sizeof(request)); | ||
+ if (res <= 0){ | ||
+ return -1; | ||
+ } | ||
+ | ||
+ mbed::Timer timer; | ||
+ timer.start(); | ||
+ int elapsed = -1; | ||
+ do { | ||
+ struct __attribute__((__packed__)) { | ||
+ struct ip_hdr ipHeader; | ||
+ struct icmp_echo_hdr header; | ||
+ } response; | ||
+ | ||
+ int rxSize = recvfrom(&socketAddress, &response, sizeof(response)); | ||
+ if (rxSize < 0) { | ||
+ // time out | ||
+ break; | ||
+ } | ||
+ | ||
+ if (rxSize < sizeof(response)) { | ||
+ // too short | ||
+ continue; | ||
+ } | ||
+ | ||
+ if ((response.header.id == request.header.id) && (response.header.seqno == request.header.seqno)) { | ||
+ elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(timer.elapsed_time()).count(); | ||
+ timer.stop(); | ||
+ } | ||
+ } while (elapsed == -1 && std::chrono::duration_cast<std::chrono::milliseconds>(timer.elapsed_time()).count() < timeout); | ||
+ | ||
+ return elapsed; | ||
+} | ||
+#endif | ||
+ | ||
nsapi_protocol_t ICMPSocket::get_proto() | ||
{ | ||
return NSAPI_ICMP; | ||
-- | ||
2.47.2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.