3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2006-2010 Nokia Corporation
6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
41 #include <bluetooth/bluetooth.h>
42 #include <bluetooth/sdp.h>
43 #include <bluetooth/sdp_lib.h>
44 #include <bluetooth/uuid.h>
47 #include <dbus/dbus.h>
50 #include "glib-helper.h"
52 #include "../src/adapter.h"
53 #include "../src/manager.h"
54 #include "../src/device.h"
71 #include "telephony.h"
91 struct audio_adapter {
92 struct btd_adapter *btd_adapter;
94 uint32_t hsp_ag_record_id;
95 uint32_t hfp_ag_record_id;
96 uint32_t hfp_hs_record_id;
97 GIOChannel *hsp_ag_server;
98 GIOChannel *hfp_ag_server;
99 GIOChannel *hfp_hs_server;
103 static gboolean auto_connect = TRUE;
104 static int max_connected_headsets = 1;
105 static DBusConnection *connection = NULL;
106 static GKeyFile *config = NULL;
107 static GSList *adapters = NULL;
108 static GSList *devices = NULL;
110 static struct enabled_interfaces enabled = {
121 static struct audio_adapter *find_adapter(GSList *list,
122 struct btd_adapter *btd_adapter)
124 for (; list; list = list->next) {
125 struct audio_adapter *adapter = list->data;
127 if (adapter->btd_adapter == btd_adapter)
134 gboolean server_is_enabled(bdaddr_t *src, uint16_t svc)
137 case HEADSET_SVCLASS_ID:
138 return enabled.headset;
139 case HEADSET_AGW_SVCLASS_ID:
141 case HANDSFREE_SVCLASS_ID:
142 return enabled.headset && enabled.hfp;
143 case HANDSFREE_AGW_SVCLASS_ID:
144 return enabled.gateway;
145 case AUDIO_SINK_SVCLASS_ID:
147 case AUDIO_SOURCE_SVCLASS_ID:
148 return enabled.source;
149 case AV_REMOTE_TARGET_SVCLASS_ID:
150 case AV_REMOTE_SVCLASS_ID:
151 return enabled.control;
157 static void handle_uuid(const char *uuidstr, struct audio_device *device)
162 if (bt_string2uuid(&uuid, uuidstr) < 0) {
163 error("%s not detected as an UUID-128", uuidstr);
167 if (!sdp_uuid128_to_uuid(&uuid) && uuid.type != SDP_UUID16) {
168 error("Could not convert %s to a UUID-16", uuidstr);
172 uuid16 = uuid.value.uuid16;
174 if (!server_is_enabled(&device->src, uuid16)) {
175 DBG("server not enabled for %s (0x%04x)", uuidstr, uuid16);
180 case HEADSET_SVCLASS_ID:
181 DBG("Found Headset record");
183 headset_update(device, uuid16, uuidstr);
185 device->headset = headset_init(device, uuid16,
188 case HEADSET_AGW_SVCLASS_ID:
189 DBG("Found Headset AG record");
191 case HANDSFREE_SVCLASS_ID:
192 DBG("Found Handsfree record");
194 headset_update(device, uuid16, uuidstr);
196 device->headset = headset_init(device, uuid16,
199 case HANDSFREE_AGW_SVCLASS_ID:
200 DBG("Found Handsfree AG record");
201 if (enabled.gateway && (device->gateway == NULL))
202 device->gateway = gateway_init(device);
204 case AUDIO_SINK_SVCLASS_ID:
205 DBG("Found Audio Sink");
206 if (device->sink == NULL)
207 device->sink = sink_init(device);
209 case AUDIO_SOURCE_SVCLASS_ID:
210 DBG("Found Audio Source");
211 if (device->source == NULL)
212 device->source = source_init(device);
214 case AV_REMOTE_SVCLASS_ID:
215 case AV_REMOTE_TARGET_SVCLASS_ID:
216 DBG("Found AV %s", uuid16 == AV_REMOTE_SVCLASS_ID ?
217 "Remote" : "Target");
219 control_update(device->control, uuid16);
221 device->control = control_init(device, uuid16);
223 if (device->sink && sink_is_active(device))
224 avrcp_connect(device);
227 DBG("Unrecognized UUID: 0x%04X", uuid16);
232 static sdp_record_t *hsp_ag_record(uint8_t ch)
234 sdp_list_t *svclass_id, *pfseq, *apseq, *root;
235 uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
236 uuid_t l2cap_uuid, rfcomm_uuid;
237 sdp_profile_desc_t profile;
238 sdp_record_t *record;
239 sdp_list_t *aproto, *proto[2];
242 record = sdp_record_alloc();
246 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
247 root = sdp_list_append(0, &root_uuid);
248 sdp_set_browse_groups(record, root);
250 sdp_uuid16_create(&svclass_uuid, HEADSET_AGW_SVCLASS_ID);
251 svclass_id = sdp_list_append(0, &svclass_uuid);
252 sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
253 svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
254 sdp_set_service_classes(record, svclass_id);
256 sdp_uuid16_create(&profile.uuid, HEADSET_PROFILE_ID);
257 profile.version = 0x0102;
258 pfseq = sdp_list_append(0, &profile);
259 sdp_set_profile_descs(record, pfseq);
261 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
262 proto[0] = sdp_list_append(0, &l2cap_uuid);
263 apseq = sdp_list_append(0, proto[0]);
265 sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
266 proto[1] = sdp_list_append(0, &rfcomm_uuid);
267 channel = sdp_data_alloc(SDP_UINT8, &ch);
268 proto[1] = sdp_list_append(proto[1], channel);
269 apseq = sdp_list_append(apseq, proto[1]);
271 aproto = sdp_list_append(0, apseq);
272 sdp_set_access_protos(record, aproto);
274 sdp_set_info_attr(record, "Headset Audio Gateway", 0, 0);
276 sdp_data_free(channel);
277 sdp_list_free(proto[0], 0);
278 sdp_list_free(proto[1], 0);
279 sdp_list_free(apseq, 0);
280 sdp_list_free(pfseq, 0);
281 sdp_list_free(aproto, 0);
282 sdp_list_free(root, 0);
283 sdp_list_free(svclass_id, 0);
288 static sdp_record_t *hfp_hs_record(uint8_t ch)
290 sdp_list_t *svclass_id, *pfseq, *apseq, *root;
291 uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
292 uuid_t l2cap_uuid, rfcomm_uuid;
293 sdp_profile_desc_t profile;
294 sdp_record_t *record;
295 sdp_list_t *aproto, *proto[2];
298 record = sdp_record_alloc();
302 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
303 root = sdp_list_append(0, &root_uuid);
304 sdp_set_browse_groups(record, root);
306 sdp_uuid16_create(&svclass_uuid, HANDSFREE_SVCLASS_ID);
307 svclass_id = sdp_list_append(0, &svclass_uuid);
308 sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
309 svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
310 sdp_set_service_classes(record, svclass_id);
312 sdp_uuid16_create(&profile.uuid, HANDSFREE_PROFILE_ID);
313 profile.version = 0x0105;
314 pfseq = sdp_list_append(0, &profile);
315 sdp_set_profile_descs(record, pfseq);
317 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
318 proto[0] = sdp_list_append(0, &l2cap_uuid);
319 apseq = sdp_list_append(0, proto[0]);
321 sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
322 proto[1] = sdp_list_append(0, &rfcomm_uuid);
323 channel = sdp_data_alloc(SDP_UINT8, &ch);
324 proto[1] = sdp_list_append(proto[1], channel);
325 apseq = sdp_list_append(apseq, proto[1]);
327 aproto = sdp_list_append(0, apseq);
328 sdp_set_access_protos(record, aproto);
330 sdp_set_info_attr(record, "Hands-Free", 0, 0);
332 sdp_data_free(channel);
333 sdp_list_free(proto[0], 0);
334 sdp_list_free(proto[1], 0);
335 sdp_list_free(apseq, 0);
336 sdp_list_free(pfseq, 0);
337 sdp_list_free(aproto, 0);
338 sdp_list_free(root, 0);
339 sdp_list_free(svclass_id, 0);
344 static sdp_record_t *hfp_ag_record(uint8_t ch, uint32_t feat)
346 sdp_list_t *svclass_id, *pfseq, *apseq, *root;
347 uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
348 uuid_t l2cap_uuid, rfcomm_uuid;
349 sdp_profile_desc_t profile;
350 sdp_list_t *aproto, *proto[2];
351 sdp_record_t *record;
352 sdp_data_t *channel, *features;
353 uint8_t netid = 0x01;
357 record = sdp_record_alloc();
361 network = sdp_data_alloc(SDP_UINT8, &netid);
363 sdp_record_free(record);
367 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
368 root = sdp_list_append(0, &root_uuid);
369 sdp_set_browse_groups(record, root);
371 sdp_uuid16_create(&svclass_uuid, HANDSFREE_AGW_SVCLASS_ID);
372 svclass_id = sdp_list_append(0, &svclass_uuid);
373 sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
374 svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
375 sdp_set_service_classes(record, svclass_id);
377 sdp_uuid16_create(&profile.uuid, HANDSFREE_PROFILE_ID);
378 profile.version = 0x0105;
379 pfseq = sdp_list_append(0, &profile);
380 sdp_set_profile_descs(record, pfseq);
382 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
383 proto[0] = sdp_list_append(0, &l2cap_uuid);
384 apseq = sdp_list_append(0, proto[0]);
386 sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
387 proto[1] = sdp_list_append(0, &rfcomm_uuid);
388 channel = sdp_data_alloc(SDP_UINT8, &ch);
389 proto[1] = sdp_list_append(proto[1], channel);
390 apseq = sdp_list_append(apseq, proto[1]);
392 sdpfeat = (uint16_t) feat & 0xF;
393 features = sdp_data_alloc(SDP_UINT16, &sdpfeat);
394 sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
396 aproto = sdp_list_append(0, apseq);
397 sdp_set_access_protos(record, aproto);
399 sdp_set_info_attr(record, "Hands-Free Audio Gateway", 0, 0);
401 sdp_attr_add(record, SDP_ATTR_EXTERNAL_NETWORK, network);
403 sdp_data_free(channel);
404 sdp_list_free(proto[0], 0);
405 sdp_list_free(proto[1], 0);
406 sdp_list_free(apseq, 0);
407 sdp_list_free(pfseq, 0);
408 sdp_list_free(aproto, 0);
409 sdp_list_free(root, 0);
410 sdp_list_free(svclass_id, 0);
415 static void headset_auth_cb(DBusError *derr, void *user_data)
417 struct audio_device *device = user_data;
421 if (device->hs_preauth_id) {
422 g_source_remove(device->hs_preauth_id);
423 device->hs_preauth_id = 0;
426 if (derr && dbus_error_is_set(derr)) {
427 error("Access denied: %s", derr->message);
428 headset_set_state(device, HEADSET_STATE_DISCONNECTED);
432 io = headset_get_rfcomm(device);
434 if (!bt_io_accept(io, headset_connect_cb, device, NULL, &err)) {
435 error("bt_io_accept: %s", err->message);
437 headset_set_state(device, HEADSET_STATE_DISCONNECTED);
442 static gboolean hs_preauth_cb(GIOChannel *chan, GIOCondition cond,
445 struct audio_device *device = user_data;
447 DBG("Headset disconnected during authorization");
449 audio_device_cancel_authorization(device, headset_auth_cb, device);
451 headset_set_state(device, HEADSET_STATE_DISCONNECTED);
453 device->hs_preauth_id = 0;
458 static void ag_confirm(GIOChannel *chan, gpointer data)
460 const char *server_uuid, *remote_uuid;
461 struct audio_device *device;
468 bt_io_get(chan, BT_IO_RFCOMM, &err,
469 BT_IO_OPT_SOURCE_BDADDR, &src,
470 BT_IO_OPT_DEST_BDADDR, &dst,
471 BT_IO_OPT_CHANNEL, &ch,
474 error("%s", err->message);
479 if (ch == DEFAULT_HS_AG_CHANNEL) {
481 server_uuid = HSP_AG_UUID;
482 remote_uuid = HSP_HS_UUID;
485 server_uuid = HFP_AG_UUID;
486 remote_uuid = HFP_HS_UUID;
489 device = manager_get_device(&src, &dst, TRUE);
493 if (!manager_allow_headset_connection(device)) {
494 DBG("Refusing headset: too many existing connections");
498 if (!device->headset) {
499 btd_device_add_uuid(device->btd_dev, remote_uuid);
500 if (!device->headset)
504 if (headset_get_state(device) > HEADSET_STATE_DISCONNECTED) {
505 DBG("Refusing new connection since one already exists");
509 headset_set_hfp_active(device, hfp_active);
510 headset_set_rfcomm_initiator(device, TRUE);
512 if (headset_connect_rfcomm(device, chan) < 0) {
513 error("headset_connect_rfcomm failed");
517 headset_set_state(device, HEADSET_STATE_CONNECTING);
519 perr = audio_device_request_authorization(device, server_uuid,
520 headset_auth_cb, device);
522 DBG("Authorization denied: %s", strerror(-perr));
523 headset_set_state(device, HEADSET_STATE_DISCONNECTED);
527 device->hs_preauth_id = g_io_add_watch(chan,
528 G_IO_NVAL | G_IO_HUP | G_IO_ERR,
529 hs_preauth_cb, device);
531 device->auto_connect = auto_connect;
536 g_io_channel_shutdown(chan, TRUE, NULL);
539 static void gateway_auth_cb(DBusError *derr, void *user_data)
541 struct audio_device *device = user_data;
543 if (derr && dbus_error_is_set(derr)) {
544 error("Access denied: %s", derr->message);
545 gateway_set_state(device, GATEWAY_STATE_DISCONNECTED);
549 ba2str(&device->dst, ag_address);
550 DBG("Accepted AG connection from %s for %s",
551 ag_address, device->path);
553 gateway_start_service(device);
557 static void hf_io_cb(GIOChannel *chan, gpointer data)
562 const char *server_uuid, *remote_uuid;
563 struct audio_device *device;
566 bt_io_get(chan, BT_IO_RFCOMM, &err,
567 BT_IO_OPT_SOURCE_BDADDR, &src,
568 BT_IO_OPT_DEST_BDADDR, &dst,
569 BT_IO_OPT_CHANNEL, &ch,
573 error("%s", err->message);
578 server_uuid = HFP_HS_UUID;
579 remote_uuid = HFP_AG_UUID;
581 device = manager_get_device(&src, &dst, TRUE);
585 if (!device->gateway) {
586 btd_device_add_uuid(device->btd_dev, remote_uuid);
587 if (!device->gateway)
591 if (gateway_is_active(device)) {
592 DBG("Refusing new connection since one already exists");
596 if (gateway_connect_rfcomm(device, chan) < 0) {
597 error("Allocating new GIOChannel failed!");
601 perr = audio_device_request_authorization(device, server_uuid,
602 gateway_auth_cb, device);
604 DBG("Authorization denied: %s", strerror(-perr));
605 gateway_set_state(device, GATEWAY_STATE_DISCONNECTED);
611 g_io_channel_shutdown(chan, TRUE, NULL);
614 static int headset_server_init(struct audio_adapter *adapter)
616 uint8_t chan = DEFAULT_HS_AG_CHANNEL;
617 sdp_record_t *record;
618 gboolean master = TRUE;
627 tmp = g_key_file_get_boolean(config, "General", "Master",
630 DBG("audio.conf: %s", err->message);
636 adapter_get_address(adapter->btd_adapter, &src);
638 io = bt_io_listen(BT_IO_RFCOMM, NULL, ag_confirm, adapter, NULL, &err,
639 BT_IO_OPT_SOURCE_BDADDR, &src,
640 BT_IO_OPT_CHANNEL, chan,
641 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
642 BT_IO_OPT_MASTER, master,
647 adapter->hsp_ag_server = io;
649 record = hsp_ag_record(chan);
651 error("Unable to allocate new service record");
655 if (add_record_to_server(&src, record) < 0) {
656 error("Unable to register HS AG service record");
657 sdp_record_free(record);
660 adapter->hsp_ag_record_id = record->handle;
662 features = headset_config_init(config);
667 chan = DEFAULT_HF_AG_CHANNEL;
669 io = bt_io_listen(BT_IO_RFCOMM, NULL, ag_confirm, adapter, NULL, &err,
670 BT_IO_OPT_SOURCE_BDADDR, &src,
671 BT_IO_OPT_CHANNEL, chan,
672 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
673 BT_IO_OPT_MASTER, master,
678 adapter->hfp_ag_server = io;
680 record = hfp_ag_record(chan, features);
682 error("Unable to allocate new service record");
686 if (add_record_to_server(&src, record) < 0) {
687 error("Unable to register HF AG service record");
688 sdp_record_free(record);
691 adapter->hfp_ag_record_id = record->handle;
697 error("%s", err->message);
701 if (adapter->hsp_ag_server) {
702 g_io_channel_shutdown(adapter->hsp_ag_server, TRUE, NULL);
703 g_io_channel_unref(adapter->hsp_ag_server);
704 adapter->hsp_ag_server = NULL;
707 if (adapter->hfp_ag_server) {
708 g_io_channel_shutdown(adapter->hfp_ag_server, TRUE, NULL);
709 g_io_channel_unref(adapter->hfp_ag_server);
710 adapter->hfp_ag_server = NULL;
716 static int gateway_server_init(struct audio_adapter *adapter)
718 uint8_t chan = DEFAULT_HFP_HS_CHANNEL;
719 sdp_record_t *record;
720 gboolean master = TRUE;
728 tmp = g_key_file_get_boolean(config, "General", "Master",
731 DBG("audio.conf: %s", err->message);
737 adapter_get_address(adapter->btd_adapter, &src);
739 io = bt_io_listen(BT_IO_RFCOMM, NULL, hf_io_cb, adapter, NULL, &err,
740 BT_IO_OPT_SOURCE_BDADDR, &src,
741 BT_IO_OPT_CHANNEL, chan,
742 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
743 BT_IO_OPT_MASTER, master,
746 error("%s", err->message);
751 adapter->hfp_hs_server = io;
752 record = hfp_hs_record(chan);
754 error("Unable to allocate new service record");
758 if (add_record_to_server(&src, record) < 0) {
759 error("Unable to register HFP HS service record");
760 sdp_record_free(record);
764 adapter->hfp_hs_record_id = record->handle;
769 g_io_channel_shutdown(adapter->hfp_hs_server, TRUE, NULL);
770 g_io_channel_unref(adapter->hfp_hs_server);
771 adapter->hfp_hs_server = NULL;
775 static int audio_probe(struct btd_device *device, GSList *uuids)
777 struct btd_adapter *adapter = device_get_adapter(device);
779 struct audio_device *audio_dev;
781 adapter_get_address(adapter, &src);
782 device_get_address(device, &dst, NULL);
784 audio_dev = manager_get_device(&src, &dst, TRUE);
786 DBG("unable to get a device object");
790 g_slist_foreach(uuids, (GFunc) handle_uuid, audio_dev);
795 static void audio_remove(struct btd_device *device)
797 struct audio_device *dev;
800 path = device_get_path(device);
802 dev = manager_find_device(path, NULL, NULL, NULL, FALSE);
806 devices = g_slist_remove(devices, dev);
808 audio_device_unregister(dev);
812 static struct audio_adapter *audio_adapter_ref(struct audio_adapter *adp)
816 DBG("%p: ref=%d", adp, adp->ref);
821 static void audio_adapter_unref(struct audio_adapter *adp)
825 DBG("%p: ref=%d", adp, adp->ref);
830 adapters = g_slist_remove(adapters, adp);
831 btd_adapter_unref(adp->btd_adapter);
835 static struct audio_adapter *audio_adapter_create(struct btd_adapter *adapter)
837 struct audio_adapter *adp;
839 adp = g_new0(struct audio_adapter, 1);
840 adp->btd_adapter = btd_adapter_ref(adapter);
842 return audio_adapter_ref(adp);
845 static struct audio_adapter *audio_adapter_get(struct btd_adapter *adapter)
847 struct audio_adapter *adp;
849 adp = find_adapter(adapters, adapter);
851 adp = audio_adapter_create(adapter);
852 adapters = g_slist_append(adapters, adp);
854 audio_adapter_ref(adp);
859 static void state_changed(struct btd_adapter *adapter, gboolean powered)
861 struct audio_adapter *adp;
862 #ifndef __TIZEN_PATCH__
863 static gboolean telephony = FALSE;
867 DBG("%s powered %s", adapter_get_path(adapter),
868 powered ? "on" : "off");
870 /* ignore powered change, adapter is powering down */
871 if (powered && adapter_powering_down(adapter))
874 adp = find_adapter(adapters, adapter);
878 adp->powered = powered;
880 #ifndef __TIZEN_PATCH__
882 /* telephony driver already initialized*/
883 if (telephony == TRUE)
891 /* telephony not initialized just ignore power down */
892 if (telephony == FALSE)
896 for (l = adapters; l; l = l->next) {
899 if (adp->powered == TRUE)
903 #ifndef __TIZEN_PATCH__
909 static int headset_server_probe(struct btd_adapter *adapter)
911 struct audio_adapter *adp;
912 const gchar *path = adapter_get_path(adapter);
915 DBG("path %s", path);
917 adp = audio_adapter_get(adapter);
921 #ifdef __TIZEN_PATCH__
922 /*telephony driver initialization*/
924 DBG("telephony_init failed\n");
927 err = headset_server_init(adp);
929 audio_adapter_unref(adp);
933 btd_adapter_register_powered_callback(adapter, state_changed);
938 static void headset_server_remove(struct btd_adapter *adapter)
940 struct audio_adapter *adp;
941 const gchar *path = adapter_get_path(adapter);
943 DBG("path %s", path);
945 #ifdef __TIZEN_PATCH__
949 btd_adapter_unregister_powered_callback(adapter, state_changed);
951 adp = find_adapter(adapters, adapter);
955 if (adp->hsp_ag_record_id) {
956 remove_record_from_server(adp->hsp_ag_record_id);
957 adp->hsp_ag_record_id = 0;
960 if (adp->hsp_ag_server) {
961 g_io_channel_shutdown(adp->hsp_ag_server, TRUE, NULL);
962 g_io_channel_unref(adp->hsp_ag_server);
963 adp->hsp_ag_server = NULL;
966 if (adp->hfp_ag_record_id) {
967 remove_record_from_server(adp->hfp_ag_record_id);
968 adp->hfp_ag_record_id = 0;
971 if (adp->hfp_ag_server) {
972 g_io_channel_shutdown(adp->hfp_ag_server, TRUE, NULL);
973 g_io_channel_unref(adp->hfp_ag_server);
974 adp->hfp_ag_server = NULL;
977 audio_adapter_unref(adp);
980 static int gateway_server_probe(struct btd_adapter *adapter)
982 struct audio_adapter *adp;
985 adp = audio_adapter_get(adapter);
989 err = gateway_server_init(adp);
991 audio_adapter_unref(adp);
996 static void gateway_server_remove(struct btd_adapter *adapter)
998 struct audio_adapter *adp;
999 const gchar *path = adapter_get_path(adapter);
1001 DBG("path %s", path);
1003 adp = find_adapter(adapters, adapter);
1007 if (adp->hfp_hs_record_id) {
1008 remove_record_from_server(adp->hfp_hs_record_id);
1009 adp->hfp_hs_record_id = 0;
1012 if (adp->hfp_hs_server) {
1013 g_io_channel_shutdown(adp->hfp_hs_server, TRUE, NULL);
1014 g_io_channel_unref(adp->hfp_hs_server);
1015 adp->hfp_hs_server = NULL;
1018 audio_adapter_unref(adp);
1021 static int a2dp_server_probe(struct btd_adapter *adapter)
1023 struct audio_adapter *adp;
1024 const gchar *path = adapter_get_path(adapter);
1028 DBG("path %s", path);
1030 adp = audio_adapter_get(adapter);
1034 adapter_get_address(adapter, &src);
1036 err = a2dp_register(connection, &src, config);
1038 audio_adapter_unref(adp);
1043 static void a2dp_server_remove(struct btd_adapter *adapter)
1045 struct audio_adapter *adp;
1046 const gchar *path = adapter_get_path(adapter);
1049 DBG("path %s", path);
1051 adp = find_adapter(adapters, adapter);
1055 adapter_get_address(adapter, &src);
1056 a2dp_unregister(&src);
1057 audio_adapter_unref(adp);
1060 static int avrcp_server_probe(struct btd_adapter *adapter)
1062 struct audio_adapter *adp;
1063 const gchar *path = adapter_get_path(adapter);
1067 DBG("path %s", path);
1069 adp = audio_adapter_get(adapter);
1073 adapter_get_address(adapter, &src);
1075 err = avrcp_register(connection, &src, config);
1077 audio_adapter_unref(adp);
1082 static void avrcp_server_remove(struct btd_adapter *adapter)
1084 struct audio_adapter *adp;
1085 const gchar *path = adapter_get_path(adapter);
1088 DBG("path %s", path);
1090 adp = find_adapter(adapters, adapter);
1094 adapter_get_address(adapter, &src);
1095 avrcp_unregister(&src);
1096 audio_adapter_unref(adp);
1099 static int media_server_probe(struct btd_adapter *adapter)
1101 struct audio_adapter *adp;
1102 const gchar *path = adapter_get_path(adapter);
1106 DBG("path %s", path);
1108 adp = audio_adapter_get(adapter);
1112 adapter_get_address(adapter, &src);
1114 err = media_register(connection, path, &src);
1116 audio_adapter_unref(adp);
1121 static void media_server_remove(struct btd_adapter *adapter)
1123 struct audio_adapter *adp;
1124 const gchar *path = adapter_get_path(adapter);
1126 DBG("path %s", path);
1128 adp = find_adapter(adapters, adapter);
1132 media_unregister(path);
1133 audio_adapter_unref(adp);
1136 static struct btd_device_driver audio_driver = {
1138 .uuids = BTD_UUIDS(HSP_HS_UUID, HFP_HS_UUID, HSP_AG_UUID, HFP_AG_UUID,
1139 ADVANCED_AUDIO_UUID, A2DP_SOURCE_UUID, A2DP_SINK_UUID,
1140 AVRCP_TARGET_UUID, AVRCP_REMOTE_UUID),
1141 .probe = audio_probe,
1142 .remove = audio_remove,
1145 static struct btd_adapter_driver headset_server_driver = {
1146 .name = "audio-headset",
1147 .probe = headset_server_probe,
1148 .remove = headset_server_remove,
1151 static struct btd_adapter_driver gateway_server_driver = {
1152 .name = "audio-gateway",
1153 .probe = gateway_server_probe,
1154 .remove = gateway_server_remove,
1157 static struct btd_adapter_driver a2dp_server_driver = {
1158 .name = "audio-a2dp",
1159 .probe = a2dp_server_probe,
1160 .remove = a2dp_server_remove,
1163 static struct btd_adapter_driver avrcp_server_driver = {
1164 .name = "audio-control",
1165 .probe = avrcp_server_probe,
1166 .remove = avrcp_server_remove,
1169 static struct btd_adapter_driver media_server_driver = {
1171 .probe = media_server_probe,
1172 .remove = media_server_remove,
1175 int audio_manager_init(DBusConnection *conn, GKeyFile *conf,
1176 gboolean *enable_sco)
1183 connection = dbus_connection_ref(conn);
1190 list = g_key_file_get_string_list(config, "General", "Enable",
1192 for (i = 0; list && list[i] != NULL; i++) {
1193 if (g_str_equal(list[i], "Headset"))
1194 enabled.headset = TRUE;
1195 else if (g_str_equal(list[i], "Gateway"))
1196 enabled.gateway = TRUE;
1197 else if (g_str_equal(list[i], "Sink"))
1198 enabled.sink = TRUE;
1199 else if (g_str_equal(list[i], "Source"))
1200 enabled.source = TRUE;
1201 else if (g_str_equal(list[i], "Control"))
1202 enabled.control = TRUE;
1203 else if (g_str_equal(list[i], "Socket"))
1204 enabled.socket = TRUE;
1205 else if (g_str_equal(list[i], "Media"))
1206 enabled.media = TRUE;
1211 list = g_key_file_get_string_list(config, "General", "Disable",
1213 for (i = 0; list && list[i] != NULL; i++) {
1214 if (g_str_equal(list[i], "Headset"))
1215 enabled.headset = FALSE;
1216 else if (g_str_equal(list[i], "Gateway"))
1217 enabled.gateway = FALSE;
1218 else if (g_str_equal(list[i], "Sink"))
1219 enabled.sink = FALSE;
1220 else if (g_str_equal(list[i], "Source"))
1221 enabled.source = FALSE;
1222 else if (g_str_equal(list[i], "Control"))
1223 enabled.control = FALSE;
1224 else if (g_str_equal(list[i], "Socket"))
1225 enabled.socket = FALSE;
1226 else if (g_str_equal(list[i], "Media"))
1227 enabled.media = FALSE;
1231 b = g_key_file_get_boolean(config, "General", "AutoConnect", &err);
1233 DBG("audio.conf: %s", err->message);
1234 g_clear_error(&err);
1238 b = g_key_file_get_boolean(config, "Headset", "HFP",
1241 g_clear_error(&err);
1246 i = g_key_file_get_integer(config, "Headset", "MaxConnected",
1249 DBG("audio.conf: %s", err->message);
1250 g_clear_error(&err);
1252 max_connected_headsets = i;
1259 btd_register_adapter_driver(&media_server_driver);
1261 if (enabled.headset)
1262 btd_register_adapter_driver(&headset_server_driver);
1264 if (enabled.gateway)
1265 btd_register_adapter_driver(&gateway_server_driver);
1267 if (enabled.source || enabled.sink)
1268 btd_register_adapter_driver(&a2dp_server_driver);
1270 if (enabled.control)
1271 btd_register_adapter_driver(&avrcp_server_driver);
1273 btd_register_device_driver(&audio_driver);
1275 *enable_sco = (enabled.gateway || enabled.headset);
1280 void audio_manager_exit(void)
1282 /* Bail out early if we haven't been initialized */
1283 if (connection == NULL)
1286 dbus_connection_unref(connection);
1290 g_key_file_free(config);
1298 btd_unregister_adapter_driver(&media_server_driver);
1300 if (enabled.headset)
1301 btd_unregister_adapter_driver(&headset_server_driver);
1303 if (enabled.gateway)
1304 btd_unregister_adapter_driver(&gateway_server_driver);
1306 if (enabled.source || enabled.sink)
1307 btd_unregister_adapter_driver(&a2dp_server_driver);
1309 if (enabled.control)
1310 btd_unregister_adapter_driver(&avrcp_server_driver);
1312 btd_unregister_device_driver(&audio_driver);
1315 GSList *manager_find_devices(const char *path,
1316 const bdaddr_t *src,
1317 const bdaddr_t *dst,
1318 const char *interface,
1321 GSList *result = NULL;
1324 for (l = devices; l != NULL; l = l->next) {
1325 struct audio_device *dev = l->data;
1327 if ((path && (strcmp(path, "")) && strcmp(dev->path, path)))
1330 if ((src && bacmp(src, BDADDR_ANY)) && bacmp(&dev->src, src))
1333 if ((dst && bacmp(dst, BDADDR_ANY)) && bacmp(&dev->dst, dst))
1336 if (interface && !strcmp(AUDIO_HEADSET_INTERFACE, interface)
1340 if (interface && !strcmp(AUDIO_GATEWAY_INTERFACE, interface)
1344 if (interface && !strcmp(AUDIO_SINK_INTERFACE, interface)
1348 if (interface && !strcmp(AUDIO_SOURCE_INTERFACE, interface)
1352 if (interface && !strcmp(AUDIO_CONTROL_INTERFACE, interface)
1356 if (connected && !audio_device_is_active(dev, interface))
1359 result = g_slist_append(result, dev);
1365 struct audio_device *manager_find_device(const char *path,
1366 const bdaddr_t *src,
1367 const bdaddr_t *dst,
1368 const char *interface,
1371 struct audio_device *result;
1374 l = manager_find_devices(path, src, dst, interface, connected);
1383 struct audio_device *manager_get_device(const bdaddr_t *src,
1384 const bdaddr_t *dst,
1387 struct audio_device *dev;
1388 struct btd_adapter *adapter;
1389 struct btd_device *device;
1393 dev = manager_find_device(NULL, src, dst, NULL, FALSE);
1402 adapter = manager_find_adapter(src);
1404 error("Unable to get a btd_adapter object for %s",
1411 device = adapter_get_device(connection, adapter, addr);
1413 error("Unable to get btd_device object for %s", addr);
1417 path = device_get_path(device);
1419 dev = audio_device_register(connection, device, path, src, dst);
1423 devices = g_slist_append(devices, dev);
1428 gboolean manager_allow_headset_connection(struct audio_device *device)
1433 for (l = devices; l != NULL; l = l->next) {
1434 struct audio_device *dev = l->data;
1435 struct headset *hs = dev->headset;
1440 if (device && bacmp(&dev->src, &device->src) != 0)
1446 if (headset_get_state(dev) > HEADSET_STATE_DISCONNECTED)
1449 if (connected >= max_connected_headsets)
1456 void manager_set_fast_connectable(gboolean enable)
1460 if (enable && !manager_allow_headset_connection(NULL)) {
1461 DBG("Refusing enabling fast connectable");
1465 for (l = adapters; l != NULL; l = l->next) {
1466 struct audio_adapter *adapter = l->data;
1468 if (btd_adapter_set_fast_connectable(adapter->btd_adapter,
1470 error("Changing fast connectable for hci%d failed",
1471 adapter_get_dev_id(adapter->btd_adapter));