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 "ndk_app_event_watcher_service.h"
17 
18 #include "ndk_app_event_watcher_proxy.h"
19 #include "hiappevent_base.h"
20 #include "hiappevent_config.h"
21 
22 using namespace OHOS::HiviewDFX;
23 
24 #ifndef CHECK_WATCHER_PTR
25 #define CHECK_WATCHER_PTR(ptr)                  \
26     if ((ptr) == nullptr) {                     \
27         return;                                 \
28     }
29 #endif
30 
31 #ifndef CHECK_WATCHER_PTR_AND_RETURN
32 #define CHECK_WATCHER_PTR_AND_RETURN(ptr, ret)  \
33     if ((ptr) == nullptr) {                     \
34         return ret;                             \
35     }
36 #endif
37 
CreateWatcher(const char * name)38 struct HiAppEvent_Watcher* CreateWatcher(const char *name)
39 {
40     CHECK_WATCHER_PTR_AND_RETURN(name, nullptr)
41     auto ndkWatcherPtr = new (std::nothrow) NdkAppEventWatcherProxy(name);
42     return reinterpret_cast<HiAppEvent_Watcher *>(ndkWatcherPtr);
43 }
44 
SetAppEventFilter(struct HiAppEvent_Watcher * watcher,const char * domain,uint8_t eventTypes,const char * const * names,int namesLen)45 int SetAppEventFilter(struct HiAppEvent_Watcher* watcher, const char* domain, uint8_t eventTypes,
46                       const char* const* names, int namesLen)
47 {
48     CHECK_WATCHER_PTR_AND_RETURN(watcher, ErrorCode::ERROR_INVALID_WATCHER)
49     auto ndkWatcher = reinterpret_cast<NdkAppEventWatcherProxy *>(watcher);
50     return ndkWatcher->SetAppEventFilter(domain, eventTypes, names, namesLen);
51 }
52 
SetTriggerCondition(struct HiAppEvent_Watcher * watcher,int row,int size,int timeOut)53 int SetTriggerCondition(struct HiAppEvent_Watcher* watcher, int row, int size, int timeOut)
54 {
55     CHECK_WATCHER_PTR_AND_RETURN(watcher, ErrorCode::ERROR_INVALID_WATCHER);
56     auto ndkWatcher = reinterpret_cast<NdkAppEventWatcherProxy *>(watcher);
57     return ndkWatcher->SetTriggerCondition(row, size, timeOut);
58 }
59 
SetWatcherOnTrigger(struct HiAppEvent_Watcher * watcher,OH_HiAppEvent_OnTrigger onTrigger)60 int SetWatcherOnTrigger(struct HiAppEvent_Watcher* watcher, OH_HiAppEvent_OnTrigger onTrigger)
61 {
62     CHECK_WATCHER_PTR_AND_RETURN(watcher, ErrorCode::ERROR_INVALID_WATCHER)
63     auto ndkWatcher = reinterpret_cast<NdkAppEventWatcherProxy *>(watcher);
64     return ndkWatcher->SetWatcherOnTrigger(onTrigger);
65 }
66 
SetWatcherOnReceiver(struct HiAppEvent_Watcher * watcher,OH_HiAppEvent_OnReceive onReceiver)67 int SetWatcherOnReceiver(struct HiAppEvent_Watcher* watcher, OH_HiAppEvent_OnReceive onReceiver)
68 {
69     CHECK_WATCHER_PTR_AND_RETURN(watcher, ErrorCode::ERROR_INVALID_WATCHER)
70     auto ndkWatcher = reinterpret_cast<NdkAppEventWatcherProxy *>(watcher);
71     return ndkWatcher->SetWatcherOnReceiver(onReceiver);
72 }
73 
AddWatcher(struct HiAppEvent_Watcher * watcher)74 int AddWatcher(struct HiAppEvent_Watcher* watcher)
75 {
76     CHECK_WATCHER_PTR_AND_RETURN(watcher, ErrorCode::ERROR_INVALID_WATCHER)
77     auto ndkWatcher = reinterpret_cast<NdkAppEventWatcherProxy *>(watcher);
78     return ndkWatcher->AddWatcher();
79 }
80 
TakeWatcherData(struct HiAppEvent_Watcher * watcher,uint32_t size,OH_HiAppEvent_OnTake onTake)81 int TakeWatcherData(struct HiAppEvent_Watcher* watcher, uint32_t size, OH_HiAppEvent_OnTake onTake)
82 {
83     CHECK_WATCHER_PTR_AND_RETURN(watcher, ErrorCode::ERROR_INVALID_WATCHER)
84     return reinterpret_cast<NdkAppEventWatcherProxy *>(watcher)->TakeWatcherData(size, onTake);
85 }
86 
RemoveWatcher(struct HiAppEvent_Watcher * watcher)87 int RemoveWatcher(struct HiAppEvent_Watcher *watcher)
88 {
89     CHECK_WATCHER_PTR_AND_RETURN(watcher, ErrorCode::ERROR_INVALID_WATCHER)
90     return reinterpret_cast<NdkAppEventWatcherProxy *>(watcher)->RemoveWatcher();
91 }
92 
DestroyWatcher(struct HiAppEvent_Watcher * watcher)93 void DestroyWatcher(struct HiAppEvent_Watcher* watcher)
94 {
95     CHECK_WATCHER_PTR(watcher)
96     auto *watcherProxy = reinterpret_cast<NdkAppEventWatcherProxy *>(watcher);
97     watcherProxy->RemoveWatcher();
98     delete watcherProxy;
99 }
100