diff --git a/android/avrcp.c b/android/avrcp.c index ef833dfe0..8600d9885 100644 --- a/android/avrcp.c +++ b/android/avrcp.c @@ -54,6 +54,7 @@ static GIOChannel *server = NULL; struct avrcp_device { bdaddr_t dst; struct avctp *session; + GIOChannel *io; }; static const struct ipc_handler cmd_handlers[] = { @@ -197,17 +198,20 @@ static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data) } ba2str(&dst, address); - DBG("Incoming connection from %s", address); l = g_slist_find_custom(devices, &dst, device_cmp); if (l) { - error("Unexpected connection"); - return; + dev = l->data; + if (dev->session) { + error("Unexpected connection"); + return; + } + } else { + DBG("Incoming connection from %s", address); + dev = avrcp_device_new(&dst); } fd = g_io_channel_unix_get_fd(chan); - - dev = avrcp_device_new(&dst); dev->session = avctp_new(fd, imtu, omtu, 0x0100); if (!dev->session) { @@ -222,6 +226,11 @@ static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data) g_io_channel_set_close_on_unref(chan, FALSE); + if (dev->io) { + g_io_channel_unref(dev->io); + dev->io = NULL; + } + DBG("%s connected", address); } @@ -288,3 +297,44 @@ void bt_avrcp_unregister(void) server = NULL; } } + +static bool avrcp_device_connect(struct avrcp_device *dev, BtIOConnect cb) +{ + GError *err = NULL; + + dev->io = bt_io_connect(cb, dev, NULL, &err, + BT_IO_OPT_SOURCE_BDADDR, &adapter_addr, + BT_IO_OPT_DEST_BDADDR, &dev->dst, + BT_IO_OPT_PSM, L2CAP_PSM_AVCTP, + BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM, + BT_IO_OPT_INVALID); + if (err) { + error("%s", err->message); + g_error_free(err); + return false; + } + + return true; +} + +void bt_avrcp_connect(const bdaddr_t *dst) +{ + struct avrcp_device *dev; + char addr[18]; + GSList *l; + + DBG(""); + + l = g_slist_find_custom(devices, dst, device_cmp); + if (l) + return; + + dev = avrcp_device_new(dst); + if (!avrcp_device_connect(dev, connect_cb)) { + avrcp_device_free(dev); + return; + } + + ba2str(&dev->dst, addr); + DBG("connecting to %s", addr); +} diff --git a/android/avrcp.h b/android/avrcp.h index 6fe7fbf0c..7b0a5ede1 100644 --- a/android/avrcp.h +++ b/android/avrcp.h @@ -23,3 +23,5 @@ bool bt_avrcp_register(const bdaddr_t *addr); void bt_avrcp_unregister(void); + +void bt_avrcp_connect(const bdaddr_t *dst);