From 294bfb100148c7554f0c3edf51f5bbf494b8f3c6 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Fri, 27 Dec 2013 11:14:02 +0200 Subject: [PATCH] android/ipc: Add initial code for audio IPC This add initial code for listen and accept connections on the abstract socket defined for the audio IPC. --- android/Android.mk | 1 + android/Makefile.am | 2 + android/audio-ipc.c | 104 ++++++++++++++++++++++++++++++++++++++++++++ android/audio-ipc.h | 25 +++++++++++ android/audio-msg.h | 26 +++++++++++ android/ipc.c | 14 +++--- android/ipc.h | 1 + 7 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 android/audio-ipc.c create mode 100644 android/audio-ipc.h create mode 100644 android/audio-msg.h diff --git a/android/Android.mk b/android/Android.mk index 7d9cc4f25..27631cc42 100644 --- a/android/Android.mk +++ b/android/Android.mk @@ -26,6 +26,7 @@ LOCAL_SRC_FILES := \ hidhost.c \ socket.c \ ipc.c ipc.h \ + audio-ipc.c audio-ipc.h \ avdtp.c \ a2dp.c \ pan.c \ diff --git a/android/Makefile.am b/android/Makefile.am index 7d9b58083..881036907 100644 --- a/android/Makefile.am +++ b/android/Makefile.am @@ -15,6 +15,7 @@ noinst_PROGRAMS += android/bluetoothd android_bluetoothd_SOURCES = android/main.c \ src/log.c \ android/hal-msg.h \ + android/audio-msg.h \ android/utils.h \ src/sdpd-database.c src/sdpd-server.c \ src/sdpd-service.c src/sdpd-request.c \ @@ -25,6 +26,7 @@ android_bluetoothd_SOURCES = android/main.c \ android/bluetooth.h android/bluetooth.c \ android/hidhost.h android/hidhost.c \ android/ipc.h android/ipc.c \ + android/audio-ipc.h android/audio-ipc.c \ android/avdtp.h android/avdtp.c \ android/a2dp.h android/a2dp.c \ android/socket.h android/socket.c \ diff --git a/android/audio-ipc.c b/android/audio-ipc.c new file mode 100644 index 000000000..b537f2195 --- /dev/null +++ b/android/audio-ipc.c @@ -0,0 +1,104 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2014 Intel Corporation. All rights reserved. + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; 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 +#include +#include +#include +#include +#include + +#include "ipc.h" +#include "log.h" +#include "audio-msg.h" +#include "audio-ipc.h" + +static GIOChannel *audio_io = NULL; + +static gboolean audio_watch_cb(GIOChannel *io, GIOCondition cond, + gpointer user_data) +{ + char buf[BLUEZ_AUDIO_MTU]; + ssize_t ret; + int fd; + + if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) { + info("Audio IPC: command socket closed"); + goto fail; + } + + fd = g_io_channel_unix_get_fd(io); + + ret = read(fd, buf, sizeof(buf)); + if (ret < 0) { + error("Audio IPC: command read failed (%s)", strerror(errno)); + goto fail; + } + + return TRUE; + +fail: + audio_ipc_cleanup(); + return FALSE; +} + +static gboolean audio_connect_cb(GIOChannel *io, GIOCondition cond, + gpointer user_data) +{ + DBG(""); + + if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) { + error("Audio IPC: socket connect failed"); + audio_ipc_cleanup(); + return FALSE; + } + + cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL; + + g_io_add_watch(audio_io, cond, audio_watch_cb, NULL); + + info("Audio IPC: successfully connected"); + + return FALSE; +} + +void audio_ipc_init(void) +{ + audio_io = ipc_connect(BLUEZ_AUDIO_SK_PATH, sizeof(BLUEZ_AUDIO_SK_PATH), + audio_connect_cb); +} + +void audio_ipc_cleanup(void) +{ + if (audio_io) { + g_io_channel_shutdown(audio_io, TRUE, NULL); + g_io_channel_unref(audio_io); + audio_io = NULL; + } +} diff --git a/android/audio-ipc.h b/android/audio-ipc.h new file mode 100644 index 000000000..769bfd644 --- /dev/null +++ b/android/audio-ipc.h @@ -0,0 +1,25 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2014 Intel Corporation. All rights reserved. + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +void audio_ipc_init(void); +void audio_ipc_cleanup(void); diff --git a/android/audio-msg.h b/android/audio-msg.h new file mode 100644 index 000000000..d5b409931 --- /dev/null +++ b/android/audio-msg.h @@ -0,0 +1,26 @@ +/* + * + * 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 + * + */ + +#define BLUEZ_AUDIO_MTU 1024 + +static const char BLUEZ_AUDIO_SK_PATH[] = "\0bluez_audio_socket"; diff --git a/android/ipc.c b/android/ipc.c index 9e8ccc39e..8a9124829 100644 --- a/android/ipc.c +++ b/android/ipc.c @@ -145,7 +145,7 @@ static gboolean notif_watch_cb(GIOChannel *io, GIOCondition cond, return FALSE; } -static GIOChannel *connect_hal(GIOFunc connect_cb) +GIOChannel *ipc_connect(const char *path, size_t size, GIOFunc connect_cb) { struct sockaddr_un addr; GIOCondition cond; @@ -167,11 +167,11 @@ static GIOChannel *connect_hal(GIOFunc connect_cb) memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; - memcpy(addr.sun_path, BLUEZ_HAL_SK_PATH, sizeof(BLUEZ_HAL_SK_PATH)); + memcpy(addr.sun_path, path, size); if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - error("IPC: failed to connect HAL socket: %d (%s)", errno, - strerror(errno)); + error("IPC: failed to connect HAL socket %s: %d (%s)", &path[1], + errno, strerror(errno)); g_io_channel_unref(io); return NULL; } @@ -218,7 +218,8 @@ static gboolean cmd_connect_cb(GIOChannel *io, GIOCondition cond, return FALSE; } - notif_io = connect_hal(notif_connect_cb); + notif_io = ipc_connect(BLUEZ_HAL_SK_PATH, sizeof(BLUEZ_HAL_SK_PATH), + notif_connect_cb); if (!notif_io) raise(SIGTERM); @@ -227,7 +228,8 @@ static gboolean cmd_connect_cb(GIOChannel *io, GIOCondition cond, void ipc_init(void) { - cmd_io = connect_hal(cmd_connect_cb); + cmd_io = ipc_connect(BLUEZ_HAL_SK_PATH, sizeof(BLUEZ_HAL_SK_PATH), + cmd_connect_cb); if (!cmd_io) raise(SIGTERM); } diff --git a/android/ipc.h b/android/ipc.h index 6cd102b06..02ad6bb7b 100644 --- a/android/ipc.h +++ b/android/ipc.h @@ -28,6 +28,7 @@ struct ipc_handler { }; void ipc_init(void); void ipc_cleanup(void); +GIOChannel *ipc_connect(const char *path, size_t size, GIOFunc connect_cb); void ipc_send_rsp(uint8_t service_id, uint8_t opcode, uint8_t status); void ipc_send_rsp_full(uint8_t service_id, uint8_t opcode, uint16_t len, -- 2.47.3