summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_bt_device.c
blob: 6b06dd13100b280297427b6f1f32741b469a85cc (plain)
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
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* for ppoll */
#endif

#include <dbus/dbus.h>

#include <errno.h>
#include <poll.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <syslog.h>

#include "bluetooth.h"
#include "cras_a2dp_endpoint.h"
#include "cras_bt_adapter.h"
#include "cras_bt_device.h"
#include "cras_bt_constants.h"
#include "cras_bt_log.h"
#include "cras_bt_io.h"
#include "cras_bt_profile.h"
#include "cras_hfp_ag_profile.h"
#include "cras_hfp_slc.h"
#include "cras_iodev.h"
#include "cras_iodev_list.h"
#include "cras_main_message.h"
#include "cras_server_metrics.h"
#include "cras_system_state.h"
#include "cras_tm.h"
#include "sfh.h"
#include "utlist.h"

/*
 * Bluetooth Core 5.0 spec, vol 4, part B, section 2 describes
 * the recommended HCI packet size in one USB transfer for CVSD
 * and MSBC codec.
 */
#define USB_MSBC_PKT_SIZE 60
#define USB_CVSD_PKT_SIZE 48
#define DEFAULT_SCO_PKT_SIZE USB_CVSD_PKT_SIZE

static const unsigned int PROFILE_SWITCH_DELAY_MS = 500;
static const unsigned int PROFILE_DROP_SUSPEND_DELAY_MS = 5000;

/* Check profile connections every 2 seconds and rerty 30 times maximum.
 * Attemp to connect profiles which haven't been ready every 3 retries.
 */
static const unsigned int CONN_WATCH_PERIOD_MS = 2000;
static const unsigned int CONN_WATCH_MAX_RETRIES = 30;

/* This is used when a critical SCO failure happens and is worth scheduling a
 * suspend in case for some reason BT headset stays connected in baseband and
 * confuses user.
 */
static const unsigned int SCO_SUSPEND_DELAY_MS = 5000;

static const unsigned int CRAS_SUPPORTED_PROFILES =
	CRAS_BT_DEVICE_PROFILE_A2DP_SINK | CRAS_BT_DEVICE_PROFILE_HFP_HANDSFREE;

/* Object to represent a general bluetooth device, and used to
 * associate with some CRAS modules if it supports audio.
 * Members:
 *    conn - The dbus connection object used to send message to bluetoothd.
 *    object_path - Object path of the bluetooth device.
 *    adapter - The object path of the adapter associates with this device.
 *    address - The BT address of this device.
 *    name - The readable name of this device.
 *    bluetooth_class - The bluetooth class of this device.
 *    paired - If this device is paired.
 *    trusted - If this device is trusted.
 *    connected - If this devices is connected.
 *    connected_profiles - OR'ed all connected audio profiles.
 *    profiles - OR'ed by all audio profiles this device supports.
 *    hidden_profiles - OR'ed by all audio profiles this device actually
 *        supports but is not scanned by BlueZ.
 *    bt_iodevs - The pointer to the cras_iodevs of this device.
 *    active_profile - The flag to indicate the active audio profile this
 *        device is currently using.
 *    conn_watch_retries - The retry count for conn_watch_timer.
 *    conn_watch_timer - The timer used to watch connected profiles and start
 *        BT audio input/ouput when all profiles are ready.
 *    suspend_timer - The timer used to suspend device.
 *    switch_profile_timer - The timer used to delay enabling iodev after
 *        profile switch.
 *    sco_fd - The file descriptor of the SCO connection.
 *    sco_ref_count - The reference counts of the SCO connection.
 *    suspend_reason - The reason code for why suspend is scheduled.
 *    stable_id - The unique and persistent id of this bt_device.
 */
struct cras_bt_device {
	DBusConnection *conn;
	char *object_path;
	char *adapter_obj_path;
	char *address;
	char *name;
	uint32_t bluetooth_class;
	int paired;
	int trusted;
	int connected;
	unsigned int connected_profiles;
	unsigned int profiles;
	unsigned int hidden_profiles;
	struct cras_iodev *bt_iodevs[CRAS_NUM_DIRECTIONS];
	unsigned int active_profile;
	int use_hardware_volume;
	int conn_watch_retries;
	struct cras_timer *conn_watch_timer;
	struct cras_timer *suspend_timer;
	struct cras_timer *switch_profile_timer;
	int sco_fd;
	size_t sco_ref_count;
	enum cras_bt_device_suspend_reason suspend_reason;
	unsigned int stable_id;

	struct cras_bt_device *prev, *next;
};

enum BT_DEVICE_COMMAND {
	BT_DEVICE_CANCEL_SUSPEND,
	BT_DEVICE_SCHEDULE_SUSPEND,
	BT_DEVICE_SWITCH_PROFILE,
	BT_DEVICE_SWITCH_PROFILE_ENABLE_DEV,
};

struct bt_device_msg {
	struct cras_main_message header;
	enum BT_DEVICE_COMMAND cmd;
	struct cras_bt_device *device;
	struct cras_iodev *dev;
	unsigned int arg1;
	unsigned int arg2;
};

static struct cras_bt_device *devices;

enum cras_bt_device_profile cras_bt_device_profile_from_uuid(const char *uuid)
{
	if (strcmp(uuid, HSP_HS_UUID) == 0)
		return CRAS_BT_DEVICE_PROFILE_HSP_HEADSET;
	else if (strcmp(uuid, HSP_AG_UUID) == 0)
		return CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY;
	else if (strcmp(uuid, HFP_HF_UUID) == 0)
		return CRAS_BT_DEVICE_PROFILE_HFP_HANDSFREE;
	else if (strcmp(uuid, HFP_AG_UUID) == 0)
		return CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY;
	else if (strcmp(uuid, A2DP_SOURCE_UUID) == 0)
		return CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE;
	else if (strcmp(uuid, A2DP_SINK_UUID) == 0)
		return CRAS_BT_DEVICE_PROFILE_A2DP_SINK;
	else if (strcmp(uuid, AVRCP_REMOTE_UUID) == 0)
		return CRAS_BT_DEVICE_PROFILE_AVRCP_REMOTE;
	else if (strcmp(uuid, AVRCP_TARGET_UUID) == 0)
		return CRAS_BT_DEVICE_PROFILE_AVRCP_TARGET;
	else
		return 0;
}

