ut: add bluetooth adapter unit test coverage

Signed-off-by: songzhengteng <songzhengteng@huawei.com>
Change-Id: Iaa57b034386069a7ecbfc8fa4318c3769fe39ca8
This commit is contained in:
songzhengteng 2022-11-25 21:20:45 +08:00
parent da0d9b82f6
commit 2addd3190b
9 changed files with 468 additions and 267 deletions

View File

@ -26,10 +26,6 @@
#include "softbus_log.h"
#include "softbus_utils.h"
#define SOFTBUS_SCAN_CLIENT_ID 0
#define ADV_MAX_NUM 7
#define SCAN_MAX_NUM 5
typedef struct {
int advId;
bool isUsed;
@ -64,33 +60,17 @@ int BleGattLockInit(void)
return SOFTBUS_OK;
}
if (SoftBusMutexInit(&g_advLock, NULL) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "g_advLock init failed");
CLOGE("g_advLock init failed");
return SOFTBUS_ERR;
}
if (SoftBusMutexInit(&g_scanerLock, NULL) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "g_scanerLock init failed");
CLOGE("g_scanerLock init failed");
return SOFTBUS_ERR;
}
g_lockInit = true;
return SOFTBUS_OK;
}
static unsigned char ConvertScanFilterPolicy(unsigned char policy)
{
switch (policy) {
case OHOS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL:
return SOFTBUS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL;
case OHOS_BLE_SCAN_FILTER_POLICY_ONLY_WHITE_LIST:
return SOFTBUS_BLE_SCAN_FILTER_POLICY_ONLY_WHITE_LIST;
case OHOS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL_AND_RPA:
return SOFTBUS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL_AND_RPA;
case OHOS_BLE_SCAN_FILTER_POLICY_ONLY_WHITE_LIST_AND_RPA:
return SOFTBUS_BLE_SCAN_FILTER_POLICY_ONLY_WHITE_LIST_AND_RPA;
default:
return SOFTBUS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL;
}
}
static unsigned char ConvertScanEventType(unsigned char eventType)
{
switch (eventType) {
@ -173,18 +153,6 @@ static unsigned char ConvertScanAddrType(unsigned char addrType)
}
}
static unsigned char ConvertScanType(unsigned char scanType)
{
switch (scanType) {
case OHOS_BLE_SCAN_TYPE_PASSIVE:
return SOFTBUS_BLE_SCAN_TYPE_PASSIVE;
case OHOS_BLE_SCAN_TYPE_ACTIVE:
return SOFTBUS_BLE_SCAN_TYPE_ACTIVE;
default:
return SOFTBUS_BLE_SCAN_TYPE_PASSIVE;
}
}
static int ConvertScanMode(unsigned short scanInterval, unsigned short scanWindow)
{
if (scanInterval == SOFTBUS_BLE_SCAN_INTERVAL_P2 && scanWindow == SOFTBUS_BLE_SCAN_WINDOW_P2) {
@ -202,25 +170,9 @@ static int ConvertScanMode(unsigned short scanInterval, unsigned short scanWindo
return OHOS_BLE_SCAN_MODE_LOW_POWER;
}
void ConvertScanParam(const SoftBusBleScanParams *src, BleScanParams *dst)
{
if (src == NULL || dst == NULL) {
return;
}
dst->scanInterval = src->scanInterval;
dst->scanWindow = src->scanWindow;
dst->scanType = ConvertScanType(src->scanType);
dst->scanPhy = ConvertScanPhyType(src->scanPhy);
dst->scanFilterPolicy = ConvertScanFilterPolicy(src->scanFilterPolicy);
}
static void SetAndGetSuitableScanConfig(int listenerId, const SoftBusBleScanParams *params, BleScanConfigs *configs)
{
static int lastScanMode = OHOS_BLE_SCAN_MODE_LOW_POWER;
if (params == NULL || configs == NULL) {
return;
}
(void)memset_s(configs, sizeof(BleScanConfigs), 0x0, sizeof(BleScanConfigs));
g_scanListener[listenerId].param = *params;
for (int index = 0; index < SCAN_MAX_NUM; index++) {
@ -241,30 +193,22 @@ static void SetAndGetSuitableScanConfig(int listenerId, const SoftBusBleScanPara
static void DumpBleScanFilter(BleScanNativeFilter *nativeFilter, uint8_t filterSize)
{
#define HEX_STR_MULTIPLE_NUM 2
if (nativeFilter == NULL || filterSize == 0) {
return;
}
while (filterSize-- > 0) {
if ((nativeFilter + filterSize) == NULL) {
continue;
}
int32_t len = (nativeFilter + filterSize)->serviceDataLength;
int32_t hexLen = (nativeFilter + filterSize)->serviceDataLength * HEX_STR_MULTIPLE_NUM + 1;
char *serviceData = (char *)SoftBusCalloc(sizeof(char) * hexLen);
if (serviceData == NULL) {
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 (serviceDataMask == NULL) {
if (serviceData == NULL || serviceDataMask == NULL) {
SoftBusFree(serviceData);
serviceData = NULL;
SoftBusFree(serviceDataMask);
continue;
}
(void)ConvertBytesToHexString(serviceData, hexLen, (nativeFilter + filterSize)->serviceData, len);
(void)ConvertBytesToHexString(serviceDataMask, hexLen, (nativeFilter + filterSize)->serviceDataMask, len);
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "BLE Scan Filter id:%d [serviceData:%s, serviceDataMask:%s]",
CLOGI("BLE Scan Filter id:%d [serviceData:%s, serviceDataMask:%s]",
filterSize, serviceData, serviceDataMask);
SoftBusFree(serviceData);
SoftBusFree(serviceDataMask);
@ -274,10 +218,6 @@ static void DumpBleScanFilter(BleScanNativeFilter *nativeFilter, uint8_t filterS
static void GetAllNativeScanFilter(int thisListenerId, BleScanNativeFilter **nativeFilter, uint8_t *filterSize)
{
uint8_t nativeSize = 0;
if (nativeFilter == NULL || filterSize == NULL) {
return;
}
for (int index = 0; index < SCAN_MAX_NUM; index++) {
if (!g_scanListener[index].isUsed || (!g_scanListener[index].isScanning && index != thisListenerId)) {
g_scanListener[index].isNeedReset = false;
@ -286,12 +226,11 @@ static void GetAllNativeScanFilter(int thisListenerId, BleScanNativeFilter **nat
g_scanListener[index].isNeedReset = true;
nativeSize += g_scanListener[index].filterSize;
}
*filterSize = nativeSize;
*nativeFilter = (BleScanNativeFilter *)SoftBusCalloc(sizeof(BleScanNativeFilter) * nativeSize);
if (*nativeFilter == NULL) {
*filterSize = 0;
return;
}
*filterSize = nativeSize;
for (int index = 0; index < SCAN_MAX_NUM; index++) {
if (!g_scanListener[index].isNeedReset) {
continue;
@ -318,14 +257,11 @@ static void GetAllNativeScanFilter(int thisListenerId, BleScanNativeFilter **nat
static void ConvertScanResult(const BtScanResultData *src, SoftBusBleScanResult *dst)
{
if (src == NULL || dst == NULL) {
return;
}
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) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "ConvertScanResult memcpy addr fail");
CLOGE("ConvertScanResult memcpy addr fail");
return;
}
dst->primaryPhy = ConvertScanPhyType(src->primaryPhy);
@ -336,7 +272,7 @@ static void ConvertScanResult(const BtScanResultData *src, SoftBusBleScanResult
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) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "ConvertScanResult memcpy directAddr fail");
CLOGE("ConvertScanResult memcpy directAddr fail");
return;
}
dst->advLen = src->advLen;
@ -393,7 +329,7 @@ static void ConvertAdvParam(const SoftBusBleAdvParams *src, BleAdvParams *dst)
dst->ownAddrType = 0x00;
dst->peerAddrType = 0x00;
if (memcpy_s(dst->peerAddr.addr, BT_ADDR_LEN, src->peerAddr.addr, BT_ADDR_LEN) != EOK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "ConvertScanResult memcpy directAddr fail");
CLOGE("ConvertAdvParam memcpy directAddr fail");
return;
}
dst->channelMap = src->channelMap;
@ -413,7 +349,7 @@ static void WrapperAdvEnableCallback(int advId, int status)
advChannel->advCallback->AdvEnableCallback == NULL) {
continue;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "WrapperAdvEnableCallback, inner-advId: %d, bt-advId: %d, "
CLOGI("WrapperAdvEnableCallback, inner-advId: %d, bt-advId: %d, "
"status: %d", index, advId, st);
if (st == SOFTBUS_BT_STATUS_SUCCESS) {
advChannel->isAdvertising = true;
@ -435,7 +371,7 @@ static void WrapperAdvDisableCallback(int advId, int status)
advChannel->advCallback->AdvDisableCallback == NULL) {
continue;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "WrapperAdvDisableCallback, inner-advId: %d, bt-advId: %d, "
CLOGI("WrapperAdvDisableCallback, inner-advId: %d, bt-advId: %d, "
"status: %d", index, advId, st);
if (st == SOFTBUS_BT_STATUS_SUCCESS) {
advChannel->advId = -1;
@ -458,7 +394,7 @@ static void WrapperAdvDataCallback(int advId, int status)
advChannel->advCallback->AdvDataCallback == NULL) {
continue;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "WrapperAdvDataCallback, inner-advId: %d, bt-advId: %d, "
CLOGI("WrapperAdvDataCallback, inner-advId: %d, bt-advId: %d, "
"status: %d", index, advId, st);
advChannel->advCallback->AdvDataCallback(index, st);
break;
@ -476,7 +412,7 @@ static void WrapperAdvUpdateCallback(int advId, int status)
advChannel->advCallback->AdvUpdateCallback == NULL) {
continue;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "WrapperAdvUpdateCallback, inner-advId: %d, bt-advId: %d, "
CLOGI("WrapperAdvUpdateCallback, inner-advId: %d, bt-advId: %d, "
"status: %d", index, advId, st);
advChannel->advCallback->AdvUpdateCallback(index, st);
break;
@ -486,7 +422,7 @@ static void WrapperAdvUpdateCallback(int advId, int status)
static void WrapperSecurityRespondCallback(const BdAddr *bdAddr)
{
(void)bdAddr;
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "WrapperSecurityRespondCallback");
CLOGI("WrapperSecurityRespondCallback");
}
static void WrapperScanResultCallback(BtScanResultData *scanResultdata)
@ -514,7 +450,7 @@ static void WrapperScanParameterSetCompletedCallback(int clientId, int status)
{
(void)clientId;
(void)status;
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "WrapperScanParameterSetCompletedCallback");
CLOGI("WrapperScanParameterSetCompletedCallback");
}
static BtGattCallbacks g_softbusGattCb = {
@ -545,7 +481,7 @@ static bool CheckAdvChannelInUsed(int advId)
return false;
}
if (!g_advChannel[advId].isUsed) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "advId %d is ready released", advId);
CLOGE("advId %d is ready released", advId);
return false;
}
return true;
@ -555,23 +491,20 @@ static int SetAdvData(int advId, const SoftBusBleAdvData *data)
{
g_advChannel[advId].advData.advLength = data->advLength;
g_advChannel[advId].advData.scanRspLength = data->scanRspLength;
if (g_advChannel[advId].advData.advData != NULL) {
SoftBusFree(g_advChannel[advId].advData.advData);
g_advChannel[advId].advData.advData = NULL;
}
if (g_advChannel[advId].advData.scanRspData != NULL) {
SoftBusFree(g_advChannel[advId].advData.scanRspData);
g_advChannel[advId].advData.scanRspData = NULL;
}
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) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SetAdvData calloc advData failed");
CLOGE("SetAdvData calloc advData failed");
return SOFTBUS_MALLOC_ERR;
}
if (memcpy_s(g_advChannel[advId].advData.advData, data->advLength,
data->advData, data->advLength) != EOK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SetAdvData memcpy advData failed");
CLOGE("SetAdvData memcpy advData failed");
SoftBusFree(g_advChannel[advId].advData.advData);
g_advChannel[advId].advData.advData = NULL;
return SOFTBUS_MEM_ERR;
@ -580,14 +513,14 @@ static int SetAdvData(int advId, const SoftBusBleAdvData *data)
if (data->scanRspLength != 0) {
g_advChannel[advId].advData.scanRspData = SoftBusCalloc(data->scanRspLength);
if (g_advChannel[advId].advData.scanRspData == NULL) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SetAdvData calloc scanRspData failed");
CLOGE("SetAdvData 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) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SetAdvData memcpy scanRspData failed");
CLOGE("SetAdvData memcpy scanRspData failed");
SoftBusFree(g_advChannel[advId].advData.advData);
SoftBusFree(g_advChannel[advId].advData.scanRspData);
g_advChannel[advId].advData.advData = NULL;
@ -627,7 +560,7 @@ int SoftBusGetAdvChannel(const SoftBusAdvCallback *callback)
}
}
if (freeAdvId == ADV_MAX_NUM) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "no available adv channel");
CLOGE("no available adv channel");
SoftBusMutexUnlock(&g_advLock);
return SOFTBUS_ERR;
}
@ -694,7 +627,7 @@ int SoftBusStartAdv(int advId, const SoftBusBleAdvParams *param)
return SOFTBUS_ERR;
}
if (g_advChannel[advId].isAdvertising) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_WARN, "SoftBusStartAdv, wait condition inner-advId: %d", advId);
CLOGW("SoftBusStartAdv, wait condition inner-advId: %d", advId);
SoftBusCondWait(&g_advChannel[advId].cond, &g_advLock, NULL);
}
int innerAdvId;
@ -703,7 +636,7 @@ int SoftBusStartAdv(int advId, const SoftBusBleAdvParams *param)
ConvertAdvParam(param, &dstParam);
ConvertAdvData(&g_advChannel[advId].advData, &advData);
int ret = BleStartAdvEx(&innerAdvId, advData, dstParam);
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "BleStartAdvEx, inner-advId: %d, bt-advId: %d, "
CLOGI("BleStartAdvEx, inner-advId: %d, bt-advId: %d, "
"ret: %d", advId, innerAdvId, ret);
if (ret != OHOS_BT_STATUS_SUCCESS) {
g_advChannel[advId].advCallback->AdvEnableCallback(advId, SOFTBUS_BT_STATUS_FAIL);
@ -726,12 +659,12 @@ int SoftBusStopAdv(int advId)
return SOFTBUS_ERR;
}
if (!g_advChannel[advId].isAdvertising) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_WARN, "SoftBusStopAdv, wait condition inner-advId: %d, "
CLOGW("SoftBusStopAdv, wait condition inner-advId: %d, "
"bt-advId: %d", advId, g_advChannel[advId].advId);
SoftBusCondWait(&g_advChannel[advId].cond, &g_advLock, NULL);
}
int ret = BleStopAdv(g_advChannel[advId].advId);
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "SoftBusStopAdv, inner-advId: %d, "
CLOGI("SoftBusStopAdv, inner-advId: %d, "
"bt-advId: %d, ret: %d", advId, g_advChannel[advId].advId, ret);
if (ret != OHOS_BT_STATUS_SUCCESS) {
g_advChannel[advId].advCallback->AdvDisableCallback(advId, SOFTBUS_BT_STATUS_FAIL);
@ -791,44 +724,19 @@ static void FreeScanFilter(int listenerId)
{
uint8_t filterSize = g_scanListener[listenerId].filterSize;
SoftBusBleScanFilter *filter = g_scanListener[listenerId].filter;
if (filter == NULL || filterSize == 0) {
return;
}
if (!g_scanListener[listenerId].isUsed) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_DBG, "ScanListener id:%d is not in use", listenerId);
return;
}
while (filterSize-- > 0) {
if ((filter + filterSize) == NULL) {
continue;
}
if ((filter + filterSize)->address != NULL) {
SoftBusFree((filter + filterSize)->address);
}
if ((filter + filterSize)->deviceName != NULL) {
SoftBusFree((filter + filterSize)->deviceName);
}
if ((filter + filterSize)->serviceUuid != NULL) {
SoftBusFree((filter + filterSize)->serviceUuid);
}
if ((filter + filterSize)->serviceUuidMask != NULL) {
SoftBusFree((filter + filterSize)->serviceUuidMask);
}
if ((filter + filterSize)->serviceData != NULL) {
SoftBusFree((filter + filterSize)->serviceData);
}
if ((filter + filterSize)->serviceDataMask != NULL) {
SoftBusFree((filter + filterSize)->serviceDataMask);
}
if ((filter + filterSize)->manufactureData != NULL) {
SoftBusFree((filter + filterSize)->manufactureData);
}
if ((filter + filterSize)->manufactureDataMask != NULL) {
SoftBusFree((filter + filterSize)->manufactureDataMask);
}
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)
@ -883,7 +791,7 @@ int SoftBusSetScanFilter(int listenerId, const SoftBusBleScanFilter *filter, uin
return SOFTBUS_LOCK_ERR;
}
if (!g_scanListener[listenerId].isUsed) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "ScanListener id:%d is not in use", listenerId);
CLOGE("ScanListener id:%d is not in use", listenerId);
SoftBusMutexUnlock(&g_scanerLock);
return SOFTBUS_ERR;
}
@ -904,7 +812,7 @@ int SoftBusStartScan(int listenerId, const SoftBusBleScanParams *param)
return SOFTBUS_LOCK_ERR;
}
if (!g_scanListener[listenerId].isUsed) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "ScanListener id:%d is not in use", listenerId);
CLOGE("ScanListener id:%d is not in use", listenerId);
SoftBusMutexUnlock(&g_scanerLock);
return SOFTBUS_ERR;
}

