tar文件传输 328/1 328/head
authorhcj <1224667075@qq.com>
Wed, 19 Jun 2024 12:43:36 +0000 (20:43 +0800)
committerhcj <1224667075@qq.com>
Wed, 19 Jun 2024 12:43:36 +0000 (20:43 +0800)
code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp
code/sdk/application/wearable/service/ble_service/blemoder/bletest.h
code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c
code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.h

index f4f20be..cdeb65a 100644 (file)
@@ -42,7 +42,7 @@
 extern "C" {
 #endif /* __cplusplus */
 #endif /* __cplusplus */
-
+#include "custom_utils.h"
 #include "pinctrl.h"
 #include "debug_print.h"
 #include "soc_osal.h"
@@ -68,6 +68,8 @@ extern "C" {
 #include "bletest.h"
 #include <cywee_hal.h>
 
+#include <dirent.h>
+
 
 
 void ble_set_password(uint8_t falg, char * data)
@@ -450,11 +452,11 @@ char * struct_to_json_buf_malloc_Alarm(void)
     cJSON_AddItemToObject(dataJSON,"alarms",data_arryJSON);
 
     char *json_format = cJSON_PrintUnformatted(dataJSON);
-    cJSON_Delete(dataJSON);
+   
     int pSize = cJSON_GetArraySize(dataJSON);
+     cJSON_Delete(dataJSON);
     printf("闹钟数据 :%s\r\n",json_format);
     return json_format;
-
 }
 
 char *  struct_to_json_buf_malloc_(uint8_t day)
@@ -487,6 +489,7 @@ char *  struct_to_json_buf_malloc_(uint8_t day)
             cJSON_AddNumberToObject(itemJSON,"di",data->di);
             cJSON_AddNumberToObject(itemJSON,"hr",data->hr);
             cJSON_AddNumberToObject(itemJSON,"stp",data->stp);
+
             cJSON_AddItemReferenceToArray(data_arryJSON,itemJSON);
             free(data);
             data=NULL;
@@ -499,14 +502,169 @@ char *  struct_to_json_buf_malloc_(uint8_t day)
     cJSON_AddItemToObject(dataJSON,"recs",data_arryJSON);
 
     char *json_format = cJSON_PrintUnformatted(dataJSON);
-    cJSON_Delete(dataJSON);
+   
     int pSize = cJSON_GetArraySize(dataJSON);
     printf("训练历史数据:%s\r\n",json_format);
+    cJSON_Delete(dataJSON);
     return json_format;
 #else
     return "";
 #endif
 }
