From 9382e96247063118a8f1a7cf1a0bbd2aa3c643a4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E9=82=93=E9=9B=AA=E8=8B=B1?= <1731678236@qq.com> Date: Mon, 29 Apr 2024 06:52:50 +0000 Subject: [PATCH] Merge #111 into xts_origin from refs/changes/111/head MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Code review from 邓雪英 * refs/changes/111/head: (2 commits squashed) - 解析协议相关内容 - Merge branch 'xts_origin' of codeup.aliyun.com:64191456b9581d62e8f81ac0/FD230 into xts_origin Signed-off-by: d15521469561 <1731678236@qq.com> Reviewed-by: snickersun@163.com Merged-by: snickersun@163.com CR-link: https://codeup.aliyun.com/64191456b9581d62e8f81ac0/FD230/change/111 --- .../service/ble_service/blemoder/bletest.cpp | 85 ++++++ .../service/ble_service/blemoder/bletest.h | 28 +- .../service/ble_service/bleservice/bleEvent.c | 288 +++++++++++------- .../service/ble_service/bleservice/bleEvent.h | 7 + 4 files changed, 295 insertions(+), 113 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 cee04da9f..3ade5fb86 100644 --- a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp +++ b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp @@ -256,6 +256,91 @@ void set_weather_one_data(int tempMin, int iconDay,int fxDate, int tempMax, char if(x==6) return UI_set_weather_seven_day_data(data_t); } +struct message_infor_t message_infor_s; +void read_message_data(void) +{ + if(!creatMessageDir())//创建失败 + { + return; + } + char path[100]; + uint32_t data_len = 0; + char *str_data = get_message_dir_path(); + sprintf(path,"%s%s",str_data,ANCS_DATA_CJSON); + printf("buf file %s\n",path); + FILE *jsfile = fopen(path, "r"); + cJSON *data_root; + if (jsfile != NULL) { + printf("succes opening file\n"); + fseek(jsfile, 0, SEEK_END); + long fileSize = ftell(jsfile); + fseek(jsfile, 0, SEEK_SET); + + char *jsonData = (char *)malloc(fileSize + 1); + fread(jsonData, 1, fileSize, jsfile); + jsonData[fileSize] = '\0'; + fclose(jsfile); + + // 解析JSON数据 + printf("jsonData %s",jsonData); + data_len = strlen(jsonData); + data_root= cJSON_Parse(jsonData); + free(jsonData); + if(data_len > 0)//只有读出来的内容不为空就认为文件里面有数据 + { + cJSON *dataList = cJSON_GetObjectItem(data_root, "infor"); + if (dataList != NULL && cJSON_IsArray(dataList))//判断是否为NULL并且是否为数组类型的内容 + { + int count = cJSON_GetArraySize(dataList);//获取数组的大小 10//9 8 7 6 5 4 3 2 1 0 + memset(&message_infor_s,0,sizeof(message_infor_t)); + message_infor_s.num = count; + for (uint8_t i = 0; i < count; i++)//反序存储是反序 所以读出来显示也需要反序度 + { + cJSON *object = NULL; + cJSON *uid_t = NULL; + cJSON *appid_t = NULL; + cJSON *title_buf = NULL; + cJSON *msg_buf = NULL; + cJSON *time = NULL; + cJSON *hasCode = NULL; + cJSON *read_t = NULL; + cJSON *detialJSON = cJSON_CreateObject(); + object = cJSON_GetArrayItem(dataList,i);//1 0 + uid_t = cJSON_GetObjectItem(object, "uid"); + appid_t = cJSON_GetObjectItem(object, "appid_t"); + title_buf = cJSON_GetObjectItem(object, "title"); + msg_buf = cJSON_GetObjectItem(object, "msg"); + time = cJSON_GetObjectItem(object, "time"); + hasCode = cJSON_GetObjectItem(object, "hasCode"); + read_t = cJSON_GetObjectItem(object, "read"); + message_infor_s.message_t[count - 1- i].uid = uid_t->valueint;// + message_infor_s.message_t[count - 1- i].appid = appid_t->valueint; + if(strlen(title_buf->valuestring) == 0) + { + memcpy(message_infor_s.message_t[count - 1- i].title,title_buf->valuestring,strlen(title_buf->valuestring)); + }else{ + memset(message_infor_s.message_t[count - 1- i].title,0,TITLE_BUF_LEN); + + } + + if(strlen(msg_buf->valuestring) == 0) + { + memcpy(message_infor_s.message_t[count - 1- i].msg,msg_buf->valuestring,strlen(msg_buf->valuestring)); + }else{ + memset(message_infor_s.message_t[count - 1- i].msg,0,MSG_BUF_LEN); + } + message_infor_s.message_t[count - 1- i].time = time->valueint; + message_infor_s.message_t[count - 1- i].hasCode = hasCode->valueint; + message_infor_s.message_t[count - 1- i].read = read_t->valueint; + } + } + } + } +} +message_infor_t *get_message_info(void) +{ + return &message_infor_s; +} #ifdef __cplusplus #if __cplusplus } 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 227c99419..5eef800f5 100644 --- a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.h +++ b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.h @@ -27,15 +27,7 @@ extern "C" #include "errcode.h" #include "bleEvent.h" -struct ancs_data_info_t -{ - int uid;//uid 消息唯一编号 - int appid;//消息类型:1:微信 2:QQ 3:短信 4:其他 - char title[256];//消息发送者 - char msg[300];//消息内容 - char time[20];//时间戳"20240417T102400" 用T分隔日期和时间,显示需要注意 - int hasCode;//消息hash code -}; + enum PAIRING { @@ -77,7 +69,25 @@ typedef struct uint32_t sportTag; //训练目标 uint8_t controlmd; //训练控制md } training_ble; +#define MAX_MESSAGE_NUM 10 +#define TITLE_BUF_LEN 256 +#define MSG_BUF_LEN 301 +struct ancs_data_info_t +{ + uint32_t time;//时间戳 + int uid;//uid 消息唯一编号 + int appid;//消息类型:1:微信 2:QQ 3:短信 4:其他 + char title[TITLE_BUF_LEN];//消息发送者 + char msg[MSG_BUF_LEN];//消息内容 + int hasCode;//消息hash code + int read;//是否已读 0 未读 1 已读 +}; +struct message_infor_t +{ + uint8_t num; + struct ancs_data_info_t message_t[MAX_MESSAGE_NUM]; +}; void set_weather_now_data(int humidity, char* windScale, char* textNight,int uvIndex,int temp,int iconNight); void set_weather_address_data(int lat, char* atm, char* city,char* subLocality,int lon); 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 cb2947805..7cc91b471 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c @@ -31,15 +31,15 @@ extern "C" { #include "bleEvent.h" #include "ppg_service.h" #include "broadcast_feature.h" +#include #if 1//#ifdef //SUPPORT_BLE_SERVICE #include "app_common_api.h" #include "hidService.h" #include "../../../../../open_source/openharmony/third_party/cJSON/cJSON.h" #include "errcode.h" -#define PHONE_WATCH_CONFIG_PATH "/user/userdata/phone_watch.json" -#define ALARM_DATA_CONFIG_PATH "/user/userdata/alarm_test.json" -#define ANCS_DATA_CONFIG_PATH "/user/userdata/ancs.json" + #include "bletest.h" + #define DEBUG_TEST (0) #define CRC_CHECK_ERR 0 //crc校验失败 #define JSON_CHECK_OK 1 //json解析成功 @@ -181,6 +181,15 @@ static bool Check_CRC(uint8_t *crcnum,uint16_t len,uint8_t crcvalue)//计算20by { uint8_t crcbuf = 0; uint8_t lrcValue = 0x00; + if(crcnum == NULL) + { + if(crcvalue == 0x00) + { + return true; + }else{ + return false; + } + } for(uint16_t i = 0; i < len; i++) { lrcValue =lrcValue+ crcnum[i]; @@ -449,7 +458,7 @@ errcode_t UnbindSucessCmd(uint8_t flag)//发送解绑指令,解绑界面调用 gap_remove_all_pairs(); } - // uapi_reboot_device(); + uapi_reboot_device(); return sendok; } @@ -475,6 +484,8 @@ void clearPaireFlag(uint8_t flag) value[17] = flag; value[18] = Calculate_CRC(value,18); sendBleData(value,19); + + UnbindSucessCmd(1);//1确认解绑; } void clearPaireCmd(uint16_t cmd,uint8_t *crcnum,uint16_t len,uint8_t *totaldata,uint8_t crcvalue)//发送清除配对指令 { @@ -3210,20 +3221,10 @@ static void GetAllDialUidCmd(uint16_t cmd,uint8_t *crcnum,uint16_t len,uint8_t * memcpy(value+15,&tmp_data,2); if(1) { - uint8_t sleepTime = 0;//作息时间设定 0关闭 1开启 - uint8_t bedTimeHour = 0;//就寝时间 - uint8_t bedTimeMin = 0;//就寝时间 - uint8_t wakeTimeHour = 0;//起床时间 - uint8_t wakeTimeMin = 0;//起床时间 - uint8_t bedTimeOn = 0;//就寝前20分钟提醒时间 - value[17] = sleepTime; - value[18] = bedTimeHour; - value[19] = bedTimeMin; - value[20] = wakeTimeHour; - value[21] = wakeTimeMin; - value[22] = bedTimeOn; - value[23] = Calculate_CRC(value,23); - sendBleData(value,24); + + value[17] = 0; + value[18] = Calculate_CRC(value,18); + sendBleData(value,19); }else { value[17] = 0; @@ -4205,21 +4206,145 @@ void bleLongRecvData(uint8_t *data, uint16_t data_size,uint8_t *buf_cmd,uint8_t { uint8_t type=buf_cmd[1]; printf("type:%d %d data_size:%d data:%d crcvalue:%x",buf_cmd[0],type,data_size,data[0],crcvalue); - // FILE *fp= fopen("user/ble_test.json","w+"); - // fwrite((void *)data,1,data_size,fp); - // fclose(fp); switch (type) { case 0x02://获取手机信息 type2AE4Data(buf_cmd,data,data_size,totalData,crcvalue); break; - // case 0x02://获取手机信息 - // type2AE4Data(buf_cmd,data,data_size,totalData,crcvalue); - // break; default: break; } } +bool creatMessageDir(void)//创建消息文件夹 +{ + struct stat info; + if (stat(ANCS_DATA_CONFIG_PATH, &info) != 0) + { + if (mkdir(ANCS_DATA_CONFIG_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) + { + return false; // Failed to create folder + } + } + return true; +} +char *get_message_dir_path(void)//获取信息的文件夹路径 +{ + return ANCS_DATA_CONFIG_PATH; +} +void message_data(int uid,int appid,char *title,char *msg,int hasCode) +{ + if(!creatMessageDir())//创建文件夹失败 + { + return; + } + uint8_t buf_size = 0; + char *path; + path = malloc(100); + if(path == NULL) + { + return; + } + char *str_data = get_message_dir_path(); + sprintf(path,"%s%s",str_data,ANCS_DATA_CJSON); + printf("creat file %s\n",path); + FILE *jsfile = fopen(path, "r"); + cJSON *data_root; + if (jsfile != NULL) { + printf("succes opening file\n"); + fseek(jsfile, 0, SEEK_END); + long fileSize = ftell(jsfile); + fseek(jsfile, 0, SEEK_SET); + + char *jsonData = (char *)malloc(fileSize + 1); + fread(jsonData, 1, fileSize, jsfile); + jsonData[fileSize] = '\0'; + fclose(jsfile); + + // 解析JSON数据 + printf("jsonData %s",jsonData); + data_root= cJSON_Parse(jsonData); + // data_root = cJSON_GetObjectItem(modifiedJson,"infor"); + free(jsonData); + }else{ + data_root = cJSON_Parse("{\ + \"infor\": [\ + ]}"); + } + cJSON *dataJSON = cJSON_CreateObject(); + + cJSON *detialJSON = cJSON_CreateObject(); + + cJSON* itemJSON = cJSON_CreateObject(); + + uint32_t timeBuf = 0; + if (uapi_get_timestampe_ss(&timeBuf) != ERRCODE_SUCC) + { + timeBuf = 0; + } + cJSON_AddNumberToObject(itemJSON,"uid",uid); + cJSON_AddNumberToObject(itemJSON,"appid_t",appid);//消息类型 微信 qq 等看结构体或者协议定义 + cJSON_AddStringToObject(itemJSON,"title",title);//消息发送者 + cJSON_AddStringToObject(itemJSON,"msg",msg);//消息内容 + cJSON_AddNumberToObject(itemJSON,"time",timeBuf);//时间戳 + cJSON_AddNumberToObject(itemJSON,"hasCode",hasCode);//暂时不需要预留接口 + cJSON_AddBoolToObject(itemJSON,"read",false);//是否已读 false未读 1 已读默认存的时候已经未读 + cJSON *dataList = cJSON_GetObjectItem(data_root, "infor"); + if (dataList != NULL && cJSON_IsArray(dataList)) + { + int numData = cJSON_GetArraySize(dataList); + if(numData >= MAX_MESSAGE_NUM)//大于20条就循环迭代显示并保存数据 + { + for (uint8_t i = MAX_MESSAGE_NUM-1; i > 0; i--)//2-1 1 0 + { + // printf("ancs !!!!!!!!!!!!\r\n"); + cJSON *object = NULL; + cJSON *uid = NULL; + cJSON *appid_t = NULL; + cJSON *title = NULL; + cJSON *msg = NULL; + cJSON *time = NULL; + cJSON *hasCode = NULL; + cJSON *read = NULL; + cJSON* deitemJSON = cJSON_CreateObject(); + cJSON *detialJSON = cJSON_CreateObject(); + object = cJSON_GetArrayItem(dataList,i);//1 0 + uid = cJSON_GetObjectItem(object, "uid"); + appid_t = cJSON_GetObjectItem(object, "appid_t"); + title = cJSON_GetObjectItem(object, "title"); + msg = cJSON_GetObjectItem(object, "msg"); + time = cJSON_GetObjectItem(object, "time"); + hasCode = cJSON_GetObjectItem(object, "hasCode"); + read = cJSON_GetObjectItem(object, "read"); + printf("ancs title->valuestring:%s msg->valuestring:%s\r\n",title->valuestring,msg->valuestring); + cJSON_AddNumberToObject(deitemJSON,"uid",uid->valueint); + cJSON_AddNumberToObject(deitemJSON,"appid_t",appid_t->valueint);//消息类型 微信 qq 等看结构体或者协议定义 + cJSON_AddStringToObject(deitemJSON,"title",title->valuestring);//消息发送者 + cJSON_AddStringToObject(deitemJSON,"msg",msg->valuestring);//消息内容 + cJSON_AddNumberToObject(deitemJSON,"time",time->valueint);//时间戳 + cJSON_AddNumberToObject(deitemJSON,"hasCode",hasCode->valueint);//暂时不需要预留接口 + cJSON_AddBoolToObject(deitemJSON,"read",read->valueint);//是否已读 false未读 1 已读默认存的时候已经未读 + cJSON_ReplaceItemInArray(dataList, i-1, deitemJSON); + } + cJSON_ReplaceItemInArray(dataList,MAX_MESSAGE_NUM-1, itemJSON); + }else{ + cJSON_AddItemToArray(dataList, itemJSON); + } + } + char *jsonString = cJSON_Print(data_root); + printf("ancs %s",jsonString); + FILE *file = fopen(path, "wb"); + if (file) + { + fwrite(jsonString, 1, strlen(jsonString), file); + fclose(file); + } + free(jsonString); + if(path != NULL) + { + free(path); + } + +} //安卓消息接收解析数据 //ANCS 2AE1 uint8_t jscheck_ancs(uint8_t *data) @@ -4229,19 +4354,28 @@ uint8_t jscheck_ancs(uint8_t *data) json = data; cJSON* root = cJSON_Parse((const char *)json); if (root == NULL) { - printf("信息解析 JSON解析\n"); + printf("信息解析 JSON解析 失败\n"); return 2; } + int uid_t = 0; int appid_t = 0; - char titleBuf[256]; - char msgBuf[300]; - char timeBuf[32]; int hasCode_t = 0; + char *titleBuf; + char *msgBuf; + titleBuf = malloc(TITLE_BUF_LEN); + msgBuf = malloc(MSG_BUF_LEN); + if(titleBuf == NULL || msgBuf == NULL)//申请内存失败不在申请 + { + return 2; + } + memset(titleBuf,0,TITLE_BUF_LEN); + memset(msgBuf,0,MSG_BUF_LEN); cJSON* uid = cJSON_GetObjectItem(root, "uid"); if (cJSON_IsNumber(uid)) { printf("uid_t: %d\n", uid->valueint); + uid_t = uid->valueint; } cJSON* appid = cJSON_GetObjectItem(root, "appid"); if (cJSON_IsNumber(appid)) { @@ -4265,10 +4399,6 @@ uint8_t jscheck_ancs(uint8_t *data) if (cJSON_IsString(time)) { printf("time: %s\n", time->valuestring); // timeBuf = time->valueint; - time->valuestring[strlen(time->valuestring)]='\0'; - memcpy(timeBuf,time->valuestring,strlen(time->valuestring)); - - } cJSON* hasCode = cJSON_GetObjectItem(root, "hasCode"); if (cJSON_IsNumber(hasCode)) { @@ -4276,61 +4406,13 @@ uint8_t jscheck_ancs(uint8_t *data) hasCode_t = hasCode->valueint; } cJSON_Delete(root); - uint8_t buf_size = 0; - FILE *jsfile = fopen(ANCS_DATA_CONFIG_PATH, "r"); - cJSON *data_root; - if (jsfile != NULL) { - printf("succes opening file\n"); - fseek(jsfile, 0, SEEK_END); - long fileSize = ftell(jsfile); - fseek(jsfile, 0, SEEK_SET); - - char *jsonData = (char *)malloc(fileSize + 1); - fread(jsonData, 1, fileSize, jsfile); - jsonData[fileSize] = '\0'; - fclose(jsfile); - - // 解析JSON数据 - printf("jsonData %s",jsonData); - data_root= cJSON_Parse(jsonData); - // data_root = cJSON_GetObjectItem(modifiedJson,"infor"); - free(jsonData); - }else{ - data_root = cJSON_Parse("{\ - \"infor\": [\ - ]}"); + message_data(uid_t,appid_t,title_c,msgBuf,hasCode); + if(title_c != NULL){ + free(title_c); } - cJSON *dataJSON = cJSON_CreateObject(); - - cJSON *detialJSON = cJSON_CreateObject(); - - cJSON* itemJSON = cJSON_CreateObject(); - - - - cJSON_AddNumberToObject(itemJSON,"uid",uid_t); - cJSON_AddNumberToObject(itemJSON,"appid_t",appid_t); - cJSON_AddStringToObject(itemJSON,"title",titleBuf); - cJSON_AddStringToObject(itemJSON,"msg",msgBuf); - cJSON_AddStringToObject(itemJSON,"time",timeBuf); - cJSON_AddNumberToObject(itemJSON,"hasCode",hasCode_t); - cJSON *dataList = cJSON_GetObjectItem(data_root, "infor"); - // printf("dataList %s",dataList); - cJSON *array = cJSON_GetObjectItemCaseSensitive(data_root, "infor"); - cJSON_AddItemToArray(array, itemJSON); - - // char *modified_json = cJSON_Print(data_root); - - - char *jsonString = cJSON_Print(data_root); - printf("ancs %s",jsonString); - FILE *file = fopen(ANCS_DATA_CONFIG_PATH, "wb"); - if (file) - { - fwrite(jsonString, 1, strlen(jsonString), file); - fclose(file); + if(msgBuf != NULL){ + free(msgBuf); } - free(jsonString); return 1; } void ancsDataShowCmd(uint16_t cmd,uint8_t *crcnum,uint16_t len,uint8_t *totalData,uint8_t crcvalue)// @@ -4377,7 +4459,7 @@ void bleRecvANCSData(uint8_t *data, uint16_t data_size,uint8_t *buf_cmd,uint8_t printf("type:%d %d data_size:%d data:%d crcvalue:%x",buf_cmd[0],type,data_size,data[0],crcvalue); switch (type) { - case 0x03://获取手机信息 + case 0x03:// type2AE1Data(buf_cmd,data,data_size,totalData,crcvalue); break; default: @@ -4446,34 +4528,32 @@ void bleDataRecv(uint8_t *totalData, uint16_t event_size,uint16_t handle) memset(cmd_data,0,17); memcpy(cmd_data,totalData,17); totalpack=1; - break; }else{//直接发送数据解析 - printf("muilt totalDataLen11;%d packdataLen:%d",totalDataLen,packdataLen); muiltFlag = 0; packdataLen = event_size-17; memset(cmd_data,0,17); memcpy(cmd_data,totalData,17); - - // 73 2 10 1 0 0 0 16 0 0 0 1 0 0 0 16 0 - recvData = malloc(packdataLen-1); - if(recvData == NULL)return; - memcpy(recvData,totalData+17,packdataLen-1); - if(totalDataLen == 0){ - crcvalue = 0x00; - }else{ + printf("muilt totalDataLen11;%d packdataLen:%d",totalDataLen,packdataLen); + if(totalData == 0 && event_size == 18)//数据长度为0 没数据数据包 + { crcvalue = totalData[event_size-1]; + bleLongRecvData(NULL,0,cmd_data,totalData,crcvalue); + }else{ + // 73 2 10 1 0 0 0 16 0 0 0 1 0 0 0 16 0 + recvData = malloc(packdataLen-1); + if(recvData != NULL){ + memcpy(recvData,totalData+17,packdataLen-1); + crcvalue = totalData[event_size-1]; + bleLongRecvData(recvData,packdataLen-1,cmd_data,totalData,crcvalue); + if(recvData != NULL){ + free(recvData); + } + packdataLen = 0; + } } - - bleLongRecvData(recvData,packdataLen-1,cmd_data,totalData,crcvalue); - if(recvData != NULL){ - free(recvData); - } - packdataLen = 0; - break; } } break; - }else//这是接收两包以上的数据 { totalpack+=1;//包数 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 6c90bac78..005c1c885 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.h +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.h @@ -9,9 +9,16 @@ extern "C" { #include "stdbool.h" #include "errcode.h" #include "bts_def.h" +#define PHONE_WATCH_CONFIG_PATH "/user/userdata/phone_watch.json" +#define ALARM_DATA_CONFIG_PATH "/user/userdata/alarm_test.json" +#define ANCS_DATA_CONFIG_PATH "/user/userdata/message" + +#define ANCS_DATA_CJSON "ancs.json" //消息文件可以任意更改 #define READ_FLASH_DATA 0x01 //读flash数据 #define WRITE_FLASH_DATA 0x02 //写flash数据 +bool creatMessageDir(void);//创建消息文件夹 +char *get_message_dir_path(void); typedef struct { uint8_t *data; /* data */ uint16_t data_len; /* data_len 数据长度 */ -- 2.17.1