summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_bt_io.c
blob: 3cffe148aee68e13848afa1200f08c0ae2c3e405 (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
/* Copyright (c) 2014 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.
 */

#include <sys/time.h>
#include <syslog.h>

#include "cras_bt_io.h"
#include "cras_bt_device.h"
#include "cras_hfp_iodev.h"
#include "cras_utf8.h"
#include "cras_iodev.h"
#include "cras_iodev_list.h"
#include "sfh.h"
#include "utlist.h"

#define DEFAULT_BT_DEVICE_NAME "BLUETOOTH"

/* Extends cras_ionode to hold bluetooth profile information
 * so that iodevs of different profile(A2DP or HFP/HSP) can be
 * associated with the same bt_io.
 * Members:
 *    base - The base class cras_ionode.
 *    profile_dev - Pointer to the profile specific iodev.
 *    profile - The bluetooth profile profile_dev runs on.
 */
struct bt_node {
	struct cras_ionode base;
	struct cras_iodev *profile_dev;
	unsigned int profile;
};

/* The structure represents a virtual input or output device of a
 * bluetooth audio device, speaker or headset for example. A node
 * will be added to this virtual iodev for each profile supported
 * by the bluetooth audio device.
 * Member:
 *    base - The base class cras_iodev
 *    next_node_id - The index will give to the next node
 */
struct bt_io {
	struct cras_iodev base;
	unsigned int next_node_id;
	struct cras_bt_device *device;
};

/* Returns the active profile specific iodev. */
static struct cras_iodev *active_profile_dev(const struct cras_iodev *iodev)
{
	struct bt_node *active = (struct bt_node *)iodev->active_node;

	return active->profile_dev;
}

/* Adds a profile specific iodev to btio. */
static struct cras_ionode *add_profile_dev(struct cras_iodev *bt_iodev,
					   struct cras_iodev *dev,
					   enum cras_bt_device_profile profile)
{
	struct bt_node *n;
	struct bt_io *btio = (struct bt_io *)bt_iodev;

	n = (struct bt_node *)calloc(1, sizeof(*n));
	if (!n)
		return NULL;

	n->base.dev = bt_iodev;
	n->base.idx = btio->next_node_id++;
	n->base.type = CRAS_NODE_TYPE_BLUETOOTH;
	n->base.volume = 100;
	n->base.stable_id = dev->info.stable_id;
	n->base.capture_gain = 0;
	gettimeofday(&n->base.plugged_time, NULL);

	strcpy(n->base.name, dev->info.name);
	n->profile_dev = dev;
	n->profile = profile;

	cras_iodev_add_node(bt_iodev, &n->base);
	return &n->base;
}

/* Forces bt device to switch to use the given profile. Note that if
 * it has already been open for streaming, the new active profile will
 * take effect after the related btio(s) are reopened.
 */
static void bt_switch_to_profile(struct cras_bt_device *device,
				 enum cras_bt_device_profile profile)
{
	switch (profile) {
	case CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY:
	case CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY:
		cras_bt_device_set_active_profile(
			device,
			CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY |
				CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY);
		break;
	case CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE:
		cras_bt_device_set_active_profile(
			device, CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE);
		break;
	default:
		syslog(LOG_ERR, "Unexpect profile %u", profile);
		break;
	}
}

/* Switches the active profile to A2DP if it can. */
static void bt_possibly_switch_to_a2dp(struct bt_io *btio)
{
	if (!cras_bt_device_has_a2dp(btio->device))
		return;
	cras_bt_device_set_active_profile(btio->device,
					  CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE);
	cras_bt_device_switch_profile(btio->device, &btio->base);
}

/* Checks if bt device is active for the given profile.
 */
static int device_using_profile(struct cras_bt_device *device,
				unsigned int profile)
{
	return cras_bt_device_get_active_profile(device) & profile;
}

/* Checks if the condition is met to switch to a different profile based
 * on two rules:
 * (1) Prefer to use A2DP for output since the audio quality is better.
 * (2) Must use HFP/HSP for input since A2DP doesn't support audio input.
 *
 * If the profile switch happens, return non-zero error code, otherwise
 * return zero.
 */
static int open_dev(struct cras_iodev *iodev)
{
	struct bt_io *btio = (struct bt_io *)iodev;
	struct cras_iodev *dev = active_profile_dev(iodev);
	int rc;

	/* Force to use HFP if opening input dev. */
	if (device_using_profile(btio->device,
				 CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE) &&
	    iodev->direction == CRAS_STREAM_INPUT) {
		bt_switch_to_profile(btio->device,
				     CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY);
		cras_bt_device_switch_profile_enable_dev(btio->device, iodev);
		return -EAGAIN;
	}

	if (dev && dev->open_dev) {
		rc = dev->open_dev(dev);
		if (rc == 0)
			return 0;

		/* If input iodev open fails, switch profile back to A2DP. */
		if (iodev->direction == CRAS_STREAM_INPUT)
			bt_possibly_switch_to_a2dp(btio);
		return rc;
	}

	return 0;
}

static int update_supported_formats(struct cras_iodev *iodev)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	int rc, length, i;

	if (!dev)
		return -EINVAL;

	if (dev->update_supported_formats) {
		rc = dev->update_supported_formats(dev);
		if (rc)
			return rc;
	}

	/* Fill in the supported rates and channel counts. */
	for (length = 0; dev->supported_rates[length]; length++)
		;
	free(iodev->supported_rates);
	iodev->supported_rates = (size_t *)malloc(
		(length + 1) * sizeof(*iodev->supported_rates));
	for (i = 0; i < length + 1; i++)
		iodev->supported_rates[i] = dev->supported_rates[i];

	for (length = 0; dev->supported_channel_counts[length]; length++)
		;
	iodev->supported_channel_counts = (size_t *)malloc(
		(length + 1) * sizeof(*iodev->supported_channel_counts));
	for (i = 0; i < length + 1; i++)
		iodev->supported_channel_counts[i] =
			dev->supported_channel_counts[i];

	for (length = 0; dev->supported_formats[length]; length++)
		;

	iodev->supported_formats = (snd_pcm_format_t *)malloc(
		(length + 1) * sizeof(*iodev->supported_formats));
	for (i = 0; i < length + 1; i++)
		iodev->supported_formats[i] = dev->supported_formats[i];

	/* Record max supported channels into cras_iodev_info. */
	iodev->info.max_supported_channels = dev->info.max_supported_channels;
	return 0;
}

