• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             StrToInt(TrimStr(GetProp(policyNodePtr, "id")), pmp.id);
83             StrToInt(TrimStr(GetProp(policyNodePtr, "recover_flag")), pmp.recover_flag);
84             StrToInt(TrimStr(GetProp(policyNodePtr, "value")), pmp.value);
85             listPolicy.push_back(pmp);
86             POWER_HILOGD(FEATURE_POWER_MODE, "id=%{public}d, value=%{public}d, recover_flag=%{public}d", pmp.id,
87                 pmp.value, pmp.recover_flag);
88         }
89         std::pair<int32_t, std::list<ModePolicy>> policyPair(policyId, listPolicy);
90         this->policyCache_.insert(policyPair);
91     }
92     return true;
93 }
94 
GetProp(const xmlNodePtr & nodePtr,const std::string & key)95 std::string PowerSaveMode::GetProp(const xmlNodePtr& nodePtr, const std::string& key)
96 {
97     xmlChar* prop = xmlGetProp(nodePtr, BAD_CAST(key.c_str()));
98     if (prop == nullptr) {
99         return "";
100     }
101     std::string value = reinterpret_cast<char*>(prop);
102     xmlFree(prop);
103     return value;
104 }
105 
GetValuePolicy(std::list<ModePolicy> & openPolicy,int32_t mode)106 bool PowerSaveMode::GetValuePolicy(std::list<ModePolicy> &openPolicy, int32_t mode)
107 {
108     bool result = GetFilterPolicy(openPolicy, mode, ValueProp::value);
109     return result;
110 }
111 
GetRecoverPolicy(std::list<ModePolicy> & recoverPolicy,int32_t mode)112 bool PowerSaveMode::GetRecoverPolicy(std::list<ModePolicy> &recoverPolicy, int32_t mode)
113 {
114     bool result = GetFilterPolicy(recoverPolicy, mode, ValueProp::recover);
115     return result;
116 }
117 
GetFilterPolicy(std::list<ModePolicy> & policyList,int32_t mode,int32_t value)118 bool PowerSaveMode::GetFilterPolicy(std::list<ModePolicy> &policyList, int32_t mode, int32_t value)
119 {
120     if (this->policyCache_.size() == 0) {
121         return false;
122     }
123     policyList = policyCache_[mode];
124     if (ValueProp::recover == value) {
125         policyList.remove_if([&](ModePolicy mp) {return mp.recover_flag != ValueProp::recover;});
126     }
127     return true;
128 }
129 
GetSleepTime(int32_t mode)130 int32_t PowerSaveMode::GetSleepTime(int32_t mode)
131 {
132     if (this->policyCache_.size() == 0) {
133         return RETURN_FLAG_FALSE;
134     }
135     std::list<ModePolicy>& modePolicyList = this->policyCache_[mode];
136     const auto& itemPolicy = std::find_if(modePolicyList.begin(), modePolicyList.end(), [](const auto& modePolicy) {
137         return modePolicy.id == SLEEP_FILTER;
138     });
139 
140     return (itemPolicy != modePolicyList.end()) ? itemPolicy->value : RETURN_FLAG_FALSE;
141 }
142 } // namespace PowerMgr
143 } // namespace OHOS
144