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