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 "hdf_base.h"
18 #include "hdf_log.h"
19
20 #define HDF_LOG_TAG BatteryConfig
21
22 namespace OHOS {
23 namespace HDI {
24 namespace Battery {
25 namespace V1_0 {
26 const std::string CONFIG_FILE = "/system/etc/ledconfig/led_config.json";
27 const int DEFAULT_CAPACITY_CONF = 3;
28 const int DEFAULT_UPPER_TEMP_CONF = 600;
29 const int DEFAULT_LOWER_TEMP_CONF = -100;
30 const int DEFAULT_CAPACITY_BEGIN_CONF = 0;
31 const int DEFAULT_CAPACITY_END_CONF = 100;
32 const int DEFAULT_LED_COLOR_CONF = 4;
33 const int DEFAULT_BRIGHTNESS_CONF = 255;
34
Init()35 int32_t BatteryConfig::Init()
36 {
37 return ParseConfig(CONFIG_FILE);
38 }
39
GetLedConf()40 std::vector<BatteryConfig::LedConf> BatteryConfig::GetLedConf()
41 {
42 return ledConf_;
43 }
44
GetTempConf()45 BatteryConfig::TempConf BatteryConfig::GetTempConf()
46 {
47 return tempConf_;
48 }
49
GetCapacityConf()50 int BatteryConfig::GetCapacityConf()
51 {
52 return capacityConf_;
53 }
54
ParseLedConf(Json::Value & root)55 int32_t BatteryConfig::ParseLedConf(Json::Value& root)
56 {
57 struct LedConf ledConf;
58 int size = root["led"]["table"].size();
59 if (size == 0) {
60 HDF_LOGW("%{public}s: read json file fail.", __func__);
61 ledConf.capacityBegin = DEFAULT_CAPACITY_BEGIN_CONF;
62 ledConf.capacityEnd = DEFAULT_CAPACITY_END_CONF;
63 ledConf.color = DEFAULT_LED_COLOR_CONF;
64 ledConf.brightness = DEFAULT_BRIGHTNESS_CONF;
65 ledConf_.emplace_back(ledConf);
66 return HDF_ERR_INVALID_OBJECT;
67 }
68 ledConf_.clear();
69
70 for (int i = 0; i < size; ++i) {
71 ledConf.capacityBegin = root["led"]["table"][i][INDEX_ZERO].asInt();
72 ledConf.capacityEnd = root["led"]["table"][i][INDEX_ONE].asInt();
73 ledConf.color = root["led"]["table"][i][INDEX_TWO].asInt();
74 ledConf.brightness = root["led"]["table"][i][INDEX_THREE].asInt();
75 ledConf_.emplace_back(ledConf);
76 }
77 return HDF_SUCCESS;
78 }
79
ParseTempConf(Json::Value & root)80 int32_t BatteryConfig::ParseTempConf(Json::Value& root)
81 {
82 int size = root["temperature"]["table"].size();
83 if (size == 0) {
84 HDF_LOGW("%{public}s parse temperature config file fail.", __func__);
85 tempConf_.lower = DEFAULT_LOWER_TEMP_CONF;
86 tempConf_.upper = DEFAULT_UPPER_TEMP_CONF;
87 return HDF_ERR_INVALID_OBJECT;
88 }
89
90 tempConf_.lower = root["temperature"]["table"][INDEX_ZERO].asInt();
91 tempConf_.upper = root["temperature"]["table"][INDEX_ONE].asInt();
92 return HDF_SUCCESS;
93 }
94
ParseCapacityConf(Json::Value & root)95 int32_t BatteryConfig::ParseCapacityConf(Json::Value& root)
96 {
97 int size = root["soc"]["table"].size();
98 if (size == 0) {
99 HDF_LOGW("%{public}s parse capacity config file fail.", __func__);
100 capacityConf_ = DEFAULT_CAPACITY_CONF;
101 return HDF_ERR_INVALID_OBJECT;
102 }
103
104 capacityConf_ = root["soc"]["table"][INDEX_ZERO].asInt();
105 return HDF_SUCCESS;
106 }
107
ParseConfig(const std::string filename)108 int32_t BatteryConfig::ParseConfig(const std::string filename)
109 {
110 Json::Value root;
111 Json::CharReaderBuilder readerBuilder;
112
113 std::ifstream ledConfig;
114 ledConfig.open(filename);
115
116 root.clear();
117 readerBuilder["collectComments"] = false;
118 JSONCPP_STRING errs;
119
120 if (parseFromStream(readerBuilder, ledConfig, &root, &errs)) {
121 int32_t ret = ParseLedConf(root);
122 if (ret != HDF_SUCCESS) {
123 HDF_LOGI("%{public}s: parse led config fail.", __func__);
124 }
125
126 ret = ParseTempConf(root);
127 if (ret != HDF_SUCCESS) {
128 HDF_LOGI("%{public}s: parse temperature config fail.", __func__);
129 }
130
131 ret = ParseCapacityConf(root);
132 if (ret != HDF_SUCCESS) {
133 HDF_LOGI("%{public}s: parse soc config fail.", __func__);
134 }
135 }
136 ledConfig.close();
137 return HDF_SUCCESS;
138 }
139 } // namespace V1_0
140 } // namespace Battery
141 } // namespace HDI
142 } // namespace OHOS
143