增加dmp 鉴权认证
authorhcj <1224667075@qq.com>
Wed, 5 Nov 2025 07:26:30 +0000 (15:26 +0800)
committerhcj <1224667075@qq.com>
Wed, 5 Nov 2025 07:26:30 +0000 (15:26 +0800)
14 files changed:
code/sdk/build/config/target_config/brandy/config.py
code/sdk/zhihui/CMakeLists.txt
code/sdk/zhihui/ai/src/dev_authentication.c
code/sdk/zhihui/ai_old/ai_old_at_cmd.cpp
code/sdk/zhihui/config/include/ConfigModel.h
code/sdk/zhihui/config/include/ConfigModelC.h
code/sdk/zhihui/config/src/ConfigModel.cpp
code/sdk/zhihui/dmp/CMakeLists.txt [new file with mode: 0644]
code/sdk/zhihui/dmp/dmp_api.h [new file with mode: 0644]
code/sdk/zhihui/dmp/dmp_api_at.c [new file with mode: 0644]
code/sdk/zhihui/dmp/dmp_api_at.h [new file with mode: 0644]
code/sdk/zhihui/dmp/dmp_dev_authentication.c [new file with mode: 0644]
code/sdk/zhihui/dmp/dmp_http.c [new file with mode: 0644]
code/sdk/zhihui/imsl/src/vad_server.c

index 6509d8c..5525f3b 100644 (file)
@@ -1307,6 +1307,7 @@ target = {
                         #   "-:lwip",
                           "thunderbird_toys",#雷鸟
                           "leiniaoMediaService",
+                          "dmp",
                          #cat1 相关配置
                          #有方需要开启  PPP
                         #   "ppp",
index 8908022..7db8cfe 100644 (file)
@@ -31,3 +31,5 @@ add_subdirectory_if_exist(ai_new)
 add_subdirectory_if_exist(imsl)
 add_subdirectory_if_exist(minimp3)
 add_subdirectory_if_exist(shake)
+add_subdirectory_if_exist(dmp)
+
index e179342..c8554f1 100644 (file)
@@ -485,7 +485,7 @@ bool read_dev_token(char *data_token);
 //     sprintf(device_id_str,"%02X%02x%02X%02X%02X%02x",device_id[5],device_id[4],device_id[3],device_id[2],device_id[1],device_id[0]);
 // }
 
-uint32_t aut_login_time=0;
+static uint32_t aut_login_time=0;
 
 bool aut_login(void)//登录
 {
index 3e40cfd..7c97d45 100644 (file)
@@ -237,7 +237,7 @@ static uint32_t autlogin(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
         printf("CAT1TEST ATCMD Convert Command Failed\n");
         return ERRCODE_FAIL;
     }
-    pre_AI_init();
+    // pre_AI_init();
     return 0;
 }
 
index 41411b1..8130443 100644 (file)
@@ -156,6 +156,9 @@ namespace OHOS
 
             CONFIGMODEL_ITEM_DEV_ID,
 
+            // data_pass 保存项
+            CONFIGMODEL_ITEM_DMP_DATA_PASS,
+
             CONFIGMODEL_ITEM_MAX,
 
            
@@ -303,6 +306,10 @@ namespace OHOS
         void SetOpenAIAPIKeys(const char * value);
         std::string GetOpenAIAPIKeys(void);
 
+        // data_pass 存取
+        void SetDmpDataPass(const char *str);
+        std::string GetDmpDataPass(void);
+
         // flash light config data
         void GetFlashLightConfigData(uint8_t * ColorIndex, int64_t * ColorValue, uint8_t * FlashingLevel);
         void SetFlashLightConfigData(uint8_t ColorIndex, int64_t ColorValue, uint8_t FlashingLevel);
@@ -433,6 +440,9 @@ namespace OHOS
             {ConfigDataType::DATATYPE_INT,     "runtime_log_status",             "0"   },//CONFIGMODEL_ITEM_SAVE_RUNTIMR_LOG_STATUS,//运行日志保存
             {ConfigDataType::DATATYPE_STRING,     "ai_dev_id",       ""},//CONFIGMODEL_ITEM_DEV_ID///移动id
 
+            // data_pass key
+            {ConfigDataType::DATATYPE_STRING,  "dmp_data_pass",      ""},//CONFIGMODEL_ITEM_DMP_DATA_PASS
+
 
         };
         ConfigModel();                                        // 私有构造函数,确保无法在外部实例化
