summaryrefslogtreecommitdiffstats
path: root/audio
diff options
context:
space:
mode:
authorLuiz Augusto von Dentz <luiz.dentz@openbossa.org>2007-06-22 19:47:53 +0000
committerLuiz Augusto von Dentz <luiz.dentz@openbossa.org>2007-06-22 19:47:53 +0000
commit1ad4df62d7b0ca988bc5ae62444420fc0273d53a (patch)
tree2d8021f5c3732d81b8021040b8c57f54f406a353 /audio
parent7fb02908be38aaa273643ac6b78c5d72e40ae545 (diff)
Merge changes from git://git.infradead.org/users/vudentz/bluez-utils.git.
Diffstat (limited to 'audio')
-rw-r--r--audio/device.c201
-rw-r--r--audio/device.h73
-rw-r--r--audio/error.c99
-rw-r--r--audio/error.h36
-rw-r--r--audio/gateway.c555
-rw-r--r--audio/gateway.h30
6 files changed, 994 insertions, 0 deletions
diff --git a/audio/device.c b/audio/device.c
new file mode 100644
index 00000000..50d07a2c
--- /dev/null
+++ b/audio/device.c
@@ -0,0 +1,201 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ * 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 <config.h>
+#endif
+
+#include <glib.h>
+#include <dbus/dbus.h>
+
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/hci.h>
+#include <bluetooth/hci_lib.h>
+
+#include "dbus.h"
+#include "dbus-helper.h"
+#include "logging.h"
+
+#include "device.h"
+
+void device_finish_sdp_transaction(struct device *device)
+{
+ char address[18], *addr_ptr = address;
+ DBusMessage *msg, *reply;
+ DBusError derr;
+
+ ba2str(&device->bda, address);
+
+ msg = dbus_message_new_method_call("org.bluez", device->adapter_path,
+ "org.bluez.Adapter",
+ "FinishRemoteServiceTransaction");
+ if (!msg) {
+ error("Unable to allocate new method call");
+ return;
+ }
+
+ dbus_message_append_args(msg, DBUS_TYPE_STRING, &addr_ptr,
+ DBUS_TYPE_INVALID);
+
+ dbus_error_init(&derr);
+ reply = dbus_connection_send_with_reply_and_block(device->conn, msg, -1,
+ &derr);
+
+ dbus_message_unref(msg);
+
+ if (dbus_error_is_set(&derr) ||
+ dbus_set_error_from_message(&derr, reply)) {
+ error("FinishRemoteServiceTransaction(%s) failed: %s",
+ address, derr.message);
+ dbus_error_free(&derr);
+ return;
+ }
+
+ dbus_message_unref(reply);
+}
+
+static DBusHandlerResult device_get_address(DBusConnection *conn,
+ DBusMessage *msg,
+ void *data)
+{
+ struct device *device = data;
+ DBusMessage *reply;
+ char address[18], *ptr = address;
+
+ reply = dbus_message_new_method_return(msg);
+ if (!reply)
+ return DBUS_HANDLER_RESULT_NEED_MEMORY;
+
+ ba2str(&device->bda, address);
+
+ dbus_message_append_args(reply, DBUS_TYPE_STRING, &ptr,
+ DBUS_TYPE_INVALID);
+
+ return send_message_and_unref(conn, reply);
+}
+
+static DBusHandlerResult device_get_connected(DBusConnection *conn,
+ DBusMessage *msg,
+ void *data)
+{
+ DBusMessageIter iter, array_iter;
+ struct device *device = data;
+ DBusMessage *reply;
+ const char *iface;
+
+ reply = dbus_message_new_method_return(msg);
+ if (!reply)
+ return DBUS_HANDLER_RESULT_NEED_MEMORY;
+
+ dbus_message_iter_init_append(reply, &iter);
+
+ dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
+ DBUS_TYPE_STRING_AS_STRING, &array_iter);
+
+ if (device->headset &&
+ headset_get_state(device->headset) >= HEADSET_STATE_CONNECTED) {
+ iface = AUDIO_HEADSET_INTERFACE;
+ dbus_message_iter_append_basic(&array_iter,
+ DBUS_TYPE_STRING, &iface);
+ }
+
+ dbus_message_iter_close_container(&iter, &array_iter);
+
+ return send_message_and_unref(conn, reply);
+}
+
+static DBusMethodVTable device_methods[] = {
+ { "GetAddress", device_get_address,
+ "", "s" },
+ { "GetConnectedInterfaces", device_get_connected,
+ "", "s" },
+ { NULL, NULL, NULL, NULL }
+};
+
+static void device_free(struct device *device)
+{
+ if (device->headset)
+ headset_free(device);
+
+ if (device->conn)
+ dbus_connection_unref(device->conn);
+
+ if (device->adapter_path)
+ g_free(device->adapter_path);
+
+ if (device->path)
+ g_free(device->path);
+
+ g_free(device);
+}
+
+static void device_unregister(DBusConnection *conn, void *data)
+{
+ struct device *device = data;
+
+ info("Unregistered device path:%s", device->path);
+
+ device_free(device);
+}
+
+struct device * device_register(DBusConnection *conn, const char *path, bdaddr_t *bda)
+{
+ struct device *device;
+ bdaddr_t src;
+ int dev_id;
+
+ if (!conn || !path)
+ return NULL;
+
+ bacpy(&src, BDADDR_ANY);
+ dev_id = hci_get_route(NULL);
+ if ((dev_id < 0) || (hci_devba(dev_id, &src) < 0))
+ return NULL;
+
+ device = g_new0(struct device, 1);
+
+ if (!dbus_connection_create_object_path(conn, path, device,
+ device_unregister)) {
+ error("D-Bus failed to register %s path", path);
+ device_free(device);
+ return NULL;
+ }
+
+ if (!dbus_connection_register_interface(conn,
+ path,
+ AUDIO_DEVICE_INTERFACE,
+ device_methods, NULL, NULL)) {
+ error("Failed to register %s interface to %s",
+ AUDIO_DEVICE_INTERFACE, path);
+ dbus_connection_destroy_object_path(conn, path);
+ return NULL;
+ }
+
+ device->path = g_strdup(path);
+ bacpy(&device->bda, bda);
+ device->conn = dbus_connection_ref(conn);
+ device->adapter_path = g_malloc0(16);
+ snprintf(device->adapter_path, 16, "/org/bluez/hci%d", dev_id);
+
+ return device;
+}
diff --git a/audio/device.h b/audio/device.h
new file mode 100644
index 00000000..daa9d6d1
--- /dev/null
+++ b/audio/device.h
@@ -0,0 +1,73 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ * 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
+ *
+ */
+
+#include <bluetooth/bluetooth.h>
+
+#include "headset.h"
+#include "gateway.h"
+
+#define AUDIO_DEVICE_INTERFACE "org.bluez.audio.Device"
+
+#define GENERIC_AUDIO_UUID "00001203-0000-1000-8000-00805F9B34FB"
+
+#define HSP_HS_UUID "00001108-0000-1000-8000-00805F9B34FB"
+#define HSP_AG_UUID "00001112-0000-1000-8000-00805F9B34FB"
+
+#define HFP_HS_UUID "0000111E-0000-1000-8000-00805F9B34FB"
+#define HFP_AG_UUID "0000111F-0000-1000-8000-00805F9B34FB"
+
+#define ADVANCED_AUDIO_UUID "0000110D-0000-1000-8000-00805F9B34FB"
+
+#define A2DP_SOURCE_UUID "0000110A-0000-1000-8000-00805F9B34FB"
+#define A2DP_SINK_UUID "0000110B-0000-1000-8000-00805F9B34FB"
+
+#define AVRCP_REMOTE_UUID "0000110E-0000-1000-8000-00805F9B34FB"
+#define AVRCP_TARGET_UUID "0000110C-0000-1000-8000-00805F9B34FB"
+
+/* Move these to respective .h files once they exist */
+#define AUDIO_SINK_INTERFACE "org.bluez.audio.Sink"
+#define AUDIO_SOURCE_INTERFACE "org.bluez.audio.Source"
+#define AUDIO_CONTROL_INTERFACE "org.bluez.audio.Control"
+#define AUDIO_TARGET_INTERFACE "org.bluez.audio.Target"
+struct sink;
+struct source;
+struct control;
+struct target;
+
+struct device {
+ DBusConnection *conn;
+ char *adapter_path;
+ char *path;
+ bdaddr_t bda;
+
+ struct headset *headset;
+ struct gateway *gateway;
+ struct sink *sink;
+ struct source *source;
+ struct control *control;
+ struct target *target;
+};
+
+struct device *device_register(DBusConnection *conn, const char *path,
+ bdaddr_t *bda);
+void device_finish_sdp_transaction(struct device *device);
diff --git a/audio/error.c b/audio/error.c
new file mode 100644
index 00000000..97307030
--- /dev/null
+++ b/audio/error.c
@@ -0,0 +1,99 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ * 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 <config.h>
+#endif
+
+#include "error.h"
+#include "logging.h"
+
+#define AUDIO_ERROR_INTERFACE "org.bluez.audio.Error"
+
+/* FIXME: Remove these once global error functions exist */
+static DBusHandlerResult error_reply(DBusConnection *conn, DBusMessage *msg,
+ const char *name, const char *descr)
+{
+ DBusMessage *derr;
+
+ if (!conn || !msg)
+ return DBUS_HANDLER_RESULT_HANDLED;
+
+ derr = dbus_message_new_error(msg, name, descr);
+ if (!derr) {
+ error("Unable to allocate new error return");
+ return DBUS_HANDLER_RESULT_NEED_MEMORY;
+ }
+
+ return send_message_and_unref(conn, derr);
+}
+
+DBusHandlerResult err_invalid_args(DBusConnection *conn, DBusMessage *msg,
+ const char *descr)
+{
+ return error_reply(conn, msg, AUDIO_ERROR_INTERFACE ".InvalidArguments",
+ descr ? descr : "Invalid arguments in method call");
+}
+
+DBusHandlerResult err_already_connected(DBusConnection *conn, DBusMessage *msg)
+{
+ return error_reply(conn, msg, AUDIO_ERROR_INTERFACE ".AlreadyConnected",
+ "Already connected to a device");
+}
+
+DBusHandlerResult err_not_connected(DBusConnection *conn, DBusMessage *msg)
+{
+ return error_reply(conn, msg, AUDIO_ERROR_INTERFACE ".NotConnected",
+ "Not connected to any device");
+}
+
+DBusHandlerResult err_not_supported(DBusConnection *conn, DBusMessage *msg)
+{
+ return error_reply(conn, msg, AUDIO_ERROR_INTERFACE ".NotSupported",
+ "The service is not supported by the remote device");
+}
+
+DBusHandlerResult err_connect_failed(DBusConnection *conn,
+ DBusMessage *msg, const char *err)
+{
+ return error_reply(conn, msg, AUDIO_ERROR_INTERFACE ".ConnectFailed",
+ err);
+}
+
+DBusHandlerResult err_does_not_exist(DBusConnection *conn, DBusMessage *msg)
+{
+ return error_reply(conn, msg, AUDIO_ERROR_INTERFACE ".DoesNotExist",
+ "Does not exist");
+}
+
+DBusHandlerResult err_not_available(DBusConnection *conn, DBusMessage *msg)
+{
+ return error_reply(conn, msg, ".NotAvailable",
+ "Not available");
+}
+
+DBusHandlerResult err_failed(DBusConnection *conn, DBusMessage *msg,
+ const char *dsc)
+{
+ return error_reply(conn, msg, AUDIO_ERROR_INTERFACE ".Failed", dsc);
+}
diff --git a/audio/error.h b/audio/error.h
new file mode 100644
index 00000000..833943af
--- /dev/null
+++ b/audio/error.h
@@ -0,0 +1,36 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ * 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
+ *
+ */
+
+#include "dbus.h"
+
+DBusHandlerResult err_invalid_args(DBusConnection *conn, DBusMessage *msg,
+ const char *descr);
+DBusHandlerResult err_already_connected(DBusConnection *conn, DBusMessage *msg);
+DBusHandlerResult err_not_connected(DBusConnection *conn, DBusMessage *msg);
+DBusHandlerResult err_not_supported(DBusConnection *conn, DBusMessage *msg);
+DBusHandlerResult err_connect_failed(DBusConnection *conn,
+ DBusMessage *msg, const char *err);
+DBusHandlerResult err_does_not_exist(DBusConnection *conn, DBusMessage *msg);
+DBusHandlerResult err_not_available(DBusConnection *conn, DBusMessage *msg);
+DBusHandlerResult err_failed(DBusConnection *conn, DBusMessage *msg,
+ const char *dsc);
diff --git a/audio/gateway.c b/audio/gateway.c
new file mode 100644
index 00000000..687d0969
--- /dev/null
+++ b/audio/gateway.c
@@ -0,0 +1,555 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ * 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 <config.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <string.h>
+#include <getopt.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/rfcomm.h>
+
+#include <glib.h>
+
+#include <dbus/dbus.h>
+
+#include "dbus.h"
+#include "dbus-helper.h"
+#include "logging.h"
+#include "manager.h"
+#include "error.h"
+
+static gboolean disable_hfp = FALSE;
+static gboolean sco_over_hci = TRUE;
+
+static uint32_t hs_record_id = 0;
+static uint32_t hf_record_id = 0;
+
+static GIOChannel *hs_server = NULL;
+static GIOChannel *hf_server = NULL;
+
+static DBusConnection *connection = NULL;
+
+static int gateway_hsp_ag_record(sdp_buf_t *buf, uint8_t ch)
+{
+ sdp_list_t *svclass_id, *pfseq, *apseq, *root;
+ uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
+ uuid_t l2cap_uuid, rfcomm_uuid;
+ sdp_profile_desc_t profile;
+ sdp_list_t *aproto, *proto[2];
+ sdp_record_t record;
+ sdp_data_t *channel;
+ int ret;
+
+ memset(&record, 0, sizeof(sdp_record_t));
+
+ sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
+ root = sdp_list_append(0, &root_uuid);
+ sdp_set_browse_groups(&record, root);
+
+ sdp_uuid16_create(&svclass_uuid, HEADSET_AGW_SVCLASS_ID);
+ svclass_id = sdp_list_append(0, &svclass_uuid);
+ sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
+ svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
+ sdp_set_service_classes(&record, svclass_id);
+
+ sdp_uuid16_create(&profile.uuid, HEADSET_PROFILE_ID);
+ profile.version = 0x0100;
+ pfseq = sdp_list_append(0, &profile);
+ sdp_set_profile_descs(&record, pfseq);
+
+ sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
+ proto[0] = sdp_list_append(0, &l2cap_uuid);
+ apseq = sdp_list_append(0, proto[0]);
+
+ sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
+ proto[1] = sdp_list_append(0, &rfcomm_uuid);
+ channel = sdp_data_alloc(SDP_UINT8, &ch);
+ proto[1] = sdp_list_append(proto[1], channel);
+ apseq = sdp_list_append(apseq, proto[1]);
+
+ aproto = sdp_list_append(0, apseq);
+ sdp_set_access_protos(&record, aproto);
+
+ sdp_set_info_attr(&record, "Headset Audio Gateway", 0, 0);
+
+ if (sdp_gen_record_pdu(&record, buf) < 0)
+ ret = -1;
+ else
+ ret = 0;
+
+ sdp_data_free(channel);
+ sdp_list_free(proto[0], 0);
+ sdp_list_free(proto[1], 0);
+ sdp_list_free(apseq, 0);
+ sdp_list_free(pfseq, 0);
+ sdp_list_free(aproto, 0);
+ sdp_list_free(root, 0);
+ sdp_list_free(svclass_id, 0);
+ sdp_list_free(record.attrlist, (sdp_free_func_t) sdp_data_free);
+ sdp_list_free(record.pattern, free);
+
+ return ret;
+}
+
+static int gateway_hfp_ag_record(sdp_buf_t *buf, uint8_t ch)
+{
+ sdp_list_t *svclass_id, *pfseq, *apseq, *root;
+ uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
+ uuid_t l2cap_uuid, rfcomm_uuid;
+ sdp_profile_desc_t profile;
+ sdp_list_t *aproto, *proto[2];
+ sdp_record_t record;
+ uint16_t u16 = 0x0009;
+ sdp_data_t *channel, *features;
+ uint8_t netid = 0x01;
+ sdp_data_t *network = sdp_data_alloc(SDP_UINT8, &netid);
+ int ret;
+
+ memset(&record, 0, sizeof(sdp_record_t));
+
+ sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
+ root = sdp_list_append(0, &root_uuid);
+ sdp_set_browse_groups(&record, root);
+
+ sdp_uuid16_create(&svclass_uuid, HANDSFREE_AGW_SVCLASS_ID);
+ svclass_id = sdp_list_append(0, &svclass_uuid);
+ sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
+ svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
+ sdp_set_service_classes(&record, svclass_id);
+
+ sdp_uuid16_create(&profile.uuid, HANDSFREE_PROFILE_ID);
+ profile.version = 0x0105;
+ pfseq = sdp_list_append(0, &profile);
+ sdp_set_profile_descs(&record, pfseq);
+
+ sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
+ proto[0] = sdp_list_append(0, &l2cap_uuid);
+ apseq = sdp_list_append(0, proto[0]);
+
+ sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
+ proto[1] = sdp_list_append(0, &rfcomm_uuid);
+ channel = sdp_data_alloc(SDP_UINT8, &ch);
+ proto[1] = sdp_list_append(proto[1], channel);
+ apseq = sdp_list_append(apseq, proto[1]);
+
+ features = sdp_data_alloc(SDP_UINT16, &u16);
+ sdp_attr_add(&record, SDP_ATTR_SUPPORTED_FEATURES, features);
+
+ aproto = sdp_list_append(0, apseq);
+ sdp_set_access_protos(&record, aproto);
+
+ sdp_set_info_attr(&record, "Hands-Free Audio Gateway", 0, 0);
+
+ sdp_attr_add(&record, SDP_ATTR_EXTERNAL_NETWORK, network);
+
+ if (sdp_gen_record_pdu(&record, buf) < 0)
+ ret = -1;
+ else
+ ret = 0;
+
+ sdp_data_free(channel);
+ sdp_list_free(proto[0], 0);
+ sdp_list_free(proto[1], 0);
+ sdp_list_free(apseq, 0);
+ sdp_list_free(pfseq, 0);
+ sdp_list_free(aproto, 0);
+ sdp_list_free(root, 0);
+ sdp_list_free(svclass_id, 0);
+ sdp_list_free(record.attrlist, (sdp_free_func_t) sdp_data_free);
+ sdp_list_free(record.pattern, free);
+
+ return ret;
+}
+
+static uint32_t gateway_add_ag_record(uint8_t channel, sdp_buf_t *buf)
+{
+ DBusMessage *msg, *reply;
+ DBusError derr;
+ dbus_uint32_t rec_id;
+
+ msg = dbus_message_new_method_call("org.bluez", "/org/bluez",
+ "org.bluez.Database", "AddServiceRecord");
+ if (!msg) {
+ error("Can't allocate new method call");
+ return 0;
+ }
+
+ dbus_message_append_args(msg, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
+ &buf->data, buf->data_size, DBUS_TYPE_INVALID);
+
+ dbus_error_init(&derr);
+ reply = dbus_connection_send_with_reply_and_block(connection, msg,
+ -1, &derr);
+
+ dbus_message_unref(msg);
+
+ if (dbus_error_is_set(&derr) ||
+ dbus_set_error_from_message(&derr, reply)) {
+ error("Adding service record failed: %s", derr.message);
+ dbus_error_free(&derr);
+ return 0;
+ }
+
+ dbus_message_get_args(reply, &derr, DBUS_TYPE_UINT32, &rec_id,
+ DBUS_TYPE_INVALID);
+
+ if (dbus_error_is_set(&derr)) {
+ error("Invalid arguments to AddServiceRecord reply: %s",
+ derr.message);
+ dbus_message_unref(reply);
+ dbus_error_free(&derr);
+ return 0;
+ }
+
+ dbus_message_unref(reply);
+
+ debug("add_ag_record: got record id 0x%x", rec_id);
+
+ return rec_id;
+}
+
+static int gateway_remove_ag_record(uint32_t rec_id)
+{
+ DBusMessage *msg, *reply;
+ DBusError derr;
+
+ msg = dbus_message_new_method_call("org.bluez", "/org/bluez",
+ "org.bluez.Database",
+ "RemoveServiceRecord");
+ if (!msg) {
+ error("Can't allocate new method call");
+ return 0;
+ }
+
+ dbus_message_append_args(msg, DBUS_TYPE_UINT32, &rec_id,
+ DBUS_TYPE_INVALID);
+
+ dbus_error_init(&derr);
+ reply = dbus_connection_send_with_reply_and_block(connection, msg,
+ -1, &derr);
+
+ dbus_message_unref(msg);
+
+ if (dbus_error_is_set(&derr)) {
+ error("Removing service record 0x%x failed: %s",
+ rec_id, derr.message);
+ dbus_error_free(&derr);
+ return 0;
+ }
+
+ dbus_message_unref(reply);
+
+ return 0;
+}
+
+static void send_cancel_auth(struct device *device)
+{
+ DBusMessage *cancel;
+ char addr[18], *address = addr;
+ const char *uuid;
+
+ if (headset_get_type(device) == SVC_HEADSET)
+ uuid = HSP_AG_UUID;
+ else
+ uuid = HFP_AG_UUID;
+
+ cancel = dbus_message_new_method_call("org.bluez", "/org/bluez",
+ "org.bluez.Database",
+ "CancelAuthorizationRequest");
+ if (!cancel) {
+ error("Unable to allocate new method call");
+ return;
+ }
+
+ ba2str(&device->bda, addr);
+
+ dbus_message_append_args(cancel, DBUS_TYPE_STRING, &address,
+ DBUS_TYPE_STRING, &uuid, DBUS_TYPE_INVALID);
+
+ send_message_and_unref(connection, cancel);
+}
+
+static void auth_cb(DBusPendingCall *call, void *data)
+{
+ struct device *device = data;
+ DBusMessage *reply = dbus_pending_call_steal_reply(call);
+ DBusError err;
+
+ dbus_error_init(&err);
+ if (dbus_set_error_from_message(&err, reply)) {
+ error("Access denied: %s", err.message);
+ if (dbus_error_has_name(&err, DBUS_ERROR_NO_REPLY)) {
+ debug("Canceling authorization request");
+ send_cancel_auth(device);
+ }
+ dbus_error_free(&err);
+ headset_close_rfcomm(device);
+ } else {
+ char hs_address[18];
+
+ headset_set_state(device, HEADSET_STATE_CONNECTED);
+
+ ba2str(&device->bda, hs_address);
+
+ debug("Accepted headset connection from %s for %s", hs_address,
+ device->path);
+ }
+
+ dbus_message_unref(reply);
+}
+
+static gboolean gateway_io_cb(GIOChannel *chan, GIOCondition cond,
+ void *data)
+{
+ int srv_sk, cli_sk;
+ struct sockaddr_rc addr;
+ socklen_t size;
+ char hs_address[18], *address = hs_address;
+ const char *uuid;
+ struct device *device;
+ DBusMessage *auth;
+ DBusPendingCall *pending;
+
+ if (cond & G_IO_NVAL)
+ return FALSE;
+
+ if (cond & (G_IO_HUP | G_IO_ERR)) {
+ error("Hangup or error on rfcomm server socket");
+ g_io_channel_close(chan);
+ raise(SIGTERM);
+ return FALSE;
+ }
+
+ srv_sk = g_io_channel_unix_get_fd(chan);
+
+ size = sizeof(struct sockaddr_rc);
+ cli_sk = accept(srv_sk, (struct sockaddr *) &addr, &size);
+ if (cli_sk < 0) {
+ error("accept: %s (%d)", strerror(errno), errno);
+ return TRUE;
+ }
+
+ device = manager_device_connected(&addr.rc_bdaddr);
+ if (!device) {
+ close(cli_sk);
+ return TRUE;
+ }
+
+ if (headset_get_state(device) > HEADSET_STATE_DISCONNECTED) {
+ debug("Refusing new connection since one already exists");
+ close(cli_sk);
+ return TRUE;
+ }
+
+ if (headset_connect_rfcomm(device, cli_sk) < 0) {
+ error("Allocating new GIOChannel failed!");
+ close(cli_sk);
+ return TRUE;
+ }
+
+ if (chan == hs_server) {
+ headset_set_type(device, SVC_HEADSET);
+ uuid = HSP_AG_UUID;
+ } else {
+ headset_set_type(device, SVC_HANDSFREE);
+ uuid = HFP_AG_UUID;
+ }
+
+ auth = dbus_message_new_method_call("org.bluez", "/org/bluez",
+ "org.bluez.Database",
+ "RequestAuthorization");
+ if (!auth) {
+ error("Unable to allocate RequestAuthorization method call");
+ goto failed;
+ }
+
+ ba2str(&device->bda, hs_address);
+
+ dbus_message_append_args(auth, DBUS_TYPE_STRING, &address,
+ DBUS_TYPE_STRING, &uuid, DBUS_TYPE_INVALID);
+
+ if (!dbus_connection_send_with_reply(connection, auth, &pending, -1)) {
+ error("Sending of authorization request failed");
+ goto failed;
+ }
+
+ dbus_pending_call_set_notify(pending, auth_cb, device, NULL);
+ dbus_pending_call_unref(pending);
+ dbus_message_unref(auth);
+
+ return TRUE;
+
+failed:
+ headset_close_rfcomm(device);
+
+ return TRUE;
+}
+
+static GIOChannel *server_socket(uint8_t *channel)
+{
+ int sock, lm;
+ struct sockaddr_rc addr;
+ socklen_t sa_len;
+ GIOChannel *io;
+
+ sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
+ if (sock < 0) {
+ error("server socket: %s (%d)", strerror(errno), errno);
+ return NULL;
+ }
+
+ lm = RFCOMM_LM_SECURE;
+ if (setsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &lm, sizeof(lm)) < 0) {
+ error("server setsockopt: %s (%d)", strerror(errno), errno);
+ close(sock);
+ return NULL;
+ }
+
+ memset(&addr, 0, sizeof(addr));
+ addr.rc_family = AF_BLUETOOTH;
+ bacpy(&addr.rc_bdaddr, BDADDR_ANY);
+ addr.rc_channel = channel ? *channel : 0;
+
+ if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ error("server bind: %s", strerror(errno), errno);
+ close(sock);
+ return NULL;
+ }
+
+ if (listen(sock, 1) < 0) {
+ error("server listen: %s", strerror(errno), errno);
+ close(sock);
+ return NULL;
+ }
+
+ sa_len = sizeof(struct sockaddr_rc);
+ getsockname(sock, (struct sockaddr *) &addr, &sa_len);
+ *channel = addr.rc_channel;
+
+ io = g_io_channel_unix_new(sock);
+ if (!io) {
+ error("Unable to allocate new io channel");
+ close(sock);
+ return NULL;
+ }
+
+ return io;
+}
+
+int gateway_init(DBusConnection *conn, gboolean no_hfp, gboolean sco_hci)
+{
+ uint8_t chan = DEFAULT_HS_AG_CHANNEL;
+ sdp_buf_t buf;
+
+ connection = dbus_connection_ref(conn);
+
+ hs_server = server_socket(&chan);
+ if (!hs_server)
+ return -1;
+
+ if (gateway_hsp_ag_record(&buf, chan) < 0) {
+ error("Unable to allocate new service record");
+ return -1;
+ }
+
+ hs_record_id = gateway_add_ag_record(chan, &buf);
+ free(buf.data);
+ if (!hs_record_id) {
+ error("Unable to register HS AG service record");
+ g_io_channel_unref(hs_server);
+ hs_server = NULL;
+ return -1;
+ }
+
+ g_io_add_watch(hs_server, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+ (GIOFunc) gateway_io_cb, NULL);
+
+ disable_hfp = no_hfp;
+
+ sco_over_hci = sco_hci;
+
+ if (disable_hfp)
+ return 0;
+
+ chan = DEFAULT_HF_AG_CHANNEL;
+
+ hf_server = server_socket(&chan);
+ if (!hf_server)
+ return -1;
+
+ if (gateway_hfp_ag_record(&buf, chan) < 0) {
+ error("Unable to allocate new service record");
+ return -1;
+ }
+
+ hf_record_id = gateway_add_ag_record(chan, &buf);
+ free(buf.data);
+ if (!hf_record_id) {
+ error("Unable to register HS AG service record");
+ g_io_channel_unref(hf_server);
+ hs_server = NULL;
+ return -1;
+ }
+
+ g_io_add_watch(hf_server, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+ (GIOFunc) gateway_io_cb, NULL);
+
+ return 0;
+}
+
+void gateway_exit(void)
+{
+ if (hs_record_id) {
+ gateway_remove_ag_record(hs_record_id);
+ hs_record_id = 0;
+ }
+
+ if (hs_server) {
+ g_io_channel_unref(hs_server);
+ hs_server = NULL;
+ }
+
+ if (hf_record_id) {
+ gateway_remove_ag_record(hf_record_id);
+ hf_record_id = 0;
+ }
+
+ if (hf_server) {
+ g_io_channel_unref(hf_server);
+ hf_server = NULL;
+ }
+
+ dbus_connection_unref(connection);
+ connection = NULL;
+}
diff --git a/audio/gateway.h b/audio/gateway.h
new file mode 100644
index 00000000..137187cb
--- /dev/null
+++ b/audio/gateway.h
@@ -0,0 +1,30 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ * 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
+ *
+ */
+
+#define AUDIO_GATEWAY_INTERFACE "org.bluez.audio.Gateway"
+
+struct gateway;
+
+int gateway_init(DBusConnection *conn, gboolean disable_hfp, gboolean sco_hci);
+
+void gateway_exit(void);