4996b451d9
集成CH390驱动、LwIP协议栈和FreeRTOS多任务透传框架,确保TCP Server/Client与UART链路按配置稳定联动。
120 lines
2.7 KiB
C
120 lines
2.7 KiB
C
/**
|
|
* @file tcp_server.h
|
|
* @brief TCP Server module for transparent transmission with UART2
|
|
*/
|
|
|
|
#ifndef __TCP_SERVER_H__
|
|
#define __TCP_SERVER_H__
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/* Default TCP Server port */
|
|
#define TCP_SERVER_DEFAULT_PORT 8080
|
|
|
|
/* Maximum number of simultaneous connections */
|
|
#define TCP_SERVER_MAX_CONNECTIONS 1
|
|
|
|
/* Buffer sizes */
|
|
#define TCP_SERVER_RX_BUFFER_SIZE 1024
|
|
#define TCP_SERVER_TX_BUFFER_SIZE 1024
|
|
|
|
/* TCP Server state */
|
|
typedef enum {
|
|
TCP_SERVER_STATE_IDLE,
|
|
TCP_SERVER_STATE_LISTENING,
|
|
TCP_SERVER_STATE_CONNECTED,
|
|
TCP_SERVER_STATE_ERROR
|
|
} tcp_server_state_t;
|
|
|
|
/* TCP Server configuration */
|
|
typedef struct {
|
|
uint16_t port;
|
|
bool auto_reconnect;
|
|
} tcp_server_config_t;
|
|
|
|
/* TCP Server status */
|
|
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;
|
|
|
|
/**
|
|
* @brief Initialize TCP Server module
|
|
* @param config Server configuration
|
|
* @return 0 on success, negative on error
|
|
*/
|
|
int tcp_server_init(const tcp_server_config_t *config);
|
|
|
|
/**
|
|
* @brief Start TCP Server (begin listening)
|
|
* @return 0 on success, negative on error
|
|
*/
|
|
int tcp_server_start(void);
|
|
|
|
/**
|
|
* @brief Stop TCP Server
|
|
* @return 0 on success, negative on error
|
|
*/
|
|
int tcp_server_stop(void);
|
|
|
|
/**
|
|
* @brief Send data to connected client
|
|
* @param data Data buffer
|
|
* @param len Data length
|
|
* @return Number of bytes sent, negative on error
|
|
*/
|
|
int tcp_server_send(const uint8_t *data, uint16_t len);
|
|
|
|
/**
|
|
* @brief Receive data from connected client
|
|
* @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_server_recv(uint8_t *data, uint16_t max_len, uint32_t timeout_ms);
|
|
|
|
/**
|
|
* @brief Check if client is connected
|
|
* @return true if connected
|
|
*/
|
|
bool tcp_server_is_connected(void);
|
|
|
|
/**
|
|
* @brief Get TCP Server status
|
|
* @param status Pointer to status structure
|
|
*/
|
|
void tcp_server_get_status(tcp_server_status_t *status);
|
|
|
|
/**
|
|
* @brief Get TCP Server StreamBuffer handle for UART integration
|
|
* @return StreamBuffer handle for receiving data from TCP
|
|
*/
|
|
void *tcp_server_get_rx_stream(void);
|
|
|
|
/**
|
|
* @brief Get TCP Server TX StreamBuffer handle for UART integration
|
|
* @return StreamBuffer handle for sending data to TCP
|
|
*/
|
|
void *tcp_server_get_tx_stream(void);
|
|
|
|
/**
|
|
* @brief TCP Server task function (for FreeRTOS)
|
|
* @param argument Task argument (unused)
|
|
*/
|
|
void tcp_server_task(void *argument);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* __TCP_SERVER_H__ */
|