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