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