1bd021c4eSMatthias Ringwald /* 2bd021c4eSMatthias Ringwald * Copyright (C) 2016 BlueKitchen GmbH 3bd021c4eSMatthias Ringwald * 4bd021c4eSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5bd021c4eSMatthias Ringwald * modification, are permitted provided that the following conditions 6bd021c4eSMatthias Ringwald * are met: 7bd021c4eSMatthias Ringwald * 8bd021c4eSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9bd021c4eSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10bd021c4eSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11bd021c4eSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12bd021c4eSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13bd021c4eSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14bd021c4eSMatthias Ringwald * contributors may be used to endorse or promote products derived 15bd021c4eSMatthias Ringwald * from this software without specific prior written permission. 16bd021c4eSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17bd021c4eSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18bd021c4eSMatthias Ringwald * monetary gain. 19bd021c4eSMatthias Ringwald * 20bd021c4eSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21bd021c4eSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22bd021c4eSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23bd021c4eSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24bd021c4eSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25bd021c4eSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26bd021c4eSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27bd021c4eSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28bd021c4eSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29bd021c4eSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30bd021c4eSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31bd021c4eSMatthias Ringwald * SUCH DAMAGE. 32bd021c4eSMatthias Ringwald * 33bd021c4eSMatthias Ringwald * Please inquire about commercial licensing options at 34bd021c4eSMatthias Ringwald * [email protected] 35bd021c4eSMatthias Ringwald * 36bd021c4eSMatthias Ringwald */ 37bd021c4eSMatthias Ringwald 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hci_transport_h5.c" 39ab2c6ae4SMatthias Ringwald 40bd021c4eSMatthias Ringwald /* 41bd021c4eSMatthias Ringwald * hci_transport_h5.c 42bd021c4eSMatthias Ringwald * 43bd021c4eSMatthias Ringwald * HCI Transport API implementation for basic H5 protocol 44bd021c4eSMatthias Ringwald * 45bd021c4eSMatthias Ringwald * Created by Matthias Ringw ald on 4/29/09. 46bd021c4eSMatthias Ringwald */ 47bd021c4eSMatthias Ringwald 48d0756da4SMatthias Ringwald #include <inttypes.h> 49d0756da4SMatthias Ringwald 50bd021c4eSMatthias Ringwald #include "hci.h" 51bd021c4eSMatthias Ringwald #include "btstack_slip.h" 52bd021c4eSMatthias Ringwald #include "btstack_debug.h" 53bd021c4eSMatthias Ringwald #include "hci_transport.h" 54bd021c4eSMatthias Ringwald #include "btstack_uart_block.h" 55bd021c4eSMatthias Ringwald 56bd021c4eSMatthias Ringwald typedef enum { 57bd021c4eSMatthias Ringwald LINK_UNINITIALIZED, 58bd021c4eSMatthias Ringwald LINK_INITIALIZED, 59bd021c4eSMatthias Ringwald LINK_ACTIVE 60bd021c4eSMatthias Ringwald } hci_transport_link_state_t; 61bd021c4eSMatthias Ringwald 62bd021c4eSMatthias Ringwald typedef enum { 63bd021c4eSMatthias Ringwald HCI_TRANSPORT_LINK_SEND_SYNC = 1 << 0, 64bd021c4eSMatthias Ringwald HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE = 1 << 1, 65bd021c4eSMatthias Ringwald HCI_TRANSPORT_LINK_SEND_CONFIG = 1 << 2, 66bf71c899SMatthias Ringwald HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE_EMPTY = 1 << 3, 67bf71c899SMatthias Ringwald HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE = 1 << 4, 68bf71c899SMatthias Ringwald HCI_TRANSPORT_LINK_SEND_SLEEP = 1 << 5, 69bf71c899SMatthias Ringwald HCI_TRANSPORT_LINK_SEND_WOKEN = 1 << 6, 70bf71c899SMatthias Ringwald HCI_TRANSPORT_LINK_SEND_WAKEUP = 1 << 7, 71bf71c899SMatthias Ringwald HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET = 1 << 8, 72bf71c899SMatthias Ringwald HCI_TRANSPORT_LINK_SEND_ACK_PACKET = 1 << 9, 73bf71c899SMatthias Ringwald HCI_TRANSPORT_LINK_ENTER_SLEEP = 1 << 10, 7494764e99SMatthias Ringwald 75bd021c4eSMatthias Ringwald } hci_transport_link_actions_t; 76bd021c4eSMatthias Ringwald 7779b61987SMatthias Ringwald // Configuration Field. No packet buffers -> sliding window = 1, no OOF flow control, support data integrity check 78bd021c4eSMatthias Ringwald #define LINK_CONFIG_SLIDING_WINDOW_SIZE 1 79bd021c4eSMatthias Ringwald #define LINK_CONFIG_OOF_FLOW_CONTROL 0 8079b61987SMatthias Ringwald #define LINK_CONFIG_DATA_INTEGRITY_CHECK 1 81bd021c4eSMatthias Ringwald #define LINK_CONFIG_VERSION_NR 0 82bd021c4eSMatthias Ringwald #define LINK_CONFIG_FIELD (LINK_CONFIG_SLIDING_WINDOW_SIZE | (LINK_CONFIG_OOF_FLOW_CONTROL << 3) | (LINK_CONFIG_DATA_INTEGRITY_CHECK << 4) | (LINK_CONFIG_VERSION_NR << 5)) 83bd021c4eSMatthias Ringwald 84bd021c4eSMatthias Ringwald // periodic sending during link establishment 85bd021c4eSMatthias Ringwald #define LINK_PERIOD_MS 250 86bd021c4eSMatthias Ringwald 87bd021c4eSMatthias Ringwald // resend wakeup 88bd021c4eSMatthias Ringwald #define LINK_WAKEUP_MS 50 89bd021c4eSMatthias Ringwald 90bd021c4eSMatthias Ringwald // additional packet types 91bd021c4eSMatthias Ringwald #define LINK_ACKNOWLEDGEMENT_TYPE 0x00 92bd021c4eSMatthias Ringwald #define LINK_CONTROL_PACKET_TYPE 0x0f 93bd021c4eSMatthias Ringwald 946ae10533SMatthias Ringwald // max size of write requests 956ae10533SMatthias Ringwald #define LINK_SLIP_TX_CHUNK_LEN 64 966ae10533SMatthias Ringwald 97bd021c4eSMatthias Ringwald // --- 98bd021c4eSMatthias Ringwald static const uint8_t link_control_sync[] = { 0x01, 0x7e}; 99bd021c4eSMatthias Ringwald static const uint8_t link_control_sync_response[] = { 0x02, 0x7d}; 100bd021c4eSMatthias Ringwald static const uint8_t link_control_config[] = { 0x03, 0xfc, LINK_CONFIG_FIELD}; 101cbc04f48SMatthias Ringwald static const uint8_t link_control_config_prefix_len = 2; 102bf71c899SMatthias Ringwald static const uint8_t link_control_config_response_empty[] = { 0x04, 0x7b}; 103bd021c4eSMatthias Ringwald static const uint8_t link_control_config_response[] = { 0x04, 0x7b, LINK_CONFIG_FIELD}; 104bd021c4eSMatthias Ringwald static const uint8_t link_control_config_response_prefix_len = 2; 105bd021c4eSMatthias Ringwald static const uint8_t link_control_wakeup[] = { 0x05, 0xfa}; 106bd021c4eSMatthias Ringwald static const uint8_t link_control_woken[] = { 0x06, 0xf9}; 107bd021c4eSMatthias Ringwald static const uint8_t link_control_sleep[] = { 0x07, 0x78}; 108bd021c4eSMatthias Ringwald 109c6df6d84SMatthias Ringwald // max size of link control messages 110c6df6d84SMatthias Ringwald #define LINK_CONTROL_MAX_LEN 3 111c6df6d84SMatthias Ringwald 112bd021c4eSMatthias Ringwald // incoming pre-bufffer + 4 bytes H5 header + max(acl header + acl payload, event header + event data) + 2 bytes opt CRC 113*fc6cde64SMatthias Ringwald static uint8_t hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 6 + HCI_INCOMING_PACKET_BUFFER_SIZE]; 114bd021c4eSMatthias Ringwald 115c65ae2a0SMatthias Ringwald // outgoing slip encoded buffer. +4 to assert that DIC fits in buffer. +1 to assert that last SOF fits in buffer. 116c65ae2a0SMatthias Ringwald static uint8_t slip_outgoing_buffer[LINK_SLIP_TX_CHUNK_LEN+4+1]; 117c65ae2a0SMatthias Ringwald static uint16_t slip_outgoing_dic; 118c65ae2a0SMatthias Ringwald static uint16_t slip_outgoing_dic_present; 1196ae10533SMatthias Ringwald static int slip_write_active; 120bd021c4eSMatthias Ringwald 121bd021c4eSMatthias Ringwald // H5 Link State 122bd021c4eSMatthias Ringwald static hci_transport_link_state_t link_state; 123bd021c4eSMatthias Ringwald static btstack_timer_source_t link_timer; 124bd021c4eSMatthias Ringwald static uint8_t link_seq_nr; 125bd021c4eSMatthias Ringwald static uint8_t link_ack_nr; 126bd021c4eSMatthias Ringwald static uint16_t link_resend_timeout_ms; 127bd021c4eSMatthias Ringwald static uint8_t link_peer_asleep; 1287c2aa31eSMatthias Ringwald static uint8_t link_peer_supports_data_integrity_check; 129bd021c4eSMatthias Ringwald 13094764e99SMatthias Ringwald // auto sleep-mode 13194764e99SMatthias Ringwald static btstack_timer_source_t inactivity_timer; 13294764e99SMatthias Ringwald static uint16_t link_inactivity_timeout_ms; // auto-sleep if set 13394764e99SMatthias Ringwald 134bd021c4eSMatthias Ringwald // Outgoing packet 135bd021c4eSMatthias Ringwald static uint8_t hci_packet_type; 136bd021c4eSMatthias Ringwald static uint16_t hci_packet_size; 137bd021c4eSMatthias Ringwald static uint8_t * hci_packet; 138bd021c4eSMatthias Ringwald 139bd021c4eSMatthias Ringwald // hci packet handler 140bd021c4eSMatthias Ringwald static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 141bd021c4eSMatthias Ringwald 142bd021c4eSMatthias Ringwald static int hci_transport_link_actions; 143bd021c4eSMatthias Ringwald 144bd021c4eSMatthias Ringwald // UART Driver + Config 145bd021c4eSMatthias Ringwald static const btstack_uart_block_t * btstack_uart; 146bd021c4eSMatthias Ringwald static btstack_uart_config_t uart_config; 147d8e28fa3SMatthias Ringwald static btstack_uart_sleep_mode_t btstack_uart_sleep_mode; 148daa2e90cSMatthias Ringwald static int hci_transport_bcsp_mode; 149bd021c4eSMatthias Ringwald 150bd021c4eSMatthias Ringwald // Prototypes 151bd021c4eSMatthias Ringwald static void hci_transport_h5_process_frame(uint16_t frame_size); 152bd021c4eSMatthias Ringwald static int hci_transport_link_have_outgoing_packet(void); 153bd021c4eSMatthias Ringwald static void hci_transport_link_send_queued_packet(void); 154bd021c4eSMatthias Ringwald static void hci_transport_link_set_timer(uint16_t timeout_ms); 155bd021c4eSMatthias Ringwald static void hci_transport_link_timeout_handler(btstack_timer_source_t * timer); 156bd021c4eSMatthias Ringwald static void hci_transport_link_run(void); 157bd021c4eSMatthias Ringwald static void hci_transport_slip_init(void); 158bd021c4eSMatthias Ringwald 1597c2aa31eSMatthias Ringwald // ----------------------------- 16079b61987SMatthias Ringwald // CRC16-CCITT Calculation - compromise: use 32 byte table - 512 byte table would be faster, but that's too large 16179b61987SMatthias Ringwald 16279b61987SMatthias Ringwald static const uint16_t crc16_ccitt_table[] ={ 16379b61987SMatthias Ringwald 0x0000, 0x1081, 0x2102, 0x3183, 16479b61987SMatthias Ringwald 0x4204, 0x5285, 0x6306, 0x7387, 16579b61987SMatthias Ringwald 0x8408, 0x9489, 0xa50a, 0xb58b, 16679b61987SMatthias Ringwald 0xc60c, 0xd68d, 0xe70e, 0xf78f 16779b61987SMatthias Ringwald }; 16879b61987SMatthias Ringwald 16979b61987SMatthias Ringwald static uint16_t crc16_ccitt_update (uint16_t crc, uint8_t ch){ 17079b61987SMatthias Ringwald crc = (crc >> 4) ^ crc16_ccitt_table[(crc ^ ch) & 0x000f]; 17179b61987SMatthias Ringwald crc = (crc >> 4) ^ crc16_ccitt_table[(crc ^ (ch >> 4)) & 0x000f]; 17279b61987SMatthias Ringwald return crc; 17379b61987SMatthias Ringwald } 17479b61987SMatthias Ringwald 17579b61987SMatthias Ringwald static uint16_t btstack_reverse_bits_16(uint16_t value){ 17679b61987SMatthias Ringwald int reverse = 0; 17779b61987SMatthias Ringwald int i; 17879b61987SMatthias Ringwald for (i = 0; i < 16; i++) { 17979b61987SMatthias Ringwald reverse = reverse << 1; 18079b61987SMatthias Ringwald reverse |= value & 1; 18179b61987SMatthias Ringwald value = value >> 1; 18279b61987SMatthias Ringwald } 18379b61987SMatthias Ringwald return reverse; 18479b61987SMatthias Ringwald } 18579b61987SMatthias Ringwald 186c6df6d84SMatthias Ringwald static uint16_t crc16_calc_for_slip_frame(const uint8_t * header, const uint8_t * payload, uint16_t len){ 18779b61987SMatthias Ringwald int i; 18879b61987SMatthias Ringwald uint16_t crc = 0xffff; 18979b61987SMatthias Ringwald for (i=0 ; i < 4 ; i++){ 19079b61987SMatthias Ringwald crc = crc16_ccitt_update(crc, header[i]); 19179b61987SMatthias Ringwald } 19279b61987SMatthias Ringwald for (i=0 ; i < len ; i++){ 19379b61987SMatthias Ringwald crc = crc16_ccitt_update(crc, payload[i]); 19479b61987SMatthias Ringwald } 19579b61987SMatthias Ringwald return btstack_reverse_bits_16(crc); 19679b61987SMatthias Ringwald } 19779b61987SMatthias Ringwald 198bd021c4eSMatthias Ringwald // ----------------------------- 19994764e99SMatthias Ringwald static void hci_transport_inactivity_timeout_handler(btstack_timer_source_t * ts){ 200b304e673SMatthias Ringwald log_info("inactivity timeout. link state %d, peer asleep %u, actions 0x%02x, outgoing packet %u", 20194764e99SMatthias Ringwald link_state, link_peer_asleep, hci_transport_link_actions, hci_transport_link_have_outgoing_packet()); 20294764e99SMatthias Ringwald if (hci_transport_link_have_outgoing_packet()) return; 20394764e99SMatthias Ringwald if (link_state != LINK_ACTIVE) return; 20494764e99SMatthias Ringwald if (hci_transport_link_actions) return; 20594764e99SMatthias Ringwald if (link_peer_asleep) return; 20694764e99SMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SLEEP; 20794764e99SMatthias Ringwald hci_transport_link_run(); 20894764e99SMatthias Ringwald } 209bd021c4eSMatthias Ringwald 21094764e99SMatthias Ringwald static void hci_transport_inactivity_timer_set(void){ 21194764e99SMatthias Ringwald if (!link_inactivity_timeout_ms) return; 21294764e99SMatthias Ringwald btstack_run_loop_set_timer_handler(&inactivity_timer, &hci_transport_inactivity_timeout_handler); 21394764e99SMatthias Ringwald btstack_run_loop_set_timer(&inactivity_timer, link_inactivity_timeout_ms); 21494764e99SMatthias Ringwald btstack_run_loop_remove_timer(&inactivity_timer); 21594764e99SMatthias Ringwald btstack_run_loop_add_timer(&inactivity_timer); 21694764e99SMatthias Ringwald } 217bd021c4eSMatthias Ringwald 21894764e99SMatthias Ringwald // ----------------------------- 219bd021c4eSMatthias Ringwald // SLIP Outgoing 220bd021c4eSMatthias Ringwald 2216ae10533SMatthias Ringwald // Fill chunk and write 2226ae10533SMatthias Ringwald static void hci_transport_slip_encode_chunk_and_send(int pos){ 2236ae10533SMatthias Ringwald while (btstack_slip_encoder_has_data() & (pos < LINK_SLIP_TX_CHUNK_LEN)) { 2246ae10533SMatthias Ringwald slip_outgoing_buffer[pos++] = btstack_slip_encoder_get_byte(); 2256ae10533SMatthias Ringwald } 226c65ae2a0SMatthias Ringwald 2276ae10533SMatthias Ringwald if (!btstack_slip_encoder_has_data()){ 228c65ae2a0SMatthias Ringwald // Payload encoded, append DIC if present. 229c65ae2a0SMatthias Ringwald // note: slip_outgoing_buffer is guaranteed to be big enough to add DIC + SOF after LINK_SLIP_TX_CHUNK_LEN 230c65ae2a0SMatthias Ringwald if (slip_outgoing_dic_present){ 231c65ae2a0SMatthias Ringwald uint8_t dic_buffer[2]; 232c65ae2a0SMatthias Ringwald big_endian_store_16(dic_buffer, 0, slip_outgoing_dic); 233c65ae2a0SMatthias Ringwald btstack_slip_encoder_start(dic_buffer, 2); 234c65ae2a0SMatthias Ringwald while (btstack_slip_encoder_has_data()){ 235c65ae2a0SMatthias Ringwald slip_outgoing_buffer[pos++] = btstack_slip_encoder_get_byte(); 236c65ae2a0SMatthias Ringwald } 237c65ae2a0SMatthias Ringwald } 2386ae10533SMatthias Ringwald // Start of Frame 2396ae10533SMatthias Ringwald slip_outgoing_buffer[pos++] = BTSTACK_SLIP_SOF; 2406ae10533SMatthias Ringwald } 2416ae10533SMatthias Ringwald slip_write_active = 1; 242b304e673SMatthias Ringwald log_debug("slip: send %d bytes", pos); 2436ae10533SMatthias Ringwald btstack_uart->send_block(slip_outgoing_buffer, pos); 2446ae10533SMatthias Ringwald } 2456ae10533SMatthias Ringwald 2466ae10533SMatthias Ringwald static inline void hci_transport_slip_send_next_chunk(void){ 2476ae10533SMatthias Ringwald hci_transport_slip_encode_chunk_and_send(0); 2486ae10533SMatthias Ringwald } 2496ae10533SMatthias Ringwald 250c65ae2a0SMatthias Ringwald // format: 0xc0 HEADER PACKET [DIC] 0xc0 251bd021c4eSMatthias Ringwald // @param uint8_t header[4] 252c65ae2a0SMatthias Ringwald static void hci_transport_slip_send_frame(const uint8_t * header, const uint8_t * packet, uint16_t packet_size, uint16_t data_integrity_check){ 253bd021c4eSMatthias Ringwald 254bd021c4eSMatthias Ringwald int pos = 0; 255bd021c4eSMatthias Ringwald 256c65ae2a0SMatthias Ringwald // store data integrity check info 257c65ae2a0SMatthias Ringwald slip_outgoing_dic = data_integrity_check; 258c65ae2a0SMatthias Ringwald slip_outgoing_dic_present = header[0] & 0x40; 259c65ae2a0SMatthias Ringwald 260bd021c4eSMatthias Ringwald // Start of Frame 261bd021c4eSMatthias Ringwald slip_outgoing_buffer[pos++] = BTSTACK_SLIP_SOF; 262bd021c4eSMatthias Ringwald 263bd021c4eSMatthias Ringwald // Header 264bd021c4eSMatthias Ringwald btstack_slip_encoder_start(header, 4); 265bd021c4eSMatthias Ringwald while (btstack_slip_encoder_has_data()){ 266bd021c4eSMatthias Ringwald slip_outgoing_buffer[pos++] = btstack_slip_encoder_get_byte(); 267bd021c4eSMatthias Ringwald } 268bd021c4eSMatthias Ringwald 269bd021c4eSMatthias Ringwald // Packet 270bd021c4eSMatthias Ringwald btstack_slip_encoder_start(packet, packet_size); 271bd021c4eSMatthias Ringwald 2726ae10533SMatthias Ringwald // Fill rest of chunk from packet and send 2736ae10533SMatthias Ringwald hci_transport_slip_encode_chunk_and_send(pos); 274bd021c4eSMatthias Ringwald } 275bd021c4eSMatthias Ringwald 276bd021c4eSMatthias Ringwald // SLIP Incoming 277bd021c4eSMatthias Ringwald 278bd021c4eSMatthias Ringwald static void hci_transport_slip_init(void){ 279*fc6cde64SMatthias Ringwald btstack_slip_decoder_init(&hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], 6 + HCI_INCOMING_PACKET_BUFFER_SIZE); 280bd021c4eSMatthias Ringwald } 281bd021c4eSMatthias Ringwald 282bd021c4eSMatthias Ringwald // H5 Three-Wire Implementation 283bd021c4eSMatthias Ringwald 284bd021c4eSMatthias Ringwald static void hci_transport_link_calc_header(uint8_t * header, 285bd021c4eSMatthias Ringwald uint8_t sequence_nr, 286bd021c4eSMatthias Ringwald uint8_t acknowledgement_nr, 287bd021c4eSMatthias Ringwald uint8_t data_integrity_check_present, 288bd021c4eSMatthias Ringwald uint8_t reliable_packet, 289bd021c4eSMatthias Ringwald uint8_t packet_type, 290bd021c4eSMatthias Ringwald uint16_t payload_length){ 291bd021c4eSMatthias Ringwald 292bd021c4eSMatthias Ringwald header[0] = sequence_nr | (acknowledgement_nr << 3) | (data_integrity_check_present << 6) | (reliable_packet << 7); 293bd021c4eSMatthias Ringwald header[1] = packet_type | ((payload_length & 0x0f) << 4); 294bd021c4eSMatthias Ringwald header[2] = payload_length >> 4; 295bd021c4eSMatthias Ringwald header[3] = 0xff - (header[0] + header[1] + header[2]); 296bd021c4eSMatthias Ringwald } 297bd021c4eSMatthias Ringwald 298bd021c4eSMatthias Ringwald static void hci_transport_link_send_control(const uint8_t * message, int message_len){ 299bd021c4eSMatthias Ringwald uint8_t header[4]; 300c6df6d84SMatthias Ringwald hci_transport_link_calc_header(header, 0, 0, link_peer_supports_data_integrity_check, 0, LINK_CONTROL_PACKET_TYPE, message_len); 301c65ae2a0SMatthias Ringwald uint16_t data_integrity_check = 0; 302c6df6d84SMatthias Ringwald if (link_peer_supports_data_integrity_check){ 303c65ae2a0SMatthias Ringwald data_integrity_check = crc16_calc_for_slip_frame(header, message, message_len); 304c6df6d84SMatthias Ringwald } 305b304e673SMatthias Ringwald log_debug("hci_transport_link_send_control: size %u, append dic %u", message_len, link_peer_supports_data_integrity_check); 306b304e673SMatthias Ringwald log_debug_hexdump(message, message_len); 307c65ae2a0SMatthias Ringwald hci_transport_slip_send_frame(header, message, message_len, data_integrity_check); 308bd021c4eSMatthias Ringwald } 309bd021c4eSMatthias Ringwald 310bd021c4eSMatthias Ringwald static void hci_transport_link_send_sync(void){ 311b304e673SMatthias Ringwald log_debug("link send sync"); 312bd021c4eSMatthias Ringwald hci_transport_link_send_control(link_control_sync, sizeof(link_control_sync)); 313bd021c4eSMatthias Ringwald } 314bd021c4eSMatthias Ringwald 315bd021c4eSMatthias Ringwald static void hci_transport_link_send_sync_response(void){ 316b304e673SMatthias Ringwald log_debug("link send sync response"); 317bd021c4eSMatthias Ringwald hci_transport_link_send_control(link_control_sync_response, sizeof(link_control_sync_response)); 318bd021c4eSMatthias Ringwald } 319bd021c4eSMatthias Ringwald 320bd021c4eSMatthias Ringwald static void hci_transport_link_send_config(void){ 321b304e673SMatthias Ringwald log_debug("link send config"); 322bd021c4eSMatthias Ringwald hci_transport_link_send_control(link_control_config, sizeof(link_control_config)); 323bd021c4eSMatthias Ringwald } 324bd021c4eSMatthias Ringwald 325bd021c4eSMatthias Ringwald static void hci_transport_link_send_config_response(void){ 326b304e673SMatthias Ringwald log_debug("link send config response"); 327bd021c4eSMatthias Ringwald hci_transport_link_send_control(link_control_config_response, sizeof(link_control_config_response)); 328bd021c4eSMatthias Ringwald } 329bd021c4eSMatthias Ringwald 330bf71c899SMatthias Ringwald static void hci_transport_link_send_config_response_empty(void){ 331bf71c899SMatthias Ringwald log_debug("link send config response empty"); 332bf71c899SMatthias Ringwald hci_transport_link_send_control(link_control_config_response_empty, sizeof(link_control_config_response_empty)); 333bf71c899SMatthias Ringwald } 334bf71c899SMatthias Ringwald 335bd021c4eSMatthias Ringwald static void hci_transport_link_send_woken(void){ 336b304e673SMatthias Ringwald log_debug("link send woken"); 337bd021c4eSMatthias Ringwald hci_transport_link_send_control(link_control_woken, sizeof(link_control_woken)); 338bd021c4eSMatthias Ringwald } 339bd021c4eSMatthias Ringwald 340bd021c4eSMatthias Ringwald static void hci_transport_link_send_wakeup(void){ 341b304e673SMatthias Ringwald log_debug("link send wakeup"); 342bd021c4eSMatthias Ringwald hci_transport_link_send_control(link_control_wakeup, sizeof(link_control_wakeup)); 343bd021c4eSMatthias Ringwald } 344bd021c4eSMatthias Ringwald 34594764e99SMatthias Ringwald static void hci_transport_link_send_sleep(void){ 346b304e673SMatthias Ringwald log_debug("link send sleep"); 34794764e99SMatthias Ringwald hci_transport_link_send_control(link_control_sleep, sizeof(link_control_sleep)); 34894764e99SMatthias Ringwald } 34994764e99SMatthias Ringwald 350bd021c4eSMatthias Ringwald static void hci_transport_link_send_queued_packet(void){ 351bd021c4eSMatthias Ringwald 352bd021c4eSMatthias Ringwald uint8_t header[4]; 353c6df6d84SMatthias Ringwald hci_transport_link_calc_header(header, link_seq_nr, link_ack_nr, link_peer_supports_data_integrity_check, 1, hci_packet_type, hci_packet_size); 354c6df6d84SMatthias Ringwald 355c65ae2a0SMatthias Ringwald uint16_t data_integrity_check = 0; 356c6df6d84SMatthias Ringwald if (link_peer_supports_data_integrity_check){ 357c65ae2a0SMatthias Ringwald data_integrity_check = crc16_calc_for_slip_frame(header, hci_packet, hci_packet_size); 358c6df6d84SMatthias Ringwald } 359b304e673SMatthias Ringwald log_debug("hci_transport_link_send_queued_packet: seq %u, ack %u, size %u. Append dic %u, dic = 0x%04x", link_seq_nr, link_ack_nr, hci_packet_size, link_peer_supports_data_integrity_check, data_integrity_check); 360b304e673SMatthias Ringwald log_debug_hexdump(hci_packet, hci_packet_size); 361c6df6d84SMatthias Ringwald 362c65ae2a0SMatthias Ringwald hci_transport_slip_send_frame(header, hci_packet, hci_packet_size, data_integrity_check); 36394764e99SMatthias Ringwald 36494764e99SMatthias Ringwald // reset inactvitiy timer 36594764e99SMatthias Ringwald hci_transport_inactivity_timer_set(); 366bd021c4eSMatthias Ringwald } 367bd021c4eSMatthias Ringwald 368bd021c4eSMatthias Ringwald static void hci_transport_link_send_ack_packet(void){ 369c65ae2a0SMatthias Ringwald // Pure ACK package is without DIC as there is no payload either 370b304e673SMatthias Ringwald log_debug("send ack %u", link_ack_nr); 371bd021c4eSMatthias Ringwald uint8_t header[4]; 372bd021c4eSMatthias Ringwald hci_transport_link_calc_header(header, 0, link_ack_nr, 0, 0, LINK_ACKNOWLEDGEMENT_TYPE, 0); 373c65ae2a0SMatthias Ringwald hci_transport_slip_send_frame(header, NULL, 0, 0); 374bd021c4eSMatthias Ringwald } 375bd021c4eSMatthias Ringwald 376bd021c4eSMatthias Ringwald static void hci_transport_link_run(void){ 377bd021c4eSMatthias Ringwald // exit if outgoing active 3786ae10533SMatthias Ringwald if (slip_write_active) return; 379bd021c4eSMatthias Ringwald 380bd021c4eSMatthias Ringwald // process queued requests 381bd021c4eSMatthias Ringwald if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SYNC){ 382bd021c4eSMatthias Ringwald hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SYNC; 383bd021c4eSMatthias Ringwald hci_transport_link_send_sync(); 384bd021c4eSMatthias Ringwald return; 385bd021c4eSMatthias Ringwald } 386bd021c4eSMatthias Ringwald if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE){ 387bd021c4eSMatthias Ringwald hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE; 388bd021c4eSMatthias Ringwald hci_transport_link_send_sync_response(); 389bd021c4eSMatthias Ringwald return; 390bd021c4eSMatthias Ringwald } 391bd021c4eSMatthias Ringwald if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_CONFIG){ 392bd021c4eSMatthias Ringwald hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_CONFIG; 393bd021c4eSMatthias Ringwald hci_transport_link_send_config(); 394bd021c4eSMatthias Ringwald return; 395bd021c4eSMatthias Ringwald } 396bd021c4eSMatthias Ringwald if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE){ 397bd021c4eSMatthias Ringwald hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE; 398bd021c4eSMatthias Ringwald hci_transport_link_send_config_response(); 399bd021c4eSMatthias Ringwald return; 400bd021c4eSMatthias Ringwald } 401bf71c899SMatthias Ringwald if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE_EMPTY){ 402bf71c899SMatthias Ringwald hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE_EMPTY; 403bf71c899SMatthias Ringwald hci_transport_link_send_config_response_empty(); 404bf71c899SMatthias Ringwald return; 405bf71c899SMatthias Ringwald } 406bd021c4eSMatthias Ringwald if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_WOKEN){ 407bd021c4eSMatthias Ringwald hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_WOKEN; 408bd021c4eSMatthias Ringwald hci_transport_link_send_woken(); 409bd021c4eSMatthias Ringwald return; 410bd021c4eSMatthias Ringwald } 411bd021c4eSMatthias Ringwald if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_WAKEUP){ 412bd021c4eSMatthias Ringwald hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_WAKEUP; 413bd021c4eSMatthias Ringwald hci_transport_link_send_wakeup(); 414bd021c4eSMatthias Ringwald return; 415bd021c4eSMatthias Ringwald } 416bd021c4eSMatthias Ringwald if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET){ 417bd021c4eSMatthias Ringwald hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET; 418bd021c4eSMatthias Ringwald // packet already contains ack, no need to send addtitional one 419bd021c4eSMatthias Ringwald hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_ACK_PACKET; 420bd021c4eSMatthias Ringwald hci_transport_link_send_queued_packet(); 421bd021c4eSMatthias Ringwald return; 422bd021c4eSMatthias Ringwald } 423bd021c4eSMatthias Ringwald if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_ACK_PACKET){ 424bd021c4eSMatthias Ringwald hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_ACK_PACKET; 425bd021c4eSMatthias Ringwald hci_transport_link_send_ack_packet(); 426bd021c4eSMatthias Ringwald return; 427bd021c4eSMatthias Ringwald } 42894764e99SMatthias Ringwald if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SLEEP){ 42994764e99SMatthias Ringwald hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SLEEP; 43094764e99SMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_ENTER_SLEEP; 43194764e99SMatthias Ringwald link_peer_asleep = 1; 43294764e99SMatthias Ringwald hci_transport_link_send_sleep(); 43394764e99SMatthias Ringwald return; 43494764e99SMatthias Ringwald } 435bd021c4eSMatthias Ringwald } 436bd021c4eSMatthias Ringwald 437bd021c4eSMatthias Ringwald static void hci_transport_link_set_timer(uint16_t timeout_ms){ 438bd021c4eSMatthias Ringwald btstack_run_loop_set_timer_handler(&link_timer, &hci_transport_link_timeout_handler); 439bd021c4eSMatthias Ringwald btstack_run_loop_set_timer(&link_timer, timeout_ms); 440bd021c4eSMatthias Ringwald btstack_run_loop_add_timer(&link_timer); 441bd021c4eSMatthias Ringwald } 442bd021c4eSMatthias Ringwald 443bd021c4eSMatthias Ringwald static void hci_transport_link_timeout_handler(btstack_timer_source_t * timer){ 444bd021c4eSMatthias Ringwald switch (link_state){ 445bd021c4eSMatthias Ringwald case LINK_UNINITIALIZED: 446bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC; 447bd021c4eSMatthias Ringwald hci_transport_link_set_timer(LINK_PERIOD_MS); 448bd021c4eSMatthias Ringwald break; 449bd021c4eSMatthias Ringwald case LINK_INITIALIZED: 450bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG; 451bd021c4eSMatthias Ringwald hci_transport_link_set_timer(LINK_PERIOD_MS); 452bd021c4eSMatthias Ringwald break; 453bd021c4eSMatthias Ringwald case LINK_ACTIVE: 454bd021c4eSMatthias Ringwald if (!hci_transport_link_have_outgoing_packet()){ 455bd021c4eSMatthias Ringwald log_info("h5 timeout while active, but no outgoing packet"); 456bd021c4eSMatthias Ringwald return; 457bd021c4eSMatthias Ringwald } 458bd021c4eSMatthias Ringwald if (link_peer_asleep){ 459bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WAKEUP; 460bd021c4eSMatthias Ringwald hci_transport_link_set_timer(LINK_WAKEUP_MS); 461bd021c4eSMatthias Ringwald return; 462bd021c4eSMatthias Ringwald } 463bd021c4eSMatthias Ringwald // resend packet 464bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET; 465bd021c4eSMatthias Ringwald hci_transport_link_set_timer(link_resend_timeout_ms); 466bd021c4eSMatthias Ringwald break; 467bd021c4eSMatthias Ringwald default: 468bd021c4eSMatthias Ringwald break; 469bd021c4eSMatthias Ringwald } 470bd021c4eSMatthias Ringwald 471bd021c4eSMatthias Ringwald hci_transport_link_run(); 472bd021c4eSMatthias Ringwald } 473bd021c4eSMatthias Ringwald 474bd021c4eSMatthias Ringwald static void hci_transport_link_init(void){ 475bd021c4eSMatthias Ringwald link_state = LINK_UNINITIALIZED; 476bd021c4eSMatthias Ringwald link_peer_asleep = 0; 4777c2aa31eSMatthias Ringwald link_peer_supports_data_integrity_check = 0; 478bd021c4eSMatthias Ringwald 479bd021c4eSMatthias Ringwald // get started 480bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC; 481bd021c4eSMatthias Ringwald hci_transport_link_set_timer(LINK_PERIOD_MS); 482bd021c4eSMatthias Ringwald hci_transport_link_run(); 483bd021c4eSMatthias Ringwald } 484bd021c4eSMatthias Ringwald 485bd021c4eSMatthias Ringwald static int hci_transport_link_inc_seq_nr(int seq_nr){ 486bd021c4eSMatthias Ringwald return (seq_nr + 1) & 0x07; 487bd021c4eSMatthias Ringwald } 488bd021c4eSMatthias Ringwald 489bd021c4eSMatthias Ringwald static int hci_transport_link_have_outgoing_packet(void){ 490bd021c4eSMatthias Ringwald return hci_packet != 0; 491bd021c4eSMatthias Ringwald } 492bd021c4eSMatthias Ringwald 493bd021c4eSMatthias Ringwald static void hci_transport_link_clear_queue(void){ 494bd021c4eSMatthias Ringwald btstack_run_loop_remove_timer(&link_timer); 495bd021c4eSMatthias Ringwald hci_packet = NULL; 496bd021c4eSMatthias Ringwald } 497bd021c4eSMatthias Ringwald 498bd021c4eSMatthias Ringwald static void hci_transport_h5_queue_packet(uint8_t packet_type, uint8_t *packet, int size){ 499bd021c4eSMatthias Ringwald hci_packet = packet; 500bd021c4eSMatthias Ringwald hci_packet_type = packet_type; 501bd021c4eSMatthias Ringwald hci_packet_size = size; 502bd021c4eSMatthias Ringwald } 503bd021c4eSMatthias Ringwald 50462ca45d7SMatthias Ringwald static void hci_transport_h5_emit_sleep_state(int sleep_active){ 50562ca45d7SMatthias Ringwald static int last_state = 0; 50662ca45d7SMatthias Ringwald if (sleep_active == last_state) return; 50762ca45d7SMatthias Ringwald last_state = sleep_active; 50862ca45d7SMatthias Ringwald 509b304e673SMatthias Ringwald log_info("emit_sleep_state: %u", sleep_active); 51062ca45d7SMatthias Ringwald uint8_t event[3]; 51162ca45d7SMatthias Ringwald event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE; 51262ca45d7SMatthias Ringwald event[1] = sizeof(event) - 2; 51362ca45d7SMatthias Ringwald event[2] = sleep_active; 51462ca45d7SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 51562ca45d7SMatthias Ringwald } 51662ca45d7SMatthias Ringwald 517bd021c4eSMatthias Ringwald static void hci_transport_h5_process_frame(uint16_t frame_size){ 518bd021c4eSMatthias Ringwald 519bd021c4eSMatthias Ringwald if (frame_size < 4) return; 520bd021c4eSMatthias Ringwald 521bd021c4eSMatthias Ringwald uint8_t * slip_header = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE]; 522bd021c4eSMatthias Ringwald uint8_t * slip_payload = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 4]; 523bd021c4eSMatthias Ringwald int frame_size_without_header = frame_size - 4; 524bd021c4eSMatthias Ringwald 525f04a0c31SMatthias Ringwald uint8_t seq_nr = slip_header[0] & 0x07; 526f04a0c31SMatthias Ringwald uint8_t ack_nr = (slip_header[0] >> 3) & 0x07; 527f04a0c31SMatthias Ringwald uint8_t data_integrity_check_present = (slip_header[0] & 0x40) != 0; 528f04a0c31SMatthias Ringwald uint8_t reliable_packet = (slip_header[0] & 0x80) != 0; 529bd021c4eSMatthias Ringwald uint8_t link_packet_type = slip_header[1] & 0x0f; 530bd021c4eSMatthias Ringwald uint16_t link_payload_len = (slip_header[1] >> 4) | (slip_header[2] << 4); 531bd021c4eSMatthias Ringwald 53299136b1cSMatthias Ringwald log_debug("process_frame, reliable %u, packet type %u, seq_nr %u, ack_nr %u , dic %u, payload 0x%04x bytes", reliable_packet, link_packet_type, seq_nr, ack_nr, data_integrity_check_present, frame_size_without_header); 533b304e673SMatthias Ringwald log_debug_hexdump(slip_header, 4); 534b304e673SMatthias Ringwald log_debug_hexdump(slip_payload, frame_size_without_header); 535bd021c4eSMatthias Ringwald 536bd021c4eSMatthias Ringwald // CSR 8811 does not seem to auto-detect H5 mode and sends data with even parity. 537bd021c4eSMatthias Ringwald // if this byte sequence is detected, just enable even parity 538bd021c4eSMatthias Ringwald const uint8_t sync_response_bcsp[] = {0x01, 0x7a, 0x06, 0x10}; 539bd021c4eSMatthias Ringwald if (memcmp(sync_response_bcsp, slip_header, 4) == 0){ 540b304e673SMatthias Ringwald log_info("detected BSCP SYNC sent with Even Parity -> discard frame and enable Even Parity"); 541bd021c4eSMatthias Ringwald btstack_uart->set_parity(1); 542bd021c4eSMatthias Ringwald return; 543bd021c4eSMatthias Ringwald } 544bd021c4eSMatthias Ringwald 545bd021c4eSMatthias Ringwald // validate header checksum 546bd021c4eSMatthias Ringwald uint8_t header_checksum = slip_header[0] + slip_header[1] + slip_header[2] + slip_header[3]; 547bd021c4eSMatthias Ringwald if (header_checksum != 0xff){ 548b304e673SMatthias Ringwald log_info("header checksum 0x%02x (instead of 0xff)", header_checksum); 549bd021c4eSMatthias Ringwald return; 550bd021c4eSMatthias Ringwald } 551bd021c4eSMatthias Ringwald 552bd021c4eSMatthias Ringwald // validate payload length 553bd021c4eSMatthias Ringwald int data_integrity_len = data_integrity_check_present ? 2 : 0; 554bd021c4eSMatthias Ringwald uint16_t received_payload_len = frame_size_without_header - data_integrity_len; 555bd021c4eSMatthias Ringwald if (link_payload_len != received_payload_len){ 556b304e673SMatthias Ringwald log_info("expected payload len %u but got %u", link_payload_len, received_payload_len); 557bd021c4eSMatthias Ringwald return; 558bd021c4eSMatthias Ringwald } 559bd021c4eSMatthias Ringwald 56079b61987SMatthias Ringwald // validate data integrity check 56179b61987SMatthias Ringwald if (data_integrity_check_present){ 56279b61987SMatthias Ringwald uint16_t dic_packet = big_endian_read_16(slip_payload, received_payload_len); 56379b61987SMatthias Ringwald uint16_t dic_calculate = crc16_calc_for_slip_frame(slip_header, slip_payload, received_payload_len); 56479b61987SMatthias Ringwald if (dic_packet != dic_calculate){ 565b304e673SMatthias Ringwald log_info("expected dic value 0x%04x but got 0x%04x", dic_calculate, dic_packet); 56679b61987SMatthias Ringwald return; 56779b61987SMatthias Ringwald } 56879b61987SMatthias Ringwald } 569bd021c4eSMatthias Ringwald 570bd021c4eSMatthias Ringwald switch (link_state){ 571bd021c4eSMatthias Ringwald case LINK_UNINITIALIZED: 572bd021c4eSMatthias Ringwald if (link_packet_type != LINK_CONTROL_PACKET_TYPE) break; 573bd021c4eSMatthias Ringwald if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){ 574b304e673SMatthias Ringwald log_debug("link received sync"); 575bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE; 576bf71c899SMatthias Ringwald break; 577bd021c4eSMatthias Ringwald } 578bd021c4eSMatthias Ringwald if (memcmp(slip_payload, link_control_sync_response, sizeof(link_control_sync_response)) == 0){ 579b304e673SMatthias Ringwald log_debug("link received sync response"); 580bd021c4eSMatthias Ringwald link_state = LINK_INITIALIZED; 581bd021c4eSMatthias Ringwald btstack_run_loop_remove_timer(&link_timer); 582bd021c4eSMatthias Ringwald log_info("link initialized"); 583bd021c4eSMatthias Ringwald // 584bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG; 585bd021c4eSMatthias Ringwald hci_transport_link_set_timer(LINK_PERIOD_MS); 586bf71c899SMatthias Ringwald break; 587bd021c4eSMatthias Ringwald } 588bd021c4eSMatthias Ringwald break; 589bd021c4eSMatthias Ringwald case LINK_INITIALIZED: 590bd021c4eSMatthias Ringwald if (link_packet_type != LINK_CONTROL_PACKET_TYPE) break; 591bd021c4eSMatthias Ringwald if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){ 592b304e673SMatthias Ringwald log_debug("link received sync"); 593bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE; 594bf71c899SMatthias Ringwald break; 595bd021c4eSMatthias Ringwald } 596cbc04f48SMatthias Ringwald if (memcmp(slip_payload, link_control_config, link_control_config_prefix_len) == 0){ 597bf71c899SMatthias Ringwald if (link_payload_len == link_control_config_prefix_len){ 598bf71c899SMatthias Ringwald log_debug("link received config, no config field"); 599bf71c899SMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE_EMPTY; 600bf71c899SMatthias Ringwald } else { 601b304e673SMatthias Ringwald log_debug("link received config, 0x%02x", slip_payload[2]); 602bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE; 603bd021c4eSMatthias Ringwald } 604bf71c899SMatthias Ringwald break; 605bf71c899SMatthias Ringwald } 606bd021c4eSMatthias Ringwald if (memcmp(slip_payload, link_control_config_response, link_control_config_response_prefix_len) == 0){ 6077c2aa31eSMatthias Ringwald uint8_t config = slip_payload[2]; 6087c2aa31eSMatthias Ringwald link_peer_supports_data_integrity_check = (config & 0x10) != 0; 609b304e673SMatthias Ringwald log_info("link received config response 0x%02x, data integrity check supported %u", config, link_peer_supports_data_integrity_check); 610bd021c4eSMatthias Ringwald link_state = LINK_ACTIVE; 611bd021c4eSMatthias Ringwald btstack_run_loop_remove_timer(&link_timer); 612bd021c4eSMatthias Ringwald log_info("link activated"); 613bd021c4eSMatthias Ringwald // 614bd021c4eSMatthias Ringwald link_seq_nr = 0; 615bd021c4eSMatthias Ringwald link_ack_nr = 0; 616bd021c4eSMatthias Ringwald // notify upper stack that it can start 617bd021c4eSMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 618bd021c4eSMatthias Ringwald packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 619bf71c899SMatthias Ringwald break; 620bd021c4eSMatthias Ringwald } 621bd021c4eSMatthias Ringwald break; 622bd021c4eSMatthias Ringwald case LINK_ACTIVE: 623bd021c4eSMatthias Ringwald 624bd021c4eSMatthias Ringwald // validate packet sequence nr in reliable packets (check for out of sequence error) 625bd021c4eSMatthias Ringwald if (reliable_packet){ 626bd021c4eSMatthias Ringwald if (seq_nr != link_ack_nr){ 627bd021c4eSMatthias Ringwald log_info("expected seq nr %u, but received %u", link_ack_nr, seq_nr); 628bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_ACK_PACKET; 629bd021c4eSMatthias Ringwald break; 630bd021c4eSMatthias Ringwald } 631bd021c4eSMatthias Ringwald // ack packet right away 632bd021c4eSMatthias Ringwald link_ack_nr = hci_transport_link_inc_seq_nr(link_ack_nr); 633bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_ACK_PACKET; 634bd021c4eSMatthias Ringwald } 635bd021c4eSMatthias Ringwald 636bd021c4eSMatthias Ringwald // Process ACKs in reliable packet and explicit ack packets 637bd021c4eSMatthias Ringwald if (reliable_packet || link_packet_type == LINK_ACKNOWLEDGEMENT_TYPE){ 638bd021c4eSMatthias Ringwald // our packet is good if the remote expects our seq nr + 1 639bd021c4eSMatthias Ringwald int next_seq_nr = hci_transport_link_inc_seq_nr(link_seq_nr); 640bd021c4eSMatthias Ringwald if (hci_transport_link_have_outgoing_packet() && next_seq_nr == ack_nr){ 641b304e673SMatthias Ringwald log_debug("outoing packet with seq %u ack'ed", link_seq_nr); 642bd021c4eSMatthias Ringwald link_seq_nr = next_seq_nr; 643bd021c4eSMatthias Ringwald hci_transport_link_clear_queue(); 644bd021c4eSMatthias Ringwald 645bd021c4eSMatthias Ringwald // notify upper stack that it can send again 646bd021c4eSMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 647bd021c4eSMatthias Ringwald packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 648bd021c4eSMatthias Ringwald } 649bd021c4eSMatthias Ringwald } 650bd021c4eSMatthias Ringwald 651bd021c4eSMatthias Ringwald switch (link_packet_type){ 652bd021c4eSMatthias Ringwald case LINK_CONTROL_PACKET_TYPE: 653bd021c4eSMatthias Ringwald if (memcmp(slip_payload, link_control_config, sizeof(link_control_config)) == 0){ 654bf71c899SMatthias Ringwald if (link_payload_len == link_control_config_prefix_len){ 655bf71c899SMatthias Ringwald log_debug("link received config, no config field"); 656bf71c899SMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE_EMPTY; 657bf71c899SMatthias Ringwald } else { 658bf71c899SMatthias Ringwald log_debug("link received config, 0x%02x", slip_payload[2]); 659bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE; 660bf71c899SMatthias Ringwald } 661bd021c4eSMatthias Ringwald break; 662bd021c4eSMatthias Ringwald } 663bd021c4eSMatthias Ringwald if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){ 664b304e673SMatthias Ringwald log_debug("link received sync in ACTIVE STATE!"); 665bd021c4eSMatthias Ringwald // TODO sync during active indicates peer reset -> full upper layer reset necessary 666bd021c4eSMatthias Ringwald break; 667bd021c4eSMatthias Ringwald } 668bd021c4eSMatthias Ringwald if (memcmp(slip_payload, link_control_sleep, sizeof(link_control_sleep)) == 0){ 669d8e28fa3SMatthias Ringwald if (btstack_uart_sleep_mode){ 670d8e28fa3SMatthias Ringwald log_info("link: received sleep message. Enabling UART Sleep."); 671d8e28fa3SMatthias Ringwald btstack_uart->set_sleep(btstack_uart_sleep_mode); 67262ca45d7SMatthias Ringwald hci_transport_h5_emit_sleep_state(1); 673d8e28fa3SMatthias Ringwald } else { 674d8e28fa3SMatthias Ringwald log_info("link: received sleep message. UART Sleep not supported"); 675d8e28fa3SMatthias Ringwald } 676bd021c4eSMatthias Ringwald link_peer_asleep = 1; 677bd021c4eSMatthias Ringwald break; 678bd021c4eSMatthias Ringwald } 679bd021c4eSMatthias Ringwald if (memcmp(slip_payload, link_control_wakeup, sizeof(link_control_wakeup)) == 0){ 680bd021c4eSMatthias Ringwald log_info("link: received wakupe message -> send woken"); 681bd021c4eSMatthias Ringwald link_peer_asleep = 0; 682bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WOKEN; 683bd021c4eSMatthias Ringwald break; 684bd021c4eSMatthias Ringwald } 685bd021c4eSMatthias Ringwald if (memcmp(slip_payload, link_control_woken, sizeof(link_control_woken)) == 0){ 686bd021c4eSMatthias Ringwald log_info("link: received woken message"); 687bd021c4eSMatthias Ringwald link_peer_asleep = 0; 68894764e99SMatthias Ringwald // queued packet will be sent in hci_transport_link_run if needed 689bd021c4eSMatthias Ringwald break; 690bd021c4eSMatthias Ringwald } 691bd021c4eSMatthias Ringwald break; 692bd021c4eSMatthias Ringwald case HCI_EVENT_PACKET: 693bd021c4eSMatthias Ringwald case HCI_ACL_DATA_PACKET: 694bd021c4eSMatthias Ringwald case HCI_SCO_DATA_PACKET: 69594764e99SMatthias Ringwald // seems like peer is awake 69694764e99SMatthias Ringwald link_peer_asleep = 0; 69794764e99SMatthias Ringwald // forward packet to stack 698bd021c4eSMatthias Ringwald packet_handler(link_packet_type, slip_payload, link_payload_len); 69994764e99SMatthias Ringwald // reset inactvitiy timer 70094764e99SMatthias Ringwald hci_transport_inactivity_timer_set(); 701bd021c4eSMatthias Ringwald break; 702bd021c4eSMatthias Ringwald } 703bd021c4eSMatthias Ringwald 704bd021c4eSMatthias Ringwald break; 705bd021c4eSMatthias Ringwald default: 706bd021c4eSMatthias Ringwald break; 707bd021c4eSMatthias Ringwald } 708bd021c4eSMatthias Ringwald 709bd021c4eSMatthias Ringwald hci_transport_link_run(); 710bd021c4eSMatthias Ringwald } 711bd021c4eSMatthias Ringwald 712bd021c4eSMatthias Ringwald // recommendet time until resend: 3 * time of largest packet 713bd021c4eSMatthias Ringwald static uint16_t hci_transport_link_calc_resend_timeout(uint32_t baudrate){ 714*fc6cde64SMatthias Ringwald uint32_t max_packet_size_in_bit = (HCI_INCOMING_PACKET_BUFFER_SIZE + 6) << 3; 715bd021c4eSMatthias Ringwald uint32_t t_max_x3_ms = max_packet_size_in_bit * 3000 / baudrate; 7165c5f3f76SMatthias Ringwald 7175c5f3f76SMatthias Ringwald // allow for BTstack logging and other delays 7185c5f3f76SMatthias Ringwald t_max_x3_ms += 50; 7195c5f3f76SMatthias Ringwald 720d0756da4SMatthias Ringwald log_info("resend timeout for %"PRIu32" baud: %u ms", baudrate, (int) t_max_x3_ms); 721bd021c4eSMatthias Ringwald return t_max_x3_ms; 722bd021c4eSMatthias Ringwald } 723bd021c4eSMatthias Ringwald 724bd021c4eSMatthias Ringwald static void hci_transport_link_update_resend_timeout(uint32_t baudrate){ 725bd021c4eSMatthias Ringwald link_resend_timeout_ms = hci_transport_link_calc_resend_timeout(baudrate); 726bd021c4eSMatthias Ringwald } 727bd021c4eSMatthias Ringwald 728bd021c4eSMatthias Ringwald /// H5 Interface 729bd021c4eSMatthias Ringwald 730bd021c4eSMatthias Ringwald static uint8_t hci_transport_link_read_byte; 731bd021c4eSMatthias Ringwald 732bd021c4eSMatthias Ringwald static void hci_transport_h5_read_next_byte(void){ 733bd021c4eSMatthias Ringwald btstack_uart->receive_block(&hci_transport_link_read_byte, 1); 734bd021c4eSMatthias Ringwald } 735bd021c4eSMatthias Ringwald 736e9ff703eSMatthias Ringwald // track time receiving SLIP frame 737e9ff703eSMatthias Ringwald static uint32_t hci_transport_h5_receive_start; 738bd021c4eSMatthias Ringwald static void hci_transport_h5_block_received(){ 739e9ff703eSMatthias Ringwald // track start time when receiving first byte // a bit hackish 740e9ff703eSMatthias Ringwald if (hci_transport_h5_receive_start == 0 && hci_transport_link_read_byte != BTSTACK_SLIP_SOF){ 741e9ff703eSMatthias Ringwald hci_transport_h5_receive_start = btstack_run_loop_get_time_ms(); 742e9ff703eSMatthias Ringwald } 743bd021c4eSMatthias Ringwald btstack_slip_decoder_process(hci_transport_link_read_byte); 744bd021c4eSMatthias Ringwald uint16_t frame_size = btstack_slip_decoder_frame_size(); 745bd021c4eSMatthias Ringwald if (frame_size) { 746e9ff703eSMatthias Ringwald // track time 747e9ff703eSMatthias Ringwald uint32_t packet_receive_time = btstack_run_loop_get_time_ms() - hci_transport_h5_receive_start; 748e9ff703eSMatthias Ringwald uint32_t nominmal_time = (frame_size + 6) * 10 * 1000 / uart_config.baudrate; 749e9ff703eSMatthias Ringwald log_info("slip frame time %u ms for %u decoded bytes. nomimal time %u ms", (int) packet_receive_time, frame_size, (int) nominmal_time); 750e9ff703eSMatthias Ringwald // reset state 751e9ff703eSMatthias Ringwald hci_transport_h5_receive_start = 0; 752e9ff703eSMatthias Ringwald // 753bd021c4eSMatthias Ringwald hci_transport_h5_process_frame(frame_size); 754bd021c4eSMatthias Ringwald hci_transport_slip_init(); 755bd021c4eSMatthias Ringwald } 756bd021c4eSMatthias Ringwald hci_transport_h5_read_next_byte(); 757bd021c4eSMatthias Ringwald } 758bd021c4eSMatthias Ringwald 759bd021c4eSMatthias Ringwald static void hci_transport_h5_block_sent(void){ 7606ae10533SMatthias Ringwald 7616ae10533SMatthias Ringwald // check if more data to send 7626ae10533SMatthias Ringwald if (btstack_slip_encoder_has_data()){ 7636ae10533SMatthias Ringwald hci_transport_slip_send_next_chunk(); 7646ae10533SMatthias Ringwald return; 7656ae10533SMatthias Ringwald } 7666ae10533SMatthias Ringwald 7676ae10533SMatthias Ringwald // done 7686ae10533SMatthias Ringwald slip_write_active = 0; 76994764e99SMatthias Ringwald 77094764e99SMatthias Ringwald // enter sleep mode after sending sleep message 77194764e99SMatthias Ringwald if (hci_transport_link_actions & HCI_TRANSPORT_LINK_ENTER_SLEEP){ 77294764e99SMatthias Ringwald hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_ENTER_SLEEP; 77394764e99SMatthias Ringwald if (btstack_uart_sleep_mode){ 77494764e99SMatthias Ringwald log_info("link: sent sleep message. Enabling UART Sleep."); 77594764e99SMatthias Ringwald btstack_uart->set_sleep(btstack_uart_sleep_mode); 77694764e99SMatthias Ringwald } else { 77794764e99SMatthias Ringwald log_info("link: sent sleep message. UART Sleep not supported"); 77894764e99SMatthias Ringwald } 77962ca45d7SMatthias Ringwald hci_transport_h5_emit_sleep_state(1); 78094764e99SMatthias Ringwald } 78194764e99SMatthias Ringwald 782bd021c4eSMatthias Ringwald hci_transport_link_run(); 783bd021c4eSMatthias Ringwald } 784bd021c4eSMatthias Ringwald 785bd021c4eSMatthias Ringwald static void hci_transport_h5_init(const void * transport_config){ 786bd021c4eSMatthias Ringwald // check for hci_transport_config_uart_t 787bd021c4eSMatthias Ringwald if (!transport_config) { 788bd021c4eSMatthias Ringwald log_error("hci_transport_h5: no config!"); 789bd021c4eSMatthias Ringwald return; 790bd021c4eSMatthias Ringwald } 791bd021c4eSMatthias Ringwald if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) { 792bd021c4eSMatthias Ringwald log_error("hci_transport_h5: config not of type != HCI_TRANSPORT_CONFIG_UART!"); 793bd021c4eSMatthias Ringwald return; 794bd021c4eSMatthias Ringwald } 795bd021c4eSMatthias Ringwald 796bd021c4eSMatthias Ringwald // extract UART config from transport config 797bd021c4eSMatthias Ringwald hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config; 798bd021c4eSMatthias Ringwald uart_config.baudrate = hci_transport_config_uart->baudrate_init; 799bd021c4eSMatthias Ringwald uart_config.flowcontrol = hci_transport_config_uart->flowcontrol; 800bd021c4eSMatthias Ringwald uart_config.device_name = hci_transport_config_uart->device_name; 801bd021c4eSMatthias Ringwald 802bd021c4eSMatthias Ringwald // setup UART driver 803bd021c4eSMatthias Ringwald btstack_uart->init(&uart_config); 804bd021c4eSMatthias Ringwald btstack_uart->set_block_received(&hci_transport_h5_block_received); 805bd021c4eSMatthias Ringwald btstack_uart->set_block_sent(&hci_transport_h5_block_sent); 806bd021c4eSMatthias Ringwald } 807bd021c4eSMatthias Ringwald 808bd021c4eSMatthias Ringwald static int hci_transport_h5_open(void){ 809bd021c4eSMatthias Ringwald int res = btstack_uart->open(); 810bd021c4eSMatthias Ringwald if (res){ 811bd021c4eSMatthias Ringwald return res; 812bd021c4eSMatthias Ringwald } 813bd021c4eSMatthias Ringwald 814daa2e90cSMatthias Ringwald // 815daa2e90cSMatthias Ringwald if (hci_transport_bcsp_mode){ 816b304e673SMatthias Ringwald log_info("enable even parity for BCSP mode"); 817daa2e90cSMatthias Ringwald btstack_uart->set_parity(1); 818daa2e90cSMatthias Ringwald } 819daa2e90cSMatthias Ringwald 820d8e28fa3SMatthias Ringwald // check if wake on RX can be used 821d8e28fa3SMatthias Ringwald btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF; 822d8e28fa3SMatthias Ringwald int supported_sleep_modes = 0; 823d8e28fa3SMatthias Ringwald if (btstack_uart->get_supported_sleep_modes){ 824d8e28fa3SMatthias Ringwald supported_sleep_modes = btstack_uart->get_supported_sleep_modes(); 825d8e28fa3SMatthias Ringwald } 826d8e28fa3SMatthias Ringwald if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){ 827b304e673SMatthias Ringwald log_info("using wake on RX"); 828d8e28fa3SMatthias Ringwald btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE; 829d8e28fa3SMatthias Ringwald } else { 830b304e673SMatthias Ringwald log_info("UART driver does not provide compatible sleep mode"); 831d8e28fa3SMatthias Ringwald } 832d8e28fa3SMatthias Ringwald 833bd021c4eSMatthias Ringwald // setup resend timeout 834bd021c4eSMatthias Ringwald hci_transport_link_update_resend_timeout(uart_config.baudrate); 835bd021c4eSMatthias Ringwald 836bd021c4eSMatthias Ringwald // init slip parser state machine 837bd021c4eSMatthias Ringwald hci_transport_slip_init(); 838bd021c4eSMatthias Ringwald 839bd021c4eSMatthias Ringwald // init link management - already starts syncing 840bd021c4eSMatthias Ringwald hci_transport_link_init(); 841bd021c4eSMatthias Ringwald 842bd021c4eSMatthias Ringwald // start receiving 843bd021c4eSMatthias Ringwald hci_transport_h5_read_next_byte(); 844bd021c4eSMatthias Ringwald 845bd021c4eSMatthias Ringwald return 0; 846bd021c4eSMatthias Ringwald } 847bd021c4eSMatthias Ringwald 848bd021c4eSMatthias Ringwald static int hci_transport_h5_close(void){ 849bd021c4eSMatthias Ringwald return btstack_uart->close(); 850bd021c4eSMatthias Ringwald } 851bd021c4eSMatthias Ringwald 852bd021c4eSMatthias Ringwald static void hci_transport_h5_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 853bd021c4eSMatthias Ringwald packet_handler = handler; 854bd021c4eSMatthias Ringwald } 855bd021c4eSMatthias Ringwald 856bd021c4eSMatthias Ringwald static int hci_transport_h5_can_send_packet_now(uint8_t packet_type){ 85794764e99SMatthias Ringwald int res = !hci_transport_link_have_outgoing_packet() && link_state == LINK_ACTIVE; 858b304e673SMatthias Ringwald // log_info("can_send_packet_now: %u", res); 85994764e99SMatthias Ringwald return res; 860bd021c4eSMatthias Ringwald } 861bd021c4eSMatthias Ringwald 862bd021c4eSMatthias Ringwald static int hci_transport_h5_send_packet(uint8_t packet_type, uint8_t *packet, int size){ 863bd021c4eSMatthias Ringwald if (!hci_transport_h5_can_send_packet_now(packet_type)){ 864f04a0c31SMatthias Ringwald log_error("hci_transport_h5_send_packet called but in state %d", link_state); 865bd021c4eSMatthias Ringwald return -1; 866bd021c4eSMatthias Ringwald } 867bd021c4eSMatthias Ringwald 868bd021c4eSMatthias Ringwald // store request 869bd021c4eSMatthias Ringwald hci_transport_h5_queue_packet(packet_type, packet, size); 870bd021c4eSMatthias Ringwald 871bd021c4eSMatthias Ringwald // send wakeup first 872bd021c4eSMatthias Ringwald if (link_peer_asleep){ 87362ca45d7SMatthias Ringwald hci_transport_h5_emit_sleep_state(0); 874d8e28fa3SMatthias Ringwald if (btstack_uart_sleep_mode){ 875b304e673SMatthias Ringwald log_info("disable UART sleep"); 876d8e28fa3SMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 877d8e28fa3SMatthias Ringwald } 878bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WAKEUP; 879bd021c4eSMatthias Ringwald hci_transport_link_set_timer(LINK_WAKEUP_MS); 880bd021c4eSMatthias Ringwald } else { 881bd021c4eSMatthias Ringwald hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET; 882bd021c4eSMatthias Ringwald hci_transport_link_set_timer(link_resend_timeout_ms); 883bd021c4eSMatthias Ringwald } 884bd021c4eSMatthias Ringwald hci_transport_link_run(); 885bd021c4eSMatthias Ringwald return 0; 886bd021c4eSMatthias Ringwald } 887bd021c4eSMatthias Ringwald 888bd021c4eSMatthias Ringwald static int hci_transport_h5_set_baudrate(uint32_t baudrate){ 889bd021c4eSMatthias Ringwald 890d0756da4SMatthias Ringwald log_info("set_baudrate %"PRIu32, baudrate); 891bd021c4eSMatthias Ringwald int res = btstack_uart->set_baudrate(baudrate); 892bd021c4eSMatthias Ringwald 893bd021c4eSMatthias Ringwald if (res) return res; 894bd021c4eSMatthias Ringwald hci_transport_link_update_resend_timeout(baudrate); 895bd021c4eSMatthias Ringwald return 0; 896bd021c4eSMatthias Ringwald } 897bd021c4eSMatthias Ringwald 898bd021c4eSMatthias Ringwald static void hci_transport_h5_reset_link(void){ 899bd021c4eSMatthias Ringwald 900b304e673SMatthias Ringwald log_info("reset_link"); 901bd021c4eSMatthias Ringwald 902bd021c4eSMatthias Ringwald // clear outgoing queue 903bd021c4eSMatthias Ringwald hci_transport_link_clear_queue(); 904bd021c4eSMatthias Ringwald 905bd021c4eSMatthias Ringwald // init slip parser state machine 906bd021c4eSMatthias Ringwald hci_transport_slip_init(); 907bd021c4eSMatthias Ringwald 908bd021c4eSMatthias Ringwald // init link management - already starts syncing 909bd021c4eSMatthias Ringwald hci_transport_link_init(); 910bd021c4eSMatthias Ringwald } 911bd021c4eSMatthias Ringwald 912bd021c4eSMatthias Ringwald static const hci_transport_t hci_transport_h5 = { 913bd021c4eSMatthias Ringwald /* const char * name; */ "H5", 914bd021c4eSMatthias Ringwald /* void (*init) (const void *transport_config); */ &hci_transport_h5_init, 915bd021c4eSMatthias Ringwald /* int (*open)(void); */ &hci_transport_h5_open, 916bd021c4eSMatthias Ringwald /* int (*close)(void); */ &hci_transport_h5_close, 917bd021c4eSMatthias Ringwald /* void (*register_packet_handler)(void (*handler)(...); */ &hci_transport_h5_register_packet_handler, 918bd021c4eSMatthias Ringwald /* int (*can_send_packet_now)(uint8_t packet_type); */ &hci_transport_h5_can_send_packet_now, 919bd021c4eSMatthias Ringwald /* int (*send_packet)(...); */ &hci_transport_h5_send_packet, 920bd021c4eSMatthias Ringwald /* int (*set_baudrate)(uint32_t baudrate); */ &hci_transport_h5_set_baudrate, 921bd021c4eSMatthias Ringwald /* void (*reset_link)(void); */ &hci_transport_h5_reset_link, 922ee752bb8SMatthias Ringwald /* void (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL, 923bd021c4eSMatthias Ringwald }; 924bd021c4eSMatthias Ringwald 925bd021c4eSMatthias Ringwald // configure and return h5 singleton 926bd021c4eSMatthias Ringwald const hci_transport_t * hci_transport_h5_instance(const btstack_uart_block_t * uart_driver) { 927bd021c4eSMatthias Ringwald btstack_uart = uart_driver; 928bd021c4eSMatthias Ringwald return &hci_transport_h5; 929bd021c4eSMatthias Ringwald } 93094764e99SMatthias Ringwald 93194764e99SMatthias Ringwald void hci_transport_h5_set_auto_sleep(uint16_t inactivity_timeout_ms){ 93294764e99SMatthias Ringwald link_inactivity_timeout_ms = inactivity_timeout_ms; 93394764e99SMatthias Ringwald } 934daa2e90cSMatthias Ringwald 935daa2e90cSMatthias Ringwald void hci_transport_h5_enable_bcsp_mode(void){ 936daa2e90cSMatthias Ringwald hci_transport_bcsp_mode = 1; 937daa2e90cSMatthias Ringwald } 938