33#include "WiFiGeneric.h"
40 : m_queue(xQueueCreate(1, sizeof(uint8_t))), m_waitingID(rand() & 0xFFFF), m_waitingSeq(-1)
47 vQueueDelete(m_queue);
53int ICMP::ping(
char const * host)
55 IPAddress hostIP((uint32_t)0);
56 if (!WiFiGenericClass::hostByName(host, hostIP))
63int ICMP::ping(IPAddress
const &dest)
65 static int32_t
const TIMEOUT = 1000;
66 static int32_t
const TIMEOUT_RESULT = -1;
70 int result = TIMEOUT_RESULT;
76 pbuf * hdrbuf = pbuf_alloc(PBUF_IP,
sizeof(icmp_echo_hdr), PBUF_RAM);
77 icmp_echo_hdr * hdr = (icmp_echo_hdr *) hdrbuf->payload;
78 hdr->type = ICMP_ECHO;
81 hdr->id = htons(m_waitingID);
82 hdr->seqno = htons(m_waitingSeq);
83 hdr->chksum = inet_chksum((uint16_t *) hdr,
sizeof(icmp_echo_hdr));
86 raw_pcb * pcb = raw_new(IP_PROTO_ICMP);
87 raw_recv(pcb, ICMP::raw_recv_fn,
this);
88 raw_bind(pcb, IP_ADDR_ANY);
91 addr.type = IPADDR_TYPE_V4;
92 addr.u_addr.ip4.addr = dest;
93 raw_sendto(pcb, hdrbuf, &addr);
97 int32_t t1 = micros();
99 if (xQueueReceive(m_queue, &c, pdMS_TO_TICKS(TIMEOUT)))
100 result = micros() - t1;
107uint8_t ICMP::raw_recv_fn(
void * arg, raw_pcb * pcb, pbuf * p,
const ip_addr_t * addr)
109 ICMP * this_ = (ICMP *)arg;
111 ip_hdr * iphdr = (ip_hdr *)p->payload;
113 int ttl = IPH_TTL(iphdr);
115 if (p->tot_len >=
sizeof(ip_hdr) +
sizeof(icmp_echo_hdr) && pbuf_header(p, -(s16_t)
sizeof(ip_hdr)) == 0) {
116 icmp_echo_hdr * hdr = (icmp_echo_hdr *) p->payload;
117 if (ntohs(hdr->id) == this_->m_waitingID && ntohs(hdr->seqno) == this_->m_waitingSeq) {
118 this_->m_receivedBytes = p->tot_len;
119 this_->m_receivedTTL = ttl;
121 xQueueSendToBack(this_->m_queue, &c, portMAX_DELAY);
This file contains ICMP (ping) class.