From 3a60f89dcef2614383ec4501b606d2344bf917aa Mon Sep 17 00:00:00 2001 From: tianhang Date: Mon, 18 Sep 2023 14:10:38 +0800 Subject: [PATCH] Add support for ble dicovery and classic bluetooth connection --- 0014-add-support-for-classic-bluetooth.patch | 2528 ++++++++++++++++++ distributed-utils.spec | 13 +- 2 files changed, 2539 insertions(+), 2 deletions(-) create mode 100644 0014-add-support-for-classic-bluetooth.patch diff --git a/0014-add-support-for-classic-bluetooth.patch b/0014-add-support-for-classic-bluetooth.patch new file mode 100644 index 0000000..9e1ff84 --- /dev/null +++ b/0014-add-support-for-classic-bluetooth.patch @@ -0,0 +1,2528 @@ +From 7c45229fbbd133c8214cc115031dc11e88c627a6 Mon Sep 17 00:00:00 2001 +From: tianhang +Date: Wed, 13 Sep 2023 15:26:44 +0800 +Subject: [PATCH] add support for classic bluetooth + +--- + adapter/BUILD.gn | 26 + + adapter/common/bluez_adapter/bluez_adapter.c | 662 ++++++++++++++++ + .../common/bluez_adapter/bluez_adapter_spp.c | 217 ++++++ + .../bluez_adapter/include/ohos_bt_def.h | 32 + + .../bluez_adapter/include/ohos_bt_gap.h | 350 +++++++++ + .../bluez_adapter/include/ohos_bt_gatt.h | 725 ++++++++++++++++++ + .../bluez_adapter/include/ohos_bt_spp.h | 131 ++++ + .../bus_center/network/lnn_bt_monitor.c | 3 +- + .../bluetooth/ble/softbus_adapter_ble_gatt.c | 2 +- + .../common/net/bluetooth/net_bluetooth.gni | 6 +- + .../feature_config/standard/config.gni | 4 +- + core/adapter/core_adapter.gni | 1 + + core/connection/ble/conn_ble.gni | 3 + + .../softbus_ble_connection_utils_virtual.c | 22 + + .../ble/src/softbus_ble_connection_virtual.c | 5 - + core/connection/br/src/br_connection.c | 63 +- + core/discovery/ble/disc_ble.gni | 14 + + 17 files changed, 2254 insertions(+), 12 deletions(-) + create mode 100644 adapter/common/bluez_adapter/bluez_adapter.c + create mode 100644 adapter/common/bluez_adapter/bluez_adapter_spp.c + create mode 100644 adapter/common/bluez_adapter/include/ohos_bt_def.h + create mode 100644 adapter/common/bluez_adapter/include/ohos_bt_gap.h + create mode 100644 adapter/common/bluez_adapter/include/ohos_bt_gatt.h + create mode 100644 adapter/common/bluez_adapter/include/ohos_bt_spp.h + create mode 100644 core/connection/ble/src/softbus_ble_connection_utils_virtual.c + +diff --git a/adapter/BUILD.gn b/adapter/BUILD.gn +index 2b7fa0fb8..493bfafe7 100644 +--- a/adapter/BUILD.gn ++++ b/adapter/BUILD.gn +@@ -213,4 +213,30 @@ if (defined(ohos_lite)) { + part_name = "dsoftbus" + subsystem_name = "communication" + } ++ ++ ohos_shared_library("bluez_adapter") { ++ include_dirs = [ ++ "$softbus_adapter_common/bluez_adapter/include", ++ ] ++ sources = [ ++ "$softbus_adapter_common/bluez_adapter/bluez_adapter.c", ++ "$softbus_adapter_common/bluez_adapter/bluez_adapter_spp.c", ++ ] ++ deps = [ ++ "//third_party/bounds_checking_function:libsec_shared", ++ "$dsoftbus_root_path/core/common:softbus_utils", ++ ] ++ libs = [ "bluetooth" ] ++ ++ public_configs = [ ":config_adapter_common" ] ++ if (is_standard_system) { ++ external_deps = [ ++ "hiviewdfx_hilog_native:libhilog", ++ ] ++ } ++ ++ part_name = "dsoftbus" ++ subsystem_name = "communication" ++ } ++ + } +diff --git a/adapter/common/bluez_adapter/bluez_adapter.c b/adapter/common/bluez_adapter/bluez_adapter.c +new file mode 100644 +index 000000000..8524549b8 +--- /dev/null ++++ b/adapter/common/bluez_adapter/bluez_adapter.c +@@ -0,0 +1,662 @@ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "bluetooth/bluetooth.h" ++#include "bluetooth/hci.h" ++#include "bluetooth/hci_lib.h" ++#include "ohos_bt_gap.h" ++#include "ohos_bt_gatt.h" ++ ++#include "common_list.h" ++#include "softbus_log.h" ++ ++#define INSTANCE_MAX 2 ++#define MAX_ADV_DATA_LEN 31 ++#define SCAN_WINDOW 10 ++#define SCAN_INTERVAL 10 ++ ++#define ADV_DATA_MAX_LEN 24 ++#define RESP_DATA_MAX_LEN 26 ++#define BROADCAST_MAX_LEN (ADV_DATA_MAX_LEN + RESP_DATA_MAX_LEN) ++#define HCI_TIMEOUT 10000 ++#define HCI_FILTER_DUP 0x01 ++ ++#define POS_BUSINESS_EXTENSION 9 ++#define POS_BR_MAC 25 ++#define BYTE_NON_TYPE 0x10 ++ ++#define BLUEZ_ERR -1 ++#define BLUEZ_OK 0 ++ ++static int g_instance_arr[INSTANCE_MAX] = {0}; ++ ++static int g_dev_id = -1; ++ ++static StartAdvRawData g_advData[INSTANCE_MAX]; ++ ++static bdaddr_t g_bdaddr[INSTANCE_MAX]; ++ ++static ScanResultCallback g_resultCallback = NULL; ++ ++volatile bool g_isAdvertising = false; ++ ++volatile bool g_isScanning = false; ++ ++static bool g_isRandomAddr = false; ++ ++static int g_scan_dd = -1; ++ ++static int g_adv_dd = -1; ++ ++static pthread_t g_scanTid = -1; ++ ++static pthread_t g_advTid = -1; ++ ++static bool g_hci_reset = false; ++ ++static int softbus_reset_hci_status(int dd); ++ ++typedef struct { ++ ListNode node; ++ uint8_t nodeNum; ++} RecvMessageInfo; ++ ++// 接收到的NON数据消息队列,用于缓存组包 ++static RecvMessageInfo g_nonRcvMsgInfo = {0}; ++ ++typedef struct { ++ ListNode node; ++ char brMac[OHOS_BD_ADDR_LEN]; // 消息来源的随机br mac地址 ++ uint8_t dataLen; ++ char recvData[BROADCAST_MAX_LEN]; ++} RecvMessage; ++ ++#define PRINT_BLUETOOTH_MAC_ADDRESS(macAddress) \ ++ do { \ ++ DLOGD("[BLE_DEBUG] bt mac is: %02X:%02X:%02X:%02X:%02X:%02X\n", (macAddress)[0], (macAddress)[1], \ ++ (macAddress)[2], (macAddress)[3], (macAddress)[4], (macAddress)[5]); \ ++ } while (0) ++ ++// 是否是未满的Non响应广播包 ++static bool IsPartialNonRsp(le_advertising_info *info) ++{ ++ return (info->length == MAX_ADV_DATA_LEN && info->data[POS_BUSINESS_EXTENSION] == BYTE_NON_TYPE && ++ info->data[POS_BR_MAC] == 0x56); ++} ++ ++// 是否是最后一个可以用来拼凑出完整响应广播包的数据包 ++static bool IsLastNonRsp(le_advertising_info *info) ++{ ++ return (info->length > 4 && info->data[1] == 0xff && info->data[2] == 0x7d && info->data[3] == 0x02 && ++ info->data[0] + 1 == info->length); ++} ++ ++static RecvMessage *GetRecvMessage(const char *key) ++{ ++ RecvMessage *msg = NULL; ++ LIST_FOR_EACH_ENTRY(msg, &g_nonRcvMsgInfo.node, RecvMessage, node) ++ { ++ if (memcmp((void *)key, (void *)msg->brMac, 6) == 0) { ++ return msg; ++ } ++ } ++ return NULL; ++} ++ ++static int AddRecvMessage(le_advertising_info *info) ++{ ++ RecvMessage *rcvMsg = (RecvMessage *)malloc(sizeof(RecvMessage)); ++ if (rcvMsg == NULL) { ++ DLOGE("Failed to malloc rcv msg"); ++ return BLUEZ_ERR; ++ } ++ memcpy(rcvMsg->brMac, info->bdaddr.b, 6); ++ memcpy(rcvMsg->recvData, info->data, info->length); ++ rcvMsg->dataLen = info->length; ++ ListTailInsert(&g_nonRcvMsgInfo.node, &(rcvMsg->node)); ++ g_nonRcvMsgInfo.nodeNum++; ++ return BLUEZ_OK; ++} ++ ++static void ClearRecvMessage(void) ++{ ++ ListNode *head = &(g_nonRcvMsgInfo.node); ++ while (!IsListEmpty(head)) { ++ RecvMessage *msg = LIST_ENTRY(head->next, RecvMessage, node); ++ ListDelete(&msg->node); ++ free(msg); ++ } ++ g_nonRcvMsgInfo.nodeNum = 0; ++} ++ ++static int CombineNonRsp(le_advertising_info *info, BtScanResultData *scanResult) ++{ ++ RecvMessage *msg = GetRecvMessage(info->bdaddr.b); ++ if (msg == NULL) { ++ DLOGE("Failed to get recv msg"); ++ return BLUEZ_ERR; ++ } ++ memcpy(&(msg->recvData[msg->dataLen]), info->data, info->length); ++ msg->dataLen += info->length; ++ scanResult->advData = msg->recvData; ++ scanResult->advLen = msg->dataLen; ++ ++ SignalingMsgPrint("combine info:", scanResult->advData, scanResult->advLen, SOFTBUS_LOG_DISC); ++ ++ ListDelete(&msg->node); ++ free(msg); ++ g_nonRcvMsgInfo.nodeNum--; ++ return BLUEZ_OK; ++} ++ ++inline void InitScanRes(BtScanResultData *scanResult) ++{ ++ scanResult->eventType = OHOS_BLE_EVT_LEGACY_CONNECTABLE; ++ scanResult->dataStatus = OHOS_BLE_DATA_COMPLETE; ++ scanResult->addrType = OHOS_BLE_RANDOM_DEVICE_ADDRESS; ++ scanResult->primaryPhy = OHOS_BLE_SCAN_PHY_1M; ++ scanResult->secondaryPhy = OHOS_BLE_SCAN_PHY_1M; ++ scanResult->advSid = 1; ++ scanResult->txPower = 1; ++ scanResult->rssi = 0xE0; ++ scanResult->directAddrType = OHOS_BLE_RANDOM_DEVICE_ADDRESS; ++} ++ ++void scanResultFunc(void *arg) ++{ ++ unsigned char buf[HCI_MAX_EVENT_SIZE]; ++ struct hci_filter nf, of; // new_filter old_filter ++ int len = 0; ++ ++ socklen_t olen = sizeof(of); ++ if (getsockopt(g_scan_dd, SOL_HCI, HCI_FILTER, &of, &olen) < 0) { ++ DLOGE("Could not get socket options"); ++ } ++ ++ hci_filter_clear(&nf); ++ hci_filter_set_ptype(HCI_EVENT_PKT, &nf); ++ hci_filter_set_event(EVT_LE_META_EVENT, &nf); ++ ++ if (setsockopt(g_scan_dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)) < 0) { ++ DLOGE("Could not set socket options"); ++ } ++ ++ evt_le_meta_event *meta = NULL; ++ le_advertising_info *info = NULL; ++ while (g_isScanning) { ++ len = read(g_scan_dd, buf, sizeof(buf)); ++ if (len < 0) { ++ DLOGE("Could not read scan data\n"); ++ continue; ++ } ++ ++ len -= (1 + HCI_EVENT_HDR_SIZE); ++ meta = (void *)(buf + (1 + HCI_EVENT_HDR_SIZE)); ++ if (meta->subevent != EVT_LE_ADVERTISING_REPORT) { ++ continue; ++ } ++ ++ info = (le_advertising_info *)(meta->data + 1); ++ ++ if (IsPartialNonRsp(info)) { ++ AddRecvMessage(info); ++ continue; ++ } ++ ++ BtScanResultData scanResult; ++ InitScanRes(&scanResult); ++ memcpy(scanResult.addr.addr, info->bdaddr.b, OHOS_BD_ADDR_LEN); // 这个扫描的地址是随机地址 ++ ++ if (IsLastNonRsp(info)) { ++ CombineNonRsp(info, &scanResult); ++ } else { ++ scanResult.advData = info->data; // 真正的扫描地址在advData中 ++ scanResult.advLen = info->length; ++ } ++ ++ if (g_resultCallback != NULL && scanResult.advLen != 0) { ++ g_resultCallback(&scanResult); ++ } ++ } ++ hci_close_dev(g_scan_dd); ++} ++ ++int BleStartScanEx(void) ++{ ++ g_dev_id = hci_get_route(NULL); ++ if (g_dev_id < 0) { ++ DLOGE("Could not get device id"); ++ return BLUEZ_ERR; ++ } ++ if (g_scan_dd == -1) { ++ g_scan_dd = hci_open_dev(g_dev_id); ++ if (g_scan_dd < 0) { ++ DLOGE("BleStartScanEx Could not open device %d", g_dev_id); ++ return BLUEZ_ERR; ++ } ++ } ++ ++ if(g_hci_reset == false){ ++ g_hci_reset = true; ++ softbus_reset_hci_status(g_scan_dd); ++ } ++ ++ ListInit(&g_nonRcvMsgInfo.node); ++ if (!g_isScanning) { ++ uint16_t interval = htobs(SCAN_INTERVAL); ++ uint16_t window = htobs(SCAN_WINDOW); ++ hci_le_set_scan_parameters(g_scan_dd, OHOS_BLE_SCAN_TYPE_ACTIVE, interval, window, LE_PUBLIC_ADDRESS, ++ OHOS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, HCI_TIMEOUT); ++ hci_le_set_scan_enable(g_scan_dd, 0x01, HCI_FILTER_DUP, HCI_TIMEOUT); ++ g_isScanning = true; ++ } ++ ++ if (g_scanTid == -1) { ++ int err = pthread_create(&g_scanTid, NULL, (void *)&scanResultFunc, NULL); ++ if (err < 0) { ++ DLOGE("Failed to create scan thread"); ++ hci_close_dev(g_scan_dd); ++ g_scan_dd = -1; ++ return BLUEZ_ERR; ++ } ++ } ++ return BLUEZ_OK; ++} ++ ++static int hci_reset_fun(int dd,int to ){ ++ ++ struct hci_request rq; ++ uint8_t status; ++ memset(&rq, 0, sizeof(rq)); ++ rq.ogf = OGF_HOST_CTL; ++ rq.ocf = OCF_RESET; ++ rq.rparam = &status; ++ rq.rlen = 1; ++ ++ if (hci_send_req(dd, &rq, to) < 0){ ++ DLOGE("Failed to call hci reset"); ++ return -1; ++ } ++ ++ return 0; ++} ++ ++static int hci_set_page_scan(){ ++ int ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); ++ struct hci_dev_req dr; ++ ++ dr.dev_id = 0; ++ dr.dev_opt = SCAN_PAGE; ++ if (ioctl(ctl, HCISETSCAN, (unsigned long) &dr) < 0) { ++ DLOGE("Failed to set hci page scan"); ++ } ++ close(ctl); ++ return 0; ++} ++ ++static int hci_set_event_mask(int dd, int to){ ++ struct hci_request rq; ++ set_event_mask_cp mask_cp; ++ uint8_t status; ++ ++ memset(&mask_cp, 0, sizeof(mask_cp)); ++ uint8_t mask[8] = {0xFF,0xFF,0xFB,0xFF,0x07,0xF8,0xBF,0x3D}; ++ memcpy(mask_cp.mask, mask,sizeof(mask)); ++ ++ memset(&rq, 0, sizeof(rq)); ++ rq.ogf = OGF_HOST_CTL; ++ rq.ocf = OCF_SET_EVENT_MASK; ++ rq.cparam = &mask_cp; ++ rq.clen = SET_EVENT_MASK_CP_SIZE; ++ rq.rparam = &status; ++ rq.rlen = 1; ++ ++ if (hci_send_req(dd, &rq, to) < 0){ ++ DLOGE("Failed to set hci event mask"); ++ return -1; ++ } ++ ++ return 0; ++} ++ ++static int hci_le_set_event_mask(int dd, int to){ ++ struct hci_request rq; ++ le_set_event_mask_cp mask_cp; ++ uint8_t status; ++ ++ memset(&mask_cp, 0, sizeof(mask_cp)); ++ uint8_t mask[8] = {0xFF,0x0F,0x08,0x00,0x00,0x00,0x00,0x00}; ++ memcpy(mask_cp.mask, mask,sizeof(mask)); ++ ++ memset(&rq, 0, sizeof(rq)); ++ rq.ogf = OGF_LE_CTL; ++ rq.ocf = OCF_LE_SET_EVENT_MASK; ++ rq.cparam = &mask_cp; ++ rq.clen = LE_SET_EVENT_MASK_CP_SIZE; ++ rq.rparam = &status; ++ rq.rlen = 1; ++ ++ if (hci_send_req(dd, &rq, to) < 0){ ++ DLOGE("Failed to set hci le event mask"); ++ return -1; ++ } ++ ++ return 0; ++} ++ ++static int softbus_reset_hci_status(int dd) ++{ ++ hci_reset_fun(dd,HCI_TIMEOUT); ++ hci_set_event_mask(dd,HCI_TIMEOUT); ++ hci_set_page_scan(); ++ hci_le_set_event_mask(dd,HCI_TIMEOUT); ++ ++ return 0; ++} ++ ++static int HciLeSendCmd(int dd, uint16_t ocf, void *cparam, uint16_t clen, void *rparam, uint16_t rlen, int to) ++{ ++ struct hci_request rq = {0}; ++ uint8_t status = 0; ++ ++ memset(&rq, 0, sizeof(rq)); ++ rq.ogf = OGF_LE_CTL; ++ rq.ocf = ocf; ++ rq.cparam = cparam; ++ rq.clen = clen; ++ rq.rparam = &status; ++ rq.rlen = 1; ++ ++ if (hci_send_req(dd, &rq, to) < 0) { ++ DLOGE("hci send req failed, ocf is %u", ocf); ++ return BLUEZ_ERR; ++ } ++ ++ if (status) { ++ DLOGE("hci send req status failed, ocf is %u", ocf); ++ errno = EIO; ++ return BLUEZ_ERR; ++ } ++ ++ return BLUEZ_OK; ++} ++ ++static int HciLeSetAdvParam( ++ int dd, uint16_t min_interval, uint16_t max_interval, uint8_t advtype, uint8_t filter, int to) ++{ ++ le_set_advertising_parameters_cp adv_params_cp; ++ memset(&adv_params_cp, 0, sizeof(adv_params_cp)); ++ adv_params_cp.min_interval = min_interval; ++ adv_params_cp.max_interval = max_interval; ++ adv_params_cp.advtype = advtype; ++ adv_params_cp.own_bdaddr_type = LE_RANDOM_ADDRESS; ++ adv_params_cp.chan_map = 7; ++ ++ return HciLeSendCmd( ++ dd, OCF_LE_SET_ADVERTISING_PARAMETERS, &adv_params_cp, LE_SET_ADVERTISING_PARAMETERS_CP_SIZE, NULL, 0, to); ++} ++ ++static int HciLeSetAdvData(int dd, unsigned char *data, unsigned int dataLen, int to) ++{ ++ le_set_advertising_data_cp adv_data_cp; ++ memset(&adv_data_cp, 0, sizeof(adv_data_cp)); ++ memcpy(adv_data_cp.data, data, dataLen); ++ adv_data_cp.length = dataLen; ++ ++ return HciLeSendCmd(dd, OCF_LE_SET_ADVERTISING_DATA, &adv_data_cp, LE_SET_ADVERTISING_DATA_CP_SIZE, NULL, 0, to); ++} ++ ++static int HciLeSetAdvRspData(int dd, unsigned char *rspData, unsigned int dataLen, int to) ++{ ++ le_set_scan_response_data_cp adv_rspdata_cp; ++ memset(&adv_rspdata_cp, 0, sizeof(adv_rspdata_cp)); ++ memcpy(adv_rspdata_cp.data, rspData, dataLen); ++ adv_rspdata_cp.length = dataLen; ++ ++ return HciLeSendCmd( ++ dd, OCF_LE_SET_SCAN_RESPONSE_DATA, &adv_rspdata_cp, LE_SET_SCAN_RESPONSE_DATA_CP_SIZE, NULL, 0, to); ++} ++ ++static int HciLeSetRandomAddr(int dd, bdaddr_t addr, int to) ++{ ++ le_set_random_address_cp cp; ++ memset(&cp, 0, sizeof(cp)); ++ cp.bdaddr = addr; ++ ++ return HciLeSendCmd(dd, OCF_LE_SET_RANDOM_ADDRESS, &cp, LE_SET_RANDOM_ADDRESS_CP_SIZE, NULL, 0, to); ++} ++ ++int get_instance_index() ++{ ++ for (int i = 0; i < INSTANCE_MAX; ++i) { ++ if (g_instance_arr[i] == 0) { ++ g_instance_arr[i] = 1; ++ return i; ++ } ++ } ++ return -1; ++} ++ ++void advFunc(void *arg) ++{ ++ DLOGD("[BLE_DEBUG] advertise begin."); ++ while (1) { ++ while (g_isAdvertising && g_isScanning) { ++ for (int i = 0; i < INSTANCE_MAX; ++i) { ++ if (g_instance_arr[i] == 1) { ++ int dd = hci_open_dev(g_dev_id); ++ hci_le_set_scan_enable(dd, 0x00, HCI_FILTER_DUP, HCI_TIMEOUT); ++ hci_le_set_advertise_enable(dd, 0x00, HCI_TIMEOUT); ++ ++ HciLeSetAdvData(dd, g_advData[i].advData, g_advData[i].advDataLen, HCI_TIMEOUT); ++ HciLeSetAdvRspData(dd, g_advData[i].rspData, g_advData[i].rspDataLen, HCI_TIMEOUT); ++ HciLeSetRandomAddr(dd, g_bdaddr[i], HCI_TIMEOUT); ++ ++ hci_le_set_advertise_enable(dd, HCI_FILTER_DUP, HCI_TIMEOUT); ++ hci_le_set_scan_enable(dd, 0x01, HCI_FILTER_DUP, HCI_TIMEOUT); ++ hci_close_dev(dd); ++ } ++ sleep(2); ++ } ++ } ++ } ++} ++ ++void initRandomAddr() ++{ ++ srand((unsigned)time(NULL)); ++ for (int i = 0; i < INSTANCE_MAX; ++i) { ++ g_bdaddr[i].b[0] = rand() % 32; ++ for (int j = 1; j < 6; ++j) { ++ g_bdaddr[i].b[j] = rand() % 128; ++ } ++ } ++ g_isRandomAddr = true; ++} ++ ++int BleStartAdvEx(int *advId, const StartAdvRawData rawData, BleAdvParams advParam) ++{ ++ int err, instance; ++ ++ if (g_dev_id < 0) { ++ g_dev_id = hci_get_route(NULL); ++ } ++ ++ ++ if (g_adv_dd == -1) { ++ g_adv_dd = hci_open_dev(g_dev_id); ++ if (g_adv_dd < 0) { ++ perror("[BLE_ERROR BleStartAdvEx]Could not open device"); ++ return BLUEZ_ERR; ++ } ++ } ++ ++ if(g_hci_reset == false){ ++ g_hci_reset = true; ++ softbus_reset_hci_status(g_adv_dd); ++ } ++ ++ if (!g_isAdvertising) { ++ ++ err = HciLeSetAdvParam(g_adv_dd, advParam.minInterval, advParam.maxInterval, advParam.advType, ++ advParam.advFilterPolicy, HCI_TIMEOUT); ++ if (err < 0) { ++ perror("[BLE_ERROR]Set Adv Parameters failed"); ++ hci_close_dev(g_adv_dd); ++ return BLUEZ_ERR; ++ } ++ err = hci_le_set_advertise_enable(g_adv_dd, 0x01, HCI_TIMEOUT); ++ g_isAdvertising = true; ++ } ++ ++ instance = get_instance_index(); ++ if (g_advData[instance].advData == NULL) ++ g_advData[instance].advData = (unsigned char *)malloc(sizeof(unsigned char) * MAX_ADV_DATA_LEN); ++ if (g_advData[instance].rspData == NULL) ++ g_advData[instance].rspData = (unsigned char *)malloc(sizeof(unsigned char) * MAX_ADV_DATA_LEN); ++ ++ memcpy(g_advData[instance].advData, rawData.advData, rawData.advDataLen); ++ memcpy(g_advData[instance].rspData, rawData.rspData, rawData.rspDataLen); ++ g_advData[instance].advDataLen = rawData.advDataLen; ++ g_advData[instance].rspDataLen = rawData.rspDataLen; ++ if (!g_isRandomAddr) { ++ initRandomAddr(); ++ } ++ ++ if (g_advTid == -1) { ++ err = pthread_create(&g_advTid, NULL, (void *)&advFunc, NULL); ++ if (err < 0) { ++ perror("[BLE_ERROR]pthread_create failed"); ++ hci_close_dev(g_adv_dd); ++ return BLUEZ_ERR; ++ } ++ } ++ ++ return BLUEZ_OK; ++} ++ ++int BleGattRegisterCallbacks(BtGattCallbacks *func) ++{ ++ if (func == NULL || func->scanResultCb == NULL) { ++ perror("[BLE_ERROR]RegisterResultCb failed"); ++ return BLUEZ_ERR; ++ } ++ g_resultCallback = func->scanResultCb; ++ return BLUEZ_OK; ++} ++ ++int BleStopAdv(int advId) ++{ ++ int dd, err; ++ ++ dd = hci_open_dev(g_dev_id); ++ DLOGD("device fd is %d", dd); ++ if (dd < 0) { ++ perror("[BleStopAdv BLE_ERROR]Could not open device"); ++ return BLUEZ_ERR; ++ } ++ err = hci_le_set_advertise_enable(dd, 0x00, HCI_TIMEOUT); ++ if (err < 0) { ++ perror("[BLE_ERROR]Disable adv failed"); ++ hci_close_dev(dd); ++ return BLUEZ_ERR; ++ } ++ g_isAdvertising = false; ++ hci_close_dev(dd); ++ DLOGI("[BLE_DEBUG]Ble stop advertise!"); ++ return BLUEZ_OK; ++} ++ ++int BleStopScan(void) ++{ ++ int dd, err; ++ uint8_t filter_dup = 0x01; ++ dd = hci_open_dev(g_dev_id); ++ DLOGD("device fd is %d", dd); ++ if (dd < 0) { ++ perror("[BleStopScan BLE_ERROR]Could not open device"); ++ return BLUEZ_ERR; ++ } ++ err = hci_le_set_scan_enable(dd, 0x00, filter_dup, HCI_TIMEOUT); ++ if (err < 0) { ++ perror("[BLE_ERROR]Disable scan failed"); ++ hci_close_dev(dd); ++ return BLUEZ_ERR; ++ } ++ g_isScanning = false; ++ ClearRecvMessage(); ++ hci_close_dev(dd); ++ DLOGI("[BLE_DEBUG]Ble stop scan!"); ++ return BLUEZ_OK; ++} ++ ++bool GetLocalAddr(unsigned char *mac, unsigned int len) ++{ ++ int dd, err; ++ ++ if (g_dev_id < 0) { ++ g_dev_id = hci_get_route(NULL); ++ } ++ ++ dd = hci_open_dev(g_dev_id); ++ DLOGD("open device:%d fd is %d",g_dev_id, dd); ++ if (dd < 0) { ++ perror("[GetLocalAddr BLE_ERROR]Could not open device"); ++ return false; ++ } ++ bdaddr_t bdaddr; ++ err = hci_read_bd_addr(dd, &bdaddr, HCI_TIMEOUT); ++ if (err < 0) { ++ perror("[BLE_ERROR]Disable scan failed"); ++ hci_close_dev(dd); ++ return false; ++ } ++ // 读出来的mac地址是逆序的 ++ memcpy(mac, bdaddr.b, len); ++ hci_close_dev(dd); ++ return true; ++} ++ ++bool EnableBle(void) ++{ ++ return true; ++} ++ ++bool DisableBle(void) ++{ ++ return true; ++} ++ ++int BleSetAdvData(int advId, const StartAdvRawData data){ ++ return OHOS_BT_STATUS_SUCCESS; ++} ++ ++bool SetLocalName(unsigned char *localName, unsigned char length) ++{ ++ return true; ++} ++ ++bool PairRequestReply(const BdAddr *bdAddr, int transport, bool accept) ++{ ++ return true; ++} ++ ++bool SetDevicePairingConfirmation(const BdAddr *bdAddr, int transport, bool accept) ++{ ++ return true; ++} ++ ++bool IsBleEnabled() ++{ ++ return true; ++} ++ ++int GapRegisterCallbacks(BtGapCallBacks *func) ++{ ++ return BLUEZ_OK; ++} ++ +diff --git a/adapter/common/bluez_adapter/bluez_adapter_spp.c b/adapter/common/bluez_adapter/bluez_adapter_spp.c +new file mode 100644 +index 000000000..b12877c19 +--- /dev/null ++++ b/adapter/common/bluez_adapter/bluez_adapter_spp.c +@@ -0,0 +1,217 @@ ++/* ++ * 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 ++#include ++#include ++#include ++#include ++#include ++ ++#include "softbus_log.h" ++ ++#include "ohos_bt_spp.h" ++bool g_spp_connect = false; ++ ++/** ++ * @brief Creates an server listening socket based on the service record. ++ * ++ * @param socketPara The parameters to create a server socket. ++ * @param name The service's name. ++ * @param len The length of the service's name. ++ * @return Returns a server ID, if create fail return {@link BT_SPP_INVALID_ID}. ++ */ ++int SppServerCreate(BtCreateSocketPara *socketPara, const char *name, unsigned int len) ++{ ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "SERVER SppServerCreate..."); ++ struct sockaddr_rc loc_addr, rem_addr; ++ char buf[1024] = { 0 }; ++ int s, client, bytes_read; ++ socklen_t opt = sizeof(rem_addr); ++ ++ // allocate socket ++ s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); ++ ++ // bind socket to port 1 of the first available ++ // local bluetooth adapter ++ loc_addr.rc_family = AF_BLUETOOTH; ++ loc_addr.rc_bdaddr = *BDADDR_ANY; ++ loc_addr.rc_channel = (uint8_t) 1; ++ bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); ++ ++ // put socket into listening mode ++ listen(s, 1); ++ return s; ++} ++ ++/** ++ * @brief Waits for a remote device to connect to this server socket. ++ * ++ * This method return a client ID indicates a client socket ++ * can be used to read data from and write data to remote device. ++ * ++ * @param serverId The relative ID used to identify the current server socket, obtain the value by calling ++ * {@link SppServerCreate}. ++ * @return Returns a client ID, if accept fail return {@link BT_SPP_INVALID_ID}. ++ */ ++int SppServerAccept(int serverId) ++{ ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "SERVER SppServerAccept..."); ++ struct sockaddr_rc rem_addr; ++ socklen_t opt = sizeof(rem_addr); ++ int client; ++ char buf[1024] = { 0 }; ++ ++ client = accept(serverId, (struct sockaddr *)&rem_addr, &opt); ++ ba2str( &rem_addr.rc_bdaddr, buf ); ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,"SERVER accepted connection from %s",buf); ++ return client; ++} ++ ++/** ++ * @brief Disables an spp server socket and releases related resources. ++ * ++ * @param serverId The relative ID used to identify the current server socket, obtain the value by calling ++ * {@link SppServerCreate}. ++ * @return Returns the operation result status {@link BtStatus}. ++ */ ++int SppServerClose(int serverId) ++{ ++ close(serverId); ++} ++ ++/** ++ * @brief Connects to a remote device over the socket. ++ * ++ * @param socketPara The param to create a client socket and connect to a remote device. ++ * @return Returns a client ID, if connect fail return {@link BT_SPP_INVALID_ID}. ++ */ ++int SppConnect(BtCreateSocketPara *socketPara, const BdAddr *bdAddr) ++{ ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,"CLIENT SppConnect..."); ++ int s,status; ++ struct sockaddr_rc addr = { 0 }; ++ ++ s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); ++ ++ // set the connection parameters (who to connect to) ++ addr.rc_family = AF_BLUETOOTH; ++ addr.rc_channel = (uint8_t) 1; ++ ++ memcpy(addr.rc_bdaddr.b,bdAddr->addr,6); ++ ++ // connect to server ++ status = connect(s, (struct sockaddr *)&addr, sizeof(addr)); ++ if(status == 0){ ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,"CLIENT SppConnect OK"); ++ g_spp_connect = true; ++ return s; ++ }else{ ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,"CLIENT SppConnect ERROR,connect ret:%d",status); ++ close(s); ++ return -1; ++ } ++} ++ ++/** ++ * @brief Disables a connection and releases related resources. ++ * ++ * @param clientId The relative ID used to identify the current client socket. ++ * @return Returns the operation result status {@link BtStatus}. ++ */ ++int SppDisconnect(int clientId) ++{ ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,"CLIENT closed...,fd:%d",clientId); ++ g_spp_connect = false; ++ int ret = close(clientId); ++ if(ret == 0){ ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "CLIENT close fd:%d success",clientId); ++ }else{ ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "CLIENT close fd:%d failed,ret:%d",clientId,ret); ++ } ++ return 0; ++} ++ ++/** ++ * @brief Get the connection status of this socket. ++ * ++ * @param clientId The relative ID used to identify the current client socket. ++ * @return Returns true is connected or false is not connected. ++ */ ++bool IsSppConnected(int clientId) ++{ ++ return g_spp_connect; ++} ++ ++/** ++ * @brief Spp get remote device's address. ++ * ++ * @param clientId The relative ID used to identify the current client socket. ++ * @param remoteAddr Remote device's address, memory allocated by caller. ++ * @return Returns the operation result status {@link BtStatus}. ++ */ ++int SppGetRemoteAddr(int clientId, BdAddr *remoteAddr) ++{ ++ struct sockaddr_rc addr; ++ socklen_t optlen; ++ ++ memset(&addr, 0, sizeof(addr)); ++ optlen = sizeof(addr); ++ ++ if (getpeername(clientId, (struct sockaddr *) &addr, &optlen) < 0) { ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "SppGetRemoteAddr error"); ++ } ++ memcpy(remoteAddr->addr,addr.rc_bdaddr.b,6); ++ ++ char buf[1024] = { 0 }; ++ ba2str( &addr.rc_bdaddr, buf ); ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,"SppGetRemoteAddr: %s", buf); ++ ++ return 1; ++} ++ ++/** ++ * @brief Read data from socket. ++ * ++ * @param clientId The relative ID used to identify the current client socket. ++ * @param buf Indicate the buffer which read in, memory allocated by caller. ++ * @param bufLen Indicate the buffer length. ++ * @return Returns the length greater than 0 as read the actual length. ++ * Returns {@link BT_SPP_READ_SOCKET_CLOSED} if the socket is closed. ++ * Returns {@link BT_SPP_READ_FAILED} if the operation failed. ++ */ ++int SppRead(int clientId, char *buf, const unsigned int bufLen) ++{ ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,"SppRead start"); ++ int len = read(clientId, buf, bufLen); ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,"read buf:%s,len:%d,bufLen:%u",buf,len,bufLen); ++ return len; ++} ++ ++/** ++ * @brief Client write data to socket. ++ * ++ * @param clientId The relative ID used to identify the current client socket. ++ * @param data Indicate the data to be written. ++ * @param len Indicates the length of the data to be written. ++ * @return Returns the actual write length. ++ * Returns {@link BT_SPP_WRITE_FAILED} if the operation failed. ++ */ ++int SppWrite(int clientId, const char *data, const unsigned int len) ++{ ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,"SppWrite start"); ++ int s = write(clientId,data,len); ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,"SppWrite len:%d",s); ++ return s; ++} +diff --git a/adapter/common/bluez_adapter/include/ohos_bt_def.h b/adapter/common/bluez_adapter/include/ohos_bt_def.h +new file mode 100644 +index 000000000..1864e9c80 +--- /dev/null ++++ b/adapter/common/bluez_adapter/include/ohos_bt_def.h +@@ -0,0 +1,32 @@ ++#ifndef OHOS_BT_DEF_H ++#define OHOS_BT_DEF_H ++ ++#define OHOS_BD_ADDR_LEN 6 ++ ++typedef struct { ++ unsigned char addr[OHOS_BD_ADDR_LEN]; ++} BdAddr; ++ ++typedef enum { ++ OHOS_BT_STATUS_SUCCESS = 0x00, ++ OHOS_BT_STATUS_FAIL, ++ OHOS_BT_STATUS_NOT_READY, ++ OHOS_BT_STATUS_NOMEM, ++ OHOS_BT_STATUS_BUSY, ++ OHOS_BT_STATUS_DONE, ++ OHOS_BT_STATUS_UNSUPPORTED, ++ OHOS_BT_STATUS_PARM_INVALID, ++ OHOS_BT_STATUS_UNHANDLED, ++ OHOS_BT_STATUS_AUTH_FAILURE, ++ OHOS_BT_STATUS_RMT_DEV_DOWN, ++ OHOS_BT_STATUS_AUTH_REJECTED ++} BtStatus; ++ ++typedef struct { ++ /** UUID length */ ++ unsigned char uuidLen; ++ /** UUID field */ ++ char *uuid; ++} BtUuid; ++ ++#endif +diff --git a/adapter/common/bluez_adapter/include/ohos_bt_gap.h b/adapter/common/bluez_adapter/include/ohos_bt_gap.h +new file mode 100644 +index 000000000..eb570cca6 +--- /dev/null ++++ b/adapter/common/bluez_adapter/include/ohos_bt_gap.h +@@ -0,0 +1,350 @@ ++/* ++ * 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. ++ */ ++#ifndef OHOS_BT_GAP_H ++#define OHOS_BT_GAP_H ++ ++#include "ohos_bt_def.h" ++#include ++ ++#ifdef __cplusplus ++extern "C" { ++#endif ++ ++#define OHOS_BD_NAME_LEN 32 ++typedef struct { ++ BdAddr addr; ++ unsigned char deviceName[OHOS_BD_NAME_LEN]; /* Name of the device record, must be in UTF-8 */ ++} PairedDeviceInfo; ++ ++/* Bluetooth stack state */ ++typedef enum { ++ OHOS_GAP_STATE_TURNING_ON = 0x0, ++ OHOS_GAP_STATE_TURN_ON, ++ OHOS_GAP_STATE_TURNING_OFF, ++ OHOS_GAP_STATE_TURN_OFF ++} BtStackState; ++ ++/* Bluetooth ACL connection state */ ++typedef enum { ++ OHOS_GAP_ACL_STATE_CONNECTED, ++ OHOS_GAP_ACL_STATE_DISCONNECTED, ++ OHOS_GAP_ACL_STATE_LE_CONNECTED, ++ OHOS_GAP_ACL_STATE_LE_DISCONNECTED ++} GapAclState; ++ ++/* bondable mode */ ++typedef enum { ++ OHOS_GAP_BONDABLE_MODE_OFF = 0x00, ++ OHOS_GAP_BONDABLE_MODE_ON = 0x01 ++} GapBtBondableMode; ++ ++typedef enum { ++ OHOS_GAP_ACCESS_UNKNOWN, ++ OHOS_GAP_ACCESS_ALLOWED, ++ OHOS_GAP_ACCESS_FORBIDDEN, ++} GapBtPermissionType; ++ ++typedef enum { ++ OHOS_GAP_SCAN_MODE_NONE = 0x00, ++ OHOS_GAP_SCAN_MODE_CONNECTABLE, ++ OHOS_GAP_SCAN_MODE_GENERAL_DISCOVERABLE, ++ OHOS_GAP_SCAN_MODE_LIMITED_DISCOVERABLE, ++ OHOS_GAP_SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, ++ OHOS_GAP_SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE ++} GapBtScanMode; ++ ++typedef enum { ++ OHOS_PROFILE_HEADSET = 0x0, ++ OHOS_PROFILE_A2DP, ++ OHOS_PROFILE_OPP, ++ OHOS_PROFILE_HID, ++ OHOS_PROFILE_PANU, ++ OHOS_PROFILE_NAP, ++ OHOS_PROFILE_HFP ++} GapBtProfileType; ++ ++typedef enum { ++ OHOS_GAP_PAIR_NONE = 0x01, ++ OHOS_GAP_PAIR_PAIRING = 0x02, ++ OHOS_GAP_PAIR_PAIRED = 0x03 ++} GapBtPairedState; ++ ++/** ++ * @brief Enable ble. ++ * ++ * @return Returns true if the operation is accepted; ++ * returns false if the operation is rejected. ++ */ ++bool EnableBle(void); ++ ++/** ++ * @brief Disable ble. ++ * ++ * @return Returns true if the operation is accepted; ++ * returns false if the operation is rejected. ++ */ ++bool DisableBle(void); ++ ++/** ++ * @brief Enable classic. ++ * ++ * @return Returns true if the operation is accepted; ++ * returns false if the operation is rejected. ++ */ ++bool EnableBt(void); ++ ++/** ++ * @brief Disable classic. ++ * ++ * @return Returns true if the operation is accepted; ++ * returns false if the operation is rejected. ++ */ ++bool DisableBt(void); ++ ++/** ++ * @brief Get classic enable/disable state. ++ * ++ * @return Returns classic enable/disable state, {@link BtStackState}. ++ */ ++int GetBtState(); ++ ++/** ++ * @brief Get ble enable/disable state. ++ * ++ * @return Returns true if ble is enabled; ++ * returns false if ble is not enabled. ++ */ ++bool IsBleEnabled(); ++ ++/** ++ * @brief Get local host bluetooth address. ++ * ++ * @return Returns true if the operation is accepted; ++ * returns false if the operation is rejected. ++ */ ++bool GetLocalAddr(unsigned char *mac, unsigned int len); ++ ++/** ++ * @brief Set local device name. ++ * ++ * @param localName Device name. ++ * @param length localName length, The maximum length of the name is {@link OHOS_BD_NAME_LEN}. ++ * @return Returns true if the operation is successful; ++ * returns false if the operation fails. ++ */ ++bool SetLocalName(unsigned char *localName, unsigned char length); ++ ++/** ++ * @brief Set device scan mode. ++ * @param mode Scan mode, see details {@link GapBtScanMode}. ++ * @param duration Scan time. ++ * @return Returns true if the operation is successful; ++ * returns false if the operation fails. ++ */ ++bool SetBtScanMode(int mode, int duration); ++ ++/** ++ * @brief Check device pair request reply. ++ * ++ * @param bdAddr device address. ++ * @param transport Transport type, details see {@link BtTransportId} ++ * @param accept Set gap accept flag. ++ * @return Returns true if the operation is successful; ++ * returns false if the operation fails. ++ */ ++bool PairRequestReply(const BdAddr *bdAddr, int transport, bool accept); ++ ++/** ++ * @brief Set device pairing confirmation. ++ * ++ * @param bdAddr device address. ++ * @param transport Transport type, details see {@link BtTransportId} ++ * @param accept Set gap accept flag. ++ * @return Returns true if the operation is successful; ++ * returns false if the operation fails. ++ */ ++bool SetDevicePairingConfirmation(const BdAddr *bdAddr, int transport, bool accept); ++ ++/** ++ * @brief Gap state changed callback. ++ * ++ * @param transport Transport type when state change, details see {@link BtTransportId} ++ * @param status Change to the new state, details see {@link BtStackState}. ++ */ ++typedef void (*GapStateChangedCallback)(const int transport, const int status); ++ ++/** ++ * @brief ACL state changed callback. ++ * ++ * @param bdAddr device address. ++ * @param state details of acl state changed. ++ * @param reason clarify detals of REASON CODE from stack - If possible, suggest using the standard HCI error code. ++ */ ++typedef void (*GapAclStateChangedCallback)(const BdAddr *bdAddr, GapAclState state, unsigned int reason); ++ ++/** ++ * @brief Discovery state changed callback. ++ * ++ * @param status DISCOVERY_STARTED/DISCOVERYING/DISCOVERY_STOPED ++ */ ++typedef void (*GapDiscoveryStateChangedCallback)(int status); ++ ++/** ++ * @brief Discovery state changed observer. ++ * ++ * @param status Device discovery status. ++ */ ++typedef void (*GapDiscoveryResultCallback)(const BdAddr *bdAddr); ++ ++/** ++ * @brief Pair request observer. ++ * ++ * @param bdAddr Remote device address. ++ * @param transport Transport type, details see {@link BtTransportId} ++ */ ++typedef void (*GapPairRequestedCallback)(const BdAddr *bdAddr, int transport); ++ ++/** ++ * @brief Pair confirmed observer. ++ * ++ * @param bdAddr Remote device address. ++ * @param transport Transport type, details see {@link BtTransportId} ++ * @param reqType Pair type. ++ * @param number Paired passkey. ++ */ ++typedef void (*GapPairConfirmedCallback)(const BdAddr *bdAddr, int transport, int reqType, int number); ++ ++/** ++ * @brief Scan mode changed observer. ++ * ++ * @param mode Device scan mode. ++ */ ++typedef void (*GapScanModeChangedCallback)(int mode); ++ ++/** ++ * @brief Device name changed observer. ++ * ++ * @param deviceName Device name. ++ */ ++typedef void (*GapLocalDeviceNameChangedCallback)(const unsigned char *deviceName, unsigned char length); ++ ++/** ++ * @brief Device address changed observer. ++ * ++ * @param bdAddr Device address. ++ */ ++typedef void (*GapLocalDeviceAddrChangedCallback)(const BdAddr *bdAddr); ++ ++/** ++ * @brief Pair status changed observer. ++ * ++ * @param bdAddr Device address. ++ * @param status Remote device pair status. ++ */ ++typedef void (*GapPairStatusChangedCallback)(const BdAddr *bdAddr, int status); ++ ++/** ++ * @brief Remote uuid changed observer. ++ * ++ * @param bdAddr Device address. ++ * @param uuid Remote device uuids. ++ */ ++typedef void (*GapRemoteUuidChangedCallback)(const BdAddr *bdAddr, BtUuid uuid); ++ ++/** ++ * @brief Remote name changed observer. ++ * ++ * @param bdAddr Device address. ++ * @param deviceName Remote device name. ++ */ ++typedef void (*GapRemoteNameChangedCallback)(const BdAddr *bdAddr, ++ const unsigned char *deviceName, unsigned char length); ++ ++/** ++ * @brief Remote alias changed observer. ++ * ++ * @param bdAddr Device address. ++ * @param alias Remote device alias. ++ */ ++typedef void (*GapRemoteAliasChangedCallback)(const BdAddr *bdAddr, const unsigned char *alias, unsigned char length); ++ ++/** ++ * @brief Remote cod changed observer. ++ * ++ * @param bdAddr Device address. ++ * @param cod Remote device cod. ++ */ ++typedef void (*GapRemoteCodChangedCallback)(const BdAddr *bdAddr, int cod); ++ ++/** ++ * @brief Remote battery level changed observer. ++ * ++ * @param bdAddr Device address. ++ * @param cod Remote device battery Level. ++ */ ++typedef void (*GapRemoteBatteryLevelChangedCallback)(const BdAddr *bdAddr, int batteryLevel); ++ ++/** ++ * @brief Remote rssi event observer. ++ * ++ * @param bdAddr Device address. ++ * @param rssi Remote device rssi. ++ * @param status Read status. ++ */ ++typedef void (*GapReadRemoteRssiEventCallback)(const BdAddr *bdAddr, int rssi, int status); ++ ++/** ++ * @brief Query application whether to accept the connection. ++ * ++ * @param bdAddr Device address. ++ * @param res Application decision, 0 is reject, 1 is accept. ++ */ ++typedef void (*GapIsAcceptConnOnSafeModeCallback)(const BdAddr *bdAddr, bool *res); ++ ++/** ++ * @brief Defines callbacks for the GAP ++ */ ++typedef struct { ++ GapStateChangedCallback stateChangeCallback; ++ GapAclStateChangedCallback aclStateChangedCallbak; ++ GapScanModeChangedCallback scanModeChangedCallback; ++ GapPairStatusChangedCallback pairStatusChangedCallback; ++ GapLocalDeviceNameChangedCallback deviceNameChangedCallback; ++ GapLocalDeviceAddrChangedCallback deviceAddrChangedCallback; ++ GapPairRequestedCallback pairRequestedCallback; ++ GapPairConfirmedCallback pairConfiremedCallback; ++ GapDiscoveryStateChangedCallback discoveryStateChangedCallback; ++ GapDiscoveryResultCallback discoveryResultCallback; ++ GapRemoteUuidChangedCallback remoteUuidChangedCallback; ++ GapRemoteNameChangedCallback remoteNameChangedCallback; ++ GapRemoteAliasChangedCallback remoteAliasChangedCallback; ++ GapRemoteCodChangedCallback remoteCodChangedCallback; ++ GapRemoteBatteryLevelChangedCallback remoteBatteryLevelChangedCallback; ++ GapReadRemoteRssiEventCallback readRemoteRssiEventCallback; ++ GapIsAcceptConnOnSafeModeCallback isAcceptConnOnSafeModeCallback; ++} BtGapCallBacks; ++ ++/** ++ * @brief Registers GAP callbacks. ++ * ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the callbacks are registered; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ */ ++int GapRegisterCallbacks(BtGapCallBacks *func); ++ ++#ifdef __cplusplus ++} ++#endif ++#endif +\ No newline at end of file +diff --git a/adapter/common/bluez_adapter/include/ohos_bt_gatt.h b/adapter/common/bluez_adapter/include/ohos_bt_gatt.h +new file mode 100644 +index 000000000..03544b5ae +--- /dev/null ++++ b/adapter/common/bluez_adapter/include/ohos_bt_gatt.h +@@ -0,0 +1,725 @@ ++/* ++ * Copyright (C) 2021-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. ++ */ ++ ++/** ++ * @addtogroup Bluetooth ++ * @{ ++ * ++ * @brief Provides basic Bluetooth capabilities. ++ * ++ * This module allows you to enable and disable Bluetooth, and access basic Bluetooth capabilities.\n ++ * Bluetooth uses profiles such as BT-GAP, BLE, BLE-GATT, BT-data transmission, HFP, A2DP, AVRCP, MAP, and PBAP. ++ * ++ * @since 6 ++ */ ++ ++/** ++ * @file ohos_bt_gatt.h ++ * ++ * @brief Declares basic GATT data structures and functions, such as advertising and scan functions. ++ * ++ * @since 6 ++ */ ++ ++ ++#ifndef OHOS_BT_GATT_H ++#define OHOS_BT_GATT_H ++ ++#include "ohos_bt_def.h" ++ ++#ifdef __cplusplus ++extern "C" { ++#endif ++ ++/** ++ * @brief Enumerates advertising filtering parameters. ++ * ++ * The parameters specify whether the advertiser uses a whitelist to filter scan or connection requests from scanners. ++ * ++ * @since 6 ++ */ ++typedef enum { ++ /** Processing scan and connection requests from all devices */ ++ OHOS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY = 0x00, ++ /** Processing connection requests from all devices and only the scan requests from devices in the whitelist */ ++ OHOS_BLE_ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY = 0x01, ++ /** Processing scan requests from all devices and only the connection requests from devices in the whitelist */ ++ OHOS_BLE_ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST = 0x02, ++ /** Processing only the scan and connection requests from devices in the whitelist */ ++ OHOS_BLE_ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST = 0x03, ++} BleAdvFilter; ++ ++/** ++ * @brief Enumerates advertisement types. ++ * ++ * @since 6 ++ */ ++typedef enum { ++ /** Connectable and scannable undirected advertisement (default) */ ++ OHOS_BLE_ADV_IND = 0x00, ++ /** Connectable directed advertisement with a high duty ratio */ ++ OHOS_BLE_ADV_DIRECT_IND_HIGH = 0x01, ++ /** Scannable undirected advertisement */ ++ OHOS_BLE_ADV_SCAN_IND = 0x02, ++ /** Unconnectable undirected advertisement */ ++ OHOS_BLE_ADV_NONCONN_IND = 0x03, ++ /** Connectable directed advertisement with a low duty ratio */ ++ OHOS_BLE_ADV_DIRECT_IND_LOW = 0x04, ++} BleAdvType; ++ ++/** ++ * @brief Enumerates I/O capability modes. ++ * ++ * @since 6 ++ */ ++typedef enum { ++ /** DisplayOnly: display capability only */ ++ OHOS_BLE_IO_CAP_OUT = 0x00, ++ /** DisplayYesNo: YES/NO input and display capabilities */ ++ OHOS_BLE_IO_CAP_IO, ++ /** ++ * KeyboardOnly: Input of a number from 0 to 9, the confirmation key, and YES/NO, ++ * with no display capability ++ */ ++ OHOS_BLE_IO_CAP_IN, ++ /** NoInputNoOutput: no I/O capability */ ++ OHOS_BLE_IO_CAP_NONE, ++ /** ++ * KeyboardDisplay: Input of a number from 0 to 9, the confirmation key, and YES/NO, ++ * with the display capability ++ */ ++ OHOS_BLE_IO_CAP_KBDISP ++} BleIoCapMode; ++ ++/** ++ * @brief Enumerates authentication modes for secure connection requests. ++ * ++ * @since 6 ++ */ ++typedef enum { ++ /** No bonding */ ++ OHOS_BLE_AUTH_NO_BOND = 0x00, ++ /** Bonding */ ++ OHOS_BLE_AUTH_BOND, ++ /** MITM only */ ++ OHOS_BLE_AUTH_REQ_MITM, ++ /** Secure connection only */ ++ OHOS_BLE_AUTH_REQ_SC_ONLY, ++ /** Secure connection and bonding */ ++ OHOS_BLE_AUTH_REQ_SC_BOND, ++ /** Secure connection and MITM */ ++ OHOS_BLE_AUTH_REQ_SC_MITM, ++ /** Secure connection, MITM, and bonding */ ++ OHOS_BLE_AUTH_REQ_SC_MITM_BOND ++} BleAuthReqMode; ++ ++/** ++ * @brief Enumerates BLE scan types. ++ * ++ * @since 6 ++ */ ++typedef enum { ++ /** A passive scan with no scan request */ ++ OHOS_BLE_SCAN_TYPE_PASSIVE = 0x00, ++ /** An active scan that may contain a scan request */ ++ OHOS_BLE_SCAN_TYPE_ACTIVE, ++} BleScanType; ++ ++/** ++ * @brief Enumerates BLE scan modes. ++ * ++ * @since 6 ++ */ ++typedef enum { ++ /** Low power */ ++ OHOS_BLE_SCAN_MODE_LOW_POWER = 0x00, ++ /** Balance */ ++ OHOS_BLE_SCAN_MODE_BALANCED = 0x01, ++ /** Low latency */ ++ OHOS_BLE_SCAN_MODE_LOW_LATENCY = 0x02, ++ /** Duty cycle 2 */ ++ OHOS_BLE_SCAN_MODE_OP_P2_60_3000 = 0x03, ++ /** Duty cycle 10 */ ++ OHOS_BLE_SCAN_MODE_OP_P10_60_600 = 0x04, ++ /** Duty cycle 25 */ ++ OHOS_BLE_SCAN_MODE_OP_P25_60_240 = 0x05, ++ /** Duty cycle 100 */ ++ OHOS_BLE_SCAN_MODE_OP_P100_1000_1000 = 0x06 ++} BleScanMode; ++ ++/** ++ * @brief Enumerates policies for filtering advertisements in a BLE scan. ++ * ++ * @since 6 ++ */ ++typedef enum { ++ /** ++ * Accepting all advertisements except the directed advertisements ++ * that are not sent to the current device (default) ++ */ ++ OHOS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL = 0x00, ++ /** ++ * Accepting advertisements from devices in the whitelist and ignoring the directed advertisements ++ * that are not sent to the current device */ ++ OHOS_BLE_SCAN_FILTER_POLICY_ONLY_WHITE_LIST, ++ /** ++ * Accepting all undirected advertisements, directed advertisements sent by advertisers with ++ * resolvable private addresses, and all the directed advertisements sent to the current device ++ */ ++ OHOS_BLE_SCAN_FILTER_POLICY_ACCEPT_ALL_AND_RPA, ++ /** ++ * Accepting all undirected advertisements from the devices in the whitelist, ++ * directed advertisements sent by advertisers with resolvable private addresses, ++ * and all the directed advertisements sent to the current device ++ */ ++ OHOS_BLE_SCAN_FILTER_POLICY_ONLY_WHITE_LIST_AND_RPA ++} BleScanFilterPolicy; ++ ++/** ++ * @brief Enumerates advertisement types in the BLE scan result. ++ * ++ * @since 6 ++ */ ++typedef enum { ++ /** Extended, unconnectable, unscannable, and undirected advertisement */ ++ OHOS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE = 0x00, ++ /** Extended, unconnectable, unscannable, and directed advertisement */ ++ OHOS_BLE_EVT_NON_CONNECTABLE_NON_SCANNABLE_DIRECTED = 0x04, ++ /** Extended, connectable, and undirected advertisement */ ++ OHOS_BLE_EVT_CONNECTABLE = 0x01, ++ /** Extended, connectable, and directed advertisement */ ++ OHOS_BLE_EVT_CONNECTABLE_DIRECTED = 0x05, ++ /** Extended, scannable, and undirected advertisement */ ++ OHOS_BLE_EVT_SCANNABLE = 0x02, ++ /** Extended, scannable, and directed advertisement */ ++ OHOS_BLE_EVT_SCANNABLE_DIRECTED = 0x06, ++ ++ /** Legacy, unconnectable, and undirected advertisement */ ++ OHOS_BLE_EVT_LEGACY_NON_CONNECTABLE = 0x10, ++ /** Legacy, scannable, and undirected advertisement */ ++ OHOS_BLE_EVT_LEGACY_SCANNABLE = 0x12, ++ /** Legacy, connectable, scannable, and undirected advertisement */ ++ OHOS_BLE_EVT_LEGACY_CONNECTABLE = 0x13, ++ /** Legacy, connectable, and directed advertisement */ ++ OHOS_BLE_EVT_LEGACY_CONNECTABLE_DIRECTED = 0x15, ++ /** Legacy scan response corresponding to ADV_SCAN_IND */ ++ OHOS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV_SCAN = 0x1A, ++ /** Legacy scan response corresponding to ADV_IND */ ++ OHOS_BLE_EVT_LEGACY_SCAN_RSP_TO_ADV = 0x1B ++} BleScanResultEvtType; ++ ++/** ++ * @brief Enumerates data integrity types for a BLE scan result. ++ * ++ * @since 6 ++ */ ++typedef enum { ++ /** Complete data or the last segment */ ++ OHOS_BLE_DATA_COMPLETE = 0x00, ++ /** Incomplete data, with more data to come */ ++ OHOS_BLE_DATA_INCOMPLETE_MORE_TO_COME = 0x01, ++ /** Incomplete truncated data, with no more data to come */ ++ OHOS_BLE_DATA_INCOMPLETE_TRUNCATED = 0x02, ++} BleScanResultDataStatus; ++ ++/** ++ * @brief Enumerates address types for a BLE scan result. ++ * ++ * @since 6 ++ */ ++typedef enum { ++ /** Public device address */ ++ OHOS_BLE_PUBLIC_DEVICE_ADDRESS = 0x00, ++ /** Random device address */ ++ OHOS_BLE_RANDOM_DEVICE_ADDRESS = 0x01, ++ /** Public identity address */ ++ OHOS_BLE_PUBLIC_IDENTITY_ADDRESS = 0x02, ++ /** Random (static) identity address */ ++ OHOS_BLE_RANDOM_STATIC_IDENTITY_ADDRESS = 0x03, ++ /** Unresolvable random device address */ ++ OHOS_BLE_UNRESOLVABLE_RANDOM_DEVICE_ADDRESS = 0xFE, ++ /** No address (anonymous advertisement) */ ++ OHOS_BLE_NO_ADDRESS = 0xFF, ++} BleScanResultAddrType; ++ ++/** ++ * @brief Enumerates PHY types for a BLE scan result. ++ * ++ * @since 6 ++ */ ++typedef enum { ++ /** No advertisement packet */ ++ OHOS_BLE_SCAN_PHY_NO_PACKET = 0x00, ++ /** 1M PHY */ ++ OHOS_BLE_SCAN_PHY_1M = 0x01, ++ /** 2M PHY */ ++ OHOS_BLE_SCAN_PHY_2M = 0x02, ++ /** Coded PHY */ ++ OHOS_BLE_SCAN_PHY_CODED = 0x03 ++} BleScanResultPhyType; ++ ++/** ++ * @brief Defines BLE advertising parameters. ++ * ++ * @since 6 ++ */ ++typedef struct { ++ /** ++ * Minimum advertising interval. ++ * It is calculated as follows: [N * 0.625 ms], where N indicates the value of this field. ++ */ ++ int minInterval; ++ /** ++ * Maximum advertising interval. ++ * It is calculated as follows: [N * 0.625 ms], where N indicates the value of this field. ++ */ ++ int maxInterval; ++ /** Advertising type */ ++ BleAdvType advType; ++ /** Local address type */ ++ unsigned char ownAddrType; ++ /** Peer address type */ ++ unsigned char peerAddrType; ++ /** Peer address */ ++ BdAddr peerAddr; ++ /** ++ * Advertising channel to be used. For example, 0x01 indicates that channel 37 is to be used, ++ * and 0x07 indicates that channels 37, 38, and 39 are used. ++ */ ++ int channelMap; ++ /** Advertisement filtering policy based on a whitelist */ ++ BleAdvFilter advFilterPolicy; ++ /** Transmit power (dBm) */ ++ int txPower; ++ /** Advertising duration. It is calculated as follows: [N * 10 ms], where N indicates the value of this field. */ ++ int duration; ++} BleAdvParams; ++ ++/** ++ * @brief Defines BLE scan parameters. ++ * ++ * @since 6 ++ */ ++typedef struct { ++ /** Scan interval. It is calculated as follows: [N * 0.625 ms], where N indicates the value of this field. */ ++ unsigned short scanInterval; ++ /** Scan window. It is calculated as follows: [N * 0.625 ms], where N indicates the value of this field. */ ++ unsigned short scanWindow; ++ /** Scan type, as enumerated in {@link BleScanType} */ ++ unsigned char scanType; ++ /** ++ * PHY on which the advertising packets are received. ++ * bit0 indicates 1M PHY and bit2 indicates LE Coded PHY. ++ * bit0 and bit2 can both be set to 1. All other bits are reserved for future use. ++ */ ++ unsigned char scanPhy; ++ /** Policy for filtering the scan result, as enumerated in {@link BleScanFilterPolicy} */ ++ unsigned char scanFilterPolicy; ++} BleScanParams; ++ ++/** ++ * @brief Defines BLE scan native filter. ++ * ++ * @since 6 ++ */ ++typedef struct { ++ /** Handling advertisments sent by advertisers with specific address */ ++ char *address; ++ /** Handling advertisments sent by advertisers with specific deviceName */ ++ char *deviceName; ++ /** The length of the service uuid */ ++ unsigned int serviceUuidLength; ++ /** Handling advertisments sent by advertisers with specific service uuid */ ++ unsigned char *serviceUuid; ++ /** Handling advertisments sent by advertisers with specific service uuid mask */ ++ unsigned char *serviceUuidMask; ++ /** The length of the service data */ ++ unsigned int serviceDataLength; ++ /** Handling advertisments sent by advertisers with specific serviceData */ ++ unsigned char *serviceData; ++ /** Handling advertisments sent by advertisers with specific serviceDataMask */ ++ unsigned char *serviceDataMask; ++ /** The length of the manufacture data */ ++ unsigned int manufactureDataLength; ++ /** Handling advertisments sent by advertisers with specific manufactureData */ ++ unsigned char *manufactureData; ++ /** Handling advertisments sent by advertisers with specific manufactureDataMask */ ++ unsigned char *manufactureDataMask; ++ /** Handling advertisments sent by advertisers with specific manufactureId */ ++ unsigned short manufactureId; ++} BleScanNativeFilter; ++ ++/** ++ * @brief Defines BLE scan configurations. ++ * ++ * @since 6 ++ */ ++typedef struct { ++ /** Repport delay time */ ++ long reportDelayMillis; ++ /** Scan mode */ ++ int scanMode; ++ /** Legacy */ ++ bool legacy; ++ /** Phy */ ++ int phy; ++} BleScanConfigs; ++ ++/** ++ * @brief Defines raw data for the BLE advertising and scan response. ++ * ++ * This structure is available for system applications only. ++ * ++ * @since 6 ++ */ ++typedef struct { ++ /** Advertising data */ ++ unsigned char *advData; ++ /** Advertising data length */ ++ unsigned int advDataLen; ++ /** Scan response data */ ++ unsigned char *rspData; ++ /** Scan response data length */ ++ unsigned int rspDataLen; ++} StartAdvRawData; ++ ++/** ++ * @brief Defines the BLE scan result data. ++ * ++ * @since 6 ++ */ ++typedef struct { ++ /** Advertisement type, as enumerated in {@link BleScanResultEvtType} */ ++ unsigned char eventType; ++ /** Data status, as enumerated in {@link BleScanResultDataStatus} */ ++ unsigned char dataStatus; ++ /** Address type, as enumerated in {@link BleScanResultAddrType}. Value 0xFE is unavailable. */ ++ unsigned char addrType; ++ /** Address */ ++ BdAddr addr; ++ /** ++ * PHY type of a primary advertisement, as enumerated in {@link BleScanResultPhyType}. ++ * The value can only be 0x01 or 0x03. ++ */ ++ unsigned char primaryPhy; ++ /** ++ * PHY type of a secondary advertisement. ++ * The value can be any of the enumerations of {@link BleScanResultPhyType}. ++ */ ++ unsigned char secondaryPhy; ++ /** ++ * Advertising SID in the ADI field of the PDU or of the original scannable advertisement (for scan responses). ++ * Value 0xFF indicates that no ADI field is available. ++ */ ++ unsigned char advSid; ++ /** ++ * Transmit power. The value ranges from -127 to +20 dBm. ++ * Value 0x7F indicates that this field is invalid. ++ */ ++ char txPower; ++ /** RSSI. The value ranges from -127 to +20 dBm. Value 0x7F indicates that this field is invalid. */ ++ char rssi; ++ /** ++ * Periodic advertising interval. ++ * It is calculated as follows: [N * 1.25 ms], where N indicates the value of this field. ++ * Value 0x00 indicates that this field is invalid. ++ */ ++ unsigned short periodicAdvInterval; ++ /** ++ * Address type for directed advertising, as enumerated in {@link BleScanResultAddrType}. ++ * Value 0xFF is unavailable. ++ */ ++ unsigned char directAddrType; ++ /** Address for directed advertising */ ++ BdAddr directAddr; ++ /** Advertising data length */ ++ unsigned char advLen; ++ /** Advertising data */ ++ unsigned char *advData; ++} BtScanResultData; ++ ++/** ++ * @brief Called when advertising is enabled. For details, see {@link BleStartAdv}. ++ * ++ * @since 6 ++ */ ++typedef void (*AdvEnableCallback)(int advId, int status); ++ ++/** ++ * @brief Called when advertising is disabled. For details, see {@link BleStopAdv}. ++ * ++ * @since 6 ++ */ ++typedef void (*AdvDisableCallback)(int advId, int status); ++ ++/** ++ * @brief Called when advertising data is set. For details, see {@link BleSetAdvData}. ++ * ++ * @since 6 ++ */ ++typedef void (*AdvDataCallback)(int advId, int status); ++ ++/** ++ * @brief Called when advertising parameters are updated. For details, see {@link BleUpdateAdv} ++ * ++ * @since 6 ++ */ ++typedef void (*AdvUpdateCallback)(int advId, int status); ++ ++/** ++ * @brief Called when a secure access request is received. ++ * ++ * {@link BleGattSecurityRsp} is used to grant the secure access permission. ++ * ++ * @since 6 ++ */ ++typedef void (*SecurityRespondCallback)(const BdAddr *bdAddr); ++ ++/** ++ * @brief Called when the scan result is received. ++ * ++ * @since 6 ++ */ ++typedef void (*ScanResultCallback)(BtScanResultData *scanResultdata); ++ ++/** ++ * @brief Called when scan parameters are set. ++ * ++ * @since 6 ++ */ ++typedef void (*ScanParameterSetCompletedCallback)(int clientId, int status); ++ ++/** ++ * @brief Defines GATT callbacks. ++ * ++ * @since 6 ++ */ ++typedef struct { ++ /** Called when advertising is enabled. */ ++ AdvEnableCallback advEnableCb; ++ /** Called when advertising is disabled. */ ++ AdvDisableCallback advDisableCb; ++ /** Called when advertising data is set. */ ++ AdvDataCallback advDataCb; ++ /** Called when advertising parameters are updated. */ ++ AdvUpdateCallback advUpdateCb; ++ /** Called when a secure access request is received. */ ++ SecurityRespondCallback securityRespondCb; ++ /** Called when the scan result is received. */ ++ ScanResultCallback scanResultCb; ++ /** Called when scan parameters are set. */ ++ ScanParameterSetCompletedCallback scanParamSetCb; ++} BtGattCallbacks; ++ ++/** ++ * @brief Initializes the Bluetooth protocol stack. ++ * ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the Bluetooth protocol stack is initialized; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int InitBtStack(void); ++ ++/** ++ * @brief Enables the Bluetooth protocol stack. ++ * ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the Bluetooth protocol stack is enabled; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int EnableBtStack(void); ++ ++/** ++ * @brief Disables the Bluetooth protocol stack. ++ * ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the Bluetooth protocol stack is disabled; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int DisableBtStack(void); ++ ++/** ++ * @brief Sets the Bluetooth device name. ++ * ++ * @param name Indicates the pointer to the name to set. ++ * @param len Indicates the length of the name to set. ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the Bluetooth device name is set; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int SetDeviceName(const char *name, unsigned int len); ++ ++/** ++ * @brief Sets advertising data. ++ * ++ * @param advId Indicates the advertisement ID, which is allocated by the upper layer of the advertiser. ++ * @param data Indicates the pointer to the advertising data. For details, see {@link StartAdvRawData}. ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if advertising data is set; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int BleSetAdvData(int advId, const StartAdvRawData data); ++ ++/** ++ * @brief Starts advertising. ++ * ++ * @param advId Indicates the advertisement ID, which is allocated by the upper layer of the advertiser. ++ * @param param Indicates the pointer to the advertising parameters. For details, see {@link BleAdvParams}. ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if advertising is started; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int BleStartAdv(int advId, const BleAdvParams *param); ++ ++/** ++ * @brief Stops advertising. ++ * ++ * @param advId Indicates the advertisement ID, which is allocated by the upper layer of the advertiser. ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if advertising is stopped; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int BleStopAdv(int advId); ++ ++/** ++ * @brief Updates advertising parameters. ++ * ++ * @param advId Indicates the advertisement ID, which is allocated by the upper layer of the advertiser. ++ * @param param Indicates the pointer to the advertising parameters. For details, see {@link BleAdvParams}. ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if advertising parameters are updated; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int BleUpdateAdv(int advId, const BleAdvParams *param); ++ ++/** ++ * @brief Sets the secure I/O capability mode. ++ * ++ * @param mode Indicates the capability mode to set. For details, see {@link BleIoCapMode}. ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the capability mode is set; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int BleSetSecurityIoCap(BleIoCapMode mode); ++ ++/** ++ * @brief Sets the authentication mode for secure connection requests. ++ * ++ * @param mode Indicates the authentication mode to set. For details, see {@link BleAuthReqMode}. ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the authentication mode is set; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int BleSetSecurityAuthReq(BleAuthReqMode mode); ++ ++/** ++ * @brief Responds to a secure connection request. ++ * ++ * @param bdAddr Indicates the address of the device that sends the request. ++ * @param accept Specifies whether to accept the request. The value true means to accept the request, ++ * and false means to reject the request. ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the request is responded to; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int BleGattSecurityRsp(BdAddr bdAddr, bool accept); ++ ++/** ++ * @brief Obtains the device MAC address. ++ * ++ * @param mac Indicates the pointer to the device MAC address. ++ * @param len Indicates the length of the device MAC address. ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the device MAC address is obtained; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int ReadBtMacAddr(unsigned char *mac, unsigned int len); ++ ++/** ++ * @brief Sets scan parameters. ++ * ++ * @param clientId Indicates the client ID, which is obtained during client registration. ++ * @param param Indicates the pointer to the scan parameters. For details, see {@link BleScanParams}. ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the scan parameters are set; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int BleSetScanParameters(int clientId, BleScanParams *param); ++ ++/** ++ * @brief Starts a scan. ++ * ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the scan is started; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int BleStartScan(void); ++ ++/** ++ * @brief Stops a scan. ++ * ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the scan is stopped; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int BleStopScan(void); ++ ++/** ++ * @brief Registers GATT callbacks. ++ * ++ * @param func Indicates the pointer to the callbacks to register. For details, see {@link BtGattCallbacks}. ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the GATT callbacks are registered; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int BleGattRegisterCallbacks(BtGattCallbacks *func); ++ ++/** ++ * @brief Sets advertising data and parameters and starts advertising. ++ * ++ * This function is available for system applications only. \n ++ * ++ * @param advId Indicates the pointer to the advertisement ID. ++ * @param rawData Indicates the advertising data. For details, see {@link StartAdvRawData}. ++ * @param advParam Indicates the advertising parameters. For details, see {@link BleAdvParams}. ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the operation is successful; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int BleStartAdvEx(int *advId, const StartAdvRawData rawData, ++ BleAdvParams advParam); ++ ++/** ++ * @brief Starts a scan with BleScanConfigs. ++ * If don't need ble scan filter, set BleScanNativeFilter to NULL or filterSize to zero. ++ * If one of the ble scan filtering rules is not required, set it to NULL. ++ * For example, set the address to NULL when you don't need it. ++ * Don't support only using manufactureId as filter conditions, need to use it with manufactureData. ++ * The manufactureId need to be set a related number when you need a filtering condition of manufactureData. ++ * ++ * @param configs Indicates the pointer to the scan filter. For details, see {@link BleScanConfigs}. ++ * @param filter Indicates the pointer to the scan filter. For details, see {@link BleScanNativeFilter}. ++ * @param filterSize Indicates the number of the scan filter. ++ * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the scan is started; ++ * returns an error code defined in {@link BtStatus} otherwise. ++ * @since 6 ++ */ ++int BleStartScanEx(void); ++ ++#ifdef __cplusplus ++} ++#endif ++#endif ++/** @} */ +diff --git a/adapter/common/bluez_adapter/include/ohos_bt_spp.h b/adapter/common/bluez_adapter/include/ohos_bt_spp.h +new file mode 100644 +index 000000000..9dbf01ea9 +--- /dev/null ++++ b/adapter/common/bluez_adapter/include/ohos_bt_spp.h +@@ -0,0 +1,131 @@ ++/* ++ * 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. ++ */ ++ ++#ifndef OHOS_BT_SPP_H ++#define OHOS_BT_SPP_H ++ ++#include ++#include "ohos_bt_def.h" ++ ++#ifdef __cplusplus ++extern "C" { ++#endif ++ ++#define BT_SPP_READ_SOCKET_CLOSED (0) ++#define BT_SPP_READ_FAILED (-1) ++#define BT_SPP_WRITE_FAILED (-1) ++#define BT_SPP_INVALID_ID (-1) ++ ++typedef enum { ++ OHOS_SPP_SOCKET_RFCOMM = 0x0, ++} BtSppSocketType; ++ ++typedef struct { ++ BtUuid uuid; ++ BtSppSocketType socketType; ++ bool isEncrypt; ++} BtCreateSocketPara; ++ ++/** ++ * @brief Creates an server listening socket based on the service record. ++ * ++ * @param socketPara The parameters to create a server socket. ++ * @param name The service's name. ++ * @param len The length of the service's name. ++ * @return Returns a server ID, if create fail return {@link BT_SPP_INVALID_ID}. ++ */ ++int SppServerCreate(BtCreateSocketPara *socketPara, const char *name, unsigned int len); ++ ++/** ++ * @brief Waits for a remote device to connect to this server socket. ++ * ++ * This method return a client ID indicates a client socket ++ * can be used to read data from and write data to remote device. ++ * ++ * @param serverId The relative ID used to identify the current server socket, obtain the value by calling ++ * {@link SppServerCreate}. ++ * @return Returns a client ID, if accept fail return {@link BT_SPP_INVALID_ID}. ++ */ ++int SppServerAccept(int serverId); ++ ++/** ++ * @brief Disables an spp server socket and releases related resources. ++ * ++ * @param serverId The relative ID used to identify the current server socket, obtain the value by calling ++ * {@link SppServerCreate}. ++ * @return Returns the operation result status {@link BtStatus}. ++ */ ++int SppServerClose(int serverId); ++ ++/** ++ * @brief Connects to a remote device over the socket. ++ * ++ * @param socketPara The param to create a client socket and connect to a remote device. ++ * @return Returns a client ID, if connect fail return {@link BT_SPP_INVALID_ID}. ++ */ ++int SppConnect(BtCreateSocketPara *socketPara, const BdAddr *bdAddr); ++ ++/** ++ * @brief Disables a connection and releases related resources. ++ * ++ * @param clientId The relative ID used to identify the current client socket. ++ * @return Returns the operation result status {@link BtStatus}. ++ */ ++int SppDisconnect(int clientId); ++ ++/** ++ * @brief Get the connection status of this socket. ++ * ++ * @param clientId The relative ID used to identify the current client socket. ++ * @return Returns true is connected or false is not connected. ++ */ ++bool IsSppConnected(int clientId); ++ ++/** ++ * @brief Spp get remote device's address. ++ * ++ * @param clientId The relative ID used to identify the current client socket. ++ * @param remoteAddr Remote device's address, memory allocated by caller. ++ * @return Returns the operation result status {@link BtStatus}. ++ */ ++int SppGetRemoteAddr(int clientId, BdAddr *remoteAddr); ++ ++/** ++ * @brief Read data from socket. ++ * ++ * @param clientId The relative ID used to identify the current client socket. ++ * @param buf Indicate the buffer which read in, memory allocated by caller. ++ * @param bufLen Indicate the buffer length. ++ * @return Returns the length greater than 0 as read the actual length. ++ * Returns {@link BT_SPP_READ_SOCKET_CLOSED} if the socket is closed. ++ * Returns {@link BT_SPP_READ_FAILED} if the operation failed. ++ */ ++int SppRead(int clientId, char *buf, const unsigned int bufLen); ++ ++/** ++ * @brief Client write data to socket. ++ * ++ * @param clientId The relative ID used to identify the current client socket. ++ * @param data Indicate the data to be written. ++ * @param len Indicates the length of the data to be written. ++ * @return Returns the actual write length. ++ * Returns {@link BT_SPP_WRITE_FAILED} if the operation failed. ++ */ ++int SppWrite(int clientId, const char *data, const unsigned int len); ++ ++#ifdef __cplusplus ++} ++#endif ++#endif +\ No newline at end of file +diff --git a/adapter/common/bus_center/network/lnn_bt_monitor.c b/adapter/common/bus_center/network/lnn_bt_monitor.c +index 26c23d5ac..c873f6e49 100644 +--- a/adapter/common/bus_center/network/lnn_bt_monitor.c ++++ b/adapter/common/bus_center/network/lnn_bt_monitor.c +@@ -112,10 +112,11 @@ int32_t LnnInitBtStateMonitorImpl(void) + return SOFTBUS_ERR; + } + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "lnn bt state monitor impl start success"); ++ LnnOnBtStateChanged(g_btStateListenerId,SOFTBUS_BR_STATE_TURN_ON); + return SOFTBUS_OK; + } + + void LnnDeinitBtStateMonitorImpl(void) + { + (void)SoftBusRemoveBtStateListener(g_btStateListenerId); +-} +\ No newline at end of file ++} +diff --git a/adapter/common/net/bluetooth/ble/softbus_adapter_ble_gatt.c b/adapter/common/net/bluetooth/ble/softbus_adapter_ble_gatt.c +index 4ea60d30c..a93783a52 100644 +--- a/adapter/common/net/bluetooth/ble/softbus_adapter_ble_gatt.c ++++ b/adapter/common/net/bluetooth/ble/softbus_adapter_ble_gatt.c +@@ -952,7 +952,7 @@ int SoftBusStartScan(int listenerId, const SoftBusBleScanParams *param) + BleScanNativeFilter *nativeFilter = NULL; + GetAllNativeScanFilter(listenerId, &nativeFilter, &filterSize); + DumpBleScanFilter(nativeFilter, filterSize); +- status = BleOhosStatusToSoftBus(BleStartScanEx(&scanConfig, nativeFilter, filterSize)); ++ status = BleOhosStatusToSoftBus(BleStartScanEx()); + SoftBusFree(nativeFilter); + } + if (status != SOFTBUS_BT_STATUS_SUCCESS) { +diff --git a/adapter/common/net/bluetooth/net_bluetooth.gni b/adapter/common/net/bluetooth/net_bluetooth.gni +index 55355a7d6..cea791d6a 100644 +--- a/adapter/common/net/bluetooth/net_bluetooth.gni ++++ b/adapter/common/net/bluetooth/net_bluetooth.gni +@@ -18,6 +18,7 @@ adapter_ble_inc = [ + "$dsoftbus_root_path/adapter/common/net/bluetooth/include", + "$dsoftbus_root_path/adapter/common/net/bluetooth/common", + "//foundation/communication/bluetooth/interfaces/inner_api/include/c_header", ++ "$softbus_adapter_common/bluez_adapter/include", + ] + adapter_ble_src = [ + "$dsoftbus_root_path/adapter/common/net/bluetooth/ble/softbus_adapter_ble_gatt.c", +@@ -27,5 +28,6 @@ adapter_ble_src = [ + "$dsoftbus_root_path/adapter/common/net/bluetooth/common/softbus_adapter_bt_common.c", + ] + adapter_net_br_src = [ "$dsoftbus_root_path/adapter/common/net/bluetooth/common/softbus_adapter_bt_common.c" ] +-adapter_ble_deps = +- [ "//foundation/communication/bluetooth/frameworks/inner:btframework" ] ++#adapter_ble_deps = ++# [ "//foundation/communication/bluetooth/frameworks/inner:btframework" ] ++adapter_ble_deps = ["$dsoftbus_root_path/adapter:bluez_adapter"] +diff --git a/adapter/default_config/feature_config/standard/config.gni b/adapter/default_config/feature_config/standard/config.gni +index 6602d7ed7..a3cb9cb85 100644 +--- a/adapter/default_config/feature_config/standard/config.gni ++++ b/adapter/default_config/feature_config/standard/config.gni +@@ -12,11 +12,11 @@ + # limitations under the License. + + declare_args() { +- dsoftbus_feature_disc_ble = false ++ dsoftbus_feature_disc_ble = true + dsoftbus_feature_disc_coap = true + + dsoftbus_feature_conn_tcp = true +- dsoftbus_feature_conn_br = false ++ dsoftbus_feature_conn_br = true + dsoftbus_feature_conn_ble = false + dsoftbus_feature_conn_p2p = false + +diff --git a/core/adapter/core_adapter.gni b/core/adapter/core_adapter.gni +index f69543e95..f3a35fb9f 100644 +--- a/core/adapter/core_adapter.gni ++++ b/core/adapter/core_adapter.gni +@@ -19,6 +19,7 @@ br_adapter_src = + + br_adapter_inc = [ + "$dsoftbus_root_path/core/adapter/br/include", ++ "$dsoftbus_root_path/adapter/common/bluez_adapter/include", + "//foundation/communication/bluetooth/interfaces/innerkits/native_c/include", + ] + +diff --git a/core/connection/ble/conn_ble.gni b/core/connection/ble/conn_ble.gni +index 1bd2edf51..45ac9dfd1 100644 +--- a/core/connection/ble/conn_ble.gni ++++ b/core/connection/ble/conn_ble.gni +@@ -23,10 +23,13 @@ if (dsoftbus_feature_conn_ble == false) { + "$dsoftbus_root_path/core/connection/ble/include", + "$dsoftbus_root_path/core/common/include", + ] ++ + if (dsoftbus_feature_conn_br == true) { + ble_connection_src += adapter_net_br_src + ble_connection_inc += adapter_ble_inc + ble_connection_deps += adapter_ble_deps ++ }else if(dsoftbus_feature_disc_ble == false){ ++ ble_connection_src += [ "$dsoftbus_root_path/core/connection/ble/src/softbus_ble_connection_utils_virtual.c" ] + } + } else { + ble_connection_src = [ +diff --git a/core/connection/ble/src/softbus_ble_connection_utils_virtual.c b/core/connection/ble/src/softbus_ble_connection_utils_virtual.c +new file mode 100644 +index 000000000..941f0b0e9 +--- /dev/null ++++ b/core/connection/ble/src/softbus_ble_connection_utils_virtual.c +@@ -0,0 +1,22 @@ ++/* ++ * 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 "softbus_adapter_bt_common.h" ++#include "softbus_conn_interface.h" ++#include "softbus_conn_manager.h" ++ ++int SoftBusGetBtState(void) ++{ ++ return BLE_DISABLE; ++} +diff --git a/core/connection/ble/src/softbus_ble_connection_virtual.c b/core/connection/ble/src/softbus_ble_connection_virtual.c +index 8de3d58c3..20c0271c6 100644 +--- a/core/connection/ble/src/softbus_ble_connection_virtual.c ++++ b/core/connection/ble/src/softbus_ble_connection_virtual.c +@@ -21,8 +21,3 @@ ConnectFuncInterface *ConnInitBle(const ConnectCallback *callback) + (void)callback; + return NULL; + } +- +-int SoftBusGetBtState(void) +-{ +- return BLE_DISABLE; +-} +\ No newline at end of file +diff --git a/core/connection/br/src/br_connection.c b/core/connection/br/src/br_connection.c +index 0f934015d..6c381398f 100644 +--- a/core/connection/br/src/br_connection.c ++++ b/core/connection/br/src/br_connection.c +@@ -77,6 +77,10 @@ static int32_t StopLocalListening(const LocalListenerInfo *info); + + static int32_t PostBytes(uint32_t connectionId, const char *data, int32_t len, int32_t pid, int32_t flag); + ++static void PostPackRequest(uint32_t connectionId); ++ ++static pthread_t g_read_conn; ++ + static ConnectFuncInterface g_brInterface = { + .ConnectDevice = ConnectDevice, + .PostBytes = PostBytes, +@@ -133,6 +137,12 @@ static void PostBytesInnerListener(uint32_t connId, uint64_t seq, int32_t module + } else { + SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "PostInner success, connId:%u, seq:%" PRIu64 ", module:%d", + connId, seq, module); ++ //adapt for bluez ++ int32_t state = GetBrConnStateByConnectionId(connId); ++ if (state == BR_CONNECTION_STATE_CLOSING){ ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,"after post bytes connect : BR_CONNECTION_STATE_CLOSING"); ++ PostPackRequest(connId); ++ } + } + } + +@@ -184,6 +194,32 @@ static int32_t PostClosingTimeoutEvent(uint32_t connectionId) + g_brAsyncHandler.looper->PostMessageDelay(g_brAsyncHandler.looper, msg, BR_CLOSE_TIMEOUT); + return SOFTBUS_OK; + } ++//adapt for bluez ++static void PostPackRequest(uint32_t connectionId){ ++ ++ BrConnectionInfo *connInfo = GetConnectionRef(connectionId); ++ if (connInfo == NULL) { ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, ++ "br OnPackResponse failed: not find device, connectionId: %u", connectionId); ++ return; ++ } ++ int32_t mySocketFd = connInfo->socketFd; ++ int32_t sideType = connInfo->sideType; ++ ++ pthread_cancel(g_read_conn); ++ ListDelete(&connInfo->node); ++ ReleaseBrconnectionNode(connInfo); ++ ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, ++ "br close local connection after notify remote device, connectionId=%u", connectionId); ++ if (sideType == BR_SERVICE_TYPE) { ++ ConnBrOnEvent(ADD_CONN_BR_SERVICE_DISCONNECTED_MSG, mySocketFd, mySocketFd); ++ } else { ++ ConnBrOnEvent(ADD_CONN_BR_CLIENT_DISCONNECTED_MSG, mySocketFd, mySocketFd); ++ } ++ ++ return; ++} + + static void PackRequest(int32_t delta, uint32_t connectionId) + { +@@ -197,12 +233,14 @@ static void PackRequest(int32_t delta, uint32_t connectionId) + SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, + "br pack request: delta:%d, ref:%d, connectionId:%u, state:%d", delta, refCount, connectionId, state); + ++ //adapt for bluez ++ /* + if (state == BR_CONNECTION_STATE_CLOSING) { + int32_t ret = PostClosingTimeoutEvent(connectionId); + SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "post close %u connection timeout event, ret: %d", + connectionId, ret); + // continue, anyway +- } ++ }*/ + + int32_t dataLen = 0; + char *buf = BrPackRequestOrResponse(METHOD_NOTIFY_REQUEST, delta, refCount, &dataLen); +@@ -1037,6 +1075,9 @@ void BrConnectedEventHandle(bool isClient, uint32_t value) + SoftBusFree(args); + goto EXIT; + } ++ //adapt for bluez ++ g_read_conn = tid; ++ + return; + EXIT: + SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BrConnectedEventHandle EXIT"); +@@ -1096,6 +1137,12 @@ static void OnPackResponse(int32_t delta, int32_t peerRef, uint32_t connectionId + SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, + "br OnPackResponse local device ref <= 0, and remote ref <=0 close connection now"); + SetBrConnStateByConnId(connectionId, BR_CONNECTION_STATE_CLOSING); ++ ++ //adapt for bluez ++ pthread_cancel(g_read_conn); ++ ListDelete(&connInfo->node); ++ ReleaseBrconnectionNode(connInfo); ++ + if (sideType == BR_SERVICE_TYPE) { + ConnBrOnEvent(ADD_CONN_BR_SERVICE_DISCONNECTED_MSG, mySocketFd, mySocketFd); + } else { +@@ -1244,6 +1291,16 @@ static int32_t BrConnLooperInit(void) + return SOFTBUS_OK; + } + ++//adapt for bluez ++static void InitBtState(void){ ++ SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "Mock Init BR STAT"); ++ ++ LocalListenerInfo info; ++ info.type = CONNECT_BR; ++ g_brEnable = SOFTBUS_BR_STATE_TURN_ON; ++ (void)StartLocalListening(&info); ++} ++ + static void StateChangedCallback(int32_t listenerId, int32_t status) + { + SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "StateChanged id: %d, status: %d", listenerId, status); +@@ -1300,6 +1357,10 @@ ConnectFuncInterface *ConnInitBr(const ConnectCallback *callback) + DestroyLooper(g_brAsyncHandler.looper); + return NULL; + } ++ ++ //adapt for bluez ++ InitBtState(); ++ + pthread_mutex_init(&g_brConnLock, NULL); + g_connectCallback = (ConnectCallback *)callback; + return &g_brInterface; +diff --git a/core/discovery/ble/disc_ble.gni b/core/discovery/ble/disc_ble.gni +index 179089bd3..294e0c6ae 100644 +--- a/core/discovery/ble/disc_ble.gni ++++ b/core/discovery/ble/disc_ble.gni +@@ -12,6 +12,7 @@ + # limitations under the License. + + import("//foundation/communication/dsoftbus/dsoftbus.gni") ++import("//foundation/communication/dsoftbus/adapter/common/net/bluetooth/net_bluetooth.gni") + + native_source_path = rebase_path("$dsoftbus_root_path") + disc_dep_dir = "dsoftbus_enhance/core/discovery/ble/share_ble" +@@ -39,7 +40,20 @@ if (dsoftbus_feature_disc_ble == false) { + "$dsoftbus_root_path/core/discovery/ble/dispatcher/src/disc_ble_dispatcher.c", + "$dsoftbus_root_path/core/discovery/ble/softbus_ble/src/disc_ble_utils.c", + "$dsoftbus_root_path/core/discovery/ble/softbus_ble/src/disc_ble.c", ++ "$dsoftbus_root_path/adapter/common/net/bluetooth/ble/softbus_adapter_ble_gatt.c", ++ "$dsoftbus_root_path/adapter/common/net/bluetooth/common/adapter_bt_utils.c", ++ #"$dsoftbus_root_path/adapter/common/net/bluetooth/common/softbus_adapter_bt_common.c", + ] ++ ++ if (dsoftbus_feature_conn_br ==false && dsoftbus_feature_conn_ble == false){ ++ ble_discovery_src += ["$dsoftbus_root_path/adapter/common/net/bluetooth/common/softbus_adapter_bt_common.c"] ++ } ++ ++ ble_discovery_inc += adapter_ble_inc ++ ble_discovery_deps += [ ++ "//foundation/communication/dsoftbus/adapter/:bluez_adapter", ++ ] ++ + } + + if (enhanced_share) { +-- +2.33.0 + diff --git a/distributed-utils.spec b/distributed-utils.spec index 81073fc..cca92a6 100644 --- a/distributed-utils.spec +++ b/distributed-utils.spec @@ -23,7 +23,7 @@ Name: distributed-utils Version: 1.0.0 -Release: 4 +Release: 5 Summary: Distributed middleware used components. License: Apache-2.0 and MIT Url: https://gitee.com/openharmony/ @@ -59,12 +59,15 @@ Patch10: 0010-adapt-for-build-mbedtls.patch Patch11: 0011-remove-dependency-and-adapt-for-build-device_manager.patch Patch12: 0012-remove-dependency-and-adapt-for-build-device_security_level.patch Patch13: 0013-increase-the-pthread-stack-size-of-x86-and-other-env-dsoftbus.patch +Patch14: 0014-add-support-for-classic-bluetooth.patch BuildRequires: python3-jinja2 python3-pyyaml cjson cjson-devel BuildRequires: libatomic libicu-devel libxml2-devel openssl-devel BuildRequires: distributed-build hilog +#support for linux bluez +BuildRequires: bluez-devel -Requires: hilog python3-jinja2 python3-pyyaml cjson cjson-devel libatomic libicu-devel libxml2-devel openssl-devel +Requires: hilog python3-jinja2 python3-pyyaml cjson cjson-devel libatomic libicu-devel libxml2-devel openssl-devel bluez-devel %description Tools used by distributed middleware. @@ -115,6 +118,7 @@ rm -rf %{_builddir}/third_party/mbedtls/* %patch11 -p1 -d %{device_manager_dir} %patch12 -p1 -d %{device_security_level_dir} %patch13 -p1 -d %{dsoftbus_dir} +%patch14 -p1 -d %{dsoftbus_dir} # build all components with build.sh %build @@ -213,6 +217,11 @@ cp -rvf single_include/nlohmann %{buildroot}/%{_includedir}/nlohmann_json/single /usr/bin/* %changelog +* Mon Sep 18 2023 tianhang - 1.0.0-5 +- backport patches from openEuler Embedded to support Ble discovery +- fix discovery failure for Ble discovery +- add support for classic bluetooth connection + * Wed Jul 26 2023 Ge Wang - 1.0.0-4 - add deficient component. -- Gitee