~ubuntu-branches/ubuntu/wily/bluez/wily

« back to all changes in this revision

Viewing changes to audio/manager.c

ImportĀ upstreamĀ versionĀ 4.81

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *
3
3
 *  BlueZ - Bluetooth protocol stack for Linux
4
4
 *
5
 
 *  Copyright (C) 2006-2007  Nokia Corporation
6
 
 *  Copyright (C) 2004-2009  Marcel Holtmann <marcel@holtmann.org>
 
5
 *  Copyright (C) 2006-2010  Nokia Corporation
 
6
 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
7
7
 *
8
8
 *
9
9
 *  This program is free software; you can redistribute it and/or modify
39
39
#include <signal.h>
40
40
 
41
41
#include <bluetooth/bluetooth.h>
42
 
#include <bluetooth/hci.h>
43
 
#include <bluetooth/hci_lib.h>
44
 
#include <bluetooth/rfcomm.h>
45
42
#include <bluetooth/sdp.h>
46
43
#include <bluetooth/sdp_lib.h>
47
44
 
55
52
#include "../src/adapter.h"
56
53
#include "../src/device.h"
57
54
 
58
 
#include "logging.h"
 
55
#include "log.h"
59
56
#include "textfile.h"
60
57
#include "ipc.h"
61
58
#include "device.h"
62
59
#include "error.h"
63
60
#include "avdtp.h"
 
61
#include "media.h"
64
62
#include "a2dp.h"
65
63
#include "headset.h"
66
64
#include "gateway.h"
67
65
#include "sink.h"
 
66
#include "source.h"
68
67
#include "control.h"
69
68
#include "manager.h"
70
69
#include "sdpd.h"
71
70
#include "telephony.h"
 
71
#include "unix.h"
 
72
 
 
73
#ifndef DBUS_TYPE_UNIX_FD
 
74
#define DBUS_TYPE_UNIX_FD -1
 
75
#endif
72
76
 
73
77
typedef enum {
74
78
        HEADSET = 1 << 0,
88
92
} audio_sdp_state_t;
89
93
 
90
94
struct audio_adapter {
91
 
        bdaddr_t src;
92
 
        char    *path;
 
95
        struct btd_adapter *btd_adapter;
 
96
        gboolean powered;
93
97
        uint32_t hsp_ag_record_id;
94
98
        uint32_t hfp_ag_record_id;
95
 
        uint32_t hsp_hs_record_id;
 
99
        uint32_t hfp_hs_record_id;
96
100
        GIOChannel *hsp_ag_server;
97
101
        GIOChannel *hfp_ag_server;
98
 
        GIOChannel *hsp_hs_server;
 
102
        GIOChannel *hfp_hs_server;
 
103
        gint ref;
99
104
};
100
105
 
101
106
static gboolean auto_connect = TRUE;
112
117
        .sink           = TRUE,
113
118
        .source         = FALSE,
114
119
        .control        = TRUE,
 
120
        .socket         = TRUE,
 
121
        .media          = FALSE
115
122
};
116
123
 
117
 
static struct audio_adapter *find_adapter(GSList *list, const char *path)
 
124
static struct audio_adapter *find_adapter(GSList *list,
 
125
                                        struct btd_adapter *btd_adapter)
118
126
{
119
127
        GSList *l;
120
128
 
121
129
        for (l = list; l; l = l->next) {
122
130
                struct audio_adapter *adapter = l->data;
123
131
 
124
 
                if (g_str_equal(adapter->path, path))
 
132
                if (adapter->btd_adapter == btd_adapter)
125
133
                        return adapter;
126
134
        }
127
135
 
134
142
        case HEADSET_SVCLASS_ID:
135
143
                return enabled.headset;
136
144
        case HEADSET_AGW_SVCLASS_ID:
 
145
                return FALSE;
 
146
        case HANDSFREE_SVCLASS_ID:
 
147
                return enabled.headset && enabled.hfp;
 
148
        case HANDSFREE_AGW_SVCLASS_ID:
137
149
                return enabled.gateway;
138
 
        case HANDSFREE_SVCLASS_ID:
139
 
                return enabled.headset && enabled.hfp;
140
 
        case HANDSFREE_AGW_SVCLASS_ID:
141
 
                return  FALSE;
142
150
        case AUDIO_SINK_SVCLASS_ID:
143
151
                return enabled.sink;
 
152
        case AUDIO_SOURCE_SVCLASS_ID:
 
153
                return enabled.source;
144
154
        case AV_REMOTE_TARGET_SVCLASS_ID:
145
155
        case AV_REMOTE_SVCLASS_ID:
146
156
                return enabled.control;
167
177
        uuid16 = uuid.value.uuid16;
168
178
 
169
179
        if (!server_is_enabled(&device->src, uuid16)) {
170
 
                debug("audio handle_uuid: server not enabled for %s (0x%04x)",
171
 
                                uuidstr, uuid16);
 
180
                DBG("server not enabled for %s (0x%04x)", uuidstr, uuid16);
172
181
                return;
173
182
        }
174
183
 
175
184
        switch (uuid16) {
176
185
        case HEADSET_SVCLASS_ID:
177
 
                debug("Found Headset record");
 
186
                DBG("Found Headset record");
178
187
                if (device->headset)
179
188
                        headset_update(device, uuid16, uuidstr);
180
189
                else
182
191
                                                        uuidstr);
183
192
                break;
184
193
        case HEADSET_AGW_SVCLASS_ID:
185
 
                debug("Found Headset AG record");
 
194
                DBG("Found Headset AG record");
186
195
                break;
187
196
        case HANDSFREE_SVCLASS_ID:
188
 
                debug("Found Handsfree record");
 
197
                DBG("Found Handsfree record");
189
198
                if (device->headset)
190
199
                        headset_update(device, uuid16, uuidstr);
191
200
                else
192
201
                        device->headset = headset_init(device, uuid16,
193
 
                                                        uuidstr);
 
202
                                                                uuidstr);
