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 "thermal_hdf_config.h"
17
18 #include "thermal_log.h"
19 #include "hdf_remote_service.h"
20 #include "osal/osal_mem.h"
21
22 namespace OHOS {
23 namespace HDI {
24 namespace Thermal {
25 namespace V1_0 {
GetInsance()26 ThermalHdfConfig& ThermalHdfConfig::GetInsance()
27 {
28 static ThermalHdfConfig instance;
29 return instance;
30 }
31
ThermalHDIConfigInit(const std::string & path)32 int32_t ThermalHdfConfig::ThermalHDIConfigInit(const std::string& path)
33 {
34 if (!baseConfig_) {
35 baseConfig_ = std::make_shared<BaseInfoConfig>();
36 }
37 return ParseThermalHdiXMLConfig(path);
38 }
39
GetSensorTypeMap()40 ThermalHdfConfig::ThermalTypeMap ThermalHdfConfig::GetSensorTypeMap()
41 {
42 return typesMap_;
43 }
44
ParseThermalHdiXMLConfig(const std::string & path)45 int32_t ThermalHdfConfig::ParseThermalHdiXMLConfig(const std::string& path)
46 {
47 std::unique_ptr<xmlDoc, decltype(&xmlFreeDoc)> docPtr(
48 xmlReadFile(path.c_str(), nullptr, XML_PARSE_NOBLANKS), xmlFreeDoc);
49 if (docPtr == nullptr) {
50 THERMAL_HILOGE(COMP_HDI, "failed to read xml file");
51 return HDF_ERR_INVALID_OBJECT;
52 }
53
54 auto rootNode = xmlDocGetRootElement(docPtr.get());
55 if (rootNode == nullptr) {
56 THERMAL_HILOGE(COMP_HDI, "failed to read root node");
57 return HDF_ERR_INVALID_OBJECT;
58 }
59
60 if (!xmlStrcmp(rootNode->name, BAD_CAST"thermal")) {
61 xmlChar* xmlVersion = xmlGetProp(rootNode, BAD_CAST"version");
62 if (xmlVersion != nullptr) {
63 this->thermal_.version = std::stof(std::string(reinterpret_cast<char*>(xmlVersion)));
64 xmlFree(xmlVersion);
65 THERMAL_HILOGD(COMP_HDI, "version: %{public}s", this->thermal_.version.c_str());
66 }
67
68 xmlChar* xmlProduct = xmlGetProp(rootNode, BAD_CAST"product");
69 if (xmlProduct != nullptr) {
70 this->thermal_.product = std::string(reinterpret_cast<char*>(xmlProduct));
71 xmlFree(xmlProduct);
72 THERMAL_HILOGD(COMP_HDI, "product: %{public}s", this->thermal_.product.c_str());
73 }
74 }
75
76 for (auto node = rootNode->children; node; node = node->next) {
77 if (node == nullptr) {
78 continue;
79 }
80 if (!xmlStrcmp(node->name, BAD_CAST"base")) {
81 ParseBaseNode(node);
82 } else if (!xmlStrcmp(node->name, BAD_CAST"polling")) {
83 ParsePollingNode(node);
84 } else if (!xmlStrcmp(node->name, BAD_CAST"tracing")) {
85 ParseTracingNode(node);
86 }
87 }
88 return HDF_SUCCESS;
89 }
90
ParseBaseNode(xmlNodePtr node)91 void ThermalHdfConfig::ParseBaseNode(xmlNodePtr node)
92 {
93 auto cur = node->xmlChildrenNode;
94 std::vector<BaseItem> vBase;
95 while (cur != nullptr) {
96 BaseItem item;
97 xmlChar* xmlTag = xmlGetProp(cur, BAD_CAST"tag");
98 if (xmlTag != nullptr) {
99 item.tag = std::string(reinterpret_cast<char*>(xmlTag));
100 xmlFree(xmlTag);
101 THERMAL_HILOGD(COMP_HDI, "ParseBaseNode tag: %{public}s", item.tag.c_str());
102 }
103
104 xmlChar* xmlValue = xmlGetProp(cur, BAD_CAST"value");
105 if (xmlValue != nullptr) {
106 item.value = std::string(reinterpret_cast<char*>(xmlValue));
107 xmlFree(xmlValue);
108 THERMAL_HILOGD(COMP_HDI, "ParseBaseNode value: %{public}s", item.value.c_str());
109 }
110
111 vBase.push_back(item);
112 cur = cur->next;
113 }
114 baseConfig_->SetBase(vBase);
115 }
116
ParsePollingNode(xmlNodePtr node)117 void ThermalHdfConfig::ParsePollingNode(xmlNodePtr node)
118 {
119 auto cur = node->xmlChildrenNode;
120 while (cur != nullptr) {
121 std::shared_ptr<SensorInfoConfig> sensorInfo = std::make_shared<SensorInfoConfig>();
122 std::string groupName;
123 xmlChar* xmlName = xmlGetProp(cur, BAD_CAST"name");
124 if (xmlName != nullptr) {
125 groupName = std::string(reinterpret_cast<char*>(xmlName));
126 xmlFree(xmlName);
127 sensorInfo->SetGroupName(groupName);
128 THERMAL_HILOGD(COMP_HDI, "ParsePollingNode groupName: %{public}s", groupName.c_str());
129 }
130
131 xmlChar* xmlInterval = xmlGetProp(cur, BAD_CAST"interval");
132 if (xmlInterval != nullptr) {
133 uint32_t interval = atoi(reinterpret_cast<char*>(xmlInterval));
134 xmlFree(xmlInterval);
135 THERMAL_HILOGD(COMP_HDI, "ParsePollingNode interval: %{public}d", interval);
136 sensorInfo->SetGroupInterval(interval);
137 }
138
139 std::vector<XMLThermalZoneInfo> xmlTzInfoList;
140 std::vector<XMLThermalNodeInfo> xmlTnInfoList;
141 for (auto subNode = cur->children; subNode; subNode = subNode->next) {
142 if (!xmlStrcmp(subNode->name, BAD_CAST"thermal_zone")) {
143 XMLThermalZoneInfo tz;
144 GetThermalZoneNodeInfo(tz, subNode);
145 THERMAL_HILOGI(COMP_HDI, "ParsePollingNode ParsePollingNodetztype: %{public}s, replace: %{public}s",
146 tz.type.c_str(), tz.replace.c_str());
147 xmlTzInfoList.push_back(tz);
148 } else if (!xmlStrcmp(subNode->name, BAD_CAST"thermal_node")) {
149 XMLThermalNodeInfo tn;
150 ParsePollingSubNode(subNode, tn);
151 THERMAL_HILOGI(COMP_HDI, "ParsePollingNode tntype: %{public}s", tn.type.c_str());
152 xmlTnInfoList.push_back(tn);
153 }
154 }
155 sensorInfo->SetXMLThermalZoneInfo(xmlTzInfoList);
156 sensorInfo->SetXMLThermalNodeInfo(xmlTnInfoList);
157 typesMap_.insert(std::make_pair(groupName, sensorInfo));
158 cur = cur->next;
159 }
160 }
161
ParsePollingSubNode(xmlNodePtr node,XMLThermalNodeInfo & tn)162 void ThermalHdfConfig::ParsePollingSubNode(xmlNodePtr node, XMLThermalNodeInfo& tn)
163 {
164 DfxTraceInfo info;
165
166 xmlChar* xmlType = xmlGetProp(node, BAD_CAST"type");
167 if (xmlType != nullptr) {
168 tn.type = std::string(reinterpret_cast<char*>(xmlType));
169 xmlFree(xmlType);
170 }
171
172 xmlChar* xmlPath = xmlGetProp(node, BAD_CAST"path");
173 if (xmlPath != nullptr) {
174 tn.path = std::string(reinterpret_cast<char*>(xmlPath));
175 xmlFree(xmlPath);
176 }
177 }
178
ParseTracingNode(xmlNodePtr node)179 void ThermalHdfConfig::ParseTracingNode(xmlNodePtr node)
180 {
181 xmlChar* xmlInterval = xmlGetProp(node, BAD_CAST"interval");
182 if (xmlInterval != nullptr) {
183 this->trace_.interval = std::string(reinterpret_cast<char *>(xmlInterval));
184 xmlFree(xmlInterval);
185 THERMAL_HILOGD(COMP_HDI, "interval: %{public}s", this->trace_.interval.c_str());
186 }
187
188 xmlChar* xmlOutpath = xmlGetProp(node, BAD_CAST"outpath");
189 if (xmlOutpath != nullptr) {
190 this->trace_.outpath = std::string(reinterpret_cast<char *>(xmlOutpath));
191 xmlFree(xmlOutpath);
192 }
193
194 auto cur = node->xmlChildrenNode;
195 while (cur != nullptr) {
196 ParseTracingSubNode(cur);
197 cur = cur->next;
198 }
199 }
200
ParseTracingSubNode(xmlNodePtr node)201 void ThermalHdfConfig::ParseTracingSubNode(xmlNodePtr node)
202 {
203 std::string namePath;
204 DfxTraceInfo info;
205 std::string valuePath;
206
207 for (auto subNode = node->children; subNode != nullptr; subNode = subNode->next) {
208 if (!xmlStrcmp(subNode->name, BAD_CAST"title")) {
209 xmlChar* titlePath = xmlGetProp(subNode, BAD_CAST"path");
210 if (titlePath != nullptr) {
211 namePath = std::string(reinterpret_cast<char*>(titlePath));
212 xmlFree(titlePath);
213 }
214
215 xmlChar* titleName = xmlGetProp(subNode, BAD_CAST"name");
216 if (titleName != nullptr) {
217 namePath = std::string(reinterpret_cast<char*>(titleName));
218 xmlFree(titleName);
219 }
220 }
221
222 if (!xmlStrcmp(subNode->name, BAD_CAST"value")) {
223 xmlChar* xmlValuePath = xmlGetProp(subNode, BAD_CAST"path");
224 if (xmlValuePath != nullptr) {
225 valuePath = std::string(reinterpret_cast<char*>(xmlValuePath));
226 xmlFree(xmlValuePath);
227 }
228 }
229 }
230
231 info.title = namePath;
232 info.value = valuePath;
233 traceInfo_.emplace_back(info);
234
235 for (const auto& item : traceInfo_) {
236 THERMAL_HILOGD(COMP_HDI, "item.title = %{public}s, item.value = %{public}s",
237 item.title.c_str(), item.value.c_str());
238 }
239 }
240
GetThermalZoneNodeInfo(XMLThermalZoneInfo & tz,const xmlNode * node)241 void ThermalHdfConfig::GetThermalZoneNodeInfo(XMLThermalZoneInfo& tz, const xmlNode* node)
242 {
243 xmlChar* xmlType = xmlGetProp(node, BAD_CAST"type");
244 if (xmlType != nullptr) {
245 tz.type = std::string(reinterpret_cast<char*>(xmlType));
246 xmlFree(xmlType);
247 }
248
249 auto replace = xmlGetProp(node, BAD_CAST("replace"));
250 if (replace != nullptr) {
251 tz.replace = std::string(reinterpret_cast<char*>(replace));
252 tz.isReplace = true;
253 xmlFree(replace);
254 }
255 }
256 } // V1_0
257 } // Thermal
258 } // HDI
259 } // OHOS
260