struct cras_bt_device *cras_bt_device_create(DBusConnection *conn,
					     const char *object_path)
{
	struct cras_bt_device *device;

	device = calloc(1, sizeof(*device));
	if (device == NULL)
		return NULL;

	device->conn = conn;
	device->object_path = strdup(object_path);
	if (device->object_path == NULL) {
		free(device);
		return NULL;
	}
	device->stable_id =
		SuperFastHash(device->object_path, strlen(device->object_path),
			      strlen(device->object_path));

	DL_APPEND(devices, device);

	return device;
}

static void on_connect_profile_reply(DBusPendingCall *pending_call, void *data)
{
	DBusMessage *reply;

	reply = dbus_pending_call_steal_reply(pending_call);
	dbus_pending_call_unref(pending_call);

	if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
		syslog(LOG_ERR, "Connect profile message replied error: %s",
		       dbus_message_get_error_name(reply));

	dbus_message_unref(reply);
}

static void on_disconnect_reply(DBusPendingCall *pending_call, void *data)
{
	DBusMessage *reply;

	reply = dbus_pending_call_steal_reply(pending_call);
	dbus_pending_call_unref(pending_call);

	if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
		syslog(LOG_ERR, "Disconnect message replied error");

	dbus_message_unref(reply);
}

int cras_bt_device_connect_profile(DBusConnection *conn,
				   struct cras_bt_device *device,
				   const char *uuid)
{
	DBusMessage *method_call;
	DBusError dbus_error;
	DBusPendingCall *pending_call;

	method_call =
		dbus_message_new_method_call(BLUEZ_SERVICE, device->object_path,
					     BLUEZ_INTERFACE_DEVICE,
					     "ConnectProfile");
	if (!method_call)
		return -ENOMEM;

	if (!dbus_message_append_args(method_call, DBUS_TYPE_STRING, &uuid,
				      DBUS_TYPE_INVALID))
		return -ENOMEM;

	dbus_error_init(&dbus_error);

	pending_call = NULL;
	if (!dbus_connection_send_with_reply(conn, method_call, &pending_call,
					     DBUS_TIMEOUT_USE_DEFAULT)) {
		dbus_message_unref(method_call);
		syslog(LOG_ERR, "Failed to send Disconnect message");
		return -EIO;
	}

	dbus_message_unref(method_call);
	if (!dbus_pending_call_set_notify(
		    pending_call, on_connect_profile_reply, conn, NULL)) {
		dbus_pending_call_cancel(pending_call);
		dbus_pending_call_unref(pending_call);
		return -EIO;
	}
	return 0;
}

int cras_bt_device_disconnect(DBusConnection *conn,
			      struct cras_bt_device *device)
{
	DBusMessage *method_call;
	DBusError dbus_error;
	DBusPendingCall *pending_call;

	method_call =
		dbus_message_new_method_call(BLUEZ_SERVICE, device->object_path,
					     BLUEZ_INTERFACE_DEVICE,
					     "Disconnect");
	if (!method_call)
		return -ENOMEM;

	dbus_error_init(&dbus_error);

	pending_call = NULL;
	if (!dbus_connection_send_with_reply(conn, method_call, &pending_call,
					     DBUS_TIMEOUT_USE_DEFAULT)) {
		dbus_message_unref(method_call);
		syslog(LOG_ERR, "Failed to send Disconnect message");
		return -EIO;
	}

	dbus_message_unref(method_call);
	if (!dbus_pending_call_set_notify(pending_call, on_disconnect_reply,
					  conn, NULL)) {
		dbus_pending_call_cancel(pending_call);
		dbus_pending_call_unref(pending_call);
		return -EIO;
	}
	return 0;
}

static void cras_bt_device_destroy(struct cras_bt_device *device)
{
	struct cras_tm *tm = cras_system_state_get_tm();
	DL_DELETE(devices, device);

	if (device->conn_watch_timer)
		cras_tm_cancel_timer(tm, device->conn_watch_timer);
	if (device->switch_profile_timer)
		cras_tm_cancel_timer(tm, device->switch_profile_timer);
	if (device->suspend_timer)
		cras_tm_cancel_timer(tm, device->suspend_timer);
	free(device->adapter_obj_path);
	free(device->object_path);
	free(device->address);
	free(device->name);
	free(device);
}

void cras_bt_device_remove(struct cras_bt_device *device)
{
	/*
	 * We expect BT stack to disconnect this device before removing it,
	 * but it may not the case if there's issue at BT side. Print error
	 * log whenever this happens.
	 */
	if (device->connected)
		syslog(LOG_ERR, "Removing dev with connected profiles %u",
		       device->connected_profiles);
	/*
	 * Possibly clean up the associated A2DP and HFP AG iodevs that are
	 * still accessing this device.
	 */
	cras_a2dp_suspend_connected_device(device);
	cras_hfp_ag_suspend_connected_device(device);
	cras_bt_device_destroy(device);
}

void cras_bt_device_reset()
{
	while (devices) {
		syslog(LOG_INFO, "Bluetooth Device: %s removed",
		       devices->address);
		cras_bt_device_destroy(devices);
	}
}

struct cras_bt_device *cras_bt_device_get(const char *object_path)
{
	struct cras_bt_device *device;

	DL_FOREACH (devices, device) {
		if (strcmp(device->object_path, object_path) == 0)
			return device;
	}

	return NULL;
}

const char *cras_bt_device_object_path(const struct cras_bt_device *device)
{
	return device->object_path;
}

int cras_bt_device_get_stable_id(const struct cras_bt_device *device)
{
	return device->stable_id;
}

struct cras_bt_adapter *
cras_bt_device_adapter(const struct cras_bt_device *device)
{
	return cras_bt_adapter_get(device->adapter_obj_path);
}

const char *cras_bt_device_address(const struct cras_bt_device *device)
{
	return device->address;
}

const char *cras_bt_device_name(const struct cras_bt_device *device)
{
	return device->name;
}