+#define TAR_PATH "user/tar"
+
+
+static uint8_t Calculate_CRC_file(char *path)//
+{
+    uint8_t crcbuf  = 0;
+    uint8_t lrcValue = 0x00;
+       lrcValue = calculate_lrc_file(path);
+
+    crcbuf = get_crc_from_lrc(lrcValue);
+    return crcbuf;
+}
+
+static uint8_t file_count=0;
+
+static uint8_t file_index=1;
+
+void get_tar_dir_file_count(void)
+{
+    char dir_path[128]={0};
+    sprintf_s(dir_path,128,"%s",TAR_PATH);
+    
+    struct DIR *dir;
+    struct dirent *ent;
+
+    std::string fileName = ent->d_name;
+    file_count=0;
+
+    if ((dir = opendir(dir_path)) != NULL)
+    {
+        while ((ent = readdir(dir)) != NULL)
+        {
+            if (fileName != "." && fileName != "..")
+            {
+             
+                if (ent->d_type == DT_REG || ent->d_type == DT_UNKNOWN ) // 普通文件
+                {
+                    //if(strstr(fileName.c_str(),".tar"))
+                    file_count++;
+
+                }
+            }
+            else if (ent->d_type == DT_DIR) // 目录
+            {
+                continue;
+            }
+        }
+    }
+    closedir(dir);
+    file_index=1;
+    return;
+}
+
+uint32_t tar_rmove(char *tar_path)
+{
+    
+    printf("remove  %s\r\n",tar_path);
+    if(0==remove(tar_path))
+    return 1;
+    else
+    return 0;
+}
+
+char * tar_dir_to_json_buf_malloc_(char* file_name,uint32_t *file_size)
+{
+    char dir_path[128]={0};
+    sprintf_s(dir_path,128,"%s",TAR_PATH);
+
+    cJSON *dataJSON = cJSON_CreateObject();
+    cJSON* data_arryJSON = cJSON_CreateArray();
+
+    struct DIR *dir;
+    struct dirent *ent;
+   
+
+    if ((dir = opendir(dir_path)) != NULL)
+    {
+        
+        while ((ent = readdir(dir)) != NULL)
+        {
+            std::string fileName = ent->d_name;
+
+            char file_path[128]={0};
+            sprintf_s(file_path,128,"%s/%s",TAR_PATH,ent->d_name);
+              
+            FILE * fp = fopen(file_path,"rb+");
+            if(fp==NULL)
+            {
+                printf(" fopen fail %s\r\n",file_path);
+                return NULL;
+            }
+            if (fp != NULL)
+            {
+                fseek(fp, 0, SEEK_END); // 将文件指针移动到文件末尾
+                *file_size = ftell(fp);       // 获取当前文件指针位置,即文件大小
+                fseek(fp, 0, SEEK_SET); // 将文件指针移回文件开头
+            }
+            fclose(fp);
+
+            if (fileName != "." && fileName != "..")
+            {
+                //std::string filePath = directory + fileName;
+                
+
+                if (ent->d_type == DT_REG || ent->d_type == DT_UNKNOWN ) // 普通文件
+                {
+                   
+                    //*file_size=ent->d_reclen;
+                    memcpy(file_name,file_path,strlen(file_path));
+                    uint8_t crc= Calculate_CRC_file(file_name);
+
+                    cJSON* itemJSON = cJSON_CreateObject();
+
+                    cJSON_AddNumberToObject(itemJSON,"index",file_index);
+                    cJSON_AddNumberToObject(itemJSON,"count",file_count);
+
+                    cJSON_AddNumberToObject(itemJSON,"crc",crc);
+
+                    cJSON_AddStringToObject(itemJSON,"name",fileName.c_str());
+                    cJSON_AddNumberToObject(itemJSON,"size",*file_size);
+
+                    char *json_format = cJSON_PrintUnformatted(itemJSON);
+                 
+                    int pSize = cJSON_GetArraySize(dataJSON);
+                    printf("文件数据:%s\r\n",json_format);
+                    cJSON_Delete(dataJSON);
+                    file_index++;
+
+                    return json_format;
+
+                    //cJSON_AddItemReferenceToArray(data_arryJSON,itemJSON);
+
+                }
+                else if (ent->d_type == DT_DIR) // 目录
+                {
+                    continue;
+                }
+
+            }
+
+        }
+        closedir(dir);
+    }
+    else
+    {
+         printf("opendir fail\r\n");
+    }
+    return NULL;
+}
+
+
+
+
+
 
 void add_addressbookToRead(char *data) 
 {
index 136b771..7e62f5a 100644 (file)
@@ -230,6 +230,14 @@ void get_sedent_param(  msg_sedent_set_param_info_t * data);
 ///////////////////////手表密码//////////////////////////
 void ble_set_password(uint8_t falg, char * data);
 uint8_t ble_get_password(void);
+
+///////////////////////文件传输//////////////////////////
+char * tar_dir_to_json_buf_malloc_(char* file_name,uint32_t *file_size);
+
+void get_tar_dir_file_count(void);
+
+uint32_t tar_rmove(char *tar_path);
+
 #ifdef __cplusplus
 #if __cplusplus
 }
index 6018a59..aa62ccf 100644 (file)
@@ -267,10 +267,11 @@ static uint8_t calculate_lrc(uint8_t *data,uint16_t len,uint8_t _lrcValue)
     return lrcValue;\r
 }\r
 \r
+\r
 /***\r
  * 根据求和后的数据计算crc 值 \r
 */\r
-static uint8_t get_crc_from_lrc(uint8_t lrcValue)\r
+uint8_t get_crc_from_lrc(uint8_t lrcValue)\r
 {\r
     uint8_t crcbuf  = 0;\r
     crcbuf = lrcValue%256;\r
@@ -279,6 +280,33 @@ static uint8_t get_crc_from_lrc(uint8_t lrcValue)
     return crcbuf;\r
 }\r
 \r