194
203
                break;
195
204
        case HANDSFREE_AGW_SVCLASS_ID:
196
 
                debug("Found Handsfree AG record");
 
205
                DBG("Found Handsfree AG record");
 
206
                if (enabled.gateway && (device->gateway == NULL))
 
207
                        device->gateway = gateway_init(device);
197
208
                break;
198
209
        case AUDIO_SINK_SVCLASS_ID:
199
 
                debug("Found Audio Sink");
 
210
                DBG("Found Audio Sink");
200
211
                if (device->sink == NULL)
201
212
                        device->sink = sink_init(device);
202
213
                break;
203
214
        case AUDIO_SOURCE_SVCLASS_ID:
204
 
                debug("Found Audio Source");
 
215
                DBG("Found Audio Source");
 
216
                if (device->source == NULL)
 
217
                        device->source = source_init(device);
205
218
                break;
206
219
        case AV_REMOTE_SVCLASS_ID:
207
 
                debug("Found AV Remote");
208
 
                if (device->control == NULL)
209
 
                        device->control = control_init(device);
210
 
                if (device->sink && sink_is_active(device))
211
 
                        avrcp_connect(device);
212
 
                break;
213
220
        case AV_REMOTE_TARGET_SVCLASS_ID:
214
 
                debug("Found AV Target");
215
 
                if (device->control == NULL)
216
 
                        device->control = control_init(device);
 
221
                DBG("Found AV %s", uuid16 == AV_REMOTE_SVCLASS_ID ?
 
222
                                                        "Remote" : "Target");
 
223
                if (device->control)
 
224
                        control_update(device, uuid16);
 
225
                else
 
226
                        device->control = control_init(device, uuid16);
217
227
                if (device->sink && sink_is_active(device))
218
228
                        avrcp_connect(device);
219
229
                break;
220
230
        default:
221
 
                debug("Unrecognized UUID: 0x%04X", uuid16);
 
231
                DBG("Unrecognized UUID: 0x%04X", uuid16);
222
232
                break;
223
233
        }
224
234
}
279
289
        return record;
280
290
}
281
291
 
282
 
static sdp_record_t *hsp_hs_record(uint8_t ch)
 
292
static sdp_record_t *hfp_hs_record(uint8_t ch)
283
293
{
284
294
        sdp_list_t *svclass_id, *pfseq, *apseq, *root;
285
295
        uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
297
307
        root = sdp_list_append(0, &root_uuid);
298
308
        sdp_set_browse_groups(record, root);
299
309
 
300
 
        sdp_uuid16_create(&svclass_uuid, HEADSET_SVCLASS_ID);
 
310
        sdp_uuid16_create(&svclass_uuid, HANDSFREE_SVCLASS_ID);
301
311
        svclass_id = sdp_list_append(0, &svclass_uuid);
302
312
        sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
303
313
        svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
304
314
        sdp_set_service_classes(record, svclass_id);
305
315
 
306
 
        sdp_uuid16_create(&profile.uuid, HEADSET_PROFILE_ID);
307
 
        profile.version = 0x0100;
 
316
        sdp_uuid16_create(&profile.uuid, HANDSFREE_PROFILE_ID);
 
317
        profile.version = 0x0105;
308
318
        pfseq = sdp_list_append(0, &profile);
309
319
        sdp_set_profile_descs(record, pfseq);
310
320
 
321
331
        aproto = sdp_list_append(0, apseq);
322
332
        sdp_set_access_protos(record, aproto);
323
333
 
324
 
        sdp_set_info_attr(record, "Headset", 0, 0);
 
334
        sdp_set_info_attr(record, "Hands-Free", 0, 0);
325
335
 
326
336
        sdp_data_free(channel);
327
337
        sdp_list_free(proto[0], 0);
346
356
        sdp_data_t *channel, *features;
347
357
        uint8_t netid = 0x01;
348
358
        uint16_t sdpfeat;
349
 
        sdp_data_t *network = sdp_data_alloc(SDP_UINT8, &netid);
 
359
        sdp_data_t *network;
350
360
 
351
361
        record = sdp_record_alloc();
352
362
        if (!record)
353
363
                return NULL;
354
364
 
 
365
        network = sdp_data_alloc(SDP_UINT8, &netid);
 
366
        if (!network) {
 
367
                sdp_record_free(record);
 
368
                return NULL;
 
369
        }
 
370
 
355
371
        sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
356
372
        root = sdp_list_append(0, &root_uuid);
357
373
        sdp_set_browse_groups(record, root);
400
416
        return record;
401
417
}
402
418
 
403
 
static void auth_cb(DBusError *derr, void *user_data)
 
