andded user data pointer gatt_connect_async signature. these data will be passed to gatt_connect_cb callback to get information about object requested connection

pull/83/head
Daniel Vlasenko 2017-09-08 12:29:36 +03:00 committed by Olivier Martin
parent 0f8c02c6a6
commit 7a2fdbd062
7 changed files with 26 additions and 28 deletions

View File

@ -46,6 +46,7 @@ typedef struct {
int connected; int connected;
int timeout; int timeout;
GError* error; GError* error;
void* user_data;
} io_connect_arg_t; } io_connect_arg_t;
static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data) { static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data) {
@ -121,10 +122,10 @@ static void io_connect_cb(GIOChannel *io, GError *err, gpointer user_data) {
io_connect_arg->error = err; io_connect_arg->error = err;
// Call callback if defined // Call callback if defined
if (io_connect_arg->connect_cb) { if (io_connect_arg->connect_cb) {
io_connect_arg->connect_cb(NULL); io_connect_arg->connect_cb(NULL, io_connect_arg->user_data);
} }
} else { } else {
gattlib_context_t* conn_context = io_connect_arg->conn->context; gattlib_context_t* conn_context = io_connect_arg->conn->context;
#if BLUEZ_VERSION_MAJOR == 4 #if BLUEZ_VERSION_MAJOR == 4
@ -135,7 +136,7 @@ static void io_connect_cb(GIOChannel *io, GError *err, gpointer user_data) {
// //
// Register the listener callback // Register the listener callback
// //
GSource *source = g_idle_source_new (); GSource *source = g_idle_source_new ();
assert(source != NULL); assert(source != NULL);
@ -148,14 +149,14 @@ static void io_connect_cb(GIOChannel *io, GError *err, gpointer user_data) {
// //
// Save list of characteristics to do the correspondence handle/UUID // Save list of characteristics to do the correspondence handle/UUID
// //
gattlib_discover_char(io_connect_arg->conn, &conn_context->characteristics, &conn_context->characteristic_count); gattlib_discover_char(io_connect_arg->conn, &conn_context->characteristics, &conn_context->characteristic_count);
// //
// Call callback if defined // Call callback if defined
// //
if (io_connect_arg->connect_cb) { if (io_connect_arg->connect_cb) {
io_connect_arg->connect_cb(io_connect_arg->conn); io_connect_arg->connect_cb(io_connect_arg->conn, io_connect_arg->user_data);
} }
io_connect_arg->connected = TRUE; io_connect_arg->connected = TRUE;
@ -241,7 +242,7 @@ static gatt_connection_t *initialize_gattlib_connection(const gchar *src, const
conn->context = conn_context; conn->context = conn_context;
/* Intialize bt_io_connect argument */ /* Intialize bt_io_connect argument */
io_connect_arg->conn = conn; io_connect_arg->conn = conn;
io_connect_arg->connect_cb = connect_cb; io_connect_arg->connect_cb = connect_cb;
io_connect_arg->connected = FALSE; io_connect_arg->connected = FALSE;
@ -310,9 +311,10 @@ static BtIOSecLevel get_bt_io_sec_level(gattlib_bt_sec_level_t sec_level) {
gatt_connection_t *gattlib_connect_async(const char *src, const char *dst, gatt_connection_t *gattlib_connect_async(const char *src, const char *dst,
uint8_t dest_type, gattlib_bt_sec_level_t sec_level, int psm, int mtu, uint8_t dest_type, gattlib_bt_sec_level_t sec_level, int psm, int mtu,
gatt_connect_cb_t connect_cb) gatt_connect_cb_t connect_cb, void* data)
{ {
io_connect_arg_t* io_connect_arg = malloc(sizeof(io_connect_arg_t)); io_connect_arg_t* io_connect_arg = malloc(sizeof(io_connect_arg_t));
io_connect_arg->user_data = data;
BtIOSecLevel bt_io_sec_level = get_bt_io_sec_level(sec_level); BtIOSecLevel bt_io_sec_level = get_bt_io_sec_level(sec_level);
return initialize_gattlib_connection(src, dst, dest_type, bt_io_sec_level, return initialize_gattlib_connection(src, dst, dest_type, bt_io_sec_level,
@ -340,8 +342,7 @@ gatt_connection_t *gattlib_connect(const char *src, const char *dst,
{ {
BtIOSecLevel bt_io_sec_level = get_bt_io_sec_level(sec_level); BtIOSecLevel bt_io_sec_level = get_bt_io_sec_level(sec_level);
io_connect_arg_t io_connect_arg; io_connect_arg_t io_connect_arg;
GSource* timeout; GSource* timeout;
gatt_connection_t *conn = initialize_gattlib_connection(src, dst, dest_type, bt_io_sec_level, gatt_connection_t *conn = initialize_gattlib_connection(src, dst, dest_type, bt_io_sec_level,
psm, mtu, NULL, &io_connect_arg); psm, mtu, NULL, &io_connect_arg);
if (conn == NULL) { if (conn == NULL) {
@ -359,8 +360,7 @@ gatt_connection_t *gattlib_connect(const char *src, const char *dst,
// Wait for the connection to be done // Wait for the connection to be done
while ((io_connect_arg.connected == FALSE) && (io_connect_arg.timeout == FALSE)) { while ((io_connect_arg.connected == FALSE) && (io_connect_arg.timeout == FALSE)) {
g_main_context_iteration(g_gattlib_thread.loop_context, FALSE); g_main_context_iteration(g_gattlib_thread.loop_context, FALSE);
} }
// Disconnect the timeout source // Disconnect the timeout source
g_source_destroy(timeout); g_source_destroy(timeout);
@ -411,13 +411,13 @@ int gattlib_disconnect(gatt_connection_t* connection) {
GSource* gattlib_watch_connection_full(GIOChannel* io, GIOCondition condition, GSource* gattlib_watch_connection_full(GIOChannel* io, GIOCondition condition,
GIOFunc func, gpointer user_data, GDestroyNotify notify) GIOFunc func, gpointer user_data, GDestroyNotify notify)
{ {
// Create a main loop source // Create a main loop source
GSource *source = g_io_create_watch (io, condition); GSource *source = g_io_create_watch (io, condition);
assert(source != NULL); assert(source != NULL);
g_source_set_callback (source, (GSourceFunc)func, user_data, notify); g_source_set_callback (source, (GSourceFunc)func, user_data, notify);
// Attaches it to the main loop context // Attaches it to the main loop context
guint id = g_source_attach(source, g_gattlib_thread.loop_context); guint id = g_source_attach(source, g_gattlib_thread.loop_context);
g_source_unref (source); g_source_unref (source);
assert(id != 0); assert(id != 0);

View File

@ -141,18 +141,17 @@ int gattlib_discover_char_range(gatt_connection_t* connection, int start, int en
bzero(&user_data, sizeof(user_data)); bzero(&user_data, sizeof(user_data));
user_data.discovered = FALSE; user_data.discovered = FALSE;
gattlib_context_t* conn_context = connection->context; gattlib_context_t* conn_context = connection->context;
ret = gatt_discover_char(conn_context->attrib, start, end, NULL, characteristic_cb, &user_data); ret = gatt_discover_char(conn_context->attrib, start, end, NULL, characteristic_cb, &user_data);
if (ret == 0) { if (ret == 0) {
fprintf(stderr, "Fail to discover characteristics.\n"); fprintf(stderr, "Fail to discover characteristics.\n");
return 1; return 1;
} }
// Wait for completion // Wait for completion
while(user_data.discovered == FALSE) { while(user_data.discovered == FALSE) {
g_main_context_iteration(g_gattlib_thread.loop_context, FALSE); g_main_context_iteration(g_gattlib_thread.loop_context, FALSE);
} }
*characteristics = user_data.characteristics; *characteristics = user_data.characteristics;
*characteristics_count = user_data.characteristics_count; *characteristics_count = user_data.characteristics_count;

View File

@ -179,8 +179,7 @@ int gattlib_write_char_by_handle(gatt_connection_t* connection, uint16_t handle,
// Wait for completion of the event // Wait for completion of the event
while(write_completed == FALSE) { while(write_completed == FALSE) {
g_main_context_iteration(g_gattlib_thread.loop_context, FALSE); g_main_context_iteration(g_gattlib_thread.loop_context, FALSE);
} }
return 0; return 0;
} }

View File

@ -290,7 +290,7 @@ FREE_CONNECTION:
gatt_connection_t *gattlib_connect_async(const char *src, const char *dst, gatt_connection_t *gattlib_connect_async(const char *src, const char *dst,
uint8_t dest_type, gattlib_bt_sec_level_t sec_level, int psm, int mtu, uint8_t dest_type, gattlib_bt_sec_level_t sec_level, int psm, int mtu,
gatt_connect_cb_t connect_cb) gatt_connect_cb_t connect_cb, void* data)
{ {
return NULL; return NULL;
} }

View File

@ -549,7 +549,7 @@ int main(int argc, char *argv[])
dest_type = get_dest_type_from_str(opt_dst_type); dest_type = get_dest_type_from_str(opt_dst_type);
sec_level = get_sec_level_from_str(opt_sec_level); sec_level = get_sec_level_from_str(opt_sec_level);
connection = gattlib_connect_async(opt_src, opt_dst, dest_type, sec_level, connection = gattlib_connect_async(opt_src, opt_dst, dest_type, sec_level,
opt_psm, opt_mtu, connect_cb); opt_psm, opt_mtu, connect_cb, NULL);
if (connection == NULL) { if (connection == NULL) {
got_error = TRUE; got_error = TRUE;
goto done; goto done;

View File

@ -110,7 +110,7 @@ static void set_state(enum state st)
rl_redisplay(); rl_redisplay();
} }
static void connect_cb(gatt_connection_t* connection) static void connect_cb(gatt_connection_t* connection, void* user_data)
{ {
if (connection == NULL) { if (connection == NULL) {
set_state(STATE_DISCONNECTED); set_state(STATE_DISCONNECTED);
@ -304,7 +304,7 @@ static void cmd_connect(int argcp, char **argvp)
dst_type = get_dest_type_from_str(opt_dst_type); dst_type = get_dest_type_from_str(opt_dst_type);
sec_level = get_sec_level_from_str(opt_sec_level); sec_level = get_sec_level_from_str(opt_sec_level);
connection = gattlib_connect_async(opt_src, opt_dst, dst_type, sec_level, connection = gattlib_connect_async(opt_src, opt_dst, dst_type, sec_level,
opt_psm, opt_mtu, connect_cb); opt_psm, opt_mtu, connect_cb, NULL);
if (connection == NULL) { if (connection == NULL) {
set_state(STATE_DISCONNECTED); set_state(STATE_DISCONNECTED);
} else { } else {

View File

@ -80,7 +80,7 @@ typedef struct _gatt_connection_t {
} gatt_connection_t; } gatt_connection_t;
typedef void (*gattlib_discovered_device_t)(const char* addr, const char* name); typedef void (*gattlib_discovered_device_t)(const char* addr, const char* name);
typedef void (*gatt_connect_cb_t)(gatt_connection_t* connection); typedef void (*gatt_connect_cb_t)(gatt_connection_t* connection, void* user_data);
typedef void* (*gatt_read_cb_t)(const void* buffer, size_t buffer_len); typedef void* (*gatt_read_cb_t)(const void* buffer, size_t buffer_len);
@ -107,7 +107,7 @@ gatt_connection_t *gattlib_connect(const char *src, const char *dst,
gatt_connection_t *gattlib_connect_async(const char *src, const char *dst, gatt_connection_t *gattlib_connect_async(const char *src, const char *dst,
uint8_t dest_type, gattlib_bt_sec_level_t sec_level, int psm, int mtu, uint8_t dest_type, gattlib_bt_sec_level_t sec_level, int psm, int mtu,
gatt_connect_cb_t connect_cb); gatt_connect_cb_t connect_cb, void* data);
int gattlib_disconnect(gatt_connection_t* connection); int gattlib_disconnect(gatt_connection_t* connection);