+uint8_t  calculate_lrc_file(char *file_path)\r
+{\r
+    uint8_t lrcValue =0;\r
+    FILE * fp = fopen(file_path,"rb+");\r
+\r
+    char data[1]={0};\r
+    \r
+    printf("calculate_lrc_file %s\r\n",file_path);\r
+    uint8_t  read_size=0;\r
+\r
+    if(fp==NULL)\r
+    {\r
+        printf("fopen fail %s\r\n",file_path);\r
+    }\r
+    do\r
+    {\r
+        memset_s(data,1,0,1);\r
+        read_size=fread(data,1,1,fp);\r
+        lrcValue+=data[0];\r
+\r
+    }while(read_size==1);\r
+\r
+    fclose(fp);\r
+    return lrcValue;\r
+}\r
+\r
+\r
 #define USE_DEMO\r
 static bool Check_CRC(uint8_t *data,uint16_t len,uint8_t crcvalue)//计算20byte的的校验位\r
 {\r
@@ -312,6 +340,8 @@ static bool Check_CRC(uint8_t *data,uint16_t len,uint8_t crcvalue)//计算20byte
     }\r
         \r
 }\r
+\r
+\r
 static uint8_t Calculate_CRC(uint8_t *data,uint16_t len)//计算20byte\r
 {\r
     uint8_t crcbuf  = 0;\r
@@ -3961,6 +3991,10 @@ static void Send_json_Data_Cmd(uint8_t day,uint16_t pack_size,uint8_t id)
             case 0x01:\r
                 json_format=struct_to_json_buf_malloc_Alarm();\r
                 break;\r
+            case 0x21:\r
+                json_format=NULL;\r
+                break;\r
+\r
             default:\r
                 break;\r
         }\r
@@ -4743,8 +4777,17 @@ void readDevDataBle(uint16_t cmd,uint8_t *data,uint16_t len,uint8_t *totalData,u
                 EACH_DATA_LEN = DIS_DATA_LEN;\r
                 num=(ble_nus_max_data_len-18)/EACH_DATA_LEN;\r
                 Read_JSON_file_buf_send_bin_Data(data[0],num,0x4b,"heart_data_list");\r
-\r
                 break;\r
+            case 0x21://文件目录\r
+                EACH_DATA_LEN = DIS_DATA_LEN;\r
+                num=(ble_nus_max_data_len-18);\r
+                if(num>482)\r
+                {\r
+                    num=482;\r
+                }\r
+                Send_json_Data_Cmd(data[0],num,0x21);\r
+                break;\r
+\r
             default:\r
                 break;\r
         }\r
@@ -5524,8 +5567,64 @@ static errcode_t install_dial(void)
         printf(".lzma not found\n");\r
         return ERRCODE_FAIL;\r
     }\r
+}\r
+\r
+static char file_name[128]={0};\r
+static uint32_t file_size=0;\r
+\r
+bool Read_tar_file_buf_send_Data(uint16_t pack_size,uint8_t id)\r
+{\r
+    sendbigDataBufState = true;//分包同步数据不允许打断\r
+\r
+    char *data= (char *)malloc(pack_size+1);\r
+    uint16_t read_size=0;\r
+    FILE *fp=fopen(file_name,"rb+");\r
+    \r
+    printf("Read_tar_file_buf_send_Data size %d\r\n",file_size);\r
+\r
+    uint16_t pack=file_size/pack_size+1;\r
+    uint8_t pack_id=1;\r
+    if(fp==NULL)\r
+    {\r
+        printf("fopen fail %s\r\n",file_name);\r
+        return false;\r
+    }\r
 \r
+    do\r
+    {\r
+        memset_s(data,pack_size+1,0,pack_size+1);\r
+        read_size=fread(data,1,pack_size,fp);\r
+        \r
+        send_big_data(1,data,read_size,file_size,pack_id,pack,id,5);\r
+        pack_id++;\r
+        msleep(50);\r
+    } while (read_size==pack_size);\r
+    free(data);\r
+    fclose(fp);\r
+    sendbigDataBufState = false;\r
+    return true;\r
+}\r
+\r
+\r
+bool read_dir_send_json()\r
+{\r
+   \r
+    char *send_buf=NULL;\r
+    printf("read_dir_send_json xxxxxxxx\r\n");\r
+    send_buf=tar_dir_to_json_buf_malloc_(file_name,&file_size);\r
+    if( send_buf!=NULL)\r
+    {\r
+        send_big_data(1,send_buf,strlen(send_buf),strlen(send_buf),1,1,0x26,0x04);\r
+        free(send_buf);\r
+        return true;\r
+    }\r
+    else\r
+    {\r
+         return false;\r
+    }\r
 }\r
