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 <cJSON.h>
22 #include "config_policy_utils.h"
23 #include "power_cjson_utils.h"
24 #include "power_log.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 cJSON* root = cJSON_Parse(jsonStr.c_str());
83 if (!root) {
84 POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json parse error[%{public}s]", jsonStr.c_str());
85 return parseSources;
86 }
87
88 if (!PowerMgrJsonUtils::IsValidJsonObjectOrJsonArray(root)) {
89 POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json root invalid[%{public}s]", jsonStr.c_str());
90 cJSON_Delete(root);
91 return parseSources;
92 }
93
94 cJSON* item = nullptr;
95 cJSON_ArrayForEach(item, root) {
96 const char* key = item->string;
97 if (!key) {
98 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "invalid key in json object");
99 continue;
100 }
101 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "key=%{public}s", key);
102 std::string keyStr = std::string(key);
103 bool ret = ParseSourcesProc(parseSources, item, keyStr);
104 if (ret == false) {
105 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "lost map config key");
106 continue;
107 }
108 }
109
110 cJSON_Delete(root);
111 return parseSources;
112 }
113
ParseSourcesProc(std::shared_ptr<WakeupActionSources> & parseSources,cJSON * valueObj,std::string & key)114 bool WakeupActionSourceParser::ParseSourcesProc(
115 std::shared_ptr<WakeupActionSources>& parseSources, cJSON* valueObj, std::string& key)
116 {
117 std::string scene{""};
118 uint32_t action = 0;
119 if (PowerMgrJsonUtils::IsValidJsonObject(valueObj)) {
120 cJSON* sceneValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupActionSource::SCENE_KEY);
121 if (PowerMgrJsonUtils::IsValidJsonString(sceneValue)) {
122 scene = sceneValue->valuestring;
123 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "scene=%{public}s", scene.c_str());
124 }
125 cJSON* actionValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupActionSource::ACTION_KEY);
126 if (PowerMgrJsonUtils::IsValidJsonNumber(actionValue)) {
127 action = static_cast<uint32_t>(actionValue->valueint);
128 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "action=%{public}u", action);
129 if (action >= ILLEGAL_ACTION) {
130 action = 0;
131 }
132 }
133 }
134
135 if (action != 0) {
136 std::shared_ptr<WakeupActionSource> wakeupActionSource = std::make_shared<WakeupActionSource>(scene, action);
137 parseSources->PutSource(key, wakeupActionSource);
138 }
139
140 return true;
141 }
142
143 } // namespace PowerMgr
144 } // namespace OHOS