Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'hfp_ag' into 'master'
HFP AG BlueZ5 plugin reconnects to daemon if daemon restarts.

See merge request mer-core/ofono!201
  • Loading branch information
Slava Monich committed Nov 30, 2018
2 parents c1c3eca + 4f6f964 commit 75b07c5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 29 deletions.
102 changes: 73 additions & 29 deletions ofono/plugins/hfp_ag_bluez5.c
Expand Up @@ -47,15 +47,13 @@ typedef struct GAtResult GAtResult;
#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

#define HFP_AG_DRIVER "hfp-ag-driver"

static gboolean hfp_ag_enabled;
static guint service_watch_id;
static guint modemwatch_id;
static GList *modems;
static GHashTable *sim_hash = NULL;
Expand Down Expand Up @@ -466,29 +464,27 @@ static void call_modemwatch(struct ofono_modem *modem, void *user)
modem_watch(modem, TRUE, user);
}

static int hfp_ag_init(void)
static void hfp_ag_enable(DBusConnection *conn)
{
DBusConnection *conn = ofono_dbus_get_connection();
int err;

if (DBUS_TYPE_UNIX_FD < 0)
return -EBADF;

/* Registers External Profile handler */
if (!g_dbus_register_interface(conn, HFP_AG_EXT_PROFILE_PATH,
BLUEZ_PROFILE_INTERFACE,
profile_methods, NULL,
NULL, NULL, NULL)) {
if (!g_dbus_register_interface(conn,
HFP_AG_EXT_PROFILE_PATH,
BLUEZ_PROFILE_INTERFACE,
profile_methods,
NULL, NULL, NULL, NULL)) {
ofono_error("Register Profile interface failed: %s",
HFP_AG_EXT_PROFILE_PATH);
return -EIO;
HFP_AG_EXT_PROFILE_PATH);
return;
}

err = ofono_handsfree_card_driver_register(&hfp_ag_driver);
if (err < 0) {
g_dbus_unregister_interface(conn, HFP_AG_EXT_PROFILE_PATH,
BLUEZ_PROFILE_INTERFACE);
return err;
BLUEZ_PROFILE_INTERFACE);
ofono_error("Failed to register driver: %d", err);
return;
}

sim_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
Expand All @@ -497,30 +493,78 @@ static int hfp_ag_init(void)
__ofono_modem_foreach(call_modemwatch, NULL);

connection_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, connection_destroy);
g_free, connection_destroy);

ofono_handsfree_audio_ref();

return 0;
hfp_ag_enabled = TRUE;
}

static void hfp_ag_exit(void)
static void hfp_ag_disable(DBusConnection *conn)
{
if (modemwatch_id) {
__ofono_modemwatch_remove(modemwatch_id);
modemwatch_id = 0;
}

if (connection_hash) {
g_hash_table_destroy(connection_hash);
connection_hash = NULL;
}

g_list_free(modems);
modems = NULL;

if (sim_hash) {
g_hash_table_foreach_remove(sim_hash, sim_watch_remove, NULL);
g_hash_table_destroy(sim_hash);
sim_hash = NULL;
}

if (hfp_ag_enabled) {
g_dbus_unregister_interface(conn, HFP_AG_EXT_PROFILE_PATH,
BLUEZ_PROFILE_INTERFACE);
ofono_handsfree_card_driver_unregister(&hfp_ag_driver);
ofono_handsfree_audio_unref();
}

hfp_ag_enabled = FALSE;
}

static void bluez_connect_cb(DBusConnection *connection, void *user_data)
{
hfp_ag_enable(connection);
}

static void bluez_disconnect_cb(DBusConnection *connection, void *user_data)
{
hfp_ag_disable(connection);
}

static int hfp_ag_init(void)
{
DBusConnection *conn = ofono_dbus_get_connection();

__ofono_modemwatch_remove(modemwatch_id);
g_dbus_unregister_interface(conn, HFP_AG_EXT_PROFILE_PATH,
BLUEZ_PROFILE_INTERFACE);
hfp_ag_enable(conn);

ofono_handsfree_card_driver_unregister(&hfp_ag_driver);
service_watch_id = g_dbus_add_service_watch(conn, "org.bluez",
bluez_connect_cb,
bluez_disconnect_cb,
NULL, NULL);

g_hash_table_destroy(connection_hash);
return 0;
}

g_list_free(modems);
g_hash_table_foreach_remove(sim_hash, sim_watch_remove, NULL);
g_hash_table_destroy(sim_hash);
static void hfp_ag_exit(void)
{
DBusConnection *conn = ofono_dbus_get_connection();

if (service_watch_id) {
g_dbus_remove_watch(conn, service_watch_id);
service_watch_id = 0;
}

ofono_handsfree_audio_unref();
hfp_ag_disable(conn);
}

OFONO_PLUGIN_DEFINE(hfp_ag_bluez5, "Hands-Free Audio Gateway Profile Plugins",
Expand Down
1 change: 1 addition & 0 deletions ofono/src/handsfree-audio.c
Expand Up @@ -912,6 +912,7 @@ void ofono_handsfree_audio_unref(void)
if (agent) {
agent_release(agent);
agent_free(agent);
agent = NULL;
}

__ofono_handsfree_audio_manager_cleanup();
Expand Down

0 comments on commit 75b07c5

Please sign in to comment.