feat: add delayed uart and runtime mac configuration

This commit is contained in:
2026-04-29 04:35:48 +08:00
parent c9ece65182
commit ac0c464910
7 changed files with 93 additions and 15 deletions
+70 -2
View File
@@ -13,6 +13,7 @@
#include "route_msg.h"
#include "app_runtime.h"
#include "debug_log.h"
#include "ethernetif.h"
#include "uart_trans.h"
#define CONFIG_RX_BUFFER_SIZE 160u
@@ -115,6 +116,19 @@ static int parse_link_uart(const char *value, uint8_t *uart)
return -1;
}
static int parse_uart_name(const char *value, uint8_t *uart_index)
{
if (equals_ignore_case(value, "U0") || equals_ignore_case(value, "UART2")) {
*uart_index = LINK_UART_U0;
return 0;
}
if (equals_ignore_case(value, "U1") || equals_ignore_case(value, "UART3")) {
*uart_index = LINK_UART_U1;
return 0;
}
return -1;
}
static const char *link_uart_to_str(uint8_t uart)
{
return (uart == LINK_UART_U1) ? "U1" : "U0";
@@ -136,6 +150,15 @@ static const char *link_index_to_name(uint32_t index)
}
}
static void config_get_display_mac(uint8_t *mac)
{
if (ethernetif_get_effective_mac(mac) != 0u) {
return;
}
memcpy(mac, g_config.net.mac, sizeof(g_config.net.mac));
}
static int parse_link_name(const char *value, uint32_t *index)
{
if (equals_ignore_case(value, "S1")) {
@@ -231,13 +254,15 @@ static at_result_t handle_summary_query(char *response, uint16_t max_len)
char mask_str[16];
char gw_str[16];
char mac_str[18];
uint8_t display_mac[6];
char rip_str[CONFIG_LINK_COUNT][16];
uint32_t i;
config_ip_to_str(g_config.net.ip, ip_str);
config_ip_to_str(g_config.net.mask, mask_str);
config_ip_to_str(g_config.net.gw, gw_str);
config_mac_to_str(g_config.net.mac, mac_str);
config_get_display_mac(display_mac);
config_mac_to_str(display_mac, mac_str);
for (i = 0; i < CONFIG_LINK_COUNT; ++i) {
config_ip_to_str(g_config.links[i].remote_ip, rip_str[i]);
}
@@ -317,6 +342,14 @@ const device_config_t *config_get(void)
return &g_config;
}
uint32_t config_get_uart_baudrate(uint8_t uart_index)
{
if (uart_index >= CONFIG_UART_COUNT) {
return DEFAULT_UART_BAUDRATE;
}
return g_config.uart_baudrate[uart_index];
}
device_config_t *config_get_mutable(void)
{
return &g_config;
@@ -372,6 +405,39 @@ at_result_t config_process_at_cmd(const char *cmd, char *response, uint16_t max_
snprintf(response, max_len, "+MUX:%u\r\nOK\r\n", g_config.mux_mode);
return AT_OK;
}
if (equals_ignore_case(p, "BAUD?")) {
snprintf(response, max_len,
"+BAUD:U0=%lu,U1=%lu\r\nOK\r\n",
(unsigned long)g_config.uart_baudrate[0],
(unsigned long)g_config.uart_baudrate[1]);
return AT_OK;
}
if (parse_command_with_value(p, "BAUD", &value)) {
char value_copy[48];
char *cursor;
char *token;
uint8_t uart_index;
uint32_t baudrate;
strncpy(value_copy, value, sizeof(value_copy) - 1u);
value_copy[sizeof(value_copy) - 1u] = '\0';
cursor = value_copy;
token = config_next_token(&cursor);
if (token == NULL || parse_uart_name(token, &uart_index) != 0) {
snprintf(response, max_len, "ERROR: Invalid UART\r\n");
return AT_INVALID_PARAM;
}
token = config_next_token(&cursor);
if (token == NULL || parse_u32_value(token, 1200u, 921600u, &baudrate) != 0) {
snprintf(response, max_len, "ERROR: Invalid baudrate\r\n");
return AT_INVALID_PARAM;
}
g_config.uart_baudrate[uart_index] = baudrate;
snprintf(response, max_len, "OK\r\n");
return AT_NEED_REBOOT;
}
if (parse_command_with_value(p, "MUX", &value)) {
uint32_t mux_value;
if (parse_u32_value(value, 0u, 1u, &mux_value) != 0) {
@@ -387,11 +453,13 @@ at_result_t config_process_at_cmd(const char *cmd, char *response, uint16_t max_
char mask_str[16];
char gw_str[16];
char mac_str[18];
uint8_t display_mac[6];
config_ip_to_str(g_config.net.ip, ip_str);
config_ip_to_str(g_config.net.mask, mask_str);
config_ip_to_str(g_config.net.gw, gw_str);
config_mac_to_str(g_config.net.mac, mac_str);
config_get_display_mac(display_mac);
config_mac_to_str(display_mac, mac_str);
snprintf(response, max_len, "+NET:IP=%s,MASK=%s,GW=%s,MAC=%s\r\nOK\r\n", ip_str, mask_str, gw_str, mac_str);
return AT_OK;
}