static int configure_dev(struct cras_iodev *iodev)
{
	int rc;
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;

	/* Fill back the format iodev is using. */
	if (dev->format == NULL) {
		dev->format = (struct cras_audio_format *)malloc(
			sizeof(*dev->format));
		if (!dev->format)
			return -ENOMEM;
		*dev->format = *iodev->format;
	}

	rc = dev->configure_dev(dev);
	if (rc)
		return rc;

	iodev->buffer_size = dev->buffer_size;
	iodev->min_buffer_level = dev->min_buffer_level;
	if (dev->start)
		dev->state = CRAS_IODEV_STATE_OPEN;
	else
		dev->state = CRAS_IODEV_STATE_NO_STREAM_RUN;

	return 0;
}

static int close_dev(struct cras_iodev *iodev)
{
	struct bt_io *btio = (struct bt_io *)iodev;
	int rc;
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;

	/* If input iodev is in open state and being closed, switch profile
	 * from HFP to A2DP. */
	if (cras_iodev_is_open(iodev) &&
	    device_using_profile(
		    btio->device,
		    CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY |
			    CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY) &&
	    (iodev->direction == CRAS_STREAM_INPUT))
		bt_possibly_switch_to_a2dp(btio);

	rc = dev->close_dev(dev);
	if (rc < 0)
		return rc;
	cras_iodev_free_format(iodev);
	dev->state = CRAS_IODEV_STATE_CLOSE;
	return 0;
}

static void set_bt_volume(struct cras_iodev *iodev)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return;

	if (dev->active_node)
		dev->active_node->volume = iodev->active_node->volume;

	/* The parent bt_iodev could set software_volume_needed flag for cases
	 * that software volume provides better experience across profiles
	 * (HFP and A2DP). Otherwise, use the profile specific implementation
	 * to adjust volume. */
	if (dev->set_volume && !iodev->software_volume_needed)
		dev->set_volume(dev);
}

static int frames_queued(const struct cras_iodev *iodev,
			 struct timespec *tstamp)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;
	return dev->frames_queued(dev, tstamp);
}

