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 STANDBYSERVICE_LOGI("succeed to parse the config of TIMER");
433 return true;
434 }
435
ParseDefaultResCtrlConfig(const std::string & resCtrlKey,const nlohmann::json & resConfigArray)436 bool StandbyConfigManager::ParseDefaultResCtrlConfig(const std::string& resCtrlKey,
437 const nlohmann::json& resConfigArray)
438 {
439 if (!resConfigArray.is_array()) {
440 STANDBYSERVICE_LOGW("the value of %{public}s should be an array", resCtrlKey.c_str());
441 return false;
442 }
443 auto defaultResConfigPtr = std::make_shared<std::vector<DefaultResourceConfig>>();
444 for (const auto &singleConfigItem : resConfigArray) {
445 DefaultResourceConfig defaultResourceConfig;
446 if (!ParseCommonResCtrlConfig(singleConfigItem, defaultResourceConfig)) {
447 STANDBYSERVICE_LOGW("the value of %{public}s can not be parsed", resCtrlKey.c_str());
448 return false;
449 }
450 defaultResConfigPtr->emplace_back(std::move(defaultResourceConfig));
451 }
452 defaultResourceConfigMap_[resCtrlKey] = defaultResConfigPtr;
453 STANDBYSERVICE_LOGI("succeed to parse the config of %{public}s", resCtrlKey.c_str());
454 return true;
455 }
456
ParseCommonResCtrlConfig(const nlohmann::json & singleConfigItem,DefaultResourceConfig & resCtrlConfig)457 bool StandbyConfigManager::ParseCommonResCtrlConfig(const nlohmann::json& singleConfigItem,
458 DefaultResourceConfig& resCtrlConfig)
459 {
460 if (!singleConfigItem.contains(TAG_ACTION) || !singleConfigItem.contains(TAG_CONDITION)) {
461 STANDBYSERVICE_LOGW("there is no necessary field %{public}s or %{public}s",
462 TAG_ACTION.c_str(), TAG_CONDITION.c_str());
463 return false;
464 }
465 std::string resCtrlAction;
466 std::vector<std::string> conditionItemArray {};
467 if (!JsonUtils::GetStringFromJsonValue(singleConfigItem, TAG_ACTION, resCtrlAction) ||
468 !JsonUtils::GetStrArrFromJsonValue(singleConfigItem, TAG_CONDITION, conditionItemArray)) {
469 STANDBYSERVICE_LOGW("get necessary field %{public}s or %{public}s config failed",
470 TAG_ACTION.c_str(), TAG_CONDITION.c_str());
471 return false;
472 }
473 resCtrlConfig.isAllow_ = resCtrlAction == TAG_ALLOW;
474
475 for (const auto &singleConditionItem : conditionItemArray) {
476 uint32_t conditionValue = ParseCondition(singleConditionItem);
477 if (conditionValue > 0) {
478 resCtrlConfig.conditions_.emplace_back(conditionValue);
479 }
480 }
481
482 JsonUtils::GetStrArrFromJsonValue(singleConfigItem, TAG_PROCESSES, resCtrlConfig.processes_);
483 JsonUtils::GetStrArrFromJsonValue(singleConfigItem, TAG_APPS, resCtrlConfig.apps_);
484 ParseTimeLimitedConfig(singleConfigItem, TAG_PROCESSES_LIMIT, resCtrlConfig.timeLtdProcesses_);
485 ParseTimeLimitedConfig(singleConfigItem, TAG_APPS_LIMIT, resCtrlConfig.timeLtdApps_);
486 return true;
487 }
488
ParseTimeLimitedConfig(const nlohmann::json & singleConfigItem,const std::string & key,std::vector<TimeLtdProcess> & timeLimitedConfig)489 void StandbyConfigManager::ParseTimeLimitedConfig(const nlohmann::json& singleConfigItem,
490 const std::string& key, std::vector<TimeLtdProcess>& timeLimitedConfig)
491 {
492 nlohmann::json timeLimitedItems;
493 if (!JsonUtils::GetArrayFromJsonValue(singleConfigItem, key, timeLimitedItems)) {
494 return;
495 }
496 for (const auto &singleLtdItem : timeLimitedItems) {
497 std::string name {};
498 int32_t duration {0};
499 if (!JsonUtils::GetStringFromJsonValue(singleLtdItem, TAG_NAME, name) ||
500 !JsonUtils::GetInt32FromJsonValue(singleLtdItem, TAG_MAX_DURATION_LIM, duration)) {
501 STANDBYSERVICE_LOGW("there is error in %{public}s config", key.c_str());
502 continue;
503 }
504 timeLimitedConfig.emplace_back(TimeLtdProcess{name, duration});
505 }
506 }
507
ParseCondition(const std::string & conditionStr)508 uint32_t StandbyConfigManager::ParseCondition(const std::string& conditionStr)
509 {
510 uint32_t conditionValue = 0;
511 std::stringstream ss(conditionStr);
512 std::string conditionSubstr;
513 while (std::getline(ss, conditionSubstr, TAG_CONDITION_DELIM)) {
514 auto iter = conditionMap.find(conditionSubstr);
515 if (iter == conditionMap.end()) {
516 continue;
517 }
518 conditionValue |= iter->second;
519 }
520 return conditionValue;
521 }
522
DumpSetDebugMode(bool debugMode)523 void StandbyConfigManager::DumpSetDebugMode(bool debugMode)
524 {
525 std::lock_guard<std::mutex> lock(configMutex_);
526 if (debugMode) {
527 backStandbySwitchMap_ = standbySwitchMap_;
528 backStandbyParaMap_ = standbyParaMap_;
529 } else {
530 standbySwitchMap_ = backStandbySwitchMap_;
531 standbyParaMap_ = backStandbyParaMap_;
532 backStandbySwitchMap_.clear();
533 backStandbyParaMap_.clear();
534 }
535 }
536
DumpSetSwitch(const std::string & switchName,bool switchStatus,std::string & result)537 void StandbyConfigManager::DumpSetSwitch(const std::string& switchName, bool switchStatus, std::string& result)
538 {
539 std::lock_guard<std::mutex> lock(configMutex_);
540 auto iter = standbySwitchMap_.find(switchName);
541 if (iter == standbySwitchMap_.end()) {
542 result += switchName + " not exist\n";
543 return;
544 }
545 iter->second = switchStatus;
546 }
547
DumpSetParameter(const std::string & paramName,int32_t paramValue,std::string & result)548 void StandbyConfigManager::DumpSetParameter(const std::string& paramName, int32_t paramValue, std::string& result)
549 {
550 std::lock_guard<std::mutex> lock(configMutex_);
551 auto iter = standbyParaMap_.find(paramName);
552 if (iter == standbyParaMap_.end()) {
553 result += paramName + " not exist\n";
554 return;
555 }
556 iter->second = paramValue;
557 }
558
DumpStandbyConfigInfo(std::string & result)559 void StandbyConfigManager::DumpStandbyConfigInfo(std::string& result)
560 {
561 std::lock_guard<std::mutex> lock(configMutex_);
562 std::stringstream stream;
563 for (const auto& [switchName, switchVal] : standbySwitchMap_) {
564 stream << switchName << ": " << (switchVal ? "true" : "false") << "\n";
565 }
566 for (const auto& [paraName, paraVal] : standbyParaMap_) {
567 stream << paraName << ": " << paraVal << "\n";
568 }
569 for (const auto& [strategyName, strategyVal] : strategySwitchMap_) {
570 stream << strategyName << ": " << (strategyVal ? "true" : "false") << "\n";
571 }
572 stream << "strategy:";
573 for (const auto& strategy : strategyList_) {
574 stream << " " << strategy;
575 }
576 stream << "\n";
577 auto printConditions = [&stream](const int32_t& condition) { stream << "\t\t" << condition << " "; };
578 auto printProceses = [&stream](const std::string& process) { stream << "\t\t" << process << "\n"; };
579 auto printLtdProceses = [&stream](const TimeLtdProcess& timeLtdProcess) {
580 stream << "\t\t" << timeLtdProcess.name_ << " " << timeLtdProcess.maxDurationLim_ << "\n";
581 };
582 for (const auto& [resCtrlKey, resConfigVec] : defaultResourceConfigMap_) {
583 for (const auto& resConfig : *resConfigVec) {
584 stream << resCtrlKey << ": \n";
585 stream << "\tisAllow: " << resConfig.isAllow_ << "\n";
586 DumpResCtrlConfig<uint32_t>("conditions", resConfig.conditions_, stream, printConditions);
587 stream << "\n";
588 DumpResCtrlConfig<std::string>("processes", resConfig.processes_, stream, printProceses);
589 DumpResCtrlConfig<std::string>("apps", resConfig.apps_, stream, printProceses);
590 DumpResCtrlConfig<TimeLtdProcess>("timeLtdProcesses", resConfig.timeLtdProcesses_,
591 stream, printLtdProceses);
592 DumpResCtrlConfig<TimeLtdProcess>("timeLtdApps", resConfig.timeLtdApps_, stream, printLtdProceses);
593 }
594 }
595 result += stream.str();
596 stream.str("");
597 stream.clear();
598 }
599
DumpResCtrlConfig(const char * name,const std::vector<T> & configArray,std::stringstream & stream,const std::function<void (const T &)> & func)600 template<typename T> void StandbyConfigManager::DumpResCtrlConfig(const char* name, const std::vector<T>& configArray,
601 std::stringstream& stream, const std::function<void(const T&)>& func)
602 {
603 if (configArray.empty()) {
604 return;
605 }
606 stream << "\t" << name << ":\n";
607 for_each(configArray.begin(), configArray.end(), func);
608 }
609 } // namespace DevStandbyMgr
610 } // namespace OHOS