419
static void headset_auth_cb(DBusError *derr, void *user_data)
404
420
{
405
421
        struct audio_device *device = user_data;
406
 
        const char *uuid;
407
422
        GError *err = NULL;
408
423
        GIOChannel *io;
409
424
 
410
 
        if (get_hfp_active(device))
411
 
                uuid = HFP_AG_UUID;
412
 
        else
413
 
                uuid = HSP_AG_UUID;
 
425
        if (device->hs_preauth_id) {
 
426
                g_source_remove(device->hs_preauth_id);
 
427
                device->hs_preauth_id = 0;
 
428
        }
414
429
 
415
430
        if (derr && dbus_error_is_set(derr)) {
416
431
                error("Access denied: %s", derr->message);
428
443
        }
429
444
}
430
445
 
 
446
static gboolean hs_preauth_cb(GIOChannel *chan, GIOCondition cond,
 
447
                                                        gpointer user_data)
 
448
{
 
449
        struct audio_device *device = user_data;
 
450
 
 
451
        DBG("Headset disconnected during authorization");
 
452
 
 
453
        audio_device_cancel_authorization(device, headset_auth_cb, device);
 
454
 
 
455
        headset_set_state(device, HEADSET_STATE_DISCONNECTED);
 
456
 
 
457
        device->hs_preauth_id = 0;
 
458
 
 
459
        return FALSE;
 
460
}
 
461
 
431
462
static void ag_confirm(GIOChannel *chan, gpointer data)
432
463
{
433
464
        const char *server_uuid, *remote_uuid;
434
 
        uint16_t svclass;
435
465
        struct audio_device *device;
436
466
        gboolean hfp_active;
437
467
        bdaddr_t src, dst;
454
484
                hfp_active = FALSE;
455
485
                server_uuid = HSP_AG_UUID;
456
486
                remote_uuid = HSP_HS_UUID;
457
 
                svclass = HEADSET_SVCLASS_ID;
458
487
        } else {
459
488
                hfp_active = TRUE;
460
489
                server_uuid = HFP_AG_UUID;
461
490
                remote_uuid = HFP_HS_UUID;
462
 
                svclass = HANDSFREE_SVCLASS_ID;
463
491
        }
464
492
 
465
 
        device = manager_get_device(&src, &dst);
 
493
        device = manager_get_device(&src, &dst, TRUE);
466
494
        if (!device)
467
495
                goto drop;
468
496
 
469
 
        if (!manager_allow_headset_connection(&device->src)) {
470
 
                debug("Refusing headset: too many existing connections");
 
497
        if (!manager_allow_headset_connection(device)) {
 
498
                DBG("Refusing headset: too many existing connections");
471
499
                goto drop;
472
500
        }
473
501
 
478
506
        }
479
507
 
480
508
        if (headset_get_state(device) > HEADSET_STATE_DISCONNECTED) {
481
 
                debug("Refusing new connection since one already exists");
 
509
                DBG("Refusing new connection since one already exists");
482
510
                goto drop;
483
511
        }
484
512
 
489
517
                goto drop;
490
518
        }
491
519
 
492
 
        headset_set_state(device, HEADSET_STATE_CONNECT_IN_PROGRESS);
 
520
        headset_set_state(device, HEADSET_STATE_CONNECTING);
493
521
 
494
 
        perr = btd_request_authorization(&device->src, &device->dst,
495
 
                                        server_uuid, auth_cb, device);
 
522
        perr = audio_device_request_authorization(device, server_uuid,
 
523
                                                headset_auth_cb, device);
496
524
        if (perr < 0) {
497
 
                debug("Authorization denied: %s", strerror(-perr));
 
525
                DBG("Authorization denied: %s", strerror(-perr));
498
526
                headset_set_state(device, HEADSET_STATE_DISCONNECTED);
499
527
                return;
500
528
        }
501
529
 
 
530
        device->hs_preauth_id = g_io_add_watch(chan,
 
531
                                        G_IO_NVAL | G_IO_HUP | G_IO_ERR,
 
532
                                        hs_preauth_cb, device);
 
533
 
502
534
        device->auto_connect = auto_connect;
503
535
 
504
536
        return;
507
539
        g_io_channel_shutdown(chan, TRUE, NULL);
508
540
}
509
541
 
510
 
static void hs_io_cb(GIOChannel *chan, GError *err, void *data)
511
 
