优化小睡数据存储 1228/1
authorpenglei <1624952103@qq.com>
Sun, 20 Oct 2024 10:06:56 +0000 (18:06 +0800)
committerpenglei <1624952103@qq.com>
Sun, 20 Oct 2024 10:06:56 +0000 (18:06 +0800)
code/sdk/drivers/peripheral/cywee/src/main_dml.c
code/sdk/drivers/peripheral/utils/custom_utils.c

index 8c6c634..5bee5e3 100644 (file)
@@ -150,10 +150,18 @@ void clean_sleep_data(void)
 
 void cywee_save_nap_data(cywee_inactivity_accumulate_time_t c_inactivity_accumulate_time_t)
 {
-    char day[12] = {0};
-    uapi_get_YYMMDD_Time(day, '\0');
+    if (access(nap_file_path, 0) == -1) //如果小睡文件不存在,创建睡眠文件夹
+    {
+        memset(dir_path,0,sizeof(dir_path));
+        memset(day,0,sizeof(day));
+        memset(nap_file_path,0,sizeof(nap_file_path));
+
+        uapi_get_YYMMDD_Time(day, '\0');
+        sprintf_s(dir_path, 64, SLEEP_DIR_PATH, day);
+        uapi_create_folder(dir_path);
+        sprintf_s(nap_file_path, 64, NAP_PATH, day);
+    }
 
-    sprintf_s(nap_file_path, 64, NAP_PATH, day);
 
     uint32_t size = uapi_get_file_size(nap_file_path);
 
@@ -756,7 +764,6 @@ void cywee_time_sync(void)
         uapi_get_YYMMDD_Time_extend(day, '\0', 0);
 
     sprintf_s(dir_path, 64, SLEEP_DIR_PATH, day);
-    uapi_create_folder("user/userdata/sleep");
     uapi_create_folder(dir_path);
 }
 
index ee4e0ee..f7cc525 100644 (file)
@@ -2872,18 +2872,56 @@ void uapi_vibrator_test(uint32_t test_time)
     }
 }
 
-void uapi_create_folder(char *dir_path)
+//创建指定路径文件夹
+void uapi_create_folder(char *dir_path) 
 {
+    if(dir_path == NULL)
+        return;
+
+    char *path_copy = strdup(dir_path);
+    char *token;
+    char *saveptr;
+
+    token = strtok_r(path_copy, "/", &saveptr);
+    if (token == NULL) 
+    {
+        free(path_copy);
+        return;
+    }
+
+    char current_path[256] = "";
+    strcat(current_path, token);
+    strcat(current_path, "/");
+
     struct stat info;
-    if (stat(dir_path, &info) != 0)
+    if (stat(current_path, &info)!= 0) 
+    {
+        if (mkdir(current_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)!= 0) 
+        {
+            PRINT("mkdir %s failed\n", current_path);
+            free(path_copy);
+            return;
+        }
+    }
+
+    while ((token = strtok_r(NULL, "/", &saveptr))!= NULL) 
     {
-        if(mkdir(dir_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
+        strcat(current_path, token);
+        strcat(current_path, "/");
+        if (stat(current_path, &info)!= 0) 
         {
-            PRINT("mkdir %s failed \n",dir_path);
+            if (mkdir(current_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)!= 0) 
+            {
+                PRINT("mkdir %s failed\n", current_path);
+                free(path_copy);
+                return;
+            }
         }
     }
-}
 
+    free(path_copy);
+    PRINT("mkdir %s succed\n", dir_path);
+}
 
 //获取小睡数据长度(协议用)
 uint32_t uapi_get_file_size(char *filepath)