View File

@ -33,10 +33,10 @@ static SoftBusGattcCallback *g_softBusGattcCallback = NULL;
static void GattcConnectionStateChangedCallback(int clientId, int connectionState, int status)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "StateChangedCallback id=%d, state=%d, status=%d",
CLOGI("StateChangedCallback id=%d, state=%d, status=%d",
clientId, connectionState, status);
if (connectionState != OHOS_STATE_CONNECTED && connectionState != OHOS_STATE_DISCONNECTED) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "GattcConnectionStateChangedCallback ignore");
CLOGI("GattcConnectionStateChangedCallback ignore");
return;
}
@ -45,52 +45,52 @@ static void GattcConnectionStateChangedCallback(int clientId, int connectionStat
static void GattcConnectParaUpdateCallback(int clientId, int interval, int latency, int timeout, int status)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "ParaUpdateCallback");
CLOGI("ParaUpdateCallback");
}
static void GattcSearchServiceCompleteCallback(int clientId, int status)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "SearchServiceCompleteCallback, id=%d, status=%d",
CLOGI("SearchServiceCompleteCallback, id=%d, status=%d",
clientId, status);
g_softBusGattcCallback->ServiceCompleteCallback(clientId, status);
}
static void GattcReadCharacteristicCallback(int clientId, BtGattReadData *readData, int status)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "ReadCharacteristicCallback, id=%d, status=%d", clientId, status);
CLOGI("ReadCharacteristicCallback, id=%d, status=%d", clientId, status);
}
static void GattcWriteCharacteristicCallback(int clientId, BtGattCharacteristic *characteristic, int status)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "WriteCharacteristicCallback, id=%d, status=%d", clientId, status);
CLOGI("WriteCharacteristicCallback, id=%d, status=%d", clientId, status);
}
static void GattcReadDescriptorCallback(int clientId, BtGattReadData *readData, int status)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "ReadDescriptorCallback, id=%d, status=%d", clientId, status);
CLOGI("ReadDescriptorCallback, id=%d, status=%d", clientId, status);
}
static void GattcWriteDescriptorCallback(int clientId, BtGattDescriptor *descriptor, int status)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "WriteDescriptorCallback, id=%d, status=%d", clientId, status);
CLOGI("WriteDescriptorCallback, id=%d, status=%d", clientId, status);
}
static void GattcConfigureMtuSizeCallback(int clientId, int mtuSize, int status)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "ConfigureMtuSizeCallback, id=%d, mtusize=%d, status=%d",
CLOGI("ConfigureMtuSizeCallback, id=%d, mtusize=%d, status=%d",
clientId, mtuSize, status);
g_softBusGattcCallback->ConfigureMtuSizeCallback(clientId, mtuSize, status);
}
static void GattcRegisterNotificationCallback(int clientId, int status)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "RegisterNotificationCallback, id=%d, status=%d", clientId, status);
CLOGI("RegisterNotificationCallback, id=%d, status=%d", clientId, status);
g_softBusGattcCallback->RegistNotificationCallback(clientId, status);
}
static void GattcNotificationCallback(int clientId, BtGattReadData *notifyData, int status)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "GattcNotificationCallback, id=%d, status=%d", clientId, status);
CLOGI("GattcNotificationCallback, id=%d, status=%d", clientId, status);
if (notifyData == NULL) {
return;
}
@ -100,7 +100,7 @@ static void GattcNotificationCallback(int clientId, BtGattReadData *notifyData,
notify.data = notifyData->data;
notify.charaUuid.uuid = notifyData->attribute.characteristic.characteristicUuid.uuid;
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "GattcNotificationCallback, id=%d, status=%d", clientId, status);
CLOGI("GattcNotificationCallback, id=%d, status=%d", clientId, status);
g_softBusGattcCallback->NotificationReceiveCallback(clientId, &notify, status);
}
@ -127,17 +127,17 @@ int32_t SoftbusGattcRegister(void)
appId.uuidLen = APP_UUID_LEN;
int32_t clientId = BleGattcRegister(appId);
if (clientId <= 0) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleGattcRegister error");
CLOGE("BleGattcRegister error");
return INVALID_ID;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "BleGattcRegister %d", clientId);
CLOGI("BleGattcRegister %d", clientId);
return clientId;
}
int32_t SoftbusGattcUnRegister(int32_t clientId)
{
if (BleGattcUnRegister(clientId) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleGattcUnRegister error");
CLOGE("BleGattcUnRegister error");
return SOFTBUS_GATTC_INTERFACE_FAILED;
}
return SOFTBUS_OK;
@ -147,13 +147,13 @@ int32_t SoftbusGattcConnect(int32_t clientId, SoftBusBtAddr *addr)
{
BdAddr bdAddr;
if (memcpy_s(bdAddr.addr, OHOS_BD_ADDR_LEN, addr->addr, BT_ADDR_LEN) != EOK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SoftbusGattcConnect memcpy error");
CLOGE("SoftbusGattcConnect memcpy error");
return SOFTBUS_INVALID_PARAM;
}
int32_t status = BleOhosStatusToSoftBus(
BleGattcConnect(clientId, &g_btGattClientCallbacks, &bdAddr, false, OHOS_BT_TRANSPORT_TYPE_LE));
if (status != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleGattcConnect error");
CLOGE("BleGattcConnect error");
return SOFTBUS_GATTC_INTERFACE_FAILED;
}
@ -163,7 +163,7 @@ int32_t SoftbusGattcConnect(int32_t clientId, SoftBusBtAddr *addr)
int32_t SoftbusBleGattcDisconnect(int32_t clientId)
{
if (BleGattcDisconnect(clientId) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleGattcDisconnect error");
CLOGE("BleGattcDisconnect error");
return SOFTBUS_GATTC_INTERFACE_FAILED;
}
return SOFTBUS_OK;
@ -171,10 +171,10 @@ int32_t SoftbusBleGattcDisconnect(int32_t clientId)
int32_t SoftbusGattcSearchServices(int32_t clientId)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "SoftbusGattcSearchServices %d", clientId);
CLOGI("SoftbusGattcSearchServices %d", clientId);
int32_t status = BleOhosStatusToSoftBus(BleGattcSearchServices(clientId));
if (status != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleGattcSearchServices error, status = %d", status);
CLOGE("BleGattcSearchServices error, status = %d", status);
return SOFTBUS_GATTC_INTERFACE_FAILED;
}
return SOFTBUS_OK;
@ -183,14 +183,14 @@ int32_t SoftbusGattcSearchServices(int32_t clientId)
int32_t SoftbusGattcGetService(int32_t clientId, SoftBusBtUuid *serverUuid)
{
if (clientId <= 0) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SoftbusGattcGetService invalid param");
CLOGE("SoftbusGattcGetService invalid param");
return SOFTBUS_INVALID_PARAM;
}
BtUuid btUuid;
btUuid.uuid = serverUuid->uuid;
btUuid.uuidLen = serverUuid->uuidLen;
if (!BleGattcGetService(clientId, btUuid)) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleGattcGetService error");
CLOGE("BleGattcGetService error");
return SOFTBUS_GATTC_INTERFACE_FAILED;
}
return SOFTBUS_OK;
@ -205,7 +205,7 @@ int32_t SoftbusGattcRegisterNotification(int32_t clientId, SoftBusBtUuid *server
btCharaUuid.characteristicUuid.uuidLen = charaUuid->uuidLen;
int32_t status = BleOhosStatusToSoftBus(BleGattcRegisterNotification(clientId, btCharaUuid, true));
if (status != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleGattcRegisterNotification error");
CLOGE("BleGattcRegisterNotification error");
return SOFTBUS_GATTC_INTERFACE_FAILED;
}
return SOFTBUS_OK;
@ -214,7 +214,7 @@ int32_t SoftbusGattcRegisterNotification(int32_t clientId, SoftBusBtUuid *server
int32_t SoftbusGattcConfigureMtuSize(int32_t clientId, int mtuSize)
{
if (BleGattcConfigureMtuSize(clientId, mtuSize) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleGattcConfigureMtuSize error");
CLOGE("BleGattcConfigureMtuSize error");
return SOFTBUS_GATTC_INTERFACE_FAILED;
}
return SOFTBUS_OK;
@ -223,10 +223,10 @@ int32_t SoftbusGattcConfigureMtuSize(int32_t clientId, int mtuSize)
int32_t SoftbusGattcWriteCharacteristic(int32_t clientId, SoftBusGattcData *clientData)
{
if (clientId <= 0 || clientData == NULL) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SoftbusGattcWriteCharacteristic invalid param");
CLOGE("SoftbusGattcWriteCharacteristic invalid param");
return SOFTBUS_INVALID_PARAM;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "SoftbusGattcRegisterNotification clientId = %d", clientId);
CLOGI("SoftbusGattcRegisterNotification clientId = %d", clientId);
BtGattCharacteristic characteristic;
characteristic.serviceUuid.uuid = clientData->serviceUuid.uuid;
characteristic.serviceUuid.uuidLen = clientData->serviceUuid.uuidLen;
@ -234,7 +234,7 @@ int32_t SoftbusGattcWriteCharacteristic(int32_t clientId, SoftBusGattcData *clie
characteristic.characteristicUuid.uuidLen = clientData->characterUuid.uuidLen;
if (BleGattcWriteCharacteristic(clientId, characteristic, OHOS_GATT_WRITE_NO_RSP,
clientData->valueLen, clientData->value) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SoftbusGattcWriteCharacteristic error");
CLOGE("SoftbusGattcWriteCharacteristic error");
return SOFTBUS_GATTC_INTERFACE_FAILED;
}
return SOFTBUS_OK;

View File

@ -44,7 +44,7 @@ int CheckGattsStatus(void)
return SOFTBUS_ERR;
}
while (g_halRegFlag == 0) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "ble hal registerring");
CLOGE("ble hal registerring");
static int tryTimes = WAIT_HAL_REG_RETRY;
if (tryTimes > 0) {
SoftBusSleepMs(WAIT_HAL_REG_TIME);
@ -119,8 +119,7 @@ int SoftBusGattsStartService(int srvcHandle)
if (CheckGattsStatus() != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"BLEINFOPRTINT:BleGattsStartService(%d, %d)", g_halServerId, srvcHandle);
CLOGI("BLEINFOPRTINT:BleGattsStartService(%d, %d)", g_halServerId, srvcHandle);
if (BleGattsStartService(g_halServerId, srvcHandle) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
@ -132,8 +131,7 @@ int SoftBusGattsStopService(int srvcHandle)
if (CheckGattsStatus() != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"BLEINFOPRTINT:SoftBusGattsStopService(%d, %d)", g_halServerId, srvcHandle);
CLOGI("BLEINFOPRTINT:SoftBusGattsStopService(%d, %d)", g_halServerId, srvcHandle);
if (BleGattsStopService(g_halServerId, srvcHandle) != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
@ -158,7 +156,7 @@ int SoftBusGattsDisconnect(SoftBusBtAddr btAddr, int connId)
}
BdAddr addr;
if (memcpy_s(addr.addr, BT_ADDR_LEN, btAddr.addr, BT_ADDR_LEN) != EOK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SoftBusGattsDisconnect memcpy fail");
CLOGE("SoftBusGattsDisconnect memcpy fail");
return SOFTBUS_ERR;
}
if (BleGattsDisconnect(g_halServerId, addr, connId) != SOFTBUS_OK) {
@ -187,7 +185,7 @@ int SoftBusGattsSendResponse(SoftBusGattsResponse *param)
int SoftBusGattsSendNotify(SoftBusGattsNotify *param)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "SoftBusGattsSendNotify enter");
CLOGI("SoftBusGattsSendNotify enter");
if (CheckGattsStatus() != SOFTBUS_OK) {
return SOFTBUS_ERR;
}
@ -198,11 +196,10 @@ int SoftBusGattsSendNotify(SoftBusGattsNotify *param)
.valueLen = param->valueLen,
.value = param->value
};
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"SoftBusGattsSendNotify call BleGattsSendIndication halconnId:%d attrHandle:%d confirm:%d",
CLOGI("SoftBusGattsSendNotify call BleGattsSendIndication halconnId:%d attrHandle:%d confirm:%d",
notify.connectId, notify.attrHandle, notify.confirm);
if (BleGattsSendIndication(g_halServerId, &notify) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SoftBusGattsSendNotify failed");
CLOGE("SoftBusGattsSendNotify failed");
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
@ -210,33 +207,30 @@ int SoftBusGattsSendNotify(SoftBusGattsNotify *param)
static void BleRegisterServerCallback(int status, int serverId, BtUuid *appUuid)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"BleRegisterServerCallback status=%d severId=%d", status, serverId);
CLOGI("BleRegisterServerCallback status=%d severId=%d", status, serverId);
if ((appUuid == NULL) || (appUuid->uuid == NULL)) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleRegisterServerCallback appUuid is null");
CLOGE("BleRegisterServerCallback appUuid is null");
return;
}
if (memcmp(appUuid->uuid, SOFTBUS_APP_UUID, appUuid->uuidLen) != 0) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleRegisterServerCallback unknown uuid");
CLOGE("BleRegisterServerCallback unknown uuid");
return;
}
if (status != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleRegisterServerCallback failed, status=%d", status);
CLOGE("BleRegisterServerCallback failed, status=%d", status);
g_halRegFlag = -1;
} else {
g_halRegFlag = 1;
g_halServerId = serverId;
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"BLEINFOPRTINT:BleRegisterServerCallback g_halServerId:%d)", g_halServerId);
CLOGI("BLEINFOPRTINT:BleRegisterServerCallback g_halServerId:%d)", g_halServerId);
}
}
static void BleConnectServerCallback(int connId, int serverId, const BdAddr *bdAddr)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"ConnectServerCallback is coming, connId=%d serverId=%d\n", connId, serverId);
CLOGI("ConnectServerCallback is coming, connId=%d serverId=%d\n", connId, serverId);
if (serverId != g_halServerId) {
return;
}
@ -245,8 +239,7 @@ static void BleConnectServerCallback(int connId, int serverId, const BdAddr *bdA
static void BleDisconnectServerCallback(int connId, int serverId, const BdAddr *bdAddr)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"DisconnectServerCallback is coming, connId=%d severId=%d", connId, serverId);
CLOGI("DisconnectServerCallback is coming, connId=%d severId=%d", connId, serverId);
if (serverId != g_halServerId) {
return;
}
@ -256,12 +249,11 @@ static void BleDisconnectServerCallback(int connId, int serverId, const BdAddr *
static void BleServiceAddCallback(int status, int serverId, BtUuid *uuid, int srvcHandle)
{
(void)serverId;
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "ServiceAddCallback srvcHandle=%d\n", srvcHandle);
CLOGI("ServiceAddCallback srvcHandle=%d\n", srvcHandle);
if (serverId != g_halServerId) {
return;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"BLEINFOPRTINT:BleServiceAddCallback srvcHandle:%d)", srvcHandle);
CLOGI("BLEINFOPRTINT:BleServiceAddCallback srvcHandle:%d)", srvcHandle);
g_gattsCallback->ServiceAddCallback(status, (SoftBusBtUuid *)uuid, srvcHandle);
}
@ -269,53 +261,45 @@ static void BleIncludeServiceAddCallback(int status, int serverId, int srvcHandl
{
(void)serverId;
(void)srvcHandle;
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"IncludeServiceAddCallback srvcHandle=%d,includeSrvcHandle=%d\n", srvcHandle, includeSrvcHandle);
CLOGI("IncludeServiceAddCallback srvcHandle=%d,includeSrvcHandle=%d\n", srvcHandle, includeSrvcHandle);
}
static void BleCharacteristicAddCallback(int status, int serverId, BtUuid *uuid, int srvcHandle,
int characteristicHandle)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"CharacteristicAddCallback srvcHandle=%d,charHandle=%d\n", srvcHandle, characteristicHandle);
CLOGI("CharacteristicAddCallback srvcHandle=%d,charHandle=%d\n", srvcHandle, characteristicHandle);
if (serverId != g_halServerId) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "bad server id");
CLOGE("bad server id");
return;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"BLEINFOPRTINT:BleCharacteristicAddCallback characteristicHandle:%d)", characteristicHandle);
CLOGI("BLEINFOPRTINT:BleCharacteristicAddCallback characteristicHandle:%d)", characteristicHandle);
g_gattsCallback->CharacteristicAddCallback(status, (SoftBusBtUuid *)uuid, srvcHandle, characteristicHandle);
}
static void BleDescriptorAddCallback(int status, int serverId, BtUuid *uuid,
int srvcHandle, int descriptorHandle)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "DescriptorAddCallback srvcHandle=%d,descriptorHandle=%d\n",
srvcHandle, descriptorHandle);
CLOGI("DescriptorAddCallback srvcHandle=%d,descriptorHandle=%d", srvcHandle, descriptorHandle);
if (serverId != g_halServerId) {
return;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"BLEINFOPRTINT:BleDescriptorAddCallback descriptorHandle:%d)", descriptorHandle);
CLOGI("BLEINFOPRTINT:BleDescriptorAddCallback descriptorHandle:%d)", descriptorHandle);
g_gattsCallback->DescriptorAddCallback(status, (SoftBusBtUuid *)uuid, srvcHandle, descriptorHandle);
}
static void BleServiceStartCallback(int status, int serverId, int srvcHandle)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"ServiceStartCallback serverId=%d,srvcHandle=%d\n", serverId, srvcHandle);
CLOGI("ServiceStartCallback serverId=%d,srvcHandle=%d\n", serverId, srvcHandle);
if (serverId != g_halServerId) {
return;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"BLEINFOPRTINT:BleServiceStartCallback srvcHandle:%d)", srvcHandle);
CLOGI("BLEINFOPRTINT:BleServiceStartCallback srvcHandle:%d)", srvcHandle);
g_gattsCallback->ServiceStartCallback(status, srvcHandle);
}
static void BleServiceStopCallback(int status, int serverId, int srvcHandle)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"ServiceStopCallback serverId=%d,srvcHandle=%d\n", serverId, srvcHandle);
CLOGI("ServiceStopCallback serverId=%d,srvcHandle=%d\n", serverId, srvcHandle);
if (serverId != g_halServerId) {
return;
}
@ -324,8 +308,7 @@ static void BleServiceStopCallback(int status, int serverId, int srvcHandle)
static void BleServiceDeleteCallback(int status, int serverId, int srvcHandle)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"ServiceDeleteCallback serverId=%d,srvcHandle=%d\n", serverId, srvcHandle);
CLOGI("ServiceDeleteCallback serverId=%d,srvcHandle=%d\n", serverId, srvcHandle);
if (serverId != g_halServerId) {
return;
}
@ -334,8 +317,7 @@ static void BleServiceDeleteCallback(int status, int serverId, int srvcHandle)
static void BleRequestReadCallback(BtReqReadCbPara readCbPara)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"RequestReadCallback transId=%d, attrHandle=%d\n", readCbPara.transId, readCbPara.attrHandle);
CLOGI("RequestReadCallback transId=%d, attrHandle=%d\n", readCbPara.transId, readCbPara.attrHandle);
SoftBusGattReadRequest req = {
.connId = readCbPara.connId,
.transId = readCbPara.transId,
@ -349,8 +331,7 @@ static void BleRequestReadCallback(BtReqReadCbPara readCbPara)
static void BleRequestWriteCallback(BtReqWriteCbPara writeCbPara)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"RequestWriteCallback transId=%d, attrHandle=%d\n", writeCbPara.transId, writeCbPara.attrHandle);
CLOGI("RequestWriteCallback transId=%d, attrHandle=%d\n", writeCbPara.transId, writeCbPara.attrHandle);
SoftBusGattWriteRequest req = {
.connId = writeCbPara.connId,
.transId = writeCbPara.transId,
@ -367,22 +348,19 @@ static void BleRequestWriteCallback(BtReqWriteCbPara writeCbPara)
static void BleResponseConfirmationCallback(int status, int handle)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"ResponseConfirmationCallback status=%d, handle=%d\n", status, handle);
CLOGI("ResponseConfirmationCallback status=%d, handle=%d\n", status, handle);
g_gattsCallback->ResponseConfirmationCallback(status, handle);
}
static void BleIndicationSentCallback(int connId, int status)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"IndicationSentCallback status=%d, connId=%d\n", status, connId);
CLOGI("IndicationSentCallback status=%d, connId=%d\n", status, connId);
g_gattsCallback->NotifySentCallback(connId, status);
}
static void BleMtuChangeCallback(int connId, int mtu)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"MtuChangeCallback connId=%d, mtu=%d\n", connId, mtu);
CLOGI("MtuChangeCallback connId=%d, mtu=%d\n", connId, mtu);
g_gattsCallback->MtuChangeCallback(connId, mtu);
}
@ -409,18 +387,17 @@ static int GattsRegisterHalCallback(void)
int SoftBusRegisterGattsCallbacks(SoftBusGattsCallback *callback)
{
if (callback == NULL) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SoftBusRegisterGattsCallbacks fail:nullptr");
CLOGE("SoftBusRegisterGattsCallbacks fail:nullptr");
return SOFTBUS_BLECONNECTION_REG_GATTS_CALLBACK_FAIL;
}
if (g_gattsCallback != NULL) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_WARN, "SoftBusRegisterGattsCallbacks register again");
CLOGW("SoftBusRegisterGattsCallbacks register again");
} else {
g_gattsCallback = callback;
}
int ret = GattsRegisterHalCallback();
if (ret != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR,
"SoftBusRegisterGattsCallbacks GattsRegisterCallbacks failed:%d", ret);
CLOGE("SoftBusRegisterGattsCallbacks GattsRegisterCallbacks failed:%d", ret);
return SOFTBUS_BLECONNECTION_REG_GATTS_CALLBACK_FAIL;
}
if (g_halRegFlag == -1) {
@ -431,7 +408,7 @@ int SoftBusRegisterGattsCallbacks(SoftBusGattsCallback *callback)
ret = BleGattsRegister(uuid);
if (ret != SOFTBUS_OK) {
g_halRegFlag = -1;
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleGattsRegister failed%d", ret);
CLOGE("BleGattsRegister failed%d", ret);
return SOFTBUS_BLECONNECTION_REG_GATTS_CALLBACK_FAIL;
}
}
@ -441,15 +418,15 @@ int SoftBusRegisterGattsCallbacks(SoftBusGattsCallback *callback)
void SoftBusUnRegisterGattsCallbacks(void)
{
if (g_gattsCallback == NULL) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "no need to unregist gatts callback.");
CLOGI("no need to unregist gatts callback.");
return;
}
if (g_halRegFlag == -1) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "no need to unregist gatt server.");
CLOGI("no need to unregist gatt server.");
return;
}
if (BleGattsUnRegister(g_halServerId) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleGattsUnRegister error.");
CLOGE("BleGattsUnRegister error.");
return;
}
g_halServerId = -1;

