!4851 [新需求]: GATT Adapter层面向接口适配,移除管理功能

Merge pull request !4851 from yuxpro/master
This commit is contained in:
openharmony_ci 2023-12-24 18:23:44 +00:00 committed by Gitee
commit 0d1a0ebdb8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 1334 additions and 3 deletions

View File

@ -25,13 +25,12 @@
#define SOFTBUS_BLE_GATT_H
#include "softbus_broadcast_adapter_interface.h"
#include "softbus_broadcast_adapter_type.h"
#ifdef __cplusplus
extern "C"{
#endif
void softbus_ble_adapter_init(void);
void SoftbusBleAdapterInit(void);
#ifdef __cplusplus
}

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file softbus_ble_utils.h
* @brief ble utils
*
* @since 4.1
* @version 1.0
*/
#ifndef SOFTBUS_BLE_UTILS_H
#define SOFTBUS_BLE_UTILS_H
#include "softbus_broadcast_adapter_interface.h"
#include "c_header/ohos_bt_gatt.h"
#include "c_header/ohos_bt_def.h"
#ifdef __cplusplus
extern "C"{
#endif
int32_t BtStatusToSoftBus(BtStatus btStatus);
void SoftbusAdvParamToBt(const SoftbusBroadcastParam *src, BleAdvParams *dst);
void BtScanResultToSoftbus(const BtScanResultData *src, SoftBusBcScanResult *dst);
void SoftbusFilterToBt(BleScanNativeFilter *nativeFilter, const SoftBusBcScanFilter *filter, uint8_t filterSize);
void FreeBtFilter(BleScanNativeFilter *nativeFilter, int32_t filterSize);
void DumpBleScanFilter(BleScanNativeFilter *nativeFilter, int32_t filterSize);
int GetBtScanMode(uint16_t scanInterval, uint16_t scanWindow);
uint8_t *AssembleAdvData(const SoftbusBroadcastData *data, uint16_t *dataLen);
uint8_t *AssembleRspData(const SoftbusBroadcastPayload *data, uint16_t *dataLen);
int32_t ParseScanResult(const uint8_t *advData, uint8_t advLen, SoftBusBcScanResult *dst);
void DumpSoftbusAdapterData(const char *description, uint8_t *data, uint16_t len);
#ifdef __cplusplus
}
#endif
#endif /* SOFTBUS_BLE_UTILS_H */

View File