{
512
 
        /*Stub*/
 
542
static void gateway_auth_cb(DBusError *derr, void *user_data)
 
543
{
 
544
        struct audio_device *device = user_data;
 
545
 
 
546
        if (derr && dbus_error_is_set(derr))
 
547
                error("Access denied: %s", derr->message);
 
548
        else {
 
549
                char ag_address[18];
 
550
 
 
551
                ba2str(&device->dst, ag_address);
 
552
                DBG("Accepted AG connection from %s for %s",
 
553
                        ag_address, device->path);
 
554
 
 
555
                gateway_start_service(device);
 
556
        }
 
557
}
 
558
 
 
559
static void hf_io_cb(GIOChannel *chan, gpointer data)
 
560
{
 
561
        bdaddr_t src, dst;
 
562
        GError *err = NULL;
 
563
        uint8_t ch;
 
564
        const char *server_uuid, *remote_uuid;
 
565
        uint16_t svclass;
 
566
        struct audio_device *device;
 
567
        int perr;
 
568
 
 
569
        bt_io_get(chan, BT_IO_RFCOMM, &err,
 
570
                        BT_IO_OPT_SOURCE_BDADDR, &src,
 
571
                        BT_IO_OPT_DEST_BDADDR, &dst,
 
572
                        BT_IO_OPT_CHANNEL, &ch,
 
573
                        BT_IO_OPT_INVALID);
 
574
 
 
575
        if (err) {
 
576
                error("%s", err->message);
 
577
                g_error_free(err);
 
578
                return;
 
579
        }
 
580
 
 
581
        server_uuid = HFP_AG_UUID;
 
582
        remote_uuid = HFP_HS_UUID;
 
583
        svclass = HANDSFREE_AGW_SVCLASS_ID;
 
584
 
 
585
        device = manager_get_device(&src, &dst, TRUE);
 
586
        if (!device)
 
587
                goto drop;
 
588
 
 
589
        if (!device->gateway) {
 
590
                btd_device_add_uuid(device->btd_dev, remote_uuid);
 
591
                if (!device->gateway)
 
592
                        goto drop;
 
593
        }
 
594
 
 
595
        if (gateway_is_connected(device)) {
 
596
                DBG("Refusing new connection since one already exists");
 
597
                goto drop;
 
598
        }
 
599
 
 
600
        if (gateway_connect_rfcomm(device, chan) < 0) {
 
601
                error("Allocating new GIOChannel failed!");
 
602
                goto drop;
 
603
        }
 
604
 
 
605
        perr = audio_device_request_authorization(device, server_uuid,
 
606
                                                gateway_auth_cb, device);
 
607
        if (perr < 0) {
 
608
                DBG("Authorization denied!");
 
609
                goto drop;
 
610
        }
 
611
 
513
612
        return;
 
613
 
 
614
drop:
 
615
        g_io_channel_shutdown(chan, TRUE, NULL);
 
616
        g_io_channel_unref(chan);
514
617
}
515
618
 
516
619
static int headset_server_init(struct audio_adapter *adapter)
521
624
        GError *err = NULL;
522
625
        uint32_t features;
523
626
        GIOChannel *io;
 
627
        bdaddr_t src;
524
628
 
525
629
        if (config) {
526
630
                gboolean tmp;
528
632
                tmp = g_key_file_get_boolean(config, "General", "Master",
529
633
                                                &err);
530
634
                if (err) {
531
 
                        debug("audio.conf: %s", err->message);
 
635
                        DBG("audio.conf: %s", err->message);
532
636
                        g_clear_error(&err);
533
637
                } else
534
638
                        master = tmp;
535
639
        }
536
640
 
 
641
        adapter_get_address(adapter->btd_adapter, &src);
 
642
 
537
643
        io =  bt_io_listen(BT_IO_RFCOMM, NULL, ag_confirm, adapter, NULL, &err,
538
 
                                BT_IO_OPT_SOURCE_BDADDR, &adapter->src,
 
644
                                BT_IO_OPT_SOURCE_BDADDR, &src,
539
645
                                BT_IO_OPT_CHANNEL, chan,
540
646
                                BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
541
647
                                BT_IO_OPT_MASTER, master,
551
657
                goto failed;
552
658
        }
553
659
 
554
 
        if (add_record_to_server(&adapter->src, record) < 0) {
 
660
        if (add_record_to_server(&src, record) < 0) {
555
661
                error("Unable to register HS AG service record");
556
662
                sdp_record_free(record);
557
663
                goto failed;
566
672
        chan = DEFAULT_HF_AG_CHANNEL;
567
673
 
568
674
        io = bt_io_listen(BT_IO_RFCOMM, NULL, ag_confirm, adapter, NULL, &err,
569
 
                                BT_IO_OPT_SOURCE_BDADDR, &adapter->src,
 
675
                                BT_IO_OPT_SOURCE_BDADDR, &src,
570
676
                                BT_IO_OPT_CHANNEL, chan,
571
677
                                BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
572
678
                                BT_IO_OPT_MASTER, master,
582
688
                goto failed;
583
689
        }
584
690
 
585
 
        if (add_record_to_server(&adapter->src, record) < 0) {
 
691
        if (add_record_to_server(&src, record) < 0) {
586
692
                error("Unable to register HF AG service record");
587
693
                sdp_record_free(record);
588
694
                goto failed;
611
717
 
612
718
static int gateway_server_init(struct audio_adapter *adapter)
613
719
{
614
 
        uint8_t chan = DEFAULT_HSP_HS_CHANNEL;
 
720
        uint8_t chan = DEFAULT_HFP_HS_CHANNEL;
615
721
        sdp_record_t *record;
616
722
        gboolean master = TRUE;
617
723
        GError *err = NULL;
618
724
        GIOChannel *io;
 
725
        bdaddr_t src;
619
726
 
620
727
        if (config) {
621
728
                gboolean tmp;
623
730
                tmp = g_key_file_get_boolean(config, "General", "Master",
624
731
                                                &err);
625
732
                if (err) {
626
 
                        debug("audio.conf: %s", err->message);
 
733
                        DBG("audio.conf: %s", err->message);
627
734
                        g_clear_error(&err);
628
735
                } else
629
736
                        master = tmp;
630
737
        }
631
738
 
632
 
        io = bt_io_listen(BT_IO_RFCOMM, hs_io_cb, NULL, adapter, NULL, &err,
633
 
                                BT_IO_OPT_SOURCE_BDADDR, &adapter->src,
 
739
        adapter_get_address(adapter->btd_adapter, &src);
 
740
 
 
741
        io = bt_io_listen(BT_IO_RFCOMM, NULL, hf_io_cb, adapter, NULL, &err,
 
742
                                BT_IO_OPT_SOURCE_BDADDR, &src,
634
743
                                BT_IO_OPT_CHANNEL, chan,
635
744
                                BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
636
745
                                BT_IO_OPT_MASTER, master,
641
750
                return -1;
642
751
        }
643
752
 
644
 
        adapter->hsp_hs_server = io;
645
 
 
646
 
        record = hsp_hs_record(chan);
 
753
        adapter->hfp_hs_server = io;
 
754
        record = hfp_hs_record(chan);
647
755
        if (!record) {
648
756
                error("Unable to allocate new service record");
649
757
                return -1;
650
758
        }
651
759
 
652
 
        if (add_record_to_server(&adapter->src, record) < 0) {
653
 
                error("Unable to register HSP HS service record");
 
760
        if (add_record_to_server(&src, record) < 0) {
 
761
                error("Unable to register HFP HS service record");
654
762
                sdp_record_free(record);
655
 
                g_io_channel_unref(adapter->hsp_hs_server);
656
 
                adapter->hsp_hs_server = NULL;
 
763
                g_io_channel_unref(adapter->hfp_hs_server);
 
764
                adapter->hfp_hs_server = NULL;
657
765
                return -1;
658
766
        }
659
767
 
660
 
        adapter->hsp_hs_record_id = record->handle;
 
768
        adapter->hfp_hs_record_id = record->handle;
661
769
 
662
770
        return 0;
663
771
}
671
779
        adapter_get_address(adapter, &src);
672
780
        device_get_address(device, &dst);
673
781
 
674
 
        audio_dev = manager_get_device(&src, &dst);
 
782
        audio_dev = manager_get_device(&src, &dst, TRUE);
675
783
        if (!audio_dev) {
676
 
                debug("audio_probe: unable to get a device object");
 
784
                DBG("unable to get a device object");
677
785
                return -1;
678
786
        }
679
787
 
696
804
        devices = g_slist_remove(devices, dev);
697
805
 
698
806
        audio_device_unregister(dev);
699
 
}
700
 
 
701
 
static struct audio_adapter *create_audio_adapter(const char *path, bdaddr_t *src)
 
807
 
 
808
}
 
809
 
 
810
static struct audio_adapter *audio_adapter_ref(struct audio_adapter *adp)
 
811
{
 
812
        adp->ref++;
 
813
 
 
814
        DBG("%p: ref=%d", adp, adp->ref);
 
815
 
 
816
        return adp;
 
817
}
 
818
 
 
819
static void audio_adapter_unref(struct audio_adapter *adp)
 
820
{
 
821
        adp->ref--;
 
822
 
 
823
        DBG("%p: ref=%d", adp, adp->ref);
 
824
 
 
825
        if (adp->ref > 0)
 
826
                return;
 
827
 
 
828
        adapters = g_slist_remove(adapters, adp);
 
829
        btd_adapter_unref(adp->btd_adapter);
 
830
        g_free(adp);
 
831
}
 
832
 
 
833
static struct audio_adapter *audio_adapter_create(struct btd_adapter *adapter)
702
834
{
703
835
        struct audio_adapter *adp;
704
836
 
705
837
        adp = g_new0(struct audio_adapter, 1);
706
 
        adp->path = g_strdup(path);
707
 
        bacpy(&adp->src, src);
 
838
        adp->btd_adapter = btd_adapter_ref(adapter);
708
839
 
709
 
        return adp;
 
840
        return audio_adapter_ref(adp);
710
841
}
711
842
 
712
 
static struct audio_adapter *get_audio_adapter(struct btd_adapter *adapter)
 
843
static struct audio_adapter *audio_adapter_get(struct btd_adapter *adapter)
713
844
{
714
845
        struct audio_adapter *adp;
715
 
        const gchar *path = adapter_get_path(adapter);
716
 
        bdaddr_t src;
717
 
 
718
 
        adapter_get_address(adapter, &src);
719
 
 
720
 
        adp = find_adapter(adapters, path);
 
846
 
 
847
        adp = find_adapter(adapters, adapter);
721
848
        if (!adp) {
722
 
                adp = create_audio_adapter(path, &src);
 
849
                adp = audio_adapter_create(adapter);
723
850
                if (!adp)
724
851
                        return NULL;
725
852
                adapters = g_slist_append(adapters, adp);
726
 
        }
 
853
        } else
 
854
                audio_adapter_ref(adp);
727
855
 
728
856
        return adp;
729
857
}
730
858
 
 
859
static void state_changed(struct btd_adapter *adapter, gboolean powered)
 
860
{
 
861
        struct audio_adapter *adp;
 
862
        static gboolean telephony = FALSE;
 
863
        GSList *l;
 
864
 
 
865
        DBG("%s powered %s", adapter_get_path(adapter),
 
866
                                                powered ? "on" : "off");
 
867
 
 
868
        /* ignore powered change, adapter is powering down */
 
869
        if (powered && adapter_powering_down(adapter))
 
870
                return;
 
871
 
 
872
        adp = find_adapter(adapters, adapter);
 
873
        if (!adp)
 
874
                return;
 
875
 
 
876
        adp->powered = powered;
 
877
 
 
878
        if (powered) {
 
879
                /* telephony driver already initialized*/
 
880
                if (telephony == TRUE)
 
881
                        return;
 
882
                telephony_init();
 
883
                telephony = TRUE;
 
884
                return;
 
885
        }
 
886
 
 
887
        /* telephony not initialized just ignore power down */
 
888
        if (telephony == FALSE)
 
889
                return;
 
890
 
 
891
        for (l = adapters; l; l = l->next) {
 
892
                adp = l->data;
 
893
 
 
894
                if (adp->powered == TRUE)
 
895
                        return;
 
896
        }
 
897
 
 
898
        telephony_exit();
 
899
        telephony = FALSE;
 
900
}
 
901
 
731
902
static int headset_server_probe(struct btd_adapter *adapter)
732
903
{
733
904
        struct audio_adapter *adp;
734
905
        const gchar *path = adapter_get_path(adapter);
 
906
        int err;
735
907
 
736
908
        DBG("path %s", path);
737
909
 
738
 
        adp = get_audio_adapter(adapter);
 
910
        adp = audio_adapter_get(adapter);
739
911
        if (!adp)
740
912
                return -EINVAL;
741
913
 
742
 
        return headset_server_init(adp);
 
914
        err = headset_server_init(adp);
 
915
        if (err < 0) {
 
916
                audio_adapter_unref(adp);
 
917
                return err;
 
918
        }
 
919
 
 
920
        btd_adapter_register_powered_callback(adapter, state_changed);
 
921
        state_changed(adapter, TRUE);
 
922
 
 
923
        return 0;
743
924
}
744
925
 
745
926
static void headset_server_remove(struct btd_adapter *adapter)
749
930
 
750
931
        DBG("path %s", path);
751
932
 
752
 
        adp = find_adapter(adapters, path);
 
933
        adp = find_adapter(adapters, adapter);
753
934
        if (!adp)
754
935
                return;
755
936
 
759
940
        }
760
941
 
761
942
        if (adp->hsp_ag_server) {
 
943
                g_io_channel_shutdown(adp->hsp_ag_server, TRUE, NULL);
762
944
                g_io_channel_unref(adp->hsp_ag_server);
763
945
                adp->hsp_ag_server = NULL;
764
946
        }
769
951
        }
770
952
 
771
953
        if (adp->hfp_ag_server) {
 
954
                g_io_channel_shutdown(adp->hfp_ag_server, TRUE, NULL);
772
955
                g_io_channel_unref(adp->hfp_ag_server);
773
956
                adp->hfp_ag_server = NULL;
774
957
        }
 
958
 
 
959
        btd_adapter_unregister_powered_callback(adapter, state_changed);
 
960
 
 
961
        audio_adapter_unref(adp);
775
962
}
776
963
 
777
964
static int gateway_server_probe(struct btd_adapter *adapter)
778
965
{
779
966
        struct audio_adapter *adp;
780
 
        const gchar *path = adapter_get_path(adapter);
781
 
 
782
 
        DBG("path %s", path);
783
 
 
784
 
        adp = get_audio_adapter(adapter);
 
967
 
 
968
        adp = audio_adapter_get(adapter);
785
969
        if (!adp)
786
970
                return -EINVAL;
787
971
 
795
979
 
796
980
        DBG("path %s", path);
797
981
 
798
 
        adp = find_adapter(adapters, path);
 
982
        adp = find_adapter(adapters, adapter);
799
983
        if (!adp)
800
984
                return;
801
985
 
802
 
        if (adp->hsp_hs_record_id) {
803
 
                remove_record_from_server(adp->hsp_hs_record_id);
804
 
                adp->hsp_hs_record_id = 0;
805
 
        }
806
 
 
807
 
        if (adp->hsp_hs_server) {
808
 
                g_io_channel_unref(adp->hsp_hs_server);
809
 
                adp->hsp_hs_server = NULL;
810
 
        }
 
986
        if (adp->hfp_hs_record_id) {
 
987
                remove_record_from_server(adp->hfp_hs_record_id);
 
988
                adp->hfp_hs_record_id = 0;
 
989
        }
 
990
 
 
991
        if (adp->hfp_hs_server) {
 
992
                g_io_channel_unref(adp->hfp_hs_server);
 
993
                adp->hfp_hs_server = NULL;
 
994
        }
 
995
 
 
996
        audio_adapter_unref(adp);
811
997
}
812
998
 
813
999
static int a2dp_server_probe(struct btd_adapter *adapter)
814
1000
{
815
1001
        struct audio_adapter *adp;
816
1002
        const gchar *path = adapter_get_path(adapter);
 
1003
        bdaddr_t src;
 
1004
        int err;
817
1005
 
818
1006
        DBG("path %s", path);
819
1007
 
820
 
        adp = get_audio_adapter(adapter);
 
1008
        adp = audio_adapter_get(adapter);
821
1009
        if (!adp)
822
1010
                return -EINVAL;
823
1011
 
824
 
        return a2dp_register(connection, &adp->src, config);
 
1012
        adapter_get_address(adapter, &src);
 
1013
 
 
1014
        err = a2dp_register(connection, &src, config);
 
1015
        if (err < 0)
 
1016
                audio_adapter_unref(adp);
 
1017
 
 
1018
        return err;
825
1019
}
826
1020
 
827
1021
static void a2dp_server_remove(struct btd_adapter *adapter)
828
1022
{
829
1023
        struct audio_adapter *adp;
830
1024
        const gchar *path = adapter_get_path(adapter);
 
1025
        bdaddr_t src;
831
1026
 
832
1027
        DBG("path %s", path);
833
1028
 
834
 
        adp = find_adapter(adapters, path);
 
1029
        adp = find_adapter(adapters, adapter);
835
1030
        if (!adp)
836
1031
                return;
837
1032
 
838
 
        return a2dp_unregister(&adp->src);
 
1033
        adapter_get_address(adapter, &src);
 
1034
        a2dp_unregister(&src);
 
1035
        audio_adapter_unref(adp);
839
1036
}
840
1037
 
841
1038
static int avrcp_server_probe(struct btd_adapter *adapter)
842
1039
{
843
1040
        struct audio_adapter *adp;
844
1041
        const gchar *path = adapter_get_path(adapter);
 
1042
        bdaddr_t src;
845
1043
 
846
1044
        DBG("path %s", path);
847
1045
 
848
 
        adp = get_audio_adapter(adapter);
 
1046
        adp = audio_adapter_get(adapter);
849
1047
        if (!adp)
850
1048
                return -EINVAL;
851
1049
 
852
 
        return avrcp_register(connection, &adp->src, config);
 
1050
        adapter_get_address(adapter, &src);
 
1051
 
 
1052
        return avrcp_register(connection, &src, config);
853
1053
}
854
1054
 
855
1055
static void avrcp_server_remove(struct btd_adapter *adapter)
856
1056
{
857
1057
        struct audio_adapter *adp;
858
1058
        const gchar *path = adapter_get_path(adapter);
859
 
 
860
 
        DBG("path %s", path);
861
 
 
862
 
        adp = find_adapter(adapters, path);
863
 
        if (!adp)
864
 
                return;
865
 
 
866
 
        return avrcp_unregister(&adp->src);
 
1059
        bdaddr_t src;
 
1060
 
 
1061
        DBG("path %s", path);
 
1062
 
 
1063
        adp = find_adapter(adapters, adapter);
 
1064
        if (!adp)
 
1065
                return;
 
1066
 
 
1067
        adapter_get_address(adapter, &src);
 
1068
        avrcp_unregister(&src);
 
1069
        audio_adapter_unref(adp);
 
1070
}
 
1071
 
 
1072
static int media_server_probe(struct btd_adapter *adapter)
 
1073
{
 
1074
        struct audio_adapter *adp;
 
1075
        const gchar *path = adapter_get_path(adapter);
 
1076
        bdaddr_t src;
 
1077
 
 
1078
        DBG("path %s", path);
 
1079
 
 
1080
        adp = audio_adapter_get(adapter);
 
1081
        if (!adp)
 
1082
                return -EINVAL;
 
1083
 
 
1084
        adapter_get_address(adapter, &src);
 
1085
 
 
1086
        return media_register(connection, path, &src);
 
1087
}
 
1088
 
 
1089
static void media_server_remove(struct btd_adapter *adapter)
 
1090
{
 
1091
        struct audio_adapter *adp;
 
1092
        const gchar *path = adapter_get_path(adapter);
 
1093
 
 
1094
        DBG("path %s", path);
 
1095
 
 
1096
        adp = find_adapter(adapters, adapter);
 
1097
        if (!adp)
 
1098
                return;
 
1099
 
 
1100
        media_unregister(path);
 
1101
        audio_adapter_unref(adp);
867
1102
}
868
1103
 
869
1104
static struct btd_device_driver audio_driver = {
899
1134
        .remove = avrcp_server_remove,
900
1135
};
901
1136
 
902
 
int audio_manager_init(DBusConnection *conn, GKeyFile *conf)
 
1137
static struct btd_adapter_driver media_server_driver = {
 
1138
        .name   = "media",
 
1139
        .probe  = media_server_probe,
 
1140
        .remove = media_server_remove,
 
1141
};
 
1142
 
 
1143
int audio_manager_init(DBusConnection *conn, GKeyFile *conf,
 
1144
                                                        gboolean *enable_sco)
903
1145
{
904
1146
        char **list;
905
1147
        int i;
926
1168
                        enabled.source = TRUE;
927
1169
                else if (g_str_equal(list[i], "Control"))
928
1170
                        enabled.control = TRUE;
 
1171
                else if (g_str_equal(list[i], "Socket"))
 
1172
                        enabled.socket = TRUE;
 
1173
                else if (g_str_equal(list[i], "Media"))
 
1174
                        enabled.media = TRUE;
929
1175
        }
930
1176
        g_strfreev(list);
931
1177
 
942
1188
                        enabled.source = FALSE;
943
1189
                else if (g_str_equal(list[i], "Control"))
944
1190
                        enabled.control = FALSE;
 
1191
                else if (g_str_equal(list[i], "Socket"))
 
1192
                        enabled.socket = FALSE;
 
1193
                else if (g_str_equal(list[i], "Media"))
 
1194
                        enabled.media = FALSE;
945
1195
        }
