1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <stdint.h>
#include <assert.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
#include <glib.h>
#include <dbus/dbus.h>
#include "dbus.h"
#include "dbus-helper.h"
#include "logging.h"
#include "headset.h"
#include "manager.h"
typedef enum {
HEADSET = 1 << 0,
GATEWAY = 1 << 1,
SINK = 1 << 2,
SOURCE = 1 << 3,
CONTROL = 1 << 4,
TARGET = 1 << 5,
INVALID = 1 << 6
} audio_service_type;
typedef enum {
GENERIC_AUDIO = 0,
ADVANCED_AUDIO,
AV_REMOTE,
GET_RECORDS
} audio_sdp_state_t;
struct audio_sdp_data {
audio_device_t *device;
DBusMessage *msg; /* Method call or NULL */
GSList *handles; /* uint32_t * */
GSList *records; /* sdp_record_t * */
audio_sdp_state_t state;
};
static DBusConnection *connection = NULL;
static audio_device_t *default_hs = NULL;
static GSList *devices = NULL;
static void get_next_record(struct audio_sdp_data *data);
static DBusHandlerResult get_handles(const char *uuid,
struct audio_sdp_data *data);
/* FIXME: Remove these once global error functions exist */
static DBusHandlerResult error_reply(DBusConnection *conn, DBusMessage *msg,
const char *name, const char *descr)
{
DBusMessage *derr;
if (!conn || !msg)
return DBUS_HANDLER_RESULT_HANDLED;
derr = dbus_message_new_error(msg, name, descr);
if (!derr) {
error("Unable to allocate new error return");
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
return send_message_and_unref(conn, derr);
}
DBusHandlerResult err_invalid_args(DBusConnection *conn, DBusMessage *msg,
const char *descr)
{
return error_reply(conn, msg, "org.bluez.audio.Error.InvalidArguments",
descr ? descr : "Invalid arguments in method call");
}
DBusHandlerResult err_already_connected(DBusConnection *conn, DBusMessage *msg)
{
return error_reply(conn, msg, "org.bluez.audio.Error.AlreadyConnected",
"Already connected to a device");
}
DBusHandlerResult err_not_connected(DBusConnection *conn, DBusMessage *msg)
{
return error_reply(conn, msg, "org.bluez.audio.Error.NotConnected",
"Not connected to any device");
}
DBusHandlerResult err_not_supported(DBusConnection *conn, DBusMessage *msg)
{
return error_reply(conn, msg, "org.bluez.audio.Error.NotSupported",
"The service is not supported by the remote device");
}
DBusHandlerResult err_connect_failed(DBusConnection *conn,
DBusMessage *msg, int err)
{
return error_reply(conn, msg, "org.bluez.audio.Error.ConnectFailed",
strerror(err));
}
DBusHandlerResult err_does_not_exist(DBusConnection *conn, DBusMessage *msg)
{
return error_reply(conn, msg, "org.bluez.audio.Error.DoesNotExist",
"Does not exist");
}
DBusHandlerResult err_not_available(DBusConnection *conn, DBusMessage *msg)
{
return error_reply(conn, msg, "org.bluez.audio.Error.NotAvailable",
"Not available");
}
DBusHandlerResult err_failed(DBusConnection *conn, DBusMessage *msg,
const char *dsc)
{
return error_reply(conn, msg, "org.bluez.audio.Error.Failed", dsc);
}
static audio_device_t *find_device(bdaddr_t *bda)
{
GSList *l;
for (l = devices; l != NULL; l = l->next) {
audio_device_t *device = l->data;
if (bacmp(&device->bda, bda) == 0)
return device;
}
return NULL;
}
static DBusHandlerResult device_get_address(DBusConnection *conn,
DBusMessage *msg,
void *data)
{
audio_device_t *device = data;
DBusMessage *reply;
char address[18], *ptr = address;
reply = dbus_message_new_method_return(msg);
if (!reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
ba2str(&device->bda, address);
dbus_message_append_args(reply, DBUS_TYPE_STRING, &ptr,
DBUS_TYPE_INVALID);
return send_message_and_unref(conn, reply);
}
static DBusHandlerResult device_get_connected(DBusConnection *conn,
DBusMessage *msg,
void *data)
{
DBusMessageIter iter, array_iter;
audio_device_t *device = data;
DBusMessage *reply;
const char *iface;
reply = dbus_message_new_method_return(msg);
if (!reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
DBUS_TYPE_STRING_AS_STRING, &array_iter);
if (device->headset && headset_is_connected(device->headset)) {
iface = AUDIO_HEADSET_INTERFACE;
dbus_message_iter_append_basic(&array_iter,
DBUS_TYPE_STRING, &iface);
}
dbus_message_iter_close_container(&iter, &array_iter);
return send_message_and_unref(conn, reply);
}
static DBusMethodVTable device_methods[] = {
{ "GetAddress", device_get_address,
"", "s" },
{ "GetConnectedInterfaces", device_get_connected,
"", "s" },
{ NULL, NULL, NULL, NULL }
};
static void free_device(audio_device_t *device)
{
g_free(device);
}
static audio_device_t *create_device(bdaddr_t *bda)
{
static int device_id = 0;
audio_device_t *device;
device = g_new0(audio_device_t, 1);
bacpy(&device->bda, bda);
snprintf(device->object_path, sizeof(device->object_path) - 1,
"%s/device%d", AUDIO_MANAGER_PATH, device_id++);
return device;
}
static void remove_device(audio_device_t *device)
{
devices = g_slist_remove(devices, device);
if (device->headset)
headset_free(device->object_path);
dbus_connection_destroy_object_path(connection, device->object_path);
g_free(device);
}
static gboolean add_device(audio_device_t *device)
{
if (!dbus_connection_create_object_path(connection,
device->object_path,
device, NULL)) {
error("D-Bus failed to register %s path", device->object_path);
return FALSE;
}
if (!dbus_connection_register_interface(connection,
device->object_path,
AUDIO_DEVICE_INTERFACE,
device_methods, NULL, NULL)) {
error("Failed to register %s interface to %s",
AUDIO_DEVICE_INTERFACE, device->object_path);
dbus_connection_destroy_object_path(connection,
device->object_path);
return FALSE;
}
devices = g_slist_append(devices, device);
return TRUE;
}
static uint16_t get_service_uuid(const sdp_record_t *record)
{
sdp_list_t *classes;
uuid_t uuid;
uint16_t uuid16 = 0;
if (sdp_get_service_classes(record, &classes) < 0) {
error("Unable to get service classes from record");
return 0;
}
memcpy(&uuid, classes->data, sizeof(uuid));
if (!sdp_uuid128_to_uuid(&uuid)) {
error("Not a 16 bit UUID");
sdp_list_free(classes, free);
return 0;
}
if (uuid.type == SDP_UUID32) {
if (uuid.value.uuid32 > 0xFFFF) {
error("Not a 16 bit UUID");
goto done;
}
uuid16 = (uint16_t) uuid.value.uuid32;
} else
uuid16 = uuid.value.uuid16;
done:
sdp_list_free(classes, free);
return uuid16;
}
void finish_sdp_transaction(DBusConnection *conn, bdaddr_t *dba)
{
char address[18], *addr_ptr = address;
DBusMessage *msg, *reply;
DBusError derr;
ba2str(dba, address);
msg = dbus_message_new_method_call("org.bluez", "/org/bluez/hci0",
"org.bluez.Adapter",
"FinishRemoteServiceTransaction");
if (!msg) {
error("Unable to allocate new method call");
return;
}
dbus_message_append_args(msg, DBUS_TYPE_STRING, &addr_ptr,
DBUS_TYPE_INVALID);
dbus_error_init(&derr);
reply = dbus_connection_send_with_reply_and_block(conn, msg, -1,
&derr);
dbus_message_unref(msg);
if (dbus_error_is_set(&derr) ||
dbus_set_error_from_message(&derr, reply)) {
error("FinishRemoteServiceTransaction(%s) failed: %s",
address, derr.message);
dbus_error_free(&derr);
return;
}
dbus_message_unref(reply);
}
static void handle_record(sdp_record_t *record, audio_device_t *device)
{
uint16_t uuid16;
uuid16 = get_service_uuid(record);
switch (uuid16) {
case HEADSET_SVCLASS_ID:
debug("Found Headset record");
if (device->headset)
headset_update(device->headset, record, uuid16);
else
device->headset = headset_init(device->object_path,
record, uuid16);
break;
case HEADSET_AGW_SVCLASS_ID:
debug("Found Headset AG record");
break;
case HANDSFREE_SVCLASS_ID:
debug("Found Hansfree record");
break;
case HANDSFREE_AGW_SVCLASS_ID:
debug("Found Handsfree AG record");
break;
case AUDIO_SINK_SVCLASS_ID:
debug("Found Audio Sink");
break;
case AUDIO_SOURCE_SVCLASS_ID:
debug("Found Audio Source");
break;
case AV_REMOTE_SVCLASS_ID:
debug("Found AV Remote");
break;
case AV_REMOTE_TARGET_SVCLASS_ID:
debug("Found AV Target");
break;
default:
debug("Unrecognized UUID: 0x%04X", uuid16);
break;
}
}
static gint record_iface_cmp(gconstpointer a, gconstpointer b)
{
const sdp_record_t *record = a;
const char *interface = b;
switch (get_service_uuid(record)) {
case HEADSET_SVCLASS_ID:
case HANDSFREE_SVCLASS_ID:
return strcmp(interface, AUDIO_HEADSET_INTERFACE);
case HEADSET_AGW_SVCLASS_ID:
case HANDSFREE_AGW_SVCLASS_ID:
return strcmp(interface, AUDIO_GATEWAY_INTERFACE);
case AUDIO_SINK_SVCLASS_ID:
return strcmp(interface, AUDIO_SINK_INTERFACE);
case AUDIO_SOURCE_SVCLASS_ID:
return strcmp(interface, AUDIO_SOURCE_INTERFACE);
case AV_REMOTE_SVCLASS_ID:
return strcmp(interface, AUDIO_CONTROL_INTERFACE);
case AV_REMOTE_TARGET_SVCLASS_ID:
return strcmp(interface, AUDIO_TARGET_INTERFACE);
default:
return -1;
}
}
static void finish_sdp(struct audio_sdp_data *data, gboolean success)
{
const char *path, *addr;
char **required;
int required_len, i;
DBusMessage *reply = NULL;
debug("Audio service discovery completed with %s",
success ? "success" : "failure");
finish_sdp_transaction(connection, &data->device->bda);
if (!success)
goto done;
if (!data->msg)
goto update;
if (!dbus_message_get_args(data->msg, NULL,
DBUS_TYPE_STRING, &addr,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
&required, &required_len,
DBUS_TYPE_INVALID)) {
error("Unable to get message args");
success = FALSE;
goto done;
}
/* Return error if no audio related service records were found */
if (!data->records) {
debug("No audio audio related service records were found");
success = FALSE;
err_not_supported(connection, data->msg);
goto done;
}
for (i = 0; i < required_len; i++) {
const char *iface = required[i];
if (g_slist_find_custom(data->records, iface, record_iface_cmp))
continue;
debug("Required interface %s not supported", iface);
success = FALSE;
err_not_supported(connection, data->msg);
dbus_free_string_array(required);
goto done;
}
dbus_free_string_array(required);
path = data->device->object_path;
reply = dbus_message_new_method_return(data->msg);
if (!reply) {
success = FALSE;
err_failed(connection, data->msg, "Out of memory");
goto done;
}
add_device(data->device);
update:
g_slist_foreach(data->records, (GFunc) handle_record, data->device);
if (reply) {
dbus_connection_emit_signal(connection, AUDIO_MANAGER_PATH,
AUDIO_MANAGER_INTERFACE,
"DeviceCreated",
DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID);
dbus_message_append_args(reply, DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID);
send_message_and_unref(connection, reply);
}
done:
if (!success)
free_device(data->device);
if (data->msg)
dbus_message_unref(data->msg);
g_slist_foreach(data->handles, (GFunc) g_free, NULL);
g_slist_free(data->handles);
g_slist_foreach(data->records, (GFunc) sdp_record_free, NULL);
g_slist_free(data->records);
g_free(data);
}
static void get_record_reply(DBusPendingCall *call,
struct audio_sdp_data *data)
{
DBusMessage *reply;
DBusError derr;
uint8_t *array;
int array_len, record_len;
sdp_record_t *record;
reply = dbus_pending_call_steal_reply(call);
dbus_error_init(&derr);
if (dbus_set_error_from_message(&derr, reply)) {
error("GetRemoteServiceRecord failed: %s", derr.message);
if (dbus_error_has_name(&derr,
"org.bluez.Error.ConnectionAttemptFailed"))
err_connect_failed(connection, data->msg, EHOSTDOWN);
else
err_failed(connection, data->msg, derr.message);
dbus_error_free(&derr);
goto failed;
}
if (!dbus_message_get_args(reply, NULL,
DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &array, &array_len,
DBUS_TYPE_INVALID)) {
err_failed(connection, data->msg,
"Unable to get args from GetRecordReply");
goto failed;
}
record = sdp_extract_pdu(array, &record_len);
if (!record) {
error("Unable to extract service record from reply");
goto done;
}
if (record_len != array_len)
debug("warning: array len (%d) != record len (%d)",
array_len, record_len);
data->records = g_slist_append(data->records, record);
done:
dbus_message_unref(reply);
if (data->handles)
get_next_record(data);
else
finish_sdp(data, TRUE);
return;
failed:
if (reply)
dbus_message_unref(reply);
finish_sdp(data, FALSE);
}
static void get_next_record(struct audio_sdp_data *data)
{
DBusMessage *msg;
DBusPendingCall *pending;
char address[18], *ptr = address;
dbus_uint32_t *handle;
msg = dbus_message_new_method_call("org.bluez", "/org/bluez/hci0",
"org.bluez.Adapter",
"GetRemoteServiceRecord");
if (!msg) {
error("Unable to allocate new method call");
err_connect_failed(connection, data->msg, ENOMEM);
finish_sdp(data, FALSE);
return;
}
handle = data->handles->data;
data->handles = g_slist_remove(data->handles, data->handles->data);
ba2str(&data->device->bda, address);
dbus_message_append_args(msg, DBUS_TYPE_STRING, &ptr,
DBUS_TYPE_UINT32, handle,
DBUS_TYPE_INVALID);
g_free(handle);
if (!dbus_connection_send_with_reply(connection, msg, &pending, -1)) {
error("Sending GetRemoteServiceRecord failed");
err_connect_failed(connection, data->msg, EIO);
finish_sdp(data, FALSE);
return;
}
dbus_pending_call_set_notify(pending,
(DBusPendingCallNotifyFunction) get_record_reply,
data, NULL);
dbus_pending_call_unref(pending);
dbus_message_unref(msg);
}
static GSList *find_handle(GSList *handles, dbus_uint32_t handle)
{
while (handles) {
if (*(dbus_uint32_t *) handles->data == handle)
return handles;
handles = handles->next;
}
return NULL;
}
static void get_handles_reply(DBusPendingCall *call,
struct audio_sdp_data *data)
{
DBusMessage *reply;
DBusError derr;
dbus_uint32_t *array = NULL;
int array_len, i;
reply = dbus_pending_call_steal_reply(call);
dbus_error_init(&derr);
if (dbus_set_error_from_message(&derr, reply)) {
error("GetRemoteServiceHandles failed: %s", derr.message);
if (dbus_error_has_name(&derr,
"org.bluez.Error.ConnectionAttemptFailed"))
err_connect_failed(connection, data->msg, EHOSTDOWN);
else
err_failed(connection, data->msg, derr.message);
dbus_error_free(&derr);
goto failed;
}
if (!dbus_message_get_args(reply, NULL,
DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32,
&array, &array_len,
DBUS_TYPE_INVALID)) {
err_failed(connection, data->msg,
"Unable to get args from reply");
goto failed;
}
for (i = 0; i < array_len; i++) {
if (!find_handle(data->handles, array[i])) {
dbus_uint32_t *handle = g_new(dbus_uint32_t, 1);
*handle = array[i];
data->handles = g_slist_append(data->handles, handle);
}
}
data->state++;
switch (data->state) {
case ADVANCED_AUDIO:
get_handles(ADVANCED_AUDIO_UUID, data);
break;
case AV_REMOTE:
get_handles(AVRCP_REMOTE_UUID, data);
break;
default:
if (data->handles)
get_next_record(data);
else
finish_sdp(data, TRUE);
}
dbus_message_unref(reply);
return;
failed:
dbus_message_unref(reply);
finish_sdp(data, FALSE);
}
static DBusHandlerResult get_handles(const char *uuid,
struct audio_sdp_data *data)
{
DBusPendingCall *pending;
char address[18];
const char *ptr = address;
DBusMessage *msg;
msg = dbus_message_new_method_call("org.bluez", "/org/bluez/hci0",
"org.bluez.Adapter",
"GetRemoteServiceHandles");
if (!msg) {
err_failed(connection, data->msg,
"Could not create a new dbus message");
goto failed;
}
ba2str(&data->device->bda, address);
dbus_message_append_args(msg, DBUS_TYPE_STRING, &ptr,
DBUS_TYPE_STRING, &uuid,
DBUS_TYPE_INVALID);
if (!dbus_connection_send_with_reply(connection, msg, &pending, -1)) {
err_failed(connection, data->msg,
"Sending GetRemoteServiceHandles failed");
goto failed;
}
dbus_pending_call_set_notify(pending,
(DBusPendingCallNotifyFunction) get_handles_reply,
data, NULL);
dbus_pending_call_unref(pending);
dbus_message_unref(msg);
return DBUS_HANDLER_RESULT_HANDLED;
failed:
if (msg)
dbus_message_unref(msg);
finish_sdp(data, FALSE);
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult resolve_services(DBusMessage *msg,
audio_device_t *device)
{
struct audio_sdp_data *sdp_data;
sdp_data = g_new0(struct audio_sdp_data, 1);
if (msg)
sdp_data->msg = dbus_message_ref(msg);
sdp_data->device = device;
return get_handles(GENERIC_AUDIO_UUID, sdp_data);
}
audio_device_t *manager_headset_connected(bdaddr_t *bda)
{
audio_device_t *device;
const char *path;
gboolean created = FALSE;
device = find_device(bda);
if (device && device->headset)
return device;
if (!device) {
device = create_device(bda);
if (!add_device(device)) {
free_device(device);
return NULL;
}
created = TRUE;
}
if (!device->headset)
device->headset = headset_init(device->object_path, NULL, 0);
if (!device->headset)
return NULL;
path = device->object_path;
if (created) {
dbus_connection_emit_signal(connection, AUDIO_MANAGER_PATH,
AUDIO_MANAGER_INTERFACE,
"DeviceCreated",
DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID);
resolve_services(NULL, device);
}
dbus_connection_emit_signal(connection, AUDIO_MANAGER_PATH,
AUDIO_MANAGER_INTERFACE,
"HeadsetCreated",
DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID);
if (!default_hs) {
default_hs = device;
dbus_connection_emit_signal(connection, AUDIO_MANAGER_PATH,
AUDIO_MANAGER_INTERFACE,
"DefaultHeadsetChanged",
DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID);
}
return device;
}
static gboolean device_supports_interface(audio_device_t *device,
const char *iface)
{
if (strcmp(iface, AUDIO_HEADSET_INTERFACE) == 0)
return device->headset ? TRUE : FALSE;
if (strcmp(iface, AUDIO_GATEWAY_INTERFACE) == 0)
return device->gateway ? TRUE : FALSE;
if (strcmp(iface, AUDIO_SOURCE_INTERFACE) == 0)
return device->gateway ? TRUE : FALSE;
if (strcmp(iface, AUDIO_SINK_INTERFACE) == 0)
return device->sink ? TRUE : FALSE;
if (strcmp(iface, AUDIO_CONTROL_INTERFACE) == 0)
return device->control ? TRUE : FALSE;
if (strcmp(iface, AUDIO_TARGET_INTERFACE) == 0)
return device->target ? TRUE : FALSE;
debug("Unknown interface %s", iface);
return FALSE;
}
static gboolean device_matches(audio_device_t *device, char **interfaces)
{
int i;
for (i = 0; interfaces[i]; i++) {
if (device_supports_interface(device, interfaces[i]))
continue;
debug("Device does not support interface %s", interfaces[i]);
return FALSE;
}
return TRUE;
}
static DBusHandlerResult am_create_device(DBusConnection *conn,
DBusMessage *msg,
void *data)
{
const char *address, *path;
char **required;
int required_len;
bdaddr_t bda;
audio_device_t *device;
DBusMessage *reply;
DBusError derr;
dbus_error_init(&derr);
dbus_message_get_args(msg, &derr,
DBUS_TYPE_STRING, &address,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
&required, &required_len,
DBUS_TYPE_INVALID);
if (dbus_error_is_set(&derr)) {
err_invalid_args(connection, msg, derr.message);
dbus_error_free(&derr);
return DBUS_HANDLER_RESULT_HANDLED;
}
str2ba(address, &bda);
device = find_device(&bda);
if (!device) {
device = create_device(&bda);
dbus_free_string_array(required);
return resolve_services(msg, device);
}
if (!device_matches(device, required)) {
dbus_free_string_array(required);
return err_not_supported(conn, msg);
}
dbus_free_string_array(required);
path = device->object_path;
reply = dbus_message_new_method_return(msg);
if (!reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
dbus_message_append_args(reply, DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID);
return send_message_and_unref(conn, reply);
}
static DBusHandlerResult am_list_devices(DBusConnection *conn,
DBusMessage *msg,
void *data)
{
DBusMessageIter iter, array_iter;
DBusMessage *reply;
DBusError derr;
GSList *l;
char **required;
int required_len;
dbus_error_init(&derr);
dbus_message_get_args(msg, &derr,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
&required, &required_len,
DBUS_TYPE_INVALID);
if (dbus_error_is_set(&derr)) {
err_invalid_args(connection, msg, derr.message);
dbus_error_free(&derr);
return DBUS_HANDLER_RESULT_HANDLED;
}
reply = dbus_message_new_method_return(msg);
if (!reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
DBUS_TYPE_STRING_AS_STRING, &array_iter);
for (l = devices; l != NULL; l = l->next) {
audio_device_t *device = l->data;
const char *path;
if (!device_matches(device, required))
continue;
path = device->object_path;
dbus_message_iter_append_basic(&array_iter,
DBUS_TYPE_STRING, &path);
}
dbus_message_iter_close_container(&iter, &array_iter);
dbus_free_string_array(required);
return send_message_and_unref(connection, reply);
}
static DBusHandlerResult am_create_headset(DBusConnection *conn, DBusMessage *msg,
void *data)
{
const char *path, *address;
bdaddr_t bda;
DBusMessage *reply;
DBusError derr;
audio_device_t *device;
gboolean created = FALSE;
dbus_error_init(&derr);
dbus_message_get_args(msg, &derr,
DBUS_TYPE_STRING, &address,
DBUS_TYPE_INVALID);
if (dbus_error_is_set(&derr)) {
err_invalid_args(connection, msg, derr.message);
dbus_error_free(&derr);
return DBUS_HANDLER_RESULT_HANDLED;
}
str2ba(address, &bda);
device = find_device(&bda);
if (device)
goto done;
device = create_device(&bda);
if (!add_device(device)) {
free_device(device);
return error_reply(connection, msg,
"org.bluez.audio.Error.Failed",
"Unable to create new audio device");
}
if (!device->headset) {
device->headset = headset_init(device->object_path, NULL, 0);
created = TRUE;
}
if (!device->headset) {
remove_device(device);
return error_reply(connection, msg,
"org.bluez.audio.Error.Failed",
"Unable to init Headset interface");
}
done:
path = device->object_path;
reply = dbus_message_new_method_return(msg);
if (!reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
if (created)
dbus_connection_emit_signal(connection, AUDIO_MANAGER_PATH,
AUDIO_MANAGER_INTERFACE,
"HeadsetCreated",
DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID);
dbus_message_append_args(reply, DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID);
return send_message_and_unref(connection, reply);
}
static gint device_path_cmp(gconstpointer a, gconstpointer b)
{
const audio_device_t *device = a;
const char *path = b;
return strcmp(device->object_path, path);
}
static DBusHandlerResult am_remove_device(DBusConnection *conn,
DBusMessage *msg,
void *data)
{
DBusError derr;
DBusMessage *reply;
GSList *match;
const char *path;
audio_device_t *device;
dbus_error_init(&derr);
if (!dbus_message_get_args(msg, &derr,
DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID)) {
err_invalid_args(connection, msg, derr.message);
return DBUS_HANDLER_RESULT_HANDLED;
}
if (dbus_error_is_set(&derr)) {
err_invalid_args(connection, msg, derr.message);
dbus_error_free(&derr);
return DBUS_HANDLER_RESULT_HANDLED;
}
match = g_slist_find_custom(devices, path, device_path_cmp);
if (!match)
return error_reply(connection, msg,
"org.bluez.audio.Error.DoesNotExist",
"The headset does not exist");
reply = dbus_message_new_method_return(msg);
if (!reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
device = match->data;
remove_device(device);
if (default_hs == device) {
const char *param;
GSList *l;
default_hs = NULL;
for (l = devices; l != NULL; l = l->next) {
device = l->data;
if (device->headset)
default_hs = device;
}
param = default_hs ? default_hs->object_path : "";
dbus_connection_emit_signal(conn, AUDIO_MANAGER_PATH,
AUDIO_MANAGER_INTERFACE,
"DefaultHeadsetChanged",
DBUS_TYPE_STRING, ¶m,
DBUS_TYPE_INVALID);
}
dbus_connection_emit_signal(conn, AUDIO_MANAGER_PATH,
AUDIO_MANAGER_INTERFACE,
"HeadsetRemoved",
DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID);
dbus_connection_emit_signal(conn, AUDIO_MANAGER_PATH,
AUDIO_MANAGER_INTERFACE,
"DeviceRemoved",
DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID);
return send_message_and_unref(connection, reply);
}
static DBusHandlerResult am_remove_headset(DBusConnection *conn,
DBusMessage *msg,
void *data)
{
return am_remove_device(conn, msg, data);
}
static DBusHandlerResult am_list_headsets(DBusConnection *conn,
DBusMessage *msg,
void *data)
{
DBusMessageIter iter;
DBusMessageIter array_iter;
DBusMessage *reply;
GSList *l;
reply = dbus_message_new_method_return(msg);
if (!reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
DBUS_TYPE_STRING_AS_STRING, &array_iter);
for (l = devices; l != NULL; l = l->next) {
audio_device_t *device = l->data;
const char *path;
if (!device->headset)
continue;
path = device->object_path;
dbus_message_iter_append_basic(&array_iter,
DBUS_TYPE_STRING, &path);
}
dbus_message_iter_close_container(&iter, &array_iter);
return send_message_and_unref(connection, reply);
}
static DBusHandlerResult am_find_by_addr(DBusConnection *conn,
DBusMessage *msg,
void *data)
{
const char *path, *address;
DBusMessage *reply;
DBusError derr;
audio_device_t *device;
bdaddr_t bda;
dbus_error_init(&derr);
dbus_message_get_args(msg, &derr,
DBUS_TYPE_STRING, &address,
DBUS_TYPE_INVALID);
if (dbus_error_is_set(&derr)) {
err_invalid_args(connection, msg, derr.message);
dbus_error_free(&derr);
return DBUS_HANDLER_RESULT_HANDLED;
}
str2ba(address, &bda);
device = find_device(&bda);
if (!device)
return err_does_not_exist(conn, msg);
reply = dbus_message_new_method_return(msg);
if (!reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
path = device->object_path;
dbus_message_append_args(reply, DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID);
return send_message_and_unref(conn, reply);
}
static DBusHandlerResult am_get_default_headset(DBusConnection *conn,
DBusMessage *msg,
void *data)
{
DBusMessage *reply;
const char *path;
if (!default_hs)
return error_reply(connection, msg,
"org.bluez.audio.Error.DoesNotExist",
"There is no default headset");
reply = dbus_message_new_method_return(msg);
if (!reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
path = default_hs->object_path;
dbus_message_append_args(reply, DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID);
return send_message_and_unref(connection, reply);
}
static DBusHandlerResult am_change_default_headset(DBusConnection *conn,
DBusMessage *msg,
void *data)
{
DBusError derr;
DBusMessage *reply;
GSList *match;
const char *path;
dbus_error_init(&derr);
if (!dbus_message_get_args(msg, &derr,
DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID)) {
err_invalid_args(connection, msg, derr.message);
return DBUS_HANDLER_RESULT_HANDLED;
}
if (dbus_error_is_set(&derr)) {
err_invalid_args(connection, msg, derr.message);
dbus_error_free(&derr);
return DBUS_HANDLER_RESULT_HANDLED;
}
match = g_slist_find_custom(devices, path, device_path_cmp);
if (!match)
return error_reply(connection, msg,
"org.bluez.audio.Error.DoesNotExist",
"The headset does not exist");
reply = dbus_message_new_method_return(msg);
if (!reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
default_hs = match->data;
path = default_hs->object_path;
dbus_connection_emit_signal(conn, AUDIO_MANAGER_PATH,
AUDIO_MANAGER_INTERFACE,
"DefaultHeadsetChanged",
DBUS_TYPE_STRING, &path,
DBUS_TYPE_INVALID);
return send_message_and_unref(connection, reply);
}
static DBusMethodVTable manager_methods[] = {
{ "CreateDevice", am_create_device,
"sas", "s" },
{ "RemoveDevice", am_remove_device,
"s", "" },
{ "ListDevices", am_list_devices,
"as", "as" },
{ "CreateHeadset", am_create_headset,
"s", "s" },
{ "RemoveHeadset", am_remove_headset,
"s", "" },
{ "ListHeadsets", am_list_headsets,
"", "as" },
{ "FindDeviceByAddress", am_find_by_addr,
"s", "s" },
{ "DefaultHeadset", am_get_default_headset,
"", "s" },
{ "ChangeDefaultHeadset", am_change_default_headset,
"s", "" },
{ NULL, NULL, NULL, NULL },
};
static DBusSignalVTable manager_signals[] = {
{ "DeviceCreated", "s" },
{ "DeviceRemoved", "s" },
{ "HeadsetCreated", "s" },
{ "HeadsetRemoved", "s" },
{ "DefaultHeadsetChanged", "s" },
{ NULL, NULL }
};
int audio_init(DBusConnection *conn)
{
if (!dbus_connection_create_object_path(conn, AUDIO_MANAGER_PATH,
NULL, NULL)) {
error("D-Bus failed to register %s path", AUDIO_MANAGER_PATH);
return -1;
}
if (!dbus_connection_register_interface(conn, AUDIO_MANAGER_PATH,
AUDIO_MANAGER_INTERFACE,
manager_methods,
manager_signals, NULL)) {
error("Failed to register %s interface to %s",
AUDIO_MANAGER_INTERFACE, AUDIO_MANAGER_PATH);
dbus_connection_destroy_object_path(conn,
AUDIO_MANAGER_PATH);
return -1;
}
connection = dbus_connection_ref(conn);
return 0;
}
void audio_exit(void)
{
g_slist_foreach(devices, (GFunc) remove_device, NULL);
g_slist_free(devices);
devices = NULL;
dbus_connection_unref(connection);
connection = NULL;
}
int manager_get_device(uint8_t role, struct ipc_data_cfg *cfg)
{
GSList *l;
if (default_hs && default_hs->headset &&
headset_is_connected(default_hs->headset))
return headset_get_config(default_hs->headset, cfg);
for (l = devices; l != NULL; l = l->next) {
audio_device_t *dev = l->data;
if (dev->headset && headset_is_connected(dev->headset))
return headset_get_config(dev->headset, cfg);
}
return -1;
}
|