1 /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #ifndef CRAS_BT_DEVICE_H_
7 #define CRAS_BT_DEVICE_H_
8 
9 #include <dbus/dbus.h>
10 
11 struct cras_bt_adapter;
12 struct cras_bt_device;
13 struct cras_iodev;
14 struct cras_timer;
15 
16 /* All the reasons for when CRAS schedule a suspend to BT device. */
17 enum cras_bt_device_suspend_reason {
18 	A2DP_LONG_TX_FAILURE,
19 	A2DP_TX_FATAL_ERROR,
20 	CONN_WATCH_TIME_OUT,
21 	HFP_SCO_SOCKET_ERROR,
22 	HFP_AG_START_FAILURE,
23 	UNEXPECTED_PROFILE_DROP,
24 };
25 
26 enum cras_bt_device_profile {
27 	CRAS_BT_DEVICE_PROFILE_A2DP_SOURCE = (1 << 0),
28 	CRAS_BT_DEVICE_PROFILE_A2DP_SINK = (1 << 1),
29 	CRAS_BT_DEVICE_PROFILE_AVRCP_REMOTE = (1 << 2),
30 	CRAS_BT_DEVICE_PROFILE_AVRCP_TARGET = (1 << 3),
31 	CRAS_BT_DEVICE_PROFILE_HFP_HANDSFREE = (1 << 4),
32 	CRAS_BT_DEVICE_PROFILE_HFP_AUDIOGATEWAY = (1 << 5),
33 	CRAS_BT_DEVICE_PROFILE_HSP_HEADSET = (1 << 6),
34 	CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY = (1 << 7)
35 };
36 
37 enum cras_bt_device_profile cras_bt_device_profile_from_uuid(const char *uuid);
38 
39 struct cras_bt_device *cras_bt_device_create(DBusConnection *conn,
40 					     const char *object_path);
41 
42 /*
43  * Removes a BT device from record. If this device is connected state,
44  * ensure the associated A2DP and HFP AG be removed cleanly.
45  */
46 void cras_bt_device_remove(struct cras_bt_device *device);
47 
48 void cras_bt_device_reset();
49 
50 struct cras_bt_device *cras_bt_device_get(const char *object_path);
51 
52 const char *cras_bt_device_object_path(const struct cras_bt_device *device);
53 
54 /* Gets the stable id of given cras_bt_device. */
55 int cras_bt_device_get_stable_id(const struct cras_bt_device *device);
56 
57 struct cras_bt_adapter *
58 cras_bt_device_adapter(const struct cras_bt_device *device);
59 const char *cras_bt_device_address(const struct cras_bt_device *device);
60 const char *cras_bt_device_name(const struct cras_bt_device *device);
61 int cras_bt_device_paired(const struct cras_bt_device *device);
62 int cras_bt_device_trusted(const struct cras_bt_device *device);
63 int cras_bt_device_connected(const struct cras_bt_device *device);
64 
65 void cras_bt_device_update_properties(struct cras_bt_device *device,
66 				      DBusMessageIter *properties_array_iter,
67 				      DBusMessageIter *invalidated_array_iter);
68 
69 /* Updates the supported profiles on dev. Expose for unit test. */
70 int cras_bt_device_set_supported_profiles(struct cras_bt_device *device,
71 					  unsigned int profiles);
72 
73 /* Checks if profile is claimed supported by the device. */
74 int cras_bt_device_supports_profile(const struct cras_bt_device *device,
75 				    enum cras_bt_device_profile profile);
76 
77 /* Sets if the BT audio device should use hardware volume.
78  * Args:
79  *    device - The remote bluetooth audio device.
80  *    use_hardware_volume - Set to true to indicate hardware volume
81  *        is preferred over software volume.
82  */
83 void cras_bt_device_set_use_hardware_volume(struct cras_bt_device *device,
84 					    int use_hardware_volume);
85 
86 /* Gets if the BT audio device should use hardware volume. */
87 int cras_bt_device_get_use_hardware_volume(struct cras_bt_device *device);
88 
89 /* Sets device connected state. Expose for unit test. */
90 void cras_bt_device_set_connected(struct cras_bt_device *device, int value);
91 
92 /* Forces disconnect the bt device. Used when handling audio error
93  * that we want to make the device be completely disconnected from
94  * host to reflect the state that an error has occurred.
95  * Args:
96  *    conn - The dbus connection.
97  *    device - The bt device to disconnect.
98  */
99 int cras_bt_device_disconnect(DBusConnection *conn,
100 			      struct cras_bt_device *device);
101 
102 /* Gets the SCO socket for the device.
103  * Args:
104  *     device - The device object to get SCO socket for.
105  *     codec - 1 for CVSD, 2 for mSBC
106  */
107 int cras_bt_device_sco_connect(struct cras_bt_device *device, int codec);
108 
109 /* Gets the SCO packet size in bytes, used by HFP iodev for audio I/O.
110  * The logic is built base on experience: for USB bus, respect BT Core spec
111  * that has clear recommendation of packet size of codecs (CVSD, mSBC).
112  * As for other buses, use the MTU value of SCO socket filled by driver.
113  * Args:
114  *    device - The bt device to query mtu.
115  *    sco_socket - The SCO socket.
116  *    codec - 1 for CVSD, 2 for mSBC per HFP 1.7 specification.
117  */
118 int cras_bt_device_sco_packet_size(struct cras_bt_device *device,
119 				   int sco_socket, int codec);
120 
121 /* Appends an iodev to bt device.
122  * Args:
123  *    device - The device to append iodev to.
124  *    iodev - The iodev to add.
125  *    profile - The profile of the iodev about to add.
126  */
127 void cras_bt_device_append_iodev(struct cras_bt_device *device,
128 				 struct cras_iodev *iodev,
129 				 enum cras_bt_device_profile profile);
130 
131 /* Removes an iodev from bt device.
132  * Args:
133  *    device - The device to remove iodev from.
134  *    iodev - The iodev to remove.
135  */
136 void cras_bt_device_rm_iodev(struct cras_bt_device *device,
137 			     struct cras_iodev *iodev);
138 
139 /* Gets the active profile of the bt device. */
140 unsigned int
141 cras_bt_device_get_active_profile(const struct cras_bt_device *device);
142 
143 /* Sets the active profile of the bt device. */
144 void cras_bt_device_set_active_profile(struct cras_bt_device *device,
145 				       unsigned int profile);
146 
147 /* Switches profile after the active profile of bt device has changed and
148  * enables bt iodev immediately. This function is used for profile switching
149  * at iodev open.
150  * Args:
151  *    device - The bluetooth device.
152  *    bt_iodev - The iodev triggers the reactivaion.
153  */
154 int cras_bt_device_switch_profile_enable_dev(struct cras_bt_device *device,
155 					     struct cras_iodev *bt_iodev);
156 
157 /* Switches profile after the active profile of bt device has changed. This
158  * function is used when we want to switch profile without changing the
159  * iodev's status.
160  * Args:
161  *    device - The bluetooth device.
162  *    bt_iodev - The iodev triggers the reactivaion.
163  */
164 int cras_bt_device_switch_profile(struct cras_bt_device *device,
165 				  struct cras_iodev *bt_iodev);
166 
167 void cras_bt_device_start_monitor();
168 
169 /* Checks if the device has an iodev for A2DP. */
170 int cras_bt_device_has_a2dp(struct cras_bt_device *device);
171 
172 /* Returns true if and only if device has an iodev for A2DP and the bt device
173  * is not opening for audio capture.
174  */
175 int cras_bt_device_can_switch_to_a2dp(struct cras_bt_device *device);
176 
177 /* Updates the volume to bt_device when a volume change event is reported. */
178 void cras_bt_device_update_hardware_volume(struct cras_bt_device *device,
179 					   int volume);
180 
181 /* Notifies bt_device that a2dp connection is configured. */
182 void cras_bt_device_a2dp_configured(struct cras_bt_device *device);
183 
184 /* Cancels any scheduled suspension of device. */
185 int cras_bt_device_cancel_suspend(struct cras_bt_device *device);
186 
187 /* Schedules device to suspend after given delay. */
188 int cras_bt_device_schedule_suspend(
189 	struct cras_bt_device *device, unsigned int msec,
190 	enum cras_bt_device_suspend_reason suspend_reason);
191 
192 /* Notifies bt device that audio gateway is initialized.
193  * Args:
194  *   device - The bluetooth device.
195  * Returns:
196  *   0 on success, error code otherwise.
197  */
198 int cras_bt_device_audio_gateway_initialized(struct cras_bt_device *device);
199 
200 /*
201  * Notifies bt device about a profile no longer works. It could be caused
202  * by initialize failure or fatal error has occurred.
203  * Args:
204  *    device - The bluetooth audio device.
205  *    profile - The BT audio profile that has dropped.
206  */
207 void cras_bt_device_notify_profile_dropped(struct cras_bt_device *device,
208 					   enum cras_bt_device_profile profile);
209 
210 /*
211  * Establishes SCO connection if it has not been established on the BT device.
212  * Note: this function should be only used for hfp_alsa_io.
213  * Args:
214  *    device - The bluetooth device.
215  *    codec - 1 for CVSD, 2 for mSBC
216  * Returns:
217  *   0 on success, error code otherwise.
218  */
219 int cras_bt_device_get_sco(struct cras_bt_device *device, int codec);
220 
221 /*
222  * Closes SCO connection if the caller is the last user for the connection on
223  * the BT device.
224  * Note: this function should be only used for hfp_alsa_io.
225  * Args:
226  *   device - The bluetooth device.
227  */
228 void cras_bt_device_put_sco(struct cras_bt_device *device);
229 
230 #endif /* CRAS_BT_DEVICE_H_ */
231