From cbd73dfc5fb330f926bb1f0592a5f83f43edb510 Mon Sep 17 00:00:00 2001 From: zhusiyuan Date: Sun, 5 Nov 2023 21:46:46 +0800 Subject: [PATCH] Feat: Support BLE Approach Discovery & Publish Signed-off-by: zhusiyuan --- .../common/softbus_adapter_bt_common.c | 6 +- .../include/softbus_adapter_ble_gatt.h | 6 +- core/common/include/softbus_json_utils.h | 6 +- core/common/json_utils/softbus_json_utils.c | 23 +- .../approach_ble/include/disc_approach_ble.h | 13 +- .../src/disc_approach_ble_virtual.c | 97 ++++++- .../ble/dispatcher/src/disc_ble_dispatcher.c | 17 +- .../softbus_ble/include/disc_ble_constant.h | 8 - .../ble/softbus_ble/include/disc_ble_utils.h | 13 +- core/discovery/ble/softbus_ble/src/disc_ble.c | 245 ++++++++---------- .../ble/softbus_ble/src/disc_ble_utils.c | 60 +---- .../kits/common_socket/softbus_common.h | 7 +- 12 files changed, 256 insertions(+), 245 deletions(-) diff --git a/adapter/common/net/bluetooth/common/softbus_adapter_bt_common.c b/adapter/common/net/bluetooth/common/softbus_adapter_bt_common.c index f68ee16fe9..45e2ee1dba 100644 --- a/adapter/common/net/bluetooth/common/softbus_adapter_bt_common.c +++ b/adapter/common/net/bluetooth/common/softbus_adapter_bt_common.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 @@ -26,7 +26,7 @@ #include "softbus_errcode.h" #include "softbus_log_old.h" -#define STATE_LISTENER_MAX_NUM 8 +#define STATE_LISTENER_MAX_NUM 9 #define BR_STATE_CB_TRANSPORT 1 typedef struct { @@ -253,4 +253,4 @@ void SoftBusBtInit(void) SoftBusOnBtSateChanged(SOFTBUS_BT_STATE_TURN_ON); SoftBusOnBtSateChanged(SOFTBUS_BR_STATE_TURN_ON); } -} \ No newline at end of file +} diff --git a/adapter/common/net/bluetooth/include/softbus_adapter_ble_gatt.h b/adapter/common/net/bluetooth/include/softbus_adapter_ble_gatt.h index 770ad773a6..0a2c9ef9de 100644 --- a/adapter/common/net/bluetooth/include/softbus_adapter_ble_gatt.h +++ b/adapter/common/net/bluetooth/include/softbus_adapter_ble_gatt.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 @@ -26,7 +26,7 @@ extern "C" { // max adv and scan limit #define ADV_MAX_NUM 9 -#define SCAN_MAX_NUM 8 +#define SCAN_MAX_NUM 9 // Bluetooth scan duty cycle, unit: ms #define SOFTBUS_BLE_SCAN_INTERVAL_P2 3000 @@ -221,4 +221,4 @@ int32_t SoftBusSetLpDeviceParam(int duration, int maxExtAdvEvents, int window, } #endif /* __cplusplus */ #endif /* __cplusplus */ -#endif \ No newline at end of file +#endif diff --git a/core/common/include/softbus_json_utils.h b/core/common/include/softbus_json_utils.h index f0d6adeae7..6cdd602d99 100644 --- a/core/common/include/softbus_json_utils.h +++ b/core/common/include/softbus_json_utils.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 @@ -48,6 +48,8 @@ bool GetJsonObjectBoolItem(const cJSON *json, const char * const string, bool *t bool AddStringToJsonObject(cJSON *json, const char * const string, const char *value); +bool AddStringArrayToJsonObject(cJSON *json, const char * const string, const char * const *strings, int32_t count); + bool AddNumber16ToJsonObject(cJSON *json, const char * const string, uint16_t num); bool AddNumberToJsonObject(cJSON *json, const char * const string, int num); @@ -62,4 +64,4 @@ bool GetJsonObjectInt32Item(const cJSON *json, const char * const string, int32_ } #endif /* __cplusplus */ #endif /* __cplusplus */ -#endif /* SOFTBUS_JSON_UTILS_H */ \ No newline at end of file +#endif /* SOFTBUS_JSON_UTILS_H */ diff --git a/core/common/json_utils/softbus_json_utils.c b/core/common/json_utils/softbus_json_utils.c index 4f6e9def09..14cdeef43a 100644 --- a/core/common/json_utils/softbus_json_utils.c +++ b/core/common/json_utils/softbus_json_utils.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 @@ -196,6 +196,25 @@ bool AddStringToJsonObject(cJSON *json, const char * const string, const char *v return true; } +bool AddStringArrayToJsonObject(cJSON *json, const char * const string, const char * const *strings, int32_t count) +{ + COMM_CHECK_AND_RETURN_RET_LOG(json != NULL && string != NULL && strings != NULL, false, "param is null"); + COMM_CHECK_AND_RETURN_RET_LOG(count > 0, false, "count <= 0"); + + cJSON *item = cJSON_CreateStringArray(strings, count); + if (item == NULL) { + MLOGE("Cannot create cJSON string array object [%s]", string); + return false; + } + + if (!cJSON_AddItemToObject(json, string, item)) { + MLOGE("Cannot add string array object to json [%s]", string); + cJSON_Delete(item); + return false; + } + return true; +} + bool AddNumber16ToJsonObject(cJSON *json, const char * const string, uint16_t num) { if (json == NULL || string == NULL) { @@ -262,4 +281,4 @@ bool AddBoolToJsonObject(cJSON *json, const char * const string, bool value) return false; } return true; -} \ No newline at end of file +} diff --git a/core/discovery/ble/approach_ble/include/disc_approach_ble.h b/core/discovery/ble/approach_ble/include/disc_approach_ble.h index 54612aac57..08f18d9632 100644 --- a/core/discovery/ble/approach_ble/include/disc_approach_ble.h +++ b/core/discovery/ble/approach_ble/include/disc_approach_ble.h @@ -16,20 +16,17 @@ #ifndef DISC_APPROACH_BLE_H #define DISC_APPROACH_BLE_H -#include "disc_manager.h" -#include "softbus_adapter_ble_gatt.h" +#include "disc_ble_dispatcher.h" #ifdef __cplusplus -#if __cplusplus extern "C" { -#endif -#endif +#endif /* __cplusplus */ -void ProcessApproachPacket(const SoftBusBleScanResult *scanResultData, DiscInnerCallback *callback); +DiscoveryBleDispatcherInterface *DiscApproachBleInit(DiscInnerCallback *discInnerCb); +void DiscApproachBleDeinit(void); #ifdef __cplusplus -#if __cplusplus } #endif /* __cplusplus */ -#endif /* __cplusplus */ + #endif /* DISC_APPROACH_BLE_H */ \ No newline at end of file diff --git a/core/discovery/ble/approach_ble/src/disc_approach_ble_virtual.c b/core/discovery/ble/approach_ble/src/disc_approach_ble_virtual.c index 067ca7987d..01572b1f22 100644 --- a/core/discovery/ble/approach_ble/src/disc_approach_ble_virtual.c +++ b/core/discovery/ble/approach_ble/src/disc_approach_ble_virtual.c @@ -15,8 +15,97 @@ #include "disc_approach_ble.h" -void ProcessApproachPacket(const SoftBusBleScanResult *scanResultData, DiscInnerCallback *callback) +#include "disc_manager.h" +#include "softbus_errcode.h" + +static int32_t ApproachBleStartActivePublish(const PublishOption *option) +{ + (void)option; + return SOFTBUS_NOT_IMPLEMENT; +} + +static int32_t ApproachBleStartPassivePublish(const PublishOption *option) +{ + (void)option; + return SOFTBUS_NOT_IMPLEMENT; +} + +static int32_t ApproachBleStopActivePublish(const PublishOption *option) +{ + (void)option; + return SOFTBUS_NOT_IMPLEMENT; +} + +static int32_t ApproachBleStopPassivePublish(const PublishOption *option) +{ + (void)option; + return SOFTBUS_NOT_IMPLEMENT; +} + +static int32_t ApproachBleStartActiveDiscovery(const SubscribeOption *option) +{ + (void)option; + return SOFTBUS_NOT_IMPLEMENT; +} + +static int32_t ApproachBleStartPassiveDiscovery(const SubscribeOption *option) +{ + (void)option; + return SOFTBUS_NOT_IMPLEMENT; +} + +static int32_t ApproachBleStopPassiveDiscovery(const SubscribeOption *option) +{ + (void)option; + return SOFTBUS_NOT_IMPLEMENT; +} + +static int32_t ApproachBleStopActiveDiscovery(const SubscribeOption *option) +{ + (void)option; + return SOFTBUS_NOT_IMPLEMENT; +} + +static void ApproachBleLinkStatusChanged(LinkStatus status) +{ + (void)status; +} + +static void ApproachBleUpdateLocalDeviceInfo(InfoTypeChanged type) +{ + (void)type; +} + +static bool ApproachBleIsConcern(uint32_t capability) +{ + (void)capability; + return false; +} + +static DiscoveryFuncInterface g_discApproachFuncInterface = { + .Publish = ApproachBleStartActivePublish, + .StartScan = ApproachBleStartPassivePublish, + .Unpublish = ApproachBleStopActivePublish, + .StopScan = ApproachBleStopPassivePublish, + .StartAdvertise = ApproachBleStartActiveDiscovery, + .Subscribe = ApproachBleStartPassiveDiscovery, + .Unsubscribe = ApproachBleStopPassiveDiscovery, + .StopAdvertise = ApproachBleStopActiveDiscovery, + .LinkStatusChanged = ApproachBleLinkStatusChanged, + .UpdateLocalDeviceInfo = ApproachBleUpdateLocalDeviceInfo +}; + +static DiscoveryBleDispatcherInterface g_approachBleInterface = { + .IsConcern = ApproachBleIsConcern, + .mediumInterface = &g_discApproachFuncInterface, +}; + +DiscoveryBleDispatcherInterface *DiscApproachBleInit(DiscInnerCallback *discInnerCb) +{ + (void)discInnerCb; + return &g_approachBleInterface; +} + +void DiscApproachBleDeinit(void) { - (void)scanResultData; - (void)callback; -} \ No newline at end of file +} diff --git a/core/discovery/ble/dispatcher/src/disc_ble_dispatcher.c b/core/discovery/ble/dispatcher/src/disc_ble_dispatcher.c index 32fda49167..3ea2e115ad 100644 --- a/core/discovery/ble/dispatcher/src/disc_ble_dispatcher.c +++ b/core/discovery/ble/dispatcher/src/disc_ble_dispatcher.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -14,14 +14,15 @@ */ #include "disc_ble_dispatcher.h" -#include "disc_manager.h" +#include "disc_approach_ble.h" #include "disc_ble.h" +#include "disc_manager.h" #include "disc_share_ble.h" #include "softbus_def.h" #include "softbus_errcode.h" #include "softbus_log_old.h" -#define DISPATCHER_SIZE 2 +#define DISPATCHER_SIZE 3 static DiscoveryBleDispatcherInterface *g_dispatchers[DISPATCHER_SIZE]; static uint32_t g_dispatcherSize = 0; @@ -176,6 +177,13 @@ DiscoveryFuncInterface *DiscBleInit(DiscInnerCallback *discInnerCb) } g_dispatchers[g_dispatcherSize++] = shareInterface; + DiscoveryBleDispatcherInterface *approachInterface = DiscApproachBleInit(discInnerCb); + if (approachInterface == NULL) { + DLOGE("DiscShareBleInit err"); + return NULL; + } + g_dispatchers[g_dispatcherSize++] = approachInterface; + return &g_discBleFrameFuncInterface; } @@ -197,4 +205,5 @@ void DiscBleDeinit(void) g_dispatcherSize = 0; DiscSoftBusBleDeinit(); DiscShareBleDeinit(); -} \ No newline at end of file + DiscApproachBleDeinit(); +} diff --git a/core/discovery/ble/softbus_ble/include/disc_ble_constant.h b/core/discovery/ble/softbus_ble/include/disc_ble_constant.h index 799359f084..5bada0dd9c 100644 --- a/core/discovery/ble/softbus_ble/include/disc_ble_constant.h +++ b/core/discovery/ble/softbus_ble/include/disc_ble_constant.h @@ -20,7 +20,6 @@ #define MAX_CAP_NUM (CAPABILITY_NUM * INT32_MAX_BIT_NUM) #define BLE_SCAN_FILTER_LEN 7 -#define MAX_BLE_FILTER_SIZE 2 #define CUST_DATA_MAX_LEN 14 #define SOFTBUS_BLE_CLIENT_ID 0x1 @@ -41,9 +40,6 @@ #define UUID_LEN 2 #define ADV_HEAD_LEN 7 #define RSP_HEAD_LEN 4 -#define POS_UUID_NO_FLAG 2 -#define ADV_HEAD_LEN_NO_FLAG 4 -#define APPROACH_RSP_HEAD_LEN 1 #define FLAG_BYTE_LEN 0x2 #define FLAG_AD_TYPE 0x1 @@ -72,7 +68,6 @@ #define POS_CAPABLITY 5 #define POS_CAPABLITY_EXTENSION 6 #define POS_TLV 7 -#define POS_TLV_APPROACH 3 #define SHORT_USER_ID_HASH_LEN 2 #define SHORT_DEVICE_ID_HASH_LENGTH 8 @@ -87,7 +82,4 @@ #define MAX_BROADCAST_DATA_LEN 31 #define SCAN_RSP_HEADER_LEN 4 -#define AD_TYPE_LOCAL_NAME_SHORT 0x08 -#define AD_TYPE_LOCAL_NAME_CMPL 0x09 - #endif diff --git a/core/discovery/ble/softbus_ble/include/disc_ble_utils.h b/core/discovery/ble/softbus_ble/include/disc_ble_utils.h index 39b8b2df7d..07cdaa061f 100644 --- a/core/discovery/ble/softbus_ble/include/disc_ble_utils.h +++ b/core/discovery/ble/softbus_ble/include/disc_ble_utils.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 @@ -52,11 +52,6 @@ typedef struct { int8_t power; } DeviceWrapper; -typedef struct { - uint8_t version; - uint8_t business; -} DiscBleScanFilter; - bool CheckBitMapEmpty(uint32_t capBitMapNum, const uint32_t *capBitMap); bool CheckCapBitMapExist(uint32_t capBitMapNum, const uint32_t *capBitMap, uint32_t pos); void SetCapBitMapPos(uint32_t capBitMapNum, uint32_t *capBitMap, uint32_t pos); @@ -70,13 +65,9 @@ int32_t DiscBleGetShortUserIdHash(unsigned char *hashStr, uint32_t len); int32_t AssembleTLV(BroadcastData *broadcastData, unsigned char dataType, const void *data, uint32_t dataLen); int32_t GetDeviceInfoFromDisAdvData(DeviceWrapper *info, const unsigned char *data, uint32_t dataLen); -int32_t ConvertBleAddr(DeviceInfo *foundInfo); -int32_t RangeDevice(DeviceInfo *foundInfo, char rssi, int8_t power); -bool CheckAdvFlagExist(const uint8_t *data, uint32_t len); - #ifdef __cplusplus #if __cplusplus } #endif /* __cplusplus */ #endif /* __cplusplus */ -#endif \ No newline at end of file +#endif diff --git a/core/discovery/ble/softbus_ble/src/disc_ble.c b/core/discovery/ble/softbus_ble/src/disc_ble.c index cc884d0032..fc5542b5f6 100644 --- a/core/discovery/ble/softbus_ble/src/disc_ble.c +++ b/core/discovery/ble/softbus_ble/src/disc_ble.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 @@ -22,7 +22,6 @@ #include "disc_ble_constant.h" #include "disc_ble_utils.h" #include "disc_manager.h" -#include "disc_approach_ble.h" #include "discovery_service.h" #include "lnn_device_info.h" #include "lnn_ohos_account.h" @@ -61,8 +60,6 @@ // Defination of boardcast #define BLE_VERSION 4 -#define APPROACH_VERSION 1 -#define APPROACH_BUSINESS 0x1 #define DISTRIBUTE_BUSINESS 0x5 #define BYTE_MASK 0xFF #define DEVICE_NAME_MAX_LEN 15 @@ -169,17 +166,11 @@ static DiscBleListener g_bleListener = { .scanListenerId = -1 }; -static const DiscBleScanFilter g_bleScanFilters[MAX_BLE_FILTER_SIZE] = { - { BLE_VERSION, DISTRIBUTE_BUSINESS }, - { APPROACH_VERSION, APPROACH_BUSINESS } -}; - //g_conncernCapabilityMask support capability of this ble discovery static uint32_t g_concernCapabilityMask = 1 << CASTPLUS_CAPABILITY_BITMAP | 1 << DVKIT_CAPABILITY_BITMAP | - 1 << OSD_CAPABILITY_BITMAP | - 1 << APPROACH_CAPABILITY_BITMAP; + 1 << OSD_CAPABILITY_BITMAP; static const int g_bleTransCapabilityMap[CAPABILITY_MAX_BITNUM] = { -1, @@ -188,7 +179,7 @@ static const int g_bleTransCapabilityMap[CAPABILITY_MAX_BITNUM] = { -1, OSD_CAPABILITY_BITMAP, -1, - APPROACH_CAPABILITY_BITMAP, + -1, -1, -1, -1, @@ -317,47 +308,30 @@ static bool CheckScanner(void) return scanCapBit != 0; } -static inline bool IsDistributedBusiness(const uint8_t *data) +static int32_t ScanFilter(const SoftBusBleScanResult *scanResultData) { - return data[POS_BUSINESS + ADV_HEAD_LEN_NO_FLAG] == DISTRIBUTE_BUSINESS; -} - -static inline bool IsApproachBusiness(const uint8_t *data) -{ - return data[POS_BUSINESS + ADV_HEAD_LEN_NO_FLAG] == APPROACH_BUSINESS; -} - -static int32_t ScanFilter(const uint8_t *advData, uint32_t advLen) -{ - DISC_CHECK_AND_RETURN_RET_LOG(advData[POS_UUID_NO_FLAG] == (uint8_t)(BLE_UUID & BYTE_MASK), SOFTBUS_ERR, - "uuid low byte[%hhu] is invalid", advData[POS_UUID_NO_FLAG]); - DISC_CHECK_AND_RETURN_RET_LOG(advData[POS_UUID_NO_FLAG + 1] == - (uint8_t)((BLE_UUID >> BYTE_SHIFT_BIT) & BYTE_MASK), - SOFTBUS_ERR, "uuid high byte[%hhu] is invalid", advData[POS_UUID_NO_FLAG + 1]); - - uint32_t broadcastAdvLen = advData[0]; - if (IsApproachBusiness(advData)) { - DISC_CHECK_AND_RETURN_RET_LOG(advLen >= (POS_TLV_APPROACH + ADV_HEAD_LEN_NO_FLAG), SOFTBUS_ERR, - "advLen[%u] is too short, less than adv header length", advLen); - DISC_CHECK_AND_RETURN_RET_LOG(broadcastAdvLen >= ADV_HEAD_LEN_NO_FLAG, SOFTBUS_ERR, - "broadcastAdvLen[%u] is too short, less than adv header length", broadcastAdvLen); - DISC_CHECK_AND_RETURN_RET_LOG(advLen > broadcastAdvLen, SOFTBUS_ERR, - "advLen[%u] is too short, less than adv packet length", advLen); - DISC_CHECK_AND_RETURN_RET_LOG(advData[POS_VERSION + ADV_HEAD_LEN_NO_FLAG] == APPROACH_VERSION, SOFTBUS_ERR, - "adv version[%hhu] is invalid", advData[POS_VERSION + ADV_HEAD_LEN_NO_FLAG]); - } else { - DISC_CHECK_AND_RETURN_RET_LOG(advLen >= (POS_TLV + ADV_HEAD_LEN_NO_FLAG), SOFTBUS_ERR, - "advLen[%u] is too short, less than adv header length", advLen); - DISC_CHECK_AND_RETURN_RET_LOG(broadcastAdvLen >= (ADV_HEAD_LEN_NO_FLAG + RSP_HEAD_LEN - 1), SOFTBUS_ERR, - "broadcastAdvLen[%u] is too short, less than adv header length", broadcastAdvLen); - DISC_CHECK_AND_RETURN_RET_LOG(advLen > (broadcastAdvLen + 1), SOFTBUS_ERR, - "advLen[%u] is too short, less than adv packet length", advLen); - uint32_t broadcastRspLen = advData[broadcastAdvLen + 1]; - DISC_CHECK_AND_RETURN_RET_LOG(advLen >= (broadcastAdvLen + 1 + broadcastRspLen + 1), - SOFTBUS_ERR, "advLen[%u] is too short, less than adv+rsp packet length", advLen); - DISC_CHECK_AND_RETURN_RET_LOG(advData[POS_VERSION + ADV_HEAD_LEN_NO_FLAG] == BLE_VERSION, SOFTBUS_ERR, - "adv version[%hhu] is invalid", advData[POS_VERSION + ADV_HEAD_LEN_NO_FLAG]); - } + uint32_t advLen = scanResultData->advLen; + uint8_t *advData = scanResultData->advData; + DISC_CHECK_AND_RETURN_RET_LOG(scanResultData->dataStatus == SOFTBUS_BLE_DATA_COMPLETE, SOFTBUS_ERR, + "dataStatus[%u] is invalid", scanResultData->dataStatus); + DISC_CHECK_AND_RETURN_RET_LOG(advLen >= (POS_TLV + ADV_HEAD_LEN), SOFTBUS_ERR, + "advLen[%u] is too short, less than adv header length", advLen); + + uint32_t broadcastAdvLen = advData[POS_PACKET_LENGTH]; + DISC_CHECK_AND_RETURN_RET_LOG(broadcastAdvLen >= (ADV_HEAD_LEN + RSP_HEAD_LEN - 1), SOFTBUS_ERR, + "broadcastAdvLen[%u] is too short, less than adv header length", broadcastAdvLen); + DISC_CHECK_AND_RETURN_RET_LOG(advLen > (POS_PACKET_LENGTH + broadcastAdvLen + 1), SOFTBUS_ERR, + "advLen[%u] is too short, less than adv packet length", advLen); + uint32_t broadcastRspLen = advData[POS_PACKET_LENGTH + broadcastAdvLen + 1]; + DISC_CHECK_AND_RETURN_RET_LOG(advLen >= (POS_PACKET_LENGTH + broadcastAdvLen + 1 + broadcastRspLen + 1), + SOFTBUS_ERR, "advLen[%u] is too short, less than adv+rsp packet length", advLen); + + DISC_CHECK_AND_RETURN_RET_LOG(advData[POS_UUID] == (uint8_t)(BLE_UUID & BYTE_MASK), SOFTBUS_ERR, + "uuid low byte[%hhu] is invalid", advData[POS_UUID]); + DISC_CHECK_AND_RETURN_RET_LOG(advData[POS_UUID + 1] == (uint8_t)((BLE_UUID >> BYTE_SHIFT_BIT) & BYTE_MASK), + SOFTBUS_ERR, "uuid high byte[%hhu] is invalid", advData[POS_UUID + 1]); + DISC_CHECK_AND_RETURN_RET_LOG(advData[POS_VERSION + ADV_HEAD_LEN] == BLE_VERSION, SOFTBUS_ERR, + "adv version[%hhu] is invalid", advData[POS_VERSION + ADV_HEAD_LEN]); if (!CheckScanner()) { DLOGI("no need to scan"); @@ -422,6 +396,55 @@ static bool ProcessHashAccount(DeviceInfo *foundInfo) return false; } +static int32_t ConvertBleAddr(DeviceInfo *foundInfo) +{ + // convert ble bin mac to string mac before report + char bleMac[BT_MAC_LEN] = {0}; + if (ConvertBtMacToStr(bleMac, BT_MAC_LEN, + (uint8_t *)foundInfo->addr[0].info.ble.bleMac, BT_ADDR_LEN) != SOFTBUS_OK) { + DLOGE("convert ble mac to string failed"); + return SOFTBUS_ERR; + } + + if (memset_s(foundInfo->addr[0].info.ble.bleMac, BT_MAC_LEN, 0, BT_MAC_LEN) != EOK) { + DLOGE("memset failed"); + return SOFTBUS_MEM_ERR; + } + + if (memcpy_s(foundInfo->addr[0].info.ble.bleMac, BT_MAC_LEN, bleMac, BT_MAC_LEN) != EOK) { + DLOGE("memcpy failed"); + return SOFTBUS_MEM_ERR; + } + + return SOFTBUS_OK; +} + +static int32_t RangeDevice(DeviceInfo *foundInfo, char rssi, int8_t power) +{ + int32_t range = -1; + if (power != SOFTBUS_ILLEGAL_BLE_POWER) { + SoftBusRangeParam param = { + .rssi = *(signed char *)(&rssi), + .power = power, + .identity = {0} + }; + + if (memcpy_s(param.identity, SOFTBUS_DEV_IDENTITY_LEN, foundInfo->devId, DISC_MAX_DEVICE_ID_LEN) != EOK) { + DLOGE("memcpy failed"); + return SOFTBUS_MEM_ERR; + } + + int ret = SoftBusBleRange(¶m, &range); + if (ret != SOFTBUS_OK) { + DLOGE("range device failed, ret=%d", ret); + range = -1; + // range failed should report device continually + } + } + foundInfo->range = range; + return SOFTBUS_OK; +} + static void ProcessDisNonPacket(const uint8_t *advData, uint32_t advLen, char rssi, DeviceInfo *foundInfo) { @@ -494,32 +517,9 @@ static void ProcessDistributePacket(const SoftBusBleScanResult *scanResultData) } } -static bool IgnoreUnConcernedData(SoftBusBleScanResult *scanResult) +static inline bool IsDistributedBusiness(const uint8_t *data) { - DISC_CHECK_AND_RETURN_RET_LOG(scanResult->advData != NULL, false, "scan result advData is null"); - uint8_t *advData = scanResult->advData; - uint32_t curPos = 0; - while (curPos < scanResult->advLen) { - uint32_t len = advData[curPos++] & BYTE_MASK; - if (len == 0) { - break; - } - uint32_t dataLen = len - 1; - uint8_t fieldType = advData[curPos++] & BYTE_MASK; - switch (fieldType) { - case AD_TYPE: - // fall-through - case RSP_TYPE: - scanResult->advData += curPos - TL_LEN - TL_LEN; - scanResult->advLen -= curPos - TL_LEN - TL_LEN; - return true; - default: - // ignore other type - break; - } - curPos += dataLen; - } - return false; + return data[POS_BUSINESS + ADV_HEAD_LEN] == DISTRIBUTE_BUSINESS; } static void BleScanResultCallback(int listenerId, const SoftBusBleScanResult *scanResultData) @@ -527,41 +527,12 @@ static void BleScanResultCallback(int listenerId, const SoftBusBleScanResult *sc (void)listenerId; DISC_CHECK_AND_RETURN_LOG(scanResultData != NULL, "scan result is null"); DISC_CHECK_AND_RETURN_LOG(scanResultData->advData != NULL, "scan result advData is null"); - DISC_CHECK_AND_RETURN_LOG(scanResultData->dataStatus == SOFTBUS_BLE_DATA_COMPLETE, "dataStatus[%u] is invalid", - scanResultData->dataStatus); + DISC_CHECK_AND_RETURN_LOG(ScanFilter(scanResultData) == SOFTBUS_OK, "scan filter failed"); uint8_t *advData = scanResultData->advData; - uint32_t advLen = scanResultData->advLen; - // check adv flag and skip it - if (CheckAdvFlagExist(advData, advLen)) { - advLen -= (FLAG_BYTE_LEN + TL_LEN); - advData += (FLAG_BYTE_LEN + TL_LEN); - } - DISC_CHECK_AND_RETURN_LOG(advLen > (ADV_HEAD_LEN_NO_FLAG + POS_BUSINESS), - "advLen[%u] is too short, less than adv business header length", advLen); - if (IsDistributedBusiness(advData)) { - if (ScanFilter(advData, advLen) != SOFTBUS_OK) { - DLOGE("scan filter failed"); - return; - } - SignalingMsgPrint("ble rcv", scanResultData->advData, scanResultData->advLen, SOFTBUS_LOG_DISC); + SignalingMsgPrint("ble rcv", advData, scanResultData->advLen, SOFTBUS_LOG_DISC); ProcessDistributePacket(scanResultData); - return; - } - - SoftBusBleScanResult scanResult = *scanResultData; - if (!IgnoreUnConcernedData(&scanResult)) { - DLOGE("ignore unconcerned data failed"); - return; - } - if (IsApproachBusiness(scanResult.advData)) { - DLOGI("Process packet for approach business"); - if (ScanFilter(scanResult.advData, scanResult.advLen) != SOFTBUS_OK) { - DLOGE("scan filter failed"); - return; - } - ProcessApproachPacket(&scanResult, g_discBleInnerCb); } else { DLOGI("ignore other business"); } @@ -1600,45 +1571,41 @@ static int32_t DiscBleLooperInit(void) return SOFTBUS_OK; } -static void DiscFreeBleScanFilter(SoftBusBleScanFilter *filters) +static void DiscFreeBleScanFilter(SoftBusBleScanFilter *filter) { - DISC_CHECK_AND_RETURN_LOG(filters != NULL, "filters is NULL"); - for (int32_t i = 0; i < MAX_BLE_FILTER_SIZE; i++) { - SoftBusFree(filters[i].serviceData); - SoftBusFree(filters[i].serviceDataMask); + if (filter) { + SoftBusFree(filter->serviceData); + SoftBusFree(filter->serviceDataMask); + SoftBusFree(filter); } - SoftBusFree(filters); } static void DiscBleSetScanFilter(int32_t listenerId) { - SoftBusBleScanFilter *filters = (SoftBusBleScanFilter *)SoftBusCalloc(sizeof(SoftBusBleScanFilter) * - MAX_BLE_FILTER_SIZE); - DISC_CHECK_AND_RETURN_LOG(filters != NULL, "calloc disc ble scan filters failed"); - - for (int32_t i = 0; i < MAX_BLE_FILTER_SIZE; i++) { - filters[i].serviceData = (uint8_t *)SoftBusCalloc(BLE_SCAN_FILTER_LEN); - filters[i].serviceDataMask = (uint8_t *)SoftBusCalloc(BLE_SCAN_FILTER_LEN); - if (filters[i].serviceData == NULL || filters[i].serviceDataMask == NULL) { - DLOGE("malloc filters data failed"); - DiscFreeBleScanFilter(filters); - return; - } + SoftBusBleScanFilter *filter = (SoftBusBleScanFilter *)SoftBusCalloc(sizeof(SoftBusBleScanFilter)); + DISC_CHECK_AND_RETURN_LOG(filter != NULL, "malloc filter failed"); - filters[i].serviceDataLength = BLE_SCAN_FILTER_LEN; - filters[i].serviceData[0] = BLE_UUID & BYTE_MASK; - filters[i].serviceData[1] = (BLE_UUID >> BYTE_SHIFT_BIT) & BYTE_MASK; - filters[i].serviceData[UUID_LEN + POS_VERSION] = g_bleScanFilters[i].version; - filters[i].serviceData[UUID_LEN + POS_BUSINESS] = g_bleScanFilters[i].business; - filters[i].serviceDataMask[0] = BYTE_MASK; - filters[i].serviceDataMask[1] = BYTE_MASK; - filters[i].serviceDataMask[UUID_LEN + POS_VERSION] = BYTE_MASK; - filters[i].serviceDataMask[UUID_LEN + POS_BUSINESS] = BYTE_MASK; + filter->serviceData = (uint8_t *)SoftBusCalloc(BLE_SCAN_FILTER_LEN); + filter->serviceDataMask = (uint8_t *)SoftBusCalloc(BLE_SCAN_FILTER_LEN); + if (filter->serviceData == NULL || filter->serviceDataMask == NULL) { + DLOGE("malloc filter data failed"); + DiscFreeBleScanFilter(filter); + return; } - if (SoftBusSetScanFilter(listenerId, filters, MAX_BLE_FILTER_SIZE) != SOFTBUS_OK) { - DLOGE("set scan filters failed"); - DiscFreeBleScanFilter(filters); + filter->serviceDataLength = BLE_SCAN_FILTER_LEN; + filter->serviceData[0] = BLE_UUID & BYTE_MASK; + filter->serviceData[1] = (BLE_UUID >> BYTE_SHIFT_BIT) & BYTE_MASK; + filter->serviceData[UUID_LEN + POS_VERSION] = BLE_VERSION; + filter->serviceData[UUID_LEN + POS_BUSINESS] = DISTRIBUTE_BUSINESS; + filter->serviceDataMask[0] = BYTE_MASK; + filter->serviceDataMask[1] = BYTE_MASK; + filter->serviceDataMask[UUID_LEN + POS_VERSION] = BYTE_MASK; + filter->serviceDataMask[UUID_LEN + POS_BUSINESS] = BYTE_MASK; + + if (SoftBusSetScanFilter(listenerId, filter, 1) != SOFTBUS_OK) { + DLOGE("set scan filter failed"); + DiscFreeBleScanFilter(filter); } } @@ -1819,4 +1786,4 @@ static int32_t RecvMessageInfoDump(int fd) } (void)SoftBusMutexUnlock(&g_recvMessageInfo.lock); return SOFTBUS_OK; -} \ No newline at end of file +} diff --git a/core/discovery/ble/softbus_ble/src/disc_ble_utils.c b/core/discovery/ble/softbus_ble/src/disc_ble_utils.c index c236027d65..d05286dc0a 100644 --- a/core/discovery/ble/softbus_ble/src/disc_ble_utils.c +++ b/core/discovery/ble/softbus_ble/src/disc_ble_utils.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 @@ -24,7 +24,6 @@ #include "securec.h" #include "softbus_adapter_crypto.h" #include "softbus_adapter_mem.h" -#include "softbus_adapter_range.h" #include "softbus_common.h" #include "softbus_def.h" #include "softbus_errcode.h" @@ -42,8 +41,6 @@ #define MAC_BIT_FOUR 4 #define MAC_BIT_FIVE 5 -#define INVALID_RANGE (-1) - bool CheckBitMapEmpty(uint32_t capBitMapNum, const uint32_t *capBitMap) { for (uint32_t i = 0; i < capBitMapNum; i++) { @@ -308,58 +305,3 @@ int32_t GetDeviceInfoFromDisAdvData(DeviceWrapper *device, const uint8_t *data, return ret; } -int32_t ConvertBleAddr(DeviceInfo *foundInfo) -{ - DISC_CHECK_AND_RETURN_RET_LOG(foundInfo != NULL, SOFTBUS_INVALID_PARAM, "invalid foundInfo"); - // convert ble bin mac to string mac before report - char bleMac[BT_MAC_LEN] = {0}; - if (ConvertBtMacToStr(bleMac, BT_MAC_LEN, - (uint8_t *)foundInfo->addr[0].info.ble.bleMac, BT_ADDR_LEN) != SOFTBUS_OK) { - DLOGE("convert ble mac to string failed"); - return SOFTBUS_ERR; - } - if (memset_s(foundInfo->addr[0].info.ble.bleMac, BT_MAC_LEN, 0, BT_MAC_LEN) != EOK) { - DLOGE("memset ble mac failed"); - return SOFTBUS_ERR; - } - if (memcpy_s(foundInfo->addr[0].info.ble.bleMac, BT_MAC_LEN, bleMac, BT_MAC_LEN) != EOK) { - DLOGE("memcopy ble mac failed"); - return SOFTBUS_ERR; - } - return SOFTBUS_OK; -} - -int32_t RangeDevice(DeviceInfo *foundInfo, char rssi, int8_t power) -{ - DISC_CHECK_AND_RETURN_RET_LOG(foundInfo != NULL, SOFTBUS_INVALID_PARAM, "invalid foundInfo"); - if (power == SOFTBUS_ILLEGAL_BLE_POWER) { - foundInfo->range = INVALID_RANGE; - return SOFTBUS_OK; - } - - int32_t range = INVALID_RANGE; - SoftBusRangeParam param = { - .rssi = *(signed char *)(&rssi), - .power = power, - .identity = {0} - }; - (void)memcpy_s(param.identity, SOFTBUS_DEV_IDENTITY_LEN, foundInfo->addr->info.ble.bleMac, BT_MAC_LEN); - int ret = SoftBusBleRange(¶m, &range); - if (ret != SOFTBUS_OK) { - DLOGE("range device failed, ret=%d", ret); - range = INVALID_RANGE; - // range failed should report device continually - } - foundInfo->range = range; - return SOFTBUS_OK; -} - -bool CheckAdvFlagExist(const uint8_t *data, uint32_t len) -{ - DISC_CHECK_AND_RETURN_RET_LOG(data != NULL, SOFTBUS_INVALID_PARAM, "invalid data"); - if (len < FLAG_BYTE_LEN + TL_LEN) { - DLOGE("adv len too short, len=%u", len); - return false; - } - return (data[0] == FLAG_BYTE_LEN && data[1] == FLAG_AD_TYPE); -} \ No newline at end of file diff --git a/interfaces/kits/common_socket/softbus_common.h b/interfaces/kits/common_socket/softbus_common.h index 7621a8fc81..0f55dbcc9e 100644 --- a/interfaces/kits/common_socket/softbus_common.h +++ b/interfaces/kits/common_socket/softbus_common.h @@ -333,8 +333,10 @@ typedef enum { DDMP_CAPABILITY_BITMAP, /** Osd capability */ OSD_CAPABILITY_BITMAP, - /**Share capability */ - SHARE_CAPABILITY_BITMAP + /** Share capability */ + SHARE_CAPABILITY_BITMAP, + /** Approach capability */ + APPROACH_CAPABILITY_BITMAP, } DataBitMap; /** @@ -362,6 +364,7 @@ static const CapabilityMap g_capabilityMap[] = { {DDMP_CAPABILITY_BITMAP, (char *)"ddmpCapability"}, {OSD_CAPABILITY_BITMAP, (char *)"osdCapability"}, {SHARE_CAPABILITY_BITMAP, (char *)"share"}, + {APPROACH_CAPABILITY_BITMAP, (char *)"approach"}, }; /** -- Gitee