hfp.c (323d300063a7e0193cf61fbe114062cf000a3fe5) | hfp.c (ca59be5193cced59664efef288ea86d02eaef3a4) |
---|---|
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 --- 85 unchanged lines hidden (view full) --- 94 "Codec negotiation", 95 "HF Indicators", 96 "eSCO S4 (and T2) Settings Supported", 97 "Reserved for future definition" 98}; 99 100static btstack_linked_list_t hfp_connections = NULL; 101static void parse_sequence(hfp_connection_t * context); | 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 --- 85 unchanged lines hidden (view full) --- 94 "Codec negotiation", 95 "HF Indicators", 96 "eSCO S4 (and T2) Settings Supported", 97 "Reserved for future definition" 98}; 99 100static btstack_linked_list_t hfp_connections = NULL; 101static void parse_sequence(hfp_connection_t * context); |
102static btstack_packet_handler_t hfp_callback; 103static btstack_packet_handler_t rfcomm_packet_handler; | |
104 | 102 |
103static btstack_packet_handler_t hfp_hf_callback; 104static btstack_packet_handler_t hfp_ag_callback; 105 106static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler; 107static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler; 108 |
|
105static hfp_connection_t * sco_establishment_active; 106 | 109static hfp_connection_t * sco_establishment_active; 110 |
107void hfp_set_callback(btstack_packet_handler_t callback){ 108 hfp_callback = callback; | 111void hfp_set_hf_callback(btstack_packet_handler_t callback){ 112 hfp_hf_callback = callback; |
109} 110 | 113} 114 |
115void hfp_set_ag_callback(btstack_packet_handler_t callback){ 116 hfp_ag_callback = callback; 117} 118 |
|
111const char * hfp_hf_feature(int index){ 112 if (index > HFP_HF_FEATURES_SIZE){ 113 return hfp_hf_features[HFP_HF_FEATURES_SIZE]; 114 } 115 return hfp_hf_features[index]; 116} 117 118const char * hfp_ag_feature(int index){ --- 76 unchanged lines hidden (view full) --- 195 } 196 197 if (i<values_nr){ 198 offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i)); 199 } 200 return offset; 201} 202 | 119const char * hfp_hf_feature(int index){ 120 if (index > HFP_HF_FEATURES_SIZE){ 121 return hfp_hf_features[HFP_HF_FEATURES_SIZE]; 122 } 123 return hfp_hf_features[index]; 124} 125 126const char * hfp_ag_feature(int index){ --- 76 unchanged lines hidden (view full) --- 203 } 204 205 if (i<values_nr){ 206 offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i)); 207 } 208 return offset; 209} 210 |
203void hfp_emit_simple_event(btstack_packet_handler_t callback, uint8_t event_subtype){ 204 if (!callback) return; | 211static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){ 212 if (!hfp_connection) return; 213 btstack_packet_handler_t callback = NULL; 214 switch (hfp_connection->local_role){ 215 case HFP_ROLE_HF: 216 callback = hfp_hf_callback; 217 case HFP_ROLE_AG: 218 callback = hfp_ag_callback; 219 default: 220 return; 221 } 222 (*callback)(HCI_EVENT_PACKET, 0, packet, size); 223} 224 225void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){ |
205 uint8_t event[3]; 206 event[0] = HCI_EVENT_HFP_META; 207 event[1] = sizeof(event) - 2; 208 event[2] = event_subtype; | 226 uint8_t event[3]; 227 event[0] = HCI_EVENT_HFP_META; 228 event[1] = sizeof(event) - 2; 229 event[2] = event_subtype; |
209 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); | 230 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); |
210} 211 | 231} 232 |
212void hfp_emit_event(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t value){ 213 if (!callback) return; | 233void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){ |
214 uint8_t event[4]; 215 event[0] = HCI_EVENT_HFP_META; 216 event[1] = sizeof(event) - 2; 217 event[2] = event_subtype; 218 event[3] = value; // status 0 == OK | 234 uint8_t event[4]; 235 event[0] = HCI_EVENT_HFP_META; 236 event[1] = sizeof(event) - 2; 237 event[2] = event_subtype; 238 event[3] = value; // status 0 == OK |
219 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); | 239 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); |
220} 221 | 240} 241 |
222void hfp_emit_slc_connection_event(btstack_packet_handler_t callback, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr){ 223 if (!callback) return; | 242void hfp_emit_slc_connection_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr){ |
224 uint8_t event[12]; 225 int pos = 0; 226 event[pos++] = HCI_EVENT_HFP_META; 227 event[pos++] = sizeof(event) - 2; 228 event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 229 event[pos++] = status; // status 0 == OK 230 little_endian_store_16(event, pos, con_handle); 231 pos += 2; 232 reverse_bd_addr(addr,&event[pos]); 233 pos += 6; | 243 uint8_t event[12]; 244 int pos = 0; 245 event[pos++] = HCI_EVENT_HFP_META; 246 event[pos++] = sizeof(event) - 2; 247 event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 248 event[pos++] = status; // status 0 == OK 249 little_endian_store_16(event, pos, con_handle); 250 pos += 2; 251 reverse_bd_addr(addr,&event[pos]); 252 pos += 6; |
234 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); | 253 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); |
235} 236 | 254} 255 |
237static void hfp_emit_sco_event(btstack_packet_handler_t callback, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr, uint8_t negotiated_codec){ 238 if (!callback) return; | 256static void hfp_emit_sco_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr, uint8_t negotiated_codec){ |
239 uint8_t event[13]; 240 int pos = 0; 241 event[pos++] = HCI_EVENT_HFP_META; 242 event[pos++] = sizeof(event) - 2; 243 event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED; 244 event[pos++] = status; // status 0 == OK 245 little_endian_store_16(event, pos, con_handle); 246 pos += 2; 247 reverse_bd_addr(addr,&event[pos]); 248 pos += 6; 249 event[pos++] = negotiated_codec; | 257 uint8_t event[13]; 258 int pos = 0; 259 event[pos++] = HCI_EVENT_HFP_META; 260 event[pos++] = sizeof(event) - 2; 261 event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED; 262 event[pos++] = status; // status 0 == OK 263 little_endian_store_16(event, pos, con_handle); 264 pos += 2; 265 reverse_bd_addr(addr,&event[pos]); 266 pos += 6; 267 event[pos++] = negotiated_codec; |
250 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); | 268 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); |
251} 252 | 269} 270 |
253void hfp_emit_string_event(btstack_packet_handler_t callback, uint8_t event_subtype, const char * value){ 254 if (!callback) return; | 271void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){ |
255 uint8_t event[40]; 256 event[0] = HCI_EVENT_HFP_META; 257 event[1] = sizeof(event) - 2; 258 event[2] = event_subtype; 259 int size = ( strlen(value) < sizeof(event) - 4) ? (int) strlen(value) : (int) sizeof(event) - 4; 260 strncpy((char*)&event[3], value, size); 261 event[3 + size] = 0; | 272 uint8_t event[40]; 273 event[0] = HCI_EVENT_HFP_META; 274 event[1] = sizeof(event) - 2; 275 event[2] = event_subtype; 276 int size = ( strlen(value) < sizeof(event) - 4) ? (int) strlen(value) : (int) sizeof(event) - 4; 277 strncpy((char*)&event[3], value, size); 278 event[3 + size] = 0; |
262 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); | 279 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); |
263} 264 265btstack_linked_list_t * hfp_get_connections(void){ 266 return (btstack_linked_list_t *) &hfp_connections; 267} 268 269hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){ 270 btstack_linked_list_iterator_t it; --- 203 unchanged lines hidden (view full) --- 474 case SDP_EVENT_QUERY_RFCOMM_SERVICE: 475 hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 476 break; 477 case SDP_EVENT_QUERY_COMPLETE: 478 connection_doing_sdp_query = NULL; 479 if (hfp_connection->rfcomm_channel_nr > 0){ 480 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 481 log_info("HFP: SDP_EVENT_QUERY_COMPLETE context %p, addr %s, state %d", hfp_connection, bd_addr_to_str( hfp_connection->remote_addr), hfp_connection->state); | 280} 281 282btstack_linked_list_t * hfp_get_connections(void){ 283 return (btstack_linked_list_t *) &hfp_connections; 284} 285 286hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){ 287 btstack_linked_list_iterator_t it; --- 203 unchanged lines hidden (view full) --- 491 case SDP_EVENT_QUERY_RFCOMM_SERVICE: 492 hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 493 break; 494 case SDP_EVENT_QUERY_COMPLETE: 495 connection_doing_sdp_query = NULL; 496 if (hfp_connection->rfcomm_channel_nr > 0){ 497 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 498 log_info("HFP: SDP_EVENT_QUERY_COMPLETE context %p, addr %s, state %d", hfp_connection, bd_addr_to_str( hfp_connection->remote_addr), hfp_connection->state); |
482 rfcomm_create_channel(rfcomm_packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL); | 499 btstack_packet_handler_t packet_handler = hfp_connection->local_role == HFP_ROLE_AG ? hfp_ag_rfcomm_packet_handler : hfp_hf_rfcomm_packet_handler; 500 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL); |
483 break; 484 } 485 hfp_connection->state = HFP_IDLE; | 501 break; 502 } 503 hfp_connection->state = HFP_IDLE; |
486 hfp_emit_slc_connection_event(hfp_callback, sdp_event_query_complete_get_status(packet), HCI_CON_HANDLE_INVALID, hfp_connection->remote_addr); | 504 hfp_emit_slc_connection_event(hfp_connection, sdp_event_query_complete_get_status(packet), HCI_CON_HANDLE_INVALID, hfp_connection->remote_addr); |
487 log_info("rfcomm service not found, status %u.", sdp_event_query_complete_get_status(packet)); 488 break; 489 default: 490 break; 491 } 492} 493 494static void hfp_handle_failed_sco_connection(uint8_t status){ --- 92 unchanged lines hidden (view full) --- 587 588 rfcomm_event_channel_opened_get_bd_addr(packet, event_addr); 589 status = rfcomm_event_channel_opened_get_status(packet); 590 591 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 592 if (!hfp_connection || hfp_connection->state != HFP_W4_RFCOMM_CONNECTED) return; 593 594 if (status) { | 505 log_info("rfcomm service not found, status %u.", sdp_event_query_complete_get_status(packet)); 506 break; 507 default: 508 break; 509 } 510} 511 512static void hfp_handle_failed_sco_connection(uint8_t status){ --- 92 unchanged lines hidden (view full) --- 605 606 rfcomm_event_channel_opened_get_bd_addr(packet, event_addr); 607 status = rfcomm_event_channel_opened_get_status(packet); 608 609 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 610 if (!hfp_connection || hfp_connection->state != HFP_W4_RFCOMM_CONNECTED) return; 611 612 if (status) { |
595 hfp_emit_slc_connection_event(hfp_callback, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr); | 613 hfp_emit_slc_connection_event(hfp_connection, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr); |
596 remove_hfp_connection_context(hfp_connection); 597 } else { 598 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet); 599 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 600 bd_addr_copy(hfp_connection->remote_addr, event_addr); 601 // uint16_t mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 602 // printf("RFCOMM channel open succeeded. hfp_connection %p, RFCOMM Channel ID 0x%02x, max frame size %u\n", hfp_connection, hfp_connection->rfcomm_cid, mtu); 603 --- 73 unchanged lines hidden (view full) --- 677 if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 678 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN"); 679 hfp_connection->state = HFP_W2_DISCONNECT_SCO; 680 break; 681 } 682 hfp_connection->sco_handle = sco_handle; 683 hfp_connection->establish_audio_connection = 0; 684 hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED; | 614 remove_hfp_connection_context(hfp_connection); 615 } else { 616 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet); 617 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 618 bd_addr_copy(hfp_connection->remote_addr, event_addr); 619 // uint16_t mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 620 // printf("RFCOMM channel open succeeded. hfp_connection %p, RFCOMM Channel ID 0x%02x, max frame size %u\n", hfp_connection, hfp_connection->rfcomm_cid, mtu); 621 --- 73 unchanged lines hidden (view full) --- 695 if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 696 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN"); 697 hfp_connection->state = HFP_W2_DISCONNECT_SCO; 698 break; 699 } 700 hfp_connection->sco_handle = sco_handle; 701 hfp_connection->establish_audio_connection = 0; 702 hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED; |
685 hfp_emit_sco_event(hfp_callback, packet[2], sco_handle, event_addr, hfp_connection->negotiated_codec); | 703 hfp_emit_sco_event(hfp_connection, packet[2], sco_handle, event_addr, hfp_connection->negotiated_codec); |
686 break; 687 } 688 689 case RFCOMM_EVENT_CHANNEL_CLOSED: 690 rfcomm_cid = little_endian_read_16(packet,2); 691 hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 692 if (!hfp_connection) break; 693 if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){ 694 hfp_connection->state = HFP_IDLE; 695 hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role); 696 break; 697 } 698 | 704 break; 705 } 706 707 case RFCOMM_EVENT_CHANNEL_CLOSED: 708 rfcomm_cid = little_endian_read_16(packet,2); 709 hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 710 if (!hfp_connection) break; 711 if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){ 712 hfp_connection->state = HFP_IDLE; 713 hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role); 714 break; 715 } 716 |
699 hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); | 717 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); |
700 remove_hfp_connection_context(hfp_connection); 701 break; 702 703 case HCI_EVENT_DISCONNECTION_COMPLETE: 704 handle = little_endian_read_16(packet,3); 705 hfp_connection = get_hfp_connection_context_for_sco_handle(handle); 706 707 if (!hfp_connection) break; 708 709 if (hfp_connection->state != HFP_W4_SCO_DISCONNECTED){ 710 log_info("Received gap disconnect in wrong hfp state"); 711 } 712 log_info("Check SCO handle: incoming 0x%02x, hfp_connection 0x%02x\n", handle, hfp_connection->sco_handle); 713 714 if (handle == hfp_connection->sco_handle){ 715 log_info("SCO disconnected, w2 disconnect RFCOMM\n"); 716 hfp_connection->sco_handle = 0; 717 hfp_connection->release_audio_connection = 0; 718 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; | 718 remove_hfp_connection_context(hfp_connection); 719 break; 720 721 case HCI_EVENT_DISCONNECTION_COMPLETE: 722 handle = little_endian_read_16(packet,3); 723 hfp_connection = get_hfp_connection_context_for_sco_handle(handle); 724 725 if (!hfp_connection) break; 726 727 if (hfp_connection->state != HFP_W4_SCO_DISCONNECTED){ 728 log_info("Received gap disconnect in wrong hfp state"); 729 } 730 log_info("Check SCO handle: incoming 0x%02x, hfp_connection 0x%02x\n", handle, hfp_connection->sco_handle); 731 732 if (handle == hfp_connection->sco_handle){ 733 log_info("SCO disconnected, w2 disconnect RFCOMM\n"); 734 hfp_connection->sco_handle = 0; 735 hfp_connection->release_audio_connection = 0; 736 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; |
719 hfp_emit_event(hfp_callback, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0); | 737 hfp_emit_event(hfp_connection, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0); |
720 break; 721 } 722 break; 723 724 default: 725 break; 726 } 727} --- 718 unchanged lines hidden (view full) --- 1446 uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1447 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1448 sco_voice_setting = 0x0043; // Transparent data 1449 } 1450 hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency, 1451 sco_voice_setting, hfp_link_settings[setting].retransmission_effort, hfp_link_settings[setting].packet_types); // all types 0x003f, only 2-ev3 0x380 1452} 1453 | 738 break; 739 } 740 break; 741 742 default: 743 break; 744 } 745} --- 718 unchanged lines hidden (view full) --- 1464 uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1465 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1466 sco_voice_setting = 0x0043; // Transparent data 1467 } 1468 hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency, 1469 sco_voice_setting, hfp_link_settings[setting].retransmission_effort, hfp_link_settings[setting].packet_types); // all types 0x003f, only 2-ev3 0x380 1470} 1471 |
1454void hfp_set_packet_handler_for_rfcomm_connections(btstack_packet_handler_t handler){ 1455 rfcomm_packet_handler = handler; | 1472void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){ 1473 hfp_ag_rfcomm_packet_handler = handler; |
1456} | 1474} |
1475void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){ 1476 hfp_hf_rfcomm_packet_handler = handler; 1477} |
|
1457 | 1478 |