From 4351d46804d65fcffa7a6f0e21b1693a2d0d5b75 Mon Sep 17 00:00:00 2001 From: Andrei Emeltchenko Date: Thu, 13 Feb 2014 17:20:07 +0200 Subject: [PATCH] android: Add initial AVRCP common code The patch makes AVRCP to be transport agnostic so that it can be used in with socket pair to build unit tests. The idea is that all AVRCP specific logic will stay on avrcp-lib until it receives proper unit tests and then eventually will be used by audio plugin as well. --- android/Android.mk | 1 + android/Makefile.am | 1 + android/avrcp-lib.c | 75 +++++++++++++++++++++++++++++++++++++++++++++ android/avrcp-lib.h | 31 +++++++++++++++++++ android/avrcp.c | 42 +++++++++++++------------ 5 files changed, 131 insertions(+), 19 deletions(-) create mode 100644 android/avrcp-lib.c create mode 100644 android/avrcp-lib.h diff --git a/android/Android.mk b/android/Android.mk index ed728904f..82c834b1b 100644 --- a/android/Android.mk +++ b/android/Android.mk @@ -40,6 +40,7 @@ LOCAL_SRC_FILES := \ bluez/android/a2dp.c \ bluez/android/avctp.c \ bluez/android/avrcp.c \ + bluez/android/avrcp-lib.c \ bluez/android/pan.c \ bluez/android/handsfree.c \ bluez/src/log.c \ diff --git a/android/Makefile.am b/android/Makefile.am index 36a9e8213..50d0dc0e5 100644 --- a/android/Makefile.am +++ b/android/Makefile.am @@ -37,6 +37,7 @@ android_bluetoothd_SOURCES = android/main.c \ android/a2dp.h android/a2dp.c \ android/avctp.h android/avctp.c \ android/avrcp.h android/avrcp.c \ + android/avrcp-lib.h android/avrcp-lib.c \ android/socket.h android/socket.c \ android/pan.h android/pan.c \ android/handsfree.h android/handsfree.c \ diff --git a/android/avrcp-lib.c b/android/avrcp-lib.c new file mode 100644 index 000000000..ef48fa7e4 --- /dev/null +++ b/android/avrcp-lib.c @@ -0,0 +1,75 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2014 Intel Corporation. All rights reserved. + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +#include "lib/bluetooth.h" + +#include "src/log.h" + +#include "avctp.h" +#include "avrcp-lib.h" + +struct avrcp { + struct avctp *session; +}; + +void avrcp_shutdown(struct avrcp *session) +{ + if (session->session) + avctp_shutdown(session->session); + + g_free(session); +} + +struct avrcp *avrcp_new(int fd, size_t imtu, size_t omtu, uint16_t version) +{ + struct avrcp *session; + + session = g_new0(struct avrcp, 1); + + session->session = avctp_new(fd, imtu, omtu, version); + if (!session->session) { + g_free(session); + return NULL; + } + + return session; +} + +void avrcp_set_destroy_cb(struct avrcp *session, avrcp_destroy_cb_t cb, + void *user_data) +{ + avctp_set_destroy_cb(session->session, cb, user_data); +} + +int avrcp_init_uinput(struct avrcp *session, const char *name, + const char *address) +{ + return avctp_init_uinput(session->session, name, address); +} diff --git a/android/avrcp-lib.h b/android/avrcp-lib.h new file mode 100644 index 000000000..7955d566f --- /dev/null +++ b/android/avrcp-lib.h @@ -0,0 +1,31 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2014 Intel Corporation. All rights reserved. + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +typedef void (*avrcp_destroy_cb_t) (void *user_data); + +struct avrcp *avrcp_new(int fd, size_t imtu, size_t omtu, uint16_t version); +void avrcp_shutdown(struct avrcp *session); +void avrcp_set_destroy_cb(struct avrcp *session, avrcp_destroy_cb_t cb, + void *user_data); +int avrcp_init_uinput(struct avrcp *session, const char *name, + const char *address); diff --git a/android/avrcp.c b/android/avrcp.c index 2cca64af5..8ac9e2f43 100644 --- a/android/avrcp.c +++ b/android/avrcp.c @@ -35,9 +35,9 @@ #include "src/log.h" #include "bluetooth.h" #include "avrcp.h" +#include "avrcp-lib.h" #include "hal-msg.h" #include "ipc.h" -#include "avctp.h" #define L2CAP_PSM_AVCTP 0x17 @@ -53,7 +53,7 @@ static GIOChannel *server = NULL; struct avrcp_device { bdaddr_t dst; - struct avctp *session; + struct avrcp *session; GIOChannel *io; }; @@ -242,7 +242,7 @@ static void avrcp_device_free(void *data) struct avrcp_device *dev = data; if (dev->session) - avctp_shutdown(dev->session); + avrcp_shutdown(dev->session); if (dev->io) { g_io_channel_shutdown(dev->io, FALSE, NULL); @@ -277,6 +277,17 @@ static int device_cmp(gconstpointer s, gconstpointer user_data) return bacmp(&dev->dst, dst); } +static struct avrcp_device *avrcp_device_find(const bdaddr_t *dst) +{ + GSList *l; + + l = g_slist_find_custom(devices, dst, device_cmp); + if (!l) + return NULL; + + return l->data; +} + static void disconnect_cb(void *data) { struct avrcp_device *dev = data; @@ -295,7 +306,6 @@ static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data) char address[18]; uint16_t imtu, omtu; GError *gerr = NULL; - GSList *l; int fd; if (err) { @@ -318,9 +328,8 @@ static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data) ba2str(&dst, address); - l = g_slist_find_custom(devices, &dst, device_cmp); - if (l) { - dev = l->data; + dev = avrcp_device_find(&dst); + if (dev) { if (dev->session) { error("Unexpected connection"); return; @@ -331,17 +340,17 @@ static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data) } fd = g_io_channel_unix_get_fd(chan); - dev->session = avctp_new(fd, imtu, omtu, 0x0100); + dev->session = avrcp_new(fd, imtu, omtu, 0x0100); if (!dev->session) { avrcp_device_free(dev); return; } - avctp_set_destroy_cb(dev->session, disconnect_cb, dev); + avrcp_set_destroy_cb(dev->session, disconnect_cb, dev); /* FIXME: get the real name of the device */ - avctp_init_uinput(dev->session, "bluetooth", address); + avrcp_init_uinput(dev->session, "bluetooth", address); g_io_channel_set_close_on_unref(chan, FALSE); @@ -440,12 +449,10 @@ 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) + if (avrcp_device_find(dst)) return; dev = avrcp_device_new(dst); @@ -461,18 +468,15 @@ void bt_avrcp_connect(const bdaddr_t *dst) void bt_avrcp_disconnect(const bdaddr_t *dst) { struct avrcp_device *dev; - GSList *l; DBG(""); - l = g_slist_find_custom(devices, dst, device_cmp); - if (!l) + dev = avrcp_device_find(dst); + if (!dev) return; - dev = l->data; - if (dev->session) { - avctp_shutdown(dev->session); + avrcp_shutdown(dev->session); return; } -- 2.47.3