1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 // ***************************************************************************** 39 // 40 // HFP Hands-Free (HF) unit and Audio-Gateway Commons 41 // 42 // ***************************************************************************** 43 44 45 #ifndef btstack_hfp_h 46 #define btstack_hfp_h 47 48 #include "hci.h" 49 #include "sdp_query_rfcomm.h" 50 51 #if defined __cplusplus 52 extern "C" { 53 #endif 54 55 56 /* HF Supported Features: 57 0: EC and/or NR function 58 1: Three-way calling 59 2: CLI presentation capability 60 3: Voice recognition activation 61 4: Remote volume control 62 5: Enhanced call status 63 6: Enhanced call control 64 7: Codec negotiation 65 8: HF Indicators 66 9: eSCO S4 (and T2) Settings Supported 67 10-31: Reserved for future definition 68 */ 69 #define HFP_HFSF_THREE_WAY_CALLING 1 70 #define HFP_HFSF_EC_NR_FUNCTION 0 71 #define HFP_HFSF_CODEC_NEGOTIATION 7 72 #define HFP_HFSF_HF_INDICATORS 8 73 #define HFP_HFSF_ESCO 9 74 75 /* AG Supported Features: 76 0: Three-way calling 77 1: EC and/or NR function 78 2: Voice recognition function 79 3: In-band ring tone capability 80 4: Attach a number to a voice tag 81 5: Ability to reject a call 82 6: Enhanced call status 83 7: Enhanced call control 84 8: Extended Error Result Codes 85 9: Codec negotiation 86 10: HF Indicators 87 11: eSCO S4 (and T2) Settings Supported 88 12-31: Reserved for future definition 89 */ 90 #define HFP_AGSF_THREE_WAY_CALLING 0 91 #define HFP_AGSF_EC_NR_FUNCTION 1 92 #define HFP_AGSF_CODEC_NEGOTIATION 9 93 #define HFP_AGSF_HF_INDICATORS 10 94 #define HFP_AGSF_ESCO 11 95 96 #define HFP_DEFAULT_HF_SUPPORTED_FEATURES 0x0000 97 #define HFP_DEFAULT_AG_SUPPORTED_FEATURES 0x0009 98 99 #define HFP_MAX_NUM_CODECS 20 100 #define HFP_MAX_NUM_AG_INDICATORS 20 101 #define HFP_MAX_NUM_HF_INDICATORS 20 102 #define HFP_MAX_INDICATOR_DESC_SIZE 20 103 104 #define HFP_SUPPORTED_FEATURES "+BRSF" 105 #define HFP_AVAILABLE_CODECS "+BAC" 106 #define HFP_INDICATOR "+CIND" 107 #define HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS "+CMER" 108 #define HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS "+BIA" // +BIA:<enabled>,,<enabled>,,,<enabled> 109 #define HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES "+CHLD" 110 #define HFP_GENERIC_STATUS_INDICATOR "+BIND" 111 #define HFP_TRANSFER_AG_INDICATOR_STATUS "+CIEV" // +CIEV: <index>,<value> 112 #define HFP_QUERY_OPERATOR_SELECTION "+COPS" // +COPS: <mode>,0,<opearator> 113 #define HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR "+CMEE" 114 #define HFP_EXTENDED_AUDIO_GATEWAY_ERROR "+CME ERROR" 115 #define HFP_TRIGGER_CODEC_CONNECTION_SETUP "+BCC" 116 #define HFP_CONFIRM_COMMON_CODEC "+BCS" 117 118 #define HFP_OK "OK" 119 #define HFP_ERROR "ERROR" 120 121 // Codecs 122 #define HFP_CODEC_CVSD 0x01 123 #define HFP_CODEC_MSBC 0x02 124 125 typedef enum { 126 HFP_CMD_NONE = 0, 127 HFP_CMD_ERROR, 128 HFP_CMD_OK, 129 HFP_CMD_SUPPORTED_FEATURES, 130 HFP_CMD_AVAILABLE_CODECS, 131 HFP_CMD_INDICATOR, // 5 132 HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE, 133 HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, 134 HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, 135 136 HFP_CMD_GENERIC_STATUS_INDICATOR, 137 138 HFP_CMD_TRANSFER_AG_INDICATOR_STATUS, 139 HFP_CMD_QUERY_OPERATOR_SELECTION, 140 141 HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, 142 HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR, 143 HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP, 144 HFP_CMD_AG_SUGGESTED_CODEC, 145 HFP_CMD_HF_CONFIRMED_CODEC 146 147 } hfp_command_t; 148 149 typedef enum { 150 HFP_CME_ERROR_AG_FAILURE = 0, 151 HFP_CME_ERROR_NO_CONNECTION_TO_PHONE, 152 HFP_CME_ERROR_2, 153 HFP_CME_ERROR_OPERATION_NOT_ALLOWED, 154 HFP_CME_ERROR_OPERATION_NOT_SUPPORTED, 155 HFP_CME_ERROR_PH_SIM_PIN_REQUIRED, 156 HFP_CME_ERROR_6, 157 HFP_CME_ERROR_7, 158 HFP_CME_ERROR_8, 159 HFP_CME_ERROR_9, 160 HFP_CME_ERROR_SIM_NOT_INSERTED, 161 HFP_CME_ERROR_SIM_PIN_REQUIRED, 162 HFP_CME_ERROR_SIM_PUK_REQUIRED, 163 HFP_CME_ERROR_SIM_FAILURE, 164 HFP_CME_ERROR_SIM_BUSY, 165 HFP_CME_ERROR_15, 166 HFP_CME_ERROR_INCORRECT_PASSWORD, 167 HFP_CME_ERROR_SIM_PIN2_REQUIRED, 168 HFP_CME_ERROR_SIM_PUK2_REQUIRED, 169 HFP_CME_ERROR_19, 170 HFP_CME_ERROR_MEMORY_FULL, 171 HFP_CME_ERROR_INVALID_INDEX, 172 HFP_CME_ERROR_22, 173 HFP_CME_ERROR_MEMORY_FAILURE, 174 HFP_CME_ERROR_TEXT_STRING_TOO_LONG, 175 HFP_CME_ERROR_INVALID_CHARACTERS_IN_TEXT_STRING, 176 HFP_CME_ERROR_DIAL_STRING_TOO_LONG, 177 HFP_CME_ERROR_INVALID_CHARACTERS_IN_DIAL_STRING, 178 HFP_CME_ERROR_28, 179 HFP_CME_ERROR_29, 180 HFP_CME_ERROR_NO_NETWORK_SERVICE, 181 HFP_CME_ERROR_NETWORK_TIMEOUT, 182 HFP_CME_ERROR_NETWORK_NOT_ALLOWED_EMERGENCY_CALLS_ONLY 183 } hfp_cme_error_t; 184 185 typedef enum { 186 HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS = 0, 187 HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT 188 } hfp_call_status_t; 189 190 typedef enum { 191 HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS = 0, 192 HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS, 193 HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE, 194 HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE 195 } hfp_callsetup_status_t; 196 197 typedef enum { 198 HFP_HELDCALL_STATUS_NO_CALLS_HELD = 0, 199 HFP_HELDCALL_STATUS_CALL_ON_HOLD_OR_SWAPPED, 200 HFP_HELDCALL_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS 201 } hfp_callheld_status_t; 202 203 typedef enum { 204 HFP_PARSER_CMD_HEADER = 0, 205 HFP_PARSER_CMD_SEQUENCE, 206 HFP_PARSER_SECOND_ITEM, 207 HFP_PARSER_THIRD_ITEM 208 } hfp_parser_state_t; 209 210 211 typedef enum { 212 HFP_IDLE = 0, //0 213 HFP_SDP_QUERY_RFCOMM_CHANNEL, 214 HFP_W4_SDP_QUERY_COMPLETE, 215 HFP_W4_RFCOMM_CONNECTED, 216 217 HFP_EXCHANGE_SUPPORTED_FEATURES, 218 HFP_W4_EXCHANGE_SUPPORTED_FEATURES, // 5 219 220 HFP_NOTIFY_ON_CODECS, 221 HFP_W4_NOTIFY_ON_CODECS, 222 223 HFP_RETRIEVE_INDICATORS, 224 HFP_W4_RETRIEVE_INDICATORS, 225 226 HFP_RETRIEVE_INDICATORS_STATUS, // 10 227 HFP_W4_RETRIEVE_INDICATORS_STATUS, 228 229 HFP_ENABLE_INDICATORS_STATUS_UPDATE, 230 HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE, 231 232 HFP_RETRIEVE_CAN_HOLD_CALL, 233 HFP_W4_RETRIEVE_CAN_HOLD_CALL, // 15 234 235 HFP_LIST_GENERIC_STATUS_INDICATORS, 236 HFP_W4_LIST_GENERIC_STATUS_INDICATORS, 237 238 HFP_RETRIEVE_GENERIC_STATUS_INDICATORS, 239 HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS, 240 241 HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS, // 20 242 HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS, 243 244 HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED, // 22 245 246 HFP_SLE_W2_EXCHANGE_COMMON_CODEC, 247 HFP_SLE_W4_EXCHANGE_COMMON_CODEC, 248 249 HFP_CODECS_CONNECTION_ESTABLISHED, // 25 250 251 HFP_CCE_W2_ESTABLISH_SCO, 252 HFP_CCE_W4_SCO_CONNECTION_ESTABLISHED, 253 254 HFP_AUDIO_CONNECTION_ESTABLISHED, 255 256 HFP_W2_DISCONNECT_SCO, 257 HFP_W4_SCO_DISCONNECTED, // 30 258 259 HFP_W2_DISCONNECT_RFCOMM, 260 HFP_W4_RFCOMM_DISCONNECTED, 261 HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART, 262 HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN 263 } hfp_state_t; 264 265 typedef void (*hfp_callback_t)(uint8_t * event, uint16_t event_size); 266 267 typedef struct{ 268 uint16_t uuid; 269 uint8_t state; // enabled 270 } hfp_generic_status_indicator_t; 271 272 typedef struct{ 273 uint8_t index; 274 char name[HFP_MAX_INDICATOR_DESC_SIZE]; 275 uint8_t min_range; 276 uint8_t max_range; 277 uint8_t status; 278 uint8_t mandatory; 279 uint8_t enabled; 280 uint8_t status_changed; 281 } hfp_ag_indicator_t; 282 283 typedef struct{ 284 char name[3]; 285 } hfp_call_service_t; 286 287 288 typedef struct{ 289 uint8_t mode; 290 uint8_t format; 291 char name[17]; // enabled 292 } hfp_network_opearator_t; 293 294 295 typedef struct hfp_connection { 296 linked_item_t item; 297 298 bd_addr_t remote_addr; 299 uint16_t con_handle; 300 uint16_t sco_handle; 301 uint16_t rfcomm_channel_nr; 302 uint16_t rfcomm_cid; 303 304 hfp_state_t state; 305 // needed for reestablishing connection 306 uint16_t service_uuid; 307 308 // used during service level connection establishment 309 hfp_command_t command; 310 hfp_parser_state_t parser_state; 311 int parser_item_index; 312 uint8_t line_buffer[HFP_MAX_INDICATOR_DESC_SIZE]; 313 int line_size; 314 315 uint32_t remote_supported_features; 316 317 // TODO: rename into hf_codecs_nr 318 int remote_codecs_nr; 319 uint16_t remote_codecs[HFP_MAX_INDICATOR_DESC_SIZE]; 320 int ag_indicators_nr; 321 hfp_ag_indicator_t ag_indicators[HFP_MAX_INDICATOR_DESC_SIZE]; 322 int remote_call_services_nr; 323 hfp_call_service_t remote_call_services[HFP_MAX_INDICATOR_DESC_SIZE]; 324 325 // TODO: use bitmap. 326 int generic_status_indicators_nr; 327 hfp_generic_status_indicator_t generic_status_indicators[HFP_MAX_INDICATOR_DESC_SIZE]; 328 uint8_t enable_status_update_for_ag_indicators; 329 330 uint32_t ag_indicators_status_update_bitmap; 331 hfp_network_opearator_t network_operator; 332 333 // Retrieved during service level connection establishment, not used yet 334 uint8_t negotiated_codec; 335 336 // TODO: put these bit flags in a bitmap 337 uint8_t wait_ok; 338 uint8_t send_ok; 339 uint8_t send_error; 340 341 uint8_t keep_separator; 342 343 uint8_t retrieve_ag_indicators; // HFP_CMD_INDICATOR, check if needed 344 uint8_t retrieve_ag_indicators_status; 345 346 uint8_t list_generic_status_indicators; // HFP_CMD_LIST_GENERIC_STATUS_INDICATOR 347 uint8_t retrieve_generic_status_indicators; // HFP_CMD_GENERIC_STATUS_INDICATOR 348 uint8_t retrieve_generic_status_indicators_state; // HFP_CMD_GENERIC_STATUS_INDICATOR_STATE 349 350 uint8_t change_status_update_for_individual_ag_indicators; 351 352 uint8_t operator_name_format; 353 uint8_t operator_name; 354 uint8_t operator_name_changed; 355 356 uint8_t enable_extended_audio_gateway_error_report; 357 uint8_t extended_audio_gateway_error; 358 359 // can come any time (here taken into account only after SLE), 360 // if codec negotiation feature is set 361 uint8_t notify_ag_on_new_codecs; 362 363 // establish codecs connection 364 uint8_t hf_trigger_codec_connection_setup; 365 uint8_t ag_trigger_codec_connection_setup; 366 uint8_t ag_ready_for_codecs_connection_setup; 367 uint8_t suggested_codec; 368 uint8_t codec_confirmed; 369 370 uint8_t establish_audio_connection; 371 uint8_t release_audio_connection; 372 373 } hfp_connection_t; 374 375 // UTILS_START : TODO move to utils 376 int send_str_over_rfcomm(uint16_t cid, char * command); 377 int join(char * buffer, int buffer_size, uint8_t * values, int values_nr); 378 int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr); 379 int get_bit(uint16_t bitmap, int position); 380 int store_bit(uint32_t bitmap, int position, uint8_t value); 381 // UTILS_END 382 383 void hfp_create_sdp_record(uint8_t * service, uint16_t service_uuid, int rfcomm_channel_nr, const char * name, uint16_t supported_features); 384 void hfp_handle_hci_event(hfp_callback_t callback, uint8_t packet_type, uint8_t *packet, uint16_t size); 385 void hfp_emit_event(hfp_callback_t callback, uint8_t event_subtype, uint8_t value); 386 hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid); 387 hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr); 388 int get_hfp_generic_status_indicators_nr(void); 389 hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void); 390 void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr); 391 392 linked_list_t * hfp_get_connections(void); 393 void hfp_parse(hfp_connection_t * context, uint8_t byte); 394 395 void hfp_init(uint16_t rfcomm_channel_nr); 396 void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid); 397 void hfp_release_service_level_connection(hfp_connection_t * connection); 398 void hfp_reset_context_flags(hfp_connection_t * context); 399 400 void hfp_release_audio_connection(hfp_connection_t * context); 401 402 const char * hfp_hf_feature(int index); 403 const char * hfp_ag_feature(int index); 404 405 #if defined __cplusplus 406 } 407 #endif 408 409 #endif