946
1196
        g_strfreev(list);
947
1197
 
948
1198
        b = g_key_file_get_boolean(config, "General", "AutoConnect", &err);
949
1199
        if (err) {
950
 
                debug("audio.conf: %s", err->message);
 
1200
                DBG("audio.conf: %s", err->message);
951
1201
                g_clear_error(&err);
952
1202
        } else
953
1203
                auto_connect = b;
963
1213
        i = g_key_file_get_integer(config, "Headset", "MaxConnected",
964
1214
                                        &err);
965
1215
        if (err) {
966
 
                debug("audio.conf: %s", err->message);
 
1216
                DBG("audio.conf: %s", err->message);
967
1217
                g_clear_error(&err);
968
1218
        } else
969
1219
                max_connected_headsets = i;
970
1220
 
971
1221
proceed:
972
 
        if (enabled.headset) {
973
 
                telephony_init();
 
1222
        if (enabled.socket)
 
1223
                unix_init();
 
1224
 
 
1225
        if (enabled.media)
 
1226
                btd_register_adapter_driver(&media_server_driver);
 
1227
 
 
1228
        if (enabled.headset)
974
1229
                btd_register_adapter_driver(&headset_server_driver);
975
 
        }
976
1230
 
977
1231
        if (enabled.gateway)
