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