mirror of https://github.com/labapart/gattlib
gattlib: Pass 'adapter' rather than MAC address to gattlib_connect()
parent
7107d9f5b4
commit
71c4e71952
|
@ -325,14 +325,23 @@ static void get_connection_options(unsigned long options, BtIOSecLevel *bt_io_se
|
|||
*mtu = GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_MTU(options);
|
||||
}
|
||||
|
||||
gatt_connection_t *gattlib_connect_async(const char *src, const char *dst,
|
||||
gatt_connection_t *gattlib_connect_async(void *adapter, const char *dst,
|
||||
unsigned long options,
|
||||
gatt_connect_cb_t connect_cb, void* data)
|
||||
{
|
||||
const char *adapter_mac_address;
|
||||
gatt_connection_t *conn;
|
||||
BtIOSecLevel bt_io_sec_level;
|
||||
int psm, mtu;
|
||||
|
||||
if (adapter != NULL) {
|
||||
fprintf(stderr, "Missing support");
|
||||
assert(0); // Need to add support
|
||||
return NULL;
|
||||
} else {
|
||||
adapter_mac_address = NULL;
|
||||
}
|
||||
|
||||
// Check parameters
|
||||
if ((options & (GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC | GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM)) == 0) {
|
||||
// Please, set GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC or
|
||||
|
@ -350,7 +359,7 @@ gatt_connection_t *gattlib_connect_async(const char *src, const char *dst,
|
|||
io_connect_arg->user_data = data;
|
||||
|
||||
if (options & GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC) {
|
||||
conn = initialize_gattlib_connection(src, dst, BDADDR_LE_PUBLIC, bt_io_sec_level,
|
||||
conn = initialize_gattlib_connection(adapter_mac_address, dst, BDADDR_LE_PUBLIC, bt_io_sec_level,
|
||||
psm, mtu, connect_cb, io_connect_arg);
|
||||
if (conn != NULL) {
|
||||
return conn;
|
||||
|
@ -358,7 +367,7 @@ gatt_connection_t *gattlib_connect_async(const char *src, const char *dst,
|
|||
}
|
||||
|
||||
if (options & GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM) {
|
||||
conn = initialize_gattlib_connection(src, dst, BDADDR_LE_RANDOM, bt_io_sec_level,
|
||||
conn = initialize_gattlib_connection(adapter_mac_address, dst, BDADDR_LE_RANDOM, bt_io_sec_level,
|
||||
psm, mtu, connect_cb, io_connect_arg);
|
||||
}
|
||||
|
||||
|
@ -431,12 +440,21 @@ static gatt_connection_t *gattlib_connect_with_options(const char *src, const ch
|
|||
* @param dst Remote Bluetooth address
|
||||
* @param options Options to connect to BLE device. See `GATTLIB_CONNECTION_OPTIONS_*`
|
||||
*/
|
||||
gatt_connection_t *gattlib_connect(const char *src, const char *dst, unsigned long options)
|
||||
gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long options)
|
||||
{
|
||||
const char* adapter_mac_address;
|
||||
gatt_connection_t *conn;
|
||||
BtIOSecLevel bt_io_sec_level;
|
||||
int psm, mtu;
|
||||
|
||||
if (adapter != NULL) {
|
||||
fprintf(stderr, "Missing support");
|
||||
assert(0); // Need to add support
|
||||
return NULL;
|
||||
} else {
|
||||
adapter_mac_address = NULL;
|
||||
}
|
||||
|
||||
// Check parameters
|
||||
if ((options & (GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC | GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM)) == 0) {
|
||||
// Please, set GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC or
|
||||
|
@ -448,14 +466,14 @@ gatt_connection_t *gattlib_connect(const char *src, const char *dst, unsigned lo
|
|||
get_connection_options(options, &bt_io_sec_level, &psm, &mtu);
|
||||
|
||||
if (options & GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC) {
|
||||
conn = gattlib_connect_with_options(src, dst, BDADDR_LE_PUBLIC, bt_io_sec_level, psm, mtu);
|
||||
conn = gattlib_connect_with_options(adapter_mac_address, dst, BDADDR_LE_PUBLIC, bt_io_sec_level, psm, mtu);
|
||||
if (conn != NULL) {
|
||||
return conn;
|
||||
}
|
||||
}
|
||||
|
||||
if (options & GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM) {
|
||||
conn = gattlib_connect_with_options(src, dst, BDADDR_LE_RANDOM, bt_io_sec_level, psm, mtu);
|
||||
conn = gattlib_connect_with_options(adapter_mac_address, dst, BDADDR_LE_RANDOM, bt_io_sec_level, psm, mtu);
|
||||
}
|
||||
|
||||
return conn;
|
||||
|
|
|
@ -126,12 +126,18 @@ void get_device_path_from_mac(const char *adapter_name, const char *mac_address,
|
|||
* @param psm Specify the PSM for GATT/ATT over BR/EDR
|
||||
* @param mtu Specify the MTU size
|
||||
*/
|
||||
gatt_connection_t *gattlib_connect(const char *src, const char *dst, unsigned long options)
|
||||
gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long options)
|
||||
{
|
||||
struct gattlib_adapter *gattlib_adapter = adapter;
|
||||
const char* adapter_name = NULL;
|
||||
GError *error = NULL;
|
||||
char object_path[100];
|
||||
|
||||
get_device_path_from_mac(src, dst, object_path, sizeof(object_path));
|
||||
if (gattlib_adapter != NULL) {
|
||||
adapter_name = gattlib_adapter->adapter_name;
|
||||
}
|
||||
|
||||
get_device_path_from_mac(adapter_name, dst, object_path, sizeof(object_path));
|
||||
|
||||
gattlib_context_t* conn_context = calloc(sizeof(gattlib_context_t), 1);
|
||||
if (conn_context == NULL) {
|
||||
|
@ -211,13 +217,13 @@ FREE_CONN_CONTEXT:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
gatt_connection_t *gattlib_connect_async(const char *src, const char *dst,
|
||||
gatt_connection_t *gattlib_connect_async(void *adapter, const char *dst,
|
||||
unsigned long options,
|
||||
gatt_connect_cb_t connect_cb, void* data)
|
||||
{
|
||||
gatt_connection_t *connection;
|
||||
|
||||
connection = gattlib_connect(src, dst, options);
|
||||
connection = gattlib_connect(adapter, dst, options);
|
||||
if ((connection != NULL) && (connect_cb != NULL)) {
|
||||
connect_cb(connection, data);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
*
|
||||
* GattLib - GATT Library
|
||||
*
|
||||
* Copyright (C) 2016-2019 Olivier Martin <olivier@labapart.org>
|
||||
* Copyright (C) 2016-2020 Olivier Martin <olivier@labapart.org>
|
||||
*
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
|
@ -45,6 +45,8 @@
|
|||
#endif
|
||||
|
||||
typedef struct {
|
||||
struct gattlib_adapter *adapter;
|
||||
|
||||
char* device_object_path;
|
||||
OrgBluezDevice1* device;
|
||||
|
||||
|
@ -57,6 +59,8 @@ typedef struct {
|
|||
|
||||
struct gattlib_adapter {
|
||||
OrgBluezAdapter1 *adapter_proxy;
|
||||
char* adapter_name;
|
||||
|
||||
GMainLoop *scan_loop;
|
||||
guint timeout_id;
|
||||
};
|
||||
|
|
|
@ -292,24 +292,24 @@ int gattlib_adapter_close(void* adapter);
|
|||
/**
|
||||
* @brief Function to connect to a BLE device
|
||||
*
|
||||
* @param src Local Adaptater interface
|
||||
* @param adapter Local Adaptater interface. When passing NULL, we use default adapter.
|
||||
* @param dst Remote Bluetooth address
|
||||
* @param options Options to connect to BLE device. See `GATTLIB_CONNECTION_OPTIONS_*`
|
||||
*/
|
||||
gatt_connection_t *gattlib_connect(const char *src, const char *dst, unsigned long options);
|
||||
gatt_connection_t *gattlib_connect(void *adapter, const char *dst, unsigned long options);
|
||||
|
||||
/**
|
||||
* @brief Function to asynchronously connect to a BLE device
|
||||
*
|
||||
* @note This function is mainly used before Bluez v5.42 (prior to D-BUS support)
|
||||
*
|
||||
* @param src Local Adaptater interface
|
||||
* @param adapter Local Adaptater interface. When passing NULL, we use default adapter.
|
||||
* @param dst Remote Bluetooth address
|
||||
* @param options Options to connect to BLE device. See `GATTLIB_CONNECTION_OPTIONS_*`
|
||||
* @param connect_cb is the callback to call when the connection is established
|
||||
* @param user_data is the user specific data to pass to the callback
|
||||
*/
|
||||
gatt_connection_t *gattlib_connect_async(const char *src, const char *dst,
|
||||
gatt_connection_t *gattlib_connect_async(void *adapter, const char *dst,
|
||||
unsigned long options,
|
||||
gatt_connect_cb_t connect_cb, void* user_data);
|
||||
|
||||
|
|
Loading…
Reference in New Issue