| | |
| | | /***************************************************************************** |
| | | * @file PathTest.c |
| | | * @brief 路径下载和读取测试模块 |
| | | * @brief 路径下载和读取测试模块 + OTA升级测试模块 |
| | | * @author AI Assistant |
| | | * @date 2025-12-19 |
| | | *****************************************************************************/ |
| | | |
| | | #include "PathTest.h" |
| | | #include "PathStorage.h" |
| | | #include "OTAUpgrade.h" |
| | | #include "TCPClient.h" |
| | | #include "HIDO_Debug.h" |
| | | #include "HIDO_Timer.h" |
| | |
| | | { |
| | | return s_test_ctx.state; |
| | | } |
| | | |
| | | /***************************************************************************** |
| | | * OTA升级测试功能 |
| | | *****************************************************************************/ |
| | | |
| | | /***************************************************************************** |
| | | * 私有数据结构(OTA测试) |
| | | *****************************************************************************/ |
| | | |
| | | typedef struct { |
| | | E_OTATestState state; // 当前测试状态 |
| | | HIDO_BOOL tcp_connected_triggered; // TCP连接后是否已触发下载 |
| | | HIDO_UINT32 last_check_tick; // 上次检查时间戳 |
| | | HIDO_UINT8 last_progress; // 上次进度 |
| | | } OTATestContext_t; |
| | | |
| | | /***************************************************************************** |
| | | * 私有全局变量(OTA测试) |
| | | *****************************************************************************/ |
| | | static OTATestContext_t s_ota_test_ctx = { |
| | | .state = OTA_TEST_STATE_IDLE, |
| | | .tcp_connected_triggered = HIDO_FALSE, |
| | | .last_check_tick = 0, |
| | | .last_progress = 0 |
| | | }; |
| | | |
| | | /***************************************************************************** |
| | | * 私有函数声明(OTA测试) |
| | | *****************************************************************************/ |
| | | static void OTATest_StartDownload(void); |
| | | static void OTATest_CheckDownloadStatus(void); |
| | | static void OTATest_CheckDecryptStatus(void); |
| | | static void OTATest_CheckVerifyStatus(void); |
| | | |
| | | /***************************************************************************** |
| | | * OTA测试函数实现 |
| | | *****************************************************************************/ |
| | | |
| | | /** |
| | | * @brief 初始化OTA升级测试模块 |
| | | */ |
| | | HIDO_INT32 OTATest_Init(void) |
| | | { |
| | | HIDO_INT32 ret; |
| | | |
| | | // 初始化OTA模块 |
| | | ret = OTA_Init(); |
| | | if (ret != HIDO_OK) { |
| | | HIDO_Debug("[OTATest] OTA_Init failed: %d\r\n", ret); |
| | | return ret; |
| | | } |
| | | |
| | | memset(&s_ota_test_ctx, 0, sizeof(s_ota_test_ctx)); |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_WAIT_NETWORK; |
| | | s_ota_test_ctx.tcp_connected_triggered = HIDO_FALSE; |
| | | |
| | | HIDO_Debug("[OTATest] ===== OTA Upgrade Test Initialized =====\r\n"); |
| | | HIDO_Debug("[OTATest] Waiting for TCP connection...\r\n"); |
| | | return HIDO_OK; |
| | | } |
| | | |
| | | /** |
| | | * @brief OTA测试主循环(需要在app_task中定期调用) |
| | | */ |
| | | void OTATest_Poll(void) |
| | | { |
| | | HIDO_UINT32 current_tick = HIDO_TimerGetTick(); |
| | | |
| | | // OTA模块需要周期调用(处理解密任务) |
| | | OTA_Poll(); |
| | | |
| | | // 限制检查频率 |
| | | if (current_tick - s_ota_test_ctx.last_check_tick < TEST_CHECK_INTERVAL) { |
| | | return; |
| | | } |
| | | s_ota_test_ctx.last_check_tick = current_tick; |
| | | |
| | | switch (s_ota_test_ctx.state) { |
| | | case OTA_TEST_STATE_WAIT_NETWORK: |
| | | // 等待TCP连接 |
| | | if (PathTest_IsTCPConnected() && !s_ota_test_ctx.tcp_connected_triggered) { |
| | | HIDO_Debug("[OTATest] TCP connected! Starting OTA firmware download test...\r\n"); |
| | | OTATest_StartDownload(); |
| | | s_ota_test_ctx.tcp_connected_triggered = HIDO_TRUE; |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_DOWNLOADING; |
| | | } |
| | | break; |
| | | |
| | | case OTA_TEST_STATE_DOWNLOADING: |
| | | // 检查下载状态 |
| | | OTATest_CheckDownloadStatus(); |
| | | break; |
| | | |
| | | case OTA_TEST_STATE_DECRYPTING: |
| | | // 检查解密状态 |
| | | OTATest_CheckDecryptStatus(); |
| | | break; |
| | | |
| | | case OTA_TEST_STATE_VERIFYING: |
| | | // 检查校验状态 |
| | | OTATest_CheckVerifyStatus(); |
| | | break; |
| | | |
| | | case OTA_TEST_STATE_COMPLETE: |
| | | // 测试完成,停止轮询 |
| | | HIDO_Debug("[OTATest] ===== OTA TEST COMPLETED SUCCESSFULLY =====\r\n"); |
| | | HIDO_Debug("[OTATest] Decrypted firmware is ready at 0x08140000\r\n"); |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_IDLE; |
| | | |
| | | // 恢复TCP连接 |
| | | extern HIDO_INT32 TCPClient_Resume(void); |
| | | TCPClient_Resume(); |
| | | break; |
| | | |
| | | case OTA_TEST_STATE_FAILED: |
| | | // 测试失败 |
| | | HIDO_Debug("[OTATest] ===== OTA TEST FAILED =====\r\n"); |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_IDLE; |
| | | |
| | | // 恢复TCP连接 |
| | | extern HIDO_INT32 TCPClient_Resume(void); |
| | | TCPClient_Resume(); |
| | | break; |
| | | |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @brief 启动OTA固件下载 |
| | | */ |
| | | static void OTATest_StartDownload(void) |
| | | { |
| | | const HIDO_CHAR *url = "http://123.57.87.125:7001/hfs/STM32H743.Bin"; |
| | | HIDO_INT32 ret; |
| | | |
| | | HIDO_Debug("[OTATest] ===== Starting OTA Firmware Download =====\r\n"); |
| | | HIDO_Debug("[OTATest] URL: %s\r\n", url); |
| | | |
| | | // 暂停TCP连接以停止差分数据接收 |
| | | extern HIDO_INT32 TCPClient_Pause(void); |
| | | TCPClient_Pause(); |
| | | |
| | | ret = OTA_StartDownload(url); |
| | | if (ret != HIDO_OK) { |
| | | HIDO_Debug("[OTATest] Failed to start download, error: %d\r\n", ret); |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_FAILED; |
| | | } else { |
| | | HIDO_Debug("[OTATest] Download started successfully\r\n"); |
| | | s_ota_test_ctx.last_progress = 0; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @brief 检查下载状态 |
| | | */ |
| | | static void OTATest_CheckDownloadStatus(void) |
| | | { |
| | | E_OTAState ota_state = OTA_GetState(); |
| | | HIDO_UINT8 progress = OTA_GetProgress(); |
| | | |
| | | // 进度变化时打印 |
| | | if (progress != s_ota_test_ctx.last_progress && progress % 10 == 0) { |
| | | HIDO_Debug("[OTATest] Download progress: %u%%\r\n", progress); |
| | | s_ota_test_ctx.last_progress = progress; |
| | | } |
| | | |
| | | if (ota_state == OTA_STATE_DOWNLOAD_COMPLETE) { |
| | | HIDO_Debug("[OTATest] Download completed! Starting decryption...\r\n"); |
| | | |
| | | // 启动解密 |
| | | if (OTA_StartDecrypt() == HIDO_OK) { |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_DECRYPTING; |
| | | s_ota_test_ctx.last_progress = 0; |
| | | } else { |
| | | HIDO_Debug("[OTATest] Failed to start decryption\r\n"); |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_FAILED; |
| | | } |
| | | } else if (ota_state == OTA_STATE_DOWNLOAD_FAILED) { |
| | | ST_OTAInfo info; |
| | | OTA_GetInfo(&info); |
| | | HIDO_Debug("[OTATest] Download failed, error code: %d\r\n", info.eLastError); |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_FAILED; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @brief 检查解密状态 |
| | | */ |
| | | static void OTATest_CheckDecryptStatus(void) |
| | | { |
| | | E_OTAState ota_state = OTA_GetState(); |
| | | HIDO_UINT8 progress = OTA_GetProgress(); |
| | | |
| | | // 进度变化时打印 |
| | | if (progress != s_ota_test_ctx.last_progress && progress % 10 == 0) { |
| | | HIDO_Debug("[OTATest] Decrypt progress: %u%%\r\n", progress); |
| | | s_ota_test_ctx.last_progress = progress; |
| | | } |
| | | |
| | | if (ota_state == OTA_STATE_DECRYPT_COMPLETE) { |
| | | HIDO_Debug("[OTATest] Decryption completed! Starting verification...\r\n"); |
| | | |
| | | // 启动校验 |
| | | if (OTA_VerifyFirmware() == HIDO_OK) { |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_VERIFYING; |
| | | } else { |
| | | HIDO_Debug("[OTATest] Failed to start verification\r\n"); |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_FAILED; |
| | | } |
| | | } else if (ota_state == OTA_STATE_DECRYPT_FAILED) { |
| | | ST_OTAInfo info; |
| | | OTA_GetInfo(&info); |
| | | HIDO_Debug("[OTATest] Decryption failed, error code: %d\r\n", info.eLastError); |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_FAILED; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @brief 检查校验状态 |
| | | */ |
| | | static void OTATest_CheckVerifyStatus(void) |
| | | { |
| | | E_OTAState ota_state = OTA_GetState(); |
| | | |
| | | if (ota_state == OTA_STATE_VERIFY_SUCCESS) { |
| | | ST_OTAInfo info; |
| | | OTA_GetInfo(&info); |
| | | |
| | | HIDO_Debug("[OTATest] ===== Firmware Verification SUCCESS! =====\r\n"); |
| | | HIDO_Debug("[OTATest] Firmware size: %u bytes (%.1f KB)\r\n", |
| | | info.u32TotalSize, info.u32TotalSize / 1024.0f); |
| | | HIDO_Debug("[OTATest] Encrypted firmware at: 0x%08X\r\n", OTA_ENCRYPTED_FLASH_ADDR); |
| | | HIDO_Debug("[OTATest] Decrypted firmware at: 0x%08X\r\n", OTA_DECRYPTED_FLASH_ADDR); |
| | | |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_COMPLETE; |
| | | } else if (ota_state == OTA_STATE_VERIFY_FAILED) { |
| | | ST_OTAInfo info; |
| | | OTA_GetInfo(&info); |
| | | HIDO_Debug("[OTATest] Firmware verification FAILED, error code: %d\r\n", info.eLastError); |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_FAILED; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @brief 手动触发OTA测试(可在Shell中调用) |
| | | */ |
| | | void OTATest_Trigger(void) |
| | | { |
| | | HIDO_Debug("[OTATest] Manual trigger - resetting test state\r\n"); |
| | | OTA_Reset(); |
| | | s_ota_test_ctx.state = OTA_TEST_STATE_WAIT_NETWORK; |
| | | s_ota_test_ctx.tcp_connected_triggered = HIDO_FALSE; |
| | | } |
| | | |
| | | /** |
| | | * @brief 获取OTA测试状态 |
| | | */ |
| | | E_OTATestState OTATest_GetState(void) |
| | | { |
| | | return s_ota_test_ctx.state; |
| | | } |