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 "softbus_client_event_manager.h"
17 
18 #include "comm_log.h"
19 #include "softbus.h"
20 #include "softbus_adapter_mem.h"
21 #include "softbus_adapter_thread.h"
22 #include "softbus_errcode.h"
23 #include "softbus_utils.h"
24 
25 #define MAX_OBSERVER_CNT 128
26 
27 typedef struct {
28     ListNode node;
29     enum SoftBusEvent event;
30     EventCallback callback;
31     char *userData;
32 } Observer;
33 
34 static SoftBusList *g_observerList = NULL;
35 static bool g_isInit = false;
36 
EventClientInit(void)37 int EventClientInit(void)
38 {
39     if (g_isInit) {
40         return SOFTBUS_OK;
41     }
42 
43     if (g_observerList != NULL) {
44         DestroySoftBusList(g_observerList);
45     }
46     g_observerList = CreateSoftBusList();
47     if (g_observerList == NULL) {
48         COMM_LOGE(COMM_SDK, "create observer list failed");
49         return SOFTBUS_MALLOC_ERR;
50     }
51 
52     g_isInit = true;
53     return SOFTBUS_OK;
54 }
55 
EventClientDeinit(void)56 void EventClientDeinit(void)
57 {
58     if (!g_isInit) {
59         COMM_LOGE(COMM_SDK, "event client not init");
60         return;
61     }
62     if (g_observerList != NULL) {
63         DestroySoftBusList(g_observerList);
64         g_observerList = NULL;
65     }
66 
67     g_isInit = false;
68 }
69 
IsEventValid(enum SoftBusEvent event)70 static bool IsEventValid(enum SoftBusEvent event)
71 {
72     if (event < EVENT_SERVER_DEATH || event >= EVENT_BUTT) {
73         return false;
74     }
75     return true;
76 }
77 
RegisterEventCallback(enum SoftBusEvent event,EventCallback cb,void * userData)78 int RegisterEventCallback(enum SoftBusEvent event, EventCallback cb, void *userData)
79 {
80     if (!IsEventValid(event) || cb == NULL) {
81         COMM_LOGE(COMM_SDK, "invalid param");
82         return SOFTBUS_INVALID_PARAM;
83     }
84 
85     if (g_isInit != true) {
86         COMM_LOGE(COMM_SDK, "event manager not init");
87         return SOFTBUS_NO_INIT;
88     }
89 
90     if (SoftBusMutexLock(&g_observerList->lock) != SOFTBUS_OK) {
91         COMM_LOGE(COMM_SDK, "lock failed");
92         return SOFTBUS_LOCK_ERR;
93     }
94 
95     if (g_observerList->cnt >= MAX_OBSERVER_CNT) {
96         COMM_LOGE(COMM_SDK, "observer count over limit");
97         (void)SoftBusMutexUnlock(&g_observerList->lock);
98         return SOFTBUS_TRANS_OBSERVER_EXCEED_LIMIT;
99     }
100 
101     Observer *observer = (Observer *)SoftBusCalloc(sizeof(Observer));
102     if (observer == NULL) {
103         COMM_LOGE(COMM_SDK, "malloc observer failed");
104         (void)SoftBusMutexUnlock(&g_observerList->lock);
105         return SOFTBUS_MALLOC_ERR;
106     }
107 
108     observer->event = event;
109     observer->callback = cb;
110     observer->userData = (char *)userData;
111 
112     ListInit(&observer->node);
113     ListAdd(&g_observerList->list, &observer->node);
114     g_observerList->cnt++;
115     (void)SoftBusMutexUnlock(&g_observerList->lock);
116     return SOFTBUS_OK;
117 }
118 
CLIENT_NotifyObserver(enum SoftBusEvent event,void * arg,unsigned int argLen)119 void CLIENT_NotifyObserver(enum SoftBusEvent event, void *arg, unsigned int argLen)
120 {
121     if (!IsEventValid(event)) {
122         COMM_LOGE(COMM_SDK, "invalid event. event=%{public}d", event);
123         return;
124     }
125 
126     if (g_isInit != true) {
127         COMM_LOGE(COMM_SDK, "event manager not init");
128         return;
129     }
130 
131     Observer *observer = NULL;
132     if (SoftBusMutexLock(&g_observerList->lock) != SOFTBUS_OK) {
133         COMM_LOGE(COMM_SDK, "lock failed");
134         return;
135     }
136 
137     LIST_FOR_EACH_ENTRY(observer, &g_observerList->list, Observer, node) {
138         if ((observer->event == event) && (observer->callback != NULL) &&
139             (observer->callback(arg, argLen, observer->userData) != SOFTBUS_OK)) {
140             COMM_LOGE(COMM_SDK, "execute callback failed. event=%{public}d", event);
141         }
142     }
143 
144     (void)SoftBusMutexUnlock(&g_observerList->lock);
145 }
146