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