From afc8cdfb40888c6029d7fdbbf902e9e55dfcd758 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Tue, 11 Dec 2012 10:29:49 +0200 Subject: [PATCH] profile: Use direct calls for profile connection states In order to be able to track also remotely initiated connections we cannot have a callback system where device.c passes a function pointer to the profile implementation. Instead we need explicit calls to device.c whenever the connection state changes. --- profiles/audio/manager.c | 32 +++++++++++++----------------- profiles/input/device.c | 13 +++++-------- profiles/input/device.h | 7 +++---- profiles/network/manager.c | 40 +++++++++++++++++++------------------- src/device.c | 20 +++++++++---------- src/device.h | 7 +++++++ src/profile.c | 18 ++++++++--------- src/profile.h | 11 +++++------ 8 files changed, 71 insertions(+), 77 deletions(-) diff --git a/profiles/audio/manager.c b/profiles/audio/manager.c index 349894556..689a30962 100644 --- a/profiles/audio/manager.c +++ b/profiles/audio/manager.c @@ -199,14 +199,13 @@ static void profile_cb(struct audio_device *dev, int err, void *data) struct profile_req *req = data; if (req->cb) - req->cb(req->profile, req->device, err); + req->cb(req->device, req->profile, err); g_free(req); } static int a2dp_source_connect(struct btd_device *dev, - struct btd_profile *profile, - btd_profile_cb cb) + struct btd_profile *profile) { const gchar *path = device_get_path(dev); struct audio_device *audio_dev; @@ -221,7 +220,7 @@ static int a2dp_source_connect(struct btd_device *dev, return -1; } - req = new_profile_request(dev, profile, cb); + req = new_profile_request(dev, profile, device_profile_connected); err = source_connect(audio_dev, profile_cb, req); if (err < 0) { @@ -233,8 +232,7 @@ static int a2dp_source_connect(struct btd_device *dev, } static int a2dp_source_disconnect(struct btd_device *dev, - struct btd_profile *profile, - btd_profile_cb cb) + struct btd_profile *profile) { const gchar *path = device_get_path(dev); struct audio_device *audio_dev; @@ -249,7 +247,7 @@ static int a2dp_source_disconnect(struct btd_device *dev, return -1; } - req = new_profile_request(dev, profile, cb); + req = new_profile_request(dev, profile, device_profile_disconnected); err = source_disconnect(audio_dev, FALSE, profile_cb, req); if (err < 0) { @@ -261,8 +259,7 @@ static int a2dp_source_disconnect(struct btd_device *dev, } static int a2dp_sink_connect(struct btd_device *dev, - struct btd_profile *profile, - btd_profile_cb cb) + struct btd_profile *profile) { const gchar *path = device_get_path(dev); struct audio_device *audio_dev; @@ -277,7 +274,7 @@ static int a2dp_sink_connect(struct btd_device *dev, return -1; } - req = new_profile_request(dev, profile, cb); + req = new_profile_request(dev, profile, device_profile_connected); err = sink_connect(audio_dev, profile_cb, req); if (err < 0) { @@ -289,8 +286,7 @@ static int a2dp_sink_connect(struct btd_device *dev, } static int a2dp_sink_disconnect(struct btd_device *dev, - struct btd_profile *profile, - btd_profile_cb cb) + struct btd_profile *profile) { const gchar *path = device_get_path(dev); struct audio_device *audio_dev; @@ -305,7 +301,7 @@ static int a2dp_sink_disconnect(struct btd_device *dev, return -1; } - req = new_profile_request(dev, profile, cb); + req = new_profile_request(dev, profile, device_profile_disconnected); err = sink_disconnect(audio_dev, FALSE, profile_cb, req); if (err < 0) { @@ -317,8 +313,7 @@ static int a2dp_sink_disconnect(struct btd_device *dev, } static int avrcp_control_connect(struct btd_device *dev, - struct btd_profile *profile, - btd_profile_cb cb) + struct btd_profile *profile) { const gchar *path = device_get_path(dev); struct audio_device *audio_dev; @@ -333,7 +328,7 @@ static int avrcp_control_connect(struct btd_device *dev, return -1; } - req = new_profile_request(dev, profile, cb); + req = new_profile_request(dev, profile, device_profile_connected); err = control_connect(audio_dev, profile_cb, req); if (err < 0) { @@ -345,8 +340,7 @@ static int avrcp_control_connect(struct btd_device *dev, } static int avrcp_control_disconnect(struct btd_device *dev, - struct btd_profile *profile, - btd_profile_cb cb) + struct btd_profile *profile) { const gchar *path = device_get_path(dev); struct audio_device *audio_dev; @@ -361,7 +355,7 @@ static int avrcp_control_disconnect(struct btd_device *dev, return -1; } - req = new_profile_request(dev, profile, cb); + req = new_profile_request(dev, profile, device_profile_disconnected); err = control_disconnect(audio_dev, profile_cb, req); if (err < 0) { diff --git a/profiles/input/device.c b/profiles/input/device.c index b22e0a76d..aaf1ff8a3 100644 --- a/profiles/input/device.c +++ b/profiles/input/device.c @@ -498,7 +498,7 @@ static void connect_reply(struct input_device *idev, int err, if (err_msg) error("%s", err_msg); - pending->cb(pending->profile, idev->device, err); + pending->cb(idev->device, pending->profile, err); g_free(pending); } @@ -615,8 +615,7 @@ static int dev_connect(struct input_device *idev) return -EIO; } -int input_device_connect(struct btd_device *dev, struct btd_profile *profile, - btd_profile_cb cb) +int input_device_connect(struct btd_device *dev, struct btd_profile *profile) { struct input_device *idev; @@ -632,13 +631,12 @@ int input_device_connect(struct btd_device *dev, struct btd_profile *profile, idev->pending = g_new0(struct pending_connect, 1); idev->pending->profile = profile; - idev->pending->cb = cb; + idev->pending->cb = device_profile_connected; return dev_connect(idev); } -int input_device_disconnect(struct btd_device *dev, struct btd_profile *profile, - btd_profile_cb cb) +int input_device_disconnect(struct btd_device *dev, struct btd_profile *profile) { struct input_device *idev; int err; @@ -651,8 +649,7 @@ int input_device_disconnect(struct btd_device *dev, struct btd_profile *profile, if (err < 0) return err; - if (cb) - cb(profile, dev, 0); + device_profile_disconnected(dev, profile, 0); return 0; } diff --git a/profiles/input/device.h b/profiles/input/device.h index d68798c9d..aabc79c39 100644 --- a/profiles/input/device.h +++ b/profiles/input/device.h @@ -36,7 +36,6 @@ int input_device_set_channel(const bdaddr_t *src, const bdaddr_t *dst, int psm, GIOChannel *io); int input_device_close_channels(const bdaddr_t *src, const bdaddr_t *dst); -int input_device_connect(struct btd_device *dev, struct btd_profile *profile, - btd_profile_cb cb); -int input_device_disconnect(struct btd_device *dev, struct btd_profile *profile, - btd_profile_cb cb); +int input_device_connect(struct btd_device *dev, struct btd_profile *profile); +int input_device_disconnect(struct btd_device *dev, + struct btd_profile *profile); diff --git a/profiles/network/manager.c b/profiles/network/manager.c index e737f6df1..1c75e8f7e 100644 --- a/profiles/network/manager.c +++ b/profiles/network/manager.c @@ -83,7 +83,7 @@ static void connect_profile_cb(struct btd_device *device, int err, { struct connect_req *req = data; - req->cb(req->profile, device, err); + req->cb(device, req->profile, err); g_free(req); } @@ -122,7 +122,7 @@ static int disconnect_profile(struct btd_device *dev, return err; if (cb) - cb(profile, dev, 0); + cb(dev, profile, 0); return 0; } @@ -142,16 +142,16 @@ static void network_remove(struct btd_profile *p, struct btd_device *device) connection_unregister(device); } -static int panu_connect(struct btd_device *dev, struct btd_profile *profile, - btd_profile_cb cb) +static int panu_connect(struct btd_device *dev, struct btd_profile *profile) { - return connect_profile(dev, profile, BNEP_SVC_PANU, cb); + return connect_profile(dev, profile, BNEP_SVC_PANU, + device_profile_connected); } -static int panu_disconnect(struct btd_device *dev, struct btd_profile *profile, - btd_profile_cb cb) +static int panu_disconnect(struct btd_device *dev, struct btd_profile *profile) { - return disconnect_profile(dev, profile, BNEP_SVC_PANU, cb); + return disconnect_profile(dev, profile, BNEP_SVC_PANU, + device_profile_disconnected); } static int panu_server_probe(struct btd_profile *p, struct btd_adapter *adapter) @@ -181,16 +181,16 @@ static int gn_probe(struct btd_profile *p, struct btd_device *device, return connection_register(device, BNEP_SVC_GN); } -static int gn_connect(struct btd_device *dev, struct btd_profile *profile, - btd_profile_cb cb) +static int gn_connect(struct btd_device *dev, struct btd_profile *profile) { - return connect_profile(dev, profile, BNEP_SVC_GN, cb); + return connect_profile(dev, profile, BNEP_SVC_GN, + device_profile_connected); } -static int gn_disconnect(struct btd_device *dev, struct btd_profile *profile, - btd_profile_cb cb) +static int gn_disconnect(struct btd_device *dev, struct btd_profile *profile) { - return disconnect_profile(dev, profile, BNEP_SVC_GN, cb); + return disconnect_profile(dev, profile, BNEP_SVC_GN, + device_profile_disconnected); } static int gn_server_probe(struct btd_profile *p, struct btd_adapter *adapter) @@ -220,16 +220,16 @@ static int nap_probe(struct btd_profile *p, struct btd_device *device, return connection_register(device, BNEP_SVC_NAP); } -static int nap_connect(struct btd_device *dev, struct btd_profile *profile, - btd_profile_cb cb) +static int nap_connect(struct btd_device *dev, struct btd_profile *profile) { - return connect_profile(dev, profile, BNEP_SVC_NAP, cb); + return connect_profile(dev, profile, BNEP_SVC_NAP, + device_profile_connected); } -static int nap_disconnect(struct btd_device *dev, struct btd_profile *profile, - btd_profile_cb cb) +static int nap_disconnect(struct btd_device *dev, struct btd_profile *profile) { - return disconnect_profile(dev, profile, BNEP_SVC_NAP, cb); + return disconnect_profile(dev, profile, BNEP_SVC_NAP, + device_profile_disconnected); } static int nap_server_probe(struct btd_profile *p, struct btd_adapter *adapter) diff --git a/src/device.c b/src/device.c index 419bf9b0b..e97265b03 100644 --- a/src/device.c +++ b/src/device.c @@ -1149,7 +1149,7 @@ static void dev_disconn_profile(gpointer a, gpointer b) if (!profile->disconnect) return; - profile->disconnect(dev, profile, NULL); + profile->disconnect(dev, profile); } void device_request_disconnect(struct btd_device *device, DBusMessage *msg) @@ -1221,7 +1221,7 @@ static DBusMessage *disconnect(DBusConnection *conn, DBusMessage *msg, return NULL; } -static int connect_next(struct btd_device *dev, btd_profile_cb cb) +static int connect_next(struct btd_device *dev) { struct btd_profile *profile; int err = -ENOENT; @@ -1231,7 +1231,7 @@ static int connect_next(struct btd_device *dev, btd_profile_cb cb) profile = dev->pending->data; - err = profile->connect(dev, profile, cb); + err = profile->connect(dev, profile); if (err == 0) return 0; @@ -1243,15 +1243,15 @@ static int connect_next(struct btd_device *dev, btd_profile_cb cb) return err; } -static void dev_profile_connected(struct btd_profile *profile, - struct btd_device *dev, int err) +void device_profile_connected(struct btd_device *dev, + struct btd_profile *profile, int err) { DBG("%s (%d)", strerror(-err), -err); dev->pending = g_slist_remove(dev->pending, profile); if (!err) { - if (connect_next(dev, dev_profile_connected) == 0) + if (connect_next(dev) == 0) return; dev->profiles_connected = TRUE; } @@ -1379,7 +1379,7 @@ static DBusMessage *connect_profiles(struct btd_device *dev, DBusMessage *msg, return btd_error_not_available(msg); start_connect: - err = connect_next(dev, dev_profile_connected); + err = connect_next(dev); if (err < 0) return btd_error_failed(msg, strerror(-err)); @@ -1415,8 +1415,8 @@ static DBusMessage *connect_profile(DBusConnection *conn, DBusMessage *msg, return reply; } -static void profile_disconnected(struct btd_profile *profile, - struct btd_device *dev, int err) +void device_profile_disconnected(struct btd_device *dev, + struct btd_profile *profile, int err) { if (!dev->disconnect) return; @@ -1457,7 +1457,7 @@ static DBusMessage *disconnect_profile(DBusConnection *conn, DBusMessage *msg, if (!p->disconnect) return btd_error_not_supported(msg); - err = p->disconnect(dev, p, profile_disconnected); + err = p->disconnect(dev, p); if (err < 0) return btd_error_failed(msg, strerror(-err)); diff --git a/src/device.h b/src/device.h index 5e173bd59..1987a8002 100644 --- a/src/device.h +++ b/src/device.h @@ -109,3 +109,10 @@ void btd_device_set_pnpid(struct btd_device *device, uint8_t vendor_id_src, uint16_t vendor_id, uint16_t product_id, uint16_t product_ver); GIOChannel *device_att_connect(gpointer user_data); + +struct btd_profile; + +void device_profile_connected(struct btd_device *dev, + struct btd_profile *profile, int err); +void device_profile_disconnected(struct btd_device *dev, + struct btd_profile *profile, int err); diff --git a/src/profile.c b/src/profile.c index afa487bb2..da34e2533 100644 --- a/src/profile.c +++ b/src/profile.c @@ -645,7 +645,7 @@ static void pending_reply(DBusPendingCall *call, void *user_data) if (!dbus_error_is_set(&err)) { if (conn->cb) { - conn->cb(&ext->p, conn->device, 0); + conn->cb(conn->device, &ext->p, 0); conn->cb = NULL; } @@ -660,7 +660,7 @@ static void pending_reply(DBusPendingCall *call, void *user_data) err.name, err.message); if (conn->cb) { - conn->cb(&ext->p, conn->device, -ECONNREFUSED); + conn->cb(conn->device, &ext->p, -ECONNREFUSED); conn->cb = NULL; } @@ -798,7 +798,7 @@ static void ext_connect(GIOChannel *io, GError *err, gpointer user_data) drop: if (conn->cb) { - conn->cb(&ext->p, conn->device, err ? -err->code : -EIO); + conn->cb(conn->device, &ext->p, err ? -err->code : -EIO); conn->cb = NULL; } if (io_err) @@ -1374,7 +1374,7 @@ static void record_cb(sdp_list_t *recs, int err, gpointer user_data) return; failed: - conn->cb(&ext->p, conn->device, err); + conn->cb(conn->device, &ext->p, err); ext->conns = g_slist_remove(ext->conns, conn); ext_io_destroy(conn); } @@ -1396,8 +1396,7 @@ static int resolve_service(struct ext_io *conn, const bdaddr_t *src, return err; } -static int ext_connect_dev(struct btd_device *dev, struct btd_profile *profile, - btd_profile_cb cb) +static int ext_connect_dev(struct btd_device *dev, struct btd_profile *profile) { struct btd_adapter *adapter; struct ext_io *conn; @@ -1432,7 +1431,7 @@ static int ext_connect_dev(struct btd_device *dev, struct btd_profile *profile, conn->adapter = btd_adapter_ref(adapter); conn->device = btd_device_ref(dev); - conn->cb = cb; + conn->cb = device_profile_connected; ext->conns = g_slist_append(ext->conns, conn); @@ -1476,8 +1475,7 @@ static int send_disconn_req(struct ext_profile *ext, struct ext_io *conn) } static int ext_disconnect_dev(struct btd_device *dev, - struct btd_profile *profile, - btd_profile_cb cb) + struct btd_profile *profile) { struct ext_profile *ext; struct ext_io *conn; @@ -1498,7 +1496,7 @@ static int ext_disconnect_dev(struct btd_device *dev, if (err < 0) return err; - conn->cb = cb; + conn->cb = device_profile_disconnected; return 0; } diff --git a/src/profile.h b/src/profile.h index 6991041d3..826d8dd52 100644 --- a/src/profile.h +++ b/src/profile.h @@ -29,8 +29,8 @@ struct btd_profile; -typedef void (*btd_profile_cb)(struct btd_profile *profile, - struct btd_device *device, int err); +typedef void (*btd_profile_cb)(struct btd_device *device, + struct btd_profile *profile, int err); struct btd_profile { const char *name; @@ -46,11 +46,10 @@ struct btd_profile { void (*device_remove) (struct btd_profile *p, struct btd_device *device); - int (*connect) (struct btd_device *device, struct btd_profile *profile, - btd_profile_cb cb); + int (*connect) (struct btd_device *device, + struct btd_profile *profile); int (*disconnect) (struct btd_device *device, - struct btd_profile *profile, - btd_profile_cb cb); + struct btd_profile *profile); int (*adapter_probe) (struct btd_profile *p, struct btd_adapter *adapter); -- 2.47.3