static int delay_frames(const struct cras_iodev *iodev)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;
	return dev->delay_frames(dev);
}

static int get_buffer(struct cras_iodev *iodev, struct cras_audio_area **area,
		      unsigned *frames)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;
	return dev->get_buffer(dev, area, frames);
}

static int put_buffer(struct cras_iodev *iodev, unsigned nwritten)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;
	return dev->put_buffer(dev, nwritten);
}

static int flush_buffer(struct cras_iodev *iodev)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;
	return dev->flush_buffer(dev);
}

/* If the first private iodev doesn't match the active profile stored on
 * device, select to the correct private iodev.
 */
static void update_active_node(struct cras_iodev *iodev, unsigned node_idx,
			       unsigned dev_enabled)
{
	struct bt_io *btio = (struct bt_io *)iodev;
	struct cras_ionode *node;
	struct bt_node *active = (struct bt_node *)iodev->active_node;
	struct cras_iodev *dev;
	int rc;

	if (device_using_profile(btio->device, active->profile))
		goto leave;

	/* Switch to the correct dev using active_profile. */
	DL_FOREACH (iodev->nodes, node) {
		struct bt_node *n = (struct bt_node *)node;
		if (n == active)
			continue;

		if (device_using_profile(btio->device, n->profile)) {
			active->profile = n->profile;
			active->profile_dev = n->profile_dev;

			/* Set volume for the new profile. */
			set_bt_volume(iodev);
		}
	}

leave:
	dev = active_profile_dev(iodev);
	if (dev && dev->update_active_node)
		dev->update_active_node(dev, node_idx, dev_enabled);

	/* Update supported formats here to get the supported formats from the
	 * new updated active profile dev.
	 */
	rc = update_supported_formats(iodev);
	if (rc) {
		syslog(LOG_ERR, "Failed to update supported formats, rc=%d",
		       rc);
	}
}

static int output_underrun(struct cras_iodev *iodev)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;

	if (dev->output_underrun) {
		dev->min_cb_level = iodev->min_cb_level;
		dev->max_cb_level = iodev->max_cb_level;
		dev->buffer_size = iodev->buffer_size;
		return dev->output_underrun(dev);
	}

	return 0;
}

static int no_stream(struct cras_iodev *iodev, int enable)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	int rc;

	if (!dev)
		return -EINVAL;

	if (dev->no_stream) {
		/*
		 * Copy iodev->min_cb_level and iodev->max_cb_level from the
		 * parent (i.e. bt_io).  no_stream() of hfp_alsa_iodev will
		 * use them.
		 * A2DP and HFP dev will use buffer and callback sizes to fill
		 * zeros in no stream state.
		 */
		dev->min_cb_level = iodev->min_cb_level;
		dev->max_cb_level = iodev->max_cb_level;
		dev->buffer_size = iodev->buffer_size;
		rc = dev->no_stream(dev, enable);
		if (rc < 0)
			return rc;
	}
	if (enable)
		dev->state = CRAS_IODEV_STATE_NO_STREAM_RUN;
	else
		dev->state = CRAS_IODEV_STATE_NORMAL_RUN;

	return 0;
}

static int is_free_running(const struct cras_iodev *iodev)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;

	if (dev->is_free_running)
		return dev->is_free_running(dev);

	return 0;
}

static int start(const struct cras_iodev *iodev)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	int rc;

	if (!dev)
		return -EINVAL;

	if (dev->start) {
		rc = dev->start(dev);
		if (rc)
			return rc;
	}
	dev->state = CRAS_IODEV_STATE_NORMAL_RUN;
	return 0;
}

static unsigned int frames_to_play_in_sleep(struct cras_iodev *iodev,
					    unsigned int *hw_level,
					    struct timespec *hw_tstamp)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev || !dev->frames_to_play_in_sleep)
		return cras_iodev_default_frames_to_play_in_sleep(
			iodev, hw_level, hw_tstamp);

	return dev->frames_to_play_in_sleep(dev, hw_level, hw_tstamp);
}

static int get_valid_frames(struct cras_iodev *iodev,
			    struct timespec *hw_tstamp)
{
	struct cras_iodev *dev = active_profile_dev(iodev);
	if (!dev)
		return -EINVAL;

	if (dev->get_valid_frames)
		return dev->get_valid_frames(dev, hw_tstamp);

