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 this->thermal_.version = std::stof((char *)xmlGetProp(rootNode, BAD_CAST"version"));
62 this->thermal_.product = (char *)xmlGetProp(rootNode, BAD_CAST"product");
63 THERMAL_HILOGI(COMP_HDI, "version: %{public}s, product: %{public}s",
64 this->thermal_.version.c_str(), this->thermal_.product.c_str());
65 }
66
67 for (auto node = rootNode->children; node; node = node->next) {
68 if (node == nullptr) {
69 continue;
70 }
71 if (!xmlStrcmp(node->name, BAD_CAST"base")) {
72 ParseBaseNode(node);
73 } else if (!xmlStrcmp(node->name, BAD_CAST"polling")) {
74 ParsePollingNode(node);
75 }
76 }
77 return HDF_SUCCESS;
78 }
79
ParseBaseNode(xmlNodePtr node)80 void ThermalHdfConfig::ParseBaseNode(xmlNodePtr node)
81 {
82 auto cur = node->xmlChildrenNode;
83 std::vector<BaseItem> vBase;
84 while (cur != nullptr) {
85 BaseItem item;
86 item.tag = (char *)xmlGetProp(cur, BAD_CAST"tag");
87 item.value = (char *)xmlGetProp(cur, BAD_CAST"value");
88 THERMAL_HILOGI(COMP_HDI, "ParseBaseNode tag: %{public}s, value: %{public}s",
89 item.tag.c_str(), item.value.c_str());
90 vBase.push_back(item);
91 cur = cur->next;
92 }
93 baseConfig_->SetBase(vBase);
94 }
95
ParsePollingNode(xmlNodePtr node)96 void ThermalHdfConfig::ParsePollingNode(xmlNodePtr node)
97 {
98 auto cur = node->xmlChildrenNode;
99 while (cur != nullptr) {
100 std::shared_ptr<SensorInfoConfig> sensorInfo = std::make_shared<SensorInfoConfig>();
101 std::string groupName = (char*)xmlGetProp(cur, BAD_CAST"name");
102 sensorInfo->SetGroupName(groupName);
103 uint32_t interval = atoi((char*)xmlGetProp(cur, BAD_CAST"interval"));
104 THERMAL_HILOGI(COMP_HDI, "ParsePollingNode groupName: %{public}s, interval: %{public}d",
105 groupName.c_str(), interval);
106 sensorInfo->SetGroupInterval(interval);
107 std::vector<XMLThermalZoneInfo> xmlTzInfoList;
108 std::vector<XMLThermalNodeInfo> xmlTnInfoList;
109 for (auto subNode = cur->children; subNode; subNode = subNode->next) {
110 if (!xmlStrcmp(subNode->name, BAD_CAST"thermal_zone")) {
111 XMLThermalZoneInfo tz;
112 GetThermalZoneNodeInfo(tz, subNode);
113 THERMAL_HILOGI(COMP_HDI, "ParsePollingNode ParsePollingNodetztype: %{public}s, replace: %{public}s",
114 tz.type.c_str(), tz.replace.c_str());
115 xmlTzInfoList.push_back(tz);
116 } else if (!xmlStrcmp(subNode->name, BAD_CAST"thermal_node")) {
117 XMLThermalNodeInfo tn;
118 tn.type = (char *)xmlGetProp(subNode, BAD_CAST"type");
119 tn.path = (char *)xmlGetProp(subNode, BAD_CAST"path");
120 THERMAL_HILOGI(COMP_HDI, "ParsePollingNode tntype: %{public}s, path: %{public}s",
121 tn.type.c_str(), tn.path.c_str());
122 xmlTnInfoList.push_back(tn);
123 }
124 }
125 sensorInfo->SetXMLThermalZoneInfo(xmlTzInfoList);
126 sensorInfo->SetXMLThermalNodeInfo(xmlTnInfoList);
127 typesMap_.insert(std::make_pair(groupName, sensorInfo));
128 cur = cur->next;
129 }
130 }
131
GetThermalZoneNodeInfo(XMLThermalZoneInfo & tz,const xmlNode * node)132 void ThermalHdfConfig::GetThermalZoneNodeInfo(XMLThermalZoneInfo &tz, const xmlNode* node)
133 {
134 tz.type = (char *)xmlGetProp(node, BAD_CAST"type");
135 auto replace = xmlGetProp(node, BAD_CAST("replace"));
136 if (replace != nullptr) {
137 tz.replace = (char *)replace;
138 tz.isReplace = true;
139 }
140 }
141 } // V1_0
142 } // Thermal
143 } // HDI
144 } // OHOS