1 /*
2  * Copyright (C) 2021-2022 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 "broadcast_manager.h"
17 #include "common_defs.h"
18 #include "device_auth_defines.h"
19 #include "group_operation_common.h"
20 #include "hc_log.h"
21 #include "hc_mutex.h"
22 #include "hc_types.h"
23 #include "hc_vector.h"
24 #include "securec.h"
25 
26 typedef struct {
27     char *appId;
28     DataChangeListener *listener;
29 } ListenerEntry;
30 
31 DECLARE_HC_VECTOR(ListenerEntryVec, ListenerEntry);
32 IMPLEMENT_HC_VECTOR(ListenerEntryVec, ListenerEntry, 1);
33 static ListenerEntryVec g_listenerEntryVec;
34 static HcMutex *g_broadcastMutex = NULL;
35 
PostOnGroupCreated(const char * messageStr)36 static void PostOnGroupCreated(const char *messageStr)
37 {
38     if (messageStr == NULL) {
39         LOGE("The messageStr is NULL!");
40         return;
41     }
42     uint32_t index;
43     ListenerEntry *entry = NULL;
44     g_broadcastMutex->lock(g_broadcastMutex);
45     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
46         if (entry->listener->onGroupCreated != NULL) {
47             LOGI("[Broadcaster]: PostOnGroupCreated! [AppId]: %s", entry->appId);
48             entry->listener->onGroupCreated(messageStr);
49         }
50     }
51     g_broadcastMutex->unlock(g_broadcastMutex);
52 }
53 
PostOnGroupDeleted(const char * messageStr)54 static void PostOnGroupDeleted(const char *messageStr)
55 {
56     if (messageStr == NULL) {
57         LOGE("The messageStr is NULL!");
58         return;
59     }
60     uint32_t index;
61     ListenerEntry *entry = NULL;
62     g_broadcastMutex->lock(g_broadcastMutex);
63     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
64         if (entry->listener->onGroupDeleted != NULL) {
65             LOGI("[Broadcaster]: PostOnGroupDeleted! [AppId]: %s", entry->appId);
66             entry->listener->onGroupDeleted(messageStr);
67         }
68     }
69     g_broadcastMutex->unlock(g_broadcastMutex);
70 }
71 
PostOnDeviceBound(const char * peerUdid,const char * messageStr)72 static void PostOnDeviceBound(const char *peerUdid, const char *messageStr)
73 {
74     if ((peerUdid == NULL) || (messageStr == NULL)) {
75         LOGE("The peerUdid or messageStr is NULL!");
76         return;
77     }
78     uint32_t index;
79     ListenerEntry *entry = NULL;
80     g_broadcastMutex->lock(g_broadcastMutex);
81     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
82         if (entry->listener->onDeviceBound != NULL) {
83             LOGI("[Broadcaster]: PostOnDeviceBound! [AppId]: %s", entry->appId);
84             entry->listener->onDeviceBound(peerUdid, messageStr);
85         }
86     }
87     g_broadcastMutex->unlock(g_broadcastMutex);
88 }
89 
PostOnDeviceUnBound(const char * peerUdid,const char * messageStr)90 static void PostOnDeviceUnBound(const char *peerUdid, const char *messageStr)
91 {
92     if ((peerUdid == NULL) || (messageStr == NULL)) {
93         LOGE("The peerUdid or messageStr is NULL!");
94         return;
95     }
96     uint32_t index;
97     ListenerEntry *entry = NULL;
98     g_broadcastMutex->lock(g_broadcastMutex);
99     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
100         if (entry->listener->onDeviceUnBound != NULL) {
101             LOGI("[Broadcaster]: PostOnDeviceUnBound! [AppId]: %s", entry->appId);
102             entry->listener->onDeviceUnBound(peerUdid, messageStr);
103         }
104     }
105     g_broadcastMutex->unlock(g_broadcastMutex);
106 }
107 
PostOnDeviceNotTrusted(const char * peerUdid)108 static void PostOnDeviceNotTrusted(const char *peerUdid)
109 {
110     if (peerUdid == NULL) {
111         LOGE("The peerUdid is NULL!");
112         return;
113     }
114     uint32_t index;
115     ListenerEntry *entry = NULL;
116     g_broadcastMutex->lock(g_broadcastMutex);
117     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
118         if (entry->listener->onDeviceNotTrusted != NULL) {
119             LOGI("[Broadcaster]: PostOnDeviceNotTrusted! [AppId]: %s", entry->appId);
120             entry->listener->onDeviceNotTrusted(peerUdid);
121         }
122     }
123     g_broadcastMutex->unlock(g_broadcastMutex);
124 }
125 
PostOnLastGroupDeleted(const char * peerUdid,int groupType)126 static void PostOnLastGroupDeleted(const char *peerUdid, int groupType)
127 {
128     if (peerUdid == NULL) {
129         LOGE("The peerUdid is NULL!");
130         return;
131     }
132     uint32_t index;
133     ListenerEntry *entry = NULL;
134     g_broadcastMutex->lock(g_broadcastMutex);
135     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
136         if (entry->listener->onLastGroupDeleted != NULL) {
137             LOGI("[Broadcaster]: PostOnLastGroupDeleted! [AppId]: %s, [GroupType]: %d", entry->appId, groupType);
138             entry->listener->onLastGroupDeleted(peerUdid, groupType);
139         }
140     }
141     g_broadcastMutex->unlock(g_broadcastMutex);
142 }
143 
PostOnTrustedDeviceNumChanged(int curTrustedDeviceNum)144 static void PostOnTrustedDeviceNumChanged(int curTrustedDeviceNum)
145 {
146     uint32_t index;
147     ListenerEntry *entry = NULL;
148     g_broadcastMutex->lock(g_broadcastMutex);
149     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
150         if (entry->listener->onTrustedDeviceNumChanged != NULL) {
151             LOGI("[Broadcaster]: PostOnTrustedDeviceNumChanged! [AppId]: %s", entry->appId);
152             entry->listener->onTrustedDeviceNumChanged(curTrustedDeviceNum);
153         }
154     }
155     g_broadcastMutex->unlock(g_broadcastMutex);
156 }
157 
UpdateListenerIfExist(const char * appId,const DataChangeListener * listener)158 static int32_t UpdateListenerIfExist(const char *appId, const DataChangeListener *listener)
159 {
160     uint32_t index;
161     ListenerEntry *entry = NULL;
162     g_broadcastMutex->lock(g_broadcastMutex);
163     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
164         if (strcmp(entry->appId, appId) == 0) {
165             if (memcpy_s(entry->listener, sizeof(DataChangeListener),
166                 listener, sizeof(DataChangeListener)) != HC_SUCCESS) {
167                 g_broadcastMutex->unlock(g_broadcastMutex);
168                 LOGE("Failed to copy listener!");
169                 return HC_ERR_MEMORY_COPY;
170             }
171             g_broadcastMutex->unlock(g_broadcastMutex);
172             LOGI("Successfully updated a listener. [AppId]: %s", appId);
173             return HC_SUCCESS;
174         }
175     }
176     g_broadcastMutex->unlock(g_broadcastMutex);
177     return HC_ERR_LISTENER_NOT_EXIST;
178 }
179 
AddListenerIfNotExist(const char * appId,const DataChangeListener * listener)180 static int32_t AddListenerIfNotExist(const char *appId, const DataChangeListener *listener)
181 {
182     uint32_t appIdLen = HcStrlen(appId) + 1;
183     char *copyAppId = (char *)HcMalloc(appIdLen, 0);
184     if (copyAppId == NULL) {
185         LOGE("Failed to allocate copyAppId memory!");
186         return HC_ERR_ALLOC_MEMORY;
187     }
188     if (strcpy_s(copyAppId, appIdLen, appId) != HC_SUCCESS) {
189         LOGE("Failed to copy appId!");
190         HcFree(copyAppId);
191         return HC_ERR_MEMORY_COPY;
192     }
193     DataChangeListener *copyListener = (DataChangeListener *)HcMalloc(sizeof(DataChangeListener), 0);
194     if (copyListener == NULL) {
195         LOGE("Failed to allocate saveCallback memory!");
196         HcFree(copyAppId);
197         return HC_ERR_ALLOC_MEMORY;
198     }
199     if (memcpy_s(copyListener, sizeof(DataChangeListener),
200         listener, sizeof(DataChangeListener)) != HC_SUCCESS) {
201         LOGE("Failed to copy listener!");
202         HcFree(copyAppId);
203         HcFree(copyListener);
204         return HC_ERR_MEMORY_COPY;
205     }
206     ListenerEntry entry;
207     entry.appId = copyAppId;
208     entry.listener = copyListener;
209     g_broadcastMutex->lock(g_broadcastMutex);
210     g_listenerEntryVec.pushBack(&g_listenerEntryVec, &entry);
211     g_broadcastMutex->unlock(g_broadcastMutex);
212     LOGI("Successfully added a listener. [AppId]: %s", appId);
213     return HC_SUCCESS;
214 }
215 
216 static Broadcaster g_broadcaster = {
217     .postOnGroupCreated = PostOnGroupCreated,
218     .postOnGroupDeleted = PostOnGroupDeleted,
219     .postOnDeviceBound = PostOnDeviceBound,
220     .postOnDeviceUnBound = PostOnDeviceUnBound,
221     .postOnDeviceNotTrusted = PostOnDeviceNotTrusted,
222     .postOnLastGroupDeleted = PostOnLastGroupDeleted,
223     .postOnTrustedDeviceNumChanged = PostOnTrustedDeviceNumChanged
224 };
225 
IsBroadcastSupported(void)226 bool IsBroadcastSupported(void)
227 {
228     return true;
229 }
230 
InitBroadcastManager(void)231 int32_t InitBroadcastManager(void)
232 {
233     if (g_broadcastMutex == NULL) {
234         g_broadcastMutex = (HcMutex *)HcMalloc(sizeof(HcMutex), 0);
235         if (g_broadcastMutex == NULL) {
236             LOGE("Failed to allocate broadcast mutex memory!");
237             return HC_ERR_ALLOC_MEMORY;
238         }
239         if (InitHcMutex(g_broadcastMutex) != HC_SUCCESS) {
240             LOGE("Init mutex failed");
241             HcFree(g_broadcastMutex);
242             g_broadcastMutex = NULL;
243             return HC_ERROR;
244         }
245     }
246     g_listenerEntryVec = CREATE_HC_VECTOR(ListenerEntryVec);
247     LOGI("[Broadcaster]: Init broadcast manager module successfully!");
248     return HC_SUCCESS;
249 }
250 
DestroyBroadcastManager(void)251 void DestroyBroadcastManager(void)
252 {
253     uint32_t index;
254     ListenerEntry *entry = NULL;
255     g_broadcastMutex->lock(g_broadcastMutex);
256     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
257         HcFree(entry->appId);
258         HcFree(entry->listener);
259     }
260     DESTROY_HC_VECTOR(ListenerEntryVec, &g_listenerEntryVec);
261     g_broadcastMutex->unlock(g_broadcastMutex);
262     DestroyHcMutex(g_broadcastMutex);
263     HcFree(g_broadcastMutex);
264     g_broadcastMutex = NULL;
265 }
266 
GetBroadcaster(void)267 const Broadcaster *GetBroadcaster(void)
268 {
269     return &g_broadcaster;
270 }
271 
AddListener(const char * appId,const DataChangeListener * listener)272 int32_t AddListener(const char *appId, const DataChangeListener *listener)
273 {
274     if ((appId == NULL) || (listener == NULL)) {
275         LOGE("The input appId or listener is NULL!");
276         return HC_ERR_INVALID_PARAMS;
277     }
278     if (UpdateListenerIfExist(appId, listener) == HC_SUCCESS) {
279         return HC_SUCCESS;
280     }
281     return AddListenerIfNotExist(appId, listener);
282 }
283 
RemoveListener(const char * appId)284 int32_t RemoveListener(const char *appId)
285 {
286     if (appId == NULL) {
287         LOGE("The input appId is NULL!");
288         return HC_ERR_INVALID_PARAMS;
289     }
290     uint32_t index;
291     ListenerEntry *entry = NULL;
292     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
293         if (strcmp(entry->appId, appId) == 0) {
294             HcFree(entry->appId);
295             HcFree(entry->listener);
296             ListenerEntry tempEntry;
297             HC_VECTOR_POPELEMENT(&g_listenerEntryVec, &tempEntry, index);
298             LOGI("Successfully removed a listener. [AppId]: %s", appId);
299             return HC_SUCCESS;
300         }
301     }
302     LOGI("The listener does not exist! [AppId]: %s", appId);
303     return HC_SUCCESS;
304 }
305