Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error count and change logging #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions ESP32Ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
#include "ESP32Ping.h"


extern "C" void esp_schedule(void) {};
extern "C" void esp_yield(void) {};

Expand All @@ -44,14 +43,14 @@ bool PingClass::ping(IPAddress dest, byte count) {
_options.recv_function = reinterpret_cast<ping_recv_function>(&PingClass::_ping_recv_cb);
_options.sent_function = NULL; //reinterpret_cast<ping_sent_function>(&_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
}

Expand All @@ -68,6 +67,10 @@ 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<struct ping_resp *>(resp);
Expand All @@ -77,7 +80,6 @@ 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(
Expand All @@ -97,10 +99,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 );
Expand Down
1 change: 1 addition & 0 deletions ESP32Ping.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 24 additions & 13 deletions ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand All @@ -321,32 +328,36 @@ 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) {
ping_recv(s);
}
if(ping_seq_num < count){
delay( interval*1000L);
#ifdef TIME_UNIT_MILLIS
delay(interval);
#else
delay(interval*1000L);
#endif
}
}

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
Expand All @@ -358,8 +369,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);
}

Expand Down