Skip to content

Ping command #1014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove ping logic from MbedSocketClass to use ICMPSocket ping
  • Loading branch information
pennam committed Feb 4, 2025
commit ad2b87daeaee1697f7863037242037912a1ee2ab
62 changes: 3 additions & 59 deletions libraries/SocketWrapper/src/SocketHelpers.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include "SocketHelpers.h"
#include "lwip/prot/icmp.h"
#include "lwip/inet_chksum.h"
#include "lwip/prot/ip4.h"
#include <ICMPSocket.h>

uint8_t* arduino::MbedSocketClass::macAddress(uint8_t* mac) {
Expand Down Expand Up @@ -141,70 +138,17 @@ void arduino::MbedSocketClass::setDNS(IPAddress dns_server1, IPAddress dns_serve
_dnsServer2 = SocketAddress(convertedDNSServer2);
}

int arduino::MbedSocketClass::ping(SocketAddress &socketAddress, uint8_t ttl)
int arduino::MbedSocketClass::ping(SocketAddress &socketAddress, uint8_t ttl, uint32_t timeout)
{
const uint32_t timeout = 5000;

/* ttl is not supported by mbed ICMPSocket. Default value used is 255 */
(void)ttl;
ICMPSocket s;
s.set_timeout(timeout);
s.open(getNetwork());

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(0xffff);

for (size_t i = 0; i < sizeof(request.data); i++) {
request.data[i] = i;
}

request.header.chksum = inet_chksum(&request, sizeof(request));
unsigned long recvTime = 0;
unsigned long sendTime = millis();

int res = s.sendto(socketAddress,&request, sizeof(request));
if(res <= 0){
return -1;
}

uint32_t startRec = millis();
do {
struct __attribute__((__packed__)) {
struct ip_hdr ipHeader;
struct icmp_echo_hdr header;
} response;

int rxSize = s.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)) {
recvTime = millis();
}
} while (recvTime == 0 && (millis() - startRec) < timeout);

int response = s.ping(socketAddress, timeout);
s.close();

if (recvTime == 0) {
return -1;
} else {
return (recvTime - sendTime);
}
return response;
}

arduino::IPAddress arduino::MbedSocketClass::ipAddressFromSocketAddress(SocketAddress socketAddress) {
Expand Down
2 changes: 1 addition & 1 deletion libraries/SocketWrapper/src/SocketHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class MbedSocketClass {

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

int ping(SocketAddress &socketAddress, uint8_t ttl);
int ping(SocketAddress &socketAddress, uint8_t ttl, uint32_t timeout = 5000);
static arduino::IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress);
static SocketAddress socketAddressFromIpAddress(arduino::IPAddress ip, uint16_t port);
static nsapi_error_t gethostbyname(NetworkInterface* interface, const char* aHostname, SocketAddress* socketAddress);
Expand Down
2 changes: 1 addition & 1 deletion mbed-os-to-arduino
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ generate_includes () {

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

echo -n " copying to destination... "
Expand Down
Loading