From 75c6506c98f4dea5c843d6eec7a7010c0c0443cb Mon Sep 17 00:00:00 2001 From: hcj <1224667075@qq.com> Date: Fri, 28 Jun 2024 15:42:40 +0800 Subject: [PATCH] =?utf8?q?=E5=B0=8F=E7=9D=A1=E5=8F=8A=E7=9D=A1=E7=9C=A0?= =?utf8?q?=E6=95=B0=E6=8D=AE=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/sleep/src/SleepModel.cpp | 12 +- .../ble_service/blemoder/CMakeLists.txt | 2 + .../service/ble_service/blemoder/bletest.cpp | 176 +++++++++++++-- .../service/ble_service/blemoder/bletest.h | 10 + .../service/ble_service/bleservice/bleEvent.c | 208 ++++++++++++------ 5 files changed, 321 insertions(+), 87 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp index 0658e48fb..73622aa76 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp @@ -32,10 +32,19 @@ namespace OHOS uapi_get_YYMMDD_Time(day,'\0'); sprintf_s(dir_path,64,SLEEP_DIR_PATH,day); + if (stat("user/userdata/sleep", &info) != 0) + { + if (mkdir("user/userdata/sleep", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) + { + return false; // Failed to create folder + } + } + if (stat(dir_path, &info) != 0) { if (mkdir(dir_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "mkdir fail %s",dir_path); return false; // Failed to create folder } } @@ -48,7 +57,7 @@ namespace OHOS fp=fopen(file_path,"wb+"); if(fp==NULL) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "save_nap_sleep_data fopen fail"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_nap_sleep_data fopen fail"); return false; } @@ -65,6 +74,7 @@ namespace OHOS fwrite(&total_time,1,2,fp); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_nap_sleep_data succ"); fclose(fp); return true; diff --git a/code/sdk/application/wearable/service/ble_service/blemoder/CMakeLists.txt b/code/sdk/application/wearable/service/ble_service/blemoder/CMakeLists.txt index 1b9bc4d1b..8636427bb 100644 --- a/code/sdk/application/wearable/service/ble_service/blemoder/CMakeLists.txt +++ b/code/sdk/application/wearable/service/ble_service/blemoder/CMakeLists.txt @@ -21,6 +21,7 @@ ${ROOT_DIR}/application/wearable/fd230app/nativeui/traing/training_card/include ${ROOT_DIR}/application/wearable/fd230app/nativeui/config/include ${ROOT_DIR}/application/wearable/fd230app/nativeui/traing/include ${ROOT_DIR}/application/wearable/fd230app/nativeui/uiservice/include +${ROOT_DIR}/application/wearable/fd230app/nativeui/sleep/include #${ROOT_DIR}/application/wearable/fd230app/nativeui/alarm/include ${ROOT_DIR}/application/wearable/fd230app/nativeui/alarmclockmanage/include ${ROOT_DIR}/application/wearable/fd230app/nativeui/alarmclocktimerconfig/include @@ -37,6 +38,7 @@ ${ROOT_DIR}/application/wearable/fd230app/nativeui/set_dial/include ) set(SOURCES +#${CMAKE_CURRENT_SOURCE_DIR}/bletest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/bletest.cpp ) 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 8faa4ecdd..3a2a83174 100644 --- a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp +++ b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp @@ -35,7 +35,7 @@ #include "SetDialModel.h" - +#include "SleepModel.h" //#include "AlarmClockModel.h" #ifdef __cplusplus #if __cplusplus @@ -70,7 +70,145 @@ extern "C" { #include +uint8_t ble_AT_add_nap_data(uint16_t time) +{ + OHOS::NAP_T data; + data.duration=time; + uint32_t creationTime; + int ret =uapi_get_timestampe_ss(&creationTime); + data.fall_asleep_timestamp=creationTime-60*60; + data.wake_up_timestamp=creationTime; + set_nap_data(&data); +} + +uint8_t ble_get_sleep_data(uint32_t file_size,uint8_t day_,uint8_t *data) +{ + char dir_path[64]={0}; + char day[12]={0}; + uapi_get_YYMMDD_Time_extend(day,'\0', day_); + sprintf_s(dir_path,64,SLEEP_DIR_PATH,day); + + struct DIR *dir; + struct dirent *ent; + + std::string fileName = ent->d_name; + uint32_t offset=0; + uint8_t sleep_count=1; + + 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 ) // 普通文件 + { + + char file_path[128]={0}; + sprintf_s(file_path,128,"%s/%s",dir_path,ent->d_name); + + FILE * fp = fopen(file_path,"rb+"); + if(fp==NULL) + { + printf(" fopen fail %s\r\n",file_path); + return 1 ; + } + + uint16_t sleep_file_size=0; + if (fp != NULL) + { + fseek(fp, 0, SEEK_END); // 将文件指针移动到文件末尾 + sleep_file_size = ftell(fp); // 获取当前文件指针位置,即文件大小 + sleep_file_size++; + fseek(fp, 0, SEEK_SET); // 将文件指针移回文件开头 + memcpy(data+offset,&sleep_file_size,2); + *(data+offset+2)=sleep_count; + sleep_count++; + offset+=3; + uint16_t read_size =fread(data+offset,1,sleep_file_size,fp); + + if((read_size+1)==sleep_file_size) + offset+=read_size; + else + printf(" fread fail read_size:%d!= sleep_file_size:%d \r\n",read_size,sleep_file_size); + + fclose(fp); + } + } + } + else if (ent->d_type == DT_DIR) // 目录 + { + continue; + } + } + } + closedir(dir); + + return offset; +} + + + + +uint8_t ble_get_sleep_data_dir(uint32_t *file_size,uint8_t day_) +{ + char dir_path[64]={0}; + char day[12]={0}; + uapi_get_YYMMDD_Time_extend(day,'\0', day_); + sprintf_s(dir_path,64,SLEEP_DIR_PATH,day); + + struct DIR *dir; + struct dirent *ent; + + std::string fileName = ent->d_name; + uint8_t 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 ) // 普通文件 + { + + char file_path[128]={0}; + sprintf_s(file_path,128,"%s/%s",dir_path,ent->d_name); + + FILE * fp = fopen(file_path,"rb+"); + if(fp==NULL) + { + printf(" fopen fail %s\r\n",file_path); + return file_count; + } + + if (fp != NULL) + { + fseek(fp, 0, SEEK_END); // 将文件指针移动到文件末尾 + *file_size += ftell(fp)+3; // 获取当前文件指针位置,即文件大小 + fseek(fp, 0, SEEK_SET); // 将文件指针移回文件开头 + fclose(fp); + } + file_count++; + } + } + else if (ent->d_type == DT_DIR) // 目录 + { + continue; + } + } + } + else + { + printf(" opendir fail %s\r\n",dir_path); + return file_count; + } + closedir(dir); + return file_count; +} void ble_set_password(uint8_t falg, char * data) { @@ -588,30 +726,12 @@ char * tar_dir_to_json_buf_malloc_(char* file_name,uint32_t *file_size) 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; @@ -621,6 +741,24 @@ char * tar_dir_to_json_buf_malloc_(char* file_name,uint32_t *file_size) { //*file_size=ent->d_reclen; + 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); + + memcpy(file_name,file_path,strlen(file_path)); uint8_t crc= Calculate_CRC_file(file_name); 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 3a4749d7b..bcbca3444 100644 --- a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.h +++ b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.h @@ -27,6 +27,9 @@ extern "C" #include "errcode.h" #include "bleEvent.h" + + + typedef enum transmitting_type { TRANS_DIAL_TRANSMITTING, TRANS_DIAL_SUCC, @@ -246,6 +249,13 @@ void get_tar_dir_file_count(void); uint32_t tar_rmove(char *tar_path); +uint8_t ble_get_sleep_data_dir(uint32_t *file_size,uint8_t day_); + + +uint8_t ble_get_sleep_data(uint32_t file_size,uint8_t day_,uint8_t *data); + +uint8_t ble_AT_add_nap_data(uint16_t time); + #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 56255aab2..da63cb8de 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c @@ -1717,7 +1717,9 @@ void GetVoiceVibrateParamCmd(uint16_t cmd,uint8_t *data,uint16_t len,uint8_t *to }else if(type == 1)//只改变铃声音量参数 其他还是保持不变 按照原来的参数 { int ring_volume = 0; + ring_volume = data[1]; + Set_CallVolume(ring_volume); }else if(type == 2)//只改变媒体音量 @@ -4524,8 +4526,9 @@ void type2AE3Data(ble_read_data *buffer_handle,uint8_t *cmd_buf ,uint8_t *data,u { uint16_t cmd = cmd_buf[1]*256+cmd_buf[2]; #ifdef BLE_LOG - printf("typeHexOneData:0x%04X\r\n",cmd); + #endif + printf("typeHexOneData:0x%04X\r\n",cmd); // uint8_t typeID = cmd_buf[2]; switch (cmd) { @@ -4727,10 +4730,79 @@ uint16_t max_mtu_szie_data_len = 200; uint32_t EACH_DATA_LEN=0;//每种数据类型的的长度 +void Send_Sleep_Bin_Data(uint8_t day, uint8_t num) +{ + + char *data=NULL; + uint32_t file_size=1; + uint8_t sleep_count = ble_get_sleep_data_dir(&file_size,day); + + printf("Send_Sleep_Bin_Data file_size : %d \r\n",file_size); + + data=(char *)malloc(file_size); + data[0]=sleep_count; + + if(data==NULL) + { + #ifdef BLE_LOG + printf("malloc\r\n"); + #endif + + return; + } + ble_get_sleep_data(file_size,day,(uint8_t*)data+1); + + + if(data==NULL || file_size==1) + { + send_big_data(1,NULL,0,0,1,1,0x4b,1); + sendbigDataBufState = false;//分包同步数据不允许打断 + return; + } + + uint32_t surplus_len=file_size; + uint32_t pack; + if(file_size%num==0) + { + pack=file_size/num; + } + else + { + pack=file_size/num+1; + } + //printf("%d %d %d %d\r\n",data_len,surplus_len,pack_id,pack); + uint32_t pack_id=1; + uint32_t offset=0; + while (surplus_len!=0) + { + if(surplus_len>=num) + { + surplus_len=surplus_len-num; + printf("%d %d %d %d\r\n",num,file_size,pack_id,pack); + send_big_data(1,data+offset,num,file_size,pack_id,pack,0x4b,1); + offset+=num; + } + else + { + //#ifdef BLE_LOG + printf("%d %d %d %d\r\n",surplus_len,file_size,pack_id,pack); + //#endif + send_big_data(1,data+offset,surplus_len,file_size,pack_id,pack,0x4b,1); + break; + } + pack_id++; + } + free(data); + +} + -void Read_JSON_file_buf_send_bin_Data(uint8_t day, uint8_t num,uint16_t id,char *data_item) +bool Read_JSON_file_buf_send_bin_Data(uint8_t day, uint8_t num,uint16_t id,char *data_item) { + // send_big_data(1,NULL,0,0,1,1,id,1); + // return; + char *buf=NULL; const char *path=NULL; uint8_t timeBit = 4;//默认是4个字节的时间戳 @@ -4745,21 +4817,14 @@ void Read_JSON_file_buf_send_bin_Data(uint8_t day, uint8_t num,uint16_t id,char path=get_health_OXYGEN_data_path(day); dataBit = 1;//血氧数据字节1个字节 uint8_t break; - case 0x4b://睡眠历史数据 - path=NULL; - break; } if(path==NULL) { - send_big_data(1,NULL,0,0,1,1,id,1); - sendbigDataBufState = false;//分包同步数据不允许打断 - return; + return false; } - FILE *fp= fopen(path,"r+"); if(fp==NULL) { - send_big_data(1,NULL,0,0,1,1,id,1); #ifdef BLE_LOG printf("打开文件失败 %s \r\n",path); #endif @@ -4768,11 +4833,14 @@ void Read_JSON_file_buf_send_bin_Data(uint8_t day, uint8_t num,uint16_t id,char ym_free(path); path = NULL; } - - sendbigDataBufState = false;//分包同步数据不允许打断 - return; + return false; } + buf = (char *)ym_malloc(sizeof(char)*512); + if(buf==NULL) + { + return false; + } int ret=fread(buf,1,512,fp); uint8_t count=1; while (ret==512) @@ -4781,28 +4849,39 @@ void Read_JSON_file_buf_send_bin_Data(uint8_t day, uint8_t num,uint16_t id,char ret=fread(buf+512*count,1,512,fp); count++; } + fclose(fp); + if(path) { ym_free(path); path = NULL; } + cJSON *heart_data=cJSON_Parse(buf); + + if(buf) + free(buf); + #ifdef BLE_LOG printf("读文件\r\n"); #endif if(heart_data==NULL) - { - #ifdef BLE_LOG - printf("解析失败\r\n"); - #endif - send_big_data(1,NULL,0,0,1,1,id,1); - sendbigDataBufState = false;//分包同步数据不允许打断 - return; - } + return false; cJSON * heart_data_list=cJSON_GetObjectItem(heart_data,data_item); + if(heart_data_list==NULL) + return false; int item_num=cJSON_GetArraySize(heart_data_list); + if(item_num==0) + return false; + char *send_buf=(char *)ym_malloc(sizeof(char)*HEART_DATA_LEN); + if(send_buf==NULL) + { + cJSON_Delete(heart_data); + return false; + } + int send_len=0; uint32_t Packet_id=1; uint16_t packet_total; @@ -4843,7 +4922,6 @@ void Read_JSON_file_buf_send_bin_Data(uint8_t day, uint8_t num,uint16_t id,char ym_free(send_buf); send_buf = NULL; } - sendbigDataBufState=false; }else { @@ -4866,20 +4944,14 @@ void Read_JSON_file_buf_send_bin_Data(uint8_t day, uint8_t num,uint16_t id,char ym_free(send_buf); send_buf = NULL; } - - sendbigDataBufState=false; - cJSON_Delete(heart_data); - + return true; } void Read_BIN_file_buf_send_bin_Data(uint8_t day,uint32_t pack_size,uint16_t id) { sendbigDataBufState = true;//分包同步数据不允许打断 - char *buf=NULL; const char *data=NULL; - uint8_t timeBit = 4;//默认是4个字节的时间戳 - uint8_t dataBit = 1;//默认是1个字节的数据 uint32_t data_len = 0; data=ble_sywee_history(day,&data_len,id); @@ -4890,42 +4962,39 @@ void Read_BIN_file_buf_send_bin_Data(uint8_t day,uint32_t pack_size,uint16_t id) sendbigDataBufState = false;//分包同步数据不允许打断 return; } - - - uint32_t surplus_len=data_len; - uint32_t pack; - if(data_len%pack_size==0) + uint32_t surplus_len=data_len; + uint32_t pack; + if(data_len%pack_size==0) + { + pack=data_len/pack_size; + } + else + { + pack=data_len/pack_size+1; + } + //printf("%d %d %d %d\r\n",data_len,surplus_len,pack_id,pack); + uint32_t pack_id=1; + uint32_t offset=0; + while (surplus_len!=0) + { + if(surplus_len>=pack_size) { - pack=data_len/pack_size; + surplus_len=surplus_len-pack_size; + send_big_data(1,data+offset,pack_size,data_len,pack_id,pack,id,1); + offset+=pack_size; } else - { - pack=data_len/pack_size+1; - } - //printf("%d %d %d %d\r\n",data_len,surplus_len,pack_id,pack); - uint32_t pack_id=1; - uint32_t offset=0; - while (surplus_len!=0) { - if(surplus_len>=pack_size) - { - surplus_len=surplus_len-pack_size; - send_big_data(1,data+offset,pack_size,data_len,pack_id,pack,id,1); - offset+=pack_size; - } - else - { - #ifdef BLE_LOG - printf("%d %d %d %d\r\n",data_len,surplus_len,pack_id,pack); - #endif - send_big_data(1,data+offset,surplus_len,data_len,pack_id,pack,id,1); - break; - } - pack_id++; + #ifdef BLE_LOG + printf("%d %d %d %d\r\n",data_len,surplus_len,pack_id,pack); + #endif + send_big_data(1,data+offset,surplus_len,data_len,pack_id,pack,id,1); + break; } - free(data); - sendbigDataBufState = false; + pack_id++; + } + free(data); } @@ -4959,21 +5028,25 @@ void readDevDataBle(uint16_t cmd,uint8_t *data,uint16_t len,uint8_t *totalData,u switch (bleSendTypeID) { case 0x40://同步心率历史数据 + + EACH_DATA_LEN = HEART_DATA_LEN; num=(ble_nus_max_data_len-18)/HEART_DATA_LEN; - if(num>96) - { - num=96; - } + if(num>96)num=96; #ifdef BLE_LOG printf("同步心率数据,每包最多:%d 条数据 data:%d %d\r\n",num,data[0],len); #endif - Read_JSON_file_buf_send_bin_Data(data[0],num,0x40,"heart_data_list"); + if(!Read_JSON_file_buf_send_bin_Data(data[0],num,0x40,"heart_data_list")) + send_big_data(1,NULL,0,0,1,1,0x40,1); + break; case 0x41://同步血氧历史数据 EACH_DATA_LEN = SPO2_DATA_LEN; num=(ble_nus_max_data_len-18)/EACH_DATA_LEN; - Read_JSON_file_buf_send_bin_Data(data[0],num,0x41,"spo2_data_list"); + if(num>96)num=96; + if(!Read_JSON_file_buf_send_bin_Data(data[0],num,0x41,"spo2_data_list")) + send_big_data(1,NULL,0,0,1,1,0x41,1); + break; case 0x43: //同步步数历史数据 @@ -5039,8 +5112,9 @@ void readDevDataBle(uint16_t cmd,uint8_t *data,uint16_t len,uint8_t *totalData,u break; case 0x4b://睡眠历史数据 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"); + num=(ble_nus_max_data_len-18); + if(num>482)num=482; + Send_Sleep_Bin_Data(data[0],num); break; case 0x21://文件目录 EACH_DATA_LEN = DIS_DATA_LEN; -- 2.17.1