index 1004f82..52ce2ba 100644 (file)
@@ -14,6 +14,7 @@ extern "C" {
 #endif
 #endif
 #include <stdint.h>
+#include <stddef.h>
 
 /**
  * 从存档文件初始化配置信息
@@ -159,6 +160,20 @@ int set_cmai_dev_id(char *buf);
 
 int get_cmai_dev_id(char *buf);
 
+/* --------------------------- DMP data_pass 存取接口(C接口) --------------------------- */
+/**
+ * @brief 持久化保存 data_pass
+ */
+void cm_set_dmp_data_pass(const char *data_pass);
+
+/**
+ * @brief 读取已保存的 data_pass 到缓冲区
+ * @param buf  输出缓冲区
+ * @param buf_size 缓冲区大小
+ * @return 实际写入长度(不含\0),失败返回 0
+ */
+size_t cm_get_dmp_data_pass(char *buf, size_t buf_size);
+
 
 #ifdef __cplusplus
 #if __cplusplus
index 943f0d8..19822b9 100644 (file)
@@ -1052,6 +1052,17 @@ namespace OHOS
     {
         set_condigData_String(CONFIGMOFEL_ITEM_DMP_CHINA_MOBILE_ADDRESS,address);        
     }
+
+    void ConfigModel::SetDmpDataPass(const char *str)
+    {
+        set_condigData_String(CONFIGMODEL_ITEM_DMP_DATA_PASS, str);
+    }
+
+    std::string ConfigModel::GetDmpDataPass(void)
+    {
+        return get_configData_string(CONFIGMODEL_ITEM_DMP_DATA_PASS);
+    }
+
     /*
      * @brief 设置震动模式强度
      * @param 马达 开关,3 强,2 中,1 弱,0 关
@@ -1689,6 +1700,35 @@ int get_cmai_dev_id(char *buf)
     return 0;
 }
 
+void cm_set_dmp_data_pass(const char *data_pass)
+{
+    if (data_pass == nullptr) {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "cm_set_dmp_data_pass: input string is null");
+        return;
+    }
+    OHOS::ConfigModel::GetInstance()->SetDmpDataPass(data_pass);
+    WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "cm_set_dmp_data_pass: data_pass saved");
+}
+
+size_t cm_get_dmp_data_pass(char *buf, size_t buf_size)
+{
+    if (buf == nullptr || buf_size == 0) {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "cm_get_dmp_data_pass: invalid buffer parameters");
+        return 0;
+    }
+    std::string data_pass_std = OHOS::ConfigModel::GetInstance()->GetDmpDataPass();
+    if (data_pass_std.empty()) {
+        WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "cm_get_dmp_data_pass: no data_pass found in config");
+        buf[0] = '\0';
+        return 0;
+    }
+    size_t copy_len = (data_pass_std.length() < buf_size - 1) ? data_pass_std.length() : (buf_size - 1);
+    memcpy(buf, data_pass_std.c_str(), copy_len);
+    buf[copy_len] = '\0';
+    WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "cm_get_dmp_data_pass: data_pass loaded");
+    return copy_len;
+}
+
 
 
 #ifdef __cplusplus
diff --git a/code/sdk/zhihui/dmp/CMakeLists.txt b/code/sdk/zhihui/dmp/CMakeLists.txt
new file mode 100644 (file)
index 0000000..b471ae0
--- /dev/null
@@ -0,0 +1,59 @@
+#===============================================================================
+# @brief    cmake file
+# Copyright (c) @CompanyNameMagicTag 2022-2022. All rights reserved.
+#===============================================================================
+
+set(COMPONENT_NAME "dmp")
+
+set(PUBLIC_HEADER
+${CMAKE_CURRENT_SOURCE_DIR}
+)
+set(PRIVATE_HEADER
+${ROOT_DIR}/open_source/openharmony/third_party/cJSON
+${ROOT_DIR}/open_source/openharmony/foundation/ace/ace_engine_lite/modules/netstack/websocket/include
+${ROOT_DIR}/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/include
+${ROOT_DIR}/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/include
+${ROOT_DIR}/open_source/openharmony/foundation/ace/ace_engine_lite/modules/netstack/http/include
+)
+
+set(SOURCES
+${CMAKE_CURRENT_SOURCE_DIR}/dmp_dev_authentication.c
+${CMAKE_CURRENT_SOURCE_DIR}/dmp_http.c
+${CMAKE_CURRENT_SOURCE_DIR}/dmp_api_at.c
+
+)
+
+set(COMPONENT_PUBLIC_CCFLAGS
+)
+
+set(COMPONENT_CCFLAGS
+    -Wno-error
+    -Wno-unused-parameter
+    -fno-exceptions
+    -fno-unwind-tables
+    -fno-asynchronous-unwind-tables
+    -Wno-undef
+    -Wno-bitwise-op-parentheses
+    -Wno-shift-op-parentheses
+    -nostdlibinc
+    -nostdinc
+    -nostdlib
+    -Wno-error=unused-function
+)
+
+set(WHOLE_LINK
+    true
+)
+
+set(MAIN_COMPONENT
+    false
+)
+
+set(PUBLIC_DEFINES
+SUPPORT_ZHIHUI_DMP
+)
+
+set(PRIVATE_DEFINES
+)
+
+build_component()
diff --git a/code/sdk/zhihui/dmp/dmp_api.h b/code/sdk/zhihui/dmp/dmp_api.h
new file mode 100644 (file)
index 0000000..98f33dd
--- /dev/null
@@ -0,0 +1,17 @@
+
+#ifndef DMP_HTTP_API
+#define DMP_HTTP_API
+
+int dmp_http_put(char *post_buf,char *url,char *put_buf);
+
+int dmp_http_post(char *post_buf,char *url,char *put_buf);
+
+int dmp_http_get(char *post_buf,char *url,char *put_buf);
+
+bool dmp_aut_active(void);
+
+bool dmp_aut_login(void);
+
+bool dmp_read_dev_token(char *outdata);
+
+#endif
\ No newline at end of file
diff --git a/code/sdk/zhihui/dmp/dmp_api_at.c b/code/sdk/zhihui/dmp/dmp_api_at.c
new file mode 100644 (file)
index 0000000..5cdc3cb
--- /dev/null
@@ -0,0 +1,173 @@
+#include "at_cmd_api.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "dmp_api.h"
+
+#define DMP_AT_MAX_ARGC 8
+#define DMP_AT_MAX_ARGV_LEN 512
+
+#define DMP_AT_FAILED (-1)
+#define DMP_AT_SUCCESS (0)
+
+#define DMP_UNUSED(a) ((void)(a))
+
+static int dmp_convert_cmd_to_param(uint8_t *para, uint32_t paraLen,
+                                    uint8_t argv[DMP_AT_MAX_ARGC][DMP_AT_MAX_ARGV_LEN],
+                                    uint32_t *argc)
+{
+    uint32_t pos = 0;
+    *argc = 0;
+
+    for (uint32_t i = 0; i < paraLen; i++)
+    {
+        if ((para[i] == ' ' || para[i] == '\n' || para[i] == ',' || para[i] == '=') && pos != 0)
+        {
+            argv[*argc][pos] = '\0';
+            *argc += 1;
+            pos = 0;
+            if (*argc >= DMP_AT_MAX_ARGC)
+            {
+                return DMP_AT_FAILED;
+            }
+        }
+        else
+        {
+            if (para[i] == ' ' || para[i] == '\n' || para[i] == ',' || para[i] == '=') {
+                continue;
+            }
+            argv[*argc][pos] = para[i];
+            pos++;
+            if (pos >= DMP_AT_MAX_ARGV_LEN)
+            {
+                return DMP_AT_FAILED;
+            }
+        }
+    }
+    if (pos != 0)
+    {
+        argv[*argc][pos] = '\0';
+        *argc += 1;
+    }
+
+    return DMP_AT_SUCCESS;
+}
+
+/* ============================== DMP HTTP AT 命令实现 ============================== */
+
+/* AT^DMP_HTTP_GET=url */
+static uint32_t dmp_http_get_at(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
+{
+    DMP_UNUSED(strCommonCmd);
+    if (para == NULL || paraLen == 0) {
+        printf("Usage: AT^DMP_HTTP_GET=<url>\n");
+        return ERRCODE_FAIL;
+    }
+
+    uint8_t argv[DMP_AT_MAX_ARGC][DMP_AT_MAX_ARGV_LEN] = {0};
+    uint32_t argc = 0;
+    if (dmp_convert_cmd_to_param(para, paraLen, argv, &argc) != DMP_AT_SUCCESS || argc < 1) {
+        printf("ERROR: Invalid params for DMP_HTTP_GET\n");
+        return ERRCODE_FAIL;
+    }
+    char *url = (char*)argv[0];
+
+    char empty[1] = {0};
+    int ret = dmp_http_get(empty, url, empty);
+    printf("[DMP][HTTP_GET] url=%s, ret=%d\n", url, ret);
+    return (ret == 0) ? ERRCODE_SUCC : ERRCODE_FAIL;
+}
+
+/* AT^DMP_HTTP_POST=url,body */
+static uint32_t dmp_http_post_at(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
+{
+    DMP_UNUSED(strCommonCmd);
+    if (para == NULL || paraLen == 0) {
+        printf("Usage: AT^DMP_HTTP_POST=<url>,<body>\n");
+        return ERRCODE_FAIL;
+    }
+
+    uint8_t argv[DMP_AT_MAX_ARGC][DMP_AT_MAX_ARGV_LEN] = {0};
+    uint32_t argc = 0;
+    if (dmp_convert_cmd_to_param(para, paraLen, argv, &argc) != DMP_AT_SUCCESS || argc < 2) {
+        printf("ERROR: Invalid params for DMP_HTTP_POST\n");
+        return ERRCODE_FAIL;
+    }
+    char *url = (char*)argv[0];
+    char *body = (char*)argv[1];
+
+    int ret = dmp_http_post(body, url, body);
+    printf("[DMP][HTTP_POST] url=%s, body_len=%u, ret=%d\n", url, (unsigned)strlen(body), ret);
+    return (ret == 0) ? ERRCODE_SUCC : ERRCODE_FAIL;
+}
+
+/* AT^DMP_HTTP_PUT=url,body */
+static uint32_t dmp_http_put_at(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
+{
+    DMP_UNUSED(strCommonCmd);
+    if (para == NULL || paraLen == 0) {
+        printf("Usage: AT^DMP_HTTP_PUT=<url>,<body>\n");
+        return ERRCODE_FAIL;
+    }
+
+    uint8_t argv[DMP_AT_MAX_ARGC][DMP_AT_MAX_ARGV_LEN] = {0};
+    uint32_t argc = 0;
+    if (dmp_convert_cmd_to_param(para, paraLen, argv, &argc) != DMP_AT_SUCCESS || argc < 2) {
+        printf("ERROR: Invalid params for DMP_HTTP_PUT\n");
+        return ERRCODE_FAIL;
+    }
+    char *url = (char*)argv[0];
+    char *body = (char*)argv[1];
+
+    int ret = dmp_http_put(body, url, body);
+    printf("[DMP][HTTP_PUT] url=%s, body_len=%u, ret=%d\n", url, (unsigned)strlen(body), ret);
+    return (ret == 0) ? ERRCODE_SUCC : ERRCODE_FAIL;
+}
+
+/* AT^DMP_AUT_ACTIVE - 激活认证 */
+static uint32_t dmp_aut_active_at(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
+{
+    DMP_UNUSED(para);
+    DMP_UNUSED(paraLen);
+    DMP_UNUSED(strCommonCmd);
+    
+    printf("[DMP][AUT_ACTIVE] Calling dmp_aut_active()...\n");
+    bool result = dmp_aut_active();
+    printf("[DMP][AUT_ACTIVE] result=%s\n", result ? "true" : "false");
+    return result ? ERRCODE_SUCC : ERRCODE_FAIL;
+}
+
+/* AT^DMP_AUT_LOGIN - 登录认证 */
+static uint32_t dmp_aut_login_at(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
+{
+    DMP_UNUSED(para);
+    DMP_UNUSED(paraLen);
+    DMP_UNUSED(strCommonCmd);
+    
+    printf("[DMP][AUT_LOGIN] Calling dmp_aut_login()...\n");
+    bool result = dmp_aut_login();
+    printf("[DMP][AUT_LOGIN] result=%s\n", result ? "true" : "false");
+    return result ? ERRCODE_SUCC : ERRCODE_FAIL;
+}
+
+/* 命令表导出 */
+static const at_cmd_table_t g_dmp_api_cmd[] = {
+    {(char *)"AT^DMP_HTTP_GET",  dmp_http_get_at,  (char *)"DMP HTTP GET: AT^DMP_HTTP_GET=<url>"},
+    {(char *)"AT^DMP_HTTP_POST", dmp_http_post_at, (char *)"DMP HTTP POST: AT^DMP_HTTP_POST=<url>,<body>"},
+    {(char *)"AT^DMP_HTTP_PUT",  dmp_http_put_at,  (char *)"DMP HTTP PUT: AT^DMP_HTTP_PUT=<url>,<body>"},
+    {(char *)"AT^DMP_AUT_ACTIVE", dmp_aut_active_at, (char *)"DMP 激活认证: AT^DMP_AUT_ACTIVE"},
+    {(char *)"AT^DMP_AUT_LOGIN", dmp_aut_login_at, (char *)"DMP 登录认证: AT^DMP_AUT_LOGIN"},
+};
+
+at_cmd_table_t *get_dmp_api_cmd_table(uint32_t *cmd_count)
+{
+    if (cmd_count) {
+        *cmd_count = sizeof(g_dmp_api_cmd) / sizeof(g_dmp_api_cmd[0]);
+    }
+    return (at_cmd_table_t *)g_dmp_api_cmd;
+}
+
+
diff --git a/code/sdk/zhihui/dmp/dmp_api_at.h b/code/sdk/zhihui/dmp/dmp_api_at.h
new file mode 100644 (file)
index 0000000..e06897d
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef DMP_API_AT_H
+#define DMP_API_AT_H
+
+#include <stdint.h>
+#include "at_cmd_api.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/* DMP API AT 指令表导出 */
+at_cmd_table_t *get_dmp_api_cmd_table(uint32_t *cmd_count);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* DMP_API_AT_H */
+
diff --git a/code/sdk/zhihui/dmp/dmp_dev_authentication.c b/code/sdk/zhihui/dmp/dmp_dev_authentication.c
new file mode 100644 (file)
index 0000000..ec04cc5
--- /dev/null
@@ -0,0 +1,796 @@
+//设备鉴权
+
+#define AIC_THIS_FILE    0x888899
+// #include <aic_errno.h>
+// #include <aic_init.h>
+// #include <aic_osa_ex.h>
+// // #include <aos_thread.h>
+// #include "app_msg.h"
+// #include "aic_net_pdp_mgr.h"
+// #include "aic_network_pcap.h"
+
+#include "cJSON.h"
+// #include <serial_inc.h>
+// #include "aic_osa.h"
+// #include "AI_AMP_2_5.h"
+#include "mbedtls/base64.h"
+#include "mbedtls/md5.h"
+#include <stdio.h>
+#include <string.h>
+#include "mbedtls/aes.h"
+#include "mbedtls/cipher.h"
+#include "dev_authentication.h"
+#include "bts_br_gap.h"
+#include "custom_utils.h"
+#include "wearable_log.h"
+#include "httpclient.h"
+#include "dmp_api.h"
+#if DMP_DEVICE_ID_SOURCE == 1 || DMP_DEVICE_ID_SOURCE == 2
+#include "ppp_cat1_api.h"
+#endif
+#include "ConfigModelC.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#define HTTP_JSON_SERVER_NAME "http://app-api.raisound.com"//"test.app.api.thisiot.com"//获取升级服务器
+#define PORT (80)// (80)http端口号  (443)//https端口号
+#define PUT_URL "/dev/auth/active"//激活接口
+#define POST_URL "/dev/auth/sign_in"//登录接口
+
+/* DeviceId 来源选择宏
+ * 0: 使用蓝牙地址 (bluetooth_get_local_addr)
+ * 1: 使用 IMEI (cat1_api_runtime_get_cat1_imei)
+ * 2: 使用 ICCID (cat1_api_runtime_get_cat1_iccid)
+ */
+#ifndef DMP_DEVICE_ID_SOURCE
+#define DMP_DEVICE_ID_SOURCE 2  // 默认使用蓝牙地址
+#endif
+
+static char data_pass[128]={0};
+static char data_token[128]={0};
+
+static uint32_t aut_login_time=0;
+
+
+void dmp_ase_encrypt_demo(void);
+bool dmp_read_dev_pass_save(char *buf);
+bool dmp_read_dev_token_save(char *indata);
+
+#define DMP_PID "cqxJZi5CNgzjS3mX21Zw"
+#define DMP_KEY  "uKLrChwrxA1LklgBc3VwawYjUkbT3BgX"
+// 
+// 
+bool dmp_read_dev_token(char *outdata)
+{
+    time_t timestamp;
+    timestamp = time(NULL);
+    if(aut_login_time==0 || timestamp - aut_login_time > 60 * 60 )
+    {
+        return false;
+    }
+     memcpy(outdata,data_token,strlen(data_token));
+     return true;
+}
+
+
+
+
+static char aut_token[50] ={0};
+
+
+//激活用的md5签名
+static int dmp_active_md5_sign(const char *dev_id,const char *pid,const char *timestamp,const char *productSecret,char*md5_str)
+{
+    if(strlen(dev_id)>256)
+        return -1;
+    if(strlen(pid)>256)
+        return -2; 
+    if(strlen(productSecret)>256)
+        return -3;
+    if(strlen(timestamp)>256)
+        return -4;
+    char md5_buf[16]={0};
+    char *sign_buf = (char *)malloc(1024);
+    
+    if(!sign_buf)
+        return -10;
+    memset(sign_buf,0,1024);
+    int sign_len = 0;
+    strcpy(sign_buf,dev_id);
+    sign_len = strlen(sign_buf);
+
+    strcpy(sign_buf+sign_len,pid);
+    sign_len = strlen(sign_buf);
+
+    strcpy(sign_buf+sign_len,timestamp);
+    sign_len = strlen(sign_buf);
+
+    strcpy(sign_buf+sign_len,productSecret);
+    sign_len = strlen(sign_buf);
+    printf("sign_buf:%s\r\n",sign_buf);
+   
+
+    mbedtls_md5_context ctx;
+
+    // 初始化MD5上下文
+    mbedtls_md5_init(&ctx);
+
+    // 开始计算MD5哈希
+    mbedtls_md5_starts(&ctx);
+    // 更新要计算哈希的数据
+    mbedtls_md5_update(&ctx, (const unsigned char *)sign_buf, sign_len);
+
+    // 完成哈希计算
+    mbedtls_md5_finish(&ctx, (unsigned char *)md5_buf);
+
+    // printf("md5_buf\r\n");
+    // for (int i = 0; i < 16; i++) {
+    //     printf("%02X\n", (uint8_t)md5_buf[i]);
+    // }
+    // printf("md5_buf end\r\n");
+
+    // 清理MD5上下文
+    mbedtls_md5_free(&ctx);
+    for (int i = 0; i < 16; i++) {
+        sprintf(&md5_str[i * 2], "%02x", (uint8_t)md5_buf[i]);
+    }
+    md5_str[32] = '\0';
+    
+    free(sign_buf);
+    return 0;
+}
+
+
+#define AES_BLOCK_SIZE 16
+// PKCS5填充
+static void dmp_pkcs5_padding(unsigned char *data, size_t data_len, size_t block_size) {
+    size_t padding_len = block_size - (data_len % block_size);
+    for (size_t i = 0; i < padding_len; i++) {
+        data[data_len + i] = (unsigned char)padding_len;
+    }
+}
+// 移除PKCS5填充
+static size_t dmp_pkcs5_unpadding(unsigned char *data, size_t data_len,size_t block_size) {
+    size_t padding_len = data[data_len - 1];
+    if (padding_len > 0 && padding_len <= block_size) {
+        for (size_t i = 0; i < padding_len; i++) {
+            if (data[data_len - 1 - i] != (unsigned char)padding_len) {
+                return data_len;
+            }
+        }
+        return data_len - padding_len;
+    }
+    return data_len;
+}
+
+//登录用的aes256加密
+static int dmp_aes_cbc_pkcs5_encrypt(const char *input,int input_len,const char* str_key,const char* str_iv,char *Base64_output,int *Base64_output_len)
+{
+
+    mbedtls_cipher_context_t ctx;
+    int ret;
+    unsigned char *aes_data =NULL;
+    unsigned char *padded_plaintext=NULL;
+    uint32_t aes_data_len = 0;
+    uint32_t padded_len;
+    // 初始化上下文
+    mbedtls_cipher_init(&ctx);
+
+    // 设置加密算法
+    ret = mbedtls_cipher_setup(&ctx, mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CBC));
+    if (ret != 0) {
+        printf("mbedtls_cipher_setup failed: -0x%04x\n", -ret);
+        goto exit;
+    }
+
+    // 设置密钥和IV
+    ret = mbedtls_cipher_setkey(&ctx, ( const unsigned char *)str_key, strlen(str_key) * 8, MBEDTLS_ENCRYPT);
+    if (ret != 0) {
+        printf("mbedtls_cipher_setkey failed: -0x%04x\n", -ret);
+        goto exit;
+    }
+
+    ret = mbedtls_cipher_set_iv(&ctx, ( const unsigned char *)str_iv, strlen(str_iv));
+    if (ret != 0) {
+        printf("mbedtls_cipher_set_iv failed: -0x%04x\n", -ret);
+        goto exit;
+    }
+    //printf("aes_cbc_pkcs5_encrypt2");
+    // 计算填充后的长度
+    padded_len = input_len;
+    if (padded_len % AES_BLOCK_SIZE != 0) {
+        padded_len += AES_BLOCK_SIZE - (padded_len % AES_BLOCK_SIZE);
+    }else
+        padded_len+=AES_BLOCK_SIZE;
+    //printf("aes_cbc_pkcs5_encrypt3");
+    // 复制明文并进行填充
+    padded_plaintext = (unsigned char *)malloc(padded_len);
+    if (padded_plaintext == NULL) {
+        ret = MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
+        printf("Memory allocation failed\n");
+        goto exit;
+    }
+    //printf("aes_cbc_pkcs5_encrypt4 padded_len :%d,input_len:%d",padded_len,input_len);
+    aes_data = (unsigned char *)malloc(padded_len);
+    if (aes_data == NULL) {
+        ret = MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
+        printf("Memory allocation failed\n");
+        goto exit;
+    }
+    //printf("aes_cbc_pkcs5_encrypt5");
+    memcpy(padded_plaintext, input, input_len);
+    dmp_pkcs5_padding(padded_plaintext, input_len, AES_BLOCK_SIZE);
+    printf("aes_cbc_pkcs5_encrypt6");
+    // 加密
+    ret = mbedtls_cipher_update(&ctx, padded_plaintext, padded_len, aes_data, &aes_data_len);
+    if (ret != 0) {
+        printf("mbedtls_cipher_update failed: -0x%04x\n", -ret);
+        
+        goto exit;
+    }
+    printf("aes_cbc_pkcs5_encrypt7");
+    mbedtls_base64_encode((unsigned char *)Base64_output, *Base64_output_len,(size_t *) Base64_output_len, aes_data, aes_data_len);
+    Base64_output[*Base64_output_len] = 0;
+    printf("aes_cbc_pkcs5_encrypt8.");
+    free(padded_plaintext);
+    printf("aes_cbc_pkcs5_encrypt9");
+    free(aes_data);
+    
+    // 清理上下文
+    mbedtls_cipher_free(&ctx);
+    return 0;
+
+exit:
+    mbedtls_cipher_free(&ctx);
+    if(aes_data)
+        free(aes_data);
+    if(padded_plaintext)
+        free(padded_plaintext);
+    return ret;
+}
+// AES/CBC/PKCS5Padding解密
+static int dmp_aes_cbc_pkcs5_decrypt(const char *Base64_str_input,const char* str_key,const char* str_iv,char *output,int *output_len)
+    // const unsigned char *key, size_t key_len,
+    // const unsigned char *iv, size_t iv_len,
+    // const unsigned char *ciphertext, size_t ciphertext_len,
+    // unsigned char *plaintext, size_t *plaintext_len) 
+{
+    mbedtls_aes_context ctx = {0};
+    int ret;
+    char *input_buf = NULL;
+    size_t input_buf_len;
+      char vi_tmp[16];
+    // 初始化上下文
+    mbedtls_aes_init(&ctx);
+    //printf("aes_cbc_pkcs5_decrypt str_key:%s,str_iv:%s!!!!!!!!!!!!!!!!!!!!!!!!", str_key,str_iv);
+    // // 设置加密算法
+    // ret = mbedtls_cipher_setup(&ctx, mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CBC));
+    // if (ret != 0) {
+    //     printf("mbedtls_cipher_setup failed: -0x%04x\n", -ret);
+    //     goto exit;
+    // }
+    
+    // 设置密钥和IV
+    ret = mbedtls_aes_setkey_dec(&ctx, (const unsigned char*)str_key, strlen(str_key) * 8);
+    if (ret != 0) {
+        printf("mbedtls_cipher_setkey failed: -0x%04x\n", -ret);
+        goto exit;
+    }
+
+    // ret = mbedtls_cipher_set_iv(&ctx, str_iv, strlen(str_iv));
+    // if (ret != 0) {
+    //     printf("mbedtls_cipher_set_iv failed: -0x%04x\n", -ret);
+    //     goto exit;
+    // }
+    input_buf_len = strlen(Base64_str_input)+1;
+    input_buf = (char *)malloc(input_buf_len);
+    memset(input_buf,0,input_buf_len);
+    //printf("mbedtls_base64_decode1:input_buf_len:%d,Base64_str_input:%s",input_buf_len,Base64_str_input);
+    mbedtls_base64_decode((unsigned char *)input_buf, input_buf_len, &input_buf_len,( const unsigned char *) Base64_str_input, strlen(Base64_str_input));
+    //printf("mbedtls_base64_decode2:input_buf_len:%d,output_len:%d",input_buf_len,*output_len);
+    // bk_print_hex(input_buf,input_buf_len);
+    // 解密
+
+    
+    memcpy(vi_tmp,str_iv,16);
+    ret = mbedtls_aes_crypt_cbc(&ctx,MBEDTLS_AES_DECRYPT,input_buf_len,(unsigned char *) vi_tmp,( const unsigned char *)input_buf,( unsigned char *)output);
+    *output_len = input_buf_len;
+
+    if (ret != 0) {
+        printf("mbedtls_cipher_update failed: -0x%04x\n", -ret);
+        goto exit;
+    }
+
+    // 清理上下文
+    mbedtls_aes_free(&ctx);
+    //printf("aes_cbc_pkcs5_decrypt output_len:%d output:%s",*output_len,output);
+    // 移除填充
+    *output_len = dmp_pkcs5_unpadding((unsigned char *)output, *output_len,AES_BLOCK_SIZE);
+    output[*output_len] = 0;
+    if(input_buf)
+        free(input_buf);
+    return 0;
+
+    exit:
+    if(input_buf)
+        free(input_buf);
+    mbedtls_cipher_free((mbedtls_cipher_context_t *)&ctx);
+    return ret;
+}
+
+/****************************************************************************** */
+static int dmp_set_token(char *token)
+{
+    if(strlen(token)>=sizeof(aut_token))
+    {
+        printf("ERROR token to0 long");
+        return -1;
+    }
+    memset(aut_token,0,sizeof(aut_token));
+    strcpy(aut_token,token);
+    return 0;     
+}
+
+static char * dmp_get_token(char *token)
+{
+
+    return aut_token;   
+}
+
+
+//激活
+
+
+// 设备激活
+
+
+//激活->登录的密钥
+// const char * key = "4EepovakpbqhZUnUDwH42KAADYGv4UUe" ;
+//每小时更新登录一次的更新定时器
+// static aos_timer_ptr LOGIN_1H_timer = NULL;
+
+bool dmp_aut_active(void)//设备激活
+{
+    int ret;
+    printf("aut_active");
+    // AIC_HTTPS_CLIENT_INSTANCE *http_client;
+    char dev_pass_base64[100] ={0};
+    char enc_iv[65] = {0};
+    // http创建客户端句柄
+
+    char *json_string =NULL;
+    cJSON *root = NULL;
+
+
+    unsigned char deviceId[6] = {0};
+    unsigned int len = 6;
+
+    char deviceId_temp[20] = {0};
+
+#if DMP_DEVICE_ID_SOURCE == 0
+    // 使用蓝牙地址
+    memset(deviceId, 0, 6);    
+    bluetooth_get_local_addr(&deviceId[0], len);
+    sprintf(deviceId_temp, "%02X%02X%02X%02X%02X%02X", 
+            deviceId[5], deviceId[4], deviceId[3], deviceId[2], deviceId[1], deviceId[0]);
+#elif DMP_DEVICE_ID_SOURCE == 1
+    // 使用 IMEI
+    char *imei = cat1_api_runtime_get_cat1_imei();
+    if (imei != NULL && strlen(imei) > 0) {
+        strncpy(deviceId_temp, imei, sizeof(deviceId_temp) - 1);
+        deviceId_temp[sizeof(deviceId_temp) - 1] = '\0';
+    } else {
+        sprintf(deviceId_temp, "%s", "898604B719259031337"); // 默认值
+    }
+#elif DMP_DEVICE_ID_SOURCE == 2
+    // 使用 ICCID
+    char *iccid = cat1_api_runtime_get_cat1_iccid();
+    if (iccid != NULL && strlen(iccid) > 0) {
+        strncpy(deviceId_temp, iccid, sizeof(deviceId_temp) - 1);
+        deviceId_temp[sizeof(deviceId_temp) - 1] = '\0';
+    } else {
+        sprintf(deviceId_temp, "%s", "898604B719259031337"); // 默认值
+    }
+#else
+    // 默认使用固定值
+    sprintf(deviceId_temp, "%s", "898604B719259031337");
+#endif
+
+
+     time_t timestamp;
+    timestamp = time(NULL);
+    printf("2时间戳: %ld\n", (long)timestamp);
+    long long timestamp_ms = (long long)timestamp * 1000;
+
+    char* timestamp_str_2=(char *)malloc(20);
+    memset(timestamp_str_2,0,20);
+
+    // 使用 sprintf 将 long long 类型的时间戳转换为字符串
+    ret = sprintf(timestamp_str_2, "%lld", timestamp_ms);
+    // 打印转换后的字符串形式的时间戳
+
+    // const char *dev_key = "4EepovakpbqhZUnUDwH42KAADYGv4UUe";//密钥
+
+    char md5_buf[33]={0};
+    dmp_active_md5_sign((char *)deviceId_temp,DMP_PID,timestamp_str_2,DMP_KEY,md5_buf);
+    printf("md5_buf:%s",md5_buf);
+
+
+    const char *signMethod ="MD5";
+
+    uint32_t response = 0;
+    //创建客户端实例
+
+        
+    // 创建JSON对象
+    root = cJSON_CreateObject();
+    if (root == NULL) {
+        printf( "Failed to create JSON object\n");
+        return  false ;
+    }
+
+    // 添加字段
+    cJSON_AddStringToObject(root, "deviceId", (char *)deviceId_temp);
+    cJSON_AddStringToObject(root, "pid", DMP_PID);
+    cJSON_AddStringToObject(root, "sign", md5_buf);
+
+    printf("时间戳(ms 转换为字符串): %s\n", timestamp_str_2);
+
+    cJSON_AddStringToObject(root, "timeStamp", timestamp_str_2);
+
+    free(timestamp_str_2);
+
+    cJSON_AddStringToObject(root, "signMethod", signMethod);
+
+    // 生成JSON字符串
+    json_string = cJSON_Print(root);
+    if (json_string == NULL) {
+        printf("Failed to print JSON\n");
+        cJSON_Delete(root);
+        return false;
+    }
+
+    // 输出JSON
+    printf("JSON output: %s\n", json_string);
+
+    // extern uint32_t put_data_by_get_file(char *data,uint16_t data_len);
+   
+    // if(200==put_data_by_get_file(json_string,strlen(json_string)))
+    // {
+    //     cJSON_free(json_string);
+    //     return true;    
+    // }
+    // else
+    // {
+    //     cJSON_free(json_string);
+    //     return false;
+    // }
+
+//    HTTPC_RESULT HttpClientPutRequest(HttpClient *client, const char *url, HttpClientData *clientData);
+
+    char out_buf[1024]={0};
+
+     ret= dmp_http_put(json_string,HTTP_JSON_SERVER_NAME PUT_URL,out_buf);
+
+    if(ret==200)
+    {   
+        dmp_read_dev_pass_save(out_buf);
+    }
+
+    cJSON_free(json_string);
+    return true;  
+
+}
+
+
+extern uint32_t post_data_by_get_file(char *data,uint16_t data_len);
+
+
+
+
+bool dmp_aut_login(void)//登录
+{
+     time_t timestamp;
+    timestamp = time(NULL);
+
+    if(timestamp - aut_login_time < 60 * 60 )
+    {
+        return true;
+    }
+
+    char dev_pass[64]={0};  
+    // 若内存中无 data_pass,则尝试从配置中加载
+    if (strlen(data_pass) == 0) {
+        (void)cm_get_dmp_data_pass(data_pass, sizeof(data_pass));
+    }
+    memcpy(dev_pass, data_pass, strlen(data_pass));
+    printf("dev_sign_in 2");
+    int count = 5;
+    char *encrypt_data = NULL;
+    char *response_data =NULL;
+    int aut_login_http_handle = 0;
+    char enc_iv[17]={0};
+   
+    // 随机生成16个字符串 设备生成做加密使用
+    //char *random_string= generate_random_string(); // 随机的16个字符串
+    // 设备端进行密码加密
+    /**
+     * password, productSecret, nonce utJMIAPRN1E81DzSDVigh/f0ZmHQdBgdAGZ/DhkPJ/s=   uKLrChwrxA1LklgBc3VwawYjUkbT3BgX   qgofeplofqeoadha
+     */
+    int encrypt_data_len = 512;
+    encrypt_data = (char *)malloc(encrypt_data_len);
+    
+
+    // 登录数据
+    unsigned char deviceId[6] = {0};
+    char deviceId_temp[20] = {0};
+
+#if DMP_DEVICE_ID_SOURCE == 0
+    // 使用蓝牙地址
+    memset(deviceId, 0, 6);    
+    bluetooth_get_local_addr(&deviceId[0], 6);
+    sprintf(deviceId_temp, "%02X%02X%02X%02X%02X%02X", 
+            deviceId[5], deviceId[4], deviceId[3], deviceId[2], deviceId[1], deviceId[0]);
+#elif DMP_DEVICE_ID_SOURCE == 1
+    // 使用 IMEI
+    char *imei = cat1_api_runtime_get_cat1_imei();
+    if (imei != NULL && strlen(imei) > 0) {
+        strncpy(deviceId_temp, imei, sizeof(deviceId_temp) - 1);
+        deviceId_temp[sizeof(deviceId_temp) - 1] = '\0';
+    } else {
+        sprintf(deviceId_temp, "%s", "898604B719259031337"); // 默认值
+    }
+#elif DMP_DEVICE_ID_SOURCE == 2
+    // 使用 ICCID
+    char *iccid = cat1_api_runtime_get_cat1_iccid();
+    if (iccid != NULL && strlen(iccid) > 0) {
+        strncpy(deviceId_temp, iccid, sizeof(deviceId_temp) - 1);
+        deviceId_temp[sizeof(deviceId_temp) - 1] = '\0';
+    } else {
+        sprintf(deviceId_temp, "%s", "898604B719259031337"); // 默认值
+    }
+#else
+    // 默认使用固定值
+    sprintf(deviceId_temp, "%s", "898604B719259031337");
+#endif
+
+   
+    printf("2时间戳: %ld\n", (long)timestamp);
+    long long timestamp_ms = (long long)timestamp * 1000;
+    char timestamp_str[20] ={0};
+
+    // 使用 sprintf 将 long long 类型的时间戳转换为字符串
+    sprintf(timestamp_str, "%lld", timestamp_ms);
+    memset(enc_iv,'1',16);
+    memcpy(enc_iv,timestamp_str,strlen(timestamp_str));
+
+    enc_iv[16] = 0;
+    dmp_aes_cbc_pkcs5_encrypt(dev_pass, strlen(dev_pass), DMP_KEY, enc_iv,encrypt_data,&encrypt_data_len);//加密
+    
+    cJSON *root = cJSON_CreateObject();
+    if (root == NULL) 
+    {
+        return false;
+    }
+    // 添加字段
+    cJSON_AddStringToObject(root, "deviceId", deviceId_temp);
+    cJSON_AddStringToObject(root, "password", encrypt_data);
+    cJSON_AddStringToObject(root, "nonce", enc_iv);
+    // 生成JSON字符串
+    char *json_string = cJSON_Print(root);
+    if (json_string == NULL) 
+    {
+        printf( "Failed to print JSON\n");
+        cJSON_Delete(root);
+        return false;
+    }
+    printf("json_string:%s",json_string);
+
+     char out_buf[1024]={0};
+
+    int ret= dmp_http_post(json_string,HTTP_JSON_SERVER_NAME POST_URL,out_buf);
+
+    if(ret==200)
+    {   
+        dmp_read_dev_token_save(out_buf);
+    }
+
+    aut_login_time=timestamp;
+
+    cJSON_free(json_string);
+
+    return true;
+}
+
+
+bool dmp_read_dev_pass_save(char *buf)
+{
+    char *p=buf;
+    while (*p!='{' && *p !='\0')
+    {
+        p++;
+    }
+
+    cJSON* root = cJSON_Parse((const char *)p);
+    if (root == NULL) {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"解析失败\n");
+        return false;
+    }
+    char *password_str=NULL;
+    char * nonce_str=0;
+
+    cJSON* data = cJSON_GetObjectItem(root, "data");
+    if (data)
+    {
+        cJSON* password = cJSON_GetObjectItem(data, "password");
+        if (password && cJSON_IsString(password)) {
+            password_str= password->valuestring;
+        }
+
+        cJSON* nonce = cJSON_GetObjectItem(data, "nonce");
+        if (nonce && cJSON_IsString(nonce)) {
+            nonce_str = nonce->valuestring;
+        }
+         
+    }
+
+    int dev_pass_len =64;
+    char *dev_pass =(char *)malloc(dev_pass_len);
+    if(dev_pass)
+    {
+        memset(dev_pass,0,dev_pass_len);
+        if(strlen(nonce_str)>8 && strlen(password_str)>8)
+        {
+            dmp_aes_cbc_pkcs5_decrypt(password_str, DMP_KEY, nonce_str,dev_pass,&dev_pass_len);//解密
+            dev_pass[dev_pass_len] = 0;
+            printf("dev_pass:%s dev_pass_len:%d\n",dev_pass,dev_pass_len);
+        }
+      
+    }else
+    {
+       printf("dev_pass malloc error!!!!!!!!!!!");
+    }
+
+    
+
+    if(strlen(password_str)>=0)
+    {
+        memcpy(data_pass,dev_pass,dev_pass_len);
+        // 持久化保存 data_pass(C 接口)
+        cm_set_dmp_data_pass(dev_pass);
+        free(dev_pass);
+        cJSON_Delete(data);
+        return true;
+    }
+    free(dev_pass);
+    cJSON_Delete(data);
+    return false;
+}
+
+// {"success":true,"msg":"登录成功","code":20001,"data":{"deviceId":"28110601009b","token":"95fc6dcdf5a3e9a48df535453efd097c19"}}
+
+bool dmp_read_dev_token_save(char *indata)
+{
+    time_t timestamp;
+    timestamp = time(NULL);
+
+    cJSON* root = cJSON_Parse((const char *)indata);
+    if (root == NULL) {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"解析失败\n");
+        return false;
+    }
+    char *token_str=NULL;
+
+    cJSON* data = cJSON_GetObjectItem(root, "data");
+    if (data)
+    {
+        cJSON* token = cJSON_GetObjectItem(data, "token");
+        if (token && cJSON_IsString(token)) 
+        {
+            token_str= token->valuestring;
+        }
+        else
+        {
+             cJSON_Delete(data);
+            return false; 
+        }
+    }
+    else
+    {
+         cJSON_Delete(data);
+        return false;
+    }
+   
+
+    if(strlen(token_str)>=0 && data_token!=NULL )
+    {
+        memcpy(data_token,token_str,strlen(token_str));
+         cJSON_Delete(data);
+        return true;
+    }
+    cJSON_Delete(data);
+   
+    return true;
+}
+
+static void dmp_read_file_put(void )
+{
+      char buf[1024]={0};
+
+    FILE *fp=fopen(AUTH_SIGN_IN,"rb+");
+    int read_size;
+
+    uint32_t pack=1024;
+
+    if(fp==NULL)
+    {
+        return;
+    }
+    do
+    {
+        memset_s(buf,1024,0,1024);
+        read_size=fread(buf,1,1024,fp);
+
+        if(read_size!=0)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "buf %d %s",read_size,buf);     
+        }
+    
+    } while (read_size==1024);
+    fclose(fp);
+
+}
+
+
+static int dmp_active_md5_buf(char *in_buf,char*md5_str)
+{
+
+    char md5_buf[16]={0};
+    char *sign_buf = (char *)malloc(1024);
+    
+    if(!sign_buf)
+        return -10;
+    memset(sign_buf,0,1024);
+
+    int sign_len = 0;
+    strcpy(sign_buf,in_buf);
+
+    sign_len = strlen(in_buf);
+
+
+    mbedtls_md5_context ctx;
+
+    // 初始化MD5上下文
+    mbedtls_md5_init(&ctx);
+
+    // 开始计算MD5哈希
+    mbedtls_md5_starts(&ctx);
+    // 更新要计算哈希的数据
+    mbedtls_md5_update(&ctx, (const unsigned char *)sign_buf, sign_len);
+
+    // 完成哈希计算
+    mbedtls_md5_finish(&ctx, (unsigned char *)md5_buf);
+
+    // printf("md5_buf\r\n");
+    // for (int i = 0; i < 16; i++) {
+    //     printf("%02X\n", (uint8_t)md5_buf[i]);
+    // }
+    // printf("md5_buf end\r\n");
+
+    // 清理MD5上下文
+    mbedtls_md5_free(&ctx);
+    for (int i = 0; i < 16; i++) {
+        sprintf(&md5_str[i * 2], "%02x", (uint8_t)md5_buf[i]);
+    }
+    md5_str[32] = '\0';
+    
+    free(sign_buf);
+    return 0;
+}
+
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
\ No newline at end of file
diff --git a/code/sdk/zhihui/dmp/dmp_http.c b/code/sdk/zhihui/dmp/dmp_http.c
new file mode 100644 (file)
index 0000000..25f8ed0
--- /dev/null
@@ -0,0 +1,135 @@
+
+#include "httpclient.h"
+#include <stdio.h>
+
+int dmp_http_put(char *post_buf,char *url,char *put_buf)
+{
+    HttpClient client = {0};
+    HttpClientData client_data = {0};
+    char *req_url = url;
+    int ret = -1;
+    
+    char* req_buf=malloc(2048);
+    char* rsp_buf=malloc(2048);
+
+    memset(req_buf, 0, 2048);
+    client_data.headerBuf = req_buf;
+    client_data.headerBufLen = 2048;//sizeof(req_buf);
+
+    memset(rsp_buf, 0, 2048);
+    client_data.responseBuf = rsp_buf;
+    client_data.responseBufLen= 2048;//sizeof(rsp_buf);
+
+    printf("\n http request: %s \r\n", req_url);
+
+    client_data.postBuf = post_buf;
+    size_t length = strlen(client_data.postBuf);
+    client_data.postBufLen = length;
+
+    client.header="Content-Type: application/json\r\n";
+
+
+    ret = HttpClientPutRequest(&client, req_url, &client_data);
+    if (ret >= 0 && client.responseCode == 200) {
+        printf("data received: %s \r\n", client_data.responseBuf);
+        printf("headerbuf data received: %s \r\n", client_data.headerBuf);
+        printf("responsecode: %d \r\n", client.responseCode);
+        memcpy(put_buf,client_data.responseBuf,strlen(client_data.responseBuf));
+        printf("http test success!\r\n");
+        ret = 0;
+    } else {
+        printf("responsecode = %d \n", client.responseCode);
+        printf("http test failed! ret= 0x%x \r\n", ret);
+    }
+    free(req_buf);
+    free(rsp_buf);
+    return client.responseCode;
+}
+
+
+int dmp_http_post(char *post_buf,char *url,char *put_buf)
+{
+    HttpClient client = {0};
+    HttpClientData client_data = {0};
+    char *req_url = url;
+    int ret = -1;
+    
+    char* req_buf=malloc(2048);
+    char* rsp_buf=malloc(2048);
+
+    memset(req_buf, 0, 2048);
+    client_data.headerBuf = req_buf;
+    client_data.headerBufLen = 2048;//sizeof(req_buf);
+
+    memset(rsp_buf, 0, 2048);
+    client_data.responseBuf = rsp_buf;
+    client_data.responseBufLen= 2048;//sizeof(rsp_buf);
+
+
+    printf("\n http request: %s \r\n", req_url);
+
+    client_data.postBuf = post_buf;
+    size_t length = strlen(client_data.postBuf);
+    client_data.postBufLen = length;
+
+    client.header="Content-Type: application/json\r\n";
+
+
+    ret = HttpClientPostRequest(&client, req_url, &client_data);
+    if (ret >= 0 && client.responseCode == 200) {
+        printf("data received: %s \r\n", client_data.responseBuf);
+        printf("headerbuf data received: %s \r\n", client_data.headerBuf);
+        printf("responsecode: %d \r\n", client.responseCode);
+        memcpy(put_buf,client_data.responseBuf,strlen(client_data.responseBuf));
+        printf("http test success!\r\n");
+        ret = 0;
+    } else {
+        printf("responsecode = %d \n", client.responseCode);
+        printf("http test failed! ret= 0x%x \r\n", ret);
+    }
+    free(req_buf);
+    free(rsp_buf);
+    return ret;
+}
+
+
+int dmp_http_get(char *post_buf,char *url,char *put_buf)
+{
+    HttpClient client = {0};
+    HttpClientData client_data = {0};
+    char *req_url = url;
+    int ret = -1;
+    
+    char* req_buf=malloc(1024);
+    char* rsp_buf=malloc(1024);
+
+    memset(req_buf, 0, sizeof(req_buf));
+    client_data.headerBuf = req_buf;
+    client_data.headerBufLen = sizeof(req_buf);
+
+    memset(rsp_buf, 0, sizeof(rsp_buf));
+    client_data.responseBuf = rsp_buf;
+    client_data.responseBufLen= sizeof(rsp_buf);
+
+    printf("\n http request: %s \r\n", req_url);
+
+    client_data.postBuf = post_buf;
+    size_t length = strlen(client_data.postBuf);
+    client_data.postBufLen = length;
+
+    ret = HttpClientGetRequest(&client, req_url, &client_data);
+    if (ret >= 0 && client.responseCode == 200) {
+        printf("data received: %s \r\n", client_data.responseBuf);
+        printf("headerbuf data received: %s \r\n", client_data.headerBuf);
+        printf("responsecode: %d \r\n", client.responseCode);
+        memcpy(put_buf,client_data.responseBuf,strlen(client_data.responseBuf));
+        printf("http test success!\r\n");
+        ret = 0;
+    } else {
+        printf("responsecode = %d \n", client.responseCode);
+        printf("http test failed! ret= 0x%x \r\n", ret);
+    }
+    free(req_buf);
+    free(rsp_buf);
+    return ret;
+}
\ No newline at end of file
index 828ff11..1b5e272 100644 (file)
@@ -414,6 +414,8 @@ errcode_t vad_server_init(vad_status_callback_t callback)
     errcode_t ret = uapi_create_osThread_custom_with_args(&task_cfg);
     if (ret != ERRCODE_SUCC) {
         IMSL_ERROR("[VAD_SERVER] Error: Failed to create VAD task: %d\n", ret);
+        /* 清理已初始化的VAD库资源 */
+        free_vad();
         return ret;
     }
     
@@ -427,6 +429,9 @@ errcode_t vad_server_init(vad_status_callback_t callback)
     if (g_vad_server.queue_id == 0xFFFFFFFF) {
         IMSL_ERROR("[VAD_SERVER] Error: Queue creation timeout\n");
         g_vad_server.is_running = false;
+        /* 清理已初始化的VAD库资源 */
+        free_vad();
+        /* 注意:线程已经创建,但队列创建失败,线程会自然退出(因为is_running=false) */
         return ERRCODE_FAIL;
     }
     
@@ -470,8 +475,20 @@ errcode_t vad_server_deinit(void)
         timeout--;
     }
     
-    /* 删除消息队列 */
+    /* 清理队列中剩余的消息(释放内存) */
     if (g_vad_server.queue_id) {
+        vad_process_msg_t msg;
+        unsigned int msg_size = VAD_QUEUE_MSG_SIZE;
+        /* 读取并清理队列中的所有剩余消息 */
+        while (osal_msg_queue_read_copy(g_vad_server.queue_id, &msg, &msg_size, 0) == OSAL_SUCCESS) {
+            /* 如果有数据指针,释放内存 */
+            if (msg.data && msg.data != (void*)0x1 && msg.data_size > 0 && msg.data_size < 1024*1024) {
+                free(msg.data);
+            }
+            msg_size = VAD_QUEUE_MSG_SIZE;  /* 重置消息大小 */
+        }
+        
+        /* 删除消息队列 */
         osal_msg_queue_delete(g_vad_server.queue_id);
         g_vad_server.queue_id = 0;
     }