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 targetPath;
56 bool ret = GetTargetPath(targetPath);
57 if (ret == false) {
58 POWER_HILOGE(FEATURE_SUSPEND, "GetTargetPath fail");
59 return parseSources;
60 }
61 POWER_HILOGI(FEATURE_SUSPEND, "use targetPath=%{public}s", targetPath.c_str());
62 std::ifstream inputStream(targetPath.c_str(), std::ios::in | std::ios::binary);
63 std::string fileStringStr(std::istreambuf_iterator<char> {inputStream}, std::istreambuf_iterator<char> {});
64
65 if (!isSettingAcValid) {
66 SettingHelper::SetSettingAcSuspendSources(fileStringStr);
67 }
68 if (!isSettingDcValid) {
69 SettingHelper::SetSettingDcSuspendSources(fileStringStr);
70 }
71 configJsonStr = fileStringStr;
72 }
73
74 if (isPowerConnected && isSettingAcValid) {
75 configJsonStr = SettingHelper::GetSettingAcSuspendSources();
76 } else if (!isPowerConnected && isSettingDcValid) {
77 configJsonStr = SettingHelper::GetSettingDcSuspendSources();
78 }
79
80 parseSources = ParseSources(configJsonStr);
81 return parseSources;
82 }
83 #else
ParseSources()84 std::shared_ptr<SuspendSources> SuspendSourceParser::ParseSources()
85 {
86 std::shared_ptr<SuspendSources> parseSources{nullptr};
87 bool isSettingUpdated = SettingHelper::IsSuspendSourcesSettingValid();
88 POWER_HILOGI(FEATURE_SUSPEND, "ParseSources setting=%{public}d", isSettingUpdated);
89 std::string configJsonStr;
90 if (isSettingUpdated) {
91 configJsonStr = SettingHelper::GetSettingSuspendSources();
92 } else {
93 std::string targetPath;
94 bool ret = GetTargetPath(targetPath);
95 if (ret == false) {
96 return parseSources;
97 }
98 POWER_HILOGI(FEATURE_SUSPEND, "use targetPath=%{public}s", targetPath.c_str());
99 std::ifstream inputStream(targetPath.c_str(), std::ios::in | std::ios::binary);
100 std::string fileStringStr(std::istreambuf_iterator<char> {inputStream}, std::istreambuf_iterator<char> {});
101 configJsonStr = fileStringStr;
102 }
103 parseSources = ParseSources(configJsonStr);
104 if (parseSources != nullptr) {
105 SettingHelper::SetSettingSuspendSources(configJsonStr);
106 }
107 return parseSources;
108 }
109 #endif
110
GetTargetPath(std::string & targetPath)111 bool SuspendSourceParser::GetTargetPath(std::string& targetPath)
112 {
113 targetPath.clear();
114 bool ret = true;
115 char buf[MAX_PATH_LEN];
116 char* path = GetOneCfgFile(POWER_SUSPEND_CONFIG_FILE.c_str(), buf, MAX_PATH_LEN);
117 if (path != nullptr && *path != '\0') {
118 POWER_HILOGI(FEATURE_SUSPEND, "use policy path=%{public}s", path);
119 targetPath = path;
120 return true;
121 }
122
123 if (access(VENDOR_POWER_SUSPEND_CONFIG_FILE.c_str(), F_OK | R_OK) == -1) {
124 POWER_HILOGE(FEATURE_SUSPEND, "vendor suspend config is not exist or permission denied");
125 if (access(SYSTEM_POWER_SUSPEND_CONFIG_FILE.c_str(), F_OK | R_OK) == -1) {
126 POWER_HILOGE(FEATURE_SUSPEND, "system suspend config is not exist or permission denied");
127 ret = false;
128 } else {
129 targetPath = SYSTEM_POWER_SUSPEND_CONFIG_FILE;
130 }
131 } else {
132 targetPath = VENDOR_POWER_SUSPEND_CONFIG_FILE;
133 }
134 return ret;
135 }
136
ParseSources(const std::string & jsonStr)137 std::shared_ptr<SuspendSources> SuspendSourceParser::ParseSources(const std::string& jsonStr)
138 {
139 std::shared_ptr<SuspendSources> parseSources = std::make_shared<SuspendSources>();
140 Json::Reader reader;
141 Json::Value root;
142 std::string errors;
143 if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) {
144 POWER_HILOGE(FEATURE_SUSPEND, "json parse error");
145 return parseSources;
146 }
147
148 if (root.isNull() || !root.isObject()) {
149 POWER_HILOGE(FEATURE_SUSPEND, "json root invalid[%{public}s]", jsonStr.c_str());
150 return parseSources;
151 }
152
153 Json::Value::Members members = root.getMemberNames();
154 for (auto iter = members.begin(); iter != members.end(); iter++) {
155 std::string key = *iter;
156 Json::Value valueObj = root[key];
157 bool ret = ParseSourcesProc(parseSources, valueObj, key);
158 if (ret == false) {
159 POWER_HILOGI(FEATURE_SUSPEND, "lost map config key");
160 continue;
161 }
162 }
163 return parseSources;
164 }
165
ParseSourcesProc(std::shared_ptr<SuspendSources> & parseSources,Json::Value & valueObj,std::string & key)166 bool SuspendSourceParser::ParseSourcesProc(
167 std::shared_ptr<SuspendSources>& parseSources, Json::Value& valueObj, std::string& key)
168 {
169 SuspendDeviceType suspendDeviceType = SuspendSources::mapSuspendDeviceType(key);
170 if (suspendDeviceType == SuspendDeviceType::SUSPEND_DEVICE_REASON_MIN) {
171 return false;
172 }
173
174 uint32_t action = 0;
175 uint32_t delayMs = 0;
176 if (!valueObj.isNull() && valueObj.isObject()) {
177 Json::Value actionValue = valueObj[SuspendSource::ACTION_KEY];
178 Json::Value delayValue = valueObj[SuspendSource::DELAY_KEY];
179 if (actionValue.isUInt() && delayValue.isUInt()) {
180 action = actionValue.asUInt();
181 delayMs = delayValue.asUInt();
182 if (action >= ILLEGAL_ACTION) {
183 action = 0;
184 }
185 }
186 }
187
188 POWER_HILOGI(FEATURE_SUSPEND,
189 "ParseSourcesProc key=%{public}s, type=%{public}u, action=%{public}u, delayMs=%{public}u",
190 key.c_str(), suspendDeviceType, action, delayMs);
191 SuspendSource suspendSource = SuspendSource(suspendDeviceType, action, delayMs);
192 parseSources->PutSource(suspendSource);
193 return true;
194 }
195
196 } // namespace PowerMgr
197 } // namespace OHOS