data.std = static_cast<uint32_t>(sportdata->std);
data.cdc = static_cast<uint32_t>(sportdata->cdc);
+ // 创建sport文件夹
+ CreateDirectoryIfNotExists(TRAINING_DATA_DIRECTORY);
+
// 保存数据到 bin
if (write_Trainingdata_data(data) != 1) {
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "write_Trainingdata_data is error!");
#ifdef __cplusplus
extern "C" {
#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+
// 获得数据个数
static int get_Trainingdata_data_num(FILE *fp) {
- long size = 0;
- if (fp != NULL) {
- fseek(fp, 0, SEEK_END); // 将文件指针移动到文件末尾
- size = ftell(fp); // 获取当前文件指针位置,即文件大小
- fseek(fp, 0, SEEK_SET); // 将文件指针移回文件开头
+ if (fp == NULL) {
+ return 0; // 如果文件指针为空,返回0
}
- int num = size / sizeof(Trainingdata);
- return num;
+
+ fseek(fp, 0, SEEK_END); // 将文件指针移动到文件末尾
+ long size = ftell(fp); // 获取当前文件指针位置,即文件大小
+ fseek(fp, 0, SEEK_SET); // 将文件指针移回文件开头
+
+ return (size / sizeof(Trainingdata));
}
+
+// 写训练bin文件
int write_Trainingdata_data(Trainingdata data) {
char file_path[MAX_FILE_PATH_LENGTH] = {0};
char timeday[12] = {0};
uapi_get_YYMMDD_Time(timeday, '_');
- sprintf_s(file_path, sizeof(file_path), "%s%s.bin", TRAINING_PATH,
- timeday); // 防止越界
+ snprintf(file_path, sizeof(file_path), "%s%s.bin", TRAINING_PATH, timeday);
- // Check if the directory exists, if not, create it
+ // 检查目录是否存在,如果不存在则创建
struct stat st = {0};
if (stat(TRAINING_PATH, &st) == -1) {
- mkdir(TRAINING_PATH, 0700);
+ if (mkdir(TRAINING_PATH, 0700) != 0) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create directory: %s",
+ strerror(errno));
+ return -1; // 返回错误码
+ }
}
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%s", file_path);
FILE *fp = fopen(file_path, "ab+");
if (fp == NULL) {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for append");
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for append: %s",
+ strerror(errno));
fp = fopen(file_path, "wb+");
if (fp == NULL) {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for write");
- return -1; // Return an error code to indicate file open failure
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+ "Failed to open file for write: %s", strerror(errno));
+ return -1; // 返回错误码
}
}
if (ret != 1) {
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_data error! %d", ret);
fclose(fp);
- return -1; // Return an error code on write failure
+ return -1; // 返回错误码
}
- fclose(fp); // Close the file after writing data
- return 1; // Return success code
+ fclose(fp); // 关闭文件
+ return 1; // 返回成功码
}
+
// 读数据
Trainingdata *read_Trainingdata_data(char *timedata, uint16_t offset) {
char file_path[128] = {0};
- sprintf_s(file_path, sizeof(file_path), "%s%s.bin", TRAINING_PATH,
- timedata); // 防止越界
+ snprintf(file_path, sizeof(file_path), "%s%s.bin", TRAINING_PATH, timedata);
- FILE *fp = fopen(file_path, "rb+");
+ FILE *fp = fopen(file_path, "rb");
if (fp == NULL) {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL");
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for read: %s",
+ strerror(errno));
return NULL;
}
+
Trainingdata *data = (Trainingdata *)malloc(sizeof(Trainingdata));
if (data == NULL) {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Trainingdata malloc is error");
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+ "Failed to allocate memory for Trainingdata");
fclose(fp);
return NULL;
}
- if (get_Trainingdata_data_num(fp) <= offset) {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL %d",
- get_Trainingdata_data_num(fp));
- free(data); // 修复内存泄漏
+
+ int num_data = get_Trainingdata_data_num(fp);
+ if (num_data <= offset) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+ "Offset %d exceeds number of data %d", offset, num_data);
+ free(data); // 释放内存
fclose(fp);
return NULL;
}
+
fseek(fp, sizeof(Trainingdata) * offset, SEEK_SET);
- fread((void *)data, sizeof(Trainingdata), 1, fp);
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "%d", data->src);
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "%d %d ", offset,
+ fread(data, sizeof(Trainingdata), 1, fp);
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Data src: %d", data->src);
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Offset: %d, Size: %zu", offset,
sizeof(Trainingdata));
- fseek(fp, 0, SEEK_SET);
- fclose(fp);
- return data;
+ fclose(fp); // 关闭文件
+ return data; // 返回读取的数据
}
+
#ifdef __cplusplus
}
#endif
bool TraingModel::SaveDataToJson(SportData *data) {
FILE *fp;
char *jsonStr = NULL;
- // 创建sport文件夹
- CreateDirectoryIfNotExists(TRAINING_DATA_DIRECTORY);
// 读取现有的 JSON 数据
fp = fopen(TRAINING_RECORD_FILE, "r");