mirror of https://github.com/labapart/gattlib
Refactor code to better separate gattlib_adapter_t, gattlib_device_t, gattlib_connection_t and their backends
parent
22dca4511c
commit
014c2802ee
|
@ -107,7 +107,7 @@ static char* parse_name(uint8_t* data, size_t size) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static int ble_scan(void *adapter, int device_desc, gattlib_discovered_device_t discovered_device_cb, int timeout, void *user_data) {
|
||||
static int ble_scan(gattlib_adapter_t* 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;
|
||||
|
@ -212,7 +212,7 @@ static int ble_scan(void *adapter, int device_desc, gattlib_discovered_device_t
|
|||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
|
||||
int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data) {
|
||||
int gattlib_adapter_scan_enable(gattlib_adapter_t* adapter, gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data) {
|
||||
int device_desc = *(int*)adapter;
|
||||
|
||||
uint16_t interval = htobs(DISCOV_LE_SCAN_INT);
|
||||
|
@ -241,13 +241,13 @@ int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t disco
|
|||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
|
||||
int gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
int gattlib_adapter_scan_enable_with_filter(gattlib_adapter_t* adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data)
|
||||
{
|
||||
return GATTLIB_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
int gattlib_adapter_scan_disable(void* adapter) {
|
||||
int gattlib_adapter_scan_disable(gattlib_adapter_t* adapter) {
|
||||
int device_desc = *(int*)adapter;
|
||||
|
||||
if (device_desc == -1) {
|
||||
|
@ -262,7 +262,7 @@ int gattlib_adapter_scan_disable(void* adapter) {
|
|||
return result;
|
||||
}
|
||||
|
||||
int gattlib_adapter_close(void* adapter) {
|
||||
int gattlib_adapter_close(gattlib_adapter_t* adapter) {
|
||||
hci_close_dev(*(int*)adapter);
|
||||
free(adapter);
|
||||
return GATTLIB_SUCCESS;
|
||||
|
|
|
@ -41,7 +41,7 @@
|
|||
struct gattlib_thread_t g_gattlib_thread = { 0 };
|
||||
|
||||
typedef struct {
|
||||
gatt_connection_t* conn;
|
||||
gattlib_connection_t* conn;
|
||||
gatt_connect_cb_t connect_cb;
|
||||
int connected;
|
||||
int timeout;
|
||||
|
@ -50,7 +50,7 @@ typedef struct {
|
|||
} io_connect_arg_t;
|
||||
|
||||
static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data) {
|
||||
gatt_connection_t *conn = user_data;
|
||||
gattlib_connection_t *conn = user_data;
|
||||
uint8_t opdu[ATT_MAX_MTU];
|
||||
uint16_t handle, olen = 0;
|
||||
uuid_t uuid = {};
|
||||
|
@ -98,7 +98,7 @@ static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
|
|||
}
|
||||
|
||||
static gboolean io_listen_cb(gpointer user_data) {
|
||||
gatt_connection_t *conn = user_data;
|
||||
gattlib_connection_t *conn = user_data;
|
||||
gattlib_context_t* conn_context = conn->context;
|
||||
|
||||
g_attrib_register(conn_context->attrib, ATT_OP_HANDLE_NOTIFY,
|
||||
|
@ -178,7 +178,7 @@ static void *connection_thread(void* arg) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static gatt_connection_t *initialize_gattlib_connection(const gchar *src, const gchar *dst,
|
||||
static gattlib_connection_t *initialize_gattlib_connection(const gchar *src, const gchar *dst,
|
||||
uint8_t dest_type, BtIOSecLevel sec_level, int psm, int mtu,
|
||||
gatt_connect_cb_t connect_cb,
|
||||
io_connect_arg_t* io_connect_arg)
|
||||
|
@ -250,7 +250,7 @@ static gatt_connection_t *initialize_gattlib_connection(const gchar *src, const
|
|||
return NULL;
|
||||
}
|
||||
|
||||
gatt_connection_t* conn = calloc(sizeof(gatt_connection_t), 1);
|
||||
gattlib_connection_t* conn = calloc(sizeof(gattlib_connection_t), 1);
|
||||
if (conn == NULL) {
|
||||
free(conn_context);
|
||||
return NULL;
|
||||
|
@ -325,13 +325,13 @@ static void get_connection_options(unsigned long options, BtIOSecLevel *bt_io_se
|
|||
*mtu = GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_MTU(options);
|
||||
}
|
||||
|
||||
int gattlib_connect(void *adapter, const char *dst,
|
||||
int gattlib_connect(gattlib_adapter_t* adapter, const char *dst,
|
||||
unsigned long options,
|
||||
gatt_connect_cb_t connect_cb,
|
||||
void* user_data)
|
||||
{
|
||||
const char *adapter_mac_address;
|
||||
gatt_connection_t *conn;
|
||||
gattlib_connection_t *conn;
|
||||
BtIOSecLevel bt_io_sec_level;
|
||||
int psm, mtu;
|
||||
|
||||
|
@ -393,11 +393,11 @@ static gboolean connection_timeout(gpointer user_data) {
|
|||
* @param psm Specify the PSM for GATT/ATT over BR/EDR
|
||||
* @param mtu Specify the MTU size
|
||||
*/
|
||||
static gatt_connection_t *gattlib_connect_with_options(const char *src, const char *dst,
|
||||
static gattlib_connection_t *gattlib_connect_with_options(const char *src, const char *dst,
|
||||
uint8_t dest_type, BtIOSecLevel bt_io_sec_level, int psm, int mtu)
|
||||
{
|
||||
GSource* timeout;
|
||||
gatt_connection_t *conn;
|
||||
gattlib_connection_t *conn;
|
||||
io_connect_arg_t io_connect_arg;
|
||||
|
||||
conn = initialize_gattlib_connection(src, dst, dest_type, bt_io_sec_level,
|
||||
|
@ -444,10 +444,10 @@ 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(void* adapter, const char *dst, unsigned long options)
|
||||
gattlib_connection_t *gattlib_connect(gattlib_adapter_t* adapter, const char *dst, unsigned long options)
|
||||
{
|
||||
const char* adapter_mac_address;
|
||||
gatt_connection_t *conn;
|
||||
gattlib_connection_t *conn;
|
||||
BtIOSecLevel bt_io_sec_level;
|
||||
int psm, mtu;
|
||||
|
||||
|
@ -483,7 +483,7 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
|
|||
return conn;
|
||||
}
|
||||
|
||||
int gattlib_disconnect(gatt_connection_t* connection, bool wait_disconnection) {
|
||||
int gattlib_disconnect(gattlib_connection_t* connection, bool wait_disconnection) {
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
|
||||
#if BLUEZ_VERSION_MAJOR == 4
|
||||
|
@ -546,7 +546,7 @@ GSource* gattlib_timeout_add_seconds(guint interval, GSourceFunc function, gpoin
|
|||
return source;
|
||||
}
|
||||
|
||||
int get_uuid_from_handle(gatt_connection_t* connection, uint16_t handle, uuid_t* uuid) {
|
||||
int get_uuid_from_handle(gattlib_connection_t* connection, uint16_t handle, uuid_t* uuid) {
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
int i;
|
||||
|
||||
|
@ -559,7 +559,7 @@ int get_uuid_from_handle(gatt_connection_t* connection, uint16_t handle, uuid_t*
|
|||
return GATTLIB_NOT_FOUND;
|
||||
}
|
||||
|
||||
int get_handle_from_uuid(gatt_connection_t* connection, const uuid_t* uuid, uint16_t* handle) {
|
||||
int get_handle_from_uuid(gattlib_connection_t* connection, const uuid_t* uuid, uint16_t* handle) {
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
int i;
|
||||
|
||||
|
@ -573,13 +573,13 @@ int get_handle_from_uuid(gatt_connection_t* connection, const uuid_t* uuid, uint
|
|||
}
|
||||
|
||||
#if 0 // Disable until https://github.com/labapart/gattlib/issues/75 is resolved
|
||||
int gattlib_get_rssi(gatt_connection_t *connection, int16_t *rssi)
|
||||
int gattlib_get_rssi(gattlib_connection_t *connection, int16_t *rssi)
|
||||
{
|
||||
return GATTLIB_NOT_SUPPORTED;
|
||||
}
|
||||
#endif
|
||||
|
||||
int gattlib_get_rssi_from_mac(void *adapter, const char *mac_address, int16_t *rssi)
|
||||
int gattlib_get_rssi_from_mac(gattlib_adapter_t* adapter, const char *mac_address, int16_t *rssi)
|
||||
{
|
||||
return GATTLIB_NOT_SUPPORTED;
|
||||
}
|
||||
|
|
|
@ -74,7 +74,7 @@ done:
|
|||
data->discovered = TRUE;
|
||||
}
|
||||
|
||||
int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_service_t** services, int* services_count) {
|
||||
int gattlib_discover_primary(gattlib_connection_t* connection, gattlib_primary_service_t** services, int* services_count) {
|
||||
struct primary_all_cb_t user_data;
|
||||
guint ret;
|
||||
|
||||
|
@ -146,7 +146,7 @@ done:
|
|||
data->discovered = TRUE;
|
||||
}
|
||||
|
||||
int gattlib_discover_char_range(gatt_connection_t* connection, uint16_t start, uint16_t end, gattlib_characteristic_t** characteristics, int* characteristics_count) {
|
||||
int gattlib_discover_char_range(gattlib_connection_t* connection, uint16_t start, uint16_t end, gattlib_characteristic_t** characteristics, int* characteristics_count) {
|
||||
struct characteristic_cb_t user_data;
|
||||
guint ret;
|
||||
|
||||
|
@ -170,7 +170,7 @@ int gattlib_discover_char_range(gatt_connection_t* connection, uint16_t start, u
|
|||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
|
||||
int gattlib_discover_char(gatt_connection_t* connection, gattlib_characteristic_t** characteristics, int* characteristics_count) {
|
||||
int gattlib_discover_char(gattlib_connection_t* connection, gattlib_characteristic_t** characteristics, int* characteristics_count) {
|
||||
return gattlib_discover_char_range(connection, 0x0001, 0xffff, characteristics, characteristics_count);
|
||||
}
|
||||
|
||||
|
@ -264,7 +264,7 @@ done:
|
|||
}
|
||||
#endif
|
||||
|
||||
int gattlib_discover_desc_range(gatt_connection_t* connection, int start, int end, gattlib_descriptor_t** descriptors, int* descriptor_count) {
|
||||
int gattlib_discover_desc_range(gattlib_connection_t* connection, int start, int end, gattlib_descriptor_t** descriptors, int* descriptor_count) {
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
struct descriptor_cb_t descriptor_data;
|
||||
guint ret;
|
||||
|
@ -292,7 +292,7 @@ int gattlib_discover_desc_range(gatt_connection_t* connection, int start, int en
|
|||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
|
||||
int gattlib_discover_desc(gatt_connection_t* connection, gattlib_descriptor_t** descriptors, int* descriptor_count) {
|
||||
int gattlib_discover_desc(gattlib_connection_t* connection, gattlib_descriptor_t** descriptors, int* descriptor_count) {
|
||||
return gattlib_discover_desc_range(connection, 0x0001, 0xffff, descriptors, descriptor_count);
|
||||
}
|
||||
|
||||
|
@ -309,7 +309,7 @@ int gattlib_discover_desc(gatt_connection_t* connection, gattlib_descriptor_t**
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_get_advertisement_data(gatt_connection_t *connection,
|
||||
int gattlib_get_advertisement_data(gattlib_connection_t *connection,
|
||||
gattlib_advertisement_data_t **advertisement_data, size_t *advertisement_data_count,
|
||||
uint16_t *manufacturer_id, uint8_t **manufacturer_data, size_t *manufacturer_data_size)
|
||||
{
|
||||
|
@ -329,7 +329,7 @@ int gattlib_get_advertisement_data(gatt_connection_t *connection,
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_get_advertisement_data_from_mac(void *adapter, const char *mac_address,
|
||||
int gattlib_get_advertisement_data_from_mac(gattlib_adapter_t* adapter, const char *mac_address,
|
||||
gattlib_advertisement_data_t **advertisement_data, size_t *advertisement_data_count,
|
||||
uint16_t *manufacturer_id, uint8_t **manufacturer_data, size_t *manufacturer_data_size)
|
||||
{
|
||||
|
|
|
@ -69,7 +69,7 @@ GSource* gattlib_timeout_add_seconds(guint interval, GSourceFunc function, gpoin
|
|||
void uuid_to_bt_uuid(uuid_t* uuid, bt_uuid_t* bt_uuid);
|
||||
void bt_uuid_to_uuid(bt_uuid_t* bt_uuid, uuid_t* uuid);
|
||||
|
||||
int get_uuid_from_handle(gatt_connection_t* connection, uint16_t handle, uuid_t* uuid);
|
||||
int get_handle_from_uuid(gatt_connection_t* connection, const uuid_t* uuid, uint16_t* handle);
|
||||
int get_uuid_from_handle(gattlib_connection_t* connection, uint16_t handle, uuid_t* uuid);
|
||||
int get_handle_from_uuid(gattlib_connection_t* connection, const uuid_t* uuid, uint16_t* handle);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -102,7 +102,7 @@ void uuid_to_bt_uuid(uuid_t* uuid, bt_uuid_t* bt_uuid) {
|
|||
}
|
||||
}
|
||||
|
||||
int gattlib_read_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid,
|
||||
int gattlib_read_char_by_uuid(gattlib_connection_t* connection, uuid_t* uuid,
|
||||
void **buffer, size_t* buffer_len)
|
||||
{
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
|
@ -134,7 +134,7 @@ int gattlib_read_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid,
|
|||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
|
||||
int gattlib_read_char_by_uuid_async(gatt_connection_t* connection, uuid_t* uuid,
|
||||
int gattlib_read_char_by_uuid_async(gattlib_connection_t* connection, uuid_t* uuid,
|
||||
gatt_read_cb_t gatt_read_cb)
|
||||
{
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
|
@ -170,7 +170,7 @@ void gattlib_write_result_cb(guint8 status, const guint8 *pdu, guint16 len, gpoi
|
|||
*write_completed = TRUE;
|
||||
}
|
||||
|
||||
int gattlib_write_char_by_handle(gatt_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len) {
|
||||
int gattlib_write_char_by_handle(gattlib_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len) {
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
int write_completed = FALSE;
|
||||
|
||||
|
@ -187,7 +187,7 @@ int gattlib_write_char_by_handle(gatt_connection_t* connection, uint16_t handle,
|
|||
return 0;
|
||||
}
|
||||
|
||||
int gattlib_write_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len) {
|
||||
int gattlib_write_char_by_uuid(gattlib_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len) {
|
||||
uint16_t handle = 0;
|
||||
int ret;
|
||||
|
||||
|
@ -200,19 +200,19 @@ int gattlib_write_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, cons
|
|||
return gattlib_write_char_by_handle(connection, handle, buffer, buffer_len);
|
||||
}
|
||||
|
||||
int gattlib_write_without_response_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len)
|
||||
int gattlib_write_without_response_char_by_uuid(gattlib_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len)
|
||||
{
|
||||
// Only supported in the DBUS API (ie: Bluez > v5.40) at the moment
|
||||
return GATTLIB_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
int gattlib_write_without_response_char_by_handle(gatt_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len)
|
||||
int gattlib_write_without_response_char_by_handle(gattlib_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len)
|
||||
{
|
||||
// Only supported in the DBUS API (ie: Bluez > v5.40) at the moment
|
||||
return GATTLIB_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
int gattlib_notification_start(gatt_connection_t* connection, const uuid_t* uuid) {
|
||||
int gattlib_notification_start(gattlib_connection_t* connection, const uuid_t* uuid) {
|
||||
uint16_t handle;
|
||||
uint16_t enable_notification = 0x0001;
|
||||
|
||||
|
@ -225,7 +225,7 @@ int gattlib_notification_start(gatt_connection_t* connection, const uuid_t* uuid
|
|||
return gattlib_write_char_by_handle(connection, handle + 1, &enable_notification, sizeof(enable_notification));
|
||||
}
|
||||
|
||||
int gattlib_notification_stop(gatt_connection_t* connection, const uuid_t* uuid) {
|
||||
int gattlib_notification_stop(gattlib_connection_t* connection, const uuid_t* uuid) {
|
||||
uint16_t handle;
|
||||
uint16_t enable_notification = 0x0000;
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include "gattlib_internal.h"
|
||||
|
||||
#if defined(WITH_PYTHON)
|
||||
void gattlib_connected_device_python_callback(void *adapter, const char *dst, gatt_connection_t* connection, int error, void* user_data) {
|
||||
void gattlib_connected_device_python_callback(gattlib_adapter_t* adapter, const char *dst, gattlib_connection_t* connection, int error, void* user_data) {
|
||||
struct gattlib_python_args* args = user_data;
|
||||
PyObject *result;
|
||||
|
||||
|
@ -17,7 +17,7 @@ void gattlib_connected_device_python_callback(void *adapter, const char *dst, ga
|
|||
|
||||
const char* argument_string;
|
||||
// We pass pointer into integer/long parameter. We need to check the address size of the platform
|
||||
// arguments: (void *adapter, const char *dst, gatt_connection_t* connection, void* user_data)
|
||||
// arguments: (gattlib_adapter_t* adapter, const char *dst, gattlib_connection_t* connection, void* user_data)
|
||||
if (sizeof(void*) == 8) {
|
||||
argument_string = "(LsLIO)";
|
||||
} else {
|
||||
|
@ -47,12 +47,11 @@ ON_ERROR:
|
|||
#endif
|
||||
|
||||
static gpointer _gattlib_connected_device_thread(gpointer data) {
|
||||
gatt_connection_t* connection = data;
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
const gchar *device_mac_address = org_bluez_device1_get_address(conn_context->device);
|
||||
gattlib_connection_t* connection = data;
|
||||
const gchar *device_mac_address = org_bluez_device1_get_address(connection->backend.device);
|
||||
|
||||
// Mutex to ensure the device is valid and not freed during its use
|
||||
g_mutex_lock(&connection->device_mutex);
|
||||
g_mutex_lock(&connection->device->device_mutex);
|
||||
// Mutex to ensure the handler is valid
|
||||
g_rec_mutex_lock(&connection->on_connection.mutex);
|
||||
|
||||
|
@ -61,21 +60,21 @@ static gpointer _gattlib_connected_device_thread(gpointer data) {
|
|||
}
|
||||
|
||||
connection->on_connection.callback.connection_handler(
|
||||
conn_context->adapter, device_mac_address, connection, 0 /* no error */,
|
||||
connection->device->adapter, device_mac_address, connection, 0 /* no error */,
|
||||
connection->on_connection.user_data);
|
||||
|
||||
EXIT:
|
||||
g_rec_mutex_unlock(&connection->on_connection.mutex);
|
||||
g_mutex_unlock(&connection->device_mutex);
|
||||
g_mutex_unlock(&connection->device->device_mutex);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void* _connected_device_thread_args_allocator(va_list args) {
|
||||
gatt_connection_t* connection = va_arg(args, gatt_connection_t*);
|
||||
gattlib_connection_t* connection = va_arg(args, gattlib_connection_t*);
|
||||
return connection;
|
||||
}
|
||||
|
||||
void gattlib_on_connected_device(gatt_connection_t* connection) {
|
||||
void gattlib_on_connected_device(gattlib_connection_t* connection) {
|
||||
gattlib_handler_dispatch_to_thread(
|
||||
&connection->on_connection,
|
||||
#if defined(WITH_PYTHON)
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include "gattlib_internal.h"
|
||||
|
||||
#if defined(WITH_PYTHON)
|
||||
void gattlib_disconnected_device_python_callback(gatt_connection_t* connection, void *user_data) {
|
||||
void gattlib_disconnected_device_python_callback(gattlib_connection_t* connection, void *user_data) {
|
||||
struct gattlib_python_args* args = user_data;
|
||||
PyObject *result;
|
||||
PyGILState_STATE d_gstate;
|
||||
|
@ -30,7 +30,7 @@ void gattlib_disconnected_device_python_callback(gatt_connection_t* connection,
|
|||
}
|
||||
#endif
|
||||
|
||||
void gattlib_on_disconnected_device(gatt_connection_t* connection) {
|
||||
void gattlib_on_disconnected_device(gattlib_connection_t* connection) {
|
||||
if (gattlib_has_valid_handler(&connection->on_disconnection)) {
|
||||
g_rec_mutex_lock(&connection->on_disconnection.mutex);
|
||||
|
||||
|
@ -49,10 +49,10 @@ void gattlib_on_disconnected_device(gatt_connection_t* connection) {
|
|||
}
|
||||
|
||||
// Signal the device is now disconnected
|
||||
g_mutex_lock(&connection->device_mutex);
|
||||
g_mutex_lock(&connection->device->device_mutex);
|
||||
connection->disconnection_wait.value = true;
|
||||
g_cond_broadcast(&connection->disconnection_wait.condition);
|
||||
g_mutex_unlock(&connection->device_mutex);
|
||||
g_mutex_unlock(&connection->device->device_mutex);
|
||||
|
||||
// Clean GATTLIB connection on disconnection
|
||||
gattlib_connection_free(connection);
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include "gattlib_internal.h"
|
||||
|
||||
#if defined(WITH_PYTHON)
|
||||
void gattlib_discovered_device_python_callback(void *adapter, const char* addr, const char* name, void *user_data) {
|
||||
void gattlib_discovered_device_python_callback(gattlib_adapter_t* adapter, const char* addr, const char* name, void *user_data) {
|
||||
struct gattlib_python_args* args = user_data;
|
||||
PyObject *result;
|
||||
|
||||
|
@ -46,7 +46,7 @@ ON_ERROR:
|
|||
#endif
|
||||
|
||||
struct gattlib_discovered_device_thread_args {
|
||||
struct gattlib_adapter* gattlib_adapter;
|
||||
struct _gattlib_adapter* gattlib_adapter;
|
||||
char* mac_address;
|
||||
char* name;
|
||||
OrgBluezDevice1* device1;
|
||||
|
@ -55,20 +55,20 @@ struct gattlib_discovered_device_thread_args {
|
|||
static gpointer _gattlib_discovered_device_thread(gpointer data) {
|
||||
struct gattlib_discovered_device_thread_args* args = data;
|
||||
|
||||
g_rec_mutex_lock(&args->gattlib_adapter->ble_scan.discovered_device_callback.mutex);
|
||||
g_rec_mutex_lock(&args->gattlib_adapter->discovered_device_callback.mutex);
|
||||
|
||||
if (!gattlib_has_valid_handler(&args->gattlib_adapter->ble_scan.discovered_device_callback)) {
|
||||
if (!gattlib_has_valid_handler(&args->gattlib_adapter->discovered_device_callback)) {
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
args->gattlib_adapter->ble_scan.discovered_device_callback.callback.discovered_device(
|
||||
args->gattlib_adapter->discovered_device_callback.callback.discovered_device(
|
||||
args->gattlib_adapter,
|
||||
args->mac_address, args->name,
|
||||
args->gattlib_adapter->ble_scan.discovered_device_callback.user_data
|
||||
args->gattlib_adapter->discovered_device_callback.user_data
|
||||
);
|
||||
|
||||
EXIT:
|
||||
g_rec_mutex_unlock(&args->gattlib_adapter->ble_scan.discovered_device_callback.mutex);
|
||||
g_rec_mutex_unlock(&args->gattlib_adapter->discovered_device_callback.mutex);
|
||||
|
||||
free(args->mac_address);
|
||||
if (args->name != NULL) {
|
||||
|
@ -80,7 +80,7 @@ EXIT:
|
|||
}
|
||||
|
||||
static void* _discovered_device_thread_args_allocator(va_list args) {
|
||||
struct gattlib_adapter* gattlib_adapter = va_arg(args, struct gattlib_adapter*);
|
||||
gattlib_adapter_t* gattlib_adapter = va_arg(args, gattlib_adapter_t*);
|
||||
OrgBluezDevice1* device1 = va_arg(args, OrgBluezDevice1*);
|
||||
|
||||
struct gattlib_discovered_device_thread_args* thread_args = calloc(sizeof(struct gattlib_discovered_device_thread_args), 1);
|
||||
|
@ -95,9 +95,9 @@ static void* _discovered_device_thread_args_allocator(va_list args) {
|
|||
return thread_args;
|
||||
}
|
||||
|
||||
void gattlib_on_discovered_device(struct gattlib_adapter* gattlib_adapter, OrgBluezDevice1* device1) {
|
||||
void gattlib_on_discovered_device(gattlib_adapter_t* gattlib_adapter, OrgBluezDevice1* device1) {
|
||||
gattlib_handler_dispatch_to_thread(
|
||||
&gattlib_adapter->ble_scan.discovered_device_callback,
|
||||
&gattlib_adapter->discovered_device_callback,
|
||||
#if defined(WITH_PYTHON)
|
||||
gattlib_discovered_device_python_callback /* python_callback */,
|
||||
#else
|
||||
|
|
|
@ -46,7 +46,7 @@ void gattlib_notification_device_python_callback(const uuid_t* uuid, const uint8
|
|||
#endif
|
||||
|
||||
struct gattlib_notification_device_thread_args {
|
||||
gatt_connection_t* connection;
|
||||
gattlib_connection_t* connection;
|
||||
uuid_t* uuid;
|
||||
uint8_t* data;
|
||||
size_t data_length;
|
||||
|
@ -75,7 +75,7 @@ void gattlib_notification_device_thread(gpointer data, gpointer user_data) {
|
|||
}
|
||||
}
|
||||
|
||||
static void* _notification_device_thread_args_allocator(gatt_connection_t* connection, const uuid_t* uuid, const uint8_t* data, size_t data_length) {
|
||||
static void* _notification_device_thread_args_allocator(gattlib_connection_t* connection, const uuid_t* uuid, const uint8_t* data, size_t data_length) {
|
||||
struct gattlib_notification_device_thread_args* thread_args = calloc(sizeof(struct gattlib_notification_device_thread_args), 1);
|
||||
thread_args->connection = connection;
|
||||
thread_args->uuid = calloc(sizeof(uuid_t), 1);
|
||||
|
@ -91,7 +91,7 @@ static void* _notification_device_thread_args_allocator(gatt_connection_t* conne
|
|||
return thread_args;
|
||||
}
|
||||
|
||||
void gattlib_on_gatt_notification(gatt_connection_t* connection, const uuid_t* uuid, const uint8_t* data, size_t data_length) {
|
||||
void gattlib_on_gatt_notification(gattlib_connection_t* connection, const uuid_t* uuid, const uint8_t* data, size_t data_length) {
|
||||
GError *error = NULL;
|
||||
|
||||
assert(connection->notification.thread_pool != NULL);
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
#include "gattlib_internal.h"
|
||||
|
||||
int gattlib_register_notification(gatt_connection_t* connection, gattlib_event_handler_t notification_handler, void* user_data) {
|
||||
int gattlib_register_notification(gattlib_connection_t* connection, gattlib_event_handler_t notification_handler, void* user_data) {
|
||||
GError *error = NULL;
|
||||
|
||||
if (connection == NULL) {
|
||||
|
@ -32,7 +32,7 @@ int gattlib_register_notification(gatt_connection_t* connection, gattlib_event_h
|
|||
}
|
||||
}
|
||||
|
||||
int gattlib_register_indication(gatt_connection_t* connection, gattlib_event_handler_t indication_handler, void* user_data) {
|
||||
int gattlib_register_indication(gattlib_connection_t* connection, gattlib_event_handler_t indication_handler, void* user_data) {
|
||||
GError *error = NULL;
|
||||
|
||||
if (connection == NULL) {
|
||||
|
@ -54,7 +54,7 @@ int gattlib_register_indication(gatt_connection_t* connection, gattlib_event_han
|
|||
}
|
||||
}
|
||||
|
||||
int gattlib_register_on_disconnect(gatt_connection_t *connection, gattlib_disconnection_handler_t handler, void* user_data) {
|
||||
int gattlib_register_on_disconnect(gattlib_connection_t *connection, gattlib_disconnection_handler_t handler, void* user_data) {
|
||||
if (connection == NULL) {
|
||||
return GATTLIB_INVALID_PARAMETER;
|
||||
}
|
||||
|
|
|
@ -15,55 +15,52 @@ const char* device_state_str[] = {
|
|||
};
|
||||
|
||||
static gint _compare_device_with_device_id(gconstpointer a, gconstpointer b) {
|
||||
const struct _gattlib_device* device = a;
|
||||
const gattlib_device_t* device = a;
|
||||
const char* device_id = b;
|
||||
|
||||
return g_ascii_strcasecmp(device->device_id, device_id);
|
||||
}
|
||||
|
||||
static GSList* _find_device_with_device_id(struct gattlib_adapter* adapter, const char* device_id) {
|
||||
static GSList* _find_device_with_device_id(gattlib_adapter_t* adapter, const char* device_id) {
|
||||
return g_slist_find_custom(adapter->devices, device_id, _compare_device_with_device_id);
|
||||
}
|
||||
|
||||
struct _gattlib_device* gattlib_device_get_device(void* adapter, const char* device_id) {
|
||||
struct gattlib_adapter* gattlib_adapter = adapter;
|
||||
struct _gattlib_device* device = NULL;
|
||||
gattlib_device_t* gattlib_device_get_device(gattlib_adapter_t* adapter, const char* device_id) {
|
||||
gattlib_device_t* device = NULL;
|
||||
|
||||
g_rec_mutex_lock(&gattlib_adapter->mutex);
|
||||
g_rec_mutex_lock(&adapter->mutex);
|
||||
|
||||
GSList *item = _find_device_with_device_id(gattlib_adapter, device_id);
|
||||
GSList *item = _find_device_with_device_id(adapter, device_id);
|
||||
if (item == NULL) {
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
device = (struct _gattlib_device*)item->data;
|
||||
device = (gattlib_device_t*)item->data;
|
||||
|
||||
EXIT:
|
||||
g_rec_mutex_unlock(&gattlib_adapter->mutex);
|
||||
g_rec_mutex_unlock(&adapter->mutex);
|
||||
return device;
|
||||
}
|
||||
|
||||
enum _gattlib_device_state gattlib_device_get_state(void* adapter, const char* device_id) {
|
||||
struct gattlib_adapter* gattlib_adapter = adapter;
|
||||
enum _gattlib_device_state gattlib_device_get_state(gattlib_adapter_t* adapter, const char* device_id) {
|
||||
enum _gattlib_device_state state = NOT_FOUND;
|
||||
|
||||
g_rec_mutex_lock(&gattlib_adapter->mutex);
|
||||
g_rec_mutex_lock(&adapter->mutex);
|
||||
|
||||
struct _gattlib_device* device = gattlib_device_get_device(adapter, device_id);
|
||||
gattlib_device_t* device = gattlib_device_get_device(adapter, device_id);
|
||||
if (device != NULL) {
|
||||
state = device->state;
|
||||
}
|
||||
|
||||
g_rec_mutex_unlock(&gattlib_adapter->mutex);
|
||||
g_rec_mutex_unlock(&adapter->mutex);
|
||||
return state;
|
||||
}
|
||||
|
||||
int gattlib_device_set_state(void* adapter, const char* device_id, enum _gattlib_device_state new_state) {
|
||||
struct gattlib_adapter* gattlib_adapter = adapter;
|
||||
int gattlib_device_set_state(gattlib_adapter_t* adapter, const char* device_id, enum _gattlib_device_state new_state) {
|
||||
enum _gattlib_device_state old_state;
|
||||
int ret = GATTLIB_SUCCESS;
|
||||
|
||||
g_rec_mutex_lock(&gattlib_adapter->mutex);
|
||||
g_rec_mutex_lock(&adapter->mutex);
|
||||
|
||||
old_state = gattlib_device_get_state(adapter, device_id);
|
||||
if (old_state == NOT_FOUND) {
|
||||
|
@ -71,7 +68,7 @@ int gattlib_device_set_state(void* adapter, const char* device_id, enum _gattlib
|
|||
// The device does not exist yet
|
||||
//
|
||||
if (new_state != NOT_FOUND) {
|
||||
struct _gattlib_device* device = calloc(sizeof(struct _gattlib_device), 1);
|
||||
gattlib_device_t* device = calloc(sizeof(gattlib_device_t), 1);
|
||||
if (device == NULL) {
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "gattlib_device_set_state: Cannot allocate device");
|
||||
ret = GATTLIB_OUT_OF_MEMORY;
|
||||
|
@ -83,8 +80,9 @@ int gattlib_device_set_state(void* adapter, const char* device_id, enum _gattlib
|
|||
device->adapter = adapter;
|
||||
device->device_id = g_strdup(device_id);
|
||||
device->state = new_state;
|
||||
device->connection.device = device;
|
||||
|
||||
gattlib_adapter->devices = g_slist_append(gattlib_adapter->devices, device);
|
||||
adapter->devices = g_slist_append(adapter->devices, device);
|
||||
} else {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_device_set_state:%s: No state to set", device_id);
|
||||
}
|
||||
|
@ -92,19 +90,19 @@ int gattlib_device_set_state(void* adapter, const char* device_id, enum _gattlib
|
|||
//
|
||||
// The device needs to be remove and free
|
||||
//
|
||||
GSList *item = _find_device_with_device_id(gattlib_adapter, device_id);
|
||||
GSList *item = _find_device_with_device_id(adapter, device_id);
|
||||
if (item == NULL) {
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "gattlib_device_set_state: The device is not present. It is not expected");
|
||||
ret = GATTLIB_UNEXPECTED;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
struct _gattlib_device* device = item->data;
|
||||
gattlib_device_t* device = item->data;
|
||||
|
||||
switch (device->state) {
|
||||
case DISCONNECTED:
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_device_set_state: Free device %p", device);
|
||||
gattlib_adapter->devices = g_slist_remove(gattlib_adapter->devices, device);
|
||||
adapter->devices = g_slist_remove(adapter->devices, device);
|
||||
free(device);
|
||||
break;
|
||||
case CONNECTING:
|
||||
|
@ -127,17 +125,17 @@ int gattlib_device_set_state(void* adapter, const char* device_id, enum _gattlib
|
|||
} else {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_device_set_state:%s: Set state %s", device_id, device_state_str[new_state]);
|
||||
|
||||
struct _gattlib_device* device = gattlib_device_get_device(adapter, device_id);
|
||||
gattlib_device_t* device = gattlib_device_get_device(adapter, device_id);
|
||||
device->state = new_state;
|
||||
}
|
||||
|
||||
EXIT:
|
||||
g_rec_mutex_unlock(&gattlib_adapter->mutex);
|
||||
g_rec_mutex_unlock(&adapter->mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void _gattlib_device_free(gpointer data) {
|
||||
struct _gattlib_device* device = data;
|
||||
gattlib_device_t* device = data;
|
||||
|
||||
switch (device->state) {
|
||||
case DISCONNECTED:
|
||||
|
@ -149,18 +147,16 @@ static void _gattlib_device_free(gpointer data) {
|
|||
}
|
||||
}
|
||||
|
||||
int gattlib_devices_free(void* adapter) {
|
||||
struct gattlib_adapter* gattlib_adapter = adapter;
|
||||
|
||||
g_rec_mutex_lock(&gattlib_adapter->mutex);
|
||||
g_slist_free_full(gattlib_adapter->devices, _gattlib_device_free);
|
||||
g_rec_mutex_unlock(&gattlib_adapter->mutex);
|
||||
int gattlib_devices_free(gattlib_adapter_t* adapter) {
|
||||
g_rec_mutex_lock(&adapter->mutex);
|
||||
g_slist_free_full(adapter->devices, _gattlib_device_free);
|
||||
g_rec_mutex_unlock(&adapter->mutex);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void _gattlib_device_is_disconnected(gpointer data, gpointer user_data) {
|
||||
struct _gattlib_device* device = data;
|
||||
gattlib_device_t* device = data;
|
||||
bool* devices_are_disconnected_ptr = user_data;
|
||||
|
||||
if (device->state != DISCONNECTED) {
|
||||
|
@ -168,13 +164,12 @@ static void _gattlib_device_is_disconnected(gpointer data, gpointer user_data) {
|
|||
}
|
||||
}
|
||||
|
||||
int gattlib_devices_are_disconnected(void* adapter) {
|
||||
struct gattlib_adapter* gattlib_adapter = adapter;
|
||||
int gattlib_devices_are_disconnected(gattlib_adapter_t* adapter) {
|
||||
bool devices_are_disconnected = true;
|
||||
|
||||
g_rec_mutex_lock(&gattlib_adapter->mutex);
|
||||
g_slist_foreach(gattlib_adapter->devices, _gattlib_device_is_disconnected, &devices_are_disconnected);
|
||||
g_rec_mutex_unlock(&gattlib_adapter->mutex);
|
||||
g_rec_mutex_lock(&adapter->mutex);
|
||||
g_slist_foreach(adapter->devices, _gattlib_device_is_disconnected, &devices_are_disconnected);
|
||||
g_rec_mutex_unlock(&adapter->mutex);
|
||||
|
||||
return devices_are_disconnected;
|
||||
}
|
||||
|
@ -182,17 +177,15 @@ int gattlib_devices_are_disconnected(void* adapter) {
|
|||
#ifdef DEBUG
|
||||
|
||||
static void _gattlib_device_dump_state(gpointer data, gpointer user_data) {
|
||||
struct _gattlib_device* device = data;
|
||||
gattlib_device_t* device = data;
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "\t%s: %s", device->device_id, device_state_str[device->state]);
|
||||
}
|
||||
|
||||
void gattlib_devices_dump_state(void* adapter) {
|
||||
struct gattlib_adapter* gattlib_adapter = adapter;
|
||||
|
||||
g_rec_mutex_lock(&gattlib_adapter->mutex);
|
||||
void gattlib_devices_dump_state(gattlib_adapter_t* adapter) {
|
||||
g_rec_mutex_lock(&adapter->mutex);
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "Device list:");
|
||||
g_slist_foreach(gattlib_adapter->devices, _gattlib_device_dump_state, NULL);
|
||||
g_rec_mutex_unlock(&gattlib_adapter->mutex);
|
||||
g_slist_foreach(adapter->devices, _gattlib_device_dump_state, NULL);
|
||||
g_rec_mutex_unlock(&adapter->mutex);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -25,7 +25,7 @@ struct on_eddystone_discovered_device_arg {
|
|||
void *user_data;
|
||||
};
|
||||
|
||||
static void on_eddystone_discovered_device(void *adapter, const char* addr, const char* name, void *user_data)
|
||||
static void on_eddystone_discovered_device(gattlib_adapter_t* adapter, const char* addr, const char* name, void *user_data)
|
||||
{
|
||||
struct on_eddystone_discovered_device_arg *callback_data = user_data;
|
||||
gattlib_advertisement_data_t *advertisement_data = NULL;
|
||||
|
@ -55,7 +55,7 @@ static void on_eddystone_discovered_device(void *adapter, const char* addr, cons
|
|||
}
|
||||
}
|
||||
|
||||
int gattlib_adapter_scan_eddystone(void *adapter, int16_t rssi_threshold, uint32_t eddystone_types,
|
||||
int gattlib_adapter_scan_eddystone(gattlib_adapter_t* adapter, int16_t rssi_threshold, uint32_t eddystone_types,
|
||||
gattlib_discovered_device_with_data_t discovered_device_cb, size_t timeout, void *user_data)
|
||||
{
|
||||
uuid_t eddystone_uuid;
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
* Copyright (c) 2021-2024, Olivier Martin <olivier@labapart.org>
|
||||
*/
|
||||
|
||||
#ifndef __GATTLIB_INTERNAL_DEFS_H__
|
||||
#define __GATTLIB_INTERNAL_DEFS_H__
|
||||
#ifndef __GATTLIB_INTERNAL_H__
|
||||
#define __GATTLIB_INTERNAL_H__
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <glib.h>
|
||||
|
@ -15,6 +15,7 @@
|
|||
#endif
|
||||
|
||||
#include "gattlib.h"
|
||||
#include "gattlib_backend.h"
|
||||
|
||||
#if defined(WITH_PYTHON)
|
||||
struct gattlib_python_args {
|
||||
|
@ -56,17 +57,29 @@ enum _gattlib_device_state {
|
|||
DISCONNECTED
|
||||
};
|
||||
|
||||
struct _gattlib_device {
|
||||
struct _gattlib_adapter {
|
||||
// Context specific to the backend implementation (eg: dbus backend)
|
||||
void* context;
|
||||
struct _gattlib_adapter_backend backend;
|
||||
|
||||
void* adapter;
|
||||
// On some platform, the name could be a UUID, on others its the DBUS device path
|
||||
char* device_id;
|
||||
GMutex device_mutex;
|
||||
// BLE adapter name
|
||||
char* name;
|
||||
|
||||
// We keep the state to prevent concurrent connecting/connected/disconnecting operation
|
||||
enum _gattlib_device_state state;
|
||||
// The recursive mutex allows to ensure sensible operations are always covered by a mutex in a same thread
|
||||
GRecMutex mutex;
|
||||
|
||||
// List of `gattlib_device_t`. This list allows to know weither a device is
|
||||
// discovered/disconnected/connecting/connected/disconnecting.
|
||||
GSList *devices;
|
||||
|
||||
// Handler calls on discovered device
|
||||
struct gattlib_handler discovered_device_callback;
|
||||
};
|
||||
|
||||
struct _gattlib_connection {
|
||||
struct _gattlib_device* device;
|
||||
|
||||
// Context specific to the backend implementation (eg: dbus backend)
|
||||
struct _gattlib_connection_backend backend;
|
||||
|
||||
struct {
|
||||
// Used by gattlib_disconnection when we want to wait for the disconnection to be effective
|
||||
|
@ -81,6 +94,18 @@ struct _gattlib_device {
|
|||
struct gattlib_handler on_disconnection;
|
||||
};
|
||||
|
||||
typedef struct _gattlib_device {
|
||||
struct _gattlib_adapter* adapter;
|
||||
// On some platform, the name could be a UUID, on others its the DBUS device path
|
||||
char* device_id;
|
||||
GMutex device_mutex;
|
||||
|
||||
// We keep the state to prevent concurrent connecting/connected/disconnecting operation
|
||||
enum _gattlib_device_state state;
|
||||
|
||||
struct _gattlib_connection connection;
|
||||
} gattlib_device_t;
|
||||
|
||||
void gattlib_handler_dispatch_to_thread(struct gattlib_handler* handler, void (*python_callback)(),
|
||||
GThreadFunc thread_func, const char* thread_name, void* (*thread_args_allocator)(va_list args), ...);
|
||||
void gattlib_handler_free(struct gattlib_handler* handler);
|
||||
|
@ -94,17 +119,17 @@ void gattlib_notification_device_thread(gpointer data, gpointer user_data);
|
|||
* This function is called by the disconnection callback to always be called on explicit
|
||||
* and implicit disconnection.
|
||||
*/
|
||||
void gattlib_connection_free(gatt_connection_t* connection);
|
||||
void gattlib_connection_free(gattlib_connection_t* connection);
|
||||
|
||||
extern const char* device_state_str[];
|
||||
struct _gattlib_device* gattlib_device_get_device(void* adapter, const char* device_id);
|
||||
enum _gattlib_device_state gattlib_device_get_state(void* adapter, const char* device_id);
|
||||
int gattlib_device_set_state(void* adapter, const char* device_id, enum _gattlib_device_state new_state);
|
||||
int gattlib_devices_are_disconnected(void* adapter);
|
||||
int gattlib_devices_free(void* adapter);
|
||||
gattlib_device_t* gattlib_device_get_device(gattlib_adapter_t* adapter, const char* device_id);
|
||||
enum _gattlib_device_state gattlib_device_get_state(gattlib_adapter_t* adapter, const char* device_id);
|
||||
int gattlib_device_set_state(gattlib_adapter_t* adapter, const char* device_id, enum _gattlib_device_state new_state);
|
||||
int gattlib_devices_are_disconnected(gattlib_adapter_t* adapter);
|
||||
int gattlib_devices_free(gattlib_adapter_t* adapter);
|
||||
|
||||
#ifdef DEBUG
|
||||
void gattlib_devices_dump_state(void* adapter);
|
||||
void gattlib_devices_dump_state(gattlib_adapter_t* adapter);
|
||||
#endif
|
||||
|
||||
#if defined(WITH_PYTHON)
|
||||
|
@ -114,9 +139,9 @@ void* gattlib_python_callback_args(PyObject* python_callback, PyObject* python_a
|
|||
/**
|
||||
* These functions are called by Python wrapper
|
||||
*/
|
||||
void gattlib_discovered_device_python_callback(void *adapter, const char* addr, const char* name, void *user_data);
|
||||
void gattlib_connected_device_python_callback(void *adapter, const char *dst, gatt_connection_t* connection, int error, void* user_data);
|
||||
void gattlib_disconnected_device_python_callback(gatt_connection_t* connection, void *user_data);
|
||||
void gattlib_discovered_device_python_callback(gattlib_adapter_t* adapter, const char* addr, const char* name, void *user_data);
|
||||
void gattlib_connected_device_python_callback(gattlib_adapter_t* adapter, const char *dst, gattlib_connection_t* connection, int error, void* user_data);
|
||||
void gattlib_disconnected_device_python_callback(gattlib_connection_t* connection, void *user_data);
|
||||
void gattlib_notification_device_python_callback(const uuid_t* uuid, const uint8_t* data, size_t data_length, void* user_data);
|
||||
#endif
|
||||
|
227
dbus/gattlib.c
227
dbus/gattlib.c
|
@ -15,19 +15,18 @@
|
|||
|
||||
static const char *m_dbus_error_unknown_object = "GDBus.Error:org.freedesktop.DBus.Error.UnknownObject";
|
||||
|
||||
static void _on_device_connect(gatt_connection_t* connection) {
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
static void _on_device_connect(gattlib_connection_t* connection) {
|
||||
GDBusObjectManager *device_manager;
|
||||
GError *error = NULL;
|
||||
|
||||
// Stop the timeout for connection
|
||||
if (conn_context->connection_timeout_id) {
|
||||
g_source_remove(conn_context->connection_timeout_id);
|
||||
conn_context->connection_timeout_id = 0;
|
||||
if (connection->backend.connection_timeout_id) {
|
||||
g_source_remove(connection->backend.connection_timeout_id);
|
||||
connection->backend.connection_timeout_id = 0;
|
||||
}
|
||||
|
||||
// Get list of objects belonging to Device Manager
|
||||
device_manager = get_device_manager_from_adapter(conn_context->adapter, &error);
|
||||
device_manager = get_device_manager_from_adapter(connection->device->adapter, &error);
|
||||
if (device_manager == NULL) {
|
||||
if (error != NULL) {
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "gattlib_connect: Failed to get device manager from adapter (%d, %d).", error->domain, error->code);
|
||||
|
@ -38,9 +37,9 @@ static void _on_device_connect(gatt_connection_t* connection) {
|
|||
//TODO: Free device
|
||||
return;
|
||||
}
|
||||
conn_context->dbus_objects = g_dbus_object_manager_get_objects(device_manager);
|
||||
connection->backend.dbus_objects = g_dbus_object_manager_get_objects(device_manager);
|
||||
|
||||
gattlib_device_set_state(conn_context->adapter, connection->device_id, CONNECTED);
|
||||
gattlib_device_set_state(connection->device->adapter, connection->device->device_id, CONNECTED);
|
||||
|
||||
gattlib_on_connected_device(connection);
|
||||
}
|
||||
|
@ -51,8 +50,7 @@ gboolean on_handle_device_property_change(
|
|||
const gchar *const *arg_invalidated_properties,
|
||||
gpointer user_data)
|
||||
{
|
||||
gatt_connection_t* connection = user_data;
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
gattlib_connection_t* connection = user_data;
|
||||
|
||||
// Retrieve 'Value' from 'arg_changed_properties'
|
||||
if (g_variant_n_children (arg_changed_properties) > 0) {
|
||||
|
@ -65,16 +63,16 @@ gboolean on_handle_device_property_change(
|
|||
if (strcmp(key, "Connected") == 0) {
|
||||
if (!g_variant_get_boolean(value)) {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "DBUS: device_property_change(%s): Disconnection",
|
||||
conn_context->device_object_path);
|
||||
connection->backend.device_object_path);
|
||||
gattlib_on_disconnected_device(connection);
|
||||
} else {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "DBUS: device_property_change(%s): Connection",
|
||||
conn_context->device_object_path);
|
||||
connection->backend.device_object_path);
|
||||
}
|
||||
} else if (strcmp(key, "ServicesResolved") == 0) {
|
||||
if (g_variant_get_boolean(value)) {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "DBUS: device_property_change(%s): Service Resolved",
|
||||
conn_context->device_object_path);
|
||||
connection->backend.device_object_path);
|
||||
_on_device_connect(connection);
|
||||
}
|
||||
}
|
||||
|
@ -132,10 +130,10 @@ void get_device_path_from_mac(const char *adapter_name, const char *mac_address,
|
|||
}
|
||||
|
||||
static gboolean _stop_connect_func(gpointer data) {
|
||||
gattlib_context_t *conn_context = data;
|
||||
gattlib_connection_t *connection = data;
|
||||
|
||||
// Reset the connection timeout
|
||||
conn_context->connection_timeout_id = 0;
|
||||
connection->backend.connection_timeout_id = 0;
|
||||
|
||||
// We return FALSE when it is a one-off event
|
||||
return FALSE;
|
||||
|
@ -154,30 +152,27 @@ static gboolean _stop_connect_func(gpointer data) {
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_connect(void *adapter, const char *dst,
|
||||
int gattlib_connect(gattlib_adapter_t* adapter, const char *dst,
|
||||
unsigned long options,
|
||||
gatt_connect_cb_t connect_cb,
|
||||
void* user_data)
|
||||
{
|
||||
struct gattlib_adapter *gattlib_adapter = adapter;
|
||||
const char* adapter_name = NULL;
|
||||
GError *error = NULL;
|
||||
char object_path[100];
|
||||
int ret = GATTLIB_SUCCESS;
|
||||
|
||||
// In case NULL is passed, we initialized default adapter
|
||||
if (gattlib_adapter == NULL) {
|
||||
gattlib_adapter = init_default_adapter();
|
||||
if (adapter == NULL) {
|
||||
adapter = init_default_adapter();
|
||||
if (adapter == NULL) {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: No default adapter");
|
||||
return GATTLIB_NOT_FOUND;
|
||||
}
|
||||
} else {
|
||||
adapter_name = gattlib_adapter->adapter_name;
|
||||
adapter_name = adapter->name;
|
||||
}
|
||||
|
||||
// even after init_default_adapter() - the adapter can be NULL
|
||||
if (gattlib_adapter == NULL) {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: No adapter");
|
||||
return GATTLIB_NOT_FOUND;
|
||||
}
|
||||
|
||||
if (connect_cb == NULL) {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: Missing connection callback");
|
||||
return GATTLIB_INVALID_PARAMETER;
|
||||
|
@ -185,40 +180,32 @@ int gattlib_connect(void *adapter, const char *dst,
|
|||
|
||||
get_device_path_from_mac(adapter_name, dst, object_path, sizeof(object_path));
|
||||
|
||||
gatt_connection_t* connection = gattlib_device_get_device(adapter, object_path);
|
||||
if (connection == NULL) {
|
||||
gattlib_device_t* device = gattlib_device_get_device(adapter, object_path);
|
||||
if (device == NULL) {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: Cannot find connection %s", dst);
|
||||
return GATTLIB_INVALID_PARAMETER;
|
||||
} else if (connection->state != DISCONNECTED) {
|
||||
} else if (device->state != DISCONNECTED) {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: Cannot connect to '%s'. Device is in state %s",
|
||||
dst, device_state_str[connection->state]);
|
||||
dst, device_state_str[device->state]);
|
||||
return GATTLIB_BUSY;
|
||||
}
|
||||
|
||||
gattlib_context_t* conn_context = calloc(sizeof(gattlib_context_t), 1);
|
||||
if (conn_context == NULL) {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: Cannot allocate context");
|
||||
return GATTLIB_OUT_OF_MEMORY;
|
||||
}
|
||||
conn_context->adapter = gattlib_adapter;
|
||||
|
||||
connection->context = conn_context;
|
||||
connection->on_connection.callback.connection_handler = connect_cb;
|
||||
connection->on_connection.user_data = user_data;
|
||||
device->connection.on_connection.callback.connection_handler = connect_cb;
|
||||
device->connection.on_connection.user_data = user_data;
|
||||
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "Connecting bluetooth device %s", dst);
|
||||
|
||||
// Mark the device has disconnected
|
||||
gattlib_device_set_state(connection->adapter, connection->device_id, CONNECTING);
|
||||
gattlib_device_set_state(device->adapter, device->device_id, CONNECTING);
|
||||
|
||||
OrgBluezDevice1* device = org_bluez_device1_proxy_new_for_bus_sync(
|
||||
OrgBluezDevice1* bluez_device = org_bluez_device1_proxy_new_for_bus_sync(
|
||||
G_BUS_TYPE_SYSTEM,
|
||||
G_DBUS_PROXY_FLAGS_NONE,
|
||||
"org.bluez",
|
||||
object_path,
|
||||
NULL,
|
||||
&error);
|
||||
if (device == NULL) {
|
||||
if (bluez_device == NULL) {
|
||||
ret = GATTLIB_ERROR_DBUS;
|
||||
if (error) {
|
||||
ret = GATTLIB_ERROR_DBUS_WITH_ERROR(error);
|
||||
|
@ -227,20 +214,19 @@ int gattlib_connect(void *adapter, const char *dst,
|
|||
} else {
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "gattlib_connect: Failed to connect to DBus Bluez Device");
|
||||
}
|
||||
goto FREE_CONNECTION_CONTEXT;
|
||||
} else {
|
||||
conn_context->device = device;
|
||||
conn_context->device_object_path = strdup(object_path);
|
||||
device->connection.backend.device = bluez_device;
|
||||
device->connection.backend.device_object_path = strdup(object_path);
|
||||
}
|
||||
|
||||
// Register a handle for notification
|
||||
conn_context->on_handle_device_property_change_id = g_signal_connect(device,
|
||||
device->connection.backend.on_handle_device_property_change_id = g_signal_connect(bluez_device,
|
||||
"g-properties-changed",
|
||||
G_CALLBACK (on_handle_device_property_change),
|
||||
connection);
|
||||
G_CALLBACK(on_handle_device_property_change),
|
||||
&device->connection);
|
||||
|
||||
error = NULL;
|
||||
org_bluez_device1_call_connect_sync(device, NULL, &error);
|
||||
org_bluez_device1_call_connect_sync(bluez_device, NULL, &error);
|
||||
if (error) {
|
||||
if (strncmp(error->message, m_dbus_error_unknown_object, strlen(m_dbus_error_unknown_object)) == 0) {
|
||||
// You might have this error if the computer has not scanned or has not already had
|
||||
|
@ -252,7 +238,7 @@ int gattlib_connect(void *adapter, const char *dst,
|
|||
ret = GATTLIB_TIMEOUT;
|
||||
} else {
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "Device connected error (device:%s): %s",
|
||||
conn_context->device_object_path,
|
||||
device->connection.backend.device_object_path,
|
||||
error->message);
|
||||
ret = GATTLIB_ERROR_DBUS_WITH_ERROR(error);
|
||||
}
|
||||
|
@ -260,27 +246,23 @@ int gattlib_connect(void *adapter, const char *dst,
|
|||
g_error_free(error);
|
||||
|
||||
// Fail to connect. Mark the device has disconnected to be able to reconnect
|
||||
gattlib_device_set_state(connection->adapter, connection->device_id, DISCONNECTED);
|
||||
gattlib_device_set_state(adapter, device->device_id, DISCONNECTED);
|
||||
|
||||
goto FREE_DEVICE;
|
||||
}
|
||||
|
||||
// Wait for the property 'UUIDs' to be changed. We assume 'org.bluez.GattService1
|
||||
// and 'org.bluez.GattCharacteristic1' to be advertised at that moment.
|
||||
conn_context->connection_timeout_id = g_timeout_add_seconds(CONNECT_TIMEOUT_SEC, _stop_connect_func, conn_context);
|
||||
device->connection.backend.connection_timeout_id = g_timeout_add_seconds(CONNECT_TIMEOUT_SEC, _stop_connect_func, &device->connection);
|
||||
|
||||
return GATTLIB_SUCCESS;
|
||||
|
||||
FREE_DEVICE:
|
||||
free(conn_context->device_object_path);
|
||||
g_object_unref(conn_context->device);
|
||||
|
||||
FREE_CONNECTION_CONTEXT:
|
||||
free(conn_context);
|
||||
free(device->connection.backend.device_object_path);
|
||||
|
||||
// destroy default adapter
|
||||
if(adapter == NULL) {
|
||||
gattlib_adapter_close(gattlib_adapter);
|
||||
gattlib_adapter_close(adapter);
|
||||
}
|
||||
|
||||
if (ret != GATTLIB_SUCCESS) {
|
||||
|
@ -296,36 +278,28 @@ FREE_CONNECTION_CONTEXT:
|
|||
* This function is called by the disconnection callback to always be called on explicit
|
||||
* and implicit disconnection.
|
||||
*/
|
||||
void gattlib_connection_free(gatt_connection_t* connection) {
|
||||
gattlib_context_t* conn_context;
|
||||
void* adapter;
|
||||
void gattlib_connection_free(gattlib_connection_t* connection) {
|
||||
char* device_id;
|
||||
|
||||
g_mutex_lock(&connection->device_mutex);
|
||||
conn_context = connection->context;
|
||||
adapter = conn_context->adapter;
|
||||
device_id = connection->device_id;
|
||||
g_mutex_lock(&connection->device->device_mutex);
|
||||
device_id = connection->device->device_id;
|
||||
|
||||
// Remove signal
|
||||
if (conn_context->on_handle_device_property_change_id != 0) {
|
||||
g_signal_handler_disconnect(conn_context->device, conn_context->on_handle_device_property_change_id);
|
||||
conn_context->on_handle_device_property_change_id = 0;
|
||||
if (connection->backend.on_handle_device_property_change_id != 0) {
|
||||
g_signal_handler_disconnect(connection->backend.device, connection->backend.on_handle_device_property_change_id);
|
||||
connection->backend.on_handle_device_property_change_id = 0;
|
||||
}
|
||||
|
||||
// Stop the timeout for connection
|
||||
if (conn_context->connection_timeout_id) {
|
||||
g_source_remove(conn_context->connection_timeout_id);
|
||||
conn_context->connection_timeout_id = 0;
|
||||
if (connection->backend.connection_timeout_id) {
|
||||
g_source_remove(connection->backend.connection_timeout_id);
|
||||
connection->backend.connection_timeout_id = 0;
|
||||
}
|
||||
|
||||
free(conn_context->device_object_path);
|
||||
if (conn_context->device != NULL) {
|
||||
g_object_unref(conn_context->device);
|
||||
conn_context->device = NULL;
|
||||
}
|
||||
g_list_free_full(conn_context->dbus_objects, g_object_unref);
|
||||
free(connection->backend.device_object_path);
|
||||
g_list_free_full(connection->backend.dbus_objects, g_object_unref);
|
||||
|
||||
disconnect_all_notifications(conn_context);
|
||||
disconnect_all_notifications(&connection->backend);
|
||||
|
||||
// Free all handler
|
||||
//TODO: Fixme - there is a memory leak by not freeing the handlers
|
||||
|
@ -336,17 +310,13 @@ void gattlib_connection_free(gatt_connection_t* connection) {
|
|||
|
||||
// Note: We do not free adapter as it might still be used by other devices
|
||||
|
||||
free(connection->context);
|
||||
connection->context = NULL;
|
||||
|
||||
// Mark the device has disconnected
|
||||
gattlib_device_set_state(adapter, device_id, DISCONNECTED);
|
||||
gattlib_device_set_state(connection->device->adapter, device_id, DISCONNECTED);
|
||||
|
||||
g_mutex_unlock(&connection->device_mutex);
|
||||
g_mutex_unlock(&connection->device->device_mutex);
|
||||
}
|
||||
|
||||
int gattlib_disconnect(gatt_connection_t* connection, bool wait_disconnection) {
|
||||
gattlib_context_t* conn_context;
|
||||
int gattlib_disconnect(gattlib_connection_t* connection, bool wait_disconnection) {
|
||||
int ret = GATTLIB_SUCCESS;
|
||||
GError *error = NULL;
|
||||
|
||||
|
@ -355,30 +325,25 @@ int gattlib_disconnect(gatt_connection_t* connection, bool wait_disconnection) {
|
|||
return GATTLIB_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
g_mutex_lock(&connection->device_mutex);
|
||||
conn_context = connection->context;
|
||||
g_mutex_lock(&connection->device->device_mutex);
|
||||
|
||||
if (conn_context == NULL) {
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "Cannot disconnect - connection context is not valid.");
|
||||
ret = GATTLIB_NOT_SUPPORTED;
|
||||
goto EXIT;
|
||||
} else if (connection->state != CONNECTED) {
|
||||
if (connection->device->state != CONNECTED) {
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "Cannot disconnect - connection is not in connected state (state=%s).",
|
||||
device_state_str[connection->state]);
|
||||
device_state_str[connection->device->state]);
|
||||
ret = GATTLIB_BUSY;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "Disconnecting bluetooth device %s", conn_context->device_object_path);
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "Disconnecting bluetooth device %s", connection->backend.device_object_path);
|
||||
|
||||
org_bluez_device1_call_disconnect_sync(conn_context->device, NULL, &error);
|
||||
org_bluez_device1_call_disconnect_sync(connection->backend.device, NULL, &error);
|
||||
if (error) {
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "Failed to disconnect DBus Bluez Device: %s", error->message);
|
||||
g_error_free(error);
|
||||
}
|
||||
|
||||
// Mark the device has disconnected
|
||||
gattlib_device_set_state(connection->adapter, connection->device_id, DISCONNECTING);
|
||||
gattlib_device_set_state(connection->device->adapter, connection->device->device_id, DISCONNECTING);
|
||||
|
||||
//Note: Signals and memory will be removed/clean on disconnction callback
|
||||
// See _gattlib_clean_on_disconnection()
|
||||
|
@ -389,7 +354,7 @@ int gattlib_disconnect(gatt_connection_t* connection, bool wait_disconnection) {
|
|||
end_time = g_get_monotonic_time() + GATTLIB_DISCONNECTION_WAIT_TIMEOUT_SEC * G_TIME_SPAN_SECOND;
|
||||
|
||||
while (!connection->disconnection_wait.value) {
|
||||
if (!g_cond_wait_until(&connection->disconnection_wait.condition, &connection->device_mutex, end_time)) {
|
||||
if (!g_cond_wait_until(&connection->disconnection_wait.condition, &connection->device->device_mutex, end_time)) {
|
||||
ret = GATTLIB_TIMEOUT;
|
||||
break;
|
||||
}
|
||||
|
@ -397,24 +362,23 @@ int gattlib_disconnect(gatt_connection_t* connection, bool wait_disconnection) {
|
|||
}
|
||||
|
||||
EXIT:
|
||||
g_mutex_unlock(&connection->device_mutex);
|
||||
g_mutex_unlock(&connection->device->device_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Bluez was using org.bluez.Device1.GattServices until 5.37 to expose the list of available GATT Services
|
||||
#if BLUEZ_VERSION < BLUEZ_VERSIONS(5, 38)
|
||||
int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_service_t** services, int* services_count) {
|
||||
int gattlib_discover_primary(gattlib_connection_t* connection, gattlib_primary_service_t** services, int* services_count) {
|
||||
if (connection == NULL) {
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "Gattlib connection not initialized.");
|
||||
return GATTLIB_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
OrgBluezDevice1* device = conn_context->device;
|
||||
OrgBluezDevice1* bluez_device = connection->backend.device;
|
||||
const gchar* const* service_str;
|
||||
GError *error = NULL;
|
||||
|
||||
const gchar* const* service_strs = org_bluez_device1_get_gatt_services(device);
|
||||
const gchar* const* service_strs = org_bluez_device1_get_gatt_services(bluez_device);
|
||||
|
||||
if (service_strs == NULL) {
|
||||
if (services != NULL) {
|
||||
|
@ -479,16 +443,15 @@ int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_serv
|
|||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
#else
|
||||
int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_service_t** services, int* services_count) {
|
||||
int gattlib_discover_primary(gattlib_connection_t* connection, gattlib_primary_service_t** services, int* services_count) {
|
||||
if (connection == NULL) {
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "Gattlib connection not initialized.");
|
||||
return GATTLIB_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
GError *error = NULL;
|
||||
GDBusObjectManager *device_manager = get_device_manager_from_adapter(conn_context->adapter, &error);
|
||||
OrgBluezDevice1* device = conn_context->device;
|
||||
GDBusObjectManager *device_manager = get_device_manager_from_adapter(connection->device->adapter, &error);
|
||||
OrgBluezDevice1* device = connection->backend.device;
|
||||
const gchar* const* service_str;
|
||||
int ret = GATTLIB_SUCCESS;
|
||||
|
||||
|
@ -528,7 +491,7 @@ int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_serv
|
|||
}
|
||||
|
||||
GList *l;
|
||||
for (l = conn_context->dbus_objects; l != NULL; l = l->next) {
|
||||
for (l = connection->backend.dbus_objects; l != NULL; l = l->next) {
|
||||
GDBusObject *object = l->data;
|
||||
const char* object_path = g_dbus_object_get_object_path(G_DBUS_OBJECT(object));
|
||||
|
||||
|
@ -568,7 +531,7 @@ int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_serv
|
|||
}
|
||||
continue;
|
||||
}
|
||||
if (strcmp(conn_context->device_object_path, service_property)) {
|
||||
if (strcmp(connection->backend.device_object_path, service_property)) {
|
||||
g_object_unref(service_proxy);
|
||||
continue;
|
||||
}
|
||||
|
@ -582,7 +545,7 @@ int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_serv
|
|||
primary_services[count].attr_handle_end = service_handle;
|
||||
|
||||
// Loop through all objects, as ordering is not guaranteed.
|
||||
for (GList *m = conn_context->dbus_objects; m != NULL; m = m->next) {
|
||||
for (GList *m = connection->backend.dbus_objects; m != NULL; m = m->next) {
|
||||
GDBusObject *characteristic_object = m->data;
|
||||
const char* characteristic_path = g_dbus_object_get_object_path(G_DBUS_OBJECT(characteristic_object));
|
||||
interface = g_dbus_object_manager_get_interface(device_manager, characteristic_path, "org.bluez.GattCharacteristic1");
|
||||
|
@ -633,13 +596,12 @@ int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_serv
|
|||
|
||||
// Bluez was using org.bluez.Device1.GattServices until 5.37 to expose the list of available GATT Services
|
||||
#if BLUEZ_VERSION < BLUEZ_VERSIONS(5, 38)
|
||||
int gattlib_discover_char_range(gatt_connection_t* connection, uint16_t start, uint16_t end, gattlib_characteristic_t** characteristics, int* characteristics_count) {
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
OrgBluezDevice1* device = conn_context->device;
|
||||
int gattlib_discover_char_range(gattlib_connection_t* connection, uint16_t start, uint16_t end, gattlib_characteristic_t** characteristics, int* characteristics_count) {
|
||||
OrgBluezDevice1* bluez_device = connection->backend.bluez_device;
|
||||
GError *error = NULL;
|
||||
int handle;
|
||||
|
||||
const gchar* const* service_strs = org_bluez_device1_get_gatt_services(device);
|
||||
const gchar* const* service_strs = org_bluez_device1_get_gatt_services(bluez_device);
|
||||
const gchar* const* service_str;
|
||||
const gchar* const* characteristic_strs;
|
||||
const gchar* characteristic_str;
|
||||
|
@ -785,14 +747,14 @@ int gattlib_discover_char_range(gatt_connection_t* connection, uint16_t start, u
|
|||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
#else
|
||||
static void add_characteristics_from_service(gattlib_context_t* conn_context, GDBusObjectManager *device_manager,
|
||||
static void add_characteristics_from_service(struct _gattlib_connection_backend* backend, GDBusObjectManager *device_manager,
|
||||
const char* service_object_path,
|
||||
unsigned int start, unsigned int end,
|
||||
gattlib_characteristic_t* characteristic_list, int* count)
|
||||
{
|
||||
GError *error = NULL;
|
||||
|
||||
for (GList *l = conn_context->dbus_objects; l != NULL; l = l->next) {
|
||||
for (GList *l = backend->dbus_objects; l != NULL; l = l->next) {
|
||||
GDBusObject *object = l->data;
|
||||
const char* object_path = g_dbus_object_get_object_path(G_DBUS_OBJECT(object));
|
||||
GDBusInterface *interface = g_dbus_object_manager_get_interface(device_manager, object_path, "org.bluez.GattCharacteristic1");
|
||||
|
@ -876,10 +838,9 @@ static void add_characteristics_from_service(gattlib_context_t* conn_context, GD
|
|||
}
|
||||
}
|
||||
|
||||
int gattlib_discover_char_range(gatt_connection_t* connection, uint16_t start, uint16_t end, gattlib_characteristic_t** characteristics, int* characteristics_count) {
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
int gattlib_discover_char_range(gattlib_connection_t* connection, uint16_t start, uint16_t end, gattlib_characteristic_t** characteristics, int* characteristics_count) {
|
||||
GError *error = NULL;
|
||||
GDBusObjectManager *device_manager = get_device_manager_from_adapter(conn_context->adapter, &error);
|
||||
GDBusObjectManager *device_manager = get_device_manager_from_adapter(connection->device->adapter, &error);
|
||||
GList *l;
|
||||
int ret;
|
||||
|
||||
|
@ -897,7 +858,7 @@ int gattlib_discover_char_range(gatt_connection_t* connection, uint16_t start, u
|
|||
|
||||
// Count the maximum number of characteristic to allocate the array (we count all the characterstic for all devices)
|
||||
int count_max = 0, count = 0;
|
||||
for (l = conn_context->dbus_objects; l != NULL; l = l->next) {
|
||||
for (l = connection->backend.dbus_objects; l != NULL; l = l->next) {
|
||||
GDBusObject *object = l->data;
|
||||
const char* object_path = g_dbus_object_get_object_path(G_DBUS_OBJECT(object));
|
||||
GDBusInterface *interface = g_dbus_object_manager_get_interface(device_manager, object_path, "org.bluez.GattCharacteristic1");
|
||||
|
@ -920,7 +881,7 @@ int gattlib_discover_char_range(gatt_connection_t* connection, uint16_t start, u
|
|||
}
|
||||
|
||||
// List all services for this device
|
||||
for (l = conn_context->dbus_objects; l != NULL; l = l->next) {
|
||||
for (l = connection->backend.dbus_objects; l != NULL; l = l->next) {
|
||||
GDBusObject *object = l->data;
|
||||
const char* object_path = g_dbus_object_get_object_path(G_DBUS_OBJECT(object));
|
||||
|
||||
|
@ -968,13 +929,13 @@ int gattlib_discover_char_range(gatt_connection_t* connection, uint16_t start, u
|
|||
|
||||
// Ensure the service is attached to this device
|
||||
const char* service_object_path = org_bluez_gatt_service1_get_device(service_proxy);
|
||||
if (strcmp(conn_context->device_object_path, service_object_path)) {
|
||||
if (strcmp(connection->backend.device_object_path, service_object_path)) {
|
||||
g_object_unref(service_proxy);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add all characteristics attached to this service
|
||||
add_characteristics_from_service(conn_context, device_manager, object_path, start, end, characteristic_list, &count);
|
||||
add_characteristics_from_service(&connection->backend, device_manager, object_path, start, end, characteristic_list, &count);
|
||||
g_object_unref(service_proxy);
|
||||
}
|
||||
|
||||
|
@ -984,31 +945,31 @@ int gattlib_discover_char_range(gatt_connection_t* connection, uint16_t start, u
|
|||
}
|
||||
#endif
|
||||
|
||||
int gattlib_discover_char(gatt_connection_t* connection, gattlib_characteristic_t** characteristics, int* characteristics_count)
|
||||
int gattlib_discover_char(gattlib_connection_t* connection, gattlib_characteristic_t** characteristics, int* characteristics_count)
|
||||
{
|
||||
return gattlib_discover_char_range(connection, 0x00, 0xFF, characteristics, characteristics_count);
|
||||
}
|
||||
|
||||
int gattlib_discover_desc_range(gatt_connection_t* connection, int start, int end, gattlib_descriptor_t** descriptors, int* descriptor_count) {
|
||||
int gattlib_discover_desc_range(gattlib_connection_t* connection, int start, int end, gattlib_descriptor_t** descriptors, int* descriptor_count) {
|
||||
return GATTLIB_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
int gattlib_discover_desc(gatt_connection_t* connection, gattlib_descriptor_t** descriptors, int* descriptor_count) {
|
||||
int gattlib_discover_desc(gattlib_connection_t* connection, gattlib_descriptor_t** descriptors, int* descriptor_count) {
|
||||
return GATTLIB_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
int get_bluez_device_from_mac(struct gattlib_adapter *adapter, const char *mac_address, OrgBluezDevice1 **bluez_device1)
|
||||
int get_bluez_device_from_mac(struct _gattlib_adapter *adapter, const char *mac_address, OrgBluezDevice1 **bluez_device1)
|
||||
{
|
||||
GError *error = NULL;
|
||||
char object_path[100];
|
||||
int ret;
|
||||
|
||||
if (adapter->adapter_proxy == NULL) {
|
||||
if (adapter->backend.adapter_proxy == NULL) {
|
||||
return GATTLIB_NO_ADAPTER;
|
||||
}
|
||||
|
||||
if (adapter != NULL) {
|
||||
get_device_path_from_mac_with_adapter(adapter->adapter_proxy, mac_address, object_path, sizeof(object_path));
|
||||
get_device_path_from_mac_with_adapter(adapter->backend.adapter_proxy, mac_address, object_path, sizeof(object_path));
|
||||
} else {
|
||||
get_device_path_from_mac(NULL, mac_address, object_path, sizeof(object_path));
|
||||
}
|
||||
|
@ -1030,24 +991,22 @@ int get_bluez_device_from_mac(struct gattlib_adapter *adapter, const char *mac_a
|
|||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
|
||||
int gattlib_get_rssi(gatt_connection_t *connection, int16_t *rssi)
|
||||
int gattlib_get_rssi(gattlib_connection_t *connection, int16_t *rssi)
|
||||
{
|
||||
if (connection == NULL) {
|
||||
return GATTLIB_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
|
||||
if (rssi == NULL) {
|
||||
return GATTLIB_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
*rssi = org_bluez_device1_get_rssi(conn_context->device);
|
||||
*rssi = org_bluez_device1_get_rssi(connection->backend.device);
|
||||
|
||||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
|
||||
int gattlib_get_rssi_from_mac(void *adapter, const char *mac_address, int16_t *rssi)
|
||||
int gattlib_get_rssi_from_mac(gattlib_adapter_t* adapter, const char *mac_address, int16_t *rssi)
|
||||
{
|
||||
OrgBluezDevice1 *bluez_device1;
|
||||
int ret;
|
||||
|
|
|
@ -12,10 +12,10 @@ static GSList *m_adapter_list;
|
|||
static GMutex m_adapter_list_mutex;
|
||||
|
||||
|
||||
int gattlib_adapter_open(const char* adapter_name, void** adapter) {
|
||||
int gattlib_adapter_open(const char* adapter_name, gattlib_adapter_t** adapter) {
|
||||
char object_path[20];
|
||||
gattlib_adapter_t* gattlib_adapter;
|
||||
OrgBluezAdapter1 *adapter_proxy;
|
||||
struct gattlib_adapter *gattlib_adapter;
|
||||
GError *error = NULL;
|
||||
|
||||
if (adapter == NULL) {
|
||||
|
@ -50,14 +50,14 @@ int gattlib_adapter_open(const char* adapter_name, void** adapter) {
|
|||
// Ensure the adapter is powered on
|
||||
org_bluez_adapter1_set_powered(adapter_proxy, TRUE);
|
||||
|
||||
gattlib_adapter = calloc(1, sizeof(struct gattlib_adapter));
|
||||
gattlib_adapter = calloc(1, sizeof(struct _gattlib_adapter));
|
||||
if (gattlib_adapter == NULL) {
|
||||
return GATTLIB_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
// Initialize stucture
|
||||
gattlib_adapter->adapter_name = strdup(adapter_name);
|
||||
gattlib_adapter->adapter_proxy = adapter_proxy;
|
||||
gattlib_adapter->name = strdup(adapter_name);
|
||||
gattlib_adapter->backend.adapter_proxy = adapter_proxy;
|
||||
|
||||
g_mutex_lock(&m_adapter_list_mutex);
|
||||
m_adapter_list = g_slist_append(m_adapter_list, gattlib_adapter);
|
||||
|
@ -67,16 +67,15 @@ int gattlib_adapter_open(const char* adapter_name, void** adapter) {
|
|||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
|
||||
const char *gattlib_adapter_get_name(void* adapter) {
|
||||
struct gattlib_adapter *gattlib_adapter = adapter;
|
||||
return gattlib_adapter->adapter_name;
|
||||
const char *gattlib_adapter_get_name(gattlib_adapter_t* adapter) {
|
||||
return adapter->name;
|
||||
}
|
||||
|
||||
struct gattlib_adapter *init_default_adapter(void) {
|
||||
struct gattlib_adapter *gattlib_adapter;
|
||||
gattlib_adapter_t* init_default_adapter(void) {
|
||||
gattlib_adapter_t* gattlib_adapter;
|
||||
int ret;
|
||||
|
||||
ret = gattlib_adapter_open(NULL, (void**)&gattlib_adapter);
|
||||
ret = gattlib_adapter_open(NULL, &gattlib_adapter);
|
||||
if (ret != GATTLIB_SUCCESS) {
|
||||
return NULL;
|
||||
} else {
|
||||
|
@ -84,10 +83,10 @@ struct gattlib_adapter *init_default_adapter(void) {
|
|||
}
|
||||
}
|
||||
|
||||
GDBusObjectManager *get_device_manager_from_adapter(struct gattlib_adapter *gattlib_adapter, GError **error) {
|
||||
GDBusObjectManager *get_device_manager_from_adapter(gattlib_adapter_t* gattlib_adapter, GError **error) {
|
||||
g_mutex_lock(&m_adapter_list_mutex);
|
||||
|
||||
if (gattlib_adapter->device_manager) {
|
||||
if (gattlib_adapter->backend.device_manager) {
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
|
@ -96,24 +95,24 @@ GDBusObjectManager *get_device_manager_from_adapter(struct gattlib_adapter *gatt
|
|||
// We should get notified when the connection is lost with the target to allow
|
||||
// us to advertise us again
|
||||
//
|
||||
gattlib_adapter->device_manager = g_dbus_object_manager_client_new_for_bus_sync(
|
||||
gattlib_adapter->backend.device_manager = g_dbus_object_manager_client_new_for_bus_sync(
|
||||
G_BUS_TYPE_SYSTEM,
|
||||
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
|
||||
"org.bluez",
|
||||
"/",
|
||||
NULL, NULL, NULL, NULL,
|
||||
error);
|
||||
if (gattlib_adapter->device_manager == NULL) {
|
||||
if (gattlib_adapter->backend.device_manager == NULL) {
|
||||
g_mutex_unlock(&m_adapter_list_mutex);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
EXIT:
|
||||
g_mutex_unlock(&m_adapter_list_mutex);
|
||||
return gattlib_adapter->device_manager;
|
||||
return gattlib_adapter->backend.device_manager;
|
||||
}
|
||||
|
||||
static void device_manager_on_added_device1_signal(const char* device1_path, struct gattlib_adapter* gattlib_adapter)
|
||||
static void device_manager_on_added_device1_signal(const char* device1_path, gattlib_adapter_t* gattlib_adapter)
|
||||
{
|
||||
GError *error = NULL;
|
||||
OrgBluezDevice1* device1 = org_bluez_device1_proxy_new_for_bus_sync(
|
||||
|
@ -178,7 +177,7 @@ static void on_dbus_object_removed(GDBusObjectManager *device_manager,
|
|||
gpointer user_data)
|
||||
{
|
||||
const char* object_path = g_dbus_object_get_object_path(G_DBUS_OBJECT(object));
|
||||
struct gattlib_adapter* gattlib_adapter = (struct gattlib_adapter*)user_data;
|
||||
gattlib_adapter_t* gattlib_adapter = user_data;
|
||||
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "DBUS: on_object_removed: %s", object_path);
|
||||
|
||||
|
@ -195,9 +194,9 @@ on_interface_proxy_properties_changed (GDBusObjectManagerClient *device_manager,
|
|||
gpointer user_data)
|
||||
{
|
||||
const char* proxy_object_path = g_dbus_proxy_get_object_path(interface_proxy);
|
||||
struct gattlib_adapter* gattlib_adapter = user_data;
|
||||
gattlib_adapter_t* gattlib_adapter = user_data;
|
||||
|
||||
if (gattlib_adapter->device_manager == NULL) {
|
||||
if (gattlib_adapter->backend.device_manager == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -267,29 +266,29 @@ on_interface_proxy_properties_changed (GDBusObjectManagerClient *device_manager,
|
|||
*
|
||||
* It either called when we wait for BLE scan to complete or when we close the BLE adapter
|
||||
*/
|
||||
static void _wait_scan_loop_stop_scanning(struct gattlib_adapter *gattlib_adapter) {
|
||||
g_mutex_lock(&gattlib_adapter->ble_scan.scan_loop.mutex);
|
||||
while (gattlib_adapter->ble_scan.is_scanning) {
|
||||
g_cond_wait(&gattlib_adapter->ble_scan.scan_loop.cond, &gattlib_adapter->ble_scan.scan_loop.mutex);
|
||||
static void _wait_scan_loop_stop_scanning(gattlib_adapter_t* gattlib_adapter) {
|
||||
g_mutex_lock(&gattlib_adapter->backend.ble_scan.scan_loop.mutex);
|
||||
while (gattlib_adapter->backend.ble_scan.is_scanning) {
|
||||
g_cond_wait(&gattlib_adapter->backend.ble_scan.scan_loop.cond, &gattlib_adapter->backend.ble_scan.scan_loop.mutex);
|
||||
}
|
||||
g_mutex_unlock(&gattlib_adapter->ble_scan.scan_loop.mutex);
|
||||
g_mutex_unlock(&gattlib_adapter->backend.ble_scan.scan_loop.mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function called when the BLE scan duration has timeout
|
||||
*/
|
||||
static gboolean _stop_scan_on_timeout(gpointer data) {
|
||||
struct gattlib_adapter *gattlib_adapter = data;
|
||||
gattlib_adapter_t* gattlib_adapter = data;
|
||||
|
||||
if (gattlib_adapter->ble_scan.is_scanning) {
|
||||
g_mutex_lock(&gattlib_adapter->ble_scan.scan_loop.mutex);
|
||||
gattlib_adapter->ble_scan.is_scanning = false;
|
||||
g_cond_broadcast(&gattlib_adapter->ble_scan.scan_loop.cond);
|
||||
g_mutex_unlock(&gattlib_adapter->ble_scan.scan_loop.mutex);
|
||||
if (gattlib_adapter->backend.ble_scan.is_scanning) {
|
||||
g_mutex_lock(&gattlib_adapter->backend.ble_scan.scan_loop.mutex);
|
||||
gattlib_adapter->backend.ble_scan.is_scanning = false;
|
||||
g_cond_broadcast(&gattlib_adapter->backend.ble_scan.scan_loop.cond);
|
||||
g_mutex_unlock(&gattlib_adapter->backend.ble_scan.scan_loop.mutex);
|
||||
}
|
||||
|
||||
// Unset timeout ID to not try removing it
|
||||
gattlib_adapter->ble_scan.ble_scan_timeout_id = 0;
|
||||
gattlib_adapter->backend.ble_scan.ble_scan_timeout_id = 0;
|
||||
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "BLE scan is stopped after scanning time has expired.");
|
||||
return FALSE;
|
||||
|
@ -300,18 +299,18 @@ static gboolean _stop_scan_on_timeout(gpointer data) {
|
|||
* or disabling the BLE scan
|
||||
*/
|
||||
static void* _ble_scan_loop_thread(void* args) {
|
||||
struct gattlib_adapter *gattlib_adapter = args;
|
||||
gattlib_adapter_t* gattlib_adapter = args;
|
||||
|
||||
if (gattlib_adapter->ble_scan.ble_scan_timeout_id > 0) {
|
||||
if (gattlib_adapter->backend.ble_scan.ble_scan_timeout_id > 0) {
|
||||
GATTLIB_LOG(GATTLIB_WARNING, "A BLE scan seems to already be in progress.");
|
||||
}
|
||||
|
||||
gattlib_adapter->ble_scan.is_scanning = true;
|
||||
gattlib_adapter->backend.ble_scan.is_scanning = true;
|
||||
|
||||
if (gattlib_adapter->ble_scan.ble_scan_timeout > 0) {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "Scan for BLE devices for %ld seconds", gattlib_adapter->ble_scan.ble_scan_timeout);
|
||||
if (gattlib_adapter->backend.ble_scan.ble_scan_timeout > 0) {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "Scan for BLE devices for %ld seconds", gattlib_adapter->backend.ble_scan.ble_scan_timeout);
|
||||
|
||||
gattlib_adapter->ble_scan.ble_scan_timeout_id = g_timeout_add_seconds(gattlib_adapter->ble_scan.ble_scan_timeout,
|
||||
gattlib_adapter->backend.ble_scan.ble_scan_timeout_id = g_timeout_add_seconds(gattlib_adapter->backend.ble_scan.ble_scan_timeout,
|
||||
_stop_scan_on_timeout, gattlib_adapter);
|
||||
}
|
||||
|
||||
|
@ -320,9 +319,9 @@ static void* _ble_scan_loop_thread(void* args) {
|
|||
|
||||
// Note: The function only resumes when loop timeout as expired or g_main_loop_quit has been called.
|
||||
|
||||
g_signal_handler_disconnect(G_DBUS_OBJECT_MANAGER(gattlib_adapter->device_manager), gattlib_adapter->ble_scan.added_signal_id);
|
||||
g_signal_handler_disconnect(G_DBUS_OBJECT_MANAGER(gattlib_adapter->device_manager), gattlib_adapter->ble_scan.removed_signal_id);
|
||||
g_signal_handler_disconnect(G_DBUS_OBJECT_MANAGER(gattlib_adapter->device_manager), gattlib_adapter->ble_scan.changed_signal_id);
|
||||
g_signal_handler_disconnect(G_DBUS_OBJECT_MANAGER(gattlib_adapter->backend.device_manager), gattlib_adapter->backend.ble_scan.added_signal_id);
|
||||
g_signal_handler_disconnect(G_DBUS_OBJECT_MANAGER(gattlib_adapter->backend.device_manager), gattlib_adapter->backend.ble_scan.removed_signal_id);
|
||||
g_signal_handler_disconnect(G_DBUS_OBJECT_MANAGER(gattlib_adapter->backend.device_manager), gattlib_adapter->backend.ble_scan.changed_signal_id);
|
||||
|
||||
// Ensure BLE device discovery is stopped
|
||||
gattlib_adapter_scan_disable(gattlib_adapter);
|
||||
|
@ -330,17 +329,16 @@ static void* _ble_scan_loop_thread(void* args) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
static 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, size_t timeout, void *user_data)
|
||||
static int _gattlib_adapter_scan_enable_with_filter(gattlib_adapter_t* adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data)
|
||||
{
|
||||
struct gattlib_adapter *gattlib_adapter = adapter;
|
||||
GDBusObjectManager *device_manager;
|
||||
GError *error = NULL;
|
||||
GVariantBuilder arg_properties_builder;
|
||||
GVariant *rssi_variant = NULL;
|
||||
int ret;
|
||||
|
||||
if ((gattlib_adapter == NULL) || (gattlib_adapter->adapter_proxy == NULL)) {
|
||||
if ((adapter == NULL) || (adapter->backend.adapter_proxy == NULL)) {
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "Could not start BLE scan. No opened bluetooth adapter");
|
||||
return GATTLIB_NO_ADAPTER;
|
||||
}
|
||||
|
@ -374,7 +372,7 @@ static int _gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid
|
|||
g_variant_builder_add(&arg_properties_builder, "{sv}", "RSSI", rssi_variant);
|
||||
}
|
||||
|
||||
org_bluez_adapter1_call_set_discovery_filter_sync(gattlib_adapter->adapter_proxy,
|
||||
org_bluez_adapter1_call_set_discovery_filter_sync(adapter->backend.adapter_proxy,
|
||||
g_variant_builder_end(&arg_properties_builder), NULL, &error);
|
||||
|
||||
if (rssi_variant) {
|
||||
|
@ -394,7 +392,7 @@ static int _gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid
|
|||
// We should get notified when the connection is lost with the target to allow
|
||||
// us to advertise us again
|
||||
//
|
||||
device_manager = get_device_manager_from_adapter(gattlib_adapter, &error);
|
||||
device_manager = get_device_manager_from_adapter(adapter, &error);
|
||||
if (device_manager == NULL) {
|
||||
if (error != NULL) {
|
||||
ret = GATTLIB_ERROR_DBUS_WITH_ERROR(error);
|
||||
|
@ -406,30 +404,30 @@ static int _gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid
|
|||
}
|
||||
|
||||
// Clear BLE scan structure
|
||||
memset(&gattlib_adapter->ble_scan, 0, sizeof(gattlib_adapter->ble_scan));
|
||||
gattlib_adapter->ble_scan.enabled_filters = enabled_filters;
|
||||
gattlib_adapter->ble_scan.ble_scan_timeout = timeout;
|
||||
gattlib_adapter->ble_scan.discovered_device_callback.callback.discovered_device = discovered_device_cb;
|
||||
gattlib_adapter->ble_scan.discovered_device_callback.user_data = user_data;
|
||||
memset(&adapter->backend.ble_scan, 0, sizeof(adapter->backend.ble_scan));
|
||||
adapter->backend.ble_scan.enabled_filters = enabled_filters;
|
||||
adapter->backend.ble_scan.ble_scan_timeout = timeout;
|
||||
adapter->discovered_device_callback.callback.discovered_device = discovered_device_cb;
|
||||
adapter->discovered_device_callback.user_data = user_data;
|
||||
|
||||
gattlib_adapter->ble_scan.added_signal_id = g_signal_connect(G_DBUS_OBJECT_MANAGER(device_manager),
|
||||
adapter->backend.ble_scan.added_signal_id = g_signal_connect(G_DBUS_OBJECT_MANAGER(device_manager),
|
||||
"object-added",
|
||||
G_CALLBACK(on_dbus_object_added),
|
||||
gattlib_adapter);
|
||||
adapter);
|
||||
|
||||
gattlib_adapter->ble_scan.removed_signal_id = g_signal_connect(G_DBUS_OBJECT_MANAGER(device_manager),
|
||||
adapter->backend.ble_scan.removed_signal_id = g_signal_connect(G_DBUS_OBJECT_MANAGER(device_manager),
|
||||
"object-removed",
|
||||
G_CALLBACK(on_dbus_object_removed),
|
||||
gattlib_adapter);
|
||||
adapter);
|
||||
|
||||
// List for object changes to see if there are still devices around
|
||||
gattlib_adapter->ble_scan.changed_signal_id = g_signal_connect(G_DBUS_OBJECT_MANAGER(device_manager),
|
||||
adapter->backend.ble_scan.changed_signal_id = g_signal_connect(G_DBUS_OBJECT_MANAGER(device_manager),
|
||||
"interface-proxy-properties-changed",
|
||||
G_CALLBACK(on_interface_proxy_properties_changed),
|
||||
gattlib_adapter);
|
||||
adapter);
|
||||
|
||||
// Now, start BLE discovery
|
||||
org_bluez_adapter1_call_start_discovery_sync(gattlib_adapter->adapter_proxy, NULL, &error);
|
||||
org_bluez_adapter1_call_start_discovery_sync(adapter->backend.adapter_proxy, NULL, &error);
|
||||
if (error) {
|
||||
ret = GATTLIB_ERROR_DBUS_WITH_ERROR(error);
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "Failed to start discovery: %s", error->message);
|
||||
|
@ -441,10 +439,9 @@ static int _gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid
|
|||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
|
||||
int gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
int gattlib_adapter_scan_enable_with_filter(gattlib_adapter_t* adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data)
|
||||
{
|
||||
struct gattlib_adapter *gattlib_adapter = adapter;
|
||||
GError *error = NULL;
|
||||
int ret;
|
||||
|
||||
|
@ -454,32 +451,31 @@ int gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid_list, i
|
|||
return ret;
|
||||
}
|
||||
|
||||
gattlib_adapter->ble_scan.is_scanning = true;
|
||||
adapter->backend.ble_scan.is_scanning = true;
|
||||
|
||||
gattlib_adapter->ble_scan.scan_loop_thread = g_thread_try_new("gattlib_ble_scan", _ble_scan_loop_thread, gattlib_adapter, &error);
|
||||
if (gattlib_adapter->ble_scan.scan_loop_thread == NULL) {
|
||||
adapter->backend.ble_scan.scan_loop_thread = g_thread_try_new("gattlib_ble_scan", _ble_scan_loop_thread, adapter, &error);
|
||||
if (adapter->backend.ble_scan.scan_loop_thread == NULL) {
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "Failed to create BLE scan thread: %s", error->message);
|
||||
g_error_free(error);
|
||||
return GATTLIB_ERROR_INTERNAL;
|
||||
}
|
||||
|
||||
g_mutex_lock(&gattlib_adapter->ble_scan.scan_loop.mutex);
|
||||
while (gattlib_adapter->ble_scan.is_scanning) {
|
||||
g_cond_wait(&gattlib_adapter->ble_scan.scan_loop.cond, &gattlib_adapter->ble_scan.scan_loop.mutex);
|
||||
g_mutex_lock(&adapter->backend.ble_scan.scan_loop.mutex);
|
||||
while (adapter->backend.ble_scan.is_scanning) {
|
||||
g_cond_wait(&adapter->backend.ble_scan.scan_loop.cond, &adapter->backend.ble_scan.scan_loop.mutex);
|
||||
}
|
||||
|
||||
// Free thread
|
||||
g_thread_unref(gattlib_adapter->ble_scan.scan_loop_thread);
|
||||
gattlib_adapter->ble_scan.scan_loop_thread = NULL;
|
||||
g_mutex_unlock(&gattlib_adapter->ble_scan.scan_loop.mutex);
|
||||
g_thread_unref(adapter->backend.ble_scan.scan_loop_thread);
|
||||
adapter->backend.ble_scan.scan_loop_thread = NULL;
|
||||
g_mutex_unlock(&adapter->backend.ble_scan.scan_loop.mutex);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gattlib_adapter_scan_enable_with_filter_non_blocking(void *adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
int gattlib_adapter_scan_enable_with_filter_non_blocking(gattlib_adapter_t* adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data)
|
||||
{
|
||||
struct gattlib_adapter *gattlib_adapter = adapter;
|
||||
GError *error = NULL;
|
||||
int ret;
|
||||
|
||||
|
@ -489,8 +485,8 @@ int gattlib_adapter_scan_enable_with_filter_non_blocking(void *adapter, uuid_t *
|
|||
return ret;
|
||||
}
|
||||
|
||||
gattlib_adapter->ble_scan.scan_loop_thread = g_thread_try_new("gattlib_ble_scan", _ble_scan_loop_thread, gattlib_adapter, &error);
|
||||
if (gattlib_adapter->ble_scan.scan_loop_thread == NULL) {
|
||||
adapter->backend.ble_scan.scan_loop_thread = g_thread_try_new("gattlib_ble_scan", _ble_scan_loop_thread, adapter, &error);
|
||||
if (adapter->backend.ble_scan.scan_loop_thread == NULL) {
|
||||
GATTLIB_LOG(GATTLIB_ERROR, "Failed to create BLE scan thread: %s", error->message);
|
||||
g_error_free(error);
|
||||
return GATTLIB_ERROR_INTERNAL;
|
||||
|
@ -499,7 +495,7 @@ int gattlib_adapter_scan_enable_with_filter_non_blocking(void *adapter, uuid_t *
|
|||
return 0;
|
||||
}
|
||||
|
||||
int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data)
|
||||
int gattlib_adapter_scan_enable(gattlib_adapter_t* adapter, gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data)
|
||||
{
|
||||
return gattlib_adapter_scan_enable_with_filter(adapter,
|
||||
NULL, 0 /* RSSI Threshold */,
|
||||
|
@ -507,28 +503,27 @@ int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t disco
|
|||
discovered_device_cb, timeout, user_data);
|
||||
}
|
||||
|
||||
int gattlib_adapter_scan_disable(void* adapter) {
|
||||
struct gattlib_adapter *gattlib_adapter = adapter;
|
||||
int gattlib_adapter_scan_disable(gattlib_adapter_t* adapter) {
|
||||
GError *error = NULL;
|
||||
|
||||
if (gattlib_adapter->adapter_proxy == NULL) {
|
||||
if (adapter->backend.adapter_proxy == NULL) {
|
||||
GATTLIB_LOG(GATTLIB_INFO, "Could not disable BLE scan. No BLE adapter setup.");
|
||||
return GATTLIB_NO_ADAPTER;
|
||||
}
|
||||
|
||||
g_mutex_lock(&gattlib_adapter->ble_scan.scan_loop.mutex);
|
||||
g_mutex_lock(&adapter->backend.ble_scan.scan_loop.mutex);
|
||||
|
||||
if (!org_bluez_adapter1_get_discovering(gattlib_adapter->adapter_proxy)) {
|
||||
if (!org_bluez_adapter1_get_discovering(adapter->backend.adapter_proxy)) {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "No discovery in progress. We skip discovery stopping (1).");
|
||||
goto EXIT;
|
||||
} else if (!gattlib_adapter->ble_scan.is_scanning) {
|
||||
} else if (!adapter->backend.ble_scan.is_scanning) {
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "No discovery in progress. We skip discovery stopping (2).");
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "Stop bluetooth scan.");
|
||||
|
||||
org_bluez_adapter1_call_stop_discovery_sync(gattlib_adapter->adapter_proxy, NULL, &error);
|
||||
org_bluez_adapter1_call_stop_discovery_sync(adapter->backend.adapter_proxy, NULL, &error);
|
||||
if (error != NULL) {
|
||||
if (((error->domain == 238) || (error->domain == 239)) && (error->code == 36)) {
|
||||
// Correspond to error: GDBus.Error:org.bluez.Error.Failed: No discovery started
|
||||
|
@ -539,27 +534,26 @@ int gattlib_adapter_scan_disable(void* adapter) {
|
|||
}
|
||||
|
||||
// Free and reset callback to stop calling it after we stopped
|
||||
gattlib_handler_free(&gattlib_adapter->ble_scan.discovered_device_callback);
|
||||
gattlib_handler_free(&adapter->discovered_device_callback);
|
||||
|
||||
// Stop BLE scan loop thread
|
||||
if (gattlib_adapter->ble_scan.is_scanning) {
|
||||
gattlib_adapter->ble_scan.is_scanning = false;
|
||||
g_cond_broadcast(&gattlib_adapter->ble_scan.scan_loop.cond);
|
||||
if (adapter->backend.ble_scan.is_scanning) {
|
||||
adapter->backend.ble_scan.is_scanning = false;
|
||||
g_cond_broadcast(&adapter->backend.ble_scan.scan_loop.cond);
|
||||
}
|
||||
|
||||
// Remove timeout
|
||||
if (gattlib_adapter->ble_scan.ble_scan_timeout_id) {
|
||||
g_source_remove(gattlib_adapter->ble_scan.ble_scan_timeout_id);
|
||||
gattlib_adapter->ble_scan.ble_scan_timeout_id = 0;
|
||||
if (adapter->backend.ble_scan.ble_scan_timeout_id) {
|
||||
g_source_remove(adapter->backend.ble_scan.ble_scan_timeout_id);
|
||||
adapter->backend.ble_scan.ble_scan_timeout_id = 0;
|
||||
}
|
||||
|
||||
EXIT:
|
||||
g_mutex_unlock(&gattlib_adapter->ble_scan.scan_loop.mutex);
|
||||
g_mutex_unlock(&adapter->backend.ble_scan.scan_loop.mutex);
|
||||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
|
||||
int gattlib_adapter_close(void* adapter) {
|
||||
struct gattlib_adapter *gattlib_adapter = adapter;
|
||||
int gattlib_adapter_close(gattlib_adapter_t* adapter) {
|
||||
bool are_devices_disconnected;
|
||||
|
||||
are_devices_disconnected = gattlib_devices_are_disconnected(adapter);
|
||||
|
@ -575,44 +569,44 @@ int gattlib_adapter_close(void* adapter) {
|
|||
goto EXIT;
|
||||
}
|
||||
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "Close bluetooth adapter %s", gattlib_adapter->adapter_name);
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "Close bluetooth adapter %s", adapter->name);
|
||||
|
||||
if (gattlib_adapter->ble_scan.is_scanning) {
|
||||
gattlib_adapter_scan_disable(gattlib_adapter);
|
||||
if (adapter->backend.ble_scan.is_scanning) {
|
||||
gattlib_adapter_scan_disable(adapter);
|
||||
|
||||
_wait_scan_loop_stop_scanning(gattlib_adapter);
|
||||
g_thread_join(gattlib_adapter->ble_scan.scan_loop_thread);
|
||||
_wait_scan_loop_stop_scanning(adapter);
|
||||
g_thread_join(adapter->backend.ble_scan.scan_loop_thread);
|
||||
}
|
||||
|
||||
// Ensure the thread is freed on adapter closing
|
||||
if (gattlib_adapter->ble_scan.scan_loop_thread) {
|
||||
g_thread_unref(gattlib_adapter->ble_scan.scan_loop_thread);
|
||||
gattlib_adapter->ble_scan.scan_loop_thread = NULL;
|
||||
if (adapter->backend.ble_scan.scan_loop_thread) {
|
||||
g_thread_unref(adapter->backend.ble_scan.scan_loop_thread);
|
||||
adapter->backend.ble_scan.scan_loop_thread = NULL;
|
||||
}
|
||||
|
||||
if (gattlib_adapter->device_manager) {
|
||||
g_object_unref(gattlib_adapter->device_manager);
|
||||
gattlib_adapter->device_manager = NULL;
|
||||
if (adapter->backend.device_manager) {
|
||||
g_object_unref(adapter->backend.device_manager);
|
||||
adapter->backend.device_manager = NULL;
|
||||
}
|
||||
|
||||
if (gattlib_adapter->adapter_proxy != NULL) {
|
||||
g_object_unref(gattlib_adapter->adapter_proxy);
|
||||
gattlib_adapter->adapter_proxy = NULL;
|
||||
if (adapter->backend.adapter_proxy != NULL) {
|
||||
g_object_unref(adapter->backend.adapter_proxy);
|
||||
adapter->backend.adapter_proxy = NULL;
|
||||
}
|
||||
|
||||
if (gattlib_adapter->adapter_name != NULL) {
|
||||
free(gattlib_adapter->adapter_name);
|
||||
gattlib_adapter->adapter_name = NULL;
|
||||
if (adapter->name != NULL) {
|
||||
free(adapter->name);
|
||||
adapter->name = NULL;
|
||||
}
|
||||
|
||||
gattlib_devices_free(gattlib_adapter);
|
||||
gattlib_devices_free(adapter);
|
||||
|
||||
free(gattlib_adapter);
|
||||
free(adapter);
|
||||
|
||||
// Remove adapter from the global list
|
||||
m_adapter_list = g_slist_remove(m_adapter_list, gattlib_adapter);
|
||||
m_adapter_list = g_slist_remove(m_adapter_list, adapter);
|
||||
|
||||
gattlib_adapter = NULL;
|
||||
adapter = NULL;
|
||||
|
||||
EXIT:
|
||||
g_mutex_unlock(&m_adapter_list_mutex);
|
||||
|
|
|
@ -8,14 +8,14 @@
|
|||
|
||||
#if BLUEZ_VERSION < BLUEZ_VERSIONS(5, 40)
|
||||
|
||||
int gattlib_get_advertisement_data(gatt_connection_t *connection,
|
||||
int gattlib_get_advertisement_data(gattlib_connection_t *connection,
|
||||
gattlib_advertisement_data_t **advertisement_data, size_t *advertisement_data_count,
|
||||
uint16_t *manufacturer_id, uint8_t **manufacturer_data, size_t *manufacturer_data_size)
|
||||
{
|
||||
return GATTLIB_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
int gattlib_get_advertisement_data_from_mac(void *adapter, const char *mac_address,
|
||||
int gattlib_get_advertisement_data_from_mac(gattlib_adapter_t* adapter, const char *mac_address,
|
||||
gattlib_advertisement_data_t **advertisement_data, size_t *advertisement_data_count,
|
||||
uint16_t *manufacturer_id, uint8_t **manufacturer_data, size_t *manufacturer_data_size)
|
||||
{
|
||||
|
@ -112,24 +112,20 @@ int get_advertisement_data_from_device(OrgBluezDevice1 *bluez_device1,
|
|||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
|
||||
int gattlib_get_advertisement_data(gatt_connection_t *connection,
|
||||
int gattlib_get_advertisement_data(gattlib_connection_t *connection,
|
||||
gattlib_advertisement_data_t **advertisement_data, size_t *advertisement_data_count,
|
||||
uint16_t *manufacturer_id, uint8_t **manufacturer_data, size_t *manufacturer_data_size)
|
||||
{
|
||||
gattlib_context_t* conn_context;
|
||||
|
||||
if (connection == NULL) {
|
||||
return GATTLIB_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
conn_context = connection->context;
|
||||
|
||||
return get_advertisement_data_from_device(conn_context->device,
|
||||
return get_advertisement_data_from_device(connection->backend.device,
|
||||
advertisement_data, advertisement_data_count,
|
||||
manufacturer_id, manufacturer_data, manufacturer_data_size);
|
||||
}
|
||||
|
||||
int gattlib_get_advertisement_data_from_mac(void *adapter, const char *mac_address,
|
||||
int gattlib_get_advertisement_data_from_mac(gattlib_adapter_t* adapter, const char *mac_address,
|
||||
gattlib_advertisement_data_t **advertisement_data, size_t *advertisement_data_count,
|
||||
uint16_t *manufacturer_id, uint8_t **manufacturer_data, size_t *manufacturer_data_size)
|
||||
{
|
||||
|
|
|
@ -4,13 +4,12 @@
|
|||
* Copyright (c) 2016-2024, Olivier Martin <olivier@labapart.org>
|
||||
*/
|
||||
|
||||
#ifndef __GATTLIB_INTERNAL_H__
|
||||
#define __GATTLIB_INTERNAL_H__
|
||||
#ifndef __GATTLIB_BACKEND_H__
|
||||
#define __GATTLIB_BACKEND_H__
|
||||
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "gattlib_internal_defs.h"
|
||||
#include "gattlib.h"
|
||||
|
||||
#include "org-bluez-adaptater1.h"
|
||||
|
@ -37,9 +36,7 @@
|
|||
|
||||
#define GATTLIB_DEFAULT_ADAPTER "hci0"
|
||||
|
||||
typedef struct {
|
||||
struct gattlib_adapter *adapter;
|
||||
|
||||
struct _gattlib_connection_backend {
|
||||
char* device_object_path;
|
||||
OrgBluezDevice1* device;
|
||||
|
||||
|
@ -54,16 +51,12 @@ typedef struct {
|
|||
|
||||
// List of 'OrgBluezGattCharacteristic1*' which has an attached notification
|
||||
GList *notified_characteristics;
|
||||
} gattlib_context_t;
|
||||
};
|
||||
|
||||
struct gattlib_adapter {
|
||||
struct _gattlib_adapter_backend {
|
||||
GDBusObjectManager *device_manager;
|
||||
|
||||
OrgBluezAdapter1 *adapter_proxy;
|
||||
char* adapter_name;
|
||||
|
||||
// The recursive mutex allows to ensure sensible operations are always covered by a mutex in a same thread
|
||||
GRecMutex mutex;
|
||||
|
||||
// Internal attributes only needed during BLE scanning
|
||||
struct {
|
||||
|
@ -82,13 +75,7 @@ struct gattlib_adapter {
|
|||
} scan_loop;
|
||||
|
||||
uint32_t enabled_filters;
|
||||
|
||||
struct gattlib_handler discovered_device_callback;
|
||||
} ble_scan;
|
||||
|
||||
// List of `struct _gattlib_device`. This list allows to know weither a device is
|
||||
// discovered/disconnected/connecting/connected/disconnecting.
|
||||
GSList *devices;
|
||||
};
|
||||
|
||||
struct dbus_characteristic {
|
||||
|
@ -107,24 +94,24 @@ struct dbus_characteristic {
|
|||
|
||||
extern const uuid_t m_battery_level_uuid;
|
||||
|
||||
struct gattlib_adapter *init_default_adapter(void);
|
||||
GDBusObjectManager *get_device_manager_from_adapter(struct gattlib_adapter *gattlib_adapter, GError **error);
|
||||
struct _gattlib_adapter *init_default_adapter(void);
|
||||
GDBusObjectManager *get_device_manager_from_adapter(gattlib_adapter_t* gattlib_adapter, GError **error);
|
||||
|
||||
void get_device_path_from_mac_with_adapter(OrgBluezAdapter1* adapter, const char *mac_address, char *object_path, size_t object_path_len);
|
||||
void get_device_path_from_mac(const char *adapter_name, const char *mac_address, char *object_path, size_t object_path_len);
|
||||
int get_bluez_device_from_mac(struct gattlib_adapter *adapter, const char *mac_address, OrgBluezDevice1 **bluez_device1);
|
||||
int get_bluez_device_from_mac(struct _gattlib_adapter *adapter, const char *mac_address, OrgBluezDevice1 **bluez_device1);
|
||||
|
||||
struct dbus_characteristic get_characteristic_from_uuid(gatt_connection_t* connection, const uuid_t* uuid);
|
||||
struct dbus_characteristic get_characteristic_from_uuid(gattlib_connection_t* connection, const uuid_t* uuid);
|
||||
|
||||
// Invoke when a new device has been discovered
|
||||
void gattlib_on_discovered_device(struct gattlib_adapter* gattlib_adapter, OrgBluezDevice1* device1);
|
||||
void gattlib_on_discovered_device(gattlib_adapter_t* gattlib_adapter, OrgBluezDevice1* device1);
|
||||
// Invoke when a new device is being connected
|
||||
void gattlib_on_connected_device(gatt_connection_t* connection);
|
||||
void gattlib_on_connected_device(gattlib_connection_t* connection);
|
||||
// Invoke when a new device is being disconnected
|
||||
void gattlib_on_disconnected_device(gatt_connection_t* connection);
|
||||
void gattlib_on_disconnected_device(gattlib_connection_t* connection);
|
||||
// Invoke when a new device receive a GATT notification
|
||||
void gattlib_on_gatt_notification(gatt_connection_t* connection, const uuid_t* uuid, const uint8_t* data, size_t data_length);
|
||||
void gattlib_on_gatt_notification(gattlib_connection_t* connection, const uuid_t* uuid, const uint8_t* data, size_t data_length);
|
||||
|
||||
void disconnect_all_notifications(gattlib_context_t* conn_context);
|
||||
void disconnect_all_notifications(struct _gattlib_connection_backend* backend);
|
||||
|
||||
#endif
|
|
@ -18,7 +18,7 @@ const uuid_t m_battery_level_uuid = CREATE_UUID16(0x2A19);
|
|||
static const uuid_t m_ccc_uuid = CREATE_UUID16(0x2902);
|
||||
|
||||
|
||||
static bool handle_dbus_gattcharacteristic_from_path(gattlib_context_t* conn_context, const uuid_t* uuid,
|
||||
static bool handle_dbus_gattcharacteristic_from_path(struct _gattlib_connection_backend* backend, const uuid_t* uuid,
|
||||
struct dbus_characteristic *dbus_characteristic, const char* object_path, GError **error)
|
||||
{
|
||||
OrgBluezGattCharacteristic1 *characteristic = NULL;
|
||||
|
@ -61,7 +61,7 @@ static bool handle_dbus_gattcharacteristic_from_path(gattlib_context_t* conn_con
|
|||
error);
|
||||
|
||||
if (service) {
|
||||
const bool found = !strcmp(conn_context->device_object_path, org_bluez_gatt_service1_get_device(service));
|
||||
const bool found = !strcmp(backend->device_object_path, org_bluez_gatt_service1_get_device(service));
|
||||
|
||||
g_object_unref(service);
|
||||
|
||||
|
@ -79,7 +79,7 @@ static bool handle_dbus_gattcharacteristic_from_path(gattlib_context_t* conn_con
|
|||
}
|
||||
|
||||
#if BLUEZ_VERSION > BLUEZ_VERSIONS(5, 40)
|
||||
static bool handle_dbus_battery_from_uuid(gattlib_context_t* conn_context, const uuid_t* uuid,
|
||||
static bool handle_dbus_battery_from_uuid(struct _gattlib_connection_backend* backend, const uuid_t* uuid,
|
||||
struct dbus_characteristic *dbus_characteristic, const char* object_path, GError **error)
|
||||
{
|
||||
OrgBluezBattery1 *battery = NULL;
|
||||
|
@ -101,10 +101,9 @@ static bool handle_dbus_battery_from_uuid(gattlib_context_t* conn_context, const
|
|||
}
|
||||
#endif
|
||||
|
||||
struct dbus_characteristic get_characteristic_from_uuid(gatt_connection_t* connection, const uuid_t* uuid) {
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
struct dbus_characteristic get_characteristic_from_uuid(gattlib_connection_t* connection, const uuid_t* uuid) {
|
||||
GError *error = NULL;
|
||||
GDBusObjectManager *device_manager = get_device_manager_from_adapter(conn_context->adapter, &error);
|
||||
GDBusObjectManager *device_manager = get_device_manager_from_adapter(connection->device->adapter, &error);
|
||||
bool is_battery_level_uuid = false;
|
||||
|
||||
struct dbus_characteristic dbus_characteristic = {
|
||||
|
@ -130,7 +129,7 @@ struct dbus_characteristic get_characteristic_from_uuid(gatt_connection_t* conne
|
|||
}
|
||||
|
||||
GList *l;
|
||||
for (l = conn_context->dbus_objects; l != NULL; l = l->next) {
|
||||
for (l = connection->backend.dbus_objects; l != NULL; l = l->next) {
|
||||
GDBusInterface *interface;
|
||||
bool found = false;
|
||||
GDBusObject *object = l->data;
|
||||
|
@ -140,7 +139,7 @@ struct dbus_characteristic get_characteristic_from_uuid(gatt_connection_t* conne
|
|||
if (interface) {
|
||||
g_object_unref(interface);
|
||||
|
||||
found = handle_dbus_gattcharacteristic_from_path(conn_context, uuid, &dbus_characteristic, object_path, &error);
|
||||
found = handle_dbus_gattcharacteristic_from_path(&connection->backend, uuid, &dbus_characteristic, object_path, &error);
|
||||
if (found) {
|
||||
break;
|
||||
}
|
||||
|
@ -152,7 +151,7 @@ struct dbus_characteristic get_characteristic_from_uuid(gatt_connection_t* conne
|
|||
if (interface) {
|
||||
g_object_unref(interface);
|
||||
|
||||
found = handle_dbus_battery_from_uuid(conn_context, uuid, &dbus_characteristic, object_path, &error);
|
||||
found = handle_dbus_battery_from_uuid(&connection->backend, uuid, &dbus_characteristic, object_path, &error);
|
||||
if (found) {
|
||||
break;
|
||||
}
|
||||
|
@ -166,10 +165,9 @@ struct dbus_characteristic get_characteristic_from_uuid(gatt_connection_t* conne
|
|||
return dbus_characteristic;
|
||||
}
|
||||
|
||||
static struct dbus_characteristic get_characteristic_from_handle(gatt_connection_t* connection, unsigned int handle) {
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
static struct dbus_characteristic get_characteristic_from_handle(gattlib_connection_t* connection, unsigned int handle) {
|
||||
GError *error = NULL;
|
||||
GDBusObjectManager *device_manager = get_device_manager_from_adapter(conn_context->adapter, &error);
|
||||
GDBusObjectManager *device_manager = get_device_manager_from_adapter(connection->device->adapter, &error);
|
||||
unsigned int char_handle;
|
||||
|
||||
struct dbus_characteristic dbus_characteristic = {
|
||||
|
@ -186,7 +184,7 @@ static struct dbus_characteristic get_characteristic_from_handle(gatt_connection
|
|||
return dbus_characteristic;
|
||||
}
|
||||
|
||||
for (GList *l = conn_context->dbus_objects; l != NULL; l = l->next) {
|
||||
for (GList *l = connection->backend.dbus_objects; l != NULL; l = l->next) {
|
||||
GDBusInterface *interface;
|
||||
bool found;
|
||||
GDBusObject *object = l->data;
|
||||
|
@ -204,7 +202,7 @@ static struct dbus_characteristic get_characteristic_from_handle(gatt_connection
|
|||
continue;
|
||||
}
|
||||
|
||||
found = handle_dbus_gattcharacteristic_from_path(conn_context, NULL, &dbus_characteristic, object_path, &error);
|
||||
found = handle_dbus_gattcharacteristic_from_path(&connection->backend, NULL, &dbus_characteristic, object_path, &error);
|
||||
if (found) {
|
||||
break;
|
||||
}
|
||||
|
@ -273,7 +271,7 @@ static int read_battery_level(struct dbus_characteristic *dbus_characteristic, v
|
|||
}
|
||||
#endif
|
||||
|
||||
int gattlib_read_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, void **buffer, size_t *buffer_len) {
|
||||
int gattlib_read_char_by_uuid(gattlib_connection_t* connection, uuid_t* uuid, void **buffer, size_t *buffer_len) {
|
||||
struct dbus_characteristic dbus_characteristic = get_characteristic_from_uuid(connection, uuid);
|
||||
if (dbus_characteristic.type == TYPE_NONE) {
|
||||
return GATTLIB_NOT_FOUND;
|
||||
|
@ -296,7 +294,7 @@ int gattlib_read_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, void
|
|||
}
|
||||
}
|
||||
|
||||
int gattlib_read_char_by_uuid_async(gatt_connection_t* connection, uuid_t* uuid, gatt_read_cb_t gatt_read_cb) {
|
||||
int gattlib_read_char_by_uuid_async(gattlib_connection_t* connection, uuid_t* uuid, gatt_read_cb_t gatt_read_cb) {
|
||||
int ret = GATTLIB_SUCCESS;
|
||||
|
||||
struct dbus_characteristic dbus_characteristic = get_characteristic_from_uuid(connection, uuid);
|
||||
|
@ -389,7 +387,7 @@ static int write_char(struct dbus_characteristic *dbus_characteristic, const voi
|
|||
return ret;
|
||||
}
|
||||
|
||||
int gattlib_write_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len)
|
||||
int gattlib_write_char_by_uuid(gattlib_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len)
|
||||
{
|
||||
int ret;
|
||||
|
||||
|
@ -408,7 +406,7 @@ int gattlib_write_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, cons
|
|||
return ret;
|
||||
}
|
||||
|
||||
int gattlib_write_char_by_handle(gatt_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len)
|
||||
int gattlib_write_char_by_handle(gattlib_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len)
|
||||
{
|
||||
int ret;
|
||||
|
||||
|
@ -423,7 +421,7 @@ int gattlib_write_char_by_handle(gatt_connection_t* connection, uint16_t handle,
|
|||
return ret;
|
||||
}
|
||||
|
||||
int gattlib_write_without_response_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len)
|
||||
int gattlib_write_without_response_char_by_uuid(gattlib_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len)
|
||||
{
|
||||
int ret;
|
||||
|
||||
|
@ -442,7 +440,7 @@ int gattlib_write_without_response_char_by_uuid(gatt_connection_t* connection, u
|
|||
return ret;
|
||||
}
|
||||
|
||||
int gattlib_write_without_response_char_by_handle(gatt_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len)
|
||||
int gattlib_write_without_response_char_by_handle(gattlib_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len)
|
||||
{
|
||||
int ret;
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ gboolean on_handle_battery_level_property_change(
|
|||
gpointer user_data)
|
||||
{
|
||||
static guint8 percentage;
|
||||
gatt_connection_t* connection = user_data;
|
||||
gattlib_connection_t* connection = user_data;
|
||||
|
||||
GATTLIB_LOG(GATTLIB_DEBUG, "DBUS: on_handle_battery_level_property_change: changed_properties:%s invalidated_properties:%s",
|
||||
g_variant_print(arg_changed_properties, TRUE),
|
||||
|
@ -64,7 +64,7 @@ static gboolean on_handle_characteristic_property_change(
|
|||
const gchar *const *arg_invalidated_properties,
|
||||
gpointer user_data)
|
||||
{
|
||||
gatt_connection_t* connection = user_data;
|
||||
gattlib_connection_t* connection = user_data;
|
||||
|
||||
if (gattlib_has_valid_handler(&connection->notification)) {
|
||||
GVariantDict dict;
|
||||
|
@ -105,7 +105,7 @@ static gboolean on_handle_characteristic_indication(
|
|||
const gchar *const *arg_invalidated_properties,
|
||||
gpointer user_data)
|
||||
{
|
||||
gatt_connection_t* connection = user_data;
|
||||
gattlib_connection_t* connection = user_data;
|
||||
|
||||
if (gattlib_has_valid_handler(&connection->indication)) {
|
||||
// Retrieve 'Value' from 'arg_changed_properties'
|
||||
|
@ -141,8 +141,7 @@ static gboolean on_handle_characteristic_indication(
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
static int connect_signal_to_characteristic_uuid(gatt_connection_t* connection, const uuid_t* uuid, void *callback) {
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
static int connect_signal_to_characteristic_uuid(gattlib_connection_t* connection, const uuid_t* uuid, void *callback) {
|
||||
int ret;
|
||||
|
||||
assert(callback != NULL);
|
||||
|
@ -188,7 +187,7 @@ static int connect_signal_to_characteristic_uuid(gatt_connection_t* connection,
|
|||
notification_handle->gatt = dbus_characteristic.gatt;
|
||||
notification_handle->signal_id = signal_id;
|
||||
memcpy(¬ification_handle->uuid, uuid, sizeof(*uuid));
|
||||
conn_context->notified_characteristics = g_list_append(conn_context->notified_characteristics, notification_handle);
|
||||
connection->backend.notified_characteristics = g_list_append(connection->backend.notified_characteristics, notification_handle);
|
||||
|
||||
GError *error = NULL;
|
||||
org_bluez_gatt_characteristic1_call_start_notify_sync(dbus_characteristic.gatt, NULL, &error);
|
||||
|
@ -202,17 +201,16 @@ static int connect_signal_to_characteristic_uuid(gatt_connection_t* connection,
|
|||
}
|
||||
}
|
||||
|
||||
static int disconnect_signal_to_characteristic_uuid(gatt_connection_t* connection, const uuid_t* uuid, void *callback) {
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
static int disconnect_signal_to_characteristic_uuid(gattlib_connection_t* connection, const uuid_t* uuid, void *callback) {
|
||||
struct gattlib_notification_handle *notification_handle = NULL;
|
||||
|
||||
// Find notification handle
|
||||
for (GList *l = conn_context->notified_characteristics; l != NULL; l = l->next) {
|
||||
for (GList *l = connection->backend.notified_characteristics; l != NULL; l = l->next) {
|
||||
struct gattlib_notification_handle *notification_handle_ptr = l->data;
|
||||
if (gattlib_uuid_cmp(¬ification_handle_ptr->uuid, uuid) == GATTLIB_SUCCESS) {
|
||||
notification_handle = notification_handle_ptr;
|
||||
|
||||
conn_context->notified_characteristics = g_list_delete_link(conn_context->notified_characteristics, l);
|
||||
connection->backend.notified_characteristics = g_list_delete_link(connection->backend.notified_characteristics, l);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -238,19 +236,19 @@ static int disconnect_signal_to_characteristic_uuid(gatt_connection_t* connectio
|
|||
}
|
||||
}
|
||||
|
||||
int gattlib_notification_start(gatt_connection_t* connection, const uuid_t* uuid) {
|
||||
int gattlib_notification_start(gattlib_connection_t* connection, const uuid_t* uuid) {
|
||||
return connect_signal_to_characteristic_uuid(connection, uuid, on_handle_characteristic_property_change);
|
||||
}
|
||||
|
||||
int gattlib_notification_stop(gatt_connection_t* connection, const uuid_t* uuid) {
|
||||
int gattlib_notification_stop(gattlib_connection_t* connection, const uuid_t* uuid) {
|
||||
return disconnect_signal_to_characteristic_uuid(connection, uuid, on_handle_characteristic_property_change);
|
||||
}
|
||||
|
||||
int gattlib_indication_start(gatt_connection_t* connection, const uuid_t* uuid) {
|
||||
int gattlib_indication_start(gattlib_connection_t* connection, const uuid_t* uuid) {
|
||||
return connect_signal_to_characteristic_uuid(connection, uuid, on_handle_characteristic_indication);
|
||||
}
|
||||
|
||||
int gattlib_indication_stop(gatt_connection_t* connection, const uuid_t* uuid) {
|
||||
int gattlib_indication_stop(gattlib_connection_t* connection, const uuid_t* uuid) {
|
||||
return disconnect_signal_to_characteristic_uuid(connection, uuid, on_handle_characteristic_indication);
|
||||
}
|
||||
|
||||
|
@ -261,6 +259,6 @@ static void end_notification(void *notified_characteristic) {
|
|||
free(notification_handle);
|
||||
}
|
||||
|
||||
void disconnect_all_notifications(gattlib_context_t* conn_context) {
|
||||
g_list_free_full(g_steal_pointer(&conn_context->notified_characteristics), end_notification);
|
||||
void disconnect_all_notifications(struct _gattlib_connection_backend* backend) {
|
||||
g_list_free_full(g_steal_pointer(&backend->notified_characteristics), end_notification);
|
||||
}
|
||||
|
|
|
@ -12,24 +12,24 @@
|
|||
|
||||
#if BLUEZ_VERSION < BLUEZ_VERSIONS(5, 48)
|
||||
|
||||
int gattlib_write_char_by_uuid_stream_open(gatt_connection_t* connection, uuid_t* uuid, gatt_stream_t **stream, uint16_t *mtu)
|
||||
int gattlib_write_char_by_uuid_stream_open(gattlib_connection_t* connection, uuid_t* uuid, gattlib_stream_t **stream, uint16_t *mtu)
|
||||
{
|
||||
return GATTLIB_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
int gattlib_write_char_stream_write(gatt_stream_t *stream, const void *buffer, size_t buffer_len)
|
||||
int gattlib_write_char_stream_write(gattlib_stream_t *stream, const void *buffer, size_t buffer_len)
|
||||
{
|
||||
return GATTLIB_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
int gattlib_write_char_stream_close(gatt_stream_t *stream)
|
||||
int gattlib_write_char_stream_close(gattlib_stream_t *stream)
|
||||
{
|
||||
return GATTLIB_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int gattlib_write_char_by_uuid_stream_open(gatt_connection_t* connection, uuid_t* uuid, gatt_stream_t **stream, uint16_t *mtu)
|
||||
int gattlib_write_char_by_uuid_stream_open(gattlib_connection_t* connection, uuid_t* uuid, gattlib_stream_t **stream, uint16_t *mtu)
|
||||
{
|
||||
struct dbus_characteristic dbus_characteristic = get_characteristic_from_uuid(connection, uuid);
|
||||
GError *error = NULL;
|
||||
|
@ -67,12 +67,12 @@ int gattlib_write_char_by_uuid_stream_open(gatt_connection_t* connection, uuid_t
|
|||
}
|
||||
|
||||
// We abuse the pointer 'stream' to pass the 'File Descriptor'
|
||||
*stream = (gatt_stream_t*)(unsigned long)fd;
|
||||
*stream = (gattlib_stream_t*)(unsigned long)fd;
|
||||
|
||||
return GATTLIB_SUCCESS;
|
||||
}
|
||||
|
||||
int gattlib_write_char_stream_write(gatt_stream_t *stream, const void *buffer, size_t buffer_len)
|
||||
int gattlib_write_char_stream_write(gattlib_stream_t *stream, const void *buffer, size_t buffer_len)
|
||||
{
|
||||
ssize_t ret = write((unsigned long)stream, buffer, buffer_len);
|
||||
if (ret < 0) {
|
||||
|
@ -82,7 +82,7 @@ int gattlib_write_char_stream_write(gatt_stream_t *stream, const void *buffer, s
|
|||
}
|
||||
}
|
||||
|
||||
int gattlib_write_char_stream_close(gatt_stream_t *stream)
|
||||
int gattlib_write_char_stream_close(gattlib_stream_t *stream)
|
||||
{
|
||||
close((unsigned long)stream);
|
||||
return GATTLIB_SUCCESS;
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
|
||||
static const char* adapter_name;
|
||||
|
||||
static void ble_advertising_device(void *adapter, const char* addr, const char* name, void *user_data) {
|
||||
static void ble_advertising_device(gattlib_adapter_t* adapter, const char* addr, const char* name, void *user_data) {
|
||||
gattlib_advertisement_data_t *advertisement_data;
|
||||
size_t advertisement_data_count;
|
||||
uint16_t manufacturer_id;
|
||||
|
@ -63,7 +63,7 @@ static void ble_advertising_device(void *adapter, const char* addr, const char*
|
|||
}
|
||||
|
||||
static void* ble_task(void *arg) {
|
||||
void* adapter;
|
||||
gattlib_adapter_t* adapter;
|
||||
int ret;
|
||||
|
||||
ret = gattlib_adapter_open(adapter_name, &adapter);
|
||||
|
|
|
@ -44,12 +44,12 @@ static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|||
LIST_HEAD(listhead, connection_t) g_ble_connections;
|
||||
struct connection_t {
|
||||
pthread_t thread;
|
||||
void *adapter;
|
||||
gattlib_adapter_t* adapter;
|
||||
char* addr;
|
||||
LIST_ENTRY(connection_t) entries;
|
||||
};
|
||||
|
||||
static void on_device_connect(void *adapter, const char *dst, gatt_connection_t* connection, int error, void* user_data) {
|
||||
static void on_device_connect(gattlib_adapter_t* adapter, const char *dst, gattlib_connection_t* connection, int error, void* user_data) {
|
||||
gattlib_primary_service_t* services;
|
||||
gattlib_characteristic_t* characteristics;
|
||||
int services_count, characteristics_count;
|
||||
|
@ -107,7 +107,7 @@ static void *ble_connect_device(void *arg) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static void ble_discovered_device(void *adapter, const char* addr, const char* name, void *user_data) {
|
||||
static void ble_discovered_device(gattlib_adapter_t* adapter, const char* addr, const char* name, void *user_data) {
|
||||
struct connection_t *connection;
|
||||
int ret;
|
||||
|
||||
|
@ -135,7 +135,7 @@ static void ble_discovered_device(void *adapter, const char* addr, const char* n
|
|||
}
|
||||
|
||||
static void* ble_task(void* arg) {
|
||||
void* adapter;
|
||||
gattlib_adapter_t* adapter;
|
||||
int ret;
|
||||
|
||||
ret = gattlib_adapter_open(adapter_name, &adapter);
|
||||
|
|
|
@ -42,7 +42,7 @@ static pthread_cond_t m_connection_terminated = PTHREAD_COND_INITIALIZER;
|
|||
// declaring mutex
|
||||
static pthread_mutex_t m_connection_terminated_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
static void on_device_connect(void *adapter, const char *dst, gatt_connection_t* connection, int error, void* user_data) {
|
||||
static void on_device_connect(gattlib_adapter_t* adapter, const char *dst, gattlib_connection_t* connection, int error, void* user_data) {
|
||||
gattlib_primary_service_t* services;
|
||||
gattlib_characteristic_t* characteristics;
|
||||
int services_count, characteristics_count;
|
||||
|
@ -96,7 +96,7 @@ static int stricmp(char const *a, char const *b) {
|
|||
}
|
||||
}
|
||||
|
||||
static void ble_discovered_device(void *adapter, const char* addr, const char* name, void *user_data) {
|
||||
static void ble_discovered_device(gattlib_adapter_t* adapter, const char* addr, const char* name, void *user_data) {
|
||||
const char* reference_mac_address = user_data;
|
||||
int ret;
|
||||
|
||||
|
@ -114,7 +114,7 @@ static void ble_discovered_device(void *adapter, const char* addr, const char* n
|
|||
|
||||
static void* ble_task(void* arg) {
|
||||
char* addr = arg;
|
||||
void* adapter;
|
||||
gattlib_adapter_t* adapter;
|
||||
int ret;
|
||||
|
||||
ret = gattlib_adapter_open(adapter_name, &adapter);
|
||||
|
|
|
@ -43,7 +43,7 @@ const char* m_adapter_name;
|
|||
* @param manufacturer_data_size is the size of manufacturer_data
|
||||
* @param user_data Data defined when calling `gattlib_register_on_disconnect()`
|
||||
*/
|
||||
void on_eddystone_found(void *adapter, const char* addr, const char* name,
|
||||
void on_eddystone_found(gattlib_adapter_t* adapter, const char* addr, const char* name,
|
||||
gattlib_advertisement_data_t *advertisement_data, size_t advertisement_data_count,
|
||||
uint16_t manufacturer_id, uint8_t *manufacturer_data, size_t manufacturer_data_size,
|
||||
void *user_data)
|
||||
|
@ -81,7 +81,7 @@ void on_eddystone_found(void *adapter, const char* addr, const char* name,
|
|||
}
|
||||
|
||||
static void* ble_task(void* arg) {
|
||||
void* adapter = NULL;
|
||||
gattlib_adapter_t* adapter = NULL;
|
||||
int ret;
|
||||
|
||||
ret = gattlib_adapter_open(m_adapter_name, &adapter);
|
||||
|
|
|
@ -106,7 +106,7 @@ void indication_handler(const uuid_t* uuid, const uint8_t* data, size_t data_len
|
|||
rl_forced_update_display();
|
||||
}
|
||||
|
||||
static void connect_cb(gatt_connection_t* connection, void* user_data)
|
||||
static void connect_cb(gattlib_connection_t* connection, void* user_data)
|
||||
{
|
||||
if (connection == NULL) {
|
||||
got_error = TRUE;
|
||||
|
@ -148,9 +148,8 @@ done:
|
|||
|
||||
static gboolean primary(gpointer user_data)
|
||||
{
|
||||
struct _gattlib_device *connection = (struct _gattlib_device *)user_data;
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
GAttrib *attrib = conn_context->attrib;
|
||||
gattlib_device_t* connection = (gattlib_device_t* )user_data;
|
||||
GAttrib *attrib = connection->backend.attrib;
|
||||
char uuid_str[MAX_LEN_UUID_STR + 1];
|
||||
|
||||
if (opt_uuid)
|
||||
|
@ -176,7 +175,7 @@ static gboolean primary(gpointer user_data)
|
|||
|
||||
static gboolean characteristics(gpointer user_data)
|
||||
{
|
||||
gatt_connection_t* connection = (gatt_connection_t*)user_data;
|
||||
gattlib_connection_t* connection = (gattlib_connection_t*)user_data;
|
||||
gattlib_characteristic_t* characteristics;
|
||||
int characteristic_count, i;
|
||||
|
||||
|
@ -244,7 +243,7 @@ static void bt_uuid_to_uuid(bt_uuid_t* bt_uuid, uuid_t* uuid) {
|
|||
|
||||
static gboolean characteristics_read(gpointer user_data)
|
||||
{
|
||||
gatt_connection_t* connection = (gatt_connection_t*)user_data;
|
||||
gattlib_connection_t* connection = (gattlib_connection_t*)user_data;
|
||||
gattlib_context_t* conn_context = connection->context;
|
||||
GAttrib *attrib = conn_context->attrib;
|
||||
|
||||
|
@ -296,7 +295,7 @@ static void mainloop_quit(gpointer user_data)
|
|||
|
||||
static gboolean characteristics_write(gpointer user_data)
|
||||
{
|
||||
gattlib_context_t* conn_context = ((gatt_connection_t*)user_data)->context;
|
||||
gattlib_context_t* conn_context = ((gattlib_connection_t*)user_data)->context;
|
||||
GAttrib *attrib = conn_context->attrib;
|
||||
uint8_t *value;
|
||||
size_t len;
|
||||
|
@ -354,7 +353,7 @@ done:
|
|||
|
||||
static gboolean characteristics_write_req(gpointer user_data)
|
||||
{
|
||||
gattlib_context_t* conn_context = ((gatt_connection_t*)user_data)->context;
|
||||
gattlib_context_t* conn_context = ((gattlib_connection_t*)user_data)->context;
|
||||
GAttrib *attrib = conn_context->attrib;
|
||||
uint8_t *value;
|
||||
size_t len;
|
||||
|
@ -388,7 +387,7 @@ error:
|
|||
|
||||
static gboolean characteristics_desc(gpointer user_data)
|
||||
{
|
||||
gatt_connection_t* connection = (gatt_connection_t*)user_data;
|
||||
gattlib_connection_t* connection = (gattlib_connection_t*)user_data;
|
||||
gattlib_descriptor_t* descriptors;
|
||||
int descriptor_count, i;
|
||||
|
||||
|
@ -486,7 +485,7 @@ int main(int argc, char *argv[])
|
|||
GOptionContext *context;
|
||||
GOptionGroup *gatt_group, *params_group, *char_rw_group;
|
||||
GError *gerr = NULL;
|
||||
gatt_connection_t *connection;
|
||||
gattlib_connection_t *connection;
|
||||
unsigned long conn_options = 0;
|
||||
BtIOSecLevel sec_level;
|
||||
uint8_t dest_type;
|
||||
|
|
|
@ -45,7 +45,7 @@
|
|||
|
||||
#include "gattlib_internal_defs.h"
|
||||
|
||||
static gatt_connection_t* g_connection = NULL;
|
||||
static gattlib_connection_t* g_connection = NULL;
|
||||
static GMainLoop *event_loop;
|
||||
static GString *prompt;
|
||||
|
||||
|
@ -112,7 +112,7 @@ static void set_state(enum state st)
|
|||
rl_redisplay();
|
||||
}
|
||||
|
||||
static void connect_cb(gatt_connection_t* connection, void* user_data)
|
||||
static void connect_cb(gattlib_connection_t* connection, void* user_data)
|
||||
{
|
||||
if (connection == NULL) {
|
||||
set_state(STATE_DISCONNECTED);
|
||||
|
@ -278,7 +278,7 @@ static gboolean channel_watcher(GIOChannel *chan, GIOCondition cond,
|
|||
|
||||
static void cmd_connect(int argcp, char **argvp)
|
||||
{
|
||||
gatt_connection_t *connection;
|
||||
gattlib_connection_t *connection;
|
||||
unsigned long conn_options = 0;
|
||||
BtIOSecLevel sec_level;
|
||||
uint8_t dst_type;
|
||||
|
@ -327,9 +327,8 @@ static void cmd_connect(int argcp, char **argvp)
|
|||
if (connection == NULL) {
|
||||
set_state(STATE_DISCONNECTED);
|
||||
} else {
|
||||
struct _gattlib_device *gatt_connection = (struct _gattlib_device *)g_connection;
|
||||
gattlib_context_t* conn_context = gatt_connection->context;
|
||||
g_io_add_watch(conn_context->io, G_IO_HUP, channel_watcher, NULL);
|
||||
gattlib_device_t* gatt_connection = (gattlib_device_t* )g_connection;
|
||||
g_io_add_watch(gatt_connection->backend.io, G_IO_HUP, channel_watcher, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
#define NUS_CHARACTERISTIC_TX_UUID "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
|
||||
#define NUS_CHARACTERISTIC_RX_UUID "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
|
||||
|
||||
gatt_connection_t* m_connection;
|
||||
gattlib_connection_t* m_connection;
|
||||
|
||||
void notification_cb(const uuid_t* uuid, const uint8_t* data, size_t data_length, void* user_data) {
|
||||
int i;
|
||||
|
|
|
@ -61,7 +61,7 @@ static void usage(char *argv[]) {
|
|||
int main(int argc, char *argv[]) {
|
||||
int ret;
|
||||
int argid;
|
||||
gatt_connection_t* connection;
|
||||
gattlib_connection_t* connection;
|
||||
|
||||
if (argc < 3) {
|
||||
usage(argv);
|
||||
|
|
|
@ -53,7 +53,7 @@ static void usage(char *argv[]) {
|
|||
printf("%s <device_address> <read|write> <uuid> [<hex-value-to-write>]\n", argv[0]);
|
||||
}
|
||||
|
||||
static void on_device_connect(void *adapter, const char *dst, gatt_connection_t* connection, int error, void* user_data) {
|
||||
static void on_device_connect(gattlib_adapter_t* adapter, const char *dst, gattlib_connection_t* connection, int error, void* user_data) {
|
||||
int ret;
|
||||
size_t len;
|
||||
|
||||
|
@ -116,7 +116,7 @@ static int stricmp(char const *a, char const *b) {
|
|||
}
|
||||
}
|
||||
|
||||
static void ble_discovered_device(void *adapter, const char* addr, const char* name, void *user_data) {
|
||||
static void ble_discovered_device(gattlib_adapter_t* adapter, const char* addr, const char* name, void *user_data) {
|
||||
int ret;
|
||||
|
||||
if (stricmp(addr, m_argument.mac_address) != 0) {
|
||||
|
@ -133,7 +133,7 @@ static void ble_discovered_device(void *adapter, const char* addr, const char* n
|
|||
|
||||
static void* ble_task(void* arg) {
|
||||
char* addr = arg;
|
||||
void* adapter;
|
||||
gattlib_adapter_t* adapter;
|
||||
int ret;
|
||||
|
||||
ret = gattlib_adapter_open(m_argument.adapter_name, &adapter);
|
||||
|
|
|
@ -48,7 +48,7 @@ struct connect_ble_params {
|
|||
|
||||
void *connect_ble(void *arg) {
|
||||
struct connect_ble_params *params = arg;
|
||||
gatt_connection_t* connection;
|
||||
gattlib_connection_t* connection;
|
||||
int ret, i;
|
||||
size_t len;
|
||||
|
||||
|
|
|
@ -104,21 +104,21 @@ class GattlibAdvertisementData(Structure):
|
|||
("data_length", c_size_t)]
|
||||
|
||||
|
||||
# int gattlib_adapter_open(const char* adapter_name, void** adapter);
|
||||
# int gattlib_adapter_open(const char* adapter_name, gattlib_adapter_t** adapter);
|
||||
gattlib_adapter_open = gattlib.gattlib_adapter_open
|
||||
gattlib_adapter_open.argtypes = [c_char_p, POINTER(c_void_p)]
|
||||
|
||||
# const char *gattlib_adapter_get_name(void* adapter)
|
||||
# const char *gattlib_adapter_get_name(gattlib_adapter_t* adapter)
|
||||
gattlib_adapter_get_name = gattlib.gattlib_adapter_get_name
|
||||
gattlib_adapter_get_name.argtypes = [c_void_p]
|
||||
gattlib_adapter_get_name.restype = c_char_p
|
||||
|
||||
# void gattlib_discovered_device_python_callback(void *adapter, const char* addr, const char* name, void *user_data)
|
||||
# void gattlib_discovered_device_python_callback(gattlib_adapter_t* adapter, const char* addr, const char* name, void *user_data)
|
||||
gattlib_discovered_device_python_callback = gattlib.gattlib_discovered_device_python_callback
|
||||
gattlib_discovered_device_python_callback.argtypes = [c_void_p, c_char_p, c_char_p, py_object]
|
||||
gattlib_discovered_device_python_callback.restype = c_void_p
|
||||
|
||||
# void gattlib_connected_device_python_callback(void *adapter, const char *dst, gatt_connection_t* connection, int error, void* user_data);
|
||||
# void gattlib_connected_device_python_callback(gattlib_adapter_t* adapter, const char *dst, gattlib_connection_t* connection, int error, void* user_data);
|
||||
gattlib_connected_device_python_callback = gattlib.gattlib_connected_device_python_callback
|
||||
gattlib_connected_device_python_callback.argtypes = [c_void_p, c_char_p, c_void_p, c_int, py_object]
|
||||
gattlib_connected_device_python_callback.restype = c_void_p
|
||||
|
@ -138,33 +138,33 @@ gattlib_python_callback_args = gattlib.gattlib_python_callback_args
|
|||
gattlib_python_callback_args.argtypes = [py_object, py_object]
|
||||
gattlib_python_callback_args.restype = c_void_p
|
||||
|
||||
# int gattlib_adapter_scan_enable_with_filter_non_blocking(void *adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
# int gattlib_adapter_scan_enable_with_filter_non_blocking(gattlib_adapter_t* adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
# gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data)
|
||||
gattlib_adapter_scan_enable_with_filter_non_blocking = gattlib.gattlib_adapter_scan_enable_with_filter_non_blocking
|
||||
gattlib_adapter_scan_enable_with_filter_non_blocking.argtypes = [c_void_p, POINTER(POINTER(GattlibUuid)), c_int16, c_uint32, c_void_p, c_size_t, c_void_p]
|
||||
|
||||
# int gattlib_adapter_scan_eddystone(void *adapter, int16_t rssi_threshold, uint32_t eddsytone_types,
|
||||
# int gattlib_adapter_scan_eddystone(gattlib_adapter_t* adapter, int16_t rssi_threshold, uint32_t eddsytone_types,
|
||||
# gattlib_discovered_device_with_data_t discovered_device_cb, size_t timeout, void *user_data)
|
||||
gattlib_adapter_scan_eddystone = gattlib.gattlib_adapter_scan_eddystone
|
||||
gattlib_adapter_scan_eddystone.argtypes = [c_void_p, c_int16, c_uint32, py_object, c_size_t, py_object]
|
||||
|
||||
# int gattlib_connect(void *adapter, const char *dst, unsigned long options, gatt_connect_cb_t connect_cb, void* user_data)
|
||||
# int gattlib_connect(gattlib_adapter_t* adapter, const char *dst, unsigned long options, gatt_connect_cb_t connect_cb, void* user_data)
|
||||
gattlib_connect = gattlib.gattlib_connect
|
||||
gattlib_connect.argtypes = [c_void_p, c_char_p, c_ulong, c_void_p, c_void_p]
|
||||
|
||||
# int gattlib_disconnect(gatt_connection_t* connection, bool wait_disconnection);
|
||||
# int gattlib_disconnect(gattlib_connection_t* connection, bool wait_disconnection);
|
||||
gattlib_disconnect = gattlib.gattlib_disconnect
|
||||
gattlib_disconnect.argtypes = [c_void_p, c_bool]
|
||||
|
||||
# int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_service_t** services, int* services_count);
|
||||
# int gattlib_discover_primary(gattlib_connection_t* connection, gattlib_primary_service_t** services, int* services_count);
|
||||
gattlib_discover_primary = gattlib.gattlib_discover_primary
|
||||
gattlib_discover_primary.argtypes = [c_void_p, POINTER(POINTER(GattlibPrimaryService)), POINTER(c_int)]
|
||||
|
||||
# int gattlib_discover_char(gatt_connection_t* connection, gattlib_characteristic_t** characteristics, int* characteristic_count);
|
||||
# int gattlib_discover_char(gattlib_connection_t* connection, gattlib_characteristic_t** characteristics, int* characteristic_count);
|
||||
gattlib_discover_char = gattlib.gattlib_discover_char
|
||||
gattlib_discover_char.argtypes = [c_void_p, POINTER(POINTER(GattlibCharacteristic)), POINTER(c_int)]
|
||||
|
||||
# int gattlib_read_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, void** buffer, size_t* buffer_len);
|
||||
# int gattlib_read_char_by_uuid(gattlib_connection_t* connection, uuid_t* uuid, void** buffer, size_t* buffer_len);
|
||||
gattlib_read_char_by_uuid = gattlib.gattlib_read_char_by_uuid
|
||||
gattlib_read_char_by_uuid.argtypes = [c_void_p, POINTER(GattlibUuid), POINTER(c_void_p), POINTER(c_size_t)]
|
||||
|
||||
|
@ -172,49 +172,49 @@ gattlib_read_char_by_uuid.argtypes = [c_void_p, POINTER(GattlibUuid), POINTER(c_
|
|||
gattlib_characteristic_free_value = gattlib.gattlib_characteristic_free_value
|
||||
gattlib_characteristic_free_value.argtypes = [c_void_p]
|
||||
|
||||
# int gattlib_write_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len)
|
||||
# int gattlib_write_char_by_uuid(gattlib_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len)
|
||||
gattlib_write_char_by_uuid = gattlib.gattlib_write_char_by_uuid
|
||||
gattlib_write_char_by_uuid.argtypes = [c_void_p, POINTER(GattlibUuid), c_void_p, c_size_t]
|
||||
|
||||
# int gattlib_write_without_response_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len)
|
||||
# int gattlib_write_without_response_char_by_uuid(gattlib_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len)
|
||||
gattlib_write_without_response_char_by_uuid = gattlib.gattlib_write_without_response_char_by_uuid
|
||||
gattlib_write_without_response_char_by_uuid.argtypes = [c_void_p, POINTER(GattlibUuid), c_void_p, c_size_t]
|
||||
|
||||
# int gattlib_write_char_by_uuid_stream_open(gatt_connection_t* connection, uuid_t* uuid, gatt_stream_t **stream, uint16_t *mtu)
|
||||
# int gattlib_write_char_by_uuid_stream_open(gattlib_connection_t* connection, uuid_t* uuid, gattlib_stream_t **stream, uint16_t *mtu)
|
||||
gattlib_write_char_by_uuid_stream_open = gattlib.gattlib_write_char_by_uuid_stream_open
|
||||
gattlib_write_char_by_uuid_stream_open.argtypes = [c_void_p, POINTER(GattlibUuid), POINTER(c_void_p), POINTER(c_uint16)]
|
||||
|
||||
# int gattlib_notification_start(gatt_connection_t* connection, const uuid_t* uuid);
|
||||
# int gattlib_notification_start(gattlib_connection_t* connection, const uuid_t* uuid);
|
||||
gattlib_notification_start = gattlib.gattlib_notification_start
|
||||
gattlib_notification_start.argtypes = [c_void_p, POINTER(GattlibUuid)]
|
||||
|
||||
# int gattlib_notification_stop(gatt_connection_t* connection, const uuid_t* uuid);
|
||||
# int gattlib_notification_stop(gattlib_connection_t* connection, const uuid_t* uuid);
|
||||
gattlib_notification_stop = gattlib.gattlib_notification_stop
|
||||
gattlib_notification_stop.argtypes = [c_void_p, POINTER(GattlibUuid)]
|
||||
|
||||
# int gattlib_register_notification(gatt_connection_t* connection, gattlib_event_handler_t notification_handler, void* user_data);
|
||||
# int gattlib_register_notification(gattlib_connection_t* connection, gattlib_event_handler_t notification_handler, void* user_data);
|
||||
gattlib_register_notification = gattlib.gattlib_register_notification
|
||||
gattlib_register_notification.argtypes = [c_void_p, c_void_p, c_void_p]
|
||||
|
||||
# int gattlib_register_on_disconnect(gatt_connection_t *connection, PyObject *handler, PyObject *user_data)
|
||||
# int gattlib_register_on_disconnect(gattlib_connection_t *connection, PyObject *handler, PyObject *user_data)
|
||||
gattlib_register_on_disconnect = gattlib.gattlib_register_on_disconnect
|
||||
gattlib_register_on_disconnect.argtypes = [c_void_p, c_void_p, c_void_p]
|
||||
|
||||
# int gattlib_get_rssi(gatt_connection_t *connection, int16_t *rssi)
|
||||
# int gattlib_get_rssi(gattlib_connection_t *connection, int16_t *rssi)
|
||||
gattlib_get_rssi = gattlib.gattlib_get_rssi
|
||||
gattlib_get_rssi.argtypes = [c_void_p, POINTER(c_int16)]
|
||||
|
||||
# int gattlib_get_rssi_from_mac(void *adapter, const char *mac_address, int16_t *rssi)
|
||||
# int gattlib_get_rssi_from_mac(gattlib_adapter_t* adapter, const char *mac_address, int16_t *rssi)
|
||||
gattlib_get_rssi_from_mac = gattlib.gattlib_get_rssi_from_mac
|
||||
gattlib_get_rssi_from_mac.argtypes = [c_void_p, c_char_p, POINTER(c_int16)]
|
||||
|
||||
# int gattlib_get_advertisement_data(gatt_connection_t *connection,
|
||||
# int gattlib_get_advertisement_data(gattlib_connection_t *connection,
|
||||
# gattlib_advertisement_data_t **advertisement_data, size_t *advertisement_data_count,
|
||||
# uint16_t *manufacturer_id, uint8_t **manufacturer_data, size_t *manufacturer_data_size)
|
||||
gattlib_get_advertisement_data = gattlib.gattlib_get_advertisement_data
|
||||
gattlib_get_advertisement_data.argtypes = [c_void_p, POINTER(POINTER(GattlibAdvertisementData)), POINTER(c_size_t), POINTER(c_uint16), POINTER(c_void_p), POINTER(c_size_t)]
|
||||
|
||||
# int gattlib_get_advertisement_data_from_mac(void *adapter, const char *mac_address,
|
||||
# int gattlib_get_advertisement_data_from_mac(gattlib_adapter_t* adapter, const char *mac_address,
|
||||
# gattlib_advertisement_data_t **advertisement_data, size_t *advertisement_data_length,
|
||||
# uint16_t *manufacturer_id, uint8_t **manufacturer_data, size_t *manufacturer_data_size)
|
||||
gattlib_get_advertisement_data_from_mac = gattlib.gattlib_get_advertisement_data_from_mac
|
||||
|
|
|
@ -149,8 +149,9 @@ extern "C" {
|
|||
|
||||
#define GATTLIB_LOG(level, args...) if (level <= GATTLIB_LOG_LEVEL) { gattlib_log(level, args); }
|
||||
|
||||
typedef struct _gattlib_device gatt_connection_t;
|
||||
typedef struct _gatt_stream_t gatt_stream_t;
|
||||
typedef struct _gattlib_adapter gattlib_adapter_t;
|
||||
typedef struct _gattlib_connection gattlib_connection_t;
|
||||
typedef struct _gattlib_stream_t gattlib_stream_t;
|
||||
|
||||
/**
|
||||
* Structure to represent a GATT Service and its data in the BLE advertisement packet
|
||||
|
@ -169,7 +170,7 @@ typedef void (*gattlib_event_handler_t)(const uuid_t* uuid, const uint8_t* data,
|
|||
* @param connection Connection that is disconnecting
|
||||
* @param user_data Data defined when calling `gattlib_register_on_disconnect()`
|
||||
*/
|
||||
typedef void (*gattlib_disconnection_handler_t)(gatt_connection_t* connection, void* user_data);
|
||||
typedef void (*gattlib_disconnection_handler_t)(gattlib_connection_t* connection, void* user_data);
|
||||
|
||||
/**
|
||||
* @brief Handler called on new discovered BLE device
|
||||
|
@ -179,7 +180,7 @@ typedef void (*gattlib_disconnection_handler_t)(gatt_connection_t* connection, v
|
|||
* @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);
|
||||
typedef void (*gattlib_discovered_device_t)(gattlib_adapter_t* adapter, const char* addr, const char* name, void *user_data);
|
||||
|
||||
/**
|
||||
* @brief Handler called on new discovered BLE device
|
||||
|
@ -194,7 +195,7 @@ typedef void (*gattlib_discovered_device_t)(void *adapter, const char* addr, con
|
|||
* @param manufacturer_data_size is the size of manufacturer_data
|
||||
* @param user_data Data defined when calling `gattlib_register_on_disconnect()`
|
||||
*/
|
||||
typedef void (*gattlib_discovered_device_with_data_t)(void *adapter, const char* addr, const char* name,
|
||||
typedef void (*gattlib_discovered_device_with_data_t)(gattlib_adapter_t* adapter, const char* addr, const char* name,
|
||||
gattlib_advertisement_data_t *advertisement_data, size_t advertisement_data_count,
|
||||
uint16_t manufacturer_id, uint8_t *manufacturer_data, size_t manufacturer_data_size,
|
||||
void *user_data);
|
||||
|
@ -208,7 +209,7 @@ typedef void (*gattlib_discovered_device_with_data_t)(void *adapter, const char*
|
|||
* @param error Connection error code
|
||||
* @param user_data Data defined when calling `gattlib_register_on_disconnect()`
|
||||
*/
|
||||
typedef void (*gatt_connect_cb_t)(void *adapter, const char *dst, gatt_connection_t* connection, int error, void* user_data);
|
||||
typedef void (*gatt_connect_cb_t)(gattlib_adapter_t* adapter, const char *dst, gattlib_connection_t* connection, int error, void* user_data);
|
||||
|
||||
/**
|
||||
* @brief Callback called when GATT characteristic read value has been received
|
||||
|
@ -239,7 +240,7 @@ extern const char *gattlib_eddystone_url_scheme_prefix[];
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_adapter_open(const char* adapter_name, void** adapter);
|
||||
int gattlib_adapter_open(const char* adapter_name, gattlib_adapter_t** adapter);
|
||||
|
||||
/**
|
||||
* @brief Get adapter name
|
||||
|
@ -248,7 +249,7 @@ int gattlib_adapter_open(const char* adapter_name, void** adapter);
|
|||
*
|
||||
* @return Adapter name
|
||||
*/
|
||||
const char *gattlib_adapter_get_name(void* adapter);
|
||||
const char *gattlib_adapter_get_name(gattlib_adapter_t* adapter);
|
||||
|
||||
/**
|
||||
* @brief Enable Bluetooth scanning on a given adapter
|
||||
|
@ -260,7 +261,7 @@ const char *gattlib_adapter_get_name(void* adapter);
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data);
|
||||
int gattlib_adapter_scan_enable(gattlib_adapter_t* adapter, gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data);
|
||||
|
||||
/**
|
||||
* @brief Enable Bluetooth scanning on a given adapter
|
||||
|
@ -279,7 +280,7 @@ int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t disco
|
|||
*
|
||||
* @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,
|
||||
int gattlib_adapter_scan_enable_with_filter(gattlib_adapter_t* adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data);
|
||||
|
||||
/**
|
||||
|
@ -299,7 +300,7 @@ int gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid_list, i
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_adapter_scan_enable_with_filter_non_blocking(void *adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
int gattlib_adapter_scan_enable_with_filter_non_blocking(gattlib_adapter_t* adapter, uuid_t **uuid_list, int16_t rssi_threshold, uint32_t enabled_filters,
|
||||
gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data);
|
||||
|
||||
/**
|
||||
|
@ -318,7 +319,7 @@ int gattlib_adapter_scan_enable_with_filter_non_blocking(void *adapter, uuid_t *
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_adapter_scan_eddystone(void *adapter, int16_t rssi_threshold, uint32_t eddystone_types,
|
||||
int gattlib_adapter_scan_eddystone(gattlib_adapter_t* adapter, int16_t rssi_threshold, uint32_t eddystone_types,
|
||||
gattlib_discovered_device_with_data_t discovered_device_cb, size_t timeout, void *user_data);
|
||||
|
||||
/**
|
||||
|
@ -328,7 +329,7 @@ int gattlib_adapter_scan_eddystone(void *adapter, int16_t rssi_threshold, uint32
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_adapter_scan_disable(void* adapter);
|
||||
int gattlib_adapter_scan_disable(gattlib_adapter_t* adapter);
|
||||
|
||||
/**
|
||||
* @brief Close Bluetooth adapter context
|
||||
|
@ -337,7 +338,7 @@ int gattlib_adapter_scan_disable(void* adapter);
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_adapter_close(void* adapter);
|
||||
int gattlib_adapter_close(gattlib_adapter_t* adapter);
|
||||
|
||||
/**
|
||||
* @brief Function to asynchronously connect to a BLE device
|
||||
|
@ -352,7 +353,7 @@ int gattlib_adapter_close(void* adapter);
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_connect(void *adapter, const char *dst,
|
||||
int gattlib_connect(gattlib_adapter_t* adapter, const char *dst,
|
||||
unsigned long options,
|
||||
gatt_connect_cb_t connect_cb,
|
||||
void* user_data);
|
||||
|
@ -371,7 +372,7 @@ int gattlib_connect(void *adapter, const char *dst,
|
|||
* @return GATTLIB_TIMEOUT when wait_disconnection is true and the device has not been disconnected for
|
||||
* GATTLIB_DISCONNECTION_WAIT_TIMEOUT_SEC seconds
|
||||
*/
|
||||
int gattlib_disconnect(gatt_connection_t* connection, bool wait_disconnection);
|
||||
int gattlib_disconnect(gattlib_connection_t* connection, bool wait_disconnection);
|
||||
|
||||
/**
|
||||
* @brief Function to register a callback on GATT disconnection
|
||||
|
@ -382,7 +383,7 @@ int gattlib_disconnect(gatt_connection_t* connection, bool wait_disconnection);
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_register_on_disconnect(gatt_connection_t *connection, gattlib_disconnection_handler_t handler, void* user_data);
|
||||
int gattlib_register_on_disconnect(gattlib_connection_t *connection, gattlib_disconnection_handler_t handler, void* user_data);
|
||||
|
||||
/**
|
||||
* Structure to represent GATT Primary Service
|
||||
|
@ -423,7 +424,7 @@ typedef struct {
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_service_t** services, int* services_count);
|
||||
int gattlib_discover_primary(gattlib_connection_t* connection, gattlib_primary_service_t** services, int* services_count);
|
||||
|
||||
/**
|
||||
* @brief Function to discover GATT Characteristic
|
||||
|
@ -438,7 +439,7 @@ int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_serv
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_discover_char_range(gatt_connection_t* connection, uint16_t start, uint16_t end, gattlib_characteristic_t** characteristics, int* characteristics_count);
|
||||
int gattlib_discover_char_range(gattlib_connection_t* connection, uint16_t start, uint16_t end, gattlib_characteristic_t** characteristics, int* characteristics_count);
|
||||
|
||||
/**
|
||||
* @brief Function to discover GATT Characteristic
|
||||
|
@ -451,7 +452,7 @@ int gattlib_discover_char_range(gatt_connection_t* connection, uint16_t start, u
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_discover_char(gatt_connection_t* connection, gattlib_characteristic_t** characteristics, int* characteristics_count);
|
||||
int gattlib_discover_char(gattlib_connection_t* connection, gattlib_characteristic_t** characteristics, int* characteristics_count);
|
||||
|
||||
/**
|
||||
* @brief Function to discover GATT Descriptors in a range of handles
|
||||
|
@ -464,7 +465,7 @@ int gattlib_discover_char(gatt_connection_t* connection, gattlib_characteristic_
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_discover_desc_range(gatt_connection_t* connection, int start, int end, gattlib_descriptor_t** descriptors, int* descriptors_count);
|
||||
int gattlib_discover_desc_range(gattlib_connection_t* connection, int start, int end, gattlib_descriptor_t** descriptors, int* descriptors_count);
|
||||
|
||||
/**
|
||||
* @brief Function to discover GATT Descriptor
|
||||
|
@ -475,7 +476,7 @@ int gattlib_discover_desc_range(gatt_connection_t* connection, int start, int en
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_discover_desc(gatt_connection_t* connection, gattlib_descriptor_t** descriptors, int* descriptors_count);
|
||||
int gattlib_discover_desc(gattlib_connection_t* connection, gattlib_descriptor_t** descriptors, int* descriptors_count);
|
||||
|
||||
/**
|
||||
* @brief Function to read GATT characteristic
|
||||
|
@ -489,7 +490,7 @@ int gattlib_discover_desc(gatt_connection_t* connection, gattlib_descriptor_t**
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_read_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, void** buffer, size_t* buffer_len);
|
||||
int gattlib_read_char_by_uuid(gattlib_connection_t* connection, uuid_t* uuid, void** buffer, size_t* buffer_len);
|
||||
|
||||
/**
|
||||
* @brief Function to asynchronously read GATT characteristic
|
||||
|
@ -500,7 +501,7 @@ int gattlib_read_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, void*
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_read_char_by_uuid_async(gatt_connection_t* connection, uuid_t* uuid, gatt_read_cb_t gatt_read_cb);
|
||||
int gattlib_read_char_by_uuid_async(gattlib_connection_t* connection, uuid_t* uuid, gatt_read_cb_t gatt_read_cb);
|
||||
|
||||
/**
|
||||
* @brief Free buffer allocated by the characteristic reading to store the value
|
||||
|
@ -519,7 +520,7 @@ void gattlib_characteristic_free_value(void *ptr);
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_write_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len);
|
||||
int gattlib_write_char_by_uuid(gattlib_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len);
|
||||
|
||||
/**
|
||||
* @brief Function to write to the GATT characteristic handle
|
||||
|
@ -531,7 +532,7 @@ int gattlib_write_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, cons
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_write_char_by_handle(gatt_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len);
|
||||
int gattlib_write_char_by_handle(gattlib_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len);
|
||||
|
||||
/**
|
||||
* @brief Function to write without response to the GATT characteristic UUID
|
||||
|
@ -543,7 +544,7 @@ int gattlib_write_char_by_handle(gatt_connection_t* connection, uint16_t handle,
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_write_without_response_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len);
|
||||
int gattlib_write_without_response_char_by_uuid(gattlib_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len);
|
||||
|
||||
/**
|
||||
* @brief Create a stream to a GATT characteristic to write data in continue
|
||||
|
@ -557,7 +558,7 @@ int gattlib_write_without_response_char_by_uuid(gatt_connection_t* connection, u
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_write_char_by_uuid_stream_open(gatt_connection_t* connection, uuid_t* uuid, gatt_stream_t **stream, uint16_t *mtu);
|
||||
int gattlib_write_char_by_uuid_stream_open(gattlib_connection_t* connection, uuid_t* uuid, gattlib_stream_t **stream, uint16_t *mtu);
|
||||
|
||||
/**
|
||||
* @brief Write data to the stream previously created with `gattlib_write_char_by_uuid_stream_open()`
|
||||
|
@ -568,7 +569,7 @@ int gattlib_write_char_by_uuid_stream_open(gatt_connection_t* connection, uuid_t
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_write_char_stream_write(gatt_stream_t *stream, const void *buffer, size_t buffer_len);
|
||||
int gattlib_write_char_stream_write(gattlib_stream_t *stream, const void *buffer, size_t buffer_len);
|
||||
|
||||
/**
|
||||
* @brief Close the stream previously created with `gattlib_write_char_by_uuid_stream_open()`
|
||||
|
@ -577,7 +578,7 @@ int gattlib_write_char_stream_write(gatt_stream_t *stream, const void *buffer, s
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_write_char_stream_close(gatt_stream_t *stream);
|
||||
int gattlib_write_char_stream_close(gattlib_stream_t *stream);
|
||||
|
||||
/**
|
||||
* @brief Function to write without response to the GATT characteristic handle
|
||||
|
@ -589,7 +590,7 @@ int gattlib_write_char_stream_close(gatt_stream_t *stream);
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_write_without_response_char_by_handle(gatt_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len);
|
||||
int gattlib_write_without_response_char_by_handle(gattlib_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len);
|
||||
|
||||
/*
|
||||
* @brief Enable notification on GATT characteristic represented by its UUID
|
||||
|
@ -599,7 +600,7 @@ int gattlib_write_without_response_char_by_handle(gatt_connection_t* connection,
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_notification_start(gatt_connection_t* connection, const uuid_t* uuid);
|
||||
int gattlib_notification_start(gattlib_connection_t* connection, const uuid_t* uuid);
|
||||
|
||||
/*
|
||||
* @brief Disable notification on GATT characteristic represented by its UUID
|
||||
|
@ -609,7 +610,7 @@ int gattlib_notification_start(gatt_connection_t* connection, const uuid_t* uuid
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_notification_stop(gatt_connection_t* connection, const uuid_t* uuid);
|
||||
int gattlib_notification_stop(gattlib_connection_t* connection, const uuid_t* uuid);
|
||||
|
||||
/*
|
||||
* @brief Enable indication on GATT characteristic represented by its UUID
|
||||
|
@ -619,7 +620,7 @@ int gattlib_notification_stop(gatt_connection_t* connection, const uuid_t* uuid)
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_indication_start(gatt_connection_t* connection, const uuid_t* uuid);
|
||||
int gattlib_indication_start(gattlib_connection_t* connection, const uuid_t* uuid);
|
||||
|
||||
/*
|
||||
* @brief Disable indication on GATT characteristic represented by its UUID
|
||||
|
@ -629,7 +630,7 @@ int gattlib_indication_start(gatt_connection_t* connection, const uuid_t* uuid);
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_indication_stop(gatt_connection_t* connection, const uuid_t* uuid);
|
||||
int gattlib_indication_stop(gattlib_connection_t* connection, const uuid_t* uuid);
|
||||
|
||||
/*
|
||||
* @brief Register a handle for the GATT notifications
|
||||
|
@ -640,7 +641,7 @@ int gattlib_indication_stop(gatt_connection_t* connection, const uuid_t* uuid);
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_register_notification(gatt_connection_t* connection, gattlib_event_handler_t notification_handler, void* user_data);
|
||||
int gattlib_register_notification(gattlib_connection_t* connection, gattlib_event_handler_t notification_handler, void* user_data);
|
||||
|
||||
/*
|
||||
* @brief Register a handle for the GATT indications
|
||||
|
@ -651,7 +652,7 @@ int gattlib_register_notification(gatt_connection_t* connection, gattlib_event_h
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_register_indication(gatt_connection_t* connection, gattlib_event_handler_t indication_handler, void* user_data);
|
||||
int gattlib_register_indication(gattlib_connection_t* connection, gattlib_event_handler_t indication_handler, void* user_data);
|
||||
|
||||
#if 0 // Disable until https://github.com/labapart/gattlib/issues/75 is resolved
|
||||
/**
|
||||
|
@ -662,7 +663,7 @@ int gattlib_register_indication(gatt_connection_t* connection, gattlib_event_han
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_get_rssi(gatt_connection_t *connection, int16_t *rssi);
|
||||
int gattlib_get_rssi(gattlib_connection_t *connection, int16_t *rssi);
|
||||
#endif
|
||||
|
||||
/**
|
||||
|
@ -677,7 +678,7 @@ int gattlib_get_rssi(gatt_connection_t *connection, int16_t *rssi);
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_get_rssi_from_mac(void *adapter, const char *mac_address, int16_t *rssi);
|
||||
int gattlib_get_rssi_from_mac(gattlib_adapter_t* adapter, const char *mac_address, int16_t *rssi);
|
||||
|
||||
/**
|
||||
* @brief Function to retrieve Advertisement Data from a MAC Address
|
||||
|
@ -691,7 +692,7 @@ int gattlib_get_rssi_from_mac(void *adapter, const char *mac_address, int16_t *r
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_get_advertisement_data(gatt_connection_t *connection,
|
||||
int gattlib_get_advertisement_data(gattlib_connection_t *connection,
|
||||
gattlib_advertisement_data_t **advertisement_data, size_t *advertisement_data_count,
|
||||
uint16_t *manufacturer_id, uint8_t **manufacturer_data, size_t *manufacturer_data_size);
|
||||
|
||||
|
@ -708,7 +709,7 @@ int gattlib_get_advertisement_data(gatt_connection_t *connection,
|
|||
*
|
||||
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
|
||||
*/
|
||||
int gattlib_get_advertisement_data_from_mac(void *adapter, const char *mac_address,
|
||||
int gattlib_get_advertisement_data_from_mac(gattlib_adapter_t* adapter, const char *mac_address,
|
||||
gattlib_advertisement_data_t **advertisement_data, size_t *advertisement_data_count,
|
||||
uint16_t *manufacturer_id, uint8_t **manufacturer_data, size_t *manufacturer_data_size);
|
||||
|
||||
|
|
|
@ -49,7 +49,7 @@ static struct {
|
|||
bool value;
|
||||
} m_connection_terminated;
|
||||
|
||||
static void on_device_connect(void *adapter, const char *dst, gatt_connection_t* connection, int error, void* user_data) {
|
||||
static void on_device_connect(gattlib_adapter_t* adapter, const char *dst, gattlib_connection_t* connection, int error, void* user_data) {
|
||||
int ret;
|
||||
|
||||
if (error != 0) {
|
||||
|
@ -77,7 +77,7 @@ static int stricmp(char const *a, char const *b) {
|
|||
}
|
||||
}
|
||||
|
||||
static void ble_discovered_device(void *adapter, const char* addr, const char* name, void *user_data) {
|
||||
static void ble_discovered_device(gattlib_adapter_t* adapter, const char* addr, const char* name, void *user_data) {
|
||||
int ret;
|
||||
|
||||
if (stricmp(addr, reference_mac_address) != 0) {
|
||||
|
@ -117,7 +117,7 @@ static void ble_discovered_device(void *adapter, const char* addr, const char* n
|
|||
}
|
||||
|
||||
static void* ble_task(void* arg) {
|
||||
void* adapter;
|
||||
gattlib_adapter_t* adapter;
|
||||
int ret;
|
||||
|
||||
ret = gattlib_adapter_open(adapter_name, &adapter);
|
||||
|
|
Loading…
Reference in New Issue