• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "suspend_source_parser.h"
16 
17 #include <fstream>
18 #include <securec.h>
19 #include <unistd.h>
20 
21 #include "config_policy_utils.h"
22 #include "power_log.h"
23 #include "setting_helper.h"
24 #include "json/reader.h"
25 #include "json/value.h"
26 #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
27 #include "power_mgr_service.h"
28 #endif
29 
30 namespace OHOS {
31 namespace PowerMgr {
32 
33 namespace {
34 static const std::string POWER_SUSPEND_CONFIG_FILE = "etc/power_config/power_suspend.json";
35 static const std::string VENDOR_POWER_SUSPEND_CONFIG_FILE = "/vendor/etc/power_config/power_suspend.json";
36 static const std::string SYSTEM_POWER_SUSPEND_CONFIG_FILE = "/system/etc/power_config/power_suspend.json";
37 static const uint32_t ILLEGAL_ACTION = static_cast<uint32_t>(SuspendAction::ACTION_INVALID);
38 } // namespace
39 
40 #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
ParseSources()41 std::shared_ptr<SuspendSources> SuspendSourceParser::ParseSources()
42 {
43     std::shared_ptr<SuspendSources> parseSources{nullptr};
44     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
45     if (pms == nullptr) {
46         POWER_HILOGE(FEATURE_SUSPEND, "get PowerMgrService fail");
47         return parseSources;
48     }
49     bool isPowerConnected = pms->IsPowerConnected();
50     bool isSettingAcValid = SettingHelper::IsSettingAcSuspendSourcesValid();
51     bool isSettingDcValid = SettingHelper::IsSettingDcSuspendSourcesValid();
52     std::string configJsonStr;
53 
54     if (!isSettingAcValid || !isSettingDcValid) {
55         std::string fileStringStr = GetSuspendSourcesByConfig();
56 
57         if (!isSettingAcValid) {
58             SettingHelper::SetSettingAcSuspendSources(fileStringStr);
59         }
60         if (!isSettingDcValid) {
61             SettingHelper::SetSettingDcSuspendSources(fileStringStr);
62         }
63         configJsonStr = fileStringStr;
64     }
65 
66     if (isPowerConnected && isSettingAcValid) {
67         configJsonStr = SettingHelper::GetSettingAcSuspendSources();
68     } else if (!isPowerConnected && isSettingDcValid) {
69         configJsonStr = SettingHelper::GetSettingDcSuspendSources();
70     }
71 
72     parseSources = ParseSources(configJsonStr);
73     if (parseSources->GetParseErrorFlag()) {
74         POWER_HILOGI(FEATURE_SUSPEND, "call GetSuspendSourcesByConfig again");
75         configJsonStr = GetSuspendSourcesByConfig();
76         parseSources = ParseSources(configJsonStr);
77     }
78     return parseSources;
79 }
80 #else
ParseSources()81 std::shared_ptr<SuspendSources> SuspendSourceParser::ParseSources()
82 {
83     std::shared_ptr<SuspendSources> parseSources{nullptr};
84     bool isSettingUpdated = SettingHelper::IsSuspendSourcesSettingValid();
85     POWER_HILOGI(FEATURE_SUSPEND, "ParseSources setting=%{public}d", isSettingUpdated);
86     std::string configJsonStr;
87     if (isSettingUpdated) {
88         std::string sourcesSettingStr = SettingHelper::GetSettingSuspendSources();
89         configJsonStr = sourcesSettingStr;
90 #ifdef POWER_MANAGER_ENABLE_WATCH_UPDATE_ADAPT
91         // this branch means use config file for update scene in watch
92         if (sourcesSettingStr.find(SuspendSources::TP_COVER_KEY) == std::string::npos) {
93             configJsonStr = GetSuspendSourcesByConfig();
94             POWER_HILOGW(FEATURE_SUSPEND, "update scene need use (config file)");
95         }
96 #endif
97     } else {
98         configJsonStr = GetSuspendSourcesByConfig();
99     }
100     parseSources = ParseSources(configJsonStr);
101     if (parseSources->GetParseErrorFlag()) {
102         POWER_HILOGI(FEATURE_SUSPEND, "call GetSuspendSourcesByConfig again");
103         configJsonStr = GetSuspendSourcesByConfig();
104         parseSources = ParseSources(configJsonStr);
105     }
106     if (parseSources != nullptr) {
107         SettingHelper::SetSettingSuspendSources(configJsonStr);
108     }
109     return parseSources;
110 }
111 #endif
112 
GetSuspendSourcesByConfig()113 const std::string SuspendSourceParser::GetSuspendSourcesByConfig()
114 {
115     std::string targetPath;
116     bool ret = GetTargetPath(targetPath);
117     if (ret == false) {
118         POWER_HILOGE(FEATURE_SUSPEND, "GetTargetPath fail");
119         return "";
120     }
121     POWER_HILOGI(FEATURE_SUSPEND, "use targetPath=%{public}s", targetPath.c_str());
122     std::ifstream inputStream(targetPath.c_str(), std::ios::in | std::ios::binary);
123     return std::string(std::istreambuf_iterator<char> {inputStream}, std::istreambuf_iterator<char> {});
124 }
125 
GetTargetPath(std::string & targetPath)126 bool SuspendSourceParser::GetTargetPath(std::string& targetPath)
127 {
128     targetPath.clear();
129     bool ret = true;
130     char buf[MAX_PATH_LEN];
131     char* path = GetOneCfgFile(POWER_SUSPEND_CONFIG_FILE.c_str(), buf, MAX_PATH_LEN);
132     if (path != nullptr && *path != '\0') {
133         POWER_HILOGI(FEATURE_SUSPEND, "use policy path=%{public}s", path);
134         targetPath = path;
135         return true;
136     }
137 
138     if (access(VENDOR_POWER_SUSPEND_CONFIG_FILE.c_str(), F_OK | R_OK) == -1) {
139         POWER_HILOGE(FEATURE_SUSPEND, "vendor suspend config is not exist or permission denied");
140         if (access(SYSTEM_POWER_SUSPEND_CONFIG_FILE.c_str(), F_OK | R_OK) == -1) {
141             POWER_HILOGE(FEATURE_SUSPEND, "system suspend config is not exist or permission denied");
142             ret = false;
143         } else {
144             targetPath = SYSTEM_POWER_SUSPEND_CONFIG_FILE;
145         }
146     } else {
147         targetPath = VENDOR_POWER_SUSPEND_CONFIG_FILE;
148     }
149     return ret;
150 }
151 
ParseSources(const std::string & jsonStr)152 std::shared_ptr<SuspendSources> SuspendSourceParser::ParseSources(const std::string& jsonStr)
153 {
154     std::shared_ptr<SuspendSources> parseSources = std::make_shared<SuspendSources>();
155     Json::Reader reader;
156     Json::Value root;
157     std::string errors;
158     if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) {
159         POWER_HILOGE(FEATURE_SUSPEND, "json parse error");
160         parseSources->SetParseErrorFlag(true);
161         return parseSources;
162     }
163 
164     if (root.isNull() || !root.isObject()) {
165         POWER_HILOGE(FEATURE_SUSPEND, "json root invalid[%{public}s]", jsonStr.c_str());
166         parseSources->SetParseErrorFlag(true);
167         return parseSources;
168     }
169 
170     Json::Value::Members members = root.getMemberNames();
171     for (auto iter = members.begin(); iter != members.end(); iter++) {
172         std::string key = *iter;
173         Json::Value valueObj = root[key];
174         bool ret = ParseSourcesProc(parseSources, valueObj, key);
175         if (ret == false) {
176             POWER_HILOGI(FEATURE_SUSPEND, "lost map config key");
177             continue;
178         }
179     }
180     return parseSources;
181 }
182 
ParseSourcesProc(std::shared_ptr<SuspendSources> & parseSources,Json::Value & valueObj,std::string & key)183 bool SuspendSourceParser::ParseSourcesProc(
184     std::shared_ptr<SuspendSources>& parseSources, Json::Value& valueObj, std::string& key)
185 {
186     if (parseSources == nullptr) {
187         POWER_HILOGE(FEATURE_SUSPEND, "parseSources is nullptr");
188         return false;
189     }
190 
191     SuspendDeviceType suspendDeviceType = SuspendSources::mapSuspendDeviceType(key);
192     if (suspendDeviceType == SuspendDeviceType::SUSPEND_DEVICE_REASON_MIN) {
193         return false;
194     }
195 
196     uint32_t action = 0;
197     uint32_t delayMs = 0;
198     if (!valueObj.isNull() && valueObj.isObject()) {
199         Json::Value actionValue = valueObj[SuspendSource::ACTION_KEY];
200         Json::Value delayValue = valueObj[SuspendSource::DELAY_KEY];
201         if (actionValue.isUInt() && delayValue.isUInt()) {
202             action = actionValue.asUInt();
203             delayMs = delayValue.asUInt();
204             if (action >= ILLEGAL_ACTION) {
205                 action = 0;
206             }
207         }
208     }
209 
210     POWER_HILOGI(FEATURE_SUSPEND,
211         "ParseSourcesProc key=%{public}s, type=%{public}u, action=%{public}u, delayMs=%{public}u",
212         key.c_str(), suspendDeviceType, action, delayMs);
213     SuspendSource suspendSource = SuspendSource(suspendDeviceType, action, delayMs);
214     parseSources->PutSource(suspendSource);
215     return true;
216 }
217 
218 } // namespace PowerMgr
219 } // namespace OHOS