ESP32/components/iperf/iperf.h

104 lines
3.3 KiB
C

#ifndef IPERF_H
#define IPERF_H
#include <stdint.h>
#include <stdbool.h>
// --- Configuration Flags ---
#define IPERF_FLAG_CLIENT (1 << 0)
#define IPERF_FLAG_SERVER (1 << 1)
#define IPERF_FLAG_TCP (1 << 2)
#define IPERF_FLAG_UDP (1 << 3)
// --- Defaults ---
#define IPERF_DEFAULT_PORT 5001
#define IPERF_DEFAULT_INTERVAL 3
#define IPERF_DEFAULT_TIME 30
#define IPERF_TRAFFIC_TASK_PRIORITY 4
#define IPERF_REPORT_TASK_PRIORITY 5
#define IPERF_SOCKET_RX_TIMEOUT 10
#define IPERF_SOCKET_ACCEPT_TIMEOUT 5
// --- Buffer Sizes ---
#define IPERF_UDP_TX_LEN (1470) // Default UDP Payload
#define IPERF_UDP_RX_LEN (16 << 10)
#define IPERF_TCP_TX_LEN (16 << 10)
#define IPERF_TCP_RX_LEN (16 << 10)
// --- NVS Storage Keys ---
#define NVS_KEY_IPERF_ENABLE "iperf_enabled" // 0=Disabled, 1=Enabled
#define NVS_KEY_IPERF_RATE "iperf_rate" // Target Bandwidth (Mbps)
#define NVS_KEY_IPERF_ROLE "iperf_role" // "CLIENT" or "SERVER"
#define NVS_KEY_IPERF_DST_IP "iperf_dst_ip" // Target IP String
#define NVS_KEY_IPERF_PROTO "iperf_proto" // "UDP" or "TCP"
#define NVS_KEY_IPERF_BURST "iperf_burst" // Packets per schedule tick
#define NVS_KEY_IPERF_LEN "iperf_len" // UDP Payload Length
// --- Main Configuration Structure ---
typedef struct {
uint32_t flag; // Client/Server | TCP/UDP flags
uint8_t type; // (Internal use)
uint32_t dip; // Destination IP (Network Byte Order)
uint16_t dport; // Destination Port
uint16_t sport; // Source Port
uint32_t interval; // Report Interval (seconds)
uint32_t time; // Test Duration (seconds)
uint32_t bw_lim; // Bandwidth Limit (Mbps)
uint32_t burst_count;// Burst Mode: Packets per schedule tick
uint32_t send_len; // User defined Payload Length
uint32_t buffer_len; // Internally calculated buffer size
} iperf_cfg_t;
// --- Traffic Statistics Structure ---
typedef struct {
uint64_t total_len;
uint32_t buffer_len;
uint32_t sockfd;
uint32_t actual_len;
uint32_t packet_count;
uint8_t *buffer;
uint32_t udp_lost_counter;
uint32_t udp_packet_counter;
} iperf_traffic_t;
// --- Iperf 2.0.5+ Compatible Headers ---
// Standard UDP Datagram Header (Present in EVERY packet)
typedef struct {
int32_t id; // Sequence Number
uint32_t tv_sec; // Timestamp Seconds
uint32_t tv_usec; // Timestamp Microseconds
uint32_t id2; // 64-bit seq / Padding
} udp_datagram;
// Client Header (Sent ONLY in the first UDP packet of a stream)
typedef struct {
int32_t flags; // Flags (Version, Dual Test, etc.)
int32_t numThreads; // Parallel threads
int32_t mPort; // Port
int32_t mBufLen; // Buffer Length
int32_t mWinBand; // Target Bandwidth
int32_t mAmount; // Duration / Bytes (negative = time)
} client_hdr_v1;
// Version Flag for Client Header
#define HEADER_VERSION1 0x80000000
// --- Public API ---
/**
* @brief Start the Iperf task.
* * Reads configuration from NVS ("storage" partition) to override defaults.
* If NVS_KEY_IPERF_ENABLE is 0, this function returns immediately.
* * @param cfg Pointer to initial configuration (can be overridden by NVS)
*/
void iperf_start(iperf_cfg_t *cfg);
/**
* @brief Stop the Iperf task.
* * Signals the running task to finish and close sockets.
*/
void iperf_stop(void);
#endif // IPERF_H