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