diff options
-rw-r--r-- | audio/device.c | 289 | ||||
-rw-r--r-- | audio/device.h | 61 | ||||
-rw-r--r-- | audio/error.c | 64 | ||||
-rw-r--r-- | audio/error.h | 12 | ||||
-rw-r--r-- | audio/gateway.c | 865 | ||||
-rw-r--r-- | audio/headset.c | 16 | ||||
-rw-r--r-- | audio/manager.c | 140 | ||||
-rw-r--r-- | audio/manager.h | 1 |
8 files changed, 804 insertions, 644 deletions
diff --git a/audio/device.c b/audio/device.c index 88edfc70..8442a87c 100644 --- a/audio/device.c +++ b/audio/device.c @@ -25,6 +25,12 @@ #include <config.h> #endif +#include <stdio.h> +#include <errno.h> +#include <unistd.h> +#include <sys/stat.h> +#include <netinet/in.h> + #include <glib.h> #include <dbus/dbus.h> @@ -35,167 +41,202 @@ #include "dbus.h" #include "dbus-helper.h" #include "logging.h" +#include "textfile.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) + DBusMessage *msg, void *data) { - struct device *device = data; - DBusMessage *reply; - char address[18], *ptr = address; + 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; + reply = dbus_message_new_method_return(msg); + if (!reply) + return DBUS_HANDLER_RESULT_NEED_MEMORY; - ba2str(&device->bda, address); + ba2str(&device->dst, address); - dbus_message_append_args(reply, DBUS_TYPE_STRING, &ptr, - DBUS_TYPE_INVALID); + dbus_message_append_args(reply, DBUS_TYPE_STRING, &ptr, + DBUS_TYPE_INVALID); - return send_message_and_unref(conn, reply); + return send_message_and_unref(conn, reply); } static DBusHandlerResult device_get_connected(DBusConnection *conn, - DBusMessage *msg, - void *data) + DBusMessage *msg, void *data) { - DBusMessageIter iter, array_iter; - struct device *device = data; - DBusMessage *reply; - const char *iface; + 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; + 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_init_append(reply, &iter); - dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, - DBUS_TYPE_STRING_AS_STRING, &array_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); - } + 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); + dbus_message_iter_close_container(&iter, &array_iter); - return send_message_and_unref(conn, reply); + 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 } + { "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->headset) + headset_free(device); - if (device->conn) - dbus_connection_unref(device->conn); + if (device->conn) + dbus_connection_unref(device->conn); - if (device->adapter_path) - g_free(device->adapter_path); + if (device->adapter_path) + g_free(device->adapter_path); - if (device->path) - g_free(device->path); + if (device->path) + g_free(device->path); - g_free(device); + g_free(device); } static void device_unregister(DBusConnection *conn, void *data) { - struct device *device = data; + struct device *device = data; + + info("Unregistered device path:%s", device->path); - 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->dst, bda); + bacpy(&device->src, &src); + 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; +} - device_free(device); +int device_store(struct device *device, gboolean is_default) +{ + char value[64]; + char filename[PATH_MAX + 1]; + char src_addr[18], dst_addr[18]; + int err; + + if (!device->path) + return -EINVAL; + + ba2str(&device->dst, dst_addr); + ba2str(&device->src, src_addr); + + create_name(filename, PATH_MAX, STORAGEDIR, src_addr, "audio"); + create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + + if (is_default) + err = textfile_put(filename, "default", dst_addr); + else { + if (device->headset) + snprintf(value, 64, "headset"); + if (device->gateway) + snprintf(value, 64, "%s:gateway", value); + if (device->sink) + snprintf(value, 64, "%s:sink", value); + if (device->source) + snprintf(value, 64, "%s:source", value); + if (device->control) + snprintf(value, 64, "%s:control", value); + if (device->target) + snprintf(value, 64, "%s:target", value); + err = textfile_put(filename, dst_addr, value); + } + + return err; } -struct device * device_register(DBusConnection *conn, const char *path, bdaddr_t *bda) +void device_finish_sdp_transaction(struct device *device) { - 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; + char address[18], *addr_ptr = address; + DBusMessage *msg, *reply; + DBusError derr; + + ba2str(&device->dst, 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); } diff --git a/audio/device.h b/audio/device.h index 1a9fd520..12e15806 100644 --- a/audio/device.h +++ b/audio/device.h @@ -26,48 +26,53 @@ #include "headset.h" #include "gateway.h" -#define AUDIO_DEVICE_INTERFACE "org.bluez.audio.Device" +#define AUDIO_DEVICE_INTERFACE "org.bluez.audio.Device" -#define GENERIC_AUDIO_UUID "00001203-0000-1000-8000-00805F9B34FB" +#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 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 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 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 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" +#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" +#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; + DBusConnection *conn; + char *adapter_path; + char *path; + bdaddr_t src; + bdaddr_t dst; + + 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); +struct device *device_register(DBusConnection *conn, + const char *path, bdaddr_t *bda); + +int device_store(struct device *device, gboolean is_default); + void device_finish_sdp_transaction(struct device *device); diff --git a/audio/error.c b/audio/error.c index 97ffb907..6bd6e08d 100644 --- a/audio/error.c +++ b/audio/error.c @@ -32,68 +32,72 @@ /* FIXME: Remove these once global error functions exist */ static DBusHandlerResult error_reply(DBusConnection *conn, DBusMessage *msg, - const char *name, const char *descr) + const char *name, const char *descr) { - DBusMessage *derr; + DBusMessage *derr; - if (!conn || !msg) - return DBUS_HANDLER_RESULT_HANDLED; + 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; - } + 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); + return send_message_and_unref(conn, derr); } -DBusHandlerResult err_invalid_args(DBusConnection *conn, DBusMessage *msg, - const char *descr) +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"); + 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"); + 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"); + 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"); + 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) + DBusMessage *msg, const char *err) { - return error_reply(conn, msg, AUDIO_ERROR_INTERFACE ".ConnectFailed", - 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"); + 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"); + return error_reply(conn, msg, ".NotAvailable", "Not available"); } -DBusHandlerResult err_failed(DBusConnection *conn, DBusMessage *msg, - const char *dsc) +DBusHandlerResult err_failed(DBusConnection *conn, + DBusMessage *msg, const char *dsc) { - return error_reply(conn, msg, AUDIO_ERROR_INTERFACE ".Failed", dsc); + return error_reply(conn, msg, AUDIO_ERROR_INTERFACE ".Failed", dsc); } diff --git a/audio/error.h b/audio/error.h index 82f19801..de4e8982 100644 --- a/audio/error.h +++ b/audio/error.h @@ -23,14 +23,14 @@ #include "dbus.h" -DBusHandlerResult err_invalid_args(DBusConnection *conn, DBusMessage *msg, - const char *descr); +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_connected(DBusConnection *conn, DBusMessage * msg); DBusHandlerResult err_not_supported(DBusConnection *conn, DBusMessage *msg); DBusHandlerResult err_connect_failed(DBusConnection *conn, - DBusMessage *msg, const char *err); + 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); +DBusHandlerResult err_failed(DBusConnection *conn, + DBusMessage *msg, const char *dsc); diff --git a/audio/gateway.c b/audio/gateway.c index 2ee67226..7064df2c 100644 --- a/audio/gateway.c +++ b/audio/gateway.c @@ -62,494 +62,497 @@ 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; + 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; + 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; + 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; + 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; - } + 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_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_error_init(&derr); + reply = dbus_connection_send_with_reply_and_block(connection, + msg, -1, &derr); - dbus_message_unref(msg); + 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; - } + 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); + dbus_message_unref(reply); - return 0; + 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); + 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->dst, 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); + 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->dst, 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) +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; + 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->dst, 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); + headset_close_rfcomm(device); - return TRUE; + 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 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; + uint8_t chan = DEFAULT_HS_AG_CHANNEL; + sdp_buf_t buf; - connection = dbus_connection_ref(conn); + connection = dbus_connection_ref(conn); - hs_server = server_socket(&chan); - if (!hs_server) - return -1; + 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; - } + 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; - } + 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); + 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; + disable_hfp = no_hfp; - sco_over_hci = sco_hci; + sco_over_hci = sco_hci; - if (disable_hfp) - return 0; + if (disable_hfp) + return 0; - chan = DEFAULT_HF_AG_CHANNEL; + chan = DEFAULT_HF_AG_CHANNEL; - hf_server = server_socket(&chan); - if (!hf_server) - return -1; + 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; - } + 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; - } + 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); + 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; + 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; + 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/headset.c b/audio/headset.c index 59fe21e7..a57c3e93 100644 --- a/audio/headset.c +++ b/audio/headset.c @@ -460,7 +460,7 @@ static int sco_connect(struct device *device, struct pending_connect *c) memset(&addr, 0, sizeof(addr)); addr.sco_family = AF_BLUETOOTH; - bacpy(&addr.sco_bdaddr, &device->bda); + bacpy(&addr.sco_bdaddr, &device->dst); if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) { if (!(errno == EAGAIN || errno == EINPROGRESS)) { @@ -520,7 +520,7 @@ static gboolean rfcomm_connect_cb(GIOChannel *chan, GIOCondition cond, goto failed; } - ba2str(&device->bda, hs_address); + ba2str(&device->dst, hs_address); hs->rfcomm = chan; hs->pending_connect->io = NULL; @@ -570,7 +570,7 @@ static int rfcomm_connect(struct device *device, int *err) assert(hs->pending_connect != NULL); assert(hs->state == HEADSET_STATE_CONNECT_IN_PROGRESS); - ba2str(&device->bda, address); + ba2str(&device->dst, address); debug("Connecting to %s channel %d", address, hs->rfcomm_ch); @@ -602,7 +602,7 @@ static int rfcomm_connect(struct device *device, int *err) memset(&addr, 0, sizeof(addr)); addr.rc_family = AF_BLUETOOTH; - bacpy(&addr.rc_bdaddr, &device->bda); + bacpy(&addr.rc_bdaddr, &device->dst); addr.rc_channel = hs->rfcomm_ch; hs->pending_connect->io = g_io_channel_unix_new(sk); @@ -864,7 +864,7 @@ static DBusHandlerResult hs_disconnect(DBusConnection *conn, DBusMessage *msg, hs->state = HEADSET_STATE_DISCONNECTED; - ba2str(&device->bda, hs_address); + ba2str(&device->dst, hs_address); info("Disconnected from %s, %s", hs_address, device->path); dbus_connection_emit_signal(device->conn, device->path, @@ -974,7 +974,7 @@ static void get_handles_reply(DBusPendingCall *call, void *data) goto failed; } - ba2str(&device->bda, address); + ba2str(&device->dst, address); handle = array[0]; @@ -1058,7 +1058,7 @@ static DBusHandlerResult hs_connect(DBusConnection *conn, DBusMessage *msg, else hs_svc = "hfp"; - ba2str(&device->bda, hs_address); + ba2str(&device->dst, hs_address); addr_ptr = hs_address; dbus_message_append_args(msg, DBUS_TYPE_STRING, &addr_ptr, DBUS_TYPE_STRING, &hs_svc, @@ -1556,7 +1556,7 @@ void headset_set_state(void *device, headset_state_t state) G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, (GIOFunc) rfcomm_io_cb, device); - ba2str(&((struct device *) device)->bda, hs_address); + ba2str(&((struct device *) device)->dst, hs_address); dbus_connection_emit_signal(((struct device *) device)->conn, ((struct device *) device)->path, diff --git a/audio/manager.c b/audio/manager.c index d8872968..316dd5cc 100644 --- a/audio/manager.c +++ b/audio/manager.c @@ -32,8 +32,13 @@ #include <unistd.h> #include <stdint.h> #include <assert.h> +#include <sys/stat.h> +#include <dirent.h> +#include <ctype.h> #include <bluetooth/bluetooth.h> +#include <bluetooth/hci.h> +#include <bluetooth/hci_lib.h> #include <bluetooth/rfcomm.h> #include <bluetooth/sdp.h> #include <bluetooth/sdp_lib.h> @@ -44,7 +49,7 @@ #include "dbus-helper.h" #include "logging.h" - +#include "textfile.h" #include "manager.h" #include "error.h" @@ -92,7 +97,7 @@ static struct device *find_device(bdaddr_t *bda) for (l = devices; l != NULL; l = l->next) { struct device *device = l->data; - if (bacmp(&device->bda, bda) == 0) + if (bacmp(&device->dst, bda) == 0) return device; } @@ -118,12 +123,18 @@ static void remove_device(struct device *device) static gboolean add_device(struct device *device) { + gboolean is_default; + /* First device became default */ if (g_slist_length(devices) == 0) default_dev = device; devices = g_slist_append(devices, device); + is_default = default_dev == device ? TRUE : FALSE; + + device_store(device, is_default); + return TRUE; } @@ -446,7 +457,7 @@ static void get_next_record(struct audio_sdp_data *data) data->handles = g_slist_remove(data->handles, data->handles->data); - ba2str(&data->device->bda, address); + ba2str(&data->device->dst, address); dbus_message_append_args(msg, DBUS_TYPE_STRING, &ptr, DBUS_TYPE_UINT32, handle, @@ -561,7 +572,7 @@ static DBusHandlerResult get_handles(const char *uuid, goto failed; } - ba2str(&data->device->bda, address); + ba2str(&data->device->dst, address); dbus_message_append_args(msg, DBUS_TYPE_STRING, &ptr, DBUS_TYPE_STRING, &uuid, @@ -658,27 +669,27 @@ struct device *manager_device_connected(bdaddr_t *bda) static gboolean device_supports_interface(struct device *device, const char *iface) { - if (strcmp(iface, AUDIO_HEADSET_INTERFACE) == 0) - return device->headset ? TRUE : FALSE; + if (strcmp(iface, AUDIO_HEADSET_INTERFACE) == 0) + return device->headset ? TRUE : FALSE; - if (strcmp(iface, AUDIO_GATEWAY_INTERFACE) == 0) - return device->gateway ? TRUE : FALSE; + if (strcmp(iface, AUDIO_GATEWAY_INTERFACE) == 0) + return device->gateway ? TRUE : FALSE; - if (strcmp(iface, AUDIO_SOURCE_INTERFACE) == 0) - return device->source ? TRUE : FALSE; + if (strcmp(iface, AUDIO_SOURCE_INTERFACE) == 0) + return device->source ? TRUE : FALSE; - if (strcmp(iface, AUDIO_SINK_INTERFACE) == 0) + if (strcmp(iface, AUDIO_SINK_INTERFACE) == 0) return device->sink ? TRUE : FALSE; - if (strcmp(iface, AUDIO_CONTROL_INTERFACE) == 0) - return device->control ? TRUE : FALSE; + if (strcmp(iface, AUDIO_CONTROL_INTERFACE) == 0) + return device->control ? TRUE : FALSE; - if (strcmp(iface, AUDIO_TARGET_INTERFACE) == 0) - return device->target ? TRUE : FALSE; + if (strcmp(iface, AUDIO_TARGET_INTERFACE) == 0) + return device->target ? TRUE : FALSE; - debug("Unknown interface %s", iface); + debug("Unknown interface %s", iface); - return FALSE; + return FALSE; } static gboolean device_matches(struct device *device, char **interfaces) @@ -1069,6 +1080,97 @@ static DBusSignalVTable manager_signals[] = { { NULL, NULL } }; +static void parse_stored_devices(char *key, char *value, void *data) +{ + struct device *device; + bdaddr_t dst; + char addr[18]; + char *ptr; + char ifaces[6][8]; + int len, i; + + if (!key || !value) + return; + + /* Format: XX:XX:XX:XX:XX:XX interface0:interface1:... */ + memset(addr, 0, 18); + strncpy(addr, key, 17); + str2ba(addr, &dst); + + if ((device = create_device(&dst)) == NULL) + return; + + /* Parsing the interface */ + ptr = strchr(value, ':'); + + if (!ptr) { + strncpy(ifaces[0], value, 8); + if (strcmp(ifaces[0], "headset") == 0) + device->headset = headset_init(device, NULL, 0); + } + + /* FIXME: has more than 1 interface */ + for (i = 0; ptr && i < 6; i++) { + len = ptr-value; + strncpy(ifaces[i], value, len); + value = ptr; + ptr = strchr(ptr, ':'); + } + + add_device(device); +} + +static void register_devices_stored(const char *adapter) +{ + char filename[PATH_MAX + 1]; + char *addr; + struct stat s; + bdaddr_t src; + bdaddr_t dst; + bdaddr_t default_src; + int dev_id; + + create_name(filename, PATH_MAX, STORAGEDIR, adapter, "audio"); + + str2ba(adapter, &src); + + bacpy(&default_src, BDADDR_ANY); + dev_id = hci_get_route(NULL); + if (dev_id < 0) + hci_devba(dev_id, &default_src); + + if (stat(filename, &s) == 0 && (s.st_mode & __S_IFREG)) { + textfile_foreach(filename, parse_stored_devices, &src); + addr = textfile_get(filename, "default"); + + str2ba(addr, &dst); + default_dev = find_device(&dst); + } +} + +static void register_stored(void) +{ + char dirname[PATH_MAX + 1]; + struct dirent *de; + DIR *dir; + + snprintf(dirname, PATH_MAX, "%s", STORAGEDIR); + + dir = opendir(dirname); + if (!dir) + return; + + while ((de = readdir(dir)) != NULL) { + if (!isdigit(de->d_name[0])) + continue; + + /* Device objects */ + register_devices_stored(de->d_name); + } + + closedir(dir); +} + int audio_init(DBusConnection *conn) { if (!dbus_connection_create_object_path(conn, AUDIO_MANAGER_PATH, @@ -1090,6 +1192,10 @@ int audio_init(DBusConnection *conn) connection = dbus_connection_ref(conn); + info("Registered manager path:%s", AUDIO_MANAGER_PATH); + + register_stored(); + return 0; } diff --git a/audio/manager.h b/audio/manager.h index 65515483..f21e958e 100644 --- a/audio/manager.h +++ b/audio/manager.h @@ -25,6 +25,7 @@ #include "device.h" +#define MAX_PATH_LENGTH 64 /* D-Bus path */ #define AUDIO_MANAGER_PATH "/org/bluez/audio" #define AUDIO_MANAGER_INTERFACE "org.bluez.audio.Manager" |