/** * @file tcp_client.h * @brief TCP Client module for transparent transmission with UART3 */ #ifndef __TCP_CLIENT_H__ #define __TCP_CLIENT_H__ #include #include #ifdef __cplusplus extern "C" { #endif /* Default TCP Client settings */ #define TCP_CLIENT_DEFAULT_PORT 8081 #define TCP_CLIENT_DEFAULT_SERVER "192.168.1.100" /* Reconnect settings */ #define TCP_CLIENT_RECONNECT_DELAY_MS 3000 #define TCP_CLIENT_MAX_RECONNECT_TRIES 0 /* 0 = infinite */ /* Buffer sizes */ #define TCP_CLIENT_RX_BUFFER_SIZE 512 #define TCP_CLIENT_TX_BUFFER_SIZE 512 /* TCP Client state */ typedef enum { TCP_CLIENT_STATE_IDLE, TCP_CLIENT_STATE_CONNECTING, TCP_CLIENT_STATE_CONNECTED, TCP_CLIENT_STATE_DISCONNECTED, TCP_CLIENT_STATE_ERROR } tcp_client_state_t; /* TCP Client configuration */ typedef struct { uint8_t server_ip[4]; /* Server IP address */ uint16_t server_port; /* Server port */ bool auto_reconnect; /* Auto reconnect on disconnect */ uint16_t reconnect_interval_ms; /* Reconnect interval */ } tcp_client_config_t; /* TCP Client status */ typedef struct { tcp_client_state_t state; uint32_t rx_bytes; uint32_t tx_bytes; uint32_t reconnect_count; uint32_t errors; } tcp_client_status_t; /** * @brief Initialize TCP Client module * @param config Client configuration * @return 0 on success, negative on error */ int tcp_client_init(const tcp_client_config_t *config); /** * @brief Connect to remote server * @return 0 on success, negative on error */ int tcp_client_connect(void); /** * @brief Disconnect from server * @return 0 on success, negative on error */ int tcp_client_disconnect(void); /** * @brief Send data to server * @param data Data buffer * @param len Data length * @return Number of bytes sent, negative on error */ int tcp_client_send(const uint8_t *data, uint16_t len); /** * @brief Receive data from server * @param data Data buffer * @param max_len Maximum length to receive * @param timeout_ms Timeout in milliseconds (0 = non-blocking) * @return Number of bytes received, 0 if no data, negative on error */ int tcp_client_recv(uint8_t *data, uint16_t max_len, uint32_t timeout_ms); /** * @brief Check if connected to server * @return true if connected */ bool tcp_client_is_connected(void); /** * @brief Update server configuration (for AT command) * @param ip Server IP address (4 bytes) * @param port Server port * @return 0 on success, negative on error */ int tcp_client_set_server(const uint8_t *ip, uint16_t port); /** * @brief Get TCP Client status * @param status Pointer to status structure */ void tcp_client_get_status(tcp_client_status_t *status); /** * @brief Get TCP Client RX StreamBuffer handle for UART integration * @return StreamBuffer handle for receiving data from TCP */ void *tcp_client_get_rx_stream(void); /** * @brief Get TCP Client TX StreamBuffer handle for UART integration * @return StreamBuffer handle for sending data to TCP */ void *tcp_client_get_tx_stream(void); /** * @brief TCP Client task function (for FreeRTOS) * @param argument Task argument (unused) */ void tcp_client_task(void *argument); #ifdef __cplusplus } #endif #endif /* __TCP_CLIENT_H__ */