• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 POWERMGR_BATTERY_MANAGER_BATTERY_CONFIG_H
17 #define POWERMGR_BATTERY_MANAGER_BATTERY_CONFIG_H
18 
19 #include <fstream>
20 #include <memory>
21 #include <mutex>
22 #include <vector>
23 #include <string>
24 #include <unordered_map>
25 
26 #include <json/json.h>
27 #include "nocopyable.h"
28 
29 namespace OHOS {
30 namespace PowerMgr {
31 class BatteryConfig : public NoCopyable {
32 public:
33     struct LightConf {
34         int32_t beginSoc;
35         int32_t endSoc;
36         uint32_t rgb;
37     };
38     struct CommonEventConf {
39         std::string eventName;
40         std::string uevent;
41         std::string sceneConfigName;
42         bool sceneConfigEqual;
43         std::string sceneConfigValue;
44     };
45     struct PopupConf {
46         std::string name;
47         int32_t action;
48     };
49     struct NotificationConf {
50         std::string name;
51         std::string icon;
52         std::string title;
53         std::string text;
54         std::pair<std::string, std::string> firstButton;
55         std::pair<std::string, std::string> secondButton;
GetInfoNotificationConf56         const std::string GetInfo() const
57         {
58             return "name: " + name + ", icon: " + icon + ", title: " + title + ", text: " + text +
59                 ", firstButton: " + firstButton.first + ", " + firstButton.second +
60                 ", secondButton: " + secondButton.first + ", " + secondButton.second;
61         }
62     };
63     static BatteryConfig& GetInstance();
64     bool ParseConfig();
65     bool IsExist(std::string key) const;
66     int32_t GetInt(std::string key, int32_t defVal = 0) const;
67     const std::vector<LightConf>& GetLightConf() const;
68     bool GetWirelessChargerConf() const;
69     const std::vector<BatteryConfig::CommonEventConf>& GetCommonEventConf() const;
70     const std::unordered_map<std::string, std::vector<BatteryConfig::PopupConf>>& GetPopupConf() const;
71     const std::unordered_map<std::string, BatteryConfig::NotificationConf>& GetNotificationConf() const;
72 
73 private:
74     bool OpenFile(std::ifstream& ifsConf, const std::string& configPath);
75     void ParseConfInner();
76     void ParseLightConf(std::string level);
77     void ParseWirelessChargerConf();
78     void ParseBootActionsConf();
79     void ParsePopupConf();
80     void ParseNotificationConf();
81     void ParseCommonEventConf(const Json::Value &bootActionsConfig);
82     Json::Value FindConf(const std::string& key) const;
83     bool SplitKey(const std::string& key, std::vector<std::string>& keys) const;
84     Json::Value GetValue(std::string key) const;
85     Json::Value config_;
86     std::vector<BatteryConfig::LightConf> lightConf_;
87     std::vector<BatteryConfig::CommonEventConf> commonEventConf_;
88     bool wirelessChargerEnable_ { false };
89     std::unordered_map<std::string, std::vector<BatteryConfig::PopupConf>> popupConfig_;
90     std::unordered_map<std::string, BatteryConfig::NotificationConf> notificationConfMap_;
91     static std::mutex mutex_;
92     static std::shared_ptr<BatteryConfig> instance_;
93 };
94 } // namespace PowerMgr
95 } // namespace OHOS
96 #endif // POWERMGR_BATTERY_MANAGER_BATTERY_CONFIG_H
97