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 BATTERY_CONFIG_H 17 #define 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 HDI { 30 namespace Battery { 31 namespace V1_2 { 32 class BatteryConfig : public NoCopyable { 33 public: 34 struct LightConf { 35 int32_t beginSoc; 36 int32_t endSoc; 37 uint32_t rgb; 38 }; 39 static BatteryConfig& GetInstance(); 40 static void DestroyInstance(); 41 bool ParseConfig(); 42 bool IsExist(std::string key) const; 43 int32_t GetInt(std::string key, int32_t defVal = 0) const; 44 std::string GetString(std::string key, std::string defVal = "") const; 45 const std::vector<LightConf>& GetLightConf() const; 46 47 private: 48 bool OpenFile(std::ifstream& ifsConf, const std::string& configPath); 49 void ParseConfInner(); 50 void ParseLightConf(std::string level); 51 Json::Value FindConf(const std::string& key) const; 52 bool SplitKey(const std::string& key, std::vector<std::string>& keys) const; 53 Json::Value GetValue(std::string key) const; 54 Json::Value config_; 55 std::vector<BatteryConfig::LightConf> lightConf_; 56 static std::mutex mutex_; 57 static std::shared_ptr<BatteryConfig> instance_; 58 }; 59 } // namespace V1_2 60 } // namespace Battery 61 } // namespace HDI 62 } // namespace OHOS 63 #endif 64