3
* oFono - Open Source Telephony
5
* Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
6
* Copyright (C) 2010 ProFUSION embedded systems
7
* Copyright (C) 2011 BMW Car IT GmbH. All rights reserved.
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License version 2 as
11
* published by the Free Software Foundation.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38
#define OFONO_API_SUBJECT_TO_CHANGE
39
#include <ofono/plugin.h>
40
#include <ofono/log.h>
41
#include <ofono/modem.h>
42
#include <ofono/devinfo.h>
43
#include <ofono/netreg.h>
44
#include <ofono/voicecall.h>
45
#include <ofono/call-volume.h>
46
#include <ofono/handsfree.h>
47
#include <ofono/siri.h>
49
#include <drivers/hfpmodem/slc.h>
53
#define BLUEZ_GATEWAY_INTERFACE BLUEZ_SERVICE ".HandsfreeGateway"
55
#define HFP_AGENT_INTERFACE "org.bluez.HandsfreeAgent"
56
#define HFP_AGENT_ERROR_INTERFACE "org.bluez.Error"
58
#ifndef DBUS_TYPE_UNIX_FD
59
#define DBUS_TYPE_UNIX_FD -1
62
static DBusConnection *connection;
63
static GHashTable *modem_hash = NULL;
66
struct hfp_slc_info info;
68
char *handsfree_address;
70
gboolean agent_registered;
71
DBusPendingCall *call;
74
static void hfp_debug(const char *str, void *user_data)
76
const char *prefix = user_data;
78
ofono_info("%s%s", prefix, str);
81
static void slc_established(gpointer userdata)
83
struct ofono_modem *modem = userdata;
84
struct hfp_data *data = ofono_modem_get_data(modem);
87
ofono_modem_set_powered(modem, TRUE);
89
msg = dbus_message_new_method_return(data->slc_msg);
90
g_dbus_send_message(connection, msg);
91
dbus_message_unref(data->slc_msg);
94
ofono_info("Service level connection established");
97
static void slc_failed(gpointer userdata)
99
struct ofono_modem *modem = userdata;
100
struct hfp_data *data = ofono_modem_get_data(modem);
103
msg = g_dbus_create_error(data->slc_msg, HFP_AGENT_ERROR_INTERFACE
105
"HFP Handshake failed");
106
g_dbus_send_message(connection, msg);
107
dbus_message_unref(data->slc_msg);
108
data->slc_msg = NULL;
110
ofono_error("Service level connection failed");
111
ofono_modem_set_powered(modem, FALSE);
113
g_at_chat_unref(data->info.chat);
114
data->info.chat = NULL;
117
static void hfp_disconnected_cb(gpointer user_data)
119
struct ofono_modem *modem = user_data;
120
struct hfp_data *data = ofono_modem_get_data(modem);
122
ofono_modem_set_powered(modem, FALSE);
124
g_at_chat_unref(data->info.chat);
125
data->info.chat = NULL;
128
/* either oFono or Phone could request SLC connection */
129
static int service_level_connection(struct ofono_modem *modem, int fd)
131
struct hfp_data *data = ofono_modem_get_data(modem);
136
io = g_io_channel_unix_new(fd);
138
ofono_error("Service level connection failed: %s (%d)",
139
strerror(errno), errno);
143
syntax = g_at_syntax_new_gsm_permissive();
144
chat = g_at_chat_new(io, syntax);
145
g_at_syntax_unref(syntax);
146
g_io_channel_unref(io);
151
g_at_chat_set_disconnect_function(chat, hfp_disconnected_cb, modem);
153
if (getenv("OFONO_AT_DEBUG"))
154
g_at_chat_set_debug(chat, hfp_debug, "");
156
data->info.chat = chat;
157
hfp_slc_establish(&data->info, slc_established, slc_failed, modem);
162
static DBusMessage *hfp_agent_new_connection(DBusConnection *conn,
163
DBusMessage *msg, void *data)
166
struct ofono_modem *modem = data;
167
struct hfp_data *hfp_data = ofono_modem_get_data(modem);
170
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_UNIX_FD, &fd,
171
DBUS_TYPE_UINT16, &version, DBUS_TYPE_INVALID))
172
return __ofono_error_invalid_args(msg);
174
hfp_slc_info_init(&hfp_data->info, version);
176
err = service_level_connection(modem, fd);
177
if (err < 0 && err != -EINPROGRESS)
178
return __ofono_error_failed(msg);
180
hfp_data->slc_msg = msg;
181
dbus_message_ref(msg);
186
static DBusMessage *hfp_agent_release(DBusConnection *conn,
187
DBusMessage *msg, void *data)
189
struct ofono_modem *modem = data;
190
struct hfp_data *hfp_data = ofono_modem_get_data(modem);
191
const char *obj_path = ofono_modem_get_path(modem);
193
g_dbus_unregister_interface(connection, obj_path, HFP_AGENT_INTERFACE);
194
hfp_data->agent_registered = FALSE;
196
g_hash_table_remove(modem_hash, hfp_data->handsfree_path);
197
ofono_modem_remove(modem);
199
return dbus_message_new_method_return(msg);
202
static const GDBusMethodTable agent_methods[] = {
203
{ GDBUS_ASYNC_METHOD("NewConnection",
204
GDBUS_ARGS({ "fd", "h" }, { "version", "q" }),
205
NULL, hfp_agent_new_connection) },
206
{ GDBUS_METHOD("Release", NULL, NULL, hfp_agent_release) },
210
static int hfp_hf_probe(const char *device, const char *dev_addr,
211
const char *adapter_addr, const char *alias)
213
struct ofono_modem *modem;
214
struct hfp_data *data;
217
/* We already have this device in our hash, ignore */
218
if (g_hash_table_lookup(modem_hash, device) != NULL)
221
ofono_info("Using device: %s, devaddr: %s, adapter: %s",
222
device, dev_addr, adapter_addr);
225
bluetooth_create_path(dev_addr, adapter_addr, buf + 4, sizeof(buf) - 4);
227
modem = ofono_modem_create(buf, "hfp");
231
data = g_try_new0(struct hfp_data, 1);
235
data->handsfree_path = g_strdup(device);
236
if (data->handsfree_path == NULL)
239
data->handsfree_address = g_strdup(dev_addr);
240
if (data->handsfree_address == NULL)
243
ofono_modem_set_data(modem, data);
244
ofono_modem_set_name(modem, alias);
245
ofono_modem_register(modem);
247
g_hash_table_insert(modem_hash, g_strdup(device), modem);
253
g_free(data->handsfree_path);
256
ofono_modem_remove(modem);
261
static gboolean hfp_remove_modem(gpointer key, gpointer value,
264
struct ofono_modem *modem = value;
265
const char *device = key;
266
const char *prefix = user_data;
268
if (prefix && g_str_has_prefix(device, prefix) == FALSE)
271
ofono_modem_remove(modem);
276
static void hfp_hf_remove(const char *prefix)
280
if (modem_hash == NULL)
283
g_hash_table_foreach_remove(modem_hash, hfp_remove_modem,
287
static void hfp_hf_set_alias(const char *device, const char *alias)
289
struct ofono_modem *modem;
291
if (device == NULL || alias == NULL)
294
modem = g_hash_table_lookup(modem_hash, device);
298
ofono_modem_set_name(modem, alias);
301
static int hfp_register_ofono_handsfree(struct ofono_modem *modem)
303
const char *obj_path = ofono_modem_get_path(modem);
304
struct hfp_data *data = ofono_modem_get_data(modem);
307
DBG("Registering oFono Agent to bluetooth daemon");
309
msg = dbus_message_new_method_call(BLUEZ_SERVICE, data->handsfree_path,
310
BLUEZ_GATEWAY_INTERFACE, "RegisterAgent");
314
dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH, &obj_path,
317
g_dbus_send_message(connection, msg);
321
static int hfp_unregister_ofono_handsfree(struct ofono_modem *modem)
323
const char *obj_path = ofono_modem_get_path(modem);
324
struct hfp_data *data = ofono_modem_get_data(modem);
327
DBG("Unregistering oFono Agent from bluetooth daemon");
329
msg = dbus_message_new_method_call(BLUEZ_SERVICE, data->handsfree_path,
330
BLUEZ_GATEWAY_INTERFACE, "UnregisterAgent");
334
dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH, &obj_path,
337
g_dbus_send_message(connection, msg);
341
static int hfp_probe(struct ofono_modem *modem)
343
const char *obj_path = ofono_modem_get_path(modem);
344
struct hfp_data *data = ofono_modem_get_data(modem);
349
g_dbus_register_interface(connection, obj_path, HFP_AGENT_INTERFACE,
350
agent_methods, NULL, NULL, modem, NULL);
352
data->agent_registered = TRUE;
354
if (hfp_register_ofono_handsfree(modem) != 0)
360
static void hfp_remove(struct ofono_modem *modem)
362
struct hfp_data *data = ofono_modem_get_data(modem);
363
const char *obj_path = ofono_modem_get_path(modem);
365
if (data->call != NULL)
366
dbus_pending_call_cancel(data->call);
368
if (g_dbus_unregister_interface(connection, obj_path,
369
HFP_AGENT_INTERFACE))
370
hfp_unregister_ofono_handsfree(modem);
372
g_free(data->handsfree_address);
373
g_free(data->handsfree_path);
376
ofono_modem_set_data(modem, NULL);
379
static void hfp_connect_reply(DBusPendingCall *call, gpointer user_data)
381
struct ofono_modem *modem = user_data;
382
struct hfp_data *data = ofono_modem_get_data(modem);
384
DBusMessage *reply, *msg;
386
reply = dbus_pending_call_steal_reply(call);
388
if (ofono_modem_get_powered(modem))
391
dbus_error_init(&derr);
392
if (!dbus_set_error_from_message(&derr, reply))
395
DBG("Connect reply: %s", derr.message);
397
if (dbus_error_has_name(&derr, DBUS_ERROR_NO_REPLY)) {
398
msg = dbus_message_new_method_call(BLUEZ_SERVICE,
399
data->handsfree_path,
400
BLUEZ_GATEWAY_INTERFACE, "Disconnect");
402
ofono_error("Disconnect failed");
404
g_dbus_send_message(connection, msg);
407
ofono_modem_set_powered(modem, FALSE);
409
dbus_error_free(&derr);
412
dbus_message_unref(reply);
416
/* power up hardware */
417
static int hfp_enable(struct ofono_modem *modem)
419
struct hfp_data *data = ofono_modem_get_data(modem);
424
status = bluetooth_send_with_reply(data->handsfree_path,
425
BLUEZ_GATEWAY_INTERFACE, "Connect",
426
&data->call, hfp_connect_reply,
428
DBUS_TIMEOUT, DBUS_TYPE_INVALID);
436
static void hfp_power_down(DBusPendingCall *call, gpointer user_data)
438
struct ofono_modem *modem = user_data;
439
struct hfp_data *data = ofono_modem_get_data(modem);
443
reply = dbus_pending_call_steal_reply(call);
445
dbus_error_init(&derr);
446
if (dbus_set_error_from_message(&derr, reply)) {
447
DBG("Disconnect reply: %s", derr.message);
448
dbus_error_free(&derr);
452
ofono_modem_set_powered(modem, FALSE);
455
dbus_message_unref(reply);
459
static int hfp_disable(struct ofono_modem *modem)
461
struct hfp_data *data = ofono_modem_get_data(modem);
466
g_at_chat_unref(data->info.chat);
467
data->info.chat = NULL;
469
if (data->agent_registered) {
470
status = bluetooth_send_with_reply(data->handsfree_path,
471
BLUEZ_GATEWAY_INTERFACE, "Disconnect",
472
&data->call, hfp_power_down,
474
DBUS_TIMEOUT, DBUS_TYPE_INVALID);
483
static void hfp_pre_sim(struct ofono_modem *modem)
485
struct hfp_data *data = ofono_modem_get_data(modem);
489
ofono_devinfo_create(modem, 0, "hfpmodem", data->handsfree_address);
490
ofono_voicecall_create(modem, 0, "hfpmodem", &data->info);
491
ofono_netreg_create(modem, 0, "hfpmodem", &data->info);
492
ofono_call_volume_create(modem, 0, "hfpmodem", &data->info);
493
ofono_handsfree_create(modem, 0, "hfpmodem", &data->info);
494
ofono_siri_create(modem, 0, "hfpmodem", &data->info);
497
static void hfp_post_sim(struct ofono_modem *modem)
502
static struct ofono_modem_driver hfp_driver = {
504
.modem_type = OFONO_MODEM_TYPE_HFP,
506
.remove = hfp_remove,
507
.enable = hfp_enable,
508
.disable = hfp_disable,
509
.pre_sim = hfp_pre_sim,
510
.post_sim = hfp_post_sim,
513
static struct bluetooth_profile hfp_hf = {
515
.probe = hfp_hf_probe,
516
.remove = hfp_hf_remove,
517
.set_alias = hfp_hf_set_alias,
520
static int hfp_init(void)
524
if (DBUS_TYPE_UNIX_FD < 0)
527
connection = ofono_dbus_get_connection();
529
err = ofono_modem_driver_register(&hfp_driver);
533
err = bluetooth_register_uuid(HFP_AG_UUID, &hfp_hf);
535
ofono_modem_driver_unregister(&hfp_driver);
539
modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
545
static void hfp_exit(void)
547
bluetooth_unregister_uuid(HFP_AG_UUID);
548
ofono_modem_driver_unregister(&hfp_driver);
550
g_hash_table_destroy(modem_hash);
553
OFONO_PLUGIN_DEFINE(hfp_bluez4, "Hands-Free Profile Plugins", VERSION,
554
OFONO_PLUGIN_PRIORITY_DEFAULT, hfp_init, hfp_exit)