199552fe9Sopenharmony_ci/* 299552fe9Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 399552fe9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 499552fe9Sopenharmony_ci * you may not use this file except in compliance with the License. 599552fe9Sopenharmony_ci * You may obtain a copy of the License at 699552fe9Sopenharmony_ci * 799552fe9Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 899552fe9Sopenharmony_ci * 999552fe9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1099552fe9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1199552fe9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1299552fe9Sopenharmony_ci * See the License for the specific language governing permissions and 1399552fe9Sopenharmony_ci * limitations under the License. 1499552fe9Sopenharmony_ci */ 1599552fe9Sopenharmony_ci 1699552fe9Sopenharmony_ci#include "standby_config_manager.h" 1799552fe9Sopenharmony_ci 1899552fe9Sopenharmony_ci#include <functional> 1999552fe9Sopenharmony_ci#include <string> 2099552fe9Sopenharmony_ci#include <sstream> 2199552fe9Sopenharmony_ci#include <unistd.h> 2299552fe9Sopenharmony_ci#include <dlfcn.h> 2399552fe9Sopenharmony_ci 2499552fe9Sopenharmony_ci#ifdef STANDBY_CONFIG_POLICY_ENABLE 2599552fe9Sopenharmony_ci#include "config_policy_utils.h" 2699552fe9Sopenharmony_ci#endif 2799552fe9Sopenharmony_ci#include "json_utils.h" 2899552fe9Sopenharmony_ci#include "standby_service_log.h" 2999552fe9Sopenharmony_ci 3099552fe9Sopenharmony_cinamespace OHOS { 3199552fe9Sopenharmony_cinamespace DevStandbyMgr { 3299552fe9Sopenharmony_cinamespace { 3399552fe9Sopenharmony_ci const std::string DEFAULT_CONFIG_ROOT_DIR = "/system"; 3499552fe9Sopenharmony_ci const std::string STANDBY_CONFIG_PATH = "/etc/standby_service/device_standby_config.json"; 3599552fe9Sopenharmony_ci const int32_t STANDBY_CONFIG_INDEX = 5; 3699552fe9Sopenharmony_ci const std::string STRATEGY_CONFIG_PATH = "/etc/standby_service/standby_strategy_config.json"; 3799552fe9Sopenharmony_ci const int32_t STRATEGY_CONFIG_INDEX = 6; 3899552fe9Sopenharmony_ci const int32_t CLOUD_CONFIG_INDEX = 7; 3999552fe9Sopenharmony_ci const char* EXT_CONFIG_LIB = "libsuspend_manager_service.z.so"; 4099552fe9Sopenharmony_ci const std::string TAG_PLUGIN_NAME = "plugin_name"; 4199552fe9Sopenharmony_ci const std::string TAG_STANDBY = "standby"; 4299552fe9Sopenharmony_ci const std::string TAG_MAINTENANCE_LIST = "maintenance_list"; 4399552fe9Sopenharmony_ci const std::string TAG_DETECT_LIST = "detect_list"; 4499552fe9Sopenharmony_ci const std::string TAG_STRATEGY_LIST = "strategy_list"; 4599552fe9Sopenharmony_ci const std::string TAG_HALFHOUR_SWITCH_SETTING = "halfhour_switch_setting"; 4699552fe9Sopenharmony_ci const std::string TAG_LADDER_BATTERY_LIST = "ladder_battery_threshold_list"; 4799552fe9Sopenharmony_ci 4899552fe9Sopenharmony_ci const std::string TAG_SETTING_LIST = "setting_list"; 4999552fe9Sopenharmony_ci const std::string TAG_VER = "version"; 5099552fe9Sopenharmony_ci const int VERSION_LEN = 4; 5199552fe9Sopenharmony_ci const int DEC = 10; 5299552fe9Sopenharmony_ci const char VERSION_DELIM = '.'; 5399552fe9Sopenharmony_ci const std::string TAG_CONDITION = "condition"; 5499552fe9Sopenharmony_ci const std::string TAG_ACTION = "action"; 5599552fe9Sopenharmony_ci const std::string TAG_ALLOW = "allow"; 5699552fe9Sopenharmony_ci const std::string TAG_PROCESSES = "processes"; 5799552fe9Sopenharmony_ci const std::string TAG_APPS = "apps"; 5899552fe9Sopenharmony_ci const std::string TAG_PROCESSES_LIMIT = "processes_limit"; 5999552fe9Sopenharmony_ci const std::string TAG_TIME_CLOCK_APPS = "time_clock_apps"; 6099552fe9Sopenharmony_ci const std::string TAG_APPS_LIMIT = "apps_limit"; 6199552fe9Sopenharmony_ci const std::string TAG_NAME = "name"; 6299552fe9Sopenharmony_ci const std::string TAG_MAX_DURATION_LIM = "duration"; 6399552fe9Sopenharmony_ci 6499552fe9Sopenharmony_ci const std::string TAG_TIMER = "TIMER"; 6599552fe9Sopenharmony_ci const std::string TAG_TIMER_CLOCK = "timer_clock"; 6699552fe9Sopenharmony_ci const std::string TAG_TIMER_PERIOD = "timer_period"; 6799552fe9Sopenharmony_ci 6899552fe9Sopenharmony_ci const char TAG_CONDITION_DELIM = '&'; 6999552fe9Sopenharmony_ci const std::string TAG_DAY_STANDBY = "day_standby"; 7099552fe9Sopenharmony_ci const std::string TAG_NIGHT_STANDBY = "night_standby"; 7199552fe9Sopenharmony_ci const std::string TAG_SCREENOFF = "screenoff"; 7299552fe9Sopenharmony_ci const std::string TAG_SCREENOFF_HALFHOUR = "screenoff_halfhour"; 7399552fe9Sopenharmony_ci const std::unordered_map<std::string, ConditionType::Type> conditionMap = { 7499552fe9Sopenharmony_ci {TAG_DAY_STANDBY, ConditionType::DAY_STANDBY}, 7599552fe9Sopenharmony_ci {TAG_NIGHT_STANDBY, ConditionType::NIGHT_STANDBY}, 7699552fe9Sopenharmony_ci }; 7799552fe9Sopenharmony_ci} 7899552fe9Sopenharmony_ci 7999552fe9Sopenharmony_ciStandbyConfigManager::StandbyConfigManager() {} 8099552fe9Sopenharmony_ci 8199552fe9Sopenharmony_ciStandbyConfigManager::~StandbyConfigManager() {} 8299552fe9Sopenharmony_ci 8399552fe9Sopenharmony_cistd::shared_ptr<StandbyConfigManager> StandbyConfigManager::GetInstance() 8499552fe9Sopenharmony_ci{ 8599552fe9Sopenharmony_ci return DelayedSingleton<StandbyConfigManager>::GetInstance(); 8699552fe9Sopenharmony_ci} 8799552fe9Sopenharmony_ci 8899552fe9Sopenharmony_ciErrCode StandbyConfigManager::Init() 8999552fe9Sopenharmony_ci{ 9099552fe9Sopenharmony_ci STANDBYSERVICE_LOGI("start to read config"); 9199552fe9Sopenharmony_ci LoadGetExtConfigFunc(); 9299552fe9Sopenharmony_ci GetAndParseStandbyConfig(); 9399552fe9Sopenharmony_ci GetAndParseStrategyConfig(); 9499552fe9Sopenharmony_ci if (NeedsToReadCloudConfig()) { 9599552fe9Sopenharmony_ci GetCloudConfig(); 9699552fe9Sopenharmony_ci } 9799552fe9Sopenharmony_ci return ERR_OK; 9899552fe9Sopenharmony_ci} 9999552fe9Sopenharmony_ci 10099552fe9Sopenharmony_civoid StandbyConfigManager::GetAndParseStandbyConfig() 10199552fe9Sopenharmony_ci{ 10299552fe9Sopenharmony_ci std::vector<std::string> configContentList; 10399552fe9Sopenharmony_ci if (getExtConfigFunc_ != nullptr && getExtConfigFunc_(STANDBY_CONFIG_INDEX, configContentList) == ERR_OK) { 10499552fe9Sopenharmony_ci for (const auto& content : configContentList) { 10599552fe9Sopenharmony_ci nlohmann::json devStandbyConfigRoot; 10699552fe9Sopenharmony_ci if (!JsonUtils::LoadJsonValueFromContent(devStandbyConfigRoot, content)) { 10799552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("load config failed"); 10899552fe9Sopenharmony_ci continue; 10999552fe9Sopenharmony_ci } 11099552fe9Sopenharmony_ci if (!ParseDeviceStanbyConfig(devStandbyConfigRoot)) { 11199552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("parse config failed"); 11299552fe9Sopenharmony_ci } 11399552fe9Sopenharmony_ci } 11499552fe9Sopenharmony_ci } else { 11599552fe9Sopenharmony_ci std::vector<std::string> configFileList = GetConfigFileList(STANDBY_CONFIG_PATH); 11699552fe9Sopenharmony_ci for (const auto& configFile : configFileList) { 11799552fe9Sopenharmony_ci nlohmann::json devStandbyConfigRoot; 11899552fe9Sopenharmony_ci // if failed to load one json file, read next config file 11999552fe9Sopenharmony_ci if (!JsonUtils::LoadJsonValueFromFile(devStandbyConfigRoot, configFile)) { 12099552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("load config file %{public}s failed", configFile.c_str()); 12199552fe9Sopenharmony_ci continue; 12299552fe9Sopenharmony_ci } 12399552fe9Sopenharmony_ci if (!ParseDeviceStanbyConfig(devStandbyConfigRoot)) { 12499552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("parse config file %{public}s failed", configFile.c_str()); 12599552fe9Sopenharmony_ci } 12699552fe9Sopenharmony_ci } 12799552fe9Sopenharmony_ci } 12899552fe9Sopenharmony_ci UpdateStrategyList(); 12999552fe9Sopenharmony_ci} 13099552fe9Sopenharmony_ci 13199552fe9Sopenharmony_civoid StandbyConfigManager::GetAndParseStrategyConfig() 13299552fe9Sopenharmony_ci{ 13399552fe9Sopenharmony_ci std::vector<std::string> configContentList; 13499552fe9Sopenharmony_ci if (getExtConfigFunc_ != nullptr && getExtConfigFunc_(STRATEGY_CONFIG_INDEX, configContentList) == ERR_OK) { 13599552fe9Sopenharmony_ci for (const auto& content : configContentList) { 13699552fe9Sopenharmony_ci nlohmann::json resCtrlConfigRoot; 13799552fe9Sopenharmony_ci if (!JsonUtils::LoadJsonValueFromContent(resCtrlConfigRoot, content)) { 13899552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("load config failed"); 13999552fe9Sopenharmony_ci continue; 14099552fe9Sopenharmony_ci } 14199552fe9Sopenharmony_ci if (!ParseResCtrlConfig(resCtrlConfigRoot)) { 14299552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("parse config failed"); 14399552fe9Sopenharmony_ci } 14499552fe9Sopenharmony_ci } 14599552fe9Sopenharmony_ci } else { 14699552fe9Sopenharmony_ci std::vector<std::string> configFileList = GetConfigFileList(STRATEGY_CONFIG_PATH); 14799552fe9Sopenharmony_ci for (const auto& configFile : configFileList) { 14899552fe9Sopenharmony_ci nlohmann::json resCtrlConfigRoot; 14999552fe9Sopenharmony_ci if (!JsonUtils::LoadJsonValueFromFile(resCtrlConfigRoot, configFile)) { 15099552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("load config file %{public}s failed", configFile.c_str()); 15199552fe9Sopenharmony_ci continue; 15299552fe9Sopenharmony_ci } 15399552fe9Sopenharmony_ci if (!ParseResCtrlConfig(resCtrlConfigRoot)) { 15499552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("parse config file %{public}s failed", configFile.c_str()); 15599552fe9Sopenharmony_ci } 15699552fe9Sopenharmony_ci } 15799552fe9Sopenharmony_ci } 15899552fe9Sopenharmony_ci} 15999552fe9Sopenharmony_ci 16099552fe9Sopenharmony_civoid StandbyConfigManager::GetCloudConfig() 16199552fe9Sopenharmony_ci{ 16299552fe9Sopenharmony_ci if (getSingleExtConfigFunc_ == nullptr) { 16399552fe9Sopenharmony_ci return; 16499552fe9Sopenharmony_ci } 16599552fe9Sopenharmony_ci std::string configCloud; 16699552fe9Sopenharmony_ci int32_t returnCode = getSingleExtConfigFunc_(CLOUD_CONFIG_INDEX, configCloud); 16799552fe9Sopenharmony_ci if (returnCode == ERR_OK) { 16899552fe9Sopenharmony_ci nlohmann::json ConfigRoot; 16999552fe9Sopenharmony_ci JsonUtils::LoadJsonValueFromContent(ConfigRoot, configCloud); 17099552fe9Sopenharmony_ci ParseCloudConfig(ConfigRoot); 17199552fe9Sopenharmony_ci } else { 17299552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("Decrypt errcode: %{public}d.", returnCode); 17399552fe9Sopenharmony_ci } 17499552fe9Sopenharmony_ci UpdateStrategyList(); 17599552fe9Sopenharmony_ci} 17699552fe9Sopenharmony_ci 17799552fe9Sopenharmony_civoid StandbyConfigManager::ParseCloudConfig(const nlohmann::json& devConfigRoot) 17899552fe9Sopenharmony_ci{ 17999552fe9Sopenharmony_ci nlohmann::json settingConfig; 18099552fe9Sopenharmony_ci nlohmann::json listConfig; 18199552fe9Sopenharmony_ci 18299552fe9Sopenharmony_ci if (JsonUtils::GetObjFromJsonValue(devConfigRoot, TAG_SETTING_LIST, settingConfig) && 18399552fe9Sopenharmony_ci !ParseStandbyConfig(settingConfig)) { 18499552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("Failed to parse cloud config in %{public}s", TAG_SETTING_LIST.c_str()); 18599552fe9Sopenharmony_ci } 18699552fe9Sopenharmony_ci if (JsonUtils::GetObjFromJsonValue(devConfigRoot, TAG_STRATEGY_LIST, listConfig) && 18799552fe9Sopenharmony_ci !ParseStrategyListConfig(listConfig)) { 18899552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("Failed to parse cloud config in %{public}s", TAG_STRATEGY_LIST.c_str()); 18999552fe9Sopenharmony_ci } 19099552fe9Sopenharmony_ci if (!ParseResCtrlConfig(devConfigRoot)) { 19199552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("Failed to parse cloud config in standby strategy."); 19299552fe9Sopenharmony_ci } 19399552fe9Sopenharmony_ci} 19499552fe9Sopenharmony_ci 19599552fe9Sopenharmony_civoid StandbyConfigManager::LoadGetExtConfigFunc() 19699552fe9Sopenharmony_ci{ 19799552fe9Sopenharmony_ci auto handle = dlopen(EXT_CONFIG_LIB, RTLD_NOW); 19899552fe9Sopenharmony_ci if (!handle) { 19999552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("not find lib"); 20099552fe9Sopenharmony_ci return; 20199552fe9Sopenharmony_ci } 20299552fe9Sopenharmony_ci getExtConfigFunc_ = reinterpret_cast<GetExtConfigFunc>(dlsym(handle, "GetExtMultiConfig")); 20399552fe9Sopenharmony_ci getSingleExtConfigFunc_ = reinterpret_cast<GetSingleExtConfigFunc>(dlsym(handle, "GetExtConfig")); 20499552fe9Sopenharmony_ci if (!getSingleExtConfigFunc_) { 20599552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("Failed to load GetExtConfig."); 20699552fe9Sopenharmony_ci } 20799552fe9Sopenharmony_ci if (!getExtConfigFunc_) { 20899552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("get func failed"); 20999552fe9Sopenharmony_ci dlclose(handle); 21099552fe9Sopenharmony_ci } 21199552fe9Sopenharmony_ci} 21299552fe9Sopenharmony_ci 21399552fe9Sopenharmony_cibool StandbyConfigManager::NeedsToReadCloudConfig() 21499552fe9Sopenharmony_ci{ 21599552fe9Sopenharmony_ci std::string cloudConfigVer; 21699552fe9Sopenharmony_ci std::string deviceConfigVer; 21799552fe9Sopenharmony_ci std::string strategyConfigVer; 21899552fe9Sopenharmony_ci if (!GetParamVersion(STANDBY_CONFIG_INDEX, deviceConfigVer)) { 21999552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("failed to get the version of fileIndex: %{public}d", STANDBY_CONFIG_INDEX); 22099552fe9Sopenharmony_ci } 22199552fe9Sopenharmony_ci if (!GetParamVersion(STRATEGY_CONFIG_INDEX, strategyConfigVer)) { 22299552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("failed to get the version of fileIndex: %{public}d", STRATEGY_CONFIG_INDEX); 22399552fe9Sopenharmony_ci } 22499552fe9Sopenharmony_ci if (!GetCloudVersion(CLOUD_CONFIG_INDEX, cloudConfigVer)) { 22599552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("failed to get the version of fileIndex: %{public}d", CLOUD_CONFIG_INDEX); 22699552fe9Sopenharmony_ci } 22799552fe9Sopenharmony_ci std::string temp; 22899552fe9Sopenharmony_ci int result = CompareVersion(deviceConfigVer, strategyConfigVer); 22999552fe9Sopenharmony_ci if (result < 0) { 23099552fe9Sopenharmony_ci STANDBYSERVICE_LOGI("do not need to read cloud config."); 23199552fe9Sopenharmony_ci return false; 23299552fe9Sopenharmony_ci } else { 23399552fe9Sopenharmony_ci temp = (result > 0)? deviceConfigVer : strategyConfigVer; 23499552fe9Sopenharmony_ci } 23599552fe9Sopenharmony_ci bool ret = CompareVersion(cloudConfigVer, temp) > 0; 23699552fe9Sopenharmony_ci STANDBYSERVICE_LOGI("cloud config:%{public}d, cloud:%{public}s, device:%{public}s, strategy:%{public}s", 23799552fe9Sopenharmony_ci ret, cloudConfigVer.c_str(), deviceConfigVer.c_str(), strategyConfigVer.c_str()); 23899552fe9Sopenharmony_ci return ret; 23999552fe9Sopenharmony_ci} 24099552fe9Sopenharmony_ci 24199552fe9Sopenharmony_ciint StandbyConfigManager::CompareVersion(const std::string& configVerA, const std::string& configVerB) 24299552fe9Sopenharmony_ci{ 24399552fe9Sopenharmony_ci if (!configVerA.empty() && configVerB.empty()) { 24499552fe9Sopenharmony_ci return 1; 24599552fe9Sopenharmony_ci } 24699552fe9Sopenharmony_ci if (configVerA.empty() && !configVerB.empty()) { 24799552fe9Sopenharmony_ci return 0; 24899552fe9Sopenharmony_ci } 24999552fe9Sopenharmony_ci if (configVerA.empty() && configVerB.empty()) { 25099552fe9Sopenharmony_ci return -1; 25199552fe9Sopenharmony_ci } 25299552fe9Sopenharmony_ci std::vector<std::string> segA = JsonUtils::SplitVersion(configVerA, VERSION_DELIM); 25399552fe9Sopenharmony_ci if (segA.size() != VERSION_LEN) { 25499552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("segment size error: %{public}s", configVerA.c_str()); 25599552fe9Sopenharmony_ci return -1; 25699552fe9Sopenharmony_ci } 25799552fe9Sopenharmony_ci std::vector<std::string> segB = JsonUtils::SplitVersion(configVerB, VERSION_DELIM); 25899552fe9Sopenharmony_ci if (segB.size() != VERSION_LEN) { 25999552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("segment size error: %{public}s", configVerB.c_str()); 26099552fe9Sopenharmony_ci return -1; 26199552fe9Sopenharmony_ci } 26299552fe9Sopenharmony_ci for (int i = 0; i < VERSION_LEN; i++) { 26399552fe9Sopenharmony_ci if (!isdigit(segA[i][0]) || !isdigit(segB[i][0])) { 26499552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("segment not digit"); 26599552fe9Sopenharmony_ci return -1; 26699552fe9Sopenharmony_ci } 26799552fe9Sopenharmony_ci if (segB[i] != segA[i]) { 26899552fe9Sopenharmony_ci int ret = (strtol(segB[i].c_str(), nullptr, DEC) < strtol(segA[i].c_str(), nullptr, DEC)) ? 1 : 0; 26999552fe9Sopenharmony_ci return ret; 27099552fe9Sopenharmony_ci } 27199552fe9Sopenharmony_ci } 27299552fe9Sopenharmony_ci return 1; 27399552fe9Sopenharmony_ci} 27499552fe9Sopenharmony_ci 27599552fe9Sopenharmony_cibool StandbyConfigManager::GetParamVersion(const int32_t& fileIndex, std::string& version) 27699552fe9Sopenharmony_ci{ 27799552fe9Sopenharmony_ci if (getExtConfigFunc_ == nullptr) { 27899552fe9Sopenharmony_ci return true; 27999552fe9Sopenharmony_ci } 28099552fe9Sopenharmony_ci if (fileIndex != STANDBY_CONFIG_INDEX && fileIndex != STRATEGY_CONFIG_INDEX) { 28199552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("invalid input when getting version."); 28299552fe9Sopenharmony_ci return false; 28399552fe9Sopenharmony_ci } 28499552fe9Sopenharmony_ci std::vector<std::string> configContentList; 28599552fe9Sopenharmony_ci int32_t returnCode = getExtConfigFunc_(fileIndex, configContentList); 28699552fe9Sopenharmony_ci if (returnCode != ERR_OK) { 28799552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("Decrypt fail."); 28899552fe9Sopenharmony_ci return false; 28999552fe9Sopenharmony_ci } 29099552fe9Sopenharmony_ci std::string tempVersion; 29199552fe9Sopenharmony_ci for (const auto& content : configContentList) { 29299552fe9Sopenharmony_ci nlohmann::json devStandbyConfigRoot; 29399552fe9Sopenharmony_ci if (!JsonUtils::LoadJsonValueFromContent(devStandbyConfigRoot, content)) { 29499552fe9Sopenharmony_ci continue; 29599552fe9Sopenharmony_ci } 29699552fe9Sopenharmony_ci if (!JsonUtils::GetStringFromJsonValue(devStandbyConfigRoot, TAG_VER, tempVersion)) { 29799552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("failed to get version"); 29899552fe9Sopenharmony_ci continue; 29999552fe9Sopenharmony_ci } 30099552fe9Sopenharmony_ci if (CompareVersion(tempVersion, version)) { 30199552fe9Sopenharmony_ci version = tempVersion; 30299552fe9Sopenharmony_ci } 30399552fe9Sopenharmony_ci } 30499552fe9Sopenharmony_ci return true; 30599552fe9Sopenharmony_ci} 30699552fe9Sopenharmony_ci 30799552fe9Sopenharmony_cibool StandbyConfigManager::GetCloudVersion(const int32_t& fileIndex, std::string& version) 30899552fe9Sopenharmony_ci{ 30999552fe9Sopenharmony_ci if (getSingleExtConfigFunc_ == nullptr) { 31099552fe9Sopenharmony_ci return true; 31199552fe9Sopenharmony_ci } 31299552fe9Sopenharmony_ci if (fileIndex != CLOUD_CONFIG_INDEX) { 31399552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("invalid input when getting version."); 31499552fe9Sopenharmony_ci return false; 31599552fe9Sopenharmony_ci } 31699552fe9Sopenharmony_ci std::string configCloud; 31799552fe9Sopenharmony_ci int32_t returnCode = getSingleExtConfigFunc_(fileIndex, configCloud); 31899552fe9Sopenharmony_ci if (returnCode != ERR_OK) { 31999552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("Decrypt fail."); 32099552fe9Sopenharmony_ci return false; 32199552fe9Sopenharmony_ci } 32299552fe9Sopenharmony_ci nlohmann::json devStandbyConfigRoot; 32399552fe9Sopenharmony_ci JsonUtils::LoadJsonValueFromContent(devStandbyConfigRoot, configCloud); 32499552fe9Sopenharmony_ci if (!JsonUtils::GetStringFromJsonValue(devStandbyConfigRoot, TAG_VER, version)) { 32599552fe9Sopenharmony_ci STANDBYSERVICE_LOGE("failed to get version"); 32699552fe9Sopenharmony_ci } 32799552fe9Sopenharmony_ci return true; 32899552fe9Sopenharmony_ci} 32999552fe9Sopenharmony_ci 33099552fe9Sopenharmony_cistd::vector<std::string> StandbyConfigManager::GetConfigFileList(const std::string& relativeConfigPath) 33199552fe9Sopenharmony_ci{ 33299552fe9Sopenharmony_ci std::list<std::string> rootDirList; 33399552fe9Sopenharmony_ci#ifdef STANDBY_CONFIG_POLICY_ENABLE 33499552fe9Sopenharmony_ci auto cfgDirList = GetCfgDirList(); 33599552fe9Sopenharmony_ci if (cfgDirList != nullptr) { 33699552fe9Sopenharmony_ci for (const auto &cfgDir : cfgDirList->paths) { 33799552fe9Sopenharmony_ci if (cfgDir == nullptr) { 33899552fe9Sopenharmony_ci continue; 33999552fe9Sopenharmony_ci } 34099552fe9Sopenharmony_ci STANDBYSERVICE_LOGD("cfgDir: %{public}s ", cfgDir); 34199552fe9Sopenharmony_ci rootDirList.emplace_back(cfgDir); 34299552fe9Sopenharmony_ci } 34399552fe9Sopenharmony_ci FreeCfgDirList(cfgDirList); 34499552fe9Sopenharmony_ci } 34599552fe9Sopenharmony_ci#endif 34699552fe9Sopenharmony_ci if (std::find(rootDirList.begin(), rootDirList.end(), DEFAULT_CONFIG_ROOT_DIR) 34799552fe9Sopenharmony_ci == rootDirList.end()) { 34899552fe9Sopenharmony_ci rootDirList.emplace_front(DEFAULT_CONFIG_ROOT_DIR); 34999552fe9Sopenharmony_ci } 35099552fe9Sopenharmony_ci std::string baseRealPath; 35199552fe9Sopenharmony_ci std::vector<std::string> configFilesList; 35299552fe9Sopenharmony_ci for (auto configDir : rootDirList) { 35399552fe9Sopenharmony_ci if (JsonUtils::GetRealPath(configDir + relativeConfigPath, baseRealPath) 35499552fe9Sopenharmony_ci && access(baseRealPath.c_str(), F_OK) == ERR_OK) { 35599552fe9Sopenharmony_ci STANDBYSERVICE_LOGD("Get valid base config file: %{public}s", baseRealPath.c_str()); 35699552fe9Sopenharmony_ci configFilesList.emplace_back(baseRealPath); 35799552fe9Sopenharmony_ci } 35899552fe9Sopenharmony_ci } 35999552fe9Sopenharmony_ci return configFilesList; 36099552fe9Sopenharmony_ci} 36199552fe9Sopenharmony_ci 36299552fe9Sopenharmony_ciconst std::string& StandbyConfigManager::GetPluginName() 36399552fe9Sopenharmony_ci{ 36499552fe9Sopenharmony_ci return pluginName_; 36599552fe9Sopenharmony_ci} 36699552fe9Sopenharmony_ci 36799552fe9Sopenharmony_cinlohmann::json StandbyConfigManager::GetDefaultConfig(const std::string& configName) 36899552fe9Sopenharmony_ci{ 36999552fe9Sopenharmony_ci return GetConfigWithName(configName, standbyStrategyConfigMap_); 37099552fe9Sopenharmony_ci} 37199552fe9Sopenharmony_ci 37299552fe9Sopenharmony_cibool StandbyConfigManager::GetStandbySwitch(const std::string& switchName) 37399552fe9Sopenharmony_ci{ 37499552fe9Sopenharmony_ci return GetConfigWithName(switchName, standbySwitchMap_); 37599552fe9Sopenharmony_ci} 37699552fe9Sopenharmony_ci 37799552fe9Sopenharmony_ciint32_t StandbyConfigManager::GetStandbyParam(const std::string& paramName) 37899552fe9Sopenharmony_ci{ 37999552fe9Sopenharmony_ci return GetConfigWithName(paramName, standbyParaMap_); 38099552fe9Sopenharmony_ci} 38199552fe9Sopenharmony_ci 38299552fe9Sopenharmony_cibool StandbyConfigManager::GetStrategySwitch(const std::string& switchName) 38399552fe9Sopenharmony_ci{ 38499552fe9Sopenharmony_ci return GetConfigWithName(switchName, strategySwitchMap_); 38599552fe9Sopenharmony_ci} 38699552fe9Sopenharmony_ci 38799552fe9Sopenharmony_cibool StandbyConfigManager::GetHalfHourSwitch(const std::string& switchName) 38899552fe9Sopenharmony_ci{ 38999552fe9Sopenharmony_ci return GetConfigWithName(switchName, halfhourSwitchMap_); 39099552fe9Sopenharmony_ci} 39199552fe9Sopenharmony_ci 39299552fe9Sopenharmony_cistd::shared_ptr<std::vector<DefaultResourceConfig>> StandbyConfigManager::GetResCtrlConfig(const 39399552fe9Sopenharmony_ci std::string& switchName) 39499552fe9Sopenharmony_ci{ 39599552fe9Sopenharmony_ci return GetConfigWithName(switchName, defaultResourceConfigMap_); 39699552fe9Sopenharmony_ci} 39799552fe9Sopenharmony_ci 39899552fe9Sopenharmony_citemplate<typename T> 39999552fe9Sopenharmony_ciT StandbyConfigManager::GetConfigWithName(const std::string& switchName, 40099552fe9Sopenharmony_ci std::unordered_map<std::string, T>& configMap) 40199552fe9Sopenharmony_ci{ 40299552fe9Sopenharmony_ci std::lock_guard<std::mutex> lock(configMutex_); 40399552fe9Sopenharmony_ci auto iter = configMap.find(switchName); 40499552fe9Sopenharmony_ci if (iter == configMap.end()) { 40599552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("failed to find config %{public}s", switchName.c_str()); 40699552fe9Sopenharmony_ci return T{}; 40799552fe9Sopenharmony_ci } 40899552fe9Sopenharmony_ci return iter->second; 40999552fe9Sopenharmony_ci} 41099552fe9Sopenharmony_ci 41199552fe9Sopenharmony_ciconst std::vector<TimerResourceConfig>& StandbyConfigManager::GetTimerResConfig() 41299552fe9Sopenharmony_ci{ 41399552fe9Sopenharmony_ci return timerResConfigList_; 41499552fe9Sopenharmony_ci} 41599552fe9Sopenharmony_ci 41699552fe9Sopenharmony_cibool StandbyConfigManager::GetStrategyConfigList(const std::string& switchName) 41799552fe9Sopenharmony_ci{ 41899552fe9Sopenharmony_ci return GetConfigWithName(switchName, strategyListMap_); 41999552fe9Sopenharmony_ci} 42099552fe9Sopenharmony_ci 42199552fe9Sopenharmony_ciconst std::vector<std::string>& StandbyConfigManager::GetStrategyConfigList() 42299552fe9Sopenharmony_ci{ 42399552fe9Sopenharmony_ci return strategyList_; 42499552fe9Sopenharmony_ci} 42599552fe9Sopenharmony_ci 42699552fe9Sopenharmony_cistd::vector<int32_t> StandbyConfigManager::GetStandbyDurationList(const std::string& switchName) 42799552fe9Sopenharmony_ci{ 42899552fe9Sopenharmony_ci return GetConfigWithName(switchName, intervalListMap_); 42999552fe9Sopenharmony_ci} 43099552fe9Sopenharmony_ci 43199552fe9Sopenharmony_ciint32_t StandbyConfigManager::GetMaxDuration(const std::string& name, const std::string& paramName, 43299552fe9Sopenharmony_ci uint32_t condition, bool isApp) 43399552fe9Sopenharmony_ci{ 43499552fe9Sopenharmony_ci auto eligibleAllowTimeList = GetEligibleAllowTimeConfig(paramName, condition, true, isApp); 43599552fe9Sopenharmony_ci auto findConfigTask = [&name](const auto& it) { return it.name_ == name; }; 43699552fe9Sopenharmony_ci auto it = std::find_if(eligibleAllowTimeList.begin(), eligibleAllowTimeList.end(), findConfigTask); 43799552fe9Sopenharmony_ci if (it == eligibleAllowTimeList.end()) { 43899552fe9Sopenharmony_ci return 0; 43999552fe9Sopenharmony_ci } else { 44099552fe9Sopenharmony_ci return it->maxDurationLim_; 44199552fe9Sopenharmony_ci } 44299552fe9Sopenharmony_ci} 44399552fe9Sopenharmony_ci 44499552fe9Sopenharmony_cistd::vector<int32_t> StandbyConfigManager::GetStandbyLadderBatteryList(const std::string& switchName) 44599552fe9Sopenharmony_ci{ 44699552fe9Sopenharmony_ci return GetConfigWithName(switchName, ladderBatteryListMap_); 44799552fe9Sopenharmony_ci} 44899552fe9Sopenharmony_ci 44999552fe9Sopenharmony_citemplate<typename T> std::set<T> StandbyConfigManager::GetEligibleAllowConfig(const std::string& paramName, 45099552fe9Sopenharmony_ci uint32_t condition, bool isAllow, bool isApp, const std::function<void(bool, std::set<T>&, 45199552fe9Sopenharmony_ci const DefaultResourceConfig&)>& func) 45299552fe9Sopenharmony_ci{ 45399552fe9Sopenharmony_ci if (defaultResourceConfigMap_.find(paramName) == defaultResourceConfigMap_.end()) { 45499552fe9Sopenharmony_ci return {}; 45599552fe9Sopenharmony_ci } 45699552fe9Sopenharmony_ci std::set<T> eligibleResCtrlConfig; 45799552fe9Sopenharmony_ci const auto& resCtrlConfig = *(defaultResourceConfigMap_.find(paramName)->second); 45899552fe9Sopenharmony_ci STANDBYSERVICE_LOGD("find duration from %{public}s, size is %{public}d", 45999552fe9Sopenharmony_ci paramName.c_str(), static_cast<int32_t>(resCtrlConfig.size())); 46099552fe9Sopenharmony_ci for (const auto& config : resCtrlConfig) { 46199552fe9Sopenharmony_ci if (config.isAllow_ != isAllow) { 46299552fe9Sopenharmony_ci continue; 46399552fe9Sopenharmony_ci } 46499552fe9Sopenharmony_ci bool isEligiable {false}; 46599552fe9Sopenharmony_ci for (const auto configCondition : config.conditions_) { 46699552fe9Sopenharmony_ci if ((condition & configCondition) == configCondition) { 46799552fe9Sopenharmony_ci isEligiable = true; 46899552fe9Sopenharmony_ci break; 46999552fe9Sopenharmony_ci } 47099552fe9Sopenharmony_ci } 47199552fe9Sopenharmony_ci if (!isEligiable) { 47299552fe9Sopenharmony_ci continue; 47399552fe9Sopenharmony_ci } 47499552fe9Sopenharmony_ci func(isApp, eligibleResCtrlConfig, config); 47599552fe9Sopenharmony_ci } 47699552fe9Sopenharmony_ci STANDBYSERVICE_LOGD("eligibleResCtrlConfig size is %{public}d", 47799552fe9Sopenharmony_ci static_cast<int32_t>(eligibleResCtrlConfig.size())); 47899552fe9Sopenharmony_ci return eligibleResCtrlConfig; 47999552fe9Sopenharmony_ci} 48099552fe9Sopenharmony_ci 48199552fe9Sopenharmony_cistd::set<TimeLtdProcess> StandbyConfigManager::GetEligibleAllowTimeConfig(const std::string& paramName, 48299552fe9Sopenharmony_ci uint32_t condition, bool isAllow, bool isApp) 48399552fe9Sopenharmony_ci{ 48499552fe9Sopenharmony_ci auto func = [](bool isApp, std::set<TimeLtdProcess>& eligibleResCtrlConfig, 48599552fe9Sopenharmony_ci const DefaultResourceConfig& config) { 48699552fe9Sopenharmony_ci if (isApp) { 48799552fe9Sopenharmony_ci eligibleResCtrlConfig.insert(config.timeLtdApps_.begin(), config.timeLtdApps_.end()); 48899552fe9Sopenharmony_ci } else { 48999552fe9Sopenharmony_ci eligibleResCtrlConfig.insert(config.timeLtdProcesses_.begin(), config.timeLtdProcesses_.end()); 49099552fe9Sopenharmony_ci } 49199552fe9Sopenharmony_ci STANDBYSERVICE_LOGD("after calculate, eligible size is %{public}d", 49299552fe9Sopenharmony_ci static_cast<int32_t>(eligibleResCtrlConfig.size())); 49399552fe9Sopenharmony_ci }; 49499552fe9Sopenharmony_ci return GetEligibleAllowConfig<TimeLtdProcess>(paramName, condition, isAllow, isApp, func); 49599552fe9Sopenharmony_ci} 49699552fe9Sopenharmony_ci 49799552fe9Sopenharmony_cistd::set<std::string> StandbyConfigManager::GetEligiblePersistAllowConfig(const std::string& paramName, 49899552fe9Sopenharmony_ci uint32_t condition, bool isAllow, bool isApp) 49999552fe9Sopenharmony_ci{ 50099552fe9Sopenharmony_ci auto func = [](bool isApp, std::set<std::string>& eligibleResCtrlConfig, 50199552fe9Sopenharmony_ci const DefaultResourceConfig& config) { 50299552fe9Sopenharmony_ci if (isApp) { 50399552fe9Sopenharmony_ci eligibleResCtrlConfig.insert(config.apps_.begin(), config.apps_.end()); 50499552fe9Sopenharmony_ci } else { 50599552fe9Sopenharmony_ci eligibleResCtrlConfig.insert(config.processes_.begin(), config.processes_.end()); 50699552fe9Sopenharmony_ci } 50799552fe9Sopenharmony_ci }; 50899552fe9Sopenharmony_ci return GetEligibleAllowConfig<std::string>(paramName, condition, isAllow, isApp, func); 50999552fe9Sopenharmony_ci} 51099552fe9Sopenharmony_ci 51199552fe9Sopenharmony_cibool StandbyConfigManager::ParseDeviceStanbyConfig(const nlohmann::json& devStandbyConfigRoot) 51299552fe9Sopenharmony_ci{ 51399552fe9Sopenharmony_ci nlohmann::json standbyConfig; 51499552fe9Sopenharmony_ci nlohmann::json detectlist; 51599552fe9Sopenharmony_ci nlohmann::json standbySwitchConfig; 51699552fe9Sopenharmony_ci nlohmann::json standbyListConfig; 51799552fe9Sopenharmony_ci nlohmann::json standbyIntervalList; 51899552fe9Sopenharmony_ci nlohmann::json standbyBatteryList; 51999552fe9Sopenharmony_ci 52099552fe9Sopenharmony_ci JsonUtils::GetStringFromJsonValue(devStandbyConfigRoot, TAG_PLUGIN_NAME, pluginName_); 52199552fe9Sopenharmony_ci if (JsonUtils::GetObjFromJsonValue(devStandbyConfigRoot, TAG_STANDBY, standbyConfig) && 52299552fe9Sopenharmony_ci !ParseStandbyConfig(standbyConfig)) { 52399552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("failed to parse standby config in %{public}s", STANDBY_CONFIG_PATH.c_str()); 52499552fe9Sopenharmony_ci return false; 52599552fe9Sopenharmony_ci } 52699552fe9Sopenharmony_ci if (JsonUtils::GetObjFromJsonValue(devStandbyConfigRoot, TAG_DETECT_LIST, detectlist) && 52799552fe9Sopenharmony_ci !ParseStandbyConfig(detectlist)) { 52899552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("failed to parse detect list in %{public}s", STANDBY_CONFIG_PATH.c_str()); 52999552fe9Sopenharmony_ci return false; 53099552fe9Sopenharmony_ci } 53199552fe9Sopenharmony_ci if (JsonUtils::GetObjFromJsonValue(devStandbyConfigRoot, TAG_MAINTENANCE_LIST, standbyIntervalList) && 53299552fe9Sopenharmony_ci !ParseIntervalList(standbyIntervalList)) { 53399552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("failed to parse standby interval list in %{public}s", STANDBY_CONFIG_PATH.c_str()); 53499552fe9Sopenharmony_ci return false; 53599552fe9Sopenharmony_ci } 53699552fe9Sopenharmony_ci if (JsonUtils::GetObjFromJsonValue(devStandbyConfigRoot, TAG_STRATEGY_LIST, standbyListConfig) && 53799552fe9Sopenharmony_ci !ParseStrategyListConfig(standbyListConfig)) { 53899552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("failed to parse strategy list config in %{public}s", STANDBY_CONFIG_PATH.c_str()); 53999552fe9Sopenharmony_ci return false; 54099552fe9Sopenharmony_ci } 54199552fe9Sopenharmony_ci 54299552fe9Sopenharmony_ci if (JsonUtils::GetObjFromJsonValue(devStandbyConfigRoot, TAG_HALFHOUR_SWITCH_SETTING, standbyConfig)) { 54399552fe9Sopenharmony_ci if (!ParseHalfHourSwitchConfig(standbyConfig)) { 54499552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("failed to parse halfhour config"); 54599552fe9Sopenharmony_ci return false; 54699552fe9Sopenharmony_ci } 54799552fe9Sopenharmony_ci } 54899552fe9Sopenharmony_ci 54999552fe9Sopenharmony_ci if (JsonUtils::GetObjFromJsonValue(devStandbyConfigRoot, TAG_LADDER_BATTERY_LIST, standbyBatteryList) && 55099552fe9Sopenharmony_ci !ParseBatteryList(standbyBatteryList)) { 55199552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("failed to parse standby battery list in %{public}s", STANDBY_CONFIG_PATH.c_str()); 55299552fe9Sopenharmony_ci return false; 55399552fe9Sopenharmony_ci } 55499552fe9Sopenharmony_ci return true; 55599552fe9Sopenharmony_ci} 55699552fe9Sopenharmony_ci 55799552fe9Sopenharmony_cibool StandbyConfigManager::ParseStandbyConfig(const nlohmann::json& standbyConfig) 55899552fe9Sopenharmony_ci{ 55999552fe9Sopenharmony_ci bool ret = true; 56099552fe9Sopenharmony_ci for (const auto& element : standbyConfig.items()) { 56199552fe9Sopenharmony_ci if (!element.value().is_primitive()) { 56299552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("there is unexpected type of key in standby config %{public}s", element.key().c_str()); 56399552fe9Sopenharmony_ci ret = false; 56499552fe9Sopenharmony_ci continue; 56599552fe9Sopenharmony_ci } 56699552fe9Sopenharmony_ci if (element.value().is_boolean()) { 56799552fe9Sopenharmony_ci standbySwitchMap_[element.key()] = element.value().get<bool>(); 56899552fe9Sopenharmony_ci } else if (element.value().is_number_integer()) { 56999552fe9Sopenharmony_ci if (element.value().get<int32_t>() < 0) { 57099552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("there is negative value in standby config %{public}s", element.key().c_str()); 57199552fe9Sopenharmony_ci ret = false; 57299552fe9Sopenharmony_ci continue; 57399552fe9Sopenharmony_ci } 57499552fe9Sopenharmony_ci standbyParaMap_[element.key()] = element.value().get<int32_t>(); 57599552fe9Sopenharmony_ci } 57699552fe9Sopenharmony_ci } 57799552fe9Sopenharmony_ci return ret; 57899552fe9Sopenharmony_ci} 57999552fe9Sopenharmony_ci 58099552fe9Sopenharmony_cibool StandbyConfigManager::ParseIntervalList(const nlohmann::json& standbyIntervalList) 58199552fe9Sopenharmony_ci{ 58299552fe9Sopenharmony_ci bool ret = true; 58399552fe9Sopenharmony_ci for (const auto& element : standbyIntervalList.items()) { 58499552fe9Sopenharmony_ci if (!element.value().is_array()) { 58599552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("there is unexpected value of %{public}s in standby interval list", 58699552fe9Sopenharmony_ci element.key().c_str()); 58799552fe9Sopenharmony_ci ret = false; 58899552fe9Sopenharmony_ci continue; 58999552fe9Sopenharmony_ci } 59099552fe9Sopenharmony_ci std::vector<int32_t> intervalList; 59199552fe9Sopenharmony_ci for (const int32_t interval : element.value()) { 59299552fe9Sopenharmony_ci intervalList.emplace_back(interval); 59399552fe9Sopenharmony_ci } 59499552fe9Sopenharmony_ci intervalListMap_.emplace(element.key(), std::move(intervalList)); 59599552fe9Sopenharmony_ci } 59699552fe9Sopenharmony_ci return ret; 59799552fe9Sopenharmony_ci} 59899552fe9Sopenharmony_ci 59999552fe9Sopenharmony_cibool StandbyConfigManager::ParseStrategyListConfig(const nlohmann::json& standbyListConfig) 60099552fe9Sopenharmony_ci{ 60199552fe9Sopenharmony_ci bool ret = true; 60299552fe9Sopenharmony_ci for (const auto& element : standbyListConfig.items()) { 60399552fe9Sopenharmony_ci if (!element.value().is_boolean()) { 60499552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("there is unexpected type of value in half hour standby switch config %{public}s", 60599552fe9Sopenharmony_ci element.key().c_str()); 60699552fe9Sopenharmony_ci ret = false; 60799552fe9Sopenharmony_ci continue; 60899552fe9Sopenharmony_ci } 60999552fe9Sopenharmony_ci strategyListMap_[element.key()] = element.value().get<bool>(); 61099552fe9Sopenharmony_ci } 61199552fe9Sopenharmony_ci return ret; 61299552fe9Sopenharmony_ci} 61399552fe9Sopenharmony_ci 61499552fe9Sopenharmony_civoid StandbyConfigManager::UpdateStrategyList() 61599552fe9Sopenharmony_ci{ 61699552fe9Sopenharmony_ci strategyList_.clear(); 61799552fe9Sopenharmony_ci for (const auto& it : strategyListMap_) { 61899552fe9Sopenharmony_ci if (it.second) { 61999552fe9Sopenharmony_ci strategyList_.emplace_back(it.first); 62099552fe9Sopenharmony_ci } 62199552fe9Sopenharmony_ci } 62299552fe9Sopenharmony_ci if (strategyList_.empty()) { 62399552fe9Sopenharmony_ci STANDBYSERVICE_LOGI("No strategy is set to true."); 62499552fe9Sopenharmony_ci } 62599552fe9Sopenharmony_ci} 62699552fe9Sopenharmony_ci 62799552fe9Sopenharmony_cibool StandbyConfigManager::ParseHalfHourSwitchConfig(const nlohmann::json& halfHourSwitchConfig) 62899552fe9Sopenharmony_ci{ 62999552fe9Sopenharmony_ci bool ret = true; 63099552fe9Sopenharmony_ci for (const auto& element : halfHourSwitchConfig.items()) { 63199552fe9Sopenharmony_ci if (!element.value().is_boolean()) { 63299552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("there is unexpected type of value in half hour standby switch config %{public}s", 63399552fe9Sopenharmony_ci element.key().c_str()); 63499552fe9Sopenharmony_ci ret = false; 63599552fe9Sopenharmony_ci return ret; 63699552fe9Sopenharmony_ci } 63799552fe9Sopenharmony_ci halfhourSwitchMap_[element.key()] = element.value().get<bool>(); 63899552fe9Sopenharmony_ci } 63999552fe9Sopenharmony_ci return ret; 64099552fe9Sopenharmony_ci} 64199552fe9Sopenharmony_ci 64299552fe9Sopenharmony_cibool StandbyConfigManager::ParseResCtrlConfig(const nlohmann::json& resCtrlConfigRoot) 64399552fe9Sopenharmony_ci{ 64499552fe9Sopenharmony_ci bool ret = true; 64599552fe9Sopenharmony_ci for (const auto& element : resCtrlConfigRoot.items()) { 64699552fe9Sopenharmony_ci standbyStrategyConfigMap_[element.key()] = element.value(); 64799552fe9Sopenharmony_ci if (!element.value().is_array()) { 64899552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("there is unexpected type of value in resource control config %{public}s", 64999552fe9Sopenharmony_ci element.key().c_str()); 65099552fe9Sopenharmony_ci ret = false; 65199552fe9Sopenharmony_ci continue; 65299552fe9Sopenharmony_ci } 65399552fe9Sopenharmony_ci std::string resCtrlKey = element.key(); 65499552fe9Sopenharmony_ci if (!ParseDefaultResCtrlConfig(resCtrlKey, element.value())) { 65599552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("there is error in config of %{public}s", resCtrlKey.c_str()); 65699552fe9Sopenharmony_ci ret = false; 65799552fe9Sopenharmony_ci continue; 65899552fe9Sopenharmony_ci } 65999552fe9Sopenharmony_ci // parse exemption config of timer resource 66099552fe9Sopenharmony_ci if (resCtrlKey == TAG_TIMER && !ParseTimerResCtrlConfig(element.value())) { 66199552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("there is error in config of %{public}s", resCtrlKey.c_str()); 66299552fe9Sopenharmony_ci ret = false; 66399552fe9Sopenharmony_ci continue; 66499552fe9Sopenharmony_ci } 66599552fe9Sopenharmony_ci } 66699552fe9Sopenharmony_ci return ret; 66799552fe9Sopenharmony_ci} 66899552fe9Sopenharmony_ci 66999552fe9Sopenharmony_cibool StandbyConfigManager::ParseTimerResCtrlConfig(const nlohmann::json& resConfigArray) 67099552fe9Sopenharmony_ci{ 67199552fe9Sopenharmony_ci if (!resConfigArray.is_array()) { 67299552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("the value of timer config should be an array"); 67399552fe9Sopenharmony_ci return false; 67499552fe9Sopenharmony_ci } 67599552fe9Sopenharmony_ci timerResConfigList_.clear(); 67699552fe9Sopenharmony_ci for (const auto &singleConfigItem : resConfigArray) { 67799552fe9Sopenharmony_ci TimerResourceConfig timerResourceConfig; 67899552fe9Sopenharmony_ci if (!singleConfigItem.contains(TAG_TIME_CLOCK_APPS) || !singleConfigItem.at(TAG_TIME_CLOCK_APPS).is_array()) { 67999552fe9Sopenharmony_ci timerResConfigList_.emplace_back(std::move(timerResourceConfig)); 68099552fe9Sopenharmony_ci continue; 68199552fe9Sopenharmony_ci } 68299552fe9Sopenharmony_ci const nlohmann::json& limitedAppItems = singleConfigItem.at(TAG_TIME_CLOCK_APPS); 68399552fe9Sopenharmony_ci for (const auto &singleLtdAppItem : limitedAppItems) { 68499552fe9Sopenharmony_ci TimerClockApp timerClockApp; 68599552fe9Sopenharmony_ci if (!JsonUtils::GetStringFromJsonValue(singleLtdAppItem, TAG_NAME, timerClockApp.name_) || 68699552fe9Sopenharmony_ci (!JsonUtils::GetBoolFromJsonValue(singleLtdAppItem, TAG_TIMER_CLOCK, timerClockApp.isTimerClock_) && 68799552fe9Sopenharmony_ci !JsonUtils::GetInt32FromJsonValue(singleLtdAppItem, TAG_TIMER_PERIOD, timerClockApp.timerPeriod_))) { 68899552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("there is error in timer clock config"); 68999552fe9Sopenharmony_ci return false; 69099552fe9Sopenharmony_ci } 69199552fe9Sopenharmony_ci timerResourceConfig.timerClockApps_.emplace_back(std::move(timerClockApp)); 69299552fe9Sopenharmony_ci } 69399552fe9Sopenharmony_ci timerResConfigList_.emplace_back(std::move(timerResourceConfig)); 69499552fe9Sopenharmony_ci } 69599552fe9Sopenharmony_ci return true; 69699552fe9Sopenharmony_ci} 69799552fe9Sopenharmony_ci 69899552fe9Sopenharmony_cibool StandbyConfigManager::ParseDefaultResCtrlConfig(const std::string& resCtrlKey, 69999552fe9Sopenharmony_ci const nlohmann::json& resConfigArray) 70099552fe9Sopenharmony_ci{ 70199552fe9Sopenharmony_ci if (!resConfigArray.is_array()) { 70299552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("the value of %{public}s should be an array", resCtrlKey.c_str()); 70399552fe9Sopenharmony_ci return false; 70499552fe9Sopenharmony_ci } 70599552fe9Sopenharmony_ci auto defaultResConfigPtr = std::make_shared<std::vector<DefaultResourceConfig>>(); 70699552fe9Sopenharmony_ci for (const auto &singleConfigItem : resConfigArray) { 70799552fe9Sopenharmony_ci DefaultResourceConfig defaultResourceConfig; 70899552fe9Sopenharmony_ci if (!ParseCommonResCtrlConfig(singleConfigItem, defaultResourceConfig)) { 70999552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("the value of %{public}s can not be parsed", resCtrlKey.c_str()); 71099552fe9Sopenharmony_ci return false; 71199552fe9Sopenharmony_ci } 71299552fe9Sopenharmony_ci defaultResConfigPtr->emplace_back(std::move(defaultResourceConfig)); 71399552fe9Sopenharmony_ci } 71499552fe9Sopenharmony_ci defaultResourceConfigMap_[resCtrlKey] = defaultResConfigPtr; 71599552fe9Sopenharmony_ci STANDBYSERVICE_LOGI("succeed to parse the config of %{public}s", resCtrlKey.c_str()); 71699552fe9Sopenharmony_ci return true; 71799552fe9Sopenharmony_ci} 71899552fe9Sopenharmony_ci 71999552fe9Sopenharmony_cibool StandbyConfigManager::ParseCommonResCtrlConfig(const nlohmann::json& singleConfigItem, 72099552fe9Sopenharmony_ci DefaultResourceConfig& resCtrlConfig) 72199552fe9Sopenharmony_ci{ 72299552fe9Sopenharmony_ci if (!singleConfigItem.contains(TAG_ACTION) || !singleConfigItem.contains(TAG_CONDITION)) { 72399552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("there is no necessary field %{public}s or %{public}s", 72499552fe9Sopenharmony_ci TAG_ACTION.c_str(), TAG_CONDITION.c_str()); 72599552fe9Sopenharmony_ci return false; 72699552fe9Sopenharmony_ci } 72799552fe9Sopenharmony_ci std::string resCtrlAction; 72899552fe9Sopenharmony_ci std::vector<std::string> conditionItemArray {}; 72999552fe9Sopenharmony_ci if (!JsonUtils::GetStringFromJsonValue(singleConfigItem, TAG_ACTION, resCtrlAction) || 73099552fe9Sopenharmony_ci !JsonUtils::GetStrArrFromJsonValue(singleConfigItem, TAG_CONDITION, conditionItemArray)) { 73199552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("get necessary field %{public}s or %{public}s config failed", 73299552fe9Sopenharmony_ci TAG_ACTION.c_str(), TAG_CONDITION.c_str()); 73399552fe9Sopenharmony_ci return false; 73499552fe9Sopenharmony_ci } 73599552fe9Sopenharmony_ci resCtrlConfig.isAllow_ = resCtrlAction == TAG_ALLOW; 73699552fe9Sopenharmony_ci 73799552fe9Sopenharmony_ci for (const auto &singleConditionItem : conditionItemArray) { 73899552fe9Sopenharmony_ci uint32_t conditionValue = ParseCondition(singleConditionItem); 73999552fe9Sopenharmony_ci if (conditionValue > 0) { 74099552fe9Sopenharmony_ci resCtrlConfig.conditions_.emplace_back(conditionValue); 74199552fe9Sopenharmony_ci } 74299552fe9Sopenharmony_ci } 74399552fe9Sopenharmony_ci 74499552fe9Sopenharmony_ci JsonUtils::GetStrArrFromJsonValue(singleConfigItem, TAG_PROCESSES, resCtrlConfig.processes_); 74599552fe9Sopenharmony_ci JsonUtils::GetStrArrFromJsonValue(singleConfigItem, TAG_APPS, resCtrlConfig.apps_); 74699552fe9Sopenharmony_ci ParseTimeLimitedConfig(singleConfigItem, TAG_PROCESSES_LIMIT, resCtrlConfig.timeLtdProcesses_); 74799552fe9Sopenharmony_ci ParseTimeLimitedConfig(singleConfigItem, TAG_APPS_LIMIT, resCtrlConfig.timeLtdApps_); 74899552fe9Sopenharmony_ci return true; 74999552fe9Sopenharmony_ci} 75099552fe9Sopenharmony_ci 75199552fe9Sopenharmony_civoid StandbyConfigManager::ParseTimeLimitedConfig(const nlohmann::json& singleConfigItem, 75299552fe9Sopenharmony_ci const std::string& key, std::vector<TimeLtdProcess>& timeLimitedConfig) 75399552fe9Sopenharmony_ci{ 75499552fe9Sopenharmony_ci nlohmann::json timeLimitedItems; 75599552fe9Sopenharmony_ci if (!JsonUtils::GetArrayFromJsonValue(singleConfigItem, key, timeLimitedItems)) { 75699552fe9Sopenharmony_ci return; 75799552fe9Sopenharmony_ci } 75899552fe9Sopenharmony_ci for (const auto &singleLtdItem : timeLimitedItems) { 75999552fe9Sopenharmony_ci std::string name {}; 76099552fe9Sopenharmony_ci int32_t duration {0}; 76199552fe9Sopenharmony_ci if (!JsonUtils::GetStringFromJsonValue(singleLtdItem, TAG_NAME, name) || 76299552fe9Sopenharmony_ci !JsonUtils::GetInt32FromJsonValue(singleLtdItem, TAG_MAX_DURATION_LIM, duration)) { 76399552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("there is error in %{public}s config", key.c_str()); 76499552fe9Sopenharmony_ci continue; 76599552fe9Sopenharmony_ci } 76699552fe9Sopenharmony_ci timeLimitedConfig.emplace_back(TimeLtdProcess{name, duration}); 76799552fe9Sopenharmony_ci } 76899552fe9Sopenharmony_ci} 76999552fe9Sopenharmony_ci 77099552fe9Sopenharmony_ciuint32_t StandbyConfigManager::ParseCondition(const std::string& conditionStr) 77199552fe9Sopenharmony_ci{ 77299552fe9Sopenharmony_ci uint32_t conditionValue = 0; 77399552fe9Sopenharmony_ci std::stringstream ss(conditionStr); 77499552fe9Sopenharmony_ci std::string conditionSubstr; 77599552fe9Sopenharmony_ci while (std::getline(ss, conditionSubstr, TAG_CONDITION_DELIM)) { 77699552fe9Sopenharmony_ci auto iter = conditionMap.find(conditionSubstr); 77799552fe9Sopenharmony_ci if (iter == conditionMap.end()) { 77899552fe9Sopenharmony_ci continue; 77999552fe9Sopenharmony_ci } 78099552fe9Sopenharmony_ci conditionValue |= iter->second; 78199552fe9Sopenharmony_ci } 78299552fe9Sopenharmony_ci return conditionValue; 78399552fe9Sopenharmony_ci} 78499552fe9Sopenharmony_ci 78599552fe9Sopenharmony_cibool StandbyConfigManager::ParseBatteryList(const nlohmann::json& standbyBatteryList) 78699552fe9Sopenharmony_ci{ 78799552fe9Sopenharmony_ci bool ret = true; 78899552fe9Sopenharmony_ci for (const auto& element : standbyBatteryList.items()) { 78999552fe9Sopenharmony_ci if (!element.value().is_array()) { 79099552fe9Sopenharmony_ci STANDBYSERVICE_LOGW("there is unexpected value of %{public}s in standby battery list", 79199552fe9Sopenharmony_ci element.key().c_str()); 79299552fe9Sopenharmony_ci ret = false; 79399552fe9Sopenharmony_ci continue; 79499552fe9Sopenharmony_ci } 79599552fe9Sopenharmony_ci std::vector<int32_t> batterylList; 79699552fe9Sopenharmony_ci for (const int32_t battery : element.value()) { 79799552fe9Sopenharmony_ci batterylList.emplace_back(battery); 79899552fe9Sopenharmony_ci } 79999552fe9Sopenharmony_ci ladderBatteryListMap_.emplace(element.key(), std::move(batterylList)); 80099552fe9Sopenharmony_ci } 80199552fe9Sopenharmony_ci return ret; 80299552fe9Sopenharmony_ci} 80399552fe9Sopenharmony_ci 80499552fe9Sopenharmony_civoid StandbyConfigManager::DumpSetDebugMode(bool debugMode) 80599552fe9Sopenharmony_ci{ 80699552fe9Sopenharmony_ci std::lock_guard<std::mutex> lock(configMutex_); 80799552fe9Sopenharmony_ci if (debugMode) { 80899552fe9Sopenharmony_ci backStandbySwitchMap_ = standbySwitchMap_; 80999552fe9Sopenharmony_ci backStandbyParaMap_ = standbyParaMap_; 81099552fe9Sopenharmony_ci } else { 81199552fe9Sopenharmony_ci standbySwitchMap_ = backStandbySwitchMap_; 81299552fe9Sopenharmony_ci standbyParaMap_ = backStandbyParaMap_; 81399552fe9Sopenharmony_ci backStandbySwitchMap_.clear(); 81499552fe9Sopenharmony_ci backStandbyParaMap_.clear(); 81599552fe9Sopenharmony_ci } 81699552fe9Sopenharmony_ci} 81799552fe9Sopenharmony_ci 81899552fe9Sopenharmony_civoid StandbyConfigManager::DumpSetSwitch(const std::string& switchName, bool switchStatus, std::string& result) 81999552fe9Sopenharmony_ci{ 82099552fe9Sopenharmony_ci std::lock_guard<std::mutex> lock(configMutex_); 82199552fe9Sopenharmony_ci auto iter = standbySwitchMap_.find(switchName); 82299552fe9Sopenharmony_ci if (iter == standbySwitchMap_.end()) { 82399552fe9Sopenharmony_ci result += switchName + " not exist\n"; 82499552fe9Sopenharmony_ci return; 82599552fe9Sopenharmony_ci } 82699552fe9Sopenharmony_ci iter->second = switchStatus; 82799552fe9Sopenharmony_ci} 82899552fe9Sopenharmony_ci 82999552fe9Sopenharmony_civoid StandbyConfigManager::DumpSetParameter(const std::string& paramName, int32_t paramValue, std::string& result) 83099552fe9Sopenharmony_ci{ 83199552fe9Sopenharmony_ci std::lock_guard<std::mutex> lock(configMutex_); 83299552fe9Sopenharmony_ci auto iter = standbyParaMap_.find(paramName); 83399552fe9Sopenharmony_ci if (iter == standbyParaMap_.end()) { 83499552fe9Sopenharmony_ci result += paramName + " not exist\n"; 83599552fe9Sopenharmony_ci return; 83699552fe9Sopenharmony_ci } 83799552fe9Sopenharmony_ci iter->second = paramValue; 83899552fe9Sopenharmony_ci} 83999552fe9Sopenharmony_ci 84099552fe9Sopenharmony_civoid StandbyConfigManager::DumpStandbyConfigInfo(std::string& result) 84199552fe9Sopenharmony_ci{ 84299552fe9Sopenharmony_ci std::lock_guard<std::mutex> lock(configMutex_); 84399552fe9Sopenharmony_ci std::stringstream stream; 84499552fe9Sopenharmony_ci for (const auto& [switchName, switchVal] : standbySwitchMap_) { 84599552fe9Sopenharmony_ci stream << switchName << ": " << (switchVal ? "true" : "false") << "\n"; 84699552fe9Sopenharmony_ci } 84799552fe9Sopenharmony_ci for (const auto& [paraName, paraVal] : standbyParaMap_) { 84899552fe9Sopenharmony_ci stream << paraName << ": " << paraVal << "\n"; 84999552fe9Sopenharmony_ci } 85099552fe9Sopenharmony_ci for (const auto& [strategyName, strategyVal] : strategySwitchMap_) { 85199552fe9Sopenharmony_ci stream << strategyName << ": " << (strategyVal ? "true" : "false") << "\n"; 85299552fe9Sopenharmony_ci } 85399552fe9Sopenharmony_ci for (const auto& [strategyListName, strategyListVal] : strategyListMap_) { 85499552fe9Sopenharmony_ci stream << strategyListName << ": " << (strategyListVal ? "true" : "false") << "\n"; 85599552fe9Sopenharmony_ci } 85699552fe9Sopenharmony_ci stream << "\n"; 85799552fe9Sopenharmony_ci auto printConditions = [&stream](const int32_t& condition) { stream << "\t\t" << condition << " "; }; 85899552fe9Sopenharmony_ci auto printProceses = [&stream](const std::string& process) { stream << "\t\t" << process << "\n"; }; 85999552fe9Sopenharmony_ci auto printLtdProceses = [&stream](const TimeLtdProcess& timeLtdProcess) { 86099552fe9Sopenharmony_ci stream << "\t\t" << timeLtdProcess.name_ << " " << timeLtdProcess.maxDurationLim_ << "\n"; 86199552fe9Sopenharmony_ci }; 86299552fe9Sopenharmony_ci for (const auto& [resCtrlKey, resConfigVec] : defaultResourceConfigMap_) { 86399552fe9Sopenharmony_ci for (const auto& resConfig : *resConfigVec) { 86499552fe9Sopenharmony_ci stream << resCtrlKey << ": \n"; 86599552fe9Sopenharmony_ci stream << "\tisAllow: " << resConfig.isAllow_ << "\n"; 86699552fe9Sopenharmony_ci DumpResCtrlConfig<uint32_t>("conditions", resConfig.conditions_, stream, printConditions); 86799552fe9Sopenharmony_ci stream << "\n"; 86899552fe9Sopenharmony_ci DumpResCtrlConfig<std::string>("processes", resConfig.processes_, stream, printProceses); 86999552fe9Sopenharmony_ci DumpResCtrlConfig<std::string>("apps", resConfig.apps_, stream, printProceses); 87099552fe9Sopenharmony_ci DumpResCtrlConfig<TimeLtdProcess>("timeLtdProcesses", resConfig.timeLtdProcesses_, 87199552fe9Sopenharmony_ci stream, printLtdProceses); 87299552fe9Sopenharmony_ci DumpResCtrlConfig<TimeLtdProcess>("timeLtdApps", resConfig.timeLtdApps_, stream, printLtdProceses); 87399552fe9Sopenharmony_ci } 87499552fe9Sopenharmony_ci } 87599552fe9Sopenharmony_ci result += stream.str(); 87699552fe9Sopenharmony_ci stream.str(""); 87799552fe9Sopenharmony_ci stream.clear(); 87899552fe9Sopenharmony_ci} 87999552fe9Sopenharmony_ci 88099552fe9Sopenharmony_citemplate<typename T> void StandbyConfigManager::DumpResCtrlConfig(const char* name, const std::vector<T>& configArray, 88199552fe9Sopenharmony_ci std::stringstream& stream, const std::function<void(const T&)>& func) 88299552fe9Sopenharmony_ci{ 88399552fe9Sopenharmony_ci if (configArray.empty()) { 88499552fe9Sopenharmony_ci return; 88599552fe9Sopenharmony_ci } 88699552fe9Sopenharmony_ci stream << "\t" << name << ":\n"; 88799552fe9Sopenharmony_ci for_each(configArray.begin(), configArray.end(), func); 88899552fe9Sopenharmony_ci} 88999552fe9Sopenharmony_ci} // namespace DevStandbyMgr 89099552fe9Sopenharmony_ci} // namespace OHOS