1 /* 2 * Copyright (c) 2022 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 25 #include <json/json.h> 26 #include "nocopyable.h" 27 28 namespace OHOS { 29 namespace PowerMgr { 30 class BatteryConfig : public NoCopyable { 31 public: 32 struct LightConf { 33 int32_t beginSoc; 34 int32_t endSoc; 35 uint32_t rgb; 36 }; 37 static BatteryConfig& GetInstance(); 38 bool ParseConfig(std::string configPath = ""); 39 bool IsExist(std::string key) const; 40 int32_t GetInt(std::string key, int32_t defVal = 0) const; 41 const std::vector<LightConf>& GetLightConf() const; 42 43 private: 44 bool OpenFile(std::ifstream& ifsConf, const std::string& configPath); 45 void ParseConfInner(); 46 void ParseLightConf(std::string level); 47 Json::Value FindConf(const std::string& key) const; 48 bool SplitKey(const std::string& key, std::vector<std::string>& keys) const; 49 Json::Value GetValue(std::string key) const; 50 Json::Value config_; 51 std::vector<BatteryConfig::LightConf> lightConf_; 52 static std::mutex mutex_; 53 static std::shared_ptr<BatteryConfig> instance_; 54 }; 55 } // namespace PowerMgr 56 } // namespace OHOS 57 #endif // POWERMGR_BATTERY_MANAGER_BATTERY_CONFIG_H 58