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 "wakeup_action_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 "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_ACTION_CONFIG_FILE = "etc/power_config/power_wakeup_action.json";
31 static const std::string VENDOR_POWER_WAKEUP_ACTION_CONFIG_FILE = "/vendor/etc/power_config/power_wakeup_action.json";
32 static const std::string SYSTEM_POWER_WAKEUP_ACTION_CONFIG_FILE = "/system/etc/power_config/power_wakeup_action.json";
33 static const uint32_t ILLEGAL_ACTION = static_cast<uint32_t>(WakeupAction::ACTION_INVALID);
34 } // namespace
35
ParseSources()36 std::shared_ptr<WakeupActionSources> WakeupActionSourceParser::ParseSources()
37 {
38 std::shared_ptr<WakeupActionSources> parseSources;
39 std::string configJsonStr;
40 std::string targetPath;
41 bool ret = GetTargetPath(targetPath);
42 if (ret == false) {
43 return parseSources;
44 }
45 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "use targetPath=%{public}s", targetPath.c_str());
46 std::ifstream inputStream(targetPath.c_str(), std::ios::in | std::ios::binary);
47 std::string fileStringStr(std::istreambuf_iterator<char> {inputStream}, std::istreambuf_iterator<char> {});
48 configJsonStr = fileStringStr;
49 parseSources = ParseSources(configJsonStr);
50 return parseSources;
51 }
52
GetTargetPath(std::string & targetPath)53 bool WakeupActionSourceParser::GetTargetPath(std::string& targetPath)
54 {
55 targetPath.clear();
56 bool ret = true;
57 char buf[MAX_PATH_LEN];
58 char* path = GetOneCfgFile(POWER_WAKEUP_ACTION_CONFIG_FILE.c_str(), buf, MAX_PATH_LEN);
59 if (path != nullptr && *path != '\0') {
60 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "use policy path=%{public}s", path);
61 targetPath = path;
62 return true;
63 }
64
65 if (access(VENDOR_POWER_WAKEUP_ACTION_CONFIG_FILE.c_str(), F_OK | R_OK) == -1) {
66 POWER_HILOGE(FEATURE_WAKEUP_ACTION, "vendor WakeupAction config is not exist or permission denied");
67 if (access(SYSTEM_POWER_WAKEUP_ACTION_CONFIG_FILE.c_str(), F_OK | R_OK) == -1) {
68 POWER_HILOGE(FEATURE_WAKEUP_ACTION, "system WakeupAction config is not exist or permission denied");
69 ret = false;
70 } else {
71 targetPath = SYSTEM_POWER_WAKEUP_ACTION_CONFIG_FILE;
72 }
73 } else {
74 targetPath = VENDOR_POWER_WAKEUP_ACTION_CONFIG_FILE;
75 }
76 return ret;
77 }
78
ParseSources(const std::string & jsonStr)79 std::shared_ptr<WakeupActionSources> WakeupActionSourceParser::ParseSources(const std::string& jsonStr)
80 {
81 std::shared_ptr<WakeupActionSources> parseSources = std::make_shared<WakeupActionSources>();
82 Json::Reader reader;
83 Json::Value root;
84 if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) {
85 POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json parse error");
86 return parseSources;
87 }
88
89 if (root.isNull() || !root.isObject()) {
90 POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json root invalid[%{public}s]", jsonStr.c_str());
91 return parseSources;
92 }
93
94 Json::Value::Members members = root.getMemberNames();
95 for (auto iter = members.begin(); iter != members.end(); iter++) {
96 std::string key = *iter;
97 Json::Value valueObj = root[key];
98 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "key=%{public}s", key.c_str());
99 bool ret = ParseSourcesProc(parseSources, valueObj, key);
100 if (ret == false) {
101 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "lost map config key");
102 continue;
103 }
104 }
105 return parseSources;
106 }
107
ParseSourcesProc(std::shared_ptr<WakeupActionSources> & parseSources,Json::Value & valueObj,std::string & key)108 bool WakeupActionSourceParser::ParseSourcesProc(
109 std::shared_ptr<WakeupActionSources>& parseSources, Json::Value& valueObj, std::string& key)
110 {
111 std::string scene{""};
112 uint32_t action = 0;
113 if (!valueObj.isNull() && valueObj.isObject()) {
114 Json::Value sceneValue = valueObj[WakeupActionSource::SCENE_KEY];
115 Json::Value actionValue = valueObj[WakeupActionSource::ACTION_KEY];
116 if (sceneValue.isString()) {
117 scene = sceneValue.asString();
118 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "scene=%{public}s", scene.c_str());
119 }
120 if (actionValue.isUInt()) {
121 action = actionValue.asUInt();
122 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "action=%{public}u", action);
123 if (action >= ILLEGAL_ACTION) {
124 action = 0;
125 }
126 }
127 }
128
129 if (action != 0) {
130 std::shared_ptr<WakeupActionSource> wakeupActionSource = std::make_shared<WakeupActionSource>(scene, action);
131 parseSources->PutSource(key, wakeupActionSource);
132 }
133
134 return true;
135 }
136
137 } // namespace PowerMgr
138 } // namespace OHOS