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>
48 #include <drivers/hfpmodem/slc.h>
50 #include "bluetooth.h"
52 #define BLUEZ_GATEWAY_INTERFACE BLUEZ_SERVICE ".HandsfreeGateway"
54 #define HFP_AGENT_INTERFACE "org.bluez.HandsfreeAgent"
55 #define HFP_AGENT_ERROR_INTERFACE "org.bluez.Error"
57 #ifndef DBUS_TYPE_UNIX_FD
58 #define DBUS_TYPE_UNIX_FD -1
61 static DBusConnection *connection;
62 static GHashTable *modem_hash = NULL;
65 struct hfp_slc_info info;
67 char *handsfree_address;
69 gboolean agent_registered;
70 DBusPendingCall *call;
73 static void hfp_debug(const char *str, void *user_data)
75 const char *prefix = user_data;
77 ofono_info("%s%s", prefix, str);
80 static void slc_established(gpointer userdata)
82 struct ofono_modem *modem = userdata;
83 struct hfp_data *data = ofono_modem_get_data(modem);
86 ofono_modem_set_powered(modem, TRUE);
88 msg = dbus_message_new_method_return(data->slc_msg);
89 g_dbus_send_message(connection, msg);
90 dbus_message_unref(data->slc_msg);
93 ofono_info("Service level connection established");
96 static void slc_failed(gpointer userdata)
98 struct ofono_modem *modem = userdata;
99 struct hfp_data *data = ofono_modem_get_data(modem);
102 msg = g_dbus_create_error(data->slc_msg, HFP_AGENT_ERROR_INTERFACE
104 "HFP Handshake failed");
105 g_dbus_send_message(connection, msg);
106 dbus_message_unref(data->slc_msg);
107 data->slc_msg = NULL;
109 ofono_error("Service level connection failed");
110 ofono_modem_set_powered(modem, FALSE);
112 g_at_chat_unref(data->info.chat);
113 data->info.chat = NULL;
116 static void hfp_disconnected_cb(gpointer user_data)
118 struct ofono_modem *modem = user_data;
119 struct hfp_data *data = ofono_modem_get_data(modem);
121 ofono_modem_set_powered(modem, FALSE);
123 g_at_chat_unref(data->info.chat);
124 data->info.chat = NULL;
127 /* either oFono or Phone could request SLC connection */
128 static int service_level_connection(struct ofono_modem *modem, int fd)
130 struct hfp_data *data = ofono_modem_get_data(modem);
135 io = g_io_channel_unix_new(fd);
137 ofono_error("Service level connection failed: %s (%d)",
138 strerror(errno), errno);
142 syntax = g_at_syntax_new_gsm_permissive();
143 chat = g_at_chat_new(io, syntax);
144 g_at_syntax_unref(syntax);
145 g_io_channel_unref(io);
150 g_at_chat_set_disconnect_function(chat, hfp_disconnected_cb, modem);
152 if (getenv("OFONO_AT_DEBUG"))
153 g_at_chat_set_debug(chat, hfp_debug, "");
155 data->info.chat = chat;
156 hfp_slc_establish(&data->info, slc_established, slc_failed, modem);
161 static DBusMessage *hfp_agent_new_connection(DBusConnection *conn,
162 DBusMessage *msg, void *data)
165 struct ofono_modem *modem = data;
166 struct hfp_data *hfp_data = ofono_modem_get_data(modem);
169 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_UNIX_FD, &fd,
170 DBUS_TYPE_UINT16, &version, DBUS_TYPE_INVALID))
171 return __ofono_error_invalid_args(msg);
173 hfp_slc_info_init(&hfp_data->info, version);
175 err = service_level_connection(modem, fd);
176 if (err < 0 && err != -EINPROGRESS)
177 return __ofono_error_failed(msg);
179 hfp_data->slc_msg = msg;
180 dbus_message_ref(msg);
185 static DBusMessage *hfp_agent_release(DBusConnection *conn,
186 DBusMessage *msg, void *data)
188 struct ofono_modem *modem = data;
189 struct hfp_data *hfp_data = ofono_modem_get_data(modem);
190 const char *obj_path = ofono_modem_get_path(modem);
192 g_dbus_unregister_interface(connection, obj_path, HFP_AGENT_INTERFACE);
193 hfp_data->agent_registered = FALSE;
195 g_hash_table_remove(modem_hash, hfp_data->handsfree_path);
196 ofono_modem_remove(modem);
198 return dbus_message_new_method_return(msg);
201 static const GDBusMethodTable agent_methods[] = {
202 { GDBUS_ASYNC_METHOD("NewConnection",
203 GDBUS_ARGS({ "fd", "h" }, { "version", "q" }),
204 NULL, hfp_agent_new_connection) },
205 { GDBUS_METHOD("Release", NULL, NULL, hfp_agent_release) },
209 static int hfp_hf_probe(const char *device, const char *dev_addr,
210 const char *adapter_addr, const char *alias)
212 struct ofono_modem *modem;
213 struct hfp_data *data;
216 /* We already have this device in our hash, ignore */
217 if (g_hash_table_lookup(modem_hash, device) != NULL)
220 ofono_info("Using device: %s, devaddr: %s, adapter: %s",
221 device, dev_addr, adapter_addr);
224 bluetooth_create_path(dev_addr, adapter_addr, buf + 4, sizeof(buf) - 4);
226 modem = ofono_modem_create(buf, "hfp");
230 data = g_try_new0(struct hfp_data, 1);
234 data->handsfree_path = g_strdup(device);
235 if (data->handsfree_path == NULL)
238 data->handsfree_address = g_strdup(dev_addr);
239 if (data->handsfree_address == NULL)
242 ofono_modem_set_data(modem, data);
243 ofono_modem_set_name(modem, alias);
244 ofono_modem_register(modem);
246 g_hash_table_insert(modem_hash, g_strdup(device), modem);
252 g_free(data->handsfree_path);
255 ofono_modem_remove(modem);
260 static gboolean hfp_remove_modem(gpointer key, gpointer value,
263 struct ofono_modem *modem = value;
264 const char *device = key;
265 const char *prefix = user_data;
267 if (prefix && g_str_has_prefix(device, prefix) == FALSE)
270 ofono_modem_remove(modem);
275 static void hfp_hf_remove(const char *prefix)
279 if (modem_hash == NULL)
282 g_hash_table_foreach_remove(modem_hash, hfp_remove_modem,
286 static void hfp_hf_set_alias(const char *device, const char *alias)
288 struct ofono_modem *modem;
290 if (device == NULL || alias == NULL)
293 modem = g_hash_table_lookup(modem_hash, device);
297 ofono_modem_set_name(modem, alias);
300 static int hfp_register_ofono_handsfree(struct ofono_modem *modem)
302 const char *obj_path = ofono_modem_get_path(modem);
303 struct hfp_data *data = ofono_modem_get_data(modem);
306 DBG("Registering oFono Agent to bluetooth daemon");
308 msg = dbus_message_new_method_call(BLUEZ_SERVICE, data->handsfree_path,
309 BLUEZ_GATEWAY_INTERFACE, "RegisterAgent");
313 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH, &obj_path,
316 g_dbus_send_message(connection, msg);
320 static int hfp_unregister_ofono_handsfree(struct ofono_modem *modem)
322 const char *obj_path = ofono_modem_get_path(modem);
323 struct hfp_data *data = ofono_modem_get_data(modem);
326 DBG("Unregistering oFono Agent from bluetooth daemon");
328 msg = dbus_message_new_method_call(BLUEZ_SERVICE, data->handsfree_path,
329 BLUEZ_GATEWAY_INTERFACE, "UnregisterAgent");
333 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH, &obj_path,
336 g_dbus_send_message(connection, msg);
340 static int hfp_probe(struct ofono_modem *modem)
342 const char *obj_path = ofono_modem_get_path(modem);
343 struct hfp_data *data = ofono_modem_get_data(modem);
348 g_dbus_register_interface(connection, obj_path, HFP_AGENT_INTERFACE,
349 agent_methods, NULL, NULL, modem, NULL);
351 data->agent_registered = TRUE;
353 if (hfp_register_ofono_handsfree(modem) != 0)
359 static void hfp_remove(struct ofono_modem *modem)
361 struct hfp_data *data = ofono_modem_get_data(modem);
362 const char *obj_path = ofono_modem_get_path(modem);
364 if (data->call != NULL)
365 dbus_pending_call_cancel(data->call);
367 if (g_dbus_unregister_interface(connection, obj_path,
368 HFP_AGENT_INTERFACE))
369 hfp_unregister_ofono_handsfree(modem);
371 g_free(data->handsfree_address);
372 g_free(data->handsfree_path);
375 ofono_modem_set_data(modem, NULL);
378 static void hfp_connect_reply(DBusPendingCall *call, gpointer user_data)
380 struct ofono_modem *modem = user_data;
381 struct hfp_data *data = ofono_modem_get_data(modem);
383 DBusMessage *reply, *msg;
385 reply = dbus_pending_call_steal_reply(call);
387 if (ofono_modem_get_powered(modem))
390 dbus_error_init(&derr);
391 if (!dbus_set_error_from_message(&derr, reply))
394 DBG("Connect reply: %s", derr.message);
396 if (dbus_error_has_name(&derr, DBUS_ERROR_NO_REPLY)) {
397 msg = dbus_message_new_method_call(BLUEZ_SERVICE,
398 data->handsfree_path,
399 BLUEZ_GATEWAY_INTERFACE, "Disconnect");
401 ofono_error("Disconnect failed");
403 g_dbus_send_message(connection, msg);
406 ofono_modem_set_powered(modem, FALSE);
408 dbus_error_free(&derr);
411 dbus_message_unref(reply);
415 /* power up hardware */
416 static int hfp_enable(struct ofono_modem *modem)
418 struct hfp_data *data = ofono_modem_get_data(modem);
423 status = bluetooth_send_with_reply(data->handsfree_path,
424 BLUEZ_GATEWAY_INTERFACE, "Connect",
425 &data->call, hfp_connect_reply,
427 DBUS_TIMEOUT, DBUS_TYPE_INVALID);
435 static void hfp_power_down(DBusPendingCall *call, gpointer user_data)
437 struct ofono_modem *modem = user_data;
438 struct hfp_data *data = ofono_modem_get_data(modem);
442 reply = dbus_pending_call_steal_reply(call);
444 dbus_error_init(&derr);
445 if (dbus_set_error_from_message(&derr, reply)) {
446 DBG("Disconnect reply: %s", derr.message);
447 dbus_error_free(&derr);
451 ofono_modem_set_powered(modem, FALSE);
454 dbus_message_unref(reply);
458 static int hfp_disable(struct ofono_modem *modem)
460 struct hfp_data *data = ofono_modem_get_data(modem);
465 g_at_chat_unref(data->info.chat);
466 data->info.chat = NULL;
468 if (data->agent_registered) {
469 status = bluetooth_send_with_reply(data->handsfree_path,
470 BLUEZ_GATEWAY_INTERFACE, "Disconnect",
471 &data->call, hfp_power_down,
473 DBUS_TIMEOUT, DBUS_TYPE_INVALID);
482 static void hfp_pre_sim(struct ofono_modem *modem)
484 struct hfp_data *data = ofono_modem_get_data(modem);
488 ofono_devinfo_create(modem, 0, "hfpmodem", data->handsfree_address);
489 ofono_voicecall_create(modem, 0, "hfpmodem", &data->info);
490 ofono_netreg_create(modem, 0, "hfpmodem", &data->info);
491 ofono_call_volume_create(modem, 0, "hfpmodem", &data->info);
492 ofono_handsfree_create(modem, 0, "hfpmodem", &data->info);
495 static void hfp_post_sim(struct ofono_modem *modem)
500 static struct ofono_modem_driver hfp_driver = {
502 .modem_type = OFONO_MODEM_TYPE_HFP,
504 .remove = hfp_remove,
505 .enable = hfp_enable,
506 .disable = hfp_disable,
507 .pre_sim = hfp_pre_sim,
508 .post_sim = hfp_post_sim,
511 static struct bluetooth_profile hfp_hf = {
513 .probe = hfp_hf_probe,
514 .remove = hfp_hf_remove,
515 .set_alias = hfp_hf_set_alias,
518 static int hfp_init(void)
522 if (DBUS_TYPE_UNIX_FD < 0)
525 connection = ofono_dbus_get_connection();
527 err = ofono_modem_driver_register(&hfp_driver);
531 err = bluetooth_register_uuid(HFP_AG_UUID, &hfp_hf);
533 ofono_modem_driver_unregister(&hfp_driver);
537 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
543 static void hfp_exit(void)
545 bluetooth_unregister_uuid(HFP_AG_UUID);
546 ofono_modem_driver_unregister(&hfp_driver);
548 g_hash_table_destroy(modem_hash);
551 OFONO_PLUGIN_DEFINE(hfp, "Hands-Free Profile Plugins", VERSION,
552 OFONO_PLUGIN_PRIORITY_DEFAULT, hfp_init, hfp_exit)