978
1232
                btd_register_adapter_driver(&gateway_server_driver);
985
1239
 
986
1240
        btd_register_device_driver(&audio_driver);
987
1241
 
 
1242
        *enable_sco = (enabled.gateway || enabled.headset);
 
1243
 
988
1244
        return 0;
989
1245
}
990
1246
 
1002
1258
                config = NULL;
1003
1259
        }
1004
1260
 
 
1261
        if (enabled.socket)
 
1262
                unix_exit();
 
1263
 
 
1264
        if (enabled.media)
 
1265
                btd_unregister_adapter_driver(&media_server_driver);
 
1266
 
1005
1267
        if (enabled.headset) {
1006
1268
                btd_unregister_adapter_driver(&headset_server_driver);
1007
1269
                telephony_exit();
1043
1305
                                && !dev->headset)
1044
1306
                        continue;
1045
1307
 
 
1308
                if (interface && !strcmp(AUDIO_GATEWAY_INTERFACE, interface)
 
1309
                                && !dev->gateway)
 
1310
                        continue;
 
1311
 
1046
1312
                if (interface && !strcmp(AUDIO_SINK_INTERFACE, interface)
1047
1313
                                && !dev->sink)
1048
1314
                        continue;
1055
1321
                                && !dev->control)
1056
1322
                        continue;
