• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "battery_config.h"
17 #include "string_ex.h"
18 #include "battery_log.h"
19 
20 namespace OHOS {
21 namespace HDI {
22 namespace Battery {
23 namespace V1_1 {
24 namespace {
25 constexpr const char* VENDOR_BATTERY_CONFIG_PATH = "/vendor/etc/battery/battery_config.json";
26 constexpr int32_t MAP_KEY_INDEX = 0;
27 constexpr int32_t BEGIN_SOC_INDEX = 0;
28 constexpr int32_t END_SOC_INDEX = 1;
29 constexpr int32_t RED_INDEX = 0;
30 constexpr int32_t GREEN_INDEX = 1;
31 constexpr int32_t BLUE_INDEX = 2;
32 constexpr int32_t MAX_DEPTH = 5;
33 constexpr int32_t MIN_DEPTH = 1;
34 constexpr uint32_t MOVE_LEFT_16 = 16;
35 constexpr uint32_t MOVE_LEFT_8 = 8;
36 }
37 std::shared_ptr<BatteryConfig> BatteryConfig::instance_ = nullptr;
38 std::mutex BatteryConfig::mutex_;
39 
GetInstance()40 BatteryConfig& BatteryConfig::GetInstance()
41 {
42     std::lock_guard<std::mutex> lock(mutex_);
43     if (instance_ == nullptr) {
44         instance_ = std::make_shared<BatteryConfig>();
45     }
46     return *(instance_.get());
47 }
48 
ParseConfig(std::string configPath)49 bool BatteryConfig::ParseConfig(std::string configPath)
50 {
51     Json::CharReaderBuilder readerBuilder;
52     std::ifstream ifsConf;
53 
54     if (!OpenFile(ifsConf, configPath)) {
55         return false;
56     }
57 
58     config_.clear();
59     readerBuilder["collectComments"] = false;
60     JSONCPP_STRING errs;
61 
62     if (parseFromStream(readerBuilder, ifsConf, &config_, &errs) && !config_.empty()) {
63         ParseConfInner();
64     }
65     ifsConf.close();
66     return true;
67 }
68 
IsExist(std::string key) const69 bool BatteryConfig::IsExist(std::string key) const
70 {
71     return !GetValue(key).isNull();
72 }
73 
GetInt(std::string key,int32_t defVal) const74 int32_t BatteryConfig::GetInt(std::string key, int32_t defVal) const
75 {
76     Json::Value value = GetValue(key);
77     return (value.isNull() || !value.isInt()) ? defVal : value.asInt();
78 }
79 
GetString(std::string key,std::string defVal) const80 std::string BatteryConfig::GetString(std::string key, std::string defVal) const
81 {
82     Json::Value value = GetValue(key);
83     return (value.isNull() || !value.isString()) ? defVal : value.asString();
84 }
85 
GetLightConf() const86 const std::vector<BatteryConfig::LightConf>& BatteryConfig::GetLightConf() const
87 {
88     return lightConf_;
89 }
90 
DestroyInstance()91 void BatteryConfig::DestroyInstance()
92 {
93     std::lock_guard<std::mutex> lock(mutex_);
94     instance_ = nullptr;
95 }
96 
OpenFile(std::ifstream & ifsConf,const std::string & configPath)97 bool BatteryConfig::OpenFile(std::ifstream& ifsConf, const std::string& configPath)
98 {
99     bool isOpen = false;
100     if (!configPath.empty()) {
101         ifsConf.open(configPath);
102         isOpen = ifsConf.is_open();
103         BATTERY_HILOGD(COMP_HDI, "open file is %{public}d", isOpen);
104     }
105     if (isOpen) {
106         return true;
107     }
108 
109     ifsConf.open(VENDOR_BATTERY_CONFIG_PATH);
110     isOpen = ifsConf.is_open();
111     BATTERY_HILOGI(COMP_HDI, "open then vendor battery_config.json is %{public}d", isOpen);
112     return isOpen;
113 }
114 
ParseConfInner()115 void BatteryConfig::ParseConfInner()
116 {
117     lightConf_.clear();
118     ParseLightConf("low");
119     ParseLightConf("normal");
120     ParseLightConf("high");
121     BATTERY_HILOGD(COMP_HDI, "The battery light configuration size %{public}d",
122         static_cast<int32_t>(lightConf_.size()));
123 }
124 
ParseLightConf(std::string level)125 void BatteryConfig::ParseLightConf(std::string level)
126 {
127     Json::Value soc = GetValue("light." + level + ".soc");
128     Json::Value rgb = GetValue("light." + level + ".rgb");
129     if (!soc.isArray() || !rgb.isArray()) {
130         BATTERY_HILOGW(COMP_HDI, "The battery light %{public}s configuration is invalid.", level.c_str());
131         return;
132     }
133 
134     BatteryConfig::LightConf lightConf = {
135         .beginSoc = soc[BEGIN_SOC_INDEX].asInt(),
136         .endSoc = soc[END_SOC_INDEX].asInt(),
137         .rgb = (rgb[RED_INDEX].asUInt() << MOVE_LEFT_16) |
138                (rgb[GREEN_INDEX].asUInt() << MOVE_LEFT_8) |
139                rgb[BLUE_INDEX].asUInt()
140     };
141     lightConf_.push_back(lightConf);
142 }
143 
FindConf(const std::string & key) const144 Json::Value BatteryConfig::FindConf(const std::string& key) const
145 {
146     return (config_.isObject() && config_.isMember(key)) ? config_[key] : Json::Value();
147 }
148 
SplitKey(const std::string & key,std::vector<std::string> & keys) const149 bool BatteryConfig::SplitKey(const std::string& key, std::vector<std::string>& keys) const
150 {
151     SplitStr(TrimStr(key), ".", keys);
152     return (keys.size() < MIN_DEPTH || keys.size() > MAX_DEPTH) ? false : true;
153 }
154 
GetValue(std::string key) const155 Json::Value BatteryConfig::GetValue(std::string key) const
156 {
157     std::vector<std::string> keys;
158     if (!SplitKey(key, keys)) {
159         BATTERY_HILOGW(COMP_HDI, "The key does not meet the. key=%{public}s", key.c_str());
160         return Json::Value();
161     }
162 
163     Json::Value value = FindConf(keys[MAP_KEY_INDEX]);
164     if (value.isNull()) {
165         BATTERY_HILOGW(COMP_HDI, "Value is empty. key=%{public}s", keys[MAP_KEY_INDEX].c_str());
166         return value;
167     }
168 
169     for (size_t i = 1; i < keys.size(); ++i) {
170         if (!value.isObject() || !value.isMember(keys[i])) {
171             BATTERY_HILOGW(COMP_HDI, "The key is not configured. key=%{public}s", keys[i].c_str());
172             break;
173         }
174         value = value[keys[i]];
175     }
176     return value;
177 }
178 }  // namespace V1_1
179 }  // namespace Battery
180 }  // namespace HDI
181 }  // namespace OHOS
182