1 /*
2 * Copyright (c) 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 "common_event_constant.h"
17 #include "event_log_wrapper.h"
18 #include "oh_commonevent.h"
19 #include "oh_commonevent_parameters_parse.h"
20 #include "oh_commonevent_wrapper.h"
21 #include "securec.h"
22 #include <cstdlib>
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
OH_CommonEvent_CreateSubscribeInfo(const char * events[],int32_t eventsNum)28 CommonEvent_SubscribeInfo* OH_CommonEvent_CreateSubscribeInfo(const char* events[], int32_t eventsNum)
29 {
30 if (eventsNum == 0) {
31 EVENT_LOGE("Events is empty");
32 return nullptr;
33 }
34 CommonEvent_SubscribeInfo *subscribeInfo = new CommonEvent_SubscribeInfo();
35 if (subscribeInfo == nullptr) {
36 EVENT_LOGE("Failed to create subscribeInfo");
37 return nullptr;
38 }
39 int count = 0;
40 for (int i = 0; i < eventsNum; i++) {
41 if (events + i != nullptr) {
42 count++;
43 }
44 }
45 subscribeInfo->events = new char* [count];
46 for (int i = 0; i < eventsNum; i++) {
47 if (events + i != nullptr) {
48 subscribeInfo->events[subscribeInfo->eventLength] = OHOS::EventFwk::MallocCString(events[i]);
49 subscribeInfo->eventLength++;
50 }
51 }
52 return subscribeInfo;
53 }
54
OH_CommonEvent_SetPublisherPermission(CommonEvent_SubscribeInfo * info,const char * permission)55 CommonEvent_ErrCode OH_CommonEvent_SetPublisherPermission(CommonEvent_SubscribeInfo* info, const char* permission)
56 {
57 if (info == nullptr) {
58 EVENT_LOGE("Invalid para");
59 return COMMONEVENT_ERR_INVALID_PARAMETER;
60 }
61 info->permission = OHOS::EventFwk::MallocCString(permission);
62 return COMMONEVENT_ERR_OK;
63 }
64
OH_CommonEvent_SetPublisherBundleName(CommonEvent_SubscribeInfo * info,const char * bundleName)65 CommonEvent_ErrCode OH_CommonEvent_SetPublisherBundleName(CommonEvent_SubscribeInfo* info, const char* bundleName)
66 {
67 if (info == nullptr) {
68 EVENT_LOGE("Invalid para");
69 return COMMONEVENT_ERR_INVALID_PARAMETER;
70 }
71 info->bundleName = OHOS::EventFwk::MallocCString(bundleName);
72 return COMMONEVENT_ERR_OK;
73 }
74
OH_CommonEvent_DestroySubscribeInfo(CommonEvent_SubscribeInfo * info)75 void OH_CommonEvent_DestroySubscribeInfo(CommonEvent_SubscribeInfo* info)
76 {
77 if (info != nullptr) {
78 for (uint32_t i = 0; i < info->eventLength; i++) {
79 free(info->events[i]);
80 info->events[i] = nullptr;
81 }
82 delete[] info->events;
83 info->events = nullptr;
84 free(info->permission);
85 info->permission = nullptr;
86 free(info->bundleName);
87 info->bundleName = nullptr;
88 delete info;
89 }
90 }
91
OH_CommonEvent_CreateSubscriber(const CommonEvent_SubscribeInfo * info,CommonEvent_ReceiveCallback callback)92 CommonEvent_Subscriber* OH_CommonEvent_CreateSubscriber(const CommonEvent_SubscribeInfo* info,
93 CommonEvent_ReceiveCallback callback)
94 {
95 return SubscriberManager::GetInstance()->CreateSubscriber(info, callback);
96 }
97
OH_CommonEvent_DestroySubscriber(CommonEvent_Subscriber * subscriber)98 void OH_CommonEvent_DestroySubscriber(CommonEvent_Subscriber* subscriber)
99 {
100 SubscriberManager::GetInstance()->DestroySubscriber(subscriber);
101 }
102
OH_CommonEvent_Subscribe(const CommonEvent_Subscriber * subscriber)103 CommonEvent_ErrCode OH_CommonEvent_Subscribe(const CommonEvent_Subscriber* subscriber)
104 {
105 return SubscriberManager::GetInstance()->Subscribe(subscriber);
106 }
107
OH_CommonEvent_UnSubscribe(const CommonEvent_Subscriber * subscriber)108 CommonEvent_ErrCode OH_CommonEvent_UnSubscribe(const CommonEvent_Subscriber* subscriber)
109 {
110 return SubscriberManager::GetInstance()->UnSubscribe(subscriber);
111 }
112
OH_CommonEvent_GetEventFromRcvData(const CommonEvent_RcvData * rcvData)113 const char* OH_CommonEvent_GetEventFromRcvData(const CommonEvent_RcvData* rcvData)
114 {
115 return rcvData->event;
116 }
117
OH_CommonEvent_GetCodeFromRcvData(const CommonEvent_RcvData * rcvData)118 int32_t OH_CommonEvent_GetCodeFromRcvData(const CommonEvent_RcvData* rcvData)
119 {
120 return rcvData->code;
121 }
122
OH_CommonEvent_GetDataStrFromRcvData(const CommonEvent_RcvData * rcvData)123 const char* OH_CommonEvent_GetDataStrFromRcvData(const CommonEvent_RcvData* rcvData)
124 {
125 return rcvData->data;
126 }
127
OH_CommonEvent_GetBundleNameFromRcvData(const CommonEvent_RcvData * rcvData)128 const char* OH_CommonEvent_GetBundleNameFromRcvData(const CommonEvent_RcvData* rcvData)
129 {
130 return rcvData->bundleName;
131 }
132
OH_CommonEvent_GetParametersFromRcvData(const CommonEvent_RcvData * rcvData)133 const CommonEvent_Parameters* OH_CommonEvent_GetParametersFromRcvData(const CommonEvent_RcvData* rcvData)
134 {
135 return rcvData->parameters;
136 }
137
OH_CommonEvent_HasKeyInParameters(const CommonEvent_Parameters * para,const char * key)138 bool OH_CommonEvent_HasKeyInParameters(const CommonEvent_Parameters* para, const char* key)
139 {
140 if (para == nullptr || key == nullptr) {
141 EVENT_LOGE("Invalid para");
142 return false;
143 }
144 auto parameters = reinterpret_cast<const CArrParameters*>(para);
145 if (parameters == nullptr) {
146 EVENT_LOGE("Invalid para");
147 return false;
148 }
149 return OHOS::EventFwk::HasKeyFromParameters(parameters, key);
150 }
151
OH_CommonEvent_GetIntFromParameters(const CommonEvent_Parameters * para,const char * key,const int defaultValue)152 int OH_CommonEvent_GetIntFromParameters(const CommonEvent_Parameters* para, const char* key, const int defaultValue)
153 {
154 if (para == nullptr || key == nullptr) {
155 EVENT_LOGE("Invalid para");
156 return defaultValue;
157 }
158 auto parameters = reinterpret_cast<const CArrParameters*>(para);
159 if (parameters == nullptr) {
160 EVENT_LOGE("Invalid para");
161 return defaultValue;
162 }
163 return OHOS::EventFwk::GetDataFromParams<int>(parameters, key, OHOS::EventFwk::I32_TYPE, defaultValue);
164 }
165
OH_CommonEvent_GetIntArrayFromParameters(const CommonEvent_Parameters * para,const char * key,int ** array)166 int OH_CommonEvent_GetIntArrayFromParameters(const CommonEvent_Parameters* para, const char* key, int** array)
167 {
168 if (para == nullptr || key == nullptr || array == nullptr) {
169 EVENT_LOGE("Invalid para");
170 return 0;
171 }
172 auto parameters = reinterpret_cast<const CArrParameters*>(para);
173 if (parameters == nullptr) {
174 EVENT_LOGE("Invalid para");
175 return 0;
176 }
177 return OHOS::EventFwk::GetDataArrayFromParams<int>(parameters, key, OHOS::EventFwk::I32_PTR_TYPE, array);
178 }
179
OH_CommonEvent_GetLongFromParameters(const CommonEvent_Parameters * para,const char * key,const long defaultValue)180 long OH_CommonEvent_GetLongFromParameters(const CommonEvent_Parameters* para, const char* key,
181 const long defaultValue)
182 {
183 if (para == nullptr || key == nullptr) {
184 EVENT_LOGE("Invalid para");
185 return defaultValue;
186 }
187 auto parameters = reinterpret_cast<const CArrParameters*>(para);
188 if (parameters == nullptr) {
189 EVENT_LOGE("Invalid para");
190 return defaultValue;
191 }
192 return OHOS::EventFwk::GetDataFromParams<long>(parameters, key, OHOS::EventFwk::I64_TYPE, defaultValue);
193 }
194
OH_CommonEvent_GetLongArrayFromParameters(const CommonEvent_Parameters * para,const char * key,long ** array)195 int32_t OH_CommonEvent_GetLongArrayFromParameters(const CommonEvent_Parameters* para, const char* key, long** array)
196 {
197 if (para == nullptr || key == nullptr || array == nullptr) {
198 EVENT_LOGE("Invalid para");
199 return 0;
200 }
201 auto parameters = reinterpret_cast<const CArrParameters*>(para);
202 if (parameters == nullptr) {
203 EVENT_LOGE("Invalid para");
204 return 0;
205 }
206 return OHOS::EventFwk::GetDataArrayFromParams<long>(parameters, key, OHOS::EventFwk::I64_PTR_TYPE, array);
207 }
208
OH_CommonEvent_GetBoolFromParameters(const CommonEvent_Parameters * para,const char * key,const bool defaultValue)209 bool OH_CommonEvent_GetBoolFromParameters(const CommonEvent_Parameters* para, const char* key,
210 const bool defaultValue)
211 {
212 if (para == nullptr || key == nullptr) {
213 EVENT_LOGE("Invalid para");
214 return defaultValue;
215 }
216 auto parameters = reinterpret_cast<const CArrParameters*>(para);
217 if (parameters == nullptr) {
218 EVENT_LOGE("Invalid para");
219 return defaultValue;
220 }
221 return OHOS::EventFwk::GetDataFromParams<bool>(parameters, key, OHOS::EventFwk::BOOL_TYPE, defaultValue);
222 }
223
OH_CommonEvent_GetBoolArrayFromParameters(const CommonEvent_Parameters * para,const char * key,bool ** array)224 int32_t OH_CommonEvent_GetBoolArrayFromParameters(const CommonEvent_Parameters* para, const char* key, bool** array)
225 {
226 if (para == nullptr || key == nullptr || array == nullptr) {
227 EVENT_LOGE("Invalid para");
228 return 0;
229 }
230 auto parameters = reinterpret_cast<const CArrParameters*>(para);
231 if (parameters == nullptr) {
232 EVENT_LOGE("Invalid para");
233 return 0;
234 }
235 return OHOS::EventFwk::GetDataArrayFromParams<bool>(parameters, key, OHOS::EventFwk::BOOL_PTR_TYPE, array);
236 }
237
OH_CommonEvent_GetCharFromParameters(const CommonEvent_Parameters * para,const char * key,const char defaultValue)238 char OH_CommonEvent_GetCharFromParameters(const CommonEvent_Parameters* para, const char* key,
239 const char defaultValue)
240 {
241 if (para == nullptr || key == nullptr) {
242 EVENT_LOGE("Invalid para");
243 return defaultValue;
244 }
245 auto parameters = reinterpret_cast<const CArrParameters*>(para);
246 if (parameters == nullptr) {
247 EVENT_LOGE("Invalid para");
248 return defaultValue;
249 }
250 return OHOS::EventFwk::GetDataFromParams<char>(parameters, key, OHOS::EventFwk::CHAR_TYPE, defaultValue);
251 }
252
OH_CommonEvent_GetCharArrayFromParameters(const CommonEvent_Parameters * para,const char * key,char ** array)253 int32_t OH_CommonEvent_GetCharArrayFromParameters(const CommonEvent_Parameters* para, const char* key, char** array)
254 {
255 if (para == nullptr || key == nullptr || array == nullptr) {
256 EVENT_LOGE("Invalid para");
257 return 0;
258 }
259 auto parameters = reinterpret_cast<const CArrParameters*>(para);
260 if (parameters == nullptr) {
261 EVENT_LOGE("Invalid para");
262 return 0;
263 }
264 return OHOS::EventFwk::GetDataArrayFromParams<char>(parameters, key, OHOS::EventFwk::STR_TYPE, array);
265 }
266
OH_CommonEvent_GetDoubleFromParameters(const CommonEvent_Parameters * para,const char * key,const double defaultValue)267 double OH_CommonEvent_GetDoubleFromParameters(const CommonEvent_Parameters* para, const char* key,
268 const double defaultValue)
269 {
270 if (para == nullptr || key == nullptr) {
271 EVENT_LOGE("Invalid para");
272 return defaultValue;
273 }
274 auto parameters = reinterpret_cast<const CArrParameters*>(para);
275 if (parameters == nullptr) {
276 EVENT_LOGE("Invalid para");
277 return defaultValue;
278 }
279 return OHOS::EventFwk::GetDataFromParams<double>(parameters, key, OHOS::EventFwk::DOUBLE_TYPE, defaultValue);
280 }
281
OH_CommonEvent_GetDoubleArrayFromParameters(const CommonEvent_Parameters * para,const char * key,double ** array)282 int32_t OH_CommonEvent_GetDoubleArrayFromParameters(const CommonEvent_Parameters* para, const char* key,
283 double** array)
284 {
285 if (para == nullptr || key == nullptr || array == nullptr) {
286 EVENT_LOGE("Invalid para");
287 return 0;
288 }
289 auto parameters = reinterpret_cast<const CArrParameters*>(para);
290 if (parameters == nullptr) {
291 EVENT_LOGE("Invalid para");
292 return 0;
293 }
294 return OHOS::EventFwk::GetDataArrayFromParams<double>(parameters, key, OHOS::EventFwk::DOUBLE_PTR_TYPE, array);
295 }
296 #ifdef __cplusplus
297 }
298 #endif