From: Szymon Janc <szymon.janc@tieto.com>
To: Marcin Kraglak <marcin.kraglak@tieto.com>
Cc: linux-bluetooth@vger.kernel.org
Subject: Re: [PATCH 2/5] android/handsfree: Add support for handling incoming SCO connections
Date: Mon, 10 Mar 2014 12:48:02 +0100 [thread overview]
Message-ID: <1503694.zR9slxaStU@uw000953> (raw)
In-Reply-To: <1394223800-11961-2-git-send-email-marcin.kraglak@tieto.com>
Hi Marcin,
On Friday 07 of March 2014 21:23:17 Marcin Kraglak wrote:
> This adds support for accepting SCO connections from remote devices.
> ---
> android/handsfree.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 148 insertions(+)
>
> diff --git a/android/handsfree.c b/android/handsfree.c
> index fa27f10..40f3652 100644
> --- a/android/handsfree.c
> +++ b/android/handsfree.c
> @@ -83,11 +83,14 @@ static const struct indicator inds_defaults[] = {
> static struct {
> bdaddr_t bdaddr;
> uint8_t state;
> + uint8_t audio_state;
> uint32_t features;
> bool indicators_enabled;
> struct indicator inds[IND_COUNT];
> bool hsp;
> struct hfp_gw *gw;
> + GIOChannel *sco;
> + guint sco_watch;
> } device;
>
> static bdaddr_t adapter_addr;
> @@ -99,6 +102,8 @@ static GIOChannel *hfp_server = NULL;
> static uint32_t hsp_record_id = 0;
> static GIOChannel *hsp_server = NULL;
>
> +static GIOChannel *sco_server = NULL;
> +
> static void device_set_state(uint8_t state)
> {
> struct hal_ev_handsfree_conn_state ev;
> @@ -119,6 +124,26 @@ static void device_set_state(uint8_t state)
> HAL_EV_HANDSFREE_CONN_STATE, sizeof(ev), &ev);
> }
>
> +static void device_set_audio_state(uint8_t state)
> +{
> + struct hal_ev_handsfree_audio_state ev;
> + char address[18];
> +
> + if (device.audio_state == state)
> + return;
> +
> + device.audio_state = state;
> +
> + ba2str(&device.bdaddr, address);
> + DBG("device %s audio state %u", address, state);
> +
> + bdaddr2android(&device.bdaddr, ev.bdaddr);
> + ev.state = state;
> +
> + ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
> + HAL_EV_HANDSFREE_AUDIO_STATE, sizeof(ev), &ev);
> +}
> +
> static void device_init(const bdaddr_t *bdaddr)
> {
> bacpy(&device.bdaddr, bdaddr);
> @@ -137,6 +162,19 @@ static void device_cleanup(void)
>
> device_set_state(HAL_EV_HANDSFREE_CONN_STATE_DISCONNECTED);
>
> + if (device.sco_watch) {
> + g_source_remove(device.sco_watch);
> + device.sco_watch = 0;
> + }
> +
> + if (device.sco) {
> + g_io_channel_shutdown(device.sco, TRUE, NULL);
> + g_io_channel_unref(device.sco);
> + device.sco = NULL;
> + }
> +
> + device_set_audio_state(HAL_EV_HANDSFREE_AUDIO_STATE_DISCONNECTED);
> +
> memset(&device, 0, sizeof(device));
> }
>
> @@ -739,6 +777,42 @@ failed:
> HAL_OP_HANDSFREE_DISCONNECT, status);
> }
>
> +static gboolean sco_watch_cb(GIOChannel *chan, GIOCondition cond,
> + gpointer user_data)
> +{
> + g_source_remove(device.sco_watch);
> + device.sco_watch = 0;
> +
> + g_io_channel_shutdown(device.sco, TRUE, NULL);
> + g_io_channel_unref(device.sco);
> + device.sco = NULL;
> +
> + device_set_audio_state(HAL_EV_HANDSFREE_AUDIO_STATE_DISCONNECTED);
> +
> + return FALSE;
> +}
> +
> +static void connect_sco_cb(GIOChannel *chan, GError *err, gpointer user_data)
> +{
> + if (err) {
> + uint8_t status;
> +
> + error("SCO: connect failed (%s)", err->message);
> + status = HAL_EV_HANDSFREE_AUDIO_STATE_DISCONNECTED;
> + device_set_audio_state(status);
> +
> + return;
> + }
> +
> + g_io_channel_set_close_on_unref(chan, TRUE);
> +
> + device.sco = g_io_channel_ref(chan);
> + device.sco_watch = g_io_add_watch(chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
> + sco_watch_cb, NULL);
> +
> + device_set_audio_state(HAL_EV_HANDSFREE_AUDIO_STATE_CONNECTED);
> +}
> +
> static void handle_connect_audio(const void *buf, uint16_t len)
> {
> DBG("");
> @@ -1044,6 +1118,46 @@ static sdp_record_t *headset_ag_record(void)
> return record;
> }
>
> +static void confirm_sco_cb(GIOChannel *chan, gpointer user_data)
> +{
> + char address[18];
> + bdaddr_t bdaddr;
> + GError *err = NULL;
> +
> + if (device.sco)
> + goto drop;
> +
> + bt_io_get(chan, &err,
> + BT_IO_OPT_DEST, address,
> + BT_IO_OPT_DEST_BDADDR, &bdaddr,
> + BT_IO_OPT_INVALID);
> + if (err) {
> + error("SCO: confirm failed (%s)", err->message);
> + g_error_free(err);
> + goto drop;
> + }
> +
> + DBG("incoming SCO connection from %s", address);
> +
> + if (device.state != HAL_EV_HANDSFREE_CONN_STATE_SLC_CONNECTED ||
> + bacmp(&device.bdaddr, &bdaddr)) {
> + error("SCO: connection from %s rejected", address);
> + goto drop;
> + }
> +
> + if (!bt_io_accept(chan, connect_sco_cb, NULL, NULL, NULL)) {
> + error("SCO: failed to accept connection");
> + goto drop;
> + }
> +
> + device_set_audio_state(HAL_EV_HANDSFREE_AUDIO_STATE_CONNECTING);
> +
> + return;
> +
> +drop:
> + g_io_channel_shutdown(chan, TRUE, NULL);
> +}
> +
> static bool enable_hsp_ag(void)
> {
> sdp_record_t *rec;
> @@ -1233,6 +1347,33 @@ static void cleanup_hfp_ag(void)
> }
> }
>
> +static bool enable_sco_server(void)
> +{
> + GError *err = NULL;
> +
> + sco_server = bt_io_listen(NULL, confirm_sco_cb, NULL, NULL, &err,
> + BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
> + BT_IO_OPT_INVALID);
> + if (!sco_server) {
> + error("Failed to listen on SCO: %s", err->message);
> + g_error_free(err);
> + cleanup_hsp_ag();
> + cleanup_hfp_ag();
> + return false;
> + }
> +
> + return true;
> +}
> +
> +static void disable_sco_server(void)
> +{
> + if (sco_server) {
> + g_io_channel_shutdown(sco_server, TRUE, NULL);
> + g_io_channel_unref(sco_server);
> + sco_server = NULL;
> + }
> +}
> +
> bool bt_handsfree_register(struct ipc *ipc, const bdaddr_t *addr, uint8_t mode)
> {
> DBG("mode 0x%x", mode);
> @@ -1247,6 +1388,12 @@ bool bt_handsfree_register(struct ipc *ipc, const bdaddr_t *addr, uint8_t mode)
> return false;
> }
>
> + if (!enable_sco_server()) {
> + cleanup_hsp_ag();
> + cleanup_hfp_ag();
> + return false;
> + }
> +
> hal_ipc = ipc;
> ipc_register(hal_ipc, HAL_SERVICE_ID_HANDSFREE, cmd_handlers,
> G_N_ELEMENTS(cmd_handlers));
> @@ -1263,4 +1410,5 @@ void bt_handsfree_unregister(void)
>
> cleanup_hfp_ag();
> cleanup_hsp_ag();
> + disable_sco_server();
> }
>
Patches 2, 3 and 4 are now also upstream (with minor fixed we discussed
offline), thanks.
--
Best regards,
Szymon Janc
next prev parent reply other threads:[~2014-03-10 11:48 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-07 20:23 [PATCH 1/5] android/handsfree: Fix possible g_io_channel_shutdown() on NULL channel Marcin Kraglak
2014-03-07 20:23 ` [PATCH 2/5] android/handsfree: Add support for handling incoming SCO connections Marcin Kraglak
2014-03-10 11:48 ` Szymon Janc [this message]
2014-03-07 20:23 ` [PATCH 3/5] android/handsfree: Handle connect_audio call Marcin Kraglak
2014-03-07 20:23 ` [PATCH 4/5] android/handsfree: Handle disconnect_audio call Marcin Kraglak
2014-03-07 20:23 ` [PATCH 5/5] android/handsfree: Remove empty line Marcin Kraglak
2014-03-08 19:01 ` Szymon Janc
2014-03-08 19:00 ` [PATCH 1/5] android/handsfree: Fix possible g_io_channel_shutdown() on NULL channel Szymon Janc
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1503694.zR9slxaStU@uw000953 \
--to=szymon.janc@tieto.com \
--cc=linux-bluetooth@vger.kernel.org \
--cc=marcin.kraglak@tieto.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox