/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_STANDBY_CONFIG_MANAGER_H #define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_STANDBY_CONFIG_MANAGER_H #include #include #include #include #include #include #include #include "json_utils.h" #include "single_instance.h" #include "standby_service_errors.h" namespace OHOS { namespace DevStandbyMgr { namespace { using GetExtConfigFunc = int32_t (*)(int32_t, std::vector&); using GetSingleExtConfigFunc = int32_t (*)(int32_t, std::string&); } class ConditionType { public: enum Type : uint32_t { DAY_STANDBY = 1, NIGHT_STANDBY = 1 << 1, }; }; struct TimeLtdProcess { std::string name_; int32_t maxDurationLim_; bool operator < (const TimeLtdProcess& rhs) const { return name_ < rhs.name_; } }; struct DefaultResourceConfig { bool isAllow_; std::vector conditions_; std::vector processes_; std::vector apps_; std::vector timeLtdProcesses_; std::vector timeLtdApps_; }; struct TimerClockApp { std::string name_; int32_t timerPeriod_; bool isTimerClock_; }; struct TimerResourceConfig { bool isAllow_; std::vector conditions_; std::vector timerClockApps_; }; class StandbyConfigManager { DECLARE_SINGLE_INSTANCE(StandbyConfigManager); public: ErrCode Init(); const std::string& GetPluginName(); bool GetStandbySwitch(const std::string& switchName); int32_t GetStandbyParam(const std::string& paramName); bool GetStrategySwitch(const std::string& switchName); bool GetHalfHourSwitch(const std::string& switchName); std::shared_ptr> GetResCtrlConfig(const std::string& switchName); const std::vector& GetTimerResConfig(); const std::vector& GetStrategyConfigList(); bool GetStrategyConfigList(const std::string& switchName); void UpdateStrategyList(); std::vector GetStandbyDurationList(const std::string& switchName); std::set GetEligibleAllowTimeConfig(const std::string& paramName, uint32_t condition, bool isAllow, bool isApp); std::set GetEligiblePersistAllowConfig(const std::string& paramName, uint32_t condition, bool isAllow, bool isApp); int32_t GetMaxDuration(const std::string& name, const std::string& paramName, uint32_t condition, bool isApp); void DumpSetDebugMode(bool debugMode); void DumpSetSwitch(const std::string& switchName, bool switchStatus, std::string& result); void DumpSetParameter(const std::string& paramName, int32_t paramValue, std::string& result); bool NeedsToReadCloudConfig(); /** * @brief dump config info */ void DumpStandbyConfigInfo(std::string& result); private: template std::set GetEligibleAllowConfig(const std::string& paramName, uint32_t condition, bool isAllow, bool isApp, const std::function&, const DefaultResourceConfig&)>& func); template T GetConfigWithName(const std::string& switchName, std::unordered_map& configMap); std::vector GetConfigFileList(const std::string& relativeConfigPath); bool ParseDeviceStanbyConfig(const nlohmann::json& devStandbyConfigRoot); bool ParseStandbyConfig(const nlohmann::json& standbyConfig); bool ParseIntervalList(const nlohmann::json& standbyIntervalList); bool ParseStrategyListConfig(const nlohmann::json& standbyListConfig); bool ParseHalfHourSwitchConfig(const nlohmann::json& halfHourSwitchConfig); bool ParseResCtrlConfig(const nlohmann::json& resCtrlConfigRoot); bool ParseTimerResCtrlConfig(const nlohmann::json& resConfigArray); bool ParseDefaultResCtrlConfig(const std::string& resCtrlKey, const nlohmann::json& resConfigArray); bool ParseCommonResCtrlConfig(const nlohmann::json& sigleConfigItem, DefaultResourceConfig& resCtrlConfig); void ParseTimeLimitedConfig(const nlohmann::json& singleConfigItem, const std::string& key, std::vector& resCtrlConfig); uint32_t ParseCondition(const std::string& conditionStr); template void DumpResCtrlConfig(const char* name, const std::vector& configArray, std::stringstream& stream, const std::function& func); void LoadGetExtConfigFunc(); void GetAndParseStandbyConfig(); void GetAndParseStrategyConfig(); void GetCloudConfig(); void ParseCloudConfig(const nlohmann::json& devConfigRoot); bool GetParamVersion(const int32_t& fileIndex, std::string& version); bool GetCloudVersion(const int32_t& fileIndex, std::string& version); /** * @brief Get a larger verison * * @param configVerA The version that needs to be compared * @param configVerB The version that needs to be compared * @return 1 if configVerA is larger than configVerB, or configVerA equals configVerB. * @return 0 if configVerB is larger than configVerA. * @return -1 if there is an error during the comparison. */ int CompareVersion(const std::string& configVerA, const std::string& configVerB); bool ParseVersionConfig(const nlohmann::json& standbyConfig, std::string& version); private: std::mutex configMutex_; std::string pluginName_; std::unordered_map standbySwitchMap_; std::unordered_map standbyParaMap_; std::unordered_map strategySwitchMap_; std::unordered_map strategyListMap_; std::vector strategyList_; std::unordered_map halfhourSwitchMap_; std::unordered_map>> defaultResourceConfigMap_; std::vector timerResConfigList_; std::unordered_map> intervalListMap_; std::unordered_map backStandbySwitchMap_; std::unordered_map backStandbyParaMap_; GetExtConfigFunc getExtConfigFunc_ = nullptr; GetSingleExtConfigFunc getSingleExtConfigFunc_ = nullptr; }; } // namespace DevStandbyMgr } // namespace OHOS #endif // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_STANDBY_CONFIG_MANAGER_H