Skip to content

Commit 489d637

Browse files
fabik111pennam
authored andcommitted
lwIpWrapper: CNetIf refactor ping functions
1 parent f537405 commit 489d637

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

libraries/Ethernet/src/Ethernet.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -226,24 +226,24 @@ IPAddress CEthernet::dnsServerIP() {
226226
/* -------------------------------------------------------------------------- */
227227
int CEthernet::ping(IPAddress ip, uint8_t ttl) {
228228
/* -------------------------------------------------------------------------- */
229-
return CLwipIf::getInstance().ping(ip, ttl);
229+
return CLwipIf::getInstance().ping(ip, ttl);
230230
}
231231

232232
/* -------------------------------------------------------------------------- */
233233
int CEthernet::ping(const String &hostname, uint8_t ttl)
234234
/* -------------------------------------------------------------------------- */
235235
{
236-
return ping(hostname.c_str(), ttl);
236+
return ping(hostname.c_str(), ttl);
237237
}
238238

239239
/* -------------------------------------------------------------------------- */
240240
int CEthernet::ping(const char* host, uint8_t ttl) {
241241
/* -------------------------------------------------------------------------- */
242-
IPAddress ip;
243-
if(CLwipIf::getInstance().getHostByName(host,ip)) {
244-
return CLwipIf::getInstance().ping(ip, ttl);
245-
}
246-
return -1;
242+
IPAddress ip;
243+
if(CLwipIf::getInstance().getHostByName(host,ip)) {
244+
return CLwipIf::getInstance().ping(ip, ttl);
245+
}
246+
return -1;
247247
}
248248

249249
CEthernet Ethernet;

libraries/WiFi/src/WiFi.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ int CWifi::ping(IPAddress ip, uint8_t ttl) {
335335
int CWifi::ping(const String &hostname, uint8_t ttl)
336336
/* -------------------------------------------------------------------------- */
337337
{
338-
return ping(hostname.c_str(), ttl);
338+
return ping(hostname.c_str(), ttl);
339339
}
340340

341341
/* -------------------------------------------------------------------------- */

libraries/lwIpWrapper/src/CNetIf.cpp

+21-14
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,24 @@ static u8_t icmp_receive_callback(void *arg, struct raw_pcb *pcb, struct pbuf *p
2626
LWIP_ASSERT("p != NULL", p != NULL);
2727

2828
recv_callback_data* request = (recv_callback_data*)arg;
29+
if ((p->tot_len < (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) ||
30+
pbuf_remove_header(p, PBUF_IP_HLEN) != 0) {
31+
return 0; /* don't consume the packet */
32+
}
2933

30-
if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
31-
pbuf_remove_header(p, PBUF_IP_HLEN) == 0) {
32-
iecho = (struct icmp_echo_hdr *)p->payload;
34+
iecho = (struct icmp_echo_hdr *)p->payload;
3335

34-
if ((iecho->id == 0xAFAF) && (iecho->seqno == lwip_htons(request->seqNum))) {
35-
/* do some ping result processing */
36-
request->endMillis = millis();
37-
pbuf_free(p);
38-
return 1; /* eat the packet */
39-
}
36+
if(iecho->id != 0xAFAF || iecho->seqno != lwip_htons(request->seqNum)) {
4037
/* not eaten, restore original packet */
4138
pbuf_add_header(p, PBUF_IP_HLEN);
39+
return 0;
4240
}
4341

44-
return 0; /* don't eat the packet */
42+
/* do some ping result processing */
43+
request->endMillis = millis();
44+
pbuf_free(p);
45+
return 1; /* consume the packet */
46+
4547
}
4648

4749
ip_addr_t* u8_to_ip_addr(uint8_t* ipu8, ip_addr_t* ipaddr)
@@ -202,12 +204,17 @@ int CLwipIf::ping(IPAddress ip, uint8_t ttl)
202204
}
203205
pbuf_free(p);
204206

205-
bool timeout = false;
206-
while (!requestCbkData.endMillis && !timeout) {
207-
timeout = (millis() - requestCbkData.startMillis) > 5000;
207+
CLwipIf::getInstance().startSyncRequest();
208+
209+
while (!requestCbkData.endMillis && (millis() - requestCbkData.startMillis) <= 5000) {
210+
CLwipIf::getInstance().lwip_task();
208211
}
209212

210-
if (timeout) {
213+
CLwipIf::getInstance().restartAsyncRequest();
214+
215+
raw_remove(s);
216+
217+
if (!requestCbkData.endMillis) {
211218
return -1;
212219
}
213220

0 commit comments

Comments
 (0)