1057
1323
 
1058
 
                if (connected && !audio_device_is_connected(dev, interface))
 
1324
                if (connected && !audio_device_is_active(dev, interface))
1059
1325
                        continue;
1060
1326
 
1061
1327
                return dev;
1065
1331
}
1066
1332
 
1067
1333
struct audio_device *manager_get_device(const bdaddr_t *src,
1068
 
                                        const bdaddr_t *dst)
 
1334
                                        const bdaddr_t *dst,
 
1335
                                        gboolean create)
1069
1336
{
1070
1337
        struct audio_device *dev;
1071
1338
        struct btd_adapter *adapter;
1077
1344
        if (dev)
1078
1345
                return dev;
1079
1346
 
 
1347
        if (!create)
 
1348
                return NULL;
 
1349
 
1080
1350
        ba2str(src, addr);
1081
1351
 
1082
1352
        adapter = manager_find_adapter(src);
1096
1366
 
1097
1367
        path = device_get_path(device);
1098
1368
 
1099
 
        dev = audio_device_register(connection, path, src, dst);
 
1369
        dev = audio_device_register(connection, device, path, src, dst);
1100
1370
        if (!dev)
1101
1371
                return NULL;
1102
1372
 
1103
 
        dev->btd_dev = device;
1104
 
 
1105
1373
        devices = g_slist_append(devices, dev);
1106
1374
 
1107
1375
        return dev;
1108
1376
}
1109
1377
 
