From ddfbaa5774ab17778f8b27abf4b9bbccb95179a5 Mon Sep 17 00:00:00 2001 From: hcj <1224667075@qq.com> Date: Wed, 19 Jun 2024 20:43:36 +0800 Subject: [PATCH] =?utf8?q?tar=E6=96=87=E4=BB=B6=E4=BC=A0=E8=BE=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../service/ble_service/blemoder/bletest.cpp | 166 +++++++++++++++++- .../service/ble_service/blemoder/bletest.h | 8 + .../service/ble_service/bleservice/bleEvent.c | 134 +++++++++++++- .../service/ble_service/bleservice/bleEvent.h | 13 ++ 4 files changed, 314 insertions(+), 7 deletions(-) diff --git a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp index f4f20be0e..cdeb65a6d 100644 --- a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp +++ b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp @@ -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 +#include + 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) { diff --git a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.h b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.h index 136b771cd..7e62f5ac2 100644 --- a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.h +++ b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.h @@ -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 } diff --git a/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c b/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c index 6018a59ab..aa62ccf35 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c @@ -267,10 +267,11 @@ static uint8_t calculate_lrc(uint8_t *data,uint16_t len,uint8_t _lrcValue) return lrcValue; } + /*** * 根据求和后的数据计算crc 值 */ -static uint8_t get_crc_from_lrc(uint8_t lrcValue) +uint8_t get_crc_from_lrc(uint8_t lrcValue) { uint8_t crcbuf = 0; crcbuf = lrcValue%256; @@ -279,6 +280,33 @@ static uint8_t get_crc_from_lrc(uint8_t lrcValue) return crcbuf; } +uint8_t calculate_lrc_file(char *file_path) +{ + uint8_t lrcValue =0; + FILE * fp = fopen(file_path,"rb+"); + + char data[1]={0}; + + printf("calculate_lrc_file %s\r\n",file_path); + uint8_t read_size=0; + + if(fp==NULL) + { + printf("fopen fail %s\r\n",file_path); + } + do + { + memset_s(data,1,0,1); + read_size=fread(data,1,1,fp); + lrcValue+=data[0]; + + }while(read_size==1); + + fclose(fp); + return lrcValue; +} + + #define USE_DEMO static bool Check_CRC(uint8_t *data,uint16_t len,uint8_t crcvalue)//计算20byte的的校验位 { @@ -312,6 +340,8 @@ static bool Check_CRC(uint8_t *data,uint16_t len,uint8_t crcvalue)//计算20byte } } + + static uint8_t Calculate_CRC(uint8_t *data,uint16_t len)//计算20byte { uint8_t crcbuf = 0; @@ -3961,6 +3991,10 @@ static void Send_json_Data_Cmd(uint8_t day,uint16_t pack_size,uint8_t id) case 0x01: json_format=struct_to_json_buf_malloc_Alarm(); break; + case 0x21: + json_format=NULL; + break; + default: break; } @@ -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; num=(ble_nus_max_data_len-18)/EACH_DATA_LEN; Read_JSON_file_buf_send_bin_Data(data[0],num,0x4b,"heart_data_list"); - break; + case 0x21://文件目录 + EACH_DATA_LEN = DIS_DATA_LEN; + num=(ble_nus_max_data_len-18); + if(num>482) + { + num=482; + } + Send_json_Data_Cmd(data[0],num,0x21); + break; + default: break; } @@ -5524,8 +5567,64 @@ static errcode_t install_dial(void) printf(".lzma not found\n"); return ERRCODE_FAIL; } +} + +static char file_name[128]={0}; +static uint32_t file_size=0; + +bool Read_tar_file_buf_send_Data(uint16_t pack_size,uint8_t id) +{ + sendbigDataBufState = true;//分包同步数据不允许打断 + + char *data= (char *)malloc(pack_size+1); + uint16_t read_size=0; + FILE *fp=fopen(file_name,"rb+"); + + printf("Read_tar_file_buf_send_Data size %d\r\n",file_size); + + uint16_t pack=file_size/pack_size+1; + uint8_t pack_id=1; + if(fp==NULL) + { + printf("fopen fail %s\r\n",file_name); + return false; + } + do + { + memset_s(data,pack_size+1,0,pack_size+1); + read_size=fread(data,1,pack_size,fp); + + send_big_data(1,data,read_size,file_size,pack_id,pack,id,5); + pack_id++; + msleep(50); + } while (read_size==pack_size); + free(data); + fclose(fp); + sendbigDataBufState = false; + return true; +} + + +bool read_dir_send_json() +{ + + char *send_buf=NULL; + printf("read_dir_send_json xxxxxxxx\r\n"); + send_buf=tar_dir_to_json_buf_malloc_(file_name,&file_size); + if( send_buf!=NULL) + { + send_big_data(1,send_buf,strlen(send_buf),strlen(send_buf),1,1,0x26,0x04); + free(send_buf); + return true; + } + else + { + return false; + } } + + void decode_file_checksum(ble_read_data *buffer_handle) { @@ -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; printf("type2AE0Data typeID:0x%04X datalen %d\r\n",typeID,buffer_handle->totalDataLen); bool checkcrc = (get_crc_from_lrc(buffer_handle->lrcvalue) == buffer_handle->crcvalue); - + uint32_t num; if ( (checkcrc == true ) || (buffer_handle->cmd_header.direction == MSG_DIRECTION_RECV_ANDROID)) { @@ -5683,6 +5782,35 @@ void type2AE0Data(ble_read_data *buffer_handle) break; case EPO_DATA_RECV_CMD: break; + case APP_INAUIRE_EXC_FILE_CMD: + get_tar_dir_file_count(); + + if(!read_dir_send_json()) + { + ble_reply_cmd(APP_INAUIRE_EXC_FILE_CMD,2); + } + break; + + case APP_START_SEND_EXC_FILE_CMD: + num=(ble_nus_max_data_len-18); + if(num>482)num=482; + if(!Read_tar_file_buf_send_Data(num,0x63)) + { + ble_reply_cmd(APP_START_SEND_EXC_FILE_CMD,2); + } + break; + case APP_SEND_EXC_FILE_END_CMD: + if(!tar_rmove(file_name)) + { + ble_reply_cmd(APP_SEND_EXC_FILE_END_CMD,2); + } + if(!read_dir_send_json()) + { + ble_reply_cmd(APP_SEND_EXC_FILE_END_CMD,2); + } + break; + + default: break; } diff --git a/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.h b/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.h index 7571f8015..04fc9920f 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.h +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.h @@ -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 -- 2.17.1