1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "softbus_adapter_ble_gatt.h"
16
17 #include "adapter_bt_utils.h"
18 #include "ohos_bt_def.h"
19 #include "ohos_bt_gap.h"
20 #include "ohos_bt_gatt.h"
21 #include "securec.h"
22 #include "softbus_adapter_bt_common.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_adapter_thread.h"
25 #include "softbus_errcode.h"
26 #include "softbus_log.h"
27
28 #define SOFTBUS_SCAN_CLIENT_ID 0
29 #define ADV_MAX_NUM 4
30 #define SCAN_MAX_NUM 2
31
32 typedef struct {
33 int advId;
34 bool isUsed;
35 bool isAdvertising;
36 SoftBusCond cond;
37 SoftBusBleAdvData advData;
38 SoftBusAdvCallback *advCallback;
39 } AdvChannel;
40
41 typedef struct {
42 bool isUsed;
43 bool isScanning;
44 SoftBusBleScanParams param;
45 SoftBusScanListener *listener;
46 } ScanListener;
47
48 static AdvChannel g_advChannel[ADV_MAX_NUM];
49 static ScanListener g_scanListener[SCAN_MAX_NUM];
50 static SoftBusMutex g_advLock = {0};
51 static SoftBusMutex g_scanerLock = {0};
52 static bool g_isRegCb = false;
53
BleGattLockInit(void)54 int BleGattLockInit(void)
55 {
56 if (SoftBusMutexInit(&g_advLock, NULL) != SOFTBUS_OK) {
57 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "g_advLock init failed");
58 return SOFTBUS_ERR;
59 }
60 if (SoftBusMutexInit(&g_scanerLock, NULL) != SOFTBUS_OK) {
61 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "g_scanerLock init failed");
62 return SOFTBUS_ERR;
63 }
64 return SOFTBUS_OK;
65 }
66
ConvertScanFilterPolicy(unsigned char policy)67 static unsigned char ConvertScanFilterPolicy(unsigned char policy)
68 {
69 switch (policy) {
70 case OHOS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL:
71 return SOFTBUS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL;
72 case OHOS_BLE_SCAN_FILTER_POLICY_ONLY_WHITE_LIST:
73 return SOFTBUS_BLE_SCAN_FILTER_POLICY_ONLY_WHITE_LIST;
74 case OHOS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL_AND_RPA:
75 return SOFTBUS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL_AND_RPA;
76 case OHOS_BLE_SCAN_FILTER_POLICY_ONLY_WHITE_LIST_AND_RPA:
77 return SOFTBUS_BLE_SCAN_FILTER_POLICY_ONLY_WHITE_LIST_AND_RPA;
78 default:
79 return SOFTBUS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL;
80 }
81 }
82
ConvertScanEventType(unsigned char eventType)83 static unsigned char ConvertScanEventType(unsigned char eventType)
84 {
85 switch (eventType) {
86 case OHOS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE:
87 return SOFTBUS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE;
88 case OHOS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE_DIRECTED:
89 return SOFTBUS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE_DIRECTED;
90 case OHOS_BLE_EVT_CONNECTABLE:
91 return SOFTBUS_BLE_EVT_CONNECTABLE;
92 case OHOS_BLE_EVT_CONNECTABLE_DIRECTED:
93 return SOFTBUS_BLE_EVT_CONNECTABLE_DIRECTED;
94 case OHOS_BLE_EVT_SCANNABLE:
95 return SOFTBUS_BLE_EVT_SCANNABLE;
96 case OHOS_BLE_EVT_SCANNABLE_DIRECTED:
97 return SOFTBUS_BLE_EVT_SCANNABLE_DIRECTED;
98 case OHOS_BLE_EVT_LEGACY_NON_CONNECTABLE:
99 return SOFTBUS_BLE_EVT_LEGACY_NON_CONNECTABLE;
100 case OHOS_BLE_EVT_LEGACY_SCANNABLE:
101 return SOFTBUS_BLE_EVT_LEGACY_SCANNABLE;
102 case OHOS_BLE_EVT_LEGACY_CONNECTABLE:
103 return SOFTBUS_BLE_EVT_LEGACY_CONNECTABLE;
104 case OHOS_BLE_EVT_LEGACY_CONNECTABLE_DIRECTED:
105 return SOFTBUS_BLE_EVT_LEGACY_CONNECTABLE_DIRECTED;
106 case OHOS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV_SCAN:
107 return SOFTBUS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV_SCAN;
108 case OHOS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV:
109 return SOFTBUS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV;
110 default:
111 return SOFTBUS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE;
112 }
113 }
114
ConvertScanPhyType(unsigned char phyType)115 static unsigned char ConvertScanPhyType(unsigned char phyType)
116 {
117 switch (phyType) {
118 case OHOS_BLE_SCAN_PHY_NO_PACKET:
119 return SOFTBUS_BLE_SCAN_PHY_NO_PACKET;
120 case OHOS_BLE_SCAN_PHY_1M:
121 return SOFTBUS_BLE_SCAN_PHY_1M;
122 case OHOS_BLE_SCAN_PHY_2M:
123 return SOFTBUS_BLE_SCAN_PHY_2M;
124 case OHOS_BLE_SCAN_PHY_CODED:
125 return SOFTBUS_BLE_SCAN_PHY_CODED;
126 default:
127 return SOFTBUS_BLE_SCAN_PHY_NO_PACKET;
128 }
129 }
130
ConvertScanDataStatus(unsigned char dataStatus)131 static unsigned char ConvertScanDataStatus(unsigned char dataStatus)
132 {
133 switch (dataStatus) {
134 case OHOS_BLE_DATA_COMPLETE:
135 return SOFTBUS_BLE_DATA_COMPLETE;
136 case OHOS_BLE_DATA_INCOMPLETE_MORE_TO_COME:
137 return SOFTBUS_BLE_DATA_INCOMPLETE_MORE_TO_COME;
138 case OHOS_BLE_DATA_INCOMPLETE_TRUNCATED:
139 return SOFTBUS_BLE_DATA_INCOMPLETE_TRUNCATED;
140 default:
141 return SOFTBUS_BLE_DATA_INCOMPLETE_TRUNCATED;
142 }
143 }
144
ConvertScanAddrType(unsigned char addrType)145 static unsigned char ConvertScanAddrType(unsigned char addrType)
146 {
147 switch (addrType) {
148 case OHOS_BLE_PUBLIC_DEVICE_ADDRESS:
149 return SOFTBUS_BLE_PUBLIC_DEVICE_ADDRESS;
150 case OHOS_BLE_RANDOM_DEVICE_ADDRESS:
151 return SOFTBUS_BLE_RANDOM_DEVICE_ADDRESS;
152 case OHOS_BLE_PUBLIC_IDENTITY_ADDRESS:
153 return SOFTBUS_BLE_PUBLIC_IDENTITY_ADDRESS;
154 case OHOS_BLE_RANDOM_STATIC_IDENTITY_ADDRESS:
155 return SOFTBUS_BLE_RANDOM_STATIC_IDENTITY_ADDRESS;
156 case OHOS_BLE_UNRESOLVABLE_RANDOM_DEVICE_ADDRESS:
157 return SOFTBUS_BLE_UNRESOLVABLE_RANDOM_DEVICE_ADDRESS;
158 case OHOS_BLE_NO_ADDRESS:
159 return SOFTBUS_BLE_NO_ADDRESS;
160 default:
161 return SOFTBUS_BLE_NO_ADDRESS;
162 }
163 }
164
ConvertScanType(unsigned char scanType)165 static unsigned char ConvertScanType(unsigned char scanType)
166 {
167 switch (scanType) {
168 case OHOS_BLE_SCAN_TYPE_PASSIVE:
169 return SOFTBUS_BLE_SCAN_TYPE_PASSIVE;
170 case OHOS_BLE_SCAN_TYPE_ACTIVE:
171 return SOFTBUS_BLE_SCAN_TYPE_ACTIVE;
172 default:
173 return SOFTBUS_BLE_SCAN_TYPE_PASSIVE;
174 }
175 }
176
ConvertScanParam(const SoftBusBleScanParams * src,BleScanParams * dst)177 void ConvertScanParam(const SoftBusBleScanParams *src, BleScanParams *dst)
178 {
179 if (src == NULL || dst == NULL) {
180 return;
181 }
182 dst->scanInterval = src->scanInterval;
183 dst->scanWindow = src->scanWindow;
184 dst->scanType = ConvertScanType(src->scanType);
185 dst->scanPhy = ConvertScanPhyType(src->scanPhy);
186 dst->scanFilterPolicy = ConvertScanFilterPolicy(src->scanFilterPolicy);
187 }
188
ConvertScanResult(const BtScanResultData * src,SoftBusBleScanResult * dst)189 static void ConvertScanResult(const BtScanResultData *src, SoftBusBleScanResult *dst)
190 {
191 if (src == NULL || dst == NULL) {
192 return;
193 }
194 dst->eventType = ConvertScanEventType(src->eventType);
195 dst->dataStatus = ConvertScanDataStatus(src->dataStatus);
196 dst->addrType = ConvertScanAddrType(src->addrType);
197 if (memcpy_s(dst->addr.addr, BT_ADDR_LEN, src->addr.addr, BT_ADDR_LEN) != EOK) {
198 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "ConvertScanResult memcpy addr fail");
199 return;
200 }
201 dst->primaryPhy = ConvertScanPhyType(src->primaryPhy);
202 dst->secondaryPhy = ConvertScanPhyType(src->secondaryPhy);
203 dst->advSid = src->advSid;
204 dst->txPower = src->txPower;
205 dst->rssi = src->rssi;
206 dst->periodicAdvInterval = src->periodicAdvInterval;
207 dst->directAddrType = ConvertScanAddrType(src->directAddrType);
208 if (memcpy_s(dst->directAddr.addr, BT_ADDR_LEN, src->directAddr.addr, BT_ADDR_LEN) != EOK) {
209 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "ConvertScanResult memcpy directAddr fail");
210 return;
211 }
212 dst->advLen = src->advLen;
213 dst->advData = src->advData;
214 }
215
ConvertAdvType(unsigned char advType)216 static unsigned char ConvertAdvType(unsigned char advType)
217 {
218 switch (advType) {
219 case SOFTBUS_BLE_ADV_IND:
220 return OHOS_BLE_ADV_IND;
221 case SOFTBUS_BLE_ADV_DIRECT_IND_HIGH:
222 return OHOS_BLE_ADV_DIRECT_IND_HIGH;
223 case SOFTBUS_BLE_ADV_SCAN_IND:
224 return OHOS_BLE_ADV_SCAN_IND;
225 case SOFTBUS_BLE_ADV_NONCONN_IND:
226 return OHOS_BLE_ADV_NONCONN_IND;
227 case SOFTBUS_BLE_ADV_DIRECT_IND_LOW:
228 return OHOS_BLE_ADV_DIRECT_IND_LOW;
229 default:
230 return OHOS_BLE_ADV_IND;
231 }
232 }
233
ConvertAdvFilter(unsigned char advFilter)234 static unsigned char ConvertAdvFilter(unsigned char advFilter)
235 {
236 switch (advFilter) {
237 case SOFTBUS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY:
238 return OHOS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
239 case SOFTBUS_BLE_ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY:
240 return OHOS_BLE_ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY;
241 case SOFTBUS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST:
242 return OHOS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST;
243 case SOFTBUS_BLE_ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST:
244 return OHOS_BLE_ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST;
245 default:
246 return OHOS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
247 }
248 }
249
ConvertAdvData(const SoftBusBleAdvData * src,StartAdvRawData * dst)250 static void ConvertAdvData(const SoftBusBleAdvData *src, StartAdvRawData *dst)
251 {
252 dst->advDataLen = src->advLength;
253 dst->advData = (unsigned char *)src->advData;
254 dst->rspDataLen = src->scanRspLength;
255 dst->rspData = (unsigned char *)src->scanRspData;
256 }
257
ConvertAdvParam(const SoftBusBleAdvParams * src,BleAdvParams * dst)258 static void ConvertAdvParam(const SoftBusBleAdvParams *src, BleAdvParams *dst)
259 {
260 dst->minInterval = src->minInterval;
261 dst->maxInterval = src->maxInterval;
262 dst->advType = ConvertAdvType(src->advType);
263 dst->ownAddrType = 0x00;
264 dst->peerAddrType = 0x00;
265 if (memcpy_s(dst->peerAddr.addr, BT_ADDR_LEN, src->peerAddr.addr, BT_ADDR_LEN) != EOK) {
266 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "ConvertScanResult memcpy directAddr fail");
267 return;
268 }
269 dst->channelMap = src->channelMap;
270 dst->advFilterPolicy = ConvertAdvFilter(src->advFilterPolicy);
271 dst->txPower = src->txPower;
272 dst->duration = src->duration;
273 }
274
WrapperAdvEnableCallback(int advId,int status)275 static void WrapperAdvEnableCallback(int advId, int status)
276 {
277 int st = BleOhosStatusToSoftBus((BtStatus)status);
278 for (uint32_t index = 0; index < ADV_MAX_NUM; index++) {
279 AdvChannel *advChannel = &g_advChannel[index];
280 if (advChannel->advId != advId ||
281 advChannel->isUsed == false ||
282 advChannel->advCallback == NULL ||
283 advChannel->advCallback->AdvEnableCallback == NULL) {
284 continue;
285 }
286 if (st == SOFTBUS_BT_STATUS_SUCCESS) {
287 advChannel->isAdvertising = true;
288 SoftBusCondSignal(&advChannel->cond);
289 }
290 advChannel->advCallback->AdvEnableCallback(index, st);
291 break;
292 }
293 }
294
WrapperAdvDisableCallback(int advId,int status)295 static void WrapperAdvDisableCallback(int advId, int status)
296 {
297 int st = BleOhosStatusToSoftBus((BtStatus)status);
298 for (uint32_t index = 0; index < ADV_MAX_NUM; index++) {
299 AdvChannel *advChannel = &g_advChannel[index];
300 if (advChannel->advId != advId ||
301 advChannel->isUsed == false ||
302 advChannel->advCallback == NULL ||
303 advChannel->advCallback->AdvDisableCallback == NULL) {
304 continue;
305 }
306 if (st == SOFTBUS_BT_STATUS_SUCCESS) {
307 advChannel->isAdvertising = false;
308 SoftBusCondSignal(&advChannel->cond);
309 }
310 advChannel->advCallback->AdvDisableCallback(index, st);
311 break;
312 }
313 }
314
WrapperAdvDataCallback(int advId,int status)315 static void WrapperAdvDataCallback(int advId, int status)
316 {
317 int st = BleOhosStatusToSoftBus((BtStatus)status);
318 for (uint32_t index = 0; index < ADV_MAX_NUM; index++) {
319 AdvChannel *advChannel = &g_advChannel[index];
320 if (advChannel->advId != advId ||
321 advChannel->isUsed == false ||
322 advChannel->advCallback == NULL ||
323 advChannel->advCallback->AdvDataCallback == NULL) {
324 continue;
325 }
326 advChannel->advCallback->AdvDataCallback(index, st);
327 break;
328 }
329 }
330
WrapperAdvUpdateCallback(int advId,int status)331 static void WrapperAdvUpdateCallback(int advId, int status)
332 {
333 int st = BleOhosStatusToSoftBus((BtStatus)status);
334 for (uint32_t index = 0; index < ADV_MAX_NUM; index++) {
335 AdvChannel *advChannel = &g_advChannel[index];
336 if (advChannel->advId != advId ||
337 advChannel->isUsed == false ||
338 advChannel->advCallback == NULL ||
339 advChannel->advCallback->AdvUpdateCallback == NULL) {
340 continue;
341 }
342 advChannel->advCallback->AdvUpdateCallback(index, st);
343 break;
344 }
345 }
346
WrapperSecurityRespondCallback(const BdAddr * bdAddr)347 static void WrapperSecurityRespondCallback(const BdAddr *bdAddr)
348 {
349 (void)bdAddr;
350 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "WrapperSecurityRespondCallback");
351 }
352
WrapperScanResultCallback(BtScanResultData * scanResultdata)353 static void WrapperScanResultCallback(BtScanResultData *scanResultdata)
354 {
355 if (scanResultdata == NULL) {
356 return;
357 }
358 int listenerId;
359 SoftBusBleScanResult sr;
360 ConvertScanResult(scanResultdata, &sr);
361 for (listenerId = 0; listenerId < SCAN_MAX_NUM; listenerId++) {
362 SoftBusMutexLock(&g_scanerLock);
363 ScanListener *scanListener = &g_scanListener[listenerId];
364 if (!scanListener->isUsed || scanListener->listener == NULL || !scanListener->isScanning ||
365 scanListener->listener->OnScanResult == NULL) {
366 SoftBusMutexUnlock(&g_scanerLock);
367 continue;
368 }
369 SoftBusMutexUnlock(&g_scanerLock);
370 scanListener->listener->OnScanResult(listenerId, &sr);
371 }
372 }
373
WrapperScanParameterSetCompletedCallback(int clientId,int status)374 static void WrapperScanParameterSetCompletedCallback(int clientId, int status)
375 {
376 (void)clientId;
377 (void)status;
378 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "WrapperScanParameterSetCompletedCallback");
379 }
380
381 static BtGattCallbacks g_softbusGattCb = {
382 .advEnableCb = WrapperAdvEnableCallback,
383 .advDisableCb = WrapperAdvDisableCallback,
384 .advDataCb = WrapperAdvDataCallback,
385 .advUpdateCb = WrapperAdvUpdateCallback,
386 .securityRespondCb = WrapperSecurityRespondCallback,
387 .scanResultCb = WrapperScanResultCallback,
388 .scanParamSetCb = WrapperScanParameterSetCompletedCallback
389 };
390
RegisterBleGattCallback(void)391 static int RegisterBleGattCallback(void)
392 {
393 if (g_isRegCb) {
394 return SOFTBUS_OK;
395 }
396 if (BleGattRegisterCallbacks(&g_softbusGattCb) != 0) {
397 return SOFTBUS_ERR;
398 }
399 g_isRegCb = true;
400 return SOFTBUS_OK;
401 }
402
CheckAdvChannelInUsed(int advId)403 static bool CheckAdvChannelInUsed(int advId)
404 {
405 if (advId < 0 || advId >= ADV_MAX_NUM) {
406 return false;
407 }
408 if (!g_advChannel[advId].isUsed) {
409 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "advId %d is ready released", advId);
410 return false;
411 }
412 return true;
413 }
414
SetAdvData(int advId,const SoftBusBleAdvData * data)415 static int SetAdvData(int advId, const SoftBusBleAdvData *data)
416 {
417 g_advChannel[advId].advData.advLength = data->advLength;
418 g_advChannel[advId].advData.scanRspLength = data->scanRspLength;
419 if (g_advChannel[advId].advData.advData != NULL) {
420 SoftBusFree(g_advChannel[advId].advData.advData);
421 g_advChannel[advId].advData.advData = NULL;
422 }
423 if (g_advChannel[advId].advData.scanRspData != NULL) {
424 SoftBusFree(g_advChannel[advId].advData.scanRspData);
425 g_advChannel[advId].advData.scanRspData = NULL;
426 }
427 if (data->advLength != 0) {
428 g_advChannel[advId].advData.advData = SoftBusCalloc(data->advLength);
429 if (g_advChannel[advId].advData.advData == NULL) {
430 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SetAdvData calloc advData failed");
431 return SOFTBUS_MALLOC_ERR;
432 }
433 if (memcpy_s(g_advChannel[advId].advData.advData, data->advLength,
434 data->advData, data->advLength) != EOK) {
435 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SetAdvData memcpy advData failed");
436 SoftBusFree(g_advChannel[advId].advData.advData);
437 g_advChannel[advId].advData.advData = NULL;
438 return SOFTBUS_MEM_ERR;
439 }
440 }
441 if (data->scanRspLength != 0) {
442 g_advChannel[advId].advData.scanRspData = SoftBusCalloc(data->scanRspLength);
443 if (g_advChannel[advId].advData.scanRspData == NULL) {
444 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SetAdvData calloc scanRspData failed");
445 SoftBusFree(g_advChannel[advId].advData.advData);
446 g_advChannel[advId].advData.advData = NULL;
447 return SOFTBUS_MALLOC_ERR;
448 }
449 if (memcpy_s(g_advChannel[advId].advData.scanRspData, data->scanRspLength,
450 data->scanRspData, data->scanRspLength) != EOK) {
451 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SetAdvData memcpy scanRspData failed");
452 SoftBusFree(g_advChannel[advId].advData.advData);
453 SoftBusFree(g_advChannel[advId].advData.scanRspData);
454 g_advChannel[advId].advData.advData = NULL;
455 g_advChannel[advId].advData.scanRspData = NULL;
456 return SOFTBUS_MEM_ERR;
457 }
458 }
459 return SOFTBUS_OK;
460 }
461
ClearAdvData(int advId)462 static void ClearAdvData(int advId)
463 {
464 g_advChannel[advId].advData.advLength = 0;
465 g_advChannel[advId].advData.scanRspLength = 0;
466 SoftBusFree(g_advChannel[advId].advData.advData);
467 SoftBusFree(g_advChannel[advId].advData.scanRspData);
468 g_advChannel[advId].advData.advData = NULL;
469 g_advChannel[advId].advData.scanRspData = NULL;
470 }
471
SoftBusGetAdvChannel(const SoftBusAdvCallback * callback)472 int SoftBusGetAdvChannel(const SoftBusAdvCallback *callback)
473 {
474 if (callback == NULL) {
475 return SOFTBUS_INVALID_PARAM;
476 }
477 if (SoftBusMutexLock(&g_advLock) != 0) {
478 return SOFTBUS_LOCK_ERR;
479 }
480 if (RegisterBleGattCallback() != SOFTBUS_OK) {
481 SoftBusMutexUnlock(&g_advLock);
482 return SOFTBUS_ERR;
483 }
484 int freeAdvId;
485 for (freeAdvId = 0; freeAdvId < ADV_MAX_NUM; freeAdvId++) {
486 if (!g_advChannel[freeAdvId].isUsed) {
487 break;
488 }
489 }
490 if (freeAdvId == ADV_MAX_NUM) {
491 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "no available adv channel");
492 SoftBusMutexUnlock(&g_advLock);
493 return SOFTBUS_ERR;
494 }
495 g_advChannel[freeAdvId].advId = -1;
496 g_advChannel[freeAdvId].isUsed = true;
497 g_advChannel[freeAdvId].isAdvertising = false;
498 SoftBusCondInit(&g_advChannel[freeAdvId].cond);
499 g_advChannel[freeAdvId].advCallback = (SoftBusAdvCallback *)callback;
500 SoftBusMutexUnlock(&g_advLock);
501 return freeAdvId;
502 }
503
SoftBusReleaseAdvChannel(int advId)504 int SoftBusReleaseAdvChannel(int advId)
505 {
506 if (SoftBusMutexLock(&g_advLock) != 0) {
507 return SOFTBUS_LOCK_ERR;
508 }
509 if (!CheckAdvChannelInUsed(advId)) {
510 SoftBusMutexUnlock(&g_advLock);
511 return SOFTBUS_ERR;
512 }
513 ClearAdvData(advId);
514 g_advChannel[advId].advId = -1;
515 g_advChannel[advId].isUsed = false;
516 g_advChannel[advId].isAdvertising = false;
517 SoftBusCondDestroy(&g_advChannel[advId].cond);
518 g_advChannel[advId].advCallback = NULL;
519 SoftBusMutexUnlock(&g_advLock);
520 return SOFTBUS_OK;
521 }
522
SoftBusSetAdvData(int advId,const SoftBusBleAdvData * data)523 int SoftBusSetAdvData(int advId, const SoftBusBleAdvData *data)
524 {
525 if (data == NULL) {
526 return SOFTBUS_INVALID_PARAM;
527 }
528 if (SoftBusMutexLock(&g_advLock) != 0) {
529 return SOFTBUS_LOCK_ERR;
530 }
531 if (!CheckAdvChannelInUsed(advId)) {
532 SoftBusMutexUnlock(&g_advLock);
533 return SOFTBUS_ERR;
534 }
535 int ret = SetAdvData(advId, data);
536 if (ret == SOFTBUS_OK) {
537 g_advChannel[advId].advCallback->AdvDataCallback(advId, SOFTBUS_BT_STATUS_SUCCESS);
538 } else {
539 g_advChannel[advId].advCallback->AdvDataCallback(advId, SOFTBUS_BT_STATUS_FAIL);
540 }
541 SoftBusMutexUnlock(&g_advLock);
542 return ret;
543 }
544
SoftBusStartAdv(int advId,const SoftBusBleAdvParams * param)545 int SoftBusStartAdv(int advId, const SoftBusBleAdvParams *param)
546 {
547 if (param == NULL) {
548 return SOFTBUS_INVALID_PARAM;
549 }
550 if (SoftBusMutexLock(&g_advLock) != 0) {
551 return SOFTBUS_LOCK_ERR;
552 }
553 if (!CheckAdvChannelInUsed(advId)) {
554 SoftBusMutexUnlock(&g_advLock);
555 return SOFTBUS_ERR;
556 }
557 if (g_advChannel[advId].isAdvertising) {
558 SoftBusCondWait(&g_advChannel[advId].cond, &g_advLock, NULL);
559 }
560 int innerAdvId;
561 BleAdvParams dstParam;
562 StartAdvRawData advData;
563 ConvertAdvParam(param, &dstParam);
564 ConvertAdvData(&g_advChannel[advId].advData, &advData);
565 int ret = BleStartAdvEx(&innerAdvId, advData, dstParam);
566 if (ret != OHOS_BT_STATUS_SUCCESS) {
567 g_advChannel[advId].advCallback->AdvEnableCallback(advId, SOFTBUS_BT_STATUS_FAIL);
568 SoftBusMutexUnlock(&g_advLock);
569 return SOFTBUS_ERR;
570 }
571 g_advChannel[advId].advCallback->AdvEnableCallback(advId, SOFTBUS_BT_STATUS_SUCCESS);
572 g_advChannel[advId].advId = innerAdvId;
573 SoftBusMutexUnlock(&g_advLock);
574 return SOFTBUS_OK;
575 }
576
SoftBusStopAdv(int advId)577 int SoftBusStopAdv(int advId)
578 {
579 if (SoftBusMutexLock(&g_advLock) != 0) {
580 return SOFTBUS_LOCK_ERR;
581 }
582 if (!CheckAdvChannelInUsed(advId)) {
583 SoftBusMutexUnlock(&g_advLock);
584 return SOFTBUS_ERR;
585 }
586 if (!g_advChannel[advId].isAdvertising) {
587 SoftBusCondWait(&g_advChannel[advId].cond, &g_advLock, NULL);
588 }
589 int ret = BleStopAdv(g_advChannel[advId].advId);
590 if (ret != OHOS_BT_STATUS_SUCCESS) {
591 g_advChannel[advId].advCallback->AdvDisableCallback(advId, SOFTBUS_BT_STATUS_FAIL);
592 SoftBusMutexUnlock(&g_advLock);
593 return SOFTBUS_OK;
594 }
595 ClearAdvData(advId);
596 g_advChannel[advId].advCallback->AdvDisableCallback(advId, SOFTBUS_BT_STATUS_SUCCESS);
597 SoftBusMutexUnlock(&g_advLock);
598 return SOFTBUS_OK;
599 }
600
SoftBusUpdateAdv(int advId,const SoftBusBleAdvData * data,const SoftBusBleAdvParams * param)601 int SoftBusUpdateAdv(int advId, const SoftBusBleAdvData *data, const SoftBusBleAdvParams *param)
602 {
603 if (param == NULL || data == NULL) {
604 return SOFTBUS_INVALID_PARAM;
605 }
606 if (!CheckAdvChannelInUsed(advId)) {
607 return SOFTBUS_ERR;
608 }
609 int ret = SoftBusStopAdv(advId);
610 if (ret != SOFTBUS_OK) {
611 return ret;
612 }
613 ret = SetAdvData(advId, data);
614 if (ret != SOFTBUS_OK) {
615 return ret;
616 }
617 return SoftBusStartAdv(advId, param);
618 }
619
SoftBusAddScanListener(const SoftBusScanListener * listener)620 int SoftBusAddScanListener(const SoftBusScanListener *listener)
621 {
622 if (listener == NULL) {
623 return SOFTBUS_ERR;
624 }
625 if (SoftBusMutexLock(&g_scanerLock) != 0) {
626 return SOFTBUS_LOCK_ERR;
627 }
628 if (RegisterBleGattCallback() != SOFTBUS_OK) {
629 SoftBusMutexUnlock(&g_scanerLock);
630 return SOFTBUS_ERR;
631 }
632 for (int index = 0; index < SCAN_MAX_NUM; index++) {
633 if (!g_scanListener[index].isUsed) {
634 g_scanListener[index].isUsed = true;
635 g_scanListener[index].isScanning = false;
636 (void)memset_s(&g_scanListener[index].param, sizeof(SoftBusBleScanParams),
637 0x0, sizeof(SoftBusBleScanParams));
638 g_scanListener[index].listener = (SoftBusScanListener *)listener;
639 SoftBusMutexUnlock(&g_scanerLock);
640 return index;
641 }
642 }
643 SoftBusMutexUnlock(&g_scanerLock);
644 return SOFTBUS_ERR;
645 }
646
SoftBusRemoveScanListener(int listenerId)647 int SoftBusRemoveScanListener(int listenerId)
648 {
649 if (listenerId < 0 || listenerId >= SCAN_MAX_NUM) {
650 return SOFTBUS_INVALID_PARAM;
651 }
652 if (SoftBusMutexLock(&g_scanerLock) != 0) {
653 return SOFTBUS_LOCK_ERR;
654 }
655 g_scanListener[listenerId].isUsed = false;
656 g_scanListener[listenerId].isScanning = false;
657 g_scanListener[listenerId].listener = NULL;
658 SoftBusMutexUnlock(&g_scanerLock);
659 return SOFTBUS_OK;
660 }
661
CheckNeedStartScan(void)662 static bool CheckNeedStartScan(void)
663 {
664 for (int listenerId = 0; listenerId < SCAN_MAX_NUM; listenerId++) {
665 if (g_scanListener[listenerId].isScanning) {
666 return false;
667 }
668 }
669 return true;
670 }
671
CheckNeedStopScan(int listenerId)672 static bool CheckNeedStopScan(int listenerId)
673 {
674 for (int index = 0; index < SCAN_MAX_NUM; index++) {
675 if (index == listenerId && !g_scanListener[index].isScanning) {
676 return false;
677 }
678 if (index != listenerId && g_scanListener[index].isScanning) {
679 return false;
680 }
681 }
682 return true;
683 }
684
SoftBusStartScan(int listenerId,const SoftBusBleScanParams * param)685 int SoftBusStartScan(int listenerId, const SoftBusBleScanParams *param)
686 {
687 if (param == NULL) {
688 return SOFTBUS_INVALID_PARAM;
689 }
690 if (SoftBusMutexLock(&g_scanerLock) != 0) {
691 return SOFTBUS_LOCK_ERR;
692 }
693 if (!g_scanListener[listenerId].isUsed) {
694 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "ScanListener id:%d is not in use", listenerId);
695 SoftBusMutexUnlock(&g_scanerLock);
696 return SOFTBUS_ERR;
697 }
698 int status = SOFTBUS_BT_STATUS_SUCCESS;
699 if (CheckNeedStartScan()) {
700 status = BleOhosStatusToSoftBus(BleStartScan());
701 }
702 if (status != SOFTBUS_BT_STATUS_SUCCESS) {
703 SoftBusMutexUnlock(&g_scanerLock);
704 return SOFTBUS_ERR;
705 }
706 g_scanListener[listenerId].isScanning = true;
707 if (g_scanListener[listenerId].listener != NULL &&
708 g_scanListener[listenerId].listener->OnScanStart != NULL) {
709 g_scanListener[listenerId].listener->OnScanStart(listenerId, SOFTBUS_BT_STATUS_SUCCESS);
710 }
711 SoftBusMutexUnlock(&g_scanerLock);
712 if (status == SOFTBUS_BT_STATUS_SUCCESS) {
713 return SOFTBUS_OK;
714 }
715 return SOFTBUS_ERR;
716 }
717
SoftBusStopScan(int listenerId)718 int SoftBusStopScan(int listenerId)
719 {
720 if (SoftBusMutexLock(&g_scanerLock) != 0) {
721 return SOFTBUS_LOCK_ERR;
722 }
723 if (!g_scanListener[listenerId].isUsed) {
724 SoftBusMutexUnlock(&g_scanerLock);
725 return SOFTBUS_ERR;
726 }
727 if (!g_scanListener[listenerId].isScanning) {
728 SoftBusMutexUnlock(&g_scanerLock);
729 return SOFTBUS_OK;
730 }
731 int status = SOFTBUS_BT_STATUS_SUCCESS;
732 if (CheckNeedStopScan(listenerId)) {
733 status = BleOhosStatusToSoftBus(BleStopScan());
734 }
735 if (status != SOFTBUS_BT_STATUS_SUCCESS) {
736 SoftBusMutexUnlock(&g_scanerLock);
737 return SOFTBUS_ERR;
738 }
739 g_scanListener[listenerId].isScanning = false;
740 if (g_scanListener[listenerId].listener != NULL &&
741 g_scanListener[listenerId].listener->OnScanStop != NULL) {
742 g_scanListener[listenerId].listener->OnScanStop(listenerId, status);
743 }
744 SoftBusMutexUnlock(&g_scanerLock);
745 if (status == SOFTBUS_BT_STATUS_SUCCESS) {
746 return SOFTBUS_OK;
747 }
748 return SOFTBUS_ERR;
749 }
750