diff --git a/bundle.json b/bundle.json index fa3dc112239a677ea6e51d963f59b54934ce2a90..d3d351d4953507e31d789794a1f97286aedd1f04 100644 --- a/bundle.json +++ b/bundle.json @@ -82,7 +82,6 @@ "input", "ipc", "image_framework", - "jsoncpp", "libxml2", "napi", "os_account", diff --git a/services/BUILD.gn b/services/BUILD.gn index c1495f03bdc426aee8679f3a5327bc4840913886..22e404ad59263f0da6b01dabdc922acfe68adcbc 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -107,6 +107,7 @@ ohos_shared_library("powermgrservice") { "ability_base:want", "ability_runtime:ability_connect_callback_stub", "bundle_framework:appexecfwk_core", + "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_innerkits", "config_policy:configpolicy_util", @@ -123,7 +124,6 @@ ohos_shared_library("powermgrservice") { "image_framework:image_native", "init:libbegetutil", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/services/native/src/power_mode_policy.cpp b/services/native/src/power_mode_policy.cpp index 469871e99fee7fa5edff3d5b6e04b7cea9ea9ac7..0b519011d5db524df41fb32451c09ee603a67a09 100644 --- a/services/native/src/power_mode_policy.cpp +++ b/services/native/src/power_mode_policy.cpp @@ -13,17 +13,19 @@ * limitations under the License. */ +#include #include "power_mode_policy.h" #include "power_mgr_service.h" #include "power_log.h" #include "power_save_mode.h" #include "singleton.h" #include "setting_helper.h" -#include "json/json.h" using namespace std; namespace OHOS { namespace PowerMgr { +constexpr int32_t KEY_BASE = 10; + int32_t PowerModePolicy::GetPowerModeValuePolicy(uint32_t type) { int32_t ret = INIT_VALUE_FALSE; @@ -68,20 +70,33 @@ void PowerModePolicy::ComparePowerModePolicy() bool PowerModePolicy::InitRecoverMap() { std::string jsonStr = SettingHelper::ReadPowerModeRecoverMap(); - Json::Value recoverJson; - Json::Reader reader; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), recoverJson)) { + + cJSON* recoverJson = cJSON_Parse(jsonStr.c_str()); + if (!recoverJson) { POWER_HILOGW(FEATURE_POWER_MODE, "parse recover json str error"); return false; } - for (const auto &member : recoverJson.getMemberNames()) { - int32_t key = std::stoi(member); - if (!recoverJson[member].isInt()) { + if (!cJSON_IsObject(recoverJson)) { + POWER_HILOGW(FEATURE_POWER_MODE, "recover json root is not an object"); + cJSON_Delete(recoverJson); + return false; + } + cJSON* item = nullptr; + cJSON_ArrayForEach(item, recoverJson) { + const char* keyStr = item->string; + if (!keyStr || !cJSON_IsNumber(item)) { continue; } - int32_t value = recoverJson[member].asInt(); + errno = 0; + int32_t key = static_cast(strtol(keyStr, nullptr, KEY_BASE)); + if (errno == ERANGE && (key == INT_MAX || key == INT_MIN)) { + continue; + } + int32_t value = static_cast(item->valueint); recoverMap_[key] = value; } + + cJSON_Delete(recoverJson); POWER_HILOGI(FEATURE_POWER_MODE, "init recover map succeed"); return true; } @@ -193,11 +208,27 @@ void PowerModePolicy::RemoveBackupMapSettingSwitch(uint32_t switchId) void PowerModePolicy::SavePowerModeRecoverMap() { - Json::Value recoverJson; + cJSON* recoverJson = cJSON_CreateObject(); + if (!recoverJson) { + POWER_HILOGE(FEATURE_POWER_MODE, "Failed to create cJSON object"); + return; + } + for (const auto& pair : recoverMap_) { - recoverJson[to_string(pair.first)] = pair.second; + std::string keyStr = std::to_string(pair.first); + cJSON_AddNumberToObject(recoverJson, keyStr.c_str(), pair.second); + } + + char* jsonStr = cJSON_Print(recoverJson); + if (!jsonStr) { + POWER_HILOGE(FEATURE_POWER_MODE, "Failed to print cJSON to string"); + cJSON_Delete(recoverJson); + return; } - SettingHelper::SavePowerModeRecoverMap(recoverJson.toStyledString()); + std::string jsonConfig = std::string(jsonStr); + SettingHelper::SavePowerModeRecoverMap(jsonConfig); + cJSON_free(jsonStr); + cJSON_Delete(recoverJson); } } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/shutdown/shutdown_dialog.cpp b/services/native/src/shutdown/shutdown_dialog.cpp index 536f8820a5a9222d231c9842c8e6955727278f5f..ab3d15c10e594b317c38d4b0681296555cbf210b 100644 --- a/services/native/src/shutdown/shutdown_dialog.cpp +++ b/services/native/src/shutdown/shutdown_dialog.cpp @@ -27,11 +27,10 @@ #include #include #endif +#include #include #include "config_policy_utils.h" -#include "json/reader.h" -#include "json/value.h" #include "power_log.h" #include "power_mgr_service.h" #include "power_vibrator.h" @@ -187,30 +186,41 @@ void ShutdownDialog::LoadDialogConfig() std::ifstream inputStream(configPath, std::ios::in | std::ios::binary); std::string contentStr(std::istreambuf_iterator {inputStream}, std::istreambuf_iterator {}); - Json::Reader reader; - Json::Value root; - if (!reader.parse(contentStr.data(), contentStr.data() + contentStr.size(), root)) { - POWER_HILOGE(COMP_UTILS, "json parse error"); + if (contentStr.empty()) { + POWER_HILOGE(COMP_UTILS, "json file is empty"); return; } - if (root.isNull() || !root.isObject()) { - POWER_HILOGE(COMP_UTILS, "json root invalid[%{public}s]", contentStr.c_str()); + cJSON* root = cJSON_Parse(contentStr.c_str()); + if (!root) { + POWER_HILOGE(COMP_UTILS, "json parse error[%{public}s]", contentStr.c_str()); return; } - - if (!root["bundleName"].isString() || - !root["abilityName"].isString() || !root["uiExtensionType"].isString()) { - POWER_HILOGE(COMP_UTILS, "json varibale not support"); + + if (!cJSON_IsObject(root)) { + POWER_HILOGE(COMP_UTILS, "json root invalid"); + cJSON_Delete(root); + return; + } + + cJSON* bundleNameItem = cJSON_GetObjectItemCaseSensitive(root, "bundleName"); + cJSON* abilityNameItem = cJSON_GetObjectItemCaseSensitive(root, "abilityName"); + cJSON* uiExtensionTypeItem = cJSON_GetObjectItemCaseSensitive(root, "uiExtensionType"); + if (!bundleNameItem || !cJSON_IsString(bundleNameItem) || + !abilityNameItem || !cJSON_IsString(abilityNameItem) || + !uiExtensionTypeItem || !cJSON_IsString(uiExtensionTypeItem)) { + POWER_HILOGE(COMP_UTILS, "json variable not supported"); + cJSON_Delete(root); return; } - bundleName_ = root["bundleName"].asString(); - abilityName_ = root["abilityName"].asString(); - uiExtensionType_ = root["uiExtensionType"].asString(); - dialogBundleName_ = root["bundleName"].asString(); + bundleName_ = bundleNameItem->valuestring; + abilityName_ = abilityNameItem->valuestring; + uiExtensionType_ = uiExtensionTypeItem->valuestring; + dialogBundleName_ = bundleNameItem->valuestring; dialogAbilityName_ = "com.ohos.sceneboard.systemdialog"; POWER_HILOGI(COMP_UTILS, "PowerOff variables have changed"); + cJSON_Delete(root); } void ShutdownDialog::DialogAbilityConnection::OnAbilityConnectDone( diff --git a/services/native/src/suspend/suspend_source_parser.cpp b/services/native/src/suspend/suspend_source_parser.cpp index 6d835c74594829e68f6a977678727ae7bcc521e7..66f9f7491cc22a80b2502a3daa358dad5392667d 100644 --- a/services/native/src/suspend/suspend_source_parser.cpp +++ b/services/native/src/suspend/suspend_source_parser.cpp @@ -18,11 +18,11 @@ #include #include +#include + #include "config_policy_utils.h" #include "power_log.h" #include "setting_helper.h" -#include "json/reader.h" -#include "json/value.h" #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING #include "power_mgr_service.h" #endif @@ -152,36 +152,40 @@ bool SuspendSourceParser::GetTargetPath(std::string& targetPath) std::shared_ptr SuspendSourceParser::ParseSources(const std::string& jsonStr) { std::shared_ptr parseSources = std::make_shared(); - Json::Reader reader; - Json::Value root; - std::string errors; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) { + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { POWER_HILOGE(FEATURE_SUSPEND, "json parse error"); parseSources->SetParseErrorFlag(true); return parseSources; } - - if (root.isNull() || !root.isObject()) { + if (!cJSON_IsObject(root)) { POWER_HILOGE(FEATURE_SUSPEND, "json root invalid[%{public}s]", jsonStr.c_str()); parseSources->SetParseErrorFlag(true); + cJSON_Delete(root); return parseSources; } - Json::Value::Members members = root.getMemberNames(); - for (auto iter = members.begin(); iter != members.end(); iter++) { - std::string key = *iter; - Json::Value valueObj = root[key]; - bool ret = ParseSourcesProc(parseSources, valueObj, key); + cJSON* item = nullptr; + cJSON_ArrayForEach(item, root) { + const char* key = item->string; + if (!key) { + POWER_HILOGI(FEATURE_SUSPEND, "invalid key in json object"); + continue; + } + std::string keyStr = std::string(key); + bool ret = ParseSourcesProc(parseSources, item, keyStr); if (ret == false) { POWER_HILOGI(FEATURE_SUSPEND, "lost map config key"); continue; } } + + cJSON_Delete(root); return parseSources; } bool SuspendSourceParser::ParseSourcesProc( - std::shared_ptr& parseSources, Json::Value& valueObj, std::string& key) + std::shared_ptr& parseSources, cJSON* valueObj, std::string& key) { if (parseSources == nullptr) { POWER_HILOGE(FEATURE_SUSPEND, "parseSources is nullptr"); @@ -195,12 +199,12 @@ bool SuspendSourceParser::ParseSourcesProc( uint32_t action = 0; uint32_t delayMs = 0; - if (!valueObj.isNull() && valueObj.isObject()) { - Json::Value actionValue = valueObj[SuspendSource::ACTION_KEY]; - Json::Value delayValue = valueObj[SuspendSource::DELAY_KEY]; - if (actionValue.isUInt() && delayValue.isUInt()) { - action = actionValue.asUInt(); - delayMs = delayValue.asUInt(); + if (valueObj && cJSON_IsObject(valueObj)) { + cJSON* actionValue = cJSON_GetObjectItemCaseSensitive(valueObj, SuspendSource::ACTION_KEY); + cJSON* delayValue = cJSON_GetObjectItemCaseSensitive(valueObj, SuspendSource::DELAY_KEY); + if (actionValue && cJSON_IsNumber(actionValue) && delayValue && cJSON_IsNumber(delayValue)) { + action = static_cast(actionValue->valueint); + delayMs = static_cast(delayValue->valueint); if (action >= ILLEGAL_ACTION) { action = 0; } diff --git a/services/native/src/suspend/suspend_source_parser.h b/services/native/src/suspend/suspend_source_parser.h index f209d4d6a894b9882b4f0d532027374885a77ac3..6b2e5b6591bab0aca77eaa0969ff8a2b5eb25245 100644 --- a/services/native/src/suspend/suspend_source_parser.h +++ b/services/native/src/suspend/suspend_source_parser.h @@ -19,8 +19,8 @@ #include #include +#include #include "suspend_sources.h" -#include "json/value.h" namespace OHOS { namespace PowerMgr { @@ -30,7 +30,7 @@ public: static std::shared_ptr ParseSources(const std::string& config); static bool GetTargetPath(std::string& targetPath); static bool ParseSourcesProc( - std::shared_ptr &parseSources, Json::Value& valueObj, std::string& key); + std::shared_ptr &parseSources, cJSON* valueObj, std::string& key); static const std::string GetSuspendSourcesByConfig(); }; } // namespace PowerMgr diff --git a/services/native/src/wakeup/wakeup_controller.cpp b/services/native/src/wakeup/wakeup_controller.cpp index 973b8244b4fd08943f4cc6898af048994bb090d8..d4e7d527485a14fddc3aa75c9506182eac555bae 100644 --- a/services/native/src/wakeup/wakeup_controller.cpp +++ b/services/native/src/wakeup/wakeup_controller.cpp @@ -23,9 +23,9 @@ #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART #include #endif +#include #include #include -#include #include #include "permission.h" #include "power_errors.h" @@ -198,33 +198,46 @@ void WakeupController::ChangeWakeupSourceConfig(bool updateEnable) return; } POWER_HILOGI(COMP_SVC, "the origin ccmJson is: %{public}s", jsonStr.c_str()); - Json::Value root; - Json::Reader reader; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) { + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { POWER_HILOGE(COMP_SVC, "json parse error"); return; } - if (root["touchscreen"].isNull()) { - POWER_HILOGE(COMP_SVC, "this touchscreenNode is empty"); + if (!cJSON_IsObject(root)) { + POWER_HILOGW(COMP_SVC, "json root is not an object"); + cJSON_Delete(root); return; } - if (root["touchscreen"]["enable"].isNull()) { - POWER_HILOGE(COMP_SVC, "the touchscreenNode is empty"); + cJSON* touchscreenNode = cJSON_GetObjectItemCaseSensitive(root, "touchscreen"); + if (!touchscreenNode || !cJSON_IsObject(touchscreenNode)) { + POWER_HILOGE(COMP_SVC, "this touchscreenNode is empty"); + cJSON_Delete(root); return; } - if (!root["touchscreen"]["enable"].isBool()) { - POWER_HILOGE(COMP_SVC, "the origin touchscreenEnable value is invalid"); + cJSON* enableNode = cJSON_GetObjectItemCaseSensitive(touchscreenNode, "enable"); + if (!enableNode || !cJSON_IsBool(enableNode)) { + POWER_HILOGE(COMP_SVC, "the touchscreenNode enable value is invalid"); + cJSON_Delete(root); return; } - bool originEnable = root["touchscreen"]["enable"].asBool(); + bool originEnable = cJSON_IsTrue(enableNode); if (originEnable == updateEnable) { POWER_HILOGI(COMP_SVC, "no need change jsonConfig value"); + cJSON_Delete(root); return; } - - root["touchscreen"]["enable"] = updateEnable; - POWER_HILOGI(COMP_SVC, "the new doubleJsonConfig is: %{public}s", root.toStyledString().c_str()); - SettingHelper::SetSettingWakeupSources(root.toStyledString()); + enableNode->valueint = updateEnable ? 1 : 0; + char* jsonUpdatedStr = cJSON_Print(root); + if (!jsonUpdatedStr) { + POWER_HILOGI(COMP_SVC, "Failed to print cJSON to string"); + cJSON_Delete(root); + return; + } + POWER_HILOGI(COMP_SVC, "the new doubleJsonConfig is: %{public}s", jsonUpdatedStr); + std::string jsonConfig = std::string(jsonUpdatedStr); + SettingHelper::SetSettingWakeupSources(jsonConfig); + cJSON_free(jsonUpdatedStr); + cJSON_Delete(root); } static const char* POWER_MANAGER_EXT_PATH = "libpower_manager_ext.z.so"; @@ -307,33 +320,46 @@ void WakeupController::ChangePickupWakeupSourceConfig(bool updataEnable) return; } POWER_HILOGI(COMP_SVC, "%{public}s(%{public}d)", __func__, updataEnable); - Json::Value root; - Json::Reader reader; - reader.parse(jsonStr, root); - if (!reader.parse(jsonStr, root)) { + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { POWER_HILOGE(COMP_SVC, "Failed to parse json string"); return; } - if (root["pickup"].isNull()) { - POWER_HILOGE(COMP_SVC, "this pickNode is empty"); + if (!cJSON_IsObject(root)) { + POWER_HILOGW(COMP_SVC, "json root is not an object"); + cJSON_Delete(root); return; } - if (root["pickup"]["enable"].isNull()) { - POWER_HILOGE(COMP_SVC, "the pickupNode is empty"); + cJSON* pickupNode = cJSON_GetObjectItemCaseSensitive(root, "pickup"); + if (!pickupNode || !cJSON_IsObject(pickupNode)) { + POWER_HILOGE(COMP_SVC, "this pickNode is empty"); + cJSON_Delete(root); return; } - if (!root["pickup"]["enable"].isBool()) { - POWER_HILOGE(COMP_SVC, "the origin pickupEnable value is invalid"); + cJSON* enableNode = cJSON_GetObjectItemCaseSensitive(pickupNode, "enable"); + if (!enableNode || !cJSON_IsBool(enableNode)) { + POWER_HILOGE(COMP_SVC, "the pickupNode enable value is invalid"); + cJSON_Delete(root); return; } - bool originEnable = root["pickup"]["enable"].asBool(); + bool originEnable = cJSON_IsTrue(enableNode); if (originEnable == updataEnable) { POWER_HILOGI(COMP_SVC, "no need change jsonconfig_value"); + cJSON_Delete(root); return; } - root["pickup"]["enable"] = updataEnable; - POWER_HILOGI(COMP_SVC, "the new pickupJsonConfig is: %{public}s", root.toStyledString().c_str()); - SettingHelper::SetSettingWakeupSources(root.toStyledString()); + enableNode->valueint = updataEnable ? 1 : 0; + char* jsonUpdatedStr = cJSON_Print(root); + if (!jsonUpdatedStr) { + POWER_HILOGI(COMP_SVC, "Failed to print cJSON to string"); + cJSON_Delete(root); + return; + } + POWER_HILOGI(COMP_SVC, "the new pickupJsonConfig is: %{public}s", jsonUpdatedStr); + std::string jsonConfig = std::string(jsonUpdatedStr); + SettingHelper::SetSettingWakeupSources(jsonConfig); + cJSON_free(jsonUpdatedStr); + cJSON_Delete(root); } #endif @@ -342,29 +368,49 @@ void WakeupController::ChangeLidWakeupSourceConfig(bool updataEnable) std::lock_guard lock(sourceUpdateMutex_); std::string jsonStr = SettingHelper::GetSettingWakeupSources(); POWER_HILOGI(FEATURE_POWER_STATE, "%{public}s", jsonStr.c_str()); - Json::Value root; - Json::Reader reader; - reader.parse(jsonStr, root); - if (!reader.parse(jsonStr, root)) { + + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { POWER_HILOGE(FEATURE_POWER_STATE, "Failed to parse json string"); return; } + if (!cJSON_IsObject(root)) { + POWER_HILOGW(FEATURE_POWER_STATE, "json root is not an object"); + cJSON_Delete(root); + return; + } + cJSON* lidNode = cJSON_GetObjectItemCaseSensitive(root, "lid"); + if (!lidNode || !cJSON_IsObject(lidNode)) { + POWER_HILOGE(FEATURE_POWER_STATE, "this lidNode is empty or not an object"); + cJSON_Delete(root); + return; + } bool originEnable = true; - if (root["lid"]["enable"].isBool()) { - originEnable = root["lid"]["enable"].asBool(); + cJSON* enableNode = cJSON_GetObjectItemCaseSensitive(lidNode, "enable"); + if (enableNode && cJSON_IsBool(enableNode)) { + originEnable = cJSON_IsTrue(enableNode); } - if (originEnable == updataEnable) { POWER_HILOGI(FEATURE_POWER_STATE, "no need change jsonConfig value"); + cJSON_Delete(root); return; } - if (root["lid"]["enable"].isBool()) { - root["lid"]["enable"] = updataEnable; + + if (enableNode && cJSON_IsBool(enableNode)) { + enableNode->valueint = updataEnable ? 1 : 0; + } + char* jsonUpdatedStr = cJSON_Print(root); + if (!jsonUpdatedStr) { + POWER_HILOGI(COMP_SVC, "Failed to print cJSON to string"); + cJSON_Delete(root); + return; } - SettingHelper::SetSettingWakeupSources(root.toStyledString()); + std::string jsonConfig = std::string(jsonUpdatedStr); + SettingHelper::SetSettingWakeupSources(jsonConfig); + cJSON_free(jsonUpdatedStr); + cJSON_Delete(root); } - void WakeupController::ExecWakeupMonitorByReason(WakeupDeviceType reason) { FFRTUtils::SubmitTask([this, reason] { diff --git a/services/native/src/wakeup/wakeup_source_parser.cpp b/services/native/src/wakeup/wakeup_source_parser.cpp index 9df7dad1633122af173087b84ebab6d2d6eea8a3..337a787f06d7be391db24345caa25b1daf2c5d76 100644 --- a/services/native/src/wakeup/wakeup_source_parser.cpp +++ b/services/native/src/wakeup/wakeup_source_parser.cpp @@ -16,12 +16,11 @@ #include #include +#include #include "config_policy_utils.h" #include "power_log.h" #include "setting_helper.h" #include "wakeup_source_parser.h" -#include "json/reader.h" -#include "json/value.h" namespace OHOS { namespace PowerMgr { @@ -109,51 +108,54 @@ bool WakeupSourceParser::GetTargetPath(std::string& targetPath) std::shared_ptr WakeupSourceParser::ParseSources(const std::string& jsonStr) { std::shared_ptr parseSources = std::make_shared(); - Json::Reader reader; - Json::Value root; - std::string errors; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) { + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { POWER_HILOGE(FEATURE_WAKEUP, "json parse error"); parseSources->SetParseErrorFlag(true); return parseSources; } - if (root.isNull() || !root.isObject()) { + if (!cJSON_IsObject(root)) { POWER_HILOGE(FEATURE_WAKEUP, "json root invalid[%{public}s]", jsonStr.c_str()); parseSources->SetParseErrorFlag(true); + cJSON_Delete(root); return parseSources; } - Json::Value::Members members = root.getMemberNames(); - for (auto iter = members.begin(); iter != members.end(); iter++) { - std::string key = *iter; - Json::Value valueObj = root[key]; - - bool ret = ParseSourcesProc(parseSources, valueObj, key); + cJSON* item = nullptr; + cJSON_ArrayForEach(item, root) { + const char* key = item->string; + if (!key) { + POWER_HILOGI(FEATURE_WAKEUP, "invalid key in json object"); + continue; + } + std::string keyStr = std::string(key); + bool ret = ParseSourcesProc(parseSources, item, keyStr); if (ret == false) { POWER_HILOGI(FEATURE_WAKEUP, "lost map config key"); continue; } } + cJSON_Delete(root); return parseSources; } bool WakeupSourceParser::ParseSourcesProc( - std::shared_ptr& parseSources, Json::Value& valueObj, std::string& key) + std::shared_ptr& parseSources, cJSON* valueObj, std::string& key) { bool enable = true; uint32_t click = DOUBLE_CLICK; WakeupDeviceType wakeupDeviceType = WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN; - if (!valueObj.isNull() && valueObj.isObject()) { - Json::Value enableValue = valueObj[WakeupSource::ENABLE_KEY]; - Json::Value clickValue = valueObj[WakeupSource::KEYS_KEY]; - if (!clickValue.isNull() && clickValue.isUInt()) { - click = (clickValue.asUInt() == SINGLE_CLICK || clickValue.asUInt() == DOUBLE_CLICK) ? clickValue.asUInt() : - DOUBLE_CLICK; + if (valueObj && cJSON_IsObject(valueObj)) { + cJSON* enableValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupSource::ENABLE_KEY); + if (enableValue && cJSON_IsBool(enableValue)) { + enable = cJSON_IsTrue(enableValue); } - if (enableValue.isBool()) { - enable = enableValue.asBool(); + cJSON* clickValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupSource::KEYS_KEY); + if (clickValue && cJSON_IsNumber(clickValue)) { + uint32_t clickInt = static_cast(clickValue->valueint); + click = (clickInt == SINGLE_CLICK || clickInt == DOUBLE_CLICK) ? clickInt : DOUBLE_CLICK; } } diff --git a/services/native/src/wakeup/wakeup_source_parser.h b/services/native/src/wakeup/wakeup_source_parser.h index d5ff08ec9aa637baa999d9c76fb3ae80a1acead1..8e1953296ef8f9ff90ebd7af5be812297cf3ab3e 100644 --- a/services/native/src/wakeup/wakeup_source_parser.h +++ b/services/native/src/wakeup/wakeup_source_parser.h @@ -21,7 +21,7 @@ #include #include -#include "json/value.h" +#include namespace OHOS { namespace PowerMgr { @@ -30,7 +30,7 @@ public: static std::shared_ptr ParseSources(); static std::shared_ptr ParseSources(const std::string& config); static bool ParseSourcesProc( - std::shared_ptr& parseSources, Json::Value& valueObj, std::string& key); + std::shared_ptr& parseSources, cJSON* valueObj, std::string& key); static bool GetTargetPath(std::string& targetPath); static void SetSettingsToDatabase(WakeupDeviceType type, bool enable); static const std::string GetWakeupSourcesByConfig(); diff --git a/services/native/src/wakeup_action/wakeup_action_source_parser.cpp b/services/native/src/wakeup_action/wakeup_action_source_parser.cpp index 9ea264050c9ac487b4908a579fb281395cc354b1..5cf07df32f0b7ba3ce13578b2096ada72d0a283d 100644 --- a/services/native/src/wakeup_action/wakeup_action_source_parser.cpp +++ b/services/native/src/wakeup_action/wakeup_action_source_parser.cpp @@ -18,10 +18,9 @@ #include #include +#include #include "config_policy_utils.h" #include "power_log.h" -#include "json/reader.h" -#include "json/value.h" namespace OHOS { namespace PowerMgr { @@ -79,46 +78,52 @@ bool WakeupActionSourceParser::GetTargetPath(std::string& targetPath) std::shared_ptr WakeupActionSourceParser::ParseSources(const std::string& jsonStr) { std::shared_ptr parseSources = std::make_shared(); - Json::Reader reader; - Json::Value root; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) { - POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json parse error"); + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { + POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json parse error[%{public}s]", jsonStr.c_str()); return parseSources; } - if (root.isNull() || !root.isObject()) { - POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json root invalid[%{public}s]", jsonStr.c_str()); + if (!cJSON_IsObject(root)) { + POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json root invalid"); + cJSON_Delete(root); return parseSources; } - - Json::Value::Members members = root.getMemberNames(); - for (auto iter = members.begin(); iter != members.end(); iter++) { - std::string key = *iter; - Json::Value valueObj = root[key]; - POWER_HILOGI(FEATURE_WAKEUP_ACTION, "key=%{public}s", key.c_str()); - bool ret = ParseSourcesProc(parseSources, valueObj, key); + + cJSON* item = nullptr; + cJSON_ArrayForEach(item, root) { + const char* key = item->string; + if (!key) { + POWER_HILOGI(FEATURE_WAKEUP_ACTION, "invalid key in json object"); + continue; + } + POWER_HILOGI(FEATURE_WAKEUP_ACTION, "key=%{public}s", key); + std::string keyStr = std::string(key); + bool ret = ParseSourcesProc(parseSources, item, keyStr); if (ret == false) { POWER_HILOGI(FEATURE_WAKEUP_ACTION, "lost map config key"); continue; } } + + cJSON_Delete(root); return parseSources; } bool WakeupActionSourceParser::ParseSourcesProc( - std::shared_ptr& parseSources, Json::Value& valueObj, std::string& key) + std::shared_ptr& parseSources, cJSON* valueObj, std::string& key) { std::string scene{""}; uint32_t action = 0; - if (!valueObj.isNull() && valueObj.isObject()) { - Json::Value sceneValue = valueObj[WakeupActionSource::SCENE_KEY]; - Json::Value actionValue = valueObj[WakeupActionSource::ACTION_KEY]; - if (sceneValue.isString()) { - scene = sceneValue.asString(); + if (valueObj && cJSON_IsObject(valueObj)) { + cJSON* sceneValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupActionSource::SCENE_KEY); + if (sceneValue && cJSON_IsString(sceneValue)) { + scene = sceneValue->valuestring; POWER_HILOGI(FEATURE_WAKEUP_ACTION, "scene=%{public}s", scene.c_str()); } - if (actionValue.isUInt()) { - action = actionValue.asUInt(); + cJSON* actionValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupActionSource::ACTION_KEY); + if (actionValue && cJSON_IsNumber(actionValue)) { + action = static_cast(actionValue->valueint); POWER_HILOGI(FEATURE_WAKEUP_ACTION, "action=%{public}u", action); if (action >= ILLEGAL_ACTION) { action = 0; diff --git a/services/native/src/wakeup_action/wakeup_action_source_parser.h b/services/native/src/wakeup_action/wakeup_action_source_parser.h index b7f13063b8a252f1cd9b69c92f79c55db0ab0a87..2203ecbf422b299d63b58a81fdc93ffbb05fe76c 100644 --- a/services/native/src/wakeup_action/wakeup_action_source_parser.h +++ b/services/native/src/wakeup_action/wakeup_action_source_parser.h @@ -19,8 +19,8 @@ #include #include +#include #include "wakeup_action_sources.h" -#include "json/value.h" namespace OHOS { namespace PowerMgr { @@ -30,7 +30,7 @@ public: static std::shared_ptr ParseSources(const std::string& jsonStr); static bool GetTargetPath(std::string& targetPath); static bool ParseSourcesProc( - std::shared_ptr &parseSources, Json::Value& valueObj, std::string& key); + std::shared_ptr &parseSources, cJSON* valueObj, std::string& key); }; } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/watch_customized_screen_common_event/customized_screen_event_rules.cpp b/services/native/src/watch_customized_screen_common_event/customized_screen_event_rules.cpp index 15b28b767cee35f86f2e831154c520bc3e8827da..14558709bab6bfcf54a88263e373d963ee1b53fc 100644 --- a/services/native/src/watch_customized_screen_common_event/customized_screen_event_rules.cpp +++ b/services/native/src/watch_customized_screen_common_event/customized_screen_event_rules.cpp @@ -15,7 +15,7 @@ #include "customized_screen_event_rules.h" #ifdef POWER_MANAGER_ENABLE_WATCH_CUSTOMIZED_SCREEN_COMMON_EVENT_RULES -#include "json/json.h" +#include #include "power_ext_intf_wrapper.h" #endif diff --git a/test/apitest/inner_api/shutdown/BUILD.gn b/test/apitest/inner_api/shutdown/BUILD.gn index 249a9f8f2c50b1fa8705e7534a672aeb37f35e09..80cde21cb12d291d2426868e95b689fa1abf11cc 100644 --- a/test/apitest/inner_api/shutdown/BUILD.gn +++ b/test/apitest/inner_api/shutdown/BUILD.gn @@ -45,6 +45,7 @@ deps_ex = [ "ability_base:base", "ability_base:want", "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_innerkits", "ffrt:libffrt", diff --git a/test/fuzztest/asyncshutdowncallback_fuzzer/BUILD.gn b/test/fuzztest/asyncshutdowncallback_fuzzer/BUILD.gn index 8b56d233cbd2eba4d83c5cf44b5b473310a2d56f..e1b464e5e088e33456b43c5fbb5c7e8ce19dbcd5 100644 --- a/test/fuzztest/asyncshutdowncallback_fuzzer/BUILD.gn +++ b/test/fuzztest/asyncshutdowncallback_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("AsyncShutdownCallbackFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/createrunninglock_fuzzer/BUILD.gn b/test/fuzztest/createrunninglock_fuzzer/BUILD.gn index b372c3b9e5cbb0cc5970f906d141e573c03739fb..88bf822b3f471254b8453a5f7336d56aaeacdf6f 100644 --- a/test/fuzztest/createrunninglock_fuzzer/BUILD.gn +++ b/test/fuzztest/createrunninglock_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("CreateRunningLockFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/forcesuspenddevice_fuzzer/BUILD.gn b/test/fuzztest/forcesuspenddevice_fuzzer/BUILD.gn index 865f0b3c08c23ec507d8cab9d2b422abaef9a037..2c8d1a3a7842ea83efe8414be0cffc18dc4231df 100644 --- a/test/fuzztest/forcesuspenddevice_fuzzer/BUILD.gn +++ b/test/fuzztest/forcesuspenddevice_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("ForceSuspendDeviceFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/getsetdevicemode_fuzzer/BUILD.gn b/test/fuzztest/getsetdevicemode_fuzzer/BUILD.gn index a0adea9e032749740195ee324cc4bc01a982e40d..70eda90dcd7d4637af6b8c2f9ddc524b5d6ac243 100644 --- a/test/fuzztest/getsetdevicemode_fuzzer/BUILD.gn +++ b/test/fuzztest/getsetdevicemode_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("GetSetDeviceModeFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/getstate_fuzzer/BUILD.gn b/test/fuzztest/getstate_fuzzer/BUILD.gn index 8b03cbd85de67301484ae061f92d4a57c5c56725..14049f5d3768acaa617bf91ae39bf02a1523c807 100644 --- a/test/fuzztest/getstate_fuzzer/BUILD.gn +++ b/test/fuzztest/getstate_fuzzer/BUILD.gn @@ -59,12 +59,12 @@ ohos_fuzztest("GetStateFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/hibernate_fuzzer/BUILD.gn b/test/fuzztest/hibernate_fuzzer/BUILD.gn index c956fb727b45690ff71cbb6388677b1aa4f33a3d..c8822fadc2cd235a0cc91760051f394b8297dc47 100644 --- a/test/fuzztest/hibernate_fuzzer/BUILD.gn +++ b/test/fuzztest/hibernate_fuzzer/BUILD.gn @@ -59,12 +59,12 @@ ohos_fuzztest("HibernateFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/iscollaborationscreenon_fuzzer/BUILD.gn b/test/fuzztest/iscollaborationscreenon_fuzzer/BUILD.gn index 608a734cedb090ba16d74274b0562f66292f12a6..7f46109f303575f5192bbb065ba2ac36d73b2614 100644 --- a/test/fuzztest/iscollaborationscreenon_fuzzer/BUILD.gn +++ b/test/fuzztest/iscollaborationscreenon_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("IsCollaborationScreenOnFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/isfoldscreenon_fuzzer/BUILD.gn b/test/fuzztest/isfoldscreenon_fuzzer/BUILD.gn index dcb5c564bdd8e816d0d417bcd90f431f49f0001a..d5f0a9c764f091bb0a7993f1463d06692cab5c86 100644 --- a/test/fuzztest/isfoldscreenon_fuzzer/BUILD.gn +++ b/test/fuzztest/isfoldscreenon_fuzzer/BUILD.gn @@ -59,12 +59,12 @@ ohos_fuzztest("IsFoldScreenOnFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/isrunninglocktypesupported_fuzzer/BUILD.gn b/test/fuzztest/isrunninglocktypesupported_fuzzer/BUILD.gn index ff485451f44bb0ba0e5e8fc0765c9ba96ef19284..8816956d6bd4ae606ee05582d53ebd0aa62acbd1 100644 --- a/test/fuzztest/isrunninglocktypesupported_fuzzer/BUILD.gn +++ b/test/fuzztest/isrunninglocktypesupported_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("IsRunningLockTypeSupportedFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/isscreenon_fuzzer/BUILD.gn b/test/fuzztest/isscreenon_fuzzer/BUILD.gn index 44a29441075200eee47c11573788ce9da69ce42a..b9e9d0f31e29020ae725810b80d3900e76d606b9 100644 --- a/test/fuzztest/isscreenon_fuzzer/BUILD.gn +++ b/test/fuzztest/isscreenon_fuzzer/BUILD.gn @@ -60,11 +60,11 @@ ohos_fuzztest("IsScreenOnFuzzTest") { external_deps = [ "ability_runtime:ability_manager", "c_utils:utils", + "cJSON:cjson", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/isstandby_fuzzer/BUILD.gn b/test/fuzztest/isstandby_fuzzer/BUILD.gn index 442a8a77d2387023c9b24eef5ce169e0d401bf69..b9ed4b7c0e7f952fe0722716e1b59567b0a1b7dc 100644 --- a/test/fuzztest/isstandby_fuzzer/BUILD.gn +++ b/test/fuzztest/isstandby_fuzzer/BUILD.gn @@ -59,12 +59,12 @@ ohos_fuzztest("IsStandbyFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/lockscreenaftertimingout_fuzzer/BUILD.gn b/test/fuzztest/lockscreenaftertimingout_fuzzer/BUILD.gn index f99f36ff134bb3f3ae3422af65abe2e87c5c9b35..1b2a35f8c2ecc9e4bd707b608ce14aeae04d7451 100644 --- a/test/fuzztest/lockscreenaftertimingout_fuzzer/BUILD.gn +++ b/test/fuzztest/lockscreenaftertimingout_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("LockScreenAfterTimingOutFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/lockunlockisused_fuzzer/BUILD.gn b/test/fuzztest/lockunlockisused_fuzzer/BUILD.gn index 3ffbc8381db6334f706d024de829e3048ea9fff0..0531119237c1b8b53b765401b80a2f1960815303 100644 --- a/test/fuzztest/lockunlockisused_fuzzer/BUILD.gn +++ b/test/fuzztest/lockunlockisused_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("LockUnLockIsUsedFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/overridescreenofftime_fuzzer/BUILD.gn b/test/fuzztest/overridescreenofftime_fuzzer/BUILD.gn index 2a01aba3d4fe8cf3488c4630b56448c3548d6154..aaeb5c7d9465cfb014e637f65b901766103ea1ce 100644 --- a/test/fuzztest/overridescreenofftime_fuzzer/BUILD.gn +++ b/test/fuzztest/overridescreenofftime_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("OverrideScreenOffTimeFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/powermodecallback_fuzzer/BUILD.gn b/test/fuzztest/powermodecallback_fuzzer/BUILD.gn index ed354614b6b917bb459fc9d5b52d1ed4552bacc5..8f8896125ca35c51bde4bd0940944f74a721609c 100644 --- a/test/fuzztest/powermodecallback_fuzzer/BUILD.gn +++ b/test/fuzztest/powermodecallback_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("PowerModeCallbackFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/powerstatecallback_fuzzer/BUILD.gn b/test/fuzztest/powerstatecallback_fuzzer/BUILD.gn index b51b2de6300a77d9ac420a531c1e22800ba90a56..3f267ada9824f0ab06a3f7244b1dfe698aa13fd2 100644 --- a/test/fuzztest/powerstatecallback_fuzzer/BUILD.gn +++ b/test/fuzztest/powerstatecallback_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("PowerStateCallbackFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "json:nlohmann_json_static", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/proxyrunninglock_fuzzer/BUILD.gn b/test/fuzztest/proxyrunninglock_fuzzer/BUILD.gn index da725e68655086f0debb3fb42848be610b58bdd5..f9da0f0b9baa7a5805c5e95ea644538ed6f369d2 100644 --- a/test/fuzztest/proxyrunninglock_fuzzer/BUILD.gn +++ b/test/fuzztest/proxyrunninglock_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("ProxyRunningLockFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/proxyrunninglocks_fuzzer/BUILD.gn b/test/fuzztest/proxyrunninglocks_fuzzer/BUILD.gn index 794b1321c4058cc9433d4f63d3caacba1db23f21..d595909b58e32179e8488f5e0f280d8e96f6cddb 100644 --- a/test/fuzztest/proxyrunninglocks_fuzzer/BUILD.gn +++ b/test/fuzztest/proxyrunninglocks_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("ProxyRunningLocksFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/queryrunninglocklists_fuzzer/BUILD.gn b/test/fuzztest/queryrunninglocklists_fuzzer/BUILD.gn index 8b55bcd52664016fde878cc76e87dcc48e2f1c8b..e606441dd241ebf2744a43e3870780aecaf04abb 100644 --- a/test/fuzztest/queryrunninglocklists_fuzzer/BUILD.gn +++ b/test/fuzztest/queryrunninglocklists_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("QueryRunningLockListsFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/rebootdevice_fuzzer/BUILD.gn b/test/fuzztest/rebootdevice_fuzzer/BUILD.gn index e9a2511ca965cc586dfb7ba2043695990359b3cc..fc9bc91b559a3540cfe84e7488dc4af4b95c25c9 100644 --- a/test/fuzztest/rebootdevice_fuzzer/BUILD.gn +++ b/test/fuzztest/rebootdevice_fuzzer/BUILD.gn @@ -59,12 +59,12 @@ ohos_fuzztest("RebootDeviceFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "json:nlohmann_json_static", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/refreshactivity_fuzzer/BUILD.gn b/test/fuzztest/refreshactivity_fuzzer/BUILD.gn index 81dd43a05305c971c9eb8b208c73999d6774b62d..eb4b205150bf2d9e86ff1e6a1870e4afb0e628e1 100644 --- a/test/fuzztest/refreshactivity_fuzzer/BUILD.gn +++ b/test/fuzztest/refreshactivity_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("RefreshActivityFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/releaserunninglock_fuzzer/BUILD.gn b/test/fuzztest/releaserunninglock_fuzzer/BUILD.gn index f47612fb53816991872bdd024775db9aafc5ee5c..c0e92730e3d71e53c38677eeaff85364b2c1631b 100644 --- a/test/fuzztest/releaserunninglock_fuzzer/BUILD.gn +++ b/test/fuzztest/releaserunninglock_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("ReleaseRunningLockFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/resetrunninglocks_fuzzer/BUILD.gn b/test/fuzztest/resetrunninglocks_fuzzer/BUILD.gn index c61c50315f7683fc0717b2c8cccf975d1c31d71e..5042f7f92eb56d9879aeb8e7abc6463e8b6fd167 100644 --- a/test/fuzztest/resetrunninglocks_fuzzer/BUILD.gn +++ b/test/fuzztest/resetrunninglocks_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("ResetRunningLocksFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/runninglockcallback_fuzzer/BUILD.gn b/test/fuzztest/runninglockcallback_fuzzer/BUILD.gn index 72fc714b776def04dd0435fc181b9d4fd6eaa598..4f79f85a7ac611cd6fe006045131308a1f46f508 100644 --- a/test/fuzztest/runninglockcallback_fuzzer/BUILD.gn +++ b/test/fuzztest/runninglockcallback_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("RunningLockCallbackFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/screenstatecallback_fuzzer/BUILD.gn b/test/fuzztest/screenstatecallback_fuzzer/BUILD.gn index 2e5ab5ec97b3bbe692a8670ad7827a79cfde0509..404ea65f34b205bd7aebf8e8fec1b993d606fdec 100644 --- a/test/fuzztest/screenstatecallback_fuzzer/BUILD.gn +++ b/test/fuzztest/screenstatecallback_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("ScreenStateCallbackFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/setdisplaysuspend_fuzzer/BUILD.gn b/test/fuzztest/setdisplaysuspend_fuzzer/BUILD.gn index 15a773760cbf9664391c1ed4393002e76f8bc1ee..14e9056d3eaafd064e735dbfb713ab414a75ae10 100644 --- a/test/fuzztest/setdisplaysuspend_fuzzer/BUILD.gn +++ b/test/fuzztest/setdisplaysuspend_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("SetDisplaySuspendFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/setforcetimingout_fuzzer/BUILD.gn b/test/fuzztest/setforcetimingout_fuzzer/BUILD.gn index 5d9d5a1e8fff8df8d13f3a1dd571de7a364d2b9b..a789cdc0e7e1475d4a19ea97ac9fa66aebf786c9 100644 --- a/test/fuzztest/setforcetimingout_fuzzer/BUILD.gn +++ b/test/fuzztest/setforcetimingout_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("SetForceTimingOutFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/setsuspendtag_fuzzer/BUILD.gn b/test/fuzztest/setsuspendtag_fuzzer/BUILD.gn index ed36ecfe86de517827c08ceaf72688f9ba523001..909e6c3fdf6ae664ffb75deada85a54d0cee38e4 100644 --- a/test/fuzztest/setsuspendtag_fuzzer/BUILD.gn +++ b/test/fuzztest/setsuspendtag_fuzzer/BUILD.gn @@ -59,12 +59,12 @@ ohos_fuzztest("SetSuspendTagFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/shelldump_fuzzer/BUILD.gn b/test/fuzztest/shelldump_fuzzer/BUILD.gn index 6d0688d99ee3faaa0234f0d210f16d8a7e65b8a6..6cd02553aebaeca9048b5d98f4c80fe7d2ef8723 100644 --- a/test/fuzztest/shelldump_fuzzer/BUILD.gn +++ b/test/fuzztest/shelldump_fuzzer/BUILD.gn @@ -59,12 +59,12 @@ ohos_fuzztest("ShellDumpFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/shutdowndevice_fuzzer/BUILD.gn b/test/fuzztest/shutdowndevice_fuzzer/BUILD.gn index 8901e797da952efbcfe52ec7bb10e30b19967f0a..0baf9e917e7a0bb797ec3e390187297cdba78842 100644 --- a/test/fuzztest/shutdowndevice_fuzzer/BUILD.gn +++ b/test/fuzztest/shutdowndevice_fuzzer/BUILD.gn @@ -59,12 +59,12 @@ ohos_fuzztest("ShutDownDeviceFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "json:nlohmann_json_static", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/suspenddevice_fuzzer/BUILD.gn b/test/fuzztest/suspenddevice_fuzzer/BUILD.gn index 8a3ed0d74d17d0fbbd8325189d2745f43ddf3390..280be3cadaecdf8607b22271487236527059d2ef 100644 --- a/test/fuzztest/suspenddevice_fuzzer/BUILD.gn +++ b/test/fuzztest/suspenddevice_fuzzer/BUILD.gn @@ -59,12 +59,12 @@ ohos_fuzztest("SuspendDeviceFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/synchibernatecallback_fuzzer/BUILD.gn b/test/fuzztest/synchibernatecallback_fuzzer/BUILD.gn index 6cf62cffc03d1c194418169c01cd7fc9d68eb81d..8b3c7f1eca51bdfe44768cb42c9801ca003605b3 100644 --- a/test/fuzztest/synchibernatecallback_fuzzer/BUILD.gn +++ b/test/fuzztest/synchibernatecallback_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("SyncHibernateCallbackFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/syncshutdowncallback_fuzzer/BUILD.gn b/test/fuzztest/syncshutdowncallback_fuzzer/BUILD.gn index 853e1731a6be9a2451ed00c21c07ebd9263ba32b..faff00c53a08d5e394acb27e575241981c6c6d37 100644 --- a/test/fuzztest/syncshutdowncallback_fuzzer/BUILD.gn +++ b/test/fuzztest/syncshutdowncallback_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("SyncShutdownCallbackFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/syncsleepcallback_fuzzer/BUILD.gn b/test/fuzztest/syncsleepcallback_fuzzer/BUILD.gn index 220505e66142ffadabf088da0d6cc16ed2625f2b..f936cee1c5dd97c6b09c06fd73de2f963f5cd338 100644 --- a/test/fuzztest/syncsleepcallback_fuzzer/BUILD.gn +++ b/test/fuzztest/syncsleepcallback_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("SyncSleepCallbackFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/takeovershutdowncallback_fuzzer/BUILD.gn b/test/fuzztest/takeovershutdowncallback_fuzzer/BUILD.gn index 5c0ee24501eb55cb90d2987cd90d5a3639ed88d7..02130828238f7d04966deb13e094ce61b7be7049 100644 --- a/test/fuzztest/takeovershutdowncallback_fuzzer/BUILD.gn +++ b/test/fuzztest/takeovershutdowncallback_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("TakeOverShutdownCallbackFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "json:nlohmann_json_static", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/wakeupdevice_fuzzer/BUILD.gn b/test/fuzztest/wakeupdevice_fuzzer/BUILD.gn index dc446ccff6a3e3d2855a242e8bd75bd0b1bd71b1..62db1f4b46e12c33ea3ea45192be08503991e19f 100644 --- a/test/fuzztest/wakeupdevice_fuzzer/BUILD.gn +++ b/test/fuzztest/wakeupdevice_fuzzer/BUILD.gn @@ -59,12 +59,12 @@ ohos_fuzztest("WakeupDeviceFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/systemtest/BUILD.gn b/test/systemtest/BUILD.gn index b60a620cbe0c5ed7e3cc1c20f7b7ddf946d3204a..14ea2f4bee7486ad4ccabffa9a8fb801083d16d5 100644 --- a/test/systemtest/BUILD.gn +++ b/test/systemtest/BUILD.gn @@ -43,6 +43,7 @@ deps_ex = [ "ability_runtime:ability_manager", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", + "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_innerkits", "ffrt:libffrt", diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 9472f4e3b3addf4c6d74aedad5ae2b7e073be0cc..f7b7d58e4831c14e756fc0f6b6bcedc300354353 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -61,6 +61,7 @@ deps_ex = [ "ability_base:base", "ability_base:want", "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_innerkits", "config_policy:configpolicy_util", @@ -70,7 +71,6 @@ deps_ex = [ "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", @@ -1282,6 +1282,39 @@ ohos_unittest("test_running_lock_timer_handler") { external_deps = deps_ex } +##############################test_power_config_parse.############################# +ohos_unittest("test_power_config_parse") { + module_out_path = module_output_path + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + + sources = [ + "src/power_config_parse_test.cpp", + ] + + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ":module_mock_private_config", + "${powermgr_utils_path}:coverage_flags", + ] + + include_dirs = [ "${powermgr_service_path}/native/src/setting/" ] + + deps = [ + "${powermgr_inner_api}:powermgr_client", + "${powermgr_service_path}:powermgr_stub", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "${powermgr_utils_path}/setting:power_setting", + ] + + external_deps = deps_ex +} + group("unittest") { testonly = true deps = [ @@ -1291,6 +1324,7 @@ group("unittest") { ":test_mock_parcel", ":test_mock_peer", ":test_mock_proxy", + ":test_power_config_parse", ":test_power_coordination_lock", ":test_power_device_mode", ":test_power_getcontroller_mock", diff --git a/test/unittest/src/interface_test/power_wakeup_controller_test.cpp b/test/unittest/src/interface_test/power_wakeup_controller_test.cpp index aa598bce751f1248881851e0c53ae7ad13ebfe23..6389ced392b8efb5bf0990268669d0710755d48a 100644 --- a/test/unittest/src/interface_test/power_wakeup_controller_test.cpp +++ b/test/unittest/src/interface_test/power_wakeup_controller_test.cpp @@ -16,7 +16,7 @@ #include #include #include - +#include #include "axis_event.h" #include "input_device.h" #include "pointer_event.h" @@ -28,7 +28,6 @@ #include "power_mgr_service.h" #include "power_state_machine.h" #include "setting_helper.h" -#include "json/reader.h" using namespace testing::ext; using namespace OHOS::PowerMgr; @@ -380,19 +379,30 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest011, TestSize.Level "false},\"lid\": {\"enable\": false},\"switch\": {\"enable\": true},\"xxx\": {\"enable\": false}}"; std::shared_ptr parseSources = std::make_shared(); - Json::Reader reader; - Json::Value root; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) { + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { GTEST_LOG_(INFO) << "PowerWakeupControllerTest011: json parse error"; + return; + } + if (!cJSON_IsObject(root)) { + GTEST_LOG_(INFO) << "PowerWakeupControllerTest011: root is not object"; + cJSON_Delete(root); + return; } - Json::Value::Members members = root.getMemberNames(); - for (auto iter = members.begin(); iter != members.end(); iter++) { - std::string key = *iter; - Json::Value valueObj = root[key]; - WakeupSourceParser::ParseSourcesProc(parseSources, valueObj, key); + cJSON* item = NULL; + cJSON_ArrayForEach(item, root) { + const char* key = item->string; + if (!key) { + continue; + } + std::string keyStr = std::string(key); + WakeupSourceParser::ParseSourcesProc(parseSources, item, keyStr); } + + cJSON_Delete(root); + EXPECT_TRUE(parseSources->GetSourceList().size() != 0); GTEST_LOG_(INFO) << "PowerWakeupControllerTest011: end"; POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest011 function end!"); diff --git a/test/unittest/src/power_config_parse_test.cpp b/test/unittest/src/power_config_parse_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3b56dc68b8ba0174c2b9f6b99fa5d8ab207ea41 --- /dev/null +++ b/test/unittest/src/power_config_parse_test.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ +#include +#include +#include "power_mgr_service.h" +#include "power_log.h" + +using namespace testing; +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; + +class PowerConfigParseTest : public Test { +public: + void SetUp() override + { + root_ = cJSON_CreateObject(); + } + + void TearDown() override + { + cJSON_Delete(root_); + } + + cJSON* root_; +}; + +namespace { +constexpr size_t NUMBER_ONE = 1; +constexpr size_t NUMBER_TWO = 2; + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest001, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest001 function start!"); + cJSON_AddItemToObject(root_, "action", cJSON_CreateNumber(NUMBER_ONE)); + cJSON_AddItemToObject(root_, "delayMs", cJSON_CreateNumber(NUMBER_TWO)); + + std::string key = "lid"; + std::shared_ptr parseSources = std::make_shared(); + bool result = SuspendSourceParser::ParseSourcesProc(parseSources, root_, key); + + EXPECT_TRUE(result); + std::vector sources = parseSources->GetSourceList(); + EXPECT_EQ(sources.size(), NUMBER_ONE); + EXPECT_EQ(sources[0].reason_, SuspendDeviceType::SUSPEND_DEVICE_REASON_LID); + EXPECT_EQ(sources[0].action_, NUMBER_ONE); + EXPECT_EQ(sources[0].delayMs_, NUMBER_TWO); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest001 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest002, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest002 function start!"); + cJSON* valueObj = cJSON_CreateNumber(NUMBER_TWO); + cJSON_AddItemToObject(root_, "action", valueObj); + std::string key = "lid"; + std::shared_ptr parseSources = std::make_shared(); + bool result = SuspendSourceParser::ParseSourcesProc(parseSources, root_, key); + EXPECT_TRUE(result); + std::vector sources = parseSources->GetSourceList(); + EXPECT_EQ(sources.size(), NUMBER_ONE); + EXPECT_EQ(sources[0].reason_, SuspendDeviceType::SUSPEND_DEVICE_REASON_LID); + EXPECT_EQ(sources[0].action_, 0); + EXPECT_EQ(sources[0].delayMs_, 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest002 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest003, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest003 function start!"); + std::string key = "lid"; + std::shared_ptr parseSources = std::make_shared(); + bool result = SuspendSourceParser::ParseSourcesProc(parseSources, nullptr, key); + EXPECT_TRUE(result); + std::vector sources = parseSources->GetSourceList(); + EXPECT_EQ(sources.size(), NUMBER_ONE); + EXPECT_EQ(sources[0].reason_, SuspendDeviceType::SUSPEND_DEVICE_REASON_LID); + EXPECT_EQ(sources[0].action_, 0); + EXPECT_EQ(sources[0].delayMs_, 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest003 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest004, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest004 function start!"); + cJSON_AddItemToObject(root_, "action", cJSON_CreateNumber(NUMBER_ONE)); + + std::string key = "lid"; + std::shared_ptr parseSources = std::make_shared(); + bool result = SuspendSourceParser::ParseSourcesProc(parseSources, root_, key); + + EXPECT_TRUE(result); + std::vector sources = parseSources->GetSourceList(); + EXPECT_EQ(sources.size(), NUMBER_ONE); + EXPECT_EQ(sources[0].reason_, SuspendDeviceType::SUSPEND_DEVICE_REASON_LID); + EXPECT_EQ(sources[0].action_, 0); + EXPECT_EQ(sources[0].delayMs_, 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest004 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest005, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest005 function start!"); + cJSON_AddItemToObject(root_, "delayMs", cJSON_CreateNumber(NUMBER_TWO)); + + std::string key = "lid"; + std::shared_ptr parseSources = std::make_shared(); + bool result = SuspendSourceParser::ParseSourcesProc(parseSources, root_, key); + + EXPECT_TRUE(result); + std::vector sources = parseSources->GetSourceList(); + EXPECT_EQ(sources.size(), NUMBER_ONE); + EXPECT_EQ(sources[0].reason_, SuspendDeviceType::SUSPEND_DEVICE_REASON_LID); + EXPECT_EQ(sources[0].action_, 0); + EXPECT_EQ(sources[0].delayMs_, 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest005 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest006, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest006 function start!"); + const std::string json = R"({"lid": {"action": 1, "delayMs": 2}})"; + auto parseSources = SuspendSourceParser::ParseSources(json); + std::vector sources = parseSources->GetSourceList(); + EXPECT_EQ(sources.size(), NUMBER_ONE); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest006 function end!"); +} + + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest007, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest007 function start!"); + auto parseSources = SuspendSourceParser::ParseSources(""); + std::vector sources = parseSources->GetSourceList(); + EXPECT_EQ(sources.size(), 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest007 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest008, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest008 function start!"); + auto parseSources = SuspendSourceParser::ParseSources("[]"); + std::vector sources = parseSources->GetSourceList(); + EXPECT_EQ(sources.size(), 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest008 function end!"); +} +} // namespace diff --git a/test/unittest/src/power_parsesources_mock_test.cpp b/test/unittest/src/power_parsesources_mock_test.cpp index 36408109d5b67527a6b98756b49ccc4c6794b4b7..9aad63d6c55b9475c2aad51fa28d1bf8aaf0f491 100644 --- a/test/unittest/src/power_parsesources_mock_test.cpp +++ b/test/unittest/src/power_parsesources_mock_test.cpp @@ -38,13 +38,13 @@ bool SettingHelper::IsSuspendSourcesSettingValid() } bool SuspendSourceParser::ParseSourcesProc( - std::shared_ptr& parseSources, Json::Value& valueObj, std::string& key) + std::shared_ptr& parseSources, cJSON* valueObj, std::string& key) { return false; } bool WakeupSourceParser::ParseSourcesProc( - std::shared_ptr& parseSources, Json::Value& valueObj, std::string& key) + std::shared_ptr& parseSources, cJSON* valueObj, std::string& key) { return false; } diff --git a/test/unittest/src/scenario_test/wakeup_suspend/power_wakeup_test.cpp b/test/unittest/src/scenario_test/wakeup_suspend/power_wakeup_test.cpp index e4b80d8b277f4638af4ebcd292245c47d2a67250..97f7a7737a0ccee9e1ca5dc2f1ae48a867cf78be 100644 --- a/test/unittest/src/scenario_test/wakeup_suspend/power_wakeup_test.cpp +++ b/test/unittest/src/scenario_test/wakeup_suspend/power_wakeup_test.cpp @@ -16,7 +16,7 @@ #include #include #include - +#include #include "axis_event.h" #include "input_device.h" #include "pointer_event.h" @@ -27,7 +27,6 @@ #include "power_mgr_client.h" #include "power_state_machine.h" #include "setting_helper.h" -#include "json/reader.h" using namespace testing::ext; using namespace OHOS::PowerMgr; diff --git a/test/unittest/src/servicetest/BUILD.gn b/test/unittest/src/servicetest/BUILD.gn index c73c8069e36c40a7d333f7085a33ed92d3057ddd..75635d4c2e728d3dce51c7330d53063bfadf21b9 100644 --- a/test/unittest/src/servicetest/BUILD.gn +++ b/test/unittest/src/servicetest/BUILD.gn @@ -52,6 +52,7 @@ deps_ex = [ "ability_base:base", "ability_base:want", "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_innerkits", "ffrt:libffrt", diff --git a/utils/vibrator/BUILD.gn b/utils/vibrator/BUILD.gn index 8fc1dc9ac36072219cb2dbccea261352fa42261f..fa13dcbeaab5f50acc8cae6ef6d21953a0e8d836 100644 --- a/utils/vibrator/BUILD.gn +++ b/utils/vibrator/BUILD.gn @@ -35,10 +35,10 @@ ohos_shared_library("power_vibrator") { public_configs = [ ":public_config" ] external_deps = [ + "cJSON:cjson", "c_utils:utils", "config_policy:configpolicy_util", "hilog:libhilog", - "jsoncpp:jsoncpp", ] if (defined(global_parts_info) && defined(global_parts_info.sensors_miscdevice)) { diff --git a/utils/vibrator/include/vibrator_source_parser.h b/utils/vibrator/include/vibrator_source_parser.h index d2cad83bdcac98aa3c41be18ad71119ab9ebda55..6e3b10da9618a16d1507193187386ef34acdcbb3 100644 --- a/utils/vibrator/include/vibrator_source_parser.h +++ b/utils/vibrator/include/vibrator_source_parser.h @@ -18,7 +18,7 @@ #include #include -#include "json/value.h" +#include namespace OHOS { namespace PowerMgr { @@ -56,7 +56,7 @@ private: std::vector ParseSources(const std::string& config); void GetTargetPath(std::string& targetPath, const std::string& etcPath, const std::string& vendorPath, const std::string& systemPath); - void ParseSourcesProc(std::vector& sources, Json::Value& valueObj, std::string& key); + void ParseSourcesProc(std::vector& sources, cJSON* valueObj, std::string& key); }; } // namespace PowerMgr } // namespace OHOS diff --git a/utils/vibrator/src/vibrator_source_parser.cpp b/utils/vibrator/src/vibrator_source_parser.cpp index 20f4e4b86af1cfdbb9e752d0b5f7f39ee702828a..51f594fad2baaa035d18f592c8b1866f5abd5d82 100644 --- a/utils/vibrator/src/vibrator_source_parser.cpp +++ b/utils/vibrator/src/vibrator_source_parser.cpp @@ -18,10 +18,9 @@ #include #include #include +#include #include "config_policy_utils.h" #include "power_log.h" -#include "json/reader.h" -#include "json/value.h" namespace OHOS { namespace PowerMgr { @@ -71,37 +70,64 @@ void VibratorSourceParser::GetTargetPath( std::vector VibratorSourceParser::ParseSources(const std::string& jsonStr) { std::vector sources; - Json::Reader reader; - Json::Value root; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) { - POWER_HILOGE(COMP_UTILS, "json parse error"); + + if (jsonStr.empty()) { + POWER_HILOGE(COMP_UTILS, "Input JSON string is empty"); return sources; } - Json::Value::Members members = root.getMemberNames(); - for (auto iter = members.begin(); iter != members.end(); iter++) { - std::string key = *iter; - Json::Value valueObj = root[key]; - POWER_HILOGI(COMP_UTILS, "key=%{public}s", key.c_str()); - ParseSourcesProc(sources, valueObj, key); + + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { + POWER_HILOGE(COMP_UTILS, "JSON parse error"); + return sources; + } + if (!cJSON_IsObject(root)) { + POWER_HILOGE(COMP_UTILS, "JSON root is not object"); + cJSON_Delete(root); + return sources; } + + cJSON* item = nullptr; + cJSON_ArrayForEach(item, root) { + const char* key = item->string; + if (!key) { + POWER_HILOGE(COMP_UTILS, "invalid key in json object"); + continue; + } + std::string keyStr = std::string(key); + POWER_HILOGI(COMP_UTILS, "key=%{public}s", keyStr.c_str()); + ParseSourcesProc(sources, item, keyStr); + } + + cJSON_Delete(root); return sources; } void VibratorSourceParser::ParseSourcesProc( - std::vector& sources, Json::Value& valueObj, std::string& key) + std::vector& sources, cJSON* valueObj, std::string& key) { - if (!valueObj.isObject()) { + if (!cJSON_IsObject(valueObj)) { + POWER_HILOGE(COMP_UTILS, "ValueObj is not a json object."); return; } - std::string type; + bool enable = false; - Json::Value enableValue = valueObj[VibratorSource::ENABLE_KEY]; - Json::Value typeValue = valueObj[VibratorSource::TYPE_KEY]; - if (!typeValue.isString() || !enableValue.isBool()) { + std::string type; + + cJSON* enableItem = cJSON_GetObjectItemCaseSensitive(valueObj, VibratorSource::ENABLE_KEY); + if (!enableItem || !cJSON_IsBool(enableItem)) { + POWER_HILOGE(COMP_UTILS, "Parse enable error."); return; } - enable = enableValue.asBool(); - type = typeValue.asString(); + enable = cJSON_IsTrue(enableItem); + + cJSON* typeItem = cJSON_GetObjectItemCaseSensitive(valueObj, VibratorSource::TYPE_KEY); + if (!typeItem || !cJSON_IsString(typeItem)) { + POWER_HILOGE(COMP_UTILS, "Parse type error."); + return; + } + type = typeItem->valuestring; + if (!enable || type.empty()) { return; }