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 "vibrator_source_parser.h"
17
18 #include <fstream>
19 #include <securec.h>
20 #include <unistd.h>
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 {
ParseSources(const std::string & etcPath,const std::string & vendorPath,const std::string & systemPath)28 std::vector<VibratorSource> VibratorSourceParser::ParseSources(
29 const std::string& etcPath, const std::string& vendorPath, const std::string& systemPath)
30 {
31 std::vector<VibratorSource> sources;
32 std::string targetPath;
33 GetTargetPath(targetPath, etcPath, vendorPath, systemPath);
34 if (targetPath.empty()) {
35 POWER_HILOGE(COMP_UTILS, "targetPath is null");
36 return sources;
37 }
38 POWER_HILOGI(COMP_UTILS, "use targetPath=%{public}s", targetPath.c_str());
39 std::ifstream inputStream(targetPath.c_str(), std::ios::in | std::ios::binary);
40 std::string fileStringStr(std::istreambuf_iterator<char> {inputStream}, std::istreambuf_iterator<char> {});
41 targetPath = fileStringStr;
42 sources = ParseSources(targetPath);
43 return sources;
44 }
45
GetTargetPath(std::string & targetPath,const std::string & etcPath,const std::string & vendorPath,const std::string & systemPath)46 void VibratorSourceParser::GetTargetPath(
47 std::string& targetPath, const std::string& etcPath, const std::string& vendorPath, const std::string& systemPath)
48 {
49 targetPath.clear();
50 char buf[MAX_PATH_LEN];
51 char* path = GetOneCfgFile(etcPath.c_str(), buf, MAX_PATH_LEN);
52 if (path != nullptr && *path != '\0') {
53 POWER_HILOGI(COMP_UTILS, "use policy path=%{public}s", path);
54 targetPath = path;
55 return;
56 }
57
58 if (access(vendorPath.c_str(), F_OK | R_OK) == -1) {
59 POWER_HILOGE(COMP_UTILS, "vendor vibrator config is not exist or permission denied");
60 if (access(systemPath.c_str(), F_OK | R_OK) == -1) {
61 POWER_HILOGE(COMP_UTILS, "system vibrator config is not exist or permission denied");
62 return;
63 } else {
64 targetPath = systemPath;
65 }
66 } else {
67 targetPath = vendorPath;
68 }
69 }
70
ParseSources(const std::string & jsonStr)71 std::vector<VibratorSource> VibratorSourceParser::ParseSources(const std::string& jsonStr)
72 {
73 std::vector<VibratorSource> sources;
74 Json::Reader reader;
75 Json::Value root;
76 if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) {
77 POWER_HILOGE(COMP_UTILS, "json parse error");
78 return sources;
79 }
80 Json::Value::Members members = root.getMemberNames();
81 for (auto iter = members.begin(); iter != members.end(); iter++) {
82 std::string key = *iter;
83 Json::Value valueObj = root[key];
84 POWER_HILOGI(COMP_UTILS, "key=%{public}s", key.c_str());
85 ParseSourcesProc(sources, valueObj, key);
86 }
87 return sources;
88 }
89
ParseSourcesProc(std::vector<VibratorSource> & sources,Json::Value & valueObj,std::string & key)90 void VibratorSourceParser::ParseSourcesProc(
91 std::vector<VibratorSource>& sources, Json::Value& valueObj, std::string& key)
92 {
93 if (!valueObj.isObject()) {
94 return;
95 }
96 std::string type;
97 bool enable = false;
98 Json::Value enableValue = valueObj[VibratorSource::ENABLE_KEY];
99 Json::Value typeValue = valueObj[VibratorSource::TYPE_KEY];
100 if (!typeValue.isString() || !enableValue.isBool()) {
101 return;
102 }
103 enable = enableValue.asBool();
104 type = typeValue.asString();
105 if (!enable || type.empty()) {
106 return;
107 }
108 VibratorSource vibratorSource = VibratorSource(key, enable, type);
109 sources.emplace_back(vibratorSource);
110 }
111
112 }
113 }