	return cras_iodev_frames_queued(iodev, hw_tstamp);
}

struct cras_iodev *cras_bt_io_create(struct cras_bt_device *device,
				     struct cras_iodev *dev,
				     enum cras_bt_device_profile profile)
{
	int err;
	struct bt_io *btio;
	struct cras_iodev *iodev;
	struct cras_ionode *node;
	struct bt_node *active;

	if (!dev)
		return NULL;

	btio = (struct bt_io *)calloc(1, sizeof(*btio));
	if (!btio)
		goto error;
	btio->device = device;

	iodev = &btio->base;
	iodev->direction = dev->direction;
	strcpy(iodev->info.name, dev->info.name);
	iodev->info.stable_id = dev->info.stable_id;

	iodev->open_dev = open_dev;
	iodev->configure_dev = configure_dev;
	iodev->frames_queued = frames_queued;
	iodev->delay_frames = delay_frames;
	iodev->get_buffer = get_buffer;
	iodev->put_buffer = put_buffer;
	iodev->flush_buffer = flush_buffer;
	iodev->close_dev = close_dev;
	iodev->update_supported_formats = update_supported_formats;
	iodev->update_active_node = update_active_node;
	iodev->no_stream = no_stream;
	iodev->output_underrun = output_underrun;
	iodev->is_free_running = is_free_running;
	iodev->get_valid_frames = get_valid_frames;
	iodev->start = start;
	iodev->frames_to_play_in_sleep = frames_to_play_in_sleep;

	/* Input also checks |software_volume_needed| flag for using software
	 * gain. Keep it as false for BT input.
	 * TODO(hychao): after wide band speech mode is supported, consider
	 * enable software gain.
	 */
	if (dev->direction == CRAS_STREAM_OUTPUT) {
		iodev->software_volume_needed =
			!cras_bt_device_get_use_hardware_volume(device);
		iodev->set_volume = set_bt_volume;
	}

	/* Create the dummy node so it's the only node exposed to UI, and
	 * point it to the first profile dev. */
	active = (struct bt_node *)calloc(1, sizeof(*active));
	if (!active)
		goto error;
	active->base.dev = iodev;
	active->base.idx = btio->next_node_id++;
	active->base.type = dev->active_node->type;
	active->base.volume = 100;
	active->base.stable_id =
		SuperFastHash(cras_bt_device_object_path(device),
			      strlen(cras_bt_device_object_path(device)),
			      strlen(cras_bt_device_object_path(device)));
	active->base.ui_gain_scaler = 1.0f;
	/*
	 * If the same headset is connected in wideband mode, we shall assign
	 * a separate stable_id so the node priority/preference mechanism in
	 * Chrome UI doesn't break.
	 */
	if ((active->base.type == CRAS_NODE_TYPE_BLUETOOTH) &&
	    (dev->direction == CRAS_STREAM_INPUT))
		active->base.stable_id =
			SuperFastHash((const char *)&active->base.type,
				      sizeof(active->base.type),
				      active->base.stable_id);
	active->profile = profile;
	active->profile_dev = dev;
	strcpy(active->base.name, dev->info.name);
	/* The node name exposed to UI should be a valid UTF8 string. */
	if (!is_utf8_string(active->base.name))
		strcpy(active->base.name, DEFAULT_BT_DEVICE_NAME);
	cras_iodev_add_node(iodev, &active->base);

	node = add_profile_dev(&btio->base, dev, profile);
	if (node == NULL)
		goto error;

	/* Default active profile to a2dp whenever it's allowed. */
	if (!cras_bt_device_get_active_profile(device) ||
	    (profile == CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE &&
	     cras_bt_device_can_switch_to_a2dp(device)))
		bt_switch_to_profile(device, profile);

	if (iodev->direction == CRAS_STREAM_OUTPUT)
		err = cras_iodev_list_add_output(iodev);
	else
		err = cras_iodev_list_add_input(iodev);
	if (err)
		goto error;

	cras_iodev_set_active_node(iodev, &active->base);
	return &btio->base;

error:
	if (btio)
		free(btio);
	return NULL;
}

