Skip to content

Commit ad2b87d

Browse files
committed
Remove ping logic from MbedSocketClass to use ICMPSocket ping
1 parent 1ddadbd commit ad2b87d

File tree

3 files changed

+5
-61
lines changed

3 files changed

+5
-61
lines changed

libraries/SocketWrapper/src/SocketHelpers.cpp

+3-59
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#include "SocketHelpers.h"
2-
#include "lwip/prot/icmp.h"
3-
#include "lwip/inet_chksum.h"
4-
#include "lwip/prot/ip4.h"
52
#include <ICMPSocket.h>
63

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

144-
int arduino::MbedSocketClass::ping(SocketAddress &socketAddress, uint8_t ttl)
141+
int arduino::MbedSocketClass::ping(SocketAddress &socketAddress, uint8_t ttl, uint32_t timeout)
145142
{
146-
const uint32_t timeout = 5000;
147-
148143
/* ttl is not supported by mbed ICMPSocket. Default value used is 255 */
149144
(void)ttl;
150145
ICMPSocket s;
151146
s.set_timeout(timeout);
152147
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-
148+
int response = s.ping(socketAddress, timeout);
201149
s.close();
202150

203-
if (recvTime == 0) {
204-
return -1;
205-
} else {
206-
return (recvTime - sendTime);
207-
}
151+
return response;
208152
}
209153

210154
arduino::IPAddress arduino::MbedSocketClass::ipAddressFromSocketAddress(SocketAddress socketAddress) {

libraries/SocketWrapper/src/SocketHelpers.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class MbedSocketClass {
185185

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

188-
int ping(SocketAddress &socketAddress, uint8_t ttl);
188+
int ping(SocketAddress &socketAddress, uint8_t ttl, uint32_t timeout = 5000);
189189
static arduino::IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress);
190190
static SocketAddress socketAddressFromIpAddress(arduino::IPAddress ip, uint16_t port);
191191
static nsapi_error_t gethostbyname(NetworkInterface* interface, const char* aHostname, SocketAddress* socketAddress);

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' | cat \
167+
| sed '/^-I./d' | sed '/lwipstack/d' | cat \
168168
> "$ARDUINOVARIANT"/includes.txt
169169

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

0 commit comments

Comments
 (0)