diff --git a/adapter/common/net/bluetooth/ble/softbus_adapter_ble_gatt.c b/adapter/common/net/bluetooth/ble/softbus_adapter_ble_gatt.c deleted file mode 100644 index e197541e1..000000000 --- a/adapter/common/net/bluetooth/ble/softbus_adapter_ble_gatt.c +++ /dev/null @@ -1,1232 +0,0 @@ -/* - * Copyright (c) 2021 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 "adapter_bt_utils.h" -#include "c_header/ohos_bt_def.h" -#include "c_header/ohos_bt_gap.h" -#include "c_header/ohos_bt_gatt.h" -#include "disc_log.h" -#include "securec.h" -#include "softbus_adapter_ble_gatt.h" -#include "softbus_adapter_bt_common.h" -#include "softbus_adapter_mem.h" -#include "softbus_adapter_thread.h" -#include "softbus_errcode.h" -#include "softbus_utils.h" - -#define BT_UUID "43d4a49f-604d-45b5-9302-4ddbbfd538fd" -#define DELIVERY_MODE_REPLY 0xF0 // Lpdevice delivery mode: reply adv when filter matched -#define ADV_DURATION_MS 0 -#define ADV_WAIT_TIME_SEC 2 -static int32_t g_scannerId = -1; -typedef struct { - int advId; - bool isUsed; - bool isAdvertising; - SoftBusCond cond; - SoftBusBleAdvData advData; - SoftBusAdvCallback *advCallback; -} AdvChannel; - -typedef struct { - bool isUsed; - bool isNeedReset; - bool isScanning; - SoftBusBleScanParams param; - SoftBusBleScanFilter *filter; - uint8_t filterSize; - SoftBusScanListener *listener; - int32_t scannerId; -} ScanListener; - -typedef struct { - BleScanResultEvtType bleEvtType; - SoftBusBleScanResultEvtType softBusEvtType; -} OhosBleEvtToSoftBusEvt; - -OhosBleEvtToSoftBusEvt g_bleEvtSoftBusEvt[] = { - {OHOS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE, SOFTBUS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE}, - {OHOS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE_DIRECTED, SOFTBUS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE_DIRECTED}, - {OHOS_BLE_EVT_CONNECTABLE, SOFTBUS_BLE_EVT_CONNECTABLE}, - {OHOS_BLE_EVT_CONNECTABLE_DIRECTED, SOFTBUS_BLE_EVT_CONNECTABLE_DIRECTED}, - {OHOS_BLE_EVT_SCANNABLE, SOFTBUS_BLE_EVT_SCANNABLE}, - {OHOS_BLE_EVT_SCANNABLE_DIRECTED, SOFTBUS_BLE_EVT_SCANNABLE_DIRECTED}, - {OHOS_BLE_EVT_LEGACY_NON_CONNECTABLE, SOFTBUS_BLE_EVT_LEGACY_NON_CONNECTABLE}, - {OHOS_BLE_EVT_LEGACY_SCANNABLE, SOFTBUS_BLE_EVT_LEGACY_SCANNABLE}, - {OHOS_BLE_EVT_LEGACY_CONNECTABLE, SOFTBUS_BLE_EVT_LEGACY_CONNECTABLE}, - {OHOS_BLE_EVT_LEGACY_CONNECTABLE_DIRECTED, SOFTBUS_BLE_EVT_LEGACY_CONNECTABLE_DIRECTED}, - {OHOS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV_SCAN, SOFTBUS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV_SCAN}, - {OHOS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV, SOFTBUS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV}, -}; - -static AdvChannel g_advChannel[ADV_MAX_NUM]; -static ScanListener g_scanListener[SCAN_MAX_NUM]; - -static volatile bool g_lockInit = false; -static SoftBusMutex g_advLock; -static SoftBusMutex g_scanerLock; - -static volatile bool g_isRegCb = false; -static volatile bool g_isLpDeviceRegCb = false; - -static void OnBtStateChanged(int32_t listenerId, int32_t state) -{ - (void)listenerId; - if (state != SOFTBUS_BT_STATE_TURN_OFF) { - return; - } - - DISC_LOGI(DISC_BLE, "receive bt turn off event, start reset bt adapter state..."); - if (SoftBusMutexLock(&g_advLock) != SOFTBUS_OK) { - DISC_LOGE(DISC_BLE, "ATTENTION, try to get adv lock failed, something unexpected happened"); - return; - } - for (uint32_t index = 0; index < ADV_MAX_NUM; index++) { - AdvChannel *advChannel = &g_advChannel[index]; - if (advChannel->isUsed && advChannel->advId == -1) { - continue; - } - - // ignore status code explicitedly, just to notify bt cleanup resources associated with this advertisement - (void)BleStopAdv(advChannel->advId); - advChannel->advId = -1; - advChannel->isAdvertising = false; - SoftBusCondBroadcast(&advChannel->cond); - advChannel->advCallback->AdvDisableCallback(index, SOFTBUS_BT_STATUS_SUCCESS); - } - (void)SoftBusMutexUnlock(&g_advLock); - - if (SoftBusMutexLock(&g_scanerLock) != SOFTBUS_OK) { - DISC_LOGE(DISC_BLE, "ATTENTION, try to get scan lock failed, something unexpected happened"); - return; - } - for (int index = 0; index < SCAN_MAX_NUM; index++) { - ScanListener *scanListener = &g_scanListener[index]; - if (scanListener->isUsed && scanListener->isScanning) { - scanListener->isScanning = false; - scanListener->listener->OnScanStop(index, SOFTBUS_BT_STATUS_SUCCESS); - (void)BleStopScan(scanListener->scannerId); - } - } - (void)SoftBusMutexUnlock(&g_scanerLock); -} - -int BleGattLockInit(void) -{ - if (g_lockInit) { - return SOFTBUS_OK; - } - if (SoftBusMutexInit(&g_advLock, NULL) != SOFTBUS_OK) { - DISC_LOGE(DISC_BLE, "g_advLock init failed"); - return SOFTBUS_ERR; - } - if (SoftBusMutexInit(&g_scanerLock, NULL) != SOFTBUS_OK) { - DISC_LOGE(DISC_BLE, "g_scanerLock init failed"); - return SOFTBUS_ERR; - } - g_lockInit = true; - return SOFTBUS_OK; -} - -static unsigned char ConvertScanEventType(unsigned char eventType) -{ - int32_t status = SOFTBUS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE; - int len = sizeof(g_bleEvtSoftBusEvt) / sizeof(g_bleEvtSoftBusEvt[0]); - OhosBleEvtToSoftBusEvt *ptr = g_bleEvtSoftBusEvt; - - for (int i = 0; i < len; i++) { - if (eventType == (ptr + i)->bleEvtType) { - status = (ptr + i)->softBusEvtType; - break; - } - } - - return status; -} - -static unsigned char ConvertScanPhyType(unsigned char phyType) -{ - switch (phyType) { - case OHOS_BLE_SCAN_PHY_NO_PACKET: - return SOFTBUS_BLE_SCAN_PHY_NO_PACKET; - case OHOS_BLE_SCAN_PHY_1M: - return SOFTBUS_BLE_SCAN_PHY_1M; - case OHOS_BLE_SCAN_PHY_2M: - return SOFTBUS_BLE_SCAN_PHY_2M; - case OHOS_BLE_SCAN_PHY_CODED: - return SOFTBUS_BLE_SCAN_PHY_CODED; - default: - return SOFTBUS_BLE_SCAN_PHY_NO_PACKET; - } -} - -static unsigned char ConvertScanDataStatus(unsigned char dataStatus) -{ - switch (dataStatus) { - case OHOS_BLE_DATA_COMPLETE: - return SOFTBUS_BLE_DATA_COMPLETE; - case OHOS_BLE_DATA_INCOMPLETE_MORE_TO_COME: - return SOFTBUS_BLE_DATA_INCOMPLETE_MORE_TO_COME; - default: // fall-through - return SOFTBUS_BLE_DATA_INCOMPLETE_TRUNCATED; - } -} - -static unsigned char ConvertScanAddrType(unsigned char addrType) -{ - switch (addrType) { - case OHOS_BLE_PUBLIC_DEVICE_ADDRESS: - return SOFTBUS_BLE_PUBLIC_DEVICE_ADDRESS; - case OHOS_BLE_RANDOM_DEVICE_ADDRESS: - return SOFTBUS_BLE_RANDOM_DEVICE_ADDRESS; - case OHOS_BLE_PUBLIC_IDENTITY_ADDRESS: - return SOFTBUS_BLE_PUBLIC_IDENTITY_ADDRESS; - case OHOS_BLE_RANDOM_STATIC_IDENTITY_ADDRESS: - return SOFTBUS_BLE_RANDOM_STATIC_IDENTITY_ADDRESS; - case OHOS_BLE_UNRESOLVABLE_RANDOM_DEVICE_ADDRESS: - return SOFTBUS_BLE_UNRESOLVABLE_RANDOM_DEVICE_ADDRESS; - default: // fall-through - return SOFTBUS_BLE_NO_ADDRESS; - } -} - -static int ConvertScanMode(unsigned short scanInterval, unsigned short scanWindow) -{ - if (scanInterval == SOFTBUS_BLE_SCAN_INTERVAL_P2 && scanWindow == SOFTBUS_BLE_SCAN_WINDOW_P2) { - return OHOS_BLE_SCAN_MODE_OP_P2_60_3000; - } - if (scanInterval == SOFTBUS_BLE_SCAN_INTERVAL_P10 && scanWindow == SOFTBUS_BLE_SCAN_WINDOW_P10) { - return OHOS_BLE_SCAN_MODE_OP_P10_60_600; - } - if (scanInterval == SOFTBUS_BLE_SCAN_INTERVAL_P25 && scanWindow == SOFTBUS_BLE_SCAN_WINDOW_P25) { - return OHOS_BLE_SCAN_MODE_OP_P25_60_240; - } - if (scanInterval == SOFTBUS_BLE_SCAN_INTERVAL_P100 && scanWindow == SOFTBUS_BLE_SCAN_WINDOW_P100) { - return OHOS_BLE_SCAN_MODE_OP_P100_1000_1000; - } - return OHOS_BLE_SCAN_MODE_LOW_POWER; -} - -static void SetAndGetSuitableScanConfig(int listenerId, const SoftBusBleScanParams *params, BleScanConfigs *configs) -{ - static int lastScanMode = OHOS_BLE_SCAN_MODE_LOW_POWER; - (void)memset_s(configs, sizeof(BleScanConfigs), 0x0, sizeof(BleScanConfigs)); - g_scanListener[listenerId].param = *params; - for (int index = 0; index < SCAN_MAX_NUM; index++) { - if (!g_scanListener[index].isUsed || (!g_scanListener[index].isScanning && index != listenerId)) { - continue; - } - int scanMode = ConvertScanMode(g_scanListener[index].param.scanInterval, - g_scanListener[index].param.scanWindow); - if (scanMode > configs->scanMode) { - configs->scanMode = scanMode; - } - } - if (lastScanMode != configs->scanMode) { - g_scanListener[listenerId].isNeedReset = true; - lastScanMode = configs->scanMode; - } -} - -static void DumpBleScanFilter(BleScanNativeFilter *nativeFilter, uint8_t filterSize) -{ - while (filterSize-- > 0) { - int32_t len = (nativeFilter + filterSize)->serviceDataLength; - if (len <= 0) { - continue; - } - int32_t hexLen = HEXIFY_LEN(len); - char *serviceData = (char *)SoftBusCalloc(sizeof(char) * hexLen); - char *serviceDataMask = (char *)SoftBusCalloc(sizeof(char) * hexLen); - if (serviceData == NULL || serviceDataMask == NULL) { - SoftBusFree(serviceData); - SoftBusFree(serviceDataMask); - continue; - } - (void)ConvertBytesToHexString(serviceData, hexLen, (nativeFilter + filterSize)->serviceData, len); - (void)ConvertBytesToHexString(serviceDataMask, hexLen, (nativeFilter + filterSize)->serviceDataMask, len); - DISC_LOGI(DISC_BLE, "BLE Scan Filter id=%{public}u, serviceData=%{public}s, serviceDataMask=%{public}s", - filterSize, serviceData, serviceDataMask); - SoftBusFree(serviceData); - SoftBusFree(serviceDataMask); - } -} - -static void GetAllNativeScanFilter(int thisListenerId, BleScanNativeFilter **nativeFilter, uint8_t *filterSize) -{ - uint8_t nativeSize = 0; - for (int index = 0; index < SCAN_MAX_NUM; index++) { - if (!g_scanListener[index].isUsed || (!g_scanListener[index].isScanning && index != thisListenerId) || - g_scanListener[index].scannerId != g_scannerId) { - g_scanListener[index].isNeedReset = false; - continue; - } - g_scanListener[index].isNeedReset = true; - nativeSize += g_scanListener[index].filterSize; - } - *nativeFilter = (BleScanNativeFilter *)SoftBusCalloc(sizeof(BleScanNativeFilter) * nativeSize); - if (*nativeFilter == NULL) { - return; - } - *filterSize = nativeSize; - for (int index = 0; index < SCAN_MAX_NUM; index++) { - if (!g_scanListener[index].isNeedReset) { - continue; - } - uint8_t size = g_scanListener[index].filterSize; - const SoftBusBleScanFilter *filter = g_scanListener[index].filter; - BleScanNativeFilter **nativeOffset = NULL; - const SoftBusBleScanFilter *filterOffset = NULL; - - while (size-- > 0) { - nativeSize--; - *nativeOffset = *nativeFilter + nativeSize; - filterOffset = filter + size; - (*nativeOffset)->address = (filterOffset)->address; - (*nativeOffset)->deviceName = (filterOffset)->deviceName; - (*nativeOffset)->manufactureData = (filterOffset)->manufactureData; - (*nativeOffset)->manufactureDataLength = (filterOffset)->manufactureDataLength; - (*nativeOffset)->manufactureDataMask = (filterOffset)->manufactureDataMask; - (*nativeOffset)->manufactureId = (filterOffset)->manufactureId; - (*nativeOffset)->serviceData = (filterOffset)->serviceData; - (*nativeOffset)->serviceDataLength = (filterOffset)->serviceDataLength; - (*nativeOffset)->serviceDataMask = (filterOffset)->serviceDataMask; - (*nativeOffset)->serviceUuid = (filterOffset)->serviceUuid; - (*nativeOffset)->serviceUuidLength = (filterOffset)->serviceUuidLength; - (*nativeOffset)->serviceUuidMask = (filterOffset)->serviceUuidMask; - } - } -} - -static void SoftBusGetBurstScanFilter(int thisListenerId, BleScanNativeFilter **nativeFilter, uint8_t *filterSize) -{ - uint8_t size = g_scanListener[thisListenerId].filterSize; - *nativeFilter = (BleScanNativeFilter *)SoftBusCalloc(sizeof(BleScanNativeFilter) * size); - if (*nativeFilter == NULL) { - DISC_LOGE(DISC_BLE, "nativeFilter calloc addr fail"); - return; - } - const SoftBusBleScanFilter *filter = g_scanListener[thisListenerId].filter; - BleScanNativeFilter **nativeOffset = NULL; - const SoftBusBleScanFilter *filterOffset = NULL; - - while (size-- > 0) { - *nativeOffset = *nativeFilter + size; - filterOffset = filter + size; - (*nativeOffset)->address = (filterOffset)->address; - (*nativeOffset)->deviceName = (filterOffset)->deviceName; - (*nativeOffset)->manufactureData = (filterOffset)->manufactureData; - (*nativeOffset)->manufactureDataLength = (filterOffset)->manufactureDataLength; - (*nativeOffset)->manufactureDataMask = (filterOffset)->manufactureDataMask; - (*nativeOffset)->manufactureId = (filterOffset)->manufactureId; - (*nativeOffset)->serviceData = (filterOffset)->serviceData; - (*nativeOffset)->serviceDataLength = (filterOffset)->serviceDataLength; - (*nativeOffset)->serviceDataMask = (filterOffset)->serviceDataMask; - (*nativeOffset)->serviceUuid = (filterOffset)->serviceUuid; - (*nativeOffset)->serviceUuidLength = (filterOffset)->serviceUuidLength; - (*nativeOffset)->serviceUuidMask = (filterOffset)->serviceUuidMask; - } - *filterSize = g_scanListener[thisListenerId].filterSize; -} - -static void ConvertScanResult(const BtScanResultData *src, SoftBusBleScanResult *dst) -{ - dst->eventType = ConvertScanEventType(src->eventType); - dst->dataStatus = ConvertScanDataStatus(src->dataStatus); - dst->addrType = ConvertScanAddrType(src->addrType); - if (memcpy_s(dst->addr.addr, BT_ADDR_LEN, src->addr.addr, BT_ADDR_LEN) != EOK) { - DISC_LOGE(DISC_BLE, "ConvertScanResult memcpy addr fail"); - return; - } - dst->primaryPhy = ConvertScanPhyType(src->primaryPhy); - dst->secondaryPhy = ConvertScanPhyType(src->secondaryPhy); - dst->advSid = src->advSid; - dst->txPower = src->txPower; - dst->rssi = src->rssi; - dst->periodicAdvInterval = src->periodicAdvInterval; - dst->directAddrType = ConvertScanAddrType(src->directAddrType); - if (memcpy_s(dst->directAddr.addr, BT_ADDR_LEN, src->directAddr.addr, BT_ADDR_LEN) != EOK) { - DISC_LOGE(DISC_BLE, "ConvertScanResult memcpy directAddr fail"); - return; - } - dst->advLen = src->advLen; - dst->advData = src->advData; -} - -static unsigned char ConvertAdvType(unsigned char advType) -{ - switch (advType) { - case SOFTBUS_BLE_ADV_IND: - return OHOS_BLE_ADV_IND; - case SOFTBUS_BLE_ADV_DIRECT_IND_HIGH: - return OHOS_BLE_ADV_DIRECT_IND_HIGH; - case SOFTBUS_BLE_ADV_SCAN_IND: - return OHOS_BLE_ADV_SCAN_IND; - case SOFTBUS_BLE_ADV_NONCONN_IND: - return OHOS_BLE_ADV_NONCONN_IND; - case SOFTBUS_BLE_ADV_DIRECT_IND_LOW: - return OHOS_BLE_ADV_DIRECT_IND_LOW; - default: - return OHOS_BLE_ADV_IND; - } -} - -static unsigned char ConvertAdvFilter(unsigned char advFilter) -{ - switch (advFilter) { - case SOFTBUS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY: - return OHOS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY; - case SOFTBUS_BLE_ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY: - return OHOS_BLE_ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY; - case SOFTBUS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST: - return OHOS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST; - case SOFTBUS_BLE_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 void ConvertAdvData(const SoftBusBleAdvData *src, StartAdvRawData *dst) -{ - dst->advDataLen = src->advLength; - dst->advData = src->advData; - dst->rspDataLen = src->scanRspLength; - dst->rspData = src->scanRspData; -} - -static void ConvertAdvParam(const SoftBusBleAdvParams *src, BleAdvParams *dst) -{ - dst->minInterval = src->minInterval; - dst->maxInterval = src->maxInterval; - dst->advType = ConvertAdvType(src->advType); - dst->ownAddrType = 0x00; - dst->peerAddrType = 0x00; - if (memcpy_s(dst->peerAddr.addr, BT_ADDR_LEN, src->peerAddr.addr, BT_ADDR_LEN) != EOK) { - DISC_LOGE(DISC_BLE, "ConvertAdvParam memcpy directAddr fail"); - return; - } - dst->channelMap = src->channelMap; - dst->advFilterPolicy = ConvertAdvFilter(src->advFilterPolicy); - dst->txPower = src->txPower; - dst->duration = src->duration; -} - -static void WrapperAdvEnableCallback(int advId, int status) -{ - int32_t st = BleOhosStatusToSoftBus((BtStatus)status); - for (uint32_t index = 0; index < ADV_MAX_NUM; index++) { - AdvChannel *advChannel = &g_advChannel[index]; - if (advChannel->advId != advId || - advChannel->isUsed == false || - advChannel->advCallback == NULL || - advChannel->advCallback->AdvEnableCallback == NULL) { - continue; - } - DISC_LOGI(DISC_BLE, "inner-advId=%{public}u, bt-advId=%{public}d, status=%{public}u", index, advId, st); - if (st == SOFTBUS_BT_STATUS_SUCCESS) { - advChannel->isAdvertising = true; - SoftBusCondSignal(&advChannel->cond); - } - advChannel->advCallback->AdvEnableCallback(index, st); - break; - } -} - -static void WrapperAdvDisableCallback(int advId, int status) -{ - int32_t st = BleOhosStatusToSoftBus((BtStatus)status); - for (uint32_t index = 0; index < ADV_MAX_NUM; index++) { - AdvChannel *advChannel = &g_advChannel[index]; - if (advChannel->advId != advId || - advChannel->isUsed == false || - advChannel->advCallback == NULL || - advChannel->advCallback->AdvDisableCallback == NULL) { - continue; - } - DISC_LOGI(DISC_BLE, "inner-advId=%{public}u, bt-advId=%{public}d, status=%{public}d", index, advId, st); - if (st == SOFTBUS_BT_STATUS_SUCCESS) { - advChannel->advId = -1; - advChannel->isAdvertising = false; - SoftBusCondSignal(&advChannel->cond); - } - advChannel->advCallback->AdvDisableCallback(index, st); - break; - } -} - -static void WrapperAdvDataCallback(int advId, int status) -{ - int32_t st = BleOhosStatusToSoftBus((BtStatus)status); - for (uint32_t index = 0; index < ADV_MAX_NUM; index++) { - AdvChannel *advChannel = &g_advChannel[index]; - if (advChannel->advId != advId || - advChannel->isUsed == false || - advChannel->advCallback == NULL || - advChannel->advCallback->AdvDataCallback == NULL) { - continue; - } - DISC_LOGI(DISC_BLE, "inner-advId=%{public}u, bt-advId=%{public}d, status=%{public}d", index, advId, st); - advChannel->advCallback->AdvDataCallback(index, st); - break; - } -} - -static void WrapperAdvUpdateCallback(int advId, int status) -{ - int32_t st = BleOhosStatusToSoftBus((BtStatus)status); - for (uint32_t index = 0; index < ADV_MAX_NUM; index++) { - AdvChannel *advChannel = &g_advChannel[index]; - if (advChannel->advId != advId || - advChannel->isUsed == false || - advChannel->advCallback == NULL || - advChannel->advCallback->AdvUpdateCallback == NULL) { - continue; - } - DISC_LOGI(DISC_BLE, "inner-advId=%{public}u, bt-advId=%{public}d, status=%{public}d", index, advId, st); - advChannel->advCallback->AdvUpdateCallback(index, st); - break; - } -} - -static void WrapperSecurityRespondCallback(const BdAddr *bdAddr) -{ - (void)bdAddr; - DISC_LOGI(DISC_BLE, "WrapperSecurityRespondCallback"); -} - -static void WrapperScanResultCallback(BtScanResultData *scanResultdata) -{ - if (scanResultdata == NULL) { - return; - } - - SoftBusBleScanResult sr; - ConvertScanResult(scanResultdata, &sr); - for (int listenerId = 0; listenerId < SCAN_MAX_NUM; listenerId++) { - SoftBusMutexLock(&g_scanerLock); - ScanListener *scanListener = &g_scanListener[listenerId]; - if (!scanListener->isUsed || scanListener->listener == NULL || !scanListener->isScanning || - scanListener->listener->OnScanResult == NULL) { - SoftBusMutexUnlock(&g_scanerLock); - continue; - } - SoftBusMutexUnlock(&g_scanerLock); - scanListener->listener->OnScanResult(listenerId, &sr); - } -} - -static void WrapperLpDeviceInfoCallback(BtUuid *uuid, int32_t type, uint8_t *data, uint32_t dataSize) -{ - for (uint32_t index = 0; index < ADV_MAX_NUM; index++) { - AdvChannel *advChannel = &g_advChannel[index]; - if (advChannel->isUsed == false || advChannel->advCallback == NULL || - advChannel->advCallback->LpDeviceInfoCallback == NULL) { - continue; - } - advChannel->advCallback->LpDeviceInfoCallback((SoftBusBtUuid *)uuid, type, data, dataSize); - break; - } -} - -static void WrapperScanStateChangeCallback(int32_t resultCode, bool isStartScan) -{ - for (int32_t listenerId = 0; listenerId < SCAN_MAX_NUM; listenerId++) { - SoftBusMutexLock(&g_scanerLock); - ScanListener *scanListener = &g_scanListener[listenerId]; - if (!scanListener->isUsed || scanListener->listener == NULL || !scanListener->isScanning || - scanListener->listener->OnScanStateChanged == NULL) { - SoftBusMutexUnlock(&g_scanerLock); - continue; - } - SoftBusMutexUnlock(&g_scanerLock); - scanListener->listener->OnScanStateChanged(resultCode, isStartScan); - } -} - -static BtGattCallbacks g_softbusGattCb = { - .advEnableCb = WrapperAdvEnableCallback, - .advDisableCb = WrapperAdvDisableCallback, - .advDataCb = WrapperAdvDataCallback, - .advUpdateCb = WrapperAdvUpdateCallback, - .securityRespondCb = WrapperSecurityRespondCallback, -}; - -static BleScanCallbacks g_softbusBleScanCb = { - .scanResultCb = WrapperScanResultCallback, - .scanStateChangeCb = WrapperScanStateChangeCallback, - .lpDeviceInfoCb = WrapperLpDeviceInfoCallback, -}; - -static SoftBusBtStateListener g_btStateLister = { - .OnBtStateChanged = OnBtStateChanged, - .OnBtAclStateChanged = NULL, -}; - -static int RegisterBleGattCallback(int32_t *scannerId, bool isLpDeviceScan) -{ - if (!g_isRegCb && !g_isLpDeviceRegCb) { - if (BleGattRegisterCallbacks(&g_softbusGattCb) != 0) { - return SOFTBUS_ERR; - } - if (SoftBusAddBtStateListener(&g_btStateLister) < 0) { - return SOFTBUS_ERR; - } - } - if (!isLpDeviceScan) { - if (g_isRegCb) { - *scannerId = g_scannerId; - return SOFTBUS_OK; - } - if (BleRegisterScanCallbacks(&g_softbusBleScanCb, scannerId) != 0) { - DISC_LOGE(DISC_BLE, "SH register ble scan callback failed"); - return SOFTBUS_ERR; - } - g_scannerId = *scannerId; - g_isRegCb = true; - return SOFTBUS_OK; - } else { - if (g_isLpDeviceRegCb) { - return SOFTBUS_OK; - } - if (BleRegisterScanCallbacks(&g_softbusBleScanCb, scannerId) != 0) { - DISC_LOGE(DISC_BLE, "SH register ble scan callback failed"); - return SOFTBUS_ERR; - } - g_isLpDeviceRegCb = true; - return SOFTBUS_OK; - } -} - -static bool CheckAdvChannelInUsed(int advId) -{ - if (advId < 0 || advId >= ADV_MAX_NUM) { - return false; - } - if (!g_advChannel[advId].isUsed) { - DISC_LOGE(DISC_BLE, "advId is ready released. advId=%{public}d", advId); - return false; - } - return true; -} - -static bool CheckScanChannelInUsed(int listenerId) -{ - if (listenerId < 0 || listenerId >= SCAN_MAX_NUM) { - return false; - } - if (!g_scanListener[listenerId].isUsed) { - DISC_LOGE(DISC_BLE, "listenerId is ready released. listenerId=%{public}d", listenerId); - return false; - } - return true; -} - -static int SetAdvData(int advId, const SoftBusBleAdvData *data) -{ - g_advChannel[advId].advData.advLength = data->advLength; - g_advChannel[advId].advData.scanRspLength = data->scanRspLength; - SoftBusFree(g_advChannel[advId].advData.advData); - g_advChannel[advId].advData.advData = NULL; - SoftBusFree(g_advChannel[advId].advData.scanRspData); - g_advChannel[advId].advData.scanRspData = NULL; - - if (data->advLength != 0) { - g_advChannel[advId].advData.advData = SoftBusCalloc(data->advLength); - if (g_advChannel[advId].advData.advData == NULL) { - DISC_LOGE(DISC_BLE, "calloc advData failed"); - return SOFTBUS_MALLOC_ERR; - } - if (memcpy_s(g_advChannel[advId].advData.advData, data->advLength, - data->advData, data->advLength) != EOK) { - DISC_LOGE(DISC_BLE, "memcpy advData failed"); - SoftBusFree(g_advChannel[advId].advData.advData); - g_advChannel[advId].advData.advData = NULL; - return SOFTBUS_MEM_ERR; - } - } - if (data->scanRspLength != 0) { - g_advChannel[advId].advData.scanRspData = SoftBusCalloc(data->scanRspLength); - if (g_advChannel[advId].advData.scanRspData == NULL) { - DISC_LOGE(DISC_BLE, "calloc scanRspData failed"); - SoftBusFree(g_advChannel[advId].advData.advData); - g_advChannel[advId].advData.advData = NULL; - return SOFTBUS_MALLOC_ERR; - } - if (memcpy_s(g_advChannel[advId].advData.scanRspData, data->scanRspLength, - data->scanRspData, data->scanRspLength) != EOK) { - DISC_LOGE(DISC_BLE, "memcpy scanRspData failed"); - SoftBusFree(g_advChannel[advId].advData.advData); - SoftBusFree(g_advChannel[advId].advData.scanRspData); - g_advChannel[advId].advData.advData = NULL; - g_advChannel[advId].advData.scanRspData = NULL; - return SOFTBUS_MEM_ERR; - } - } - return SOFTBUS_OK; -} - -static void ClearAdvData(int advId) -{ - g_advChannel[advId].advData.advLength = 0; - g_advChannel[advId].advData.scanRspLength = 0; - SoftBusFree(g_advChannel[advId].advData.advData); - SoftBusFree(g_advChannel[advId].advData.scanRspData); - g_advChannel[advId].advData.advData = NULL; - g_advChannel[advId].advData.scanRspData = NULL; -} - -int SoftBusGetAdvChannel(const SoftBusAdvCallback *callback, int *scannerId, bool isLpDeviceScan) -{ - if (callback == NULL) { - return SOFTBUS_INVALID_PARAM; - } - if (SoftBusMutexLock(&g_advLock) != SOFTBUS_OK) { - return SOFTBUS_LOCK_ERR; - } - if (RegisterBleGattCallback(scannerId, isLpDeviceScan) != SOFTBUS_OK) { - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_ERR; - } - int freeAdvId; - for (freeAdvId = 0; freeAdvId < ADV_MAX_NUM; freeAdvId++) { - if (!g_advChannel[freeAdvId].isUsed) { - break; - } - } - if (freeAdvId == ADV_MAX_NUM) { - DISC_LOGE(DISC_BLE, "no available adv channel"); - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_ERR; - } - g_advChannel[freeAdvId].advId = -1; - g_advChannel[freeAdvId].isUsed = true; - g_advChannel[freeAdvId].isAdvertising = false; - SoftBusCondInit(&g_advChannel[freeAdvId].cond); - g_advChannel[freeAdvId].advCallback = (SoftBusAdvCallback *)callback; - SoftBusMutexUnlock(&g_advLock); - return freeAdvId; -} - -int SoftBusReleaseAdvChannel(int advId) -{ - if (SoftBusMutexLock(&g_advLock) != 0) { - return SOFTBUS_LOCK_ERR; - } - if (!CheckAdvChannelInUsed(advId)) { - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_ERR; - } - ClearAdvData(advId); - g_advChannel[advId].advId = -1; - g_advChannel[advId].isUsed = false; - g_advChannel[advId].isAdvertising = false; - SoftBusCondDestroy(&g_advChannel[advId].cond); - g_advChannel[advId].advCallback = NULL; - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_OK; -} - -int SoftBusSetAdvData(int advId, const SoftBusBleAdvData *data) -{ - if (data == NULL) { - return SOFTBUS_INVALID_PARAM; - } - if (SoftBusMutexLock(&g_advLock) != 0) { - return SOFTBUS_LOCK_ERR; - } - if (!CheckAdvChannelInUsed(advId)) { - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_ERR; - } - int ret = SetAdvData(advId, data); - if (ret == SOFTBUS_OK) { - g_advChannel[advId].advCallback->AdvDataCallback(advId, SOFTBUS_BT_STATUS_SUCCESS); - } else { - g_advChannel[advId].advCallback->AdvDataCallback(advId, SOFTBUS_BT_STATUS_FAIL); - } - SoftBusMutexUnlock(&g_advLock); - return ret; -} - -static int OhosBleStartAdvEx(int *advId, const SoftBusBleAdvParams *param, const SoftBusBleAdvData *data) -{ - int btAdvId = -1; - BleAdvParams dstParam; - StartAdvRawData advData; - ConvertAdvParam(param, &dstParam); - ConvertAdvData(data, &advData); - int ret = BleStartAdvEx(&btAdvId, advData, dstParam); - if (ret != OHOS_BT_STATUS_SUCCESS) { - DISC_LOGE(DISC_BLE, "BleStartAdvEx, bt-advId=%{public}d, ret=%{public}d", btAdvId, ret); - return SOFTBUS_ERR; - } - *advId = btAdvId; - return SOFTBUS_OK; -} - -int SoftBusStartAdv(int advId, const SoftBusBleAdvParams *param) -{ - return SoftBusStartAdvEx(advId, param, OhosBleStartAdvEx); -} - -int SoftBusStartAdvEx(int advId, const SoftBusBleAdvParams *param, - int (*startAdvEx)(int *, const SoftBusBleAdvParams *, const SoftBusBleAdvData *)) -{ - if (param == NULL || startAdvEx == NULL) { - return SOFTBUS_INVALID_PARAM; - } - if (SoftBusMutexLock(&g_advLock) != 0) { - return SOFTBUS_LOCK_ERR; - } - if (!CheckAdvChannelInUsed(advId)) { - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_ERR; - } - if (g_advChannel[advId].isAdvertising) { - DISC_LOGW(DISC_BLE, "SoftBusStartAdv, wait condition inner-advId=%{public}d", advId); - SoftBusSysTime absTime = {0}; - if (SoftBusGetTime(&absTime) != SOFTBUS_OK) { - DISC_LOGE(DISC_BLE, "Softbus get time failed"); - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_ERR; - } - - absTime.sec += ADV_WAIT_TIME_SEC; - if (SoftBusCondWait(&g_advChannel[advId].cond, &g_advLock, &absTime) != SOFTBUS_OK) { - DISC_LOGE(DISC_BLE, "SoftBusStartAdv, wait timeout"); - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_ERR; - } - } - if (g_advChannel[advId].advId != -1) { - DISC_LOGE(DISC_BLE, "already assigned an advId. advId=%{public}d", g_advChannel[advId].advId); - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_ERR; - } - - int ret = startAdvEx(&g_advChannel[advId].advId, param, &g_advChannel[advId].advData); - DISC_LOGI(DISC_BLE, - "inner-advId=%{public}d, bt-advId=%{public}d, ret=%{public}d", advId, g_advChannel[advId].advId, ret); - if (ret != SOFTBUS_OK) { - g_advChannel[advId].advCallback->AdvEnableCallback(advId, SOFTBUS_BT_STATUS_FAIL); - SoftBusMutexUnlock(&g_advLock); - return ret; - } - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_OK; -} - -int SoftBusStopAdv(int advId) -{ - if (SoftBusMutexLock(&g_advLock) != 0) { - return SOFTBUS_LOCK_ERR; - } - if (!CheckAdvChannelInUsed(advId)) { - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_ERR; - } - if (!g_advChannel[advId].isAdvertising) { - DISC_LOGI(DISC_BLE, "adv is not advertising. advId=%{public}d", advId); - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_OK; - } - int ret = BleStopAdv(g_advChannel[advId].advId); - DISC_LOGI(DISC_BLE, - "inner-advId=%{public}d, bt-advId=%{public}d, ret=%{public}d", advId, g_advChannel[advId].advId, ret); - if (ret != OHOS_BT_STATUS_SUCCESS) { - g_advChannel[advId].advCallback->AdvDisableCallback(advId, SOFTBUS_BT_STATUS_FAIL); - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_ERR; - } - ClearAdvData(advId); - g_advChannel[advId].advCallback->AdvDisableCallback(advId, SOFTBUS_BT_STATUS_SUCCESS); - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_OK; -} - -int SoftBusUpdateAdv(int advId, const SoftBusBleAdvData *data, const SoftBusBleAdvParams *param) -{ - if (param == NULL || data == NULL) { - DISC_LOGE(DISC_BLE, "invalid param"); - return SOFTBUS_INVALID_PARAM; - } - int ret = SoftBusStopAdv(advId); - if (ret != SOFTBUS_OK) { - DISC_LOGE(DISC_BLE, "SoftBusStopAdv return failed, ret=%{public}d", ret); - return ret; - } - ret = SetAdvData(advId, data); - if (ret != SOFTBUS_OK) { - DISC_LOGE(DISC_BLE, "SetAdvData return failed, ret=%{public}d", ret); - return ret; - } - return SoftBusStartAdv(advId, param); -} - -int SoftBusAddScanListener(const SoftBusScanListener *listener, int *scannerId, bool isLpDeviceScan) -{ - if (listener == NULL) { - return SOFTBUS_ERR; - } - if (SoftBusMutexLock(&g_scanerLock) != 0) { - return SOFTBUS_LOCK_ERR; - } - if (RegisterBleGattCallback(scannerId, isLpDeviceScan) != SOFTBUS_OK) { - SoftBusMutexUnlock(&g_scanerLock); - return SOFTBUS_ERR; - } - for (int index = 0; index < SCAN_MAX_NUM; index++) { - if (!g_scanListener[index].isUsed) { - (void)memset_s(g_scanListener + index, sizeof(ScanListener), 0x0, sizeof(ScanListener)); - g_scanListener[index].isUsed = true; - g_scanListener[index].isNeedReset = true; - g_scanListener[index].isScanning = false; - g_scanListener[index].scannerId = *scannerId; - g_scanListener[index].listener = (SoftBusScanListener *)listener; - SoftBusMutexUnlock(&g_scanerLock); - return index; - } - } - SoftBusMutexUnlock(&g_scanerLock); - return SOFTBUS_ERR; -} - -static void FreeScanFilter(int listenerId) -{ - uint8_t filterSize = g_scanListener[listenerId].filterSize; - SoftBusBleScanFilter *filter = g_scanListener[listenerId].filter; - while (filterSize-- > 0) { - SoftBusFree((filter + filterSize)->address); - SoftBusFree((filter + filterSize)->deviceName); - SoftBusFree((filter + filterSize)->serviceUuid); - SoftBusFree((filter + filterSize)->serviceUuidMask); - SoftBusFree((filter + filterSize)->serviceData); - SoftBusFree((filter + filterSize)->serviceDataMask); - SoftBusFree((filter + filterSize)->manufactureData); - SoftBusFree((filter + filterSize)->manufactureDataMask); - } - SoftBusFree(filter); - g_scanListener[listenerId].filterSize = 0; - g_scanListener[listenerId].filter = NULL; -} - -int SoftBusRemoveScanListener(int listenerId) -{ - if (listenerId < 0 || listenerId >= SCAN_MAX_NUM) { - return SOFTBUS_INVALID_PARAM; - } - if (SoftBusMutexLock(&g_scanerLock) != 0) { - return SOFTBUS_LOCK_ERR; - } - g_scanListener[listenerId].isUsed = false; - g_scanListener[listenerId].isNeedReset = true; - g_scanListener[listenerId].isScanning = false; - g_scanListener[listenerId].listener = NULL; - FreeScanFilter(listenerId); - SoftBusMutexUnlock(&g_scanerLock); - return SOFTBUS_OK; -} - -static bool CheckNeedReStartScan(int scannerId) -{ - bool isNeedReset = false; - bool isScanning = false; - for (int listenerId = 0; listenerId < SCAN_MAX_NUM; listenerId++) { - if (g_scanListener[listenerId].isNeedReset) { - isNeedReset = true; - } - if (g_scanListener[listenerId].isScanning) { - isScanning = true; - } - } - if (isNeedReset && isScanning) { - if (BleOhosStatusToSoftBus(BleStopScan(scannerId)) != SOFTBUS_BT_STATUS_SUCCESS) { - DISC_LOGE(DISC_BLE, "ble stop scan failed"); - return false; - } - return true; - } - if (!isNeedReset && isScanning) { - return false; - } - return true; -} - -static bool CheckNeedStopScan(int listenerId) -{ - for (int index = 0; index < SCAN_MAX_NUM; index++) { - if (index == listenerId && !g_scanListener[index].isScanning) { - return false; - } - if (index != listenerId && g_scanListener[index].isScanning) { - return false; - } - } - return true; -} - -int SoftBusSetScanFilter(int listenerId, SoftBusBleScanFilter *filter, uint8_t filterSize) -{ - if (filter == NULL || filterSize == 0) { - return SOFTBUS_INVALID_PARAM; - } - if (SoftBusMutexLock(&g_scanerLock) != 0) { - return SOFTBUS_LOCK_ERR; - } - if (!CheckScanChannelInUsed(listenerId)) { - DISC_LOGE(DISC_BLE, "ScanListener is not in useid, listenerId=%{public}d", listenerId); - SoftBusMutexUnlock(&g_scanerLock); - return SOFTBUS_ERR; - } - FreeScanFilter(listenerId); - g_scanListener[listenerId].filter = (SoftBusBleScanFilter *)filter; - g_scanListener[listenerId].filterSize = filterSize; - g_scanListener[listenerId].isNeedReset = true; - SoftBusMutexUnlock(&g_scanerLock); - return SOFTBUS_OK; -} - -int SoftBusStartScan(int listenerId, int scannerId, const SoftBusBleScanParams *param) -{ - if (param == NULL) { - return SOFTBUS_INVALID_PARAM; - } - if (SoftBusMutexLock(&g_scanerLock) != 0) { - return SOFTBUS_LOCK_ERR; - } - if (!CheckScanChannelInUsed(listenerId)) { - DISC_LOGE(DISC_BLE, "ScanListener is not in use, listenerId=%{public}d", listenerId); - SoftBusMutexUnlock(&g_scanerLock); - return SOFTBUS_ERR; - } - BleScanConfigs scanConfig; - SetAndGetSuitableScanConfig(listenerId, param, &scanConfig); - int32_t status = SOFTBUS_BT_STATUS_SUCCESS; - if (CheckNeedReStartScan(scannerId)) { - uint8_t filterSize = 0; - BleScanNativeFilter *nativeFilter = NULL; - if (scannerId == g_scannerId) { - GetAllNativeScanFilter(listenerId, &nativeFilter, &filterSize); - } else { - SoftBusGetBurstScanFilter(listenerId, &nativeFilter, &filterSize); - } - DumpBleScanFilter(nativeFilter, filterSize); - status = BleOhosStatusToSoftBus(BleStartScanEx(scannerId, &scanConfig, nativeFilter, filterSize)); - SoftBusFree(nativeFilter); - } - if (status != SOFTBUS_BT_STATUS_SUCCESS) { - SoftBusMutexUnlock(&g_scanerLock); - return SOFTBUS_ERR; - } - g_scanListener[listenerId].isScanning = true; - if (g_scanListener[listenerId].listener != NULL && - g_scanListener[listenerId].listener->OnScanStart != NULL) { - g_scanListener[listenerId].listener->OnScanStart(listenerId, SOFTBUS_BT_STATUS_SUCCESS); - } - SoftBusMutexUnlock(&g_scanerLock); - if (status == SOFTBUS_BT_STATUS_SUCCESS) { - return SOFTBUS_OK; - } - return SOFTBUS_ERR; -} - -int SoftBusStopScan(int listenerId, int scannerId) -{ - if (SoftBusMutexLock(&g_scanerLock) != 0) { - return SOFTBUS_LOCK_ERR; - } - if (!CheckScanChannelInUsed(listenerId)) { - SoftBusMutexUnlock(&g_scanerLock); - return SOFTBUS_ERR; - } - if (!g_scanListener[listenerId].isScanning) { - SoftBusMutexUnlock(&g_scanerLock); - return SOFTBUS_OK; - } - int32_t status = SOFTBUS_BT_STATUS_SUCCESS; - if (CheckNeedStopScan(listenerId)) { - status = BleOhosStatusToSoftBus(BleStopScan(scannerId)); - } - if (status != SOFTBUS_BT_STATUS_SUCCESS) { - SoftBusMutexUnlock(&g_scanerLock); - return SOFTBUS_ERR; - } - g_scanListener[listenerId].isScanning = false; - if (g_scanListener[listenerId].listener != NULL && - g_scanListener[listenerId].listener->OnScanStop != NULL) { - g_scanListener[listenerId].listener->OnScanStop(listenerId, status); - } - SoftBusMutexUnlock(&g_scanerLock); - if (status == SOFTBUS_BT_STATUS_SUCCESS) { - return SOFTBUS_OK; - } - return SOFTBUS_ERR; -} - -int SoftBusStopScanImmediately(int listenerId, int scannerId) -{ - if (SoftBusMutexLock(&g_scanerLock) != 0) { - return SOFTBUS_LOCK_ERR; - } - if (!CheckScanChannelInUsed(listenerId)) { - SoftBusMutexUnlock(&g_scanerLock); - return SOFTBUS_ERR; - } - if (!g_scanListener[listenerId].isScanning) { - SoftBusMutexUnlock(&g_scanerLock); - return SOFTBUS_OK; - } - if (BleOhosStatusToSoftBus(BleStopScan(scannerId)) != SOFTBUS_BT_STATUS_SUCCESS) { - DISC_LOGE(DISC_BLE, "ble stop scan immediately failed"); - SoftBusMutexUnlock(&g_scanerLock); - return SOFTBUS_ERR; - } - - g_scanListener[listenerId].isUsed = false; - uint8_t filterSize = 0; - BleScanNativeFilter *nativeFilter = NULL; - if (scannerId == g_scannerId) { - GetAllNativeScanFilter(listenerId, &nativeFilter, &filterSize); - } else { - SoftBusGetBurstScanFilter(listenerId, &nativeFilter, &filterSize); - } - DumpBleScanFilter(nativeFilter, filterSize); - DISC_LOGI(DISC_BLE, - "listenerId=%{public}d, scannerId=%{public}d, filterSize=%{public}u", listenerId, scannerId, filterSize); - int32_t status = SOFTBUS_BT_STATUS_SUCCESS; - if (filterSize != 0) { - BleScanConfigs scanConfig = { 0 }; - SoftBusBleScanParams param = { 0 }; - SetAndGetSuitableScanConfig(listenerId, ¶m, &scanConfig); - status = BleOhosStatusToSoftBus(BleStartScanEx(scannerId, &scanConfig, nativeFilter, filterSize)); - } - SoftBusFree(nativeFilter); - g_scanListener[listenerId].isUsed = true; - - g_scanListener[listenerId].isScanning = false; - if (g_scanListener[listenerId].listener != NULL && - g_scanListener[listenerId].listener->OnScanStop != NULL) { - g_scanListener[listenerId].listener->OnScanStop(listenerId, status); - } - SoftBusMutexUnlock(&g_scanerLock); - if (status != SOFTBUS_BT_STATUS_SUCCESS) { - DISC_LOGE(DISC_BLE, "ble stop and rescan failed, status=%{public}d", status); - return SOFTBUS_ERR; - } - return SOFTBUS_OK; -} - -int SoftBusReplaceAdvertisingAdv(int advId, const SoftBusBleAdvData *data) -{ - if (data == NULL) { - return SOFTBUS_INVALID_PARAM; - } - if (SoftBusMutexLock(&g_advLock) != 0) { - return SOFTBUS_LOCK_ERR; - } - if (!CheckAdvChannelInUsed(advId)) { - SoftBusMutexUnlock(&g_advLock); - return SOFTBUS_ERR; - } - if (!g_advChannel[advId].isAdvertising) { - SoftBusMutexUnlock(&g_advLock); - DISC_LOGE(DISC_BLE, "adv is not advertising. advId=%{public}d", advId); - return SOFTBUS_ERR; - } - int btAdvId = g_advChannel[advId].advId; - int ret = SetAdvData(advId, data); - SoftBusMutexUnlock(&g_advLock); - if (ret != SOFTBUS_OK) { - DISC_LOGE(DISC_BLE, "SetAdvData failed, advId=%{public}d, btadvId=%{public}d", advId, btAdvId); - return SOFTBUS_ERR; - } - DISC_LOGI(DISC_BLE, "BleSetAdvData, advId=%{public}d, btadvId=%{public}d", advId, btAdvId); - StartAdvRawData advData = {0}; - ConvertAdvData(data, &advData); - ret = BleOhosStatusToSoftBus(BleSetAdvData(btAdvId, advData)); - return ret; -} - -bool SoftBusIsLpDeviceAvailable(void) -{ - return IsLpDeviceAvailable(); -} - -bool SoftBusSetAdvFilterParam(int advHandle, int advId, SoftBusBleAdvParams *advParam, - int listenerId, SoftBusBleScanParams *scanParam) -{ - BleScanConfigs scanConfig; - SetAndGetSuitableScanConfig(listenerId, scanParam, &scanConfig); - BleScanNativeFilter *filter = NULL; - uint8_t filterSize = 0; - SoftBusGetBurstScanFilter(listenerId, &filter, &filterSize); - DumpBleScanFilter(filter, filterSize); - BleAdvParams btAdvParam; - StartAdvRawData rawData; - ConvertAdvParam(advParam, &btAdvParam); - ConvertAdvData(&g_advChannel[advId].advData, &rawData); - BtUuid btUuid; - btUuid.uuid = BT_UUID; - btUuid.uuidLen = strlen(BT_UUID); - BtLpDeviceParam btLpDeviceParam; - btLpDeviceParam.scanConfig = &scanConfig; - btLpDeviceParam.filter = filter; - btLpDeviceParam.filterSize = filterSize; - btLpDeviceParam.advParam = btAdvParam; - btLpDeviceParam.rawData = rawData; - btLpDeviceParam.uuid = btUuid; - btLpDeviceParam.activeDeviceInfo = NULL; - btLpDeviceParam.activeDeviceSize = 0; - btLpDeviceParam.deliveryMode = DELIVERY_MODE_REPLY; - btLpDeviceParam.advHandle = advHandle; - btLpDeviceParam.duration = ADV_DURATION_MS; - if (SetLpDeviceParam(&btLpDeviceParam) != SOFTBUS_OK) { - DISC_LOGE(DISC_BLE, "SH SetLpDeviceParam failed"); - return false; - } - SoftBusFree(filter); - return true; -} - -int32_t SoftBusGetAdvHandle(int32_t advId, int32_t *advHandle) -{ - return GetAdvHandle(g_advChannel[advId].advId, advHandle); -} - -int32_t SoftBusEnableSyncDataToLpDevice(void) -{ - return EnableSyncDataToLpDevice(); -} - -int32_t SoftBusDisableSyncDataToLpDevice(void) -{ - return DisableSyncDataToLpDevice(); -} - -int32_t SoftBusDeregisterScanCallbacks(int32_t scannerId) -{ - return BleDeregisterScanCallbacks(scannerId); -} - -int32_t SoftBusSetScanReportChannelToLpDevice(int32_t scannerId, bool enable) -{ - return SetScanReportChannelToLpDevice(scannerId, enable); -} - -int32_t SoftBusSetLpDeviceParam(int duration, int maxExtAdvEvents, int window, - int interval, int advHandle) -{ - return SetLpDeviceAdvParam(duration, maxExtAdvEvents, window, interval, advHandle); -} diff --git a/adapter/common/net/bluetooth/include/adapter_bt_utils.h b/adapter/common/net/bluetooth/include/adapter_bt_utils.h index 7c739fe22..821665d9e 100644 --- a/adapter/common/net/bluetooth/include/adapter_bt_utils.h +++ b/adapter/common/net/bluetooth/include/adapter_bt_utils.h @@ -20,8 +20,6 @@ #include "c_header/ohos_bt_def.h" #include "c_header/ohos_bt_gap.h" -#include "c_header/ohos_bt_gatt.h" -#include "softbus_adapter_ble_gatt.h" #include "softbus_adapter_bt_common.h" #ifdef __cplusplus diff --git a/adapter/common/net/bluetooth/include/softbus_adapter_ble_gatt.h b/adapter/common/net/bluetooth/include/softbus_adapter_ble_gatt.h deleted file mode 100644 index cd5efe914..000000000 --- a/adapter/common/net/bluetooth/include/softbus_adapter_ble_gatt.h +++ /dev/null @@ -1,229 +0,0 @@ -/* - * Copyright (c) 2021-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. - */ - -#ifndef SOFTBUS_ADAPTER_BLE_GATT_H -#define SOFTBUS_ADAPTER_BLE_GATT_H - -#include "softbus_adapter_bt_common.h" - -#ifdef __cplusplus -#if __cplusplus -extern "C" { -#endif -#endif - -// max adv and scan limit -#define ADV_MAX_NUM 16 -#define SCAN_MAX_NUM 16 - -// Bluetooth scan duty cycle, unit: ms -#define SOFTBUS_BLE_SCAN_INTERVAL_P2 3000 -#define SOFTBUS_BLE_SCAN_INTERVAL_P10 600 -#define SOFTBUS_BLE_SCAN_INTERVAL_P25 240 -#define SOFTBUS_BLE_SCAN_INTERVAL_P100 1000 -#define SOFTBUS_BLE_SCAN_WINDOW_P2 60 -#define SOFTBUS_BLE_SCAN_WINDOW_P10 60 -#define SOFTBUS_BLE_SCAN_WINDOW_P25 60 -#define SOFTBUS_BLE_SCAN_WINDOW_P100 1000 - -typedef enum { - SOFTBUS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE = 0x00, - SOFTBUS_BLE_EVT_CONNECTABLE = 0x01, - SOFTBUS_BLE_EVT_SCANNABLE = 0x02, - SOFTBUS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE_DIRECTED = 0x04, - SOFTBUS_BLE_EVT_CONNECTABLE_DIRECTED = 0x05, - SOFTBUS_BLE_EVT_SCANNABLE_DIRECTED = 0x06, - SOFTBUS_BLE_EVT_LEGACY_NON_CONNECTABLE = 0x10, - SOFTBUS_BLE_EVT_LEGACY_SCANNABLE = 0x12, - SOFTBUS_BLE_EVT_LEGACY_CONNECTABLE = 0x13, - SOFTBUS_BLE_EVT_LEGACY_CONNECTABLE_DIRECTED = 0x15, - SOFTBUS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV_SCAN = 0x1A, - SOFTBUS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV = 0x1B -} SoftBusBleScanResultEvtType; - -typedef enum { - SOFTBUS_BLE_PUBLIC_DEVICE_ADDRESS = 0x00, - SOFTBUS_BLE_RANDOM_DEVICE_ADDRESS = 0x01, - SOFTBUS_BLE_PUBLIC_IDENTITY_ADDRESS = 0x02, - SOFTBUS_BLE_RANDOM_STATIC_IDENTITY_ADDRESS = 0x03, - SOFTBUS_BLE_UNRESOLVABLE_RANDOM_DEVICE_ADDRESS = 0xFE, - SOFTBUS_BLE_NO_ADDRESS = 0xFF, -} SoftBusBleScanResultAddrType; - -typedef enum { - SOFTBUS_BLE_SCAN_TYPE_PASSIVE = 0x00, - SOFTBUS_BLE_SCAN_TYPE_ACTIVE, -} SoftBusBleScanType; - -typedef enum { - SOFTBUS_BLE_SCAN_PHY_NO_PACKET = 0x00, - SOFTBUS_BLE_SCAN_PHY_1M = 0x01, - SOFTBUS_BLE_SCAN_PHY_2M = 0x02, - SOFTBUS_BLE_SCAN_PHY_CODED = 0x03 -} SoftBusBleScanResultPhyType; - -typedef enum { - SOFTBUS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL = 0x00, - SOFTBUS_BLE_SCAN_FILTER_POLICY_ONLY_WHITE_LIST, - SOFTBUS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL_AND_RPA, - SOFTBUS_BLE_SCAN_FILTER_POLICY_ONLY_WHITE_LIST_AND_RPA -} SoftBusBleScanFilterPolicy; - -typedef enum { - SOFTBUS_BLE_ADV_IND = 0x00, - SOFTBUS_BLE_ADV_DIRECT_IND_HIGH = 0x01, - SOFTBUS_BLE_ADV_SCAN_IND = 0x02, - SOFTBUS_BLE_ADV_NONCONN_IND = 0x03, - SOFTBUS_BLE_ADV_DIRECT_IND_LOW = 0x04, -} SoftBusBleAdvType; - -typedef enum { - SOFTBUS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY = 0x00, - SOFTBUS_BLE_ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY = 0x01, - SOFTBUS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST = 0x02, - SOFTBUS_BLE_ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST = 0x03, -} SoftBusBleAdvFilter; - -typedef struct { - unsigned short scanInterval; - unsigned short scanWindow; - unsigned char scanType; - unsigned char scanPhy; - unsigned char scanFilterPolicy; -} SoftBusBleScanParams; - -typedef struct { - char *address; - char *deviceName; - unsigned int serviceUuidLength; - unsigned char *serviceUuid; - unsigned char *serviceUuidMask; - unsigned int serviceDataLength; - unsigned char *serviceData; - unsigned char *serviceDataMask; - unsigned int manufactureDataLength; - unsigned char *manufactureData; - unsigned char *manufactureDataMask; - unsigned short manufactureId; -} SoftBusBleScanFilter; - -typedef enum { - SOFTBUS_BLE_DATA_COMPLETE = 0x00, - SOFTBUS_BLE_DATA_INCOMPLETE_MORE_TO_COME = 0x01, - SOFTBUS_BLE_DATA_INCOMPLETE_TRUNCATED = 0x02, -} SoftBusScanResultDataStatus; - -typedef struct { - unsigned char eventType; - unsigned char dataStatus; - unsigned char addrType; - SoftBusBtAddr addr; - unsigned char primaryPhy; - unsigned char secondaryPhy; - unsigned char advSid; - char txPower; - char rssi; - unsigned short periodicAdvInterval; - unsigned char directAddrType; - SoftBusBtAddr directAddr; - unsigned char advLen; - unsigned char *advData; - char *deviceName; - unsigned char rspDataLen; - unsigned char *rspData; -} SoftBusBleScanResult; - -typedef struct { - void (*OnScanStart)(int listenerId, int status); - void (*OnScanStop)(int listenerId, int status); - void (*OnScanResult)(int listenerId, const SoftBusBleScanResult *scanResultdata); - void (*OnScanStateChanged)(int32_t resultCode, bool isStartScan); -} SoftBusScanListener; - -typedef struct { - int minInterval; - int maxInterval; - unsigned char advType; - unsigned char ownAddrType; - unsigned char peerAddrType; - SoftBusBtAddr peerAddr; - int channelMap; - unsigned char advFilterPolicy; - int8_t txPower; - int duration; -} SoftBusBleAdvParams; - -typedef struct { - void (*AdvEnableCallback)(int advId, int status); - void (*AdvDisableCallback)(int advId, int status); - void (*AdvDataCallback)(int advId, int status); - void (*AdvUpdateCallback)(int advId, int status); - void (*LpDeviceInfoCallback)(SoftBusBtUuid *uuid, int32_t type, uint8_t *data, uint32_t dataSize); -} SoftBusAdvCallback; - -int BleGattLockInit(void); - -int SoftBusAddScanListener(const SoftBusScanListener *listener, int *scannerId, bool isLpDeviceScan); - -int SoftBusRemoveScanListener(int listenerId); - -int SoftBusSetScanFilter(int listenerId, SoftBusBleScanFilter *filter, uint8_t filterSize); - -int SoftBusStartScan(int listenerId, int scannerId, const SoftBusBleScanParams *param); - -int SoftBusStopScan(int listenerId, int scannerId); - -int SoftBusStopScanImmediately(int listenerId, int scannerId); - -int SoftBusGetAdvChannel(const SoftBusAdvCallback *callback, int *scannerId, bool isLpDeviceScan); - -int SoftBusReleaseAdvChannel(int advId); - -int SoftBusSetAdvData(int advId, const SoftBusBleAdvData *data); - -int SoftBusStartAdv(int advId, const SoftBusBleAdvParams *param); - -int SoftBusStartAdvEx(int advId, const SoftBusBleAdvParams *param, - int (*startAdvEx)(int *, const SoftBusBleAdvParams *, const SoftBusBleAdvData *)); - -int SoftBusStopAdv(int advId); - -int SoftBusUpdateAdv(int advId, const SoftBusBleAdvData *data, const SoftBusBleAdvParams *param); - -int SoftBusReplaceAdvertisingAdv(int advId, const SoftBusBleAdvData *data); - -bool SoftBusIsLpDeviceAvailable(void); - -bool SoftBusSetAdvFilterParam(int advHandle, int advId, SoftBusBleAdvParams *advParam, - int listenerId, SoftBusBleScanParams *scanParam); - -int32_t SoftBusGetAdvHandle(int32_t advId, int32_t *advHandle); - -int32_t SoftBusEnableSyncDataToLpDevice(void); - -int32_t SoftBusDisableSyncDataToLpDevice(void); - -int32_t SoftBusDeregisterScanCallbacks(int32_t scannerId); - -int32_t SoftBusSetScanReportChannelToLpDevice(int32_t scannerId, bool enable); - -int32_t SoftBusSetLpDeviceParam(int duration, int maxExtAdvEvents, int window, - int interval, int advHandle); -#ifdef __cplusplus -#if __cplusplus -} -#endif /* __cplusplus */ -#endif /* __cplusplus */ -#endif /* SOFTBUS_ADAPTER_BLE_GATT_H */ diff --git a/adapter/common/net/bluetooth/net_bluetooth.gni b/adapter/common/net/bluetooth/net_bluetooth.gni index 961f7194c..e8b1fca72 100644 --- a/adapter/common/net/bluetooth/net_bluetooth.gni +++ b/adapter/common/net/bluetooth/net_bluetooth.gni @@ -23,7 +23,6 @@ adapter_ble_inc = [ "$dsoftbus_root_path/adapter/common/net/bluetooth/broadcast/adapter/ble/include", ] adapter_ble_src = [ - "$dsoftbus_root_path/adapter/common/net/bluetooth/ble/softbus_adapter_ble_gatt.c", "$dsoftbus_root_path/adapter/common/net/bluetooth/ble/softbus_adapter_ble_gatt_client.c", "$dsoftbus_root_path/adapter/common/net/bluetooth/ble/softbus_adapter_ble_gatt_server.c", "$dsoftbus_root_path/adapter/common/net/bluetooth/common/adapter_bt_utils.c", diff --git a/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_heartbeat_ctrl.c b/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_heartbeat_ctrl.c index 6d734ce44..f5d6e42de 100644 --- a/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_heartbeat_ctrl.c +++ b/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_heartbeat_ctrl.c @@ -34,9 +34,9 @@ #include "lnn_net_builder.h" #include "lnn_ohos_account.h" -#include "softbus_adapter_ble_gatt.h" #include "softbus_adapter_bt_common.h" #include "softbus_adapter_mem.h" +#include "softbus_broadcast_type.h" #include "softbus_def.h" #include "softbus_errcode.h" #include "softbus_hisysevt_bus_center.h" @@ -333,12 +333,12 @@ static void HbChangeMediumParamByState(SoftBusScreenState state) }; switch (state) { case SOFTBUS_SCREEN_ON: - param.info.ble.scanInterval = SOFTBUS_BLE_SCAN_INTERVAL_P10; - param.info.ble.scanWindow = SOFTBUS_BLE_SCAN_WINDOW_P10; + param.info.ble.scanInterval = SOFTBUS_BC_SCAN_INTERVAL_P10; + param.info.ble.scanWindow = SOFTBUS_BC_SCAN_WINDOW_P10; break; case SOFTBUS_SCREEN_OFF: - param.info.ble.scanInterval = SOFTBUS_BLE_SCAN_INTERVAL_P2; - param.info.ble.scanWindow = SOFTBUS_BLE_SCAN_WINDOW_P2; + param.info.ble.scanInterval = SOFTBUS_BC_SCAN_INTERVAL_P2; + param.info.ble.scanWindow = SOFTBUS_BC_SCAN_WINDOW_P2; break; default: LNN_LOGD(LNN_HEART_BEAT, "ctrl reset ble scan medium param get invalid state"); diff --git a/core/bus_center/lnn/lane_hub/lane_hub.gni b/core/bus_center/lnn/lane_hub/lane_hub.gni index 78a784a08..6df4e2755 100644 --- a/core/bus_center/lnn/lane_hub/lane_hub.gni +++ b/core/bus_center/lnn/lane_hub/lane_hub.gni @@ -74,6 +74,7 @@ if (dsoftbus_feature_lnn_net) { if (dsoftbus_feature_lnn_heartbeat) { bus_center_hub_inc += [ "$dsoftbus_root_path/adapter/common/net/bluetooth/include", + "$dsoftbus_root_path/adapter/common/net/bluetooth/broadcast/interface", "$dsoftbus_core_path/core/adapter/bus_center/include", "$dsoftbus_core_path/core/authentication/interface", "$dsoftbus_core_path/connection/p2p/interface", diff --git a/core/discovery/ble/softbus_ble/src/disc_ble.c b/core/discovery/ble/softbus_ble/src/disc_ble.c index b242705c2..2a8517045 100644 --- a/core/discovery/ble/softbus_ble/src/disc_ble.c +++ b/core/discovery/ble/softbus_ble/src/disc_ble.c @@ -29,7 +29,6 @@ #include "lnn_ohos_account.h" #include "message_handler.h" #include "securec.h" -#include "softbus_adapter_ble_gatt.h" #include "softbus_adapter_bt_common.h" #include "softbus_adapter_crypto.h" #include "softbus_adapter_mem.h" @@ -318,7 +317,7 @@ static int32_t ScanFilter(const BroadcastReportInfo *reportInfo) uint32_t advLen = reportInfo->packet.bcData.payloadLen; uint8_t *advData = reportInfo->packet.bcData.payload; - DISC_CHECK_AND_RETURN_RET_LOGE(reportInfo->dataStatus == SOFTBUS_BLE_DATA_COMPLETE, SOFTBUS_ERR, DISC_BLE, + DISC_CHECK_AND_RETURN_RET_LOGE(reportInfo->dataStatus == SOFTBUS_BC_DATA_COMPLETE, SOFTBUS_ERR, DISC_BLE, "dataStatus is invalid. dataStatus=%{public}u", reportInfo->dataStatus); DISC_CHECK_AND_RETURN_RET_LOGE(advData != NULL, SOFTBUS_ERR, DISC_BLE, "advData is null"); DISC_CHECK_AND_RETURN_RET_LOGE(advLen >= POS_TLV, SOFTBUS_ERR, DISC_BLE, @@ -1762,7 +1761,7 @@ DiscoveryBleDispatcherInterface *DiscSoftBusBleInit(DiscInnerCallback *callback) g_discBleInnerCb = callback; if (SoftBusMutexInit(&g_recvMessageInfo.lock, NULL) != SOFTBUS_OK || - SoftBusMutexInit(&g_bleInfoLock, NULL) != SOFTBUS_OK || BleGattLockInit() != SOFTBUS_OK) { + SoftBusMutexInit(&g_bleInfoLock, NULL) != SOFTBUS_OK) { DISC_LOGE(DISC_INIT, "init ble lock failed"); return NULL; } diff --git a/tests/adapter/bluetooth/BUILD.gn b/tests/adapter/bluetooth/BUILD.gn index 5d257ebb0..c2d4d45b3 100644 --- a/tests/adapter/bluetooth/BUILD.gn +++ b/tests/adapter/bluetooth/BUILD.gn @@ -45,16 +45,6 @@ if (!defined(ohos_lite)) { bt_test_external_deps += [ "bluetooth:btframework" ] } - ohos_unittest("AdapterBleGattTest") { - module_out_path = module_output_path - sources = bt_test_sources - sources += [ "softbus_adapter_ble_gatt_test.cpp" ] - - include_dirs = bt_test_inc - deps = bt_test_deps - external_deps = bt_test_external_deps - } - ohos_unittest("AdapterBleGattServerTest") { module_out_path = module_output_path sources = bt_test_sources @@ -103,7 +93,6 @@ if (!defined(ohos_lite)) { deps += [ # ":AdapterBleGattClientTest", # ":AdapterBleGattServerTest", - # ":AdapterBleGattTest", # ":AdapterBtCommonTest", # ":AdapterBtUtilsTest", ] diff --git a/tests/adapter/bluetooth/assert_helper.h b/tests/adapter/bluetooth/assert_helper.h index a026da7e5..e95ddf261 100644 --- a/tests/adapter/bluetooth/assert_helper.h +++ b/tests/adapter/bluetooth/assert_helper.h @@ -20,7 +20,6 @@ #include "gmock/gmock.h" -#include "softbus_adapter_ble_gatt.h" #include "softbus_adapter_ble_gatt_client.h" #include "softbus_adapter_bt_common.h" #include "softbus_adapter_mem.h" diff --git a/tests/adapter/bluetooth/softbus_adapter_ble_gatt_test.cpp b/tests/adapter/bluetooth/softbus_adapter_ble_gatt_test.cpp deleted file mode 100644 index 59328dcf4..000000000 --- a/tests/adapter/bluetooth/softbus_adapter_ble_gatt_test.cpp +++ /dev/null @@ -1,620 +0,0 @@ -/* - * Copyright (c) 2022 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 "gmock/gmock.h" -#include "gtest/gtest.h" - -#include "c_header/ohos_bt_gatt.h" -#include "softbus_adapter_ble_gatt.h" -#include "softbus_errcode.h" -#include "conn_log.h" -#include "assert_helper.h" -#include "bluetooth_mock.h" - -using namespace testing::ext; -using ::testing::AtMost; -using ::testing::Return; - -namespace OHOS { - -class ScanResultCtx : public RecordCtx { -public: - explicit ScanResultCtx(const char *identifier); - ~ScanResultCtx(); - bool Update(int id, const SoftBusBleScanResult *scanResult); - testing::AssertionResult Expect(int id, const SoftBusBleScanResult *scanResult); -private: - SoftBusBleScanResult scanResult; - void Reset(); -}; - -class AdapterBleGattTest : public testing::Test { -public: - static StRecordCtx scanStartCtx; - static StRecordCtx scanStopCtx; - static ScanResultCtx scanResultCtx; - - static StRecordCtx advEnableCtx; - static StRecordCtx advDisableCtx; - static StRecordCtx advDataCtx; - static StRecordCtx advUpdateCtx; - // btInnerAdvId 模拟蓝牙生成的广播id - static int btInnerAdvId; - - static void SetUpTestCase(void); -}; - -StRecordCtx AdapterBleGattTest::scanStartCtx("OnScanStart"); -StRecordCtx AdapterBleGattTest::scanStopCtx("OnScanStop"); -ScanResultCtx AdapterBleGattTest::scanResultCtx("OnScanResult"); - -StRecordCtx AdapterBleGattTest::advEnableCtx("AdvEnableCallback"); -StRecordCtx AdapterBleGattTest::advDisableCtx("AdvDisableCallback"); -StRecordCtx AdapterBleGattTest::advDataCtx("AdvDataCallback"); -StRecordCtx AdapterBleGattTest::advUpdateCtx("AdvUpdateCallback"); -int AdapterBleGattTest::btInnerAdvId = -1; - -void AdapterBleGattTest::SetUpTestCase() -{ - BleGattLockInit(); -} - -static void StubOnScanStart(int listenerId, int status) -{ - AdapterBleGattTest::scanStartCtx.Update(listenerId, status); -} - -static void StubOnScanStop(int listenerId, int status) -{ - AdapterBleGattTest::scanStopCtx.Update(listenerId, status); -} - -static void StubOnScanResult(int listenerId, const SoftBusBleScanResult *scanResultdata) -{ - AdapterBleGattTest::scanResultCtx.Update(listenerId, scanResultdata); -} - -SoftBusScanListener *GetStubScanListener() -{ - static SoftBusScanListener listener = { - .OnScanStart = StubOnScanStart, - .OnScanStop = StubOnScanStop, - .OnScanResult = StubOnScanResult, - .OnScanStateChanged = NULL - }; - return &listener; -} - -static void StubAdvEnableCallback(int advId, int status) -{ - AdapterBleGattTest::advEnableCtx.Update(advId, status); -} - -static void StubAdvDisableCallback(int advId, int status) -{ - AdapterBleGattTest::advDisableCtx.Update(advId, status); -} - -static void StubAdvDataCallback(int advId, int status) -{ - AdapterBleGattTest::advDataCtx.Update(advId, status); -} - -static void StubAdvUpdateCallback(int advId, int status) -{ - AdapterBleGattTest::advUpdateCtx.Update(advId, status); -} - -// Notice:考虑到BleStartAdvEx的回调需要异步触发,实现会导致专注点不在用来本身。这里不手动mock, -// !!!IMPORANT: 一定需要手动触发成功回调,否则回导致adapter状态异常!!! -static int ActionSuccessBleStartAdvEx(int *advId, const StartAdvRawData rawData, BleAdvParams advParam) -{ - static int advIdGenerator = 0; - *advId = advIdGenerator++; - AdapterBleGattTest::btInnerAdvId = *advId; - return OHOS_BT_STATUS_SUCCESS; -} - -static int ActionSuccessBleStopAdv(int advId) -{ - MockBluetooth::btGattCallback->advDisableCb(advId, SOFTBUS_BT_STATUS_SUCCESS); - return OHOS_BT_STATUS_SUCCESS; -} - -SoftBusAdvCallback *GetStubAdvCallback() -{ - static SoftBusAdvCallback callback = {.AdvEnableCallback = StubAdvEnableCallback, - .AdvDisableCallback = StubAdvDisableCallback, - .AdvDataCallback = StubAdvDataCallback, - .AdvUpdateCallback = StubAdvUpdateCallback, - .LpDeviceInfoCallback = NULL}; - return &callback; -} - -static testing::AssertionResult PrepareScanListener(MockBluetooth &mocker, int *outId, int *scannerId) -{ - auto id = SoftBusAddScanListener(GetStubScanListener(), scannerId, false); - if (id == SOFTBUS_ERR) { - return testing::AssertionFailure() << "SoftBusAddScanListener failed"; - } - if (MockBluetooth::btGattCallback == nullptr) { - return testing::AssertionFailure() << "BleGattRegisterCallbacks is not invoke"; - } - *outId = id; - return testing::AssertionSuccess(); -} - -static testing::AssertionResult PrepareAdvCallback(MockBluetooth &mocker, int *outId, int *scannerId) -{ - EXPECT_CALL(mocker, BleStartAdvEx).WillRepeatedly(ActionSuccessBleStartAdvEx); - EXPECT_CALL(mocker, BleStopAdv).WillRepeatedly(ActionSuccessBleStopAdv); - auto id = SoftBusGetAdvChannel(GetStubAdvCallback(), scannerId, false); - if (id == SOFTBUS_ERR) { - return testing::AssertionFailure() << "GetStubAdvCallback failed"; - } - if (MockBluetooth::btGattCallback == nullptr) { - return testing::AssertionFailure() << "BleGattRegisterCallbacks is not invoke"; - } - *outId = id; - return testing::AssertionSuccess(); -} - -static SoftBusBleScanFilter *CreateScanFilter() -{ - unsigned char serviceData[] = {0xE, 0xE, 0xF, 0xF, 0x04, 0x05}; - int len = sizeof(serviceData); - - SoftBusBleScanFilter *filter = (SoftBusBleScanFilter *)SoftBusCalloc(sizeof(SoftBusBleScanFilter)); - unsigned char *serviceDataPtr = (unsigned char *)SoftBusCalloc(len); - unsigned char *serviceDataMaskPtr = (unsigned char *)SoftBusCalloc(len); - if (filter == nullptr || serviceDataPtr == nullptr || serviceDataMaskPtr == nullptr) { - goto EXIT; - } - if (memcpy_s(serviceDataPtr, len, serviceData, len) != EOK) { - goto EXIT; - } - if (memset_s(serviceDataMaskPtr, len, 0xFF, len) != EOK) { - goto EXIT; - } - filter->serviceData = serviceDataPtr; - filter->serviceDataMask = serviceDataMaskPtr; - filter->serviceDataLength = len; - return filter; -EXIT: - SoftBusFree(filter); - SoftBusFree(serviceDataPtr); - SoftBusFree(serviceDataMaskPtr); - return nullptr; -} - -/** - * @tc.name: AdapterBleGattTest_SoftBusAddScanListener - * @tc.desc: test add scan listener - * @tc.type: FUNC - * @tc.require: NONE - */ -HWTEST_F(AdapterBleGattTest, SoftBusAddScanListener, TestSize.Level3) -{ - MockBluetooth mocker; - int scannerId = -1; - ASSERT_EQ(SoftBusAddScanListener(nullptr, &scannerId, false), SOFTBUS_ERR); - - int scanListerIds[SCAN_MAX_NUM]; - for (size_t i = 0; i < SCAN_MAX_NUM; i++) { - scanListerIds[i] = SoftBusAddScanListener(GetStubScanListener(), &scannerId, false); - ASSERT_NE(scanListerIds[i], SOFTBUS_ERR); - } - - ASSERT_EQ(SoftBusAddScanListener(GetStubScanListener(), &scannerId, false), SOFTBUS_ERR); - - for (size_t i = 0; i < SCAN_MAX_NUM; i++) { - ASSERT_EQ(SoftBusRemoveScanListener(scanListerIds[i]), SOFTBUS_OK); - } -} - -/** - * @tc.name: AdapterBleGattTest_SoftBusRemoveScanListener - * @tc.desc: test remove scan listener - * @tc.type: FUNC - * @tc.require: NONE - */ -HWTEST_F(AdapterBleGattTest, SoftBusRemoveScanListener, TestSize.Level3) -{ - MockBluetooth mocker; - int listenerId = -1; - int scannerId = -1; - auto result = PrepareScanListener(mocker, &listenerId, &scannerId); - ASSERT_TRUE(result); - - ASSERT_EQ(SoftBusRemoveScanListener(-1), SOFTBUS_INVALID_PARAM); - ASSERT_EQ(SoftBusRemoveScanListener(SCAN_MAX_NUM), SOFTBUS_INVALID_PARAM); - ASSERT_EQ(SoftBusRemoveScanListener(listenerId), SOFTBUS_OK); -} - -/** - * @tc.name: AdapterBleGattTest_SoftBusSetScanFilter - * @tc.desc: test set scan filter - * @tc.type: FUNC - * @tc.require: NONE - */ -HWTEST_F(AdapterBleGattTest, SoftBusSetScanFilter, TestSize.Level3) -{ - MockBluetooth mocker; - int listenerId = -1; - int scannerId = -1; - auto result = PrepareScanListener(mocker, &listenerId, &scannerId); - ASSERT_TRUE(result); - - auto filter = CreateScanFilter(); - ASSERT_NE(filter, nullptr); - - ASSERT_EQ(SoftBusSetScanFilter(listenerId, nullptr, 0), SOFTBUS_INVALID_PARAM); - ASSERT_EQ(SoftBusSetScanFilter(listenerId, filter, 0), SOFTBUS_INVALID_PARAM); - - // not exist scaner - ASSERT_EQ(SoftBusSetScanFilter(SCAN_MAX_NUM - 1, filter, 1), SOFTBUS_ERR); - ASSERT_EQ(SoftBusSetScanFilter(listenerId, filter, 1), SOFTBUS_OK); - ASSERT_EQ(SoftBusRemoveScanListener(listenerId), SOFTBUS_OK); -} - -/** - * @tc.name: AdapterBleGattTest_ScanLifecycle - * @tc.desc: test complete scan life cycle - * @tc.type: FUNC - * @tc.require: NONE - */ -HWTEST_F(AdapterBleGattTest, ScanLifecycle, TestSize.Level3) -{ - MockBluetooth mocker; - int listenerId = -1; - int scannerId = -1; - auto result = PrepareScanListener(mocker, &listenerId, &scannerId); - ASSERT_TRUE(result); - - auto filter = CreateScanFilter(); - ASSERT_NE(filter, nullptr); - auto ret = SoftBusSetScanFilter(listenerId, filter, 1); - ASSERT_EQ(ret, SOFTBUS_OK); - - SoftBusBleScanParams scanParam = { - .scanInterval = 60, - .scanWindow = 600, - .scanType = 1, - .scanPhy = 1, - .scanFilterPolicy = 0, - }; - - EXPECT_CALL(mocker, BleStartScanEx).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS)); - ret = SoftBusStartScan(listenerId, scannerId, &scanParam); - ASSERT_EQ(ret, SOFTBUS_OK); - ASSERT_TRUE(scanStartCtx.Expect(listenerId, SOFTBUS_BT_STATUS_SUCCESS)); - - const unsigned char scanDataExample[] = {0x02, 0x01, 0x02, 0x15, 0x16, 0xEE, 0xFD, 0x04, 0x05, 0x90, 0x00, 0x00, - 0x04, 0x00, 0x18, 0x33, 0x39, 0x36, 0x62, 0x33, 0x61, 0x33, 0x31, 0x21, 0x00, 0x02, 0x0A, 0xEF, 0x03, 0xFF, - 0x7D, 0x02}; - BtScanResultData mockScanResult = {0}; - mockScanResult.advLen = sizeof(scanDataExample); - mockScanResult.advData = (unsigned char *)scanDataExample; - - - SoftBusBleScanResult expectScanResult = {0}; - expectScanResult.advLen = sizeof(scanDataExample); - expectScanResult.advData = (unsigned char *)scanDataExample; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - EXPECT_CALL(mocker, BleStopScan).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS)); - ret = SoftBusStopScan(listenerId, scannerId); - ASSERT_EQ(ret, SOFTBUS_OK); - ASSERT_TRUE(scanStopCtx.Expect(listenerId, SOFTBUS_BT_STATUS_SUCCESS)); - - ASSERT_EQ(SoftBusRemoveScanListener(listenerId), SOFTBUS_OK); -} - -/** - * @tc.name: AdapterBleGattTest_SoftBusStopScanImmediately - * @tc.desc: test stop ble scan immediately - * @tc.type: FUNC - * @tc.require: NONE - */ -HWTEST_F(AdapterBleGattTest, SoftBusStopScanImmediately, TestSize.Level3) -{ - MockBluetooth mocker; - int listenerId = -1; - int scannerId = -1; - auto result = PrepareScanListener(mocker, &listenerId, &scannerId); - ASSERT_TRUE(result); - - auto filter = CreateScanFilter(); - ASSERT_NE(filter, nullptr); - auto ret = SoftBusSetScanFilter(listenerId, filter, 1); - ASSERT_EQ(ret, SOFTBUS_OK); - - SoftBusBleScanParams scanParam = { - .scanInterval = 60, - .scanWindow = 600, - .scanType = 1, - .scanPhy = 1, - .scanFilterPolicy = 0, - }; - - EXPECT_CALL(mocker, BleStartScanEx).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS)); - ret = SoftBusStartScan(listenerId, scannerId, &scanParam); - ASSERT_EQ(ret, SOFTBUS_OK); - ASSERT_TRUE(scanStartCtx.Expect(listenerId, SOFTBUS_BT_STATUS_SUCCESS)); - - const unsigned char scanDataExample[] = {0x02, 0x01, 0x02, 0x15, 0x16, 0xEE, 0xFD, 0x04, 0x05, 0x90, 0x00, 0x00, - 0x04, 0x00, 0x18, 0x33, 0x39, 0x36, 0x62, 0x33, 0x61, 0x33, 0x31, 0x21, 0x00, 0x02, 0x0A, 0xEF, 0x03, 0xFF, - 0x7D, 0x02}; - BtScanResultData mockScanResult = {0}; - mockScanResult.advLen = sizeof(scanDataExample); - mockScanResult.advData = (unsigned char *)scanDataExample; - - SoftBusBleScanResult expectScanResult = {0}; - expectScanResult.advLen = sizeof(scanDataExample); - expectScanResult.advData = (unsigned char *)scanDataExample; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - EXPECT_CALL(mocker, BleStopScan).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS)); - ret = SoftBusStopScanImmediately(listenerId, scannerId); - ASSERT_EQ(ret, SOFTBUS_OK); - ASSERT_TRUE(scanStopCtx.Expect(listenerId, SOFTBUS_BT_STATUS_SUCCESS)); - - ASSERT_EQ(SoftBusRemoveScanListener(listenerId), SOFTBUS_OK); -} - -/** - * @tc.name: AdapterBleGattTest_ScanResultCb - * @tc.desc: test scan result callback - * @tc.type: FUNC - * @tc.require: NONE - */ -HWTEST_F(AdapterBleGattTest, ScanResultCb, TestSize.Level3) -{ - MockBluetooth mocker; - int listenerId = -1; - int scannerId = -1; - auto result = PrepareScanListener(mocker, &listenerId, &scannerId); - ASSERT_TRUE(result); - - auto filter = CreateScanFilter(); - ASSERT_NE(filter, nullptr); - auto ret = SoftBusSetScanFilter(listenerId, filter, 1); - ASSERT_EQ(ret, SOFTBUS_OK); - - SoftBusBleScanParams scanParam = { - .scanInterval = 60, - .scanWindow = 600, - .scanType = 1, - .scanPhy = 1, - .scanFilterPolicy = 0, - }; - EXPECT_CALL(mocker, BleStartScanEx).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS)); - ASSERT_EQ(SoftBusStartScan(listenerId, scannerId, nullptr), SOFTBUS_INVALID_PARAM); - ASSERT_EQ(SoftBusStartScan(listenerId, scannerId, &scanParam), SOFTBUS_OK); - ASSERT_TRUE(scanStartCtx.Expect(listenerId, SOFTBUS_BT_STATUS_SUCCESS)); - - const unsigned char scanDataExample[] = {0x02, 0x01, 0x02, 0x15, 0x16, 0xEE, 0xFD, 0x04, 0x05, 0x90, 0x00, 0x00, - 0x04, 0x00, 0x18, 0x33, 0x39, 0x36, 0x62, 0x33, 0x61, 0x33, 0x31, 0x21, 0x00, 0x02, 0x0A, 0xEF, 0x03, 0xFF, - 0x7D, 0x02}; - SoftBusBleScanResult expectScanResult = {0}; - expectScanResult.advLen = sizeof(scanDataExample); - expectScanResult.advData = (unsigned char *)scanDataExample; - BtScanResultData mockScanResult = {0}; - mockScanResult.advLen = sizeof(scanDataExample); - mockScanResult.advData = (unsigned char *)scanDataExample; - - mockScanResult.eventType = OHOS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE; - mockScanResult.dataStatus = OHOS_BLE_DATA_COMPLETE; - mockScanResult.addrType = OHOS_BLE_PUBLIC_DEVICE_ADDRESS; - mockScanResult.primaryPhy = OHOS_BLE_SCAN_PHY_NO_PACKET; - mockScanResult.secondaryPhy = OHOS_BLE_SCAN_PHY_NO_PACKET; - mockScanResult.directAddrType = OHOS_BLE_PUBLIC_DEVICE_ADDRESS; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - mockScanResult.eventType = OHOS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE_DIRECTED; - mockScanResult.dataStatus = OHOS_BLE_DATA_INCOMPLETE_MORE_TO_COME; - mockScanResult.addrType = OHOS_BLE_RANDOM_DEVICE_ADDRESS; - mockScanResult.primaryPhy = OHOS_BLE_SCAN_PHY_1M; - mockScanResult.secondaryPhy = OHOS_BLE_SCAN_PHY_1M; - mockScanResult.directAddrType = OHOS_BLE_RANDOM_DEVICE_ADDRESS; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - mockScanResult.eventType = OHOS_BLE_EVT_CONNECTABLE; - mockScanResult.dataStatus = OHOS_BLE_DATA_INCOMPLETE_TRUNCATED; - mockScanResult.addrType = OHOS_BLE_PUBLIC_IDENTITY_ADDRESS; - mockScanResult.primaryPhy = OHOS_BLE_SCAN_PHY_2M; - mockScanResult.secondaryPhy = OHOS_BLE_SCAN_PHY_2M; - mockScanResult.directAddrType = OHOS_BLE_PUBLIC_IDENTITY_ADDRESS; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - mockScanResult.eventType = OHOS_BLE_EVT_CONNECTABLE_DIRECTED; - mockScanResult.dataStatus = UINT8_MAX; - mockScanResult.addrType = OHOS_BLE_RANDOM_STATIC_IDENTITY_ADDRESS; - mockScanResult.primaryPhy = OHOS_BLE_SCAN_PHY_CODED; - mockScanResult.secondaryPhy = OHOS_BLE_SCAN_PHY_CODED; - mockScanResult.directAddrType = OHOS_BLE_RANDOM_STATIC_IDENTITY_ADDRESS; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - mockScanResult.eventType = OHOS_BLE_EVT_SCANNABLE; - mockScanResult.addrType = OHOS_BLE_UNRESOLVABLE_RANDOM_DEVICE_ADDRESS; - mockScanResult.primaryPhy = UINT8_MAX; - mockScanResult.secondaryPhy = UINT8_MAX; - mockScanResult.directAddrType = OHOS_BLE_UNRESOLVABLE_RANDOM_DEVICE_ADDRESS; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - mockScanResult.eventType = OHOS_BLE_EVT_SCANNABLE_DIRECTED; - mockScanResult.addrType = OHOS_BLE_NO_ADDRESS; - mockScanResult.directAddrType = OHOS_BLE_NO_ADDRESS; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - mockScanResult.eventType = OHOS_BLE_EVT_LEGACY_NON_CONNECTABLE; - mockScanResult.addrType = UINT8_MAX; - mockScanResult.directAddrType = UINT8_MAX; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - mockScanResult.eventType = OHOS_BLE_EVT_LEGACY_SCANNABLE; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - mockScanResult.eventType = OHOS_BLE_EVT_LEGACY_CONNECTABLE; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - mockScanResult.eventType = OHOS_BLE_EVT_LEGACY_CONNECTABLE_DIRECTED; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - mockScanResult.eventType = OHOS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV_SCAN; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - mockScanResult.eventType = OHOS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); - - mockScanResult.eventType = UINT8_MAX; - ASSERT_FALSE(scanResultCtx.Expect(listenerId, &expectScanResult)); -} - -/** - * @tc.name: AdapterBleGattTest_AdvertiseLifecycle - * @tc.desc: test complete Advertisement life cycle - * @tc.type: FUNC - * @tc.require: NONE - */ -HWTEST_F(AdapterBleGattTest, AdvertiseLifecycle, TestSize.Level3) -{ - MockBluetooth mocker; - int advId = 0; - int scannerId = -1; - auto result = PrepareAdvCallback(mocker, &advId, &scannerId); - ASSERT_TRUE(result); - - const char advDataExample[] = {0x02, 0x01, 0x02, 0x15, 0x16, 0xEE, 0xFD, 0x04, 0x05, 0x90, 0x00, 0x00, 0x04, 0x00, - 0x18, 0x33, 0x39, 0x36, 0x62, 0x33, 0x61, 0x33, 0x31, 0x21, 0x00, 0x02, 0x0A, 0xEF}; - const unsigned char scanRspDataExample[] = {0x03, 0xFF, 0x7D, 0x02}; - SoftBusBleAdvData data = {.advLength = sizeof(advDataExample), - .advData = (uint8_t *)advDataExample, - .scanRspLength = sizeof(scanRspDataExample), - .scanRspData = (uint8_t *)scanRspDataExample}; - ASSERT_EQ(SoftBusSetAdvData(advId, &data), SOFTBUS_OK); - ASSERT_TRUE(advDataCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS)); - - SoftBusBleAdvParams params = {0}; - ASSERT_EQ(SoftBusStartAdv(advId, ¶ms), SOFTBUS_OK); - ASSERT_FALSE(advEnableCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS)); - // 模拟蓝牙广播成功回调, 广播成功会被再次回调, adapter状态才能恢复正常 - MockBluetooth::btGattCallback->advEnableCb(btInnerAdvId, SOFTBUS_BT_STATUS_SUCCESS); - ASSERT_TRUE(advEnableCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS)); - - MockBluetooth::btGattCallback->advDataCb(btInnerAdvId, SOFTBUS_BT_STATUS_SUCCESS); - ASSERT_TRUE(advDataCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS)); - - ASSERT_EQ(SoftBusUpdateAdv(advId, nullptr, ¶ms), SOFTBUS_INVALID_PARAM); - ASSERT_EQ(SoftBusUpdateAdv(advId, &data, nullptr), SOFTBUS_INVALID_PARAM); - ASSERT_EQ(SoftBusUpdateAdv(advId, &data, ¶ms), SOFTBUS_OK); - ASSERT_FALSE(advEnableCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS)); - ASSERT_TRUE(advDisableCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS)); - // 模拟蓝牙广播成功回调, 广播成功会被再次回调, adapter状态才能恢复正常 - MockBluetooth::btGattCallback->advEnableCb(btInnerAdvId, SOFTBUS_BT_STATUS_SUCCESS); - ASSERT_TRUE(advEnableCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS)); - - MockBluetooth::btGattCallback->advUpdateCb(btInnerAdvId, SOFTBUS_BT_STATUS_SUCCESS); - ASSERT_TRUE(advUpdateCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS)); - - MockBluetooth::btGattCallback->securityRespondCb(nullptr); - - ASSERT_EQ(SoftBusStopAdv(advId), SOFTBUS_OK); - ASSERT_TRUE(advDisableCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS)); - - ASSERT_EQ(SoftBusReleaseAdvChannel(advId), SOFTBUS_OK); - ASSERT_EQ(SoftBusReleaseAdvChannel(advId), SOFTBUS_ERR); - ASSERT_EQ(SoftBusReleaseAdvChannel(-1), SOFTBUS_ERR); - ASSERT_EQ(SoftBusReleaseAdvChannel(ADV_MAX_NUM), SOFTBUS_ERR); -} - -/** - * @tc.name: AdapterBleGattTest_SoftBusGetAdvChannel - * @tc.desc: test get adv channel - * @tc.type: FUNC - * @tc.require: NONE - */ -HWTEST_F(AdapterBleGattTest, SoftBusGetAdvChannel, TestSize.Level3) -{ - int scannerId = -1; - ASSERT_EQ(SoftBusGetAdvChannel(nullptr, &scannerId, false), SOFTBUS_INVALID_PARAM); - MockBluetooth mocker; - int advIds[ADV_MAX_NUM]; - for (size_t i = 0; i < ADV_MAX_NUM; i++) { - advIds[i] = SoftBusGetAdvChannel(GetStubAdvCallback(), &scannerId, false); - ASSERT_NE(advIds[i], SOFTBUS_ERR); - } - ASSERT_EQ(SoftBusGetAdvChannel(GetStubAdvCallback(), &scannerId, false), SOFTBUS_ERR); - for (size_t i = 0; i < ADV_MAX_NUM; i++) { - ASSERT_EQ(SoftBusReleaseAdvChannel(advIds[i]), SOFTBUS_OK); - } -} - -ScanResultCtx::ScanResultCtx(const char *identifier) : RecordCtx(identifier) -{ - Reset(); -} -ScanResultCtx::~ScanResultCtx() -{ - Reset(); -} - -void ScanResultCtx::Reset() -{ - SoftBusFree(scanResult.advData); - scanResult.advData = nullptr; - memset_s(&scanResult, sizeof(SoftBusBtAddr), 0, sizeof(SoftBusBtAddr)); -} - -bool ScanResultCtx::Update(int id, const SoftBusBleScanResult *scanResult) -{ - if (!RecordCtx::Update(id)) { - return false; - } - this->scanResult = *scanResult; - unsigned char *cpyAdvData = (unsigned char *)SoftBusCalloc(this->scanResult.advLen); - if (cpyAdvData == nullptr) { - CONN_LOGE(CONN_BLE, "malloc failed in OnScanResult, can not save ctx, id=%{public}d", id); - return false; - } - - if (memcpy_s(cpyAdvData, this->scanResult.advLen, scanResult->advData, scanResult->advLen) != EOK) { - CONN_LOGE(CONN_BLE, "malloc failed in OnScanResult, can not save ctx, id=%{public}d", id); - SoftBusFree(cpyAdvData); - return false; - } - this->scanResult.advData = cpyAdvData; - return true; -} - -testing::AssertionResult ScanResultCtx::Expect(int id, const SoftBusBleScanResult *scanResultParam) -{ - auto result = RecordCtx::Expect(id); - if (!result) { - goto ClEANUP; - } - - if (this->scanResult.advLen == scanResultParam->advLen && - memcmp(this->scanResult.advData, scanResultParam->advData, scanResultParam->advLen) == 0) { - result = testing::AssertionSuccess(); - goto ClEANUP; - } - result = testing::AssertionFailure() << identifier << " is call by unexpectedly scan result."; -ClEANUP: - Reset(); - return result; -} - -} // namespace OHOS \ No newline at end of file diff --git a/tests/core/bus_center/lnn/BUILD.gn b/tests/core/bus_center/lnn/BUILD.gn index 9d83de767..0a45247f8 100644 --- a/tests/core/bus_center/lnn/BUILD.gn +++ b/tests/core/bus_center/lnn/BUILD.gn @@ -1669,6 +1669,7 @@ if (!defined(ohos_lite) && enhanced_test) { "$dsoftbus_root_path/core/bus_center/service/include", "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/decision_db/include", "$dsoftbus_root_path/adapter/common/net/bluetooth/include", + "$dsoftbus_root_path/adapter/common/net/bluetooth/broadcast/interface", ] include_dirs += bus_center_decision_center_inc diff --git a/tests/core/discovery/ble/softbus_ble/disc_ble_distributed_test.cpp b/tests/core/discovery/ble/softbus_ble/disc_ble_distributed_test.cpp index 55357d541..b3a1f0ec2 100755 --- a/tests/core/discovery/ble/softbus_ble/disc_ble_distributed_test.cpp +++ b/tests/core/discovery/ble/softbus_ble/disc_ble_distributed_test.cpp @@ -138,13 +138,12 @@ HWTEST_F(DiscDistributedBleTest, TestScanFilter001, TestSize.Level1) BroadcastReportInfo reportInfo = {0}; - // when not reportInfo->dataStatus == SOFTBUS_BLE_DATA_COMPLETE - reportInfo.dataStatus = SOFTBUS_BLE_DATA_INCOMPLETE_MORE_TO_COME; + reportInfo.dataStatus = SOFTBUS_BC_DATA_INCOMPLETE_MORE_TO_COME; int32_t ret = ScanFilter(&reportInfo); EXPECT_NE(ret, SOFTBUS_OK); // when not advData != NULL - reportInfo.dataStatus = SOFTBUS_BLE_DATA_COMPLETE; + reportInfo.dataStatus = SOFTBUS_BC_DATA_COMPLETE; ret = ScanFilter(&reportInfo); EXPECT_NE(ret, SOFTBUS_OK); @@ -184,7 +183,7 @@ HWTEST_F(DiscDistributedBleTest, TestScanFilter002, TestSize.Level1) uint8_t payload[POS_TLV] = {0}; BroadcastReportInfo reportInfo = { - .dataStatus = SOFTBUS_BLE_DATA_COMPLETE, + .dataStatus = SOFTBUS_BC_DATA_COMPLETE, .packet = { .bcData = { .payload = &payload[0], @@ -244,7 +243,7 @@ HWTEST_F(DiscDistributedBleTest, TestScanFilter003, TestSize.Level1) uint8_t payload[POS_TLV] = {0}; BroadcastReportInfo reportInfo = { - .dataStatus = SOFTBUS_BLE_DATA_COMPLETE, + .dataStatus = SOFTBUS_BC_DATA_COMPLETE, .packet = { .bcData = { .payload = &payload[0], @@ -471,7 +470,7 @@ HWTEST_F(DiscDistributedBleTest, TestStopScaner001, TestSize.Level1) EXPECT_EQ(ret, SOFTBUS_OK); g_isScanning = true; - g_bleListener.scanListenerId = SCAN_MAX_NUM; + g_bleListener.scanListenerId = SCAN_NUM_MAX; ret = StopScaner(); EXPECT_NE(ret, SOFTBUS_OK); diff --git a/tests/core/discovery/ble/softbus_ble/disc_ble_softbus_test.cpp b/tests/core/discovery/ble/softbus_ble/disc_ble_softbus_test.cpp deleted file mode 100644 index 473000d07..000000000 --- a/tests/core/discovery/ble/softbus_ble/disc_ble_softbus_test.cpp +++ /dev/null @@ -1,1676 +0,0 @@ -/* - * Copyright (c) 2022 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 -#include - -#include "disc_ble.c" -#include "disc_log.h" -#include "softbus_errcode.h" -#include "message_handler.h" - -using namespace testing::ext; -namespace OHOS { -class DiscSoftBusBleTest : public testing::Test { -public: - void SetUp() override {} - void TearDown() override {} -}; - -static int g_info = 0; -static bool g_softbusBtState = BLE_ENABLE; -static InnerDeviceInfoAddtions addtionsTest; - -static void TestOnDeviceFound(const DeviceInfo *device, const InnerDeviceInfoAddtions *addtions) -{ - addtionsTest.medium = addtions->medium; -} - -static DiscInnerCallback g_discInnerCallback = { - .OnDeviceFound = TestOnDeviceFound, -}; - -static DiscInnerCallback g_discInnerCallBack = { - .OnDeviceFound = nullptr, -}; - -int SoftBusGetBtState() -{ - if (g_softbusBtState) { - return BLE_ENABLE; - } - return BLE_DISABLE; -} - -int32_t GetDeviceInfo(DeviceInfo *info) -{ - (void)info; - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, GetDeviceInfo, START"); - if (g_info == 0) { - return 0; - } - return -1; -} - -int SoftBusSetAdvData(int advId, const SoftBusBleAdvData *data) -{ - (void)data; - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, SoftBusSetAdvData, START"); - if (advId == 1 || advId == 2) { - return SOFTBUS_OK; - } - return SOFTBUS_ERR; -} - -int SoftBusStartAdv(int advId, const SoftBusBleAdvParams *param) -{ - (void)param; - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, SoftBusBleAdvParams, START"); - if (advId == 2) { - return SOFTBUS_OK; - } - return SOFTBUS_ERR; -} - -/* -* @tc.name: GetNeedUpdateAdvertiser001 -* @tc.desc: test GetNeedUpdateAdvertiser -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, GetNeedUpdateAdvertiser001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, GetNeedUpdateAdvertiser001, Start"); - int32_t adv = NON_ADV_ID; - ResetInfoUpdate(adv); - int ret = GetNeedUpdateAdvertiser(adv); - EXPECT_EQ(ret, SOFTBUS_OK); - - adv = CON_ADV_ID; - ResetInfoUpdate(CON_ADV_ID); - ret = GetNeedUpdateAdvertiser(CON_ADV_ID); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, GetNeedUpdateAdvertiser001, End"); -} - -/* -* @tc.name: CheckScanner001 -* @tc.desc: test CheckScanner -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, CheckScanner001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, CheckScanner001, Start"); - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].capBitMap[0] = 0x0; - g_bleInfoManager[BLE_SUBSCRIBE | BLE_PASSIVE].capBitMap[0] = 0x0; - g_bleInfoManager[BLE_PUBLISH | BLE_PASSIVE].capBitMap[0] = 0x0; - bool ret = CheckScanner(); - EXPECT_EQ(ret, false); - - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].capBitMap[0] = 0x1; - g_bleInfoManager[BLE_SUBSCRIBE | BLE_PASSIVE].capBitMap[0] = 0x1; - g_bleInfoManager[BLE_PUBLISH | BLE_PASSIVE].capBitMap[0] = 0x1; - ret = CheckScanner(); - EXPECT_EQ(ret, true); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, CheckScanner001, End"); -} - -/* -* @tc.name: ScanFilter001 -* @tc.desc: test ScanFilter -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, ScanFilter001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, ScanFilter001, Start"); - int32_t ret = ScanFilter(nullptr); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - - unsigned char advDataTest[INT32_MAX_BIT_NUM]; - unsigned char advLenTest = sizeof(advDataTest); - SoftBusBleScanResult scanResultDataTest{ - .dataStatus = SOFTBUS_BLE_DATA_COMPLETE, - .advLen = advLenTest, - .advData = advDataTest, - }; - scanResultDataTest.dataStatus = SOFTBUS_BLE_DATA_INCOMPLETE_MORE_TO_COME; - ret = ScanFilter(&scanResultDataTest); - EXPECT_EQ(ret, SOFTBUS_ERR); - - scanResultDataTest.dataStatus = SOFTBUS_BLE_DATA_COMPLETE; - scanResultDataTest.advData[POS_UUID] = (unsigned char) BYTE_MASK; - scanResultDataTest.advData[POS_UUID + 1] = (unsigned char) ((BLE_UUID >> BYTE_SHIFT_BIT) & BYTE_MASK); - ret = ScanFilter(&scanResultDataTest); - EXPECT_EQ(ret, SOFTBUS_ERR); - - scanResultDataTest.advData[POS_UUID] = (unsigned char) (BLE_UUID & BYTE_MASK); - scanResultDataTest.advData[POS_UUID + 1] = (unsigned char) BYTE_MASK; - ret = ScanFilter(&scanResultDataTest); - EXPECT_EQ(ret, SOFTBUS_ERR); - - scanResultDataTest.advData[POS_UUID] = (unsigned char) BYTE_MASK; - scanResultDataTest.advData[POS_UUID + 1] = (unsigned char) BYTE_MASK; - ret = ScanFilter(&scanResultDataTest); - EXPECT_EQ(ret, SOFTBUS_ERR); - - scanResultDataTest.advData[POS_UUID] = (unsigned char) (BLE_UUID & BYTE_MASK); - scanResultDataTest.advData[POS_UUID + 1] = (unsigned char) ((BLE_UUID >> BYTE_SHIFT_BIT) & BYTE_MASK); - scanResultDataTest.advData[POS_VERSION + ADV_HEAD_LEN] = BLE_VERSION; - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].capBitMap[0] = 0x0; - g_bleInfoManager[BLE_SUBSCRIBE | BLE_PASSIVE].capBitMap[0] = 0x0; - g_bleInfoManager[BLE_PUBLISH | BLE_PASSIVE].capBitMap[0] = 0x0; - ret = ScanFilter(&scanResultDataTest); - EXPECT_EQ(ret, SOFTBUS_ERR); - - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].capBitMap[0] = 0x1; - g_bleInfoManager[BLE_SUBSCRIBE | BLE_PASSIVE].capBitMap[0] = 0x1; - g_bleInfoManager[BLE_PUBLISH | BLE_PASSIVE].capBitMap[0] = 0x1; - ret = ScanFilter(&scanResultDataTest); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, ScanFilter001, End"); -} - -/* -* @tc.name: ProcessHwHashAccout001 -* @tc.desc: test ProcessHwHashAccout -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, ProcessHwHashAccout001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, ProcessHwHashAccout001, Start"); - uint32_t pos = 1; - DeviceInfo foundInfoTest; - foundInfoTest.capabilityBitmap[0] = 1 << pos; - foundInfoTest.capabilityBitmap[1] = 0x0; - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].isSameAccount[pos] = false; - bool ret = ProcessHashAccount(&foundInfoTest); - EXPECT_EQ(ret, true); - - foundInfoTest.capabilityBitmap[0] = 1 << pos; - foundInfoTest.capabilityBitmap[1] = 0x0; - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].isSameAccount[pos] = true; - ret = ProcessHashAccount(&foundInfoTest); - EXPECT_EQ(ret, false); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, ProcessHwHashAccout001, End"); -} - -/* -* @tc.name: RangeDevice001 -* @tc.desc: test RangeDevice -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, RangeDevice001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, RangeDevice001, Start"); - DeviceInfo foundInfoTest; - const char rssiTest = 's'; - int8_t powerTest = SOFTBUS_ILLEGAL_BLE_POWER; - RangeDevice(&foundInfoTest, rssiTest, powerTest); - EXPECT_EQ(foundInfoTest.range, -1); - - powerTest = 10; - foundInfoTest.devId[0] = 's'; - RangeDevice(&foundInfoTest, rssiTest, powerTest); - EXPECT_NE(foundInfoTest.range, 1); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, RangeDevice001, End"); -} - -/* -* @tc.name: ProcessDisNonPacket001 -* @tc.desc: test ProcessDisNonPacket -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, ProcessDisNonPacket001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, ProcessDisNonPacket001, Start"); - uint8_t advDataTest[100]; - uint8_t advLenTest = sizeof(advDataTest); - const char rssiTest = 's'; - DeviceInfo foundInfoTest; - ProcessDisNonPacket(nullptr, advLenTest, rssiTest, &foundInfoTest); - - foundInfoTest.capabilityBitmap[0] = 0x0; - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].capBitMap[0] = 0x1; - ProcessDisNonPacket(advDataTest, advLenTest, rssiTest, &foundInfoTest); - - g_discBleInnerCb = &g_discInnerCallback; - ListInit(&g_recvMessageInfo.node); - uint32_t pos = 0; - foundInfoTest.capabilityBitmap[0] = 1 << pos; - foundInfoTest.capabilityBitmap[1] = 0x0; - advDataTest[POS_CAPABLITY + ADV_HEAD_LEN] = 1 << pos; - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].capBitMap[0] = 0x1; - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].isSameAccount[pos] = false; - ProcessDisNonPacket(advDataTest, advLenTest, rssiTest, &foundInfoTest); - - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].isSameAccount[pos] = true; - ProcessDisNonPacket(advDataTest, advLenTest, rssiTest, &foundInfoTest); - EXPECT_NE(addtionsTest.medium, BLE); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, ProcessDisNonPacket001, End"); -} - -/* -* @tc.name: BleScanResultCallback001 -* @tc.desc: test BleScanResultCallback -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleScanResultCallback001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, BleScanResultCallback001, Start"); - int listenerIdTest = 1; - BleScanResultCallback(listenerIdTest, nullptr); - - uint8_t advDataTest[INT32_MAX_BIT_NUM]; - uint8_t advLenTest = sizeof(advDataTest); - SoftBusBleScanResult scanResultDataTest = { - .dataStatus = SOFTBUS_BLE_DATA_COMPLETE, - .advLen = advLenTest, - .advData = advDataTest, - }; - scanResultDataTest.dataStatus = SOFTBUS_BLE_DATA_COMPLETE; - scanResultDataTest.advData[POS_UUID] = (unsigned char) (BLE_UUID & BYTE_MASK); - scanResultDataTest.advData[POS_UUID + 1] = (unsigned char) ((BLE_UUID >> BYTE_SHIFT_BIT) & BYTE_MASK); - scanResultDataTest.advData[POS_VERSION + ADV_HEAD_LEN] = BLE_VERSION; - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].capBitMap[0] = 0x1; - g_bleInfoManager[BLE_SUBSCRIBE | BLE_PASSIVE].capBitMap[0] = 0x1; - g_bleInfoManager[BLE_PUBLISH | BLE_PASSIVE].capBitMap[0] = 0x1; - BleScanResultCallback(listenerIdTest, &scanResultDataTest); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, BleScanResultCallback001, End"); -} - -/* -* @tc.name: BleOnStateChanged001 -* @tc.desc: test BleOnStateChanged -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleOnStateChanged001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, BleOnStateChanged001, Start"); - LooperInit(); - EXPECT_TRUE(DiscBleLooperInit() == SOFTBUS_OK); - - int listenerIdTest = 1; - BleOnStateChanged(listenerIdTest, SOFTBUS_BT_STATE_TURN_ON); - BleOnStateChanged(listenerIdTest, SOFTBUS_BT_STATE_TURN_OFF); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, BleOnStateChanged001, End"); -} - -/* -* @tc.name: GetWakeRemote001 -* @tc.desc: test GetWakeRemote -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, GetWakeRemote001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, GetWakeRemote001, Start"); - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].isWakeRemote[0] = true; - EXPECT_TRUE(GetWakeRemote() == true); - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].isWakeRemote[0] = false; - EXPECT_TRUE(GetWakeRemote() == false); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, GetWakeRemote001, End"); -} - -/* -* @tc.name: GetConDeviceInfo001 -* @tc.desc: test GetConDeviceInfo -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, GetConDeviceInfo001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, GetConDeviceInfo001, Start"); - int32_t ret = GetConDeviceInfo(nullptr); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].capBitMap[0] = 0x0; - DeviceInfo foundInfoTest; - ret = GetConDeviceInfo(&foundInfoTest); - EXPECT_EQ(ret, SOFTBUS_ERR); - - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].capBitMap[0] = 0x1; - ret = GetConDeviceInfo(&foundInfoTest); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, GetConDeviceInfo001, End"); -} - -/* -* @tc.name: GetNonDeviceInfo001 -* @tc.desc: test GetNonDeviceInfo -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, GetNonDeviceInfo001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, GetNonDeviceInfo001, Start"); - int32_t ret = GetNonDeviceInfo(nullptr); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - - DeviceInfo foundInfoTest; - g_bleInfoManager[BLE_PUBLISH | BLE_ACTIVE].capBitMap[0] = 0x0; - ret = GetNonDeviceInfo(&foundInfoTest); - EXPECT_EQ(ret, SOFTBUS_ERR); - - g_bleInfoManager[BLE_PUBLISH | BLE_ACTIVE].capBitMap[0] = 0x1; - ret = GetNonDeviceInfo(&foundInfoTest); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, GetNonDeviceInfo001, End"); -} - -/* -* @tc.name: BuildBleConfigAdvData001 -* @tc.desc: test BuildBleConfigAdvData -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BuildBleConfigAdvData001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, BuildBleConfigAdvData001, Start"); - int32_t ret = BuildBleConfigAdvData(nullptr, nullptr); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - - SoftBusBleAdvData advDataTest {}; - BroadcastData broadcastDataTest {}; - broadcastDataTest.dataLen = ADV_DATA_MAX_LEN; - ret = BuildBleConfigAdvData(&advDataTest, &broadcastDataTest); - EXPECT_EQ(advDataTest.scanRspData[POS_RSP_TYPE], RSP_TYPE); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, BuildBleConfigAdvData001, End"); -} - -/* -* @tc.name: GetBroadcastData001 -* @tc.desc: test GetBroadcastData -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, GetBroadcastData001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, GetBroadcastData001, Start"); - int32_t ret = GetBroadcastData(nullptr, NUM_ADVERTISER, nullptr); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - - int32_t advId = CON_ADV_ID; - DeviceInfo infoTest {}; - BroadcastData broadcastDataTest {}; - g_bleInfoManager[BLE_SUBSCRIBE | BLE_ACTIVE].isWakeRemote[0] = true; - ret = GetBroadcastData(&infoTest, advId, &broadcastDataTest); - EXPECT_EQ(ret, SOFTBUS_OK); - - advId = NON_ADV_ID; - ret = GetBroadcastData(&infoTest, advId, &broadcastDataTest); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, GetBroadcastData001, End"); -} - -/* -* @tc.name: StartAdvertiser001 -* @tc.desc: test StartAdvertiser -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, StartAdvertiser001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, StartAdvertiser001, Start"); - InitAdvertiser(); - int32_t adv = NON_ADV_ID; - g_bleAdvertiser[adv].isAdvertising = true; - g_bleInfoManager[BLE_PUBLISH | BLE_ACTIVE].needUpdate = true; - g_bleInfoManager[BLE_PUBLISH | BLE_PASSIVE].needUpdate = true; - int32_t ret = StartAdvertiser(adv); - EXPECT_EQ(ret, SOFTBUS_ERR); - - g_bleAdvertiser[adv].isAdvertising = true; - g_bleInfoManager[BLE_PUBLISH | BLE_ACTIVE].needUpdate = false; - g_bleInfoManager[BLE_PUBLISH | BLE_PASSIVE].needUpdate = false; - ret = StartAdvertiser(adv); - EXPECT_EQ(ret, SOFTBUS_ERR); - - g_bleAdvertiser[adv].isAdvertising = false; - g_info = -1; - g_bleAdvertiser[adv].GetDeviceInfo = GetDeviceInfo; - g_bleAdvertiser[adv].advId = 0; - ret = StartAdvertiser(adv); - EXPECT_EQ(ret, SOFTBUS_OK); - - g_info = 0; - ret = StartAdvertiser(adv); - EXPECT_EQ(ret, SOFTBUS_ERR); - - g_bleAdvertiser[adv].advId = 1; - ret = StartAdvertiser(adv); - EXPECT_EQ(ret, SOFTBUS_ERR); - - g_bleAdvertiser[adv].advId = 2; - ret = StartAdvertiser(adv); - EXPECT_NE(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, StartAdvertiser001, End"); -} - -/* -* @tc.name: GetScannerParam001 -* @tc.desc: test GetScannerParam -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, GetScannerParam001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, GetScannerParam001, Start"); - constexpr int32_t FREQ = -1; - int32_t ret = GetScannerParam(FREQ, nullptr); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, GetScannerParam001, End"); -} - -/* -* @tc.name: RegisterCapability001 -* @tc.desc: test RegisterCapability -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, RegisterCapability001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, RegisterCapability001, Start"); - uint8_t *capabilityDate = (uint8_t *)SoftBusMalloc(sizeof(uint8_t)); - ASSERT_TRUE(capabilityDate != nullptr); - capabilityDate[0] = 'a'; - SubscribeOption subscribeOption = {}; - PublishOption publishOption = { - .ranging = true, - }; - DiscBleInfo infoTest = {}; - DiscBleOption optionTest = { - .publishOption = &publishOption, - .subscribeOption = &subscribeOption, - }; - int32_t ret = RegisterCapability(nullptr, nullptr); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - ret = RegisterCapability(nullptr, &optionTest); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - ret = RegisterCapability(&infoTest, nullptr); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - RegisterCapability(&infoTest, &optionTest); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, RegisterCapability001, End"); -} - -/* -* @tc.name: UnregisterCapability001 -* @tc.desc: test dispatcher -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, UnregisterCapability001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UnregisterCapability001 Start"); - uint8_t *capabilityDate = (uint8_t *)SoftBusMalloc(sizeof(uint8_t)); - ASSERT_TRUE(capabilityDate != nullptr); - capabilityDate[0] = 'a'; - SubscribeOption subscribeOption = { - .freq = LOW, - .isSameAccount = false, - .isWakeRemote = false, - .capabilityBitmap = { 0x1 }, - .capabilityData = capabilityDate, - .dataLen = 1, - }; - DiscBleOption discBleOption = { - .publishOption = nullptr, - .subscribeOption = &subscribeOption, - }; - DiscBleInfo discBleInfo = { - .needUpdate = false, - .capBitMap = { 0x01 }, - .capCount = { 1 }, - .capabilityData = {capabilityDate}, - .capDataLen = { 1 }, - .isSameAccount = { false }, - .isWakeRemote = { false }, - .freq = { LOW }, - .rangingRefCnt = 1, - }; - - UnregisterCapability(&discBleInfo, &discBleOption); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UnregisterCapability001 End"); -} - -/* -* @tc.name: UnregisterCapability002 -* @tc.desc: test UnregisterCapability -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, UnregisterCapability002, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UnregisterCapability002 Start"); - uint8_t *capabilityDate = (uint8_t *)SoftBusMalloc(sizeof(uint8_t)); - ASSERT_TRUE(capabilityDate != nullptr); - capabilityDate[0] = 'a'; - PublishOption publishOption = { - .freq = LOW, - .capabilityBitmap = { 0x01 }, - .capabilityData = capabilityDate, - .dataLen = 1, - .ranging = false, - }; - DiscBleInfo discBleInfo = { - .needUpdate = false, - .capBitMap = { 0x01 }, - .capCount = { 1 }, - .capabilityData = { capabilityDate }, - .capDataLen = { 1 }, - .isSameAccount = { false }, - .isWakeRemote = { false }, - .freq = { LOW }, - .rangingRefCnt = 1, - }; - DiscBleOption discBleOption = { - .publishOption = &publishOption, - .subscribeOption = nullptr, - }; - UnregisterCapability(&discBleInfo, &discBleOption); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UnregisterCapability002 End"); -} - -/* -* @tc.name: UnregisterCapability003 -* @tc.desc: test UnregisterCapability -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, UnregisterCapability003, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UnregisterCapability003 Start"); - uint8_t *capabilityDate = (uint8_t *)SoftBusMalloc(sizeof(uint8_t)); - ASSERT_TRUE(capabilityDate != nullptr); - capabilityDate[0] = 'a'; - PublishOption publishOption = { - .freq = LOW, - .capabilityBitmap = { 0x01 }, - .capabilityData = capabilityDate, - .dataLen = 1, - .ranging = true, - }; - DiscBleInfo discBleInfo = { - .needUpdate = false, - .capBitMap = { 0x01 }, - .capCount = { 1 }, - .capabilityData = { capabilityDate }, - .capDataLen = { 1 }, - .isSameAccount = { false }, - .isWakeRemote = { false }, - .freq = { LOW }, - .rangingRefCnt = -1, - }; - DiscBleOption discBleOption = { - .publishOption = &publishOption, - .subscribeOption = nullptr, - }; - UnregisterCapability(&discBleInfo, &discBleOption); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UnregisterCapability003 End"); -} - -/* -* @tc.name: UnregisterCapability004 -* @tc.desc: test UnregisterCapability -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, UnregisterCapability004, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UnregisterCapability004 Start"); - uint8_t *capabilityDate = (uint8_t *)SoftBusMalloc(sizeof(uint8_t)); - ASSERT_TRUE(capabilityDate != nullptr); - capabilityDate[0] = 'a'; - PublishOption publishOption = { - .freq = LOW, - .capabilityBitmap = { 0x01 }, - .capabilityData = capabilityDate, - .dataLen = 1, - .ranging = false, - }; - DiscBleInfo discBleInfo = { - .needUpdate = false, - .capBitMap = { 0x01 }, - .capCount = { 1 }, - .capabilityData = { capabilityDate }, - .capDataLen = { 1 }, - .isSameAccount = { false }, - .isWakeRemote = { false }, - .freq = { LOW }, - .rangingRefCnt = -1, - }; - DiscBleOption discBleOption = { - .publishOption = &publishOption, - .subscribeOption = nullptr, - }; - UnregisterCapability(&discBleInfo, &discBleOption); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UnregisterCapability004 End"); -} - -/* -* @tc.name: UnregisterCapability005 -* @tc.desc: test UnregisterCapability -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, UnregisterCapability005, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UnregisterCapability005 Start"); - uint8_t *capabilityDate = (uint8_t *)SoftBusMalloc(sizeof(uint8_t)); - ASSERT_TRUE(capabilityDate != nullptr); - capabilityDate[0] = 'a'; - - PublishOption publishOption = { - .freq = LOW, - .capabilityBitmap = { 0x1 }, - .capabilityData = capabilityDate, - .dataLen = 1, - .ranging = true, - }; - DiscBleInfo discBleInfo = { - .needUpdate = false, - .capBitMap = { 0x01 }, - .capCount = { 1 }, - .capabilityData = { capabilityDate }, - .capDataLen = { 1 }, - .isSameAccount = { false }, - .isWakeRemote = { false }, - .freq = { LOW }, - .rangingRefCnt = 1, - }; - DiscBleOption discBleOption = { - .publishOption = &publishOption, - .subscribeOption = nullptr, - }; - UnregisterCapability(&discBleInfo, &discBleOption); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UnregisterCapability005 End"); -} - -/* -* @tc.name: ProcessBleInfoManager001 -* @tc.desc: test ProcessBleInfoManager -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, ProcessBleInfoManager001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ProcessBleInfoManager001 Start"); - bool isStart = false; - uint8_t publishFlag = 1; - uint8_t activeFlag = 1; - void *option = nullptr; - auto ret = ProcessBleInfoManager(isStart, publishFlag, activeFlag, option); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ProcessBleInfoManager001 End"); -} - -/* -* @tc.name: BleStartActivePublish001 -* @tc.desc: test BleStartActivePublish -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleStartActivePublish001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStartActivePublish001 Start"); - ListInit(&g_recvMessageInfo.node); - SoftBusMutexInit(&g_recvMessageInfo.lock, nullptr); - BleGattLockInit(); - SoftBusMutexInit(&g_bleInfoLock, nullptr); - LooperInit(); - DiscBleLooperInit(); - uint8_t capabilityDate[] = {'a'}; - PublishOption publishOption = { - .freq = LOW, - .capabilityBitmap = { 0x01 }, - .capabilityData = capabilityDate, - .dataLen = 1, - .ranging = false, - }; - g_softbusBtState = false; - auto ret = BleStartActivePublish(&publishOption); - EXPECT_EQ(ret, SOFTBUS_ERR); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStartActivePublish001 End"); -} - -/* -* @tc.name: BleStartActivePublish002 -* @tc.desc: test BleStartActivePublish -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleStartActivePublish002, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStartActivePublish002 Start"); - ListInit(&g_recvMessageInfo.node); - SoftBusMutexInit(&g_recvMessageInfo.lock, nullptr); - BleGattLockInit(); - SoftBusMutexInit(&g_bleInfoLock, nullptr); - DiscBleLooperInit(); - uint8_t capabilityDate[] = { 'a' }; - PublishOption publishOption = { - .freq = LOW, - .capabilityBitmap = { 0x01 }, - .capabilityData = capabilityDate, - .dataLen = 1, - .ranging = false, - }; - g_softbusBtState = true; - auto ret = BleStartActivePublish(&publishOption); - EXPECT_NE(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStartActivePublish002 End"); -} - -/* -* @tc.name: BleStartPassivePublish001 -* @tc.desc: test BleStartPassivePublish -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleStartPassivePublish001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStartPassivePublish001 Start"); - ListInit(&g_recvMessageInfo.node); - SoftBusMutexInit(&g_recvMessageInfo.lock, nullptr); - BleGattLockInit(); - SoftBusMutexInit(&g_bleInfoLock, nullptr); - DiscBleLooperInit(); - uint8_t capabilityDate[] = { 'a' }; - PublishOption publishOption = { - .freq = LOW, - .capabilityBitmap = { 0x01 }, - .capabilityData = capabilityDate, - .dataLen = 1, - .ranging = false, - }; - auto ret = BleStartPassivePublish(&publishOption); - EXPECT_NE(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStartPassivePublish001 End"); -} - -/* -* @tc.name: BleStopActivePublish001 -* @tc.desc: test BleStopActivePublish -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleStopActivePublish001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStopActivePublish001 Start"); - ListInit(&g_recvMessageInfo.node); - SoftBusMutexInit(&g_recvMessageInfo.lock, nullptr); - BleGattLockInit(); - SoftBusMutexInit(&g_bleInfoLock, nullptr); - DiscBleLooperInit(); - uint8_t capabilityDate[] = { 'a' }; - PublishOption publishOption = { - .freq = LOW, - .capabilityBitmap = { 0x01 }, - .capabilityData = capabilityDate, - .dataLen = 1, - .ranging = false, - }; - auto ret = BleStopActivePublish(&publishOption); - EXPECT_NE(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStopActivePublish001 End"); -} - -/* -* @tc.name: BleStopPassivePublish001 -* @tc.desc: test BleStopPassivePublish -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleStopPassivePublish001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStopPassivePublish001 Start"); - ListInit(&g_recvMessageInfo.node); - SoftBusMutexInit(&g_recvMessageInfo.lock, nullptr); - BleGattLockInit(); - SoftBusMutexInit(&g_bleInfoLock, nullptr); - DiscBleLooperInit(); - uint8_t capabilityDate[] = {'a'}; - PublishOption publishOption = { - .freq = LOW, - .capabilityBitmap = { 0x01 }, - .capabilityData = capabilityDate, - .dataLen = 1, - .ranging = false, - }; - auto ret = BleStopPassivePublish(&publishOption); - EXPECT_NE(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStopPassivePublish001 End"); -} - -/* -* @tc.name: BleStartActiveDiscovery001 -* @tc.desc: test BleStartActiveDiscovery -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleStartActiveDiscovery001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStartActiveDiscovery001 Start"); - ListInit(&g_recvMessageInfo.node); - SoftBusMutexInit(&g_recvMessageInfo.lock, nullptr); - BleGattLockInit(); - SoftBusMutexInit(&g_bleInfoLock, nullptr); - DiscBleLooperInit(); - uint8_t *capabilityDate = (uint8_t *)SoftBusMalloc(sizeof(uint8_t)); - ASSERT_TRUE(capabilityDate != nullptr); - capabilityDate[0] = 'a'; - SubscribeOption subscribeOption = { - .freq = LOW, - .isSameAccount = false, - .isWakeRemote = false, - .capabilityBitmap = { 0x1 }, - .capabilityData = capabilityDate, - .dataLen = 1, - }; - auto ret = BleStartActiveDiscovery(&subscribeOption); - EXPECT_NE(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStartActiveDiscovery001 End"); -} - -/* -* @tc.name: BleStartPassiveDiscovery001 -* @tc.desc: test BleStartPassiveDiscovery -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleStartPassiveDiscovery001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStartPassiveDiscovery001 Start"); - ListInit(&g_recvMessageInfo.node); - SoftBusMutexInit(&g_recvMessageInfo.lock, nullptr); - BleGattLockInit(); - SoftBusMutexInit(&g_bleInfoLock, nullptr); - DiscBleLooperInit(); - uint8_t *capabilityDate = (uint8_t *)SoftBusMalloc(sizeof(uint8_t)); - ASSERT_TRUE(capabilityDate != nullptr); - capabilityDate[0] = 'a'; - SubscribeOption subscribeOption = { - .freq = LOW, - .isSameAccount = false, - .isWakeRemote = false, - .capabilityBitmap = { 0x1 }, - .capabilityData = capabilityDate, - .dataLen = 1, - }; - auto ret = BleStartPassiveDiscovery(&subscribeOption); - EXPECT_NE(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStartPassiveDiscovery001 End"); -} - -/* -* @tc.name: BleStopActiveDiscovery001 -* @tc.desc: test BleStopActiveDiscovery -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleStopActiveDiscovery001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStopActiveDiscovery001 Start"); - ListInit(&g_recvMessageInfo.node); - SoftBusMutexInit(&g_recvMessageInfo.lock, nullptr); - BleGattLockInit(); - SoftBusMutexInit(&g_bleInfoLock, nullptr); - DiscBleLooperInit(); - uint8_t *capabilityDate = (uint8_t *)SoftBusMalloc(sizeof(uint8_t)); - ASSERT_TRUE(capabilityDate != nullptr); - capabilityDate[0] = 'a'; - SubscribeOption subscribeOption = { - .freq = LOW, - .isSameAccount = false, - .isWakeRemote = false, - .capabilityBitmap = { 0x1 }, - .capabilityData = capabilityDate, - .dataLen = 1, - }; - auto ret = BleStopActiveDiscovery(&subscribeOption); - EXPECT_NE(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStopActiveDiscovery001 End"); -} -void OnDeviceFound1(const DeviceInfo *device, const InnerDeviceInfoAddtions *addtions) {} - -/* -* @tc.name: BleStopPassiveDiscovery001 -* @tc.desc: test BleStopPassiveDiscovery -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleStopPassiveDiscovery001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStopPassiveDiscovery001 Start"); - ListInit(&g_recvMessageInfo.node); - SoftBusMutexInit(&g_recvMessageInfo.lock, nullptr); - BleGattLockInit(); - SoftBusMutexInit(&g_bleInfoLock, nullptr); - DiscBleLooperInit(); - uint8_t *capabilityDate = (uint8_t *)SoftBusMalloc(sizeof(uint8_t)); - ASSERT_TRUE(capabilityDate != nullptr); - capabilityDate[0] = 'a'; - SubscribeOption subscribeOption = { - .freq = LOW, - .isSameAccount = false, - .isWakeRemote = false, - .capabilityBitmap = { 0x1 }, - .capabilityData = capabilityDate, - .dataLen = 1, - }; - auto ret = BleStopPassiveDiscovery(&subscribeOption); - EXPECT_NE(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleStopPassiveDiscovery001 End"); -} - -/* -* @tc.name: BleIsConcern001 -* @tc.desc: test BleIsConcern -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleIsConcern001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleIsConcern001 Start"); - auto ret = BleIsConcern(0); - EXPECT_EQ (ret, false); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleIsConcern001 End"); -} - -/* -* @tc.name: BleIsConcern002 -* @tc.desc: test BleIsConcern -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleIsConcern002, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleIsConcern002 Start"); - auto ret = BleIsConcern(1); - EXPECT_NE (ret, true); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleIsConcern002 End"); -} - -/* -* @tc.name: DiscSoftbusBleInit001 -* @tc.desc: test DiscSoftbusBleInit -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, DiscSoftbusBleInit001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, DiscSoftbusBleInit001, Start"); - auto *ret = DiscSoftBusBleInit(nullptr); - EXPECT_EQ(ret, nullptr); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, DiscSoftbusBleInit001, End"); -} - -/* -* @tc.name: DiscSoftbusBleInit002 -* @tc.desc: test DiscSoftbusBleInit -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, DiscSoftbusBleInit002, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest DiscSoftbusBleInit002 Start"); - auto *ret = DiscSoftBusBleInit(&g_discInnerCallBack); - EXPECT_EQ(ret, nullptr); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest DiscSoftbusBleInit002 End"); -} - -/* -* @tc.name: DiscSoftbusBleInit003 -* @tc.desc: test DiscSoftbusBleInit -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, DiscSoftbusBleInit003, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest DiscSoftbusBleInit003 Start"); - auto *ret = DiscSoftBusBleInit(&g_discInnerCallback); - EXPECT_EQ(ret, &g_discBleDispatcherInterface); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest DiscSoftbusBleInit003 End"); -} - -/* -* @tc.name: DiscBleLooperInit001 -* @tc.desc: test DiscBleLooperInit -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, DiscBleLooperInit001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest DiscBleLooperInit001 Start"); - auto ret = DiscBleLooperInit(); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest DiscBleLooperInit001 End"); -} - -/* -* @tc.name: InitBleListener001 -* @tc.desc: test InitBleListener -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, InitBleListener001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest InitBleListener001 Start"); - auto ret = InitBleListener(); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest InitBleListener001 End"); -} - -/* -* @tc.name: UpdateAdvertiserDeviceInfo001 -* @tc.desc: test UpdateAdvertiserDeviceInfo -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, UpdateAdvertiserDeviceInfo001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UpdateAdvertiserDeviceInfo001 Start"); - auto ret = UpdateAdvertiserDeviceInfo(1); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UpdateAdvertiserDeviceInfo001 End"); -} - -/* -* @tc.name: UpdateAdvertiserDeviceInfo002 -* @tc.desc: test UpdateAdvertiserDeviceInfo -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, UpdateAdvertiserDeviceInfo002, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UpdateAdvertiserDeviceInfo002 Start"); - uint32_t capabilityBitmap = 0x01; - ConnectionAddr connectionAddr = { - .type = CONNECTION_ADDR_BLE, - .info = { - .ble = { - .bleMac = "123456789abc", - .udidHash = { 0x01 }, - }, - }, - .peerUid = "abcd", - }; - DeviceInfo deviceInfo = { - .devId = "bac", - .accountHash = "abc", - .devType = DESKTOP_PC, - .devName = "abc", - .isOnline = true, - .addrNum = 1, - .addr = { connectionAddr }, - .capabilityBitmapNum = 1, - .capabilityBitmap = { capabilityBitmap }, - .custData = { "hello" }, - .range = 1, - }; - DiscBleAdvertiser discBleAdvertiser = { - .advId = 1, - .isAdvertising = true, - .deviceInfo = deviceInfo, - .GetDeviceInfo = GetDeviceInfo, - }; - g_bleAdvertiser[1] = discBleAdvertiser; - auto ret = UpdateAdvertiserDeviceInfo(1); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest UpdateAdvertiserDeviceInfo002 End"); -} - -/* -* @tc.name: ProcessBleDiscFunc001 -* @tc.desc: test ProcessBleDiscFunc -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, ProcessBleDiscFunc001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ProcessBleDiscFunc001 Start"); - auto ret = ProcessBleDiscFunc(true, BLE_PUBLISH, BLE_ACTIVE, PUBLISH_ACTIVE_SERVICE, nullptr); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ProcessBleDiscFunc001 End"); -} - -/* -* @tc.name: ProcessBleDiscFunc002 -* @tc.desc: test ProcessBleDiscFunc -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, ProcessBleDiscFunc002, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ProcessBleDiscFunc002 Start"); - uint8_t capabilityDate[] = { 'a' }; - PublishOption publishOption = { - .freq = LOW, - .capabilityBitmap = {0x01}, - .capabilityData = capabilityDate, - .dataLen = 1, - .ranging = false, - }; - PublishOption publishOption1 [] = {publishOption}; - g_softbusBtState = false; - auto ret = ProcessBleDiscFunc(true, BLE_PUBLISH, BLE_ACTIVE, PUBLISH_ACTIVE_SERVICE, (void *)publishOption1); - EXPECT_EQ(ret, SOFTBUS_ERR); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ProcessBleDiscFunc002 End"); -} - -/* -* @tc.name: StartPassiveDiscovery001 -* @tc.desc: test ProcessBleDiscFunc -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, StartPassiveDiscovery001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ProcessBleDiscFunc001 Start"); - StartPassiveDiscovery(nullptr); - SoftBusMessage softBusMessage = { - .what = START_PASSIVE_DISCOVERY, - .arg1 = 1, - .arg2 = 2, - .time = 12, - .obj = nullptr, - .handler = nullptr, - .FreeMessage = nullptr, - }; - g_isScanning = true; - StartActiveDiscovery(&softBusMessage); - softBusMessage.what = START_ACTIVE_DISCOVERY; - StartActiveDiscovery(&softBusMessage); - g_softbusBtState = true; - EXPECT_TRUE(DiscSoftBusBleInit(&g_discInnerCallback) != nullptr); - softBusMessage.what = START_PASSIVE_DISCOVERY; - StartActiveDiscovery(&softBusMessage); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ProcessBleDiscFunc001 End"); -} - -/* -* @tc.name: Recovery001 -* @tc.desc: test Recovery -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, Recovery001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest Recovery001 Start"); - Recovery(nullptr); - SoftBusMessage softBusMessage = { - .what = START_PASSIVE_DISCOVERY, - .arg1 = 1, - .arg2 = 2, - .time = 12, - .obj = nullptr, - .handler = nullptr, - .FreeMessage = nullptr, - }; - Recovery(&softBusMessage); - g_softbusBtState = true; - EXPECT_TRUE(DiscSoftBusBleInit(&g_discInnerCallback) != nullptr); - softBusMessage.what = RECOVERY; - Recovery(&softBusMessage); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest Recovery001 End"); -} - -/* -* @tc.name: BleDiscTurnOff001 -* @tc.desc: test BleDiscTurnOff -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleDiscTurnOff001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleDiscTurnOff001 Start"); - BleDiscTurnOff(nullptr); - SoftBusMessage softBusMessage = { - .what = START_PASSIVE_DISCOVERY, - .arg1 = 1, - .arg2 = 2, - .time = 12, - .obj = nullptr, - .handler = nullptr, - .FreeMessage = nullptr, - }; - BleDiscTurnOff(&softBusMessage); - g_softbusBtState = true; - EXPECT_TRUE(DiscSoftBusBleInit(&g_discInnerCallback) != nullptr); - softBusMessage.what = RECOVERY; - BleDiscTurnOff(&softBusMessage); - DiscBleDeinit(); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleDiscTurnOff001 End"); -} - -/* -* @tc.name: ReplyPassiveNonBroadcast001 -* @tc.desc: test ReplyPassiveNonBroadcast -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, ReplyPassiveNonBroadcast001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ReplyPassiveNonBroadcast001 Start"); - auto ret = ReplyPassiveNonBroadcast(); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ReplyPassiveNonBroadcast001 End"); -} - -/* -* @tc.name: RemoveRecvMsgFunc001 -* @tc.desc: test RemoveRecvMsgFunc -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, RemoveRecvMsgFunc001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest RemoveRecvMsgFunc001 Start"); - SoftBusMessage softBusMessage = { - .what = START_PASSIVE_DISCOVERY, - .arg1 = 1, - .arg2 = 2, - .time = 12, - .obj = nullptr, - .handler = nullptr, - .FreeMessage = nullptr, - }; - auto args = softBusMessage.arg1; - auto ret = MessageRemovePredicate(&softBusMessage, (void *) &args); - EXPECT_EQ(ret, 1); - args = args + 1; - auto ans = MessageRemovePredicate(&softBusMessage, (void *) &args); - EXPECT_EQ(ans, 1); - softBusMessage.what = PROCESS_TIME_OUT; - auto tmp = MessageRemovePredicate(&softBusMessage, (void *) &args); - EXPECT_EQ(tmp, 1); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest RemoveRecvMsgFunc001 End"); -} - -/* -* @tc.name: GetRecvMessage001 -* @tc.desc: test GetRecvMessage -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, GetRecvMessage001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest GetRecvMessage001 Start"); - auto ret = GetRecvMessage(nullptr); - EXPECT_EQ(ret, nullptr); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest GetRecvMessage001 End"); -} - -/* -* @tc.name: MatchRecvMessage001 -* @tc.desc: test MatchRecvMessage -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, MatchRecvMessage001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest MatchRecvMessage001 Start"); - auto ret = MatchRecvMessage(nullptr, nullptr, 1); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest MatchRecvMessage001 End"); -} - -/* -* @tc.name: MatchRecvMessage002 -* @tc.desc: test MatchRecvMessage -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, MatchRecvMessage002, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest MatchRecvMessage002 Start"); - uint32_t publishInfo[] = { 0x1, 0xa }; - uint32_t capBitMap[] = { 0x1 }; - DiscSoftBusBleInit(&g_discInnerCallback); - auto ret = MatchRecvMessage(publishInfo, capBitMap, 1); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest MatchRecvMessage002 End"); -} - -/* -* @tc.name: StartTimeout001 -* @tc.desc: test StartTimeout -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, StartTimeout001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest StartTimeout001 Start"); - StartTimeout(nullptr); - StartTimeout("123"); - EXPECT_TRUE(DiscSoftBusBleInit(&g_discInnerCallback) != nullptr); - StartTimeout("123"); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest StartTimeout001 End"); -} - -/* -* @tc.name: RemoveTimeout001 -* @tc.desc: test RemoveTimeout -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, RemoveTimeout001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest RemoveTimeout001 Start"); - RemoveTimeout(nullptr); - RemoveTimeout("123"); - EXPECT_TRUE(DiscSoftBusBleInit(&g_discInnerCallback) != nullptr); - RemoveTimeout("123"); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest RemoveTimeout001 End"); -} - -/* -* @tc.name: AddRecvMessage001 -* @tc.desc: test AddRecvMessage -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, AddRecvMessage001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest AddRecvMessage001 Start"); - SoftBusMutexInit(&g_recvMessageInfo.lock, nullptr); - BleGattLockInit(); - SoftBusMutexInit(&g_bleInfoLock, nullptr); - DiscSoftBusBleInit(&g_discInnerCallback); - auto ret = AddRecvMessage(nullptr, nullptr, true); - EXPECT_EQ (ret, SOFTBUS_INVALID_PARAM); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest AddRecvMessage001 End"); -} - -/* -* @tc.name: AddRecvMessage002 -* @tc.desc: test AddRecvMessage -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, AddRecvMessage002, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest AddRecvMessage002 Start"); - SoftBusMutexInit(&g_recvMessageInfo.lock, nullptr); - BleGattLockInit(); - SoftBusMutexInit(&g_bleInfoLock, nullptr); - uint32_t capbitmap[] = { 0x1 }; - auto ret = AddRecvMessage("123", capbitmap, true); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest AddRecvMessage002 End"); -} - -/* -* @tc.name: RemoveRecvMessage001 -* @tc.desc: test RemoveRecvMessage -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, RemoveRecvMessage001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest RemoveRecvMessage001 Start"); - EXPECT_TRUE(DiscSoftBusBleInit(&g_discInnerCallback) != nullptr); - RemoveRecvMessage(0X01); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest RemoveRecvMessage001 End"); -} - -/* -* @tc.name: BleInfoDump001 -* @tc.desc: test BleInfoDump -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, BleInfoDump001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleInfoDump001 Start"); - DiscSoftBusBleInit(&g_discInnerCallback); - EXPECT_TRUE(BleInfoDump(9) == SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest BleInfoDump001 End"); -} - -/* -* @tc.name: RegisterCapability002 -* @tc.desc: test RegisterCapability -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, RegisterCapability002, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest RegisterCapability002 Start"); - uint8_t *capabilityDate = (uint8_t *)SoftBusMalloc(sizeof(uint8_t)); - ASSERT_TRUE(capabilityDate != nullptr); - capabilityDate[0] = 'a'; - SubscribeOption subscribeOption = { - .freq = LOW, - .isSameAccount = false, - .isWakeRemote = false, - .capabilityBitmap = { 0x0 }, - .capabilityData = capabilityDate, - .dataLen = 1, - }; - DiscBleOption discBleOption = { - .publishOption = nullptr, - .subscribeOption = &subscribeOption, - }; - DiscBleInfo discBleInfo = { - .needUpdate = false, - .capBitMap = { 0x01 }, - .capCount = { 1 }, - .capabilityData = {capabilityDate}, - .capDataLen = { 1 }, - .isSameAccount = { false }, - .isWakeRemote = { false }, - .freq = { LOW }, - .rangingRefCnt = 1, - }; - auto ret = RegisterCapability(&discBleInfo, &discBleOption); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest RegisterCapability002 End"); -} - -/* -* @tc.name: RegisterCapability002 -* @tc.desc: test RegisterCapability -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, RegisterCapability003, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest RegisterCapability003 Start"); - uint8_t *capabilityDate = (uint8_t *)SoftBusMalloc(sizeof(uint8_t)); - ASSERT_TRUE(capabilityDate != nullptr); - capabilityDate[0] = 'a'; - SubscribeOption subscribeOption = { - .freq = LOW, - .isSameAccount = false, - .isWakeRemote = false, - .capabilityBitmap = { 0x1 }, - .capabilityData = capabilityDate, - .dataLen = 1, - }; - DiscBleOption discBleOption = { - .publishOption = nullptr, - .subscribeOption = &subscribeOption, - }; - DiscBleInfo discBleInfo = { - .needUpdate = false, - .capBitMap = { 0x0 }, - .capCount = { 1 }, - .capabilityData = {capabilityDate}, - .capDataLen = { 1 }, - .isSameAccount = { false }, - .isWakeRemote = { false }, - .freq = { LOW }, - .rangingRefCnt = 1, - }; - auto ret = RegisterCapability(&discBleInfo, &discBleOption); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest RegisterCapability003 End"); -} - -/* -* @tc.name: ProcessBleInfoManager002 -* @tc.desc: test ProcessBleInfoManager -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, ProcessBleInfoManager002, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ProcessBleInfoManager002 Start"); - bool isStart = false; - uint8_t publishFlag = BLE_PUBLISH; - uint8_t activeFlag = 1; - uint8_t capabilityDate[] = { 'a' }; - PublishOption publishOption = { - .freq = LOW, - .capabilityBitmap = {0x01}, - .capabilityData = capabilityDate, - .dataLen = 1, - .ranging = false, - }; - PublishOption publishOption2[] = {publishOption}; - auto ret = ProcessBleInfoManager(isStart, publishFlag, activeFlag, (void *)publishOption2); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ProcessBleInfoManager002 End"); -} -/* -* @tc.name: ProcessBleInfoManager002 -* @tc.desc: test ProcessBleInfoManager -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, ProcessBleInfoManager003, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ProcessBleInfoManager003 Start"); - bool isStart = false; - uint8_t publishFlag = BLE_PUBLISH; - uint8_t activeFlag = 1; - uint8_t capabilityDate[] = { 'a' }; - SubscribeOption subscribeOption = { - .freq = LOW, - .isSameAccount = false, - .isWakeRemote = false, - .capabilityBitmap = { 0x0 }, - .capabilityData = capabilityDate, - .dataLen = 1, - }; - SubscribeOption subscribeOption1[] = { subscribeOption }; - auto ret = ProcessBleInfoManager(isStart, publishFlag, activeFlag, (void *)subscribeOption1); - EXPECT_EQ(ret, SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest ProcessBleInfoManager003 End"); -} - -/* -* @tc.name: InitDiscBleInfo001 -* @tc.desc: test InitDiscBleInfo -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, InitDiscBleInfo001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest InitDiscBleInfo001 Start"); - auto ret = InitDiscBleInfo(nullptr); - EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest InitDiscBleInfo001 End"); -} -/* -* @tc.name: RemoveRecvMsgFunc002 -* @tc.desc: test RemoveRecvMsgFunc -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, RemoveRecvMsgFunc002, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest RemoveRecvMsgFunc002 Start"); - uintptr_t args = 12; - SoftBusMessage msg = { - .what = PROCESS_TIME_OUT, - .arg1 = reinterpret_cast(&args), - .arg2 = args, - .time = 13, - .obj = nullptr, - .handler = nullptr, - .FreeMessage = nullptr, - }; - auto ret = MessageRemovePredicate(&msg, &args); - EXPECT_EQ(ret, 0); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest RemoveRecvMsgFunc002 End"); -} - -/* -* @tc.name: UpdateAdvertiser001 -* @tc.desc: test UpdateAdvertiser -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, UpdateAdvertiser001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, UpdateAdvertiser001, Start"); - int32_t advTest = 0; - g_bleAdvertiser[advTest] = { - .advId = 0, - .isAdvertising = true, - .GetDeviceInfo = GetDeviceInfo, - }; - g_info = 0; - EXPECT_TRUE(UpdateAdvertiser(advTest) == SOFTBUS_OK); - - g_info = -1; - EXPECT_TRUE(UpdateAdvertiser(advTest) != SOFTBUS_OK); - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest, UpdateAdvertiser001, End"); -} - -/* -* @tc.name: UpdateAdvertiser001 -* @tc.desc: test UpdateAdvertiser -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(DiscSoftBusBleTest, DiscFreeBleScanFilter001, TestSize.Level1) -{ - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest DiscFreeBleScanFilter001 Start"); - SoftBusBleScanFilter *filter = nullptr; - DiscFreeBleScanFilter(&filter); - - filter = static_cast(SoftBusMalloc(sizeof(SoftBusBleScanFilter))); - ASSERT_TRUE(filter != nullptr); - filter->serviceData = nullptr; - filter->serviceDataMask = nullptr; - DiscFreeBleScanFilter(&filter); - - filter = static_cast(SoftBusMalloc(sizeof(SoftBusBleScanFilter))); - ASSERT_TRUE(filter != nullptr); - filter->serviceData = static_cast(SoftBusMalloc(128)); - ASSERT_TRUE(filter->serviceData != nullptr); - filter->serviceDataMask = nullptr; - DiscFreeBleScanFilter(&filter); - - filter = static_cast(SoftBusMalloc(sizeof(SoftBusBleScanFilter))); - ASSERT_TRUE(filter != nullptr); - filter->serviceData = nullptr; - filter->serviceDataMask = static_cast(SoftBusMalloc(128)); - ASSERT_TRUE(filter->serviceDataMask != nullptr); - DiscFreeBleScanFilter(&filter); - - DISC_LOGI(DISC_TEST, "DiscSoftBusBleTest DiscFreeBleScanFilter001 End"); -} -} \ No newline at end of file diff --git a/tests/core/discovery/ble/softbus_ble_mock/ble_mock.cpp b/tests/core/discovery/ble/softbus_ble_mock/ble_mock.cpp index f0fbd5ddf..43a5e9ae3 100644 --- a/tests/core/discovery/ble/softbus_ble_mock/ble_mock.cpp +++ b/tests/core/discovery/ble/softbus_ble_mock/ble_mock.cpp @@ -25,11 +25,6 @@ using testing::_; using testing::NotNull; /* implement related global function of BLE */ -int BleGattLockInit() -{ - return BleMock::GetMock()->BleGattLockInit(); -} - int SoftBusAddBtStateListener(const SoftBusBtStateListener *listener) { return BleMock::GetMock()->SoftBusAddBtStateListener(listener); @@ -127,11 +122,6 @@ BleMock::~BleMock() mock.store(nullptr); } -int32_t BleMock::ActionOfBleGattLockInit() -{ - return SOFTBUS_OK; -} - int32_t BleMock::ActionOfInitBroadcastMgr() { return SOFTBUS_OK; @@ -476,7 +466,6 @@ bool BleMock::IsDeInitSuccess() void BleMock::SetupSuccessStub() { - EXPECT_CALL(*this, BleGattLockInit).WillRepeatedly(BleMock::ActionOfBleGattLockInit); EXPECT_CALL(*this, InitBroadcastMgr).WillRepeatedly(BleMock::ActionOfInitBroadcastMgr); EXPECT_CALL(*this, DeInitBroadcastMgr).WillRepeatedly(BleMock::ActionOfDeInitBroadcastMgr); EXPECT_CALL(*this, SoftBusGetBtState).WillRepeatedly(BleMock::ActionOfGetBtState); diff --git a/tests/core/discovery/ble/softbus_ble_mock/ble_mock.h b/tests/core/discovery/ble/softbus_ble_mock/ble_mock.h index 21fa05fd3..b3745e603 100644 --- a/tests/core/discovery/ble/softbus_ble_mock/ble_mock.h +++ b/tests/core/discovery/ble/softbus_ble_mock/ble_mock.h @@ -21,7 +21,6 @@ #include #include -#include "softbus_adapter_ble_gatt.h" #include "softbus_adapter_ble_gatt_client.h" #include "softbus_adapter_ble_gatt_server.h" #include "softbus_adapter_bt_common.h" @@ -30,7 +29,6 @@ class BleInterface { public: - virtual int BleGattLockInit() = 0; virtual int SoftBusAddBtStateListener(const SoftBusBtStateListener *listener) = 0; virtual int SoftBusRemoveBtStateListener(int listenerId) = 0; @@ -68,7 +66,6 @@ public: BleMock(); ~BleMock(); - MOCK_METHOD(int, BleGattLockInit, (), (override)); MOCK_METHOD(int, SoftBusAddBtStateListener, (const SoftBusBtStateListener *listener), (override)); MOCK_METHOD(int, SoftBusRemoveBtStateListener, (int listenerId), (override)); MOCK_METHOD(int32_t, InitBroadcastMgr, (), (override)); @@ -98,7 +95,6 @@ public: bool GetAsyncAdvertiseResult(); bool IsScanning(); - static int32_t ActionOfBleGattLockInit(); static int32_t ActionOfAddBtStateListener(const SoftBusBtStateListener *listener); static int32_t ActionOfRemoveBtStateListener(int listenerId); static int32_t ActionOfInitBroadcastMgr();