To let others (PulseAudio) be notified when a handsfree device is
connected with us and can be used for audio routing we need to
expose this by registering a audio card with the correct type.
For now there is no need to provide a card driver and we just stick
to what the core provides.
---
plugins/hfp_ag_bluez5.c | 77 +++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 68 insertions(+), 9 deletions(-)
diff --git a/plugins/hfp_ag_bluez5.c b/plugins/hfp_ag_bluez5.c
index 93de302..50238d8 100644
--- a/plugins/hfp_ag_bluez5.c
+++ b/plugins/hfp_ag_bluez5.c
@@ -22,6 +22,7 @@
#include <config.h>
#endif
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
@@ -35,21 +36,30 @@
#include <ofono/plugin.h>
#include <ofono/log.h>
#include <ofono/modem.h>
+#include <ofono/handsfree.h>
+#include <ofono/handsfree-audio.h>
#include "hfp.h"
#include "bluez5.h"
+#include "bluetooth.h"
#ifndef DBUS_TYPE_UNIX_FD
#define DBUS_TYPE_UNIX_FD -1
#endif
#define HFP_AG_EXT_PROFILE_PATH "/bluetooth/profile/hfp_ag"
+#define BT_ADDR_SIZE 18
static guint modemwatch_id;
static GList *modems;
static GHashTable *sim_hash = NULL;
static GHashTable *connection_hash;
+struct device_info {
+ int fd;
+ struct ofono_handsfree_card *card;
+};
+
static void connection_destroy(gpointer data)
{
int fd = GPOINTER_TO_INT(data);
@@ -76,9 +86,14 @@ static DBusMessage *profile_new_connection(DBusConnection *conn,
DBusMessageIter entry;
const char *device;
GIOChannel *io;
- int fd, fd_dup;
+ int fd;
+ struct sockaddr_rc saddr;
+ socklen_t optlen;
struct ofono_emulator *em;
struct ofono_modem *modem;
+ struct device_info *dev;
+ char local[BT_ADDR_SIZE], remote[BT_ADDR_SIZE];
+ int err;
DBG("Profile handler NewConnection");
@@ -95,7 +110,10 @@ static DBusMessage *profile_new_connection(DBusConnection *conn,
goto invalid;
dbus_message_iter_get_basic(&entry, &fd);
+
dbus_message_iter_next(&entry);
+ if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_ARRAY)
+ goto invalid;
if (fd < 0)
goto invalid;
@@ -111,8 +129,35 @@ static DBusMessage *profile_new_connection(DBusConnection *conn,
}
modem = modems->data;
+
DBG("Picked modem %p for emulator", modem);
+ memset(&saddr, 0, sizeof(saddr));
+ optlen = sizeof(saddr);
+
+ if (getsockname(fd, (struct sockaddr *) &saddr, &optlen) < 0) {
+ err = errno;
+ ofono_error("RFCOMM getsockname(): %s (%d)", strerror(err),
+ err);
+ close(fd);
+ goto invalid;
+ }
+
+ bt_ba2str(&saddr.rc_bdaddr, local);
+
+ memset(&saddr, 0, sizeof(saddr));
+ optlen = sizeof(saddr);
+
+ if (getpeername(fd, (struct sockaddr *) &saddr, &optlen) < 0) {
+ err = errno;
+ ofono_error("RFCOMM getpeername(): %s (%d)", strerror(err),
+ err);
+ close(fd);
+ goto invalid;
+ }
+
+ bt_ba2str(&saddr.rc_bdaddr, remote);
+
em = ofono_emulator_create(modem, OFONO_EMULATOR_TYPE_HFP);
if (em == NULL) {
close(fd);
@@ -123,14 +168,24 @@ static DBusMessage *profile_new_connection(DBusConnection *conn,
ofono_emulator_register(em, fd);
- fd_dup = dup(fd);
- io = g_io_channel_unix_new(fd_dup);
+ io = g_io_channel_unix_new(fd);
g_io_add_watch_full(io, G_PRIORITY_DEFAULT, G_IO_HUP, io_hup_cb,
g_strdup(device), g_free);
g_io_channel_unref(io);
- g_hash_table_insert(connection_hash, g_strdup(device),
- GINT_TO_POINTER(fd_dup));
+ dev = g_new0(struct device_info, 1);
+ dev->fd = fd;
+
+ dev->card = ofono_handsfree_card_create(0,
+ OFONO_HANDSFREE_CARD_TYPE_GATEWAY,
+ NULL, NULL);
+
+ ofono_handsfree_card_set_local(dev->card, local);
+ ofono_handsfree_card_set_remote(dev->card, remote);
+
+ g_hash_table_insert(connection_hash, g_strdup(device), dev);
+
+ ofono_handsfree_card_register(dev->card);
return dbus_message_new_method_return(msg);
@@ -164,7 +219,7 @@ static DBusMessage *profile_disconnection(DBusConnection *conn,
{
DBusMessageIter iter;
const char *device;
- gpointer fd;
+ struct device_info *dev;
DBG("Profile handler RequestDisconnection");
@@ -178,11 +233,11 @@ static DBusMessage *profile_disconnection(DBusConnection *conn,
DBG("%s", device);
- fd = g_hash_table_lookup(connection_hash, device);
- if (fd == NULL)
+ dev = g_hash_table_lookup(connection_hash, device);
+ if (dev == NULL)
goto invalid;
- shutdown(GPOINTER_TO_INT(fd), SHUT_RDWR);
+ shutdown(dev->fd, SHUT_RDWR);
g_hash_table_remove(connection_hash, device);
@@ -346,6 +401,8 @@ static int hfp_ag_init(void)
connection_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, connection_destroy);
+ ofono_handsfree_audio_ref();
+
return 0;
}
@@ -362,6 +419,8 @@ static void hfp_ag_exit(void)
g_list_free(modems);
g_hash_table_foreach_remove(sim_hash, sim_watch_remove, NULL);
g_hash_table_destroy(sim_hash);
+
+ ofono_handsfree_audio_unref();
}
OFONO_PLUGIN_DEFINE(hfp_ag_bluez5, "Hands-Free Audio Gateway Profile Plugins",
--
2.5.0