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
16 #include <fstream>
17 #include <unistd.h>
18
19 #include "config_policy_utils.h"
20 #include "power_log.h"
21 #include "setting_helper.h"
22 #include "wakeup_source_parser.h"
23 #include "json/reader.h"
24 #include "json/value.h"
25
26 namespace OHOS {
27 namespace PowerMgr {
28
29 namespace {
30 static const std::string POWER_WAKEUP_CONFIG_FILE = "etc/power_config/power_wakeup.json";
31 static const std::string VENDOR_POWER_WAKEUP_CONFIG_FILE = "/vendor/etc/power_config/power_wakeup.json";
32 static const std::string SYSTEM_POWER_WAKEUP_CONFIG_FILE = "/system/etc/power_config/power_wakeup.json";
33 static const uint32_t SINGLE_CLICK = static_cast<uint32_t>(WakeUpAction::CLICK_SINGLE);
34 static const uint32_t DOUBLE_CLICK = static_cast<uint32_t>(WakeUpAction::CLICK_DOUBLE);
35 } // namespace
36
ParseSources()37 std::shared_ptr<WakeupSources> WakeupSourceParser::ParseSources()
38 {
39 std::shared_ptr<WakeupSources> parseSources;
40 bool isSettingUpdated = SettingHelper::IsWakeupSourcesSettingValid();
41 POWER_HILOGI(FEATURE_WAKEUP, "ParseSources setting=%{public}d", isSettingUpdated);
42 std::string configJsonStr;
43 if (isSettingUpdated) {
44 configJsonStr = SettingHelper::GetSettingWakeupSources();
45 } else {
46 std::string targetPath;
47 bool ret = GetTargetPath(targetPath);
48 if (ret == false) {
49 return parseSources;
50 }
51
52 POWER_HILOGI(FEATURE_WAKEUP, "use targetPath=%{public}s", targetPath.c_str());
53 std::ifstream inputStream(targetPath.c_str(), std::ios::in | std::ios::binary);
54 std::string fileStringStr(std::istreambuf_iterator<char> {inputStream}, std::istreambuf_iterator<char> {});
55 configJsonStr = fileStringStr;
56 }
57 parseSources = ParseSources(configJsonStr);
58 if (parseSources != nullptr) {
59 SettingHelper::SetSettingWakeupSources(configJsonStr);
60 }
61 return parseSources;
62 }
63
GetTargetPath(std::string & targetPath)64 bool WakeupSourceParser::GetTargetPath(std::string& targetPath)
65 {
66 targetPath.clear();
67 char buf[MAX_PATH_LEN];
68 char* path = GetOneCfgFile(POWER_WAKEUP_CONFIG_FILE.c_str(), buf, MAX_PATH_LEN);
69 if (path != nullptr && *path != '\0') {
70 POWER_HILOGI(FEATURE_WAKEUP, "use policy path=%{public}s", path);
71 targetPath = path;
72 return true;
73 }
74
75 if (access(VENDOR_POWER_WAKEUP_CONFIG_FILE.c_str(), F_OK | R_OK) == -1) {
76 POWER_HILOGE(FEATURE_WAKEUP, "vendor wakeup config is not exist or permission denied");
77 if (access(SYSTEM_POWER_WAKEUP_CONFIG_FILE.c_str(), F_OK | R_OK) == -1) {
78 POWER_HILOGE(FEATURE_WAKEUP, "system wakeup config is not exist or permission denied");
79 return false;
80 } else {
81 targetPath = SYSTEM_POWER_WAKEUP_CONFIG_FILE;
82 }
83 } else {
84 targetPath = VENDOR_POWER_WAKEUP_CONFIG_FILE;
85 }
86
87 return true;
88 }
89
ParseSources(const std::string & jsonStr)90 std::shared_ptr<WakeupSources> WakeupSourceParser::ParseSources(const std::string& jsonStr)
91 {
92 std::shared_ptr<WakeupSources> parseSources = std::make_shared<WakeupSources>();
93 Json::Reader reader;
94 Json::Value root;
95 std::string errors;
96 if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) {
97 POWER_HILOGE(FEATURE_WAKEUP, "json parse error");
98 return parseSources;
99 }
100
101 Json::Value::Members members = root.getMemberNames();
102 for (auto iter = members.begin(); iter != members.end(); iter++) {
103 std::string key = *iter;
104 Json::Value valueObj = root[key];
105
106 POWER_HILOGI(FEATURE_WAKEUP, "key=%{public}s", key.c_str());
107
108 bool ret = ParseSourcesProc(parseSources, valueObj, key);
109 if (ret == false) {
110 POWER_HILOGI(FEATURE_WAKEUP, "lost map config key");
111 continue;
112 }
113 }
114
115 return parseSources;
116 }
117
ParseSourcesProc(std::shared_ptr<WakeupSources> & parseSources,Json::Value & valueObj,std::string & key)118 bool WakeupSourceParser::ParseSourcesProc(
119 std::shared_ptr<WakeupSources>& parseSources, Json::Value& valueObj, std::string& key)
120 {
121 bool enable = true;
122 uint32_t click = DOUBLE_CLICK;
123 WakeupDeviceType wakeupDeviceType = WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN;
124 if (valueObj.isObject()) {
125 Json::Value enableValue = valueObj[WakeupSource::ENABLE_KEY];
126 Json::Value clickValue = valueObj[WakeupSource::KEYS_KEY];
127 if (!clickValue.isNull() && clickValue.isUInt()) {
128 POWER_HILOGI(FEATURE_WAKEUP, "clickValue=%{public}u", clickValue.asUInt());
129 click = (clickValue.asUInt() == SINGLE_CLICK || clickValue.asUInt() == DOUBLE_CLICK) ? clickValue.asUInt() :
130 DOUBLE_CLICK;
131 }
132 if (enableValue.isBool()) {
133 enable = enableValue.asBool();
134 POWER_HILOGD(FEATURE_WAKEUP, "enable=%{public}d", enable);
135 }
136 }
137
138 wakeupDeviceType = WakeupSources::mapWakeupDeviceType(key, click);
139 POWER_HILOGI(FEATURE_WAKEUP, "key map type=%{public}u", wakeupDeviceType);
140
141 if (wakeupDeviceType == WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN) {
142 return false;
143 }
144 if (enable == true) {
145 WakeupSource wakeupSource = WakeupSource(wakeupDeviceType, enable, click);
146 parseSources->PutSource(wakeupSource);
147 }
148
149 return true;
150 }
151
152 } // namespace PowerMgr
153 } // namespace OHOS