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 <errno.h> 31 #include <fcntl.h> 32 #include <unistd.h> 33 #include <stdlib.h> 34 #include <stdarg.h> 35 #include <signal.h> 36 #include <string.h> 37 #include <getopt.h> 38 #include <sys/ioctl.h> 39 #include <sys/socket.h> 40 #include <assert.h> 41 42 #include <bluetooth/bluetooth.h> 43 #include <bluetooth/sdp.h> 44 #include <bluetooth/sdp_lib.h> 45 46 #include <glib.h> 47 #include <dbus/dbus.h> 48 #include <gdbus.h> 49 50 #include "log.h" 51 #include "device.h" 52 #include "manager.h" 53 #include "error.h" 54 #include "telephony.h" 55 #include "headset.h" 56 #include "glib-helper.h" 57 #include "btio.h" 58 #include "dbus-common.h" 59 #include "../src/adapter.h" 60 #include "../src/device.h" 61 62 #define DC_TIMEOUT 3 63 64 #define RING_INTERVAL 3 65 66 #define BUF_SIZE 1024 67 68 #define HEADSET_GAIN_SPEAKER 'S' 69 #define HEADSET_GAIN_MICROPHONE 'M' 70 71 static struct { 72 gboolean telephony_ready; /* Telephony plugin initialized */ 73 uint32_t features; /* HFP AG features */ 74 const struct indicator *indicators; /* Available HFP indicators */ 75 int er_mode; /* Event reporting mode */ 76 int er_ind; /* Event reporting for indicators */ 77 int rh; /* Response and Hold state */ 78 char *number; /* Incoming phone number */ 79 int number_type; /* Incoming number type */ 80 guint ring_timer; /* For incoming call indication */ 81 const char *chld; /* Response to AT+CHLD=? */ 82 } ag = { 83 .telephony_ready = FALSE, 84 .features = 0, 85 .er_mode = 3, 86 .er_ind = 0, 87 .rh = BTRH_NOT_SUPPORTED, 88 .number = NULL, 89 .number_type = 0, 90 .ring_timer = 0, 91 }; 92 93 static gboolean sco_hci = TRUE; 94 static gboolean fast_connectable = FALSE; 95 96 static GSList *active_devices = NULL; 97 98 static char *str_state[] = { 99 "HEADSET_STATE_DISCONNECTED", 100 "HEADSET_STATE_CONNECTING", 101 "HEADSET_STATE_CONNECTED", 102 "HEADSET_STATE_PLAY_IN_PROGRESS", 103 "HEADSET_STATE_PLAYING", 104 }; 105 106 struct headset_state_callback { 107 headset_state_cb cb; 108 void *user_data; 109 unsigned int id; 110 }; 111 112 struct headset_nrec_callback { 113 unsigned int id; 114 headset_nrec_cb cb; 115 void *user_data; 116 }; 117 118 struct connect_cb { 119 unsigned int id; 120 headset_stream_cb_t cb; 121 void *cb_data; 122 }; 123 124 struct pending_connect { 125 DBusMessage *msg; 126 DBusPendingCall *call; 127 GIOChannel *io; 128 int err; 129 headset_state_t target_state; 130 GSList *callbacks; 131 uint16_t svclass; 132 }; 133 134 struct headset_slc { 135 char buf[BUF_SIZE]; 136 int data_start; 137 int data_length; 138 139 gboolean cli_active; 140 gboolean cme_enabled; 141 gboolean cwa_enabled; 142 gboolean pending_ring; 143 gboolean inband_ring; 144 gboolean nrec; 145 gboolean nrec_req; 146 147 int sp_gain; 148 int mic_gain; 149 150 unsigned int hf_features; 151 }; 152 153 struct headset { 154 uint32_t hsp_handle; 155 uint32_t hfp_handle; 156 157 int rfcomm_ch; 158 159 GIOChannel *rfcomm; 160 GIOChannel *tmp_rfcomm; 161 GIOChannel *sco; 162 guint sco_id; 163 164 gboolean auto_dc; 165 166 guint dc_timer; 167 168 gboolean hfp_active; 169 gboolean search_hfp; 170 171 headset_state_t state; 172 struct pending_connect *pending; 173 174 headset_lock_t lock; 175 struct headset_slc *slc; 176 GSList *nrec_cbs; 177 }; 178 179 struct event { 180 const char *cmd; 181 int (*callback) (struct audio_device *device, const char *buf); 182 }; 183 184 static GSList *headset_callbacks = NULL; 185 186 static void error_connect_failed(DBusConnection *conn, DBusMessage *msg, 187 int err) 188 { 189 DBusMessage *reply = btd_error_failed(msg, 190 err < 0 ? strerror(-err) : "Connect failed"); 191 g_dbus_send_message(conn, reply); 192 } 193 194 static int rfcomm_connect(struct audio_device *device, headset_stream_cb_t cb, 195 void *user_data, unsigned int *cb_id); 196 static int get_records(struct audio_device *device, headset_stream_cb_t cb, 197 void *user_data, unsigned int *cb_id); 198 199 static void print_ag_features(uint32_t features) 200 { 201 GString *gstr; 202 char *str; 203 204 if (features == 0) { 205 DBG("HFP AG features: (none)"); 206 return; 207 } 208 209 gstr = g_string_new("HFP AG features: "); 210 211 if (features & AG_FEATURE_THREE_WAY_CALLING) 212 g_string_append(gstr, "\"Three-way calling\" "); 213 if (features & AG_FEATURE_EC_ANDOR_NR) 214 g_string_append(gstr, "\"EC and/or NR function\" "); 215 if (features & AG_FEATURE_VOICE_RECOGNITION) 216 g_string_append(gstr, "\"Voice recognition function\" "); 217 if (features & AG_FEATURE_INBAND_RINGTONE) 218 g_string_append(gstr, "\"In-band ring tone capability\" "); 219 if (features & AG_FEATURE_ATTACH_NUMBER_TO_VOICETAG) 220 g_string_append(gstr, "\"Attach a number to a voice tag\" "); 221 if (features & AG_FEATURE_REJECT_A_CALL) 222 g_string_append(gstr, "\"Ability to reject a call\" "); 223 if (features & AG_FEATURE_ENHANCED_CALL_STATUS) 224 g_string_append(gstr, "\"Enhanced call status\" "); 225 if (features & AG_FEATURE_ENHANCED_CALL_CONTROL) 226 g_string_append(gstr, "\"Enhanced call control\" "); 227 if (features & AG_FEATURE_EXTENDED_ERROR_RESULT_CODES) 228 g_string_append(gstr, "\"Extended Error Result Codes\" "); 229 230 str = g_string_free(gstr, FALSE); 231 232 DBG("%s", str); 233 234 g_free(str); 235 } 236 237 static void print_hf_features(uint32_t features) 238 { 239 GString *gstr; 240 char *str; 241 242 if (features == 0) { 243 DBG("HFP HF features: (none)"); 244 return; 245 } 246 247 gstr = g_string_new("HFP HF features: "); 248 249 if (features & HF_FEATURE_EC_ANDOR_NR) 250 g_string_append(gstr, "\"EC and/or NR function\" "); 251 if (features & HF_FEATURE_CALL_WAITING_AND_3WAY) 252 g_string_append(gstr, "\"Call waiting and 3-way calling\" "); 253 if (features & HF_FEATURE_CLI_PRESENTATION) 254 g_string_append(gstr, "\"CLI presentation capability\" "); 255 if (features & HF_FEATURE_VOICE_RECOGNITION) 256 g_string_append(gstr, "\"Voice recognition activation\" "); 257 if (features & HF_FEATURE_REMOTE_VOLUME_CONTROL) 258 g_string_append(gstr, "\"Remote volume control\" "); 259 if (features & HF_FEATURE_ENHANCED_CALL_STATUS) 260 g_string_append(gstr, "\"Enhanced call status\" "); 261 if (features & HF_FEATURE_ENHANCED_CALL_CONTROL) 262 g_string_append(gstr, "\"Enhanced call control\" "); 263 264 str = g_string_free(gstr, FALSE); 265 266 DBG("%s", str); 267 268 g_free(str); 269 } 270 271 static const char *state2str(headset_state_t state) 272 { 273 switch (state) { 274 case HEADSET_STATE_DISCONNECTED: 275 return "disconnected"; 276 case HEADSET_STATE_CONNECTING: 277 return "connecting"; 278 case HEADSET_STATE_CONNECTED: 279 case HEADSET_STATE_PLAY_IN_PROGRESS: 280 return "connected"; 281 case HEADSET_STATE_PLAYING: 282 return "playing"; 283 } 284 285 return NULL; 286 } 287 288 static int headset_send_valist(struct headset *hs, char *format, va_list ap) 289 { 290 char rsp[BUF_SIZE]; 291 ssize_t total_written, count; 292 int fd; 293 294 count = vsnprintf(rsp, sizeof(rsp), format, ap); 295 296 if (count < 0) 297 return -EINVAL; 298 299 if (!hs->rfcomm) { 300 error("headset_send: the headset is not connected"); 301 return -EIO; 302 } 303 304 total_written = 0; 305 fd = g_io_channel_unix_get_fd(hs->rfcomm); 306 307 while (total_written < count) { 308 ssize_t written; 309 310 written = write(fd, rsp + total_written, 311 count - total_written); 312 if (written < 0) 313 return -errno; 314 315 total_written += written; 316 } 317 318 return 0; 319 } 320 321 static int __attribute__((format(printf, 2, 3))) 322 headset_send(struct headset *hs, char *format, ...) 323 { 324 va_list ap; 325 int ret; 326 327 va_start(ap, format); 328 ret = headset_send_valist(hs, format, ap); 329 va_end(ap); 330 331 return ret; 332 } 333 334 static int supported_features(struct audio_device *device, const char *buf) 335 { 336 struct headset *hs = device->headset; 337 struct headset_slc *slc = hs->slc; 338 int err; 339 340 if (strlen(buf) < 9) 341 return -EINVAL; 342 343 slc->hf_features = strtoul(&buf[8], NULL, 10); 344 345 print_hf_features(slc->hf_features); 346 347 err = headset_send(hs, "\r\n+BRSF: %u\r\n", ag.features); 348 if (err < 0) 349 return err; 350 351 return headset_send(hs, "\r\nOK\r\n"); 352 } 353 354 static char *indicator_ranges(const struct indicator *indicators) 355 { 356 int i; 357 GString *gstr; 358 359 gstr = g_string_new("\r\n+CIND: "); 360 361 for (i = 0; indicators[i].desc != NULL; i++) { 362 if (i == 0) 363 g_string_append_printf(gstr, "(\"%s\",(%s))", 364 indicators[i].desc, 365 indicators[i].range); 366 else 367 g_string_append_printf(gstr, ",(\"%s\",(%s))", 368 indicators[i].desc, 369 indicators[i].range); 370 } 371 372 g_string_append(gstr, "\r\n"); 373 374 return g_string_free(gstr, FALSE); 375 } 376 377 static char *indicator_values(const struct indicator *indicators) 378 { 379 int i; 380 GString *gstr; 381 382 gstr = g_string_new("\r\n+CIND: "); 383 384 for (i = 0; indicators[i].desc != NULL; i++) { 385 if (i == 0) 386 g_string_append_printf(gstr, "%d", indicators[i].val); 387 else 388 g_string_append_printf(gstr, ",%d", indicators[i].val); 389 } 390 391 g_string_append(gstr, "\r\n"); 392 393 return g_string_free(gstr, FALSE); 394 } 395 396 static int report_indicators(struct audio_device *device, const char *buf) 397 { 398 struct headset *hs = device->headset; 399 int err; 400 char *str; 401 402 if (strlen(buf) < 8) 403 return -EINVAL; 404 405 if (ag.indicators == NULL) { 406 error("HFP AG indicators not initialized"); 407 return headset_send(hs, "\r\nERROR\r\n"); 408 } 409 410 if (buf[7] == '=') 411 str = indicator_ranges(ag.indicators); 412 else 413 str = indicator_values(ag.indicators); 414 415 err = headset_send(hs, "%s", str); 416 417 g_free(str); 418 419 if (err < 0) 420 return err; 421 422 return headset_send(hs, "\r\nOK\r\n"); 423 } 424 425 static void pending_connect_complete(struct connect_cb *cb, struct audio_device *dev) 426 { 427 struct headset *hs = dev->headset; 428 429 if (hs->pending->err < 0) 430 cb->cb(NULL, cb->cb_data); 431 else 432 cb->cb(dev, cb->cb_data); 433 } 434 435 static void pending_connect_finalize(struct audio_device *dev) 436 { 437 struct headset *hs = dev->headset; 438 struct pending_connect *p = hs->pending; 439 440 if (p == NULL) 441 return; 442 443 if (p->svclass) 444 bt_cancel_discovery(&dev->src, &dev->dst); 445 446 g_slist_foreach(p->callbacks, (GFunc) pending_connect_complete, dev); 447 448 g_slist_foreach(p->callbacks, (GFunc) g_free, NULL); 449 g_slist_free(p->callbacks); 450 451 if (p->io) { 452 g_io_channel_shutdown(p->io, TRUE, NULL); 453 g_io_channel_unref(p->io); 454 } 455 456 if (p->msg) 457 dbus_message_unref(p->msg); 458 459 if (p->call) { 460 dbus_pending_call_cancel(p->call); 461 dbus_pending_call_unref(p->call); 462 } 463 464 g_free(p); 465 466 hs->pending = NULL; 467 } 468 469 static void pending_connect_init(struct headset *hs, headset_state_t target_state) 470 { 471 if (hs->pending) { 472 if (hs->pending->target_state < target_state) 473 hs->pending->target_state = target_state; 474 return; 475 } 476 477 hs->pending = g_new0(struct pending_connect, 1); 478 hs->pending->target_state = target_state; 479 } 480 481 static unsigned int connect_cb_new(struct headset *hs, 482 headset_state_t target_state, 483 headset_stream_cb_t func, 484 void *user_data) 485 { 486 struct connect_cb *cb; 487 static unsigned int free_cb_id = 1; 488 489 pending_connect_init(hs, target_state); 490 491 if (!func) 492 return 0; 493 494 cb = g_new(struct connect_cb, 1); 495 496 cb->cb = func; 497 cb->cb_data = user_data; 498 cb->id = free_cb_id++; 499 500 hs->pending->callbacks = g_slist_append(hs->pending->callbacks, 501 cb); 502 503 return cb->id; 504 } 505 506 static void __attribute__((format(printf, 3, 4))) 507 send_foreach_headset(GSList *devices, 508 int (*cmp) (struct headset *hs), 509 char *format, ...) 510 { 511 GSList *l; 512 va_list ap; 513 514 for (l = devices; l != NULL; l = l->next) { 515 struct audio_device *device = l->data; 516 struct headset *hs = device->headset; 517 int ret; 518 519 assert(hs != NULL); 520 521 if (cmp && cmp(hs) != 0) 522 continue; 523 524 va_start(ap, format); 525 ret = headset_send_valist(hs, format, ap); 526 if (ret < 0) 527 error("Failed to send to headset: %s (%d)", 528 strerror(-ret), -ret); 529 va_end(ap); 530 } 531 } 532 533 static int cli_cmp(struct headset *hs) 534 { 535 struct headset_slc *slc = hs->slc; 536 537 if (!hs->hfp_active) 538 return -1; 539 540 if (slc->cli_active) 541 return 0; 542 else 543 return -1; 544 } 545 546 static gboolean ring_timer_cb(gpointer data) 547 { 548 send_foreach_headset(active_devices, NULL, "\r\nRING\r\n"); 549 550 if (ag.number) 551 send_foreach_headset(active_devices, cli_cmp, 552 "\r\n+CLIP: \"%s\",%d\r\n", 553 ag.number, ag.number_type); 554 555 return TRUE; 556 } 557 558 static void sco_connect_cb(GIOChannel *chan, GError *err, gpointer user_data) 559 { 560 int sk; 561 struct audio_device *dev = user_data; 562 struct headset *hs = dev->headset; 563 struct headset_slc *slc = hs->slc; 564 struct pending_connect *p = hs->pending; 565 566 if (err) { 567 error("%s", err->message); 568 569 if (p != NULL) { 570 p->err = -errno; 571 if (p->msg) 572 error_connect_failed(dev->conn, p->msg, p->err); 573 pending_connect_finalize(dev); 574 } 575 576 if (hs->rfcomm) 577 headset_set_state(dev, HEADSET_STATE_CONNECTED); 578 else 579 headset_set_state(dev, HEADSET_STATE_DISCONNECTED); 580 581 return; 582 } 583 584 DBG("SCO socket opened for headset %s", dev->path); 585 586 sk = g_io_channel_unix_get_fd(chan); 587 588 DBG("SCO fd=%d", sk); 589 590 if (p) { 591 p->io = NULL; 592 if (p->msg) { 593 DBusMessage *reply; 594 reply = dbus_message_new_method_return(p->msg); 595 g_dbus_send_message(dev->conn, reply); 596 } 597 598 pending_connect_finalize(dev); 599 } 600 601 fcntl(sk, F_SETFL, 0); 602 603 headset_set_state(dev, HEADSET_STATE_PLAYING); 604 605 if (slc->pending_ring) { 606 ring_timer_cb(NULL); 607 ag.ring_timer = g_timeout_add_seconds(RING_INTERVAL, 608 ring_timer_cb, 609 NULL); 610 slc->pending_ring = FALSE; 611 } 612 } 613 614 static int sco_connect(struct audio_device *dev, headset_stream_cb_t cb, 615 void *user_data, unsigned int *cb_id) 616 { 617 struct headset *hs = dev->headset; 618 GError *err = NULL; 619 GIOChannel *io; 620 621 if (hs->state != HEADSET_STATE_CONNECTED) 622 return -EINVAL; 623 624 io = bt_io_connect(BT_IO_SCO, sco_connect_cb, dev, NULL, &err, 625 BT_IO_OPT_SOURCE_BDADDR, &dev->src, 626 BT_IO_OPT_DEST_BDADDR, &dev->dst, 627 BT_IO_OPT_INVALID); 628 if (!io) { 629 error("%s", err->message); 630 g_error_free(err); 631 return -EIO; 632 } 633 634 hs->sco = io; 635 636 headset_set_state(dev, HEADSET_STATE_PLAY_IN_PROGRESS); 637 638 pending_connect_init(hs, HEADSET_STATE_PLAYING); 639 640 if (cb) { 641 unsigned int id = connect_cb_new(hs, HEADSET_STATE_PLAYING, 642 cb, user_data); 643 if (cb_id) 644 *cb_id = id; 645 } 646 647 return 0; 648 } 649 650 static int hfp_cmp(struct headset *hs) 651 { 652 if (hs->hfp_active) 653 return 0; 654 else 655 return -1; 656 } 657 658 static void hfp_slc_complete(struct audio_device *dev) 659 { 660 struct headset *hs = dev->headset; 661 struct pending_connect *p = hs->pending; 662 663 DBG("HFP Service Level Connection established"); 664 665 headset_set_state(dev, HEADSET_STATE_CONNECTED); 666 667 if (p == NULL) 668 return; 669 670 if (p->target_state == HEADSET_STATE_CONNECTED) { 671 if (p->msg) { 672 DBusMessage *reply = dbus_message_new_method_return(p->msg); 673 g_dbus_send_message(dev->conn, reply); 674 } 675 pending_connect_finalize(dev); 676 return; 677 } 678 679 p->err = sco_connect(dev, NULL, NULL, NULL); 680 if (p->err < 0) { 681 if (p->msg) 682 error_connect_failed(dev->conn, p->msg, p->err); 683 pending_connect_finalize(dev); 684 } 685 } 686 687 static int telephony_generic_rsp(struct audio_device *device, cme_error_t err) 688 { 689 struct headset *hs = device->headset; 690 struct headset_slc *slc = hs->slc; 691 692 if ((err != CME_ERROR_NONE) && slc->cme_enabled) 693 return headset_send(hs, "\r\n+CME ERROR: %d\r\n", err); 694 695 switch (err) { 696 case CME_ERROR_NONE: 697 return headset_send(hs, "\r\nOK\r\n"); 698 case CME_ERROR_NO_NETWORK_SERVICE: 699 return headset_send(hs, "\r\nNO CARRIER\r\n"); 700 default: 701 return headset_send(hs, "\r\nERROR\r\n"); 702 } 703 } 704 705 int telephony_event_reporting_rsp(void *telephony_device, cme_error_t err) 706 { 707 struct audio_device *device = telephony_device; 708 struct headset *hs = device->headset; 709 struct headset_slc *slc = hs->slc; 710 int ret; 711 712 if (err != CME_ERROR_NONE) 713 return telephony_generic_rsp(telephony_device, err); 714 715 ret = headset_send(hs, "\r\nOK\r\n"); 716 if (ret < 0) 717 return ret; 718 719 if (hs->state != HEADSET_STATE_CONNECTING) 720 return 0; 721 722 if (slc->hf_features & HF_FEATURE_CALL_WAITING_AND_3WAY && 723 ag.features & AG_FEATURE_THREE_WAY_CALLING) 724 return 0; 725 726 hfp_slc_complete(device); 727 728 return 0; 729 } 730 731 static int event_reporting(struct audio_device *dev, const char *buf) 732 { 733 char **tokens; /* <mode>, <keyp>, <disp>, <ind>, <bfr> */ 734 735 if (strlen(buf) < 13) 736 return -EINVAL; 737 738 tokens = g_strsplit(&buf[8], ",", 5); 739 if (g_strv_length(tokens) < 4) { 740 g_strfreev(tokens); 741 return -EINVAL; 742 } 743 744 ag.er_mode = atoi(tokens[0]); 745 ag.er_ind = atoi(tokens[3]); 746 747 g_strfreev(tokens); 748 tokens = NULL; 749 750 DBG("Event reporting (CMER): mode=%d, ind=%d", 751 ag.er_mode, ag.er_ind); 752 753 switch (ag.er_ind) { 754 case 0: 755 case 1: 756 telephony_event_reporting_req(dev, ag.er_ind); 757 break; 758 default: 759 return -EINVAL; 760 } 761 762 return 0; 763 } 764 765 static int call_hold(struct audio_device *dev, const char *buf) 766 { 767 struct headset *hs = dev->headset; 768 int err; 769 770 if (strlen(buf) < 9) 771 return -EINVAL; 772 773 if (buf[8] != '?') { 774 telephony_call_hold_req(dev, &buf[8]); 775 return 0; 776 } 777 778 err = headset_send(hs, "\r\n+CHLD: (%s)\r\n", ag.chld); 779 if (err < 0) 780 return err; 781 782 err = headset_send(hs, "\r\nOK\r\n"); 783 if (err < 0) 784 return err; 785 786 if (hs->state != HEADSET_STATE_CONNECTING) 787 return 0; 788 789 hfp_slc_complete(dev); 790 791 return 0; 792 } 793 794 int telephony_key_press_rsp(void *telephony_device, cme_error_t err) 795 { 796 return telephony_generic_rsp(telephony_device, err); 797 } 798 799 static int key_press(struct audio_device *device, const char *buf) 800 { 801 if (strlen(buf) < 9) 802 return -EINVAL; 803 804 g_dbus_emit_signal(device->conn, device->path, 805 AUDIO_HEADSET_INTERFACE, "AnswerRequested", 806 DBUS_TYPE_INVALID); 807 808 if (ag.ring_timer) { 809 g_source_remove(ag.ring_timer); 810 ag.ring_timer = 0; 811 } 812 813 telephony_key_press_req(device, &buf[8]); 814 815 return 0; 816 } 817 818 int telephony_answer_call_rsp(void *telephony_device, cme_error_t err) 819 { 820 return telephony_generic_rsp(telephony_device, err); 821 } 822 823 static int answer_call(struct audio_device *device, const char *buf) 824 { 825 if (ag.ring_timer) { 826 g_source_remove(ag.ring_timer); 827 ag.ring_timer = 0; 828 } 829 830 if (ag.number) { 831 g_free(ag.number); 832 ag.number = NULL; 833 } 834 835 telephony_answer_call_req(device); 836 837 return 0; 838 } 839 840 int telephony_terminate_call_rsp(void *telephony_device, 841 cme_error_t err) 842 { 843 struct audio_device *device = telephony_device; 844 struct headset *hs = device->headset; 845 846 if (err != CME_ERROR_NONE) 847 return telephony_generic_rsp(telephony_device, err); 848 849 g_dbus_emit_signal(device->conn, device->path, 850 AUDIO_HEADSET_INTERFACE, "CallTerminated", 851 DBUS_TYPE_INVALID); 852 853 return headset_send(hs, "\r\nOK\r\n"); 854 } 855 856 static int terminate_call(struct audio_device *device, const char *buf) 857 { 858 if (ag.number) { 859 g_free(ag.number); 860 ag.number = NULL; 861 } 862 863 if (ag.ring_timer) { 864 g_source_remove(ag.ring_timer); 865 ag.ring_timer = 0; 866 } 867 868 telephony_terminate_call_req(device); 869 870 return 0; 871 } 872 873 static int cli_notification(struct audio_device *device, const char *buf) 874 { 875 struct headset *hs = device->headset; 876 struct headset_slc *slc = hs->slc; 877 878 if (strlen(buf) < 9) 879 return -EINVAL; 880 881 slc->cli_active = buf[8] == '1' ? TRUE : FALSE; 882 883 return headset_send(hs, "\r\nOK\r\n"); 884 } 885 886 int telephony_response_and_hold_rsp(void *telephony_device, cme_error_t err) 887 { 888 return telephony_generic_rsp(telephony_device, err); 889 } 890 891 static int response_and_hold(struct audio_device *device, const char *buf) 892 { 893 struct headset *hs = device->headset; 894 895 if (strlen(buf) < 8) 896 return -EINVAL; 897 898 if (ag.rh == BTRH_NOT_SUPPORTED) 899 return telephony_generic_rsp(device, CME_ERROR_NOT_SUPPORTED); 900 901 if (buf[7] == '=') { 902 telephony_response_and_hold_req(device, atoi(&buf[8]) < 0); 903 return 0; 904 } 905 906 if (ag.rh >= 0) 907 headset_send(hs, "\r\n+BTRH: %d\r\n", ag.rh); 908 909 return headset_send(hs, "\r\nOK\r\n"); 910 } 911 912 int telephony_last_dialed_number_rsp(void *telephony_device, cme_error_t err) 913 { 914 return telephony_generic_rsp(telephony_device, err); 915 } 916 917 static int last_dialed_number(struct audio_device *device, const char *buf) 918 { 919 telephony_last_dialed_number_req(device); 920 921 return 0; 922 } 923 924 int telephony_dial_number_rsp(void *telephony_device, cme_error_t err) 925 { 926 return telephony_generic_rsp(telephony_device, err); 927 } 928 929 static int dial_number(struct audio_device *device, const char *buf) 930 { 931 char number[BUF_SIZE]; 932 size_t buf_len; 933 934 buf_len = strlen(buf); 935 936 if (buf[buf_len - 1] != ';') { 937 DBG("Rejecting non-voice call dial request"); 938 return -EINVAL; 939 } 940 941 memset(number, 0, sizeof(number)); 942 strncpy(number, &buf[3], buf_len - 4); 943 944 telephony_dial_number_req(device, number); 945 946 return 0; 947 } 948 949 static int headset_set_gain(struct audio_device *device, uint16_t gain, char type) 950 { 951 struct headset *hs = device->headset; 952 struct headset_slc *slc = hs->slc; 953 const char *name, *property; 954 955 if (gain > 15) { 956 error("Invalid gain value: %u", gain); 957 return -EINVAL; 958 } 959 960 switch (type) { 961 case HEADSET_GAIN_SPEAKER: 962 if (slc->sp_gain == gain) { 963 DBG("Ignoring no-change in speaker gain"); 964 return -EALREADY; 965 } 966 name = "SpeakerGainChanged"; 967 property = "SpeakerGain"; 968 slc->sp_gain = gain; 969 break; 970 case HEADSET_GAIN_MICROPHONE: 971 if (slc->mic_gain == gain) { 972 DBG("Ignoring no-change in microphone gain"); 973 return -EALREADY; 974 } 975 name = "MicrophoneGainChanged"; 976 property = "MicrophoneGain"; 977 slc->mic_gain = gain; 978 break; 979 default: 980 error("Unknown gain setting"); 981 return -EINVAL; 982 } 983 984 g_dbus_emit_signal(device->conn, device->path, 985 AUDIO_HEADSET_INTERFACE, name, 986 DBUS_TYPE_UINT16, &gain, 987 DBUS_TYPE_INVALID); 988 989 emit_property_changed(device->conn, device->path, 990 AUDIO_HEADSET_INTERFACE, property, 991 DBUS_TYPE_UINT16, &gain); 992 993 return 0; 994 } 995 996 static int signal_gain_setting(struct audio_device *device, const char *buf) 997 { 998 struct headset *hs = device->headset; 999 dbus_uint16_t gain; 1000 int err; 1001 1002 if (strlen(buf) < 8) { 1003 error("Too short string for Gain setting"); 1004 return -EINVAL; 1005 } 1006 1007 gain = (dbus_uint16_t) strtol(&buf[7], NULL, 10); 1008 1009 err = headset_set_gain(device, gain, buf[5]); 1010 if (err < 0 && err != -EALREADY) 1011 return err; 1012 1013 return headset_send(hs, "\r\nOK\r\n"); 1014 } 1015 1016 int telephony_transmit_dtmf_rsp(void *telephony_device, cme_error_t err) 1017 { 1018 return telephony_generic_rsp(telephony_device, err); 1019 } 1020 1021 static int dtmf_tone(struct audio_device *device, const char *buf) 1022 { 1023 char tone; 1024 1025 if (strlen(buf) < 8) { 1026 error("Too short string for DTMF tone"); 1027 return -EINVAL; 1028 } 1029 1030 tone = buf[7]; 1031 if (tone >= '#' && tone <= 'D') 1032 telephony_transmit_dtmf_req(device, tone); 1033 else 1034 return -EINVAL; 1035 1036 return 0; 1037 } 1038 1039 int telephony_subscriber_number_rsp(void *telephony_device, cme_error_t err) 1040 { 1041 return telephony_generic_rsp(telephony_device, err); 1042 } 1043 1044 static int subscriber_number(struct audio_device *device, const char *buf) 1045 { 1046 telephony_subscriber_number_req(device); 1047 1048 return 0; 1049 } 1050 1051 int telephony_list_current_calls_rsp(void *telephony_device, cme_error_t err) 1052 { 1053 return telephony_generic_rsp(telephony_device, err); 1054 } 1055 1056 static int list_current_calls(struct audio_device *device, const char *buf) 1057 { 1058 telephony_list_current_calls_req(device); 1059 1060 return 0; 1061 } 1062 1063 static int extended_errors(struct audio_device *device, const char *buf) 1064 { 1065 struct headset *hs = device->headset; 1066 struct headset_slc *slc = hs->slc; 1067 1068 if (strlen(buf) < 9) 1069 return -EINVAL; 1070 1071 if (buf[8] == '1') { 1072 slc->cme_enabled = TRUE; 1073 DBG("CME errors enabled for headset %p", hs); 1074 } else { 1075 slc->cme_enabled = FALSE; 1076 DBG("CME errors disabled for headset %p", hs); 1077 } 1078 1079 return headset_send(hs, "\r\nOK\r\n"); 1080 } 1081 1082 static int call_waiting_notify(struct audio_device *device, const char *buf) 1083 { 1084 struct headset *hs = device->headset; 1085 struct headset_slc *slc = hs->slc; 1086 1087 if (strlen(buf) < 9) 1088 return -EINVAL; 1089 1090 if (buf[8] == '1') { 1091 slc->cwa_enabled = TRUE; 1092 DBG("Call waiting notification enabled for headset %p", hs); 1093 } else { 1094 slc->cwa_enabled = FALSE; 1095 DBG("Call waiting notification disabled for headset %p", hs); 1096 } 1097 1098 return headset_send(hs, "\r\nOK\r\n"); 1099 } 1100 1101 int telephony_operator_selection_rsp(void *telephony_device, cme_error_t err) 1102 { 1103 return telephony_generic_rsp(telephony_device, err); 1104 } 1105 1106 int telephony_call_hold_rsp(void *telephony_device, cme_error_t err) 1107 { 1108 return telephony_generic_rsp(telephony_device, err); 1109 } 1110 1111 int telephony_nr_and_ec_rsp(void *telephony_device, cme_error_t err) 1112 { 1113 struct audio_device *device = telephony_device; 1114 struct headset *hs = device->headset; 1115 struct headset_slc *slc = hs->slc; 1116 1117 if (err == CME_ERROR_NONE) { 1118 GSList *l; 1119 1120 for (l = hs->nrec_cbs; l; l = l->next) { 1121 struct headset_nrec_callback *nrec_cb = l->data; 1122 1123 nrec_cb->cb(device, slc->nrec_req, nrec_cb->user_data); 1124 } 1125 1126 slc->nrec = hs->slc->nrec_req; 1127 } 1128 1129 return telephony_generic_rsp(telephony_device, err); 1130 } 1131 1132 int telephony_voice_dial_rsp(void *telephony_device, cme_error_t err) 1133 { 1134 return telephony_generic_rsp(telephony_device, err); 1135 } 1136 1137 int telephony_operator_selection_ind(int mode, const char *oper) 1138 { 1139 if (!active_devices) 1140 return -ENODEV; 1141 1142 send_foreach_headset(active_devices, hfp_cmp, 1143 "\r\n+COPS: %d,0,\"%s\"\r\n", 1144 mode, oper); 1145 return 0; 1146 } 1147 1148 static int operator_selection(struct audio_device *device, const char *buf) 1149 { 1150 struct headset *hs = device->headset; 1151 1152 if (strlen(buf) < 8) 1153 return -EINVAL; 1154 1155 switch (buf[7]) { 1156 case '?': 1157 telephony_operator_selection_req(device); 1158 break; 1159 case '=': 1160 return headset_send(hs, "\r\nOK\r\n"); 1161 default: 1162 return -EINVAL; 1163 } 1164 1165 return 0; 1166 } 1167 1168 static int nr_and_ec(struct audio_device *device, const char *buf) 1169 { 1170 struct headset *hs = device->headset; 1171 struct headset_slc *slc = hs->slc; 1172 1173 if (strlen(buf) < 9) 1174 return -EINVAL; 1175 1176 if (buf[8] == '0') 1177 slc->nrec_req = FALSE; 1178 else 1179 slc->nrec_req = TRUE; 1180 1181 telephony_nr_and_ec_req(device, slc->nrec_req); 1182 1183 return 0; 1184 } 1185 1186 static int voice_dial(struct audio_device *device, const char *buf) 1187 { 1188 gboolean enable; 1189 1190 if (strlen(buf) < 9) 1191 return -EINVAL; 1192 1193 if (buf[8] == '0') 1194 enable = FALSE; 1195 else 1196 enable = TRUE; 1197 1198 telephony_voice_dial_req(device, enable); 1199 1200 return 0; 1201 } 1202 1203 static int apple_command(struct audio_device *device, const char *buf) 1204 { 1205 DBG("Got Apple command: %s", buf); 1206 1207 return telephony_generic_rsp(device, CME_ERROR_NONE); 1208 } 1209 1210 static struct event event_callbacks[] = { 1211 { "ATA", answer_call }, 1212 { "ATD", dial_number }, 1213 { "AT+VG", signal_gain_setting }, 1214 { "AT+BRSF", supported_features }, 1215 { "AT+CIND", report_indicators }, 1216 { "AT+CMER", event_reporting }, 1217 { "AT+CHLD", call_hold }, 1218 { "AT+CHUP", terminate_call }, 1219 { "AT+CKPD", key_press }, 1220 { "AT+CLIP", cli_notification }, 1221 { "AT+BTRH", response_and_hold }, 1222 { "AT+BLDN", last_dialed_number }, 1223 { "AT+VTS", dtmf_tone }, 1224 { "AT+CNUM", subscriber_number }, 1225 { "AT+CLCC", list_current_calls }, 1226 { "AT+CMEE", extended_errors }, 1227 { "AT+CCWA", call_waiting_notify }, 1228 { "AT+COPS", operator_selection }, 1229 { "AT+NREC", nr_and_ec }, 1230 { "AT+BVRA", voice_dial }, 1231 { "AT+XAPL", apple_command }, 1232 { "AT+IPHONEACCEV", apple_command }, 1233 { 0 } 1234 }; 1235 1236 static int handle_event(struct audio_device *device, const char *buf) 1237 { 1238 struct event *ev; 1239 1240 DBG("Received %s", buf); 1241 1242 for (ev = event_callbacks; ev->cmd; ev++) { 1243 if (!strncmp(buf, ev->cmd, strlen(ev->cmd))) 1244 return ev->callback(device, buf); 1245 } 1246 1247 return -EINVAL; 1248 } 1249 1250 static void close_sco(struct audio_device *device) 1251 { 1252 struct headset *hs = device->headset; 1253 1254 if (hs->sco) { 1255 int sock = g_io_channel_unix_get_fd(hs->sco); 1256 shutdown(sock, SHUT_RDWR); 1257 g_io_channel_shutdown(hs->sco, TRUE, NULL); 1258 g_io_channel_unref(hs->sco); 1259 hs->sco = NULL; 1260 } 1261 1262 if (hs->sco_id) { 1263 g_source_remove(hs->sco_id); 1264 hs->sco_id = 0; 1265 } 1266 } 1267 1268 static gboolean rfcomm_io_cb(GIOChannel *chan, GIOCondition cond, 1269 struct audio_device *device) 1270 { 1271 struct headset *hs; 1272 struct headset_slc *slc; 1273 unsigned char buf[BUF_SIZE]; 1274 ssize_t bytes_read; 1275 size_t free_space; 1276 int fd; 1277 1278 if (cond & G_IO_NVAL) 1279 return FALSE; 1280 1281 hs = device->headset; 1282 slc = hs->slc; 1283 1284 if (cond & (G_IO_ERR | G_IO_HUP)) { 1285 DBG("ERR or HUP on RFCOMM socket"); 1286 goto failed; 1287 } 1288 1289 fd = g_io_channel_unix_get_fd(chan); 1290 1291 bytes_read = read(fd, buf, sizeof(buf) - 1); 1292 if (bytes_read < 0) 1293 return TRUE; 1294 1295 free_space = sizeof(slc->buf) - slc->data_start - 1296 slc->data_length - 1; 1297 1298 if (free_space < (size_t) bytes_read) { 1299 /* Very likely that the HS is sending us garbage so 1300 * just ignore the data and disconnect */ 1301 error("Too much data to fit incomming buffer"); 1302 goto failed; 1303 } 1304 1305 memcpy(&slc->buf[slc->data_start], buf, bytes_read); 1306 slc->data_length += bytes_read; 1307 1308 /* Make sure the data is null terminated so we can use string 1309 * functions */ 1310 slc->buf[slc->data_start + slc->data_length] = '\0'; 1311 1312 while (slc->data_length > 0) { 1313 char *cr; 1314 int err; 1315 off_t cmd_len; 1316 1317 cr = strchr(&slc->buf[slc->data_start], '\r'); 1318 if (!cr) 1319 break; 1320 1321 cmd_len = 1 + (off_t) cr - (off_t) &slc->buf[slc->data_start]; 1322 *cr = '\0'; 1323 1324 if (cmd_len > 1) 1325 err = handle_event(device, &slc->buf[slc->data_start]); 1326 else 1327 /* Silently skip empty commands */ 1328 err = 0; 1329 1330 if (err == -EINVAL) { 1331 error("Badly formated or unrecognized command: %s", 1332 &slc->buf[slc->data_start]); 1333 err = headset_send(hs, "\r\nERROR\r\n"); 1334 if (err < 0) 1335 goto failed; 1336 } else if (err < 0) 1337 error("Error handling command %s: %s (%d)", 1338 &slc->buf[slc->data_start], 1339 strerror(-err), -err); 1340 1341 slc->data_start += cmd_len; 1342 slc->data_length -= cmd_len; 1343 1344 if (!slc->data_length) 1345 slc->data_start = 0; 1346 } 1347 1348 return TRUE; 1349 1350 failed: 1351 headset_set_state(device, HEADSET_STATE_DISCONNECTED); 1352 1353 return FALSE; 1354 } 1355 1356 static gboolean sco_cb(GIOChannel *chan, GIOCondition cond, 1357 struct audio_device *device) 1358 { 1359 if (cond & G_IO_NVAL) 1360 return FALSE; 1361 1362 error("Audio connection got disconnected"); 1363 1364 pending_connect_finalize(device); 1365 headset_set_state(device, HEADSET_STATE_CONNECTED); 1366 1367 return FALSE; 1368 } 1369 1370 void headset_connect_cb(GIOChannel *chan, GError *err, gpointer user_data) 1371 { 1372 struct audio_device *dev = user_data; 1373 struct headset *hs = dev->headset; 1374 struct pending_connect *p = hs->pending; 1375 char hs_address[18]; 1376 1377 if (err) { 1378 error("%s", err->message); 1379 goto failed; 1380 } 1381 1382 /* For HFP telephony isn't ready just disconnect */ 1383 if (hs->hfp_active && !ag.telephony_ready) { 1384 error("Unable to accept HFP connection since the telephony " 1385 "subsystem isn't initialized"); 1386 goto failed; 1387 } 1388 1389 hs->rfcomm = hs->tmp_rfcomm; 1390 hs->tmp_rfcomm = NULL; 1391 1392 ba2str(&dev->dst, hs_address); 1393 1394 if (p) 1395 p->io = NULL; 1396 else 1397 hs->auto_dc = FALSE; 1398 1399 g_io_add_watch(chan, G_IO_IN | G_IO_ERR | G_IO_HUP| G_IO_NVAL, 1400 (GIOFunc) rfcomm_io_cb, dev); 1401 1402 DBG("%s: Connected to %s", dev->path, hs_address); 1403 1404 hs->slc = g_new0(struct headset_slc, 1); 1405 hs->slc->sp_gain = 15; 1406 hs->slc->mic_gain = 15; 1407 hs->slc->nrec = TRUE; 1408 1409 /* In HFP mode wait for Service Level Connection */ 1410 if (hs->hfp_active) 1411 return; 1412 1413 headset_set_state(dev, HEADSET_STATE_CONNECTED); 1414 1415 if (p && p->target_state == HEADSET_STATE_PLAYING) { 1416 p->err = sco_connect(dev, NULL, NULL, NULL); 1417 if (p->err < 0) 1418 goto failed; 1419 return; 1420 } 1421 1422 if (p && p->msg) { 1423 DBusMessage *reply = dbus_message_new_method_return(p->msg); 1424 g_dbus_send_message(dev->conn, reply); 1425 } 1426 1427 pending_connect_finalize(dev); 1428 1429 return; 1430 1431 failed: 1432 if (p && p->msg) 1433 error_connect_failed(dev->conn, p->msg, p->err); 1434 pending_connect_finalize(dev); 1435 if (hs->rfcomm) 1436 headset_set_state(dev, HEADSET_STATE_CONNECTED); 1437 else 1438 headset_set_state(dev, HEADSET_STATE_DISCONNECTED); 1439 } 1440 1441 static int headset_set_channel(struct headset *headset, 1442 const sdp_record_t *record, uint16_t svc) 1443 { 1444 int ch; 1445 sdp_list_t *protos; 1446 1447 if (sdp_get_access_protos(record, &protos) < 0) { 1448 error("Unable to get access protos from headset record"); 1449 return -1; 1450 } 1451 1452 ch = sdp_get_proto_port(protos, RFCOMM_UUID); 1453 1454 sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free, NULL); 1455 sdp_list_free(protos, NULL); 1456 1457 if (ch <= 0) { 1458 error("Unable to get RFCOMM channel from Headset record"); 1459 return -1; 1460 } 1461 1462 headset->rfcomm_ch = ch; 1463 1464 if (svc == HANDSFREE_SVCLASS_ID) { 1465 headset->hfp_handle = record->handle; 1466 DBG("Discovered Handsfree service on channel %d", ch); 1467 } else { 1468 headset->hsp_handle = record->handle; 1469 DBG("Discovered Headset service on channel %d", ch); 1470 } 1471 1472 return 0; 1473 } 1474 1475 static void get_record_cb(sdp_list_t *recs, int err, gpointer user_data) 1476 { 1477 struct audio_device *dev = user_data; 1478 struct headset *hs = dev->headset; 1479 struct pending_connect *p = hs->pending; 1480 sdp_record_t *record = NULL; 1481 sdp_list_t *r; 1482 uuid_t uuid; 1483 1484 assert(hs->pending != NULL); 1485 1486 if (err < 0) { 1487 error("Unable to get service record: %s (%d)", 1488 strerror(-err), -err); 1489 p->err = -err; 1490 if (p->msg) 1491 error_connect_failed(dev->conn, p->msg, p->err); 1492 goto failed; 1493 } 1494 1495 if (!recs || !recs->data) { 1496 error("No records found"); 1497 goto failed_not_supported; 1498 } 1499 1500 sdp_uuid16_create(&uuid, p->svclass); 1501 1502 for (r = recs; r != NULL; r = r->next) { 1503 sdp_list_t *classes; 1504 uuid_t class; 1505 1506 record = r->data; 1507 1508 if (sdp_get_service_classes(record, &classes) < 0) { 1509 error("Unable to get service classes from record"); 1510 continue; 1511 } 1512 1513 memcpy(&class, classes->data, sizeof(uuid)); 1514 1515 sdp_list_free(classes, free); 1516 1517 1518 if (sdp_uuid_cmp(&class, &uuid) == 0) 1519 break; 1520 } 1521 1522 if (r == NULL) { 1523 error("No record found with UUID 0x%04x", p->svclass); 1524 goto failed_not_supported; 1525 } 1526 1527 if (headset_set_channel(hs, record, p->svclass) < 0) { 1528 error("Unable to extract RFCOMM channel from service record"); 1529 goto failed_not_supported; 1530 } 1531 1532 /* Set svclass to 0 so we can easily check that SDP is no-longer 1533 * going on (to know if bt_cancel_discovery needs to be called) */ 1534 p->svclass = 0; 1535 1536 err = rfcomm_connect(dev, NULL, NULL, NULL); 1537 if (err < 0) { 1538 error("Unable to connect: %s (%d)", strerror(-err), -err); 1539 p->err = -err; 1540 error_connect_failed(dev->conn, p->msg, p->err); 1541 goto failed; 1542 } 1543 1544 return; 1545 1546 failed_not_supported: 1547 if (p->svclass == HANDSFREE_SVCLASS_ID && 1548 get_records(dev, NULL, NULL, NULL) == 0) 1549 return; 1550 if (p->msg) { 1551 DBusMessage *reply = btd_error_not_supported(p->msg); 1552 g_dbus_send_message(dev->conn, reply); 1553 } 1554 failed: 1555 p->svclass = 0; 1556 pending_connect_finalize(dev); 1557 headset_set_state(dev, HEADSET_STATE_DISCONNECTED); 1558 } 1559 1560 static int get_records(struct audio_device *device, headset_stream_cb_t cb, 1561 void *user_data, unsigned int *cb_id) 1562 { 1563 struct headset *hs = device->headset; 1564 uint16_t svclass; 1565 uuid_t uuid; 1566 int err; 1567 1568 if (hs->pending && hs->pending->svclass == HANDSFREE_SVCLASS_ID) 1569 svclass = HEADSET_SVCLASS_ID; 1570 else 1571 svclass = hs->search_hfp ? HANDSFREE_SVCLASS_ID : 1572 HEADSET_SVCLASS_ID; 1573 1574 sdp_uuid16_create(&uuid, svclass); 1575 1576 err = bt_search_service(&device->src, &device->dst, &uuid, 1577 get_record_cb, device, NULL); 1578 if (err < 0) 1579 return err; 1580 1581 if (hs->pending) { 1582 hs->pending->svclass = svclass; 1583 return 0; 1584 } 1585 1586 headset_set_state(device, HEADSET_STATE_CONNECTING); 1587 1588 pending_connect_init(hs, HEADSET_STATE_CONNECTED); 1589 1590 hs->pending->svclass = svclass; 1591 1592 if (cb) { 1593 unsigned int id; 1594 id = connect_cb_new(hs, HEADSET_STATE_CONNECTED, 1595 cb, user_data); 1596 if (cb_id) 1597 *cb_id = id; 1598 } 1599 1600 return 0; 1601 } 1602 1603 static int rfcomm_connect(struct audio_device *dev, headset_stream_cb_t cb, 1604 void *user_data, unsigned int *cb_id) 1605 { 1606 struct headset *hs = dev->headset; 1607 char address[18]; 1608 GError *err = NULL; 1609 1610 if (!manager_allow_headset_connection(dev)) 1611 return -ECONNREFUSED; 1612 1613 if (hs->rfcomm_ch < 0) 1614 return get_records(dev, cb, user_data, cb_id); 1615 1616 ba2str(&dev->dst, address); 1617 1618 DBG("%s: Connecting to %s channel %d", dev->path, address, 1619 hs->rfcomm_ch); 1620 1621 hs->tmp_rfcomm = bt_io_connect(BT_IO_RFCOMM, headset_connect_cb, dev, 1622 NULL, &err, 1623 BT_IO_OPT_SOURCE_BDADDR, &dev->src, 1624 BT_IO_OPT_DEST_BDADDR, &dev->dst, 1625 BT_IO_OPT_CHANNEL, hs->rfcomm_ch, 1626 BT_IO_OPT_INVALID); 1627 1628 hs->rfcomm_ch = -1; 1629 1630 if (!hs->tmp_rfcomm) { 1631 error("%s", err->message); 1632 g_error_free(err); 1633 return -EIO; 1634 } 1635 1636 hs->hfp_active = hs->hfp_handle != 0 ? TRUE : FALSE; 1637 1638 headset_set_state(dev, HEADSET_STATE_CONNECTING); 1639 1640 pending_connect_init(hs, HEADSET_STATE_CONNECTED); 1641 1642 if (cb) { 1643 unsigned int id = connect_cb_new(hs, HEADSET_STATE_CONNECTED, 1644 cb, user_data); 1645 if (cb_id) 1646 *cb_id = id; 1647 } 1648 1649 return 0; 1650 } 1651 1652 static DBusMessage *hs_stop(DBusConnection *conn, DBusMessage *msg, 1653 void *data) 1654 { 1655 struct audio_device *device = data; 1656 struct headset *hs = device->headset; 1657 DBusMessage *reply = NULL; 1658 1659 if (hs->state < HEADSET_STATE_PLAY_IN_PROGRESS) 1660 return btd_error_not_connected(msg); 1661 1662 reply = dbus_message_new_method_return(msg); 1663 if (!reply) 1664 return NULL; 1665 1666 headset_set_state(device, HEADSET_STATE_CONNECTED); 1667 1668 return reply; 1669 } 1670 1671 static DBusMessage *hs_is_playing(DBusConnection *conn, DBusMessage *msg, 1672 void *data) 1673 { 1674 struct audio_device *device = data; 1675 struct headset *hs = device->headset; 1676 DBusMessage *reply; 1677 dbus_bool_t playing; 1678 1679 reply = dbus_message_new_method_return(msg); 1680 if (!reply) 1681 return NULL; 1682 1683 playing = (hs->state == HEADSET_STATE_PLAYING); 1684 1685 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &playing, 1686 DBUS_TYPE_INVALID); 1687 1688 return reply; 1689 } 1690 1691 static DBusMessage *hs_disconnect(DBusConnection *conn, DBusMessage *msg, 1692 void *data) 1693 { 1694 struct audio_device *device = data; 1695 struct headset *hs = device->headset; 1696 char hs_address[18]; 1697 1698 if (hs->state == HEADSET_STATE_DISCONNECTED) 1699 return btd_error_not_connected(msg); 1700 1701 headset_shutdown(device); 1702 ba2str(&device->dst, hs_address); 1703 info("Disconnected from %s, %s", hs_address, device->path); 1704 1705 return dbus_message_new_method_return(msg); 1706 1707 } 1708 1709 static DBusMessage *hs_is_connected(DBusConnection *conn, 1710 DBusMessage *msg, 1711 void *data) 1712 { 1713 struct audio_device *device = data; 1714 DBusMessage *reply; 1715 dbus_bool_t connected; 1716 1717 reply = dbus_message_new_method_return(msg); 1718 if (!reply) 1719 return NULL; 1720 1721 connected = (device->headset->state >= HEADSET_STATE_CONNECTED); 1722 1723 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &connected, 1724 DBUS_TYPE_INVALID); 1725 1726 return reply; 1727 } 1728 1729 static DBusMessage *hs_connect(DBusConnection *conn, DBusMessage *msg, 1730 void *data) 1731 { 1732 struct audio_device *device = data; 1733 struct headset *hs = device->headset; 1734 int err; 1735 1736 if (hs->state == HEADSET_STATE_CONNECTING) 1737 return btd_error_in_progress(msg); 1738 else if (hs->state > HEADSET_STATE_CONNECTING) 1739 return btd_error_already_connected(msg); 1740 1741 if (hs->hfp_handle && !ag.telephony_ready) 1742 return btd_error_not_ready(msg); 1743 1744 device->auto_connect = FALSE; 1745 1746 err = rfcomm_connect(device, NULL, NULL, NULL); 1747 if (err < 0) 1748 return btd_error_failed(msg, strerror(-err)); 1749 1750 hs->auto_dc = FALSE; 1751 1752 hs->pending->msg = dbus_message_ref(msg); 1753 1754 return NULL; 1755 } 1756 1757 static DBusMessage *hs_ring(DBusConnection *conn, DBusMessage *msg, 1758 void *data) 1759 { 1760 struct audio_device *device = data; 1761 struct headset *hs = device->headset; 1762 DBusMessage *reply = NULL; 1763 int err; 1764 1765 if (hs->state < HEADSET_STATE_CONNECTED) 1766 return btd_error_not_connected(msg); 1767 1768 reply = dbus_message_new_method_return(msg); 1769 if (!reply) 1770 return NULL; 1771 1772 if (ag.ring_timer) { 1773 DBG("IndicateCall received when already indicating"); 1774 goto done; 1775 } 1776 1777 err = headset_send(hs, "\r\nRING\r\n"); 1778 if (err < 0) { 1779 dbus_message_unref(reply); 1780 return btd_error_failed(msg, strerror(-err)); 1781 } 1782 1783 ring_timer_cb(NULL); 1784 ag.ring_timer = g_timeout_add_seconds(RING_INTERVAL, ring_timer_cb, 1785 NULL); 1786 1787 done: 1788 return reply; 1789 } 1790 1791 static DBusMessage *hs_cancel_call(DBusConnection *conn, 1792 DBusMessage *msg, 1793 void *data) 1794 { 1795 struct audio_device *device = data; 1796 struct headset *hs = device->headset; 1797 DBusMessage *reply = NULL; 1798 1799 if (hs->state < HEADSET_STATE_CONNECTED) 1800 return btd_error_not_connected(msg); 1801 1802 reply = dbus_message_new_method_return(msg); 1803 if (!reply) 1804 return NULL; 1805 1806 if (ag.ring_timer) { 1807 g_source_remove(ag.ring_timer); 1808 ag.ring_timer = 0; 1809 } else 1810 DBG("Got CancelCall method call but no call is active"); 1811 1812 return reply; 1813 } 1814 1815 static DBusMessage *hs_play(DBusConnection *conn, DBusMessage *msg, 1816 void *data) 1817 { 1818 struct audio_device *device = data; 1819 struct headset *hs = device->headset; 1820 int err; 1821 1822 if (sco_hci) { 1823 error("Refusing Headset.Play() because SCO HCI routing " 1824 "is enabled"); 1825 return btd_error_not_available(msg); 1826 } 1827 1828 switch (hs->state) { 1829 case HEADSET_STATE_DISCONNECTED: 1830 case HEADSET_STATE_CONNECTING: 1831 return btd_error_not_connected(msg); 1832 case HEADSET_STATE_PLAY_IN_PROGRESS: 1833 if (hs->pending && hs->pending->msg == NULL) { 1834 hs->pending->msg = dbus_message_ref(msg); 1835 return NULL; 1836 } 1837 return btd_error_busy(msg); 1838 case HEADSET_STATE_PLAYING: 1839 return btd_error_already_connected(msg); 1840 case HEADSET_STATE_CONNECTED: 1841 default: 1842 break; 1843 } 1844 1845 err = sco_connect(device, NULL, NULL, NULL); 1846 if (err < 0) 1847 return btd_error_failed(msg, strerror(-err)); 1848 1849 hs->pending->msg = dbus_message_ref(msg); 1850 1851 return NULL; 1852 } 1853 1854 static DBusMessage *hs_get_speaker_gain(DBusConnection *conn, 1855 DBusMessage *msg, 1856 void *data) 1857 { 1858 struct audio_device *device = data; 1859 struct headset *hs = device->headset; 1860 struct headset_slc *slc = hs->slc; 1861 DBusMessage *reply; 1862 dbus_uint16_t gain; 1863 1864 if (hs->state < HEADSET_STATE_CONNECTED) 1865 return btd_error_not_available(msg); 1866 1867 reply = dbus_message_new_method_return(msg); 1868 if (!reply) 1869 return NULL; 1870 1871 gain = (dbus_uint16_t) slc->sp_gain; 1872 1873 dbus_message_append_args(reply, DBUS_TYPE_UINT16, &gain, 1874 DBUS_TYPE_INVALID); 1875 1876 return reply; 1877 } 1878 1879 static DBusMessage *hs_get_mic_gain(DBusConnection *conn, 1880 DBusMessage *msg, 1881 void *data) 1882 { 1883 struct audio_device *device = data; 1884 struct headset *hs = device->headset; 1885 struct headset_slc *slc = hs->slc; 1886 DBusMessage *reply; 1887 dbus_uint16_t gain; 1888 1889 if (hs->state < HEADSET_STATE_CONNECTED || slc == NULL) 1890 return btd_error_not_available(msg); 1891 1892 reply = dbus_message_new_method_return(msg); 1893 if (!reply) 1894 return NULL; 1895 1896 gain = (dbus_uint16_t) slc->mic_gain; 1897 1898 dbus_message_append_args(reply, DBUS_TYPE_UINT16, &gain, 1899 DBUS_TYPE_INVALID); 1900 1901 return reply; 1902 } 1903 1904 static DBusMessage *hs_set_gain(DBusConnection *conn, 1905 DBusMessage *msg, 1906 void *data, uint16_t gain, 1907 char type) 1908 { 1909 struct audio_device *device = data; 1910 struct headset *hs = device->headset; 1911 DBusMessage *reply; 1912 int err; 1913 1914 if (hs->state < HEADSET_STATE_CONNECTED) 1915 return btd_error_not_connected(msg); 1916 1917 err = headset_set_gain(device, gain, type); 1918 if (err < 0) 1919 return btd_error_invalid_args(msg); 1920 1921 reply = dbus_message_new_method_return(msg); 1922 if (!reply) 1923 return NULL; 1924 1925 if (hs->state == HEADSET_STATE_PLAYING) { 1926 err = headset_send(hs, "\r\n+VG%c=%u\r\n", type, gain); 1927 if (err < 0) { 1928 dbus_message_unref(reply); 1929 return btd_error_failed(msg, strerror(-err)); 1930 } 1931 } 1932 1933 return reply; 1934 } 1935 1936 static DBusMessage *hs_set_speaker_gain(DBusConnection *conn, 1937 DBusMessage *msg, 1938 void *data) 1939 { 1940 uint16_t gain; 1941 1942 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_UINT16, &gain, 1943 DBUS_TYPE_INVALID)) 1944 return NULL; 1945 1946 return hs_set_gain(conn, msg, data, gain, HEADSET_GAIN_SPEAKER); 1947 } 1948 1949 static DBusMessage *hs_set_mic_gain(DBusConnection *conn, 1950 DBusMessage *msg, 1951 void *data) 1952 { 1953 uint16_t gain; 1954 1955 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_UINT16, &gain, 1956 DBUS_TYPE_INVALID)) 1957 return NULL; 1958 1959 return hs_set_gain(conn, msg, data, gain, HEADSET_GAIN_MICROPHONE); 1960 } 1961 1962 static DBusMessage *hs_get_properties(DBusConnection *conn, 1963 DBusMessage *msg, void *data) 1964 { 1965 struct audio_device *device = data; 1966 DBusMessage *reply; 1967 DBusMessageIter iter; 1968 DBusMessageIter dict; 1969 gboolean value; 1970 const char *state; 1971 1972 reply = dbus_message_new_method_return(msg); 1973 if (!reply) 1974 return NULL; 1975 1976 dbus_message_iter_init_append(reply, &iter); 1977 1978 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, 1979 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING 1980 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING 1981 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict); 1982 1983 1984 /* Playing */ 1985 value = (device->headset->state == HEADSET_STATE_PLAYING); 1986 dict_append_entry(&dict, "Playing", DBUS_TYPE_BOOLEAN, &value); 1987 1988 /* State */ 1989 state = state2str(device->headset->state); 1990 if (state) 1991 dict_append_entry(&dict, "State", DBUS_TYPE_STRING, &state); 1992 1993 /* Connected */ 1994 value = (device->headset->state >= HEADSET_STATE_CONNECTED); 1995 dict_append_entry(&dict, "Connected", DBUS_TYPE_BOOLEAN, &value); 1996 1997 if (!value) 1998 goto done; 1999 2000 /* SpeakerGain */ 2001 dict_append_entry(&dict, "SpeakerGain", 2002 DBUS_TYPE_UINT16, 2003 &device->headset->slc->sp_gain); 2004 2005 /* MicrophoneGain */ 2006 dict_append_entry(&dict, "MicrophoneGain", 2007 DBUS_TYPE_UINT16, 2008 &device->headset->slc->mic_gain); 2009 2010 done: 2011 dbus_message_iter_close_container(&iter, &dict); 2012 2013 return reply; 2014 } 2015 2016 static DBusMessage *hs_set_property(DBusConnection *conn, 2017 DBusMessage *msg, void *data) 2018 { 2019 const char *property; 2020 DBusMessageIter iter; 2021 DBusMessageIter sub; 2022 uint16_t gain; 2023 2024 if (!dbus_message_iter_init(msg, &iter)) 2025 return btd_error_invalid_args(msg); 2026 2027 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) 2028 return btd_error_invalid_args(msg); 2029 2030 dbus_message_iter_get_basic(&iter, &property); 2031 dbus_message_iter_next(&iter); 2032 2033 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) 2034 return btd_error_invalid_args(msg); 2035 dbus_message_iter_recurse(&iter, &sub); 2036 2037 if (g_str_equal("SpeakerGain", property)) { 2038 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT16) 2039 return btd_error_invalid_args(msg); 2040 2041 dbus_message_iter_get_basic(&sub, &gain); 2042 return hs_set_gain(conn, msg, data, gain, 2043 HEADSET_GAIN_SPEAKER); 2044 } else if (g_str_equal("MicrophoneGain", property)) { 2045 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT16) 2046 return btd_error_invalid_args(msg); 2047 2048 dbus_message_iter_get_basic(&sub, &gain); 2049 return hs_set_gain(conn, msg, data, gain, 2050 HEADSET_GAIN_MICROPHONE); 2051 } 2052 2053 return btd_error_invalid_args(msg); 2054 } 2055 static GDBusMethodTable headset_methods[] = { 2056 { "Connect", "", "", hs_connect, 2057 G_DBUS_METHOD_FLAG_ASYNC }, 2058 { "Disconnect", "", "", hs_disconnect }, 2059 { "IsConnected", "", "b", hs_is_connected }, 2060 { "IndicateCall", "", "", hs_ring }, 2061 { "CancelCall", "", "", hs_cancel_call }, 2062 { "Play", "", "", hs_play, 2063 G_DBUS_METHOD_FLAG_ASYNC }, 2064 { "Stop", "", "", hs_stop }, 2065 { "IsPlaying", "", "b", hs_is_playing, 2066 G_DBUS_METHOD_FLAG_DEPRECATED }, 2067 { "GetSpeakerGain", "", "q", hs_get_speaker_gain, 2068 G_DBUS_METHOD_FLAG_DEPRECATED }, 2069 { "GetMicrophoneGain", "", "q", hs_get_mic_gain, 2070 G_DBUS_METHOD_FLAG_DEPRECATED }, 2071 { "SetSpeakerGain", "q", "", hs_set_speaker_gain, 2072 G_DBUS_METHOD_FLAG_DEPRECATED }, 2073 { "SetMicrophoneGain", "q", "", hs_set_mic_gain, 2074 G_DBUS_METHOD_FLAG_DEPRECATED }, 2075 { "GetProperties", "", "a{sv}",hs_get_properties }, 2076 { "SetProperty", "sv", "", hs_set_property }, 2077 { NULL, NULL, NULL, NULL } 2078 }; 2079 2080 static GDBusSignalTable headset_signals[] = { 2081 { "Connected", "", G_DBUS_SIGNAL_FLAG_DEPRECATED }, 2082 { "Disconnected", "", G_DBUS_SIGNAL_FLAG_DEPRECATED }, 2083 { "AnswerRequested", "" }, 2084 { "Stopped", "", G_DBUS_SIGNAL_FLAG_DEPRECATED }, 2085 { "Playing", "", G_DBUS_SIGNAL_FLAG_DEPRECATED }, 2086 { "SpeakerGainChanged", "q", G_DBUS_SIGNAL_FLAG_DEPRECATED }, 2087 { "MicrophoneGainChanged", "q", G_DBUS_SIGNAL_FLAG_DEPRECATED }, 2088 { "CallTerminated", "" }, 2089 { "PropertyChanged", "sv" }, 2090 { NULL, NULL } 2091 }; 2092 2093 void headset_update(struct audio_device *dev, uint16_t svc, 2094 const char *uuidstr) 2095 { 2096 struct headset *headset = dev->headset; 2097 const sdp_record_t *record; 2098 2099 record = btd_device_get_record(dev->btd_dev, uuidstr); 2100 if (!record) 2101 return; 2102 2103 switch (svc) { 2104 case HANDSFREE_SVCLASS_ID: 2105 if (headset->hfp_handle && 2106 (headset->hfp_handle != record->handle)) { 2107 error("More than one HFP record found on device"); 2108 return; 2109 } 2110 2111 headset->hfp_handle = record->handle; 2112 break; 2113 2114 case HEADSET_SVCLASS_ID: 2115 if (headset->hsp_handle && 2116 (headset->hsp_handle != record->handle)) { 2117 error("More than one HSP record found on device"); 2118 return; 2119 } 2120 2121 headset->hsp_handle = record->handle; 2122 2123 /* Ignore this record if we already have access to HFP */ 2124 if (headset->hfp_handle) 2125 return; 2126 2127 break; 2128 2129 default: 2130 DBG("Invalid record passed to headset_update"); 2131 return; 2132 } 2133 } 2134 2135 static int headset_close_rfcomm(struct audio_device *dev) 2136 { 2137 struct headset *hs = dev->headset; 2138 GIOChannel *rfcomm = hs->tmp_rfcomm ? hs->tmp_rfcomm : hs->rfcomm; 2139 2140 if (rfcomm) { 2141 g_io_channel_shutdown(rfcomm, TRUE, NULL); 2142 g_io_channel_unref(rfcomm); 2143 hs->tmp_rfcomm = NULL; 2144 hs->rfcomm = NULL; 2145 } 2146 2147 g_free(hs->slc); 2148 hs->slc = NULL; 2149 2150 return 0; 2151 } 2152 2153 static void headset_free(struct audio_device *dev) 2154 { 2155 struct headset *hs = dev->headset; 2156 2157 if (hs->dc_timer) { 2158 g_source_remove(hs->dc_timer); 2159 hs->dc_timer = 0; 2160 } 2161 2162 close_sco(dev); 2163 2164 headset_close_rfcomm(dev); 2165 2166 g_slist_foreach(hs->nrec_cbs, (GFunc) g_free, NULL); 2167 g_slist_free(hs->nrec_cbs); 2168 2169 g_free(hs); 2170 dev->headset = NULL; 2171 } 2172 2173 static void path_unregister(void *data) 2174 { 2175 struct audio_device *dev = data; 2176 struct headset *hs = dev->headset; 2177 2178 if (hs->state > HEADSET_STATE_DISCONNECTED) { 2179 DBG("Headset unregistered while device was connected!"); 2180 headset_shutdown(dev); 2181 } 2182 2183 DBG("Unregistered interface %s on path %s", 2184 AUDIO_HEADSET_INTERFACE, dev->path); 2185 2186 headset_free(dev); 2187 } 2188 2189 void headset_unregister(struct audio_device *dev) 2190 { 2191 g_dbus_unregister_interface(dev->conn, dev->path, 2192 AUDIO_HEADSET_INTERFACE); 2193 } 2194 2195 struct headset *headset_init(struct audio_device *dev, uint16_t svc, 2196 const char *uuidstr) 2197 { 2198 struct headset *hs; 2199 const sdp_record_t *record; 2200 2201 hs = g_new0(struct headset, 1); 2202 hs->rfcomm_ch = -1; 2203 hs->search_hfp = server_is_enabled(&dev->src, HANDSFREE_SVCLASS_ID); 2204 2205 record = btd_device_get_record(dev->btd_dev, uuidstr); 2206 if (!record) 2207 goto register_iface; 2208 2209 switch (svc) { 2210 case HANDSFREE_SVCLASS_ID: 2211 hs->hfp_handle = record->handle; 2212 break; 2213 2214 case HEADSET_SVCLASS_ID: 2215 hs->hsp_handle = record->handle; 2216 break; 2217 2218 default: 2219 DBG("Invalid record passed to headset_init"); 2220 g_free(hs); 2221 return NULL; 2222 } 2223 2224 register_iface: 2225 if (!g_dbus_register_interface(dev->conn, dev->path, 2226 AUDIO_HEADSET_INTERFACE, 2227 headset_methods, headset_signals, NULL, 2228 dev, path_unregister)) { 2229 g_free(hs); 2230 return NULL; 2231 } 2232 2233 DBG("Registered interface %s on path %s", 2234 AUDIO_HEADSET_INTERFACE, dev->path); 2235 2236 return hs; 2237 } 2238 2239 uint32_t headset_config_init(GKeyFile *config) 2240 { 2241 GError *err = NULL; 2242 char *str; 2243 2244 /* Use the default values if there is no config file */ 2245 if (config == NULL) 2246 return ag.features; 2247 2248 str = g_key_file_get_string(config, "General", "SCORouting", 2249 &err); 2250 if (err) { 2251 DBG("audio.conf: %s", err->message); 2252 g_clear_error(&err); 2253 } else { 2254 if (strcmp(str, "PCM") == 0) 2255 sco_hci = FALSE; 2256 else if (strcmp(str, "HCI") == 0) 2257 sco_hci = TRUE; 2258 else 2259 error("Invalid Headset Routing value: %s", str); 2260 g_free(str); 2261 } 2262 2263 /* Init fast connectable option */ 2264 str = g_key_file_get_string(config, "Headset", "FastConnectable", 2265 &err); 2266 if (err) { 2267 DBG("audio.conf: %s", err->message); 2268 g_clear_error(&err); 2269 } else { 2270 fast_connectable = strcmp(str, "true") == 0; 2271 if (fast_connectable) 2272 manager_set_fast_connectable(FALSE); 2273 g_free(str); 2274 } 2275 2276 return ag.features; 2277 } 2278 2279 static gboolean hs_dc_timeout(struct audio_device *dev) 2280 { 2281 headset_set_state(dev, HEADSET_STATE_DISCONNECTED); 2282 return FALSE; 2283 } 2284 2285 gboolean headset_cancel_stream(struct audio_device *dev, unsigned int id) 2286 { 2287 struct headset *hs = dev->headset; 2288 struct pending_connect *p = hs->pending; 2289 GSList *l; 2290 struct connect_cb *cb = NULL; 2291 2292 if (!p) 2293 return FALSE; 2294 2295 for (l = p->callbacks; l != NULL; l = l->next) { 2296 struct connect_cb *tmp = l->data; 2297 2298 if (tmp->id == id) { 2299 cb = tmp; 2300 break; 2301 } 2302 } 2303 2304 if (!cb) 2305 return FALSE; 2306 2307 p->callbacks = g_slist_remove(p->callbacks, cb); 2308 g_free(cb); 2309 2310 if (p->callbacks || p->msg) 2311 return TRUE; 2312 2313 if (hs->auto_dc) { 2314 if (hs->rfcomm) 2315 hs->dc_timer = g_timeout_add_seconds(DC_TIMEOUT, 2316 (GSourceFunc) hs_dc_timeout, 2317 dev); 2318 else 2319 headset_set_state(dev, HEADSET_STATE_DISCONNECTED); 2320 } 2321 2322 return TRUE; 2323 } 2324 2325 static gboolean dummy_connect_complete(struct audio_device *dev) 2326 { 2327 pending_connect_finalize(dev); 2328 return FALSE; 2329 } 2330 2331 unsigned int headset_request_stream(struct audio_device *dev, 2332 headset_stream_cb_t cb, 2333 void *user_data) 2334 { 2335 struct headset *hs = dev->headset; 2336 unsigned int id; 2337 2338 if (hs->state == HEADSET_STATE_PLAYING) { 2339 id = connect_cb_new(hs, HEADSET_STATE_PLAYING, cb, user_data); 2340 g_idle_add((GSourceFunc) dummy_connect_complete, dev); 2341 return id; 2342 } 2343 2344 if (hs->dc_timer) { 2345 g_source_remove(hs->dc_timer); 2346 hs->dc_timer = 0; 2347 } 2348 2349 if (hs->state == HEADSET_STATE_CONNECTING || 2350 hs->state == HEADSET_STATE_PLAY_IN_PROGRESS) 2351 return connect_cb_new(hs, HEADSET_STATE_PLAYING, cb, user_data); 2352 2353 if (hs->rfcomm == NULL) { 2354 if (rfcomm_connect(dev, cb, user_data, &id) < 0) 2355 return 0; 2356 hs->auto_dc = TRUE; 2357 } else if (sco_connect(dev, cb, user_data, &id) < 0) 2358 return 0; 2359 2360 hs->pending->target_state = HEADSET_STATE_PLAYING; 2361 2362 return id; 2363 } 2364 2365 unsigned int headset_config_stream(struct audio_device *dev, 2366 gboolean auto_dc, 2367 headset_stream_cb_t cb, 2368 void *user_data) 2369 { 2370 struct headset *hs = dev->headset; 2371 unsigned int id = 0; 2372 2373 if (hs->dc_timer) { 2374 g_source_remove(hs->dc_timer); 2375 hs->dc_timer = 0; 2376 } 2377 2378 if (hs->state == HEADSET_STATE_CONNECTING) 2379 return connect_cb_new(hs, HEADSET_STATE_CONNECTED, cb, 2380 user_data); 2381 2382 if (hs->rfcomm) 2383 goto done; 2384 2385 if (rfcomm_connect(dev, cb, user_data, &id) < 0) 2386 return 0; 2387 2388 hs->auto_dc = auto_dc; 2389 hs->pending->target_state = HEADSET_STATE_CONNECTED; 2390 2391 return id; 2392 2393 done: 2394 id = connect_cb_new(hs, HEADSET_STATE_CONNECTED, cb, user_data); 2395 g_idle_add((GSourceFunc) dummy_connect_complete, dev); 2396 return id; 2397 } 2398 2399 unsigned int headset_suspend_stream(struct audio_device *dev, 2400 headset_stream_cb_t cb, 2401 void *user_data) 2402 { 2403 struct headset *hs = dev->headset; 2404 unsigned int id; 2405 int sock; 2406 2407 if (hs->state == HEADSET_STATE_DISCONNECTED || 2408 hs->state == HEADSET_STATE_CONNECTING) 2409 return 0; 2410 2411 if (hs->dc_timer) { 2412 g_source_remove(hs->dc_timer); 2413 hs->dc_timer = 0; 2414 } 2415 2416 if (hs->sco) { 2417 sock = g_io_channel_unix_get_fd(hs->sco); 2418 2419 /* shutdown but leave the socket open and wait for hup */ 2420 shutdown(sock, SHUT_RDWR); 2421 } else { 2422 headset_set_state(dev, HEADSET_STATE_CONNECTED); 2423 2424 g_idle_add((GSourceFunc) dummy_connect_complete, dev); 2425 } 2426 2427 id = connect_cb_new(hs, HEADSET_STATE_CONNECTED, cb, user_data); 2428 2429 return id; 2430 } 2431 2432 gboolean get_hfp_active(struct audio_device *dev) 2433 { 2434 struct headset *hs = dev->headset; 2435 2436 return hs->hfp_active; 2437 } 2438 2439 void set_hfp_active(struct audio_device *dev, gboolean active) 2440 { 2441 struct headset *hs = dev->headset; 2442 2443 hs->hfp_active = active; 2444 } 2445 2446 GIOChannel *headset_get_rfcomm(struct audio_device *dev) 2447 { 2448 struct headset *hs = dev->headset; 2449 2450 return hs->tmp_rfcomm; 2451 } 2452 2453 int headset_connect_rfcomm(struct audio_device *dev, GIOChannel *io) 2454 { 2455 struct headset *hs = dev->headset; 2456 2457 if (hs->tmp_rfcomm) 2458 return -EALREADY; 2459 2460 hs->tmp_rfcomm = g_io_channel_ref(io); 2461 2462 return 0; 2463 } 2464 2465 int headset_connect_sco(struct audio_device *dev, GIOChannel *io) 2466 { 2467 struct headset *hs = dev->headset; 2468 struct headset_slc *slc = hs->slc; 2469 2470 if (hs->sco) 2471 return -EISCONN; 2472 2473 hs->sco = g_io_channel_ref(io); 2474 2475 if (slc->pending_ring) { 2476 ring_timer_cb(NULL); 2477 ag.ring_timer = g_timeout_add_seconds(RING_INTERVAL, 2478 ring_timer_cb, 2479 NULL); 2480 slc->pending_ring = FALSE; 2481 } 2482 2483 return 0; 2484 } 2485 2486 void headset_set_state(struct audio_device *dev, headset_state_t state) 2487 { 2488 struct headset *hs = dev->headset; 2489 struct headset_slc *slc = hs->slc; 2490 gboolean value; 2491 const char *state_str; 2492 headset_state_t old_state = hs->state; 2493 GSList *l; 2494 2495 if (old_state == state) 2496 return; 2497 2498 state_str = state2str(state); 2499 2500 switch (state) { 2501 case HEADSET_STATE_DISCONNECTED: 2502 value = FALSE; 2503 close_sco(dev); 2504 headset_close_rfcomm(dev); 2505 emit_property_changed(dev->conn, dev->path, 2506 AUDIO_HEADSET_INTERFACE, "State", 2507 DBUS_TYPE_STRING, &state_str); 2508 g_dbus_emit_signal(dev->conn, dev->path, 2509 AUDIO_HEADSET_INTERFACE, 2510 "Disconnected", 2511 DBUS_TYPE_INVALID); 2512 if (hs->state > HEADSET_STATE_CONNECTING) { 2513 emit_property_changed(dev->conn, dev->path, 2514 AUDIO_HEADSET_INTERFACE, "Connected", 2515 DBUS_TYPE_BOOLEAN, &value); 2516 telephony_device_disconnected(dev); 2517 } 2518 active_devices = g_slist_remove(active_devices, dev); 2519 break; 2520 case HEADSET_STATE_CONNECTING: 2521 emit_property_changed(dev->conn, dev->path, 2522 AUDIO_HEADSET_INTERFACE, "State", 2523 DBUS_TYPE_STRING, &state_str); 2524 break; 2525 case HEADSET_STATE_CONNECTED: 2526 close_sco(dev); 2527 if (hs->state != HEADSET_STATE_PLAY_IN_PROGRESS) 2528 emit_property_changed(dev->conn, dev->path, 2529 AUDIO_HEADSET_INTERFACE, "State", 2530 DBUS_TYPE_STRING, &state_str); 2531 if (hs->state < state) { 2532 if (ag.features & AG_FEATURE_INBAND_RINGTONE) 2533 slc->inband_ring = TRUE; 2534 else 2535 slc->inband_ring = FALSE; 2536 g_dbus_emit_signal(dev->conn, dev->path, 2537 AUDIO_HEADSET_INTERFACE, 2538 "Connected", 2539 DBUS_TYPE_INVALID); 2540 value = TRUE; 2541 emit_property_changed(dev->conn, dev->path, 2542 AUDIO_HEADSET_INTERFACE, 2543 "Connected", 2544 DBUS_TYPE_BOOLEAN, &value); 2545 active_devices = g_slist_append(active_devices, dev); 2546 telephony_device_connected(dev); 2547 } else if (hs->state == HEADSET_STATE_PLAYING) { 2548 value = FALSE; 2549 g_dbus_emit_signal(dev->conn, dev->path, 2550 AUDIO_HEADSET_INTERFACE, 2551 "Stopped", 2552 DBUS_TYPE_INVALID); 2553 emit_property_changed(dev->conn, dev->path, 2554 AUDIO_HEADSET_INTERFACE, 2555 "Playing", 2556 DBUS_TYPE_BOOLEAN, &value); 2557 } 2558 break; 2559 case HEADSET_STATE_PLAY_IN_PROGRESS: 2560 break; 2561 case HEADSET_STATE_PLAYING: 2562 value = TRUE; 2563 emit_property_changed(dev->conn, dev->path, 2564 AUDIO_HEADSET_INTERFACE, "State", 2565 DBUS_TYPE_STRING, &state_str); 2566 2567 /* Do not watch HUP since we need to know when the link is 2568 really disconnected */ 2569 hs->sco_id = g_io_add_watch(hs->sco, 2570 G_IO_ERR | G_IO_NVAL, 2571 (GIOFunc) sco_cb, dev); 2572 2573 g_dbus_emit_signal(dev->conn, dev->path, 2574 AUDIO_HEADSET_INTERFACE, "Playing", 2575 DBUS_TYPE_INVALID); 2576 emit_property_changed(dev->conn, dev->path, 2577 AUDIO_HEADSET_INTERFACE, "Playing", 2578 DBUS_TYPE_BOOLEAN, &value); 2579 2580 if (slc->sp_gain >= 0) 2581 headset_send(hs, "\r\n+VGS=%u\r\n", slc->sp_gain); 2582 if (slc->mic_gain >= 0) 2583 headset_send(hs, "\r\n+VGM=%u\r\n", slc->mic_gain); 2584 break; 2585 } 2586 2587 hs->state = state; 2588 2589 DBG("State changed %s: %s -> %s", dev->path, str_state[old_state], 2590 str_state[state]); 2591 2592 for (l = headset_callbacks; l != NULL; l = l->next) { 2593 struct headset_state_callback *cb = l->data; 2594 cb->cb(dev, old_state, state, cb->user_data); 2595 } 2596 } 2597 2598 headset_state_t headset_get_state(struct audio_device *dev) 2599 { 2600 struct headset *hs = dev->headset; 2601 2602 return hs->state; 2603 } 2604 2605 int headset_get_channel(struct audio_device *dev) 2606 { 2607 struct headset *hs = dev->headset; 2608 2609 return hs->rfcomm_ch; 2610 } 2611 2612 gboolean headset_is_active(struct audio_device *dev) 2613 { 2614 struct headset *hs = dev->headset; 2615 2616 if (hs->state != HEADSET_STATE_DISCONNECTED) 2617 return TRUE; 2618 2619 return FALSE; 2620 } 2621 2622 headset_lock_t headset_get_lock(struct audio_device *dev) 2623 { 2624 struct headset *hs = dev->headset; 2625 2626 return hs->lock; 2627 } 2628 2629 gboolean headset_lock(struct audio_device *dev, headset_lock_t lock) 2630 { 2631 struct headset *hs = dev->headset; 2632 2633 if (hs->lock & lock) 2634 return FALSE; 2635 2636 hs->lock |= lock; 2637 2638 return TRUE; 2639 } 2640 2641 gboolean headset_unlock(struct audio_device *dev, headset_lock_t lock) 2642 { 2643 struct headset *hs = dev->headset; 2644 2645 if (!(hs->lock & lock)) 2646 return FALSE; 2647 2648 hs->lock &= ~lock; 2649 2650 if (hs->lock) 2651 return TRUE; 2652 2653 if (hs->state == HEADSET_STATE_PLAYING) 2654 headset_set_state(dev, HEADSET_STATE_CONNECTED); 2655 2656 if (hs->auto_dc) { 2657 if (hs->state == HEADSET_STATE_CONNECTED) 2658 hs->dc_timer = g_timeout_add_seconds(DC_TIMEOUT, 2659 (GSourceFunc) hs_dc_timeout, 2660 dev); 2661 else 2662 headset_set_state(dev, HEADSET_STATE_DISCONNECTED); 2663 } 2664 2665 return TRUE; 2666 } 2667 2668 gboolean headset_suspend(struct audio_device *dev, void *data) 2669 { 2670 return TRUE; 2671 } 2672 2673 gboolean headset_play(struct audio_device *dev, void *data) 2674 { 2675 return TRUE; 2676 } 2677 2678 int headset_get_sco_fd(struct audio_device *dev) 2679 { 2680 struct headset *hs = dev->headset; 2681 2682 if (!hs->sco) 2683 return -1; 2684 2685 return g_io_channel_unix_get_fd(hs->sco); 2686 } 2687 2688 gboolean headset_get_nrec(struct audio_device *dev) 2689 { 2690 struct headset *hs = dev->headset; 2691 2692 if (!hs->slc) 2693 return TRUE; 2694 2695 return hs->slc->nrec; 2696 } 2697 2698 unsigned int headset_add_nrec_cb(struct audio_device *dev, 2699 headset_nrec_cb cb, void *user_data) 2700 { 2701 struct headset *hs = dev->headset; 2702 struct headset_nrec_callback *nrec_cb; 2703 static unsigned int id = 0; 2704 2705 nrec_cb = g_new(struct headset_nrec_callback, 1); 2706 nrec_cb->cb = cb; 2707 nrec_cb->user_data = user_data; 2708 nrec_cb->id = ++id; 2709 2710 hs->nrec_cbs = g_slist_prepend(hs->nrec_cbs, nrec_cb); 2711 2712 return nrec_cb->id; 2713 } 2714 2715 gboolean headset_remove_nrec_cb(struct audio_device *dev, unsigned int id) 2716 { 2717 struct headset *hs = dev->headset; 2718 GSList *l; 2719 2720 for (l = hs->nrec_cbs; l != NULL; l = l->next) { 2721 struct headset_nrec_callback *cb = l->data; 2722 if (cb && cb->id == id) { 2723 hs->nrec_cbs = g_slist_remove(hs->nrec_cbs, cb); 2724 g_free(cb); 2725 return TRUE; 2726 } 2727 } 2728 2729 return FALSE; 2730 } 2731 2732 gboolean headset_get_inband(struct audio_device *dev) 2733 { 2734 struct headset *hs = dev->headset; 2735 2736 if (!hs->slc) 2737 return TRUE; 2738 2739 return hs->slc->inband_ring; 2740 } 2741 2742 gboolean headset_get_sco_hci(struct audio_device *dev) 2743 { 2744 return sco_hci; 2745 } 2746 2747 void headset_shutdown(struct audio_device *dev) 2748 { 2749 struct pending_connect *p = dev->headset->pending; 2750 2751 if (p && p->msg) 2752 error_connect_failed(dev->conn, p->msg, ECANCELED); 2753 2754 pending_connect_finalize(dev); 2755 headset_set_state(dev, HEADSET_STATE_DISCONNECTED); 2756 } 2757 2758 int telephony_event_ind(int index) 2759 { 2760 if (!active_devices) 2761 return -ENODEV; 2762 2763 if (!ag.er_ind) { 2764 DBG("telephony_report_event called but events are disabled"); 2765 return -EINVAL; 2766 } 2767 2768 send_foreach_headset(active_devices, hfp_cmp, 2769 "\r\n+CIEV: %d,%d\r\n", index + 1, 2770 ag.indicators[index].val); 2771 2772 return 0; 2773 } 2774 2775 int telephony_response_and_hold_ind(int rh) 2776 { 2777 if (!active_devices) 2778 return -ENODEV; 2779 2780 ag.rh = rh; 2781 2782 /* If we aren't in any response and hold state don't send anything */ 2783 if (ag.rh < 0) 2784 return 0; 2785 2786 send_foreach_headset(active_devices, hfp_cmp, "\r\n+BTRH: %d\r\n", 2787 ag.rh); 2788 2789 return 0; 2790 } 2791 2792 int telephony_incoming_call_ind(const char *number, int type) 2793 { 2794 struct audio_device *dev; 2795 struct headset *hs; 2796 struct headset_slc *slc; 2797 2798 if (fast_connectable) 2799 manager_set_fast_connectable(TRUE); 2800 2801 if (!active_devices) 2802 return -ENODEV; 2803 2804 /* Get the latest connected device */ 2805 dev = active_devices->data; 2806 hs = dev->headset; 2807 slc = hs->slc; 2808 2809 if (ag.ring_timer) { 2810 DBG("telephony_incoming_call_ind: already calling"); 2811 return -EBUSY; 2812 } 2813 2814 /* With HSP 1.2 the RING messages should *not* be sent if inband 2815 * ringtone is being used */ 2816 if (!hs->hfp_active && slc->inband_ring) 2817 return 0; 2818 2819 g_free(ag.number); 2820 ag.number = g_strdup(number); 2821 ag.number_type = type; 2822 2823 if (slc->inband_ring && hs->hfp_active && 2824 hs->state != HEADSET_STATE_PLAYING) { 2825 slc->pending_ring = TRUE; 2826 return 0; 2827 } 2828 2829 ring_timer_cb(NULL); 2830 ag.ring_timer = g_timeout_add_seconds(RING_INTERVAL, ring_timer_cb, 2831 NULL); 2832 2833 return 0; 2834 } 2835 2836 int telephony_calling_stopped_ind(void) 2837 { 2838 struct audio_device *dev; 2839 2840 if (fast_connectable) 2841 manager_set_fast_connectable(FALSE); 2842 2843 if (ag.ring_timer) { 2844 g_source_remove(ag.ring_timer); 2845 ag.ring_timer = 0; 2846 } 2847 2848 if (!active_devices) 2849 return 0; 2850 2851 /* In case SCO isn't fully up yet */ 2852 dev = active_devices->data; 2853 2854 if (!dev->headset->slc->pending_ring && !ag.ring_timer) 2855 return -EINVAL; 2856 2857 dev->headset->slc->pending_ring = FALSE; 2858 2859 return 0; 2860 } 2861 2862 int telephony_ready_ind(uint32_t features, 2863 const struct indicator *indicators, int rh, 2864 const char *chld) 2865 { 2866 ag.telephony_ready = TRUE; 2867 ag.features = features; 2868 ag.indicators = indicators; 2869 ag.rh = rh; 2870 ag.chld = chld; 2871 2872 DBG("Telephony plugin initialized"); 2873 2874 print_ag_features(ag.features); 2875 2876 return 0; 2877 } 2878 2879 int telephony_deinit(void) 2880 { 2881 g_free(ag.number); 2882 2883 memset(&ag, 0, sizeof(ag)); 2884 2885 ag.er_mode = 3; 2886 ag.rh = BTRH_NOT_SUPPORTED; 2887 2888 DBG("Telephony deinitialized"); 2889 2890 return 0; 2891 } 2892 2893 int telephony_list_current_call_ind(int idx, int dir, int status, int mode, 2894 int mprty, const char *number, 2895 int type) 2896 { 2897 if (!active_devices) 2898 return -ENODEV; 2899 2900 if (number && strlen(number) > 0) 2901 send_foreach_headset(active_devices, hfp_cmp, 2902 "\r\n+CLCC: %d,%d,%d,%d,%d,\"%s\",%d\r\n", 2903 idx, dir, status, mode, mprty, number, type); 2904 else 2905 send_foreach_headset(active_devices, hfp_cmp, 2906 "\r\n+CLCC: %d,%d,%d,%d,%d\r\n", 2907 idx, dir, status, mode, mprty); 2908 2909 return 0; 2910 } 2911 2912 int telephony_subscriber_number_ind(const char *number, int type, int service) 2913 { 2914 if (!active_devices) 2915 return -ENODEV; 2916 2917 send_foreach_headset(active_devices, hfp_cmp, 2918 "\r\n+CNUM: ,%s,%d,,%d\r\n", 2919 number, type, service); 2920 2921 return 0; 2922 } 2923 2924 static int cwa_cmp(struct headset *hs) 2925 { 2926 if (!hs->hfp_active) 2927 return -1; 2928 2929 if (hs->slc->cwa_enabled) 2930 return 0; 2931 else 2932 return -1; 2933 } 2934 2935 int telephony_call_waiting_ind(const char *number, int type) 2936 { 2937 if (!active_devices) 2938 return -ENODEV; 2939 2940 send_foreach_headset(active_devices, cwa_cmp, 2941 "\r\n+CCWA: \"%s\",%d\r\n", 2942 number, type); 2943 2944 return 0; 2945 } 2946 2947 unsigned int headset_add_state_cb(headset_state_cb cb, void *user_data) 2948 { 2949 struct headset_state_callback *state_cb; 2950 static unsigned int id = 0; 2951 2952 state_cb = g_new(struct headset_state_callback, 1); 2953 state_cb->cb = cb; 2954 state_cb->user_data = user_data; 2955 state_cb->id = ++id; 2956 2957 headset_callbacks = g_slist_append(headset_callbacks, state_cb); 2958 2959 return state_cb->id; 2960 } 2961 2962 gboolean headset_remove_state_cb(unsigned int id) 2963 { 2964 GSList *l; 2965 2966 for (l = headset_callbacks; l != NULL; l = l->next) { 2967 struct headset_state_callback *cb = l->data; 2968 if (cb && cb->id == id) { 2969 headset_callbacks = g_slist_remove(headset_callbacks, cb); 2970 g_free(cb); 2971 return TRUE; 2972 } 2973 } 2974 2975 return FALSE; 2976 } 2977