• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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>
AddParamValue(ParamList list,const char * name,T value)41 ParamList AddParamValue(ParamList list, const char* name, T value)
42 {
43     if (list == nullptr || name == nullptr) {
44         HILOG_WARN(LOG_CORE, "ParamList is nullptr or name is nullptr.");
45         return list;
46     }
47     auto ndkAppEventPackPtr = reinterpret_cast<AppEventPack *>(list);
48     ndkAppEventPackPtr->AddParam(name, value);
49     return reinterpret_cast<ParamList>(ndkAppEventPackPtr);
50 }
51 
52 template<typename T>
AddParamArrayValue(ParamList list,const char * name,const T * arr,int len)53 ParamList AddParamArrayValue(ParamList list, const char* name, const T* arr, int len)
54 {
55     if (list == nullptr || name == nullptr || arr == nullptr) {
56         HILOG_WARN(LOG_CORE, "ParamList is nullptr or name is nullptr or param array is nullptr.");
57         return list;
58     }
59     std::vector<T> params(arr, (len > MAX_SIZE_OF_LIST_PARAM) ? (arr + MAX_SIZE_OF_LIST_PARAM + 1) : (arr + len));
60     auto ndkAppEventPackPtr = reinterpret_cast<AppEventPack *>(list);
61     ndkAppEventPackPtr->AddParam(name, params);
62     return reinterpret_cast<ParamList>(ndkAppEventPackPtr);
63 }
64 }
65 
HiAppEventCreateParamList()66 ParamList HiAppEventCreateParamList()
67 {
68     auto ndkAppEventPackPtr = new (std::nothrow) AppEventPack();
69     if (ndkAppEventPackPtr == nullptr) {
70         HILOG_ERROR(LOG_CORE, "failed to new ParamList.");
71     }
72     return reinterpret_cast<ParamList>(ndkAppEventPackPtr);
73 }
74 
HiAppEventDestroyParamList(ParamList list)75 void HiAppEventDestroyParamList(ParamList list)
76 {
77     if (list != nullptr) {
78         delete reinterpret_cast<AppEventPack *>(list);
79         list = nullptr;
80     }
81 }
82 
AddBoolParamValue(ParamList list,const char * name,bool boolean)83 ParamList AddBoolParamValue(ParamList list, const char* name, bool boolean)
84 {
85     return AddParamValue(list, name, boolean);
86 }
87 
AddBoolArrayParamValue(ParamList list,const char * name,const bool * booleans,int arrSize)88 ParamList AddBoolArrayParamValue(ParamList list, const char* name, const bool* booleans, int arrSize)
89 {
90     return AddParamArrayValue(list, name, booleans, arrSize);
91 }
92 
AddInt8ParamValue(ParamList list,const char * name,int8_t num)93 ParamList AddInt8ParamValue(ParamList list, const char* name, int8_t num)
94 {
95     return AddParamValue(list, name, num);
96 }
97 
AddInt8ArrayParamValue(ParamList list,const char * name,const int8_t * nums,int arrSize)98 ParamList AddInt8ArrayParamValue(ParamList list, const char* name, const int8_t* nums, int arrSize)
99 {
100     return AddParamArrayValue(list, name, nums, arrSize);
101 }
102 
AddInt16ParamValue(ParamList list,const char * name,int16_t num)103 ParamList AddInt16ParamValue(ParamList list, const char* name, int16_t num)
104 {
105     return AddParamValue(list, name, num);
106 }
107 
AddInt16ArrayParamValue(ParamList list,const char * name,const int16_t * nums,int arrSize)108 ParamList AddInt16ArrayParamValue(ParamList list, const char* name, const int16_t* nums, int arrSize)
109 {
110     return AddParamArrayValue(list, name, nums, arrSize);
111 }
112 
AddInt32ParamValue(ParamList list,const char * name,int32_t num)113 ParamList AddInt32ParamValue(ParamList list, const char* name, int32_t num)
114 {
115     return AddParamValue(list, name, num);
116 }
117 
AddInt32ArrayParamValue(ParamList list,const char * name,const int32_t * nums,int arrSize)118 ParamList AddInt32ArrayParamValue(ParamList list, const char* name, const int32_t* nums, int arrSize)
119 {
120     return AddParamArrayValue(list, name, nums, arrSize);
121 }
122 
AddInt64ParamValue(ParamList list,const char * name,int64_t num)123 ParamList AddInt64ParamValue(ParamList list, const char* name, int64_t num)
124 {
125     return AddParamValue(list, name, num);
126 }
127 
AddInt64ArrayParamValue(ParamList list,const char * name,const int64_t * nums,int arrSize)128 ParamList AddInt64ArrayParamValue(ParamList list, const char* name, const int64_t* nums, int arrSize)
129 {
130     return AddParamArrayValue(list, name, nums, arrSize);
131 }
132 
AddFloatParamValue(ParamList list,const char * name,float num)133 ParamList AddFloatParamValue(ParamList list, const char* name, float num)
134 {
135     return AddParamValue(list, name, num);
136 }
137 
AddFloatArrayParamValue(ParamList list,const char * name,const float * nums,int arrSize)138 ParamList AddFloatArrayParamValue(ParamList list, const char* name, const float* nums, int arrSize)
139 {
140     return AddParamArrayValue(list, name, nums, arrSize);
141 }
142 
AddDoubleParamValue(ParamList list,const char * name,double num)143 ParamList AddDoubleParamValue(ParamList list, const char* name, double num)
144 {
145     return AddParamValue(list, name, num);
146 }
147 
AddDoubleArrayParamValue(ParamList list,const char * name,const double * nums,int arrSize)148 ParamList AddDoubleArrayParamValue(ParamList list, const char* name, const double* nums, int arrSize)
149 {
150     return AddParamArrayValue(list, name, nums, arrSize);
151 }
152 
AddStringParamValue(ParamList list,const char * name,const char * str)153 ParamList AddStringParamValue(ParamList list, const char* name, const char* str)
154 {
155     if (str == nullptr) {
156         HILOG_WARN(LOG_CORE, "the str is nullptr.");
157         return list;
158     }
159     return AddParamValue(list, name, str);
160 }
161 
AddStringArrayParamValue(ParamList list,const char * name,const char * const * strs,int arrSize)162 ParamList AddStringArrayParamValue(ParamList list, const char* name, const char* const *strs, int arrSize)
163 {
164     return AddParamArrayValue(list, name, strs, arrSize);
165 }
166 
HiAppEventInnerConfigure(const char * name,const char * value)167 bool HiAppEventInnerConfigure(const char* name, const char* value)
168 {
169     if (name == nullptr || value == nullptr) {
170         HILOG_ERROR(LOG_CORE, "Failed to configure, because the input params contain a null pointer.");
171         return false;
172     }
173     return HiAppEventConfig::GetInstance().SetConfigurationItem(name, value);
174 }
175 
HiAppEventInnerWrite(const char * domain,const char * name,EventType type,const ParamList list)176 int HiAppEventInnerWrite(const char* domain, const char* name, EventType type, const ParamList list)
177 {
178     if (domain == nullptr) {
179         HILOG_ERROR(LOG_CORE, "Failed to write event, domain is null");
180         return ErrorCode::ERROR_INVALID_EVENT_DOMAIN;
181     }
182     if (name == nullptr) {
183         HILOG_ERROR(LOG_CORE, "Failed to write event, name is null");
184         return ErrorCode::ERROR_INVALID_EVENT_NAME;
185     }
186 
187     std::shared_ptr<AppEventPack> appEventPack = std::make_shared<AppEventPack>(domain, name, type);
188     if (list != nullptr) {
189         auto ndkAppEventPackPtr = reinterpret_cast<AppEventPack *>(list);
190         appEventPack->SetBaseParams(ndkAppEventPackPtr->GetBaseParams());
191     }
192     int res = VerifyAppEvent(appEventPack);
193     if (res >= 0) {
194         SubmitWritingTask(appEventPack, "app_c_event");
195     }
196     return res;
197 }
198 
ClearData()199 void ClearData()
200 {
201     HiAppEventClean::ClearData(HiAppEventConfig::GetInstance().GetStorageDir());
202 }
203 
HiAppEventCreateConfig()204 HiAppEvent_Config* HiAppEventCreateConfig()
205 {
206     auto ndkConfigMapPtr = new (std::nothrow) std::map<std::string, std::string>;
207     if (ndkConfigMapPtr == nullptr) {
208         HILOG_ERROR(LOG_CORE, "failed to new HiAppEvent_Config.");
209     }
210     return reinterpret_cast<HiAppEvent_Config *>(ndkConfigMapPtr);
211 }
212 
HiAppEventSetConfigItem(HiAppEvent_Config * config,const char * itemName,const char * itemValue)213 int HiAppEventSetConfigItem(HiAppEvent_Config* config, const char* itemName, const char* itemValue)
214 {
215     if (config == nullptr) {
216         HILOG_ERROR(LOG_CORE, "Failed to Set Config Item, the event config is null.");
217         return ErrorCode::ERROR_EVENT_CONFIG_IS_NULL;
218     }
219 
220     if (itemName == nullptr || std::strlen(itemName) > MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE) {
221         HILOG_ERROR(LOG_CORE, "Failed to Set Config Item, the itemName is nullptr or length is more than %{public}zu.",
222             MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE);
223         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
224     }
225 
226     std::string itemNameStr = itemName;
227     std::string itemValueStr = "";
228     if (itemValue != nullptr) {
229         if (std::strlen(itemValue) > MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE) {
230             HILOG_ERROR(LOG_CORE, "Failed to Set Config Item, the itemValue length is more than %{public}zu.",
231                 MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE);
232             return ErrorCode::ERROR_INVALID_PARAM_VALUE;
233         }
234         itemValueStr = itemValue;
235     }
236     auto ndkConfigMapPtr = reinterpret_cast<std::map<std::string, std::string> *>(config);
237     (*ndkConfigMapPtr)[itemNameStr] = itemValueStr;
238     return ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL;
239 }
240 
HiAppEventSetEventConfig(const char * name,HiAppEvent_Config * config)241 int HiAppEventSetEventConfig(const char* name, HiAppEvent_Config* config)
242 {
243     if (config == nullptr) {
244         HILOG_ERROR(LOG_CORE, "Failed to set event config, the event config is null.");
245         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
246     }
247     if (name == nullptr || std::strlen(name) > MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE) {
248         HILOG_ERROR(LOG_CORE, "Failed to set event config, the name is nullptr or length more than %{public}zu.",
249             MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE);
250         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
251     }
252 
253     auto configMap = reinterpret_cast<std::map<std::string, std::string> *>(config);
254     int res = HiAppEventConfig::GetInstance().SetEventConfig(name, *configMap);
255     if (res != ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL) {
256         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
257     }
258     return res;
259 }
260 
HiAppEventDestroyConfig(HiAppEvent_Config * config)261 void HiAppEventDestroyConfig(HiAppEvent_Config* config)
262 {
263     if (config != nullptr) {
264         delete reinterpret_cast<std::map<std::string, std::string> *>(config);
265         config = nullptr;
266     }
267 }
268