xref: /btstack/src/hci_transport_h5.c (revision a48f52afac3c677b8e8a9edd1837d94587efa4b8)
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 
38*a48f52afSMatthias Ringwald #define __BTSTACK_FILE__ "hci_transport_h5.c"
39ab2c6ae4SMatthias Ringwald 
40bd021c4eSMatthias Ringwald /*
41bd021c4eSMatthias Ringwald  *  hci_transport_h5.c
42bd021c4eSMatthias Ringwald  *
43*a48f52afSMatthias Ringwald  *  HCI Transport API implementation for basic H5 protocol based on UART driver with SLIP support
44bd021c4eSMatthias Ringwald  */
45bd021c4eSMatthias Ringwald 
46*a48f52afSMatthias Ringwald // #define ENABLE_LOG_DEBUG
47*a48f52afSMatthias Ringwald 
48d0756da4SMatthias Ringwald #include <inttypes.h>
49d0756da4SMatthias Ringwald 
50bd021c4eSMatthias Ringwald #include "btstack_debug.h"
51*a48f52afSMatthias Ringwald #include "hci.h"
52bd021c4eSMatthias Ringwald #include "hci_transport.h"
53*a48f52afSMatthias Ringwald 
54*a48f52afSMatthias Ringwald // assert pre-buffer for packet type is available
55*a48f52afSMatthias Ringwald #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE < 4)
56*a48f52afSMatthias Ringwald #error HCI_OUTGOING_PRE_BUFFER_SIZE not defined or smaller than 4. Please update hci.h
57*a48f52afSMatthias Ringwald #endif
58bd021c4eSMatthias Ringwald 
59bd021c4eSMatthias Ringwald typedef enum {
60bd021c4eSMatthias Ringwald     LINK_UNINITIALIZED,
61bd021c4eSMatthias Ringwald     LINK_INITIALIZED,
62bd021c4eSMatthias Ringwald     LINK_ACTIVE
63bd021c4eSMatthias Ringwald } hci_transport_link_state_t;
64bd021c4eSMatthias Ringwald 
65bd021c4eSMatthias Ringwald typedef enum {
66bd021c4eSMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_SYNC                  = 1 <<  0,
67bd021c4eSMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE         = 1 <<  1,
68bd021c4eSMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_CONFIG                = 1 <<  2,
69bf71c899SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE_EMPTY = 1 <<  3,
70bf71c899SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE       = 1 <<  4,
71bf71c899SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_SLEEP                 = 1 <<  5,
72bf71c899SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_WOKEN                 = 1 <<  6,
73bf71c899SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_WAKEUP                = 1 <<  7,
74bf71c899SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET         = 1 <<  8,
75bf71c899SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_ACK_PACKET            = 1 <<  9,
76bf71c899SMatthias Ringwald     HCI_TRANSPORT_LINK_ENTER_SLEEP                = 1 << 10,
77*a48f52afSMatthias Ringwald     HCI_TRANSPORT_LINK_SET_BAUDRATE               = 1 << 11,
7894764e99SMatthias Ringwald 
79bd021c4eSMatthias Ringwald } hci_transport_link_actions_t;
80bd021c4eSMatthias Ringwald 
8179b61987SMatthias Ringwald // Configuration Field. No packet buffers -> sliding window = 1, no OOF flow control, support data integrity check
82bd021c4eSMatthias Ringwald #define LINK_CONFIG_SLIDING_WINDOW_SIZE 1
83bd021c4eSMatthias Ringwald #define LINK_CONFIG_OOF_FLOW_CONTROL 0
8479b61987SMatthias Ringwald #define LINK_CONFIG_DATA_INTEGRITY_CHECK 1
85bd021c4eSMatthias Ringwald #define LINK_CONFIG_VERSION_NR 0
86bd021c4eSMatthias 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))
87bd021c4eSMatthias Ringwald 
88bd021c4eSMatthias Ringwald // periodic sending during link establishment
89bd021c4eSMatthias Ringwald #define LINK_PERIOD_MS 250
90bd021c4eSMatthias Ringwald 
91bd021c4eSMatthias Ringwald // resend wakeup
92bd021c4eSMatthias Ringwald #define LINK_WAKEUP_MS 50
93bd021c4eSMatthias Ringwald 
94bd021c4eSMatthias Ringwald // additional packet types
95bd021c4eSMatthias Ringwald #define LINK_ACKNOWLEDGEMENT_TYPE 0x00
96bd021c4eSMatthias Ringwald #define LINK_CONTROL_PACKET_TYPE 0x0f
97bd021c4eSMatthias Ringwald 
98bd021c4eSMatthias Ringwald // ---
99bd021c4eSMatthias Ringwald static const uint8_t link_control_sync[] =   { 0x01, 0x7e};
100bd021c4eSMatthias Ringwald static const uint8_t link_control_sync_response[] = { 0x02, 0x7d};
101bd021c4eSMatthias Ringwald static const uint8_t link_control_config[] = { 0x03, 0xfc, LINK_CONFIG_FIELD};
102*a48f52afSMatthias Ringwald static const uint8_t link_control_config_prefix_len  = 2;
103*a48f52afSMatthias Ringwald static const uint8_t link_control_config_response_empty[] = { 0x04, 0x7b};
104bd021c4eSMatthias Ringwald static const uint8_t link_control_config_response[] = { 0x04, 0x7b, LINK_CONFIG_FIELD};
105*a48f52afSMatthias Ringwald static const uint8_t link_control_config_response_prefix_len  = 2;
106bd021c4eSMatthias Ringwald static const uint8_t link_control_wakeup[] = { 0x05, 0xfa};
107bd021c4eSMatthias Ringwald static const uint8_t link_control_woken[] =  { 0x06, 0xf9};
108bd021c4eSMatthias Ringwald static const uint8_t link_control_sleep[] =  { 0x07, 0x78};
109bd021c4eSMatthias Ringwald 
110c6df6d84SMatthias Ringwald // max size of link control messages
111c6df6d84SMatthias Ringwald #define LINK_CONTROL_MAX_LEN 3
112c6df6d84SMatthias Ringwald 
113bd021c4eSMatthias Ringwald // incoming pre-bufffer + 4 bytes H5 header + max(acl header + acl payload, event header + event data) + 2 bytes opt CRC
114fc6cde64SMatthias Ringwald static uint8_t   hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 6 + HCI_INCOMING_PACKET_BUFFER_SIZE];
115bd021c4eSMatthias Ringwald 
116c65ae2a0SMatthias Ringwald // outgoing slip encoded buffer. +4 to assert that DIC fits in buffer. +1 to assert that last SOF fits in buffer.
1176ae10533SMatthias Ringwald static int       slip_write_active;
118bd021c4eSMatthias Ringwald 
119bd021c4eSMatthias Ringwald // H5 Link State
120bd021c4eSMatthias Ringwald static hci_transport_link_state_t link_state;
121bd021c4eSMatthias Ringwald static btstack_timer_source_t link_timer;
122bd021c4eSMatthias Ringwald static uint8_t  link_seq_nr;
123bd021c4eSMatthias Ringwald static uint8_t  link_ack_nr;
124bd021c4eSMatthias Ringwald static uint16_t link_resend_timeout_ms;
125bd021c4eSMatthias Ringwald static uint8_t  link_peer_asleep;
1267c2aa31eSMatthias Ringwald static uint8_t  link_peer_supports_data_integrity_check;
127*a48f52afSMatthias Ringwald static uint32_t link_new_baudrate;
128bd021c4eSMatthias Ringwald 
12994764e99SMatthias Ringwald // auto sleep-mode
13094764e99SMatthias Ringwald static btstack_timer_source_t inactivity_timer;
13194764e99SMatthias Ringwald static uint16_t link_inactivity_timeout_ms; // auto-sleep if set
13294764e99SMatthias Ringwald 
133bd021c4eSMatthias Ringwald // Outgoing packet
134bd021c4eSMatthias Ringwald static uint8_t   hci_packet_type;
135bd021c4eSMatthias Ringwald static uint16_t  hci_packet_size;
136bd021c4eSMatthias Ringwald static uint8_t * hci_packet;
137bd021c4eSMatthias Ringwald 
138*a48f52afSMatthias Ringwald // restore 2 bytes temp overwritten by DIC
139*a48f52afSMatthias Ringwald static uint8_t * hci_packet_restore_dic_address;
140*a48f52afSMatthias Ringwald static uint16_t  hci_packet_restore_dic_data;
141*a48f52afSMatthias Ringwald 
142bd021c4eSMatthias Ringwald // hci packet handler
143bd021c4eSMatthias Ringwald static  void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size);
144bd021c4eSMatthias Ringwald 
145bd021c4eSMatthias Ringwald static int hci_transport_link_actions;
146bd021c4eSMatthias Ringwald 
147bd021c4eSMatthias Ringwald // UART Driver + Config
148*a48f52afSMatthias Ringwald static const btstack_uart_t * btstack_uart;
149bd021c4eSMatthias Ringwald static btstack_uart_config_t uart_config;
150d8e28fa3SMatthias Ringwald static btstack_uart_sleep_mode_t btstack_uart_sleep_mode;
151daa2e90cSMatthias Ringwald static int hci_transport_bcsp_mode;
152bd021c4eSMatthias Ringwald 
153bd021c4eSMatthias Ringwald // Prototypes
154bd021c4eSMatthias Ringwald static int  hci_transport_link_have_outgoing_packet(void);
155*a48f52afSMatthias Ringwald static void hci_transport_h5_frame_sent(void);
156*a48f52afSMatthias Ringwald static void hci_transport_h5_process_frame(uint16_t frame_size);
157*a48f52afSMatthias Ringwald static void hci_transport_link_run(void);
158bd021c4eSMatthias Ringwald static void hci_transport_link_send_queued_packet(void);
159bd021c4eSMatthias Ringwald static void hci_transport_link_set_timer(uint16_t timeout_ms);
160bd021c4eSMatthias Ringwald static void hci_transport_link_timeout_handler(btstack_timer_source_t * timer);
161bd021c4eSMatthias Ringwald static void hci_transport_slip_init(void);
162bd021c4eSMatthias Ringwald 
1637c2aa31eSMatthias Ringwald // -----------------------------
16479b61987SMatthias Ringwald // CRC16-CCITT Calculation - compromise: use 32 byte table - 512 byte table would be faster, but that's too large
16579b61987SMatthias Ringwald 
16679b61987SMatthias Ringwald static const uint16_t crc16_ccitt_table[] ={
16779b61987SMatthias Ringwald     0x0000, 0x1081, 0x2102, 0x3183,
16879b61987SMatthias Ringwald     0x4204, 0x5285, 0x6306, 0x7387,
16979b61987SMatthias Ringwald     0x8408, 0x9489, 0xa50a, 0xb58b,
17079b61987SMatthias Ringwald     0xc60c, 0xd68d, 0xe70e, 0xf78f
17179b61987SMatthias Ringwald };
17279b61987SMatthias Ringwald 
173*a48f52afSMatthias Ringwald static uint16_t crc16_ccitt_update (uint16_t crc, uint8_t ch){
174*a48f52afSMatthias Ringwald     crc = (crc >> 4) ^ crc16_ccitt_table[(crc ^ ch) & 0x000f];
175*a48f52afSMatthias Ringwald     crc = (crc >> 4) ^ crc16_ccitt_table[(crc ^ (ch >> 4)) & 0x000f];
17679b61987SMatthias Ringwald     return crc;
17779b61987SMatthias Ringwald }
17879b61987SMatthias Ringwald 
17979b61987SMatthias Ringwald static uint16_t btstack_reverse_bits_16(uint16_t value){
18079b61987SMatthias Ringwald     int reverse = 0;
18179b61987SMatthias Ringwald     int i;
18279b61987SMatthias Ringwald     for (i = 0; i < 16; i++) {
18379b61987SMatthias Ringwald         reverse = reverse << 1;
184*a48f52afSMatthias Ringwald         reverse |= value & 1;
18579b61987SMatthias Ringwald         value = value >> 1;
18679b61987SMatthias Ringwald     }
18779b61987SMatthias Ringwald     return reverse;
18879b61987SMatthias Ringwald }
18979b61987SMatthias Ringwald 
190*a48f52afSMatthias Ringwald static uint16_t crc16_calc_for_slip_frame(const uint8_t * data, uint16_t len){
19179b61987SMatthias Ringwald     int i;
19279b61987SMatthias Ringwald     uint16_t crc = 0xffff;
19379b61987SMatthias Ringwald     for (i=0 ; i < len ; i++){
194*a48f52afSMatthias Ringwald         crc = crc16_ccitt_update(crc, data[i]);
19579b61987SMatthias Ringwald     }
19679b61987SMatthias Ringwald     return btstack_reverse_bits_16(crc);
19779b61987SMatthias Ringwald }
19879b61987SMatthias Ringwald 
199bd021c4eSMatthias Ringwald // -----------------------------
20094764e99SMatthias Ringwald static void hci_transport_inactivity_timeout_handler(btstack_timer_source_t * ts){
201b304e673SMatthias Ringwald     log_info("inactivity timeout. link state %d, peer asleep %u, actions 0x%02x, outgoing packet %u",
20294764e99SMatthias Ringwald         link_state, link_peer_asleep, hci_transport_link_actions, hci_transport_link_have_outgoing_packet());
20394764e99SMatthias Ringwald     if (hci_transport_link_have_outgoing_packet()) return;
20494764e99SMatthias Ringwald     if (link_state != LINK_ACTIVE) return;
20594764e99SMatthias Ringwald     if (hci_transport_link_actions) return;
20694764e99SMatthias Ringwald     if (link_peer_asleep) return;
20794764e99SMatthias Ringwald     hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SLEEP;
20894764e99SMatthias Ringwald     hci_transport_link_run();
20994764e99SMatthias Ringwald }
210bd021c4eSMatthias Ringwald 
21194764e99SMatthias Ringwald static void hci_transport_inactivity_timer_set(void){
21294764e99SMatthias Ringwald     if (!link_inactivity_timeout_ms) return;
21394764e99SMatthias Ringwald     btstack_run_loop_set_timer_handler(&inactivity_timer, &hci_transport_inactivity_timeout_handler);
21494764e99SMatthias Ringwald     btstack_run_loop_set_timer(&inactivity_timer, link_inactivity_timeout_ms);
21594764e99SMatthias Ringwald     btstack_run_loop_remove_timer(&inactivity_timer);
21694764e99SMatthias Ringwald     btstack_run_loop_add_timer(&inactivity_timer);
21794764e99SMatthias Ringwald }
218bd021c4eSMatthias Ringwald 
219bd021c4eSMatthias Ringwald static void hci_transport_slip_init(void){
220*a48f52afSMatthias Ringwald     btstack_uart->receive_frame(&hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], 6 + HCI_INCOMING_PACKET_BUFFER_SIZE);
221bd021c4eSMatthias Ringwald }
222bd021c4eSMatthias Ringwald 
223bd021c4eSMatthias Ringwald // H5 Three-Wire Implementation
224bd021c4eSMatthias Ringwald 
225bd021c4eSMatthias Ringwald static void hci_transport_link_calc_header(uint8_t * header,
226bd021c4eSMatthias Ringwald     uint8_t  sequence_nr,
227bd021c4eSMatthias Ringwald     uint8_t  acknowledgement_nr,
228bd021c4eSMatthias Ringwald     uint8_t  data_integrity_check_present,
229bd021c4eSMatthias Ringwald     uint8_t  reliable_packet,
230bd021c4eSMatthias Ringwald     uint8_t  packet_type,
231bd021c4eSMatthias Ringwald     uint16_t payload_length){
232bd021c4eSMatthias Ringwald 
233*a48f52afSMatthias Ringwald     // unreliable packets have seq_nr = 0
234*a48f52afSMatthias Ringwald     if (reliable_packet == 0) {
235*a48f52afSMatthias Ringwald         sequence_nr = 0;
236*a48f52afSMatthias Ringwald     }
237*a48f52afSMatthias Ringwald 
238bd021c4eSMatthias Ringwald     header[0] = sequence_nr | (acknowledgement_nr << 3) | (data_integrity_check_present << 6) | (reliable_packet << 7);
239*a48f52afSMatthias Ringwald     header[1] = packet_type | ((payload_length & 0x0f) << 4);
240bd021c4eSMatthias Ringwald     header[2] = payload_length >> 4;
241*a48f52afSMatthias Ringwald     header[3] = 0xff - (header[0] + header[1] + header[2]);
242*a48f52afSMatthias Ringwald }
243*a48f52afSMatthias Ringwald 
244*a48f52afSMatthias Ringwald // Store DIC after packet, assuming 2 bytes in buffer - keep track of overwritten bytes - relevant for fragmented packets
245*a48f52afSMatthias Ringwald static void hci_transport_slip_send_frame_with_dic(uint8_t * frame, uint16_t frame_size){
246*a48f52afSMatthias Ringwald     int slip_outgoing_dic_present = frame[0] & 0x40;
247*a48f52afSMatthias Ringwald     if (slip_outgoing_dic_present){
248*a48f52afSMatthias Ringwald         // preserved data at DIC location
249*a48f52afSMatthias Ringwald         hci_packet_restore_dic_address = &frame[frame_size];
250*a48f52afSMatthias Ringwald         hci_packet_restore_dic_data = little_endian_read_16(hci_packet_restore_dic_address, 0);
251*a48f52afSMatthias Ringwald         // calc and set DIC
252*a48f52afSMatthias Ringwald         uint16_t data_integrity_check = crc16_calc_for_slip_frame(frame, frame_size);
253*a48f52afSMatthias Ringwald         big_endian_store_16(frame, frame_size, data_integrity_check);
254*a48f52afSMatthias Ringwald         frame_size += 2;
255*a48f52afSMatthias Ringwald     }
256*a48f52afSMatthias Ringwald 
257*a48f52afSMatthias Ringwald     // set slip send active and go
258*a48f52afSMatthias Ringwald     slip_write_active = 1;
259*a48f52afSMatthias Ringwald     btstack_uart->send_frame(frame, frame_size);
260*a48f52afSMatthias Ringwald }
261*a48f52afSMatthias Ringwald 
262*a48f52afSMatthias Ringwald static void hci_transport_link_send_queued_packet(void){
263*a48f52afSMatthias Ringwald     uint8_t * buffer =      hci_packet      - 4;
264*a48f52afSMatthias Ringwald     uint16_t  buffer_size = hci_packet_size + 4;
265*a48f52afSMatthias Ringwald 
266*a48f52afSMatthias Ringwald     // setup header
267*a48f52afSMatthias Ringwald     int reliable = hci_packet_type == HCI_SCO_DATA_PACKET ? 0 : 1;
268*a48f52afSMatthias Ringwald     hci_transport_link_calc_header(buffer, link_seq_nr, link_ack_nr, link_peer_supports_data_integrity_check, reliable, hci_packet_type, hci_packet_size);
269*a48f52afSMatthias Ringwald 
270*a48f52afSMatthias Ringwald     // send frame with dic
271*a48f52afSMatthias Ringwald     log_debug("send queued packet: seq %u, ack %u, size %u, append dic %u", link_seq_nr, link_ack_nr, hci_packet_size, link_peer_supports_data_integrity_check);
272*a48f52afSMatthias Ringwald     log_debug_hexdump(hci_packet, hci_packet_size);
273*a48f52afSMatthias Ringwald     hci_transport_slip_send_frame_with_dic(buffer, buffer_size);
274*a48f52afSMatthias Ringwald 
275*a48f52afSMatthias Ringwald     // reset inactvitiy timer
276*a48f52afSMatthias Ringwald     hci_transport_inactivity_timer_set();
277bd021c4eSMatthias Ringwald }
278bd021c4eSMatthias Ringwald 
279bd021c4eSMatthias Ringwald static void hci_transport_link_send_control(const uint8_t * message, int message_len){
280*a48f52afSMatthias Ringwald     uint8_t  buffer[4 + LINK_CONTROL_MAX_LEN + 2];
281*a48f52afSMatthias Ringwald     uint16_t buffer_size = 4 + message_len;
282*a48f52afSMatthias Ringwald 
283*a48f52afSMatthias Ringwald     // setup header
284*a48f52afSMatthias Ringwald     hci_transport_link_calc_header(buffer, 0, 0, link_peer_supports_data_integrity_check, 0, LINK_CONTROL_PACKET_TYPE, message_len);
285*a48f52afSMatthias Ringwald 
286*a48f52afSMatthias Ringwald     // setup payload
287*a48f52afSMatthias Ringwald     memcpy(&buffer[4], message, message_len);
288*a48f52afSMatthias Ringwald 
289*a48f52afSMatthias Ringwald     // send frame with dic
290*a48f52afSMatthias Ringwald     log_debug("send control: size %u, append dic %u", message_len, link_peer_supports_data_integrity_check);
291b304e673SMatthias Ringwald     log_debug_hexdump(message, message_len);
292*a48f52afSMatthias Ringwald     hci_transport_slip_send_frame_with_dic(buffer, buffer_size);
293*a48f52afSMatthias Ringwald }
294*a48f52afSMatthias Ringwald 
295*a48f52afSMatthias Ringwald static void hci_transport_link_send_ack_packet(void){
296*a48f52afSMatthias Ringwald     // Pure ACK package is without DIC as there is no payload either
297*a48f52afSMatthias Ringwald     log_debug("send ack %u", link_ack_nr);
298*a48f52afSMatthias Ringwald     uint8_t header[4];
299*a48f52afSMatthias Ringwald     hci_transport_link_calc_header(header, 0, link_ack_nr, 0, 0, LINK_ACKNOWLEDGEMENT_TYPE, 0);
300*a48f52afSMatthias Ringwald     hci_transport_slip_send_frame_with_dic(header, sizeof(header));
301bd021c4eSMatthias Ringwald }
302bd021c4eSMatthias Ringwald 
303bd021c4eSMatthias Ringwald static void hci_transport_link_send_sync(void){
304b304e673SMatthias Ringwald     log_debug("link send sync");
305bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_sync, sizeof(link_control_sync));
306bd021c4eSMatthias Ringwald }
307bd021c4eSMatthias Ringwald 
308bd021c4eSMatthias Ringwald static void hci_transport_link_send_sync_response(void){
309b304e673SMatthias Ringwald     log_debug("link send sync response");
310bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_sync_response, sizeof(link_control_sync_response));
311bd021c4eSMatthias Ringwald }
312bd021c4eSMatthias Ringwald 
313bd021c4eSMatthias Ringwald static void hci_transport_link_send_config(void){
314b304e673SMatthias Ringwald     log_debug("link send config");
315bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_config, sizeof(link_control_config));
316bd021c4eSMatthias Ringwald }
317bd021c4eSMatthias Ringwald 
318bd021c4eSMatthias Ringwald static void hci_transport_link_send_config_response(void){
319b304e673SMatthias Ringwald     log_debug("link send config response");
320bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_config_response, sizeof(link_control_config_response));
321bd021c4eSMatthias Ringwald }
322bd021c4eSMatthias Ringwald 
323bf71c899SMatthias Ringwald static void hci_transport_link_send_config_response_empty(void){
324bf71c899SMatthias Ringwald     log_debug("link send config response empty");
325bf71c899SMatthias Ringwald     hci_transport_link_send_control(link_control_config_response_empty, sizeof(link_control_config_response_empty));
326bf71c899SMatthias Ringwald }
327bf71c899SMatthias Ringwald 
328bd021c4eSMatthias Ringwald static void hci_transport_link_send_woken(void){
329b304e673SMatthias Ringwald     log_debug("link send woken");
330bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_woken, sizeof(link_control_woken));
331bd021c4eSMatthias Ringwald }
332bd021c4eSMatthias Ringwald 
333bd021c4eSMatthias Ringwald static void hci_transport_link_send_wakeup(void){
334b304e673SMatthias Ringwald     log_debug("link send wakeup");
335bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_wakeup, sizeof(link_control_wakeup));
336bd021c4eSMatthias Ringwald }
337bd021c4eSMatthias Ringwald 
33894764e99SMatthias Ringwald static void hci_transport_link_send_sleep(void){
339b304e673SMatthias Ringwald     log_debug("link send sleep");
34094764e99SMatthias Ringwald     hci_transport_link_send_control(link_control_sleep, sizeof(link_control_sleep));
34194764e99SMatthias Ringwald }
34294764e99SMatthias Ringwald 
343bd021c4eSMatthias Ringwald static void hci_transport_link_run(void){
344bd021c4eSMatthias Ringwald     // exit if outgoing active
3456ae10533SMatthias Ringwald     if (slip_write_active) return;
346bd021c4eSMatthias Ringwald 
347bd021c4eSMatthias Ringwald     // process queued requests
348bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SYNC){
349bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SYNC;
350bd021c4eSMatthias Ringwald         hci_transport_link_send_sync();
351bd021c4eSMatthias Ringwald         return;
352bd021c4eSMatthias Ringwald     }
353bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE){
354bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE;
355bd021c4eSMatthias Ringwald         hci_transport_link_send_sync_response();
356bd021c4eSMatthias Ringwald         return;
357bd021c4eSMatthias Ringwald     }
358bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_CONFIG){
359bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_CONFIG;
360bd021c4eSMatthias Ringwald         hci_transport_link_send_config();
361bd021c4eSMatthias Ringwald         return;
362bd021c4eSMatthias Ringwald     }
363bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE){
364bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE;
365bd021c4eSMatthias Ringwald         hci_transport_link_send_config_response();
366bd021c4eSMatthias Ringwald         return;
367bd021c4eSMatthias Ringwald     }
368bf71c899SMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE_EMPTY){
369bf71c899SMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE_EMPTY;
370bf71c899SMatthias Ringwald         hci_transport_link_send_config_response_empty();
371bf71c899SMatthias Ringwald         return;
372bf71c899SMatthias Ringwald     }
373bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_WOKEN){
374bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_WOKEN;
375bd021c4eSMatthias Ringwald         hci_transport_link_send_woken();
376bd021c4eSMatthias Ringwald         return;
377bd021c4eSMatthias Ringwald     }
378bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_WAKEUP){
379bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_WAKEUP;
380bd021c4eSMatthias Ringwald         hci_transport_link_send_wakeup();
381bd021c4eSMatthias Ringwald         return;
382bd021c4eSMatthias Ringwald     }
383bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET){
384bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET;
385bd021c4eSMatthias Ringwald         // packet already contains ack, no need to send addtitional one
386bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_ACK_PACKET;
387bd021c4eSMatthias Ringwald         hci_transport_link_send_queued_packet();
388bd021c4eSMatthias Ringwald         return;
389bd021c4eSMatthias Ringwald     }
390bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_ACK_PACKET){
391bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_ACK_PACKET;
392bd021c4eSMatthias Ringwald         hci_transport_link_send_ack_packet();
393bd021c4eSMatthias Ringwald         return;
394bd021c4eSMatthias Ringwald     }
39594764e99SMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SLEEP){
39694764e99SMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SLEEP;
39794764e99SMatthias Ringwald         hci_transport_link_actions |=  HCI_TRANSPORT_LINK_ENTER_SLEEP;
39894764e99SMatthias Ringwald         link_peer_asleep = 1;
39994764e99SMatthias Ringwald         hci_transport_link_send_sleep();
40094764e99SMatthias Ringwald         return;
40194764e99SMatthias Ringwald     }
402bd021c4eSMatthias Ringwald }
403bd021c4eSMatthias Ringwald 
404bd021c4eSMatthias Ringwald static void hci_transport_link_set_timer(uint16_t timeout_ms){
405*a48f52afSMatthias Ringwald     btstack_run_loop_set_timer_handler(&link_timer, &hci_transport_link_timeout_handler);
406bd021c4eSMatthias Ringwald     btstack_run_loop_set_timer(&link_timer, timeout_ms);
407bd021c4eSMatthias Ringwald     btstack_run_loop_add_timer(&link_timer);
408bd021c4eSMatthias Ringwald }
409bd021c4eSMatthias Ringwald 
410*a48f52afSMatthias Ringwald static void hci_transport_link_timeout_handler(btstack_timer_source_t * timer){
411bd021c4eSMatthias Ringwald     switch (link_state){
412bd021c4eSMatthias Ringwald         case LINK_UNINITIALIZED:
413bd021c4eSMatthias Ringwald             hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC;
414bd021c4eSMatthias Ringwald             hci_transport_link_set_timer(LINK_PERIOD_MS);
415bd021c4eSMatthias Ringwald             break;
416bd021c4eSMatthias Ringwald         case LINK_INITIALIZED:
417bd021c4eSMatthias Ringwald             hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG;
418bd021c4eSMatthias Ringwald             hci_transport_link_set_timer(LINK_PERIOD_MS);
419bd021c4eSMatthias Ringwald             break;
420bd021c4eSMatthias Ringwald         case LINK_ACTIVE:
421bd021c4eSMatthias Ringwald             if (!hci_transport_link_have_outgoing_packet()){
422bd021c4eSMatthias Ringwald                 log_info("h5 timeout while active, but no outgoing packet");
423bd021c4eSMatthias Ringwald                 return;
424bd021c4eSMatthias Ringwald             }
425bd021c4eSMatthias Ringwald             if (link_peer_asleep){
426bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WAKEUP;
427bd021c4eSMatthias Ringwald                 hci_transport_link_set_timer(LINK_WAKEUP_MS);
428bd021c4eSMatthias Ringwald                 return;
429bd021c4eSMatthias Ringwald             }
430bd021c4eSMatthias Ringwald             // resend packet
431bd021c4eSMatthias Ringwald             hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET;
432bd021c4eSMatthias Ringwald             hci_transport_link_set_timer(link_resend_timeout_ms);
433bd021c4eSMatthias Ringwald             break;
434bd021c4eSMatthias Ringwald         default:
435bd021c4eSMatthias Ringwald             break;
436bd021c4eSMatthias Ringwald     }
437bd021c4eSMatthias Ringwald 
438bd021c4eSMatthias Ringwald     hci_transport_link_run();
439bd021c4eSMatthias Ringwald }
440bd021c4eSMatthias Ringwald 
441bd021c4eSMatthias Ringwald static void hci_transport_link_init(void){
442bd021c4eSMatthias Ringwald     link_state = LINK_UNINITIALIZED;
443bd021c4eSMatthias Ringwald     link_peer_asleep = 0;
4447c2aa31eSMatthias Ringwald     link_peer_supports_data_integrity_check = 0;
445bd021c4eSMatthias Ringwald 
446bd021c4eSMatthias Ringwald     // get started
447bd021c4eSMatthias Ringwald     hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC;
448bd021c4eSMatthias Ringwald     hci_transport_link_set_timer(LINK_PERIOD_MS);
449bd021c4eSMatthias Ringwald     hci_transport_link_run();
450bd021c4eSMatthias Ringwald }
451bd021c4eSMatthias Ringwald 
452bd021c4eSMatthias Ringwald static int hci_transport_link_inc_seq_nr(int seq_nr){
453bd021c4eSMatthias Ringwald     return (seq_nr + 1) & 0x07;
454bd021c4eSMatthias Ringwald }
455bd021c4eSMatthias Ringwald 
456bd021c4eSMatthias Ringwald static int hci_transport_link_have_outgoing_packet(void){
457*a48f52afSMatthias Ringwald     return hci_packet != 0;
458bd021c4eSMatthias Ringwald }
459bd021c4eSMatthias Ringwald 
460bd021c4eSMatthias Ringwald static void hci_transport_link_clear_queue(void){
461bd021c4eSMatthias Ringwald     btstack_run_loop_remove_timer(&link_timer);
462bd021c4eSMatthias Ringwald     hci_packet = NULL;
463bd021c4eSMatthias Ringwald }
464bd021c4eSMatthias Ringwald 
465bd021c4eSMatthias Ringwald static void hci_transport_h5_queue_packet(uint8_t packet_type, uint8_t *packet, int size){
466bd021c4eSMatthias Ringwald     hci_packet = packet;
467bd021c4eSMatthias Ringwald     hci_packet_type = packet_type;
468bd021c4eSMatthias Ringwald     hci_packet_size = size;
469bd021c4eSMatthias Ringwald }
470bd021c4eSMatthias Ringwald 
47162ca45d7SMatthias Ringwald static void hci_transport_h5_emit_sleep_state(int sleep_active){
47262ca45d7SMatthias Ringwald     static int last_state = 0;
47362ca45d7SMatthias Ringwald     if (sleep_active == last_state) return;
47462ca45d7SMatthias Ringwald     last_state = sleep_active;
47562ca45d7SMatthias Ringwald 
476b304e673SMatthias Ringwald     log_info("emit_sleep_state: %u", sleep_active);
47762ca45d7SMatthias Ringwald     uint8_t event[3];
47862ca45d7SMatthias Ringwald     event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE;
479*a48f52afSMatthias Ringwald     event[1] = sizeof(event) - 2;
48062ca45d7SMatthias Ringwald     event[2] = sleep_active;
48162ca45d7SMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
48262ca45d7SMatthias Ringwald }
48362ca45d7SMatthias Ringwald 
484bd021c4eSMatthias Ringwald static void hci_transport_h5_process_frame(uint16_t frame_size){
485bd021c4eSMatthias Ringwald 
486*a48f52afSMatthias Ringwald     if (frame_size < 4) return;
487bd021c4eSMatthias Ringwald 
488bd021c4eSMatthias Ringwald     uint8_t * slip_header  = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE];
489bd021c4eSMatthias Ringwald     uint8_t * slip_payload = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 4];
490*a48f52afSMatthias Ringwald     int       frame_size_without_header = frame_size - 4;
491bd021c4eSMatthias Ringwald 
492*a48f52afSMatthias Ringwald     uint8_t  seq_nr =  slip_header[0] & 0x07;
493*a48f52afSMatthias Ringwald     uint8_t  ack_nr = (slip_header[0] >> 3)    & 0x07;
494*a48f52afSMatthias Ringwald     uint8_t  data_integrity_check_present = (slip_header[0] & 0x40) != 0;
495*a48f52afSMatthias Ringwald     uint8_t  reliable_packet  = (slip_header[0] & 0x80) != 0;
496*a48f52afSMatthias Ringwald     uint8_t  link_packet_type = slip_header[1] & 0x0f;
497bd021c4eSMatthias Ringwald     uint16_t link_payload_len = (slip_header[1] >> 4) | (slip_header[2] << 4);
498bd021c4eSMatthias Ringwald 
49999136b1cSMatthias 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);
500b304e673SMatthias Ringwald     log_debug_hexdump(slip_header, 4);
501b304e673SMatthias Ringwald     log_debug_hexdump(slip_payload, frame_size_without_header);
502bd021c4eSMatthias Ringwald 
503bd021c4eSMatthias Ringwald     // CSR 8811 does not seem to auto-detect H5 mode and sends data with even parity.
504bd021c4eSMatthias Ringwald     // if this byte sequence is detected, just enable even parity
505bd021c4eSMatthias Ringwald     const uint8_t sync_response_bcsp[] = {0x01, 0x7a, 0x06, 0x10};
506bd021c4eSMatthias Ringwald     if (memcmp(sync_response_bcsp, slip_header, 4) == 0){
507b304e673SMatthias Ringwald         log_info("detected BSCP SYNC sent with Even Parity -> discard frame and enable Even Parity");
508*a48f52afSMatthias Ringwald         btstack_uart->set_parity(BTSTACK_UART_PARITY_EVEN);
509bd021c4eSMatthias Ringwald         return;
510bd021c4eSMatthias Ringwald     }
511bd021c4eSMatthias Ringwald 
512bd021c4eSMatthias Ringwald     // validate header checksum
513bd021c4eSMatthias Ringwald     uint8_t header_checksum = slip_header[0] + slip_header[1] + slip_header[2] + slip_header[3];
514*a48f52afSMatthias Ringwald     if (header_checksum != 0xff){
515b304e673SMatthias Ringwald         log_info("header checksum 0x%02x (instead of 0xff)", header_checksum);
516bd021c4eSMatthias Ringwald         return;
517bd021c4eSMatthias Ringwald     }
518bd021c4eSMatthias Ringwald 
519bd021c4eSMatthias Ringwald     // validate payload length
520bd021c4eSMatthias Ringwald     int data_integrity_len = data_integrity_check_present ? 2 : 0;
521bd021c4eSMatthias Ringwald     uint16_t received_payload_len = frame_size_without_header - data_integrity_len;
522bd021c4eSMatthias Ringwald     if (link_payload_len != received_payload_len){
523b304e673SMatthias Ringwald         log_info("expected payload len %u but got %u", link_payload_len, received_payload_len);
524bd021c4eSMatthias Ringwald         return;
525bd021c4eSMatthias Ringwald     }
526bd021c4eSMatthias Ringwald 
52779b61987SMatthias Ringwald     // validate data integrity check
52879b61987SMatthias Ringwald     if (data_integrity_check_present){
52979b61987SMatthias Ringwald         uint16_t dic_packet = big_endian_read_16(slip_payload, received_payload_len);
530*a48f52afSMatthias Ringwald         uint16_t dic_calculate = crc16_calc_for_slip_frame(slip_header, 4 + received_payload_len);
53179b61987SMatthias Ringwald         if (dic_packet != dic_calculate){
532b304e673SMatthias Ringwald             log_info("expected dic value 0x%04x but got 0x%04x", dic_calculate, dic_packet);
53379b61987SMatthias Ringwald             return;
53479b61987SMatthias Ringwald         }
53579b61987SMatthias Ringwald     }
536bd021c4eSMatthias Ringwald 
537bd021c4eSMatthias Ringwald     switch (link_state){
538bd021c4eSMatthias Ringwald         case LINK_UNINITIALIZED:
539bd021c4eSMatthias Ringwald             if (link_packet_type != LINK_CONTROL_PACKET_TYPE) break;
540bd021c4eSMatthias Ringwald             if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){
541b304e673SMatthias Ringwald                 log_debug("link received sync");
542bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE;
543bf71c899SMatthias Ringwald                 break;
544bd021c4eSMatthias Ringwald             }
545bd021c4eSMatthias Ringwald             if (memcmp(slip_payload, link_control_sync_response, sizeof(link_control_sync_response)) == 0){
546b304e673SMatthias Ringwald                 log_debug("link received sync response");
547bd021c4eSMatthias Ringwald                 link_state = LINK_INITIALIZED;
548bd021c4eSMatthias Ringwald                 btstack_run_loop_remove_timer(&link_timer);
549bd021c4eSMatthias Ringwald                 log_info("link initialized");
550bd021c4eSMatthias Ringwald                 //
551bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG;
552bd021c4eSMatthias Ringwald                 hci_transport_link_set_timer(LINK_PERIOD_MS);
553bf71c899SMatthias Ringwald                 break;
554bd021c4eSMatthias Ringwald             }
555bd021c4eSMatthias Ringwald             break;
556bd021c4eSMatthias Ringwald         case LINK_INITIALIZED:
557bd021c4eSMatthias Ringwald             if (link_packet_type != LINK_CONTROL_PACKET_TYPE) break;
558bd021c4eSMatthias Ringwald             if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){
559b304e673SMatthias Ringwald                 log_debug("link received sync");
560bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE;
561bf71c899SMatthias Ringwald                 break;
562bd021c4eSMatthias Ringwald             }
563cbc04f48SMatthias Ringwald             if (memcmp(slip_payload, link_control_config, link_control_config_prefix_len) == 0){
564bf71c899SMatthias Ringwald                 if (link_payload_len == link_control_config_prefix_len){
565bf71c899SMatthias Ringwald                     log_debug("link received config, no config field");
566bf71c899SMatthias Ringwald                     hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE_EMPTY;
567bf71c899SMatthias Ringwald                 } else {
568b304e673SMatthias Ringwald                     log_debug("link received config, 0x%02x", slip_payload[2]);
569bd021c4eSMatthias Ringwald                     hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE;
570bd021c4eSMatthias Ringwald                 }
571bf71c899SMatthias Ringwald                 break;
572bf71c899SMatthias Ringwald             }
573bd021c4eSMatthias Ringwald             if (memcmp(slip_payload, link_control_config_response, link_control_config_response_prefix_len) == 0){
5747c2aa31eSMatthias Ringwald                 uint8_t config = slip_payload[2];
575*a48f52afSMatthias Ringwald                 link_peer_supports_data_integrity_check = (config & 0x10) != 0;
576b304e673SMatthias Ringwald                 log_info("link received config response 0x%02x, data integrity check supported %u", config, link_peer_supports_data_integrity_check);
577bd021c4eSMatthias Ringwald                 link_state = LINK_ACTIVE;
578bd021c4eSMatthias Ringwald                 btstack_run_loop_remove_timer(&link_timer);
579bd021c4eSMatthias Ringwald                 log_info("link activated");
580bd021c4eSMatthias Ringwald                 //
581bd021c4eSMatthias Ringwald                 link_seq_nr = 0;
582bd021c4eSMatthias Ringwald                 link_ack_nr = 0;
583bd021c4eSMatthias Ringwald                 // notify upper stack that it can start
584bd021c4eSMatthias Ringwald                 uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
585bd021c4eSMatthias Ringwald                 packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
586bf71c899SMatthias Ringwald                 break;
587bd021c4eSMatthias Ringwald             }
588bd021c4eSMatthias Ringwald             break;
589bd021c4eSMatthias Ringwald         case LINK_ACTIVE:
590bd021c4eSMatthias Ringwald 
591bd021c4eSMatthias Ringwald             // validate packet sequence nr in reliable packets (check for out of sequence error)
592bd021c4eSMatthias Ringwald             if (reliable_packet){
593bd021c4eSMatthias Ringwald                 if (seq_nr != link_ack_nr){
594bd021c4eSMatthias Ringwald                     log_info("expected seq nr %u, but received %u", link_ack_nr, seq_nr);
595bd021c4eSMatthias Ringwald                     hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_ACK_PACKET;
596bd021c4eSMatthias Ringwald                     break;
597bd021c4eSMatthias Ringwald                 }
598bd021c4eSMatthias Ringwald                 // ack packet right away
599bd021c4eSMatthias Ringwald                 link_ack_nr = hci_transport_link_inc_seq_nr(link_ack_nr);
600bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_ACK_PACKET;
601bd021c4eSMatthias Ringwald             }
602bd021c4eSMatthias Ringwald 
603bd021c4eSMatthias Ringwald             // Process ACKs in reliable packet and explicit ack packets
604*a48f52afSMatthias Ringwald             if (reliable_packet || link_packet_type == LINK_ACKNOWLEDGEMENT_TYPE){
605bd021c4eSMatthias Ringwald                 // our packet is good if the remote expects our seq nr + 1
606bd021c4eSMatthias Ringwald                 int next_seq_nr = hci_transport_link_inc_seq_nr(link_seq_nr);
607*a48f52afSMatthias Ringwald                 if (hci_transport_link_have_outgoing_packet() && next_seq_nr == ack_nr){
608b304e673SMatthias Ringwald                     log_debug("outoing packet with seq %u ack'ed", link_seq_nr);
609bd021c4eSMatthias Ringwald                     link_seq_nr = next_seq_nr;
610bd021c4eSMatthias Ringwald                     hci_transport_link_clear_queue();
611bd021c4eSMatthias Ringwald 
612bd021c4eSMatthias Ringwald                     // notify upper stack that it can send again
613bd021c4eSMatthias Ringwald                     uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
614bd021c4eSMatthias Ringwald                     packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
615bd021c4eSMatthias Ringwald                 }
616bd021c4eSMatthias Ringwald             }
617bd021c4eSMatthias Ringwald 
618bd021c4eSMatthias Ringwald             switch (link_packet_type){
619bd021c4eSMatthias Ringwald                 case LINK_CONTROL_PACKET_TYPE:
620bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_config, sizeof(link_control_config)) == 0){
621bf71c899SMatthias Ringwald                         if (link_payload_len == link_control_config_prefix_len){
622bf71c899SMatthias Ringwald                             log_debug("link received config, no config field");
623bf71c899SMatthias Ringwald                             hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE_EMPTY;
624bf71c899SMatthias Ringwald                         } else {
625bf71c899SMatthias Ringwald                             log_debug("link received config, 0x%02x", slip_payload[2]);
626bd021c4eSMatthias Ringwald                             hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE;
627bf71c899SMatthias Ringwald                         }
628bd021c4eSMatthias Ringwald                         break;
629bd021c4eSMatthias Ringwald                     }
630bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){
631b304e673SMatthias Ringwald                         log_debug("link received sync in ACTIVE STATE!");
632bd021c4eSMatthias Ringwald                         // TODO sync during active indicates peer reset -> full upper layer reset necessary
633bd021c4eSMatthias Ringwald                         break;
634bd021c4eSMatthias Ringwald                     }
635bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_sleep, sizeof(link_control_sleep)) == 0){
636d8e28fa3SMatthias Ringwald                         if (btstack_uart_sleep_mode){
637d8e28fa3SMatthias Ringwald                             log_info("link: received sleep message. Enabling UART Sleep.");
638d8e28fa3SMatthias Ringwald                             btstack_uart->set_sleep(btstack_uart_sleep_mode);
63962ca45d7SMatthias Ringwald                             hci_transport_h5_emit_sleep_state(1);
640d8e28fa3SMatthias Ringwald                         } else {
641d8e28fa3SMatthias Ringwald                             log_info("link: received sleep message. UART Sleep not supported");
642d8e28fa3SMatthias Ringwald                         }
643bd021c4eSMatthias Ringwald                         link_peer_asleep = 1;
644bd021c4eSMatthias Ringwald                         break;
645bd021c4eSMatthias Ringwald                     }
646bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_wakeup, sizeof(link_control_wakeup)) == 0){
647bd021c4eSMatthias Ringwald                         log_info("link: received wakupe message -> send woken");
648bd021c4eSMatthias Ringwald                         link_peer_asleep = 0;
649bd021c4eSMatthias Ringwald                         hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WOKEN;
650bd021c4eSMatthias Ringwald                         break;
651bd021c4eSMatthias Ringwald                     }
652bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_woken, sizeof(link_control_woken)) == 0){
653bd021c4eSMatthias Ringwald                         log_info("link: received woken message");
654bd021c4eSMatthias Ringwald                         link_peer_asleep = 0;
65594764e99SMatthias Ringwald                         // queued packet will be sent in hci_transport_link_run if needed
656bd021c4eSMatthias Ringwald                         break;
657bd021c4eSMatthias Ringwald                     }
658bd021c4eSMatthias Ringwald                     break;
659bd021c4eSMatthias Ringwald                 case HCI_EVENT_PACKET:
660bd021c4eSMatthias Ringwald                 case HCI_ACL_DATA_PACKET:
661bd021c4eSMatthias Ringwald                 case HCI_SCO_DATA_PACKET:
66294764e99SMatthias Ringwald                     // seems like peer is awake
66394764e99SMatthias Ringwald                     link_peer_asleep = 0;
66494764e99SMatthias Ringwald                     // forward packet to stack
665bd021c4eSMatthias Ringwald                     packet_handler(link_packet_type, slip_payload, link_payload_len);
66694764e99SMatthias Ringwald                     // reset inactvitiy timer
66794764e99SMatthias Ringwald                     hci_transport_inactivity_timer_set();
668bd021c4eSMatthias Ringwald                     break;
669bd021c4eSMatthias Ringwald             }
670bd021c4eSMatthias Ringwald 
671bd021c4eSMatthias Ringwald             break;
672bd021c4eSMatthias Ringwald         default:
673bd021c4eSMatthias Ringwald             break;
674bd021c4eSMatthias Ringwald     }
675bd021c4eSMatthias Ringwald 
676bd021c4eSMatthias Ringwald     hci_transport_link_run();
677bd021c4eSMatthias Ringwald }
678bd021c4eSMatthias Ringwald 
679*a48f52afSMatthias Ringwald // recommended time until resend: 3 * time of largest packet
680bd021c4eSMatthias Ringwald static uint16_t hci_transport_link_calc_resend_timeout(uint32_t baudrate){
681fc6cde64SMatthias Ringwald     uint32_t max_packet_size_in_bit = (HCI_INCOMING_PACKET_BUFFER_SIZE + 6) << 3;
682*a48f52afSMatthias Ringwald     uint32_t t_max_x3_ms = max_packet_size_in_bit * 3000 / baudrate;
6835c5f3f76SMatthias Ringwald 
6845c5f3f76SMatthias Ringwald     // allow for BTstack logging and other delays
685*a48f52afSMatthias Ringwald     t_max_x3_ms += 50;
6865c5f3f76SMatthias Ringwald 
687d0756da4SMatthias Ringwald     log_info("resend timeout for %"PRIu32" baud: %u ms", baudrate, (int) t_max_x3_ms);
688bd021c4eSMatthias Ringwald     return t_max_x3_ms;
689bd021c4eSMatthias Ringwald }
690bd021c4eSMatthias Ringwald 
691bd021c4eSMatthias Ringwald static void hci_transport_link_update_resend_timeout(uint32_t baudrate){
692bd021c4eSMatthias Ringwald     link_resend_timeout_ms = hci_transport_link_calc_resend_timeout(baudrate);
693bd021c4eSMatthias Ringwald }
694bd021c4eSMatthias Ringwald 
695bd021c4eSMatthias Ringwald /// H5 Interface
696bd021c4eSMatthias Ringwald 
697*a48f52afSMatthias Ringwald static void hci_transport_h5_frame_received(uint16_t frame_size){
698bd021c4eSMatthias Ringwald     hci_transport_h5_process_frame(frame_size);
699bd021c4eSMatthias Ringwald     hci_transport_slip_init();
700bd021c4eSMatthias Ringwald }
701bd021c4eSMatthias Ringwald 
702*a48f52afSMatthias Ringwald static void hci_transport_h5_frame_sent(void){
7036ae10533SMatthias Ringwald 
704*a48f52afSMatthias Ringwald     // restore DIC and clear flag
705*a48f52afSMatthias Ringwald     if (hci_packet_restore_dic_address){
706*a48f52afSMatthias Ringwald         little_endian_store_16(hci_packet_restore_dic_address, 0, hci_packet_restore_dic_data);
707*a48f52afSMatthias Ringwald         hci_packet_restore_dic_address = NULL;
7086ae10533SMatthias Ringwald     }
7096ae10533SMatthias Ringwald 
7106ae10533SMatthias Ringwald     // done
7116ae10533SMatthias Ringwald     slip_write_active = 0;
71294764e99SMatthias Ringwald 
713*a48f52afSMatthias Ringwald     // baudrate change pending?
714*a48f52afSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SET_BAUDRATE){
715*a48f52afSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SET_BAUDRATE;
716*a48f52afSMatthias Ringwald         btstack_uart->set_baudrate(link_new_baudrate);
717*a48f52afSMatthias Ringwald         hci_transport_link_update_resend_timeout(link_new_baudrate);
718*a48f52afSMatthias Ringwald     }
719*a48f52afSMatthias Ringwald 
72094764e99SMatthias Ringwald     // enter sleep mode after sending sleep message
72194764e99SMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_ENTER_SLEEP){
72294764e99SMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_ENTER_SLEEP;
72394764e99SMatthias Ringwald         if (btstack_uart_sleep_mode){
72494764e99SMatthias Ringwald             log_info("link: sent sleep message. Enabling UART Sleep.");
72594764e99SMatthias Ringwald             btstack_uart->set_sleep(btstack_uart_sleep_mode);
72694764e99SMatthias Ringwald         } else {
72794764e99SMatthias Ringwald             log_info("link: sent sleep message. UART Sleep not supported");
72894764e99SMatthias Ringwald         }
72962ca45d7SMatthias Ringwald         hci_transport_h5_emit_sleep_state(1);
73094764e99SMatthias Ringwald     }
73194764e99SMatthias Ringwald 
732*a48f52afSMatthias Ringwald     // SCO packets are sent as unreliable, so we're done now
733*a48f52afSMatthias Ringwald     if (hci_packet_type == HCI_SCO_DATA_PACKET){
734*a48f52afSMatthias Ringwald         hci_transport_link_clear_queue();
735*a48f52afSMatthias Ringwald         // notify upper stack that it can send again
736*a48f52afSMatthias Ringwald         uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
737*a48f52afSMatthias Ringwald         packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
738*a48f52afSMatthias Ringwald     }
739*a48f52afSMatthias Ringwald 
740bd021c4eSMatthias Ringwald     hci_transport_link_run();
741bd021c4eSMatthias Ringwald }
742bd021c4eSMatthias Ringwald 
743bd021c4eSMatthias Ringwald static void hci_transport_h5_init(const void * transport_config){
744bd021c4eSMatthias Ringwald     // check for hci_transport_config_uart_t
745bd021c4eSMatthias Ringwald     if (!transport_config) {
746bd021c4eSMatthias Ringwald         log_error("hci_transport_h5: no config!");
747bd021c4eSMatthias Ringwald         return;
748bd021c4eSMatthias Ringwald     }
749bd021c4eSMatthias Ringwald     if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) {
750bd021c4eSMatthias Ringwald         log_error("hci_transport_h5: config not of type != HCI_TRANSPORT_CONFIG_UART!");
751bd021c4eSMatthias Ringwald         return;
752bd021c4eSMatthias Ringwald     }
753bd021c4eSMatthias Ringwald 
754bd021c4eSMatthias Ringwald     // extract UART config from transport config
755bd021c4eSMatthias Ringwald     hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config;
756bd021c4eSMatthias Ringwald     uart_config.baudrate    = hci_transport_config_uart->baudrate_init;
757bd021c4eSMatthias Ringwald     uart_config.flowcontrol = hci_transport_config_uart->flowcontrol;
758bd021c4eSMatthias Ringwald     uart_config.device_name = hci_transport_config_uart->device_name;
759bd021c4eSMatthias Ringwald 
760bd021c4eSMatthias Ringwald     // setup UART driver
761bd021c4eSMatthias Ringwald     btstack_uart->init(&uart_config);
762*a48f52afSMatthias Ringwald     btstack_uart->set_frame_received(&hci_transport_h5_frame_received);
763*a48f52afSMatthias Ringwald     btstack_uart->set_frame_sent(&hci_transport_h5_frame_sent);
764bd021c4eSMatthias Ringwald }
765bd021c4eSMatthias Ringwald 
766bd021c4eSMatthias Ringwald static int hci_transport_h5_open(void){
767bd021c4eSMatthias Ringwald     int res = btstack_uart->open();
768bd021c4eSMatthias Ringwald     if (res){
769bd021c4eSMatthias Ringwald         return res;
770bd021c4eSMatthias Ringwald     }
771bd021c4eSMatthias Ringwald 
772daa2e90cSMatthias Ringwald     //
773daa2e90cSMatthias Ringwald     if (hci_transport_bcsp_mode){
774b304e673SMatthias Ringwald         log_info("enable even parity for BCSP mode");
775*a48f52afSMatthias Ringwald         btstack_uart->set_parity(BTSTACK_UART_PARITY_EVEN);
776daa2e90cSMatthias Ringwald     }
777daa2e90cSMatthias Ringwald 
778d8e28fa3SMatthias Ringwald     // check if wake on RX can be used
779d8e28fa3SMatthias Ringwald     btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF;
780d8e28fa3SMatthias Ringwald     int supported_sleep_modes = 0;
781d8e28fa3SMatthias Ringwald     if (btstack_uart->get_supported_sleep_modes){
782d8e28fa3SMatthias Ringwald         supported_sleep_modes = btstack_uart->get_supported_sleep_modes();
783d8e28fa3SMatthias Ringwald     }
784d8e28fa3SMatthias Ringwald     if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){
785b304e673SMatthias Ringwald         log_info("using wake on RX");
786d8e28fa3SMatthias Ringwald         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE;
787d8e28fa3SMatthias Ringwald     } else {
788b304e673SMatthias Ringwald         log_info("UART driver does not provide compatible sleep mode");
789d8e28fa3SMatthias Ringwald     }
790d8e28fa3SMatthias Ringwald 
791bd021c4eSMatthias Ringwald     // setup resend timeout
792bd021c4eSMatthias Ringwald     hci_transport_link_update_resend_timeout(uart_config.baudrate);
793bd021c4eSMatthias Ringwald 
794bd021c4eSMatthias Ringwald     // init link management - already starts syncing
795bd021c4eSMatthias Ringwald     hci_transport_link_init();
796bd021c4eSMatthias Ringwald 
797bd021c4eSMatthias Ringwald     // start receiving
798*a48f52afSMatthias Ringwald     hci_transport_slip_init();
799bd021c4eSMatthias Ringwald 
800bd021c4eSMatthias Ringwald     return 0;
801bd021c4eSMatthias Ringwald }
802bd021c4eSMatthias Ringwald 
803bd021c4eSMatthias Ringwald static int hci_transport_h5_close(void){
804bd021c4eSMatthias Ringwald     return btstack_uart->close();
805bd021c4eSMatthias Ringwald }
806bd021c4eSMatthias Ringwald 
807bd021c4eSMatthias Ringwald static void hci_transport_h5_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
808bd021c4eSMatthias Ringwald     packet_handler = handler;
809bd021c4eSMatthias Ringwald }
810bd021c4eSMatthias Ringwald 
811bd021c4eSMatthias Ringwald static int hci_transport_h5_can_send_packet_now(uint8_t packet_type){
812*a48f52afSMatthias Ringwald     int res = !hci_transport_link_have_outgoing_packet() && link_state == LINK_ACTIVE;
813b304e673SMatthias Ringwald     // log_info("can_send_packet_now: %u", res);
81494764e99SMatthias Ringwald     return res;
815bd021c4eSMatthias Ringwald }
816bd021c4eSMatthias Ringwald 
817bd021c4eSMatthias Ringwald static int hci_transport_h5_send_packet(uint8_t packet_type, uint8_t *packet, int size){
818bd021c4eSMatthias Ringwald     if (!hci_transport_h5_can_send_packet_now(packet_type)){
819f04a0c31SMatthias Ringwald         log_error("hci_transport_h5_send_packet called but in state %d", link_state);
820bd021c4eSMatthias Ringwald         return -1;
821bd021c4eSMatthias Ringwald     }
822bd021c4eSMatthias Ringwald 
823bd021c4eSMatthias Ringwald     // store request
824bd021c4eSMatthias Ringwald     hci_transport_h5_queue_packet(packet_type, packet, size);
825bd021c4eSMatthias Ringwald 
826bd021c4eSMatthias Ringwald     // send wakeup first
827bd021c4eSMatthias Ringwald     if (link_peer_asleep){
82862ca45d7SMatthias Ringwald         hci_transport_h5_emit_sleep_state(0);
829d8e28fa3SMatthias Ringwald         if (btstack_uart_sleep_mode){
830b304e673SMatthias Ringwald             log_info("disable UART sleep");
831d8e28fa3SMatthias Ringwald             btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
832d8e28fa3SMatthias Ringwald         }
833bd021c4eSMatthias Ringwald         hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WAKEUP;
834bd021c4eSMatthias Ringwald         hci_transport_link_set_timer(LINK_WAKEUP_MS);
835bd021c4eSMatthias Ringwald     } else {
836bd021c4eSMatthias Ringwald         hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET;
837bd021c4eSMatthias Ringwald         hci_transport_link_set_timer(link_resend_timeout_ms);
838bd021c4eSMatthias Ringwald     }
839bd021c4eSMatthias Ringwald     hci_transport_link_run();
840bd021c4eSMatthias Ringwald     return 0;
841bd021c4eSMatthias Ringwald }
842bd021c4eSMatthias Ringwald 
843bd021c4eSMatthias Ringwald static int hci_transport_h5_set_baudrate(uint32_t baudrate){
844bd021c4eSMatthias Ringwald 
845*a48f52afSMatthias Ringwald     log_info("set_baudrate %"PRIu32", h5 actions %x", baudrate, hci_transport_link_actions);
846*a48f52afSMatthias Ringwald     // Baudrate is changed after an HCI Baudrate Change Command, which usually causes an HCI Event Commmand Complete
847*a48f52afSMatthias Ringwald     // Before changing the baudrate, the HCI Command Complete needs to get acknowledged
848*a48f52afSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_ACK_PACKET){
849*a48f52afSMatthias Ringwald         hci_transport_link_actions |= HCI_TRANSPORT_LINK_SET_BAUDRATE;
850*a48f52afSMatthias Ringwald         link_new_baudrate = baudrate;
851*a48f52afSMatthias Ringwald         hci_transport_link_run();
852*a48f52afSMatthias Ringwald         return 0;
853*a48f52afSMatthias Ringwald     }
854*a48f52afSMatthias Ringwald 
855bd021c4eSMatthias Ringwald     int res = btstack_uart->set_baudrate(baudrate);
856bd021c4eSMatthias Ringwald 
857bd021c4eSMatthias Ringwald     if (res) return res;
858bd021c4eSMatthias Ringwald     hci_transport_link_update_resend_timeout(baudrate);
859bd021c4eSMatthias Ringwald     return 0;
860bd021c4eSMatthias Ringwald }
861bd021c4eSMatthias Ringwald 
862bd021c4eSMatthias Ringwald static void hci_transport_h5_reset_link(void){
863bd021c4eSMatthias Ringwald 
864b304e673SMatthias Ringwald     log_info("reset_link");
865bd021c4eSMatthias Ringwald 
866bd021c4eSMatthias Ringwald     // clear outgoing queue
867bd021c4eSMatthias Ringwald     hci_transport_link_clear_queue();
868bd021c4eSMatthias Ringwald 
869bd021c4eSMatthias Ringwald     // init slip parser state machine
870bd021c4eSMatthias Ringwald     hci_transport_slip_init();
871bd021c4eSMatthias Ringwald 
872bd021c4eSMatthias Ringwald     // init link management - already starts syncing
873bd021c4eSMatthias Ringwald     hci_transport_link_init();
874bd021c4eSMatthias Ringwald }
875bd021c4eSMatthias Ringwald 
876bd021c4eSMatthias Ringwald static const hci_transport_t hci_transport_h5 = {
877bd021c4eSMatthias Ringwald     /* const char * name; */                                        "H5",
878bd021c4eSMatthias Ringwald     /* void   (*init) (const void *transport_config); */            &hci_transport_h5_init,
879bd021c4eSMatthias Ringwald     /* int    (*open)(void); */                                     &hci_transport_h5_open,
880bd021c4eSMatthias Ringwald     /* int    (*close)(void); */                                    &hci_transport_h5_close,
881bd021c4eSMatthias Ringwald     /* void   (*register_packet_handler)(void (*handler)(...); */   &hci_transport_h5_register_packet_handler,
882bd021c4eSMatthias Ringwald     /* int    (*can_send_packet_now)(uint8_t packet_type); */       &hci_transport_h5_can_send_packet_now,
883bd021c4eSMatthias Ringwald     /* int    (*send_packet)(...); */                               &hci_transport_h5_send_packet,
884bd021c4eSMatthias Ringwald     /* int    (*set_baudrate)(uint32_t baudrate); */                &hci_transport_h5_set_baudrate,
885bd021c4eSMatthias Ringwald     /* void   (*reset_link)(void); */                               &hci_transport_h5_reset_link,
886ee752bb8SMatthias Ringwald     /* void   (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL,
887bd021c4eSMatthias Ringwald };
888bd021c4eSMatthias Ringwald 
889*a48f52afSMatthias Ringwald // configure and return h5 singleton
890*a48f52afSMatthias Ringwald const hci_transport_t * hci_transport_h5_instance(const btstack_uart_t * uart_driver) {
891bd021c4eSMatthias Ringwald     btstack_uart = uart_driver;
892bd021c4eSMatthias Ringwald     return &hci_transport_h5;
893bd021c4eSMatthias Ringwald }
89494764e99SMatthias Ringwald 
89594764e99SMatthias Ringwald void hci_transport_h5_set_auto_sleep(uint16_t inactivity_timeout_ms){
89694764e99SMatthias Ringwald     link_inactivity_timeout_ms = inactivity_timeout_ms;
89794764e99SMatthias Ringwald }
898daa2e90cSMatthias Ringwald 
899daa2e90cSMatthias Ringwald void hci_transport_h5_enable_bcsp_mode(void){
900daa2e90cSMatthias Ringwald     hci_transport_bcsp_mode = 1;
901daa2e90cSMatthias Ringwald }
902