• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2025 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 "hc_log.h"
20 #include "hc_mutex.h"
21 #include "hc_types.h"
22 #include "hc_vector.h"
23 #include "securec.h"
24 #include "string_util.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     (void)LockHcMutex(g_broadcastMutex);
45     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
46         if (entry->listener->onGroupCreated != NULL) {
47             LOGI("[Broadcaster]: PostOnGroupCreated! [AppId]: %" LOG_PUB "s", entry->appId);
48             entry->listener->onGroupCreated(messageStr);
49         }
50     }
51     UnlockHcMutex(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     (void)LockHcMutex(g_broadcastMutex);
63     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
64         if (entry->listener->onGroupDeleted != NULL) {
65             LOGI("[Broadcaster]: PostOnGroupDeleted! [AppId]: %" LOG_PUB "s", entry->appId);
66             entry->listener->onGroupDeleted(messageStr);
67         }
68     }
69     UnlockHcMutex(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     (void)LockHcMutex(g_broadcastMutex);
81     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
82         if (entry->listener->onDeviceBound != NULL) {
83             LOGI("[Broadcaster]: PostOnDeviceBound! [AppId]: %" LOG_PUB "s", entry->appId);
84             entry->listener->onDeviceBound(peerUdid, messageStr);
85         }
86     }
87     UnlockHcMutex(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     (void)LockHcMutex(g_broadcastMutex);
99     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
100         if (entry->listener->onDeviceUnBound != NULL) {
101             LOGI("[Broadcaster]: PostOnDeviceUnBound! [AppId]: %" LOG_PUB "s", entry->appId);
102             entry->listener->onDeviceUnBound(peerUdid, messageStr);
103         }
104     }
105     UnlockHcMutex(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     (void)LockHcMutex(g_broadcastMutex);
117     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
118         if (entry->listener->onDeviceNotTrusted != NULL) {
119             LOGI("[Broadcaster]: PostOnDeviceNotTrusted! [AppId]: %" LOG_PUB "s", entry->appId);
120             entry->listener->onDeviceNotTrusted(peerUdid);
121         }
122     }
123     UnlockHcMutex(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     (void)LockHcMutex(g_broadcastMutex);
135     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
136         if (entry->listener->onLastGroupDeleted != NULL) {
137             LOGI("[Broadcaster]: PostOnLastGroupDeleted! [AppId]: %" LOG_PUB "s, [GroupType]: %" LOG_PUB "d",
138                 entry->appId, groupType);
139             entry->listener->onLastGroupDeleted(peerUdid, groupType);
140         }
141     }
142     UnlockHcMutex(g_broadcastMutex);
143 }
144 
PostOnTrustedDeviceNumChanged(int curTrustedDeviceNum)145 static void PostOnTrustedDeviceNumChanged(int curTrustedDeviceNum)
146 {
147     uint32_t index;
148     ListenerEntry *entry = NULL;
149     (void)LockHcMutex(g_broadcastMutex);
150     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
151         if (entry->listener->onTrustedDeviceNumChanged != NULL) {
152             LOGI("[Broadcaster]: PostOnTrustedDeviceNumChanged! [AppId]: %" LOG_PUB "s", entry->appId);
153             entry->listener->onTrustedDeviceNumChanged(curTrustedDeviceNum);
154         }
155     }
156     UnlockHcMutex(g_broadcastMutex);
157 }
158 
UpdateListenerIfExist(const char * appId,const DataChangeListener * listener)159 static int32_t UpdateListenerIfExist(const char *appId, const DataChangeListener *listener)
160 {
161     uint32_t index;
162     ListenerEntry *entry = NULL;
163     (void)LockHcMutex(g_broadcastMutex);
164     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
165         if (IsStrEqual(entry->appId, appId)) {
166             if (memcpy_s(entry->listener, sizeof(DataChangeListener),
167                 listener, sizeof(DataChangeListener)) != HC_SUCCESS) {
168                 UnlockHcMutex(g_broadcastMutex);
169                 LOGE("Failed to copy listener!");
170                 return HC_ERR_MEMORY_COPY;
171             }
172             UnlockHcMutex(g_broadcastMutex);
173             LOGI("Successfully updated a listener. [AppId]: %" LOG_PUB "s", appId);
174             return HC_SUCCESS;
175         }
176     }
177     UnlockHcMutex(g_broadcastMutex);
178     return HC_ERR_LISTENER_NOT_EXIST;
179 }
180 
AddListenerIfNotExist(const char * appId,const DataChangeListener * listener)181 static int32_t AddListenerIfNotExist(const char *appId, const DataChangeListener *listener)
182 {
183     uint32_t appIdLen = HcStrlen(appId) + 1;
184     char *copyAppId = (char *)HcMalloc(appIdLen, 0);
185     if (copyAppId == NULL) {
186         LOGE("Failed to allocate copyAppId memory!");
187         return HC_ERR_ALLOC_MEMORY;
188     }
189     if (strcpy_s(copyAppId, appIdLen, appId) != HC_SUCCESS) {
190         LOGE("Failed to copy appId!");
191         HcFree(copyAppId);
192         return HC_ERR_MEMORY_COPY;
193     }
194     DataChangeListener *copyListener = (DataChangeListener *)HcMalloc(sizeof(DataChangeListener), 0);
195     if (copyListener == NULL) {
196         LOGE("Failed to allocate saveCallback memory!");
197         HcFree(copyAppId);
198         return HC_ERR_ALLOC_MEMORY;
199     }
200     if (memcpy_s(copyListener, sizeof(DataChangeListener),
201         listener, sizeof(DataChangeListener)) != HC_SUCCESS) {
202         LOGE("Failed to copy listener!");
203         HcFree(copyAppId);
204         HcFree(copyListener);
205         return HC_ERR_MEMORY_COPY;
206     }
207     ListenerEntry entry;
208     entry.appId = copyAppId;
209     entry.listener = copyListener;
210     (void)LockHcMutex(g_broadcastMutex);
211     if (g_listenerEntryVec.pushBack(&g_listenerEntryVec, &entry) == NULL) {
212         LOGE("Failed to push listener entity!");
213         HcFree(copyAppId);
214         HcFree(copyListener);
215         UnlockHcMutex(g_broadcastMutex);
216         return HC_ERR_ALLOC_MEMORY;
217     }
218     UnlockHcMutex(g_broadcastMutex);
219     LOGI("Successfully added a listener. [AppId]: %" LOG_PUB "s", appId);
220     return HC_SUCCESS;
221 }
222 
223 static Broadcaster g_broadcaster = {
224     .postOnGroupCreated = PostOnGroupCreated,
225     .postOnGroupDeleted = PostOnGroupDeleted,
226     .postOnDeviceBound = PostOnDeviceBound,
227     .postOnDeviceUnBound = PostOnDeviceUnBound,
228     .postOnDeviceNotTrusted = PostOnDeviceNotTrusted,
229     .postOnLastGroupDeleted = PostOnLastGroupDeleted,
230     .postOnTrustedDeviceNumChanged = PostOnTrustedDeviceNumChanged
231 };
232 
IsBroadcastSupported(void)233 bool IsBroadcastSupported(void)
234 {
235     return true;
236 }
237 
InitBroadcastManager(void)238 int32_t InitBroadcastManager(void)
239 {
240     if (g_broadcastMutex == NULL) {
241         g_broadcastMutex = (HcMutex *)HcMalloc(sizeof(HcMutex), 0);
242         if (g_broadcastMutex == NULL) {
243             LOGE("Failed to allocate broadcast mutex memory!");
244             return HC_ERR_ALLOC_MEMORY;
245         }
246         if (InitHcMutex(g_broadcastMutex, false) != HC_SUCCESS) {
247             LOGE("Init mutex failed");
248             HcFree(g_broadcastMutex);
249             g_broadcastMutex = NULL;
250             return HC_ERROR;
251         }
252     }
253     g_listenerEntryVec = CREATE_HC_VECTOR(ListenerEntryVec);
254     LOGI("[Broadcaster]: Init broadcast manager module successfully!");
255     return HC_SUCCESS;
256 }
257 
DestroyBroadcastManager(void)258 void DestroyBroadcastManager(void)
259 {
260     uint32_t index;
261     ListenerEntry *entry = NULL;
262     (void)LockHcMutex(g_broadcastMutex);
263     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
264         HcFree(entry->appId);
265         HcFree(entry->listener);
266     }
267     DESTROY_HC_VECTOR(ListenerEntryVec, &g_listenerEntryVec);
268     UnlockHcMutex(g_broadcastMutex);
269     DestroyHcMutex(g_broadcastMutex);
270     HcFree(g_broadcastMutex);
271     g_broadcastMutex = NULL;
272 }
273 
GetBroadcaster(void)274 const Broadcaster *GetBroadcaster(void)
275 {
276     return &g_broadcaster;
277 }
278 
AddListener(const char * appId,const DataChangeListener * listener)279 int32_t AddListener(const char *appId, const DataChangeListener *listener)
280 {
281     if ((appId == NULL) || (listener == NULL)) {
282         LOGE("The input appId or listener is NULL!");
283         return HC_ERR_INVALID_PARAMS;
284     }
285     if (UpdateListenerIfExist(appId, listener) == HC_SUCCESS) {
286         return HC_SUCCESS;
287     }
288     return AddListenerIfNotExist(appId, listener);
289 }
290 
RemoveListener(const char * appId)291 int32_t RemoveListener(const char *appId)
292 {
293     if (appId == NULL) {
294         LOGE("The input appId is NULL!");
295         return HC_ERR_INVALID_PARAMS;
296     }
297     uint32_t index;
298     ListenerEntry *entry = NULL;
299     FOR_EACH_HC_VECTOR(g_listenerEntryVec, index, entry) {
300         if (IsStrEqual(entry->appId, appId)) {
301             HcFree(entry->appId);
302             HcFree(entry->listener);
303             ListenerEntry tempEntry;
304             HC_VECTOR_POPELEMENT(&g_listenerEntryVec, &tempEntry, index);
305             LOGI("Successfully removed a listener. [AppId]: %" LOG_PUB "s", appId);
306             return HC_SUCCESS;
307         }
308     }
309     LOGI("The listener does not exist! [AppId]: %" LOG_PUB "s", appId);
310     return HC_SUCCESS;
311 }
312