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