int cras_bt_device_paired(const struct cras_bt_device *device)
{
	return device->paired;
}

int cras_bt_device_trusted(const struct cras_bt_device *device)
{
	return device->trusted;
}

int cras_bt_device_connected(const struct cras_bt_device *device)
{
	return device->connected;
}

int cras_bt_device_supports_profile(const struct cras_bt_device *device,
				    enum cras_bt_device_profile profile)
{
	return !!(device->profiles & profile);
}

void cras_bt_device_append_iodev(struct cras_bt_device *device,
				 struct cras_iodev *iodev,
				 enum cras_bt_device_profile profile)
{
	struct cras_iodev *bt_iodev;

	bt_iodev = device->bt_iodevs[iodev->direction];

	if (bt_iodev) {
		cras_bt_io_append(bt_iodev, iodev, profile);
	} else {
		device->bt_iodevs[iodev->direction] =
			cras_bt_io_create(device, iodev, profile);
	}
}

/*
 * Sets the audio nodes to 'plugged' means UI can select it and open it
 * for streams. Sets to 'unplugged' to hide these nodes from UI, when device
 * disconnects in progress.
 */
static void bt_device_set_nodes_plugged(struct cras_bt_device *device,
					int plugged)
{
	struct cras_iodev *iodev;

	iodev = device->bt_iodevs[CRAS_STREAM_INPUT];
	if (iodev)
		cras_iodev_set_node_plugged(iodev->active_node, plugged);

	iodev = device->bt_iodevs[CRAS_STREAM_OUTPUT];
	if (iodev)
		cras_iodev_set_node_plugged(iodev->active_node, plugged);
}

static void bt_device_switch_profile(struct cras_bt_device *device,
				     struct cras_iodev *bt_iodev,
				     int enable_dev);

void cras_bt_device_rm_iodev(struct cras_bt_device *device,
			     struct cras_iodev *iodev)
{
	struct cras_iodev *bt_iodev;
	int rc;

	bt_device_set_nodes_plugged(device, 0);

	bt_iodev = device->bt_iodevs[iodev->direction];
	if (bt_iodev) {
		unsigned try_profile;

		/* Check what will the preffered profile be if we remove dev. */
		try_profile = cras_bt_io_try_remove(bt_iodev, iodev);
		if (!try_profile)
			goto destroy_bt_io;

		/* If the check result doesn't match with the active
		 * profile we are currently using, switch to the
		 * preffered profile before actually remove the iodev.
		 */
		if (!cras_bt_io_on_profile(bt_iodev, try_profile)) {
			device->active_profile = try_profile;
			bt_device_switch_profile(device, bt_iodev, 0);
		}
		rc = cras_bt_io_remove(bt_iodev, iodev);
		if (rc) {
			syslog(LOG_ERR, "Fail to fallback to profile %u",
			       try_profile);
			goto destroy_bt_io;
		}
	}
	return;

destroy_bt_io:
	device->bt_iodevs[iodev->direction] = NULL;
	cras_bt_io_destroy(bt_iodev);

	if (!device->bt_iodevs[CRAS_STREAM_INPUT] &&
	    !device->bt_iodevs[CRAS_STREAM_OUTPUT])
		cras_bt_device_set_active_profile(device, 0);
}

void cras_bt_device_a2dp_configured(struct cras_bt_device *device)
{
	BTLOG(btlog, BT_A2DP_CONFIGURED, device->connected_profiles, 0);
	device->connected_profiles |= CRAS_BT_DEVICE_PROFILE_A2DP_SINK;
}

int cras_bt_device_has_a2dp(struct cras_bt_device *device)
{
	struct cras_iodev *odev = device->bt_iodevs[CRAS_STREAM_OUTPUT];

	/* Check if there is an output iodev with A2DP node attached. */
	return odev &&
	       cras_bt_io_get_profile(odev, CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE);
}

int cras_bt_device_can_switch_to_a2dp(struct cras_bt_device *device)
{
	struct cras_iodev *idev = device->bt_iodevs[CRAS_STREAM_INPUT];

	return cras_bt_device_has_a2dp(device) &&
	       (!idev || !cras_iodev_is_open(idev));
}

static void bt_device_remove_conflict(struct cras_bt_device *device)
{
	struct cras_bt_device *connected;

	/* Suspend other HFP audio gateways that conflict with device. */
	cras_hfp_ag_remove_conflict(device);

	/* Check if there's conflict A2DP headset and suspend it. */
	connected = cras_a2dp_connected_device();
	if (connected && (connected != device))
		cras_a2dp_suspend_connected_device(connected);
}

static void bt_device_conn_watch_cb(struct cras_timer *timer, void *arg);

int cras_bt_device_audio_gateway_initialized(struct cras_bt_device *device)
{
	BTLOG(btlog, BT_AUDIO_GATEWAY_INIT, device->profiles, 0);
	/* Marks HFP/HSP as connected. This is what connection watcher
	 * checks. */
	device->connected_profiles |= (CRAS_BT_DEVICE_PROFILE_HFP_HANDSFREE |
				       CRAS_BT_DEVICE_PROFILE_HSP_HEADSET);

	/* If device connects HFP but not reporting correct UUID, manually add
	 * it to allow CRAS to enumerate audio node for it. We're seeing this
	 * behavior on qualification test software. */
	if (!cras_bt_device_supports_profile(
		    device, CRAS_BT_DEVICE_PROFILE_HFP_HANDSFREE)) {
		unsigned int profiles =
			device->profiles | CRAS_BT_DEVICE_PROFILE_HFP_HANDSFREE;
		cras_bt_device_set_supported_profiles(device, profiles);
		device->hidden_profiles |= CRAS_BT_DEVICE_PROFILE_HFP_HANDSFREE;
		bt_device_conn_watch_cb(NULL, (void *)device);
	}

	return 0;
}

unsigned int
cras_bt_device_get_active_profile(const struct cras_bt_device *device)
{
	return device->active_profile;
}

void cras_bt_device_set_active_profile(struct cras_bt_device *device,
				       unsigned int profile)
{
	device->active_profile = profile;
}

