extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
-
+#include "custom_utils.h"
#include "pinctrl.h"
#include "debug_print.h"
#include "soc_osal.h"
#include "bletest.h"
#include <cywee_hal.h>
+#include <dirent.h>
+
void ble_set_password(uint8_t falg, char * data)
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)
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;
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)
{
///////////////////////手表密码//////////////////////////
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
}
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
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
}\r
\r
}\r
+\r
+\r
static uint8_t Calculate_CRC(uint8_t *data,uint16_t len)//计算20byte\r
{\r
uint8_t crcbuf = 0;\r
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
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
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
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
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
#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
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