summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_bt_device.h
blob: 9d3a2b9ee74342b6d499c08b10bf517fd793b407 (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
/* 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 CRAS_BT_DEVICE_H_
#define CRAS_BT_DEVICE_H_

#include <dbus/dbus.h>

struct cras_bt_adapter;
struct cras_bt_device;
struct cras_iodev;
struct cras_timer;

/* All the reasons for when CRAS schedule a suspend to BT device. */
enum cras_bt_device_suspend_reason {
	A2DP_LONG_TX_FAILURE,
	A2DP_TX_FATAL_ERROR,
	CONN_WATCH_TIME_OUT,
	HFP_SCO_SOCKET_ERROR,
	HFP_AG_START_FAILURE,
	UNEXPECTED_PROFILE_DROP,
};

enum cras_bt_device_profile {
	CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE = (1 << 0),
	CRAS_BT_DEVICE_PROFILE_A2DP_SINK = (1 << 1),
	CRAS_BT_DEVICE_PROFILE_AVRCP_REMOTE = (1 << 2),
	CRAS_BT_DEVICE_PROFILE_AVRCP_TARGET = (1 << 3),
	CRAS_BT_DEVICE_PROFILE_HFP_HANDSFREE = (1 << 4),
	CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY = (1 << 5),
	CRAS_BT_DEVICE_PROFILE_HSP_HEADSET = (1 << 6),
	CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY = (1 << 7)
};

enum cras_bt_device_profile cras_bt_device_profile_from_uuid(const char *uuid);

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

/*
 * Removes a BT device from record. If this device is connected state,
 * ensure the associated A2DP and HFP AG be removed cleanly.
 */
void cras_bt_device_remove(struct cras_bt_device *device);

void cras_bt_device_reset();

struct cras_bt_device *cras_bt_device_get(const char *object_path);

const char *cras_bt_device_object_path(const struct cras_bt_device *device);

/* Gets the stable id of given cras_bt_device. */
int cras_bt_device_get_stable_id(const struct cras_bt_device *device);

struct cras_bt_adapter *
cras_bt_device_adapter(const struct cras_bt_device *device);
const char *cras_bt_device_address(const struct cras_bt_device *device);
const char *cras_bt_device_name(const struct cras_bt_device *device);
int cras_bt_device_paired(const struct cras_bt_device *device);
int cras_bt_device_trusted(const struct cras_bt_device *device);
int cras_bt_device_connected(const struct cras_bt_device *device);

void cras_bt_device_update_properties(struct cras_bt_device *device,
				      DBusMessageIter *properties_array_iter,
				      DBusMessageIter *invalidated_array_iter);

/* Updates the supported profiles on dev. Expose for unit test. */
int cras_bt_device_set_supported_profiles(struct cras_bt_device *device,
					  unsigned int profiles);

/* Checks if profile is claimed supported by the device. */
int cras_bt_device_supports_profile(const struct cras_bt_device *device,
				    enum cras_bt_device_profile profile);

/* Sets if the BT audio device should use hardware volume.
 * Args:
 *    device - The remote bluetooth audio device.
 *    use_hardware_volume - Set to true to indicate hardware volume
 *        is preferred over software volume.
 */
void cras_bt_device_set_use_hardware_volume(struct cras_bt_device *device,
					    int use_hardware_volume);

/* Gets if the BT audio device should use hardware volume. */
int cras_bt_device_get_use_hardware_volume(struct cras_bt_device *device);

/* Sets device connected state. Expose for unit test. */
void cras_bt_device_set_connected(struct cras_bt_device *device, int value);

/* Forces disconnect the bt device. Used when handling audio error
 * that we want to make the device be completely disconnected from
 * host to reflect the state that an error has occurred.
 * Args:
 *    conn - The dbus connection.
 *    device - The bt device to disconnect.
 */
int cras_bt_device_disconnect(DBusConnection *conn,
			      struct cras_bt_device *device);

/* Gets the SCO socket for the device.
 * Args:
 *     device - The device object to get SCO socket for.
 *     codec - 1 for CVSD, 2 for mSBC
 */
int cras_bt_device_sco_connect(struct cras_bt_device *device, int codec);

/* Gets the SCO packet size in bytes, used by HFP iodev for audio I/O.
 * The logic is built base on experience: for USB bus, respect BT Core spec
 * that has clear recommendation of packet size of codecs (CVSD, mSBC).
 * As for other buses, use the MTU value of SCO socket filled by driver.
 * Args:
 *    device - The bt device to query mtu.
 *    sco_socket - The SCO socket.
 *    codec - 1 for CVSD, 2 for mSBC per HFP 1.7 specification.
 */
int cras_bt_device_sco_packet_size(struct cras_bt_device *device,
				   int sco_socket, int codec);

/* Appends an iodev to bt device.
 * Args:
 *    device - The device to append iodev to.
 *    iodev - The iodev to add.
 *    profile - The profile of the iodev about to add.
 */
void cras_bt_device_append_iodev(struct cras_bt_device *device,
				 struct cras_iodev *iodev,
				 enum cras_bt_device_profile profile);

/* Removes an iodev from bt device.
 * Args:
 *    device - The device to remove iodev from.
 *    iodev - The iodev to remove.
 */
void cras_bt_device_rm_iodev(struct cras_bt_device *device,
			     struct cras_iodev *iodev);

/* Gets the active profile of the bt device. */
unsigned int
cras_bt_device_get_active_profile(const struct cras_bt_device *device);

/* Sets the active profile of the bt device. */
void cras_bt_device_set_active_profile(struct cras_bt_device *device,
				       unsigned int profile);

/* Switches profile after the active profile of bt device has changed and
 * enables bt iodev immediately. This function is used for profile switching
 * at iodev open.
 * Args:
 *    device - The bluetooth device.
 *    bt_iodev - The iodev triggers the reactivaion.
 */
int cras_bt_device_switch_profile_enable_dev(struct cras_bt_device *device,
					     struct cras_iodev *bt_iodev);

/* Switches profile after the active profile of bt device has changed. This
 * function is used when we want to switch profile without changing the
 * iodev's status.
 * Args:
 *    device - The bluetooth device.
 *    bt_iodev - The iodev triggers the reactivaion.
 */
int cras_bt_device_switch_profile(struct cras_bt_device *device,
				  struct cras_iodev *bt_iodev);

void cras_bt_device_start_monitor();

/* Checks if the device has an iodev for A2DP. */
int cras_bt_device_has_a2dp(struct cras_bt_device *device);

/* Returns true if and only if device has an iodev for A2DP and the bt device
 * is not opening for audio capture.
 */
int cras_bt_device_can_switch_to_a2dp(struct cras_bt_device *device);

/* Updates the volume to bt_device when a volume change event is reported. */
void cras_bt_device_update_hardware_volume(struct cras_bt_device *device,
					   int volume);

/* Notifies bt_device that a2dp connection is configured. */
void cras_bt_device_a2dp_configured(struct cras_bt_device *device);

/* Cancels any scheduled suspension of device. */
int cras_bt_device_cancel_suspend(struct cras_bt_device *device);

/* Schedules device to suspend after given delay. */
int cras_bt_device_schedule_suspend(
	struct cras_bt_device *device, unsigned int msec,
	enum cras_bt_device_suspend_reason suspend_reason);

/* Notifies bt device that audio gateway is initialized.
 * Args:
 *   device - The bluetooth device.
 * Returns:
 *   0 on success, error code otherwise.
 */
int cras_bt_device_audio_gateway_initialized(struct cras_bt_device *device);

/*
 * Notifies bt device about a profile no longer works. It could be caused
 * by initialize failure or fatal error has occurred.
 * Args:
 *    device - The bluetooth audio device.
 *    profile - The BT audio profile that has dropped.
 */
void cras_bt_device_notify_profile_dropped(struct cras_bt_device *device,
					   enum cras_bt_device_profile profile);

/*
 * Establishes SCO connection if it has not been established on the BT device.
 * Note: this function should be only used for hfp_alsa_io.
 * Args:
 *    device - The bluetooth device.
 *    codec - 1 for CVSD, 2 for mSBC
 * Returns:
 *   0 on success, error code otherwise.
 */
int cras_bt_device_get_sco(struct cras_bt_device *device, int codec);

/*
 * Closes SCO connection if the caller is the last user for the connection on
 * the BT device.
 * Note: this function should be only used for hfp_alsa_io.
 * Args:
 *   device - The bluetooth device.
 */
void cras_bt_device_put_sco(struct cras_bt_device *device);

#endif /* CRAS_BT_DEVICE_H_ */