static void cras_bt_device_log_profile(const struct cras_bt_device *device,
				       enum cras_bt_device_profile profile)
{
	switch (profile) {
	case CRAS_BT_DEVICE_PROFILE_HFP_HANDSFREE:
		syslog(LOG_DEBUG, "Bluetooth Device: %s is HFP handsfree",
		       device->address);
		break;
	case CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY:
		syslog(LOG_DEBUG, "Bluetooth Device: %s is HFP audio gateway",
		       device->address);
		break;
	case CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE:
		syslog(LOG_DEBUG, "Bluetooth Device: %s is A2DP source",
		       device->address);
		break;
	case CRAS_BT_DEVICE_PROFILE_A2DP_SINK:
		syslog(LOG_DEBUG, "Bluetooth Device: %s is A2DP sink",
		       device->address);
		break;
	case CRAS_BT_DEVICE_PROFILE_AVRCP_REMOTE:
		syslog(LOG_DEBUG, "Bluetooth Device: %s is AVRCP remote",
		       device->address);
		break;
	case CRAS_BT_DEVICE_PROFILE_AVRCP_TARGET:
		syslog(LOG_DEBUG, "Bluetooth Device: %s is AVRCP target",
		       device->address);
		break;
	case CRAS_BT_DEVICE_PROFILE_HSP_HEADSET:
		syslog(LOG_DEBUG, "Bluetooth Device: %s is HSP headset",
		       device->address);
		break;
	case CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY:
		syslog(LOG_DEBUG, "Bluetooth Device: %s is HSP audio gateway",
		       device->address);
		break;
	}
}

static void cras_bt_device_log_profiles(const struct cras_bt_device *device,
					unsigned int profiles)
{
	unsigned int profile;

	while (profiles) {
		/* Get the LSB of profiles */
		profile = profiles & -profiles;
		cras_bt_device_log_profile(device, profile);
		profiles ^= profile;
	}
}

static int
cras_bt_device_is_profile_connected(const struct cras_bt_device *device,
				    enum cras_bt_device_profile profile)
{
	return !!(device->connected_profiles & profile);
}

static void
bt_device_schedule_suspend(struct cras_bt_device *device, unsigned int msec,
			   enum cras_bt_device_suspend_reason suspend_reason);

/* Callback used to periodically check if supported profiles are connected. */
static void bt_device_conn_watch_cb(struct cras_timer *timer, void *arg)
{
	struct cras_tm *tm;
	struct cras_bt_device *device = (struct cras_bt_device *)arg;
	int rc;
	bool a2dp_supported;
	bool a2dp_connected;
	bool hfp_supported;
	bool hfp_connected;

	BTLOG(btlog, BT_DEV_CONN_WATCH_CB, device->conn_watch_retries,
	      device->profiles);
	device->conn_watch_timer = NULL;

	/* Skip the callback if it is not an audio device. */
	if (!device->profiles)
		return;

	a2dp_supported = cras_bt_device_supports_profile(
		device, CRAS_BT_DEVICE_PROFILE_A2DP_SINK);
	a2dp_connected = cras_bt_device_is_profile_connected(
		device, CRAS_BT_DEVICE_PROFILE_A2DP_SINK);
	hfp_supported = cras_bt_device_supports_profile(
		device, CRAS_BT_DEVICE_PROFILE_HFP_HANDSFREE);
	hfp_connected = cras_bt_device_is_profile_connected(
		device, CRAS_BT_DEVICE_PROFILE_HFP_HANDSFREE);

	/* If not both A2DP and HFP are supported, simply wait for BlueZ
	 * to notify us about the new connection.
	 * Otherwise, when seeing one but not the other profile is connected,
	 * send message to ask BlueZ to connect the pending one.
	 */
	if (a2dp_supported && hfp_supported) {
		/* If both a2dp and hfp are not connected, do nothing. BlueZ
		 * should be responsible to notify connection of one profile.
		 */
		if (!a2dp_connected && hfp_connected)
			cras_bt_device_connect_profile(device->conn, device,
						       A2DP_SINK_UUID);
		if (a2dp_connected && !hfp_connected)
			cras_bt_device_connect_profile(device->conn, device,
						       HFP_HF_UUID);
	}

	if (a2dp_supported != a2dp_connected || hfp_supported != hfp_connected)
		goto arm_retry_timer;

	/* Expected profiles are all connected, no more connection watch
	 * callback will be scheduled.
	 * Base on the decision that we expose only the latest connected
	 * BT audio device to user, treat all other connected devices as
	 * conflict and remove them before we start A2DP/HFP of this device.
	 */
	bt_device_remove_conflict(device);

	if (cras_bt_device_is_profile_connected(
		    device, CRAS_BT_DEVICE_PROFILE_A2DP_SINK))
		cras_a2dp_start(device);

	if (cras_bt_device_is_profile_connected(
		    device, CRAS_BT_DEVICE_PROFILE_HFP_HANDSFREE)) {
		rc = cras_hfp_ag_start(device);
		if (rc) {
			syslog(LOG_ERR, "Start audio gateway failed, rc %d",
			       rc);
			bt_device_schedule_suspend(device, 0,
						   HFP_AG_START_FAILURE);
		}
	}
	bt_device_set_nodes_plugged(device, 1);
	return;

arm_retry_timer:

	syslog(LOG_DEBUG, "conn_watch_retries: %d", device->conn_watch_retries);

	if (--device->conn_watch_retries) {
		tm = cras_system_state_get_tm();
		device->conn_watch_timer =
			cras_tm_create_timer(tm, CONN_WATCH_PERIOD_MS,
					     bt_device_conn_watch_cb, device);
	} else {
		syslog(LOG_ERR, "Connection watch timeout.");
		bt_device_schedule_suspend(device, 0, CONN_WATCH_TIME_OUT);
	}
}

static void
cras_bt_device_start_new_conn_watch_timer(struct cras_bt_device *device)
{
	struct cras_tm *tm = cras_system_state_get_tm();

	if (device->conn_watch_timer) {
		cras_tm_cancel_timer(tm, device->conn_watch_timer);
	}
	device->conn_watch_retries = CONN_WATCH_MAX_RETRIES;
	device->conn_watch_timer = cras_tm_create_timer(
		tm, CONN_WATCH_PERIOD_MS, bt_device_conn_watch_cb, device);
}