+\r
+\r
 void decode_file_checksum(ble_read_data *buffer_handle)\r
 {\r
 \r
@@ -5632,7 +5731,7 @@ void type2AE0Data(ble_read_data *buffer_handle)
     uint16_t typeID = buffer_handle->cmd_header.cmdtype*256 + buffer_handle->cmd_header.cmdid;\r
     printf("type2AE0Data typeID:0x%04X datalen %d\r\n",typeID,buffer_handle->totalDataLen);\r
     bool checkcrc = (get_crc_from_lrc(buffer_handle->lrcvalue) == buffer_handle->crcvalue);\r
-    \r
+    uint32_t num;\r
     if ( (checkcrc == true ) || \r
     (buffer_handle->cmd_header.direction == MSG_DIRECTION_RECV_ANDROID))\r
     {\r
@@ -5683,6 +5782,35 @@ void type2AE0Data(ble_read_data *buffer_handle)
                 break;\r
             case EPO_DATA_RECV_CMD:\r
                 break;\r
+            case APP_INAUIRE_EXC_FILE_CMD:\r
+                get_tar_dir_file_count();\r
+\r
+                if(!read_dir_send_json())\r
+                {\r
+                    ble_reply_cmd(APP_INAUIRE_EXC_FILE_CMD,2);\r
+                }\r
+                break;\r
+                \r
+           case APP_START_SEND_EXC_FILE_CMD:\r
+                num=(ble_nus_max_data_len-18); \r
+                if(num>482)num=482;\r
+                if(!Read_tar_file_buf_send_Data(num,0x63))\r
+                {\r
+                    ble_reply_cmd(APP_START_SEND_EXC_FILE_CMD,2);\r
+                }\r
+                break;\r
+            case APP_SEND_EXC_FILE_END_CMD:\r
+                if(!tar_rmove(file_name))\r
+                {\r
+                    ble_reply_cmd(APP_SEND_EXC_FILE_END_CMD,2);\r
+                }\r
+                if(!read_dir_send_json())\r
+                {\r
+                    ble_reply_cmd(APP_SEND_EXC_FILE_END_CMD,2);\r
+                }\r
+                break;\r
+                \r
+\r
             default:\r
                 break;            \r
         }\r
index 7571f80..04fc992 100644 (file)
@@ -571,6 +571,12 @@ typedef struct
 #define FILE_CHECKSUM_RECV_CMD 0x0423
 #define SINGLE_DIAL_FILE_RECV_CMD 0x0424
 #define EPO_DATA_RECV_CMD 0x0425
+#define APP_INAUIRE_EXC_FILE_CMD 0x0426
+
+#define APP_START_SEND_EXC_FILE_CMD 0x0427
+
+#define APP_SEND_EXC_FILE_END_CMD 0x0428
+
 #define FILE_RECV_REPLY_STAT_CMD 0x0520
 
 
@@ -752,6 +758,13 @@ void sendtraningstateend(void);
 
 int char_to_dec(char ch);//字符数字转换成阿拉伯数字
 void char_to_big(uint8_t *src_str,uint8_t *res_str,uint8_t len);
+
+bool read_dir_send_json(void);
+bool Read_tar_file_buf_send_Data(uint16_t pack_size,uint8_t id);
+
+uint8_t get_crc_from_lrc(uint8_t lrcValue);
+
+uint8_t  calculate_lrc_file(char *file_path);
 #ifdef __cplusplus
 }
 #endif