mirror of https://github.com/labapart/gattlib
gattlib: Added 'adapter' and 'user_data' parameter to 'gattlib_discovered_device_t' callback
parent
a52f66834a
commit
a42f76aa7d
|
@ -84,7 +84,7 @@ static char* parse_name(uint8_t* data, size_t size) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static int ble_scan(int device_desc, gattlib_discovered_device_t discovered_device_cb, int timeout) {
|
||||
static int ble_scan(void *adapter, int device_desc, gattlib_discovered_device_t discovered_device_cb, int timeout, void *user_data) {
|
||||
struct hci_filter old_options;
|
||||
socklen_t slen = sizeof(old_options);
|
||||
struct hci_filter new_options;
|
||||
|
@ -135,7 +135,7 @@ static int ble_scan(int device_desc, gattlib_discovered_device_t discovered_devi
|
|||
ba2str(&info->bdaddr, addr);
|
||||
|
||||
char* name = parse_name(info->data, info->length);
|
||||
discovered_device_cb(addr, name);
|
||||
discovered_device_cb(adapter, addr, name, user_data);
|
||||
if (name) {
|
||||
free(name);
|
||||
}
|
||||
|
@ -170,7 +170,7 @@ static int ble_scan(int device_desc, gattlib_discovered_device_t discovered_devi
|
|||
ba2str(&info->bdaddr, addr);
|
||||
|
||||
char* name = parse_name(info->data, info->length);
|
||||
discovered_device_cb(addr, name);
|
||||
discovered_device_cb(adapter, addr, name, user_data);
|
||||
if (name) {
|
||||
free(name);
|
||||
}
|
||||
|
@ -181,7 +181,7 @@ static int ble_scan(int device_desc, gattlib_discovered_device_t discovered_devi
|
|||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
|
||||
int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t discovered_device_cb, int timeout) {
|
||||
int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t discovered_device_cb, int timeout, void *user_data) {
|
||||
int device_desc = *(int*)adapter;
|
||||
|
||||
uint16_t interval = htobs(DISCOV_LE_SCAN_INT);
|
||||
|
@ -201,7 +201,7 @@ int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t disco
|
|||
return 1;
|
||||
}
|
||||
|
||||
ret = ble_scan(device_desc, discovered_device_cb, timeout);
|
||||
ret = ble_scan(adapter, device_desc, discovered_device_cb, timeout, user_data);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "ERROR: Advertisement fail.\n");
|
||||
return 1;
|
||||
|
@ -211,7 +211,7 @@ int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t disco
|
|||
}
|
||||
|
||||
int gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
gattlib_discovered_device_t discovered_device_cb, int timeout)
|
||||
gattlib_discovered_device_t discovered_device_cb, int timeout, void *user_data)
|
||||
{
|
||||
return GATTLIB_NOT_SUPPORTED;
|
||||
}
|
||||
|
|
|
@ -64,7 +64,9 @@ int gattlib_adapter_open(const char* adapter_name, void** adapter) {
|
|||
* Internal structure to pass to Device Manager signal handlers
|
||||
*/
|
||||
struct discovered_device_arg {
|
||||
void *adapter;
|
||||
gattlib_discovered_device_t callback;
|
||||
void *user_data;
|
||||
GSList** discovered_devices_ptr;
|
||||
};
|
||||
|
||||
|
@ -96,8 +98,10 @@ static void device_manager_on_device1_signal(const char* device1_path, struct di
|
|||
*arg->discovered_devices_ptr = g_slist_append(*arg->discovered_devices_ptr, g_strdup(address));
|
||||
|
||||
arg->callback(
|
||||
arg->adapter,
|
||||
org_bluez_device1_get_address(device1),
|
||||
org_bluez_device1_get_name(device1));
|
||||
org_bluez_device1_get_name(device1),
|
||||
arg->user_data);
|
||||
|
||||
g_object_unref(device1);
|
||||
}
|
||||
|
@ -136,7 +140,7 @@ on_interface_proxy_properties_changed (GDBusObjectManagerClient *device_manager,
|
|||
}
|
||||
|
||||
int gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
gattlib_discovered_device_t discovered_device_cb, int timeout)
|
||||
gattlib_discovered_device_t discovered_device_cb, int timeout, void *user_data)
|
||||
{
|
||||
GDBusObjectManager *device_manager;
|
||||
GError *error = NULL;
|
||||
|
@ -208,8 +212,10 @@ int gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid_list, i
|
|||
|
||||
// Pass the user callback and the discovered device list pointer to the signal handlers
|
||||
struct discovered_device_arg discovered_device_arg = {
|
||||
.adapter = adapter,
|
||||
.callback = discovered_device_cb,
|
||||
.discovered_devices_ptr = &discovered_devices
|
||||
.user_data = user_data,
|
||||
.discovered_devices_ptr = &discovered_devices,
|
||||
};
|
||||
|
||||
added_signal_id = g_signal_connect(G_DBUS_OBJECT_MANAGER(device_manager),
|
||||
|
@ -244,12 +250,12 @@ DISABLE_SCAN:
|
|||
return ret;
|
||||
}
|
||||
|
||||
int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t discovered_device_cb, int timeout)
|
||||
int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t discovered_device_cb, int timeout, void *user_data)
|
||||
{
|
||||
return gattlib_adapter_scan_enable_with_filter(adapter,
|
||||
NULL, 0 /* RSSI Threshold */,
|
||||
GATTLIB_DISCOVER_FILTER_USE_NONE,
|
||||
discovered_device_cb, timeout);
|
||||
discovered_device_cb, timeout, user_data);
|
||||
}
|
||||
|
||||
int gattlib_adapter_scan_disable(void* adapter) {
|
||||
|
|
|
@ -80,7 +80,7 @@ connection_exit:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static void ble_discovered_device(const char* addr, const char* name) {
|
||||
static void ble_discovered_device(void *adapter, const char* addr, const char* name, void *user_data) {
|
||||
struct connection_t *connection;
|
||||
int ret;
|
||||
|
||||
|
@ -129,7 +129,7 @@ int main(int argc, const char *argv[]) {
|
|||
}
|
||||
|
||||
pthread_mutex_lock(&g_mutex);
|
||||
ret = gattlib_adapter_scan_enable(adapter, ble_discovered_device, BLE_SCAN_TIMEOUT);
|
||||
ret = gattlib_adapter_scan_enable(adapter, ble_discovered_device, BLE_SCAN_TIMEOUT, NULL /* user_data */);
|
||||
if (ret) {
|
||||
fprintf(stderr, "ERROR: Failed to scan.\n");
|
||||
goto EXIT;
|
||||
|
|
|
@ -29,7 +29,7 @@ def connect_ble_device(device):
|
|||
device.disconnect()
|
||||
|
||||
|
||||
def on_discovered_ble_device(device):
|
||||
def on_discovered_ble_device(device, user_data):
|
||||
threading.Thread(target=connect_ble_device, args=(device,)).start()
|
||||
|
||||
|
||||
|
|
|
@ -54,13 +54,13 @@ class GattlibCharacteristic(Structure):
|
|||
gattlib_adapter_open = gattlib.gattlib_adapter_open
|
||||
gattlib_adapter_open.argtypes = [c_char_p, POINTER(c_void_p)]
|
||||
|
||||
# typedef void (*gattlib_discovered_device_t)(const char* addr, const char* name)
|
||||
gattlib_discovered_device_type = CFUNCTYPE(None, c_char_p, c_char_p)
|
||||
# typedef void (*gattlib_discovered_device_t)(void *adapter, const char* addr, const char* name, void *user_data)
|
||||
gattlib_discovered_device_type = CFUNCTYPE(None, c_void_p, c_char_p, c_char_p, c_void_p)
|
||||
|
||||
# int gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
# gattlib_discovered_device_t discovered_device_cb, int timeout)
|
||||
# gattlib_discovered_device_t discovered_device_cb, int timeout, void *user_data)
|
||||
gattlib_adapter_scan_enable_with_filter = gattlib.gattlib_adapter_scan_enable_with_filter
|
||||
gattlib_adapter_scan_enable_with_filter.argtypes = [c_void_p, POINTER(POINTER(GattlibUuid)), c_int16, c_uint32, gattlib_discovered_device_type, c_int]
|
||||
gattlib_adapter_scan_enable_with_filter.argtypes = [c_void_p, POINTER(POINTER(GattlibUuid)), c_int16, c_uint32, gattlib_discovered_device_type, c_int, c_void_p]
|
||||
|
||||
# int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_service_t** services, int* services_count);
|
||||
gattlib_discover_primary = gattlib.gattlib_discover_primary
|
||||
|
|
|
@ -27,11 +27,11 @@ class Adapter:
|
|||
def close(self):
|
||||
return gattlib.gattlib_adapter_close(self._adapter)
|
||||
|
||||
def on_discovered_device(self, addr, name):
|
||||
def on_discovered_device(self, adapter, addr, name, user_data):
|
||||
device = Device(self, addr, name)
|
||||
self.on_discovered_device_callback(device)
|
||||
self.on_discovered_device_callback(device, user_data)
|
||||
|
||||
def scan_enable(self, on_discovered_device_callback, timeout, uuids=None, rssi_threshold=None):
|
||||
def scan_enable(self, on_discovered_device_callback, timeout, uuids=None, rssi_threshold=None, user_data=None):
|
||||
assert on_discovered_device_callback != None
|
||||
self.on_discovered_device_callback = on_discovered_device_callback
|
||||
|
||||
|
@ -61,7 +61,8 @@ class Adapter:
|
|||
|
||||
ret = gattlib_adapter_scan_enable_with_filter(self._adapter,
|
||||
uuid_list, rssi, enabled_filters,
|
||||
gattlib_discovered_device_type(self.on_discovered_device), timeout)
|
||||
gattlib_discovered_device_type(self.on_discovered_device),
|
||||
timeout, user_data)
|
||||
handle_return(ret)
|
||||
|
||||
def scan_disable(self):
|
||||
|
|
|
@ -105,7 +105,22 @@ typedef void (*gattlib_event_handler_t)(const uuid_t* uuid, const uint8_t* data,
|
|||
*/
|
||||
typedef void (*gattlib_disconnection_handler_t)(void* user_data);
|
||||
|
||||
typedef void (*gattlib_discovered_device_t)(const char* addr, const char* name);
|
||||
/**
|
||||
* @brief Handler called on new discovered BLE device
|
||||
*
|
||||
* @param adapter is the adapter that has found the BLE device
|
||||
* @param addr is the MAC address of the BLE device
|
||||
* @param name is the name of BLE device if advertised
|
||||
* @param user_data Data defined when calling `gattlib_register_on_disconnect()`
|
||||
*/
|
||||
typedef void (*gattlib_discovered_device_t)(void *adapter, const char* addr, const char* name, void *user_data);
|
||||
|
||||
/**
|
||||
* @brief Handler called on asynchronous connection when connection is ready
|
||||
*
|
||||
* @param connection Connection that is disconnecting
|
||||
* @param user_data Data defined when calling `gattlib_register_on_disconnect()`
|
||||
*/
|
||||
typedef void (*gatt_connect_cb_t)(gatt_connection_t* connection, void* user_data);
|
||||
|
||||
/**
|
||||
|
@ -133,10 +148,11 @@ int gattlib_adapter_open(const char* adapter_name, void** adapter);
|
|||
* @param adapter is the context of the newly opened adapter
|
||||
* @param discovered_device_cb is the function callback called for each new Bluetooth device discovered
|
||||
* @param timeout defines the duration of the Bluetooth scanning
|
||||
* @param user_data is the data passed to the callback `discovered_device_cb()`
|
||||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t discovered_device_cb, int timeout);
|
||||
int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t discovered_device_cb, int timeout, void *user_data);
|
||||
|
||||
/**
|
||||
* @brief Enable Bluetooth scanning on a given adapter
|
||||
|
@ -149,11 +165,12 @@ int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t disco
|
|||
* GATTLIB_DISCOVER_FILTER_USE_UUID and GATTLIB_DISCOVER_FILTER_USE_RSSI.
|
||||
* @param discovered_device_cb is the function callback called for each new Bluetooth device discovered
|
||||
* @param timeout defines the duration of the Bluetooth scanning
|
||||
* @param user_data is the data passed to the callback `discovered_device_cb()`
|
||||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
gattlib_discovered_device_t discovered_device_cb, int timeout);
|
||||
gattlib_discovered_device_t discovered_device_cb, int timeout, void *user_data);
|
||||
|
||||
/**
|
||||
* @brief Disable Bluetooth scanning on a given adapter
|
||||
|
|
Loading…
Reference in New Issue