static void bt_device_cancel_suspend(struct cras_bt_device *device);

void cras_bt_device_set_connected(struct cras_bt_device *device, int value)
{
	struct cras_tm *tm = cras_system_state_get_tm();
	if (!device->connected && value) {
		BTLOG(btlog, BT_DEV_CONNECTED, device->profiles,
		      device->stable_id);
	}

	if (device->connected && !value) {
		BTLOG(btlog, BT_DEV_DISCONNECTED, device->profiles,
		      device->stable_id);
		cras_bt_profile_on_device_disconnected(device);
		/* Device is disconnected, resets connected profiles and the
		 * suspend timer which scheduled earlier. */
		device->connected_profiles = 0;
		bt_device_cancel_suspend(device);
	}

	device->connected = value;

	if (!device->connected && device->conn_watch_timer) {
		cras_tm_cancel_timer(tm, device->conn_watch_timer);
		device->conn_watch_timer = NULL;
	}
}

void cras_bt_device_notify_profile_dropped(struct cras_bt_device *device,
					   enum cras_bt_device_profile profile)
{
	device->connected_profiles &= ~profile;

	/* Do nothing if device already disconnected. */
	if (!device->connected)
		return;

	/* If any profile, a2dp or hfp/hsp, has dropped for some reason,
	 * we shall make sure this device is fully disconnected within
	 * given time so that user does not see a headset stay connected
	 * but works with partial function.
	 */
	bt_device_schedule_suspend(device, PROFILE_DROP_SUSPEND_DELAY_MS,
				   UNEXPECTED_PROFILE_DROP);
}

/* Refresh the list of known supported profiles.
 * Args:
 *    device - The BT device holding scanned profiles bitmap.
 *    profiles - The OR'ed profiles the device claims to support as is notified
 *               by BlueZ.
 * Returns:
 *    The OR'ed profiles that are both supported by Cras and isn't previously
 *    supported by the device.
 */
int cras_bt_device_set_supported_profiles(struct cras_bt_device *device,
					  unsigned int profiles)
{
	/* Do nothing if no new profiles. */
	if ((device->profiles & profiles) == profiles)
		return 0;

	unsigned int new_profiles = profiles & ~device->profiles;

	/* Log this event as we might need to re-intialize the BT audio nodes
	 * if new audio profile is reported for already connected device. */
	if (device->connected && (new_profiles & CRAS_SUPPORTED_PROFILES))
		BTLOG(btlog, BT_NEW_AUDIO_PROFILE_AFTER_CONNECT,
		      device->profiles, new_profiles);
	cras_bt_device_log_profiles(device, new_profiles);
	device->profiles = profiles | device->hidden_profiles;

	return (new_profiles & CRAS_SUPPORTED_PROFILES);
}

void cras_bt_device_update_properties(struct cras_bt_device *device,
				      DBusMessageIter *properties_array_iter,
				      DBusMessageIter *invalidated_array_iter)
{
	int watch_needed = 0;
	while (dbus_message_iter_get_arg_type(properties_array_iter) !=
	       DBUS_TYPE_INVALID) {
		DBusMessageIter properties_dict_iter, variant_iter;
		const char *key;
		int type;

		dbus_message_iter_recurse(properties_array_iter,
					  &properties_dict_iter);

		dbus_message_iter_get_basic(&properties_dict_iter, &key);
		dbus_message_iter_next(&properties_dict_iter);

		dbus_message_iter_recurse(&properties_dict_iter, &variant_iter);
		type = dbus_message_iter_get_arg_type(&variant_iter);

		if (type == DBUS_TYPE_STRING || type == DBUS_TYPE_OBJECT_PATH) {
			const char *value;

			dbus_message_iter_get_basic(&variant_iter, &value);

			if (strcmp(key, "Adapter") == 0) {
				free(device->adapter_obj_path);
				device->adapter_obj_path = strdup(value);
			} else if (strcmp(key, "Address") == 0) {
				free(device->address);
				device->address = strdup(value);
			} else if (strcmp(key, "Alias") == 0) {
				free(device->name);
				device->name = strdup(value);
			}

		} else if (type == DBUS_TYPE_UINT32) {
			uint32_t value;

			dbus_message_iter_get_basic(&variant_iter, &value);

			if (strcmp(key, "Class") == 0)
				device->bluetooth_class = value;

		} else if (type == DBUS_TYPE_BOOLEAN) {
			int value;

			dbus_message_iter_get_basic(&variant_iter, &value);

			if (strcmp(key, "Paired") == 0) {
				device->paired = value;
			} else if (strcmp(key, "Trusted") == 0) {
				device->trusted = value;
			} else if (strcmp(key, "Connected") == 0) {
				cras_bt_device_set_connected(device, value);
				watch_needed = device->connected &&
					       cras_bt_device_supports_profile(
						       device,
						       CRAS_SUPPORTED_PROFILES);
			}

		} else if (strcmp(dbus_message_iter_get_signature(&variant_iter),
				  "as") == 0 &&
			   strcmp(key, "UUIDs") == 0) {
			DBusMessageIter uuid_array_iter;
			unsigned int profiles = 0;

			dbus_message_iter_recurse(&variant_iter,
						  &uuid_array_iter);
			while (dbus_message_iter_get_arg_type(
				       &uuid_array_iter) != DBUS_TYPE_INVALID) {
				const char *uuid;

				dbus_message_iter_get_basic(&uuid_array_iter,
							    &uuid);
				profiles |=
					cras_bt_device_profile_from_uuid(uuid);

				dbus_message_iter_next(&uuid_array_iter);
			}

			/* If updated properties includes new audio profile and
			 * device is connected, we need to start connection
			 * watcher. This is needed because on some bluetooth
			 * devices, supported profiles do not present when
			 * device interface is added and they are updated later.
			 */
			if (cras_bt_device_set_supported_profiles(device,
								  profiles))
				watch_needed = device->connected;
		}

		dbus_message_iter_next(properties_array_iter);
	}

	while (invalidated_array_iter &&
	       dbus_message_iter_get_arg_type(invalidated_array_iter) !=
		       DBUS_TYPE_INVALID) {
		const char *key;

		dbus_message_iter_get_basic(invalidated_array_iter, &key);

		if (strcmp(key, "Adapter") == 0) {
			free(device->adapter_obj_path);
			device->adapter_obj_path = NULL;
		} else if (strcmp(key, "Address") == 0) {
			free(device->address);
			device->address = NULL;
		} else if (strcmp(key, "Alias") == 0) {
			free(device->name);
			device->name = NULL;
		} else if (strcmp(key, "Class") == 0) {
			device->bluetooth_class = 0;
		} else if (strcmp(key, "Paired") == 0) {
			device->paired = 0;
		} else if (strcmp(key, "Trusted") == 0) {
			device->trusted = 0;
		} else if (strcmp(key, "Connected") == 0) {
			device->connected = 0;
		} else if (strcmp(key, "UUIDs") == 0) {
			device->profiles = device->hidden_profiles;
		}

		dbus_message_iter_next(invalidated_array_iter);
	}

	if (watch_needed)
		cras_bt_device_start_new_conn_watch_timer(device);
}

