新增UART0 RX泵任务功能,将GPIO20接收到的串口数据按切行、剥离后以tag=UART推入ring buffer,通过HTTP /logs接口显示在网页日志面板。 接线方式:任意串口源TX连接至H1/H2的ESP_RX引脚,波特率3M 8N1。 feat(motor_ota): GD32 OTA增加包级防呆机制防止错误固件刷入 中继触发前校验gd32_ota镜像向量表身份(Gd32FlashStore::validateStoredImageIdentity), 对Bootloader包或非APP包在WiFi /ota/relay与BLE 0xF7均拒绝中继, 避免GD32 APP区被错误包擦除。新增0x87完成码用于固件身份验证失败场景。 feat(motor_state): 扩展CAN状态数据结构支持CONFIG负载 扩展motor_slot_t结构体,新增config_rx_count、config_seq和config_data字段 用于存储CONFIG帧的相关信息。CAN状态数据包大小从44B扩展到76B, 页面总数从6调整为9,支持目标值回报的配置数据传输。 fix(motor_ota): 修复OTA取消流程中的信用度管理和任务清理问题 ★ 先复位credit再发送CANCEL命令,防止传输卡住时背压导致CANCEL被拒绝。 延长取消等待时间至10s覆盖最坏情况,确保feedTask正常退出而非强制删除。 取消完成后复位credit防止锁存死锁,确保后续通信正常进行。 fix(http_server): 修复HTTP请求处理中的边界检查和安全验证 拒绝chunked请求(content_len<=0)防止无限循环,添加通道参数范围验证(ch 0~2), 在OTA中继触发前验证固件身份防止错误包刷入,使用http_json_append避免缓冲区越界。 refactor(gd32_flash): 实现GD32固件身份验证算法确保刷写安全 基于Cortex-M向量表内容实现包级身份校验,通过MSP和复位向量位置 区分APP包、Bootloader包和无效固件,为OTA安全提供基础保障。 BREAKING CHANGE: 电机状态API数据格式变更,CAN通道参数验证更严格
118 lines
3.4 KiB
C++
118 lines
3.4 KiB
C++
/*
|
||
* motor_state.cpp - GD32 多电机状态推送解析与缓存 (SPI TYPE 0xC5)
|
||
*/
|
||
#include "monitor/motor_state.h"
|
||
#include "monitor/spi_ring_cache.h"
|
||
|
||
#include <string.h>
|
||
|
||
namespace {
|
||
|
||
static SemaphoreHandle_t s_mux = nullptr;
|
||
static motor_state_table_t s_table = {};
|
||
|
||
} // namespace
|
||
|
||
bool onMotorStateEvent(const uint8_t *payload, size_t len)
|
||
{
|
||
if (payload == nullptr || len < MOTOR_PAGE_HEADER) {
|
||
return false;
|
||
}
|
||
|
||
const uint8_t page = payload[0];
|
||
const uint8_t total_pages = payload[1];
|
||
const uint8_t entry_count = payload[2];
|
||
const uint8_t active_cnt = payload[3];
|
||
|
||
if (page >= MOTOR_TOTAL_PAGES || total_pages == 0 || total_pages > MOTOR_TOTAL_PAGES) {
|
||
return false;
|
||
}
|
||
|
||
/* 验证长度足够容纳声明的 entry 数 */
|
||
const size_t expected = MOTOR_PAGE_HEADER + static_cast<size_t>(entry_count) * MOTOR_ENTRY_SIZE;
|
||
if (len < expected) {
|
||
return false;
|
||
}
|
||
|
||
ringCacheEnsureMux(&s_mux);
|
||
if (s_mux == nullptr) return false;
|
||
if (xSemaphoreTake(s_mux, pdMS_TO_TICKS(20)) != pdTRUE) return false;
|
||
|
||
/* page 0 时刷新全局字段。
|
||
* active 字节 bit7 = boot_active(总线上有电机在 BOOT,0x103 心跳),
|
||
* 低 7 位才是真实在线电机数。 */
|
||
if (page == 0) {
|
||
s_table.active_count = static_cast<uint8_t>(active_cnt & 0x7FU);
|
||
s_table.boot_active = (active_cnt & 0x80U) ? 1U : 0U;
|
||
}
|
||
|
||
/* 逐条解析 entry:node×channel 双键槽位,同节点号多通道互不覆盖 */
|
||
size_t offset = MOTOR_PAGE_HEADER;
|
||
for (uint8_t i = 0; i < entry_count; ++i) {
|
||
const uint8_t *e = &payload[offset];
|
||
const uint8_t node = e[0];
|
||
const uint8_t ch = e[1];
|
||
if (node < 1 || node > MOTOR_MAX_NODES || ch >= MOTOR_CHANNELS) {
|
||
offset += MOTOR_ENTRY_SIZE;
|
||
continue;
|
||
}
|
||
|
||
motor_slot_t &slot = s_table.slots[(node - 1) * MOTOR_CHANNELS + ch];
|
||
slot.node_id = node;
|
||
slot.channel = ch;
|
||
slot.online = e[2];
|
||
slot.dlc = e[3];
|
||
slot.rx_count = rdU32Le(&e[4]);
|
||
slot.seq = rdU32Le(&e[8]);
|
||
memcpy(slot.data, &e[12], MOTOR_STATUS_DLC);
|
||
slot.config_rx_count = rdU32Le(&e[44]);
|
||
slot.config_seq = rdU32Le(&e[48]);
|
||
memcpy(slot.config_data, &e[52], MOTOR_CONFIG_DLC);
|
||
|
||
offset += MOTOR_ENTRY_SIZE;
|
||
}
|
||
|
||
/* 统计有效槽位 */
|
||
uint8_t valid = 0;
|
||
for (uint8_t i = 0; i < MOTOR_MAX_SLOTS; ++i) {
|
||
if (s_table.slots[i].node_id != 0) valid++;
|
||
}
|
||
s_table.valid_slots = valid;
|
||
s_table.recv_ms = esp_timer_get_time() / 1000;
|
||
s_table.have_data = true;
|
||
|
||
xSemaphoreGive(s_mux);
|
||
return true;
|
||
}
|
||
|
||
bool getLatestMotorState(motor_state_table_t *out)
|
||
{
|
||
if (out == nullptr) return false;
|
||
ringCacheEnsureMux(&s_mux);
|
||
if (s_mux == nullptr) return false;
|
||
|
||
bool ok = false;
|
||
if (xSemaphoreTake(s_mux, pdMS_TO_TICKS(20)) == pdTRUE) {
|
||
if (s_table.have_data) {
|
||
*out = s_table;
|
||
ok = true;
|
||
}
|
||
xSemaphoreGive(s_mux);
|
||
}
|
||
return ok;
|
||
}
|
||
|
||
uint32_t motorStateAgeMs(void)
|
||
{
|
||
ringCacheEnsureMux(&s_mux);
|
||
if (s_mux == nullptr) return UINT32_MAX;
|
||
|
||
int64_t recv = 0;
|
||
bool have = false;
|
||
if (xSemaphoreTake(s_mux, pdMS_TO_TICKS(20)) == pdTRUE) {
|
||
have = s_table.have_data;
|
||
recv = s_table.recv_ms;
|
||
xSemaphoreGive(s_mux);
|
||
}
|
||
return ringCacheAgeMs(have, recv);
|
||
} |