1110
 
gboolean manager_allow_headset_connection(bdaddr_t *src)
 
1378
gboolean manager_allow_headset_connection(struct audio_device *device)
1111
1379
{
1112
1380
        GSList *l;
1113
1381
        int connected = 0;
1116
1384
                struct audio_device *dev = l->data;
1117
1385
                struct headset *hs = dev->headset;
1118
1386
 
1119
 
                if (bacmp(&dev->src, src))
 
1387
                if (dev == device)
 
1388
                        continue;
 
1389
 
 
1390
                if (bacmp(&dev->src, &device->src))
1120
1391
                        continue;
1121
1392
 
1122
1393
                if (!hs)
1131
1402
 
1132
1403
        return TRUE;
1133
1404
}
 
1405
 
 
1406
void manager_set_fast_connectable(gboolean enable)
 
1407
{
 
1408
        GSList *l;
 
1409
 
 
1410
        for (l = adapters; l != NULL; l = l->next) {
 
1411
                struct audio_adapter *adapter = l->data;
 
1412
 
 
1413
                if (btd_adapter_set_fast_connectable(adapter->btd_adapter,
 
1414
                                                                enable))
 
1415
                        error("Changing fast connectable for hci%d failed",
 
1416
                                adapter_get_dev_id(adapter->btd_adapter));
 
1417
        }
 
1418
}