/* Converts bluetooth address string into sockaddr structure. The address
 * string is expected of the form 1A:2B:3C:4D:5E:6F, and each of the six
 * hex values will be parsed into sockaddr in inverse order.
 * Args:
 *    str - The string version of bluetooth address
 *    addr - The struct to be filled with converted address
 */
static int bt_address(const char *str, struct sockaddr *addr)
{
	int i;

	if (strlen(str) != 17) {
		syslog(LOG_ERR, "Invalid bluetooth address %s", str);
		return -1;
	}

	memset(addr, 0, sizeof(*addr));
	addr->sa_family = AF_BLUETOOTH;
	for (i = 5; i >= 0; i--) {
		addr->sa_data[i] = (unsigned char)strtol(str, NULL, 16);
		str += 3;
	}

	return 0;
}

/* Apply codec specific settings to the socket fd. */
static int apply_codec_settings(int fd, uint8_t codec)
{
	struct bt_voice voice;
	uint32_t pkt_status;

	memset(&voice, 0, sizeof(voice));
	if (codec == HFP_CODEC_ID_CVSD)
		return 0;

	if (codec != HFP_CODEC_ID_MSBC) {
		syslog(LOG_ERR, "Unsupported codec %d", codec);
		return -1;
	}

	voice.setting = BT_VOICE_TRANSPARENT;

	if (setsockopt(fd, SOL_BLUETOOTH, BT_VOICE, &voice, sizeof(voice)) <
	    0) {
		syslog(LOG_ERR, "Failed to apply voice setting");
		return -1;
	}

	pkt_status = 1;
	if (setsockopt(fd, SOL_BLUETOOTH, BT_PKT_STATUS, &pkt_status,
		       sizeof(pkt_status))) {
		syslog(LOG_ERR, "Failed to enable BT_PKT_STATUS");
	}
	return 0;
}

int cras_bt_device_sco_connect(struct cras_bt_device *device, int codec)
{
	int sk = 0, err;
	struct sockaddr addr;
	struct cras_bt_adapter *adapter;
	struct timespec timeout = { 1, 0 };
	struct pollfd pollfd;

	adapter = cras_bt_device_adapter(device);
	if (!adapter) {
		syslog(LOG_ERR, "No adapter found for device %s at SCO connect",
		       cras_bt_device_object_path(device));
		goto error;
	}

	sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET | O_NONBLOCK | SOCK_CLOEXEC,
		    BTPROTO_SCO);
	if (sk < 0) {
		syslog(LOG_ERR, "Failed to create socket: %s (%d)",
		       strerror(errno), errno);
		cras_server_metrics_hfp_sco_connection_error(
			CRAS_METRICS_SCO_SKT_OPEN_ERROR);
		return -errno;
	}

	/* Bind to local address */
	if (bt_address(cras_bt_adapter_address(adapter), &addr))
		goto error;
	if (bind(sk, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		syslog(LOG_ERR, "Failed to bind socket: %s (%d)",
		       strerror(errno), errno);
		goto error;
	}

	/* Connect to remote in nonblocking mode */
	fcntl(sk, F_SETFL, O_NONBLOCK);

	if (bt_address(cras_bt_device_address(device), &addr))
		goto error;

	err = apply_codec_settings(sk, codec);
	if (err)
		goto error;

	err = connect(sk, (struct sockaddr *)&addr, sizeof(addr));
	if (err && errno != EINPROGRESS) {
		syslog(LOG_ERR, "Failed to connect: %s (%d)", strerror(errno),
		       errno);
		cras_server_metrics_hfp_sco_connection_error(
			CRAS_METRICS_SCO_SKT_CONNECT_ERROR);
		goto error;
	}

	pollfd.fd = sk;
	pollfd.events = POLLOUT;

	err = ppoll(&pollfd, 1, &timeout, NULL);
	if (err <= 0) {
		syslog(LOG_ERR, "Connect SCO: poll for writable timeout");
		cras_server_metrics_hfp_sco_connection_error(
			CRAS_METRICS_SCO_SKT_POLL_TIMEOUT);
		goto error;
	}

	if (pollfd.revents & (POLLERR | POLLHUP)) {
		syslog(LOG_ERR,
		       "SCO socket error, revents: %u. Suspend in %u seconds",
		       pollfd.revents, SCO_SUSPEND_DELAY_MS);
		cras_server_metrics_hfp_sco_connection_error(
			CRAS_METRICS_SCO_SKT_POLL_ERR_HUP);
		bt_device_schedule_suspend(device, SCO_SUSPEND_DELAY_MS,
					   HFP_SCO_SOCKET_ERROR);
		goto error;
	}

	cras_server_metrics_hfp_sco_connection_error(
		CRAS_METRICS_SCO_SKT_SUCCESS);
	BTLOG(btlog, BT_SCO_CONNECT, 1, sk);
	return sk;

error:
	BTLOG(btlog, BT_SCO_CONNECT, 0, sk);
	if (sk)
		close(sk);
	return -1;
}

