优化睡眠数据解析函数
authorpenglei <1624952103@qq.com>
Fri, 24 Oct 2025 11:22:45 +0000 (19:22 +0800)
committerpenglei <1624952103@qq.com>
Fri, 24 Oct 2025 11:22:45 +0000 (19:22 +0800)
code/sdk/zhihui/utils/custom_utils.c

index 2e51e33..3e25fb3 100644 (file)
@@ -3674,7 +3674,7 @@ void uapi_get_a5133_rxdata_timestamp(uint32_t *timestamp)
 void uapi_analysis_sleep_data(char * timepath , char *statuspath)
 {
     uint16_t datelen = 0;
-    uint8_t sleep_data[256] = {0}; 
+    uint8_t *sleep_data = NULL;
     
     uint32_t asleep_time = 0;
     uint32_t wake_up_time = 0;
@@ -3685,16 +3685,55 @@ void uapi_analysis_sleep_data(char * timepath , char *statuspath)
     uint16_t total_wake_time = 0;
     uint32_t timestampe = 0;
     int status = -1;
-    uint8_t index = 0;
-    char timestampe_str[10] = {0};
-    char status_str[20] = {0};
+    uint16_t index = 0;
+    char *timestampe_str = NULL;
+    char *status_str = NULL;
 
-    uapi_get_file_data(timepath,sleep_data,&datelen);
+    // 参数检查
+    if (!timepath || !statuspath) {
+        PRINT("Invalid parameters: timepath or statuspath is NULL\n");
+        return;
+    }
+
+    // 分配内存
+    sleep_data = (uint8_t*)malloc(1024);  // 增大缓冲区到1KB
+    if (!sleep_data) {
+        PRINT("Failed to allocate memory for sleep_data\n");
+        return;
+    }
+    
+    timestampe_str = (char*)malloc(20);  // 时间字符串缓冲区
+    if (!timestampe_str) {
+        PRINT("Failed to allocate memory for timestampe_str\n");
+        free(sleep_data);
+        return;
+    }
+    
+    status_str = (char*)malloc(50);  // 状态字符串缓冲区
+    if (!status_str) {
+        PRINT("Failed to allocate memory for status_str\n");
+        free(sleep_data);
+        free(timestampe_str);
+        return;
+    }
+
+    // 初始化内存
+    memset(sleep_data, 0, 1024);
+    memset(timestampe_str, 0, 20);
+    memset(status_str, 0, 50);
+
+    uapi_get_file_data(timepath, sleep_data, &datelen);
 
     if(datelen == 0)
     {
-        PRINT("fread error datelen == 0 \n");
-        return;
+        PRINT("fread error datelen == 0 for timepath: %s\n", timepath);
+        goto cleanup;
+    }
+
+    // 检查数据长度是否足够
+    if(datelen < 19) {
+        PRINT("Invalid time data length: %d, expected at least 19 bytes\n", datelen);
+        goto cleanup;
     }
 
     asleep_time = *(uint32_t*)&sleep_data[1];
@@ -3706,78 +3745,124 @@ void uapi_analysis_sleep_data(char * timepath , char *statuspath)
     total_rem_sleep_time = sleep_data[15] * 60 + sleep_data[16];
     total_wake_time = sleep_data[17] * 60 + sleep_data[18];
 
-    char asleep_time_str[10] = {0};
-    uapi_get_TIME_STR(asleep_time,asleep_time_str, '.', ':',DATE_H_M);
+    // 使用临时变量处理时间字符串
+    char asleep_time_str[20] = {0};
+    uapi_get_TIME_STR(asleep_time, asleep_time_str, '.', ':', DATE_H_M);
 
-    char wake_up_time_str[10] = {0};
-    uapi_get_TIME_STR(wake_up_time,wake_up_time_str, '.', ':',DATE_H_M);
+    char wake_up_time_str[20] = {0};
+    uapi_get_TIME_STR(wake_up_time, wake_up_time_str, '.', ':', DATE_H_M);
 
-    PRINT("入睡时间 : %s %d \n",asleep_time_str,asleep_time);
-    PRINT("出睡时间 : %s %d \n",wake_up_time_str,wake_up_time);
-    PRINT("总时长 : %d 分钟\n",total_time);
-    PRINT("深睡时长 : %d 分钟\n",total_deep_sleep_time);
-    PRINT("浅睡时长 : %d 分钟\n",total_mild_sleep_time);
-    PRINT("快速眼动时长 : %d 分钟\n",total_rem_sleep_time);
-    PRINT("清醒时长 : %d 分钟\n",total_wake_time);
+    PRINT("入睡时间 : %s %d \n", asleep_time_str, asleep_time);
+    PRINT("出睡时间 : %s %d \n", wake_up_time_str, wake_up_time);
+    PRINT("总时长 : %d 分钟\n", total_time);
+    PRINT("深睡时长 : %d 分钟\n", total_deep_sleep_time);
+    PRINT("浅睡时长 : %d 分钟\n", total_mild_sleep_time);
+    PRINT("快速眼动时长 : %d 分钟\n", total_rem_sleep_time);
+    PRINT("清醒时长 : %d 分钟\n", total_wake_time);
 
-    memset(sleep_data,0,sizeof(sleep_data));
+    // 重置数据和索引
+    memset(sleep_data, 0, 1024);
     datelen = 0;
-    uapi_get_file_data(statuspath,sleep_data,&datelen);
+    index = 0;
+    
+    uapi_get_file_data(statuspath, sleep_data, &datelen);
     
     if(datelen == 0)
     {
-        PRINT("fread error datelen == 0 \n");
-        return;
+        PRINT("fread error datelen == 0 for statuspath: %s\n", statuspath);
+        goto cleanup;
     }
 
-    while(datelen)
+    PRINT("Status data length: %d bytes\n", datelen);
+
+    // 检查数据长度是否为5的倍数
+    if(datelen % 5 != 0) {
+        PRINT("Warning: Status data length %d is not multiple of 5\n", datelen);
+    }
+
+    uint16_t record_count = 0;
+    const uint16_t max_records = 100; // 增加最大记录数
+    uint16_t original_datelen = datelen; // 保存原始长度用于边界检查
+    
+    while(datelen >= 5 && index + 4 < original_datelen && record_count < max_records)
     {
-        memset(timestampe_str,0,sizeof(timestampe_str));
-        memset(status_str,0,sizeof(status_str));
+        memset(timestampe_str, 0, 20);
+        memset(status_str, 0, 50);
+        
+        // 边界检查
+        if(index + 4 >= original_datelen) {
+            PRINT("Warning: Not enough data for record at index %d\n", index);
+            break;
+        }
+        
         timestampe = *(uint32_t*)&sleep_data[index];
-        uapi_get_TIME_STR(timestampe,timestampe_str, '.', ':',DATE_H_M);
+        uapi_get_TIME_STR(timestampe, timestampe_str, '.', ':', DATE_H_M);
         status = sleep_data[index + 4];
+        
         //0进入睡眠  1潜睡 2深睡 3清醒 4结束睡眠 6入睡 7出睡最后一笔输出 8未出睡最后一笔输出 12快速动眼 13取消入睡
         switch(status)
         {
             case 0:
-                strcpy(status_str,"进入睡眠");
+                strcpy(status_str, "进入睡眠");
                 break;
             case 1:
-                strcpy(status_str,"浅睡");
+                strcpy(status_str, "浅睡");
                 break;
             case 2:
-                strcpy(status_str,"深睡");
+                strcpy(status_str, "深睡");
                 break;
             case 3:
-                strcpy(status_str,"清醒");
+                strcpy(status_str, "清醒");
                 break;
             case 4:
-                strcpy(status_str,"结束睡眠");
+                strcpy(status_str, "结束睡眠");
                 break;
             case 6:
-                strcpy(status_str,"入睡通知");
+                strcpy(status_str, "入睡通知");
                 break;
             case 7:
-                strcpy(status_str,"出睡最后一笔输出");
+                strcpy(status_str, "出睡最后一笔输出");
                 break;
             case 9:
-                strcpy(status_str,"未出睡最后一笔输出");
+                strcpy(status_str, "未出睡最后一笔输出");
                 break;
             case 12:
-                strcpy(status_str,"快速动眼");
+                strcpy(status_str, "快速动眼");
                 break;
             case 13:
-                strcpy(status_str,"取消入睡");
+                strcpy(status_str, "取消入睡");
                 break;
             default:
+                snprintf(status_str, 50, "未知状态(%d)", status);
                 break;
-
         }
 
-        PRINT("时间 :  %d %s  状态:%s\n",timestampe,timestampe_str,status_str);
-        index+=5;
+        PRINT("时间 :  %d %s  状态:%s\n", timestampe, timestampe_str, status_str);
+        
+        index += 5;
         datelen -= 5;
+        record_count++;
+    }
+    
+    if(record_count >= max_records) {
+        PRINT("Warning: Reached maximum record limit (%d), stopping processing\n", max_records);
+    }
+    
+    PRINT("Processed %d sleep status records\n", record_count);
+
+cleanup:
+    // 释放内存
+    if (sleep_data) {
+        free(sleep_data);
+        sleep_data = NULL;
+    }
+    if (timestampe_str) {
+        free(timestampe_str);
+        timestampe_str = NULL;
+    }
+    if (status_str) {
+        free(status_str);
+        status_str = NULL;
     }
 }