/**
 * @file tcp_server.h
 * @brief Indexed lwIP RAW TCP server manager.
 */

#ifndef __TCP_SERVER_H__
#define __TCP_SERVER_H__

#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define TCP_SERVER_INSTANCE_COUNT    2u
#define TCP_SERVER_RX_BUFFER_SIZE    480u

typedef enum {
    TCP_SERVER_STATE_IDLE = 0,
    TCP_SERVER_STATE_LISTENING,
    TCP_SERVER_STATE_CONNECTED,
    TCP_SERVER_STATE_ERROR
} tcp_server_state_t;

typedef struct {
    uint16_t port;
    bool enabled;
} tcp_server_instance_config_t;

typedef struct {
    tcp_server_state_t state;
    uint32_t rx_bytes;
    uint32_t tx_bytes;
    uint32_t connections;
    uint32_t errors;
} tcp_server_status_t;

int tcp_server_init_all(void);
int tcp_server_config(uint8_t instance, const tcp_server_instance_config_t *config);
int tcp_server_start(uint8_t instance);
int tcp_server_stop(uint8_t instance);
int tcp_server_send(uint8_t instance, const uint8_t *data, uint16_t len);
int tcp_server_recv(uint8_t instance, uint8_t *data, uint16_t max_len);
uint16_t tcp_server_rx_available(uint8_t instance);
uint16_t tcp_server_peek(uint8_t instance, uint8_t *data, uint16_t max_len);
void tcp_server_drop(uint8_t instance, uint16_t len);
bool tcp_server_is_connected(uint8_t instance);
void tcp_server_get_status(uint8_t instance, tcp_server_status_t *status);
void tcp_server_poll(void);

#ifdef __cplusplus
}
#endif

#endif /* __TCP_SERVER_H__ */
