hfp.c (bb6944afd9b3ec354819e79194855d0a5d9b6e85) hfp.c (eddcd3084af8a144b56e09ed9af6b20590981c8a)
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 "Reserved for future definition"
95};
96
97static btstack_linked_list_t hfp_connections = NULL;
98static void parse_sequence(hfp_connection_t * context);
99static hfp_callback_t hfp_callback;
100static btstack_packet_handler_t rfcomm_packet_handler;
101
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 "Reserved for future definition"
95};
96
97static btstack_linked_list_t hfp_connections = NULL;
98static void parse_sequence(hfp_connection_t * context);
99static hfp_callback_t hfp_callback;
100static btstack_packet_handler_t rfcomm_packet_handler;
101
102static hfp_connection_t * sco_establishment_active;
103
102void hfp_set_callback(hfp_callback_t callback){
103 hfp_callback = callback;
104}
105
106const char * hfp_hf_feature(int index){
107 if (index > HFP_HF_FEATURES_SIZE){
108 return hfp_hf_features[HFP_HF_FEATURES_SIZE];
109 }

--- 323 unchanged lines hidden (view full) ---

433 }
434 log_info("rfcomm service not found, status %u.", sdp_event_query_complete_get_status(packet));
435 break;
436 default:
437 break;
438 }
439}
440
104void hfp_set_callback(hfp_callback_t callback){
105 hfp_callback = callback;
106}
107
108const char * hfp_hf_feature(int index){
109 if (index > HFP_HF_FEATURES_SIZE){
110 return hfp_hf_features[HFP_HF_FEATURES_SIZE];
111 }

--- 323 unchanged lines hidden (view full) ---

435 }
436 log_info("rfcomm service not found, status %u.", sdp_event_query_complete_get_status(packet));
437 break;
438 default:
439 break;
440 }
441}
442
443static void hfp_handle_failed_sco_connection(uint8_t status){
444
445 if (!sco_establishment_active){
446 log_error("(e)SCO Connection failed but not started by us");
447 return;
448 }
449 log_error("(e)SCO Connection failed status %u", status);
450
451 // invalid params / unspecified error
452 if (status != 0x11 && status != 0x1f) return;
453
454 switch (sco_establishment_active->link_setting){
455 case HFP_LINK_SETTINGS_D0:
456 return; // no other option left
457 case HFP_LINK_SETTINGS_D1:
458 sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D0;
459 break;
460 case HFP_LINK_SETTINGS_S1:
461 sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
462 break;
463 case HFP_LINK_SETTINGS_S2:
464 case HFP_LINK_SETTINGS_S3:
465 case HFP_LINK_SETTINGS_S4:
466 sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S1;
467 break;
468 case HFP_LINK_SETTINGS_T1:
469 case HFP_LINK_SETTINGS_T2:
470 sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S3;
471 break;
472 }
473 sco_establishment_active->establish_audio_connection = 1;
474 sco_establishment_active = 0;
475}
476
477
441void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
442 bd_addr_t event_addr;
443 uint16_t rfcomm_cid, handle;
444 hfp_connection_t * hfp_connection = NULL;
445 uint8_t status;
446
478void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
479 bd_addr_t event_addr;
480 uint16_t rfcomm_cid, handle;
481 hfp_connection_t * hfp_connection = NULL;
482 uint8_t status;
483
447 // printf("AG packet_handler type %u, event type %x, size %u\n", packet_type, hci_event_packet_get_type(packet), size);
484 log_info("AG packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
448
449 switch (hci_event_packet_get_type(packet)) {
450
451 case RFCOMM_EVENT_INCOMING_CONNECTION:
452 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
453 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
454 hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr);
455 if (!hfp_connection || hfp_connection->state != HFP_IDLE) return;

--- 33 unchanged lines hidden (view full) ---

489 break;
490 default:
491 break;
492 }
493 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
494 }
495 break;
496
485
486 switch (hci_event_packet_get_type(packet)) {
487
488 case RFCOMM_EVENT_INCOMING_CONNECTION:
489 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
490 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
491 hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr);
492 if (!hfp_connection || hfp_connection->state != HFP_IDLE) return;

--- 33 unchanged lines hidden (view full) ---

