From 567085ead3f6adaabd884f16ab4b17c62e8f0403 Mon Sep 17 00:00:00 2001
From: yincheng.zhong <634916154@qq.com>
Date: 星期日, 21 十二月 2025 22:28:09 +0800
Subject: [PATCH] OTA升级功能调通,准备增加boot的代码

---
 STM32H743/APL/PathTest.c |  266 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 265 insertions(+), 1 deletions(-)

diff --git a/STM32H743/APL/PathTest.c b/STM32H743/APL/PathTest.c
index 1c5b65b..4a8182a 100644
--- a/STM32H743/APL/PathTest.c
+++ b/STM32H743/APL/PathTest.c
@@ -1,12 +1,13 @@
 /*****************************************************************************
 * @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"
@@ -353,3 +354,266 @@
 {
     return s_test_ctx.state;
 }
+
+/*****************************************************************************
+* OTA鍗囩骇娴嬭瘯鍔熻兘
+*****************************************************************************/
+
+/*****************************************************************************
+* 绉佹湁鏁版嵁缁撴瀯锛圤TA娴嬭瘯锛�
+*****************************************************************************/
+
+typedef struct {
+    E_OTATestState state;               // 褰撳墠娴嬭瘯鐘舵��
+    HIDO_BOOL tcp_connected_triggered;  // TCP杩炴帴鍚庢槸鍚﹀凡瑙﹀彂涓嬭浇
+    HIDO_UINT32 last_check_tick;        // 涓婃妫�鏌ユ椂闂存埑
+    HIDO_UINT8 last_progress;           // 涓婃杩涘害
+} OTATestContext_t;
+
+/*****************************************************************************
+* 绉佹湁鍏ㄥ眬鍙橀噺锛圤TA娴嬭瘯锛�
+*****************************************************************************/
+static OTATestContext_t s_ota_test_ctx = {
+    .state = OTA_TEST_STATE_IDLE,
+    .tcp_connected_triggered = HIDO_FALSE,
+    .last_check_tick = 0,
+    .last_progress = 0
+};
+
+/*****************************************************************************
+* 绉佹湁鍑芥暟澹版槑锛圤TA娴嬭瘯锛�
+*****************************************************************************/
+static void OTATest_StartDownload(void);
+static void OTATest_CheckDownloadStatus(void);
+static void OTATest_CheckDecryptStatus(void);
+static void OTATest_CheckVerifyStatus(void);
+
+/*****************************************************************************
+* OTA娴嬭瘯鍑芥暟瀹炵幇
+*****************************************************************************/
+
+/**
+ * @brief 鍒濆鍖朞TA鍗囩骇娴嬭瘯妯″潡
+ */
+HIDO_INT32 OTATest_Init(void)
+{
+    HIDO_INT32 ret;
+    
+    // 鍒濆鍖朞TA妯″潡
+    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妯″潡闇�瑕佸懆鏈熻皟鐢紙澶勭悊瑙e瘑浠诲姟锛�
+    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");
+        
+        // 鍚姩瑙e瘑
+        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娴嬭瘯锛堝彲鍦⊿hell涓皟鐢級
+ */
+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;
+}

--
Gitblit v1.10.0