Files
TCP2UART/App/config.h
T

207 lines
5.8 KiB
C

/**
* @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
*/
#ifndef __CONFIG_H__
#define __CONFIG_H__
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Configuration magic number "TCPU" */
#define CONFIG_MAGIC 0x54435055
/* Configuration version for compatibility */
#define CONFIG_VERSION 0x0001
/* 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) */
uint32_t crc;
} device_config_t;
/* Default configuration values */
#define DEFAULT_IP {192, 168, 1, 100}
#define DEFAULT_MASK {255, 255, 255, 0}
#define DEFAULT_GW {192, 168, 1, 1}
#define DEFAULT_MAC {0x02, 0x00, 0x00, 0x00, 0x00, 0x01}
#define DEFAULT_SERVER_PORT 8080
#define DEFAULT_REMOTE_IP {192, 168, 1, 200}
#define DEFAULT_REMOTE_PORT 9000
#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
/* AT command result codes */
typedef enum {
AT_OK = 0,
AT_ERROR,
AT_INVALID_PARAM,
AT_UNKNOWN_CMD,
AT_SAVE_FAILED,
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 UART1 IDLE interrupt handler for config module
*/
void config_uart_idle_handler(void);
/**
* @brief Start UART1 reception for configuration
*/
void config_start_reception(void);
/**
* @brief Poll configuration UART and process pending AT commands
*/
void config_poll(void);
/**
* @brief Check whether AT+RESET requested a system reset
*/
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);
#ifdef __cplusplus
}
#endif
#endif /* __CONFIG_H__ */