1 /* 2 * 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2006-2010 Nokia Corporation 6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel (at) holtmann.org> 7 * 8 * 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. 13 * 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. 18 * 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 22 * 23 */ 24 25 #ifdef HAVE_CONFIG_H 26 #include <config.h> 27 #endif 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <stdarg.h> 32 #include <errno.h> 33 #include <unistd.h> 34 #include <fcntl.h> 35 #include <stdint.h> 36 #include <sys/stat.h> 37 #include <dirent.h> 38 #include <ctype.h> 39 #include <signal.h> 40 41 #include <bluetooth/bluetooth.h> 42 #include <bluetooth/hci.h> 43 #include <bluetooth/hci_lib.h> 44 #include <bluetooth/rfcomm.h> 45 #include <bluetooth/sdp.h> 46 #include <bluetooth/sdp_lib.h> 47 48 #include <glib.h> 49 #include <dbus/dbus.h> 50 #include <gdbus.h> 51 52 #include "glib-helper.h" 53 #include "btio.h" 54 #include "../src/manager.h" 55 #include "../src/adapter.h" 56 #include "../src/device.h" 57 58 #include "log.h" 59 #include "textfile.h" 60 #include "ipc.h" 61 #include "device.h" 62 #include "error.h" 63 #include "avdtp.h" 64 #include "a2dp.h" 65 #include "headset.h" 66 #include "gateway.h" 67 #include "sink.h" 68 #include "source.h" 69 #include "control.h" 70 #include "manager.h" 71 #include "sdpd.h" 72 #include "telephony.h" 73 74 typedef enum { 75 HEADSET = 1 << 0, 76 GATEWAY = 1 << 1, 77 SINK = 1 << 2, 78 SOURCE = 1 << 3, 79 CONTROL = 1 << 4, 80 TARGET = 1 << 5, 81 INVALID = 1 << 6 82 } audio_service_type; 83 84 typedef enum { 85 GENERIC_AUDIO = 0, 86 ADVANCED_AUDIO, 87 AV_REMOTE, 88 GET_RECORDS 89 } audio_sdp_state_t; 90 91 struct audio_adapter { 92 struct btd_adapter *btd_adapter; 93 uint32_t hsp_ag_record_id; 94 uint32_t hfp_ag_record_id; 95 uint32_t hfp_hs_record_id; 96 GIOChannel *hsp_ag_server; 97 GIOChannel *hfp_ag_server; 98 GIOChannel *hfp_hs_server; 99 gint ref; 100 }; 101 102 static gboolean auto_connect = TRUE; 103 static int max_connected_headsets = 1; 104 static DBusConnection *connection = NULL; 105 static GKeyFile *config = NULL; 106 static GSList *adapters = NULL; 107 static GSList *devices = NULL; 108 109 static struct enabled_interfaces enabled = { 110 .hfp = TRUE, 111 .headset = TRUE, 112 .gateway = FALSE, 113 .sink = TRUE, 114 .source = FALSE, 115 .control = TRUE, 116 }; 117 118 static struct audio_adapter *find_adapter(GSList *list, 119 struct btd_adapter *btd_adapter) 120 { 121 GSList *l; 122 123 for (l = list; l; l = l->next) { 124 struct audio_adapter *adapter = l->data; 125 126 if (adapter->btd_adapter == btd_adapter) 127 return adapter; 128 } 129 130 return NULL; 131 } 132 133 gboolean server_is_enabled(bdaddr_t *src, uint16_t svc) 134 { 135 switch (svc) { 136 case HEADSET_SVCLASS_ID: 137 return enabled.headset; 138 case HEADSET_AGW_SVCLASS_ID: 139 return FALSE; 140 case HANDSFREE_SVCLASS_ID: 141 return enabled.headset && enabled.hfp; 142 case HANDSFREE_AGW_SVCLASS_ID: 143 return enabled.gateway; 144 case AUDIO_SINK_SVCLASS_ID: 145 return enabled.sink; 146 case AUDIO_SOURCE_SVCLASS_ID: 147 return enabled.source; 148 case AV_REMOTE_TARGET_SVCLASS_ID: 149 case AV_REMOTE_SVCLASS_ID: 150 return enabled.control; 151 default: 152 return FALSE; 153 } 154 } 155 156 static void handle_uuid(const char *uuidstr, struct audio_device *device) 157 { 158 uuid_t uuid; 159 uint16_t uuid16; 160 161 if (bt_string2uuid(&uuid, uuidstr) < 0) { 162 error("%s not detected as an UUID-128", uuidstr); 163 return; 164 } 165 166 if (!sdp_uuid128_to_uuid(&uuid) && uuid.type != SDP_UUID16) { 167 error("Could not convert %s to a UUID-16", uuidstr); 168 return; 169 } 170 171 uuid16 = uuid.value.uuid16; 172 173 if (!server_is_enabled(&device->src, uuid16)) { 174 DBG("server not enabled for %s (0x%04x)", uuidstr, uuid16); 175 return; 176 } 177 178 switch (uuid16) { 179 case HEADSET_SVCLASS_ID: 180 DBG("Found Headset record"); 181 if (device->headset) 182 headset_update(device, uuid16, uuidstr); 183 else 184 device->headset = headset_init(device, uuid16, 185 uuidstr); 186 break; 187 case HEADSET_AGW_SVCLASS_ID: 188 DBG("Found Headset AG record"); 189 break; 190 case HANDSFREE_SVCLASS_ID: 191 DBG("Found Handsfree record"); 192 if (device->headset) 193 headset_update(device, uuid16, uuidstr); 194 else 195 device->headset = headset_init(device, uuid16, 196 uuidstr); 197 break; 198 case HANDSFREE_AGW_SVCLASS_ID: 199 DBG("Found Handsfree AG record"); 200 if (enabled.gateway && (device->gateway == NULL)) 201 device->gateway = gateway_init(device); 202 break; 203 case AUDIO_SINK_SVCLASS_ID: 204 DBG("Found Audio Sink"); 205 if (device->sink == NULL) 206 device->sink = sink_init(device); 207 break; 208 case AUDIO_SOURCE_SVCLASS_ID: 209 DBG("Found Audio Source"); 210 if (device->source == NULL) 211 device->source = source_init(device); 212 break; 213 case AV_REMOTE_SVCLASS_ID: 214 case AV_REMOTE_TARGET_SVCLASS_ID: 215 DBG("Found AV %s", uuid16 == AV_REMOTE_SVCLASS_ID ? 216 "Remote" : "Target"); 217 if (device->control) 218 control_update(device, uuid16); 219 else 220 device->control = control_init(device, uuid16); 221 if (device->sink && sink_is_active(device)) 222 avrcp_connect(device); 223 break; 224 default: 225 DBG("Unrecognized UUID: 0x%04X", uuid16); 226 break; 227 } 228 } 229 230 static sdp_record_t *hsp_ag_record(uint8_t ch) 231 { 232 sdp_list_t *svclass_id, *pfseq, *apseq, *root; 233 uuid_t root_uuid, svclass_uuid, ga_svclass_uuid; 234 uuid_t l2cap_uuid, rfcomm_uuid; 235 sdp_profile_desc_t profile; 236 sdp_record_t *record; 237 sdp_list_t *aproto, *proto[2]; 238 sdp_data_t *channel; 239 240 record = sdp_record_alloc(); 241 if (!record) 242 return NULL; 243 244 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP); 245 root = sdp_list_append(0, &root_uuid); 246 sdp_set_browse_groups(record, root); 247 248 sdp_uuid16_create(&svclass_uuid, HEADSET_AGW_SVCLASS_ID); 249 svclass_id = sdp_list_append(0, &svclass_uuid); 250 sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID); 251 svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid); 252 sdp_set_service_classes(record, svclass_id); 253 254 sdp_uuid16_create(&profile.uuid, HEADSET_PROFILE_ID); 255 profile.version = 0x0102; 256 pfseq = sdp_list_append(0, &profile); 257 sdp_set_profile_descs(record, pfseq); 258 259 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID); 260 proto[0] = sdp_list_append(0, &l2cap_uuid); 261 apseq = sdp_list_append(0, proto[0]); 262 263 sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID); 264 proto[1] = sdp_list_append(0, &rfcomm_uuid); 265 channel = sdp_data_alloc(SDP_UINT8, &ch); 266 proto[1] = sdp_list_append(proto[1], channel); 267 apseq = sdp_list_append(apseq, proto[1]); 268 269 aproto = sdp_list_append(0, apseq); 270 sdp_set_access_protos(record, aproto); 271 272 sdp_set_info_attr(record, "Headset Audio Gateway", 0, 0); 273 274 sdp_data_free(channel); 275 sdp_list_free(proto[0], 0); 276 sdp_list_free(proto[1], 0); 277 sdp_list_free(apseq, 0); 278 sdp_list_free(pfseq, 0); 279 sdp_list_free(aproto, 0); 280 sdp_list_free(root, 0); 281 sdp_list_free(svclass_id, 0); 282 283 return record; 284 } 285 286 static sdp_record_t *hfp_hs_record(uint8_t ch) 287 { 288 sdp_list_t *svclass_id, *pfseq, *apseq, *root; 289 uuid_t root_uuid, svclass_uuid, ga_svclass_uuid; 290 uuid_t l2cap_uuid, rfcomm_uuid; 291 sdp_profile_desc_t profile; 292 sdp_record_t *record; 293 sdp_list_t *aproto, *proto[2]; 294 sdp_data_t *channel; 295 296 record = sdp_record_alloc(); 297 if (!record) 298 return NULL; 299 300 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP); 301 root = sdp_list_append(0, &root_uuid); 302 sdp_set_browse_groups(record, root); 303 304 sdp_uuid16_create(&svclass_uuid, HANDSFREE_SVCLASS_ID); 305 svclass_id = sdp_list_append(0, &svclass_uuid); 306 sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID); 307 svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid); 308 sdp_set_service_classes(record, svclass_id); 309 310 sdp_uuid16_create(&profile.uuid, HANDSFREE_PROFILE_ID); 311 profile.version = 0x0105; 312 pfseq = sdp_list_append(0, &profile); 313 sdp_set_profile_descs(record, pfseq); 314 315 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID); 316 proto[0] = sdp_list_append(0, &l2cap_uuid); 317 apseq = sdp_list_append(0, proto[0]); 318 319 sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID); 320 proto[1] = sdp_list_append(0, &rfcomm_uuid); 321 channel = sdp_data_alloc(SDP_UINT8, &ch); 322 proto[1] = sdp_list_append(proto[1], channel); 323 apseq = sdp_list_append(apseq, proto[1]); 324 325 aproto = sdp_list_append(0, apseq); 326 sdp_set_access_protos(record, aproto); 327 328 sdp_set_info_attr(record, "Hands-Free", 0, 0); 329 330 sdp_data_free(channel); 331 sdp_list_free(proto[0], 0); 332 sdp_list_free(proto[1], 0); 333 sdp_list_free(apseq, 0); 334 sdp_list_free(pfseq, 0); 335 sdp_list_free(aproto, 0); 336 sdp_list_free(root, 0); 337 sdp_list_free(svclass_id, 0); 338 339 return record; 340 } 341 342 static sdp_record_t *hfp_ag_record(uint8_t ch, uint32_t feat) 343 { 344 sdp_list_t *svclass_id, *pfseq, *apseq, *root; 345 uuid_t root_uuid, svclass_uuid, ga_svclass_uuid; 346 uuid_t l2cap_uuid, rfcomm_uuid; 347 sdp_profile_desc_t profile; 348 sdp_list_t *aproto, *proto[2]; 349 sdp_record_t *record; 350 sdp_data_t *channel, *features; 351 uint8_t netid = 0x01; 352 uint16_t sdpfeat; 353 sdp_data_t *network; 354 355 record = sdp_record_alloc(); 356 if (!record) 357 return NULL; 358 359 network = sdp_data_alloc(SDP_UINT8, &netid); 360 if (!network) { 361 sdp_record_free(record); 362 return NULL; 363 } 364 365 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP); 366 root = sdp_list_append(0, &root_uuid); 367 sdp_set_browse_groups(record, root); 368 369 sdp_uuid16_create(&svclass_uuid, HANDSFREE_AGW_SVCLASS_ID); 370 svclass_id = sdp_list_append(0, &svclass_uuid); 371 sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID); 372 svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid); 373 sdp_set_service_classes(record, svclass_id); 374 375 sdp_uuid16_create(&profile.uuid, HANDSFREE_PROFILE_ID); 376 profile.version = 0x0105; 377 pfseq = sdp_list_append(0, &profile); 378 sdp_set_profile_descs(record, pfseq); 379 380 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID); 381 proto[0] = sdp_list_append(0, &l2cap_uuid); 382 apseq = sdp_list_append(0, proto[0]); 383 384 sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID); 385 proto[1] = sdp_list_append(0, &rfcomm_uuid); 386 channel = sdp_data_alloc(SDP_UINT8, &ch); 387 proto[1] = sdp_list_append(proto[1], channel); 388 apseq = sdp_list_append(apseq, proto[1]); 389 390 sdpfeat = (uint16_t) feat & 0xF; 391 features = sdp_data_alloc(SDP_UINT16, &sdpfeat); 392 sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features); 393 394 aproto = sdp_list_append(0, apseq); 395 sdp_set_access_protos(record, aproto); 396 397 sdp_set_info_attr(record, "Hands-Free Audio Gateway", 0, 0); 398 399 sdp_attr_add(record, SDP_ATTR_EXTERNAL_NETWORK, network); 400 401 sdp_data_free(channel); 402 sdp_list_free(proto[0], 0); 403 sdp_list_free(proto[1], 0); 404 sdp_list_free(apseq, 0); 405 sdp_list_free(pfseq, 0); 406 sdp_list_free(aproto, 0); 407 sdp_list_free(root, 0); 408 sdp_list_free(svclass_id, 0); 409 410 return record; 411 } 412 413 static void headset_auth_cb(DBusError *derr, void *user_data) 414 { 415 struct audio_device *device = user_data; 416 GError *err = NULL; 417 GIOChannel *io; 418 419 if (device->hs_preauth_id) { 420 g_source_remove(device->hs_preauth_id); 421 device->hs_preauth_id = 0; 422 } 423 424 if (derr && dbus_error_is_set(derr)) { 425 error("Access denied: %s", derr->message); 426 headset_set_state(device, HEADSET_STATE_DISCONNECTED); 427 return; 428 } 429 430 io = headset_get_rfcomm(device); 431 432 if (!bt_io_accept(io, headset_connect_cb, device, NULL, &err)) { 433 error("bt_io_accept: %s", err->message); 434 g_error_free(err); 435 headset_set_state(device, HEADSET_STATE_DISCONNECTED); 436 return; 437 } 438 } 439 440 static gboolean hs_preauth_cb(GIOChannel *chan, GIOCondition cond, 441 gpointer user_data) 442 { 443 struct audio_device *device = user_data; 444 445 DBG("Headset disconnected during authorization"); 446 447 audio_device_cancel_authorization(device, headset_auth_cb, device); 448 449 headset_set_state(device, HEADSET_STATE_DISCONNECTED); 450 451 device->hs_preauth_id = 0; 452 453 return FALSE; 454 } 455 456 static void ag_confirm(GIOChannel *chan, gpointer data) 457 { 458 const char *server_uuid, *remote_uuid; 459 struct audio_device *device; 460 gboolean hfp_active; 461 bdaddr_t src, dst; 462 int perr; 463 GError *err = NULL; 464 uint8_t ch; 465 466 bt_io_get(chan, BT_IO_RFCOMM, &err, 467 BT_IO_OPT_SOURCE_BDADDR, &src, 468 BT_IO_OPT_DEST_BDADDR, &dst, 469 BT_IO_OPT_CHANNEL, &ch, 470 BT_IO_OPT_INVALID); 471 if (err) { 472 error("%s", err->message); 473 g_error_free(err); 474 goto drop; 475 } 476 477 if (ch == DEFAULT_HS_AG_CHANNEL) { 478 hfp_active = FALSE; 479 server_uuid = HSP_AG_UUID; 480 remote_uuid = HSP_HS_UUID; 481 } else { 482 hfp_active = TRUE; 483 server_uuid = HFP_AG_UUID; 484 remote_uuid = HFP_HS_UUID; 485 } 486 487 device = manager_get_device(&src, &dst, TRUE); 488 if (!device) 489 goto drop; 490 491 if (!manager_allow_headset_connection(device)) { 492 DBG("Refusing headset: too many existing connections"); 493 goto drop; 494 } 495 496 if (!device->headset) { 497 btd_device_add_uuid(device->btd_dev, remote_uuid); 498 if (!device->headset) 499 goto drop; 500 } 501 502 if (headset_get_state(device) > HEADSET_STATE_DISCONNECTED) { 503 DBG("Refusing new connection since one already exists"); 504 goto drop; 505 } 506 507 set_hfp_active(device, hfp_active); 508 509 if (headset_connect_rfcomm(device, chan) < 0) { 510 error("headset_connect_rfcomm failed"); 511 goto drop; 512 } 513 514 headset_set_state(device, HEADSET_STATE_CONNECTING); 515 516 perr = audio_device_request_authorization(device, server_uuid, 517 headset_auth_cb, device); 518 if (perr < 0) { 519 DBG("Authorization denied: %s", strerror(-perr)); 520 headset_set_state(device, HEADSET_STATE_DISCONNECTED); 521 return; 522 } 523 524 device->hs_preauth_id = g_io_add_watch(chan, 525 G_IO_NVAL | G_IO_HUP | G_IO_ERR, 526 hs_preauth_cb, device); 527 528 device->auto_connect = auto_connect; 529 530 return; 531 532 drop: 533 g_io_channel_shutdown(chan, TRUE, NULL); 534 } 535 536 static void gateway_auth_cb(DBusError *derr, void *user_data) 537 { 538 struct audio_device *device = user_data; 539 540 if (derr && dbus_error_is_set(derr)) 541 error("Access denied: %s", derr->message); 542 else { 543 char ag_address[18]; 544 545 ba2str(&device->dst, ag_address); 546 DBG("Accepted AG connection from %s for %s", 547 ag_address, device->path); 548 549 gateway_start_service(device); 550 } 551 } 552 553 static void hf_io_cb(GIOChannel *chan, gpointer data) 554 { 555 bdaddr_t src, dst; 556 GError *err = NULL; 557 uint8_t ch; 558 const char *server_uuid, *remote_uuid; 559 uint16_t svclass; 560 struct audio_device *device; 561 int perr; 562 563 bt_io_get(chan, BT_IO_RFCOMM, &err, 564 BT_IO_OPT_SOURCE_BDADDR, &src, 565 BT_IO_OPT_DEST_BDADDR, &dst, 566 BT_IO_OPT_CHANNEL, &ch, 567 BT_IO_OPT_INVALID); 568 569 if (err) { 570 error("%s", err->message); 571 g_error_free(err); 572 return; 573 } 574 575 server_uuid = HFP_AG_UUID; 576 remote_uuid = HFP_HS_UUID; 577 svclass = HANDSFREE_AGW_SVCLASS_ID; 578 579 device = manager_get_device(&src, &dst, TRUE); 580 if (!device) 581 goto drop; 582 583 if (!device->gateway) { 584 btd_device_add_uuid(device->btd_dev, remote_uuid); 585 if (!device->gateway) 586 goto drop; 587 } 588 589 if (gateway_is_connected(device)) { 590 DBG("Refusing new connection since one already exists"); 591 goto drop; 592 } 593 594 if (gateway_connect_rfcomm(device, chan) < 0) { 595 error("Allocating new GIOChannel failed!"); 596 goto drop; 597 } 598 599 perr = audio_device_request_authorization(device, server_uuid, 600 gateway_auth_cb, device); 601 if (perr < 0) { 602 DBG("Authorization denied!"); 603 goto drop; 604 } 605 606 return; 607 608 drop: 609 g_io_channel_shutdown(chan, TRUE, NULL); 610 g_io_channel_unref(chan); 611 } 612 613 static int headset_server_init(struct audio_adapter *adapter) 614 { 615 uint8_t chan = DEFAULT_HS_AG_CHANNEL; 616 sdp_record_t *record; 617 gboolean master = TRUE; 618 GError *err = NULL; 619 uint32_t features; 620 GIOChannel *io; 621 bdaddr_t src; 622 623 if (config) { 624 gboolean tmp; 625 626 tmp = g_key_file_get_boolean(config, "General", "Master", 627 &err); 628 if (err) { 629 DBG("audio.conf: %s", err->message); 630 g_clear_error(&err); 631 } else 632 master = tmp; 633 } 634 635 adapter_get_address(adapter->btd_adapter, &src); 636 637 io = bt_io_listen(BT_IO_RFCOMM, NULL, ag_confirm, adapter, NULL, &err, 638 BT_IO_OPT_SOURCE_BDADDR, &src, 639 BT_IO_OPT_CHANNEL, chan, 640 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM, 641 BT_IO_OPT_MASTER, master, 642 BT_IO_OPT_INVALID); 643 if (!io) 644 goto failed; 645 646 adapter->hsp_ag_server = io; 647 648 record = hsp_ag_record(chan); 649 if (!record) { 650 error("Unable to allocate new service record"); 651 goto failed; 652 } 653 654 if (add_record_to_server(&src, record) < 0) { 655 error("Unable to register HS AG service record"); 656 sdp_record_free(record); 657 goto failed; 658 } 659 adapter->hsp_ag_record_id = record->handle; 660 661 features = headset_config_init(config); 662 663 if (!enabled.hfp) 664 return 0; 665 666 chan = DEFAULT_HF_AG_CHANNEL; 667 668 io = bt_io_listen(BT_IO_RFCOMM, NULL, ag_confirm, adapter, NULL, &err, 669 BT_IO_OPT_SOURCE_BDADDR, &src, 670 BT_IO_OPT_CHANNEL, chan, 671 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM, 672 BT_IO_OPT_MASTER, master, 673 BT_IO_OPT_INVALID); 674 if (!io) 675 goto failed; 676 677 adapter->hfp_ag_server = io; 678 679 record = hfp_ag_record(chan, features); 680 if (!record) { 681 error("Unable to allocate new service record"); 682 goto failed; 683 } 684 685 if (add_record_to_server(&src, record) < 0) { 686 error("Unable to register HF AG service record"); 687 sdp_record_free(record); 688 goto failed; 689 } 690 adapter->hfp_ag_record_id = record->handle; 691 692 return 0; 693 694 failed: 695 error("%s", err->message); 696 g_error_free(err); 697 if (adapter->hsp_ag_server) { 698 g_io_channel_shutdown(adapter->hsp_ag_server, TRUE, NULL); 699 g_io_channel_unref(adapter->hsp_ag_server); 700 adapter->hsp_ag_server = NULL; 701 } 702 703 if (adapter->hfp_ag_server) { 704 g_io_channel_shutdown(adapter->hfp_ag_server, TRUE, NULL); 705 g_io_channel_unref(adapter->hfp_ag_server); 706 adapter->hfp_ag_server = NULL; 707 } 708 709 return -1; 710 } 711 712 static int gateway_server_init(struct audio_adapter *adapter) 713 { 714 uint8_t chan = DEFAULT_HFP_HS_CHANNEL; 715 sdp_record_t *record; 716 gboolean master = TRUE; 717 GError *err = NULL; 718 GIOChannel *io; 719 bdaddr_t src; 720 721 if (config) { 722 gboolean tmp; 723 724 tmp = g_key_file_get_boolean(config, "General", "Master", 725 &err); 726 if (err) { 727 DBG("audio.conf: %s", err->message); 728 g_clear_error(&err); 729 } else 730 master = tmp; 731 } 732 733 adapter_get_address(adapter->btd_adapter, &src); 734 735 io = bt_io_listen(BT_IO_RFCOMM, NULL, hf_io_cb, adapter, NULL, &err, 736 BT_IO_OPT_SOURCE_BDADDR, &src, 737 BT_IO_OPT_CHANNEL, chan, 738 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM, 739 BT_IO_OPT_MASTER, master, 740 BT_IO_OPT_INVALID); 741 if (!io) { 742 error("%s", err->message); 743 g_error_free(err); 744 return -1; 745 } 746 747 adapter->hfp_hs_server = io; 748 record = hfp_hs_record(chan); 749 if (!record) { 750 error("Unable to allocate new service record"); 751 return -1; 752 } 753 754 if (add_record_to_server(&src, record) < 0) { 755 error("Unable to register HFP HS service record"); 756 sdp_record_free(record); 757 g_io_channel_unref(adapter->hfp_hs_server); 758 adapter->hfp_hs_server = NULL; 759 return -1; 760 } 761 762 adapter->hfp_hs_record_id = record->handle; 763 764 return 0; 765 } 766 767 static int audio_probe(struct btd_device *device, GSList *uuids) 768 { 769 struct btd_adapter *adapter = device_get_adapter(device); 770 bdaddr_t src, dst; 771 struct audio_device *audio_dev; 772 773 adapter_get_address(adapter, &src); 774 device_get_address(device, &dst); 775 776 audio_dev = manager_get_device(&src, &dst, TRUE); 777 if (!audio_dev) { 778 DBG("unable to get a device object"); 779 return -1; 780 } 781 782 g_slist_foreach(uuids, (GFunc) handle_uuid, audio_dev); 783 784 return 0; 785 } 786 787 static void audio_remove(struct btd_device *device) 788 { 789 struct audio_device *dev; 790 const char *path; 791 792 path = device_get_path(device); 793 794 dev = manager_find_device(path, NULL, NULL, NULL, FALSE); 795 if (!dev) 796 return; 797 798 devices = g_slist_remove(devices, dev); 799 800 audio_device_unregister(dev); 801 802 } 803 804 static struct audio_adapter *audio_adapter_ref(struct audio_adapter *adp) 805 { 806 adp->ref++; 807 808 DBG("%p: ref=%d", adp, adp->ref); 809 810 return adp; 811 } 812 813 static void audio_adapter_unref(struct audio_adapter *adp) 814 { 815 adp->ref--; 816 817 DBG("%p: ref=%d", adp, adp->ref); 818 819 if (adp->ref > 0) 820 return; 821 822 adapters = g_slist_remove(adapters, adp); 823 btd_adapter_unref(adp->btd_adapter); 824 g_free(adp); 825 } 826 827 static struct audio_adapter *audio_adapter_create(struct btd_adapter *adapter) 828 { 829 struct audio_adapter *adp; 830 831 adp = g_new0(struct audio_adapter, 1); 832 adp->btd_adapter = btd_adapter_ref(adapter); 833 834 return audio_adapter_ref(adp); 835 } 836 837 static struct audio_adapter *audio_adapter_get(struct btd_adapter *adapter) 838 { 839 struct audio_adapter *adp; 840 841 adp = find_adapter(adapters, adapter); 842 if (!adp) { 843 adp = audio_adapter_create(adapter); 844 if (!adp) 845 return NULL; 846 adapters = g_slist_append(adapters, adp); 847 } else 848 audio_adapter_ref(adp); 849 850 return adp; 851 } 852 853 static int headset_server_probe(struct btd_adapter *adapter) 854 { 855 struct audio_adapter *adp; 856 const gchar *path = adapter_get_path(adapter); 857 int err; 858 859 DBG("path %s", path); 860 861 adp = audio_adapter_get(adapter); 862 if (!adp) 863 return -EINVAL; 864 865 err = headset_server_init(adp); 866 if (err < 0) 867 audio_adapter_unref(adp); 868 869 return err; 870 } 871 872 static void headset_server_remove(struct btd_adapter *adapter) 873 { 874 struct audio_adapter *adp; 875 const gchar *path = adapter_get_path(adapter); 876 877 DBG("path %s", path); 878 879 adp = find_adapter(adapters, adapter); 880 if (!adp) 881 return; 882 883 if (adp->hsp_ag_record_id) { 884 remove_record_from_server(adp->hsp_ag_record_id); 885 adp->hsp_ag_record_id = 0; 886 } 887 888 if (adp->hsp_ag_server) { 889 g_io_channel_shutdown(adp->hsp_ag_server, TRUE, NULL); 890 g_io_channel_unref(adp->hsp_ag_server); 891 adp->hsp_ag_server = NULL; 892 } 893 894 if (adp->hfp_ag_record_id) { 895 remove_record_from_server(adp->hfp_ag_record_id); 896 adp->hfp_ag_record_id = 0; 897 } 898 899 if (adp->hfp_ag_server) { 900 g_io_channel_shutdown(adp->hfp_ag_server, TRUE, NULL); 901 g_io_channel_unref(adp->hfp_ag_server); 902 adp->hfp_ag_server = NULL; 903 } 904 905 audio_adapter_unref(adp); 906 } 907 908 static int gateway_server_probe(struct btd_adapter *adapter) 909 { 910 struct audio_adapter *adp; 911 912 adp = audio_adapter_get(adapter); 913 if (!adp) 914 return -EINVAL; 915 916 return gateway_server_init(adp); 917 } 918 919 static void gateway_server_remove(struct btd_adapter *adapter) 920 { 921 struct audio_adapter *adp; 922 const gchar *path = adapter_get_path(adapter); 923 924 DBG("path %s", path); 925 926 adp = find_adapter(adapters, adapter); 927 if (!adp) 928 return; 929 930 if (adp->hfp_hs_record_id) { 931 remove_record_from_server(adp->hfp_hs_record_id); 932 adp->hfp_hs_record_id = 0; 933 } 934 935 if (adp->hfp_hs_server) { 936 g_io_channel_unref(adp->hfp_hs_server); 937 adp->hfp_hs_server = NULL; 938 } 939 940 audio_adapter_unref(adp); 941 } 942 943 static int a2dp_server_probe(struct btd_adapter *adapter) 944 { 945 struct audio_adapter *adp; 946 const gchar *path = adapter_get_path(adapter); 947 bdaddr_t src; 948 int err; 949 950 DBG("path %s", path); 951 952 adp = audio_adapter_get(adapter); 953 if (!adp) 954 return -EINVAL; 955 956 adapter_get_address(adapter, &src); 957 958 err = a2dp_register(connection, &src, config); 959 if (err < 0) 960 audio_adapter_unref(adp); 961 962 return err; 963 } 964 965 static void a2dp_server_remove(struct btd_adapter *adapter) 966 { 967 struct audio_adapter *adp; 968 const gchar *path = adapter_get_path(adapter); 969 bdaddr_t src; 970 971 DBG("path %s", path); 972 973 adp = find_adapter(adapters, adapter); 974 if (!adp) 975 return; 976 977 adapter_get_address(adapter, &src); 978 a2dp_unregister(&src); 979 audio_adapter_unref(adp); 980 } 981 982 static int avrcp_server_probe(struct btd_adapter *adapter) 983 { 984 struct audio_adapter *adp; 985 const gchar *path = adapter_get_path(adapter); 986 bdaddr_t src; 987 988 DBG("path %s", path); 989 990 adp = audio_adapter_get(adapter); 991 if (!adp) 992 return -EINVAL; 993 994 adapter_get_address(adapter, &src); 995 996 return avrcp_register(connection, &src, config); 997 } 998 999 static void avrcp_server_remove(struct btd_adapter *adapter) 1000 { 1001 struct audio_adapter *adp; 1002 const gchar *path = adapter_get_path(adapter); 1003 bdaddr_t src; 1004 1005 DBG("path %s", path); 1006 1007 adp = find_adapter(adapters, adapter); 1008 if (!adp) 1009 return; 1010 1011 adapter_get_address(adapter, &src); 1012 avrcp_unregister(&src); 1013 audio_adapter_unref(adp); 1014 } 1015 1016 static struct btd_device_driver audio_driver = { 1017 .name = "audio", 1018 .uuids = BTD_UUIDS(HSP_HS_UUID, HFP_HS_UUID, HSP_AG_UUID, HFP_AG_UUID, 1019 ADVANCED_AUDIO_UUID, A2DP_SOURCE_UUID, A2DP_SINK_UUID, 1020 AVRCP_TARGET_UUID, AVRCP_REMOTE_UUID), 1021 .probe = audio_probe, 1022 .remove = audio_remove, 1023 }; 1024 1025 static struct btd_adapter_driver headset_server_driver = { 1026 .name = "audio-headset", 1027 .probe = headset_server_probe, 1028 .remove = headset_server_remove, 1029 }; 1030 1031 static struct btd_adapter_driver gateway_server_driver = { 1032 .name = "audio-gateway", 1033 .probe = gateway_server_probe, 1034 .remove = gateway_server_remove, 1035 }; 1036 1037 static struct btd_adapter_driver a2dp_server_driver = { 1038 .name = "audio-a2dp", 1039 .probe = a2dp_server_probe, 1040 .remove = a2dp_server_remove, 1041 }; 1042 1043 static struct btd_adapter_driver avrcp_server_driver = { 1044 .name = "audio-control", 1045 .probe = avrcp_server_probe, 1046 .remove = avrcp_server_remove, 1047 }; 1048 1049 int audio_manager_init(DBusConnection *conn, GKeyFile *conf, 1050 gboolean *enable_sco) 1051 { 1052 char **list; 1053 int i; 1054 gboolean b; 1055 GError *err = NULL; 1056 1057 connection = dbus_connection_ref(conn); 1058 1059 if (!conf) 1060 goto proceed; 1061 1062 config = conf; 1063 1064 list = g_key_file_get_string_list(config, "General", "Enable", 1065 NULL, NULL); 1066 for (i = 0; list && list[i] != NULL; i++) { 1067 if (g_str_equal(list[i], "Headset")) 1068 enabled.headset = TRUE; 1069 else if (g_str_equal(list[i], "Gateway")) 1070 enabled.gateway = TRUE; 1071 else if (g_str_equal(list[i], "Sink")) 1072 enabled.sink = TRUE; 1073 else if (g_str_equal(list[i], "Source")) 1074 enabled.source = TRUE; 1075 else if (g_str_equal(list[i], "Control")) 1076 enabled.control = TRUE; 1077 } 1078 g_strfreev(list); 1079 1080 list = g_key_file_get_string_list(config, "General", "Disable", 1081 NULL, NULL); 1082 for (i = 0; list && list[i] != NULL; i++) { 1083 if (g_str_equal(list[i], "Headset")) 1084 enabled.headset = FALSE; 1085 else if (g_str_equal(list[i], "Gateway")) 1086 enabled.gateway = FALSE; 1087 else if (g_str_equal(list[i], "Sink")) 1088 enabled.sink = FALSE; 1089 else if (g_str_equal(list[i], "Source")) 1090 enabled.source = FALSE; 1091 else if (g_str_equal(list[i], "Control")) 1092 enabled.control = FALSE; 1093 } 1094 g_strfreev(list); 1095 1096 b = g_key_file_get_boolean(config, "General", "AutoConnect", &err); 1097 if (err) { 1098 DBG("audio.conf: %s", err->message); 1099 g_clear_error(&err); 1100 } else 1101 auto_connect = b; 1102 1103 b = g_key_file_get_boolean(config, "Headset", "HFP", 1104 &err); 1105 if (err) 1106 g_clear_error(&err); 1107 else 1108 enabled.hfp = b; 1109 1110 err = NULL; 1111 i = g_key_file_get_integer(config, "Headset", "MaxConnected", 1112 &err); 1113 if (err) { 1114 DBG("audio.conf: %s", err->message); 1115 g_clear_error(&err); 1116 } else 1117 max_connected_headsets = i; 1118 1119 proceed: 1120 if (enabled.headset) { 1121 telephony_init(); 1122 btd_register_adapter_driver(&headset_server_driver); 1123 } 1124 1125 if (enabled.gateway) 1126 btd_register_adapter_driver(&gateway_server_driver); 1127 1128 if (enabled.source || enabled.sink) 1129 btd_register_adapter_driver(&a2dp_server_driver); 1130 1131 if (enabled.control) 1132 btd_register_adapter_driver(&avrcp_server_driver); 1133 1134 btd_register_device_driver(&audio_driver); 1135 1136 *enable_sco = (enabled.gateway || enabled.headset); 1137 1138 return 0; 1139 } 1140 1141 void audio_manager_exit(void) 1142 { 1143 /* Bail out early if we haven't been initialized */ 1144 if (connection == NULL) 1145 return; 1146 1147 dbus_connection_unref(connection); 1148 connection = NULL; 1149 1150 if (config) { 1151 g_key_file_free(config); 1152 config = NULL; 1153 } 1154 1155 if (enabled.headset) { 1156 btd_unregister_adapter_driver(&headset_server_driver); 1157 telephony_exit(); 1158 } 1159 1160 if (enabled.gateway) 1161 btd_unregister_adapter_driver(&gateway_server_driver); 1162 1163 if (enabled.source || enabled.sink) 1164 btd_unregister_adapter_driver(&a2dp_server_driver); 1165 1166 if (enabled.control) 1167 btd_unregister_adapter_driver(&avrcp_server_driver); 1168 1169 btd_unregister_device_driver(&audio_driver); 1170 } 1171 1172 struct audio_device *manager_find_device(const char *path, 1173 const bdaddr_t *src, 1174 const bdaddr_t *dst, 1175 const char *interface, 1176 gboolean connected) 1177 { 1178 GSList *l; 1179 1180 for (l = devices; l != NULL; l = l->next) { 1181 struct audio_device *dev = l->data; 1182 1183 if ((path && (strcmp(path, "")) && strcmp(dev->path, path))) 1184 continue; 1185 1186 if ((src && bacmp(src, BDADDR_ANY)) && bacmp(&dev->src, src)) 1187 continue; 1188 1189 if ((dst && bacmp(dst, BDADDR_ANY)) && bacmp(&dev->dst, dst)) 1190 continue; 1191 1192 if (interface && !strcmp(AUDIO_HEADSET_INTERFACE, interface) 1193 && !dev->headset) 1194 continue; 1195 1196 if (interface && !strcmp(AUDIO_GATEWAY_INTERFACE, interface) 1197 && !dev->gateway) 1198 continue; 1199 1200 if (interface && !strcmp(AUDIO_SINK_INTERFACE, interface) 1201 && !dev->sink) 1202 continue; 1203 1204 if (interface && !strcmp(AUDIO_SOURCE_INTERFACE, interface) 1205 && !dev->source) 1206 continue; 1207 1208 if (interface && !strcmp(AUDIO_CONTROL_INTERFACE, interface) 1209 && !dev->control) 1210 continue; 1211 1212 if (connected && !audio_device_is_active(dev, interface)) 1213 continue; 1214 1215 return dev; 1216 } 1217 1218 return NULL; 1219 } 1220 1221 struct audio_device *manager_get_device(const bdaddr_t *src, 1222 const bdaddr_t *dst, 1223 gboolean create) 1224 { 1225 struct audio_device *dev; 1226 struct btd_adapter *adapter; 1227 struct btd_device *device; 1228 char addr[18]; 1229 const char *path; 1230 1231 dev = manager_find_device(NULL, src, dst, NULL, FALSE); 1232 if (dev) 1233 return dev; 1234 1235 if (!create) 1236 return NULL; 1237 1238 ba2str(src, addr); 1239 1240 adapter = manager_find_adapter(src); 1241 if (!adapter) { 1242 error("Unable to get a btd_adapter object for %s", 1243 addr); 1244 return NULL; 1245 } 1246 1247 ba2str(dst, addr); 1248 1249 device = adapter_get_device(connection, adapter, addr); 1250 if (!device) { 1251 error("Unable to get btd_device object for %s", addr); 1252 return NULL; 1253 } 1254 1255 path = device_get_path(device); 1256 1257 dev = audio_device_register(connection, device, path, src, dst); 1258 if (!dev) 1259 return NULL; 1260 1261 devices = g_slist_append(devices, dev); 1262 1263 return dev; 1264 } 1265 1266 gboolean manager_allow_headset_connection(struct audio_device *device) 1267 { 1268 GSList *l; 1269 int connected = 0; 1270 1271 for (l = devices; l != NULL; l = l->next) { 1272 struct audio_device *dev = l->data; 1273 struct headset *hs = dev->headset; 1274 1275 if (dev == device) 1276 continue; 1277 1278 if (bacmp(&dev->src, &device->src)) 1279 continue; 1280 1281 if (!hs) 1282 continue; 1283 1284 if (headset_get_state(dev) > HEADSET_STATE_DISCONNECTED) 1285 connected++; 1286 1287 if (connected >= max_connected_headsets) 1288 return FALSE; 1289 } 1290 1291 return TRUE; 1292 } 1293