int cras_bt_device_sco_packet_size(struct cras_bt_device *device,
				   int sco_socket, int codec)
{
	struct sco_options so;
	socklen_t len = sizeof(so);
	struct cras_bt_adapter *adapter;
	uint32_t wbs_pkt_len = 0;
	socklen_t optlen = sizeof(wbs_pkt_len);

	adapter = cras_bt_adapter_get(device->adapter_obj_path);

	if (cras_bt_adapter_on_usb(adapter)) {
		if (codec == HFP_CODEC_ID_MSBC) {
			/* BT_SNDMTU and BT_RCVMTU return the same value. */
			if (getsockopt(sco_socket, SOL_BLUETOOTH, BT_SNDMTU,
				       &wbs_pkt_len, &optlen))
				syslog(LOG_ERR, "Failed to get BT_SNDMTU");

			return (wbs_pkt_len > 0) ? wbs_pkt_len :
						   USB_MSBC_PKT_SIZE;
		} else {
			return USB_CVSD_PKT_SIZE;
		}
	}

	/* For non-USB cases, query the SCO MTU from driver. */
	if (getsockopt(sco_socket, SOL_SCO, SCO_OPTIONS, &so, &len) < 0) {
		syslog(LOG_ERR, "Get SCO options error: %s", strerror(errno));
		return DEFAULT_SCO_PKT_SIZE;
	}
	return so.mtu;
}

void cras_bt_device_set_use_hardware_volume(struct cras_bt_device *device,
					    int use_hardware_volume)
{
	struct cras_iodev *iodev;

	device->use_hardware_volume = use_hardware_volume;
	iodev = device->bt_iodevs[CRAS_STREAM_OUTPUT];
	if (iodev)
		iodev->software_volume_needed = !use_hardware_volume;
}

int cras_bt_device_get_use_hardware_volume(struct cras_bt_device *device)
{
	return device->use_hardware_volume;
}

static void init_bt_device_msg(struct bt_device_msg *msg,
			       enum BT_DEVICE_COMMAND cmd,
			       struct cras_bt_device *device,
			       struct cras_iodev *dev, unsigned int arg1,
			       unsigned int arg2)
{
	memset(msg, 0, sizeof(*msg));
	msg->header.type = CRAS_MAIN_BT;
	msg->header.length = sizeof(*msg);
	msg->cmd = cmd;
	msg->device = device;
	msg->dev = dev;
	msg->arg1 = arg1;
	msg->arg2 = arg2;
}

int cras_bt_device_cancel_suspend(struct cras_bt_device *device)
{
	struct bt_device_msg msg;
	int rc;

	init_bt_device_msg(&msg, BT_DEVICE_CANCEL_SUSPEND, device, NULL, 0, 0);
	rc = cras_main_message_send((struct cras_main_message *)&msg);
	return rc;
}

int cras_bt_device_schedule_suspend(
	struct cras_bt_device *device, unsigned int msec,
	enum cras_bt_device_suspend_reason suspend_reason)
{
	struct bt_device_msg msg;
	int rc;

	init_bt_device_msg(&msg, BT_DEVICE_SCHEDULE_SUSPEND, device, NULL, msec,
			   suspend_reason);
	rc = cras_main_message_send((struct cras_main_message *)&msg);
	return rc;
}

/* This diagram describes how the profile switching happens. When
 * certain conditions met, bt iodev will call the APIs below to interact
 * with main thread to switch to another active profile.
 *
 * Audio thread:
 *  +--------------------------------------------------------------+
 *  | bt iodev                                                     |
 *  |              +------------------+    +-----------------+     |
 *  |              | condition met to |    | open, close, or |     |
 *  |           +--| change profile   |<---| append profile  |<--+ |
 *  |           |  +------------------+    +-----------------+   | |
 *  +-----------|------------------------------------------------|-+
 *              |                                                |
 * Main thread: |
 *  +-----------|------------------------------------------------|-+
 *  |           |                                                | |
 *  |           |      +------------+     +----------------+     | |
 *  |           +----->| set active |---->| switch profile |-----+ |
 *  |                  | profile    |     +----------------+       |
 *  | bt device        +------------+                              |
 *  +--------------------------------------------------------------+
 */
int cras_bt_device_switch_profile_enable_dev(struct cras_bt_device *device,
					     struct cras_iodev *bt_iodev)
{
	struct bt_device_msg msg;
	int rc;

	init_bt_device_msg(&msg, BT_DEVICE_SWITCH_PROFILE_ENABLE_DEV, device,
			   bt_iodev, 0, 0);
	rc = cras_main_message_send((struct cras_main_message *)&msg);
	return rc;
}

int cras_bt_device_switch_profile(struct cras_bt_device *device,
				  struct cras_iodev *bt_iodev)
{
	struct bt_device_msg msg;
	int rc;

	init_bt_device_msg(&msg, BT_DEVICE_SWITCH_PROFILE, device, bt_iodev, 0,
			   0);
	rc = cras_main_message_send((struct cras_main_message *)&msg);
	return rc;
}

static void profile_switch_delay_cb(struct cras_timer *timer, void *arg)
{
	struct cras_bt_device *device = (struct cras_bt_device *)arg;
	struct cras_iodev *iodev;

	device->switch_profile_timer = NULL;
	iodev = device->bt_iodevs[CRAS_STREAM_OUTPUT];
	if (!iodev)
		return;

	/*
	 * During the |PROFILE_SWITCH_DELAY_MS| time interval, BT iodev could
	 * have been enabled by others, and its active profile may have changed.
	 * If iodev has been enabled, that means it has already picked up a
	 * reasonable profile to use and audio thread is accessing iodev now.
	 * We should NOT call into update_active_node from main thread
	 * because that may mess up the active node content.
	 */
	iodev->update_active_node(iodev, 0, 1);
	cras_iodev_list_resume_dev(iodev->info.idx);
}

static void bt_device_switch_profile_with_delay(struct cras_bt_device *device,
						unsigned int delay_ms)
{
	struct cras_tm *tm = cras_system_state_get_tm();

	if (device->switch_profile_timer) {
		cras_tm_cancel_timer(tm, device->switch_profile_timer);
		device->switch_profile_timer = NULL;
	}
	device->switch_profile_timer = cras_tm_create_timer(
		tm, delay_ms, profile_switch_delay_cb, device);
}

