1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "standby_config_manager.h"
17
18 #include <string>
19 #include <sstream>
20 #include <functional>
21 #include <unistd.h>
22
23 #ifdef STANDBY_CONFIG_POLICY_ENABLE
24 #include "config_policy_utils.h"
25 #endif
26
27 #include "json_utils.h"
28
29 namespace OHOS {
30 namespace DevStandbyMgr {
31 namespace {
32 const std::string DEFAULT_CONFIG_ROOT_DIR = "/system";
33 const std::string STANDBY_CONFIG_PATH = "/etc/standby_service/device_standby_config.json";
34 const std::string STRATEGY_CONFIG_PATH = "/etc/standby_service/standby_strategy_config.json";
35 const std::string TAG_PLUGIN_NAME = "plugin_name";
36 const std::string TAG_STANDBY = "standby";
37 const std::string TAG_MAINTENANCE_LIST = "maintenance_list";
38 const std::string TAG_DETECT_LIST = "detect_list";
39 const std::string TAG_STRATEGY_LIST = "strategy_list";
40 const std::string TAG_HALFHOUR_SWITCH_SETTING = "halfhour_switch_setting";
41
42 const std::string TAG_CONDITION = "condition";
43 const std::string TAG_ACTION = "action";
44 const std::string TAG_ALLOW = "allow";
45 const std::string TAG_PROCESSES = "processes";
46 const std::string TAG_APPS = "apps";
47 const std::string TAG_PROCESSES_LIMIT = "processes_limit";
48 const std::string TAG_TIME_CLOCK_APPS = "time_clock_apps";
49 const std::string TAG_APPS_LIMIT = "apps_limit";
50 const std::string TAG_NAME = "name";
51 const std::string TAG_MAX_DURATION_LIM = "duration";
52
53 const std::string TAG_TIMER = "TIMER";
54 const std::string TAG_TIMER_CLOCK = "timer_clock";
55 const std::string TAG_TIMER_PERIOD = "timer_period";
56
57 const char TAG_CONDITION_DELIM = '&';
58 const std::string TAG_DAY_STANDBY = "day_standby";
59 const std::string TAG_NIGHT_STANDBY = "night_standby";
60 const std::string TAG_SCREENOFF = "screenoff";
61 const std::string TAG_SCREENOFF_HALFHOUR = "screenoff_halfhour";
62 const std::unordered_map<std::string, ConditionType::Type> conditionMap = {
63 {TAG_DAY_STANDBY, ConditionType::DAY_STANDBY},
64 {TAG_NIGHT_STANDBY, ConditionType::NIGHT_STANDBY},
65 };
66 }
67
68 IMPLEMENT_SINGLE_INSTANCE(StandbyConfigManager);
69
StandbyConfigManager()70 StandbyConfigManager::StandbyConfigManager() {}
71
~StandbyConfigManager()72 StandbyConfigManager::~StandbyConfigManager() {}
73
Init()74 ErrCode StandbyConfigManager::Init()
75 {
76 STANDBYSERVICE_LOGI("start to read config");
77
78 std::vector<std::string> configFileList = GetConfigFileList(STANDBY_CONFIG_PATH);
79 for (const auto& configFile : configFileList) {
80 nlohmann::json devStandbyConfigRoot;
81 // if failed to load one json file, read next config file
82 if (!JsonUtils::LoadJsonValueFromFile(devStandbyConfigRoot, configFile)) {
83 STANDBYSERVICE_LOGE("load config file %{public}s failed", configFile.c_str());
84 continue;
85 }
86 if (!ParseDeviceStanbyConfig(devStandbyConfigRoot)) {
87 STANDBYSERVICE_LOGE("parse config file %{public}s failed", configFile.c_str());
88 }
89 }
90
91 configFileList = GetConfigFileList(STRATEGY_CONFIG_PATH);
92 for (const auto& configFile : configFileList) {
93 nlohmann::json resCtrlConfigRoot;
94 if (!JsonUtils::LoadJsonValueFromFile(resCtrlConfigRoot, configFile)) {
95 STANDBYSERVICE_LOGE("load config file %{public}s failed", configFile.c_str());
96 continue;
97 }
98 if (!ParseResCtrlConfig(resCtrlConfigRoot)) {
99 STANDBYSERVICE_LOGE("parse config file %{public}s failed", configFile.c_str());
100 }
101 }
102 return ERR_OK;
103 }
104
GetConfigFileList(const std::string & relativeConfigPath)105 std::vector<std::string> StandbyConfigManager::GetConfigFileList(const std::string& relativeConfigPath)
106 {
107 std::list<std::string> rootDirList;
108 #ifdef STANDBY_CONFIG_POLICY_ENABLE
109 auto cfgDirList = GetCfgDirList();
110 if (cfgDirList != nullptr) {
111 for (const auto &cfgDir : cfgDirList->paths) {
112 if (cfgDir == nullptr) {
113 continue;
114 }
115 STANDBYSERVICE_LOGD("cfgDir: %{public}s ", cfgDir);
116 rootDirList.emplace_back(cfgDir);
117 }
118 FreeCfgDirList(cfgDirList);
119 }
120 #endif
121 if (std::find(rootDirList.begin(), rootDirList.end(), DEFAULT_CONFIG_ROOT_DIR)
122 == rootDirList.end()) {
123 rootDirList.emplace_front(DEFAULT_CONFIG_ROOT_DIR);
124 }
125 std::string baseRealPath;
126 std::vector<std::string> configFilesList;
127 for (auto configDir : rootDirList) {
128 if (JsonUtils::GetRealPath(configDir + relativeConfigPath, baseRealPath)
129 && access(baseRealPath.c_str(), F_OK) == ERR_OK) {
130 STANDBYSERVICE_LOGD("Get valid base config file: %{public}s", baseRealPath.c_str());
131 configFilesList.emplace_back(baseRealPath);
132 }
133 }
134 return configFilesList;
135 }
136
GetPluginName()137 const std::string& StandbyConfigManager::GetPluginName()
138 {
139 return pluginName_;
140 }
141
GetStandbySwitch(const std::string & switchName)142 bool StandbyConfigManager::GetStandbySwitch(const std::string& switchName)
143 {
144 return GetConfigWithName(switchName, standbySwitchMap_);
145 }
146
GetStandbyParam(const std::string & paramName)147 int32_t StandbyConfigManager::GetStandbyParam(const std::string& paramName)
148 {
149 return GetConfigWithName(paramName, standbyParaMap_);
150 }
151
GetStrategySwitch(const std::string & switchName)152 bool StandbyConfigManager::GetStrategySwitch(const std::string& switchName)
153 {
154 return GetConfigWithName(switchName, strategySwitchMap_);
155 }
156
GetHalfHourSwitch(const std::string & switchName)157 bool StandbyConfigManager::GetHalfHourSwitch(const std::string& switchName)
158 {
159 return GetConfigWithName(switchName, halfhourSwitchMap_);
160 }
161
GetResCtrlConfig(const std::string & switchName)162 std::shared_ptr<std::vector<DefaultResourceConfig>> StandbyConfigManager::GetResCtrlConfig(const
163 std::string& switchName)
164 {
165 return GetConfigWithName(switchName, defaultResourceConfigMap_);
166 }
167
168 template<typename T>
GetConfigWithName(const std::string & switchName,std::unordered_map<std::string,T> & configMap)169 T StandbyConfigManager::GetConfigWithName(const std::string& switchName,
170 std::unordered_map<std::string, T>& configMap)
171 {
172 std::lock_guard<std::mutex> lock(configMutex_);
173 auto iter = configMap.find(switchName);
174 if (iter == configMap.end()) {
175 STANDBYSERVICE_LOGW("failed to find config %{public}s", switchName.c_str());
176 return T{};
177 }
178 return iter->second;
179 }
180
GetTimerResConfig()181 const std::vector<TimerResourceConfig>& StandbyConfigManager::GetTimerResConfig()
182 {
183 return timerResConfigList_;
184 }
185
GetStrategyConfigList()186 const std::vector<std::string>& StandbyConfigManager::GetStrategyConfigList()
187 {
188 return strategyList_;
189 }
190
GetStandbyDurationList(const std::string & switchName)191 std::vector<int32_t> StandbyConfigManager::GetStandbyDurationList(const std::string& switchName)
192 {
193 return GetConfigWithName(switchName, intervalListMap_);
194 }
195
GetMaxDuration(const std::string & name,const std::string & paramName,uint32_t condition,bool isApp)196 int32_t StandbyConfigManager::GetMaxDuration(const std::string& name, const std::string& paramName,
197 uint32_t condition, bool isApp)
198 {
199 auto eligibleAllowTimeList = GetEligibleAllowTimeConfig(paramName, condition, true, isApp);
200 auto findConfigTask = [&name](const auto& it) { return it.name_ == name; };
201 auto it = std::find_if(eligibleAllowTimeList.begin(), eligibleAllowTimeList.end(), findConfigTask);
202 if (it == eligibleAllowTimeList.end()) {
203 return 0;
204 } else {
205 return it->maxDurationLim_;
206 }
207 }
208
GetEligibleAllowConfig(const std::string & paramName,uint32_t condition,bool isAllow,bool isApp,const std::function<void (bool,std::set<T> &,const DefaultResourceConfig &)> & func)209 template<typename T> std::set<T> StandbyConfigManager::GetEligibleAllowConfig(const std::string& paramName,
210 uint32_t condition, bool isAllow, bool isApp, const std::function<void(bool, std::set<T>&,
211 const DefaultResourceConfig&)>& func)
212 {
213 if (defaultResourceConfigMap_.find(paramName) == defaultResourceConfigMap_.end()) {
214 return {};
215 }
216 std::set<T> eligibleResCtrlConfig;
217 const auto& resCtrlConfig = *(defaultResourceConfigMap_.find(paramName)->second);
218 STANDBYSERVICE_LOGD("find duration from %{public}s, size is %{public}d",
219 paramName.c_str(), static_cast<int32_t>(resCtrlConfig.size()));
220 for (const auto& config : resCtrlConfig) {
221 if (config.isAllow_ != isAllow) {
222 continue;
223 }
224 bool isEligiable {false};
225 for (const auto configCondition : config.conditions_) {
226 if ((condition & configCondition) == configCondition) {
227 isEligiable = true;
228 break;
229 }
230 }
231 if (!isEligiable) {
232 continue;
233 }
234 func(isApp, eligibleResCtrlConfig, config);
235 }
236 STANDBYSERVICE_LOGD("eligibleResCtrlConfig size is %{public}d",
237 static_cast<int32_t>(eligibleResCtrlConfig.size()));
238 return eligibleResCtrlConfig;
239 }
240
GetEligibleAllowTimeConfig(const std::string & paramName,uint32_t condition,bool isAllow,bool isApp)241 std::set<TimeLtdProcess> StandbyConfigManager::GetEligibleAllowTimeConfig(const std::string& paramName,
242 uint32_t condition, bool isAllow, bool isApp)
243 {
244 auto func = [](bool isApp, std::set<TimeLtdProcess>& eligibleResCtrlConfig,
245 const DefaultResourceConfig& config) {
246 if (isApp) {
247 eligibleResCtrlConfig.insert(config.timeLtdApps_.begin(), config.timeLtdApps_.end());
248 } else {
249 eligibleResCtrlConfig.insert(config.timeLtdProcesses_.begin(), config.timeLtdProcesses_.end());
250 }
251 STANDBYSERVICE_LOGD("after calculate, eligible size is %{public}d",
252 static_cast<int32_t>(eligibleResCtrlConfig.size()));
253 };
254 return GetEligibleAllowConfig<TimeLtdProcess>(paramName, condition, isAllow, isApp, func);
255 }
256
GetEligiblePersistAllowConfig(const std::string & paramName,uint32_t condition,bool isAllow,bool isApp)257 std::set<std::string> StandbyConfigManager::GetEligiblePersistAllowConfig(const std::string& paramName,
258 uint32_t condition, bool isAllow, bool isApp)
259 {
260 auto func = [](bool isApp, std::set<std::string>& eligibleResCtrlConfig,
261 const DefaultResourceConfig& config) {
262 if (isApp) {
263 eligibleResCtrlConfig.insert(config.apps_.begin(), config.apps_.end());
264 } else {
265 eligibleResCtrlConfig.insert(config.processes_.begin(), config.processes_.end());
266 }
267 };
268 return GetEligibleAllowConfig<std::string>(paramName, condition, isAllow, isApp, func);
269 }
270
ParseDeviceStanbyConfig(const nlohmann::json & devStandbyConfigRoot)271 bool StandbyConfigManager::ParseDeviceStanbyConfig(const nlohmann::json& devStandbyConfigRoot)
272 {
273 nlohmann::json standbyConfig;
274 nlohmann::json detectlist;
275 nlohmann::json standbySwitchConfig;
276 nlohmann::json standbyListConfig;
277 nlohmann::json standbyIntervalList;
278
279 JsonUtils::GetStringFromJsonValue(devStandbyConfigRoot, TAG_PLUGIN_NAME, pluginName_);
280 if (JsonUtils::GetObjFromJsonValue(devStandbyConfigRoot, TAG_STANDBY, standbyConfig) &&
281 !ParseStandbyConfig(standbyConfig)) {
282 STANDBYSERVICE_LOGW("failed to parse standby config in %{public}s", STANDBY_CONFIG_PATH.c_str());
283 return false;
284 }
285 if (JsonUtils::GetObjFromJsonValue(devStandbyConfigRoot, TAG_DETECT_LIST, detectlist) &&
286 !ParseStandbyConfig(detectlist)) {
287 STANDBYSERVICE_LOGW("failed to parse detect list in %{public}s", STANDBY_CONFIG_PATH.c_str());
288 return false;
289 }
290 if (JsonUtils::GetObjFromJsonValue(devStandbyConfigRoot, TAG_MAINTENANCE_LIST, standbyIntervalList) &&
291 !ParseIntervalList(standbyIntervalList)) {
292 STANDBYSERVICE_LOGW("failed to parse standby interval list in %{public}s", STANDBY_CONFIG_PATH.c_str());
293 return false;
294 }
295 if (JsonUtils::GetArrayFromJsonValue(devStandbyConfigRoot, TAG_STRATEGY_LIST, standbyListConfig) &&
296 !ParseStrategyListConfig(standbyListConfig)) {
297 STANDBYSERVICE_LOGW("failed to parse strategy list config in %{public}s", STANDBY_CONFIG_PATH.c_str());
298 return false;
299 }
300
301 if (JsonUtils::GetObjFromJsonValue(devStandbyConfigRoot, TAG_HALFHOUR_SWITCH_SETTING, standbyConfig)) {
302 if (!ParseHalfHourSwitchConfig(standbyConfig)) {
303 STANDBYSERVICE_LOGW("failed to parse halfhour config");
304 return false;
305 }
306 }
307 return true;
308 }
309
ParseStandbyConfig(const nlohmann::json & standbyConfig)310 bool StandbyConfigManager::ParseStandbyConfig(const nlohmann::json& standbyConfig)
311 {
312 bool ret = true;
313 for (const auto& element : standbyConfig.items()) {
314 if (!element.value().is_primitive()) {
315 STANDBYSERVICE_LOGW("there is unexpected type of key in standby config %{public}s", element.key().c_str());
316 ret = false;
317 continue;
318 }
319 if (element.value().is_boolean()) {
320 standbySwitchMap_[element.key()] = element.value().get<bool>();
321 } else if (element.value().is_number_integer()) {
322 if (element.value().get<int32_t>() < 0) {
323 STANDBYSERVICE_LOGW("there is negative value in standby config %{public}s", element.key().c_str());
324 ret = false;
325 continue;
326 }
327 standbyParaMap_[element.key()] = element.value().get<int32_t>();
328 }
329 }
330 return ret;
331 }
332
ParseIntervalList(const nlohmann::json & standbyIntervalList)333 bool StandbyConfigManager::ParseIntervalList(const nlohmann::json& standbyIntervalList)
334 {
335 bool ret = true;
336 for (const auto& element : standbyIntervalList.items()) {
337 if (!element.value().is_array()) {
338 STANDBYSERVICE_LOGW("there is unexpected value of %{public}s in standby interval list",
339 element.key().c_str());
340 ret = false;
341 continue;
342 }
343 std::vector<int32_t> intervalList;
344 for (const int32_t interval : element.value()) {
345 intervalList.emplace_back(interval);
346 }
347 intervalListMap_.emplace(element.key(), std::move(intervalList));
348 }
349 return ret;
350 }
351
ParseStrategyListConfig(const nlohmann::json & standbyListConfig)352 bool StandbyConfigManager::ParseStrategyListConfig(const nlohmann::json& standbyListConfig)
353 {
354 if (!standbyListConfig.is_array()) {
355 STANDBYSERVICE_LOGW("there is error in strategy list config");
356 return false;
357 }
358 strategyList_.clear();
359 for (const auto& element : standbyListConfig) {
360 strategyList_.emplace_back(element.get<std::string>());
361 }
362 return true;
363 }
364
ParseHalfHourSwitchConfig(const nlohmann::json & halfHourSwitchConfig)365 bool StandbyConfigManager::ParseHalfHourSwitchConfig(const nlohmann::json& halfHourSwitchConfig)
366 {
367 bool ret = true;
368 for (const auto& element : halfHourSwitchConfig.items()) {
369 if (!element.value().is_boolean()) {
370 STANDBYSERVICE_LOGW("there is unexpected type of value in half hour standby switch config %{public}s",
371 element.key().c_str());
372 ret = false;
373 return ret;
374 }
375 halfhourSwitchMap_[element.key()] = element.value().get<bool>();
376 }
377 return ret;
378 }
379
ParseResCtrlConfig(const nlohmann::json & resCtrlConfigRoot)380 bool StandbyConfigManager::ParseResCtrlConfig(const nlohmann::json& resCtrlConfigRoot)
381 {
382 bool ret = true;
383 for (const auto& element : resCtrlConfigRoot.items()) {
384 if (!element.value().is_array()) {
385 STANDBYSERVICE_LOGW("there is unexpected type of value in resource control config %{public}s",
386 element.key().c_str());
387 ret = false;
388 continue;
389 }
390 std::string resCtrlKey = element.key();
391 if (!ParseDefaultResCtrlConfig(resCtrlKey, element.value())) {
392 STANDBYSERVICE_LOGW("there is error in config of %{public}s", resCtrlKey.c_str());
393 ret = false;
394 continue;
395 }
396 // parse exemption config of timer resource
397 if (resCtrlKey == TAG_TIMER && !ParseTimerResCtrlConfig(element.value())) {
398 STANDBYSERVICE_LOGW("there is error in config of %{public}s", resCtrlKey.c_str());
399 ret = false;
400 continue;
401 }
402 }
403 return ret;
404 }
405
ParseTimerResCtrlConfig(const nlohmann::json & resConfigArray)406 bool StandbyConfigManager::ParseTimerResCtrlConfig(const nlohmann::json& resConfigArray)
407 {
408 if (!resConfigArray.is_array()) {
409 STANDBYSERVICE_LOGW("the value of timer config should be an array");
410 return false;
411 }
412 timerResConfigList_.clear();
413 for (const auto &singleConfigItem : resConfigArray) {
414 TimerResourceConfig timerResourceConfig;
415 if (!singleConfigItem.contains(TAG_TIME_CLOCK_APPS)) {
416 timerResConfigList_.emplace_back(std::move(timerResourceConfig));
417 continue;
418 }
419 const nlohmann::json& limitedAppItems = singleConfigItem.at(TAG_TIME_CLOCK_APPS);
420 for (const auto &singleLtdAppItem : limitedAppItems) {
421 TimerClockApp timerClockApp;
422 if (!JsonUtils::GetStringFromJsonValue(singleLtdAppItem, TAG_NAME, timerClockApp.name_) ||
423 (!JsonUtils::GetBoolFromJsonValue(singleLtdAppItem, TAG_TIMER_CLOCK, timerClockApp.isTimerClock_) &&
424 !JsonUtils::GetInt32FromJsonValue(singleLtdAppItem, TAG_TIMER_PERIOD, timerClockApp.timerPeriod_))) {
425 STANDBYSERVICE_LOGW("there is error in timer clock config");
426 return false;
427 }
428 timerResourceConfig.timerClockApps_.emplace_back(std::move(timerClockApp));
429 }
430 timerResConfigList_.emplace_back(std::move(timerResourceConfig));
431 }
432 return true;
433 }
434
ParseDefaultResCtrlConfig(const std::string & resCtrlKey,const nlohmann::json & resConfigArray)435 bool StandbyConfigManager::ParseDefaultResCtrlConfig(const std::string& resCtrlKey,
436 const nlohmann::json& resConfigArray)
437 {
438 if (!resConfigArray.is_array()) {
439 STANDBYSERVICE_LOGW("the value of %{public}s should be an array", resCtrlKey.c_str());
440 return false;
441 }
442 auto defaultResConfigPtr = std::make_shared<std::vector<DefaultResourceConfig>>();
443 for (const auto &singleConfigItem : resConfigArray) {
444 DefaultResourceConfig defaultResourceConfig;
445 if (!ParseCommonResCtrlConfig(singleConfigItem, defaultResourceConfig)) {
446 STANDBYSERVICE_LOGW("the value of %{public}s can not be parsed", resCtrlKey.c_str());
447 return false;
448 }
449 defaultResConfigPtr->emplace_back(std::move(defaultResourceConfig));
450 }
451 defaultResourceConfigMap_[resCtrlKey] = defaultResConfigPtr;
452 STANDBYSERVICE_LOGI("succeed to parse the config of %{public}s", resCtrlKey.c_str());
453 return true;
454 }
455
ParseCommonResCtrlConfig(const nlohmann::json & singleConfigItem,DefaultResourceConfig & resCtrlConfig)456 bool StandbyConfigManager::ParseCommonResCtrlConfig(const nlohmann::json& singleConfigItem,
457 DefaultResourceConfig& resCtrlConfig)
458 {
459 if (!singleConfigItem.contains(TAG_ACTION) || !singleConfigItem.contains(TAG_CONDITION)) {
460 STANDBYSERVICE_LOGW("there is no necessary field %{public}s or %{public}s",
461 TAG_ACTION.c_str(), TAG_CONDITION.c_str());
462 return false;
463 }
464 std::string resCtrlAction;
465 std::vector<std::string> conditionItemArray {};
466 if (!JsonUtils::GetStringFromJsonValue(singleConfigItem, TAG_ACTION, resCtrlAction) ||
467 !JsonUtils::GetStrArrFromJsonValue(singleConfigItem, TAG_CONDITION, conditionItemArray)) {
468 STANDBYSERVICE_LOGW("get necessary field %{public}s or %{public}s config failed",
469 TAG_ACTION.c_str(), TAG_CONDITION.c_str());
470 return false;
471 }
472 resCtrlConfig.isAllow_ = resCtrlAction == TAG_ALLOW;
473
474 for (const auto &singleConditionItem : conditionItemArray) {
475 uint32_t conditionValue = ParseCondition(singleConditionItem);
476 if (conditionValue > 0) {
477 resCtrlConfig.conditions_.emplace_back(conditionValue);
478 }
479 }
480
481 JsonUtils::GetStrArrFromJsonValue(singleConfigItem, TAG_PROCESSES, resCtrlConfig.processes_);
482 JsonUtils::GetStrArrFromJsonValue(singleConfigItem, TAG_APPS, resCtrlConfig.apps_);
483 ParseTimeLimitedConfig(singleConfigItem, TAG_PROCESSES_LIMIT, resCtrlConfig.timeLtdProcesses_);
484 ParseTimeLimitedConfig(singleConfigItem, TAG_APPS_LIMIT, resCtrlConfig.timeLtdApps_);
485 return true;
486 }
487
ParseTimeLimitedConfig(const nlohmann::json & singleConfigItem,const std::string & key,std::vector<TimeLtdProcess> & timeLimitedConfig)488 void StandbyConfigManager::ParseTimeLimitedConfig(const nlohmann::json& singleConfigItem,
489 const std::string& key, std::vector<TimeLtdProcess>& timeLimitedConfig)
490 {
491 nlohmann::json timeLimitedItems;
492 if (!JsonUtils::GetArrayFromJsonValue(singleConfigItem, key, timeLimitedItems)) {
493 return;
494 }
495 for (const auto &singleLtdItem : timeLimitedItems) {
496 std::string name {};
497 int32_t duration {0};
498 if (!JsonUtils::GetStringFromJsonValue(singleLtdItem, TAG_NAME, name) ||
499 !JsonUtils::GetInt32FromJsonValue(singleLtdItem, TAG_MAX_DURATION_LIM, duration)) {
500 STANDBYSERVICE_LOGW("there is error in %{public}s config", key.c_str());
501 continue;
502 }
503 timeLimitedConfig.emplace_back(TimeLtdProcess{name, duration});
504 }
505 }
506
ParseCondition(const std::string & conditionStr)507 uint32_t StandbyConfigManager::ParseCondition(const std::string& conditionStr)
508 {
509 uint32_t conditionValue = 0;
510 std::stringstream ss(conditionStr);
511 std::string conditionSubstr;
512 while (std::getline(ss, conditionSubstr, TAG_CONDITION_DELIM)) {
513 auto iter = conditionMap.find(conditionSubstr);
514 if (iter == conditionMap.end()) {
515 continue;
516 }
517 conditionValue |= iter->second;
518 }
519 return conditionValue;
520 }
521
DumpSetDebugMode(bool debugMode)522 void StandbyConfigManager::DumpSetDebugMode(bool debugMode)
523 {
524 std::lock_guard<std::mutex> lock(configMutex_);
525 if (debugMode) {
526 backStandbySwitchMap_ = standbySwitchMap_;
527 backStandbyParaMap_ = standbyParaMap_;
528 } else {
529 standbySwitchMap_ = backStandbySwitchMap_;
530 standbyParaMap_ = backStandbyParaMap_;
531 backStandbySwitchMap_.clear();
532 backStandbyParaMap_.clear();
533 }
534 }
535
DumpSetSwitch(const std::string & switchName,bool switchStatus,std::string & result)536 void StandbyConfigManager::DumpSetSwitch(const std::string& switchName, bool switchStatus, std::string& result)
537 {
538 std::lock_guard<std::mutex> lock(configMutex_);
539 auto iter = standbySwitchMap_.find(switchName);
540 if (iter == standbySwitchMap_.end()) {
541 result += switchName + " not exist\n";
542 return;
543 }
544 iter->second = switchStatus;
545 }
546
DumpSetParameter(const std::string & paramName,int32_t paramValue,std::string & result)547 void StandbyConfigManager::DumpSetParameter(const std::string& paramName, int32_t paramValue, std::string& result)
548 {
549 std::lock_guard<std::mutex> lock(configMutex_);
550 auto iter = standbyParaMap_.find(paramName);
551 if (iter == standbyParaMap_.end()) {
552 result += paramName + " not exist\n";
553 return;
554 }
555 iter->second = paramValue;
556 }
557
DumpStandbyConfigInfo(std::string & result)558 void StandbyConfigManager::DumpStandbyConfigInfo(std::string& result)
559 {
560 std::lock_guard<std::mutex> lock(configMutex_);
561 std::stringstream stream;
562 for (const auto& [switchName, switchVal] : standbySwitchMap_) {
563 stream << switchName << ": " << (switchVal ? "true" : "false") << "\n";
564 }
565 for (const auto& [paraName, paraVal] : standbyParaMap_) {
566 stream << paraName << ": " << paraVal << "\n";
567 }
568 for (const auto& [strategyName, strategyVal] : strategySwitchMap_) {
569 stream << strategyName << ": " << (strategyVal ? "true" : "false") << "\n";
570 }
571 stream << "strategy:";
572 for (const auto& strategy : strategyList_) {
573 stream << " " << strategy;
574 }
575 stream << "\n";
576 auto printConditions = [&stream](const int32_t& condition) { stream << "\t\t" << condition << " "; };
577 auto printProceses = [&stream](const std::string& process) { stream << "\t\t" << process << "\n"; };
578 auto printLtdProceses = [&stream](const TimeLtdProcess& timeLtdProcess) {
579 stream << "\t\t" << timeLtdProcess.name_ << " " << timeLtdProcess.maxDurationLim_ << "\n";
580 };
581 for (const auto& [resCtrlKey, resConfigVec] : defaultResourceConfigMap_) {
582 for (const auto& resConfig : *resConfigVec) {
583 stream << resCtrlKey << ": \n";
584 stream << "\tisAllow: " << resConfig.isAllow_ << "\n";
585 DumpResCtrlConfig<uint32_t>("conditions", resConfig.conditions_, stream, printConditions);
586 stream << "\n";
587 DumpResCtrlConfig<std::string>("processes", resConfig.processes_, stream, printProceses);
588 DumpResCtrlConfig<std::string>("apps", resConfig.apps_, stream, printProceses);
589 DumpResCtrlConfig<TimeLtdProcess>("timeLtdProcesses", resConfig.timeLtdProcesses_,
590 stream, printLtdProceses);
591 DumpResCtrlConfig<TimeLtdProcess>("timeLtdApps", resConfig.timeLtdApps_, stream, printLtdProceses);
592 }
593 }
594 result += stream.str();
595 stream.str("");
596 stream.clear();
597 }
598
DumpResCtrlConfig(const char * name,const std::vector<T> & configArray,std::stringstream & stream,const std::function<void (const T &)> & func)599 template<typename T> void StandbyConfigManager::DumpResCtrlConfig(const char* name, const std::vector<T>& configArray,
600 std::stringstream& stream, const std::function<void(const T&)>& func)
601 {
602 if (configArray.empty()) {
603 return;
604 }
605 stream << "\t" << name << ":\n";
606 for_each(configArray.begin(), configArray.end(), func);
607 }
608 } // namespace DevStandbyMgr
609 } // namespace OHOS