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