/* Switches associated bt iodevs to use the active profile. This is
 * achieved by close the iodevs, update their active nodes, and then
 * finally reopen them. */
static void bt_device_switch_profile(struct cras_bt_device *device,
				     struct cras_iodev *bt_iodev,
				     int enable_dev)
{
	struct cras_iodev *iodev;
	int dir;

	/* If a bt iodev is active, temporarily force close it.
	 * Note that we need to check all bt_iodevs for the situation that both
	 * input and output are active while switches from HFP/HSP to A2DP.
	 */
	for (dir = 0; dir < CRAS_NUM_DIRECTIONS; dir++) {
		iodev = device->bt_iodevs[dir];
		if (!iodev)
			continue;
		cras_iodev_list_suspend_dev(iodev->info.idx);
	}

	for (dir = 0; dir < CRAS_NUM_DIRECTIONS; dir++) {
		iodev = device->bt_iodevs[dir];
		if (!iodev)
			continue;

		/* If the iodev was active or this profile switching is
		 * triggered at opening iodev, add it to active dev list.
		 * However for the output iodev, adding it back to active dev
		 * list could cause immediate switching from HFP to A2DP if
		 * there exists an output stream. Certain headset/speaker
		 * would fail to playback afterwards when the switching happens
		 * too soon, so put this task in a delayed callback.
		 */
		if (dir == CRAS_STREAM_INPUT) {
			iodev->update_active_node(iodev, 0, 1);
			cras_iodev_list_resume_dev(iodev->info.idx);
		} else {
			bt_device_switch_profile_with_delay(
				device, PROFILE_SWITCH_DELAY_MS);
		}
	}
}

static void bt_device_suspend_cb(struct cras_timer *timer, void *arg)
{
	struct cras_bt_device *device = (struct cras_bt_device *)arg;

	BTLOG(btlog, BT_DEV_SUSPEND_CB, device->profiles,
	      device->suspend_reason);
	device->suspend_timer = NULL;

	/* Error log the reason so we can track them in user reports. */
	switch (device->suspend_reason) {
	case A2DP_LONG_TX_FAILURE:
		syslog(LOG_ERR, "Suspend dev: A2DP long Tx failure");
		break;
	case A2DP_TX_FATAL_ERROR:
		syslog(LOG_ERR, "Suspend dev: A2DP Tx fatal error");
		break;
	case CONN_WATCH_TIME_OUT:
		syslog(LOG_ERR, "Suspend dev: Conn watch times out");
		break;
	case HFP_SCO_SOCKET_ERROR:
		syslog(LOG_ERR, "Suspend dev: SCO socket error");
		break;
	case HFP_AG_START_FAILURE:
		syslog(LOG_ERR, "Suspend dev: HFP AG start failure");
		break;
	case UNEXPECTED_PROFILE_DROP:
		syslog(LOG_ERR, "Suspend dev: Unexpected profile drop");
		break;
	}

	cras_a2dp_suspend_connected_device(device);
	cras_hfp_ag_suspend_connected_device(device);
	cras_bt_device_disconnect(device->conn, device);
}

static void
bt_device_schedule_suspend(struct cras_bt_device *device, unsigned int msec,
			   enum cras_bt_device_suspend_reason suspend_reason)
{
	struct cras_tm *tm = cras_system_state_get_tm();

	if (device->suspend_timer)
		return;
	device->suspend_reason = suspend_reason;
	device->suspend_timer =
		cras_tm_create_timer(tm, msec, bt_device_suspend_cb, device);
}

static void bt_device_cancel_suspend(struct cras_bt_device *device)
{
	struct cras_tm *tm = cras_system_state_get_tm();
	if (device->suspend_timer == NULL)
		return;
	cras_tm_cancel_timer(tm, device->suspend_timer);
	device->suspend_timer = NULL;
}

static void bt_device_process_msg(struct cras_main_message *msg, void *arg)
{
	struct bt_device_msg *bt_msg = (struct bt_device_msg *)msg;
	struct cras_bt_device *device = NULL;

	DL_FOREACH (devices, device) {
		if (device == bt_msg->device)
			break;
	}

	/* Do nothing if target device no longer exists. */
	if (device == NULL)
		return;

	switch (bt_msg->cmd) {
	case BT_DEVICE_SWITCH_PROFILE:
		bt_device_switch_profile(bt_msg->device, bt_msg->dev, 0);
		break;
	case BT_DEVICE_SWITCH_PROFILE_ENABLE_DEV:
		bt_device_switch_profile(bt_msg->device, bt_msg->dev, 1);
		break;
	case BT_DEVICE_SCHEDULE_SUSPEND:
		bt_device_schedule_suspend(bt_msg->device, bt_msg->arg1,
					   bt_msg->arg2);
		break;
	case BT_DEVICE_CANCEL_SUSPEND:
		bt_device_cancel_suspend(bt_msg->device);
		break;
	default:
		break;
	}
}

void cras_bt_device_start_monitor()
{
	cras_main_message_add_handler(CRAS_MAIN_BT, bt_device_process_msg,
				      NULL);
}

void cras_bt_device_update_hardware_volume(struct cras_bt_device *device,
					   int volume)
{
	struct cras_iodev *iodev;

	iodev = device->bt_iodevs[CRAS_STREAM_OUTPUT];
	if (iodev == NULL)
		return;

	/* Check if this BT device is okay to use hardware volume. If not
	 * then ignore the reported volume change event.
	 */
	if (!cras_bt_device_get_use_hardware_volume(device))
		return;

	iodev->active_node->volume = volume;
	cras_iodev_list_notify_node_volume(iodev->active_node);
}

int cras_bt_device_get_sco(struct cras_bt_device *device, int codec)
{
	if (device->sco_ref_count == 0) {
		device->sco_fd = cras_bt_device_sco_connect(device, codec);
		if (device->sco_fd < 0)
			return device->sco_fd;
	}

	++device->sco_ref_count;
	return 0;
}

void cras_bt_device_put_sco(struct cras_bt_device *device)
{
	if (device->sco_ref_count == 0)
		return;

	if (--device->sco_ref_count == 0)
		close(device->sco_fd);
}