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