• 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 #include "battery_config.h"
17 
18 #include "battery_log.h"
19 
20 #include "string_ex.h"
21 #include "config_policy_utils.h"
22 
23 namespace OHOS {
24 namespace HDI {
25 namespace Battery {
26 namespace V1_2 {
27 namespace {
28 constexpr const char* BATTERY_CONFIG_PATH = "etc/battery/battery_config.json";
29 constexpr const char* SYSTEM_BATTERY_CONFIG_PATH = "/system/etc/battery/battery_config.json";
30 constexpr const char* VENDOR_BATTERY_CONFIG_PATH = "/vendor/etc/battery/battery_config.json";
31 constexpr const char* BATTERY_CONFIG_EXCEPTION_PATH = "";
32 constexpr int32_t MAP_KEY_INDEX = 0;
33 constexpr int32_t BEGIN_SOC_INDEX = 0;
34 constexpr int32_t END_SOC_INDEX = 1;
35 constexpr int32_t MAX_SOC_RANGE = 2;
36 constexpr int32_t RED_INDEX = 0;
37 constexpr int32_t GREEN_INDEX = 1;
38 constexpr int32_t BLUE_INDEX = 2;
39 constexpr int32_t MAX_RGB_RANGE = 3;
40 constexpr int32_t MAX_DEPTH = 5;
41 constexpr int32_t MIN_DEPTH = 1;
42 constexpr uint32_t MOVE_LEFT_16 = 16;
43 constexpr uint32_t MOVE_LEFT_8 = 8;
44 }
45 std::shared_ptr<BatteryConfig> BatteryConfig::instance_ = nullptr;
46 std::mutex BatteryConfig::mutex_;
47 
GetInstance()48 BatteryConfig& BatteryConfig::GetInstance()
49 {
50     std::lock_guard<std::mutex> lock(mutex_);
51     if (instance_ == nullptr) {
52         instance_ = std::make_shared<BatteryConfig>();
53     }
54     return *(instance_.get());
55 }
56 
ParseConfig()57 bool BatteryConfig::ParseConfig()
58 {
59     char buf[MAX_PATH_LEN];
60     char* path = GetOneCfgFile(BATTERY_CONFIG_PATH, buf, MAX_PATH_LEN);
61     if (path == nullptr || *path == '\0') {
62         BATTERY_HILOGW(COMP_HDI, "GetOneCfgFile battery_config.json is NULL");
63         path = const_cast<char*>(BATTERY_CONFIG_EXCEPTION_PATH);
64     }
65     BATTERY_HILOGD(COMP_HDI, "GetOneCfgFile battery_config.json");
66 
67     Json::CharReaderBuilder readerBuilder;
68     std::ifstream ifsConf;
69 
70     if (!OpenFile(ifsConf, path)) {
71         return false;
72     }
73 
74     config_.clear();
75     readerBuilder["collectComments"] = false;
76     JSONCPP_STRING errs;
77 
78     if (parseFromStream(readerBuilder, ifsConf, &config_, &errs) && !config_.empty()) {
79         ParseConfInner();
80     }
81     ifsConf.close();
82     return true;
83 }
84 
IsExist(std::string key) const85 bool BatteryConfig::IsExist(std::string key) const
86 {
87     return !GetValue(key).isNull();
88 }
89 
GetInt(std::string key,int32_t defVal) const90 int32_t BatteryConfig::GetInt(std::string key, int32_t defVal) const
91 {
92     Json::Value value = GetValue(key);
93     return (value.isNull() || !value.isInt()) ? defVal : value.asInt();
94 }
95 
GetString(std::string key,std::string defVal) const96 std::string BatteryConfig::GetString(std::string key, std::string defVal) const
97 {
98     Json::Value value = GetValue(key);
99     return (value.isNull() || !value.isString()) ? defVal : value.asString();
100 }
101 
GetLightConf() const102 const std::vector<BatteryConfig::LightConf>& BatteryConfig::GetLightConf() const
103 {
104     return lightConf_;
105 }
106 
DestroyInstance()107 void BatteryConfig::DestroyInstance()
108 {
109     std::lock_guard<std::mutex> lock(mutex_);
110     instance_ = nullptr;
111 }
112 
OpenFile(std::ifstream & ifsConf,const std::string & configPath)113 bool BatteryConfig::OpenFile(std::ifstream& ifsConf, const std::string& configPath)
114 {
115     bool isOpen = false;
116     if (!configPath.empty()) {
117         ifsConf.open(configPath);
118         isOpen = ifsConf.is_open();
119         BATTERY_HILOGD(COMP_HDI, "open file is %{public}d", isOpen);
120     }
121     if (isOpen) {
122         return true;
123     }
124 
125     ifsConf.open(VENDOR_BATTERY_CONFIG_PATH);
126     isOpen = ifsConf.is_open();
127     BATTERY_HILOGI(COMP_HDI, "open then vendor battery_config.json is %{public}d", isOpen);
128 
129     if (isOpen) {
130         return true;
131     }
132 
133     ifsConf.open(SYSTEM_BATTERY_CONFIG_PATH);
134     isOpen = ifsConf.is_open();
135     BATTERY_HILOGI(FEATURE_CHARGING, "open then system battery_config.json is %{public}d", isOpen);
136     return isOpen;
137 }
138 
ParseConfInner()139 void BatteryConfig::ParseConfInner()
140 {
141     lightConf_.clear();
142     ParseLightConf("low");
143     ParseLightConf("normal");
144     ParseLightConf("high");
145     BATTERY_HILOGD(COMP_HDI, "The battery light configuration size %{public}d",
146         static_cast<int32_t>(lightConf_.size()));
147 }
148 
ParseLightConf(std::string level)149 void BatteryConfig::ParseLightConf(std::string level)
150 {
151     Json::Value soc = GetValue("light." + level + ".soc");
152     Json::Value rgb = GetValue("light." + level + ".rgb");
153     if (!soc.isArray() || !rgb.isArray()) {
154         BATTERY_HILOGW(COMP_HDI, "The battery light %{public}s configuration is invalid.", level.c_str());
155         return;
156     }
157 
158     if (soc.size() != MAX_SOC_RANGE || !soc[BEGIN_SOC_INDEX].isInt() || !soc[END_SOC_INDEX].isInt()) {
159         BATTERY_HILOGW(COMP_HDI, "The battery light %{public}s soc data type error.", level.c_str());
160         return;
161     }
162     if (rgb.size() != MAX_RGB_RANGE || !rgb[RED_INDEX].isUInt() || !rgb[GREEN_INDEX].isUInt() ||
163         !rgb[BLUE_INDEX].isUInt()) {
164         BATTERY_HILOGW(COMP_HDI, "The battery light %{public}s rgb data type error.", level.c_str());
165         return;
166     }
167     BatteryConfig::LightConf lightConf = {
168         .beginSoc = soc[BEGIN_SOC_INDEX].asInt(),
169         .endSoc = soc[END_SOC_INDEX].asInt(),
170         .rgb = (rgb[RED_INDEX].asUInt() << MOVE_LEFT_16) |
171                (rgb[GREEN_INDEX].asUInt() << MOVE_LEFT_8) |
172                rgb[BLUE_INDEX].asUInt()
173     };
174     lightConf_.push_back(lightConf);
175 }
176 
FindConf(const std::string & key) const177 Json::Value BatteryConfig::FindConf(const std::string& key) const
178 {
179     return (config_.isObject() && config_.isMember(key)) ? config_[key] : Json::Value();
180 }
181 
SplitKey(const std::string & key,std::vector<std::string> & keys) const182 bool BatteryConfig::SplitKey(const std::string& key, std::vector<std::string>& keys) const
183 {
184     SplitStr(TrimStr(key), ".", keys);
185     return (keys.size() < MIN_DEPTH || keys.size() > MAX_DEPTH) ? false : true;
186 }
187 
GetValue(std::string key) const188 Json::Value BatteryConfig::GetValue(std::string key) const
189 {
190     std::vector<std::string> keys;
191     if (!SplitKey(key, keys)) {
192         BATTERY_HILOGW(COMP_HDI, "The key does not meet the. key=%{public}s", key.c_str());
193         return Json::Value();
194     }
195 
196     Json::Value value = FindConf(keys[MAP_KEY_INDEX]);
197     if (value.isNull()) {
198         BATTERY_HILOGW(COMP_HDI, "Value is empty. key=%{public}s", keys[MAP_KEY_INDEX].c_str());
199         return value;
200     }
201 
202     for (size_t i = 1; i < keys.size(); ++i) {
203         if (!value.isObject() || !value.isMember(keys[i])) {
204             BATTERY_HILOGW(COMP_HDI, "The key is not configured. key=%{public}s", keys[i].c_str());
205             break;
206         }
207         value = value[keys[i]];
208     }
209     return value;
210 }
211 }  // namespace V1_2
212 }  // namespace Battery
213 }  // namespace HDI
214 }  // namespace OHOS
215