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
27 namespace OHOS {
28 namespace PowerMgr {
29
30 namespace {
31 static const std::string POWER_SUSPEND_CONFIG_FILE = "etc/power_config/power_suspend.json";
32 static const std::string VENDOR_POWER_SUSPEND_CONFIG_FILE = "/vendor/etc/power_config/power_suspend.json";
33 static const std::string SYSTEM_POWER_SUSPEND_CONFIG_FILE = "/system/etc/power_config/power_suspend.json";
34 static const uint32_t ILLEGAL_ACTION = static_cast<uint32_t>(SuspendAction::ACTION_INVALID);
35 } // namespace
36
ParseSources()37 std::shared_ptr<SuspendSources> SuspendSourceParser::ParseSources()
38 {
39 std::shared_ptr<SuspendSources> parseSources;
40 bool isSettingUpdated = SettingHelper::IsSuspendSourcesSettingValid();
41 POWER_HILOGI(FEATURE_SUSPEND, "ParseSources setting=%{public}d", isSettingUpdated);
42 std::string configJsonStr;
43 if (isSettingUpdated) {
44 configJsonStr = SettingHelper::GetSettingSuspendSources();
45 } else {
46 std::string targetPath;
47 bool ret = GetTargetPath(targetPath);
48 if (ret == false) {
49 return parseSources;
50 }
51 POWER_HILOGI(FEATURE_SUSPEND, "use targetPath=%{public}s", targetPath.c_str());
52 std::ifstream inputStream(targetPath.c_str(), std::ios::in | std::ios::binary);
53 std::string fileStringStr(std::istreambuf_iterator<char> {inputStream}, std::istreambuf_iterator<char> {});
54 configJsonStr = fileStringStr;
55 }
56 parseSources = ParseSources(configJsonStr);
57 if (parseSources != nullptr) {
58 SettingHelper::SetSettingSuspendSources(configJsonStr);
59 }
60 return parseSources;
61 }
62
GetTargetPath(std::string & targetPath)63 bool SuspendSourceParser::GetTargetPath(std::string& targetPath)
64 {
65 targetPath.clear();
66 bool ret = true;
67 char buf[MAX_PATH_LEN];
68 char* path = GetOneCfgFile(POWER_SUSPEND_CONFIG_FILE.c_str(), buf, MAX_PATH_LEN);
69 if (path != nullptr && *path != '\0') {
70 POWER_HILOGI(FEATURE_SUSPEND, "use policy path=%{public}s", path);
71 targetPath = path;
72 return true;
73 }
74
75 if (access(VENDOR_POWER_SUSPEND_CONFIG_FILE.c_str(), F_OK | R_OK) == -1) {
76 POWER_HILOGE(FEATURE_SUSPEND, "vendor suspend config is not exist or permission denied");
77 if (access(SYSTEM_POWER_SUSPEND_CONFIG_FILE.c_str(), F_OK | R_OK) == -1) {
78 POWER_HILOGE(FEATURE_SUSPEND, "system suspend config is not exist or permission denied");
79 ret = false;
80 } else {
81 targetPath = SYSTEM_POWER_SUSPEND_CONFIG_FILE;
82 }
83 } else {
84 targetPath = VENDOR_POWER_SUSPEND_CONFIG_FILE;
85 }
86 return ret;
87 }
88
ParseSources(const std::string & jsonStr)89 std::shared_ptr<SuspendSources> SuspendSourceParser::ParseSources(const std::string& jsonStr)
90 {
91 std::shared_ptr<SuspendSources> parseSources = std::make_shared<SuspendSources>();
92 Json::Reader reader;
93 Json::Value root;
94 std::string errors;
95 if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) {
96 POWER_HILOGE(FEATURE_SUSPEND, "json parse error");
97 return parseSources;
98 }
99
100 Json::Value::Members members = root.getMemberNames();
101 for (auto iter = members.begin(); iter != members.end(); iter++) {
102 std::string key = *iter;
103 Json::Value valueObj = root[key];
104 POWER_HILOGI(FEATURE_SUSPEND, "key=%{public}s", key.c_str());
105 bool ret = ParseSourcesProc(parseSources, valueObj, key);
106 if (ret == false) {
107 POWER_HILOGI(FEATURE_SUSPEND, "lost map config key");
108 continue;
109 }
110 }
111 return parseSources;
112 }
113
ParseSourcesProc(std::shared_ptr<SuspendSources> & parseSources,Json::Value & valueObj,std::string & key)114 bool SuspendSourceParser::ParseSourcesProc(
115 std::shared_ptr<SuspendSources>& parseSources, Json::Value& valueObj, std::string& key)
116 {
117 SuspendDeviceType suspendDeviceType = SuspendSources::mapSuspendDeviceType(key);
118 POWER_HILOGI(FEATURE_SUSPEND, "key map type=%{public}u", suspendDeviceType);
119 if (suspendDeviceType == SuspendDeviceType::SUSPEND_DEVICE_REASON_MIN) {
120 return false;
121 }
122
123 uint32_t action = 0;
124 uint32_t delayMs = 0;
125 if (valueObj.isObject()) {
126 Json::Value actionValue = valueObj[SuspendSource::ACTION_KEY];
127 Json::Value delayValue = valueObj[SuspendSource::DELAY_KEY];
128 if (actionValue.isUInt() && delayValue.isUInt()) {
129 action = actionValue.asUInt();
130 POWER_HILOGI(FEATURE_SUSPEND, "action=%{public}u", action);
131 delayMs = delayValue.asUInt();
132 POWER_HILOGI(FEATURE_SUSPEND, "delayMs=%{public}u", delayMs);
133 if (action >= ILLEGAL_ACTION) {
134 action = 0;
135 }
136 }
137 }
138
139 if (action != 0) {
140 SuspendSource suspendSource = SuspendSource(suspendDeviceType, action, delayMs);
141 parseSources->PutSource(suspendSource);
142 }
143
144 return true;
145 }
146
147 } // namespace PowerMgr
148 } // namespace OHOS