View File

@ -34,7 +34,7 @@ typedef struct {
SoftBusBtStateListener *listener;
} StateListener;
static int ConvertBtState(const int transport, int state)
static int ConvertBtState(int transport, int state)
{
switch (state) {
case OHOS_GAP_STATE_TURNING_ON:
@ -71,7 +71,7 @@ static SoftBusBtAddr ConvertBtAddr(const BdAddr *bdAddr)
{
SoftBusBtAddr btAddr = {0};
if (memcpy_s(btAddr.addr, sizeof(btAddr.addr), bdAddr->addr, sizeof(bdAddr->addr)) != EOK) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "copy bdAddr fail");
CLOGE("copy bdAddr fail");
}
return btAddr;
}
@ -81,10 +81,9 @@ static bool g_isRegCb = false;
static void WrapperStateChangeCallback(const int transport, const int status)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "WrapperStateChangeCallback, transport=%d, status=%d",
transport, status);
CLOGI("WrapperStateChangeCallback, transport=%d, status=%d", transport, status);
int listenerId;
int st = ConvertBtState(transport, (BtStatus)status);
int st = ConvertBtState(transport, status);
for (listenerId = 0; listenerId < STATE_LISTENER_MAX_NUM; listenerId++) {
if (g_stateListener[listenerId].isUsed &&
g_stateListener[listenerId].listener != NULL &&
@ -97,12 +96,11 @@ static void WrapperStateChangeCallback(const int transport, const int status)
static void WrapperAclStateChangedCallback(const BdAddr *bdAddr, GapAclState state, unsigned int reason)
{
if (bdAddr == NULL) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "WrapperAclStateChangedCallback addr is null");
CLOGE("WrapperAclStateChangedCallback addr is null");
return;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"WrapperAclStateChangedCallback, addr:%02X:%02X:***%02X, state=%d, reason=%u\n",
CLOGI("WrapperAclStateChangedCallback, addr:%02X:%02X:***%02X, state=%d, reason=%u",
bdAddr->addr[MAC_FIRST_INDEX], bdAddr->addr[MAC_ONE_INDEX], bdAddr->addr[MAC_FIVE_INDEX], state, reason);
int listenerId;
int aclState = ConvertAclState(state);
@ -119,31 +117,29 @@ static void WrapperAclStateChangedCallback(const BdAddr *bdAddr, GapAclState sta
static void WrapperPairRequestedCallback(const BdAddr *bdAddr, int transport)
{
if (bdAddr == NULL) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "WrapperPairRequestedCallback addr is null");
CLOGE("WrapperPairRequestedCallback addr is null");
return;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"WrapperPairRequestedCallback, addr:%02X:%02X:***%02X, transport=%d\n",
CLOGI("WrapperPairRequestedCallback, addr:%02X:%02X:***%02X, transport=%d",
bdAddr->addr[MAC_FIRST_INDEX], bdAddr->addr[MAC_ONE_INDEX], bdAddr->addr[MAC_FIVE_INDEX], transport);
if (PairRequestReply(bdAddr, transport, true) != true) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "PairRequestReply error");
if (!PairRequestReply(bdAddr, transport, true)) {
CLOGE("PairRequestReply error");
}
}
static void WrapperPairConfiremedCallback(const BdAddr *bdAddr, int transport, int reqType, int number)
{
if (bdAddr == NULL) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "WrapperPairConfirmedCallback addr is null");
CLOGE("WrapperPairConfirmedCallback addr is null");
return;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"WrapperPairConfirmedCallback, addr=%02X:%02X:***%02X, transport=%d, reqType:%d, number:%d\n",
CLOGI("WrapperPairConfirmedCallback, addr=%02X:%02X:***%02X, transport=%d, reqType:%d, number:%d",
bdAddr->addr[MAC_FIRST_INDEX], bdAddr->addr[MAC_ONE_INDEX], bdAddr->addr[MAC_FIVE_INDEX],
transport, reqType, number);
if (SetDevicePairingConfirmation(bdAddr, transport, true) != true) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SetDevicePairingConfirmation error");
if (!SetDevicePairingConfirmation(bdAddr, transport, true)) {
CLOGE("SetDevicePairingConfirmation error");
}
}

