• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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 {
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 
75     if (jsonStr.empty()) {
76         POWER_HILOGE(COMP_UTILS, "Input JSON string is empty");
77         return sources;
78     }
79 
80     cJSON* root = cJSON_Parse(jsonStr.c_str());
81     if (!root) {
82         POWER_HILOGE(COMP_UTILS, "JSON parse error");
83         return sources;
84     }
85     if (!PowerMgrJsonUtils::IsValidJsonObjectOrJsonArray(root)) {
86         POWER_HILOGE(COMP_UTILS, "JSON root is not object");
87         cJSON_Delete(root);
88         return sources;
89     }
90 
91     cJSON* item = nullptr;
92     cJSON_ArrayForEach(item, root) {
93         const char* key = item->string;
94         if (!key) {
95             POWER_HILOGE(COMP_UTILS, "invalid key in json object");
96             continue;
97         }
98         std::string keyStr = std::string(key);
99         POWER_HILOGI(COMP_UTILS, "key=%{public}s", keyStr.c_str());
100         ParseSourcesProc(sources, item, keyStr);
101     }
102 
103     cJSON_Delete(root);
104     return sources;
105 }
106 
ParseSourcesProc(std::vector<VibratorSource> & sources,cJSON * valueObj,std::string & key)107 void VibratorSourceParser::ParseSourcesProc(
108     std::vector<VibratorSource>& sources, cJSON* valueObj, std::string& key)
109 {
110     if (!PowerMgrJsonUtils::IsValidJsonObject(valueObj)) {
111         POWER_HILOGE(COMP_UTILS, "ValueObj is not a json object.");
112         return;
113     }
114 
115     bool enable = false;
116     std::string type;
117 
118     cJSON* enableItem = cJSON_GetObjectItemCaseSensitive(valueObj, VibratorSource::ENABLE_KEY);
119     if (!PowerMgrJsonUtils::IsValidJsonBool(enableItem)) {
120         POWER_HILOGE(COMP_UTILS, "Parse enable error.");
121         return;
122     }
123     enable =  cJSON_IsTrue(enableItem);
124 
125     cJSON* typeItem = cJSON_GetObjectItemCaseSensitive(valueObj, VibratorSource::TYPE_KEY);
126     if (!PowerMgrJsonUtils::IsValidJsonString(typeItem)) {
127         POWER_HILOGE(COMP_UTILS, "Parse type error.");
128         return;
129     }
130     type = typeItem->valuestring;
131 
132     if (!enable || type.empty()) {
133         return;
134     }
135     VibratorSource vibratorSource = VibratorSource(key, enable, type);
136     sources.emplace_back(vibratorSource);
137 }
138 
139 }
140 }