29 #include "WiFiGeneric.h" 36 : m_queue(xQueueCreate(1, sizeof(uint8_t))), m_waitingID(rand() & 0xFFFF), m_waitingSeq(-1)
43 vQueueDelete(m_queue);
49 int ICMP::ping(
char const * host)
51 IPAddress hostIP((uint32_t)0);
52 if (!WiFiGenericClass::hostByName(host, hostIP))
59 int ICMP::ping(IPAddress
const &dest)
61 static int32_t
const TIMEOUT = 1000;
62 static int32_t
const TIMEOUT_RESULT = -1;
66 int result = TIMEOUT_RESULT;
72 pbuf * hdrbuf = pbuf_alloc(PBUF_IP,
sizeof(icmp_echo_hdr), PBUF_RAM);
73 icmp_echo_hdr * hdr = (icmp_echo_hdr *) hdrbuf->payload;
74 hdr->type = ICMP_ECHO;
77 hdr->id = htons(m_waitingID);
78 hdr->seqno = htons(m_waitingSeq);
79 hdr->chksum = inet_chksum((uint16_t *) hdr,
sizeof(icmp_echo_hdr));
82 raw_pcb * pcb = raw_new(IP_PROTO_ICMP);
83 raw_recv(pcb, ICMP::raw_recv_fn,
this);
84 raw_bind(pcb, IP_ADDR_ANY);
87 addr.type = IPADDR_TYPE_V4;
88 addr.u_addr.ip4.addr = dest;
89 raw_sendto(pcb, hdrbuf, &addr);
93 int32_t t1 = micros();
95 if (xQueueReceive(m_queue, &c, pdMS_TO_TICKS(TIMEOUT)))
96 result = micros() - t1;
103 uint8_t ICMP::raw_recv_fn(
void * arg, raw_pcb * pcb, pbuf * p,
const ip_addr_t * addr)
105 ICMP * this_ = (ICMP *)arg;
107 ip_hdr * iphdr = (ip_hdr *)p->payload;
109 int ttl = IPH_TTL(iphdr);
111 if (p->tot_len >=
sizeof(ip_hdr) +
sizeof(icmp_echo_hdr) && pbuf_header(p, -(s16_t)
sizeof(ip_hdr)) == 0) {
112 icmp_echo_hdr * hdr = (icmp_echo_hdr *) p->payload;
113 if (ntohs(hdr->id) == this_->m_waitingID && ntohs(hdr->seqno) == this_->m_waitingSeq) {
114 this_->m_receivedBytes = p->tot_len;
115 this_->m_receivedTTL = ttl;
117 xQueueSendToBack(this_->m_queue, &c, portMAX_DELAY);
132 #endif // #ifdef ARDUINO This file contains ICMP (ping) class.