199552fe9Sopenharmony_ci/*
299552fe9Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
399552fe9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
499552fe9Sopenharmony_ci * you may not use this file except in compliance with the License.
599552fe9Sopenharmony_ci * You may obtain a copy of the License at
699552fe9Sopenharmony_ci *
799552fe9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
899552fe9Sopenharmony_ci *
999552fe9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1099552fe9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1199552fe9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1299552fe9Sopenharmony_ci * See the License for the specific language governing permissions and
1399552fe9Sopenharmony_ci * limitations under the License.
1499552fe9Sopenharmony_ci */
1599552fe9Sopenharmony_ci
1699552fe9Sopenharmony_ci#ifndef FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_STANDBY_CONFIG_MANAGER_H
1799552fe9Sopenharmony_ci#define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_STANDBY_CONFIG_MANAGER_H
1899552fe9Sopenharmony_ci
1999552fe9Sopenharmony_ci#include <list>
2099552fe9Sopenharmony_ci#include <memory>
2199552fe9Sopenharmony_ci#include <mutex>
2299552fe9Sopenharmony_ci#include <set>
2399552fe9Sopenharmony_ci#include <string>
2499552fe9Sopenharmony_ci#include <unordered_map>
2599552fe9Sopenharmony_ci#include <vector>
2699552fe9Sopenharmony_ci
2799552fe9Sopenharmony_ci
2899552fe9Sopenharmony_ci#include "json_utils.h"
2999552fe9Sopenharmony_ci#include "singleton.h"
3099552fe9Sopenharmony_ci#include "standby_service_errors.h"
3199552fe9Sopenharmony_cinamespace OHOS {
3299552fe9Sopenharmony_cinamespace DevStandbyMgr {
3399552fe9Sopenharmony_cinamespace {
3499552fe9Sopenharmony_ci    using GetExtConfigFunc = int32_t (*)(int32_t, std::vector<std::string>&);
3599552fe9Sopenharmony_ci    using GetSingleExtConfigFunc = int32_t (*)(int32_t, std::string&);
3699552fe9Sopenharmony_ci}
3799552fe9Sopenharmony_ciclass ConditionType {
3899552fe9Sopenharmony_cipublic:
3999552fe9Sopenharmony_ci    enum Type : uint32_t {
4099552fe9Sopenharmony_ci        DAY_STANDBY = 1,
4199552fe9Sopenharmony_ci        NIGHT_STANDBY = 1 << 1,
4299552fe9Sopenharmony_ci    };
4399552fe9Sopenharmony_ci};
4499552fe9Sopenharmony_ci
4599552fe9Sopenharmony_cistruct TimeLtdProcess {
4699552fe9Sopenharmony_ci    std::string name_;
4799552fe9Sopenharmony_ci    int32_t maxDurationLim_;
4899552fe9Sopenharmony_ci
4999552fe9Sopenharmony_ci    bool operator < (const TimeLtdProcess& rhs) const
5099552fe9Sopenharmony_ci    {
5199552fe9Sopenharmony_ci        return name_ < rhs.name_;
5299552fe9Sopenharmony_ci    }
5399552fe9Sopenharmony_ci};
5499552fe9Sopenharmony_ci
5599552fe9Sopenharmony_cistruct DefaultResourceConfig {
5699552fe9Sopenharmony_ci    bool isAllow_;
5799552fe9Sopenharmony_ci    std::vector<uint32_t> conditions_;
5899552fe9Sopenharmony_ci    std::vector<std::string> processes_;
5999552fe9Sopenharmony_ci    std::vector<std::string> apps_;
6099552fe9Sopenharmony_ci    std::vector<TimeLtdProcess> timeLtdProcesses_;
6199552fe9Sopenharmony_ci    std::vector<TimeLtdProcess> timeLtdApps_;
6299552fe9Sopenharmony_ci};
6399552fe9Sopenharmony_ci
6499552fe9Sopenharmony_cistruct TimerClockApp {
6599552fe9Sopenharmony_ci    std::string name_;
6699552fe9Sopenharmony_ci    int32_t timerPeriod_;
6799552fe9Sopenharmony_ci    bool isTimerClock_;
6899552fe9Sopenharmony_ci};
6999552fe9Sopenharmony_ci
7099552fe9Sopenharmony_cistruct TimerResourceConfig {
7199552fe9Sopenharmony_ci    bool isAllow_;
7299552fe9Sopenharmony_ci    std::vector<uint32_t> conditions_;
7399552fe9Sopenharmony_ci    std::vector<TimerClockApp> timerClockApps_;
7499552fe9Sopenharmony_ci};
7599552fe9Sopenharmony_ci
7699552fe9Sopenharmony_ciclass StandbyConfigManager {
7799552fe9Sopenharmony_ci    DECLARE_DELAYED_SINGLETON(StandbyConfigManager);
7899552fe9Sopenharmony_cipublic:
7999552fe9Sopenharmony_ci    static std::shared_ptr<StandbyConfigManager> GetInstance();
8099552fe9Sopenharmony_ci    ErrCode Init();
8199552fe9Sopenharmony_ci    const std::string& GetPluginName();
8299552fe9Sopenharmony_ci    nlohmann::json GetDefaultConfig(const std::string& configName);
8399552fe9Sopenharmony_ci    bool GetStandbySwitch(const std::string& switchName);
8499552fe9Sopenharmony_ci    int32_t GetStandbyParam(const std::string& paramName);
8599552fe9Sopenharmony_ci    bool GetStrategySwitch(const std::string& switchName);
8699552fe9Sopenharmony_ci    bool GetHalfHourSwitch(const std::string& switchName);
8799552fe9Sopenharmony_ci    std::shared_ptr<std::vector<DefaultResourceConfig>> GetResCtrlConfig(const std::string& switchName);
8899552fe9Sopenharmony_ci    const std::vector<TimerResourceConfig>& GetTimerResConfig();
8999552fe9Sopenharmony_ci    const std::vector<std::string>& GetStrategyConfigList();
9099552fe9Sopenharmony_ci    bool GetStrategyConfigList(const std::string& switchName);
9199552fe9Sopenharmony_ci    void UpdateStrategyList();
9299552fe9Sopenharmony_ci    std::vector<int32_t> GetStandbyDurationList(const std::string& switchName);
9399552fe9Sopenharmony_ci
9499552fe9Sopenharmony_ci    std::set<TimeLtdProcess> GetEligibleAllowTimeConfig(const std::string& paramName,
9599552fe9Sopenharmony_ci        uint32_t condition, bool isAllow, bool isApp);
9699552fe9Sopenharmony_ci    std::set<std::string> GetEligiblePersistAllowConfig(const std::string& paramName,
9799552fe9Sopenharmony_ci        uint32_t condition, bool isAllow, bool isApp);
9899552fe9Sopenharmony_ci    int32_t GetMaxDuration(const std::string& name, const std::string& paramName, uint32_t condition, bool isApp);
9999552fe9Sopenharmony_ci
10099552fe9Sopenharmony_ci    std::vector<int32_t> GetStandbyLadderBatteryList(const std::string& switchName);
10199552fe9Sopenharmony_ci
10299552fe9Sopenharmony_ci    void DumpSetDebugMode(bool debugMode);
10399552fe9Sopenharmony_ci    void DumpSetSwitch(const std::string& switchName, bool switchStatus, std::string& result);
10499552fe9Sopenharmony_ci    void DumpSetParameter(const std::string& paramName, int32_t paramValue, std::string& result);
10599552fe9Sopenharmony_ci    bool NeedsToReadCloudConfig();
10699552fe9Sopenharmony_ci    /**
10799552fe9Sopenharmony_ci     * @brief dump config info
10899552fe9Sopenharmony_ci     */
10999552fe9Sopenharmony_ci    void DumpStandbyConfigInfo(std::string& result);
11099552fe9Sopenharmony_ciprivate:
11199552fe9Sopenharmony_ci    StandbyConfigManager(const StandbyConfigManager&) = delete;
11299552fe9Sopenharmony_ci    StandbyConfigManager& operator= (const StandbyConfigManager&) = delete;
11399552fe9Sopenharmony_ci    StandbyConfigManager(StandbyConfigManager&&) = delete;
11499552fe9Sopenharmony_ci    StandbyConfigManager& operator= (StandbyConfigManager&&) = delete;
11599552fe9Sopenharmony_ci    template<typename T> std::set<T> GetEligibleAllowConfig(const std::string& paramName,
11699552fe9Sopenharmony_ci        uint32_t condition, bool isAllow, bool isApp, const std::function<void(bool, std::set<T>&,
11799552fe9Sopenharmony_ci        const DefaultResourceConfig&)>& func);
11899552fe9Sopenharmony_ci    template<typename T> T
11999552fe9Sopenharmony_ci        GetConfigWithName(const std::string& switchName, std::unordered_map<std::string, T>& configMap);
12099552fe9Sopenharmony_ci
12199552fe9Sopenharmony_ci    std::vector<std::string> GetConfigFileList(const std::string& relativeConfigPath);
12299552fe9Sopenharmony_ci    bool ParseDeviceStanbyConfig(const nlohmann::json& devStandbyConfigRoot);
12399552fe9Sopenharmony_ci    bool ParseStandbyConfig(const nlohmann::json& standbyConfig);
12499552fe9Sopenharmony_ci    bool ParseIntervalList(const nlohmann::json& standbyIntervalList);
12599552fe9Sopenharmony_ci    bool ParseStrategyListConfig(const nlohmann::json& standbyListConfig);
12699552fe9Sopenharmony_ci    bool ParseHalfHourSwitchConfig(const nlohmann::json& halfHourSwitchConfig);
12799552fe9Sopenharmony_ci    bool ParseResCtrlConfig(const nlohmann::json& resCtrlConfigRoot);
12899552fe9Sopenharmony_ci    bool ParseTimerResCtrlConfig(const nlohmann::json& resConfigArray);
12999552fe9Sopenharmony_ci    bool ParseDefaultResCtrlConfig(const std::string& resCtrlKey, const nlohmann::json& resConfigArray);
13099552fe9Sopenharmony_ci    bool ParseCommonResCtrlConfig(const nlohmann::json& sigleConfigItem, DefaultResourceConfig& resCtrlConfig);
13199552fe9Sopenharmony_ci    void ParseTimeLimitedConfig(const nlohmann::json& singleConfigItem, const std::string& key,
13299552fe9Sopenharmony_ci        std::vector<TimeLtdProcess>& resCtrlConfig);
13399552fe9Sopenharmony_ci    uint32_t ParseCondition(const std::string& conditionStr);
13499552fe9Sopenharmony_ci    template<typename T> void DumpResCtrlConfig(const char* name, const std::vector<T>& configArray,
13599552fe9Sopenharmony_ci        std::stringstream& stream, const std::function<void(const T&)>& func);
13699552fe9Sopenharmony_ci    void LoadGetExtConfigFunc();
13799552fe9Sopenharmony_ci    void GetAndParseStandbyConfig();
13899552fe9Sopenharmony_ci    void GetAndParseStrategyConfig();
13999552fe9Sopenharmony_ci    void GetCloudConfig();
14099552fe9Sopenharmony_ci    void ParseCloudConfig(const nlohmann::json& devConfigRoot);
14199552fe9Sopenharmony_ci    bool GetParamVersion(const int32_t& fileIndex, std::string& version);
14299552fe9Sopenharmony_ci    bool GetCloudVersion(const int32_t& fileIndex, std::string& version);
14399552fe9Sopenharmony_ci    /**
14499552fe9Sopenharmony_ci     * @brief Get a larger verison
14599552fe9Sopenharmony_ci     *
14699552fe9Sopenharmony_ci     * @param configVerA The version that needs to be compared
14799552fe9Sopenharmony_ci     * @param configVerB The version that needs to be compared
14899552fe9Sopenharmony_ci     * @return 1 if configVerA is larger than configVerB, or configVerA equals configVerB.
14999552fe9Sopenharmony_ci     * @return 0 if configVerB is larger than configVerA.
15099552fe9Sopenharmony_ci     * @return -1 if there is an error during the comparison.
15199552fe9Sopenharmony_ci     */
15299552fe9Sopenharmony_ci    int CompareVersion(const std::string& configVerA, const std::string& configVerB);
15399552fe9Sopenharmony_ci    bool ParseVersionConfig(const nlohmann::json& standbyConfig, std::string& version);
15499552fe9Sopenharmony_ci
15599552fe9Sopenharmony_ci    bool ParseBatteryList(const nlohmann::json& standbyBatteryList);
15699552fe9Sopenharmony_ci
15799552fe9Sopenharmony_ciprivate:
15899552fe9Sopenharmony_ci    std::mutex configMutex_;
15999552fe9Sopenharmony_ci    std::string pluginName_;
16099552fe9Sopenharmony_ci    std::unordered_map<std::string, bool> standbySwitchMap_;
16199552fe9Sopenharmony_ci    std::unordered_map<std::string, int32_t> standbyParaMap_;
16299552fe9Sopenharmony_ci    std::unordered_map<std::string, bool> strategySwitchMap_;
16399552fe9Sopenharmony_ci    std::unordered_map<std::string, bool> strategyListMap_;
16499552fe9Sopenharmony_ci    std::vector<std::string> strategyList_;
16599552fe9Sopenharmony_ci    std::unordered_map<std::string, bool> halfhourSwitchMap_;
16699552fe9Sopenharmony_ci    std::unordered_map<std::string, std::shared_ptr<std::vector<DefaultResourceConfig>>> defaultResourceConfigMap_;
16799552fe9Sopenharmony_ci    std::vector<TimerResourceConfig> timerResConfigList_;
16899552fe9Sopenharmony_ci    std::unordered_map<std::string, std::vector<int32_t>> intervalListMap_;
16999552fe9Sopenharmony_ci    std::unordered_map<std::string, std::vector<int32_t>> ladderBatteryListMap_;
17099552fe9Sopenharmony_ci    std::unordered_map<std::string, nlohmann::json> standbyStrategyConfigMap_;
17199552fe9Sopenharmony_ci
17299552fe9Sopenharmony_ci    std::unordered_map<std::string, bool> backStandbySwitchMap_;
17399552fe9Sopenharmony_ci    std::unordered_map<std::string, int32_t> backStandbyParaMap_;
17499552fe9Sopenharmony_ci    GetExtConfigFunc getExtConfigFunc_ = nullptr;
17599552fe9Sopenharmony_ci    GetSingleExtConfigFunc getSingleExtConfigFunc_ = nullptr;
17699552fe9Sopenharmony_ci};
17799552fe9Sopenharmony_ci}  // namespace DevStandbyMgr
17899552fe9Sopenharmony_ci}  // namespace OHOS
17999552fe9Sopenharmony_ci#endif  // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_STANDBY_CONFIG_MANAGER_H
180