Files
GD32_BOOT/Robot_V2/sys/OTA.h
T
cbb c7c4451d57 refactor(build): 支持A/B分区逻辑
移除sys/flash.h、sys/flash.cpp、sys/initial.h和sys/OTA相关文件的
引用,清理cproject.yml和cbuild.yml中的未使用组件和文件配置。

BREAKING CHANGE: 移除了FlashEEPROM相关功能和接口。
2026-01-05 00:52:20 +08:00

62 lines
1.5 KiB
C++

#ifndef __OTA_H
#define __OTA_H
#include "Can.h"
#include "aes128.h"
#include "config.h"
#include "gd32c10x.h"
#include <stdint.h>
class OTAHandler {
public:
u32 ota_stateota_time;
uint32_t received_size;
uint32_t expected_size;
void init();
void process();
bool isActive() const { return ota_state; }
void handleOTAMessage(uint32_t id, uint8_t *data, uint8_t len);
void jumpToApplication();
private:
u16 ota_time;
void eraseApplicationArea();
void writeApplicationData(uint32_t offset, uint8_t *data, uint32_t size);
bool verifyApplicationData(u32 offset, u8 *data, u32 size);
typedef enum {
OTA_STATE_IDLE = 0,
OTA_STATE_WAITING_RANDOM, // 第二阶段等待随机数
OTA_STATE_WAITING_CONFIRM, // 等待上位机确认
OTA_STATE_CHECKING_FIRMWARE,
OTA_STATE_RECEIVING_FIRMWARE,
OTA_STATE_FINISHED
} ota_state_t;
typedef enum {
OTA_ERROR_NONE = 0,
OTA_ERROR_TIMEOUT,
OTA_ERROR_FLASH_ERROR,
OTA_ERROR_INVALID_KEY,
OTA_ERROR_INVALID_PACKET,
OTA_ERROR_INVALID_HANDSHAKE,
OTA_ERROR_INVALID_CRC,
OTA_ERROR_INVALID_SIZE
} ota_error_t;
uint8_t device_uid[12]; // 从GD32 UID寄存器读取
uint8_t current_K_session[16];
uint8_t nonce_server[16];
aes128_context aes_ctx; // AES上下文
ota_state_t ota_state = OTA_STATE_IDLE;
ota_error_t ota_error = OTA_ERROR_NONE;
};
void getDeviceSerialNumber(uint8_t *buffer);
void calculate_crc32_with_key(const uint8_t *key, const uint8_t *data,
u8 *output);
void send_error_frame(uint8_t error);
bool validateApplication();
fmc_state_enum move_app_data();
#endif