feat: save stable CH390 bridge baseline
This commit is contained in:
+63
-159
@@ -1,96 +1,78 @@
|
||||
/**
|
||||
* @file config.h
|
||||
* @brief AT command configuration module for TCP2UART
|
||||
*
|
||||
* Handles UART1 AT commands for network and serial port configuration.
|
||||
*
|
||||
* Supported AT commands:
|
||||
* - AT+IP=192.168.1.100 Set device IP
|
||||
* - AT+MASK=255.255.255.0 Set subnet mask
|
||||
* - AT+GW=192.168.1.1 Set gateway
|
||||
* - AT+PORT=8080 Set TCP Server listen port
|
||||
* - AT+RIP=192.168.1.200 Set TCP Client remote IP
|
||||
* - AT+RPORT=9000 Set TCP Client remote port
|
||||
* - AT+BAUD1=115200 Set UART2 baudrate
|
||||
* - AT+BAUD2=115200 Set UART3 baudrate
|
||||
* - AT+MAC=00:11:22:33:44:55 Set MAC address
|
||||
* - AT+DHCP=0/1 Enable/disable DHCP
|
||||
* - AT+SAVE Save parameters to Flash
|
||||
* - AT+RESET Reset device
|
||||
* - AT+DEFAULT Restore factory defaults
|
||||
* - AT+? Query current configuration
|
||||
* @brief Final AT configuration model for TCP2UART.
|
||||
*/
|
||||
|
||||
#ifndef __CONFIG_H__
|
||||
#define __CONFIG_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Configuration magic number "TCPU" */
|
||||
#define CONFIG_MAGIC 0x54435055
|
||||
#define CONFIG_MAGIC 0x54435055u
|
||||
#define CONFIG_VERSION 0x0003u
|
||||
|
||||
/* Configuration version for compatibility */
|
||||
#define CONFIG_VERSION 0x0002
|
||||
#define CONFIG_UART_COUNT 2u
|
||||
#define CONFIG_LINK_COUNT 4u
|
||||
|
||||
#define CONFIG_LINK_S1 0u
|
||||
#define CONFIG_LINK_S2 1u
|
||||
#define CONFIG_LINK_C1 2u
|
||||
#define CONFIG_LINK_C2 3u
|
||||
|
||||
#define ENDPOINT_C1 0x01u
|
||||
#define ENDPOINT_C2 0x02u
|
||||
#define ENDPOINT_UART2 0x04u
|
||||
#define ENDPOINT_UART3 0x08u
|
||||
#define ENDPOINT_S1 0x10u
|
||||
#define ENDPOINT_S2 0x20u
|
||||
|
||||
#define LINK_UART_U0 0u
|
||||
#define LINK_UART_U1 1u
|
||||
|
||||
typedef enum {
|
||||
MUX_MODE_RAW = 0,
|
||||
MUX_MODE_FRAME = 1
|
||||
} mux_mode_t;
|
||||
|
||||
/* Device configuration structure */
|
||||
typedef struct {
|
||||
uint32_t magic; /* Magic number for validation */
|
||||
uint16_t version; /* Configuration version */
|
||||
uint16_t reserved; /* Reserved for alignment */
|
||||
|
||||
/* Network settings */
|
||||
uint8_t mac[6]; /* MAC address */
|
||||
uint8_t dhcp_enable; /* DHCP enable flag */
|
||||
uint8_t reserved2; /* Reserved for alignment */
|
||||
uint8_t ip[4]; /* Device IP address */
|
||||
uint8_t mask[4]; /* Subnet mask */
|
||||
uint8_t gw[4]; /* Gateway */
|
||||
|
||||
/* TCP Server settings */
|
||||
uint16_t server_port; /* Server listen port */
|
||||
uint16_t reserved3; /* Reserved for alignment */
|
||||
|
||||
/* TCP Client settings */
|
||||
uint8_t remote_ip[4]; /* Remote server IP */
|
||||
uint16_t remote_port; /* Remote server port */
|
||||
uint16_t reconnect_interval;/* Reconnect interval (ms) */
|
||||
|
||||
/* UART settings */
|
||||
uint32_t uart2_baudrate; /* UART2 (Server) baudrate */
|
||||
uint32_t uart3_baudrate; /* UART3 (Client) baudrate */
|
||||
uint8_t uart2_databits; /* UART2 data bits */
|
||||
uint8_t uart2_stopbits; /* UART2 stop bits */
|
||||
uint8_t uart2_parity; /* UART2 parity */
|
||||
uint8_t uart3_databits; /* UART3 data bits */
|
||||
uint8_t uart3_stopbits; /* UART3 stop bits */
|
||||
uint8_t uart3_parity; /* UART3 parity */
|
||||
uint16_t reserved4; /* Reserved for alignment */
|
||||
|
||||
/* CRC32 checksum (must be last) */
|
||||
uint8_t ip[4];
|
||||
uint8_t mask[4];
|
||||
uint8_t gw[4];
|
||||
uint8_t mac[6];
|
||||
uint8_t reserved[2];
|
||||
} net_config_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t enabled;
|
||||
uint8_t uart;
|
||||
uint16_t local_port;
|
||||
uint8_t remote_ip[4];
|
||||
uint16_t remote_port;
|
||||
uint16_t reserved;
|
||||
} link_config_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t magic;
|
||||
uint16_t version;
|
||||
uint8_t mux_mode;
|
||||
uint8_t reserved0;
|
||||
net_config_t net;
|
||||
link_config_t links[CONFIG_LINK_COUNT];
|
||||
uint32_t uart_baudrate[CONFIG_UART_COUNT];
|
||||
uint32_t crc;
|
||||
} device_config_t;
|
||||
|
||||
/* Default configuration values */
|
||||
#define DEFAULT_IP {192, 168, 31, 100}
|
||||
#define DEFAULT_MASK {255, 255, 255, 0}
|
||||
#define DEFAULT_GW {192, 168, 31, 1}
|
||||
#define DEFAULT_MAC {0x02, 0x00, 0x00, 0x00, 0x00, 0x01}
|
||||
#define DEFAULT_SERVER_PORT 8080
|
||||
#define DEFAULT_REMOTE_IP {192, 168, 31, 1}
|
||||
#define DEFAULT_REMOTE_PORT 8081
|
||||
#define DEFAULT_UART_BAUDRATE 115200
|
||||
#define DEFAULT_UART_DATABITS 8
|
||||
#define DEFAULT_UART_STOPBITS 1
|
||||
#define DEFAULT_UART_PARITY 0
|
||||
#define DEFAULT_DHCP_ENABLE 0
|
||||
#define DEFAULT_RECONNECT_MS 3000
|
||||
#define DEFAULT_NET_IP {192, 168, 1, 100}
|
||||
#define DEFAULT_NET_MASK {255, 255, 255, 0}
|
||||
#define DEFAULT_NET_GW {192, 168, 1, 1}
|
||||
#define DEFAULT_NET_MAC {0x02, 0x00, 0x00, 0x00, 0x00, 0x01}
|
||||
#define DEFAULT_UART_BAUDRATE 115200u
|
||||
|
||||
/* AT command result codes */
|
||||
typedef enum {
|
||||
AT_OK = 0,
|
||||
AT_ERROR,
|
||||
@@ -100,108 +82,30 @@ typedef enum {
|
||||
AT_NEED_REBOOT
|
||||
} at_result_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize configuration module
|
||||
* @return 0 on success, negative on error
|
||||
*/
|
||||
int config_init(void);
|
||||
|
||||
/**
|
||||
* @brief Load configuration from Flash
|
||||
* @return 0 on success, negative on error (defaults loaded)
|
||||
*/
|
||||
int config_load(void);
|
||||
|
||||
/**
|
||||
* @brief Save configuration to Flash
|
||||
* @return 0 on success, negative on error
|
||||
*/
|
||||
int config_save(void);
|
||||
|
||||
/**
|
||||
* @brief Reset configuration to factory defaults
|
||||
*/
|
||||
void config_set_defaults(void);
|
||||
|
||||
/**
|
||||
* @brief Get current configuration
|
||||
* @return Pointer to current configuration (read-only)
|
||||
*/
|
||||
const device_config_t *config_get(void);
|
||||
|
||||
/**
|
||||
* @brief Get mutable configuration for modification
|
||||
* @return Pointer to configuration structure
|
||||
*/
|
||||
device_config_t *config_get_mutable(void);
|
||||
|
||||
/**
|
||||
* @brief Process AT command received from UART1
|
||||
* @param cmd Command string (null-terminated)
|
||||
* @param response Response buffer
|
||||
* @param max_len Maximum response length
|
||||
* @return AT command result code
|
||||
*/
|
||||
at_result_t config_process_at_cmd(const char *cmd, char *response, uint16_t max_len);
|
||||
|
||||
/**
|
||||
* @brief Poll configuration UART and process pending AT commands
|
||||
*/
|
||||
void config_poll(void);
|
||||
|
||||
/**
|
||||
* @brief Feed one byte received from the config UART.
|
||||
* @param byte Received byte.
|
||||
*/
|
||||
void config_uart_rx_byte(uint8_t byte);
|
||||
|
||||
/**
|
||||
* @brief Try to process one AT command frame from an external UART source.
|
||||
* @param data Input bytes.
|
||||
* @param len Input length.
|
||||
* @return true if the frame was recognized as an AT/config command.
|
||||
*/
|
||||
bool config_try_process_frame(const uint8_t *data, uint16_t len);
|
||||
|
||||
/**
|
||||
* @brief Check whether AT+RESET requested a system reset
|
||||
*/
|
||||
bool config_build_response_frame(const uint8_t *data,
|
||||
uint16_t len,
|
||||
char *response,
|
||||
uint16_t max_len,
|
||||
at_result_t *result);
|
||||
bool config_is_reset_requested(void);
|
||||
|
||||
/**
|
||||
* @brief Clear the pending reset request flag
|
||||
*/
|
||||
void config_clear_reset_requested(void);
|
||||
|
||||
/**
|
||||
* @brief Format IP address to string
|
||||
* @param ip IP address bytes
|
||||
* @param str Output string buffer (min 16 bytes)
|
||||
*/
|
||||
void config_ip_to_str(const uint8_t *ip, char *str);
|
||||
|
||||
/**
|
||||
* @brief Parse IP address from string
|
||||
* @param str IP address string (e.g. "192.168.1.100")
|
||||
* @param ip Output IP address bytes
|
||||
* @return 0 on success, negative on error
|
||||
*/
|
||||
int config_str_to_ip(const char *str, uint8_t *ip);
|
||||
|
||||
/**
|
||||
* @brief Format MAC address to string
|
||||
* @param mac MAC address bytes
|
||||
* @param str Output string buffer (min 18 bytes)
|
||||
*/
|
||||
void config_mac_to_str(const uint8_t *mac, char *str);
|
||||
|
||||
/**
|
||||
* @brief Parse MAC address from string
|
||||
* @param str MAC address string (e.g. "00:11:22:33:44:55")
|
||||
* @param mac Output MAC address bytes
|
||||
* @return 0 on success, negative on error
|
||||
*/
|
||||
int config_str_to_mac(const char *str, uint8_t *mac);
|
||||
uint8_t config_link_index_to_endpoint(uint8_t index);
|
||||
uint8_t config_uart_index_to_endpoint(uint8_t uart_index);
|
||||
bool config_endpoint_is_single(uint8_t endpoint);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user