526 break;
527 default:
528 break;
529 }
530 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
531 }
532 break;
533
534 case HCI_EVENT_COMMAND_STATUS:
535 if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
536 hfp_handle_failed_sco_connection(hci_event_command_status_get_status(packet));
537 }
538 break;
539
497 case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
498
499 reverse_bd_addr(&packet[5], event_addr);
500 int index = 2;
501 status = packet[index++];
502
503 if (status != 0){
540 case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
541
542 reverse_bd_addr(&packet[5], event_addr);
543 int index = 2;
544 status = packet[index++];
545
546 if (status != 0){
504 log_error("(e)SCO Connection failed status %u", status);
505 // if outgoing && link_setting != d0 && appropriate error
506 if (status != 0x11 && status != 0x1f) break; // invalid params / unspecified error
507 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr);
508 if (!hfp_connection) break;
509 switch (hfp_connection->link_setting){
510 case HFP_LINK_SETTINGS_D0:
511 return; // no other option left
512 case HFP_LINK_SETTINGS_D1:
513 // hfp_connection->link_setting = HFP_LINK_SETTINGS_D0;
514 // break;
515 case HFP_LINK_SETTINGS_S1:
516 // hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
517 // break;
518 case HFP_LINK_SETTINGS_S2:
519 case HFP_LINK_SETTINGS_S3:
520 case HFP_LINK_SETTINGS_S4:
521 // hfp_connection->link_setting = HFP_LINK_SETTINGS_S1;
522 // break;
523 case HFP_LINK_SETTINGS_T1:
524 case HFP_LINK_SETTINGS_T2:
525 // hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
526 hfp_connection->link_setting = HFP_LINK_SETTINGS_D0;
527 break;
528 }
529 hfp_connection->establish_audio_connection = 1;
547 hfp_handle_failed_sco_connection(status);
530 break;
531 }
532
533 uint16_t sco_handle = little_endian_read_16(packet, index);
534 index+=2;
535
536 reverse_bd_addr(&packet[index], event_addr);
537 index+=6;

--- 789 unchanged lines hidden (view full) ---

1327 { 0x0007, 0x01, 0x03c8 }, // HFP_LINK_SETTINGS_S1, EV3
1328 { 0x0007, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S2, 2-EV3
1329 { 0x000a, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S3, 2-EV3
1330 { 0x000c, 0x02, 0x0380 }, // HFP_LINK_SETTINGS_S4, 2-EV3
1331 { 0x0008, 0x02, 0x03c8 }, // HFP_LINK_SETTINGS_T1, EV3
1332 { 0x000d, 0x02, 0x0380 } // HFP_LINK_SETTINGS_T2, 2-EV3
1333};
1334
548 break;
549 }
550
551 uint16_t sco_handle = little_endian_read_16(packet, index);
552 index+=2;
553
554 reverse_bd_addr(&packet[index], event_addr);
555 index+=6;

--- 789 unchanged lines hidden (view full) ---

1345 { 0x0007, 0x01, 0x03c8 }, // HFP_LINK_SETTINGS_S1, EV3
1346 { 0x0007, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S2, 2-EV3
1347 { 0x000a, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S3, 2-EV3
1348 { 0x000c, 0x02, 0x0380 }, // HFP_LINK_SETTINGS_S4, 2-EV3
1349 { 0x0008, 0x02, 0x03c8 }, // HFP_LINK_SETTINGS_T1, EV3
1350 { 0x000d, 0x02, 0x0380 } // HFP_LINK_SETTINGS_T2, 2-EV3
1351};
1352
1335void hfp_setup_synchronous_connection(hci_con_handle_t handle, hfp_link_setttings_t setting){
1353void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1336 // all packet types, fixed bandwidth
1354 // all packet types, fixed bandwidth
1355 int setting = hfp_connection->link_setting;
1337 log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1356 log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1338 hci_send_cmd(&hci_setup_synchronous_connection, handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1357 sco_establishment_active = hfp_connection;
1358 hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1339 hci_get_sco_voice_setting(), hfp_link_settings[setting].retransmission_effort, hfp_link_settings[setting].packet_types); // all types 0x003f, only 2-ev3 0x380
1340}
1341
1342void hfp_set_packet_handler_for_rfcomm_connections(btstack_packet_handler_t handler){
1343 rfcomm_packet_handler = handler;
1344}
1345
1359 hci_get_sco_voice_setting(), hfp_link_settings[setting].retransmission_effort, hfp_link_settings[setting].packet_types); // all types 0x003f, only 2-ev3 0x380
1360}
1361
1362void hfp_set_packet_handler_for_rfcomm_connections(btstack_packet_handler_t handler){
1363 rfcomm_packet_handler = handler;
1364}
1365