From ed6f7c5f32f9262eb4fa735765affe09dff73a7b Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Mon, 29 Apr 2024 17:17:34 +0800 Subject: [PATCH] =?utf8?q?=E8=AE=A2=E6=AD=A3=E9=83=A8=E5=88=86=E5=86=85?= =?utf8?q?=E5=AD=98=E6=B3=84=E6=BC=8F=E4=BB=A5=E5=8F=8A=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../bloodoxygen/src/BloodOxygenPresenter.cpp | 16 ++++- .../nativeui/heartrate/src/HeartPresenter.cpp | 12 +++- .../nativeui/main/src/dial/DialModelTime.cpp | 8 +++ .../nativeui/traing/src/TraingModel.cpp | 67 +++++++------------ .../include/TraingFitnessModel.h | 8 ++- .../src/TraingFitnessModel.cpp | 2 +- .../phoneservice/src/PhoneService.cpp | 2 +- 7 files changed, 65 insertions(+), 50 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/src/BloodOxygenPresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/src/BloodOxygenPresenter.cpp index 6884a28df..c290b758e 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/src/BloodOxygenPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/src/BloodOxygenPresenter.cpp @@ -28,6 +28,8 @@ #include "graphic_service.h" #include #include +#include + #include "ppg_service.h" #include "displaybrightness/include/DisplayBrightnessModel.h" #include "power_display_service.h" @@ -45,12 +47,20 @@ namespace OHOS // 获取当前的"时间分" uint64_t BloodOxygenPresenter::GetCurrentTime() { - time_t now = time(0); - struct tm *tblock = localtime(&now); - uint64_t current_time = tblock->tm_hour * 60 + tblock->tm_min; + // time_t now = time(0); + // struct tm *tblock = localtime(&now); + + /*优化为更安全的localtime_r()*/ + struct timeval tv; + gettimeofday(&tv, NULL); + struct tm tblock; + localtime_r(&tv.tv_sec, &tblock); + + uint64_t current_time = tblock.tm_hour * 60 + tblock.tm_min; return current_time; } + // 获取时间差,现在传进来的是两个时间戳 std::string BloodOxygenPresenter::GetTDOA(uint64_t new_time, uint64_t old_time) { diff --git a/code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartPresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartPresenter.cpp index 50dce11b8..929a961d2 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartPresenter.cpp @@ -40,10 +40,16 @@ namespace OHOS uint64_t HeartPresenter::GetCurrentTime() { - time_t now = time(0); - struct tm *tblock = localtime(&now); + // time_t now = time(0); + // struct tm *tblock = localtime(&now); - uint64_t current_time = tblock->tm_hour * 60 + tblock->tm_min; + /*优化为更安全的localtime_r()*/ + struct timeval tv; + gettimeofday(&tv, NULL); + struct tm tblock; + localtime_r(&tv.tv_sec, &tblock); + + uint64_t current_time = tblock.tm_hour * 60 + tblock.tm_min; return current_time; } diff --git a/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelTime.cpp b/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelTime.cpp index dbc4efff6..d95831ea9 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelTime.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelTime.cpp @@ -70,10 +70,18 @@ static float ConvertHandSecondValueToAngle(int32_t handValue, uint16_t range) void DialModelTime::UpdateTime() { +#if 0 struct timeval tv; gettimeofday(&tv, NULL); tblock_ = localtime(&tv.tv_sec); int realMS = tblock_->tm_sec * S_MS_US_TRANS + tv.tv_usec / S_MS_US_TRANS; +#endif + /*优化为更安全的localtime_r()*/ + struct timeval tv; + gettimeofday(&tv, NULL); + struct tm tblock; + localtime_r(&tv.tv_sec, &tblock); + int realMS = tblock.tm_sec * S_MS_US_TRANS + tv.tv_usec / S_MS_US_TRANS; angleH_ = ConvertHandValueToAngle(tblock_->tm_hour, HALF_DAY_IN_HOUR, tblock_->tm_min, ONE_HOUR_IN_MINUTE); angleM_ = ConvertHandValueToAngle(tblock_->tm_min, ONE_HOUR_IN_MINUTE, tblock_->tm_sec, ONE_MINUTE_IN_SECOND); diff --git a/code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp index f918a57ba..f5fee7eb3 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp @@ -873,74 +873,58 @@ namespace OHOS FILE *fp = fopen(file_path, "ab"); if (fp == NULL) { - fopen(file_path, "wb+"); + // 未存储分配函数funcName的返回值 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for append"); + 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 + } } -#if HARD_OFF - int ret = 0; - ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp); +#if HARD_OFF + int ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp); if (ret != 1) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_data is error! %d", ret); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_data error! %d", ret); } #else - if (fp == NULL) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL"); - } Trainingdata_hard hard = {0}; fseek(fp, 0, SEEK_SET); int ret = fread((void *)&hard, sizeof(Trainingdata_hard), 1, fp); - - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "hard num %d", hard.num); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fread %d", ret); if (ret == 0) { + // Write new header if it doesn't exist hard.num = 1; hard.st = timeday; fseek(fp, 0, SEEK_SET); ret = fwrite((void *)&hard, sizeof(Trainingdata_hard), 1, fp); - - if (ret != 1) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard %d", ret); - } - - ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp); if (ret != 1) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "1 fwrite_data %d", ret); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard error! %d", ret); } } else { - ret = fseek(fp, 8, SEEK_END); - if (ret != 0) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fseek %d", ret); - } - hard.num++; - ret = fwrite((void *)&(hard), sizeof(Trainingdata_hard), 1, fp); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "hard num %d", hard.num); - if (ret) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard %d", ret); - } - fseek(fp, 0, SEEK_END); - // fclose(fp); - - // fp= fopen(file_path,"ab+"); - ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp); + hard.num++; + ret = fwrite((void *)&hard, sizeof(Trainingdata_hard), 1, fp); if (ret != 1) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "2 fwrite_data %d", ret); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard error! %d", ret); } } + fseek(fp, 0, SEEK_END); + ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp); + if (ret != 1) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_data error! %d", ret); + } #endif - fseek(fp, 0, SEEK_SET); - fclose(fp); - return 1; + + fclose(fp); // Close the file after writing data + return 0; // Return success code } // 读数据 Trainingdata *read_Trainingdata_data(char *timedata, uint16_t offset) @@ -953,6 +937,7 @@ namespace OHOS if (fp == NULL || get_Trainingdata_data_num(fp) < offset) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL %d", get_Trainingdata_data_num(fp)); + free(data); // 修复内存泄漏 return NULL; } #if HARD_OFF diff --git a/code/sdk/application/wearable/fd230app/nativeui/traing/training_list_fitness/include/TraingFitnessModel.h b/code/sdk/application/wearable/fd230app/nativeui/traing/training_list_fitness/include/TraingFitnessModel.h index e76735bb5..4ab8bc347 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/traing/training_list_fitness/include/TraingFitnessModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/traing/training_list_fitness/include/TraingFitnessModel.h @@ -1,3 +1,9 @@ +/*** + * @Date: 2024-04-15 10:55:58 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-04-29 17:02:28 + * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/traing/training_list_fitness/include/TraingFitnessModel.h + */ #ifndef TraingFitness_MODEL_H #define TraingFitness_MODEL_H @@ -25,7 +31,7 @@ namespace OHOS TraingFitnessModel(const TraingFitnessModel &); TraingFitnessModel &operator=(const TraingFitnessModel &); virtual ~TraingFitnessModel(); - static const uint16_t TraingFitnessListMaxNum = 58; + static const uint16_t TraingFitnessListMaxNum = 57; TraingFitnessItem TraingFitnesss[TraingFitnessListMaxNum] = {VIEW_INVALID, STR_EXERCISE_GENERAL}; // std::map TraingFitnessIdToViewId; bool btConnectStatus{false}; diff --git a/code/sdk/application/wearable/fd230app/nativeui/traing/training_list_fitness/src/TraingFitnessModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/traing/training_list_fitness/src/TraingFitnessModel.cpp index 174797951..2a1d164ca 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/traing/training_list_fitness/src/TraingFitnessModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/traing/training_list_fitness/src/TraingFitnessModel.cpp @@ -142,7 +142,7 @@ namespace OHOS TraingFitnesss[54] = {VIEW_TRAININGNOMAL, text[54]}; TraingFitnesss[55] = {VIEW_TRAININGNOMAL, text[55]}; TraingFitnesss[56] = {VIEW_TRAININGNOMAL, text[56]}; - TraingFitnesss[57] = {VIEW_TRAININGNOMAL, text[57]}; + //访问的数组超过了数组容量 } const TraingFitnessItem *TraingFitnessModel::GetTraingFitnesslistItems() const diff --git a/code/sdk/application/wearable/nativeapp/nativeui/phoneservice/src/PhoneService.cpp b/code/sdk/application/wearable/nativeapp/nativeui/phoneservice/src/PhoneService.cpp index bd9effd0b..c781f1f74 100755 --- a/code/sdk/application/wearable/nativeapp/nativeui/phoneservice/src/PhoneService.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/phoneservice/src/PhoneService.cpp @@ -1044,7 +1044,7 @@ extern "C" *info = NULL; *num = 0; fclose(fp); - return OHOS_SUCCESS; + return OHOS_FAILURE;//文件为空,返回失败 } number = fileSize / sizeof(CallLogInfo); -- 2.17.1