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 <list>
20 #include <memory>
21 #include <mutex>
22 #include <set>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 
28 #include "json_utils.h"
29 #include "singleton.h"
30 #include "standby_service_errors.h"
31 namespace OHOS {
32 namespace DevStandbyMgr {
33 namespace {
34     using GetExtConfigFunc = int32_t (*)(int32_t, std::vector<std::string>&);
35     using GetSingleExtConfigFunc = int32_t (*)(int32_t, std::string&);
36 }
37 class ConditionType {
38 public:
39     enum Type : uint32_t {
40         DAY_STANDBY = 1,
41         NIGHT_STANDBY = 1 << 1,
42     };
43 };
44 
45 struct TimeLtdProcess {
46     std::string name_;
47     int32_t maxDurationLim_;
48 
operator <OHOS::DevStandbyMgr::TimeLtdProcess49     bool operator < (const TimeLtdProcess& rhs) const
50     {
51         return name_ < rhs.name_;
52     }
53 };
54 
55 struct DefaultResourceConfig {
56     bool isAllow_;
57     std::vector<uint32_t> conditions_;
58     std::vector<std::string> processes_;
59     std::vector<std::string> apps_;
60     std::vector<TimeLtdProcess> timeLtdProcesses_;
61     std::vector<TimeLtdProcess> timeLtdApps_;
62 };
63 
64 struct TimerClockApp {
65     std::string name_;
66     int32_t timerPeriod_;
67     bool isTimerClock_;
68 };
69 
70 struct TimerResourceConfig {
71     bool isAllow_;
72     std::vector<uint32_t> conditions_;
73     std::vector<TimerClockApp> timerClockApps_;
74 };
75 
76 class StandbyConfigManager {
77     DECLARE_DELAYED_SINGLETON(StandbyConfigManager);
78 public:
79     static std::shared_ptr<StandbyConfigManager> GetInstance();
80     ErrCode Init();
81     const std::string& GetPluginName();
82     nlohmann::json GetDefaultConfig(const std::string& configName);
83     bool GetStandbySwitch(const std::string& switchName);
84     int32_t GetStandbyParam(const std::string& paramName);
85     bool GetStrategySwitch(const std::string& switchName);
86     bool GetHalfHourSwitch(const std::string& switchName);
87     std::shared_ptr<std::vector<DefaultResourceConfig>> GetResCtrlConfig(const std::string& switchName);
88     const std::vector<TimerResourceConfig>& GetTimerResConfig();
89     const std::vector<std::string>& GetStrategyConfigList();
90     bool GetStrategyConfigList(const std::string& switchName);
91     void UpdateStrategyList();
92     std::vector<int32_t> GetStandbyDurationList(const std::string& switchName);
93 
94     std::set<TimeLtdProcess> GetEligibleAllowTimeConfig(const std::string& paramName,
95         uint32_t condition, bool isAllow, bool isApp);
96     std::set<std::string> GetEligiblePersistAllowConfig(const std::string& paramName,
97         uint32_t condition, bool isAllow, bool isApp);
98     int32_t GetMaxDuration(const std::string& name, const std::string& paramName, uint32_t condition, bool isApp);
99 
100     std::vector<int32_t> GetStandbyLadderBatteryList(const std::string& switchName);
101 
102     void DumpSetDebugMode(bool debugMode);
103     void DumpSetSwitch(const std::string& switchName, bool switchStatus, std::string& result);
104     void DumpSetParameter(const std::string& paramName, int32_t paramValue, std::string& result);
105     bool NeedsToReadCloudConfig();
106     /**
107      * @brief dump config info
108      */
109     void DumpStandbyConfigInfo(std::string& result);
110 private:
111     StandbyConfigManager(const StandbyConfigManager&) = delete;
112     StandbyConfigManager& operator= (const StandbyConfigManager&) = delete;
113     StandbyConfigManager(StandbyConfigManager&&) = delete;
114     StandbyConfigManager& operator= (StandbyConfigManager&&) = delete;
115     template<typename T> std::set<T> GetEligibleAllowConfig(const std::string& paramName,
116         uint32_t condition, bool isAllow, bool isApp, const std::function<void(bool, std::set<T>&,
117         const DefaultResourceConfig&)>& func);
118     template<typename T> T
119         GetConfigWithName(const std::string& switchName, std::unordered_map<std::string, T>& configMap);
120 
121     std::vector<std::string> GetConfigFileList(const std::string& relativeConfigPath);
122     bool ParseDeviceStanbyConfig(const nlohmann::json& devStandbyConfigRoot);
123     bool ParseStandbyConfig(const nlohmann::json& standbyConfig);
124     bool ParseIntervalList(const nlohmann::json& standbyIntervalList);
125     bool ParseStrategyListConfig(const nlohmann::json& standbyListConfig);
126     bool ParseHalfHourSwitchConfig(const nlohmann::json& halfHourSwitchConfig);
127     bool ParseResCtrlConfig(const nlohmann::json& resCtrlConfigRoot);
128     bool ParseTimerResCtrlConfig(const nlohmann::json& resConfigArray);
129     bool ParseDefaultResCtrlConfig(const std::string& resCtrlKey, const nlohmann::json& resConfigArray);
130     bool ParseCommonResCtrlConfig(const nlohmann::json& sigleConfigItem, DefaultResourceConfig& resCtrlConfig);
131     void ParseTimeLimitedConfig(const nlohmann::json& singleConfigItem, const std::string& key,
132         std::vector<TimeLtdProcess>& resCtrlConfig);
133     uint32_t ParseCondition(const std::string& conditionStr);
134     template<typename T> void DumpResCtrlConfig(const char* name, const std::vector<T>& configArray,
135         std::stringstream& stream, const std::function<void(const T&)>& func);
136     void LoadGetExtConfigFunc();
137     void GetAndParseStandbyConfig();
138     void GetAndParseStrategyConfig();
139     void GetCloudConfig();
140     void ParseCloudConfig(const nlohmann::json& devConfigRoot);
141     bool GetParamVersion(const int32_t& fileIndex, std::string& version);
142     bool GetCloudVersion(const int32_t& fileIndex, std::string& version);
143     /**
144      * @brief Get a larger verison
145      *
146      * @param configVerA The version that needs to be compared
147      * @param configVerB The version that needs to be compared
148      * @return 1 if configVerA is larger than configVerB, or configVerA equals configVerB.
149      * @return 0 if configVerB is larger than configVerA.
150      * @return -1 if there is an error during the comparison.
151      */
152     int CompareVersion(const std::string& configVerA, const std::string& configVerB);
153     bool ParseVersionConfig(const nlohmann::json& standbyConfig, std::string& version);
154 
155     bool ParseBatteryList(const nlohmann::json& standbyBatteryList);
156 
157 private:
158     std::mutex configMutex_;
159     std::string pluginName_;
160     std::unordered_map<std::string, bool> standbySwitchMap_;
161     std::unordered_map<std::string, int32_t> standbyParaMap_;
162     std::unordered_map<std::string, bool> strategySwitchMap_;
163     std::unordered_map<std::string, bool> strategyListMap_;
164     std::vector<std::string> strategyList_;
165     std::unordered_map<std::string, bool> halfhourSwitchMap_;
166     std::unordered_map<std::string, std::shared_ptr<std::vector<DefaultResourceConfig>>> defaultResourceConfigMap_;
167     std::vector<TimerResourceConfig> timerResConfigList_;
168     std::unordered_map<std::string, std::vector<int32_t>> intervalListMap_;
169     std::unordered_map<std::string, std::vector<int32_t>> ladderBatteryListMap_;
170     std::unordered_map<std::string, nlohmann::json> standbyStrategyConfigMap_;
171 
172     std::unordered_map<std::string, bool> backStandbySwitchMap_;
173     std::unordered_map<std::string, int32_t> backStandbyParaMap_;
174     GetExtConfigFunc getExtConfigFunc_ = nullptr;
175     GetSingleExtConfigFunc getSingleExtConfigFunc_ = nullptr;
176 };
177 }  // namespace DevStandbyMgr
178 }  // namespace OHOS
179 #endif  // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_STANDBY_CONFIG_MANAGER_H
180