You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

2 years ago
/**
* Ping library, used to test connectivity
*/
#ifndef _PING_H
#define _PING_H
#include "lwip/ip.h"
#include "sdkconfig.h"
typedef void (*ping_success_print_cb_t)(int bytes, const char *ip, int seq, int elapsed_ms);
typedef void (*ping_fail_print_cb_t)(int seq);
/** Ping options */
typedef struct {
ip4_addr_t ip_addr; // dest IP addr
uint16_t count; // number of requests to send
uint16_t interval_ms; // delay between requests
uint16_t payload_size; // extra payload size
uint16_t timeout_ms; // ping timeout in ms
ping_success_print_cb_t success_cb;
ping_fail_print_cb_t fail_cb;
} ping_opts_t;
/** Ping response struct */
typedef struct {
int sockfd; // fd of the used socket, may be closed externally to abort the operation
uint16_t sent; // number of sent echo requests
uint16_t received; // number of received responses
uint16_t min_time_ms; // shortest ping
uint16_t max_time_ms; // longest ping
float loss_pt; // loss ratio in percent
} ping_result_t;
/** init all except the ip addr */
#define PING_CONFIG_DEFAULT() { \
.count = 5, \
.interval_ms = 1000, \
.payload_size = 32, \
.timeout_ms = 1000, \
.success_cb = NULL, \
.fail_cb = NULL, \
}
/**
* Send a ping request to a remote server.
* Parameters, including the IPv4 address, are specified in the config struct.
*
* Ping is blocking. The operation may be interrupted by closing the socket from another task,
* its FD is exposed from the beginning in result->sockfd.
*
* @param opts
* @param result - response struct, required, for storing statistics
* @return success or error
*/
esp_err_t ping(const ping_opts_t *opts, ping_result_t *result);
#endif //_PING_H