xref: /btstack/src/hci_transport_h5.c (revision 62ca45d7552b475c79bca8e4e34577043093f0d6)
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 
38bd021c4eSMatthias Ringwald /*
39bd021c4eSMatthias Ringwald  *  hci_transport_h5.c
40bd021c4eSMatthias Ringwald  *
41bd021c4eSMatthias Ringwald  *  HCI Transport API implementation for basic H5 protocol
42bd021c4eSMatthias Ringwald  *
43bd021c4eSMatthias Ringwald  *  Created by Matthias Ringwald on 4/29/09.
44bd021c4eSMatthias Ringwald  */
45bd021c4eSMatthias Ringwald 
46bd021c4eSMatthias Ringwald #include "hci.h"
47bd021c4eSMatthias Ringwald #include "btstack_slip.h"
48bd021c4eSMatthias Ringwald #include "btstack_debug.h"
49bd021c4eSMatthias Ringwald #include "hci_transport.h"
50bd021c4eSMatthias Ringwald #include "btstack_uart_block.h"
51bd021c4eSMatthias Ringwald 
52e9c151bcSMatthias Ringwald // assert post-buffer for 16-bit data integrity check is available
53e9c151bcSMatthias Ringwald #if !defined(HCI_OUTGOING_POST_BUFFER_SIZE) || (HCI_OUTGOING_POST_BUFFER_SIZE < 2)
54e9c151bcSMatthias Ringwald #error HCI_OUTGOING_POST_BUFFER_SIZE not defined. Please update hci.h
55e9c151bcSMatthias Ringwald #endif
56e9c151bcSMatthias Ringwald 
57bd021c4eSMatthias Ringwald typedef enum {
58bd021c4eSMatthias Ringwald     LINK_UNINITIALIZED,
59bd021c4eSMatthias Ringwald     LINK_INITIALIZED,
60bd021c4eSMatthias Ringwald     LINK_ACTIVE
61bd021c4eSMatthias Ringwald } hci_transport_link_state_t;
62bd021c4eSMatthias Ringwald 
63bd021c4eSMatthias Ringwald typedef enum {
64bd021c4eSMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_SYNC            = 1 << 0,
65bd021c4eSMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE   = 1 << 1,
66bd021c4eSMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_CONFIG          = 1 << 2,
67bd021c4eSMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE = 1 << 3,
6894764e99SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_SLEEP           = 1 << 4,
6994764e99SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_WOKEN           = 1 << 5,
7094764e99SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_WAKEUP          = 1 << 6,
7194764e99SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET   = 1 << 7,
7294764e99SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_ACK_PACKET      = 1 << 8,
7394764e99SMatthias Ringwald     HCI_TRANSPORT_LINK_ENTER_SLEEP          = 1 << 9,
7494764e99SMatthias Ringwald 
75bd021c4eSMatthias Ringwald } hci_transport_link_actions_t;
76bd021c4eSMatthias Ringwald 
7779b61987SMatthias Ringwald // Configuration Field. No packet buffers -> sliding window = 1, no OOF flow control, support data integrity check
78bd021c4eSMatthias Ringwald #define LINK_CONFIG_SLIDING_WINDOW_SIZE 1
79bd021c4eSMatthias Ringwald #define LINK_CONFIG_OOF_FLOW_CONTROL 0
8079b61987SMatthias Ringwald #define LINK_CONFIG_DATA_INTEGRITY_CHECK 1
81bd021c4eSMatthias Ringwald #define LINK_CONFIG_VERSION_NR 0
82bd021c4eSMatthias Ringwald #define LINK_CONFIG_FIELD (LINK_CONFIG_SLIDING_WINDOW_SIZE | (LINK_CONFIG_OOF_FLOW_CONTROL << 3) | (LINK_CONFIG_DATA_INTEGRITY_CHECK << 4) | (LINK_CONFIG_VERSION_NR << 5))
83bd021c4eSMatthias Ringwald 
84bd021c4eSMatthias Ringwald // periodic sending during link establishment
85bd021c4eSMatthias Ringwald #define LINK_PERIOD_MS 250
86bd021c4eSMatthias Ringwald 
87bd021c4eSMatthias Ringwald // resend wakeup
88bd021c4eSMatthias Ringwald #define LINK_WAKEUP_MS 50
89bd021c4eSMatthias Ringwald 
90bd021c4eSMatthias Ringwald // additional packet types
91bd021c4eSMatthias Ringwald #define LINK_ACKNOWLEDGEMENT_TYPE 0x00
92bd021c4eSMatthias Ringwald #define LINK_CONTROL_PACKET_TYPE 0x0f
93bd021c4eSMatthias Ringwald 
946ae10533SMatthias Ringwald // max size of write requests
956ae10533SMatthias Ringwald #define LINK_SLIP_TX_CHUNK_LEN 64
966ae10533SMatthias Ringwald 
97bd021c4eSMatthias Ringwald // ---
98bd021c4eSMatthias Ringwald static const uint8_t link_control_sync[] =   { 0x01, 0x7e};
99bd021c4eSMatthias Ringwald static const uint8_t link_control_sync_response[] = { 0x02, 0x7d};
100bd021c4eSMatthias Ringwald static const uint8_t link_control_config[] = { 0x03, 0xfc, LINK_CONFIG_FIELD};
101cbc04f48SMatthias Ringwald static const uint8_t link_control_config_prefix_len  = 2;
102bd021c4eSMatthias Ringwald static const uint8_t link_control_config_response[] = { 0x04, 0x7b, LINK_CONFIG_FIELD};
103bd021c4eSMatthias Ringwald static const uint8_t link_control_config_response_prefix_len  = 2;
104bd021c4eSMatthias Ringwald static const uint8_t link_control_wakeup[] = { 0x05, 0xfa};
105bd021c4eSMatthias Ringwald static const uint8_t link_control_woken[] =  { 0x06, 0xf9};
106bd021c4eSMatthias Ringwald static const uint8_t link_control_sleep[] =  { 0x07, 0x78};
107bd021c4eSMatthias Ringwald 
108c6df6d84SMatthias Ringwald // max size of link control messages
109c6df6d84SMatthias Ringwald #define LINK_CONTROL_MAX_LEN 3
110c6df6d84SMatthias Ringwald 
111bd021c4eSMatthias Ringwald // incoming pre-bufffer + 4 bytes H5 header + max(acl header + acl payload, event header + event data) + 2 bytes opt CRC
112bd021c4eSMatthias Ringwald static uint8_t   hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 6 + HCI_PACKET_BUFFER_SIZE];
113bd021c4eSMatthias Ringwald 
1146ae10533SMatthias Ringwald // outgoing slip encoded buffer. +1 to assert that last SOF fits in buffer
1156ae10533SMatthias Ringwald static uint8_t slip_outgoing_buffer[LINK_SLIP_TX_CHUNK_LEN+1];
1166ae10533SMatthias Ringwald static int     slip_write_active;
117bd021c4eSMatthias Ringwald 
118bd021c4eSMatthias Ringwald // H5 Link State
119bd021c4eSMatthias Ringwald static hci_transport_link_state_t link_state;
120bd021c4eSMatthias Ringwald static btstack_timer_source_t link_timer;
121bd021c4eSMatthias Ringwald static uint8_t  link_seq_nr;
122bd021c4eSMatthias Ringwald static uint8_t  link_ack_nr;
123bd021c4eSMatthias Ringwald static uint16_t link_resend_timeout_ms;
124bd021c4eSMatthias Ringwald static uint8_t  link_peer_asleep;
1257c2aa31eSMatthias Ringwald static uint8_t  link_peer_supports_data_integrity_check;
126bd021c4eSMatthias Ringwald 
12794764e99SMatthias Ringwald // auto sleep-mode
12894764e99SMatthias Ringwald static btstack_timer_source_t inactivity_timer;
12994764e99SMatthias Ringwald static uint16_t link_inactivity_timeout_ms; // auto-sleep if set
13094764e99SMatthias Ringwald 
131bd021c4eSMatthias Ringwald // Outgoing packet
132bd021c4eSMatthias Ringwald static uint8_t   hci_packet_type;
133bd021c4eSMatthias Ringwald static uint16_t  hci_packet_size;
134bd021c4eSMatthias Ringwald static uint8_t * hci_packet;
135bd021c4eSMatthias Ringwald 
136bd021c4eSMatthias Ringwald // hci packet handler
137bd021c4eSMatthias Ringwald static  void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size);
138bd021c4eSMatthias Ringwald 
139bd021c4eSMatthias Ringwald static int hci_transport_link_actions;
140bd021c4eSMatthias Ringwald 
141bd021c4eSMatthias Ringwald // UART Driver + Config
142bd021c4eSMatthias Ringwald static const btstack_uart_block_t * btstack_uart;
143bd021c4eSMatthias Ringwald static btstack_uart_config_t uart_config;
144d8e28fa3SMatthias Ringwald static btstack_uart_sleep_mode_t btstack_uart_sleep_mode;
145bd021c4eSMatthias Ringwald 
146bd021c4eSMatthias Ringwald // Prototypes
147bd021c4eSMatthias Ringwald static void hci_transport_h5_process_frame(uint16_t frame_size);
148bd021c4eSMatthias Ringwald static int  hci_transport_link_have_outgoing_packet(void);
149bd021c4eSMatthias Ringwald static void hci_transport_link_send_queued_packet(void);
150bd021c4eSMatthias Ringwald static void hci_transport_link_set_timer(uint16_t timeout_ms);
151bd021c4eSMatthias Ringwald static void hci_transport_link_timeout_handler(btstack_timer_source_t * timer);
152bd021c4eSMatthias Ringwald static void hci_transport_link_run(void);
153bd021c4eSMatthias Ringwald static void hci_transport_slip_init(void);
154bd021c4eSMatthias Ringwald 
1557c2aa31eSMatthias Ringwald // -----------------------------
15679b61987SMatthias Ringwald // CRC16-CCITT Calculation - compromise: use 32 byte table - 512 byte table would be faster, but that's too large
15779b61987SMatthias Ringwald 
15879b61987SMatthias Ringwald static const uint16_t crc16_ccitt_table[] ={
15979b61987SMatthias Ringwald     0x0000, 0x1081, 0x2102, 0x3183,
16079b61987SMatthias Ringwald     0x4204, 0x5285, 0x6306, 0x7387,
16179b61987SMatthias Ringwald     0x8408, 0x9489, 0xa50a, 0xb58b,
16279b61987SMatthias Ringwald     0xc60c, 0xd68d, 0xe70e, 0xf78f
16379b61987SMatthias Ringwald };
16479b61987SMatthias Ringwald 
16579b61987SMatthias Ringwald static uint16_t crc16_ccitt_update (uint16_t crc, uint8_t ch){
16679b61987SMatthias Ringwald     crc = (crc >> 4) ^ crc16_ccitt_table[(crc ^ ch) & 0x000f];
16779b61987SMatthias Ringwald     crc = (crc >> 4) ^ crc16_ccitt_table[(crc ^ (ch >> 4)) & 0x000f];
16879b61987SMatthias Ringwald     return crc;
16979b61987SMatthias Ringwald }
17079b61987SMatthias Ringwald 
17179b61987SMatthias Ringwald static uint16_t btstack_reverse_bits_16(uint16_t value){
17279b61987SMatthias Ringwald     int reverse = 0;
17379b61987SMatthias Ringwald     int i;
17479b61987SMatthias Ringwald     for (i = 0; i < 16; i++) {
17579b61987SMatthias Ringwald         reverse = reverse << 1;
17679b61987SMatthias Ringwald         reverse |= value & 1;
17779b61987SMatthias Ringwald         value = value >> 1;
17879b61987SMatthias Ringwald     }
17979b61987SMatthias Ringwald     return reverse;
18079b61987SMatthias Ringwald }
18179b61987SMatthias Ringwald 
182c6df6d84SMatthias Ringwald static uint16_t crc16_calc_for_slip_frame(const uint8_t * header, const uint8_t * payload, uint16_t len){
18379b61987SMatthias Ringwald     int i;
18479b61987SMatthias Ringwald     uint16_t crc = 0xffff;
18579b61987SMatthias Ringwald     for (i=0 ; i < 4 ; i++){
18679b61987SMatthias Ringwald         crc = crc16_ccitt_update(crc, header[i]);
18779b61987SMatthias Ringwald     }
18879b61987SMatthias Ringwald     for (i=0 ; i < len ; i++){
18979b61987SMatthias Ringwald         crc = crc16_ccitt_update(crc, payload[i]);
19079b61987SMatthias Ringwald     }
19179b61987SMatthias Ringwald     return btstack_reverse_bits_16(crc);
19279b61987SMatthias Ringwald }
19379b61987SMatthias Ringwald 
194bd021c4eSMatthias Ringwald // -----------------------------
19594764e99SMatthias Ringwald static void hci_transport_inactivity_timeout_handler(btstack_timer_source_t * ts){
196f04a0c31SMatthias Ringwald     log_info("h5: inactivity timeout. link state %d, peer asleep %u, actions 0x%02x, outgoing packet %u",
19794764e99SMatthias Ringwald         link_state, link_peer_asleep, hci_transport_link_actions, hci_transport_link_have_outgoing_packet());
19894764e99SMatthias Ringwald     if (hci_transport_link_have_outgoing_packet()) return;
19994764e99SMatthias Ringwald     if (link_state != LINK_ACTIVE) return;
20094764e99SMatthias Ringwald     if (hci_transport_link_actions) return;
20194764e99SMatthias Ringwald     if (link_peer_asleep) return;
20294764e99SMatthias Ringwald     hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SLEEP;
20394764e99SMatthias Ringwald     hci_transport_link_run();
20494764e99SMatthias Ringwald }
205bd021c4eSMatthias Ringwald 
20694764e99SMatthias Ringwald static void hci_transport_inactivity_timer_set(void){
20794764e99SMatthias Ringwald     if (!link_inactivity_timeout_ms) return;
20894764e99SMatthias Ringwald     btstack_run_loop_set_timer_handler(&inactivity_timer, &hci_transport_inactivity_timeout_handler);
20994764e99SMatthias Ringwald     btstack_run_loop_set_timer(&inactivity_timer, link_inactivity_timeout_ms);
21094764e99SMatthias Ringwald     btstack_run_loop_remove_timer(&inactivity_timer);
21194764e99SMatthias Ringwald     btstack_run_loop_add_timer(&inactivity_timer);
21294764e99SMatthias Ringwald }
213bd021c4eSMatthias Ringwald 
21494764e99SMatthias Ringwald // -----------------------------
215bd021c4eSMatthias Ringwald // SLIP Outgoing
216bd021c4eSMatthias Ringwald 
2176ae10533SMatthias Ringwald // Fill chunk and write
2186ae10533SMatthias Ringwald static void hci_transport_slip_encode_chunk_and_send(int pos){
2196ae10533SMatthias Ringwald     while (btstack_slip_encoder_has_data() & (pos < LINK_SLIP_TX_CHUNK_LEN)) {
2206ae10533SMatthias Ringwald         slip_outgoing_buffer[pos++] = btstack_slip_encoder_get_byte();
2216ae10533SMatthias Ringwald     }
2226ae10533SMatthias Ringwald     if (!btstack_slip_encoder_has_data()){
2236ae10533SMatthias Ringwald         // Start of Frame
2246ae10533SMatthias Ringwald         slip_outgoing_buffer[pos++] = BTSTACK_SLIP_SOF;
2256ae10533SMatthias Ringwald     }
2266ae10533SMatthias Ringwald     slip_write_active = 1;
227f04a0c31SMatthias Ringwald     log_info("hci_transport_slip: send %d bytes", pos);
2286ae10533SMatthias Ringwald     btstack_uart->send_block(slip_outgoing_buffer, pos);
2296ae10533SMatthias Ringwald }
2306ae10533SMatthias Ringwald 
2316ae10533SMatthias Ringwald static inline void hci_transport_slip_send_next_chunk(void){
2326ae10533SMatthias Ringwald     hci_transport_slip_encode_chunk_and_send(0);
2336ae10533SMatthias Ringwald }
2346ae10533SMatthias Ringwald 
2357c2aa31eSMatthias Ringwald // format: 0xc0 HEADER PACKET 0xc0
236bd021c4eSMatthias Ringwald // @param uint8_t header[4]
237bd021c4eSMatthias Ringwald static void hci_transport_slip_send_frame(const uint8_t * header, const uint8_t * packet, uint16_t packet_size){
238bd021c4eSMatthias Ringwald 
239bd021c4eSMatthias Ringwald     int pos = 0;
240bd021c4eSMatthias Ringwald 
241bd021c4eSMatthias Ringwald     // Start of Frame
242bd021c4eSMatthias Ringwald     slip_outgoing_buffer[pos++] = BTSTACK_SLIP_SOF;
243bd021c4eSMatthias Ringwald 
244bd021c4eSMatthias Ringwald     // Header
245bd021c4eSMatthias Ringwald     btstack_slip_encoder_start(header, 4);
246bd021c4eSMatthias Ringwald     while (btstack_slip_encoder_has_data()){
247bd021c4eSMatthias Ringwald         slip_outgoing_buffer[pos++] = btstack_slip_encoder_get_byte();
248bd021c4eSMatthias Ringwald     }
249bd021c4eSMatthias Ringwald 
250bd021c4eSMatthias Ringwald     // Packet
251bd021c4eSMatthias Ringwald     btstack_slip_encoder_start(packet, packet_size);
252bd021c4eSMatthias Ringwald 
2536ae10533SMatthias Ringwald     // Fill rest of chunk from packet and send
2546ae10533SMatthias Ringwald     hci_transport_slip_encode_chunk_and_send(pos);
255bd021c4eSMatthias Ringwald }
256bd021c4eSMatthias Ringwald 
257bd021c4eSMatthias Ringwald // SLIP Incoming
258bd021c4eSMatthias Ringwald 
259bd021c4eSMatthias Ringwald static void hci_transport_slip_init(void){
260bd021c4eSMatthias Ringwald     btstack_slip_decoder_init(&hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], 6 + HCI_PACKET_BUFFER_SIZE);
261bd021c4eSMatthias Ringwald }
262bd021c4eSMatthias Ringwald 
263bd021c4eSMatthias Ringwald // H5 Three-Wire Implementation
264bd021c4eSMatthias Ringwald 
265bd021c4eSMatthias Ringwald static void hci_transport_link_calc_header(uint8_t * header,
266bd021c4eSMatthias Ringwald     uint8_t  sequence_nr,
267bd021c4eSMatthias Ringwald     uint8_t  acknowledgement_nr,
268bd021c4eSMatthias Ringwald     uint8_t  data_integrity_check_present,
269bd021c4eSMatthias Ringwald     uint8_t  reliable_packet,
270bd021c4eSMatthias Ringwald     uint8_t  packet_type,
271bd021c4eSMatthias Ringwald     uint16_t payload_length){
272bd021c4eSMatthias Ringwald 
273bd021c4eSMatthias Ringwald     header[0] = sequence_nr | (acknowledgement_nr << 3) | (data_integrity_check_present << 6) | (reliable_packet << 7);
274bd021c4eSMatthias Ringwald     header[1] = packet_type | ((payload_length & 0x0f) << 4);
275bd021c4eSMatthias Ringwald     header[2] = payload_length >> 4;
276bd021c4eSMatthias Ringwald     header[3] = 0xff - (header[0] + header[1] + header[2]);
277bd021c4eSMatthias Ringwald }
278bd021c4eSMatthias Ringwald 
279bd021c4eSMatthias Ringwald static void hci_transport_link_send_control(const uint8_t * message, int message_len){
280bd021c4eSMatthias Ringwald     uint8_t header[4];
281c6df6d84SMatthias Ringwald     hci_transport_link_calc_header(header, 0, 0, link_peer_supports_data_integrity_check, 0, LINK_CONTROL_PACKET_TYPE, message_len);
282c6df6d84SMatthias Ringwald     if (link_peer_supports_data_integrity_check){
283c6df6d84SMatthias Ringwald         // append data integrity check value / need to copy control message
284c6df6d84SMatthias Ringwald         uint8_t message_with_dic[LINK_CONTROL_MAX_LEN + 2];
285c6df6d84SMatthias Ringwald         memcpy(message_with_dic, message, message_len);
286c6df6d84SMatthias Ringwald         uint16_t dic = crc16_calc_for_slip_frame(header, message, message_len);
287c6df6d84SMatthias Ringwald         big_endian_store_16(message_with_dic, message_len, dic);
288c6df6d84SMatthias Ringwald         message_len += 2;
289c6df6d84SMatthias Ringwald         message = message_with_dic;
290c6df6d84SMatthias Ringwald     }
291c6df6d84SMatthias Ringwald     log_info("hci_transport_link_send_control: size %u, append dic %u", message_len, link_peer_supports_data_integrity_check);
292c6df6d84SMatthias Ringwald     log_info_hexdump(message, message_len);
293bd021c4eSMatthias Ringwald     hci_transport_slip_send_frame(header, message, message_len);
294bd021c4eSMatthias Ringwald }
295bd021c4eSMatthias Ringwald 
296bd021c4eSMatthias Ringwald static void hci_transport_link_send_sync(void){
297bd021c4eSMatthias Ringwald     log_info("link: send sync");
298bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_sync, sizeof(link_control_sync));
299bd021c4eSMatthias Ringwald }
300bd021c4eSMatthias Ringwald 
301bd021c4eSMatthias Ringwald static void hci_transport_link_send_sync_response(void){
302bd021c4eSMatthias Ringwald     log_info("link: send sync response");
303bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_sync_response, sizeof(link_control_sync_response));
304bd021c4eSMatthias Ringwald }
305bd021c4eSMatthias Ringwald 
306bd021c4eSMatthias Ringwald static void hci_transport_link_send_config(void){
307bd021c4eSMatthias Ringwald     log_info("link: send config");
308bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_config, sizeof(link_control_config));
309bd021c4eSMatthias Ringwald }
310bd021c4eSMatthias Ringwald 
311bd021c4eSMatthias Ringwald static void hci_transport_link_send_config_response(void){
312bd021c4eSMatthias Ringwald     log_info("link: send config response");
313bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_config_response, sizeof(link_control_config_response));
314bd021c4eSMatthias Ringwald }
315bd021c4eSMatthias Ringwald 
316bd021c4eSMatthias Ringwald static void hci_transport_link_send_woken(void){
317bd021c4eSMatthias Ringwald     log_info("link: send woken");
318bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_woken, sizeof(link_control_woken));
319bd021c4eSMatthias Ringwald }
320bd021c4eSMatthias Ringwald 
321bd021c4eSMatthias Ringwald static void hci_transport_link_send_wakeup(void){
322bd021c4eSMatthias Ringwald     log_info("link: send wakeup");
323bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_wakeup, sizeof(link_control_wakeup));
324bd021c4eSMatthias Ringwald }
325bd021c4eSMatthias Ringwald 
32694764e99SMatthias Ringwald static void hci_transport_link_send_sleep(void){
32794764e99SMatthias Ringwald     log_info("link: send sleep");
32894764e99SMatthias Ringwald     hci_transport_link_send_control(link_control_sleep, sizeof(link_control_sleep));
32994764e99SMatthias Ringwald }
33094764e99SMatthias Ringwald 
331bd021c4eSMatthias Ringwald static void hci_transport_link_send_queued_packet(void){
332bd021c4eSMatthias Ringwald 
333bd021c4eSMatthias Ringwald     uint8_t header[4];
334c6df6d84SMatthias Ringwald     hci_transport_link_calc_header(header, link_seq_nr, link_ack_nr, link_peer_supports_data_integrity_check, 1, hci_packet_type, hci_packet_size);
335c6df6d84SMatthias Ringwald 
336c6df6d84SMatthias Ringwald     uint16_t dic = 0;
337c6df6d84SMatthias Ringwald     if (link_peer_supports_data_integrity_check){
338c6df6d84SMatthias Ringwald         // append data integrity check value
339c6df6d84SMatthias Ringwald         dic = crc16_calc_for_slip_frame(header, hci_packet, hci_packet_size);
340c6df6d84SMatthias Ringwald         big_endian_store_16(hci_packet, hci_packet_size, dic);
341c6df6d84SMatthias Ringwald         hci_packet_size += 2;
342c6df6d84SMatthias Ringwald     }
343c6df6d84SMatthias Ringwald     log_info("hci_transport_link_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);
344c6df6d84SMatthias Ringwald     log_info_hexdump(hci_packet, hci_packet_size);
345c6df6d84SMatthias Ringwald 
346bd021c4eSMatthias Ringwald     hci_transport_slip_send_frame(header, hci_packet, hci_packet_size);
34794764e99SMatthias Ringwald 
34894764e99SMatthias Ringwald     // reset inactvitiy timer
34994764e99SMatthias Ringwald     hci_transport_inactivity_timer_set();
350bd021c4eSMatthias Ringwald }
351bd021c4eSMatthias Ringwald 
352bd021c4eSMatthias Ringwald static void hci_transport_link_send_ack_packet(void){
353bd021c4eSMatthias Ringwald     log_info("link: send ack %u", link_ack_nr);
354bd021c4eSMatthias Ringwald     uint8_t header[4];
355bd021c4eSMatthias Ringwald     hci_transport_link_calc_header(header, 0, link_ack_nr, 0, 0, LINK_ACKNOWLEDGEMENT_TYPE, 0);
356bd021c4eSMatthias Ringwald     hci_transport_slip_send_frame(header, NULL, 0);
357bd021c4eSMatthias Ringwald }
358bd021c4eSMatthias Ringwald 
359bd021c4eSMatthias Ringwald static void hci_transport_link_run(void){
360bd021c4eSMatthias Ringwald     // exit if outgoing active
3616ae10533SMatthias Ringwald     if (slip_write_active) return;
362bd021c4eSMatthias Ringwald 
363bd021c4eSMatthias Ringwald     // process queued requests
364bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SYNC){
365bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SYNC;
366bd021c4eSMatthias Ringwald         hci_transport_link_send_sync();
367bd021c4eSMatthias Ringwald         return;
368bd021c4eSMatthias Ringwald     }
369bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE){
370bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE;
371bd021c4eSMatthias Ringwald         hci_transport_link_send_sync_response();
372bd021c4eSMatthias Ringwald         return;
373bd021c4eSMatthias Ringwald     }
374bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_CONFIG){
375bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_CONFIG;
376bd021c4eSMatthias Ringwald         hci_transport_link_send_config();
377bd021c4eSMatthias Ringwald         return;
378bd021c4eSMatthias Ringwald     }
379bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE){
380bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE;
381bd021c4eSMatthias Ringwald         hci_transport_link_send_config_response();
382bd021c4eSMatthias Ringwald         return;
383bd021c4eSMatthias Ringwald     }
384bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_WOKEN){
385bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_WOKEN;
386bd021c4eSMatthias Ringwald         hci_transport_link_send_woken();
387bd021c4eSMatthias Ringwald         return;
388bd021c4eSMatthias Ringwald     }
389bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_WAKEUP){
390bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_WAKEUP;
391bd021c4eSMatthias Ringwald         hci_transport_link_send_wakeup();
392bd021c4eSMatthias Ringwald         return;
393bd021c4eSMatthias Ringwald     }
394bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET){
395bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET;
396bd021c4eSMatthias Ringwald         // packet already contains ack, no need to send addtitional one
397bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_ACK_PACKET;
398bd021c4eSMatthias Ringwald         hci_transport_link_send_queued_packet();
399bd021c4eSMatthias Ringwald         return;
400bd021c4eSMatthias Ringwald     }
401bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_ACK_PACKET){
402bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_ACK_PACKET;
403bd021c4eSMatthias Ringwald         hci_transport_link_send_ack_packet();
404bd021c4eSMatthias Ringwald         return;
405bd021c4eSMatthias Ringwald     }
40694764e99SMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SLEEP){
40794764e99SMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SLEEP;
40894764e99SMatthias Ringwald         hci_transport_link_actions |=  HCI_TRANSPORT_LINK_ENTER_SLEEP;
40994764e99SMatthias Ringwald         link_peer_asleep = 1;
41094764e99SMatthias Ringwald         hci_transport_link_send_sleep();
41194764e99SMatthias Ringwald         return;
41294764e99SMatthias Ringwald     }
413bd021c4eSMatthias Ringwald }
414bd021c4eSMatthias Ringwald 
415bd021c4eSMatthias Ringwald static void hci_transport_link_set_timer(uint16_t timeout_ms){
416bd021c4eSMatthias Ringwald     btstack_run_loop_set_timer_handler(&link_timer, &hci_transport_link_timeout_handler);
417bd021c4eSMatthias Ringwald     btstack_run_loop_set_timer(&link_timer, timeout_ms);
418bd021c4eSMatthias Ringwald     btstack_run_loop_add_timer(&link_timer);
419bd021c4eSMatthias Ringwald }
420bd021c4eSMatthias Ringwald 
421bd021c4eSMatthias Ringwald static void hci_transport_link_timeout_handler(btstack_timer_source_t * timer){
422bd021c4eSMatthias Ringwald     switch (link_state){
423bd021c4eSMatthias Ringwald         case LINK_UNINITIALIZED:
424bd021c4eSMatthias Ringwald             hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC;
425bd021c4eSMatthias Ringwald             hci_transport_link_set_timer(LINK_PERIOD_MS);
426bd021c4eSMatthias Ringwald             break;
427bd021c4eSMatthias Ringwald         case LINK_INITIALIZED:
428bd021c4eSMatthias Ringwald             hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG;
429bd021c4eSMatthias Ringwald             hci_transport_link_set_timer(LINK_PERIOD_MS);
430bd021c4eSMatthias Ringwald             break;
431bd021c4eSMatthias Ringwald         case LINK_ACTIVE:
432bd021c4eSMatthias Ringwald             if (!hci_transport_link_have_outgoing_packet()){
433bd021c4eSMatthias Ringwald                 log_info("h5 timeout while active, but no outgoing packet");
434bd021c4eSMatthias Ringwald                 return;
435bd021c4eSMatthias Ringwald             }
436bd021c4eSMatthias Ringwald             if (link_peer_asleep){
437bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WAKEUP;
438bd021c4eSMatthias Ringwald                 hci_transport_link_set_timer(LINK_WAKEUP_MS);
439bd021c4eSMatthias Ringwald                 return;
440bd021c4eSMatthias Ringwald             }
441bd021c4eSMatthias Ringwald             // resend packet
442bd021c4eSMatthias Ringwald             hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET;
443bd021c4eSMatthias Ringwald             hci_transport_link_set_timer(link_resend_timeout_ms);
444bd021c4eSMatthias Ringwald             break;
445bd021c4eSMatthias Ringwald         default:
446bd021c4eSMatthias Ringwald             break;
447bd021c4eSMatthias Ringwald     }
448bd021c4eSMatthias Ringwald 
449bd021c4eSMatthias Ringwald     hci_transport_link_run();
450bd021c4eSMatthias Ringwald }
451bd021c4eSMatthias Ringwald 
452bd021c4eSMatthias Ringwald static void hci_transport_link_init(void){
453bd021c4eSMatthias Ringwald     link_state = LINK_UNINITIALIZED;
454bd021c4eSMatthias Ringwald     link_peer_asleep = 0;
4557c2aa31eSMatthias Ringwald     link_peer_supports_data_integrity_check = 0;
456bd021c4eSMatthias Ringwald 
457bd021c4eSMatthias Ringwald     // get started
458bd021c4eSMatthias Ringwald     hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC;
459bd021c4eSMatthias Ringwald     hci_transport_link_set_timer(LINK_PERIOD_MS);
460bd021c4eSMatthias Ringwald     hci_transport_link_run();
461bd021c4eSMatthias Ringwald }
462bd021c4eSMatthias Ringwald 
463bd021c4eSMatthias Ringwald static int hci_transport_link_inc_seq_nr(int seq_nr){
464bd021c4eSMatthias Ringwald     return (seq_nr + 1) & 0x07;
465bd021c4eSMatthias Ringwald }
466bd021c4eSMatthias Ringwald 
467bd021c4eSMatthias Ringwald static int hci_transport_link_have_outgoing_packet(void){
468bd021c4eSMatthias Ringwald     return hci_packet != 0;
469bd021c4eSMatthias Ringwald }
470bd021c4eSMatthias Ringwald 
471bd021c4eSMatthias Ringwald static void hci_transport_link_clear_queue(void){
472bd021c4eSMatthias Ringwald     btstack_run_loop_remove_timer(&link_timer);
473bd021c4eSMatthias Ringwald     hci_packet = NULL;
474bd021c4eSMatthias Ringwald }
475bd021c4eSMatthias Ringwald 
476bd021c4eSMatthias Ringwald static void hci_transport_h5_queue_packet(uint8_t packet_type, uint8_t *packet, int size){
477bd021c4eSMatthias Ringwald     hci_packet = packet;
478bd021c4eSMatthias Ringwald     hci_packet_type = packet_type;
479bd021c4eSMatthias Ringwald     hci_packet_size = size;
480bd021c4eSMatthias Ringwald }
481bd021c4eSMatthias Ringwald 
482*62ca45d7SMatthias Ringwald static void hci_transport_h5_emit_sleep_state(int sleep_active){
483*62ca45d7SMatthias Ringwald     static int last_state = 0;
484*62ca45d7SMatthias Ringwald     if (sleep_active == last_state) return;
485*62ca45d7SMatthias Ringwald     last_state = sleep_active;
486*62ca45d7SMatthias Ringwald 
487*62ca45d7SMatthias Ringwald     log_info("hci_transport_h5_emit_sleep_state: %u", sleep_active);
488*62ca45d7SMatthias Ringwald     uint8_t event[3];
489*62ca45d7SMatthias Ringwald     event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE;
490*62ca45d7SMatthias Ringwald     event[1] = sizeof(event) - 2;
491*62ca45d7SMatthias Ringwald     event[2] = sleep_active;
492*62ca45d7SMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
493*62ca45d7SMatthias Ringwald }
494*62ca45d7SMatthias Ringwald 
495bd021c4eSMatthias Ringwald static void hci_transport_h5_process_frame(uint16_t frame_size){
496bd021c4eSMatthias Ringwald 
497bd021c4eSMatthias Ringwald     if (frame_size < 4) return;
498bd021c4eSMatthias Ringwald 
499bd021c4eSMatthias Ringwald     uint8_t * slip_header  = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE];
500bd021c4eSMatthias Ringwald     uint8_t * slip_payload = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 4];
501bd021c4eSMatthias Ringwald     int       frame_size_without_header = frame_size - 4;
502bd021c4eSMatthias Ringwald 
503f04a0c31SMatthias Ringwald     uint8_t  seq_nr =  slip_header[0] & 0x07;
504f04a0c31SMatthias Ringwald     uint8_t  ack_nr = (slip_header[0] >> 3)    & 0x07;
505f04a0c31SMatthias Ringwald     uint8_t  data_integrity_check_present = (slip_header[0] & 0x40) != 0;
506f04a0c31SMatthias Ringwald     uint8_t  reliable_packet  = (slip_header[0] & 0x80) != 0;
507bd021c4eSMatthias Ringwald     uint8_t  link_packet_type = slip_header[1] & 0x0f;
508bd021c4eSMatthias Ringwald     uint16_t link_payload_len = (slip_header[1] >> 4) | (slip_header[2] << 4);
509bd021c4eSMatthias Ringwald 
51079b61987SMatthias Ringwald     log_info("hci_transport_h5_process_frame, reliable %u, packet type %u, seq_nr %u, ack_nr %u , dic %u", reliable_packet, link_packet_type, seq_nr, ack_nr, data_integrity_check_present);
511bd021c4eSMatthias Ringwald     log_info_hexdump(slip_header, 4);
512bd021c4eSMatthias Ringwald     log_info_hexdump(slip_payload, frame_size_without_header);
513bd021c4eSMatthias Ringwald 
514bd021c4eSMatthias Ringwald     // CSR 8811 does not seem to auto-detect H5 mode and sends data with even parity.
515bd021c4eSMatthias Ringwald     // if this byte sequence is detected, just enable even parity
516bd021c4eSMatthias Ringwald     const uint8_t sync_response_bcsp[] = {0x01, 0x7a, 0x06, 0x10};
517bd021c4eSMatthias Ringwald     if (memcmp(sync_response_bcsp, slip_header, 4) == 0){
518bd021c4eSMatthias Ringwald         log_info("h5: detected BSCP SYNC sent with Even Parity -> discard frame and enable Even Parity");
519bd021c4eSMatthias Ringwald         btstack_uart->set_parity(1);
520bd021c4eSMatthias Ringwald         return;
521bd021c4eSMatthias Ringwald     }
522bd021c4eSMatthias Ringwald 
523bd021c4eSMatthias Ringwald     // validate header checksum
524bd021c4eSMatthias Ringwald     uint8_t header_checksum = slip_header[0] + slip_header[1] + slip_header[2] + slip_header[3];
525bd021c4eSMatthias Ringwald     if (header_checksum != 0xff){
526bd021c4eSMatthias Ringwald         log_info("h5: header checksum 0x%02x (instead of 0xff)", header_checksum);
527bd021c4eSMatthias Ringwald         return;
528bd021c4eSMatthias Ringwald     }
529bd021c4eSMatthias Ringwald 
530bd021c4eSMatthias Ringwald     // validate payload length
531bd021c4eSMatthias Ringwald     int data_integrity_len = data_integrity_check_present ? 2 : 0;
532bd021c4eSMatthias Ringwald     uint16_t received_payload_len = frame_size_without_header - data_integrity_len;
533bd021c4eSMatthias Ringwald     if (link_payload_len != received_payload_len){
534bd021c4eSMatthias Ringwald         log_info("h5: expected payload len %u but got %u", link_payload_len, received_payload_len);
535bd021c4eSMatthias Ringwald         return;
536bd021c4eSMatthias Ringwald     }
537bd021c4eSMatthias Ringwald 
53879b61987SMatthias Ringwald     // validate data integrity check
53979b61987SMatthias Ringwald     if (data_integrity_check_present){
54079b61987SMatthias Ringwald         uint16_t dic_packet = big_endian_read_16(slip_payload, received_payload_len);
54179b61987SMatthias Ringwald         uint16_t dic_calculate = crc16_calc_for_slip_frame(slip_header, slip_payload, received_payload_len);
54279b61987SMatthias Ringwald         if (dic_packet != dic_calculate){
54379b61987SMatthias Ringwald             log_info("h5: expected dic value 0x%04x but got 0x%04x", dic_calculate, dic_packet);
54479b61987SMatthias Ringwald             return;
54579b61987SMatthias Ringwald         }
54679b61987SMatthias Ringwald     }
547bd021c4eSMatthias Ringwald 
548bd021c4eSMatthias Ringwald     switch (link_state){
549bd021c4eSMatthias Ringwald         case LINK_UNINITIALIZED:
550bd021c4eSMatthias Ringwald             if (link_packet_type != LINK_CONTROL_PACKET_TYPE) break;
551bd021c4eSMatthias Ringwald             if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){
552bd021c4eSMatthias Ringwald                 log_info("link: received sync");
553bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE;
554bd021c4eSMatthias Ringwald             }
555bd021c4eSMatthias Ringwald             if (memcmp(slip_payload, link_control_sync_response, sizeof(link_control_sync_response)) == 0){
556bd021c4eSMatthias Ringwald                 log_info("link: received sync response");
557bd021c4eSMatthias Ringwald                 link_state = LINK_INITIALIZED;
558bd021c4eSMatthias Ringwald                 btstack_run_loop_remove_timer(&link_timer);
559bd021c4eSMatthias Ringwald                 log_info("link initialized");
560bd021c4eSMatthias Ringwald                 //
561bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG;
562bd021c4eSMatthias Ringwald                 hci_transport_link_set_timer(LINK_PERIOD_MS);
563bd021c4eSMatthias Ringwald             }
564bd021c4eSMatthias Ringwald             break;
565bd021c4eSMatthias Ringwald         case LINK_INITIALIZED:
566bd021c4eSMatthias Ringwald             if (link_packet_type != LINK_CONTROL_PACKET_TYPE) break;
567bd021c4eSMatthias Ringwald             if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){
568bd021c4eSMatthias Ringwald                 log_info("link: received sync");
569bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE;
570bd021c4eSMatthias Ringwald             }
571cbc04f48SMatthias Ringwald             if (memcmp(slip_payload, link_control_config, link_control_config_prefix_len) == 0){
57279b61987SMatthias Ringwald                 log_info("link: received config, 0x%02x", slip_payload[2]);
573bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE;
574bd021c4eSMatthias Ringwald             }
575bd021c4eSMatthias Ringwald             if (memcmp(slip_payload, link_control_config_response, link_control_config_response_prefix_len) == 0){
5767c2aa31eSMatthias Ringwald                 uint8_t config = slip_payload[2];
5777c2aa31eSMatthias Ringwald                 link_peer_supports_data_integrity_check = (config & 0x10) != 0;
5787c2aa31eSMatthias Ringwald                 log_info("link: received config response 0x%02x, data integrity check supported %u", config, link_peer_supports_data_integrity_check);
579bd021c4eSMatthias Ringwald                 link_state = LINK_ACTIVE;
580bd021c4eSMatthias Ringwald                 btstack_run_loop_remove_timer(&link_timer);
581bd021c4eSMatthias Ringwald                 log_info("link activated");
582bd021c4eSMatthias Ringwald                 //
583bd021c4eSMatthias Ringwald                 link_seq_nr = 0;
584bd021c4eSMatthias Ringwald                 link_ack_nr = 0;
585bd021c4eSMatthias Ringwald                 // notify upper stack that it can start
586bd021c4eSMatthias Ringwald                 uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
587bd021c4eSMatthias Ringwald                 packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
588bd021c4eSMatthias Ringwald             }
589bd021c4eSMatthias Ringwald             break;
590bd021c4eSMatthias Ringwald         case LINK_ACTIVE:
591bd021c4eSMatthias Ringwald 
592bd021c4eSMatthias Ringwald             // validate packet sequence nr in reliable packets (check for out of sequence error)
593bd021c4eSMatthias Ringwald             if (reliable_packet){
594bd021c4eSMatthias Ringwald                 if (seq_nr != link_ack_nr){
595bd021c4eSMatthias Ringwald                     log_info("expected seq nr %u, but received %u", link_ack_nr, seq_nr);
596bd021c4eSMatthias Ringwald                     hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_ACK_PACKET;
597bd021c4eSMatthias Ringwald                     break;
598bd021c4eSMatthias Ringwald                 }
599bd021c4eSMatthias Ringwald                 // ack packet right away
600bd021c4eSMatthias Ringwald                 link_ack_nr = hci_transport_link_inc_seq_nr(link_ack_nr);
601bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_ACK_PACKET;
602bd021c4eSMatthias Ringwald             }
603bd021c4eSMatthias Ringwald 
604bd021c4eSMatthias Ringwald             // Process ACKs in reliable packet and explicit ack packets
605bd021c4eSMatthias Ringwald             if (reliable_packet || link_packet_type == LINK_ACKNOWLEDGEMENT_TYPE){
606bd021c4eSMatthias Ringwald                 // our packet is good if the remote expects our seq nr + 1
607bd021c4eSMatthias Ringwald                 int next_seq_nr = hci_transport_link_inc_seq_nr(link_seq_nr);
608bd021c4eSMatthias Ringwald                 if (hci_transport_link_have_outgoing_packet() && next_seq_nr == ack_nr){
609bd021c4eSMatthias Ringwald                     log_info("h5: outoing packet with seq %u ack'ed", link_seq_nr);
610bd021c4eSMatthias Ringwald                     link_seq_nr = next_seq_nr;
611bd021c4eSMatthias Ringwald                     hci_transport_link_clear_queue();
612bd021c4eSMatthias Ringwald 
613bd021c4eSMatthias Ringwald                     // notify upper stack that it can send again
614bd021c4eSMatthias Ringwald                     uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
615bd021c4eSMatthias Ringwald                     packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
616bd021c4eSMatthias Ringwald                 }
617bd021c4eSMatthias Ringwald             }
618bd021c4eSMatthias Ringwald 
619bd021c4eSMatthias Ringwald             switch (link_packet_type){
620bd021c4eSMatthias Ringwald                 case LINK_CONTROL_PACKET_TYPE:
621bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_config, sizeof(link_control_config)) == 0){
622bd021c4eSMatthias Ringwald                         log_info("link: received config");
623bd021c4eSMatthias Ringwald                         hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE;
624bd021c4eSMatthias Ringwald                         break;
625bd021c4eSMatthias Ringwald                     }
626bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){
627bd021c4eSMatthias Ringwald                         log_info("link: received sync in ACTIVE STATE!");
628bd021c4eSMatthias Ringwald                         // TODO sync during active indicates peer reset -> full upper layer reset necessary
629bd021c4eSMatthias Ringwald                         break;
630bd021c4eSMatthias Ringwald                     }
631bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_sleep, sizeof(link_control_sleep)) == 0){
632d8e28fa3SMatthias Ringwald                         if (btstack_uart_sleep_mode){
633d8e28fa3SMatthias Ringwald                             log_info("link: received sleep message. Enabling UART Sleep.");
634d8e28fa3SMatthias Ringwald                             btstack_uart->set_sleep(btstack_uart_sleep_mode);
635*62ca45d7SMatthias Ringwald                             hci_transport_h5_emit_sleep_state(1);
636d8e28fa3SMatthias Ringwald                         } else {
637d8e28fa3SMatthias Ringwald                             log_info("link: received sleep message. UART Sleep not supported");
638d8e28fa3SMatthias Ringwald                         }
639bd021c4eSMatthias Ringwald                         link_peer_asleep = 1;
640bd021c4eSMatthias Ringwald                         break;
641bd021c4eSMatthias Ringwald                     }
642bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_wakeup, sizeof(link_control_wakeup)) == 0){
643bd021c4eSMatthias Ringwald                         log_info("link: received wakupe message -> send woken");
644bd021c4eSMatthias Ringwald                         link_peer_asleep = 0;
645bd021c4eSMatthias Ringwald                         hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WOKEN;
646bd021c4eSMatthias Ringwald                         break;
647bd021c4eSMatthias Ringwald                     }
648bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_woken, sizeof(link_control_woken)) == 0){
649bd021c4eSMatthias Ringwald                         log_info("link: received woken message");
650bd021c4eSMatthias Ringwald                         link_peer_asleep = 0;
65194764e99SMatthias Ringwald                         // queued packet will be sent in hci_transport_link_run if needed
652bd021c4eSMatthias Ringwald                         break;
653bd021c4eSMatthias Ringwald                     }
654bd021c4eSMatthias Ringwald                     break;
655bd021c4eSMatthias Ringwald                 case HCI_EVENT_PACKET:
656bd021c4eSMatthias Ringwald                 case HCI_ACL_DATA_PACKET:
657bd021c4eSMatthias Ringwald                 case HCI_SCO_DATA_PACKET:
65894764e99SMatthias Ringwald                     // seems like peer is awake
65994764e99SMatthias Ringwald                     link_peer_asleep = 0;
66094764e99SMatthias Ringwald                     // forward packet to stack
661bd021c4eSMatthias Ringwald                     packet_handler(link_packet_type, slip_payload, link_payload_len);
66294764e99SMatthias Ringwald                     // reset inactvitiy timer
66394764e99SMatthias Ringwald                     hci_transport_inactivity_timer_set();
664bd021c4eSMatthias Ringwald                     break;
665bd021c4eSMatthias Ringwald             }
666bd021c4eSMatthias Ringwald 
667bd021c4eSMatthias Ringwald             break;
668bd021c4eSMatthias Ringwald         default:
669bd021c4eSMatthias Ringwald             break;
670bd021c4eSMatthias Ringwald     }
671bd021c4eSMatthias Ringwald 
672bd021c4eSMatthias Ringwald     hci_transport_link_run();
673bd021c4eSMatthias Ringwald }
674bd021c4eSMatthias Ringwald 
675bd021c4eSMatthias Ringwald // recommendet time until resend: 3 * time of largest packet
676bd021c4eSMatthias Ringwald static uint16_t hci_transport_link_calc_resend_timeout(uint32_t baudrate){
677bd021c4eSMatthias Ringwald     uint32_t max_packet_size_in_bit = (HCI_PACKET_BUFFER_SIZE + 6) << 3;
678bd021c4eSMatthias Ringwald     uint32_t t_max_x3_ms = max_packet_size_in_bit * 3000 / baudrate;
679bd021c4eSMatthias Ringwald     log_info("resend timeout for %u baud: %u ms", baudrate, t_max_x3_ms);
680bd021c4eSMatthias Ringwald     return t_max_x3_ms;
681bd021c4eSMatthias Ringwald }
682bd021c4eSMatthias Ringwald 
683bd021c4eSMatthias Ringwald static void hci_transport_link_update_resend_timeout(uint32_t baudrate){
684bd021c4eSMatthias Ringwald     link_resend_timeout_ms = hci_transport_link_calc_resend_timeout(baudrate);
685bd021c4eSMatthias Ringwald }
686bd021c4eSMatthias Ringwald 
687bd021c4eSMatthias Ringwald /// H5 Interface
688bd021c4eSMatthias Ringwald 
689bd021c4eSMatthias Ringwald static uint8_t hci_transport_link_read_byte;
690bd021c4eSMatthias Ringwald 
691bd021c4eSMatthias Ringwald static void hci_transport_h5_read_next_byte(void){
692cbc04f48SMatthias Ringwald     log_debug("h5: rx nxt");
693bd021c4eSMatthias Ringwald     btstack_uart->receive_block(&hci_transport_link_read_byte, 1);
694bd021c4eSMatthias Ringwald }
695bd021c4eSMatthias Ringwald 
696bd021c4eSMatthias Ringwald static void hci_transport_h5_block_received(){
697cbc04f48SMatthias Ringwald     log_debug("slip: process 0x%02x", hci_transport_link_read_byte);
698bd021c4eSMatthias Ringwald     btstack_slip_decoder_process(hci_transport_link_read_byte);
699bd021c4eSMatthias Ringwald     uint16_t frame_size = btstack_slip_decoder_frame_size();
700bd021c4eSMatthias Ringwald     if (frame_size) {
701bd021c4eSMatthias Ringwald         hci_transport_h5_process_frame(frame_size);
702bd021c4eSMatthias Ringwald         hci_transport_slip_init();
703bd021c4eSMatthias Ringwald     }
704bd021c4eSMatthias Ringwald     hci_transport_h5_read_next_byte();
705bd021c4eSMatthias Ringwald }
706bd021c4eSMatthias Ringwald 
707bd021c4eSMatthias Ringwald static void hci_transport_h5_block_sent(void){
7086ae10533SMatthias Ringwald 
7096ae10533SMatthias Ringwald     // check if more data to send
7106ae10533SMatthias Ringwald     if (btstack_slip_encoder_has_data()){
7116ae10533SMatthias Ringwald         hci_transport_slip_send_next_chunk();
7126ae10533SMatthias Ringwald         return;
7136ae10533SMatthias Ringwald     }
7146ae10533SMatthias Ringwald 
7156ae10533SMatthias Ringwald     // done
7166ae10533SMatthias Ringwald     slip_write_active = 0;
71794764e99SMatthias Ringwald 
71894764e99SMatthias Ringwald     // enter sleep mode after sending sleep message
71994764e99SMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_ENTER_SLEEP){
72094764e99SMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_ENTER_SLEEP;
72194764e99SMatthias Ringwald         if (btstack_uart_sleep_mode){
72294764e99SMatthias Ringwald             log_info("link: sent sleep message. Enabling UART Sleep.");
72394764e99SMatthias Ringwald             btstack_uart->set_sleep(btstack_uart_sleep_mode);
72494764e99SMatthias Ringwald         } else {
72594764e99SMatthias Ringwald             log_info("link: sent sleep message. UART Sleep not supported");
72694764e99SMatthias Ringwald         }
727*62ca45d7SMatthias Ringwald         hci_transport_h5_emit_sleep_state(1);
72894764e99SMatthias Ringwald     }
72994764e99SMatthias Ringwald 
730bd021c4eSMatthias Ringwald     hci_transport_link_run();
731bd021c4eSMatthias Ringwald }
732bd021c4eSMatthias Ringwald 
733bd021c4eSMatthias Ringwald static void hci_transport_h5_init(const void * transport_config){
734bd021c4eSMatthias Ringwald     // check for hci_transport_config_uart_t
735bd021c4eSMatthias Ringwald     if (!transport_config) {
736bd021c4eSMatthias Ringwald         log_error("hci_transport_h5: no config!");
737bd021c4eSMatthias Ringwald         return;
738bd021c4eSMatthias Ringwald     }
739bd021c4eSMatthias Ringwald     if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) {
740bd021c4eSMatthias Ringwald         log_error("hci_transport_h5: config not of type != HCI_TRANSPORT_CONFIG_UART!");
741bd021c4eSMatthias Ringwald         return;
742bd021c4eSMatthias Ringwald     }
743bd021c4eSMatthias Ringwald 
744bd021c4eSMatthias Ringwald     // extract UART config from transport config
745bd021c4eSMatthias Ringwald     hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config;
746bd021c4eSMatthias Ringwald     uart_config.baudrate    = hci_transport_config_uart->baudrate_init;
747bd021c4eSMatthias Ringwald     uart_config.flowcontrol = hci_transport_config_uart->flowcontrol;
748bd021c4eSMatthias Ringwald     uart_config.device_name = hci_transport_config_uart->device_name;
749bd021c4eSMatthias Ringwald 
750bd021c4eSMatthias Ringwald     // setup UART driver
751bd021c4eSMatthias Ringwald     btstack_uart->init(&uart_config);
752bd021c4eSMatthias Ringwald     btstack_uart->set_block_received(&hci_transport_h5_block_received);
753bd021c4eSMatthias Ringwald     btstack_uart->set_block_sent(&hci_transport_h5_block_sent);
754bd021c4eSMatthias Ringwald }
755bd021c4eSMatthias Ringwald 
756bd021c4eSMatthias Ringwald static int hci_transport_h5_open(void){
757bd021c4eSMatthias Ringwald     int res = btstack_uart->open();
758bd021c4eSMatthias Ringwald     if (res){
759bd021c4eSMatthias Ringwald         return res;
760bd021c4eSMatthias Ringwald     }
761bd021c4eSMatthias Ringwald 
762d8e28fa3SMatthias Ringwald     // check if wake on RX can be used
763d8e28fa3SMatthias Ringwald     btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF;
764d8e28fa3SMatthias Ringwald     int supported_sleep_modes = 0;
765d8e28fa3SMatthias Ringwald     if (btstack_uart->get_supported_sleep_modes){
766d8e28fa3SMatthias Ringwald         supported_sleep_modes = btstack_uart->get_supported_sleep_modes();
767d8e28fa3SMatthias Ringwald     }
768d8e28fa3SMatthias Ringwald     if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){
769d8e28fa3SMatthias Ringwald         log_info("H5: using wake on RX");
770d8e28fa3SMatthias Ringwald         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE;
771d8e28fa3SMatthias Ringwald     } else {
772d8e28fa3SMatthias Ringwald         log_info("H5: UART driver does not provide compatible sleep mode");
773d8e28fa3SMatthias Ringwald     }
774d8e28fa3SMatthias Ringwald 
775bd021c4eSMatthias Ringwald     // setup resend timeout
776bd021c4eSMatthias Ringwald     hci_transport_link_update_resend_timeout(uart_config.baudrate);
777bd021c4eSMatthias Ringwald 
778bd021c4eSMatthias Ringwald     // init slip parser state machine
779bd021c4eSMatthias Ringwald     hci_transport_slip_init();
780bd021c4eSMatthias Ringwald 
781bd021c4eSMatthias Ringwald     // init link management - already starts syncing
782bd021c4eSMatthias Ringwald     hci_transport_link_init();
783bd021c4eSMatthias Ringwald 
784bd021c4eSMatthias Ringwald     // start receiving
785bd021c4eSMatthias Ringwald     hci_transport_h5_read_next_byte();
786bd021c4eSMatthias Ringwald 
787bd021c4eSMatthias Ringwald     return 0;
788bd021c4eSMatthias Ringwald }
789bd021c4eSMatthias Ringwald 
790bd021c4eSMatthias Ringwald static int hci_transport_h5_close(void){
791bd021c4eSMatthias Ringwald     return btstack_uart->close();
792bd021c4eSMatthias Ringwald     return 0;
793bd021c4eSMatthias Ringwald }
794bd021c4eSMatthias Ringwald 
795bd021c4eSMatthias Ringwald static void hci_transport_h5_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
796bd021c4eSMatthias Ringwald     packet_handler = handler;
797bd021c4eSMatthias Ringwald }
798bd021c4eSMatthias Ringwald 
799bd021c4eSMatthias Ringwald static int hci_transport_h5_can_send_packet_now(uint8_t packet_type){
80094764e99SMatthias Ringwald     int res = !hci_transport_link_have_outgoing_packet() && link_state == LINK_ACTIVE;
8016ae10533SMatthias Ringwald     // log_info("hci_transport_h5_can_send_packet_now: %u", res);
80294764e99SMatthias Ringwald     return res;
803bd021c4eSMatthias Ringwald }
804bd021c4eSMatthias Ringwald 
805bd021c4eSMatthias Ringwald static int hci_transport_h5_send_packet(uint8_t packet_type, uint8_t *packet, int size){
806bd021c4eSMatthias Ringwald     if (!hci_transport_h5_can_send_packet_now(packet_type)){
807f04a0c31SMatthias Ringwald         log_error("hci_transport_h5_send_packet called but in state %d", link_state);
808bd021c4eSMatthias Ringwald         return -1;
809bd021c4eSMatthias Ringwald     }
810bd021c4eSMatthias Ringwald 
811bd021c4eSMatthias Ringwald     // store request
812bd021c4eSMatthias Ringwald     hci_transport_h5_queue_packet(packet_type, packet, size);
813bd021c4eSMatthias Ringwald 
814bd021c4eSMatthias Ringwald     // send wakeup first
815bd021c4eSMatthias Ringwald     if (link_peer_asleep){
816*62ca45d7SMatthias Ringwald         hci_transport_h5_emit_sleep_state(0);
817d8e28fa3SMatthias Ringwald         if (btstack_uart_sleep_mode){
818d8e28fa3SMatthias Ringwald             log_info("h5: disable UART sleep");
819d8e28fa3SMatthias Ringwald             btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
820d8e28fa3SMatthias Ringwald         }
821bd021c4eSMatthias Ringwald         hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WAKEUP;
822bd021c4eSMatthias Ringwald         hci_transport_link_set_timer(LINK_WAKEUP_MS);
823bd021c4eSMatthias Ringwald     } else {
824bd021c4eSMatthias Ringwald         hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET;
825bd021c4eSMatthias Ringwald         hci_transport_link_set_timer(link_resend_timeout_ms);
826bd021c4eSMatthias Ringwald     }
827bd021c4eSMatthias Ringwald     hci_transport_link_run();
828bd021c4eSMatthias Ringwald     return 0;
829bd021c4eSMatthias Ringwald }
830bd021c4eSMatthias Ringwald 
831bd021c4eSMatthias Ringwald static int hci_transport_h5_set_baudrate(uint32_t baudrate){
832bd021c4eSMatthias Ringwald 
833bd021c4eSMatthias Ringwald     log_info("hci_transport_h5_set_baudrate %u", baudrate);
834bd021c4eSMatthias Ringwald     int res = btstack_uart->set_baudrate(baudrate);
835bd021c4eSMatthias Ringwald 
836bd021c4eSMatthias Ringwald     if (res) return res;
837bd021c4eSMatthias Ringwald     hci_transport_link_update_resend_timeout(baudrate);
838bd021c4eSMatthias Ringwald     return 0;
839bd021c4eSMatthias Ringwald }
840bd021c4eSMatthias Ringwald 
841bd021c4eSMatthias Ringwald static void hci_transport_h5_reset_link(void){
842bd021c4eSMatthias Ringwald 
843bd021c4eSMatthias Ringwald     log_info("hci_transport_h5_reset_link");
844bd021c4eSMatthias Ringwald 
845bd021c4eSMatthias Ringwald     // clear outgoing queue
846bd021c4eSMatthias Ringwald     hci_transport_link_clear_queue();
847bd021c4eSMatthias Ringwald 
848bd021c4eSMatthias Ringwald     // init slip parser state machine
849bd021c4eSMatthias Ringwald     hci_transport_slip_init();
850bd021c4eSMatthias Ringwald 
851bd021c4eSMatthias Ringwald     // init link management - already starts syncing
852bd021c4eSMatthias Ringwald     hci_transport_link_init();
853bd021c4eSMatthias Ringwald }
854bd021c4eSMatthias Ringwald 
855bd021c4eSMatthias Ringwald static const hci_transport_t hci_transport_h5 = {
856bd021c4eSMatthias Ringwald     /* const char * name; */                                        "H5",
857bd021c4eSMatthias Ringwald     /* void   (*init) (const void *transport_config); */            &hci_transport_h5_init,
858bd021c4eSMatthias Ringwald     /* int    (*open)(void); */                                     &hci_transport_h5_open,
859bd021c4eSMatthias Ringwald     /* int    (*close)(void); */                                    &hci_transport_h5_close,
860bd021c4eSMatthias Ringwald     /* void   (*register_packet_handler)(void (*handler)(...); */   &hci_transport_h5_register_packet_handler,
861bd021c4eSMatthias Ringwald     /* int    (*can_send_packet_now)(uint8_t packet_type); */       &hci_transport_h5_can_send_packet_now,
862bd021c4eSMatthias Ringwald     /* int    (*send_packet)(...); */                               &hci_transport_h5_send_packet,
863bd021c4eSMatthias Ringwald     /* int    (*set_baudrate)(uint32_t baudrate); */                &hci_transport_h5_set_baudrate,
864bd021c4eSMatthias Ringwald     /* void   (*reset_link)(void); */                               &hci_transport_h5_reset_link,
865ee752bb8SMatthias Ringwald     /* void   (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL,
866bd021c4eSMatthias Ringwald };
867bd021c4eSMatthias Ringwald 
868bd021c4eSMatthias Ringwald // configure and return h5 singleton
869bd021c4eSMatthias Ringwald const hci_transport_t * hci_transport_h5_instance(const btstack_uart_block_t * uart_driver) {
870bd021c4eSMatthias Ringwald     btstack_uart = uart_driver;
871bd021c4eSMatthias Ringwald     return &hci_transport_h5;
872bd021c4eSMatthias Ringwald }
87394764e99SMatthias Ringwald 
87494764e99SMatthias Ringwald void hci_transport_h5_set_auto_sleep(uint16_t inactivity_timeout_ms){
87594764e99SMatthias Ringwald     link_inactivity_timeout_ms = inactivity_timeout_ms;
87694764e99SMatthias Ringwald }
877