• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_kernel_config_file.h"
17 #include "thermal_common.h"
18 #include "thermal_kernel_service.h"
19 
20 namespace OHOS {
21 namespace PowerMgr {
22 namespace {
23 auto &service = ThermalKernelService::GetInstance();
24 std::vector<LevelAction> g_levelActionList;
25 }
Init(const std::string & path)26 bool ThermalKernelConfigFile::Init(const std::string &path)
27 {
28     THERMAL_HILOGD(FEATURE_PROTECTOR, "Enter");
29     if (!baseInfo_) {
30         baseInfo_ = std::make_unique<ProtectorBaseInfo>();
31     }
32 
33     ParseThermalKernelXML(path);
34     return true;
35 }
36 
ParseThermalKernelXML(const std::string & path)37 void ThermalKernelConfigFile::ParseThermalKernelXML(const std::string &path)
38 {
39     THERMAL_HILOGD(FEATURE_PROTECTOR, "Enter");
40 
41     std::unique_ptr<xmlDoc, decltype(&xmlFreeDoc)> docPtr(
42         xmlReadFile(path.c_str(), nullptr, XML_PARSE_NOBLANKS), xmlFreeDoc);
43     if (docPtr == nullptr) {
44         THERMAL_HILOGE(FEATURE_PROTECTOR, "Init failed, read file failed.");
45         return;
46     }
47 
48     auto rootNode = xmlDocGetRootElement(docPtr.get());
49     if (rootNode == nullptr) {
50         THERMAL_HILOGE(FEATURE_PROTECTOR, "Get root node failed.");
51         return;
52     }
53 
54     for (auto node = rootNode->children; node; node = node->next) {
55         if (node == nullptr) continue;
56         if (!xmlStrcmp(node->name, BAD_CAST"base")) {
57             ParserBaseNode(node);
58         } else if (!xmlStrcmp(node->name, BAD_CAST"control")) {
59             ParseControlNode(node);
60         }
61     }
62 }
63 
ParserBaseNode(xmlNodePtr node)64 void ThermalKernelConfigFile::ParserBaseNode(xmlNodePtr node)
65 {
66     auto cur = node->xmlChildrenNode;
67     std::vector<BaseItem> vBase;
68     while (cur != nullptr) {
69         BaseItem baseItem;
70         xmlChar* xmlTag = xmlGetProp(cur, BAD_CAST"tag");
71         xmlChar* xmlValue = xmlGetProp(cur, BAD_CAST"value");
72         if (xmlTag != nullptr) {
73             baseItem.tag = (char *)xmlTag;
74             xmlFree(xmlTag);
75         }
76         if (xmlValue != nullptr) {
77             baseItem.value = (char *)xmlValue;
78             xmlFree(xmlValue);
79         }
80         vBase.push_back(baseItem);
81         cur = cur->next;
82     }
83     baseInfo_->SetBaseItem(vBase);
84 }
85 
ParseControlNode(xmlNodePtr node)86 void ThermalKernelConfigFile::ParseControlNode(xmlNodePtr node)
87 {
88     auto cur = node->xmlChildrenNode;
89     ThermalKernelPolicy::ThermalZoneMap tzInfoMap;
90     while (cur != nullptr) {
91         LevelAction levelAction;
92         std::string type;
93         std::shared_ptr<ProtectorThermalZoneInfo> tzinfo = std::make_shared<ProtectorThermalZoneInfo>();
94         xmlChar* xmlType = xmlGetProp(cur, BAD_CAST"type");
95         if (xmlType != nullptr) {
96             type = (char *)xmlType;
97             xmlFree(xmlType);
98         }
99         xmlChar* xmlInterval = xmlGetProp(cur, BAD_CAST"interval");
100         if (xmlInterval != nullptr) {
101             int32_t interval = atoi((char *)xmlInterval);
102             tzinfo->SetInterval(interval);
103             xmlFree(xmlInterval);
104         }
105 
106         xmlChar* desc = xmlGetProp(cur, BAD_CAST"desc");
107         if (desc != nullptr) {
108             std::string value = (char *)desc;
109             if (atoi(value.c_str()) == 1) {
110                 tzinfo->SetDesc(true);
111             }
112             xmlFree(desc);
113         }
114 
115         std::vector<ThermalZoneInfoItem> tzItemList;
116         ParseSubNode(cur, tzItemList, type);
117 
118         tzinfo->SetThermalZoneItem(tzItemList);
119         tzInfoMap.emplace(std::pair(type, tzinfo));
120         cur = cur->next;
121     }
122     service.GetPolicy()->SetThermalZoneMap(tzInfoMap);
123     service.GetPolicy()->SetLevelAction(g_levelActionList);
124 }
125 
ParseSubNode(xmlNodePtr cur,std::vector<ThermalZoneInfoItem> & tzItemList,std::string & type)126 void ThermalKernelConfigFile::ParseSubNode(xmlNodePtr cur, std::vector<ThermalZoneInfoItem>& tzItemList,
127     std::string& type)
128 {
129     THERMAL_HILOGD(FEATURE_PROTECTOR, "Enter");
130     LevelAction levelAction;
131     levelAction.name = type;
132 
133     for (auto subNode = cur->children; subNode; subNode = subNode->next) {
134         if (subNode == nullptr) {
135             continue;
136         }
137         if (xmlStrcmp(subNode->name, BAD_CAST"item") != 0) {
138             continue;
139         }
140 
141         ThermalZoneInfoItem tziItem;
142         xmlChar* xmlThreshold = xmlGetProp(subNode, BAD_CAST"threshold");
143         if (xmlThreshold != nullptr) {
144             tziItem.threshold= atoi((char*)xmlThreshold);
145             xmlFree(xmlThreshold);
146         }
147         xmlChar* xmlThresholdClr = xmlGetProp(subNode, BAD_CAST"threshold_clr");
148         if (xmlThresholdClr != nullptr) {
149             tziItem.thresholdClr = atoi((char*)xmlThresholdClr);
150             xmlFree(xmlThresholdClr);
151         }
152         xmlChar* xmlLevel = xmlGetProp(subNode, BAD_CAST"level");
153         if (xmlLevel != nullptr) {
154             tziItem.level = static_cast<uint32_t>(atoi((char*)xmlLevel));
155             levelAction.level = tziItem.level;
156             xmlFree(xmlLevel);
157         }
158         for (auto subActionNode = subNode->children; subActionNode; subActionNode = subActionNode->next) {
159             ActionItem action;
160             action.name = (char *)subActionNode->name;
161             xmlChar* xmlValue = xmlNodeGetContent(subActionNode);
162             if (xmlValue != nullptr) {
163                 action.value = static_cast<uint32_t>(atoi((char *)xmlValue));
164                 xmlFree(xmlValue);
165             }
166             levelAction.vAction.push_back(action);
167         }
168         tzItemList.push_back(tziItem);
169         g_levelActionList.push_back(levelAction);
170     }
171 }
172 } // namespace PowerMgr
173 } // namespace OHOS
174