@ -14,7 +14,804 @@
*/
#include "softbus_ble_gatt.h"
#include "softbus_adapter_thread.h"
#include "softbus_adapter_mem.h"
#include "softbus_broadcast_type.h"
#include "softbus_error_code.h"
#include "softbus_ble_utils.h"
#include "disc_log.h"
#include <stdatomic.h>
#include <string.h>
void softbus_ble_adapter_init(void)
#define GATT_ADV_MAX_NUM 16
#define GATT_SCAN_MAX_NUM 2
#define LP_BT_UUID "43d4a49f-604d-45b5-9302-4ddbbfd538fd"
#define LP_DELIVERY_MODE_REPLY 0xF0
#define LP_ADV_DURATION_MS 0
#define SCAN_CHANNEL_0 0
#define SCAN_CHANNEL_1 1
static atomic_bool g_init = false;
static atomic_bool g_bcCbReg = false;
static SoftBusMutex g_advLock = {0};
static SoftBusMutex g_scannerLock = {0};
typedef struct {
int32_t advId;
bool isUsed;
bool isAdvertising;
SoftbusBroadcastCallback *advCallback;
} AdvChannel;
typedef struct {
int32_t scannerId;
bool isUsed;
bool isScanning;
SoftbusScanCallback *scanCallback;
} ScanChannel;
static AdvChannel g_advChannel[GATT_ADV_MAX_NUM];
static ScanChannel g_scanChannel[GATT_SCAN_MAX_NUM];
static int32_t Init(void)
{
if (g_init) {
DISC_LOGI(DISC_BLE_ADAPTER, "already inited");
return SOFTBUS_OK;
}
g_init = true;
if (SoftBusMutexInit(&g_advLock, NULL) != SOFTBUS_OK) {
g_init = false;
DISC_LOGE(DISC_BLE_ADAPTER, "g_advLock init failed");
return SOFTBUS_ERR;
}
if (SoftBusMutexInit(&g_scannerLock, NULL) != SOFTBUS_OK) {
SoftBusMutexDestroy(&g_advLock);
g_init = false;
DISC_LOGE(DISC_BLE_ADAPTER, "g_scannerLock init failed");
return SOFTBUS_ERR;
}
DISC_LOGI(DISC_BLE_ADAPTER, "init success");
return SOFTBUS_OK;
}
static int32_t DeInit(void)
{
if (!g_init) {
DISC_LOGI(DISC_BLE_ADAPTER, "already deinited");
return SOFTBUS_OK;
}
g_init = false;
SoftBusMutexDestroy(&g_advLock);
SoftBusMutexDestroy(&g_scannerLock);
DISC_LOGI(DISC_BLE_ADAPTER, "deinit success");
return SOFTBUS_OK;
}
static void WrapperAdvEnableCallback(int advId, int status)
{
int32_t ret = BtStatusToSoftBus((BtStatus)status);
for (uint8_t channelId = 0; channelId < GATT_ADV_MAX_NUM; channelId++) {
if (SoftBusMutexLock(&g_advLock) != SOFTBUS_OK) {
DISC_LOGW(DISC_BLE_ADAPTER, "lock adv failed, advId: %u, bt-advId: %d", channelId, advId);
continue;
}
AdvChannel *advChannel = &g_advChannel[channelId];
if (advChannel->advId != advId || !advChannel->isUsed) {
SoftBusMutexUnlock(&g_advLock);
continue;
}
advChannel->isAdvertising = (ret == SOFTBUS_BC_STATUS_SUCCESS);
if (!advChannel->isAdvertising) {
advChannel->advId = -1;
}
DISC_LOGI(DISC_BLE_ADAPTER, "advId: %u, bt-advId: %d, status: %d", channelId, advId, ret);
SoftBusMutexUnlock(&g_advLock);
if (advChannel->advCallback != NULL && advChannel->advCallback->OnStartBroadcastingCallback != NULL) {
advChannel->advCallback->OnStartBroadcastingCallback(channelId, ret);
}
break;
}
}
static void WrapperAdvDisableCallback(int advId, int status)
{
int32_t ret = BtStatusToSoftBus((BtStatus)status);
for (uint8_t channelId = 0; channelId < GATT_ADV_MAX_NUM; channelId++) {
if (SoftBusMutexLock(&g_advLock) != SOFTBUS_OK) {
DISC_LOGW(DISC_BLE_ADAPTER, "lock adv failed, advId: %u, bt-advId: %d", channelId, advId);
continue;
}
AdvChannel *advChannel = &g_advChannel[channelId];
if (advChannel->advId != advId || !advChannel->isUsed) {
SoftBusMutexUnlock(&g_advLock);
continue;
}
advChannel->isAdvertising = false;
advChannel->advId = -1;
DISC_LOGI(DISC_BLE_ADAPTER, "advId: %u, bt-advId: %d, status: %d", channelId, advId, ret);
SoftBusMutexUnlock(&g_advLock);
if (advChannel->advCallback != NULL && advChannel->advCallback->OnStopBroadcastingCallback != NULL) {
advChannel->advCallback->OnStopBroadcastingCallback(channelId, ret);
}
break;
}
}
static void WrapperAdvSetDataCallback(int advId, int status)
{
int32_t ret = BtStatusToSoftBus((BtStatus)status);
for (uint32_t channelId = 0; channelId < GATT_ADV_MAX_NUM; channelId++) {
if (SoftBusMutexLock(&g_advLock) != SOFTBUS_OK) {
DISC_LOGW(DISC_BLE_ADAPTER, "lock adv failed, advId: %u, bt-advId: %d", channelId, advId);
continue;
}
AdvChannel *advChannel = &g_advChannel[channelId];
if (advChannel->advId != advId || !advChannel->isUsed) {
SoftBusMutexUnlock(&g_advLock);
continue;
}
DISC_LOGI(DISC_BLE_ADAPTER, "advId: %u, bt-advId: %d, status: %d", channelId, advId, ret);
SoftBusMutexUnlock(&g_advLock);
if (advChannel->advCallback != NULL && advChannel->advCallback->OnSetBroadcastingCallback != NULL) {
advChannel->advCallback->OnSetBroadcastingCallback(channelId, ret);
}
break;
}
}
static void WrapperAdvUpdateDataCallback(int advId, int status)
{
int32_t ret = BtStatusToSoftBus((BtStatus)status);
for (uint32_t channelId = 0; channelId < GATT_ADV_MAX_NUM; channelId++) {
if (SoftBusMutexLock(&g_advLock) != SOFTBUS_OK) {
DISC_LOGW(DISC_BLE_ADAPTER, "lock adv failed, advId: %u, bt-advId: %d", channelId, advId);
continue;
}
AdvChannel *advChannel = &g_advChannel[channelId];
if (advChannel->advId != advId || !advChannel->isUsed) {
SoftBusMutexUnlock(&g_advLock);
continue;
}
DISC_LOGI(DISC_BLE_ADAPTER, "advId: %u, bt-advId: %d, status: %d", channelId, advId, ret);
SoftBusMutexUnlock(&g_advLock);
if (advChannel->advCallback != NULL && advChannel->advCallback->OnUpdateBroadcastingCallback != NULL) {
advChannel->advCallback->OnUpdateBroadcastingCallback(channelId, ret);
}
break;
}
}
static BtGattCallbacks g_softbusGattCb = {
.advEnableCb = WrapperAdvEnableCallback,
.advDisableCb = WrapperAdvDisableCallback,
.advDataCb = WrapperAdvSetDataCallback,
.advUpdateCb = WrapperAdvUpdateDataCallback,
};
static int32_t RegisterAdvCallback(int32_t *advId, const SoftbusBroadcastCallback *cb)
{
if (advId == NULL || cb == NULL) {
DISC_LOGE(DISC_BLE_ADAPTER, "adv param is null");
return SOFTBUS_INVALID_PARAM;
}
if (SoftBusMutexLock(&g_advLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock failed");
return SOFTBUS_LOCK_ERR;
}
for (uint8_t channelId = 0; channelId < GATT_ADV_MAX_NUM; channelId++) {
if (g_advChannel[channelId].isUsed) {
continue;
}
if (!g_bcCbReg) {
if (BleGattRegisterCallbacks(&g_softbusGattCb) != OHOS_BT_STATUS_SUCCESS) {
DISC_LOGE(DISC_BLE_ADAPTER, "register failed, advId: %u", channelId);
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_ERR;
}
g_bcCbReg = true;
}
g_advChannel[channelId].advId = -1;
g_advChannel[channelId].isUsed = true;
g_advChannel[channelId].isAdvertising = false;
g_advChannel[channelId].advCallback = (SoftbusBroadcastCallback *)cb;
*advId = channelId;
DISC_LOGI(DISC_BLE_ADAPTER, "register success, advId: %u", channelId);
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_OK;
}
DISC_LOGE(DISC_BLE_ADAPTER, "no available adv channel");
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_ERR;
}
static int32_t UnRegisterAdvCallback(int32_t advId)
{
if (advId < 0 || advId >= GATT_ADV_MAX_NUM) {
DISC_LOGE(DISC_BLE_ADAPTER, "invalid advId: %d", advId);
return SOFTBUS_INVALID_PARAM;
}
if (SoftBusMutexLock(&g_advLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock failed, advId: %d", advId);
return SOFTBUS_LOCK_ERR;
}
if (!g_advChannel[advId].isUsed) {
DISC_LOGI(DISC_BLE_ADAPTER, "already unregistered, advId: %d, bt-advId: %d", advId, g_advChannel[advId].advId);
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_OK;
}
DISC_LOGI(DISC_BLE_ADAPTER, "advId: %d, bt-advId: %d", advId, g_advChannel[advId].advId);
g_advChannel[advId].advId = -1;
g_advChannel[advId].isUsed = false;
g_advChannel[advId].isAdvertising = false;
g_advChannel[advId].advCallback = NULL;
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_OK;
}
static void WrapperScanResultCallback(uint8_t channelId, BtScanResultData *data)
{
if (data == NULL) {
DISC_LOGE(DISC_BLE_ADAPTER, "scan result data is null, scannerId: %u", channelId);
return;
}
if (SoftBusMutexLock(&g_scannerLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock failed, scannerId: %u", channelId);
return;
}
ScanChannel *scanChannel = &g_scanChannel[channelId];
if (!scanChannel->isUsed || !scanChannel->isScanning) {
DISC_LOGE(DISC_BLE_ADAPTER, "scanner is not in used, scannerId: %u, bt-scannerId: %d",
channelId, scanChannel->scannerId);
SoftBusMutexUnlock(&g_scannerLock);
return;
}
SoftBusBcScanResult scanResult = {};
BtScanResultToSoftbus(data, &scanResult);
if (ParseScanResult(data->advData, data->advLen, &scanResult) != SOFTBUS_OK) {
SoftBusFree(scanResult.data.bcData.payload);
SoftBusFree(scanResult.data.rspData.payload);
SoftBusMutexUnlock(&g_scannerLock);
return;
}
SoftBusMutexUnlock(&g_scannerLock);
if (scanChannel->scanCallback != NULL && scanChannel->scanCallback->OnReportScanDataCallback != NULL) {
scanChannel->scanCallback->OnReportScanDataCallback(channelId, &scanResult);
}
SoftBusFree(scanResult.data.bcData.payload);
SoftBusFree(scanResult.data.rspData.payload);
}
static void WrapperScanStateChangeCallback(uint8_t channelId, int32_t resultCode, bool isStartScan)
{
if (SoftBusMutexLock(&g_scannerLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock failed, scannerId: %u", channelId);
return;
}
ScanChannel *scanChannel = &g_scanChannel[channelId];
if (!scanChannel->isUsed) {
DISC_LOGE(DISC_BLE_ADAPTER, "scanner is not in used, scannerId: %u, bt-scannerId: %d",
channelId, scanChannel->scannerId);
SoftBusMutexUnlock(&g_scannerLock);
return;
}
DISC_LOGI(DISC_BLE_ADAPTER, "scannerId: %d, bt-scannerId: %d, resultCode: %d, isStartScan: %d",
channelId, scanChannel->scannerId, resultCode, isStartScan);
SoftBusMutexUnlock(&g_scannerLock);
if (scanChannel->scanCallback != NULL && scanChannel->scanCallback->OnScanStateChanged != NULL) {
scanChannel->scanCallback->OnScanStateChanged(resultCode, isStartScan);
}
}
static void WrapperLpDeviceInfoCallback(uint8_t channelId, BtUuid *uuid, int32_t type, uint8_t *data, uint32_t dataSize)
{
if (SoftBusMutexLock(&g_scannerLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock failed, scannerId: %u", channelId);
return;
}
ScanChannel *scanChannel = &g_scanChannel[channelId];
if (!scanChannel->isUsed) {
DISC_LOGE(DISC_BLE_ADAPTER, "scanner is not in used, scannerId: %u, bt-scannerId: %d",
channelId, scanChannel->scannerId);
SoftBusMutexUnlock(&g_scannerLock);
return;
}
SoftBusMutexUnlock(&g_scannerLock);
if (scanChannel->scanCallback != NULL && scanChannel->scanCallback->OnLpDeviceInfoCallback != NULL) {
SoftbusBroadcastUuid bcUuid;
bcUuid.uuid = (uint8_t *)uuid->uuid;
bcUuid.uuidLen = (uint8_t)uuid->uuidLen;
scanChannel->scanCallback->OnLpDeviceInfoCallback(&bcUuid, type, data, dataSize);
}
}
static void WrapperScanResultCallback0(BtScanResultData *data)
{
WrapperScanResultCallback(SCAN_CHANNEL_0, data);
}
static void WrapperScanResultCallback1(BtScanResultData *data)
{
WrapperScanResultCallback(SCAN_CHANNEL_1, data);
}
static void WrapperScanStateChangeCallback0(int32_t resultCode, bool isStartScan)
{
WrapperScanStateChangeCallback(SCAN_CHANNEL_0, resultCode, isStartScan);
}
static void WrapperScanStateChangeCallback1(int32_t resultCode, bool isStartScan)
{
WrapperScanStateChangeCallback(SCAN_CHANNEL_1, resultCode, isStartScan);
}
static void WrapperLpDeviceInfoCallback0(BtUuid *uuid, int32_t type, uint8_t *data, uint32_t dataSize)
{
WrapperLpDeviceInfoCallback(SCAN_CHANNEL_0, uuid, type, data, dataSize);
}
static void WrapperLpDeviceInfoCallback1(BtUuid *uuid, int32_t type, uint8_t *data, uint32_t dataSize)
{
WrapperLpDeviceInfoCallback(SCAN_CHANNEL_1, uuid, type, data, dataSize);
}
static BleScanCallbacks g_softbusBleScanCb[GATT_SCAN_MAX_NUM] = {
{
.scanResultCb = WrapperScanResultCallback0,
.scanStateChangeCb = WrapperScanStateChangeCallback0,
.lpDeviceInfoCb = WrapperLpDeviceInfoCallback0,
},
{
.scanResultCb = WrapperScanResultCallback1,
.scanStateChangeCb = WrapperScanStateChangeCallback1,
.lpDeviceInfoCb = WrapperLpDeviceInfoCallback1,
}
};
static BleScanCallbacks *GetAdapterScanCallback(uint8_t channelId)
{
return &g_softbusBleScanCb[channelId];
}
static int32_t RegisterScanCallback(int32_t *scannerId, const SoftbusScanCallback *cb)
{
if (scannerId == NULL || cb == NULL) {
DISC_LOGE(DISC_BLE_ADAPTER, "scan param is null");
return SOFTBUS_ERR;
}
if (SoftBusMutexLock(&g_scannerLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock failed");
return SOFTBUS_LOCK_ERR;
}
for (uint8_t channelId = 0; channelId < GATT_SCAN_MAX_NUM; channelId++) {
if (g_scanChannel[channelId].isUsed) {
continue;
}
if (BleRegisterScanCallbacks(GetAdapterScanCallback(channelId),
&g_scanChannel[channelId].scannerId) != OHOS_BT_STATUS_SUCCESS) {
DISC_LOGE(DISC_BLE_ADAPTER, "register callback failed, scannerId: %u", channelId);
SoftBusMutexUnlock(&g_scannerLock);
return SOFTBUS_ERR;
}
g_scanChannel[channelId].isUsed = true;
g_scanChannel[channelId].isScanning = false;
g_scanChannel[channelId].scanCallback = (SoftbusScanCallback *)cb;
*scannerId = channelId;
DISC_LOGI(DISC_BLE_ADAPTER, "scannerId: %u, bt-scannerId: %d", channelId, g_scanChannel[channelId].scannerId);
SoftBusMutexUnlock(&g_scannerLock);
return SOFTBUS_OK;
}
DISC_LOGE(DISC_BLE_ADAPTER, "no available scan channel");
SoftBusMutexUnlock(&g_scannerLock);
return SOFTBUS_ERR;
}
static int32_t UnRegisterScanCallback(int32_t scannerId)
{
if (scannerId < 0 || scannerId >= GATT_SCAN_MAX_NUM) {
DISC_LOGE(DISC_BLE_ADAPTER, "scannerId is invalid: %d", scannerId);
return SOFTBUS_INVALID_PARAM;
}
if (SoftBusMutexLock(&g_scannerLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock failed, scannerId: %u", scannerId);
return SOFTBUS_LOCK_ERR;
}
if (!g_scanChannel[scannerId].isUsed) {
DISC_LOGI(DISC_BLE_ADAPTER, "already unregistered, scannerId: %d, bt-scannerId: %d",
scannerId, g_scanChannel[scannerId].scannerId);
SoftBusMutexUnlock(&g_scannerLock);
return SOFTBUS_OK;
}
int32_t ret = BleDeregisterScanCallbacks(g_scanChannel[scannerId].scannerId);
DISC_LOGI(DISC_BLE_ADAPTER, "scannerId: %d, bt-scannerId: %d, result: %d",
scannerId, g_scanChannel[scannerId].scannerId, ret);
g_scanChannel[scannerId].scannerId = -1;
g_scanChannel[scannerId].isUsed = false;
g_scanChannel[scannerId].isScanning = false;
g_scanChannel[scannerId].scanCallback = NULL;
SoftBusMutexUnlock(&g_scannerLock);
return SOFTBUS_OK;
}
static bool CheckAdvChannelInUsed(int32_t advId)
{
if (advId < 0 || advId >= GATT_ADV_MAX_NUM) {
DISC_LOGE(DISC_BLE_ADAPTER, "invalid advId: %d", advId);
return false;
}
if (!g_advChannel[advId].isUsed) {
DISC_LOGE(DISC_BLE_ADAPTER, "advId: %d, bt-advId: %d", advId, g_advChannel[advId].advId);
return false;
}
return true;
}
static int32_t StartBleAdv(int32_t advId, const SoftbusBroadcastParam *param, const SoftbusBroadcastData *data)
{
BleAdvParams advParam = {};
SoftbusAdvParamToBt(param, &advParam);
StartAdvRawData advRawData = {};
advRawData.advData = (unsigned char *)AssembleAdvData(data, (uint16_t *)&advRawData.advDataLen);
if (advRawData.advData == NULL) {
DISC_LOGE(DISC_BLE_ADAPTER, "assemble adv data failed, advId: %d, bt-advId: %d",
advId, g_advChannel[advId].advId);
return SOFTBUS_ERR;
}
advRawData.rspDataLen = 0;
advRawData.rspData = NULL;
if (data->rspData.payloadLen > 0 && data->rspData.payload != NULL) {
advRawData.rspData = (unsigned char *)AssembleRspData(&data->rspData, (uint16_t *)&advRawData.rspDataLen);
if (advRawData.rspData == NULL) {
SoftBusFree(advRawData.advData);
DISC_LOGE(DISC_BLE_ADAPTER, "assemble rsp data failed, advId: %d, bt-advId: %d",
advId, g_advChannel[advId].advId);
return SOFTBUS_ERR;
}
}
DumpSoftbusAdapterData("mgr pkg:", advRawData.advData, advRawData.advDataLen);
int32_t ret = BleStartAdvEx(&g_advChannel[advId].advId, advRawData, advParam);
SoftBusFree(advRawData.advData);
SoftBusFree(advRawData.rspData);
return (ret == OHOS_BT_STATUS_SUCCESS) ? SOFTBUS_OK : SOFTBUS_ERR;
}
static int32_t StartAdv(int32_t advId, const SoftbusBroadcastParam *param, const SoftbusBroadcastData *data)
{
if (param == NULL || data == NULL || data->bcData.payloadLen == 0 || data->bcData.payload == NULL) {
DISC_LOGE(DISC_BLE_ADAPTER, "invalid adv param, advId: %d", advId);
return SOFTBUS_INVALID_PARAM;
}
if (SoftBusMutexLock(&g_advLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock failed, advId: %d", advId);
return SOFTBUS_LOCK_ERR;
}
if (!CheckAdvChannelInUsed(advId)) {
DISC_LOGE(DISC_BLE_ADAPTER, "adv is not in used, advId: %d", advId);
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_ERR;
}
if (g_advChannel[advId].isAdvertising) {
DISC_LOGE(DISC_BLE_ADAPTER, "already started, advId: %d, bt-advId: %d", advId, g_advChannel[advId].advId);
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_ERR;
}
int32_t ret = StartBleAdv(advId, param, data);
g_advChannel[advId].isAdvertising = (ret == SOFTBUS_OK);
DISC_LOGI(DISC_BLE_ADAPTER, "advId: %d, bt-advId: %d, ret: %d", advId, g_advChannel[advId].advId, ret);
SoftBusMutexUnlock(&g_advLock);
if (ret != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
}
static int32_t StopAdv(int32_t advId)
{
if (SoftBusMutexLock(&g_advLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock adv failed, advId: %d", advId);
return SOFTBUS_LOCK_ERR;
}
if (!CheckAdvChannelInUsed(advId)) {
DISC_LOGE(DISC_BLE_ADAPTER, "adv is not in used, advId: %d", advId);
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_ERR;
}
if (!g_advChannel[advId].isAdvertising) {
DISC_LOGI(DISC_BLE_ADAPTER, "already stopped, advId: %d, bt-advId: %d", advId, g_advChannel[advId].advId);
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_OK;
}
int32_t ret = BleStopAdv(g_advChannel[advId].advId);
DISC_LOGI(DISC_BLE_ADAPTER, "advId: %d, bt-advId: %d, ret: %d", advId, g_advChannel[advId].advId, ret);
g_advChannel[advId].isAdvertising = false;
SoftBusMutexUnlock(&g_advLock);
if (ret != OHOS_BT_STATUS_SUCCESS) {
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
}
static int32_t SetAdvData(int32_t advId, const SoftbusBroadcastData *data)
{
if (data == NULL) {
DISC_LOGE(DISC_BLE_ADAPTER, "data is null, advId: %d", advId);
return SOFTBUS_INVALID_PARAM;
}
if (SoftBusMutexLock(&g_advLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock adv failed, advId: %d", advId);
return SOFTBUS_LOCK_ERR;
}
if (!CheckAdvChannelInUsed(advId)) {
DISC_LOGE(DISC_BLE_ADAPTER, "adv is not in used, advId: %d", advId);
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_ERR;
}
if (!g_advChannel[advId].isAdvertising) {
DISC_LOGE(DISC_BLE_ADAPTER, "adv is not advertising, advId: %d, bt-advId: %d",
advId, g_advChannel[advId].advId);
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_ERR;
}
StartAdvRawData advRawData = {};
advRawData.advData = (unsigned char *)AssembleAdvData(data, (uint16_t *)&advRawData.advDataLen);
if (advRawData.advData == NULL) {
DISC_LOGE(DISC_BLE_ADAPTER, "assemble adv data failed, advId: %d, bt-advId: %d",
advId, g_advChannel[advId].advId);
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_ERR;
}
advRawData.rspDataLen = 0;
advRawData.rspData = NULL;
if (data->rspData.payloadLen > 0 && data->rspData.payload != NULL) {
advRawData.rspData = (unsigned char *)AssembleRspData(&data->rspData, (uint16_t *)&advRawData.rspDataLen);
if (advRawData.rspData == NULL) {
SoftBusFree(advRawData.advData);
DISC_LOGE(DISC_BLE_ADAPTER, "assemble rsp data failed, advId: %d, bt-advId: %d",
advId, g_advChannel[advId].advId);
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_ERR;
}
}
int32_t ret = BtStatusToSoftBus(BleSetAdvData(g_advChannel[advId].advId, advRawData));
DISC_LOGI(DISC_BLE_ADAPTER, "advId: %d, bt-advId: %d, ret: %d", advId, g_advChannel[advId].advId, ret);
SoftBusFree(advRawData.advData);
SoftBusFree(advRawData.rspData);
SoftBusMutexUnlock(&g_advLock);
return ret;
}
static int32_t UpdateAdvData(int32_t advId, const SoftbusBroadcastParam *param, const SoftbusBroadcastData *data)
{
if (StopAdv(advId) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "update adv data failed, advId: %d", advId);
return SOFTBUS_ERR;
}
DISC_LOGI(DISC_BLE_ADAPTER, "update adv data, advId: %d", advId);
return StartAdv(advId, param, data);
}
static bool CheckScanChannelInUsed(int32_t scannerId)
{
if (scannerId < 0 || scannerId >= GATT_SCAN_MAX_NUM) {
DISC_LOGE(DISC_BLE_ADAPTER, "invalid scannerId: %d", scannerId);
return false;
}
if (!g_scanChannel[scannerId].isUsed) {
DISC_LOGE(DISC_BLE_ADAPTER, "scannerId: %d, bt-scannerId: %d", scannerId, g_scanChannel[scannerId].scannerId);
return false;
}
return true;
}
static int32_t StartScan(int32_t scannerId, const SoftBusBcScanParams *param, const SoftBusBcScanFilter *scanFilter,
int32_t filterSize)
{
if (param == NULL || scanFilter == NULL || filterSize <= 0) {
DISC_LOGE(DISC_BLE_ADAPTER, "invalid param, scannerId: %d", scannerId);
return SOFTBUS_INVALID_PARAM;
}
if (SoftBusMutexLock(&g_scannerLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock failed, scannerId: %d", scannerId);
return SOFTBUS_LOCK_ERR;
}
if (!CheckScanChannelInUsed(scannerId)) {
DISC_LOGE(DISC_BLE_ADAPTER, "scanner is not in used, scannerId: %d", scannerId);
SoftBusMutexUnlock(&g_scannerLock);
return SOFTBUS_ERR;
}
if (g_scanChannel[scannerId].isScanning) {
DISC_LOGE(DISC_BLE_ADAPTER, "already scanning, scannerId: %d, bt-scannerId: %d",
scannerId, g_scanChannel[scannerId].scannerId);
SoftBusMutexUnlock(&g_scannerLock);
return SOFTBUS_ERR;
}
BleScanNativeFilter *nativeFilter =
(BleScanNativeFilter *)SoftBusCalloc(sizeof(BleScanNativeFilter) * filterSize);
if (nativeFilter == NULL) {
DISC_LOGE(DISC_BLE_ADAPTER, "malloc native filter failed, scannerId: %d, bt-scannerId: %d",
scannerId, g_scanChannel[scannerId].scannerId);
SoftBusMutexUnlock(&g_scannerLock);
return SOFTBUS_MALLOC_ERR;
}
SoftbusFilterToBt(nativeFilter, scanFilter, filterSize);
DumpBleScanFilter(nativeFilter, filterSize);
BleScanConfigs scanConfig = {};
scanConfig.scanMode = GetBtScanMode(param->scanInterval, param->scanWindow);
scanConfig.phy = (int)param->scanPhy;
int32_t ret = BleStartScanEx(g_scanChannel[scannerId].scannerId, &scanConfig, nativeFilter, (uint32_t)filterSize);
g_scanChannel[scannerId].isScanning = (ret == OHOS_BT_STATUS_SUCCESS);
DISC_LOGI(DISC_BLE_ADAPTER, "scannerId: %d, bt-scannerId: %d, ret: %d",
scannerId, g_scanChannel[scannerId].scannerId, ret);
FreeBtFilter(nativeFilter, filterSize);
SoftBusMutexUnlock(&g_scannerLock);
if (ret != OHOS_BT_STATUS_SUCCESS) {
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
}
static int32_t StopScan(int32_t scannerId)
{
if (SoftBusMutexLock(&g_scannerLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock failed, scannerId: %d", scannerId);
return SOFTBUS_LOCK_ERR;
}
if (!CheckScanChannelInUsed(scannerId)) {
DISC_LOGE(DISC_BLE_ADAPTER, "scanner is not in used: %d", scannerId);
SoftBusMutexUnlock(&g_scannerLock);
return SOFTBUS_ERR;
}
if (!g_scanChannel[scannerId].isScanning) {
DISC_LOGI(DISC_BLE_ADAPTER, "already stopped, scannerId: %d, bt-scannerId: %d",
scannerId, g_scanChannel[scannerId].scannerId);
SoftBusMutexUnlock(&g_scannerLock);
return SOFTBUS_OK;
}
int32_t ret = BleStopScan(g_scanChannel[scannerId].scannerId);
DISC_LOGI(DISC_BLE_ADAPTER, "stop scan, scannerId: %d, bt-scannerId: %d, ret: %d",
scannerId, g_scanChannel[scannerId].scannerId, ret);
g_scanChannel[scannerId].isScanning = false;
SoftBusMutexUnlock(&g_scannerLock);
if (ret != OHOS_BT_STATUS_SUCCESS) {
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
}
static bool IsLpAvailable(void)
{
DISC_LOGI(DISC_BLE_ADAPTER, "is lp available");
return IsLpDeviceAvailable();
}
static bool SetLpParam(const SoftBusLpBroadcastParam *bcParam, const SoftBusLpScanParam *scanParam)
{
BleScanConfigs scanConfig = {};
scanConfig.scanMode = GetBtScanMode(scanParam->scanParam.scanInterval, scanParam->scanParam.scanWindow);
BtLpDeviceParam lpParam = {};
lpParam.scanConfig = &scanConfig;
lpParam.rawData.advData = (unsigned char *)AssembleAdvData(&bcParam->advData,
(uint16_t *)&lpParam.rawData.advDataLen);
if (lpParam.rawData.advData == NULL) {
DISC_LOGE(DISC_BLE_ADAPTER, "assemble adv data failed, advHandle: %d", bcParam->advHandle);
return SOFTBUS_ERR;
}
if (bcParam->advData.rspData.payloadLen > 0 && bcParam->advData.rspData.payload != NULL) {
lpParam.rawData.rspData = (unsigned char *)AssembleRspData(&bcParam->advData.rspData,
(uint16_t *)&lpParam.rawData.rspDataLen);
if (lpParam.rawData.rspData == NULL) {
SoftBusFree(lpParam.rawData.advData);
DISC_LOGE(DISC_BLE_ADAPTER, "assemble rsp data failed, advHandle: %d", bcParam->advHandle);
return SOFTBUS_ERR;
}
}
lpParam.filter = (BleScanNativeFilter *)SoftBusCalloc(sizeof(BleScanNativeFilter) * scanParam->filterSize);
if (lpParam.filter == NULL) {
SoftBusFree(lpParam.rawData.advData);
SoftBusFree(lpParam.rawData.rspData);
DISC_LOGE(DISC_BLE_ADAPTER, "malloc native filter failed, advHandle: %d", bcParam->advHandle);
return SOFTBUS_MALLOC_ERR;
}
SoftbusFilterToBt(lpParam.filter, scanParam->filter, scanParam->filterSize);
lpParam.filterSize = (unsigned int)scanParam->filterSize;
SoftbusAdvParamToBt(&bcParam->advParam, &lpParam.advParam);
BtUuid btUuid = {};
btUuid.uuid = LP_BT_UUID;
btUuid.uuidLen = (unsigned char)strlen(LP_BT_UUID);
lpParam.uuid = btUuid;
lpParam.activeDeviceInfo = NULL;
lpParam.activeDeviceSize = 0;
lpParam.deliveryMode = LP_DELIVERY_MODE_REPLY;
lpParam.advHandle = bcParam->advHandle;
lpParam.duration = LP_ADV_DURATION_MS;
int32_t ret = SetLpDeviceParam(&lpParam);
FreeBtFilter(lpParam.filter, scanParam->filterSize);
SoftBusFree(lpParam.rawData.advData);
SoftBusFree(lpParam.rawData.rspData);
DISC_LOGI(DISC_BLE_ADAPTER, "advHandle: %d, ret: %d", bcParam->advHandle, ret);
return (ret == OHOS_BT_STATUS_SUCCESS) ? true : false;
}
static int32_t GetBroadcastHandle(int32_t advId, int32_t *bcHandle)
{
if (SoftBusMutexLock(&g_advLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock adv failed, advId: %d", advId);
return SOFTBUS_LOCK_ERR;
}
if (!CheckAdvChannelInUsed(advId)) {
DISC_LOGE(DISC_BLE_ADAPTER, "adv is not in used: %d", advId);
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_ERR;
}
int32_t ret = GetAdvHandle(g_advChannel[advId].advId, bcHandle);
DISC_LOGI(DISC_BLE_ADAPTER, "advId: %d, bt-advId: %d, ret: %d", advId, g_advChannel[advId].advId, ret);
SoftBusMutexUnlock(&g_advLock);
return ret;
}
static int32_t EnableSyncDataToLp(void)
{
DISC_LOGI(DISC_BLE_ADAPTER, "enable sync data to lp");
return EnableSyncDataToLpDevice();
}
static int32_t DisableSyncDataToLp(void)
{
DISC_LOGI(DISC_BLE_ADAPTER, "disable sync data to lp");
return DisableSyncDataToLpDevice();
}
static int32_t SetScanReportChannelToLp(int32_t scannerId, bool enable)
{
if (SoftBusMutexLock(&g_scannerLock) != SOFTBUS_OK) {
DISC_LOGE(DISC_BLE_ADAPTER, "lock failed, scannerId: %d", scannerId);
return SOFTBUS_LOCK_ERR;
}
if (!CheckScanChannelInUsed(scannerId)) {
DISC_LOGE(DISC_BLE_ADAPTER, "scanner is not in used: %d", scannerId);
SoftBusMutexUnlock(&g_scannerLock);
return SOFTBUS_ERR;
}
int32_t ret = SetScanReportChannelToLpDevice(g_scanChannel[scannerId].scannerId, enable);
DISC_LOGI(DISC_BLE_ADAPTER, "scannerId: %d, bt-scannerId: %d, ret: %d",
scannerId, g_scanChannel[scannerId].scannerId, ret);
SoftBusMutexUnlock(&g_scannerLock);
return ret;
}
static int32_t SetLpAdvParam(int32_t duration, int32_t maxExtAdvEvents, int32_t window,
int32_t interval, int32_t bcHandle)
{
DISC_LOGI(DISC_BLE_ADAPTER, "advHandle: %d", bcHandle);
return SetLpDeviceAdvParam(duration, maxExtAdvEvents, window, interval, bcHandle);
}
void SoftbusBleAdapterInit(void)
{
DISC_LOGI(DISC_BLE_ADAPTER, "SoftbusBleAdapterInit");
static SoftbusBroadcastMediumInterface interface = {
.Init = Init,
.DeInit = DeInit,
.RegisterBroadcaster = RegisterAdvCallback,
.UnRegisterBroadcaster = UnRegisterAdvCallback,
.RegisterScanListener = RegisterScanCallback,
.UnRegisterScanListener = UnRegisterScanCallback,
.StartBroadcasting = StartAdv,
.StopBroadcasting = StopAdv,
.SetBroadcastingData = SetAdvData,
.UpdateBroadcasting = UpdateAdvData,
.StartScan = StartScan,
.StopScan = StopScan,
.IsLpDeviceAvailable = IsLpAvailable,
.SetAdvFilterParam = SetLpParam,
.GetBroadcastHandle = GetBroadcastHandle,
.EnableSyncDataToLpDevice = EnableSyncDataToLp,
.DisableSyncDataToLpDevice = DisableSyncDataToLp,
.SetScanReportChannelToLpDevice = SetScanReportChannelToLp,
.SetLpDeviceParam = SetLpAdvParam,
};
if (RegisterBroadcastMediumFunction(BROADCAST_MEDIUM_TYPE_BLE, &interface) != 0) {
DISC_LOGE(DISC_BLE_ADAPTER, "Register gatt interface failed.");
}
}

View File

@ -0,0 +1,474 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "softbus_ble_utils.h"
#include "softbus_adapter_mem.h"
#include "softbus_broadcast_type.h"
#include "softbus_error_code.h"
#include "softbus_utils.h"
#include "softbus_broadcast_utils.h"
#include "disc_log.h"
#include <securec.h>
#define UUID_LEN 2
#define UUID_MASK_LEN 2
#define ID_LEN 2
int32_t BtStatusToSoftBus(BtStatus btStatus)
{
switch (btStatus) {
case OHOS_BT_STATUS_SUCCESS:
return SOFTBUS_BC_STATUS_SUCCESS;
case OHOS_BT_STATUS_FAIL:
return SOFTBUS_BC_STATUS_FAIL;
case OHOS_BT_STATUS_NOT_READY:
return SOFTBUS_BC_STATUS_NOT_READY;
case OHOS_BT_STATUS_NOMEM:
return SOFTBUS_BC_STATUS_NOMEM;
case OHOS_BT_STATUS_BUSY:
return SOFTBUS_BC_STATUS_BUSY;
case OHOS_BT_STATUS_DONE:
return SOFTBUS_BC_STATUS_DONE;
case OHOS_BT_STATUS_UNSUPPORTED:
return SOFTBUS_BC_STATUS_UNSUPPORTED;
case OHOS_BT_STATUS_PARM_INVALID:
return SOFTBUS_BC_STATUS_PARM_INVALID;
case OHOS_BT_STATUS_UNHANDLED:
return SOFTBUS_BC_STATUS_UNHANDLED;
case OHOS_BT_STATUS_AUTH_FAILURE:
return SOFTBUS_BC_STATUS_AUTH_FAILURE;
case OHOS_BT_STATUS_RMT_DEV_DOWN:
return SOFTBUS_BC_STATUS_RMT_DEV_DOWN;
case OHOS_BT_STATUS_AUTH_REJECTED:
return SOFTBUS_BC_STATUS_AUTH_REJECTED;
default:
return SOFTBUS_BC_STATUS_FAIL;
}
}
static uint16_t SoftbusAdvDataTypeToBt(uint16_t advType)
{
switch (advType) {
case BC_DATA_TYPE_SERVICE:
return SERVICE_BC_TYPE;
case BC_DATA_TYPE_MANUFACTURER:
return MANUFACTURE_BC_TYPE;
default:
return 0x00;
}
}
static uint16_t BtAdvTypeToSoftbus(uint16_t advType)
{
switch (advType) {
case SERVICE_BC_TYPE:
return BC_DATA_TYPE_SERVICE;
case MANUFACTURE_BC_TYPE:
return BC_DATA_TYPE_MANUFACTURER;
default:
return 0x00;
}
}
static BleAdvFilter SoftbusAdvFilterToBt(uint8_t advFilter)
{
switch (advFilter) {
case SOFTBUS_BC_ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY:
return OHOS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
case SOFTBUS_BC_ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY:
return OHOS_BLE_ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY;
case SOFTBUS_BC_ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST:
return OHOS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST;
case SOFTBUS_BC_ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST:
return OHOS_BLE_ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST;
default:
return OHOS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
}
}
static BleAdvType SoftbusAdvTypeToBt(uint8_t advType)
{
switch (advType) {
case SOFTBUS_BC_ADV_IND:
return OHOS_BLE_ADV_IND;
case SOFTBUS_BC_ADV_DIRECT_IND_HIGH:
return OHOS_BLE_ADV_DIRECT_IND_HIGH;
case SOFTBUS_BC_ADV_SCAN_IND:
return OHOS_BLE_ADV_SCAN_IND;
case SOFTBUS_BC_ADV_NONCONN_IND:
return OHOS_BLE_ADV_NONCONN_IND;
case SOFTBUS_BC_ADV_DIRECT_IND_LOW:
return OHOS_BLE_ADV_DIRECT_IND_LOW;
default:
return OHOS_BLE_ADV_IND;
}
}
void SoftbusAdvParamToBt(const SoftbusBroadcastParam *src, BleAdvParams *dst)
{
if (memcpy_s(dst->peerAddr.addr, SOFTBUS_ADDR_MAC_LEN, src->peerAddr.addr, SOFTBUS_ADDR_MAC_LEN) != EOK) {
DISC_LOGW(DISC_BLE_ADAPTER, "copy peer addr failed");
}
dst->minInterval = src->minInterval;
dst->maxInterval = src->maxInterval;
dst->advType = SoftbusAdvTypeToBt(src->advType);
dst->ownAddrType = (unsigned char)src->ownAddrType;
dst->peerAddrType = (unsigned char)src->peerAddrType;
dst->channelMap = src->channelMap;
dst->advFilterPolicy = SoftbusAdvFilterToBt(src->advFilterPolicy);
dst->txPower = src->txPower;
dst->duration = src->duration;
}
static uint8_t BtScanEventTypeToSoftbus(unsigned char eventType)
{
switch (eventType) {
case OHOS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE:
return SOFTBUS_BC_EVT_NON_CONNECTABLE_NON_SCANNABLE;
case OHOS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE_DIRECTED:
return SOFTBUS_BC_EVT_NON_CONNECTABLE_NON_SCANNABLE_DIRECTED;
case OHOS_BLE_EVT_CONNECTABLE:
return SOFTBUS_BC_EVT_CONNECTABLE;
case OHOS_BLE_EVT_CONNECTABLE_DIRECTED:
return SOFTBUS_BC_EVT_CONNECTABLE_DIRECTED;
case OHOS_BLE_EVT_SCANNABLE:
return SOFTBUS_BC_EVT_SCANNABLE;
case OHOS_BLE_EVT_SCANNABLE_DIRECTED:
return SOFTBUS_BC_EVT_SCANNABLE_DIRECTED;
case OHOS_BLE_EVT_LEGACY_NON_CONNECTABLE:
return SOFTBUS_BC_EVT_LEGACY_NON_CONNECTABLE;
case OHOS_BLE_EVT_LEGACY_SCANNABLE:
return SOFTBUS_BC_EVT_LEGACY_SCANNABLE;
case OHOS_BLE_EVT_LEGACY_CONNECTABLE:
return SOFTBUS_BC_EVT_LEGACY_CONNECTABLE;
case OHOS_BLE_EVT_LEGACY_CONNECTABLE_DIRECTED:
return SOFTBUS_BC_EVT_LEGACY_CONNECTABLE_DIRECTED;
case OHOS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV_SCAN:
return SOFTBUS_BC_EVT_LEGACY_SCAN_RSP_TO_ADV_SCAN;
case OHOS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV:
return SOFTBUS_BC_EVT_LEGACY_SCAN_RSP_TO_ADV;
default:
return SOFTBUS_BC_EVT_NON_CONNECTABLE_NON_SCANNABLE;
}
}
static uint8_t BtScanDataStatusToSoftbus(unsigned char dataStatus)
{
switch (dataStatus) {
case OHOS_BLE_DATA_COMPLETE:
return SOFTBUS_BC_DATA_COMPLETE;
case OHOS_BLE_DATA_INCOMPLETE_MORE_TO_COME:
return SOFTBUS_BC_DATA_INCOMPLETE_MORE_TO_COME;
case OHOS_BLE_DATA_INCOMPLETE_TRUNCATED:
return SOFTBUS_BC_DATA_INCOMPLETE_TRUNCATED;
default:
return SOFTBUS_BC_DATA_INCOMPLETE_TRUNCATED;
}
}
static uint8_t BtScanAddrTypeToSoftbus(unsigned char addrType)
{
switch (addrType) {
case OHOS_BLE_PUBLIC_DEVICE_ADDRESS:
return SOFTBUS_BC_PUBLIC_DEVICE_ADDRESS;
case OHOS_BLE_RANDOM_DEVICE_ADDRESS:
return SOFTBUS_BC_RANDOM_DEVICE_ADDRESS;
case OHOS_BLE_PUBLIC_IDENTITY_ADDRESS:
return SOFTBUS_BC_PUBLIC_IDENTITY_ADDRESS;
case OHOS_BLE_RANDOM_STATIC_IDENTITY_ADDRESS:
return SOFTBUS_BC_RANDOM_STATIC_IDENTITY_ADDRESS;
case OHOS_BLE_UNRESOLVABLE_RANDOM_DEVICE_ADDRESS:
return SOFTBUS_BC_UNRESOLVABLE_RANDOM_DEVICE_ADDRESS;
case OHOS_BLE_NO_ADDRESS:
return SOFTBUS_BC_NO_ADDRESS;
default:
return SOFTBUS_BC_NO_ADDRESS;
}
}
static uint8_t BtScanPhyTypeToSoftbus(unsigned char phyType)
{
switch (phyType) {
case OHOS_BLE_SCAN_PHY_NO_PACKET:
return SOFTBUS_BC_SCAN_PHY_NO_PACKET;
case OHOS_BLE_SCAN_PHY_1M:
return SOFTBUS_BC_SCAN_PHY_1M;
case OHOS_BLE_SCAN_PHY_2M:
return SOFTBUS_BC_SCAN_PHY_2M;
case OHOS_BLE_SCAN_PHY_CODED:
return SOFTBUS_BC_SCAN_PHY_CODED;
default:
return SOFTBUS_BC_SCAN_PHY_NO_PACKET;
}
}
void BtScanResultToSoftbus(const BtScanResultData *src, SoftBusBcScanResult *dst)
{
dst->eventType = BtScanEventTypeToSoftbus(src->eventType);
dst->dataStatus = BtScanDataStatusToSoftbus(src->dataStatus);
dst->addrType = BtScanAddrTypeToSoftbus(src->addrType);
if (memcpy_s(dst->addr.addr, SOFTBUS_ADDR_MAC_LEN, src->addr.addr, SOFTBUS_ADDR_MAC_LEN) != EOK) {
DISC_LOGW(DISC_BLE_ADAPTER, "copy addr failed");
}
dst->primaryPhy = BtScanPhyTypeToSoftbus(src->primaryPhy);
dst->secondaryPhy = BtScanPhyTypeToSoftbus(src->secondaryPhy);
dst->advSid = (uint8_t)src->advSid;
dst->txPower = (int8_t)src->txPower;
dst->rssi = (int8_t)src->rssi;
}
void SoftbusFilterToBt(BleScanNativeFilter *nativeFilter, const SoftBusBcScanFilter *filter, uint8_t filterSize)
{
while (filterSize-- > 0) {
(nativeFilter + filterSize)->address = (char *)(filter + filterSize)->address;
(nativeFilter + filterSize)->deviceName = (char *)(filter + filterSize)->deviceName;
(nativeFilter + filterSize)->manufactureData = (unsigned char *)(filter + filterSize)->manufactureData;
(nativeFilter + filterSize)->manufactureDataLength =
(unsigned int)(filter + filterSize)->manufactureDataLength;
(nativeFilter + filterSize)->manufactureDataMask = (unsigned char *)(filter + filterSize)->manufactureDataMask;
(nativeFilter + filterSize)->manufactureId = (unsigned short)(filter + filterSize)->manufactureId;
if ((filter + filterSize)->serviceData == NULL || (filter + filterSize)->serviceDataMask == NULL) {
continue;
}
// serviceData = uuid + serviceData, serviceDataMask = 0xFFFF + serviceDataMask
uint16_t serviceUuid = (filter + filterSize)->serviceUuid;
uint16_t serviceDataLen = (filter + filterSize)->serviceDataLength + 2;
uint8_t *serviceData = (uint8_t *)SoftBusCalloc(serviceDataLen);
if (serviceData == NULL) {
DISC_LOGW(DISC_BLE_ADAPTER, "malloc service data failed");
continue;
}
serviceData[0] = serviceUuid & BC_BYTE_MASK;
serviceData[1] = (serviceUuid >> BC_SHIFT_BIT) & BC_BYTE_MASK;
if (memcpy_s(serviceData + UUID_LEN, serviceDataLen - UUID_LEN, (filter + filterSize)->serviceData,
serviceDataLen - UUID_LEN) != EOK) {
DISC_LOGW(DISC_BLE_ADAPTER, "copy service data failed");
}
uint8_t *serviceDataMask = (uint8_t *)SoftBusCalloc(serviceDataLen);
if (serviceDataMask == NULL) {
SoftBusFree(serviceData);
DISC_LOGW(DISC_BLE_ADAPTER, "malloc service data mask failed");
continue;
}
serviceDataMask[0] = BC_BYTE_MASK;
serviceDataMask[1] = BC_BYTE_MASK;
if (memcpy_s(serviceDataMask + UUID_MASK_LEN, serviceDataLen - UUID_MASK_LEN,
(filter + filterSize)->serviceDataMask, serviceDataLen - UUID_MASK_LEN) != EOK) {
DISC_LOGW(DISC_BLE_ADAPTER, "copy service data mask failed");
}
(nativeFilter + filterSize)->serviceData = (unsigned char *)serviceData;
(nativeFilter + filterSize)->serviceDataLength = (unsigned int)serviceDataLen;
(nativeFilter + filterSize)->serviceDataMask = (unsigned char *)serviceDataMask;
}
}
void FreeBtFilter(BleScanNativeFilter *nativeFilter, int32_t filterSize)
{
while (filterSize-- > 0) {
SoftBusFree((nativeFilter + filterSize)->serviceData);
SoftBusFree((nativeFilter + filterSize)->serviceDataMask);
}
SoftBusFree(nativeFilter);
}
void DumpBleScanFilter(BleScanNativeFilter *nativeFilter, int32_t filterSize)
{
while (filterSize-- > 0) {
uint32_t len = (nativeFilter + filterSize)->serviceDataLength;
if (len == 0) {
continue;
}
uint32_t hexLen = HEXIFY_LEN(len);
char *serviceData = (char *)SoftBusCalloc(sizeof(char) * hexLen);
if (serviceData == NULL) {
continue;
}
char *serviceDataMask = (char *)SoftBusCalloc(sizeof(char) * hexLen);
if (serviceDataMask == NULL) {
SoftBusFree(serviceData);
continue;
}
(void)ConvertBytesToHexString(serviceData, hexLen, (nativeFilter + filterSize)->serviceData, len);
(void)ConvertBytesToHexString(serviceDataMask, hexLen, (nativeFilter + filterSize)->serviceDataMask, len);
DISC_LOGI(DISC_BLE_ADAPTER, "BLE Scan Filter size:%d [serviceData:%s, serviceDataMask:%s]",
filterSize, serviceData, serviceDataMask);
SoftBusFree(serviceData);
SoftBusFree(serviceDataMask);
}
}
int GetBtScanMode(uint16_t scanInterval, uint16_t scanWindow)
{
if (scanInterval == SOFTBUS_BC_SCAN_INTERVAL_P2 && scanWindow == SOFTBUS_BC_SCAN_WINDOW_P2) {
return OHOS_BLE_SCAN_MODE_OP_P2_60_3000;
}
if (scanInterval == SOFTBUS_BC_SCAN_INTERVAL_P10 && scanWindow == SOFTBUS_BC_SCAN_WINDOW_P10) {
return OHOS_BLE_SCAN_MODE_OP_P10_60_600;
}
if (scanInterval == SOFTBUS_BC_SCAN_INTERVAL_P25 && scanWindow == SOFTBUS_BC_SCAN_WINDOW_P25) {
return OHOS_BLE_SCAN_MODE_OP_P25_60_240;
}
if (scanInterval == SOFTBUS_BC_SCAN_INTERVAL_P100 && scanWindow == SOFTBUS_BC_SCAN_WINDOW_P100) {
return OHOS_BLE_SCAN_MODE_OP_P100_1000_1000;
}
return OHOS_BLE_SCAN_MODE_LOW_POWER;
}
uint8_t *AssembleAdvData(const SoftbusBroadcastData *data, uint16_t *dataLen)
{
uint16_t payloadLen = (data->bcData.payloadLen > BC_DATA_MAX_LEN) ? BC_DATA_MAX_LEN : data->bcData.payloadLen;
uint16_t len = data->isSupportFlag ? payloadLen + BC_HEAD_LEN : payloadLen + BC_HEAD_LEN - BC_FLAG_LEN;
uint8_t *advData = (uint8_t *)SoftBusCalloc(len);
if (advData == NULL) {
DISC_LOGE(DISC_BLE_ADAPTER, "malloc adv data failed");
return NULL;
}
int8_t offset = -BC_FLAG_LEN;
if (data->isSupportFlag) {
advData[IDX_BC_FLAG_BYTE_LEN] = BC_FLAG_BYTE_LEN;
advData[IDX_BC_FLAG_AD_TYPE] = BC_FLAG_AD_TYPE;
advData[IDX_BC_FLAG_AD_DATA] = data->flag;
offset += BC_FLAG_LEN;
}
advData[IDX_PACKET_LEN + offset] = payloadLen + BC_HEAD_LEN - BC_FLAG_LEN - 1;
advData[IDX_BC_TYPE + offset] = SoftbusAdvDataTypeToBt(data->bcData.type);
uint16_t payloadId = data->bcData.id;
advData[IDX_BC_UUID + offset] = (uint8_t)(payloadId & BC_BYTE_MASK);
advData[IDX_BC_UUID + offset + 1] = (uint8_t)((payloadId >> BC_SHIFT_BIT) & BC_BYTE_MASK);
if (memcpy_s(&advData[BC_HEAD_LEN + offset], payloadLen, data->bcData.payload, payloadLen) != EOK) {
DISC_LOGE(DISC_BLE_ADAPTER, "copy adv payload failed");
SoftBusFree(advData);
return NULL;
}
*dataLen = len;
return advData;
}
uint8_t *AssembleRspData(const SoftbusBroadcastPayload *data, uint16_t *dataLen)
{
uint16_t payloadLen = (data->payloadLen > RSP_DATA_MAX_LEN) ? RSP_DATA_MAX_LEN : data->payloadLen;
uint16_t len = payloadLen + RSP_HEAD_LEN;
uint8_t *rspData = (uint8_t *)SoftBusCalloc(len);
if (rspData == NULL) {
DISC_LOGE(DISC_BLE_ADAPTER, "malloc rsp data failed");
return NULL;
}
rspData[IDX_RSP_PACKET_LEN] = payloadLen + RSP_HEAD_LEN - 1;
rspData[IDX_RSP_TYPE] = SoftbusAdvDataTypeToBt(data->type);
uint16_t payloadId = data->id;
rspData[IDX_RSP_UUID] = (uint8_t)(payloadId & BC_BYTE_MASK);
rspData[IDX_RSP_UUID + 1] = (uint8_t)((payloadId >> BC_SHIFT_BIT) & BC_BYTE_MASK);
if (memcpy_s(&rspData[RSP_HEAD_LEN], payloadLen, data->payload, payloadLen) != EOK) {
DISC_LOGE(DISC_BLE_ADAPTER, "copy rsp payload failed");
SoftBusFree(rspData);
return NULL;
}
*dataLen = len;
return rspData;
}
static int32_t ParseFlag(const uint8_t *advData, uint8_t advLen, SoftBusBcScanResult *dst, uint8_t index)
{
if (index + 1 >= advLen) {
DISC_LOGE(DISC_BLE_ADAPTER, "parse flag failed");
return SOFTBUS_ERR;
}
dst->data.flag = advData[index + 1];
dst->data.isSupportFlag = true;
return SOFTBUS_OK;
}
static int32_t ParseLocalName(const uint8_t *advData, uint8_t advLen, SoftBusBcScanResult *dst, uint8_t index,
uint8_t len)
{
if (index + 1 >= advLen) {
DISC_LOGE(DISC_BLE_ADAPTER, "parse local name failed");
return SOFTBUS_ERR;
}
if (memcpy_s(dst->localName, sizeof(dst->localName), &advData[index + 1], len - 1) != EOK) {
DISC_LOGE(DISC_BLE_ADAPTER, "copy local name failed");
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
}
int32_t ParseScanResult(const uint8_t *advData, uint8_t advLen, SoftBusBcScanResult *dst)
{
uint8_t index = 0;
bool isRsp = false;
while (index < advLen) {
uint8_t len = advData[index];
if (len == 0) {
index++;
continue;
}
if (index + len >= advLen || index + 1 >= advLen) {
break;
}
uint8_t type = advData[++index];
if (type == BC_FLAG_AD_TYPE && ParseFlag(advData, advLen, dst, index) != SOFTBUS_OK) {
return SOFTBUS_ERR;
} else if (type == SHORTENED_LOCAL_NAME_BC_TYPE || type == LOCAL_NAME_BC_TYPE) {
if (ParseLocalName(advData, advLen, dst, index, len) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
} else {
if (type != SERVICE_BC_TYPE && type != MANUFACTURE_BC_TYPE) {
index += len;
DISC_LOGW(DISC_BLE_ADAPTER, "unsupported adv type: %u", type);
continue;
}
SoftbusBroadcastPayload *data = isRsp ? &dst->data.rspData : &dst->data.bcData;
data->payloadLen = len - ID_LEN - 1;
if (data->payloadLen <= 0 || index + ID_LEN + 1 >= advLen) {
DISC_LOGE(DISC_BLE_ADAPTER, "parse payload failed");
return SOFTBUS_ERR;
}
data->payload = (uint8_t *)SoftBusCalloc(data->payloadLen);
if (data->payload == NULL) {
DISC_LOGE(DISC_BLE_ADAPTER, "malloc payload failed");
return SOFTBUS_ERR;
}
isRsp = !isRsp;
data->type = BtAdvTypeToSoftbus(type);
data->id = ((uint16_t)advData[index + ID_LEN] << BC_SHIFT_BIT) | (uint16_t)advData[index + ID_LEN - 1];
if (memcpy_s(data->payload, data->payloadLen, &advData[index + ID_LEN + 1], data->payloadLen) != EOK) {
DISC_LOGE(DISC_BLE_ADAPTER, "copy payload failed");
}
}
index += len;
}
return SOFTBUS_OK;
}
void DumpSoftbusAdapterData(const char *description, uint8_t *data, uint16_t len)
{
DISC_CHECK_AND_RETURN_LOGE(description != NULL, DISC_BLE_ADAPTER, "data is null!");
DISC_CHECK_AND_RETURN_LOGE(len != 0, DISC_BLE_ADAPTER, "len is 0!");
DISC_CHECK_AND_RETURN_LOGE(data != NULL, DISC_BLE_ADAPTER, "data is null!");
int32_t hexLen = HEXIFY_LEN(len);
char *softbusData = (char *)SoftBusCalloc(sizeof(char) * hexLen);
DISC_CHECK_AND_RETURN_LOGE(softbusData != NULL, DISC_BLE_ADAPTER, "malloc failed!");
(void)ConvertBytesToHexString(softbusData, hexLen, data, len);
DISC_LOGI(DISC_BLE_ADAPTER, "%s softbusData:%s", description, softbusData);
SoftBusFree(softbusData);
}