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 "single_instance.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 
49     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_SINGLE_INSTANCE(StandbyConfigManager);
78 public:
79     ErrCode Init();
80     const std::string& GetPluginName();
81     bool GetStandbySwitch(const std::string& switchName);
82     int32_t GetStandbyParam(const std::string& paramName);
83     bool GetStrategySwitch(const std::string& switchName);
84     bool GetHalfHourSwitch(const std::string& switchName);
85     std::shared_ptr<std::vector<DefaultResourceConfig>> GetResCtrlConfig(const std::string& switchName);
86     const std::vector<TimerResourceConfig>& GetTimerResConfig();
87     const std::vector<std::string>& GetStrategyConfigList();
88     bool GetStrategyConfigList(const std::string& switchName);
89     void UpdateStrategyList();
90     std::vector<int32_t> GetStandbyDurationList(const std::string& switchName);
91 
92     std::set<TimeLtdProcess> GetEligibleAllowTimeConfig(const std::string& paramName,
93         uint32_t condition, bool isAllow, bool isApp);
94     std::set<std::string> GetEligiblePersistAllowConfig(const std::string& paramName,
95         uint32_t condition, bool isAllow, bool isApp);
96     int32_t GetMaxDuration(const std::string& name, const std::string& paramName, uint32_t condition, bool isApp);
97 
98     void DumpSetDebugMode(bool debugMode);
99     void DumpSetSwitch(const std::string& switchName, bool switchStatus, std::string& result);
100     void DumpSetParameter(const std::string& paramName, int32_t paramValue, std::string& result);
101     bool NeedsToReadCloudConfig();
102     /**
103      * @brief dump config info
104      */
105     void DumpStandbyConfigInfo(std::string& result);
106 private:
107     template<typename T> std::set<T> GetEligibleAllowConfig(const std::string& paramName,
108         uint32_t condition, bool isAllow, bool isApp, const std::function<void(bool, std::set<T>&,
109         const DefaultResourceConfig&)>& func);
110     template<typename T> T
111         GetConfigWithName(const std::string& switchName, std::unordered_map<std::string, T>& configMap);
112 
113     std::vector<std::string> GetConfigFileList(const std::string& relativeConfigPath);
114     bool ParseDeviceStanbyConfig(const nlohmann::json& devStandbyConfigRoot);
115     bool ParseStandbyConfig(const nlohmann::json& standbyConfig);
116     bool ParseIntervalList(const nlohmann::json& standbyIntervalList);
117     bool ParseStrategyListConfig(const nlohmann::json& standbyListConfig);
118     bool ParseHalfHourSwitchConfig(const nlohmann::json& halfHourSwitchConfig);
119     bool ParseResCtrlConfig(const nlohmann::json& resCtrlConfigRoot);
120     bool ParseTimerResCtrlConfig(const nlohmann::json& resConfigArray);
121     bool ParseDefaultResCtrlConfig(const std::string& resCtrlKey, const nlohmann::json& resConfigArray);
122     bool ParseCommonResCtrlConfig(const nlohmann::json& sigleConfigItem, DefaultResourceConfig& resCtrlConfig);
123     void ParseTimeLimitedConfig(const nlohmann::json& singleConfigItem, const std::string& key,
124         std::vector<TimeLtdProcess>& resCtrlConfig);
125     uint32_t ParseCondition(const std::string& conditionStr);
126     template<typename T> void DumpResCtrlConfig(const char* name, const std::vector<T>& configArray,
127         std::stringstream& stream, const std::function<void(const T&)>& func);
128     void LoadGetExtConfigFunc();
129     void GetAndParseStandbyConfig();
130     void GetAndParseStrategyConfig();
131     void GetCloudConfig();
132     void ParseCloudConfig(const nlohmann::json& devConfigRoot);
133     bool GetParamVersion(const int32_t& fileIndex, std::string& version);
134     bool GetCloudVersion(const int32_t& fileIndex, std::string& version);
135     /**
136      * @brief Get a larger verison
137      *
138      * @param configVerA The version that needs to be compared
139      * @param configVerB The version that needs to be compared
140      * @return 1 if configVerA is larger than configVerB, or configVerA equals configVerB.
141      * @return 0 if configVerB is larger than configVerA.
142      * @return -1 if there is an error during the comparison.
143      */
144     int CompareVersion(const std::string& configVerA, const std::string& configVerB);
145     bool ParseVersionConfig(const nlohmann::json& standbyConfig, std::string& version);
146 
147 private:
148     std::mutex configMutex_;
149     std::string pluginName_;
150     std::unordered_map<std::string, bool> standbySwitchMap_;
151     std::unordered_map<std::string, int32_t> standbyParaMap_;
152     std::unordered_map<std::string, bool> strategySwitchMap_;
153     std::unordered_map<std::string, bool> strategyListMap_;
154     std::vector<std::string> strategyList_;
155     std::unordered_map<std::string, bool> halfhourSwitchMap_;
156     std::unordered_map<std::string, std::shared_ptr<std::vector<DefaultResourceConfig>>> defaultResourceConfigMap_;
157     std::vector<TimerResourceConfig> timerResConfigList_;
158     std::unordered_map<std::string, std::vector<int32_t>> intervalListMap_;
159 
160     std::unordered_map<std::string, bool> backStandbySwitchMap_;
161     std::unordered_map<std::string, int32_t> backStandbyParaMap_;
162     GetExtConfigFunc getExtConfigFunc_ = nullptr;
163     GetSingleExtConfigFunc getSingleExtConfigFunc_ = nullptr;
164 };
165 }  // namespace DevStandbyMgr
166 }  // namespace OHOS
167 #endif  // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_STANDBY_CONFIG_MANAGER_H
168