2013-02-20 21:55:45 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* oFono - Open Source Telephony
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 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 version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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 <errno.h>
|
|
|
|
#include <stdio.h>
|
2013-02-20 21:55:50 +00:00
|
|
|
#include <string.h>
|
2013-02-20 21:55:45 +00:00
|
|
|
|
|
|
|
#include <gdbus.h>
|
|
|
|
|
2013-02-21 14:27:55 +00:00
|
|
|
#include <ofono/handsfree-audio.h>
|
|
|
|
|
2013-02-20 21:55:45 +00:00
|
|
|
#include "ofono.h"
|
|
|
|
|
2013-02-21 03:28:01 +00:00
|
|
|
#define HFP_AUDIO_MANAGER_INTERFACE OFONO_SERVICE ".HandsfreeAudioManager"
|
|
|
|
#define HFP_AUDIO_AGENT_INTERFACE OFONO_SERVICE ".HandsfreeAudioAgent"
|
2013-02-26 19:22:02 +00:00
|
|
|
#define HFP_AUDIO_CARD_INTERFACE OFONO_SERVICE ".HandsfreeAudioCard"
|
2013-02-20 21:55:45 +00:00
|
|
|
|
2013-02-20 21:55:47 +00:00
|
|
|
/* Supported agent codecs */
|
|
|
|
enum hfp_codec {
|
|
|
|
HFP_CODEC_CVSD = 0x01,
|
|
|
|
HFP_CODEC_MSBC = 0x02,
|
|
|
|
};
|
|
|
|
|
2013-02-26 17:03:00 +00:00
|
|
|
struct ofono_handsfree_card {
|
|
|
|
char *remote;
|
|
|
|
char *local;
|
2013-02-26 19:22:02 +00:00
|
|
|
char *path;
|
2013-02-26 17:03:00 +00:00
|
|
|
};
|
|
|
|
|
2013-02-20 21:55:46 +00:00
|
|
|
struct agent {
|
|
|
|
char *owner;
|
|
|
|
char *path;
|
|
|
|
unsigned char *codecs;
|
|
|
|
int codecs_len;
|
2013-02-20 21:55:51 +00:00
|
|
|
guint watch;
|
2013-02-20 21:55:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct agent *agent = NULL;
|
2013-02-21 14:27:55 +00:00
|
|
|
static int ref_count = 0;
|
2013-02-26 17:03:00 +00:00
|
|
|
static GSList *card_list = 0;
|
|
|
|
|
2013-02-26 19:35:38 +00:00
|
|
|
static void card_append_properties(struct ofono_handsfree_card *card,
|
|
|
|
DBusMessageIter *dict)
|
|
|
|
{
|
|
|
|
ofono_dbus_dict_append(dict, "RemoteAddress",
|
|
|
|
DBUS_TYPE_STRING, &card->remote);
|
|
|
|
|
|
|
|
ofono_dbus_dict_append(dict, "LocalAddress",
|
|
|
|
DBUS_TYPE_STRING, &card->local);
|
|
|
|
}
|
|
|
|
|
2013-02-26 19:22:02 +00:00
|
|
|
static DBusMessage *card_get_properties(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *data)
|
|
|
|
{
|
2013-02-26 19:35:38 +00:00
|
|
|
struct ofono_handsfree_card *card = data;
|
|
|
|
DBusMessage *reply;
|
|
|
|
DBusMessageIter iter;
|
|
|
|
DBusMessageIter dict;
|
|
|
|
|
|
|
|
reply = dbus_message_new_method_return(msg);
|
|
|
|
if (reply == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dbus_message_iter_init_append(reply, &iter);
|
|
|
|
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
|
|
|
|
OFONO_PROPERTIES_ARRAY_SIGNATURE,
|
|
|
|
&dict);
|
|
|
|
|
|
|
|
card_append_properties(card, &dict);
|
|
|
|
|
|
|
|
dbus_message_iter_close_container(&iter, &dict);
|
|
|
|
|
|
|
|
return reply;
|
2013-02-26 19:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *card_connect(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *data)
|
|
|
|
{
|
|
|
|
return __ofono_error_not_implemented(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GDBusMethodTable card_methods[] = {
|
|
|
|
{ GDBUS_METHOD("GetProperties",
|
|
|
|
NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
|
|
|
|
card_get_properties) },
|
|
|
|
{ GDBUS_ASYNC_METHOD("Connect", NULL, NULL, card_connect) },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const GDBusSignalTable card_signals[] = {
|
|
|
|
{ GDBUS_SIGNAL("PropertyChanged",
|
|
|
|
GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2013-02-26 17:03:00 +00:00
|
|
|
struct ofono_handsfree_card *ofono_handsfree_card_create(const char *remote,
|
|
|
|
const char *local)
|
|
|
|
{
|
|
|
|
struct ofono_handsfree_card *card;
|
|
|
|
|
|
|
|
card = g_new0(struct ofono_handsfree_card, 1);
|
|
|
|
|
|
|
|
card->remote = g_strdup(remote);
|
|
|
|
card->local = g_strdup(local);
|
|
|
|
|
|
|
|
card_list = g_slist_prepend(card_list, card);
|
|
|
|
|
|
|
|
return card;
|
|
|
|
}
|
|
|
|
|
2013-02-26 19:22:02 +00:00
|
|
|
int ofono_handsfree_card_register(struct ofono_handsfree_card *card)
|
|
|
|
{
|
|
|
|
static int next_card_id = 1;
|
|
|
|
char path[64];
|
|
|
|
|
|
|
|
if (card == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path), "/card_%d", next_card_id);
|
|
|
|
|
|
|
|
if (!g_dbus_register_interface(ofono_dbus_get_connection(), path,
|
|
|
|
HFP_AUDIO_CARD_INTERFACE,
|
|
|
|
card_methods, card_signals, NULL,
|
|
|
|
card, NULL))
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
card->path = g_strdup(path);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-26 20:09:15 +00:00
|
|
|
static void card_unregister(struct ofono_handsfree_card *card)
|
|
|
|
{
|
|
|
|
DBusConnection *conn = ofono_dbus_get_connection();
|
|
|
|
|
|
|
|
g_dbus_unregister_interface(conn, card->path, HFP_AUDIO_CARD_INTERFACE);
|
|
|
|
|
|
|
|
g_free(card->path);
|
|
|
|
card->path = NULL;
|
|
|
|
}
|
|
|
|
|
2013-02-26 17:03:00 +00:00
|
|
|
void ofono_handsfree_card_remove(struct ofono_handsfree_card *card)
|
|
|
|
{
|
|
|
|
DBG("%p", card);
|
|
|
|
|
|
|
|
if (card == NULL)
|
|
|
|
return;
|
|
|
|
|
2013-02-26 20:09:15 +00:00
|
|
|
if (card->path)
|
|
|
|
card_unregister(card);
|
|
|
|
|
2013-02-26 17:03:00 +00:00
|
|
|
card_list = g_slist_remove(card_list, card);
|
|
|
|
|
|
|
|
g_free(card->remote);
|
|
|
|
g_free(card->local);
|
|
|
|
|
|
|
|
g_free(card);
|
|
|
|
}
|
2013-02-20 21:55:46 +00:00
|
|
|
|
2013-02-20 21:55:48 +00:00
|
|
|
static void agent_free(struct agent *agent)
|
|
|
|
{
|
2013-02-20 21:55:51 +00:00
|
|
|
if (agent->watch > 0)
|
|
|
|
g_dbus_remove_watch(ofono_dbus_get_connection(), agent->watch);
|
|
|
|
|
2013-02-20 21:55:48 +00:00
|
|
|
g_free(agent->owner);
|
|
|
|
g_free(agent->path);
|
|
|
|
g_free(agent->codecs);
|
|
|
|
g_free(agent);
|
|
|
|
}
|
|
|
|
|
2013-02-20 21:55:49 +00:00
|
|
|
static void agent_release(struct agent *agent)
|
|
|
|
{
|
|
|
|
DBusMessage *msg;
|
|
|
|
|
|
|
|
msg = dbus_message_new_method_call(agent->owner, agent->path,
|
|
|
|
HFP_AUDIO_AGENT_INTERFACE, "Release");
|
|
|
|
|
|
|
|
g_dbus_send_message(ofono_dbus_get_connection(), msg);
|
|
|
|
}
|
|
|
|
|
2013-02-20 21:55:51 +00:00
|
|
|
static void agent_disconnect(DBusConnection *conn, void *user_data)
|
|
|
|
{
|
|
|
|
DBG("Agent %s disconnected", agent->owner);
|
|
|
|
|
|
|
|
agent_free(agent);
|
|
|
|
agent = NULL;
|
|
|
|
}
|
|
|
|
|
2013-02-20 21:55:45 +00:00
|
|
|
static DBusMessage *am_get_cards(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
return __ofono_error_not_implemented(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *am_agent_register(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
2013-02-20 21:55:46 +00:00
|
|
|
const char *sender, *path;
|
|
|
|
unsigned char *codecs;
|
|
|
|
DBusMessageIter iter, array;
|
2013-02-20 21:55:47 +00:00
|
|
|
int length, i;
|
2013-02-20 21:55:46 +00:00
|
|
|
|
|
|
|
if (agent)
|
|
|
|
return __ofono_error_in_use(msg);
|
|
|
|
|
|
|
|
sender = dbus_message_get_sender(msg);
|
|
|
|
|
|
|
|
if (dbus_message_iter_init(msg, &iter) == FALSE)
|
|
|
|
return __ofono_error_invalid_args(msg);
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&iter, &path);
|
|
|
|
|
|
|
|
dbus_message_iter_next(&iter);
|
|
|
|
dbus_message_iter_recurse(&iter, &array);
|
|
|
|
dbus_message_iter_get_fixed_array(&array, &codecs, &length);
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
return __ofono_error_invalid_args(msg);
|
|
|
|
|
2013-02-20 21:55:47 +00:00
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
if (codecs[i] != HFP_CODEC_CVSD &&
|
|
|
|
codecs[i] != HFP_CODEC_MSBC)
|
|
|
|
return __ofono_error_invalid_args(msg);
|
|
|
|
}
|
|
|
|
|
2013-02-20 21:55:46 +00:00
|
|
|
agent = g_new0(struct agent, 1);
|
|
|
|
agent->owner = g_strdup(sender);
|
|
|
|
agent->path = g_strdup(path);
|
|
|
|
agent->codecs = g_memdup(codecs, length);
|
|
|
|
agent->codecs_len = length;
|
2013-02-20 21:55:51 +00:00
|
|
|
agent->watch = g_dbus_add_disconnect_watch(conn, sender,
|
|
|
|
agent_disconnect, NULL, NULL);
|
2013-02-20 21:55:46 +00:00
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
2013-02-20 21:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *am_agent_unregister(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
2013-02-20 21:55:50 +00:00
|
|
|
const char *sender, *path;
|
|
|
|
DBusMessageIter iter;
|
|
|
|
|
|
|
|
if (agent == NULL)
|
|
|
|
return __ofono_error_not_found(msg);
|
|
|
|
|
|
|
|
sender = dbus_message_get_sender(msg);
|
|
|
|
|
|
|
|
if (dbus_message_iter_init(msg, &iter) == FALSE)
|
|
|
|
return __ofono_error_invalid_args(msg);
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&iter, &path);
|
|
|
|
|
|
|
|
if (strcmp(sender, agent->owner) != 0)
|
|
|
|
return __ofono_error_not_allowed(msg);
|
|
|
|
|
|
|
|
if (strcmp(path, agent->path) != 0)
|
|
|
|
return __ofono_error_not_found(msg);
|
|
|
|
|
|
|
|
agent_free(agent);
|
|
|
|
agent = NULL;
|
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
2013-02-20 21:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const GDBusMethodTable am_methods[] = {
|
|
|
|
{ GDBUS_METHOD("GetCards",
|
|
|
|
NULL, GDBUS_ARGS({"cards", "a{oa{sv}}"}),
|
|
|
|
am_get_cards) } ,
|
|
|
|
{ GDBUS_METHOD("Register",
|
|
|
|
GDBUS_ARGS({"path", "o"}, {"codecs", "ay"}), NULL,
|
|
|
|
am_agent_register) },
|
|
|
|
{ GDBUS_METHOD("Unregister",
|
|
|
|
GDBUS_ARGS({"path", "o"}), NULL,
|
|
|
|
am_agent_unregister) },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2013-02-21 14:27:55 +00:00
|
|
|
void ofono_handsfree_audio_ref(void)
|
2013-02-20 21:55:45 +00:00
|
|
|
{
|
2013-02-21 14:27:55 +00:00
|
|
|
ref_count += 1;
|
|
|
|
|
|
|
|
if (ref_count != 1)
|
|
|
|
return;
|
|
|
|
|
2013-02-20 21:55:45 +00:00
|
|
|
if (!g_dbus_register_interface(ofono_dbus_get_connection(),
|
2013-02-26 20:01:47 +00:00
|
|
|
OFONO_MANAGER_PATH,
|
|
|
|
HFP_AUDIO_MANAGER_INTERFACE,
|
2013-02-21 14:27:55 +00:00
|
|
|
am_methods, NULL, NULL, NULL, NULL))
|
|
|
|
ofono_error("Unable to register Handsfree Audio Manager");
|
2013-02-20 21:55:45 +00:00
|
|
|
}
|
|
|
|
|
2013-02-21 14:27:55 +00:00
|
|
|
void ofono_handsfree_audio_unref(void)
|
2013-02-20 21:55:45 +00:00
|
|
|
{
|
2013-02-21 14:27:55 +00:00
|
|
|
if (ref_count == 0) {
|
|
|
|
ofono_error("Error in handsfree audio manager ref counting");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref_count -= 1;
|
|
|
|
|
|
|
|
if (ref_count > 0)
|
|
|
|
return;
|
|
|
|
|
2013-02-26 20:01:47 +00:00
|
|
|
g_dbus_unregister_interface(ofono_dbus_get_connection(),
|
|
|
|
OFONO_MANAGER_PATH,
|
|
|
|
HFP_AUDIO_MANAGER_INTERFACE);
|
2013-02-20 21:55:48 +00:00
|
|
|
|
2013-02-20 21:55:49 +00:00
|
|
|
if (agent) {
|
|
|
|
agent_release(agent);
|
2013-02-20 21:55:48 +00:00
|
|
|
agent_free(agent);
|
2013-02-20 21:55:49 +00:00
|
|
|
}
|
2013-02-20 21:55:45 +00:00
|
|
|
}
|
2013-02-21 14:27:55 +00:00
|
|
|
|
|
|
|
int __ofono_handsfree_audio_manager_init(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ofono_handsfree_audio_manager_cleanup(void)
|
|
|
|
{
|
|
|
|
if (ref_count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ofono_error("Handsfree Audio manager not cleaned up properly,"
|
|
|
|
"fixing...");
|
|
|
|
|
|
|
|
ref_count = 1;
|
|
|
|
ofono_handsfree_audio_unref();
|
|
|
|
}
|