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