void cras_bt_io_free_resources(struct cras_iodev *bt_iodev)
{
	struct cras_ionode *node;
	struct bt_node *n;

	free(bt_iodev->supported_rates);
	free(bt_iodev->supported_channel_counts);
	free(bt_iodev->supported_formats);

	DL_FOREACH (bt_iodev->nodes, node) {
		n = (struct bt_node *)node;
		cras_iodev_rm_node(bt_iodev, node);
		free(n);
	}

	cras_iodev_free_resources(bt_iodev);
}

void cras_bt_io_destroy(struct cras_iodev *bt_iodev)
{
	int rc;
	struct bt_io *btio = (struct bt_io *)bt_iodev;

	if (bt_iodev->direction == CRAS_STREAM_OUTPUT)
		rc = cras_iodev_list_rm_output(bt_iodev);
	else
		rc = cras_iodev_list_rm_input(bt_iodev);
	if (rc == -EBUSY)
		return;

	cras_bt_io_free_resources(bt_iodev);
	free(btio);
}

struct cras_ionode *cras_bt_io_get_profile(struct cras_iodev *bt_iodev,
					   enum cras_bt_device_profile profile)
{
	struct cras_ionode *node;
	DL_FOREACH (bt_iodev->nodes, node) {
		struct bt_node *n = (struct bt_node *)node;
		if (n->profile & profile)
			return node;
	}
	return NULL;
}

int cras_bt_io_append(struct cras_iodev *bt_iodev, struct cras_iodev *dev,
		      enum cras_bt_device_profile profile)
{
	struct cras_ionode *node;
	struct bt_io *btio = (struct bt_io *)bt_iodev;

	if (cras_bt_io_get_profile(bt_iodev, profile))
		return -EEXIST;

	node = add_profile_dev(bt_iodev, dev, profile);
	if (!node)
		return -ENOMEM;

	if (profile == CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE &&
	    cras_bt_device_can_switch_to_a2dp(btio->device)) {
		bt_switch_to_profile(btio->device,
				     CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE);
		cras_bt_device_switch_profile(btio->device, bt_iodev);
		syslog(LOG_ERR, "Switch to A2DP on append");
	}
	return 0;
}

int cras_bt_io_on_profile(struct cras_iodev *bt_iodev,
			  enum cras_bt_device_profile profile)
{
	struct bt_node *btnode = (struct bt_node *)bt_iodev->active_node;
	return !!(profile & btnode->profile);
}

enum cras_bt_device_profile
cras_bt_io_profile_to_log(struct cras_iodev *bt_iodev)
{
	struct bt_node *btnode = (struct bt_node *)bt_iodev->active_node;

	if (btnode->profile & CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE)
		return CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE;

	if (hfp_iodev_is_hsp(btnode->profile_dev))
		return CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY;
	else
		return CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY;
}

unsigned int cras_bt_io_try_remove(struct cras_iodev *bt_iodev,
				   struct cras_iodev *dev)
{
	struct cras_ionode *node;
	struct bt_node *active, *btnode;
	unsigned int try_profile = 0;

	active = (struct bt_node *)bt_iodev->active_node;

	if (active->profile_dev == dev) {
		DL_FOREACH (bt_iodev->nodes, node) {
			btnode = (struct bt_node *)node;
			/* Skip the active node and the node we're trying
			 * to remove. */
			if (btnode == active || btnode->profile_dev == dev)
				continue;
			try_profile = btnode->profile;
			break;
		}
	} else {
		try_profile = active->profile;
	}
	return try_profile;
}

int cras_bt_io_remove(struct cras_iodev *bt_iodev, struct cras_iodev *dev)
{
	struct cras_ionode *node;
	struct bt_node *btnode;

	DL_FOREACH (bt_iodev->nodes, node) {
		btnode = (struct bt_node *)node;
		if (btnode->profile_dev != dev)
			continue;

		/* If this is the active node, reset it. Otherwise delete
		 * this node. */
		if (node == bt_iodev->active_node) {
			btnode->profile_dev = NULL;
			btnode->profile = 0;
		} else {
			DL_DELETE(bt_iodev->nodes, node);
			free(node);
		}
	}

	/* The node of active profile could have been removed, update it.
	 * Return err when fail to locate the active profile dev. */
	update_active_node(bt_iodev, 0, 1);
	btnode = (struct bt_node *)bt_iodev->active_node;
	if ((btnode->profile == 0) || (btnode->profile_dev == NULL))
		return -EINVAL;

	return 0;
}