• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_STANDBY_CONFIG_MANAGER_H
17 #define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_STANDBY_CONFIG_MANAGER_H
18 
19 #include <unordered_map>
20 #include <vector>
21 #include <string>
22 #include <memory>
23 #include <mutex>
24 #include <list>
25 #include <set>
26 
27 #include "json_utils.h"
28 #include "standby_service_errors.h"
29 #include "standby_service_log.h"
30 #include "single_instance.h"
31 
32 namespace OHOS {
33 namespace DevStandbyMgr {
34 class ConditionType {
35 public:
36     enum Type : uint32_t {
37         DAY_STANDBY = 1,
38         NIGHT_STANDBY = 1 << 1,
39     };
40 };
41 
42 struct TimeLtdProcess {
43     std::string name_;
44     int32_t maxDurationLim_;
45 
46     bool operator < (const TimeLtdProcess& rhs) const
47     {
48         return name_ < rhs.name_;
49     }
50 };
51 
52 struct DefaultResourceConfig {
53     bool isAllow_;
54     std::vector<uint32_t> conditions_;
55     std::vector<std::string> processes_;
56     std::vector<std::string> apps_;
57     std::vector<TimeLtdProcess> timeLtdProcesses_;
58     std::vector<TimeLtdProcess> timeLtdApps_;
59 };
60 
61 struct TimerClockApp {
62     std::string name_;
63     int32_t timerPeriod_;
64     bool isTimerClock_;
65 };
66 
67 struct TimerResourceConfig {
68     bool isAllow_;
69     std::vector<uint32_t> conditions_;
70     std::vector<TimerClockApp> timerClockApps_;
71 };
72 
73 class StandbyConfigManager {
74     DECLARE_SINGLE_INSTANCE(StandbyConfigManager);
75 public:
76     ErrCode Init();
77     const std::string& GetPluginName();
78     bool GetStandbySwitch(const std::string& switchName);
79     int32_t GetStandbyParam(const std::string& paramName);
80     bool GetStrategySwitch(const std::string& switchName);
81     bool GetHalfHourSwitch(const std::string& switchName);
82     std::shared_ptr<std::vector<DefaultResourceConfig>> GetResCtrlConfig(const std::string& switchName);
83     const std::vector<TimerResourceConfig>& GetTimerResConfig();
84     const std::vector<std::string>& GetStrategyConfigList();
85     std::vector<int32_t> GetStandbyDurationList(const std::string& switchName);
86 
87     std::set<TimeLtdProcess> GetEligibleAllowTimeConfig(const std::string& paramName,
88         uint32_t condition, bool isAllow, bool isApp);
89     std::set<std::string> GetEligiblePersistAllowConfig(const std::string& paramName,
90         uint32_t condition, bool isAllow, bool isApp);
91     int32_t GetMaxDuration(const std::string& name, const std::string& paramName, uint32_t condition, bool isApp);
92 
93     void DumpSetDebugMode(bool debugMode);
94     void DumpSetSwitch(const std::string& switchName, bool switchStatus, std::string& result);
95     void DumpSetParameter(const std::string& paramName, int32_t paramValue, std::string& result);
96 
97     /**
98      * @brief dump config info
99      */
100     void DumpStandbyConfigInfo(std::string& result);
101 private:
102     template<typename T> std::set<T> GetEligibleAllowConfig(const std::string& paramName,
103         uint32_t condition, bool isAllow, bool isApp, const std::function<void(bool, std::set<T>&,
104         const DefaultResourceConfig&)>& func);
105     template<typename T> T
106         GetConfigWithName(const std::string& switchName, std::unordered_map<std::string, T>& configMap);
107 
108     std::vector<std::string> GetConfigFileList(const std::string& relativeConfigPath);
109     bool ParseDeviceStanbyConfig(const nlohmann::json& devStandbyConfigRoot);
110     bool ParseStandbyConfig(const nlohmann::json& standbyConfig);
111     bool ParseIntervalList(const nlohmann::json& standbyIntervalList);
112     bool ParseStrategyListConfig(const nlohmann::json& standbyListConfig);
113     bool ParseHalfHourSwitchConfig(const nlohmann::json& halfHourSwitchConfig);
114     bool ParseResCtrlConfig(const nlohmann::json& resCtrlConfigRoot);
115     bool ParseTimerResCtrlConfig(const nlohmann::json& resConfigArray);
116     bool ParseDefaultResCtrlConfig(const std::string& resCtrlKey, const nlohmann::json& resConfigArray);
117     bool ParseCommonResCtrlConfig(const nlohmann::json& sigleConfigItem, DefaultResourceConfig& resCtrlConfig);
118     void ParseTimeLimitedConfig(const nlohmann::json& singleConfigItem, const std::string& key,
119         std::vector<TimeLtdProcess>& resCtrlConfig);
120     uint32_t ParseCondition(const std::string& conditionStr);
121     template<typename T> void DumpResCtrlConfig(const char* name, const std::vector<T>& configArray,
122         std::stringstream& stream, const std::function<void(const T&)>& func);
123 private:
124     std::mutex configMutex_;
125     std::string pluginName_;
126     std::unordered_map<std::string, bool> standbySwitchMap_;
127     std::unordered_map<std::string, int32_t> standbyParaMap_;
128     std::unordered_map<std::string, bool> strategySwitchMap_;
129     std::vector<std::string> strategyList_;
130     std::unordered_map<std::string, bool> halfhourSwitchMap_;
131     std::unordered_map<std::string, std::shared_ptr<std::vector<DefaultResourceConfig>>> defaultResourceConfigMap_;
132     std::vector<TimerResourceConfig> timerResConfigList_;
133     std::unordered_map<std::string, std::vector<int32_t>> intervalListMap_;
134 
135     std::unordered_map<std::string, bool> backStandbySwitchMap_;
136     std::unordered_map<std::string, int32_t> backStandbyParaMap_;
137 };
138 }  // namespace DevStandbyMgr
139 }  // namespace OHOS
140 #endif  // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_STANDBY_CONFIG_MANAGER_H
141