From: Caisking <3449750381@qq.com> Date: Fri, 6 Dec 2024 09:17:18 +0000 (+0800) Subject: 表盘:优化表盘map存储 X-Git-Tag: v1.3.113-repair_tp~141^2~34 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=ea1e7558a647a3429092ba3a9e951f5e82118f3c;p=mirrors%2FFD230.git 表盘:优化表盘map存储 --- diff --git a/code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp index 0d6040fa2..65e11d06a 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp @@ -2,6 +2,8 @@ #include "DialBinParser.h" #include "MainModel.h" #include "NativeAbility.h" +#include "bleEvent.h" +#include "bleService.h" #include "clock/include/MainClockView.h" #include "custom_runtime.h" #include "custom_utils.h" @@ -23,8 +25,6 @@ #include #include #include -#include "bleEvent.h" -#include "bleService.h" extern "C" { void check_dial_space() { OHOS::SetDialModel::GetInstance().CheckDialSpace(); } } @@ -45,60 +45,83 @@ std::unordered_map registeredDials{ }; // 表盘市场UID 对应表盘name +// 存储表盘到文件 bool SetDialModel::saveDialfile() { - FILE *fp = fopen(DIAL_ID_PATH, "wb+"); + FILE *fp = fopen(DIAL_ID_PATH, "wb"); + if (!fp) { + WEARABLE_LOGE("File I/O", "Failed to open file for writing: %s", + DIAL_ID_PATH); + return false; + } for (const auto &entry : registeredDials) { char buf[DIAL_ID_LENGTH + DIAL_NAME_LENGTH] = {0}; - memcpy_s(buf, DIAL_ID_LENGTH + DIAL_NAME_LENGTH, &entry.first, 2); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uid %d", entry.first); - memcpy_s(buf + DIAL_ID_LENGTH, DIAL_ID_LENGTH + DIAL_NAME_LENGTH, - entry.second.c_str(), entry.second.length()); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uid %s", entry.second.c_str()); - int size = fwrite(buf, 1, DIAL_ID_LENGTH + DIAL_NAME_LENGTH, fp); - if (DIAL_ID_LENGTH + DIAL_NAME_LENGTH != size) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " fwrite is fail! %d", size); + // 复制UID + memcpy_s(buf, DIAL_ID_LENGTH, &entry.first, DIAL_ID_LENGTH); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RegisterDial %d %s is ok!", ID, + "uid %d", entry.first); + + // 复制表盘名称,确保不越界 + size_t name_length = + entry.second.copy(buf + DIAL_ID_LENGTH, DIAL_NAME_LENGTH - 1); + buf[DIAL_ID_LENGTH + name_length] = '\0'; // 确保字符串以null结尾 + + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RegisterDial %d %s is ok!", ID, + "uid %s", entry.second.c_str()); + + // 写入文件 + size_t size = + fwrite(buf, sizeof(char), DIAL_ID_LENGTH + DIAL_NAME_LENGTH, fp); + if (size != (DIAL_ID_LENGTH + DIAL_NAME_LENGTH)) { + WEARABLE_LOGE("File I/O", "fwrite failed! Written bytes: %zu", size); fclose(fp); return false; } else { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " fwrite is succ %d !", - entry.first); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RegisterDial %d %s is ok!", ID, + "fwrite succeeded for uid %d!", entry.first); } } fclose(fp); return true; } +// 从文件读取表盘 bool SetDialModel::readDialfile() { - FILE *fp = fopen(DIAL_ID_PATH, "rb+"); - if (fp == NULL) + FILE *fp = fopen(DIAL_ID_PATH, "rb"); + if (!fp) { + WEARABLE_LOGE("File I/O", "Failed to open file for reading: %s", + DIAL_ID_PATH); return false; + } + fseek(fp, 0, SEEK_END); - int size = ftell(fp); + long file_size = ftell(fp); fseek(fp, 0, SEEK_SET); - // char * buf=(char *)malloc(size); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " readDialfile size is %d !", size); + WEARABLE_LOGD("Log", "readDialfile size is %ld!", file_size); - while (size) { + while (file_size > 0) { uint16_t uid = 0; - if (DIAL_ID_LENGTH != fread((void *)&uid, 1, DIAL_ID_LENGTH, fp)) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " fread is fail!"); + if (fread(&uid, 1, DIAL_ID_LENGTH, fp) != DIAL_ID_LENGTH) { + WEARABLE_LOGE("File I/O", "fread failed for uid!"); + break; // 读取出错,停止循环 } - - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uid %d", uid); - + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "uid %d", uid); char name_buf[DIAL_NAME_LENGTH] = {0}; - if (DIAL_NAME_LENGTH != fread((void *)name_buf, 1, DIAL_NAME_LENGTH, fp)) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " fread is fail!"); + if (fread(name_buf, 1, DIAL_NAME_LENGTH, fp) != DIAL_NAME_LENGTH) { + WEARABLE_LOGE("File I/O", "fread failed for name_buf!"); + break; // 读取出错,停止循环 } - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "name_buf %s", name_buf); - - if (uid != 0) { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "name_buf %s", name_buf); + // 检查UID和名称的有效性 + if (uid != 0 && name_buf[0] != '\0') { if (!IsDialInstalled(uid)) { RegisterDial(uid, name_buf); } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"Invalid uid or empty name. uid: %d, name: %s", uid, name_buf); } - size -= (DIAL_ID_LENGTH + DIAL_NAME_LENGTH); + + file_size -= (DIAL_ID_LENGTH + DIAL_NAME_LENGTH); } fclose(fp); @@ -414,17 +437,22 @@ void SetDialModel::UpdateDialSetting(uint16_t ID) { /*判断是否为当前表盘 如果是自定义表盘 需要更新*/ if (currentDialID == ID && ID >= 5) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "UpdateDialSetting: Current dial %d cannot be updated %d",currentDialID,ID); - reply_app_send_switchDialCmd(false); + "UpdateDialSetting: Current dial %d cannot be updated %d", + currentDialID, ID); + reply_app_send_switchDialCmd(false); return; } - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateDialSetting: ID %d",ID); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateDialSetting: ID %d", ID); if (registeredDials.find(ID) != registeredDials.end()) { std::string dialname = registeredDials[ID]; // 根据ID找到对应的表盘名称 - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateDialSetting: Dial %s is already registered", dialname.c_str()); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "UpdateDialSetting: Dial %s is already registered", + dialname.c_str()); // 在dialInfoList中查找同名表盘 for (size_t i = 0; i < dialInfoList.size(); i++) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "dialInfoList name %s is already registered", (dialInfoList[i].dialName).c_str()); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "dialInfoList name %s is already registered", + (dialInfoList[i].dialName).c_str()); if (dialInfoList[i].dialName == dialname) { DialSetting diagSetting; SetDialInfo &info = dialInfoList[i]; @@ -436,17 +464,20 @@ void SetDialModel::UpdateDialSetting(uint16_t ID) { MainModel::GetInstance().AddDialSetting(diagSetting, currentDialID); // 更新表盘设置 NativeAbility::GetInstance().ChangeSlice(VIEW_MAIN); - svr_ble_read_data_value_updata_noyify(); //app切换表盘增加id更新 - reply_app_send_switchDialCmd(true); + svr_ble_read_data_value_updata_noyify(); // app切换表盘增加id更新 + reply_app_send_switchDialCmd(true); return; // 更新成功后直接返回 } } //没有在列表找到名称 reply_app_send_switchDialCmd(false); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "dialInfoList name %s is already registered", dialname.c_str()); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "dialInfoList name %s is already registered", + dialname.c_str()); } else { - reply_app_send_switchDialCmd(false); - WEARABLE_LOGE( WEARABLE_LOG_MODULE_APP,"SetDialModel UpdateDialSetting: Cannot find dial %d", ID); + reply_app_send_switchDialCmd(false); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "SetDialModel UpdateDialSetting: Cannot find dial %d", ID); } } @@ -530,7 +561,8 @@ void SetDialModel::LoadAllDialFromDefult(void) { SetDialInfo dialInfo{0}; int ret = ParseDialHeader(DEFULTDIAL, fullDialName, dialInfo); if (ret == 0) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Adding dial %s", fullDialName.c_str()); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Adding dial %s", + fullDialName.c_str()); dialInfoList.push_back(dialInfo); } else { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,