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 #include <map> 25 26 #include <json/json.h> 27 #include "nocopyable.h" 28 29 namespace OHOS { 30 namespace HDI { 31 namespace Battery { 32 namespace V2_0 { 33 class BatteryConfig : public NoCopyable { 34 public: 35 struct LightConfig { 36 int32_t beginSoc; 37 int32_t endSoc; 38 uint32_t rgb; 39 }; 40 41 struct ChargerConfig { 42 std::string currentPath; 43 std::string voltagePath; 44 std::string chargeTypePath; 45 }; 46 47 struct ChargeSceneConfig { 48 std::string supportPath; 49 std::string type; 50 std::string expectValue; 51 std::string getPath; 52 std::string setPath; 53 }; 54 static BatteryConfig& GetInstance(); 55 static void DestroyInstance(); 56 bool ParseConfig(); 57 const std::vector<LightConfig>& GetLightConfig() const; 58 const BatteryConfig::ChargerConfig& GetChargerConfig() const; 59 const std::map<std::string, BatteryConfig::ChargeSceneConfig>& GetChargeSceneConfigMap() const; 60 const std::map<std::string, std::vector<std::string>>& GetUeventList() const; 61 62 private: 63 bool OpenFile(std::ifstream& ifsConf, const std::string& configPath); 64 void ParseConfInner(const Json::Value& config); 65 void ParseLightConfig(const Json::Value& lightConfig); 66 void ParseChargeSceneConfig(const Json::Value& chargeSceneConfig); 67 void ParseChargerConfig(const Json::Value& chargerConfig); 68 void ParseUeventConfig(const Json::Value& ueventConfig); 69 bool SplitKey(const std::string& key, std::vector<std::string>& keys) const; 70 Json::Value GetValue(const Json::Value& config, std::string key) const; 71 bool isValidJsonString(const Json::Value& config) const; 72 std::vector<BatteryConfig::LightConfig> lightConfig_; 73 BatteryConfig::ChargerConfig chargerConfig_; 74 std::map<std::string, BatteryConfig::ChargeSceneConfig> chargeSceneConfigMap_; 75 static std::mutex mutex_; 76 static std::shared_ptr<BatteryConfig> instance_; 77 std::map<std::string, std::vector<std::string>> ueventMap_; 78 }; 79 } // namespace V2_0 80 } // namespace Battery 81 } // namespace HDI 82 } // namespace OHOS 83 #endif 84