Files

69 lines
1.7 KiB
C++
Raw Permalink Normal View History

2025-12-25 12:07:06 +08:00
#ifndef __OTA_H
#define __OTA_H
#include "Can.h"
#include "aes128.h"
2025-12-25 12:07:06 +08:00
#include "config.h"
#include "gd32c10x.h"
#include <stdint.h>
2025-12-25 12:07:06 +08:00
class OTAHandler {
public:
u32 ota_stateota_time;
u32 received_size;
u32 expected_size;
2025-12-25 12:07:06 +08:00
void init();
void process();
bool isActive() const { return ota_state; }
2025-12-25 12:07:06 +08:00
void handleOTAMessage(u32 id, u8 *data, u8 len);
2025-12-25 12:07:06 +08:00
void jumpToApplication();
private:
u32 ota_time;
2025-12-25 12:07:06 +08:00
void eraseApplicationArea();
void writeApplicationData(u32 offset, u8 *data, u32 size);
bool verifyApplicationData(u32 offset, u8 *data, u32 size);
// Flash操作标志(用于延迟执行,避免中断中耗时操作)
bool pending_flash_erase = false;
u32 pending_flash_offset = 0;
u8 pending_flash_data[64];
u8 pending_flash_size = 0;
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;
u8 device_uid[12]; // 从GD32 UID寄存器读取
u8 current_K_session[16];
u8 nonce_server[16];
aes128_context aes_ctx; // AES上下文
ota_state_t ota_state = OTA_STATE_IDLE;
ota_error_t ota_error = OTA_ERROR_NONE;
2025-12-25 12:07:06 +08:00
};
void getDeviceSerialNumber(u8 *buffer);
void calculate_crc32_with_key(const u8 *key, const u8 *data,
u8 *output);
void send_error_frame(u8 error);
bool validateApplication();
2026-01-05 00:52:20 +08:00
fmc_state_enum move_app_data();
u8 OTA_SET(u32 addr, u32 data);
2026-01-05 00:52:20 +08:00
2025-12-25 12:07:06 +08:00
#endif