From 116b61fca8e8a68d8a6bea78a15493db4d6cd5d3 Mon Sep 17 00:00:00 2001 From: Andy Powell Date: Thu, 22 Sep 2022 13:17:31 +0100 Subject: [PATCH 1/3] Add error count and change log_i to log_d Add interface to getting the error count after the ping. Change logging output from info to debug. --- ESP32Ping.cpp | 18 ++++++++++++------ ESP32Ping.h | 1 + ping.cpp | 18 +++++++++--------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/ESP32Ping.cpp b/ESP32Ping.cpp index 022fd2e..9d77acc 100644 --- a/ESP32Ping.cpp +++ b/ESP32Ping.cpp @@ -44,14 +44,14 @@ bool PingClass::ping(IPAddress dest, byte count) { _options.recv_function = reinterpret_cast(&PingClass::_ping_recv_cb); _options.sent_function = NULL; //reinterpret_cast(&_ping_sent_cb); - + // Suspend till the process end esp_yield(); // ????????? Where should this be placed? - + // Let's go! ping_start(&_options); // Here we do all the work - // Returns true if at least 1 ping had a pong response + // Returns true if at least 1 ping had a pong response return (_success > 0); //_success variable is changed by the callback function } @@ -68,6 +68,12 @@ float PingClass::averageTime() { return _avg_time; } +unsigned int PingClass::errors() { + return _errors; +} + + + void PingClass::_ping_recv_cb(void *opt, void *resp) { // Cast the parameters to get some usable info ping_resp *ping_resp = reinterpret_cast(resp); @@ -77,7 +83,7 @@ void PingClass::_ping_recv_cb(void *opt, void *resp) { _errors = ping_resp->timeout_count; _success = ping_resp->total_count - ping_resp->timeout_count; _avg_time = ping_resp->resp_time; - + // Some debug info DEBUG_PING( @@ -97,10 +103,10 @@ void PingClass::_ping_recv_cb(void *opt, void *resp) { // Is it time to end? DEBUG_PING("Avg resp time %f ms\n", _avg_time); - + // Done, return to main function esp_schedule(); - + // just a check ... if (_success + _errors != _expected_count) { DEBUG_PING("Something went wrong: _success=%d and _errors=%d do not sum up to _expected_count=%d\n",_success, _errors, _expected_count ); diff --git a/ESP32Ping.h b/ESP32Ping.h index 1c18c92..2090c6c 100644 --- a/ESP32Ping.h +++ b/ESP32Ping.h @@ -53,6 +53,7 @@ class PingClass { bool ping(const char *host, byte count = 5); float averageTime(); + unsigned int errors(); protected: static void _ping_sent_cb(void *opt, void *pdata); diff --git a/ping.cpp b/ping.cpp index 91df2bc..c64267f 100644 --- a/ping.cpp +++ b/ping.cpp @@ -137,7 +137,7 @@ static err_t ping_send(int s, ip4_addr_t *addr, int size) { iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size); if (!iecho) { - mem_free(iecho); + mem_free(iecho); return ERR_MEM; } @@ -321,10 +321,10 @@ bool ping_start(IPAddress adr, int count=0, int interval=0, int size=0, int time char ipa[16]; strcpy(ipa, inet_ntoa(ping_target)); - log_i("PING %s: %d data bytes\r\n", ipa, size); + log_d("PING %s: %d data bytes\r\n", ipa, size); ping_seq_num = 0; - + unsigned long ping_started_time = millis(); while ((ping_seq_num < count) && (!stopped)) { if (ping_send(s, &ping_target, size) == ERR_OK) { @@ -337,16 +337,16 @@ bool ping_start(IPAddress adr, int count=0, int interval=0, int size=0, int time closesocket(s); - log_i("%d packets transmitted, %d packets received, %.1f%% packet loss\r\n", + log_d("%d packets transmitted, %d packets received, %.1f%% packet loss\r\n", transmitted, received, ((((float)transmitted - (float)received) / (float)transmitted) * 100.0) ); - - + + if (ping_o) { ping_resp pingresp; - log_i("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\r\n", min_time, mean_time, max_time, sqrt(var_time / received)); + log_d("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\r\n", min_time, mean_time, max_time, sqrt(var_time / received)); pingresp.total_count = count; //Number of pings pingresp.resp_time = mean_time; //Average time for the pings pingresp.seqno = 0; //not relevant @@ -358,8 +358,8 @@ bool ping_start(IPAddress adr, int count=0, int interval=0, int size=0, int time // Call the callback function ping_o->recv_function(ping_o, &pingresp); } - - // Return true if at least one ping had a successfull "pong" + + // Return true if at least one ping had a successfull "pong" return (received > 0); } From 41278999a051c6f7d31c6d129fd0d541b1d8af34 Mon Sep 17 00:00:00 2001 From: Andy Powell Date: Thu, 22 Sep 2022 13:19:57 +0100 Subject: [PATCH 2/3] blank lines removal --- ESP32Ping.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ESP32Ping.cpp b/ESP32Ping.cpp index 9d77acc..6de59c0 100644 --- a/ESP32Ping.cpp +++ b/ESP32Ping.cpp @@ -18,7 +18,6 @@ */ #include "ESP32Ping.h" - extern "C" void esp_schedule(void) {}; extern "C" void esp_yield(void) {}; @@ -72,8 +71,6 @@ unsigned int PingClass::errors() { return _errors; } - - void PingClass::_ping_recv_cb(void *opt, void *resp) { // Cast the parameters to get some usable info ping_resp *ping_resp = reinterpret_cast(resp); @@ -84,7 +81,6 @@ void PingClass::_ping_recv_cb(void *opt, void *resp) { _success = ping_resp->total_count - ping_resp->timeout_count; _avg_time = ping_resp->resp_time; - // Some debug info DEBUG_PING( "DEBUG: ping reply\n" From e520aeeb09548e369098c183154774cacf8f22c6 Mon Sep 17 00:00:00 2001 From: Andy Powell Date: Thu, 22 Sep 2022 16:51:58 +0100 Subject: [PATCH 3/3] Interval and timeout changes --- ping.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/ping.cpp b/ping.cpp index c64267f..b25c341 100644 --- a/ping.cpp +++ b/ping.cpp @@ -244,7 +244,7 @@ static void stop_action(int i) { * Operation functions * */ -void ping(const char *name, int count, int interval, int size, int timeout) { +void ping(const char *name, int count, int interval=0, int size=0, int timeout=0) { // Resolve name hostent * target = gethostbyname(name); IPAddress adr = *target->h_addr_list[0]; @@ -297,8 +297,15 @@ bool ping_start(IPAddress adr, int count=0, int interval=0, int size=0, int time struct timeval tout; // Timeout - tout.tv_sec = timeout; - tout.tv_usec = 0; + #ifdef TIME_UNIT_MILLIS + tout.tv_sec = 0; + tout.tv_usec = timeout*1000L; + #else + tout.tv_sec = timeout; + tout.tv_usec = 0; + #endif + + if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tout, sizeof(tout)) < 0) { closesocket(s); @@ -331,7 +338,11 @@ bool ping_start(IPAddress adr, int count=0, int interval=0, int size=0, int time ping_recv(s); } if(ping_seq_num < count){ - delay( interval*1000L); +#ifdef TIME_UNIT_MILLIS + delay(interval); +#else + delay(interval*1000L); +#endif } }