1 /*
2 * Copyright (c) 2021-2024 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 "hiappevent_c.h"
17
18 #include <memory>
19 #include <vector>
20
21 #include "hiappevent_base.h"
22 #include "hiappevent_clean.h"
23 #include "hiappevent_config.h"
24 #include "hiappevent_verify.h"
25 #include "hiappevent_write.h"
26 #include "hilog/log.h"
27
28 #undef LOG_DOMAIN
29 #define LOG_DOMAIN 0xD002D07
30
31 #undef LOG_TAG
32 #define LOG_TAG "AppEventC"
33
34 using namespace OHOS::HiviewDFX;
35
36 namespace {
37 constexpr int MAX_SIZE_OF_LIST_PARAM = 100;
38 constexpr size_t MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE = 1024;
39
40 template<typename T>
AddArrayParam(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const T * arr,int len)41 void AddArrayParam(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const T* arr, int len)
42 {
43 std::vector<T> params(arr, (len > MAX_SIZE_OF_LIST_PARAM) ? (arr + MAX_SIZE_OF_LIST_PARAM + 1) : (arr + len));
44 appEventPack->AddParam(name, params);
45 }
46
47 using ParamAdder = void (*)(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value);
48
AddBoolParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)49 void AddBoolParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
50 {
51 appEventPack->AddParam(name, value->value.bool_v);
52 }
53
AddBoolArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)54 void AddBoolArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
55 {
56 AddArrayParam(appEventPack, name, value->value.bool_arr_v, value->arrSize);
57 }
58
AddInt8ParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)59 void AddInt8ParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
60 {
61 appEventPack->AddParam(name, value->value.int8_v);
62 }
63
AddInt8ArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)64 void AddInt8ArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
65 {
66 AddArrayParam(appEventPack, name, value->value.int8_arr_v, value->arrSize);
67 }
68
AddInt16ParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)69 void AddInt16ParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
70 {
71 appEventPack->AddParam(name, value->value.int16_v);
72 }
73
AddInt16ArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)74 void AddInt16ArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
75 {
76 AddArrayParam(appEventPack, name, value->value.int16_arr_v, value->arrSize);
77 }
78
AddInt32ParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)79 void AddInt32ParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
80 {
81 appEventPack->AddParam(name, value->value.int32_v);
82 }
83
AddInt32ArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)84 void AddInt32ArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
85 {
86 AddArrayParam(appEventPack, name, value->value.int32_arr_v, value->arrSize);
87 }
88
AddInt64ParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)89 void AddInt64ParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
90 {
91 appEventPack->AddParam(name, value->value.int64_v);
92 }
93
AddInt64ArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)94 void AddInt64ArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
95 {
96 AddArrayParam(appEventPack, name, value->value.int64_arr_v, value->arrSize);
97 }
98
AddFloatParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)99 void AddFloatParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
100 {
101 appEventPack->AddParam(name, value->value.float_v);
102 }
103
AddFloatArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)104 void AddFloatArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
105 {
106 AddArrayParam(appEventPack, name, value->value.float_arr_v, value->arrSize);
107 }
108
AddDoubleParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)109 void AddDoubleParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
110 {
111 appEventPack->AddParam(name, value->value.double_v);
112 }
113
AddDoubleArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)114 void AddDoubleArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
115 {
116 AddArrayParam(appEventPack, name, value->value.double_arr_v, value->arrSize);
117 }
118
AddStringParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)119 void AddStringParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
120 {
121 appEventPack->AddParam(name, value->value.str_v);
122 }
123
AddStringArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)124 void AddStringArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
125 {
126 AddArrayParam(appEventPack, name, value->value.str_arr_v, value->arrSize);
127 }
128
129 const ParamAdder PARAM_ADDERS[] = {
130 &AddBoolParamValue,
131 &AddBoolArrayParamValue,
132 &AddInt8ParamValue,
133 &AddInt8ArrayParamValue,
134 &AddInt16ParamValue,
135 &AddInt16ArrayParamValue,
136 &AddInt32ParamValue,
137 &AddInt32ArrayParamValue,
138 &AddInt64ParamValue,
139 &AddInt64ArrayParamValue,
140 &AddFloatParamValue,
141 &AddFloatArrayParamValue,
142 &AddDoubleParamValue,
143 &AddDoubleArrayParamValue,
144 &AddStringParamValue,
145 &AddStringArrayParamValue
146 };
147
AddParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)148 void AddParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
149 {
150 if (name == nullptr || value == nullptr) {
151 HILOG_ERROR(LOG_CORE, "Failed to add the param because the name or value is null.");
152 return;
153 }
154 unsigned int paramType = value->type;
155 if (paramType < (sizeof(PARAM_ADDERS) / sizeof(PARAM_ADDERS[0]))) {
156 PARAM_ADDERS[paramType](appEventPack, name, value);
157 } else {
158 HILOG_ERROR(LOG_CORE, "Failed to add the param because the param type is unknown.");
159 }
160 }
161
AddParamEntry(std::shared_ptr<AppEventPack> & appEventPack,const ParamEntry * entry)162 void AddParamEntry(std::shared_ptr<AppEventPack>& appEventPack, const ParamEntry* entry)
163 {
164 if (entry == nullptr) {
165 HILOG_ERROR(LOG_CORE, "Failed to add the param because the entry is null.");
166 return;
167 }
168 AddParamValue(appEventPack, entry->name, entry->value);
169 }
170
AddParamList(std::shared_ptr<AppEventPack> & appEventPack,const ParamList list)171 void AddParamList(std::shared_ptr<AppEventPack>& appEventPack, const ParamList list)
172 {
173 ParamList curNode = list;
174 while (curNode != nullptr) {
175 AddParamEntry(appEventPack, curNode->entry);
176 curNode = curNode->next;
177 }
178 }
179 }
180
HiAppEventInnerConfigure(const char * name,const char * value)181 bool HiAppEventInnerConfigure(const char* name, const char* value)
182 {
183 if (name == nullptr || value == nullptr) {
184 HILOG_ERROR(LOG_CORE, "Failed to configure, because the input params contain a null pointer.");
185 return false;
186 }
187 return HiAppEventConfig::GetInstance().SetConfigurationItem(name, value);
188 }
189
HiAppEventInnerWrite(const char * domain,const char * name,EventType type,const ParamList list)190 int HiAppEventInnerWrite(const char* domain, const char* name, EventType type, const ParamList list)
191 {
192 if (domain == nullptr) {
193 HILOG_ERROR(LOG_CORE, "Failed to write event, domain is null");
194 return ErrorCode::ERROR_INVALID_EVENT_DOMAIN;
195 }
196 if (name == nullptr) {
197 HILOG_ERROR(LOG_CORE, "Failed to write event, name is null");
198 return ErrorCode::ERROR_INVALID_EVENT_NAME;
199 }
200
201 std::shared_ptr<AppEventPack> appEventPack = std::make_shared<AppEventPack>(domain, name, type);
202 AddParamList(appEventPack, list);
203 int res = VerifyAppEvent(appEventPack);
204 if (res >= 0) {
205 SubmitWritingTask(appEventPack, "app_c_event");
206 }
207 return res;
208 }
209
ClearData()210 void ClearData()
211 {
212 HiAppEventClean::ClearData(HiAppEventConfig::GetInstance().GetStorageDir());
213 }
214
HiAppEventCreateConfig()215 HiAppEvent_Config* HiAppEventCreateConfig()
216 {
217 auto ndkConfigMapPtr = new (std::nothrow) std::map<std::string, std::string>;
218 if (ndkConfigMapPtr == nullptr) {
219 HILOG_ERROR(LOG_CORE, "failed to new HiAppEvent_Config.");
220 }
221 return reinterpret_cast<HiAppEvent_Config *>(ndkConfigMapPtr);
222 }
223
HiAppEventSetConfigItem(HiAppEvent_Config * config,const char * itemName,const char * itemValue)224 int HiAppEventSetConfigItem(HiAppEvent_Config* config, const char* itemName, const char* itemValue)
225 {
226 if (config == nullptr) {
227 HILOG_ERROR(LOG_CORE, "Failed to Set Config Item, the event config is null.");
228 return ErrorCode::ERROR_EVENT_CONFIG_IS_NULL;
229 }
230
231 if (itemName == nullptr || std::strlen(itemName) > MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE) {
232 HILOG_ERROR(LOG_CORE, "Failed to Set Config Item, the itemName is nullptr or length is more than %{public}zu.",
233 MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE);
234 return ErrorCode::ERROR_INVALID_PARAM_VALUE;
235 }
236
237 std::string itemNameStr = itemName;
238 std::string itemValueStr = "";
239 if (itemValue != nullptr) {
240 if (std::strlen(itemValue) > MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE) {
241 HILOG_ERROR(LOG_CORE, "Failed to Set Config Item, the itemValue length is more than %{public}zu.",
242 MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE);
243 return ErrorCode::ERROR_INVALID_PARAM_VALUE;
244 }
245 itemValueStr = itemValue;
246 }
247 auto ndkConfigMapPtr = reinterpret_cast<std::map<std::string, std::string> *>(config);
248 (*ndkConfigMapPtr)[itemNameStr] = itemValueStr;
249 return ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL;
250 }
251
HiAppEventSetEventConfig(const char * name,HiAppEvent_Config * config)252 int HiAppEventSetEventConfig(const char* name, HiAppEvent_Config* config)
253 {
254 if (config == nullptr) {
255 HILOG_ERROR(LOG_CORE, "Failed to set event config, the event config is null.");
256 return ErrorCode::ERROR_INVALID_PARAM_VALUE;
257 }
258 if (name == nullptr || std::strlen(name) > MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE) {
259 HILOG_ERROR(LOG_CORE, "Failed to set event config, the name is nullptr or length more than %{public}zu.",
260 MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE);
261 return ErrorCode::ERROR_INVALID_PARAM_VALUE;
262 }
263
264 auto configMap = reinterpret_cast<std::map<std::string, std::string> *>(config);
265 int res = SetEventConfig(name, *configMap);
266 if (res != ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL) {
267 return ErrorCode::ERROR_INVALID_PARAM_VALUE;
268 }
269 return res;
270 }
271
HiAppEventDestroyConfig(HiAppEvent_Config * config)272 void HiAppEventDestroyConfig(HiAppEvent_Config* config)
273 {
274 if (config != nullptr) {
275 delete reinterpret_cast<std::map<std::string, std::string> *>(config);
276 config = nullptr;
277 }
278 }
279