1 /*
2 * Copyright (c) 2021-2023 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 "power_save_mode.h"
17
18 #include "libxml/parser.h"
19
20 #include "config_policy_utils.h"
21 #include "power_log.h"
22 #include "string_ex.h"
23
24 namespace OHOS {
25 namespace PowerMgr {
26 namespace {
27 const std::string TAG_ROOT = "switch_policy";
28 const std::string POWER_MODE_CONFIG_PATH = "etc/power_config/power_mode_config.xml";
29 const std::string VENDOR_POWER_MODE_CONFIG_PATH = "/vendor/etc/power_config/power_mode_config.xml";
30 const std::string SYSTEM_POWER_MODE_CONFIG_PATH = "/system/etc/power_config/power_mode_config.xml";
31 constexpr uint32_t SLEEP_FILTER = SLEEP_FILTER_VALUE;
32 }
33
PowerSaveMode()34 PowerSaveMode::PowerSaveMode()
35 {
36 POWER_HILOGD(FEATURE_POWER_MODE, "Start to parse power_mode_config.xml");
37
38 char buf[MAX_PATH_LEN];
39 char* path = GetOneCfgFile(POWER_MODE_CONFIG_PATH.c_str(), buf, MAX_PATH_LEN);
40 if (path != nullptr && *path != '\0') {
41 if (!StartXMlParse(path)) {
42 POWER_HILOGE(FEATURE_POWER_MODE, "policy config file power_mode_config.xml err");
43 }
44 return;
45 }
46
47 if (!StartXMlParse(VENDOR_POWER_MODE_CONFIG_PATH)) {
48 POWER_HILOGI(FEATURE_POWER_MODE, "No vendor power_mode_config.xml, start to parse system config");
49 StartXMlParse(SYSTEM_POWER_MODE_CONFIG_PATH);
50 }
51 }
52
IsNodeLegal(const xmlNodePtr nodePtr,const std::string & tagName)53 bool IsNodeLegal(const xmlNodePtr nodePtr, const std::string& tagName)
54 {
55 return nodePtr != nullptr && nodePtr->type != XML_COMMENT_NODE && nodePtr->name != nullptr &&
56 xmlStrEqual(nodePtr->name, BAD_CAST(tagName.c_str())) == 0;
57 }
58
StartXMlParse(std::string path)59 bool PowerSaveMode::StartXMlParse(std::string path)
60 {
61 std::unique_ptr<xmlDoc, decltype(&xmlFreeDoc)> docPtr(
62 xmlReadFile(path.c_str(), nullptr, XML_PARSE_NOBLANKS), xmlFreeDoc);
63 if (docPtr == nullptr) {
64 POWER_HILOGE(FEATURE_POWER_MODE, "Parse failed, read file failed.");
65 return false;
66 }
67
68 auto rootPtr = xmlDocGetRootElement(docPtr.get());
69 if (!IsNodeLegal(rootPtr, TAG_ROOT)) {
70 POWER_HILOGE(FEATURE_POWER_MODE, "Parse failed, root node is illegal.");
71 return false;
72 }
73
74 for (auto nodePtr = rootPtr->xmlChildrenNode; nodePtr != nullptr; nodePtr = nodePtr->next) {
75 int32_t policyId = 0;
76 StrToInt(TrimStr(GetProp(nodePtr, "id")), policyId);
77 POWER_HILOGD(FEATURE_POWER_MODE, "policyId: %{public}d.", policyId);
78 std::list<ModePolicy> listPolicy;
79 for (auto policyNodePtr = nodePtr->xmlChildrenNode;
80 policyNodePtr != nullptr; policyNodePtr = policyNodePtr->next) {
81 ModePolicy pmp;
82 int32_t switchId;
83 StrToInt(TrimStr(GetProp(policyNodePtr, "id")), switchId);
84 StrToInt(TrimStr(GetProp(policyNodePtr, "recover_flag")), pmp.recover_flag);
85 StrToInt(TrimStr(GetProp(policyNodePtr, "value")), pmp.value);
86 pmp.id = static_cast<uint32_t>(switchId);
87 listPolicy.push_back(pmp);
88 POWER_HILOGD(FEATURE_POWER_MODE, "id=%{public}d, value=%{public}d, recover_flag=%{public}d", pmp.id,
89 pmp.value, pmp.recover_flag);
90 }
91 std::pair<uint32_t, std::list<ModePolicy>> policyPair(policyId, listPolicy);
92 this->policyCache_.insert(policyPair);
93 }
94 return true;
95 }
96
GetProp(const xmlNodePtr & nodePtr,const std::string & key)97 std::string PowerSaveMode::GetProp(const xmlNodePtr& nodePtr, const std::string& key)
98 {
99 xmlChar* prop = xmlGetProp(nodePtr, BAD_CAST(key.c_str()));
100 if (prop == nullptr) {
101 return "";
102 }
103 std::string value = reinterpret_cast<char*>(prop);
104 xmlFree(prop);
105 return value;
106 }
107
GetSleepTime(int32_t mode)108 int32_t PowerSaveMode::GetSleepTime(int32_t mode)
109 {
110 if (this->policyCache_.size() == 0) {
111 return RETURN_FLAG_FALSE;
112 }
113 std::list<ModePolicy>& modePolicyList = this->policyCache_[mode];
114 const auto& itemPolicy = std::find_if(modePolicyList.begin(), modePolicyList.end(), [](const auto& modePolicy) {
115 return modePolicy.id == SLEEP_FILTER;
116 });
117
118 return (itemPolicy != modePolicyList.end()) ? itemPolicy->value : RETURN_FLAG_FALSE;
119 }
120 } // namespace PowerMgr
121 } // namespace OHOS
122