• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "securec.h"
17 #include <unistd.h>
18 
19 #include "broadcast_dfx_event.h"
20 #include "disc_log.h"
21 #include "g_enhance_adapter_func_pack.h"
22 #include "softbus_adapter_bt_common.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_adapter_thread.h"
25 #include "softbus_broadcast_adapter_interface.h"
26 #include "softbus_ble_gatt_public.h"
27 #include "softbus_broadcast_adapter_interface.h"
28 #include "softbus_broadcast_manager.h"
29 #include "softbus_broadcast_mgr_utils.h"
30 #include "softbus_broadcast_utils.h"
31 #include "softbus_error_code.h"
32 #include "softbus_event.h"
33 #include "legacy/softbus_hidumper_bc_mgr.h"
34 #include "softbus_utils.h"
35 #include "softbus_conn_async_helper.h"
36 
37 
38 #define BC_WAIT_TIME_MS                  50
39 #define BC_WAIT_TIME_SEC                 1
40 #define WAIT_ADV_HANDLE_TIME_SEC         3
41 
42 #define BC_DFX_REPORT_NUM                4
43 #define MAX_BLE_ADV_NUM                  7
44 #define MGR_TIME_THOUSAND_MULTIPLIER     1000LL
45 #define BC_WAIT_TIME_MICROSEC            (BC_WAIT_TIME_MS * MGR_TIME_THOUSAND_MULTIPLIER)
46 #define MAX_FILTER_SIZE                  32
47 #define REGISTER_INFO_MANAGER            "registerInfoMgr"
48 
49 typedef struct {
50     bool isAdapterScanCbReg;
51     int32_t adapterScannerId;
52 } AdapterScannerControl;
53 
54 static int32_t RegisterInfoDump(int fd);
55 
56 typedef struct {
57     bool isUsed;
58     bool isAdvertising;
59     bool isStarted;
60     bool isDisabled;
61     bool isDisableCb;
62     BaseServiceType srvType;
63     int32_t adapterBcId;
64     int32_t advHandle;
65     int32_t minInterval;
66     int32_t maxInterval;
67     SoftBusCond cond;
68     SoftBusCond enableCond;
69     SoftBusCond disableCond;
70     SoftBusCond setParamCond;
71     BroadcastCallback *bcCallback;
72     int64_t time;
73     BroadcastProtocol protocol;
74 } BroadcastManager;
75 
76 typedef enum {
77     SCAN_FREQ_LOW_POWER,
78     SCAN_FREQ_P2_60_3000,
79     SCAN_FREQ_P2_30_1500,
80     SCAN_FREQ_P10_30_300,
81     SCAN_FREQ_P25_60_240,
82     SCAN_FREQ_P50_30_60,
83     SCAN_FREQ_P75_30_40,
84     SCAN_FREQ_P100_1000_1000,
85     SCAN_FREQ_P10_400_40_LONG_RANGE,
86     SCAN_FREQ_P100_30_30_LONG_RANGE,
87     SCAN_FREQ_BUTT,
88 } ScanFreq;
89 
90 typedef struct {
91     bool isUsed;
92     bool isFliterChanged;
93     bool isScanning;
94     uint8_t filterSize;
95     BaseServiceType srvType;
96     int32_t adapterScanId;
97     BcScanParams param;
98     ScanFreq freq;
99     BcScanFilter *filter;
100     uint8_t *deleted;
101     uint8_t deleteSize;
102     uint8_t *added;
103     uint8_t addSize;
104     ScanCallback *scanCallback;
105     BroadcastProtocol protocol;
106 } ScanManager;
107 
108 typedef struct {
109     int32_t adapterBcId;
110     BaseServiceType srvType;
111     BroadcastProtocol protocol;
112 } BroadcastOptions;
113 
114 static volatile bool g_mgrInit = false;
115 static volatile bool g_mgrLockInit = false;
116 static SoftBusMutex g_bcLock = { 0 };
117 static SoftBusMutex g_scanLock = { 0 };
118 static int32_t g_btStateListenerId = -1;
119 static int32_t g_sleStateListenerId = -1;
120 
121 static int32_t g_bcMaxNum = 0;
122 static int32_t g_bcCurrentNum = 0;
123 static int32_t g_bcOverMaxNum = 0;
124 static DiscEventExtra g_bcManagerExtra[BC_NUM_MAX] = { 0 };
125 static BroadcastManager g_bcManager[BC_NUM_MAX] = { 0 };
126 static ScanManager g_scanManager[SCAN_NUM_MAX] = { 0 };
127 static bool g_firstSetIndex[MAX_FILTER_SIZE + 1] = {false};
128 
129 static AdapterScannerControl g_AdapterStatusControl[GATT_SCAN_MAX_NUM] = {
130     { .adapterScannerId = -1, .isAdapterScanCbReg = false},
131     { .adapterScannerId = -1, .isAdapterScanCbReg = false},
132     { .adapterScannerId = -1, .isAdapterScanCbReg = false},
133     { .adapterScannerId = -1, .isAdapterScanCbReg = false},
134     { .adapterScannerId = -1, .isAdapterScanCbReg = false},
135     { .adapterScannerId = -1, .isAdapterScanCbReg = false},
136     { .adapterScannerId = -1, .isAdapterScanCbReg = false},
137     { .adapterScannerId = -1, .isAdapterScanCbReg = false},
138 };
139 
140 static SoftbusBroadcastMediumInterface *g_interface[MEDIUM_NUM_MAX];
141 
CheckProtocolIsValid(BroadcastProtocol interfaceId)142 static inline bool CheckProtocolIsValid(BroadcastProtocol interfaceId)
143 {
144     return interfaceId >= 0 && interfaceId < BROADCAST_PROTOCOL_BUTT;
145 }
146 
RegisterBroadcastMediumFunction(BroadcastProtocol type,const SoftbusBroadcastMediumInterface * interface)147 int32_t RegisterBroadcastMediumFunction(BroadcastProtocol type, const SoftbusBroadcastMediumInterface *interface)
148 {
149     DISC_LOGI(DISC_BROADCAST, "register type=%{public}d", type);
150     DISC_CHECK_AND_RETURN_RET_LOGE(type >= 0 && type < BROADCAST_PROTOCOL_BUTT, SOFTBUS_INVALID_PARAM,
151         DISC_BROADCAST, "type is invalid!");
152     DISC_CHECK_AND_RETURN_RET_LOGE(interface != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "interface is nullptr");
153 
154     g_interface[type] = (SoftbusBroadcastMediumInterface *)interface;
155     return SOFTBUS_OK;
156 }
157 
ReleaseScanIdx(int32_t listenerId)158 static void ReleaseScanIdx(int32_t listenerId)
159 {
160     if (g_scanManager[listenerId].added != NULL) {
161         SoftBusFree(g_scanManager[listenerId].added);
162         g_scanManager[listenerId].added = NULL;
163     }
164     g_scanManager[listenerId].addSize = 0;
165 
166     if (g_scanManager[listenerId].deleted != NULL) {
167         SoftBusFree(g_scanManager[listenerId].deleted);
168         g_scanManager[listenerId].deleted = NULL;
169     }
170     g_scanManager[listenerId].deleteSize = 0;
171 }
172 
HandleOnStateOff(int32_t timer,void * arg)173 static void HandleOnStateOff(int32_t timer, void *arg)
174 {
175     BroadcastProtocol protocol = *(BroadcastProtocol *)arg;
176     SoftBusFree(arg);
177     for (uint32_t managerId = 0; managerId < BC_NUM_MAX; managerId++) {
178         DISC_CHECK_AND_RETURN_LOGE(SoftBusMutexLock(&g_bcLock) == SOFTBUS_OK, DISC_BROADCAST, "bcLock mutex error");
179 
180         BroadcastManager *bcManager = &g_bcManager[managerId];
181         if (!bcManager->isUsed || bcManager->adapterBcId == -1 || bcManager->bcCallback == NULL ||
182             bcManager->bcCallback->OnStopBroadcastingCallback == NULL || protocol != bcManager->protocol) {
183             SoftBusMutexUnlock(&g_bcLock);
184             continue;
185         }
186         SoftBusMutexUnlock(&g_bcLock);
187         (void)g_interface[protocol]->StopBroadcasting(bcManager->adapterBcId);
188         DISC_CHECK_AND_RETURN_LOGE(SoftBusMutexLock(&g_bcLock) == SOFTBUS_OK, DISC_BROADCAST, "bcLock mutex error");
189         if (protocol == BROADCAST_PROTOCOL_BLE && bcManager->isAdvertising) {
190             g_bcCurrentNum--;
191         }
192         bcManager->isAdvertising = false;
193         bcManager->isStarted = false;
194         bcManager->time = 0;
195         SoftBusCondBroadcast(&bcManager->cond);
196         BroadcastCallback callback = *(bcManager->bcCallback);
197         SoftBusMutexUnlock(&g_bcLock);
198         callback.OnStopBroadcastingCallback((int32_t)managerId, (int32_t)SOFTBUS_BC_STATUS_SUCCESS);
199     }
200 
201     for (uint32_t managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
202         DISC_CHECK_AND_RETURN_LOGE(SoftBusMutexLock(&g_scanLock) == SOFTBUS_OK, DISC_BROADCAST, "scanLock mutex error");
203 
204         ScanManager *scanManager = &g_scanManager[managerId];
205         if (!scanManager->isUsed || scanManager->adapterScanId == -1 || !scanManager->isScanning ||
206             scanManager->scanCallback == NULL || scanManager->scanCallback->OnStopScanCallback == NULL ||
207             protocol != scanManager->protocol) {
208             SoftBusMutexUnlock(&g_scanLock);
209             continue;
210         }
211         (void)g_interface[protocol]->StopScan(scanManager->adapterScanId);
212         for (uint32_t i = 0; i < scanManager->filterSize; i++) {
213             g_firstSetIndex[scanManager->filter[i].filterIndex] = false;
214             scanManager->filter[i].filterIndex = 0;
215         }
216         ReleaseScanIdx(managerId);
217         scanManager->isFliterChanged = true;
218         scanManager->isScanning = false;
219         ScanCallback callback = *(scanManager->scanCallback);
220         SoftBusMutexUnlock(&g_scanLock);
221         callback.OnStopScanCallback((int32_t)managerId, (int32_t)SOFTBUS_BC_STATUS_SUCCESS);
222     }
223 }
224 
BcBtStateChanged(int32_t listenerId,int32_t state)225 static void BcBtStateChanged(int32_t listenerId, int32_t state)
226 {
227     (void)listenerId;
228     if (state != SOFTBUS_BC_BT_STATE_TURN_OFF) {
229         return;
230     }
231     DISC_LOGI(DISC_BROADCAST, "receive bt turn off event, start reset broadcast mgr state..");
232     BroadcastProtocol *protocol = SoftBusCalloc(sizeof(BroadcastProtocol));
233     DISC_CHECK_AND_RETURN_LOGE(protocol != NULL, DISC_BROADCAST, "malloc protocol failed");
234     *protocol = BROADCAST_PROTOCOL_BLE;
235     HandleOnStateOff(0, protocol);
236 }
237 
SleStateChanged(int32_t state)238 static void SleStateChanged(int32_t state)
239 {
240     if (state != SOFTBUS_SLE_STATE_TURN_OFF) {
241         return;
242     }
243     DISC_LOGI(DISC_BROADCAST, "receive sle turn off event, start reset broadcast mgr state..");
244 
245     BroadcastProtocol *protocol = SoftBusCalloc(sizeof(BroadcastProtocol));
246     DISC_CHECK_AND_RETURN_LOGE(protocol != NULL, DISC_BROADCAST, "malloc protocol failed");
247     *protocol = BROADCAST_PROTOCOL_SLE;
248     ConnAsync *sync = ConnAsyncGetInstance();
249     DISC_CHECK_AND_RETURN_LOGE(sync != NULL, DISC_BROADCAST, "get sync failed");
250     int32_t ret = ConnAsyncCall(sync, HandleOnStateOff, (void *)protocol, 0);
251     if (ret < 0) {
252         SoftBusFree(protocol);
253         DISC_LOGE(DISC_BROADCAST, "post state change to looper failed, err=%{public}d", ret);
254         return;
255     }
256 }
257 
258 static SoftBusBtStateListener g_softbusBcBtStateLister = {
259     .OnBtStateChanged = BcBtStateChanged,
260     .OnBtAclStateChanged = NULL,
261 };
262 
263 static SoftBusSleStateListener g_softbusSleStateListener = {
264     .onSleStateChanged = SleStateChanged,
265 };
266 
BcManagerLockInit(void)267 static int32_t BcManagerLockInit(void)
268 {
269     DISC_LOGI(DISC_BROADCAST, "init enter");
270     if (g_mgrLockInit) {
271         return SOFTBUS_OK;
272     }
273     if (SoftBusMutexInit(&g_bcLock, NULL) != SOFTBUS_OK) {
274         DISC_LOGE(DISC_BROADCAST, "bcLock init failed");
275         return SOFTBUS_NO_INIT;
276     }
277     if (SoftBusMutexInit(&g_scanLock, NULL) != SOFTBUS_OK) {
278         DISC_LOGE(DISC_BROADCAST, "scanLock init failed");
279         (void)SoftBusMutexDestroy(&g_bcLock);
280         return SOFTBUS_NO_INIT;
281     }
282     g_mgrLockInit = true;
283     return SOFTBUS_OK;
284 }
285 
DelayReportBroadcast(void * para)286 static void DelayReportBroadcast(void *para)
287 {
288     (void)para;
289     DiscEventExtra extra = { 0 };
290     for (int32_t managerId = 0; managerId < BC_NUM_MAX; managerId++) {
291         if (g_bcManagerExtra[managerId].isOn == 1) {
292             extra.startTime = g_bcManagerExtra[managerId].startTime;
293             extra.advHandle = g_bcManagerExtra[managerId].advHandle;
294             extra.serverType = g_bcManagerExtra[managerId].serverType;
295             extra.minInterval = g_bcManagerExtra[managerId].minInterval;
296             extra.maxInterval = g_bcManagerExtra[managerId].maxInterval;
297             extra.bcOverMaxCnt = g_bcOverMaxNum;
298             DISC_LOGI(DISC_BROADCAST, "startTime=%{public}" PRId64 ", advHandle=%{public}d, serverType=%{public}s, "
299                 "minInterval=%{public}d, maxInterval=%{public}d, bcOverMaxCnt=%{public}d", extra.startTime,
300                 extra.advHandle, extra.serverType, extra.minInterval, extra.maxInterval, extra.bcOverMaxCnt);
301             DISC_EVENT(EVENT_SCENE_BLE, EVENT_STAGE_BROADCAST, extra);
302         }
303     }
304 
305     g_bcMaxNum = 0;
306     g_bcOverMaxNum = 0;
307     memset_s(g_bcManagerExtra, sizeof(g_bcManagerExtra), 0, sizeof(g_bcManagerExtra));
308     if (BleAsyncCallbackDelayHelper(GetLooper(LOOP_TYPE_DEFAULT), DelayReportBroadcast, NULL,
309         DELAY_TIME_DEFAULT) != SOFTBUS_OK) {
310         DISC_LOGE(DISC_BROADCAST, "DelayReportBroadcast failed, due to async callback fail");
311     }
312 }
313 
InitBroadcastMgr(void)314 int32_t InitBroadcastMgr(void)
315 {
316     DISC_LOGI(DISC_BROADCAST, "init enter");
317     if (g_mgrInit) {
318         DISC_LOGD(DISC_BROADCAST, "mgr already inited");
319         return SOFTBUS_OK;
320     }
321     int32_t ret = BcManagerLockInit();
322     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "lock init failed");
323 
324     SoftbusBleAdapterInit();
325     SoftbusSleAdapterInitPacked();
326     for (BroadcastProtocol i = 0; i < BROADCAST_PROTOCOL_BUTT; ++i) {
327         if (g_interface[i] != NULL && g_interface[i]->Init != NULL) {
328             ret = g_interface[i]->Init();
329             DISC_LOGI(DISC_BROADCAST, "init protocol=%{public}d, ret=%{public}d", i, ret);
330         } else {
331             DISC_LOGE(DISC_BROADCAST, "protocol=%{public}d is not register", i);
332         }
333     }
334 
335     ret = SoftBusAddBtStateListener(&g_softbusBcBtStateLister, &g_btStateListenerId);
336     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "add bt state listener failed");
337 
338     ret = SoftBusAddSleStateListenerPacked(&g_softbusSleStateListener, &g_sleStateListenerId);
339     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "add sle state listener failed");
340     g_mgrInit = true;
341 
342     if (BleAsyncCallbackDelayHelper(GetLooper(LOOP_TYPE_DEFAULT), DelayReportBroadcast, NULL,
343         DELAY_TIME_DEFAULT) != SOFTBUS_OK) {
344         DISC_LOGE(DISC_BROADCAST, "looper init failed");
345     }
346     SoftBusRegBcMgrVarDump((char *)REGISTER_INFO_MANAGER, &RegisterInfoDump);
347     return SOFTBUS_OK;
348 }
349 
CheckLockIsInit(SoftBusMutex * lock)350 static bool CheckLockIsInit(SoftBusMutex *lock)
351 {
352     DISC_CHECK_AND_RETURN_RET_LOGE(lock != NULL, false, DISC_BROADCAST, "lock is nullptr");
353 
354     if (SoftBusMutexLock(lock) != SOFTBUS_OK) {
355         return false;
356     }
357     SoftBusMutexUnlock(lock);
358     return true;
359 }
360 
CheckBroadcastingParam(const BroadcastParam * param,const BroadcastPacket * packet)361 static int32_t CheckBroadcastingParam(const BroadcastParam *param, const BroadcastPacket *packet)
362 {
363     DISC_CHECK_AND_RETURN_RET_LOGE(param != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid param!");
364     DISC_CHECK_AND_RETURN_RET_LOGE(packet != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid param packet");
365     DISC_CHECK_AND_RETURN_RET_LOGE(packet->bcData.payload != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST,
366         "invalid param payload");
367     return SOFTBUS_OK;
368 }
369 
DeInitBroadcastMgr(void)370 int32_t DeInitBroadcastMgr(void)
371 {
372     DISC_LOGI(DISC_BROADCAST, "deinit enter");
373 
374     if (CheckLockIsInit(&g_bcLock)) {
375         (void)SoftBusMutexDestroy(&g_bcLock);
376     }
377     if (CheckLockIsInit(&g_scanLock)) {
378         (void)SoftBusMutexDestroy(&g_scanLock);
379     }
380     g_mgrLockInit = false;
381     g_mgrInit = false;
382     int32_t ret;
383     if (g_btStateListenerId != -1) {
384         ret = SoftBusRemoveBtStateListener(g_btStateListenerId);
385         DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "RemoveBtStateListener failed");
386         g_btStateListenerId = -1;
387     }
388 
389     if (g_sleStateListenerId != -1) {
390         SoftBusRemoveSleStateListenerPacked(g_sleStateListenerId);
391         g_sleStateListenerId = -1;
392     }
393 
394     for (BroadcastProtocol i = 0; i < BROADCAST_PROTOCOL_BUTT; ++i) {
395         if (g_interface[i] != NULL && g_interface[i]->DeInit != NULL) {
396             ret = g_interface[i]->DeInit();
397             DISC_LOGI(DISC_BROADCAST, "deInit protocol=%{public}d, ret=%{public}d", i, ret);
398             g_interface[i] = NULL;
399         } else {
400             DISC_LOGE(DISC_BROADCAST, "protocol=%{public}d is not register", i);
401         }
402     }
403     SoftbusBleAdapterDeInit();
404     SoftbusSleAdapterDeInitPacked();
405     return SOFTBUS_OK;
406 }
407 
GetSrvType(BaseServiceType srvType)408 static char *GetSrvType(BaseServiceType srvType)
409 {
410     if ((int32_t)srvType < 0 || (int32_t)srvType >= (int32_t)(sizeof(g_srvTypeMap)/sizeof(SrvTypeMap))) {
411         return (char *)"invalid service";
412     }
413     return g_srvTypeMap[srvType].service;
414 }
415 
ReportCurrentBroadcast(bool startBcResult)416 static void ReportCurrentBroadcast(bool startBcResult)
417 {
418     DiscEventExtra extra = { 0 };
419     for (int32_t managerId = 0; managerId < BC_NUM_MAX; managerId++) {
420         if (g_bcManager[managerId].isAdvertising) {
421             extra.startTime = g_bcManager[managerId].time;
422             extra.advHandle = g_bcManager[managerId].advHandle;
423             extra.serverType = GetSrvType(g_bcManager[managerId].srvType);
424             extra.minInterval = g_bcManager[managerId].minInterval;
425             extra.maxInterval = g_bcManager[managerId].maxInterval;
426             if (startBcResult) {
427                 extra.currentNum = g_bcCurrentNum;
428             }
429             DISC_LOGI(DISC_BROADCAST, "startTime=%{public}" PRId64 ", advHandle=%{public}d, serverType=%{public}s, "
430                 "minInterval=%{public}d, maxInterval=%{public}d", extra.startTime,
431                 extra.advHandle, extra.serverType, extra.minInterval, extra.maxInterval);
432             DISC_EVENT(EVENT_SCENE_BLE, EVENT_STAGE_BROADCAST, extra);
433         }
434     }
435 }
436 
UpdateBcMaxExtra(void)437 static void UpdateBcMaxExtra(void)
438 {
439     if (g_bcCurrentNum > BC_DFX_REPORT_NUM) {
440         ReportCurrentBroadcast(true);
441     }
442 
443     if (g_bcCurrentNum < g_bcMaxNum) {
444         return;
445     }
446 
447     g_bcMaxNum = g_bcCurrentNum;
448     memset_s(g_bcManagerExtra, sizeof(g_bcManagerExtra), 0, sizeof(g_bcManagerExtra));
449     for (int32_t managerId = 0; managerId < BC_NUM_MAX; managerId++) {
450         if (g_bcManager[managerId].isAdvertising) {
451             g_bcManagerExtra[managerId].isOn = 1;
452             g_bcManagerExtra[managerId].startTime = g_bcManager[managerId].time;
453             g_bcManagerExtra[managerId].advHandle = g_bcManager[managerId].advHandle;
454             g_bcManagerExtra[managerId].serverType = GetSrvType(g_bcManager[managerId].srvType);
455             g_bcManagerExtra[managerId].minInterval = g_bcManager[managerId].minInterval;
456             g_bcManagerExtra[managerId].maxInterval = g_bcManager[managerId].maxInterval;
457         }
458     }
459 }
460 
BcStartBroadcastingCallback(BroadcastProtocol protocol,int32_t adapterBcId,int32_t status)461 static void BcStartBroadcastingCallback(BroadcastProtocol protocol, int32_t adapterBcId, int32_t status)
462 {
463     static uint32_t callCount = 0;
464     for (uint32_t managerId = 0; managerId < BC_NUM_MAX; managerId++) {
465         int32_t ret = SoftBusMutexLock(&g_bcLock);
466         DISC_CHECK_AND_RETURN_LOGE(ret == SOFTBUS_OK,
467             DISC_BROADCAST, "mutex error, adapterBcId=%{public}d", adapterBcId);
468 
469         BroadcastManager *bcManager = &g_bcManager[managerId];
470         if (bcManager->protocol != protocol || bcManager->adapterBcId != adapterBcId) {
471             SoftBusMutexUnlock(&g_bcLock);
472             continue;
473         }
474         if (!bcManager->isUsed || bcManager->bcCallback == NULL ||
475             bcManager->bcCallback->OnStartBroadcastingCallback == NULL) {
476             SoftBusMutexUnlock(&g_bcLock);
477             DISC_LOGE(DISC_BROADCAST, "bcManager not available, adapterBcId=%{public}d, managerId=%{public}u",
478                 adapterBcId, managerId);
479             continue;
480         }
481         DISC_LOGI(DISC_BROADCAST, "srvType=%{public}s, managerId=%{public}u, adapterBcId=%{public}d, status=%{public}d,"
482             "callCount=%{public}u", GetSrvType(bcManager->srvType), managerId, adapterBcId, status, callCount++);
483         if (status == SOFTBUS_BC_STATUS_SUCCESS) {
484             if (!bcManager->isAdvertising) {
485                 g_bcCurrentNum++;
486             }
487             bcManager->isAdvertising = true;
488             UpdateBcMaxExtra();
489             SoftBusCondSignal(&bcManager->cond);
490         }
491         BroadcastCallback callback = *(bcManager->bcCallback);
492         SoftBusMutexUnlock(&g_bcLock);
493         callback.OnStartBroadcastingCallback((int32_t)managerId, status);
494         break; // The broadcast channel cannot be multiplexed.
495     }
496 }
497 
BcStopBroadcastingCallback(BroadcastProtocol protocol,int32_t adapterBcId,int32_t status)498 static void BcStopBroadcastingCallback(BroadcastProtocol protocol, int32_t adapterBcId, int32_t status)
499 {
500     for (uint32_t managerId = 0; managerId < BC_NUM_MAX; managerId++) {
501         int32_t ret = SoftBusMutexLock(&g_bcLock);
502         DISC_CHECK_AND_RETURN_LOGE(ret == SOFTBUS_OK,
503             DISC_BROADCAST, "mutex error, adapterBcId=%{public}d", adapterBcId);
504 
505         BroadcastManager *bcManager = &g_bcManager[managerId];
506         if (bcManager->protocol != protocol || bcManager->adapterBcId != adapterBcId) {
507             SoftBusMutexUnlock(&g_bcLock);
508             continue;
509         }
510         if (!bcManager->isUsed || bcManager->bcCallback == NULL ||
511             bcManager->bcCallback->OnStopBroadcastingCallback == NULL) {
512             SoftBusMutexUnlock(&g_bcLock);
513             DISC_LOGE(DISC_BROADCAST, "bcManager not available, adapterBcId=%{public}d, managerId=%{public}u",
514                 adapterBcId, managerId);
515             continue;
516         }
517         DISC_LOGI(DISC_BROADCAST, "srvType=%{public}s, managerId=%{public}u, adapterBcId=%{public}d, status=%{public}d",
518             GetSrvType(bcManager->srvType), managerId, adapterBcId, status);
519         if (status == SOFTBUS_BC_STATUS_SUCCESS) {
520             if (bcManager->isAdvertising) {
521                 g_bcCurrentNum--;
522             }
523             bcManager->isAdvertising = false;
524             bcManager->time = 0;
525             SoftBusCondSignal(&bcManager->cond);
526         }
527         BroadcastCallback callback = *(bcManager->bcCallback);
528         SoftBusMutexUnlock(&g_bcLock);
529         callback.OnStopBroadcastingCallback((int32_t)managerId, status);
530         break; // The broadcast channel cannot be multiplexed.
531     }
532 }
533 
BcUpdateBroadcastingCallback(BroadcastProtocol protocol,int32_t adapterBcId,int32_t status)534 static void BcUpdateBroadcastingCallback(BroadcastProtocol protocol, int32_t adapterBcId, int32_t status)
535 {
536     DISC_LOGD(DISC_BROADCAST, "enter update bc cb enter");
537     for (uint32_t managerId = 0; managerId < BC_NUM_MAX; managerId++) {
538         int32_t ret = SoftBusMutexLock(&g_bcLock);
539         DISC_CHECK_AND_RETURN_LOGE(ret == SOFTBUS_OK, DISC_BROADCAST, "mutex error");
540 
541         BroadcastManager *bcManager = &g_bcManager[managerId];
542         if (bcManager->protocol != protocol || bcManager->adapterBcId != adapterBcId ||
543             !bcManager->isUsed || bcManager->bcCallback == NULL ||
544             bcManager->bcCallback->OnUpdateBroadcastingCallback == NULL) {
545             SoftBusMutexUnlock(&g_bcLock);
546             continue;
547         }
548         DISC_LOGI(DISC_BROADCAST, "srvType=%{public}s, managerId=%{public}u, adapterBcId=%{public}d, status=%{public}d",
549             GetSrvType(bcManager->srvType), managerId, adapterBcId, status);
550         BroadcastCallback callback = *(bcManager->bcCallback);
551         SoftBusMutexUnlock(&g_bcLock);
552         callback.OnUpdateBroadcastingCallback((int32_t)managerId, status);
553         break; // The broadcast channel cannot be multiplexed.
554     }
555 }
556 
BcSetBroadcastingCallback(BroadcastProtocol protocol,int32_t adapterBcId,int32_t status)557 static void BcSetBroadcastingCallback(BroadcastProtocol protocol, int32_t adapterBcId, int32_t status)
558 {
559     DISC_LOGD(DISC_BROADCAST, "enter set bc cb");
560     for (uint32_t managerId = 0; managerId < BC_NUM_MAX; managerId++) {
561         int32_t ret = SoftBusMutexLock(&g_bcLock);
562         DISC_CHECK_AND_RETURN_LOGE(ret == SOFTBUS_OK, DISC_BROADCAST, "mutex error");
563 
564         BroadcastManager *bcManager = &g_bcManager[managerId];
565         if (bcManager->protocol != protocol || bcManager->adapterBcId != adapterBcId || !bcManager->isUsed ||
566             bcManager->bcCallback == NULL || bcManager->bcCallback->OnSetBroadcastingCallback == NULL) {
567             SoftBusMutexUnlock(&g_bcLock);
568             continue;
569         }
570         static uint32_t callCount = 0;
571         DISC_LOGI(DISC_BROADCAST, "srvType=%{public}s, managerId=%{public}u, adapterBcId=%{public}d, status=%{public}d,"
572             "callCount=%{public}u", GetSrvType(bcManager->srvType), managerId, adapterBcId, status, callCount++);
573         BroadcastCallback callback = *(bcManager->bcCallback);
574         SoftBusMutexUnlock(&g_bcLock);
575         callback.OnSetBroadcastingCallback((int32_t)managerId, status);
576         break; // The broadcast channel cannot be multiplexed.
577     }
578 }
579 
BcSetBroadcastingParamCallback(BroadcastProtocol protocol,int32_t adapterBcId,int32_t status)580 static void BcSetBroadcastingParamCallback(BroadcastProtocol protocol, int32_t adapterBcId, int32_t status)
581 {
582     DISC_LOGD(DISC_BROADCAST, "enter set bc param cb");
583     for (uint32_t managerId = 0; managerId < BC_NUM_MAX; managerId++) {
584         int32_t ret = SoftBusMutexLock(&g_bcLock);
585         DISC_CHECK_AND_RETURN_LOGE(ret == SOFTBUS_OK, DISC_BROADCAST, "mutex error");
586 
587         BroadcastManager *bcManager = &g_bcManager[managerId];
588         if (bcManager->protocol != protocol || bcManager->adapterBcId != adapterBcId || !bcManager->isUsed) {
589             SoftBusMutexUnlock(&g_bcLock);
590             continue;
591         }
592         static uint32_t callCount = 0;
593         DISC_LOGI(DISC_BROADCAST, "srvType=%{public}s, managerId=%{public}u, adapterBcId=%{public}d,"
594             "status=%{public}d, callCount=%{public}u", GetSrvType(bcManager->srvType),
595             managerId, adapterBcId, status, callCount++);
596         if (status == SOFTBUS_BC_STATUS_SUCCESS) {
597             SoftBusCondSignal(&bcManager->setParamCond);
598         }
599         SoftBusMutexUnlock(&g_bcLock);
600         break; // The broadcast channel cannot be multiplexed.
601     }
602 }
603 
BcEnableBroadcastingCallback(BroadcastProtocol protocol,int32_t adapterBcId,int32_t status)604 static void BcEnableBroadcastingCallback(BroadcastProtocol protocol, int32_t adapterBcId, int32_t status)
605 {
606     DISC_LOGD(DISC_BROADCAST, "enter enable bc cb");
607     for (uint32_t managerId = 0; managerId < BC_NUM_MAX; managerId++) {
608         int32_t ret = SoftBusMutexLock(&g_bcLock);
609         DISC_CHECK_AND_RETURN_LOGE(ret == SOFTBUS_OK, DISC_BROADCAST, "mutex error");
610 
611         BroadcastManager *bcManager = &g_bcManager[managerId];
612         if (bcManager->protocol != protocol || bcManager->adapterBcId != adapterBcId || !bcManager->isUsed) {
613             SoftBusMutexUnlock(&g_bcLock);
614             continue;
615         }
616         static uint32_t callCount = 0;
617         DISC_LOGI(DISC_BROADCAST, "srvType=%{public}s, managerId=%{public}u, adapterBcId=%{public}d,"
618             "status=%{public}d, callCount=%{public}u", GetSrvType(bcManager->srvType),
619             managerId, adapterBcId, status, callCount++);
620         if (status == SOFTBUS_BC_STATUS_SUCCESS) {
621             bcManager->isDisabled = false;
622             SoftBusCondSignal(&bcManager->enableCond);
623         }
624         SoftBusMutexUnlock(&g_bcLock);
625         break; // The broadcast channel cannot be multiplexed.
626     }
627 }
628 
BcDisableBroadcastingCallback(BroadcastProtocol protocol,int32_t adapterBcId,int32_t status)629 static void BcDisableBroadcastingCallback(BroadcastProtocol protocol, int32_t adapterBcId, int32_t status)
630 {
631     DISC_LOGD(DISC_BROADCAST, "enter disable bc cb");
632     for (uint32_t managerId = 0; managerId < BC_NUM_MAX; managerId++) {
633         int32_t ret = SoftBusMutexLock(&g_bcLock);
634         DISC_CHECK_AND_RETURN_LOGE(ret == SOFTBUS_OK, DISC_BROADCAST, "mutex error");
635 
636         BroadcastManager *bcManager = &g_bcManager[managerId];
637         if (bcManager->protocol != protocol || bcManager->adapterBcId != adapterBcId || !bcManager->isUsed) {
638             SoftBusMutexUnlock(&g_bcLock);
639             continue;
640         }
641         static uint32_t callCount = 0;
642         DISC_LOGI(DISC_BROADCAST, "srvType=%{public}s, managerId=%{public}u, adapterBcId=%{public}d,"
643             "status=%{public}d, callCount=%{public}u", GetSrvType(bcManager->srvType),
644             managerId, adapterBcId, status, callCount++);
645         if (status == SOFTBUS_BC_STATUS_SUCCESS) {
646             bcManager->isDisabled = true;
647             bcManager->isDisableCb = true;
648             SoftBusCondSignal(&bcManager->disableCond);
649         }
650         SoftBusMutexUnlock(&g_bcLock);
651         break; // The broadcast channel cannot be multiplexed.
652     }
653 }
654 
655 static SoftbusBroadcastCallback g_softbusBcBleCb = {
656     .OnStartBroadcastingCallback = BcStartBroadcastingCallback,
657     .OnStopBroadcastingCallback = BcStopBroadcastingCallback,
658     .OnUpdateBroadcastingCallback = BcUpdateBroadcastingCallback,
659     .OnSetBroadcastingCallback = BcSetBroadcastingCallback,
660     .OnSetBroadcastingParamCallback = BcSetBroadcastingParamCallback,
661     .OnDisableBroadcastingCallback = BcDisableBroadcastingCallback,
662     .OnEnableBroadcastingCallback = BcEnableBroadcastingCallback,
663 };
664 
BcOnStartScanCallback(BroadcastProtocol protocol,int32_t adapterScanId,int32_t status)665 static void BcOnStartScanCallback(BroadcastProtocol protocol, int32_t adapterScanId, int32_t status)
666 {
667     DISC_LOGD(DISC_BROADCAST, "enter on start scan cb");
668     for (uint32_t managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
669         ScanManager *scanManager = &g_scanManager[managerId];
670         if (scanManager->protocol != protocol || scanManager->adapterScanId != adapterScanId || !scanManager->isUsed ||
671             scanManager->scanCallback == NULL || scanManager->scanCallback->OnStartScanCallback == NULL) {
672             continue;
673         }
674         DISC_LOGI(DISC_BROADCAST, "srvType=%{public}s, managerId=%{public}u, adapterScanId=%{public}d, "
675             "status=%{public}d", GetSrvType(scanManager->srvType), managerId, adapterScanId, status);
676         if (status == SOFTBUS_BC_STATUS_SUCCESS) {
677             scanManager->isScanning = true;
678         }
679 
680         scanManager->scanCallback->OnStartScanCallback((int32_t)managerId, status);
681     }
682 }
683 
BcStopScanCallback(BroadcastProtocol protocol,int32_t adapterScanId,int32_t status)684 static void BcStopScanCallback(BroadcastProtocol protocol, int32_t adapterScanId, int32_t status)
685 {
686     DISC_LOGD(DISC_BROADCAST, "enter stop scan cb");
687     for (uint32_t managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
688         ScanManager *scanManager = &g_scanManager[managerId];
689         if (scanManager->protocol != protocol || scanManager->adapterScanId != adapterScanId || !scanManager->isUsed ||
690             scanManager->scanCallback == NULL || scanManager->scanCallback->OnStopScanCallback == NULL) {
691             continue;
692         }
693         DISC_LOGI(DISC_BROADCAST, "srvType=%{public}s, managerId=%{public}u, adapterScanId=%{public}d, "
694             "status=%{public}d", GetSrvType(scanManager->srvType), managerId, adapterScanId, status);
695         if (status == SOFTBUS_BC_STATUS_SUCCESS) {
696             scanManager->isScanning = false;
697         }
698 
699         scanManager->scanCallback->OnStopScanCallback((int32_t)managerId, status);
700     }
701 }
702 
BuildBcInfoCommon(const SoftBusBcScanResult * reportData,BroadcastReportInfo * bcInfo)703 static int32_t BuildBcInfoCommon(const SoftBusBcScanResult *reportData, BroadcastReportInfo *bcInfo)
704 {
705     DISC_CHECK_AND_RETURN_RET_LOGE(reportData != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "reportData is nullptr");
706     DISC_CHECK_AND_RETURN_RET_LOGE(bcInfo != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "bcInfo is nullptr");
707 
708     bcInfo->eventType = reportData->eventType;
709     bcInfo->dataStatus = reportData->dataStatus;
710     bcInfo->primaryPhy = reportData->primaryPhy;
711     bcInfo->secondaryPhy = reportData->secondaryPhy;
712     bcInfo->advSid = reportData->advSid;
713     bcInfo->txPower = reportData->txPower;
714     bcInfo->rssi = reportData->rssi;
715     bcInfo->addrType = reportData->addrType;
716 
717     int32_t ret = memcpy_s(bcInfo->addr.addr, BC_ADDR_MAC_LEN, reportData->addr.addr, SOFTBUS_ADDR_MAC_LEN);
718     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_MEM_ERR, DISC_BROADCAST, "memcpy addr failed");
719 
720     ret = memcpy_s(bcInfo->localName, BC_LOCAL_NAME_LEN_MAX, reportData->localName, SOFTBUS_LOCAL_NAME_LEN_MAX);
721     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_MEM_ERR, DISC_BROADCAST, "memcpy localName failed");
722 
723     return SOFTBUS_OK;
724 }
725 
CheckManufactureIsMatch(const BcScanFilter * filter,const BroadcastPayload * bcData)726 static bool CheckManufactureIsMatch(const BcScanFilter *filter, const BroadcastPayload *bcData)
727 {
728     DISC_CHECK_AND_RETURN_RET_LOGE(filter != NULL, false, DISC_BROADCAST, "filter is nullptr");
729     DISC_CHECK_AND_RETURN_RET_LOGE(bcData != NULL, false, DISC_BROADCAST, "bcData is nullptr");
730 
731     uint8_t dataLen = bcData->payloadLen;
732     uint32_t filterLen = filter->manufactureDataLength;
733     if ((uint32_t)dataLen < filterLen) {
734         DISC_LOGD(DISC_BROADCAST, "payload is too short");
735         return false;
736     }
737     if (filter->manufactureId != bcData->id) {
738         DISC_LOGD(DISC_BROADCAST, "manufacture id not match");
739         return false;
740     }
741     for (uint32_t i = 0; i < filterLen; i++) {
742         if ((filter->manufactureData[i] & filter->manufactureDataMask[i]) !=
743             (bcData->payload[i] & filter->manufactureDataMask[i])) {
744             return false;
745         }
746     }
747     return true;
748 }
749 
CheckServiceIsMatch(const BcScanFilter * filter,const BroadcastPayload * bcData)750 static bool CheckServiceIsMatch(const BcScanFilter *filter, const BroadcastPayload *bcData)
751 {
752     DISC_CHECK_AND_RETURN_RET_LOGE(filter != NULL, false, DISC_BROADCAST, "filter is nullptr");
753     DISC_CHECK_AND_RETURN_RET_LOGE(bcData != NULL, false, DISC_BROADCAST, "bcData is nullptr");
754 
755     uint8_t dataLen = bcData->payloadLen;
756     uint32_t filterLen = filter->serviceDataLength;
757     if ((uint32_t)dataLen < filterLen) {
758         DISC_LOGD(DISC_BROADCAST, "payload is too short");
759         return false;
760     }
761     if (filter->serviceUuid != bcData->id) {
762         DISC_LOGD(DISC_BROADCAST, "serviceUuid not match");
763         return false;
764     }
765     for (uint32_t i = 0; i < filterLen; i++) {
766         if ((filter->serviceData[i] & filter->serviceDataMask[i]) !=
767             (bcData->payload[i] & filter->serviceDataMask[i])) {
768             return false;
769         }
770     }
771     return true;
772 }
773 
CheckScanResultDataIsMatch(const uint32_t managerId,BroadcastPayload * bcData)774 static bool CheckScanResultDataIsMatch(const uint32_t managerId, BroadcastPayload *bcData)
775 {
776     DISC_CHECK_AND_RETURN_RET_LOGE(bcData != NULL, false, DISC_BROADCAST, "bcData is nullptr");
777 
778     if (bcData->type != BC_DATA_TYPE_SERVICE && bcData->type != BC_DATA_TYPE_MANUFACTURER) {
779         DISC_LOGE(DISC_BROADCAST, "not support type, type=%{public}d", bcData->type);
780         return false;
781     }
782 
783     uint8_t filterSize = g_scanManager[managerId].filterSize;
784     for (uint8_t i = 0; i < filterSize; i++) {
785         BcScanFilter filter = g_scanManager[managerId].filter[i];
786         if (bcData->type == BC_DATA_TYPE_SERVICE && CheckServiceIsMatch(&filter, bcData)) {
787             return true;
788         }
789         if (bcData->type == BC_DATA_TYPE_MANUFACTURER && CheckManufactureIsMatch(&filter, bcData)) {
790             return true;
791         }
792     }
793     return false;
794 }
795 
DumpSoftbusData(const char * description,uint16_t len,const uint8_t * data)796 static void DumpSoftbusData(const char *description, uint16_t len, const uint8_t *data)
797 {
798     DISC_CHECK_AND_RETURN_LOGE(description != NULL, DISC_BROADCAST, "description is nullptr");
799     DISC_CHECK_AND_RETURN_LOGD(len != 0, DISC_BROADCAST, "description=%{public}s, len is 0", description);
800     DISC_CHECK_AND_RETURN_LOGE(data != NULL, DISC_BROADCAST, "description=%{public}s, data is nullptr", description);
801 
802     int32_t hexLen = HEXIFY_LEN(len);
803     char *softbusData = (char *)SoftBusCalloc(sizeof(char) * hexLen);
804     DISC_CHECK_AND_RETURN_LOGE(softbusData != NULL, DISC_BROADCAST, "desc=%{public}s, malloc failed", description);
805 
806     (void)ConvertBytesToHexString(softbusData, hexLen, data, len);
807     DISC_LOGD(DISC_BROADCAST, "description=%{public}s, softbusData=%{public}s", description, softbusData);
808 
809     SoftBusFree(softbusData);
810 }
811 
ReleaseBroadcastReportInfo(BroadcastReportInfo * bcInfo)812 static void ReleaseBroadcastReportInfo(BroadcastReportInfo *bcInfo)
813 {
814     DISC_CHECK_AND_RETURN_LOGE(bcInfo != NULL, DISC_BROADCAST, "bcInfo is nullptr");
815 
816     SoftBusFree(bcInfo->packet.bcData.payload);
817     SoftBusFree(bcInfo->packet.rspData.payload);
818 }
819 
BuildBcPayload(const SoftbusBroadcastPayload * srcData,BroadcastPayload * dstData)820 static int32_t BuildBcPayload(const SoftbusBroadcastPayload *srcData, BroadcastPayload *dstData)
821 {
822     DISC_CHECK_AND_RETURN_RET_LOGE(srcData != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "srcData is nullptr");
823     DISC_CHECK_AND_RETURN_RET_LOGE(dstData != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "dstData is nullptr");
824     DISC_CHECK_AND_RETURN_RET_LOGE(srcData->payload != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST,
825         "broadcast payload is nullptr");
826 
827     dstData->type = (BroadcastDataType)srcData->type;
828     dstData->id = srcData->id;
829 
830     dstData->payloadLen = srcData->payloadLen;
831     dstData->payload = (uint8_t *)SoftBusCalloc(dstData->payloadLen);
832     DISC_CHECK_AND_RETURN_RET_LOGE(dstData->payload != NULL, SOFTBUS_MALLOC_ERR, DISC_BROADCAST, "malloc failed");
833 
834     if (memcpy_s(dstData->payload, dstData->payloadLen, srcData->payload, dstData->payloadLen) != EOK) {
835         DISC_LOGE(DISC_BROADCAST, "memcpy payload failed");
836         SoftBusFree(dstData->payload);
837         return SOFTBUS_MEM_ERR;
838     }
839     return SOFTBUS_OK;
840 }
841 
BuildBroadcastPacket(const SoftbusBroadcastData * softbusBcData,BroadcastPacket * packet)842 static int32_t BuildBroadcastPacket(const SoftbusBroadcastData *softbusBcData, BroadcastPacket *packet)
843 {
844     DISC_CHECK_AND_RETURN_RET_LOGE(softbusBcData != NULL, SOFTBUS_INVALID_PARAM,
845         DISC_BROADCAST, "softbusBcData is nullptr");
846     DISC_CHECK_AND_RETURN_RET_LOGE(packet != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "packet is nullptr");
847 
848     packet->isSupportFlag = softbusBcData->isSupportFlag;
849     packet->flag = softbusBcData->flag;
850 
851     // 2.1. Build broadcast payload.
852     int32_t ret = BuildBcPayload(&(softbusBcData->bcData), &(packet->bcData));
853     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_BC_MGR_BUILD_ADV_PACKT_FAIL,
854         DISC_BROADCAST, "build broadcast payload failed");
855 
856     DumpSoftbusData("scan result bcData", softbusBcData->bcData.payloadLen, softbusBcData->bcData.payload);
857 
858     // 2.2. Build broadcast response payload.
859     if (softbusBcData->rspData.payload == NULL) {
860         packet->rspData.payload = NULL;
861         DISC_LOGD(DISC_BROADCAST, "no rspData");
862     } else {
863         ret = BuildBcPayload(&(softbusBcData->rspData), &(packet->rspData));
864         if (ret != SOFTBUS_OK) {
865             SoftBusFree(packet->bcData.payload);
866             DISC_LOGE(DISC_BROADCAST, "build broadcast rsp payload failed");
867             return SOFTBUS_BC_MGR_BUILD_RSP_PACKT_FAIL;
868         }
869         DumpSoftbusData("scan result rspData", softbusBcData->rspData.payloadLen, softbusBcData->rspData.payload);
870     }
871     return SOFTBUS_OK;
872 }
873 
BuildBroadcastReportInfo(const SoftBusBcScanResult * reportData,BroadcastReportInfo * bcInfo)874 static int32_t BuildBroadcastReportInfo(const SoftBusBcScanResult *reportData, BroadcastReportInfo *bcInfo)
875 {
876     DISC_CHECK_AND_RETURN_RET_LOGE(reportData != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "reportData is nullptr");
877     DISC_CHECK_AND_RETURN_RET_LOGE(bcInfo != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "bcInfo is nullptr");
878 
879     // 1. Build BroadcastReportInfo from SoftBusBcScanResult except BroadcastPacket.
880     int32_t ret = BuildBcInfoCommon(reportData, bcInfo);
881     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "build broadcast common info failed");
882 
883     // 2. Build BroadcastPacket.
884     ret = BuildBroadcastPacket(&(reportData->data), &(bcInfo->packet));
885     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "build broadcast packet failed");
886 
887     return SOFTBUS_OK;
888 }
889 
CheckScanResultDataIsMatchApproach(const uint32_t managerId,BroadcastPayload * bcData)890 static bool CheckScanResultDataIsMatchApproach(const uint32_t managerId, BroadcastPayload *bcData)
891 {
892     DISC_CHECK_AND_RETURN_RET_LOGE(bcData != NULL, false, DISC_BROADCAST, "bcData is nullptr");
893     if (bcData->payload == NULL) {
894         return false;
895     }
896     DISC_CHECK_AND_RETURN_RET_LOGD(bcData->type == BC_DATA_TYPE_SERVICE, false, DISC_BROADCAST,
897         "type dismatch, type=%{public}d", bcData->type);
898 
899     uint8_t filterSize = g_scanManager[managerId].filterSize;
900     for (uint8_t i = 0; i < filterSize; i++) {
901         BcScanFilter filter = g_scanManager[managerId].filter[i];
902         if (CheckServiceIsMatch(&filter, bcData)) {
903             return true;
904         }
905     }
906     return false;
907 }
908 
BcReportScanDataCallback(BroadcastProtocol protocol,int32_t adapterScanId,const SoftBusBcScanResult * reportData)909 static void BcReportScanDataCallback(BroadcastProtocol protocol,
910     int32_t adapterScanId, const SoftBusBcScanResult *reportData)
911 {
912     DISC_LOGD(DISC_BROADCAST, "enter report scan cb");
913     DISC_CHECK_AND_RETURN_LOGE(reportData != NULL, DISC_BROADCAST, "reportData is nullptr");
914 
915     BroadcastReportInfo bcInfo;
916     int32_t ret = BuildBroadcastReportInfo(reportData, &bcInfo);
917     DISC_CHECK_AND_RETURN_LOGE(ret == SOFTBUS_OK, DISC_BROADCAST, "build bc report info failed");
918     bool isFindMatchFiter = false;
919     for (uint32_t managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
920         if (SoftBusMutexLock(&g_scanLock) != 0) {
921             ReleaseBroadcastReportInfo(&bcInfo);
922             return;
923         }
924         ScanManager *scanManager = &g_scanManager[managerId];
925         if (scanManager->protocol != protocol || !scanManager->isUsed || !scanManager->isScanning ||
926             scanManager->filter == NULL || scanManager->scanCallback == NULL ||
927             scanManager->scanCallback->OnReportScanDataCallback == NULL ||
928             scanManager->adapterScanId != adapterScanId ||
929             !(CheckScanResultDataIsMatch(managerId, &(bcInfo.packet.bcData)) ||
930             (scanManager->srvType == SRV_TYPE_APPROACH &&
931             CheckScanResultDataIsMatchApproach(managerId, &(bcInfo.packet.rspData))))) {
932             SoftBusMutexUnlock(&g_scanLock);
933             continue;
934         }
935         isFindMatchFiter = true;
936         DISC_LOGD(DISC_BROADCAST, "srvType=%{public}s, managerId=%{public}u, adapterScanId=%{public}d",
937             GetSrvType(scanManager->srvType), managerId, adapterScanId);
938         ScanCallback callback = *(scanManager->scanCallback);
939         SoftBusMutexUnlock(&g_scanLock);
940         callback.OnReportScanDataCallback((int32_t)managerId, &bcInfo);
941     }
942     if (!isFindMatchFiter) {
943         DISC_LOGW(DISC_BROADCAST, "not find matched filter, adapterScanId=%{public}d", adapterScanId);
944     }
945     ReleaseBroadcastReportInfo(&bcInfo);
946 }
947 
BcScanStateChanged(BroadcastProtocol protocol,int32_t resultCode,bool isStartScan)948 static void BcScanStateChanged(BroadcastProtocol protocol, int32_t resultCode, bool isStartScan)
949 {
950     DISC_LOGD(DISC_BROADCAST, "enter scan state change");
951     for (uint32_t managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
952         int32_t ret = SoftBusMutexLock(&g_scanLock);
953         DISC_CHECK_AND_RETURN_LOGE(ret == SOFTBUS_OK, DISC_BROADCAST, "mutex error");
954 
955         ScanManager *scanManager = &g_scanManager[managerId];
956         if (scanManager->protocol != protocol || scanManager->isUsed || !scanManager->isScanning ||
957             scanManager->scanCallback == NULL || scanManager->scanCallback->OnScanStateChanged == NULL) {
958             SoftBusMutexUnlock(&g_scanLock);
959             continue;
960         }
961         DISC_LOGD(DISC_BROADCAST,
962             "srvType=%{public}s, managerId=%{public}u, adapterScanId=%{public}d, isStartScan=%{public}d",
963             GetSrvType(scanManager->srvType), managerId, scanManager->adapterScanId, isStartScan);
964         ScanCallback callback = *(scanManager->scanCallback);
965         SoftBusMutexUnlock(&g_scanLock);
966         callback.OnScanStateChanged(resultCode, isStartScan);
967     }
968 }
969 
ConvertBroadcastUuid(const SoftbusBroadcastUuid * uuid,BroadcastUuid * bcUuid)970 static int32_t ConvertBroadcastUuid(const SoftbusBroadcastUuid *uuid, BroadcastUuid *bcUuid)
971 {
972     DISC_CHECK_AND_RETURN_RET_LOGE(uuid != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "uuid is nullptr");
973     DISC_CHECK_AND_RETURN_RET_LOGE(bcUuid != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "bcUuid is nullptr");
974 
975     bcUuid->uuidLen = uuid->uuidLen;
976     bcUuid->uuid = (int8_t *)SoftBusCalloc(bcUuid->uuidLen);
977     DISC_CHECK_AND_RETURN_RET_LOGE(bcUuid->uuid != NULL, SOFTBUS_MALLOC_ERR, DISC_BROADCAST, "malloc failed");
978     if (memcpy_s(bcUuid->uuid, bcUuid->uuidLen, uuid->uuid, uuid->uuidLen) != EOK) {
979         DISC_LOGE(DISC_BROADCAST, "memcpy error");
980         SoftBusFree(bcUuid->uuid);
981         return SOFTBUS_MEM_ERR;
982     }
983     return SOFTBUS_OK;
984 }
985 
BcLpDeviceInfoCallback(BroadcastProtocol protocol,const SoftbusBroadcastUuid * uuid,int32_t type,uint8_t * data,uint32_t dataSize)986 static void BcLpDeviceInfoCallback(BroadcastProtocol protocol,
987     const SoftbusBroadcastUuid *uuid, int32_t type, uint8_t *data, uint32_t dataSize)
988 {
989     DISC_LOGD(DISC_BROADCAST, "enter lp cb");
990     DISC_CHECK_AND_RETURN_LOGE(uuid != NULL, DISC_BROADCAST, "uuid is nullptr");
991 
992     BroadcastUuid bcUuid = {0};
993     int32_t ret = ConvertBroadcastUuid(uuid, &bcUuid);
994     DISC_CHECK_AND_RETURN_LOGE(ret == SOFTBUS_OK, DISC_BROADCAST, "convert broadcast Uuid failed");
995 
996     for (uint32_t managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
997         ScanManager *scanManager = &g_scanManager[managerId];
998         if (scanManager->protocol != protocol || !scanManager->isUsed || scanManager->scanCallback == NULL ||
999             scanManager->scanCallback->OnLpDeviceInfoCallback == NULL) {
1000             continue;
1001         }
1002 
1003         scanManager->scanCallback->OnLpDeviceInfoCallback(&bcUuid, type, data, dataSize);
1004         break;
1005     }
1006     SoftBusFree(bcUuid.uuid);
1007 }
1008 
1009 static SoftbusScanCallback g_softbusBcBleScanCb = {
1010     .OnStartScanCallback = BcOnStartScanCallback,
1011     .OnStopScanCallback = BcStopScanCallback,
1012     .OnReportScanDataCallback = BcReportScanDataCallback,
1013     .OnScanStateChanged = BcScanStateChanged,
1014     .OnLpDeviceInfoCallback = BcLpDeviceInfoCallback,
1015 };
1016 
IsSrvTypeValid(BaseServiceType srvType)1017 static bool IsSrvTypeValid(BaseServiceType srvType)
1018 {
1019     return srvType >= 0 && srvType < SRV_TYPE_BUTT;
1020 }
1021 
InitializeBroadcaster(int32_t * bcId,BroadcastOptions * options,const BroadcastCallback * cb)1022 static int32_t InitializeBroadcaster(int32_t *bcId, BroadcastOptions *options, const BroadcastCallback *cb)
1023 {
1024     DISC_CHECK_AND_RETURN_RET_LOGE(bcId != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "bcId is nullptr");
1025     DISC_CHECK_AND_RETURN_RET_LOGE(cb != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "cb is nullptr");
1026 
1027     int32_t ret = SOFTBUS_OK;
1028     int32_t managerId;
1029 
1030     for (managerId = 0; managerId < BC_NUM_MAX; managerId++) {
1031         if (!g_bcManager[managerId].isUsed) {
1032             break;
1033         }
1034     }
1035     if (managerId == BC_NUM_MAX) {
1036         DISC_LOGE(DISC_BROADCAST, "no available adv manager");
1037         return SOFTBUS_BC_MGR_REG_NO_AVAILABLE_BC_ID;
1038     }
1039     DISC_LOGI(DISC_BROADCAST, "srvType=%{public}s, bcId=%{public}d, adapterBcId=%{public}d",
1040         GetSrvType(options->srvType), managerId, options->adapterBcId);
1041 
1042     *bcId = managerId;
1043     ret = SoftBusCondInit(&g_bcManager[managerId].cond);
1044     if (ret != SOFTBUS_OK) {
1045         DISC_LOGE(DISC_BROADCAST, "cond Init failed");
1046         return ret;
1047     }
1048     ret = SoftBusCondInit(&g_bcManager[managerId].enableCond);
1049     if (ret != SOFTBUS_OK) {
1050         DISC_LOGE(DISC_BROADCAST, "enableCond Init failed");
1051         return ret;
1052     }
1053     ret = SoftBusCondInit(&g_bcManager[managerId].disableCond);
1054     if (ret != SOFTBUS_OK) {
1055         DISC_LOGE(DISC_BROADCAST, "disableCond Init failed");
1056         return ret;
1057     }
1058     ret = SoftBusCondInit(&g_bcManager[managerId].setParamCond);
1059     if (ret != SOFTBUS_OK) {
1060         DISC_LOGE(DISC_BROADCAST, "setParamCond Init failed");
1061         return ret;
1062     }
1063     g_bcManager[managerId].srvType = options->srvType;
1064     g_bcManager[managerId].adapterBcId = options->adapterBcId;
1065     g_bcManager[managerId].isUsed = true;
1066     g_bcManager[managerId].isAdvertising = false;
1067     g_bcManager[managerId].isDisabled = false;
1068     g_bcManager[managerId].isDisableCb = false;
1069     g_bcManager[managerId].time = 0;
1070     g_bcManager[managerId].bcCallback = (BroadcastCallback *)cb;
1071     g_bcManager[managerId].protocol = options->protocol;
1072 
1073     return SOFTBUS_OK;
1074 }
1075 
RegisterBroadcaster(BroadcastProtocol protocol,BaseServiceType srvType,int32_t * bcId,const BroadcastCallback * cb)1076 int32_t RegisterBroadcaster(BroadcastProtocol protocol,
1077     BaseServiceType srvType, int32_t *bcId, const BroadcastCallback *cb)
1078 {
1079     static uint32_t callCount = 0;
1080     DISC_LOGI(DISC_BROADCAST, "enter register bc, callCount=%{public}u", callCount++);
1081     int32_t ret = SOFTBUS_OK;
1082     int32_t adapterBcId = -1;
1083     DISC_CHECK_AND_RETURN_RET_LOGE(IsSrvTypeValid(srvType), SOFTBUS_BC_MGR_INVALID_SRV, DISC_BROADCAST, "bad srvType");
1084     DISC_CHECK_AND_RETURN_RET_LOGE(bcId != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid param bcId");
1085     DISC_CHECK_AND_RETURN_RET_LOGE(cb != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid param cb!");
1086     DISC_CHECK_AND_RETURN_RET_LOGE(CheckProtocolIsValid(protocol), SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "bad id");
1087     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[protocol] != NULL, SOFTBUS_BC_MGR_NO_FUNC_REGISTERED,
1088         DISC_BROADCAST, "interface is nullptr");
1089     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[protocol]->RegisterBroadcaster != NULL,
1090         SOFTBUS_BC_MGR_FUNC_NULL, DISC_BROADCAST, "function is nullptr");
1091     ret = SoftBusMutexLock(&g_bcLock);
1092     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
1093     ret = g_interface[protocol]->RegisterBroadcaster(&adapterBcId, &g_softbusBcBleCb);
1094     if (ret != SOFTBUS_OK) {
1095         DISC_LOGE(DISC_BROADCAST, "call from adapter failed");
1096         SoftBusMutexUnlock(&g_bcLock);
1097         return ret;
1098     }
1099     BroadcastOptions options = {
1100         .adapterBcId = adapterBcId,
1101         .srvType = srvType,
1102         .protocol = protocol,
1103     };
1104     ret = InitializeBroadcaster(bcId, &options, cb);
1105     if (ret != SOFTBUS_OK) {
1106         SoftBusMutexUnlock(&g_bcLock);
1107         return ret;
1108     }
1109 
1110     SoftBusMutexUnlock(&g_bcLock);
1111     return SOFTBUS_OK;
1112 }
1113 
CheckBcIdIsValid(int32_t bcId)1114 static bool CheckBcIdIsValid(int32_t bcId)
1115 {
1116     if (bcId < 0 || bcId >= BC_NUM_MAX || !g_bcManager[bcId].isUsed) {
1117         DISC_LOGE(DISC_BROADCAST, "invalid param bcId=%{public}d", bcId);
1118         return false;
1119     }
1120     return true;
1121 }
1122 
UnRegisterBroadcaster(int32_t bcId)1123 int32_t UnRegisterBroadcaster(int32_t bcId)
1124 {
1125     DISC_LOGI(DISC_BROADCAST, "enter unRegister bc");
1126     int32_t ret = SoftBusMutexLock(&g_bcLock);
1127     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
1128 
1129     if (!CheckBcIdIsValid(bcId)) {
1130         DISC_LOGE(DISC_BROADCAST, "bcId is invalid");
1131         SoftBusMutexUnlock(&g_bcLock);
1132         return SOFTBUS_INVALID_PARAM;
1133     }
1134     BroadcastProtocol protocol = g_bcManager[bcId].protocol;
1135 
1136     if (!CheckProtocolIsValid(protocol) || g_interface[protocol] == NULL ||
1137         g_interface[protocol]->UnRegisterBroadcaster == NULL) {
1138         DISC_LOGE(DISC_BROADCAST, "not found or not register protocol=%{public}d", protocol);
1139         SoftBusMutexUnlock(&g_bcLock);
1140         return SOFTBUS_INVALID_PARAM;
1141     }
1142 
1143     if (g_bcManager[bcId].isAdvertising) {
1144         SoftBusMutexUnlock(&g_bcLock);
1145         (void)g_interface[protocol]->StopBroadcasting(g_bcManager[bcId].adapterBcId);
1146         SoftBusMutexLock(&g_bcLock);
1147     }
1148     ret = g_interface[protocol]->UnRegisterBroadcaster(g_bcManager[bcId].adapterBcId);
1149     if (ret != SOFTBUS_OK) {
1150         DISC_LOGE(DISC_BROADCAST, "call from adapter failed");
1151         SoftBusMutexUnlock(&g_bcLock);
1152         return ret;
1153     }
1154 
1155     g_bcManager[bcId].srvType = -1;
1156     g_bcManager[bcId].adapterBcId = -1;
1157     g_bcManager[bcId].isUsed = false;
1158     g_bcManager[bcId].isAdvertising = false;
1159     g_bcManager[bcId].isDisabled = false;
1160     g_bcManager[bcId].time = 0;
1161     SoftBusCondDestroy(&g_bcManager[bcId].cond);
1162     SoftBusCondDestroy(&g_bcManager[bcId].enableCond);
1163     SoftBusCondDestroy(&g_bcManager[bcId].disableCond);
1164     SoftBusCondDestroy(&g_bcManager[bcId].setParamCond);
1165     g_bcManager[bcId].bcCallback = NULL;
1166     g_bcManager[bcId].protocol = BROADCAST_PROTOCOL_BUTT;
1167 
1168     SoftBusMutexUnlock(&g_bcLock);
1169     return SOFTBUS_OK;
1170 }
1171 
GetSrvTypeIndex(BaseServiceType srvType)1172 static int32_t GetSrvTypeIndex(BaseServiceType srvType)
1173 {
1174     if (srvType == SRV_TYPE_LP_BURST || srvType == SRV_TYPE_LP_HB) {
1175         return CHANEL_LP;
1176     } else if (srvType == SRV_TYPE_CONN || srvType == SRV_TYPE_TRANS_MSG || srvType == SRV_TYPE_AUTH_CONN ||
1177         srvType == SRV_TYPE_APPROACH || srvType == SRV_TYPE_OH_APPROACH || srvType == SRV_TYPE_VLINK ||
1178         srvType == SRV_TYPE_FAST_OFFLINE) {
1179         return CHANEL_STEADY;
1180     } else if (srvType == SRV_TYPE_SHARE || srvType == SRV_TYPE_TOUCH) {
1181         return CHANEL_SHARE;
1182     } else if (srvType == SRV_TYPE_HB || srvType == SRV_TYPE_DIS || srvType == SRV_TYPE_OOP) {
1183         return CHANEL_UNSTEADY;
1184     } else if (srvType == SRV_TYPE_D2D_PAGING) {
1185         return CHANEL_SLE_D2D_PAGING;
1186     } else if (srvType == SRV_TYPE_D2D_GROUP_TALKIE) {
1187         return CHANEL_SLE_D2D_TALKIE;
1188     }
1189     return CHANEL_UNKNOW;
1190 }
1191 
RegisterScanListenerForChannel(BroadcastProtocol protocol,int32_t channel,int32_t * adapterScanId,const ScanCallback * cb)1192 static int32_t RegisterScanListenerForChannel(BroadcastProtocol protocol,
1193     int32_t channel, int32_t *adapterScanId, const ScanCallback *cb)
1194 {
1195     DISC_CHECK_AND_RETURN_RET_LOGE(adapterScanId != NULL, SOFTBUS_INVALID_PARAM,
1196         DISC_BROADCAST, "adapterScanId is nullptr");
1197     DISC_CHECK_AND_RETURN_RET_LOGE(cb != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "cb is nullptr");
1198 
1199     int32_t ret;
1200     if (g_AdapterStatusControl[channel].isAdapterScanCbReg) {
1201         *adapterScanId = g_AdapterStatusControl[channel].adapterScannerId;
1202         DISC_LOGI(DISC_BROADCAST, "service is already registered channel=%{public}d", channel);
1203         return SOFTBUS_OK;
1204     }
1205     ret = g_interface[protocol]->RegisterScanListener(adapterScanId, &g_softbusBcBleScanCb);
1206     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST,
1207         "call from adapter failed");
1208     g_AdapterStatusControl[channel].isAdapterScanCbReg = true;
1209     g_AdapterStatusControl[channel].adapterScannerId = *adapterScanId;
1210     DISC_LOGI(DISC_BROADCAST, "channel %{public}d register scan listener", channel);
1211     return SOFTBUS_OK;
1212 }
1213 
RegisterScanListenerSub(BroadcastProtocol protocol,BaseServiceType srvType,int32_t * adapterScanId,const ScanCallback * cb)1214 static int32_t RegisterScanListenerSub(
1215     BroadcastProtocol protocol, BaseServiceType srvType, int32_t *adapterScanId, const ScanCallback *cb)
1216 {
1217     DISC_CHECK_AND_RETURN_RET_LOGE(adapterScanId != NULL, SOFTBUS_INVALID_PARAM,
1218         DISC_BROADCAST, "adapterScanId is nullptr");
1219     DISC_CHECK_AND_RETURN_RET_LOGE(cb != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "cb is nullptr");
1220     int32_t channel = GetSrvTypeIndex(srvType);
1221     switch (channel) {
1222         case CHANEL_LP:
1223         case CHANEL_STEADY:
1224         case CHANEL_SHARE:
1225         case CHANEL_UNSTEADY:
1226         case CHANEL_SLE_D2D_PAGING:
1227         case CHANEL_SLE_D2D_TALKIE:
1228             return RegisterScanListenerForChannel(protocol, channel, adapterScanId, cb);
1229         default:
1230             DISC_LOGI(DISC_BROADCAST, "no server type channel srvType=%{public}s",
1231                 GetSrvType(srvType));
1232             return SOFTBUS_TRANS_MSG_START_SCAN_FAIL;
1233     }
1234 }
1235 
CheckSrvRegistered(BaseServiceType srvType)1236 static bool CheckSrvRegistered(BaseServiceType srvType)
1237 {
1238     for (int32_t managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
1239         if (!g_scanManager[managerId].isUsed) {
1240             continue;
1241         }
1242         if (g_scanManager[managerId].srvType == srvType) {
1243             DISC_LOGE(DISC_BROADCAST, "service is registered, srvType=%{public}s", GetSrvType(srvType));
1244             return true;
1245         }
1246     }
1247     return false;
1248 }
1249 
RegisterScanListener(BroadcastProtocol protocol,BaseServiceType srvType,int32_t * listenerId,const ScanCallback * cb)1250 int32_t RegisterScanListener(BroadcastProtocol protocol,
1251     BaseServiceType srvType, int32_t *listenerId, const ScanCallback *cb)
1252 {
1253     static uint32_t callCount = 0;
1254     DISC_LOGD(DISC_BROADCAST, "enter callCount=%{public}u", callCount++);
1255     int32_t ret = SOFTBUS_OK;
1256     int32_t adapterScanId = -1;
1257     DISC_CHECK_AND_RETURN_RET_LOGE(IsSrvTypeValid(srvType), SOFTBUS_BC_MGR_INVALID_SRV, DISC_BROADCAST, "bad srvType");
1258     DISC_CHECK_AND_RETURN_RET_LOGE(listenerId != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid listenerId");
1259     DISC_CHECK_AND_RETURN_RET_LOGE(cb != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid param cb");
1260     DISC_CHECK_AND_RETURN_RET_LOGE(CheckProtocolIsValid(protocol), SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "bad id");
1261     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[protocol] != NULL, SOFTBUS_BC_MGR_NO_FUNC_REGISTERED,
1262         DISC_BROADCAST, "interface is nullptr");
1263     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[protocol]->RegisterScanListener != NULL,
1264         SOFTBUS_BC_MGR_FUNC_NULL, DISC_BROADCAST, "function is nullptr");
1265     DISC_CHECK_AND_RETURN_RET_LOGE(!CheckSrvRegistered(srvType), SOFTBUS_BC_MGR_REG_DUP,
1266         DISC_BROADCAST, "already registered");
1267     ret = SoftBusMutexLock(&g_scanLock);
1268     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
1269 
1270     ret = RegisterScanListenerSub(protocol, srvType, &adapterScanId, cb);
1271     if (ret != SOFTBUS_OK) {
1272         SoftBusMutexUnlock(&g_scanLock);
1273         DISC_LOGE(DISC_BROADCAST, "register listerner failed");
1274         return ret;
1275     }
1276 
1277     int32_t managerId;
1278     for (managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
1279         if (!g_scanManager[managerId].isUsed) {
1280             break;
1281         }
1282     }
1283     if (managerId == SCAN_NUM_MAX) {
1284         DISC_LOGE(DISC_BROADCAST, "no available scanner");
1285         SoftBusMutexUnlock(&g_scanLock);
1286         return SOFTBUS_BC_MGR_REG_NO_AVAILABLE_LISN_ID;
1287     }
1288     DISC_LOGI(DISC_BROADCAST, "srvType=%{public}s, listenerId=%{public}d, adapterScanId=%{public}d",
1289               GetSrvType(srvType), managerId, adapterScanId);
1290     *listenerId = managerId;
1291     g_scanManager[managerId].srvType = srvType;
1292     g_scanManager[managerId].adapterScanId = adapterScanId;
1293     g_scanManager[managerId].isUsed = true;
1294     g_scanManager[managerId].isFliterChanged = true;
1295     g_scanManager[managerId].isScanning = false;
1296     g_scanManager[managerId].freq = SCAN_FREQ_LOW_POWER;
1297     g_scanManager[managerId].scanCallback = (ScanCallback *)cb;
1298     g_scanManager[managerId].protocol = protocol;
1299 
1300     SoftBusMutexUnlock(&g_scanLock);
1301     return SOFTBUS_OK;
1302 }
1303 
CheckScanIdIsValid(int32_t listenerId)1304 static bool CheckScanIdIsValid(int32_t listenerId)
1305 {
1306     if (listenerId < 0 || listenerId >= SCAN_NUM_MAX || !g_scanManager[listenerId].isUsed) {
1307         DISC_LOGE(DISC_BROADCAST, "invalid param listenerId=%{public}d", listenerId);
1308         return false;
1309     }
1310     return true;
1311 }
1312 
ReleaseBcScanFilter(int listenerId)1313 static void ReleaseBcScanFilter(int listenerId)
1314 {
1315     DISC_LOGD(DISC_BROADCAST, "enter release scan filter");
1316     BcScanFilter *filter = g_scanManager[listenerId].filter;
1317     if (filter == NULL) {
1318         return;
1319     }
1320     uint8_t filterSize = g_scanManager[listenerId].filterSize;
1321     while (filterSize-- > 0) {
1322         SoftBusFree((filter + filterSize)->address);
1323         SoftBusFree((filter + filterSize)->deviceName);
1324         SoftBusFree((filter + filterSize)->serviceData);
1325         SoftBusFree((filter + filterSize)->serviceDataMask);
1326         SoftBusFree((filter + filterSize)->manufactureData);
1327         SoftBusFree((filter + filterSize)->manufactureDataMask);
1328     }
1329     SoftBusFree(filter);
1330     g_scanManager[listenerId].filterSize = 0;
1331     g_scanManager[listenerId].filter = NULL;
1332 }
1333 
CheckNeedUnRegisterScanListener(int32_t listenerId)1334 static bool CheckNeedUnRegisterScanListener(int32_t listenerId)
1335 {
1336     int32_t adapterScanId = g_scanManager[listenerId].adapterScanId;
1337     BroadcastProtocol protocol = g_scanManager[listenerId].protocol;
1338     for (int32_t managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
1339         if (managerId != listenerId && g_scanManager[managerId].adapterScanId == adapterScanId &&
1340             g_scanManager[managerId].isScanning && g_scanManager[managerId].protocol == protocol) {
1341             return false;
1342         }
1343     }
1344     return true;
1345 }
1346 
CheckNeedUpdateScan(int32_t listenerId,int32_t * liveListenerId)1347 static bool CheckNeedUpdateScan(int32_t listenerId, int32_t *liveListenerId)
1348 {
1349     DISC_CHECK_AND_RETURN_RET_LOGE(liveListenerId != NULL, false, DISC_BROADCAST, "liveListenerId is nullptr");
1350 
1351     int32_t adapterScanId = g_scanManager[listenerId].adapterScanId;
1352     BroadcastProtocol protocol = g_scanManager[listenerId].protocol;
1353     for (int32_t managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
1354         if (managerId != listenerId && g_scanManager[managerId].adapterScanId == adapterScanId &&
1355             g_scanManager[managerId].isScanning && g_scanManager[managerId].protocol == protocol) {
1356             *liveListenerId = managerId;
1357             return true;
1358         }
1359     }
1360     return false;
1361 }
1362 
CopyScanFilterServiceInfo(const BcScanFilter * srcFilter,SoftBusBcScanFilter * dstFilter)1363 static int32_t CopyScanFilterServiceInfo(const BcScanFilter *srcFilter, SoftBusBcScanFilter *dstFilter)
1364 {
1365     DISC_CHECK_AND_RETURN_RET_LOGE(srcFilter != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "srcFilter is nullptr");
1366     DISC_CHECK_AND_RETURN_RET_LOGE(dstFilter != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "dstFilter is nullptr");
1367 
1368     dstFilter->serviceUuid = srcFilter->serviceUuid;
1369     dstFilter->serviceDataLength = srcFilter->serviceDataLength;
1370     if (srcFilter->serviceData != NULL && srcFilter->serviceDataLength > 0) {
1371         dstFilter->serviceData = (uint8_t *)SoftBusCalloc(dstFilter->serviceDataLength);
1372         DISC_CHECK_AND_RETURN_RET_LOGE(dstFilter->serviceData != NULL &&
1373             memcpy_s(dstFilter->serviceData, dstFilter->serviceDataLength,
1374             srcFilter->serviceData, srcFilter->serviceDataLength) == EOK,
1375             SOFTBUS_MEM_ERR, DISC_BROADCAST, "copy filter serviceData failed");
1376     }
1377     if (srcFilter->serviceDataMask != NULL && srcFilter->serviceDataLength > 0) {
1378         dstFilter->serviceDataMask = (uint8_t *)SoftBusCalloc(dstFilter->serviceDataLength);
1379         DISC_CHECK_AND_RETURN_RET_LOGE(dstFilter->serviceDataMask != NULL &&
1380             memcpy_s(dstFilter->serviceDataMask, dstFilter->serviceDataLength,
1381             srcFilter->serviceDataMask, srcFilter->serviceDataLength) == EOK,
1382             SOFTBUS_MEM_ERR, DISC_BROADCAST, "copy filter serviceDataMask failed");
1383     }
1384     return SOFTBUS_OK;
1385 }
1386 
CopySoftBusBcScanFilter(const BcScanFilter * srcFilter,SoftBusBcScanFilter * dstFilter)1387 static int32_t CopySoftBusBcScanFilter(const BcScanFilter *srcFilter, SoftBusBcScanFilter *dstFilter)
1388 {
1389     DISC_CHECK_AND_RETURN_RET_LOGE(srcFilter != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "srcFilter is NULL");
1390     DISC_CHECK_AND_RETURN_RET_LOGE(dstFilter != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "dstFilter is NULL");
1391     if (srcFilter->address != NULL) {
1392         uint32_t addressLength = strlen((char *)srcFilter->address) + 1;
1393         dstFilter->address = (int8_t *)SoftBusCalloc(addressLength);
1394         DISC_CHECK_AND_RETURN_RET_LOGE(dstFilter->address != NULL &&
1395             memcpy_s(dstFilter->address, addressLength, srcFilter->address, addressLength) == EOK,
1396             SOFTBUS_MEM_ERR, DISC_BROADCAST, "copy filter address failed");
1397     }
1398 
1399     if (srcFilter->deviceName != NULL) {
1400         uint32_t deviceNameLength = strlen((char *)srcFilter->deviceName) + 1;
1401         dstFilter->deviceName = (int8_t *)SoftBusCalloc(deviceNameLength);
1402         DISC_CHECK_AND_RETURN_RET_LOGE(dstFilter->deviceName != NULL &&
1403             memcpy_s(dstFilter->deviceName, deviceNameLength, srcFilter->deviceName, deviceNameLength) == EOK,
1404             SOFTBUS_MEM_ERR, DISC_BROADCAST, "copy filter deviceName failed");
1405     }
1406 
1407     int ret = CopyScanFilterServiceInfo(srcFilter, dstFilter);
1408     if (ret != SOFTBUS_OK) {
1409         return ret;
1410     }
1411 
1412     dstFilter->manufactureId = srcFilter->manufactureId;
1413     dstFilter->manufactureDataLength = srcFilter->manufactureDataLength;
1414     if (srcFilter->manufactureData != NULL && srcFilter->manufactureDataLength > 0) {
1415         dstFilter->manufactureData = (uint8_t *)SoftBusCalloc(dstFilter->manufactureDataLength);
1416         DISC_CHECK_AND_RETURN_RET_LOGE(dstFilter->manufactureData != NULL &&
1417             memcpy_s(dstFilter->manufactureData, dstFilter->manufactureDataLength,
1418             srcFilter->manufactureData, srcFilter->manufactureDataLength) == EOK,
1419             SOFTBUS_MEM_ERR, DISC_BROADCAST, "copy filter manufactureData failed");
1420     }
1421     if (srcFilter->manufactureDataMask != NULL && srcFilter->manufactureDataLength > 0) {
1422         dstFilter->manufactureDataMask = (uint8_t *)SoftBusCalloc(dstFilter->manufactureDataLength);
1423         DISC_CHECK_AND_RETURN_RET_LOGE(dstFilter->manufactureDataMask != NULL &&
1424             memcpy_s(dstFilter->manufactureDataMask, dstFilter->manufactureDataLength,
1425             srcFilter->manufactureDataMask, srcFilter->manufactureDataLength) == EOK,
1426             SOFTBUS_MEM_ERR, DISC_BROADCAST, "copy filter manufactureDataMask failed");
1427     }
1428     if (srcFilter->filterIndex == 0) {
1429         DISC_LOGW(DISC_BROADCAST, "invaild filterIndex");
1430     }
1431     dstFilter->filterIndex = srcFilter->filterIndex;
1432     dstFilter->advIndReport = srcFilter->advIndReport;
1433     return SOFTBUS_OK;
1434 }
1435 
CovertSoftBusBcScanFilters(const BcScanFilter * filter,uint8_t size,SoftBusBcScanFilter * adapterFilter)1436 static int32_t CovertSoftBusBcScanFilters(const BcScanFilter *filter, uint8_t size, SoftBusBcScanFilter *adapterFilter)
1437 {
1438     DISC_CHECK_AND_RETURN_RET_LOGE(filter != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "filter is nullptr");
1439     DISC_CHECK_AND_RETURN_RET_LOGE(adapterFilter != NULL, SOFTBUS_INVALID_PARAM,
1440         DISC_BROADCAST, "adapterFilter is nullptr");
1441 
1442     while (size-- > 0) {
1443         int32_t ret = CopySoftBusBcScanFilter(filter + size, adapterFilter + size);
1444         DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "copy filter failed");
1445     }
1446     return SOFTBUS_OK;
1447 }
1448 
ReleaseSoftBusBcScanFilter(SoftBusBcScanFilter * filter,int32_t size)1449 static void ReleaseSoftBusBcScanFilter(SoftBusBcScanFilter *filter, int32_t size)
1450 {
1451     DISC_CHECK_AND_RETURN_LOGE(filter != NULL, DISC_BROADCAST, "filter is nullptr");
1452 
1453     if (filter != NULL) {
1454         while (size-- > 0) {
1455             if ((filter + size)->address != NULL) {
1456                 SoftBusFree((filter + size)->address);
1457             }
1458             if ((filter + size)->deviceName != NULL) {
1459                 SoftBusFree((filter + size)->deviceName);
1460             }
1461             if ((filter + size)->serviceData != NULL) {
1462                 SoftBusFree((filter + size)->serviceData);
1463             }
1464             if ((filter + size)->serviceDataMask != NULL) {
1465                 SoftBusFree((filter + size)->serviceDataMask);
1466             }
1467             if ((filter + size)->manufactureData != NULL) {
1468                 SoftBusFree((filter + size)->manufactureData);
1469             }
1470             if ((filter + size)->manufactureDataMask != NULL) {
1471                 SoftBusFree((filter + size)->manufactureDataMask);
1472             }
1473         }
1474         SoftBusFree(filter);
1475     }
1476 }
1477 
CombineSoftbusBcScanFilters(int32_t listenerId,SoftBusBcScanFilter ** adapterFilter,int32_t * filterSize)1478 static int32_t CombineSoftbusBcScanFilters(int32_t listenerId, SoftBusBcScanFilter **adapterFilter, int32_t *filterSize)
1479 {
1480     DISC_LOGD(DISC_BROADCAST, "enter combine scan filters");
1481     DISC_CHECK_AND_RETURN_RET_LOGE(adapterFilter != NULL, SOFTBUS_INVALID_PARAM,
1482         DISC_BROADCAST, "adapterFilter is nullptr");
1483     DISC_CHECK_AND_RETURN_RET_LOGE(filterSize != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "filterSize is nullptr");
1484 
1485     uint8_t size = 0;
1486     BroadcastProtocol protocol = g_scanManager[listenerId].protocol;
1487     for (int32_t managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
1488         ScanManager *scanManager = &g_scanManager[managerId];
1489         if (!scanManager->isUsed || (!scanManager->isScanning && managerId != listenerId) ||
1490             scanManager->adapterScanId != g_scanManager[listenerId].adapterScanId ||
1491             scanManager->protocol != protocol) {
1492             continue;
1493         }
1494 
1495         size += scanManager->filterSize;
1496     }
1497     *adapterFilter = (SoftBusBcScanFilter *)SoftBusCalloc(sizeof(SoftBusBcScanFilter) * size);
1498     DISC_CHECK_AND_RETURN_RET_LOGE(*adapterFilter != NULL, SOFTBUS_MALLOC_ERR, DISC_BROADCAST, "malloc failed");
1499     *filterSize = size;
1500 
1501     for (int32_t managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
1502         ScanManager *scanManager = &g_scanManager[managerId];
1503         if (!scanManager->isUsed || (!scanManager->isScanning && managerId != listenerId) ||
1504             scanManager->adapterScanId != g_scanManager[listenerId].adapterScanId ||
1505             scanManager->protocol != protocol) {
1506             continue;
1507         }
1508 
1509         uint8_t currentSize = g_scanManager[managerId].filterSize;
1510         BcScanFilter *filter = g_scanManager[managerId].filter;
1511         size = size - currentSize;
1512         int32_t ret = CovertSoftBusBcScanFilters(filter, currentSize, *adapterFilter + size);
1513         if (ret != SOFTBUS_OK) {
1514             ReleaseSoftBusBcScanFilter(*adapterFilter, size);
1515             *adapterFilter = NULL;
1516             DISC_LOGE(DISC_BROADCAST, "convert bc scan filters failed");
1517             return ret;
1518         }
1519     }
1520     return SOFTBUS_OK;
1521 }
1522 
GetScanFiltersForOneListener(int32_t listenerId,SoftBusBcScanFilter ** adapterFilter,int32_t * filterSize)1523 static int32_t GetScanFiltersForOneListener(int32_t listenerId, SoftBusBcScanFilter **adapterFilter,
1524     int32_t *filterSize)
1525 {
1526     DISC_CHECK_AND_RETURN_RET_LOGE(adapterFilter != NULL, SOFTBUS_INVALID_PARAM,
1527         DISC_BROADCAST, "adapterFilter is nullptr");
1528     DISC_CHECK_AND_RETURN_RET_LOGE(filterSize != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "filterSize is nullptr");
1529 
1530     if (g_scanManager[listenerId].filterSize == 0) {
1531         DISC_LOGE(DISC_BROADCAST, "adapterFilter couldn't assemble");
1532         return SOFTBUS_DISCOVER_BLE_END_SCAN_FAIL;
1533     }
1534     uint8_t size = g_scanManager[listenerId].filterSize;
1535     *adapterFilter = (SoftBusBcScanFilter *)SoftBusCalloc(sizeof(SoftBusBcScanFilter) * size);
1536     DISC_CHECK_AND_RETURN_RET_LOGE(*adapterFilter != NULL, SOFTBUS_MALLOC_ERR, DISC_BROADCAST, "malloc failed");
1537     *filterSize = size;
1538     BcScanFilter *filter = g_scanManager[listenerId].filter;
1539     int32_t ret = CovertSoftBusBcScanFilters(filter, size, *adapterFilter);
1540     if (ret != SOFTBUS_OK) {
1541         ReleaseSoftBusBcScanFilter(*adapterFilter, size);
1542         *adapterFilter = NULL;
1543         DISC_LOGE(DISC_BROADCAST, "convert bc scan filters failed");
1544         return ret;
1545     }
1546     return SOFTBUS_OK;
1547 }
1548 
DeleteFilterByIndex(int32_t listenerId,SoftBusBcScanFilter ** adapterFilter,SoftBusBcScanParams * adapterParam,int32_t filterSize)1549 static int32_t DeleteFilterByIndex(int32_t listenerId, SoftBusBcScanFilter **adapterFilter,
1550     SoftBusBcScanParams *adapterParam, int32_t filterSize)
1551 {
1552     DISC_CHECK_AND_RETURN_RET_LOGE(adapterFilter != NULL, SOFTBUS_INVALID_PARAM,
1553         DISC_BROADCAST, "adapterFilter is nullptr");
1554     DISC_CHECK_AND_RETURN_RET_LOGE(adapterParam != NULL, SOFTBUS_INVALID_PARAM,
1555         DISC_BROADCAST, "adapterParam is nullptr");
1556     ScanManager *scanManager = &g_scanManager[listenerId];
1557     DISC_LOGI(DISC_BROADCAST, "enter delete filter by index, listenerId=%{public}d, size=%{public}d",
1558         listenerId, scanManager->deleteSize);
1559     int32_t ret;
1560     uint8_t size = scanManager->deleteSize;
1561     DISC_CHECK_AND_RETURN_RET_LOGE(size != 0, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "size is 0");
1562     *adapterFilter = (SoftBusBcScanFilter *)SoftBusCalloc(sizeof(SoftBusBcScanFilter) * size);
1563     DISC_CHECK_AND_RETURN_RET_LOGE(*adapterFilter != NULL, SOFTBUS_MALLOC_ERR, DISC_BROADCAST,
1564         "memory allocation failed");
1565     for (int i = 0; i < size; i++) {
1566         int filterIndex = scanManager->deleted[i];
1567         if (filterIndex == 0) {
1568             DISC_LOGE(DISC_BROADCAST, "invalid index");
1569             ReleaseSoftBusBcScanFilter(*adapterFilter, size);
1570             *adapterFilter = NULL;
1571             return SOFTBUS_INVALID_PARAM;
1572         }
1573         (*adapterFilter + i)->filterIndex = filterIndex;
1574         BroadcastProtocol protocol = scanManager->protocol;
1575         ret = g_interface[protocol]->SetScanParams(scanManager->adapterScanId, adapterParam,
1576             *adapterFilter, filterSize, SOFTBUS_SCAN_FILTER_CMD_DELETE);
1577         if (ret == SOFTBUS_OK) {
1578             g_firstSetIndex[filterIndex] = false;
1579         }
1580     }
1581 
1582     return ret;
1583 }
1584 
GetAddFiltersByIndex(int32_t listenerId,SoftBusBcScanFilter ** adapterFilter)1585 static int32_t GetAddFiltersByIndex(int32_t listenerId, SoftBusBcScanFilter **adapterFilter)
1586 {
1587     DISC_CHECK_AND_RETURN_RET_LOGE(adapterFilter != NULL, SOFTBUS_INVALID_PARAM,
1588         DISC_BROADCAST, "adapterFilter is nullptr");
1589     DISC_LOGI(DISC_BROADCAST, "enter add filter by index, listenerId=%{public}d, size=%{public}d",
1590         listenerId, g_scanManager[listenerId].addSize);
1591 
1592     int32_t ret;
1593     uint8_t size = g_scanManager[listenerId].addSize;
1594     DISC_CHECK_AND_RETURN_RET_LOGE(size != 0, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "size is 0");
1595     *adapterFilter = (SoftBusBcScanFilter *)SoftBusCalloc(sizeof(SoftBusBcScanFilter) * size);
1596     DISC_CHECK_AND_RETURN_RET_LOGE(*adapterFilter != NULL, SOFTBUS_MALLOC_ERR, DISC_BROADCAST,
1597         "memory allocation failed");
1598     for (int i = 0; i < size; i++) {
1599         int addIndex = g_scanManager[listenerId].added[i];
1600         BcScanFilter *tempFilter = &(g_scanManager[listenerId].filter[addIndex]);
1601         if (tempFilter->filterIndex == 0) {
1602             DISC_LOGE(DISC_BROADCAST, "invalid index");
1603             ReleaseSoftBusBcScanFilter(*adapterFilter, size);
1604             *adapterFilter = NULL;
1605             return SOFTBUS_INVALID_PARAM;
1606         }
1607         ret = CopySoftBusBcScanFilter(tempFilter, (*adapterFilter) + i);
1608     }
1609 
1610     return ret;
1611 }
1612 
GetModifyFiltersByIndex(int32_t listenerId,SoftBusBcScanFilter ** adapterFilter)1613 static int32_t GetModifyFiltersByIndex(int32_t listenerId, SoftBusBcScanFilter **adapterFilter)
1614 {
1615     DISC_CHECK_AND_RETURN_RET_LOGE(adapterFilter != NULL, SOFTBUS_INVALID_PARAM,
1616         DISC_BROADCAST, "adapterFilter is nullptr");
1617     DISC_LOGI(DISC_BROADCAST, "enter Modify filter by index, listenerId=%{public}d, addSize=%{public}d",
1618         listenerId, g_scanManager[listenerId].addSize);
1619     DISC_CHECK_AND_RETURN_RET_LOGE(g_scanManager[listenerId].addSize != 0, SOFTBUS_INVALID_PARAM, DISC_BROADCAST,
1620         "addSize is 0");
1621     DISC_CHECK_AND_RETURN_RET_LOGE(g_scanManager[listenerId].deleteSize != 0, SOFTBUS_INVALID_PARAM, DISC_BROADCAST,
1622         "deleteSize is 0");
1623 
1624     int32_t ret;
1625     uint8_t size = g_scanManager[listenerId].addSize;
1626     *adapterFilter = (SoftBusBcScanFilter *)SoftBusCalloc(sizeof(SoftBusBcScanFilter) * size);
1627     DISC_CHECK_AND_RETURN_RET_LOGE(*adapterFilter != NULL, SOFTBUS_MALLOC_ERR, DISC_BROADCAST,
1628         "memory allocation failed");
1629     for (int i = 0; i < size; i++) {
1630         uint8_t addIndex = g_scanManager[listenerId].added[i];
1631         uint8_t deleteIndex = g_scanManager[listenerId].deleted[i];
1632         int replaceIndex = g_scanManager[listenerId].filter[addIndex].filterIndex;
1633         g_firstSetIndex[replaceIndex] = false;
1634         BcScanFilter *tempFilter = &(g_scanManager[listenerId].filter[addIndex]);
1635         tempFilter->filterIndex = deleteIndex;
1636         if (tempFilter->filterIndex == 0) {
1637             DISC_LOGE(DISC_BROADCAST, "invalid index");
1638             ReleaseSoftBusBcScanFilter(*adapterFilter, size);
1639             *adapterFilter = NULL;
1640             return SOFTBUS_INVALID_PARAM;
1641         }
1642         ret = CopySoftBusBcScanFilter(tempFilter, (*adapterFilter) + i);
1643     }
1644 
1645     return ret;
1646 }
1647 
GetBcScanFilters(int32_t listenerId,SoftBusBcScanFilter ** adapterFilter,int32_t * filterSize)1648 static int32_t GetBcScanFilters(int32_t listenerId, SoftBusBcScanFilter **adapterFilter, int32_t *filterSize)
1649 {
1650     DISC_CHECK_AND_RETURN_RET_LOGE(adapterFilter != NULL, SOFTBUS_INVALID_PARAM,
1651         DISC_BROADCAST, "adapterFilter is nullptr");
1652     DISC_CHECK_AND_RETURN_RET_LOGE(filterSize != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "filterSize is nullptr");
1653     return CombineSoftbusBcScanFilters(listenerId, adapterFilter, filterSize);
1654 }
1655 
DumpBcScanFilter(const SoftBusBcScanFilter * nativeFilter,uint8_t filterSize)1656 static void DumpBcScanFilter(const SoftBusBcScanFilter *nativeFilter, uint8_t filterSize)
1657 {
1658     DISC_CHECK_AND_RETURN_LOGE(nativeFilter != NULL, DISC_BROADCAST, "invalid param nativeFilter");
1659     DISC_CHECK_AND_RETURN_LOGE(filterSize != 0, DISC_BROADCAST, "filterSize is 0");
1660 
1661     while (filterSize-- > 0) {
1662         int32_t len = (nativeFilter + filterSize)->serviceDataLength;
1663         if (len > 0) {
1664             DumpSoftbusData("service data", len, (nativeFilter + filterSize)->serviceData);
1665             DumpSoftbusData("service dataMask", len, (nativeFilter + filterSize)->serviceDataMask);
1666         } else {
1667             len = (nativeFilter + filterSize)->manufactureDataLength;
1668             if (len <= 0) {
1669                 continue;
1670             }
1671             DumpSoftbusData("manufacture data", len, (nativeFilter + filterSize)->manufactureData);
1672             DumpSoftbusData("manufacture dataMask", len, (nativeFilter + filterSize)->manufactureDataMask);
1673         }
1674     }
1675 }
1676 
BuildSoftBusBcScanParams(const BcScanParams * param,SoftBusBcScanParams * adapterParam)1677 static void BuildSoftBusBcScanParams(const BcScanParams *param, SoftBusBcScanParams *adapterParam)
1678 {
1679     DISC_LOGD(DISC_BROADCAST, "enter scan param");
1680     DISC_CHECK_AND_RETURN_LOGE(param != NULL, DISC_BROADCAST, "param is nullptr");
1681     DISC_CHECK_AND_RETURN_LOGE(adapterParam != NULL, DISC_BROADCAST, "adapterParam is nullptr");
1682     (void)memset_s(adapterParam, sizeof(SoftBusBcScanParams), 0x0, sizeof(SoftBusBcScanParams));
1683 
1684     // convert params
1685     adapterParam->scanInterval = param->scanInterval;
1686     adapterParam->scanWindow = param->scanWindow;
1687     adapterParam->scanType = param->scanType;
1688     adapterParam->scanPhy = param->scanPhy;
1689     adapterParam->scanFilterPolicy = param->scanFilterPolicy;
1690     adapterParam->frameType = param->frameType;
1691 }
1692 
GetScanIntervalAndWindow(int32_t freq,SoftBusBcScanParams * adapterParam)1693 static void GetScanIntervalAndWindow(int32_t freq, SoftBusBcScanParams *adapterParam)
1694 {
1695     DISC_CHECK_AND_RETURN_LOGE(adapterParam != NULL, DISC_BROADCAST, "adapterParam is nullptr");
1696 
1697     if (freq == SCAN_FREQ_P2_60_3000) {
1698         adapterParam->scanInterval = SOFTBUS_BC_SCAN_INTERVAL_P2;
1699         adapterParam->scanWindow = SOFTBUS_BC_SCAN_WINDOW_P2;
1700     }
1701     if (freq == SCAN_FREQ_P2_30_1500) {
1702         adapterParam->scanInterval = SOFTBUS_BC_SCAN_INTERVAL_P2_FAST;
1703         adapterParam->scanWindow = SOFTBUS_BC_SCAN_WINDOW_P2_FAST;
1704     }
1705     if (freq == SCAN_FREQ_P10_30_300) {
1706         adapterParam->scanInterval = SOFTBUS_BC_SCAN_INTERVAL_P10;
1707         adapterParam->scanWindow = SOFTBUS_BC_SCAN_WINDOW_P10;
1708     }
1709     if (freq == SCAN_FREQ_P25_60_240) {
1710         adapterParam->scanInterval = SOFTBUS_BC_SCAN_INTERVAL_P25;
1711         adapterParam->scanWindow = SOFTBUS_BC_SCAN_WINDOW_P25;
1712     }
1713     if (freq == SCAN_FREQ_P50_30_60) {
1714         adapterParam->scanInterval = SOFTBUS_BC_SCAN_INTERVAL_P50;
1715         adapterParam->scanWindow = SOFTBUS_BC_SCAN_WINDOW_P50;
1716     }
1717     if (freq == SCAN_FREQ_P75_30_40) {
1718         adapterParam->scanInterval = SOFTBUS_BC_SCAN_INTERVAL_P75;
1719         adapterParam->scanWindow = SOFTBUS_BC_SCAN_WINDOW_P75;
1720     }
1721     if (freq == SCAN_FREQ_P100_1000_1000) {
1722         adapterParam->scanInterval = SOFTBUS_BC_SCAN_INTERVAL_P100;
1723         adapterParam->scanWindow = SOFTBUS_BC_SCAN_WINDOW_P100;
1724     }
1725     if (freq == SCAN_FREQ_P10_400_40_LONG_RANGE) {
1726         adapterParam->scanInterval = SOFTBUS_BC_SCAN_INTERVAL_P10_LONG_RANGE;
1727         adapterParam->scanWindow = SOFTBUS_BC_SCAN_WINDOW_P10_LONG_RANGE;
1728     }
1729     if (freq == SCAN_FREQ_P100_30_30_LONG_RANGE) {
1730         adapterParam->scanInterval = SOFTBUS_BC_SCAN_INTERVAL_P100_LONG_RANGE;
1731         adapterParam->scanWindow = SOFTBUS_BC_SCAN_WINDOW_P100_LONG_RANGE;
1732     }
1733 }
1734 
CheckScanFreq(int32_t listenerId,SoftBusBcScanParams * adapterParam)1735 static void CheckScanFreq(int32_t listenerId, SoftBusBcScanParams *adapterParam)
1736 {
1737     DISC_CHECK_AND_RETURN_LOGE(adapterParam != NULL, DISC_BROADCAST, "adapterParam is nullptr");
1738 
1739     int32_t adapterScanId = g_scanManager[listenerId].adapterScanId;
1740     int32_t maxFreq = g_scanManager[listenerId].freq;
1741     BroadcastProtocol protocol = g_scanManager[listenerId].protocol;
1742     for (uint32_t managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
1743         ScanManager *scanManager = &g_scanManager[managerId];
1744         if (!scanManager->isUsed || !scanManager->isScanning || scanManager->adapterScanId != adapterScanId ||
1745             protocol != scanManager->protocol) {
1746             continue;
1747         }
1748         maxFreq = (maxFreq > (int32_t)(scanManager->freq)) ? maxFreq : (int32_t)(scanManager->freq);
1749     }
1750 
1751     GetScanIntervalAndWindow(maxFreq, adapterParam);
1752 }
1753 
CheckAndStopScan(BroadcastProtocol protocol,int32_t listenerId)1754 static int32_t CheckAndStopScan(BroadcastProtocol protocol, int32_t listenerId)
1755 {
1756     int32_t liveListenerId = -1;
1757     int32_t ret;
1758     bool needUpdate = CheckNeedUpdateScan(listenerId, &liveListenerId);
1759     if (!needUpdate) {
1760         DISC_LOGI(DISC_BROADCAST, "call stop scan, adapterId=%{public}d", g_scanManager[listenerId].adapterScanId);
1761         ret = g_interface[protocol]->StopScan(g_scanManager[listenerId].adapterScanId);
1762         if (ret != SOFTBUS_OK) {
1763             g_scanManager[listenerId].scanCallback->OnStopScanCallback(listenerId, (int32_t)SOFTBUS_BC_STATUS_FAIL);
1764             DISC_LOGE(DISC_BROADCAST, "call from adapter failed");
1765             return ret;
1766         }
1767     } else {
1768         int32_t filterSize = 0;
1769         SoftBusBcScanFilter *adapterFilter = NULL;
1770         g_scanManager[listenerId].isScanning = false;
1771         ret = GetScanFiltersForOneListener(listenerId, &adapterFilter, &filterSize);
1772         DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "get bc scan filters failed");
1773         DumpBcScanFilter(adapterFilter, filterSize);
1774         SoftBusBcScanParams adapterParam;
1775         BuildSoftBusBcScanParams(&(g_scanManager[listenerId].param), &adapterParam);
1776         CheckScanFreq(liveListenerId, &adapterParam);
1777         ret = g_interface[protocol]->SetScanParams(g_scanManager[listenerId].adapterScanId, &adapterParam,
1778             adapterFilter, filterSize, SOFTBUS_SCAN_FILTER_CMD_DELETE);
1779 
1780         ReleaseSoftBusBcScanFilter(adapterFilter, filterSize);
1781         if (ret != SOFTBUS_OK) {
1782             g_scanManager[listenerId].isScanning = true;
1783             g_scanManager[listenerId].scanCallback->OnStartScanCallback(listenerId, (int32_t)SOFTBUS_BC_STATUS_FAIL);
1784             DISC_LOGE(DISC_BROADCAST, "call from adapter failed");
1785             return ret;
1786         }
1787         g_scanManager[listenerId].isScanning = false;
1788     }
1789     return SOFTBUS_OK;
1790 }
1791 
UnRegisterScanListener(int32_t listenerId)1792 int32_t UnRegisterScanListener(int32_t listenerId)
1793 {
1794     DISC_LOGI(DISC_BROADCAST, "enter unregister scan, listenerId=%{public}d", listenerId);
1795 
1796     int32_t ret = SoftBusMutexLock(&g_scanLock);
1797     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
1798     if (!CheckScanIdIsValid(listenerId)) {
1799         SoftBusMutexUnlock(&g_scanLock);
1800         return SOFTBUS_INVALID_PARAM;
1801     }
1802     BroadcastProtocol protocol = g_scanManager[listenerId].protocol;
1803     if (!CheckProtocolIsValid(protocol) || g_interface[protocol] == NULL ||
1804         g_interface[protocol]->UnRegisterScanListener == NULL) {
1805         SoftBusMutexUnlock(&g_scanLock);
1806         return SOFTBUS_INVALID_PARAM;
1807     }
1808     int32_t adapterScanId = g_scanManager[listenerId].adapterScanId;
1809     if (g_scanManager[listenerId].isScanning) {
1810         ret = CheckAndStopScan(protocol, listenerId);
1811         if (ret != SOFTBUS_OK) {
1812             DISC_LOGE(DISC_BROADCAST, "stop scan failed");
1813             SoftBusMutexUnlock(&g_scanLock);
1814             return ret;
1815         }
1816     }
1817     if (CheckNeedUnRegisterScanListener(listenerId)) {
1818         for (uint32_t index = 0; index < GATT_SCAN_MAX_NUM; ++index) {
1819             if (adapterScanId == g_AdapterStatusControl[index].adapterScannerId) {
1820                 g_AdapterStatusControl[index].adapterScannerId = -1;
1821                 g_AdapterStatusControl[index].isAdapterScanCbReg = false;
1822             }
1823         }
1824         SoftBusMutexUnlock(&g_scanLock);
1825         ret = g_interface[protocol]->UnRegisterScanListener(adapterScanId);
1826         DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "call from adapter failed");
1827         ret = SoftBusMutexLock(&g_scanLock);
1828         DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "mutex error");
1829     }
1830     DISC_LOGD(DISC_BROADCAST, "srvType=%{public}s", GetSrvType(g_scanManager[listenerId].srvType));
1831     ReleaseBcScanFilter(listenerId);
1832     g_scanManager[listenerId].srvType = -1;
1833     g_scanManager[listenerId].adapterScanId = -1;
1834     g_scanManager[listenerId].isUsed = false;
1835     g_scanManager[listenerId].isFliterChanged = false;
1836     g_scanManager[listenerId].freq = SCAN_FREQ_LOW_POWER;
1837     g_scanManager[listenerId].scanCallback = NULL;
1838     g_scanManager[listenerId].isScanning = false;
1839     g_scanManager[listenerId].protocol = BROADCAST_PROTOCOL_BUTT;
1840     SoftBusMutexUnlock(&g_scanLock);
1841     return SOFTBUS_OK;
1842 }
1843 
ConvertBcParams(BroadcastProtocol protocol,const BroadcastParam * srcParam,SoftbusBroadcastParam * dstParam)1844 static void ConvertBcParams(BroadcastProtocol protocol,
1845     const BroadcastParam *srcParam, SoftbusBroadcastParam *dstParam)
1846 {
1847     DISC_LOGD(DISC_BROADCAST, "enter covert bc param");
1848     DISC_CHECK_AND_RETURN_LOGE(srcParam != NULL, DISC_BROADCAST, "srcParam is nullptr");
1849     DISC_CHECK_AND_RETURN_LOGE(dstParam != NULL, DISC_BROADCAST, "dstParam is nullptr");
1850 
1851     dstParam->minInterval = srcParam->minInterval;
1852     dstParam->maxInterval = srcParam->maxInterval;
1853     dstParam->advType = srcParam->advType;
1854     dstParam->advFilterPolicy = srcParam->advFilterPolicy;
1855     dstParam->ownAddrType = srcParam->ownAddrType;
1856     dstParam->peerAddrType = srcParam->peerAddrType;
1857     if (memcpy_s(dstParam->peerAddr.addr, SOFTBUS_ADDR_MAC_LEN, srcParam->peerAddr.addr, BC_ADDR_MAC_LEN) != EOK) {
1858         DISC_LOGE(DISC_BROADCAST, "memcpy peerAddr failed");
1859         return;
1860     }
1861     dstParam->channelMap = srcParam->channelMap;
1862     dstParam->duration = srcParam->duration;
1863     dstParam->txPower = srcParam->txPower;
1864     dstParam->isSupportRpa = srcParam->isSupportRpa;
1865     dstParam->linkRole = srcParam->linkRole;
1866     dstParam->frameType = srcParam->frameType;
1867     if (memcpy_s(dstParam->ownIrk, SOFTBUS_IRK_LEN, srcParam->ownIrk, BC_IRK_LEN) != EOK) {
1868         DISC_LOGE(DISC_BROADCAST, "memcpy ownIrk failed");
1869         return;
1870     }
1871     if (memcpy_s(dstParam->ownUdidHash, SOFTBUS_UDID_HASH_LEN, srcParam->ownUdidHash, BC_UDID_HASH_LEN) != EOK) {
1872         DISC_LOGE(DISC_BROADCAST, "memcpy ownUdidHash failed");
1873         return;
1874     }
1875     if (memcpy_s(dstParam->localAddr.addr, BC_ADDR_MAC_LEN, srcParam->localAddr.addr,
1876         BC_ADDR_MAC_LEN) != EOK) {
1877         DISC_LOGE(DISC_BROADCAST, "memcpy localAddr failed");
1878         return;
1879     }
1880 }
1881 
DumpBroadcastPacket(const BroadcastPayload * bcData,const BroadcastPayload * rspData)1882 static void DumpBroadcastPacket(const BroadcastPayload *bcData, const BroadcastPayload *rspData)
1883 {
1884     DISC_CHECK_AND_RETURN_LOGE(bcData != NULL, DISC_BROADCAST, "bcData is nullptr");
1885     DISC_CHECK_AND_RETURN_LOGE(rspData != NULL, DISC_BROADCAST, "rspData is nullptr");
1886 
1887     if (bcData->payloadLen != 0 && bcData->payload != NULL) {
1888         DumpSoftbusData("BroadcastPayload bcData", bcData->payloadLen, bcData->payload);
1889     }
1890     if (rspData->payloadLen != 0 && rspData->payload != NULL) {
1891         DumpSoftbusData("BroadcastPayload rspData", rspData->payloadLen, rspData->payload);
1892     }
1893 }
1894 
SoftBusCondWaitSec(int64_t sec,int32_t bcId,SoftBusMutex * mutex)1895 static int32_t SoftBusCondWaitSec(int64_t sec, int32_t bcId, SoftBusMutex *mutex)
1896 {
1897     DISC_CHECK_AND_RETURN_RET_LOGE(mutex != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "mutex is nullptr");
1898 
1899     SoftBusSysTime absTime = {0};
1900     int32_t ret = SoftBusGetTime(&absTime);
1901     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "softbus get time failed");
1902 
1903     absTime.sec += sec;
1904     if (SoftBusCondWait(&g_bcManager[bcId].cond, mutex, &absTime) != SOFTBUS_OK) {
1905         DISC_LOGE(DISC_BROADCAST, "wait timeout");
1906         return SOFTBUS_TIMOUT;
1907     }
1908     return SOFTBUS_OK;
1909 }
1910 
SoftbusPauseCondWaitSec(int64_t sec,int32_t bcId,SoftBusMutex * mutex,SoftBusCond * cond)1911 static int32_t SoftbusPauseCondWaitSec(int64_t sec, int32_t bcId, SoftBusMutex *mutex, SoftBusCond *cond)
1912 {
1913     DISC_CHECK_AND_RETURN_RET_LOGE(mutex != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "mutex is nullptr");
1914 
1915     SoftBusSysTime absTime = {0};
1916     int32_t ret = SoftBusGetTime(&absTime);
1917     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "softbus get time failed");
1918 
1919     absTime.sec += sec;
1920     if (SoftBusCondWait(cond, mutex, &absTime) != SOFTBUS_OK) {
1921         DISC_LOGE(DISC_BROADCAST, "wait timeout");
1922         return SOFTBUS_TIMOUT;
1923     }
1924     return SOFTBUS_OK;
1925 }
1926 
BuildSoftbusBcPayload(const BroadcastPayload * srcData,SoftbusBroadcastPayload * dstData)1927 static int32_t BuildSoftbusBcPayload(const BroadcastPayload *srcData, SoftbusBroadcastPayload *dstData)
1928 {
1929     DISC_CHECK_AND_RETURN_RET_LOGE(srcData != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "srcData is nullptr");
1930     DISC_CHECK_AND_RETURN_RET_LOGE(dstData != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "dstData is nullptr");
1931 
1932     dstData->type = (SoftbusBcDataType)srcData->type;
1933     dstData->id = srcData->id;
1934     dstData->payloadLen = srcData->payloadLen;
1935 
1936     dstData->payload = (uint8_t *)SoftBusCalloc(dstData->payloadLen);
1937     DISC_CHECK_AND_RETURN_RET_LOGE(dstData->payload != NULL, SOFTBUS_MALLOC_ERR, DISC_BROADCAST, "malloc failed");
1938 
1939     if (memcpy_s(dstData->payload, dstData->payloadLen, srcData->payload, srcData->payloadLen) != EOK) {
1940         DISC_LOGE(DISC_BROADCAST, "memcpy_s error");
1941         SoftBusFree(dstData->payload);
1942         return SOFTBUS_MEM_ERR;
1943     }
1944     return SOFTBUS_OK;
1945 }
1946 
ReleaseSoftbusBroadcastData(SoftbusBroadcastData * softbusBcData)1947 static void ReleaseSoftbusBroadcastData(SoftbusBroadcastData *softbusBcData)
1948 {
1949     DISC_LOGD(DISC_BROADCAST, "enter release bc data");
1950     DISC_CHECK_AND_RETURN_LOGE(softbusBcData != NULL, DISC_BROADCAST, "softbusBcData is nullptr");
1951     SoftBusFree(softbusBcData->bcData.payload);
1952     SoftBusFree(softbusBcData->rspData.payload);
1953 }
1954 
BuildSoftbusBroadcastData(BroadcastProtocol protocol,const BroadcastPacket * packet,SoftbusBroadcastData * softbusBcData)1955 static int32_t BuildSoftbusBroadcastData(BroadcastProtocol protocol,
1956     const BroadcastPacket *packet, SoftbusBroadcastData *softbusBcData)
1957 {
1958     DISC_CHECK_AND_RETURN_RET_LOGE(packet != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "packet is nullptr");
1959     DISC_CHECK_AND_RETURN_RET_LOGE(softbusBcData != NULL, SOFTBUS_INVALID_PARAM,
1960         DISC_BROADCAST, "softbusBcData is nullptr");
1961 
1962     softbusBcData->isSupportFlag = packet->isSupportFlag;
1963     softbusBcData->flag = packet->flag;
1964 
1965     // 1. Build broadcast paylod.
1966     int32_t ret = BuildSoftbusBcPayload(&(packet->bcData), &(softbusBcData->bcData));
1967     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "BuildSoftbusBcPayload failed");
1968 
1969     // 2. Build response broadcast paylod.
1970     if (packet->rspData.payload != NULL) {
1971         ret = BuildSoftbusBcPayload(&(packet->rspData), &(softbusBcData->rspData));
1972         if (ret != SOFTBUS_OK) {
1973             DISC_LOGE(DISC_BROADCAST, "convert rspData failed");
1974             SoftBusFree(softbusBcData->bcData.payload);
1975             return ret;
1976         }
1977     } else {
1978         softbusBcData->rspData.payload = NULL;
1979         softbusBcData->rspData.payloadLen = 0;
1980     }
1981     return SOFTBUS_OK;
1982 }
1983 
MgrGetSysTime(void)1984 static int64_t MgrGetSysTime(void)
1985 {
1986     SoftBusSysTime absTime = {0};
1987     SoftBusGetTime(&absTime);
1988     int64_t time = absTime.sec * MGR_TIME_THOUSAND_MULTIPLIER * MGR_TIME_THOUSAND_MULTIPLIER + absTime.usec;
1989     return time;
1990 }
1991 
StartBroadcastingWaitSignal(int32_t bcId,SoftBusMutex * mutex,int64_t sec)1992 static void StartBroadcastingWaitSignal(int32_t bcId, SoftBusMutex *mutex, int64_t sec)
1993 {
1994     DISC_CHECK_AND_RETURN_LOGE(mutex != NULL, DISC_BROADCAST, "invalid param");
1995     DISC_CHECK_AND_RETURN_LOGE(CheckProtocolIsValid(g_bcManager[bcId].protocol), DISC_BROADCAST, "bad id");
1996     if (SoftBusCondWaitSec(sec, bcId, mutex) == SOFTBUS_OK) {
1997         return;
1998     }
1999     DISC_LOGW(DISC_BROADCAST, "wait signal failed, srvType=%{public}s, bcId=%{public}d, adapterId=%{public}d,"
2000         "call StopBroadcast", GetSrvType(g_bcManager[bcId].srvType), bcId, g_bcManager[bcId].adapterBcId);
2001     SoftBusMutexUnlock(mutex);
2002     int32_t ret = g_interface[g_bcManager[bcId].protocol]->StopBroadcasting(g_bcManager[bcId].adapterBcId);
2003     DISC_LOGW(DISC_BROADCAST, "StopBroadcasting ret=%{public}d", ret);
2004     DISC_CHECK_AND_RETURN_LOGE(SoftBusMutexLock(mutex) == SOFTBUS_OK, DISC_BROADCAST, "bcLock mutex error");
2005     ret = SoftBusCondWaitSec(sec, bcId, mutex);
2006     DISC_LOGW(DISC_BROADCAST, "wait signal ret=%{public}d", ret);
2007     g_bcManager[bcId].isAdvertising = false;
2008 }
2009 
DisableBroadcastingWaitSignal(int32_t bcId,SoftBusMutex * mutex)2010 static int32_t DisableBroadcastingWaitSignal(int32_t bcId, SoftBusMutex *mutex)
2011 {
2012     DISC_CHECK_AND_RETURN_RET_LOGE(mutex != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid param");
2013     DISC_CHECK_AND_RETURN_RET_LOGE(CheckProtocolIsValid(g_bcManager[bcId].protocol),
2014         SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "bad id");
2015     if (SoftbusPauseCondWaitSec(BC_WAIT_TIME_SEC, bcId, mutex, &g_bcManager[bcId].disableCond) == SOFTBUS_OK) {
2016         g_bcManager[bcId].isDisableCb = false;
2017         return SOFTBUS_OK;
2018     }
2019     DISC_LOGW(DISC_BROADCAST, "wait signal failed, srvType=%{public}s, bcId=%{public}d, adapterId=%{public}d,"
2020         "call enableBroadcast", GetSrvType(g_bcManager[bcId].srvType), bcId, g_bcManager[bcId].adapterBcId);
2021     SoftBusMutexUnlock(mutex);
2022     int32_t ret = g_interface[g_bcManager[bcId].protocol]->EnableBroadcasting(g_bcManager[bcId].adapterBcId);
2023     DISC_LOGW(DISC_BROADCAST, "EnableBroadcasting ret=%{public}d", ret);
2024     DISC_CHECK_AND_RETURN_RET_LOGE(SoftBusMutexLock(mutex) == SOFTBUS_OK,
2025         SOFTBUS_LOCK_ERR, DISC_BROADCAST, "bcLock mutex error");
2026     ret = SoftbusPauseCondWaitSec(BC_WAIT_TIME_SEC, bcId, mutex, &g_bcManager[bcId].enableCond);
2027     DISC_LOGW(DISC_BROADCAST, "wait signal ret=%{public}d", ret);
2028     g_bcManager[bcId].isDisabled = false;
2029 
2030     return SOFTBUS_BC_MGR_WAIT_COND_FAIL;
2031 }
2032 
SetBroadcastingParamWaitSignal(int32_t bcId,SoftBusMutex * mutex)2033 static int32_t SetBroadcastingParamWaitSignal(int32_t bcId, SoftBusMutex *mutex)
2034 {
2035     if (SoftbusPauseCondWaitSec(BC_WAIT_TIME_SEC, bcId, mutex, &g_bcManager[bcId].setParamCond) == SOFTBUS_OK) {
2036         return SOFTBUS_OK;
2037     }
2038     return SOFTBUS_BC_MGR_WAIT_COND_FAIL;
2039 }
2040 
CheckInterface(BroadcastProtocol protocol,bool isStart)2041 static int32_t CheckInterface(BroadcastProtocol protocol, bool isStart)
2042 {
2043     DISC_CHECK_AND_RETURN_RET_LOGE(CheckProtocolIsValid(protocol), SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "bad id");
2044     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[protocol] != NULL, SOFTBUS_BC_MGR_NO_FUNC_REGISTERED,
2045         DISC_BROADCAST, "interface is nullptr");
2046     if (isStart) {
2047         DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[protocol]->StartBroadcasting != NULL,
2048             SOFTBUS_BC_MGR_FUNC_NULL, DISC_BROADCAST, "function is nullptr");
2049     } else {
2050         DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[protocol]->StopBroadcasting != NULL,
2051             SOFTBUS_BC_MGR_FUNC_NULL, DISC_BROADCAST, "function is nullptr");
2052     }
2053     return SOFTBUS_OK;
2054 }
2055 
StartBroadcasting(int32_t bcId,const BroadcastParam * param,const BroadcastPacket * packet)2056 int32_t StartBroadcasting(int32_t bcId, const BroadcastParam *param, const BroadcastPacket *packet)
2057 {
2058     static uint32_t callCount = 0;
2059 
2060     int32_t ret = SoftBusMutexLock(&g_bcLock);
2061     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST,
2062         "mutex error, bcId=%{public}d", bcId);
2063     if (!CheckBcIdIsValid(bcId) || g_bcManager[bcId].bcCallback == NULL ||
2064         g_bcManager[bcId].bcCallback->OnStartBroadcastingCallback == NULL) {
2065         SoftBusMutexUnlock(&g_bcLock);
2066         DISC_LOGE(DISC_BROADCAST, "invalid bcId, bcId=%{public}d", bcId);
2067         return SOFTBUS_BC_MGR_INVALID_BC_ID;
2068     }
2069 
2070     BroadcastProtocol protocol = g_bcManager[bcId].protocol;
2071     if (CheckInterface(protocol, true) != SOFTBUS_OK || CheckBroadcastingParam(param, packet) != SOFTBUS_OK) {
2072         SoftBusMutexUnlock(&g_bcLock);
2073         DISC_LOGE(DISC_BROADCAST, "invalid param, bcId=%{public}d", bcId);
2074         return SOFTBUS_INVALID_PARAM;
2075     }
2076 
2077     if (g_bcManager[bcId].isAdvertising && !g_bcManager[bcId].isStarted) {
2078         DISC_LOGW(DISC_BROADCAST, "wait condition managerId=%{public}d", bcId);
2079         StartBroadcastingWaitSignal(bcId, &g_bcLock, BC_WAIT_TIME_SEC);
2080     }
2081 
2082     DumpBroadcastPacket(&(packet->bcData), &(packet->rspData));
2083     SoftbusBroadcastData softbusBcData = {0};
2084     ret = BuildSoftbusBroadcastData(protocol, packet, &softbusBcData);
2085     if (ret != SOFTBUS_OK) {
2086         DISC_LOGE(DISC_BROADCAST, "build SoftbusBroadcastData failed, bcId=%{public}d", bcId);
2087         SoftBusMutexUnlock(&g_bcLock);
2088         return ret;
2089     }
2090     SoftbusBroadcastParam adapterParam;
2091     ConvertBcParams(protocol, param, &adapterParam);
2092     DISC_LOGI(DISC_BROADCAST, "start service srvType=%{public}s, bcId=%{public}d, adapterId=%{public}d,"
2093         "callCount=%{public}u", GetSrvType(g_bcManager[bcId].srvType), bcId,
2094         g_bcManager[bcId].adapterBcId, callCount++);
2095     BroadcastCallback callback = *(g_bcManager[bcId].bcCallback);
2096     SoftBusMutexUnlock(&g_bcLock);
2097     ret = g_interface[protocol]->StartBroadcasting(g_bcManager[bcId].adapterBcId, &adapterParam, &softbusBcData);
2098     g_bcManager[bcId].time = MgrGetSysTime();
2099     g_bcManager[bcId].minInterval = adapterParam.minInterval;
2100     g_bcManager[bcId].maxInterval = adapterParam.maxInterval;
2101     int32_t advHandle = 0;
2102     (void)BroadcastGetBroadcastHandle(bcId, &advHandle);
2103     g_bcManager[bcId].advHandle = advHandle;
2104     if (g_bcCurrentNum >= MAX_BLE_ADV_NUM) {
2105         g_bcOverMaxNum++;
2106     }
2107     if (ret != SOFTBUS_OK) {
2108         callback.OnStartBroadcastingCallback(bcId, (int32_t)SOFTBUS_BC_STATUS_FAIL);
2109         DISC_LOGE(DISC_BROADCAST, "call from adapter failed");
2110         ReportCurrentBroadcast(false);
2111         ReleaseSoftbusBroadcastData(&softbusBcData);
2112         return ret;
2113     }
2114 
2115     ret = SoftBusMutexLock(&g_bcLock);
2116     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "lock failed");
2117     g_bcManager[bcId].isStarted = true;
2118     g_bcManager[bcId].isDisabled = false;
2119     g_bcManager[bcId].isDisableCb = false;
2120     SoftBusMutexUnlock(&g_bcLock);
2121     ReleaseSoftbusBroadcastData(&softbusBcData);
2122     return SOFTBUS_OK;
2123 }
2124 
UpdateBroadcasting(int32_t bcId,const BroadcastParam * param,const BroadcastPacket * packet)2125 int32_t UpdateBroadcasting(int32_t bcId, const BroadcastParam *param, const BroadcastPacket *packet)
2126 {
2127     DISC_LOGI(DISC_BROADCAST, "enter update bc");
2128     DISC_CHECK_AND_RETURN_RET_LOGE(param != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invald param");
2129     DISC_CHECK_AND_RETURN_RET_LOGE(packet != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invald packet");
2130 
2131     int32_t ret = SetBroadcastingData(bcId, packet);
2132     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "set data failed");
2133 
2134     ret = SetBroadcastingParam(bcId, param);
2135     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "set param failed");
2136 
2137     return SOFTBUS_OK;
2138 }
2139 
SetBroadcastingData(int32_t bcId,const BroadcastPacket * packet)2140 int32_t SetBroadcastingData(int32_t bcId, const BroadcastPacket *packet)
2141 {
2142     static uint32_t callCount = 0;
2143     DISC_LOGI(DISC_BROADCAST, "enter set bc data, bcId=%{public}d, callCount=%{public}u", bcId, callCount++);
2144     DISC_CHECK_AND_RETURN_RET_LOGE(packet != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid param packet");
2145 
2146     int32_t ret = SoftBusMutexLock(&g_bcLock);
2147     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
2148 
2149     if (!CheckBcIdIsValid(bcId) || g_bcManager[bcId].bcCallback == NULL ||
2150         g_bcManager[bcId].bcCallback->OnSetBroadcastingCallback == NULL) {
2151         SoftBusMutexUnlock(&g_bcLock);
2152         return SOFTBUS_BC_MGR_INVALID_BC_ID;
2153     }
2154     BroadcastProtocol protocol = g_bcManager[bcId].protocol;
2155     if (!CheckProtocolIsValid(protocol) || g_interface[protocol] == NULL ||
2156         g_interface[protocol]->SetBroadcastingData == NULL) {
2157         DISC_LOGE(DISC_BROADCAST, "protocol=%{public}d is not registered", protocol);
2158         SoftBusMutexUnlock(&g_bcLock);
2159         return SOFTBUS_INVALID_PARAM;
2160     }
2161 
2162     if (!g_bcManager[bcId].isAdvertising) {
2163         DISC_LOGW(DISC_BROADCAST, "bcId=%{public}d is not advertising", bcId);
2164         SoftBusMutexUnlock(&g_bcLock);
2165         return SOFTBUS_BC_MGR_NOT_BROADCASTING;
2166     }
2167     DISC_LOGI(DISC_BROADCAST, "replace BroadcastPacket srvType=%{public}s, bcId=%{public}d, adapterId=%{public}d,"
2168         "callCount=%{public}u", GetSrvType(g_bcManager[bcId].srvType), bcId, g_bcManager[bcId].adapterBcId,
2169         callCount++);
2170     SoftbusBroadcastData softbusBcData = {0};
2171     ret = BuildSoftbusBroadcastData(protocol, packet, &softbusBcData);
2172     if (ret != SOFTBUS_OK) {
2173         DISC_LOGE(DISC_BROADCAST, "build SoftbusBroadcastData failed");
2174         SoftBusMutexUnlock(&g_bcLock);
2175         return ret;
2176     }
2177     BroadcastCallback callback = *(g_bcManager[bcId].bcCallback);
2178     SoftBusMutexUnlock(&g_bcLock);
2179     ret = g_interface[protocol]->SetBroadcastingData(g_bcManager[bcId].adapterBcId, &softbusBcData);
2180     if (ret != SOFTBUS_OK) {
2181         callback.OnSetBroadcastingCallback(bcId, (int32_t)SOFTBUS_BC_STATUS_FAIL);
2182         DISC_LOGE(DISC_BROADCAST, "call from adapter failed");
2183         ReleaseSoftbusBroadcastData(&softbusBcData);
2184         return ret;
2185     }
2186 
2187     ReleaseSoftbusBroadcastData(&softbusBcData);
2188     return SOFTBUS_OK;
2189 }
2190 
DisableBroadcasting(int32_t bcId)2191 int32_t DisableBroadcasting(int32_t bcId)
2192 {
2193     int32_t ret = SoftBusMutexLock(&g_bcLock);
2194     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
2195 
2196     if (!CheckBcIdIsValid(bcId)) {
2197         SoftBusMutexUnlock(&g_bcLock);
2198         return SOFTBUS_BC_MGR_INVALID_BC_ID;
2199     }
2200     BroadcastProtocol protocol = g_bcManager[bcId].protocol;
2201 
2202     if (!CheckProtocolIsValid(protocol) || g_interface[protocol] == NULL ||
2203         g_interface[protocol]->DisableBroadcasting == NULL) {
2204         DISC_LOGE(DISC_BROADCAST, "protocol=%{public}d is not registered", protocol);
2205         SoftBusMutexUnlock(&g_bcLock);
2206         return SOFTBUS_INVALID_PARAM;
2207     }
2208     if (!g_bcManager[bcId].isAdvertising || g_bcManager[bcId].isDisabled) {
2209         DISC_LOGW(DISC_BROADCAST, "bcId=%{public}d is already disabled", bcId);
2210         SoftBusMutexUnlock(&g_bcLock);
2211         return SOFTBUS_BC_MGR_NOT_BROADCASTING;
2212     }
2213 
2214     SoftBusMutexUnlock(&g_bcLock);
2215     ret = g_interface[protocol]->DisableBroadcasting(g_bcManager[bcId].adapterBcId);
2216     if (ret != SOFTBUS_OK) {
2217         DISC_LOGE(DISC_BROADCAST, "call from adapter failed");
2218         return ret;
2219     }
2220     ret = SoftBusMutexLock(&g_bcLock);
2221     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
2222     g_bcManager[bcId].isAdvertising = false;
2223     g_bcManager[bcId].isDisabled = true;
2224     SoftBusMutexUnlock(&g_bcLock);
2225 
2226     return SOFTBUS_OK;
2227 }
2228 
EnableBroadcasting(int32_t bcId)2229 int32_t EnableBroadcasting(int32_t bcId)
2230 {
2231     int32_t ret = SoftBusMutexLock(&g_bcLock);
2232     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
2233 
2234     if (!CheckBcIdIsValid(bcId)) {
2235         SoftBusMutexUnlock(&g_bcLock);
2236         return SOFTBUS_BC_MGR_INVALID_BC_ID;
2237     }
2238     BroadcastProtocol protocol = g_bcManager[bcId].protocol;
2239 
2240     if (!CheckProtocolIsValid(protocol) || g_interface[protocol] == NULL ||
2241         g_interface[protocol]->EnableBroadcasting == NULL) {
2242         DISC_LOGE(DISC_BROADCAST, "protocol=%{public}d is not registered", protocol);
2243         SoftBusMutexUnlock(&g_bcLock);
2244         return SOFTBUS_INVALID_PARAM;
2245     }
2246 
2247     if (!g_bcManager[bcId].isAdvertising && !g_bcManager[bcId].isDisabled) {
2248         DISC_LOGW(DISC_BROADCAST, "bcId=%{public}d is already enabled", bcId);
2249         SoftBusMutexUnlock(&g_bcLock);
2250         return SOFTBUS_BC_MGR_NOT_BROADCASTING;
2251     }
2252 
2253     SoftBusMutexUnlock(&g_bcLock);
2254     ret = g_interface[protocol]->EnableBroadcasting(g_bcManager[bcId].adapterBcId);
2255     if (ret != SOFTBUS_OK) {
2256         DISC_LOGE(DISC_BROADCAST, "call from adapter failed");
2257         return ret;
2258     }
2259     ret = SoftBusMutexLock(&g_bcLock);
2260     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
2261     g_bcManager[bcId].isAdvertising = true;
2262     g_bcManager[bcId].isDisabled = false;
2263     SoftBusMutexUnlock(&g_bcLock);
2264     return SOFTBUS_OK;
2265 }
2266 
PerformSetBroadcastingParam(int32_t bcId,SoftbusBroadcastParam * softbusBcParam)2267 int32_t PerformSetBroadcastingParam(int32_t bcId, SoftbusBroadcastParam *softbusBcParam)
2268 {
2269     int32_t ret = DisableBroadcasting(bcId);
2270     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST,
2271         "call from adapter failed during disabling");
2272 
2273     ret = SoftBusMutexLock(&g_bcLock);
2274     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "lock failed");
2275     DISC_LOGW(DISC_BROADCAST, "wait pausecondition managerId=%{public}d, isDisableCb=%{public}d",
2276         bcId, g_bcManager[bcId].isDisableCb);
2277     if (!g_bcManager[bcId].isDisableCb) {
2278         ret = DisableBroadcastingWaitSignal(bcId, &g_bcLock);
2279         SoftBusMutexUnlock(&g_bcLock);
2280         DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST,
2281             "wait pausecondition fail managerId=%{public}d", bcId);
2282     } else {
2283         SoftBusMutexUnlock(&g_bcLock);
2284     }
2285 
2286     if (g_bcManager[bcId].isDisabled) {
2287         ret = g_interface[g_bcManager[bcId].protocol]->SetBroadcastingParam(g_bcManager[bcId].adapterBcId,
2288             softbusBcParam);
2289         if (ret != SOFTBUS_OK) {
2290             DISC_LOGE(DISC_BROADCAST, "call from adapter failed during setting param");
2291             return ret;
2292         }
2293         ret = SoftBusMutexLock(&g_bcLock);
2294         DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "lock failed");
2295         ret = SetBroadcastingParamWaitSignal(bcId, &g_bcLock);
2296         if (ret != SOFTBUS_OK) {
2297             DISC_LOGE(DISC_BROADCAST, "wait set broadcasting param fail managerId=%{public}d", bcId);
2298         }
2299         SoftBusMutexUnlock(&g_bcLock);
2300     }
2301 
2302     ret = EnableBroadcasting(bcId);
2303     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST,
2304         "call from adapter failed during enabling");
2305 
2306     return SOFTBUS_OK;
2307 }
2308 
SetBroadcastingParam(int32_t bcId,const BroadcastParam * param)2309 int32_t SetBroadcastingParam(int32_t bcId, const BroadcastParam *param)
2310 {
2311     static uint32_t callCount = 0;
2312     DISC_LOGI(DISC_BROADCAST, "enter set bc Param, bcId=%{public}d, callCount=%{public}u", bcId, callCount++);
2313     DISC_CHECK_AND_RETURN_RET_LOGE(param != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid param");
2314 
2315     int32_t ret = SoftBusMutexLock(&g_bcLock);
2316     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
2317 
2318     if (!CheckBcIdIsValid(bcId)) {
2319         SoftBusMutexUnlock(&g_bcLock);
2320         return SOFTBUS_BC_MGR_INVALID_BC_ID;
2321     }
2322     BroadcastProtocol protocol = g_bcManager[bcId].protocol;
2323 
2324     if (!CheckProtocolIsValid(protocol) || g_interface[protocol] == NULL ||
2325         g_interface[protocol]->SetBroadcastingParam == NULL) {
2326         DISC_LOGE(DISC_BROADCAST, "protocol=%{public}d is not registered", protocol);
2327         SoftBusMutexUnlock(&g_bcLock);
2328         return SOFTBUS_INVALID_PARAM;
2329     }
2330 
2331     if (!g_bcManager[bcId].isAdvertising) {
2332         DISC_LOGW(DISC_BROADCAST, "bcId=%{public}d is not advertising", bcId);
2333         SoftBusMutexUnlock(&g_bcLock);
2334         return SOFTBUS_BC_MGR_NOT_BROADCASTING;
2335     }
2336     DISC_LOGI(DISC_BROADCAST, "replace BroadcastParam srvType=%{public}s, bcId=%{public}d, adapterId=%{public}d,"
2337         "callCount=%{public}u", GetSrvType(g_bcManager[bcId].srvType), bcId, g_bcManager[bcId].adapterBcId,
2338         callCount++);
2339     SoftbusBroadcastParam softbusBcParam = {};
2340     ConvertBcParams(protocol, param, &softbusBcParam);
2341     g_bcManager[bcId].isDisableCb = false;
2342     SoftBusMutexUnlock(&g_bcLock);
2343 
2344     return PerformSetBroadcastingParam(bcId, &softbusBcParam);
2345 }
2346 
StopBroadcasting(int32_t bcId)2347 int32_t StopBroadcasting(int32_t bcId)
2348 {
2349     int32_t ret = SoftBusMutexLock(&g_bcLock);
2350     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR,
2351         DISC_BROADCAST, "mutex error, bcId=%{public}d", bcId);
2352     if (!CheckBcIdIsValid(bcId)) {
2353         SoftBusMutexUnlock(&g_bcLock);
2354         return SOFTBUS_BC_MGR_INVALID_BC_ID;
2355     }
2356     BroadcastProtocol protocol = g_bcManager[bcId].protocol;
2357 
2358     if (!CheckProtocolIsValid(protocol) || CheckInterface(protocol, false) != SOFTBUS_OK) {
2359         DISC_LOGE(DISC_BROADCAST, "interface check failed, bcId=%{public}d", bcId);
2360         SoftBusMutexUnlock(&g_bcLock);
2361         return SOFTBUS_INVALID_PARAM;
2362     }
2363 
2364     int64_t time = MgrGetSysTime();
2365     if (time - g_bcManager[bcId].time < BC_WAIT_TIME_MICROSEC) {
2366         int64_t diffTime = g_bcManager[bcId].time + BC_WAIT_TIME_MICROSEC - time;
2367         DISC_LOGW(DISC_BROADCAST, "wait %{public}d us", (int32_t)diffTime);
2368         usleep(diffTime);
2369     }
2370 
2371     if (!g_bcManager[bcId].isStarted) {
2372         DISC_LOGW(DISC_BROADCAST, "bcId is not start, bcId=%{public}d", bcId);
2373         SoftBusMutexUnlock(&g_bcLock);
2374         return SOFTBUS_OK;
2375     }
2376     if (g_bcManager[bcId].bcCallback == NULL || g_bcManager[bcId].bcCallback->OnStopBroadcastingCallback == NULL) {
2377         DISC_LOGE(DISC_BROADCAST, "bc callback is null, bcId=%{public}d", bcId);
2378         SoftBusMutexUnlock(&g_bcLock);
2379         return SOFTBUS_BC_MGR_INVALID_BC_ID;
2380     }
2381 
2382     DISC_LOGI(DISC_BROADCAST, "stop srvType=%{public}s, bcId=%{public}d, adapterId=%{public}d",
2383         GetSrvType(g_bcManager[bcId].srvType), bcId, g_bcManager[bcId].adapterBcId);
2384     BroadcastCallback callback = *(g_bcManager[bcId].bcCallback);
2385     SoftBusMutexUnlock(&g_bcLock);
2386     ret = g_interface[protocol]->StopBroadcasting(g_bcManager[bcId].adapterBcId);
2387     if (ret != SOFTBUS_OK) {
2388         callback.OnStopBroadcastingCallback(bcId, (int32_t)SOFTBUS_BC_STATUS_FAIL);
2389         DISC_LOGE(DISC_BROADCAST, "call from adapter failed");
2390         return ret;
2391     }
2392     DISC_CHECK_AND_RETURN_RET_LOGE(SoftBusMutexLock(&g_bcLock) == SOFTBUS_OK,
2393         SOFTBUS_LOCK_ERR, DISC_BROADCAST, "lock failed");
2394     g_bcManager[bcId].isStarted = false;
2395     SoftBusMutexUnlock(&g_bcLock);
2396     callback.OnStopBroadcastingCallback(bcId, (int32_t)SOFTBUS_BC_STATUS_SUCCESS);
2397     return SOFTBUS_OK;
2398 }
2399 
GetScanFreq(uint16_t scanInterval,uint16_t scanWindow)2400 static int32_t GetScanFreq(uint16_t scanInterval, uint16_t scanWindow)
2401 {
2402     if (scanInterval == SOFTBUS_BC_SCAN_INTERVAL_P2 && scanWindow == SOFTBUS_BC_SCAN_WINDOW_P2) {
2403         return SCAN_FREQ_P2_60_3000;
2404     }
2405     if (scanInterval == SOFTBUS_BC_SCAN_INTERVAL_P2_FAST && scanWindow == SOFTBUS_BC_SCAN_WINDOW_P2_FAST) {
2406         return SCAN_FREQ_P2_30_1500;
2407     }
2408     if (scanInterval == SOFTBUS_BC_SCAN_INTERVAL_P10 && scanWindow == SOFTBUS_BC_SCAN_WINDOW_P10) {
2409         return SCAN_FREQ_P10_30_300;
2410     }
2411     if (scanInterval == SOFTBUS_BC_SCAN_INTERVAL_P25 && scanWindow == SOFTBUS_BC_SCAN_WINDOW_P25) {
2412         return SCAN_FREQ_P25_60_240;
2413     }
2414     if (scanInterval == SOFTBUS_BC_SCAN_INTERVAL_P50 && scanWindow == SOFTBUS_BC_SCAN_WINDOW_P50) {
2415         return SCAN_FREQ_P50_30_60;
2416     }
2417     if (scanInterval == SOFTBUS_BC_SCAN_INTERVAL_P75 && scanWindow == SOFTBUS_BC_SCAN_WINDOW_P75) {
2418         return SCAN_FREQ_P75_30_40;
2419     }
2420     if (scanInterval == SOFTBUS_BC_SCAN_INTERVAL_P100 && scanWindow == SOFTBUS_BC_SCAN_WINDOW_P100) {
2421         return SCAN_FREQ_P100_1000_1000;
2422     }
2423     if (scanInterval == SOFTBUS_BC_SCAN_INTERVAL_P10_LONG_RANGE &&
2424         scanWindow == SOFTBUS_BC_SCAN_WINDOW_P10_LONG_RANGE) {
2425         return SCAN_FREQ_P10_400_40_LONG_RANGE;
2426     }
2427     if (scanInterval == SOFTBUS_BC_SCAN_INTERVAL_P100_LONG_RANGE &&
2428         scanWindow == SOFTBUS_BC_SCAN_WINDOW_P100_LONG_RANGE) {
2429         return SCAN_FREQ_P100_30_30_LONG_RANGE;
2430     }
2431     return SCAN_FREQ_LOW_POWER;
2432 }
2433 
PerformNormalStartScan(BroadcastProtocol protocol,int32_t listenerId,SoftBusBcScanParams * adapterParam,uint32_t * callCount)2434 static int32_t PerformNormalStartScan(BroadcastProtocol protocol,
2435     int32_t listenerId, SoftBusBcScanParams *adapterParam, uint32_t *callCount)
2436 {
2437     DISC_CHECK_AND_RETURN_RET_LOGE(adapterParam != NULL, SOFTBUS_INVALID_PARAM,
2438         DISC_BROADCAST, "adapterParam is nullptr");
2439     DISC_CHECK_AND_RETURN_RET_LOGE(callCount != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "callCount is nullptr");
2440 
2441     int32_t ret = 0;
2442     int32_t filterSize = 0;
2443     SoftBusBcScanFilter *adapterFilter = NULL;
2444 
2445     ret = GetBcScanFilters(listenerId, &adapterFilter, &filterSize);
2446     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK && filterSize > 0, SOFTBUS_BC_MGR_START_SCAN_NO_FILTER,
2447         DISC_BROADCAST, "no filter");
2448     DumpBcScanFilter(adapterFilter, filterSize);
2449 
2450     DISC_LOGI(DISC_BROADCAST, "start service srvType=%{public}s, listenerId=%{public}d, adapterId=%{public}d, "
2451         "interval=%{public}hu, window=%{public}hu, callCount=%{public}u",
2452         GetSrvType(g_scanManager[listenerId].srvType), listenerId,
2453         g_scanManager[listenerId].adapterScanId, adapterParam->scanInterval,
2454         adapterParam->scanWindow, (*callCount)++);
2455     ret = g_interface[protocol]->StartScan(g_scanManager[listenerId].adapterScanId, adapterParam,
2456         adapterFilter, filterSize);
2457     ReleaseSoftBusBcScanFilter(adapterFilter, filterSize);
2458     if (ret != SOFTBUS_OK) {
2459         g_scanManager[listenerId].scanCallback->OnStartScanCallback(listenerId, (int32_t)SOFTBUS_BC_STATUS_FAIL);
2460         DISC_LOGE(DISC_BROADCAST, "call from adapter failed");
2461         return ret;
2462     }
2463     g_scanManager[listenerId].isFliterChanged = false;
2464     return SOFTBUS_OK;
2465 }
2466 
CheckNotScaning(int32_t listenerId,SoftBusBcScanParams * adapterParam)2467 static int32_t CheckNotScaning(int32_t listenerId, SoftBusBcScanParams *adapterParam)
2468 {
2469     DISC_CHECK_AND_RETURN_RET_LOGE(adapterParam != NULL, SOFTBUS_INVALID_PARAM,
2470         DISC_BROADCAST, "adapterParam is nullptr");
2471 
2472     SoftBusBcScanFilter *adapterFilter = NULL;
2473     int32_t filterSize = 0;
2474     int32_t ret = 0;
2475     if (g_scanManager[listenerId].addSize > 0) {
2476         GetAddFiltersByIndex(listenerId, &adapterFilter);
2477         ret = g_interface[g_scanManager[listenerId].protocol]->SetScanParams(g_scanManager[listenerId].adapterScanId,
2478             adapterParam, adapterFilter, filterSize, SOFTBUS_SCAN_FILTER_CMD_ADD);
2479         ReleaseSoftBusBcScanFilter(adapterFilter, filterSize);
2480         adapterFilter = NULL;
2481     }
2482     if (g_scanManager[listenerId].deleteSize > 0) {
2483         DeleteFilterByIndex(listenerId, &adapterFilter, adapterParam, filterSize);
2484         ReleaseSoftBusBcScanFilter(adapterFilter, filterSize);
2485         adapterFilter = NULL;
2486     }
2487     return ret;
2488 }
2489 
ProcessFliterChanged(int32_t listenerId,SoftBusBcScanParams * adapterParam,SoftBusBcScanFilter * adapterFilter,int32_t filterSize)2490 static int32_t ProcessFliterChanged(int32_t listenerId, SoftBusBcScanParams *adapterParam,
2491     SoftBusBcScanFilter *adapterFilter, int32_t filterSize)
2492 {
2493     int32_t ret = -1;
2494     ScanManager *scanManager = &g_scanManager[listenerId];
2495     BroadcastProtocol protocol = scanManager->protocol;
2496     if (scanManager->isScanning) {
2497         DISC_LOGI(DISC_BROADCAST, "listenerId=%{public}d, srvType=%{public}s", listenerId,
2498             GetSrvType(scanManager->srvType));
2499         if (scanManager->addSize == 0 && scanManager->deleteSize == 0) {
2500             DISC_LOGI(DISC_BROADCAST, "same filter and scanning, just change params. srvType=%{public}s,"
2501                 "listenerId=%{public}d, adapterId=%{public}d, interval=%{public}hu, window=%{public}hu",
2502                 GetSrvType(scanManager->srvType), listenerId,
2503                 scanManager->adapterScanId, adapterParam->scanInterval, adapterParam->scanWindow);
2504             ret = g_interface[protocol]->SetScanParams(scanManager->adapterScanId, adapterParam,
2505                 NULL, 0, SOFTBUS_SCAN_FILTER_CMD_NONE);
2506             DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_INVALID_PARAM,
2507                 DISC_BROADCAST, "call from adapter failed");
2508             return ret;
2509         }
2510         if (scanManager->addSize == scanManager->deleteSize) {
2511             DISC_LOGI(DISC_BROADCAST, "modify filter");
2512             GetModifyFiltersByIndex(listenerId, &adapterFilter);
2513             ret = g_interface[protocol]->SetScanParams(scanManager->adapterScanId, adapterParam,
2514                 adapterFilter, filterSize, SOFTBUS_SCAN_FILTER_CMD_MODIFY);
2515             ReleaseSoftBusBcScanFilter(adapterFilter, filterSize);
2516             adapterFilter = NULL;
2517         } else {
2518             ret = CheckNotScaning(listenerId, adapterParam);
2519         }
2520         DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK,
2521             ret, DISC_BROADCAST, "call from adapter failed, ret=%{public}d", ret);
2522         DISC_LOGI(DISC_BROADCAST, "modify service srvType=%{public}s, listenerId=%{public}d,"
2523             "adapterId=%{public}d, interval=%{public}hu, window=%{public}hu",
2524             GetSrvType(scanManager->srvType), listenerId, scanManager->adapterScanId,
2525             adapterParam->scanInterval, adapterParam->scanWindow);
2526     } else {
2527         DISC_LOGI(DISC_BROADCAST, "channel is scanning, add filter");
2528         ret = GetScanFiltersForOneListener(listenerId, &adapterFilter, &filterSize);
2529         DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "get bc scan filters failed");
2530         ret = g_interface[protocol]->SetScanParams(scanManager->adapterScanId, adapterParam,
2531             adapterFilter, filterSize, SOFTBUS_SCAN_FILTER_CMD_ADD);
2532         ReleaseSoftBusBcScanFilter(adapterFilter, filterSize);
2533         DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret,
2534             DISC_BROADCAST, "call from adapter failed, ret=%{public}d", ret);
2535     }
2536     return ret;
2537 }
2538 
CheckChannelScan(BroadcastProtocol protocol,int32_t listenerId,SoftBusBcScanParams * adapterParam)2539 static int32_t CheckChannelScan(BroadcastProtocol protocol, int32_t listenerId, SoftBusBcScanParams *adapterParam)
2540 {
2541     DISC_CHECK_AND_RETURN_RET_LOGE(adapterParam != NULL, SOFTBUS_INVALID_PARAM,
2542         DISC_BROADCAST, "adapterParam is nullptr");
2543 
2544     SoftBusBcScanFilter *adapterFilter = NULL;
2545     int32_t filterSize = 0;
2546     int32_t ret = 0;
2547     if (g_scanManager[listenerId].isFliterChanged) {
2548         return ProcessFliterChanged(listenerId, adapterParam, adapterFilter, filterSize);
2549     }
2550     ret = g_interface[protocol]->SetScanParams(g_scanManager[listenerId].adapterScanId, adapterParam,
2551         NULL, 0, SOFTBUS_SCAN_FILTER_CMD_NONE);
2552     return ret;
2553 }
2554 
StartScanSub(int32_t listenerId,BroadcastProtocol protocol)2555 static int32_t StartScanSub(int32_t listenerId, BroadcastProtocol protocol)
2556 {
2557     DISC_CHECK_AND_RETURN_RET_LOGE(g_scanManager[listenerId].filterSize != 0, SOFTBUS_INVALID_PARAM,
2558         DISC_BROADCAST, "filter size is 0, need to set filter");
2559     static uint32_t callCount = 0;
2560     SoftBusBcScanParams adapterParam;
2561     BuildSoftBusBcScanParams(&g_scanManager[listenerId].param, &adapterParam);
2562     CheckScanFreq(listenerId, &adapterParam);
2563 
2564     bool isChannelScanning = false;
2565     int32_t adapterScanId = g_scanManager[listenerId].adapterScanId;
2566 
2567     for (int32_t managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
2568         if (g_scanManager[managerId].adapterScanId != adapterScanId ||
2569             protocol != g_scanManager[managerId].protocol) {
2570             continue;
2571         }
2572         if (g_scanManager[managerId].isScanning) {
2573             isChannelScanning = true;
2574             break;
2575         }
2576     }
2577     if (!isChannelScanning) {
2578         goto NORMAL_START_SCAN;
2579     }
2580     if (protocol == BROADCAST_PROTOCOL_SLE) {
2581         DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[protocol]->StopScan != NULL,
2582             SOFTBUS_BC_MGR_FUNC_NULL, DISC_BROADCAST, "function is nullptr");
2583         int32_t ret = g_interface[protocol]->StopScan(g_scanManager[listenerId].adapterScanId);
2584         DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST,
2585             "stop sle scan falied, err=%{public}d", ret);
2586         return PerformNormalStartScan(protocol, listenerId, &adapterParam, &callCount);
2587     }
2588     return CheckChannelScan(protocol, listenerId, &adapterParam);
2589 
2590 NORMAL_START_SCAN:
2591     DISC_LOGI(DISC_BROADCAST, "not scanning just start scan. listenerId=%{public}d", listenerId);
2592     // channel have stop. normal run scan
2593     return PerformNormalStartScan(protocol, listenerId, &adapterParam, &callCount);
2594 }
2595 
GetFilterIndex(uint8_t * index)2596 static int32_t GetFilterIndex(uint8_t *index)
2597 {
2598     DISC_CHECK_AND_RETURN_RET_LOGE(index != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "index is nullptr");
2599 
2600     for (int i = 1; i <= MAX_FILTER_SIZE; i++) {
2601         if (!g_firstSetIndex[i]) {
2602             g_firstSetIndex[i] = true;
2603             *index = i;
2604             return SOFTBUS_OK;
2605         }
2606     }
2607     DISC_LOGI(DISC_BROADCAST, "no index available");
2608     return SOFTBUS_INVALID_PARAM;
2609 }
2610 
StartScan(int32_t listenerId,const BcScanParams * param)2611 int32_t StartScan(int32_t listenerId, const BcScanParams *param)
2612 {
2613     static uint32_t callCount = 0;
2614     DISC_CHECK_AND_RETURN_RET_LOGE(param != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid param!");
2615 
2616     int32_t ret = SoftBusMutexLock(&g_scanLock);
2617     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
2618 
2619     if (!CheckScanIdIsValid(listenerId)) {
2620         DISC_LOGE(DISC_BROADCAST, "invalid param listenerId, listenerId=%{public}d", listenerId);
2621         SoftBusMutexUnlock(&g_scanLock);
2622         return SOFTBUS_BC_MGR_INVALID_LISN_ID;
2623     }
2624     DISC_LOGI(DISC_BROADCAST, "enter start scan, listenerId=%{public}d, callCount=%{public}u, srvType=%{public}s",
2625         listenerId, callCount++, GetSrvType(g_scanManager[listenerId].srvType));
2626 
2627     BroadcastProtocol protocol = g_scanManager[listenerId].protocol;
2628 
2629     if (!CheckProtocolIsValid(protocol) || g_interface[protocol] == NULL || g_interface[protocol]->StartScan == NULL) {
2630         DISC_LOGE(DISC_BROADCAST, "interface check failed, listenerId=%{public}d", listenerId);
2631         SoftBusMutexUnlock(&g_scanLock);
2632         return SOFTBUS_INVALID_PARAM;
2633     }
2634 
2635     g_scanManager[listenerId].param = *param;
2636     g_scanManager[listenerId].freq = GetScanFreq(param->scanInterval, param->scanWindow);
2637 
2638     if (!g_scanManager[listenerId].isScanning) {
2639         for (int i = 0; i < g_scanManager[listenerId].filterSize; i++) {
2640             if (g_scanManager[listenerId].filter[i].filterIndex != 0) {
2641                 continue;
2642             }
2643             ret = GetFilterIndex(&g_scanManager[listenerId].filter[i].filterIndex);
2644             if (ret != SOFTBUS_OK) {
2645                 DISC_LOGE(DISC_BROADCAST, "no available index");
2646                 SoftBusMutexUnlock(&g_scanLock);
2647                 return ret;
2648             }
2649             DISC_LOGI(DISC_BROADCAST, "add filter filterIndex = %{public}d",
2650                 g_scanManager[listenerId].filter[i].filterIndex);
2651         }
2652     }
2653 
2654     ret = StartScanSub(listenerId, protocol);
2655     if (ret != SOFTBUS_OK) {
2656         SoftBusMutexUnlock(&g_scanLock);
2657         return ret;
2658     }
2659 
2660     ReleaseScanIdx(listenerId);
2661     g_scanManager[listenerId].isScanning = true;
2662     g_scanManager[listenerId].isFliterChanged = false;
2663     g_scanManager[listenerId].scanCallback->OnStartScanCallback(listenerId, (int32_t)SOFTBUS_BC_STATUS_SUCCESS);
2664 
2665     SoftBusMutexUnlock(&g_scanLock);
2666     return SOFTBUS_OK;
2667 }
2668 
StopScan(int32_t listenerId)2669 int32_t StopScan(int32_t listenerId)
2670 {
2671     static uint32_t callCount = 0;
2672     DISC_LOGI(DISC_BROADCAST, "enter stop scan, listenerId=%{public}d, callCount=%{public}u", listenerId, callCount++);
2673     int32_t ret = SoftBusMutexLock(&g_scanLock);
2674     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
2675     if (!CheckScanIdIsValid(listenerId)) {
2676         DISC_LOGE(DISC_BROADCAST, "invalid param listenerId, listenerId=%{public}d", listenerId);
2677         SoftBusMutexUnlock(&g_scanLock);
2678         return SOFTBUS_BC_MGR_INVALID_LISN_ID;
2679     }
2680 
2681     BroadcastProtocol protocol = g_scanManager[listenerId].protocol;
2682     if (!CheckProtocolIsValid(protocol) || g_interface[protocol] == NULL || g_interface[protocol]->StopScan == NULL) {
2683         DISC_LOGE(DISC_BROADCAST, "not found or not register protocol=%{public}d", protocol);
2684         SoftBusMutexUnlock(&g_scanLock);
2685         return SOFTBUS_INVALID_PARAM;
2686     }
2687     DISC_LOGI(DISC_BROADCAST, "srvType=%{public}s, listenerId=%{public}d, adapterId=%{public}d, callCount=%{public}u",
2688         GetSrvType(g_scanManager[listenerId].srvType), listenerId, g_scanManager[listenerId].adapterScanId, callCount);
2689     if (!g_scanManager[listenerId].isScanning) {
2690         DISC_LOGI(DISC_BROADCAST, "listenerId is not scanning. listenerId=%{public}d", listenerId);
2691         SoftBusMutexUnlock(&g_scanLock);
2692         return SOFTBUS_OK;
2693     }
2694 
2695     ret = CheckAndStopScan(protocol, listenerId);
2696     if (ret != SOFTBUS_OK) {
2697         SoftBusMutexUnlock(&g_scanLock);
2698         return ret;
2699     }
2700 
2701     for (int i = 0; i < g_scanManager[listenerId].filterSize; ++i) {
2702         g_firstSetIndex[g_scanManager[listenerId].filter[i].filterIndex] = false;
2703         g_scanManager[listenerId].filter[i].filterIndex = 0;
2704     }
2705     ReleaseScanIdx(listenerId);
2706     g_scanManager[listenerId].isFliterChanged = true;
2707     g_scanManager[listenerId].isScanning = false;
2708     g_scanManager[listenerId].scanCallback->OnStopScanCallback(listenerId, (int32_t)SOFTBUS_BC_STATUS_SUCCESS);
2709 
2710     SoftBusMutexUnlock(&g_scanLock);
2711     return SOFTBUS_OK;
2712 }
2713 
CompareSameFilter(BcScanFilter * srcFilter,BcScanFilter * dstFilter)2714 bool CompareSameFilter(BcScanFilter *srcFilter, BcScanFilter *dstFilter)
2715 {
2716     DISC_CHECK_AND_RETURN_RET_LOGE(srcFilter != NULL, false, DISC_BROADCAST, "left filter is null");
2717     DISC_CHECK_AND_RETURN_RET_LOGE(dstFilter != NULL, false, DISC_BROADCAST, "right filter is null");
2718 
2719     return srcFilter->advIndReport == dstFilter->advIndReport &&
2720         srcFilter->serviceUuid == dstFilter->serviceUuid &&
2721         srcFilter->serviceDataLength == dstFilter->serviceDataLength &&
2722         srcFilter->manufactureId == dstFilter->manufactureId &&
2723         srcFilter->manufactureDataLength == dstFilter->manufactureDataLength &&
2724         ((srcFilter->serviceData != NULL && dstFilter->serviceData != NULL &&
2725         srcFilter->serviceDataMask != NULL && dstFilter->serviceDataMask != NULL &&
2726         memcmp(srcFilter->serviceData, dstFilter->serviceData, srcFilter->serviceDataLength) == 0 &&
2727         memcmp(srcFilter->serviceDataMask, dstFilter->serviceDataMask, srcFilter->serviceDataLength) == 0) ||
2728         (srcFilter->serviceData == NULL && dstFilter->serviceData == NULL &&
2729         srcFilter->serviceDataMask == NULL && dstFilter->serviceDataMask == NULL)) &&
2730         ((srcFilter->manufactureData != NULL && dstFilter->manufactureData != NULL &&
2731         srcFilter->manufactureDataMask != NULL && dstFilter->manufactureDataMask != NULL &&
2732         memcmp(srcFilter->manufactureData, dstFilter->manufactureData, srcFilter->manufactureDataLength) == 0 &&
2733         memcmp(srcFilter->manufactureDataMask, dstFilter->manufactureDataMask,
2734             srcFilter->manufactureDataLength) == 0) ||
2735         (srcFilter->manufactureData == NULL && dstFilter->manufactureData == NULL &&
2736         srcFilter->manufactureDataMask == NULL && dstFilter->manufactureDataMask == NULL));
2737 }
2738 
CompareFilterAndGetIndex(int32_t listenerId,BcScanFilter * filter,uint8_t filterNum)2739 static int32_t CompareFilterAndGetIndex(int32_t listenerId, BcScanFilter *filter, uint8_t filterNum)
2740 {
2741     DISC_CHECK_AND_RETURN_RET_LOGE(filter != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "filter is nullptr");
2742     DISC_CHECK_AND_RETURN_RET_LOGE(!((filterNum <= 0) || (filterNum > MAX_FILTER_SIZE)),
2743         SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid param filterNum");
2744 
2745     ReleaseScanIdx(listenerId);
2746 
2747     g_scanManager[listenerId].added = (uint8_t *)SoftBusCalloc(filterNum * sizeof(uint8_t));
2748     DISC_CHECK_AND_RETURN_RET_LOGE(g_scanManager[listenerId].added != NULL, SOFTBUS_MALLOC_ERR, DISC_BROADCAST,
2749         "memory allocation failed");
2750     g_scanManager[listenerId].addSize = 0;
2751     g_scanManager[listenerId].deleted = (uint8_t *)SoftBusCalloc(g_scanManager[listenerId].filterSize *
2752         sizeof(uint8_t));
2753     if (g_scanManager[listenerId].deleted == NULL) {
2754         DISC_LOGI(DISC_BROADCAST, "memory allocation failed");
2755         ReleaseScanIdx(listenerId);
2756         return SOFTBUS_MALLOC_ERR;
2757     }
2758     g_scanManager[listenerId].deleteSize = 0;
2759 
2760     for (int i = 0; i < g_scanManager[listenerId].filterSize; i++) {
2761         bool isSameFilter = false;
2762         for (int j = 0; j < filterNum; j++) {
2763             if (CompareSameFilter(&g_scanManager[listenerId].filter[i], &filter[j])) {
2764                 filter[j].filterIndex = g_scanManager[listenerId].filter[i].filterIndex;
2765                 DISC_LOGI(DISC_BROADCAST, "same filter, equal index=%{public}d",
2766                     g_scanManager[listenerId].filter[i].filterIndex);
2767                 isSameFilter = true;
2768                 break;
2769             }
2770         }
2771         if (!isSameFilter) {
2772             g_scanManager[listenerId].deleted[g_scanManager[listenerId].deleteSize++] =
2773                 g_scanManager[listenerId].filter[i].filterIndex;
2774             DISC_LOGI(DISC_BROADCAST, "old filter del index, filterIndex=%{public}d",
2775                 g_scanManager[listenerId].filter[i].filterIndex);
2776         }
2777     }
2778 
2779     for (int i = 0; i < filterNum; i++) {
2780         if (filter[i].filterIndex == 0) {
2781             if (GetFilterIndex(&filter[i].filterIndex) == SOFTBUS_OK) {
2782                 g_scanManager[listenerId].added[g_scanManager[listenerId].addSize++] = i;
2783                 DISC_LOGI(DISC_BROADCAST, "new filter add index, filterIndex=%{public}d", filter[i].filterIndex);
2784             } else {
2785                 DISC_LOGI(DISC_BROADCAST, "filter add index failed");
2786                 ReleaseScanIdx(listenerId);
2787                 return SOFTBUS_INVALID_PARAM;
2788             }
2789         }
2790     }
2791     return SOFTBUS_OK;
2792 }
2793 
SetScanFilter(int32_t listenerId,const BcScanFilter * scanFilter,uint8_t filterNum)2794 int32_t SetScanFilter(int32_t listenerId, const BcScanFilter *scanFilter, uint8_t filterNum)
2795 {
2796     DISC_LOGI(DISC_BROADCAST, "enter set scan filter, filterNum=%{public}d", filterNum);
2797     DISC_CHECK_AND_RETURN_RET_LOGE(scanFilter != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "param is nullptr");
2798     DISC_CHECK_AND_RETURN_RET_LOGE(!((filterNum <= 0) || (filterNum > MAX_FILTER_SIZE)),
2799         SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid param filterNum");
2800     int32_t ret = SoftBusMutexLock(&g_scanLock);
2801     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
2802 
2803     if (!CheckScanIdIsValid(listenerId)) {
2804         DISC_LOGE(DISC_BROADCAST, "invalid param listenerId. listenerId=%{public}d", listenerId);
2805         SoftBusMutexUnlock(&g_scanLock);
2806         return SOFTBUS_BC_MGR_INVALID_LISN_ID;
2807     }
2808 
2809     BcScanFilter *filter = (BcScanFilter *)scanFilter;
2810     if (g_scanManager[listenerId].isScanning) {
2811         ret = CompareFilterAndGetIndex(listenerId, filter, filterNum);
2812         if (ret != SOFTBUS_OK) {
2813             DISC_LOGE(DISC_BROADCAST, "set scan filter failed");
2814             SoftBusMutexUnlock(&g_scanLock);
2815             return SOFTBUS_INVALID_PARAM;
2816         }
2817     } else {
2818         if (g_scanManager[listenerId].filterSize != 0) {
2819             for (int i = 0; i < g_scanManager[listenerId].filterSize; i++) {
2820                 DISC_LOGI(DISC_BROADCAST, "not scanning, just release index, filterIndex=%{public}d",
2821                     g_scanManager[listenerId].filter[i].filterIndex);
2822                 g_firstSetIndex[g_scanManager[listenerId].filter[i].filterIndex] = false;
2823             }
2824         }
2825 
2826         if (filterNum > 0) {
2827             for (int i = 0; i < filterNum; i++) {
2828                 GetFilterIndex(&filter[i].filterIndex);
2829                 DISC_LOGI(DISC_BROADCAST, "add filter index, filterIndex=%{public}d",
2830                     filter[i].filterIndex);
2831             }
2832         }
2833     }
2834 
2835     ReleaseBcScanFilter(listenerId);
2836     g_scanManager[listenerId].filter = (BcScanFilter *)scanFilter;
2837     g_scanManager[listenerId].filterSize = filterNum;
2838     // Need to reset scanner when filter changed.
2839     g_scanManager[listenerId].isFliterChanged = true;
2840     DISC_LOGI(DISC_BROADCAST, "srvType=%{public}s, listenerId=%{public}d, adapterId=%{public}d",
2841               GetSrvType(g_scanManager[listenerId].srvType), listenerId, g_scanManager[listenerId].adapterScanId);
2842     SoftBusMutexUnlock(&g_scanLock);
2843     return SOFTBUS_OK;
2844 }
2845 
GetScanFilter(int32_t listenerId,BcScanFilter ** scanFilter,uint8_t * filterNum)2846 int32_t GetScanFilter(int32_t listenerId, BcScanFilter **scanFilter, uint8_t *filterNum)
2847 {
2848     DISC_LOGD(DISC_BROADCAST, "enter get scan filter");
2849     DISC_CHECK_AND_RETURN_RET_LOGE(scanFilter != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid scanFilter");
2850     DISC_CHECK_AND_RETURN_RET_LOGE(filterNum != NULL, SOFTBUS_INVALID_PARAM, DISC_BROADCAST, "invalid filterNum");
2851 
2852     int32_t ret = SoftBusMutexLock(&g_scanLock);
2853     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
2854 
2855     if (!CheckScanIdIsValid(listenerId)) {
2856         DISC_LOGE(DISC_BROADCAST, "invalid param listenerId. listenerId=%{public}d", listenerId);
2857         SoftBusMutexUnlock(&g_scanLock);
2858         return SOFTBUS_BC_MGR_INVALID_LISN_ID;
2859     }
2860 
2861     *scanFilter = g_scanManager[listenerId].filter;
2862     *filterNum = g_scanManager[listenerId].filterSize;
2863     SoftBusMutexUnlock(&g_scanLock);
2864     return SOFTBUS_OK;
2865 }
2866 
QueryBroadcastStatus(int32_t bcId,int32_t * status)2867 int32_t QueryBroadcastStatus(int32_t bcId, int32_t *status)
2868 {
2869     DISC_LOGI(DISC_BROADCAST, "enter query bc status");
2870     (void)bcId;
2871     (void)status;
2872     return SOFTBUS_OK;
2873 }
2874 
BroadcastIsLpDeviceAvailable(void)2875 bool BroadcastIsLpDeviceAvailable(void)
2876 {
2877     DISC_LOGI(DISC_BROADCAST, "enter lp available");
2878     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[BROADCAST_PROTOCOL_BLE] != NULL,
2879         false, DISC_BROADCAST, "interface is nullptr");
2880     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[BROADCAST_PROTOCOL_BLE]->IsLpDeviceAvailable != NULL,
2881         false, DISC_BROADCAST, "function is nullptr");
2882 
2883     return g_interface[BROADCAST_PROTOCOL_BLE]->IsLpDeviceAvailable();
2884 }
2885 
BroadcastSetAdvDeviceParam(LpServerType type,const LpBroadcastParam * bcParam,const LpScanParam * scanParam)2886 bool BroadcastSetAdvDeviceParam(LpServerType type, const LpBroadcastParam *bcParam,
2887     const LpScanParam *scanParam)
2888 {
2889     DISC_LOGD(DISC_BROADCAST, "enter set adv dev param");
2890     DISC_CHECK_AND_RETURN_RET_LOGE(bcParam != NULL, false, DISC_BROADCAST, "invalid param bcParam");
2891     DISC_CHECK_AND_RETURN_RET_LOGE(scanParam != NULL, false, DISC_BROADCAST, "invalid param scanParam");
2892     DISC_CHECK_AND_RETURN_RET_LOGE(type < SOFTBUS_UNKNOW_TYPE && type >= SOFTBUS_HEARTBEAT_TYPE,
2893         false, DISC_BROADCAST, "invalid app type");
2894     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[BROADCAST_PROTOCOL_BLE] != NULL,
2895         false, DISC_BROADCAST, "interface is nullptr");
2896     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[BROADCAST_PROTOCOL_BLE]->SetAdvFilterParam != NULL,
2897         false, DISC_BROADCAST, "function is nullptr");
2898 
2899     SoftBusLpBroadcastParam bcDstParam = {0};
2900     SoftBusLpScanParam scanDstParam = {0};
2901 
2902     bcDstParam.advHandle = bcParam->bcHandle;
2903     ConvertBcParams(BROADCAST_PROTOCOL_BLE, &bcParam->bcParam, &bcDstParam.advParam);
2904 
2905     int32_t ret = BuildSoftbusBroadcastData(BROADCAST_PROTOCOL_BLE, &bcParam->packet, &bcDstParam.advData);
2906     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, false, DISC_BROADCAST, "build SoftbusBroadcastData failed");
2907 
2908     BuildSoftBusBcScanParams(&scanParam->scanParam, &scanDstParam.scanParam);
2909     BcScanFilter *scanFilter = NULL;
2910     uint8_t filterNum = 0;
2911     ret = GetScanFilter(scanParam->listenerId, &scanFilter, &filterNum);
2912     if (ret != SOFTBUS_OK || scanFilter == NULL || filterNum == 0) {
2913         DISC_LOGE(DISC_BROADCAST, "get listenerId filters failed, listenerId=%{public}d", scanParam->listenerId);
2914         ReleaseSoftbusBroadcastData(&bcDstParam.advData);
2915         return false;
2916     }
2917     scanDstParam.filter = (SoftBusBcScanFilter *)SoftBusCalloc(sizeof(SoftBusBcScanFilter) * (filterNum));
2918     if (scanDstParam.filter == NULL) {
2919         ReleaseSoftbusBroadcastData(&bcDstParam.advData);
2920         return false;
2921     }
2922     scanDstParam.filterSize = filterNum;
2923     ret = CovertSoftBusBcScanFilters(scanFilter, filterNum, scanDstParam.filter);
2924     if (ret != SOFTBUS_OK) {
2925         DISC_LOGE(DISC_BROADCAST, "convert bc scan filters failed");
2926         ReleaseSoftbusBroadcastData(&bcDstParam.advData);
2927         ReleaseSoftBusBcScanFilter(scanDstParam.filter, filterNum);
2928         return false;
2929     }
2930     DISC_LOGI(DISC_BROADCAST, "set adv dev param, bcId=%{public}d, listenerId=%{public}d",
2931         bcParam->bcHandle, scanParam->listenerId);
2932     ret = g_interface[BROADCAST_PROTOCOL_BLE]->SetAdvFilterParam(type, &bcDstParam, &scanDstParam);
2933     ReleaseSoftbusBroadcastData(&bcDstParam.advData);
2934     ReleaseSoftBusBcScanFilter(scanDstParam.filter, filterNum);
2935     DISC_CHECK_AND_RETURN_RET_LOGE(ret, false, DISC_BROADCAST, "call from adapter failed");
2936     return true;
2937 }
2938 
BroadcastGetBroadcastHandle(int32_t bcId,int32_t * bcHandle)2939 int32_t BroadcastGetBroadcastHandle(int32_t bcId, int32_t *bcHandle)
2940 {
2941     DISC_LOGD(DISC_BROADCAST, "enter get bc handle");
2942     int32_t ret = SoftBusMutexLock(&g_bcLock);
2943     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
2944 
2945     if (!CheckBcIdIsValid(bcId)) {
2946         DISC_LOGE(DISC_BROADCAST, "bcId is invalid");
2947         SoftBusMutexUnlock(&g_bcLock);
2948         return SOFTBUS_INVALID_PARAM;
2949     }
2950     BroadcastProtocol protocol = g_bcManager[bcId].protocol;
2951     if (!CheckProtocolIsValid(protocol) || g_interface[protocol] == NULL ||
2952         g_interface[protocol]->GetBroadcastHandle == NULL) {
2953         DISC_LOGE(DISC_BROADCAST, "protocol =%{public}d is not registered", protocol);
2954         SoftBusMutexUnlock(&g_bcLock);
2955         return SOFTBUS_INVALID_PARAM;
2956     }
2957     SoftBusMutexUnlock(&g_bcLock);
2958 
2959     ret = g_interface[protocol]->GetBroadcastHandle(g_bcManager[bcId].adapterBcId, bcHandle);
2960     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "call from adapter failed");
2961     return SOFTBUS_OK;
2962 }
2963 
BroadcastEnableSyncDataToLpDevice(void)2964 int32_t BroadcastEnableSyncDataToLpDevice(void)
2965 {
2966     DISC_LOGI(DISC_BROADCAST, "enter enable sync");
2967     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[BROADCAST_PROTOCOL_BLE] != NULL, SOFTBUS_BC_MGR_NO_FUNC_REGISTERED,
2968         DISC_BROADCAST, "interface is nullptr");
2969     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[BROADCAST_PROTOCOL_BLE]->EnableSyncDataToLpDevice != NULL,
2970         SOFTBUS_BC_MGR_FUNC_NULL, DISC_BROADCAST, "function is nullptr");
2971 
2972     int32_t ret = g_interface[BROADCAST_PROTOCOL_BLE]->EnableSyncDataToLpDevice();
2973     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "call from adapter failed");
2974 
2975     return SOFTBUS_OK;
2976 }
2977 
BroadcastDisableSyncDataToLpDevice(void)2978 int32_t BroadcastDisableSyncDataToLpDevice(void)
2979 {
2980     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[BROADCAST_PROTOCOL_BLE] != NULL, SOFTBUS_BC_MGR_NO_FUNC_REGISTERED,
2981         DISC_BROADCAST, "interface is nullptr");
2982     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[BROADCAST_PROTOCOL_BLE]->DisableSyncDataToLpDevice != NULL,
2983         SOFTBUS_BC_MGR_FUNC_NULL, DISC_BROADCAST, "function is nullptr");
2984 
2985     int32_t ret = g_interface[BROADCAST_PROTOCOL_BLE]->DisableSyncDataToLpDevice();
2986     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "call from adapter failed");
2987 
2988     return SOFTBUS_OK;
2989 }
2990 
BroadcastSetScanReportChannelToLpDevice(int32_t listenerId,bool enable)2991 int32_t BroadcastSetScanReportChannelToLpDevice(int32_t listenerId, bool enable)
2992 {
2993     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[BROADCAST_PROTOCOL_BLE] != NULL, SOFTBUS_BC_MGR_NO_FUNC_REGISTERED,
2994         DISC_BROADCAST, "interface is nullptr");
2995     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[BROADCAST_PROTOCOL_BLE]->SetScanReportChannelToLpDevice != NULL,
2996         SOFTBUS_BC_MGR_FUNC_NULL, DISC_BROADCAST, "function is nullptr");
2997 
2998     int32_t ret = SoftBusMutexLock(&g_scanLock);
2999     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, DISC_BROADCAST, "mutex error");
3000 
3001     if (!CheckScanIdIsValid(listenerId)) {
3002         DISC_LOGE(DISC_BROADCAST, "invalid param listenerId. listenerId=%{public}d", listenerId);
3003         SoftBusMutexUnlock(&g_scanLock);
3004         return SOFTBUS_BC_MGR_INVALID_LISN_ID;
3005     }
3006 
3007     ScanManager manager = g_scanManager[listenerId];
3008     ret = g_interface[BROADCAST_PROTOCOL_BLE]->SetScanReportChannelToLpDevice(manager.adapterScanId, enable);
3009     if (ret != SOFTBUS_OK) {
3010         DISC_LOGE(DISC_BROADCAST, "call from adapter failed");
3011         SoftBusMutexUnlock(&g_scanLock);
3012         return ret;
3013     }
3014     SoftBusMutexUnlock(&g_scanLock);
3015     return SOFTBUS_OK;
3016 }
3017 
BroadcastSetLpAdvParam(int32_t duration,int32_t maxExtAdvEvents,int32_t window,int32_t interval,int32_t bcHandle)3018 int32_t BroadcastSetLpAdvParam(int32_t duration, int32_t maxExtAdvEvents, int32_t window,
3019     int32_t interval, int32_t bcHandle)
3020 {
3021     DISC_LOGI(DISC_BROADCAST, "enter set lp adv param");
3022     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[BROADCAST_PROTOCOL_BLE] != NULL, SOFTBUS_BC_MGR_NO_FUNC_REGISTERED,
3023         DISC_BROADCAST, "interface is nullptr");
3024     DISC_CHECK_AND_RETURN_RET_LOGE(g_interface[BROADCAST_PROTOCOL_BLE]->SetLpDeviceParam != NULL,
3025         SOFTBUS_BC_MGR_FUNC_NULL, DISC_BROADCAST, "function is nullptr");
3026 
3027     int32_t ret = g_interface[BROADCAST_PROTOCOL_BLE]->SetLpDeviceParam(duration,
3028         maxExtAdvEvents, window, interval, bcHandle);
3029     DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, DISC_BROADCAST, "call from adapter failed");
3030 
3031     return SOFTBUS_OK;
3032 }
3033 
RegisterInfoDump(int fd)3034 static int32_t RegisterInfoDump(int fd)
3035 {
3036     SOFTBUS_DPRINTF(fd, "\n---------------------------Register Broadcaster Info-------------------------\n");
3037     SOFTBUS_DPRINTF(fd, "max broadcaster num                   : %d\n", BC_NUM_MAX);
3038     SOFTBUS_DPRINTF(fd, "isAdvertising : 0 - false, 1 - true\n\n");
3039     int32_t managerId;
3040     for (managerId = 0; managerId < BC_NUM_MAX; managerId++) {
3041         if (!g_bcManager[managerId].isUsed) {
3042             continue;
3043         }
3044         BroadcastManager *bcManager = &g_bcManager[managerId];
3045         SOFTBUS_DPRINTF(fd, "managerId : %d, ", managerId);
3046         SOFTBUS_DPRINTF(fd, "adapterBcId : %d, ", bcManager->adapterBcId);
3047         SOFTBUS_DPRINTF(fd, "isAdvertising : %d, ", bcManager->isAdvertising);
3048         SOFTBUS_DPRINTF(fd, "serviceType : %s\n", GetSrvType(bcManager->srvType));
3049     }
3050 
3051     SOFTBUS_DPRINTF(fd, "\n---------------------------Register Listener Info----------------------------\n");
3052     SOFTBUS_DPRINTF(fd, "max listener num                      : %d\n", SCAN_NUM_MAX);
3053     SOFTBUS_DPRINTF(fd, "freq : 0 - low power, 1 - 60/3000, 2 - 30/1500, 3 - 30/300, 4 - 60/240, 5 - 30/60, "
3054         "6 - 30/40, 7 - 1000/1000\n");
3055     SOFTBUS_DPRINTF(fd, "isFliterChanged/isScanning : 0 - false, 1 - true\n\n");
3056     for (managerId = 0; managerId < SCAN_NUM_MAX; managerId++) {
3057         if (!g_scanManager[managerId].isUsed) {
3058             continue;
3059         }
3060         ScanManager *scanManager = &g_scanManager[managerId];
3061         SOFTBUS_DPRINTF(fd, "managerId : %d, ", managerId);
3062         SOFTBUS_DPRINTF(fd, "adapterScanId : %d, ", scanManager->adapterScanId);
3063         SOFTBUS_DPRINTF(fd, "isFliterChanged : %d, ", scanManager->isFliterChanged);
3064         SOFTBUS_DPRINTF(fd, "isScanning : %d, ", scanManager->isScanning);
3065         SOFTBUS_DPRINTF(fd, "scan freq: %d, ", scanManager->freq);
3066         SOFTBUS_DPRINTF(fd, "serviceType : %s\n", GetSrvType(scanManager->srvType));
3067     }
3068     return SOFTBUS_OK;
3069 }
3070