hfp.c (46f073f23a940c13c6db21186d8159e015b5c67b) hfp.c (3721a235468b7b00a1583fbaab9ebe94f874a0f4)
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

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

54#include "classic/sdp_server.h"
55#include "classic/sdp_util.h"
56#include "classic/sdp_client.h"
57#include "hci.h"
58#include "hci_cmd.h"
59#include "hci_dump.h"
60#include "l2cap.h"
61
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

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

54#include "classic/sdp_server.h"
55#include "classic/sdp_util.h"
56#include "classic/sdp_client.h"
57#include "hci.h"
58#include "hci_cmd.h"
59#include "hci_dump.h"
60#include "l2cap.h"
61
62#if defined(ENABLE_CC256X_ASSISTED_HFP) && !defined(ENABLE_SCO_OVER_PCM)
63#error "Assisted HFP is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM"
64#endif
65
62#define HFP_HF_FEATURES_SIZE 10
63#define HFP_AG_FEATURES_SIZE 12
64
65
66static const char * hfp_hf_features[] = {
67 "EC and/or NR function",
68 "Three-way calling",
69 "CLI presentation capability",

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

685 hci_event_connection_request_get_bd_addr(packet, event_addr);
686 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
687 if (!hfp_connection) break;
688 if (hci_event_connection_request_get_link_type(packet) == 2){
689 hfp_connection->hf_accept_sco = 2;
690 } else {
691 hfp_connection->hf_accept_sco = 1;
692 }
66#define HFP_HF_FEATURES_SIZE 10
67#define HFP_AG_FEATURES_SIZE 12
68
69
70static const char * hfp_hf_features[] = {
71 "EC and/or NR function",
72 "Three-way calling",
73 "CLI presentation capability",

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

689 hci_event_connection_request_get_bd_addr(packet, event_addr);
690 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
691 if (!hfp_connection) break;
692 if (hci_event_connection_request_get_link_type(packet) == 2){
693 hfp_connection->hf_accept_sco = 2;
694 } else {
695 hfp_connection->hf_accept_sco = 1;
696 }
697#ifdef ENABLE_CC256X_ASSISTED_HFP
698 hfp_cc256x_prepare_for_sco(hfp_connection);
699#endif
693 log_info("hf accept sco %u\n", hfp_connection->hf_accept_sco);
694 sco_establishment_active = hfp_connection;
695 break;
696 default:
697 break;
698 }
699 break;
700

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

773 }
774
775 case HCI_EVENT_DISCONNECTION_COMPLETE:
776 handle = little_endian_read_16(packet,3);
777 hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role);
778
779 if (!hfp_connection) break;
780
700 log_info("hf accept sco %u\n", hfp_connection->hf_accept_sco);
701 sco_establishment_active = hfp_connection;
702 break;
703 default:
704 break;
705 }
706 break;
707

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

780 }
781
782 case HCI_EVENT_DISCONNECTION_COMPLETE:
783 handle = little_endian_read_16(packet,3);
784 hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role);
785
786 if (!hfp_connection) break;
787
788#ifdef ENABLE_CC256X_ASSISTED_HFP
789 hfp_connection->cc256x_send_wbs_disassociate = true;
790#endif
791
781 hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
782 hfp_connection->release_audio_connection = 0;
783 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
784 hfp_emit_event(hfp_connection, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0);
785
786 if (hfp_connection->release_slc_connection){
787 hfp_connection->release_slc_connection = 0;
788 log_info("SCO disconnected, w2 disconnect RFCOMM\n");
789 hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
790 }
791 break;
792
793 default:
794 break;
795 }
796}
797
792 hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
793 hfp_connection->release_audio_connection = 0;
794 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
795 hfp_emit_event(hfp_connection, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0);
796
797 if (hfp_connection->release_slc_connection){
798 hfp_connection->release_slc_connection = 0;
799 log_info("SCO disconnected, w2 disconnect RFCOMM\n");
800 hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
801 }
802 break;
803
804 default:
805 break;
806 }
807}
808
809
798void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
799 UNUSED(packet_type);
800 UNUSED(channel); // ok: no channel
801 UNUSED(size);
802
803 bd_addr_t event_addr;
804 uint16_t rfcomm_cid;
805 hfp_connection_t * hfp_connection = NULL;

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

1593 sco_voice_setting = 0x0043; // Transparent data
1594 }
1595 // get packet types - bits 6-9 are 'don't allow'
1596 uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0;
1597 hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1598 sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types);
1599}
1600
810void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
811 UNUSED(packet_type);
812 UNUSED(channel); // ok: no channel
813 UNUSED(size);
814
815 bd_addr_t event_addr;
816 uint16_t rfcomm_cid;
817 hfp_connection_t * hfp_connection = NULL;

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

1605 sco_voice_setting = 0x0043; // Transparent data
1606 }
1607 // get packet types - bits 6-9 are 'don't allow'
1608 uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0;
1609 hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1610 sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types);
1611}
1612
1613#ifdef ENABLE_CC256X_ASSISTED_HFP
1614void hfp_cc256x_prepare_for_sco(hfp_connection_t * hfp_connection){
1615 hfp_connection->cc256x_send_write_codec_config = true;
1616 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1617 hfp_connection->cc256x_send_wbs_associate = true;
1618 }
1619}
1620
1621void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){
1622 uint32_t sample_rate_hz;
1623 uint16_t clock_rate_khz;
1624 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1625 clock_rate_khz = 512;
1626 sample_rate_hz = 16000;
1627 } else {
1628 clock_rate_khz = 256;
1629 sample_rate_hz = 8000;
1630 }
1631 uint8_t clock_direction = 0; // master
1632 uint16_t frame_sync_duty_cycle = 0; // i2s with 50%
1633 uint8_t frame_sync_edge = 1; // rising edge
1634 uint8_t frame_sync_polarity = 0; // active high
1635 uint8_t reserved = 0;
1636 uint16_t size = 16;
1637 uint16_t chan_1_offset = 1;
1638 uint16_t chan_2_offset = chan_1_offset + size;
1639 uint8_t out_edge = 1; // rising
1640 uint8_t in_edge = 0; // falling
1641 hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle,
1642 frame_sync_edge, frame_sync_polarity, reserved,
1643 size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved,
1644 size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved);
1645}
1646#endif
1647
1601void hfp_set_hf_callback(btstack_packet_handler_t callback){
1602 hfp_hf_callback = callback;
1603}
1604
1605void hfp_set_ag_callback(btstack_packet_handler_t callback){
1606 hfp_ag_callback = callback;
1607}
1608

--- 82 unchanged lines hidden ---
1648void hfp_set_hf_callback(btstack_packet_handler_t callback){
1649 hfp_hf_callback = callback;
1650}
1651
1652void hfp_set_ag_callback(btstack_packet_handler_t callback){
1653 hfp_ag_callback = callback;
1654}
1655

--- 82 unchanged lines hidden ---