View File

@ -24,6 +24,10 @@ extern "C" {
#endif
#endif
// max adv and scan limit
#define ADV_MAX_NUM 7
#define SCAN_MAX_NUM 5
// Bluetooth scan duty cycle, unit: ms
#define SOFTBUS_BLE_SCAN_INTERVAL_P2 3000
#define SOFTBUS_BLE_SCAN_INTERVAL_P10 600

View File

@ -20,7 +20,7 @@ import("//foundation/communication/dsoftbus/dsoftbus.gni")
module_output_path = "dsoftbus/adapter"
ohos_unittest("BluetoothAdapterTest") {
ohos_unittest("AdapterBluetoothTest") {
module_out_path = module_output_path
sources = [
"adapter_bt_utils_test.cpp",
@ -55,5 +55,5 @@ ohos_unittest("BluetoothAdapterTest") {
group("unittest") {
testonly = true
deps = [ ":BluetoothAdapterTest" ]
deps = [ ":AdapterBluetoothTest" ]
}

View File

@ -34,7 +34,8 @@ MockBluetooth::MockBluetooth()
MockBluetooth::targetMocker = this;
}
MockBluetooth::~MockBluetooth() {
MockBluetooth::~MockBluetooth()
{
MockBluetooth::targetMocker = nullptr;
}

View File

@ -210,6 +210,75 @@ EXIT:
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;
EXPECT_CALL(mocker, BleGattRegisterCallbacks).Times(AtMost(1)).WillOnce(ActionBleGattRegisterCallbacks);
ASSERT_EQ(SoftBusAddScanListener(nullptr), SOFTBUS_ERR);
int scanListerIds[SCAN_MAX_NUM];
for (size_t i = 0; i < SCAN_MAX_NUM; i++) {
scanListerIds[i] = SoftBusAddScanListener(GetStubScanListener());
ASSERT_NE(scanListerIds[i], SOFTBUS_ERR);
}
ASSERT_EQ(SoftBusAddScanListener(GetStubScanListener()), 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;
auto result = PrepareScanListener(mocker, &listenerId);
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;
auto result = PrepareScanListener(mocker, &listenerId);
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
@ -259,11 +328,130 @@ HWTEST_F(AdapterBleGattTest, ScanLifecycle, TestSize.Level3)
ASSERT_EQ(ret, SOFTBUS_OK);
ASSERT_TRUE(scanStopCtx.Expect(listenerId, SOFTBUS_BT_STATUS_SUCCESS));
ret = SoftBusRemoveScanListener(listenerId);
ASSERT_EQ(ret, SOFTBUS_OK);
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;
auto result = PrepareScanListener(mocker, &listenerId);
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).Times(1).WillOnce(Return(OHOS_BT_STATUS_SUCCESS));
ASSERT_EQ(SoftBusStartScan(listenerId, nullptr), SOFTBUS_INVALID_PARAM);
ASSERT_EQ(SoftBusStartScan(listenerId, &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;
btGattCallback->scanResultCb(&mockScanResult);
ASSERT_TRUE(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;
btGattCallback->scanResultCb(&mockScanResult);
ASSERT_TRUE(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;
btGattCallback->scanResultCb(&mockScanResult);
ASSERT_TRUE(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;
btGattCallback->scanResultCb(&mockScanResult);
ASSERT_TRUE(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;
btGattCallback->scanResultCb(&mockScanResult);
ASSERT_TRUE(scanResultCtx.Expect(listenerId, &expectScanResult));
mockScanResult.eventType = OHOS_BLE_EVT_SCANNABLE_DIRECTED;
mockScanResult.addrType = OHOS_BLE_NO_ADDRESS;
mockScanResult.directAddrType = OHOS_BLE_NO_ADDRESS;
btGattCallback->scanResultCb(&mockScanResult);
ASSERT_TRUE(scanResultCtx.Expect(listenerId, &expectScanResult));
mockScanResult.eventType = OHOS_BLE_EVT_LEGACY_NON_CONNECTABLE;
mockScanResult.addrType = UINT8_MAX;
mockScanResult.directAddrType = UINT8_MAX;
btGattCallback->scanResultCb(&mockScanResult);
ASSERT_TRUE(scanResultCtx.Expect(listenerId, &expectScanResult));
mockScanResult.eventType = OHOS_BLE_EVT_LEGACY_SCANNABLE;
btGattCallback->scanResultCb(&mockScanResult);
ASSERT_TRUE(scanResultCtx.Expect(listenerId, &expectScanResult));
mockScanResult.eventType = OHOS_BLE_EVT_LEGACY_CONNECTABLE;
btGattCallback->scanResultCb(&mockScanResult);
ASSERT_TRUE(scanResultCtx.Expect(listenerId, &expectScanResult));
mockScanResult.eventType = OHOS_BLE_EVT_LEGACY_CONNECTABLE_DIRECTED;
btGattCallback->scanResultCb(&mockScanResult);
ASSERT_TRUE(scanResultCtx.Expect(listenerId, &expectScanResult));
mockScanResult.eventType = OHOS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV_SCAN;
btGattCallback->scanResultCb(&mockScanResult);
ASSERT_TRUE(scanResultCtx.Expect(listenerId, &expectScanResult));
mockScanResult.eventType = OHOS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV;
btGattCallback->scanResultCb(&mockScanResult);
ASSERT_TRUE(scanResultCtx.Expect(listenerId, &expectScanResult));
mockScanResult.eventType = UINT8_MAX;
btGattCallback->scanResultCb(&mockScanResult);
ASSERT_TRUE(scanResultCtx.Expect(listenerId, &expectScanResult));
}
/**
* @tc.name: AdapterBleGattTest_AdvertiseLifecycle
* @tc.desc: test complete Advertisement life cycle
@ -284,32 +472,63 @@ HWTEST_F(AdapterBleGattTest, AdvertiseLifecycle, TestSize.Level3)
.advData = (char *)advDataExample,
.scanRspLength = sizeof(scanRspDataExample),
.scanRspData = (char *)scanRspDataExample};
auto ret = SoftBusSetAdvData(advId, &data);
ASSERT_EQ(ret, SOFTBUS_OK);
ASSERT_EQ(SoftBusSetAdvData(advId, &data), SOFTBUS_OK);
ASSERT_TRUE(advDataCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS));
SoftBusBleAdvParams params = {0};
ret = SoftBusStartAdv(advId, &params);
ASSERT_EQ(ret, SOFTBUS_OK);
ASSERT_EQ(SoftBusStartAdv(advId, &params), SOFTBUS_OK);
ASSERT_TRUE(advEnableCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS));
// 模拟蓝牙广播成功回调, 广播成功会被再次回调, adapter状态才能恢复正常
btGattCallback->advEnableCb(btInnerAdvId, SOFTBUS_BT_STATUS_SUCCESS);
ASSERT_TRUE(advEnableCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS));
ret = SoftBusUpdateAdv(advId, &data, &params);
ASSERT_EQ(ret, SOFTBUS_OK);
btGattCallback->advDataCb(btInnerAdvId, SOFTBUS_BT_STATUS_SUCCESS);
ASSERT_TRUE(advDataCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS));
ASSERT_EQ(SoftBusUpdateAdv(advId, nullptr, &params), SOFTBUS_INVALID_PARAM);
ASSERT_EQ(SoftBusUpdateAdv(advId, &data, nullptr), SOFTBUS_INVALID_PARAM);
ASSERT_EQ(SoftBusUpdateAdv(advId, &data, &params), SOFTBUS_OK);
ASSERT_TRUE(advEnableCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS));
ASSERT_TRUE(advDisableCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS));
// 模拟蓝牙广播成功回调, 广播成功会被再次回调, adapter状态才能恢复正常
btGattCallback->advEnableCb(btInnerAdvId, SOFTBUS_BT_STATUS_SUCCESS);
ASSERT_TRUE(advEnableCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS));
ret = SoftBusStopAdv(advId);
ASSERT_EQ(ret, SOFTBUS_OK);
btGattCallback->advUpdateCb(btInnerAdvId, SOFTBUS_BT_STATUS_SUCCESS);
ASSERT_TRUE(advUpdateCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS));
btGattCallback->securityRespondCb(nullptr);
btGattCallback->scanParamSetCb(btInnerAdvId, SOFTBUS_BT_STATUS_SUCCESS);
ASSERT_EQ(SoftBusStopAdv(advId), SOFTBUS_OK);
ASSERT_TRUE(advDisableCtx.Expect(advId, SOFTBUS_BT_STATUS_SUCCESS));
ret = SoftBusReleaseAdvChannel(advId);
ASSERT_EQ(ret, SOFTBUS_OK);
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)
{
ASSERT_EQ(SoftBusGetAdvChannel(nullptr), SOFTBUS_INVALID_PARAM);
MockBluetooth mocker;
EXPECT_CALL(mocker, BleGattRegisterCallbacks).Times(AtMost(1)).WillOnce(ActionBleGattRegisterCallbacks);
int advIds[ADV_MAX_NUM];
for (size_t i = 0; i < ADV_MAX_NUM; i++) {
advIds[i] = SoftBusGetAdvChannel(GetStubAdvCallback());
ASSERT_NE(advIds[i], SOFTBUS_ERR);
}
ASSERT_EQ(SoftBusGetAdvChannel(GetStubAdvCallback()), 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)

View File

@ -25,6 +25,7 @@
using namespace testing::ext;
using ::testing::_;
using ::testing::AtLeast;
using ::testing::AtMost;
using ::testing::Return;
@ -132,53 +133,148 @@ static testing::AssertionResult PrepareBtStateListener(MockBluetooth &mocker, in
}
/**
* @tc.name: AdapterBtCommonTest_SoftBusAddBtStateListener
* @tc.desc: test set bt name
* @tc.name: AdapterBtCommonTest_StateChangeCallback
* @tc.desc: test bt state listener callback stateChangeCallback
* @tc.type: FUNC
* @tc.require: NONE
*/
HWTEST(AdapterBtCommonTest, SoftBusAddBtStateListener, TestSize.Level3)
HWTEST(AdapterBtCommonTest, StateChangeCallback, TestSize.Level3)
{
int listenerId = -1;
MockBluetooth mocker;
auto prepareResult = PrepareBtStateListener(mocker, &listenerId);
ASSERT_TRUE(prepareResult);
g_btGapCallback->stateChangeCallback(OHOS_BT_TRANSPORT_BR_EDR, OHOS_GAP_STATE_TURNING_ON);
auto btStateResult = g_btStateChangedCtx.Expect(listenerId, SOFTBUS_BR_STATE_TURNING_ON);
EXPECT_TRUE(btStateResult);
g_btGapCallback->stateChangeCallback(OHOS_BT_TRANSPORT_LE, OHOS_GAP_STATE_TURNING_ON);
btStateResult = g_btStateChangedCtx.Expect(listenerId, SOFTBUS_BT_STATE_TURNING_ON);
EXPECT_TRUE(btStateResult);
g_btGapCallback->stateChangeCallback(OHOS_BT_TRANSPORT_BR_EDR, OHOS_GAP_STATE_TURN_ON);
btStateResult = g_btStateChangedCtx.Expect(listenerId, SOFTBUS_BR_STATE_TURN_ON);
EXPECT_TRUE(btStateResult);
g_btGapCallback->stateChangeCallback(OHOS_BT_TRANSPORT_LE, OHOS_GAP_STATE_TURN_ON);
btStateResult = g_btStateChangedCtx.Expect(listenerId, SOFTBUS_BT_STATE_TURN_ON);
EXPECT_TRUE(btStateResult);
g_btGapCallback->stateChangeCallback(OHOS_BT_TRANSPORT_BR_EDR, OHOS_GAP_STATE_TURNING_OFF);
btStateResult = g_btStateChangedCtx.Expect(listenerId, SOFTBUS_BR_STATE_TURNING_OFF);
EXPECT_TRUE(btStateResult);
g_btGapCallback->stateChangeCallback(OHOS_BT_TRANSPORT_LE, OHOS_GAP_STATE_TURNING_OFF);
btStateResult = g_btStateChangedCtx.Expect(listenerId, SOFTBUS_BT_STATE_TURNING_OFF);
EXPECT_TRUE(btStateResult);
g_btGapCallback->stateChangeCallback(OHOS_BT_TRANSPORT_BR_EDR, OHOS_GAP_STATE_TURN_OFF);
btStateResult = g_btStateChangedCtx.Expect(listenerId, SOFTBUS_BR_STATE_TURN_OFF);
EXPECT_TRUE(btStateResult);
g_btGapCallback->stateChangeCallback(OHOS_BT_TRANSPORT_LE, OHOS_GAP_STATE_TURN_OFF);
btStateResult = g_btStateChangedCtx.Expect(listenerId, SOFTBUS_BT_STATE_TURN_OFF);
EXPECT_TRUE(btStateResult);
// invalid status
g_btGapCallback->stateChangeCallback(OHOS_BT_TRANSPORT_BR_EDR, -1);
btStateResult = g_btStateChangedCtx.Expect(listenerId, -1);
EXPECT_TRUE(btStateResult);
EXPECT_EQ(SoftBusRemoveBtStateListener(listenerId), SOFTBUS_OK);
}
/**
* @tc.name: AdapterBtCommonTest_StateChangeCallback
* @tc.desc: test bt state listener callback aclStateChangedCallbak
* @tc.type: FUNC
* @tc.require: NONE
*/
HWTEST(AdapterBtCommonTest, AclStateChangedCallbak, TestSize.Level3)
{
int listenerId = -1;
MockBluetooth mocker;
auto prepareResult = PrepareBtStateListener(mocker, &listenerId);
ASSERT_TRUE(prepareResult);
g_btGapCallback->aclStateChangedCallbak(nullptr, (GapAclState)-1, 0);
BdAddr bdAddr = {
.addr = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
};
g_btGapCallback->aclStateChangedCallbak(&bdAddr, OHOS_GAP_ACL_STATE_CONNECTED, 0);
SoftBusBtAddr addr = {
.addr = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
};
g_btGapCallback->aclStateChangedCallbak(&bdAddr, OHOS_GAP_ACL_STATE_CONNECTED, 0);
auto aclStateResult = g_btAclStateChangedCtx.Expect(listenerId, &addr, SOFTBUS_ACL_STATE_CONNECTED);
EXPECT_TRUE(aclStateResult);
g_btGapCallback->aclStateChangedCallbak(&bdAddr, OHOS_GAP_ACL_STATE_DISCONNECTED, 0);
aclStateResult = g_btAclStateChangedCtx.Expect(listenerId, &addr, SOFTBUS_ACL_STATE_DISCONNECTED);
EXPECT_TRUE(aclStateResult);
g_btGapCallback->aclStateChangedCallbak(&bdAddr, OHOS_GAP_ACL_STATE_LE_CONNECTED, 0);
aclStateResult = g_btAclStateChangedCtx.Expect(listenerId, &addr, SOFTBUS_ACL_STATE_LE_CONNECTED);
EXPECT_TRUE(aclStateResult);
g_btGapCallback->aclStateChangedCallbak(&bdAddr, OHOS_GAP_ACL_STATE_LE_DISCONNECTED, 0);
aclStateResult = g_btAclStateChangedCtx.Expect(listenerId, &addr, SOFTBUS_ACL_STATE_LE_DISCONNECTED);
EXPECT_TRUE(aclStateResult);
// invalid GapAclState
g_btGapCallback->aclStateChangedCallbak(&bdAddr, (GapAclState)-1, 0);
aclStateResult = g_btAclStateChangedCtx.Expect(listenerId, &addr, -1);
EXPECT_TRUE(aclStateResult);
EXPECT_EQ(SoftBusRemoveBtStateListener(listenerId), SOFTBUS_OK);
}
/**
* @tc.name: AdapterBtCommonTest_BluetoothPair
* @tc.desc: test br pair
* @tc.desc: test bt state listener callback pairRequestedCallback
* @tc.type: FUNC
* @tc.require: NONE
*/
HWTEST(AdapterBtCommonTest, BluetoothPair, TestSize.Level3)
HWTEST(AdapterBtCommonTest, PairRequestedCallback, TestSize.Level3)
{
BdAddr bdAddr = {
.addr = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
};
MockBluetooth mocker;
EXPECT_CALL(mocker, PairRequestReply(&bdAddr, OHOS_BT_TRANSPORT_LE, true)).Times(1).WillOnce(Return(true));
EXPECT_CALL(mocker, SetDevicePairingConfirmation(&bdAddr, OHOS_BT_TRANSPORT_LE, true))
.Times(1)
EXPECT_CALL(mocker, PairRequestReply(&bdAddr, OHOS_BT_TRANSPORT_LE, true))
.Times(2)
.WillOnce(Return(false))
.WillOnce(Return(true));
int listenerId = -1;
auto prepareResult = PrepareBtStateListener(mocker, &listenerId);
ASSERT_TRUE(prepareResult);
g_btGapCallback->pairRequestedCallback(nullptr, OHOS_BT_TRANSPORT_INVALID);
g_btGapCallback->pairRequestedCallback(&bdAddr, OHOS_BT_TRANSPORT_LE);
g_btGapCallback->pairRequestedCallback(&bdAddr, OHOS_BT_TRANSPORT_LE);
EXPECT_EQ(SoftBusRemoveBtStateListener(listenerId), SOFTBUS_OK);
}
/**
* @tc.name: AdapterBtCommonTest_PairConfiremedCallback
* @tc.desc: test bt state listener callback pairConfiremedCallback
* @tc.type: FUNC
* @tc.require: NONE
*/
HWTEST(AdapterBtCommonTest, PairConfiremedCallback, TestSize.Level3)
{
BdAddr bdAddr = {
.addr = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
};
MockBluetooth mocker;
EXPECT_CALL(mocker, SetDevicePairingConfirmation(&bdAddr, OHOS_BT_TRANSPORT_LE, true))
.Times(AtLeast(2))
.WillOnce(Return(false))
.WillRepeatedly(Return(true));
int listenerId = -1;
auto prepareResult = PrepareBtStateListener(mocker, &listenerId);
ASSERT_TRUE(prepareResult);
g_btGapCallback->pairConfiremedCallback(nullptr, OHOS_BT_TRANSPORT_INVALID, 0, 0);
g_btGapCallback->pairConfiremedCallback(&bdAddr, OHOS_BT_TRANSPORT_LE, 0, 0);
g_btGapCallback->pairConfiremedCallback(&bdAddr, OHOS_BT_TRANSPORT_LE, 0, 0);
EXPECT_EQ(SoftBusRemoveBtStateListener(listenerId), SOFTBUS_OK);
}