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