xref: /btstack/src/hci_transport_h5.c (revision 6ae10533e16cc5501c260c3db290f37f1ab2d17f)
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 
52bd021c4eSMatthias Ringwald typedef enum {
53bd021c4eSMatthias Ringwald     LINK_UNINITIALIZED,
54bd021c4eSMatthias Ringwald     LINK_INITIALIZED,
55bd021c4eSMatthias Ringwald     LINK_ACTIVE
56bd021c4eSMatthias Ringwald } hci_transport_link_state_t;
57bd021c4eSMatthias Ringwald 
58bd021c4eSMatthias Ringwald typedef enum {
59bd021c4eSMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_SYNC            = 1 << 0,
60bd021c4eSMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE   = 1 << 1,
61bd021c4eSMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_CONFIG          = 1 << 2,
62bd021c4eSMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE = 1 << 3,
6394764e99SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_SLEEP           = 1 << 4,
6494764e99SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_WOKEN           = 1 << 5,
6594764e99SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_WAKEUP          = 1 << 6,
6694764e99SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET   = 1 << 7,
6794764e99SMatthias Ringwald     HCI_TRANSPORT_LINK_SEND_ACK_PACKET      = 1 << 8,
6894764e99SMatthias Ringwald     HCI_TRANSPORT_LINK_ENTER_SLEEP          = 1 << 9,
6994764e99SMatthias Ringwald 
70bd021c4eSMatthias Ringwald } hci_transport_link_actions_t;
71bd021c4eSMatthias Ringwald 
72bd021c4eSMatthias Ringwald // Configuration Field. No packet buffers -> sliding window = 1, no OOF flow control, no data integrity check
73bd021c4eSMatthias Ringwald #define LINK_CONFIG_SLIDING_WINDOW_SIZE 1
74bd021c4eSMatthias Ringwald #define LINK_CONFIG_OOF_FLOW_CONTROL 0
75bd021c4eSMatthias Ringwald #define LINK_CONFIG_DATA_INTEGRITY_CHECK 0
76bd021c4eSMatthias Ringwald #define LINK_CONFIG_VERSION_NR 0
77bd021c4eSMatthias 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))
78bd021c4eSMatthias Ringwald 
79bd021c4eSMatthias Ringwald // periodic sending during link establishment
80bd021c4eSMatthias Ringwald #define LINK_PERIOD_MS 250
81bd021c4eSMatthias Ringwald 
82bd021c4eSMatthias Ringwald // resend wakeup
83bd021c4eSMatthias Ringwald #define LINK_WAKEUP_MS 50
84bd021c4eSMatthias Ringwald 
85bd021c4eSMatthias Ringwald // additional packet types
86bd021c4eSMatthias Ringwald #define LINK_ACKNOWLEDGEMENT_TYPE 0x00
87bd021c4eSMatthias Ringwald #define LINK_CONTROL_PACKET_TYPE 0x0f
88bd021c4eSMatthias Ringwald 
89*6ae10533SMatthias Ringwald // max size of write requests
90*6ae10533SMatthias Ringwald #define LINK_SLIP_TX_CHUNK_LEN 64
91*6ae10533SMatthias Ringwald 
92bd021c4eSMatthias Ringwald // ---
93bd021c4eSMatthias Ringwald static const uint8_t link_control_sync[] =   { 0x01, 0x7e};
94bd021c4eSMatthias Ringwald static const uint8_t link_control_sync_response[] = { 0x02, 0x7d};
95bd021c4eSMatthias Ringwald static const uint8_t link_control_config[] = { 0x03, 0xfc, LINK_CONFIG_FIELD};
96cbc04f48SMatthias Ringwald static const uint8_t link_control_config_prefix_len  = 2;
97bd021c4eSMatthias Ringwald static const uint8_t link_control_config_response[] = { 0x04, 0x7b, LINK_CONFIG_FIELD};
98bd021c4eSMatthias Ringwald static const uint8_t link_control_config_response_prefix_len  = 2;
99bd021c4eSMatthias Ringwald static const uint8_t link_control_wakeup[] = { 0x05, 0xfa};
100bd021c4eSMatthias Ringwald static const uint8_t link_control_woken[] =  { 0x06, 0xf9};
101bd021c4eSMatthias Ringwald static const uint8_t link_control_sleep[] =  { 0x07, 0x78};
102bd021c4eSMatthias Ringwald 
103bd021c4eSMatthias Ringwald // incoming pre-bufffer + 4 bytes H5 header + max(acl header + acl payload, event header + event data) + 2 bytes opt CRC
104bd021c4eSMatthias Ringwald static uint8_t   hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 6 + HCI_PACKET_BUFFER_SIZE];
105bd021c4eSMatthias Ringwald 
106*6ae10533SMatthias Ringwald // outgoing slip encoded buffer. +1 to assert that last SOF fits in buffer
107*6ae10533SMatthias Ringwald static uint8_t slip_outgoing_buffer[LINK_SLIP_TX_CHUNK_LEN+1];
108*6ae10533SMatthias Ringwald static int     slip_write_active;
109bd021c4eSMatthias Ringwald 
110bd021c4eSMatthias Ringwald // H5 Link State
111bd021c4eSMatthias Ringwald static hci_transport_link_state_t link_state;
112bd021c4eSMatthias Ringwald static btstack_timer_source_t link_timer;
113bd021c4eSMatthias Ringwald static uint8_t  link_seq_nr;
114bd021c4eSMatthias Ringwald static uint8_t  link_ack_nr;
115bd021c4eSMatthias Ringwald static uint16_t link_resend_timeout_ms;
116bd021c4eSMatthias Ringwald static uint8_t  link_peer_asleep;
117bd021c4eSMatthias Ringwald 
11894764e99SMatthias Ringwald // auto sleep-mode
11994764e99SMatthias Ringwald static btstack_timer_source_t inactivity_timer;
12094764e99SMatthias Ringwald static uint16_t link_inactivity_timeout_ms; // auto-sleep if set
12194764e99SMatthias Ringwald 
122bd021c4eSMatthias Ringwald // Outgoing packet
123bd021c4eSMatthias Ringwald static uint8_t   hci_packet_type;
124bd021c4eSMatthias Ringwald static uint16_t  hci_packet_size;
125bd021c4eSMatthias Ringwald static uint8_t * hci_packet;
126bd021c4eSMatthias Ringwald 
127bd021c4eSMatthias Ringwald // hci packet handler
128bd021c4eSMatthias Ringwald static  void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size);
129bd021c4eSMatthias Ringwald 
130bd021c4eSMatthias Ringwald static int hci_transport_link_actions;
131bd021c4eSMatthias Ringwald 
132bd021c4eSMatthias Ringwald // UART Driver + Config
133bd021c4eSMatthias Ringwald static const btstack_uart_block_t * btstack_uart;
134bd021c4eSMatthias Ringwald static btstack_uart_config_t uart_config;
135d8e28fa3SMatthias Ringwald static btstack_uart_sleep_mode_t btstack_uart_sleep_mode;
136bd021c4eSMatthias Ringwald 
137bd021c4eSMatthias Ringwald // Prototypes
138bd021c4eSMatthias Ringwald static void hci_transport_h5_process_frame(uint16_t frame_size);
139bd021c4eSMatthias Ringwald static int  hci_transport_link_have_outgoing_packet(void);
140bd021c4eSMatthias Ringwald static void hci_transport_link_send_queued_packet(void);
141bd021c4eSMatthias Ringwald static void hci_transport_link_set_timer(uint16_t timeout_ms);
142bd021c4eSMatthias Ringwald static void hci_transport_link_timeout_handler(btstack_timer_source_t * timer);
143bd021c4eSMatthias Ringwald static void hci_transport_link_run(void);
144bd021c4eSMatthias Ringwald static void hci_transport_slip_init(void);
145bd021c4eSMatthias Ringwald 
146bd021c4eSMatthias Ringwald // -----------------------------
14794764e99SMatthias Ringwald static void hci_transport_inactivity_timeout_handler(btstack_timer_source_t * ts){
14894764e99SMatthias Ringwald     log_info("h5: inactivity timeout. link state %u, peer asleep %u, actions 0x%02x, outgoing packet %u",
14994764e99SMatthias Ringwald         link_state, link_peer_asleep, hci_transport_link_actions, hci_transport_link_have_outgoing_packet());
15094764e99SMatthias Ringwald     if (hci_transport_link_have_outgoing_packet()) return;
15194764e99SMatthias Ringwald     if (link_state != LINK_ACTIVE) return;
15294764e99SMatthias Ringwald     if (hci_transport_link_actions) return;
15394764e99SMatthias Ringwald     if (link_peer_asleep) return;
15494764e99SMatthias Ringwald     hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SLEEP;
15594764e99SMatthias Ringwald     hci_transport_link_run();
15694764e99SMatthias Ringwald }
157bd021c4eSMatthias Ringwald 
15894764e99SMatthias Ringwald static void hci_transport_inactivity_timer_set(void){
15994764e99SMatthias Ringwald     if (!link_inactivity_timeout_ms) return;
16094764e99SMatthias Ringwald     btstack_run_loop_set_timer_handler(&inactivity_timer, &hci_transport_inactivity_timeout_handler);
16194764e99SMatthias Ringwald     btstack_run_loop_set_timer(&inactivity_timer, link_inactivity_timeout_ms);
16294764e99SMatthias Ringwald     btstack_run_loop_remove_timer(&inactivity_timer);
16394764e99SMatthias Ringwald     btstack_run_loop_add_timer(&inactivity_timer);
16494764e99SMatthias Ringwald }
165bd021c4eSMatthias Ringwald 
16694764e99SMatthias Ringwald // -----------------------------
167bd021c4eSMatthias Ringwald // SLIP Outgoing
168bd021c4eSMatthias Ringwald 
169*6ae10533SMatthias Ringwald // Fill chunk and write
170*6ae10533SMatthias Ringwald static void hci_transport_slip_encode_chunk_and_send(int pos){
171*6ae10533SMatthias Ringwald     while (btstack_slip_encoder_has_data() & (pos < LINK_SLIP_TX_CHUNK_LEN)) {
172*6ae10533SMatthias Ringwald         slip_outgoing_buffer[pos++] = btstack_slip_encoder_get_byte();
173*6ae10533SMatthias Ringwald     }
174*6ae10533SMatthias Ringwald     if (!btstack_slip_encoder_has_data()){
175*6ae10533SMatthias Ringwald         // Start of Frame
176*6ae10533SMatthias Ringwald         slip_outgoing_buffer[pos++] = BTSTACK_SLIP_SOF;
177*6ae10533SMatthias Ringwald     }
178*6ae10533SMatthias Ringwald     slip_write_active = 1;
179*6ae10533SMatthias Ringwald     log_info("hci_transport_slip: send %u bytes", pos);
180*6ae10533SMatthias Ringwald     btstack_uart->send_block(slip_outgoing_buffer, pos);
181*6ae10533SMatthias Ringwald }
182*6ae10533SMatthias Ringwald 
183*6ae10533SMatthias Ringwald static inline void hci_transport_slip_send_next_chunk(void){
184*6ae10533SMatthias Ringwald     hci_transport_slip_encode_chunk_and_send(0);
185*6ae10533SMatthias Ringwald }
186*6ae10533SMatthias Ringwald 
187bd021c4eSMatthias Ringwald // format: 0xc0 HEADER PACKER 0xc0
188bd021c4eSMatthias Ringwald // @param uint8_t header[4]
189bd021c4eSMatthias Ringwald static void hci_transport_slip_send_frame(const uint8_t * header, const uint8_t * packet, uint16_t packet_size){
190bd021c4eSMatthias Ringwald 
191bd021c4eSMatthias Ringwald     int pos = 0;
192bd021c4eSMatthias Ringwald 
193bd021c4eSMatthias Ringwald     // Start of Frame
194bd021c4eSMatthias Ringwald     slip_outgoing_buffer[pos++] = BTSTACK_SLIP_SOF;
195bd021c4eSMatthias Ringwald 
196bd021c4eSMatthias Ringwald     // Header
197bd021c4eSMatthias Ringwald     btstack_slip_encoder_start(header, 4);
198bd021c4eSMatthias Ringwald     while (btstack_slip_encoder_has_data()){
199bd021c4eSMatthias Ringwald         slip_outgoing_buffer[pos++] = btstack_slip_encoder_get_byte();
200bd021c4eSMatthias Ringwald     }
201bd021c4eSMatthias Ringwald 
202bd021c4eSMatthias Ringwald     // Packet
203bd021c4eSMatthias Ringwald     btstack_slip_encoder_start(packet, packet_size);
204bd021c4eSMatthias Ringwald 
205*6ae10533SMatthias Ringwald     // Fill rest of chunk from packet and send
206*6ae10533SMatthias Ringwald     hci_transport_slip_encode_chunk_and_send(pos);
207bd021c4eSMatthias Ringwald }
208bd021c4eSMatthias Ringwald 
209bd021c4eSMatthias Ringwald // SLIP Incoming
210bd021c4eSMatthias Ringwald 
211bd021c4eSMatthias Ringwald static void hci_transport_slip_init(void){
212bd021c4eSMatthias Ringwald     btstack_slip_decoder_init(&hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], 6 + HCI_PACKET_BUFFER_SIZE);
213bd021c4eSMatthias Ringwald }
214bd021c4eSMatthias Ringwald 
215bd021c4eSMatthias Ringwald // H5 Three-Wire Implementation
216bd021c4eSMatthias Ringwald 
217bd021c4eSMatthias Ringwald static void hci_transport_link_calc_header(uint8_t * header,
218bd021c4eSMatthias Ringwald     uint8_t  sequence_nr,
219bd021c4eSMatthias Ringwald     uint8_t  acknowledgement_nr,
220bd021c4eSMatthias Ringwald     uint8_t  data_integrity_check_present,
221bd021c4eSMatthias Ringwald     uint8_t  reliable_packet,
222bd021c4eSMatthias Ringwald     uint8_t  packet_type,
223bd021c4eSMatthias Ringwald     uint16_t payload_length){
224bd021c4eSMatthias Ringwald 
225bd021c4eSMatthias Ringwald     // reset data integrity flag
226bd021c4eSMatthias Ringwald     if (data_integrity_check_present){
227bd021c4eSMatthias Ringwald         log_error("hci_transport_link_calc_header: data integrity not supported, dropping flag");
228bd021c4eSMatthias Ringwald         data_integrity_check_present = 0;
229bd021c4eSMatthias Ringwald     }
230bd021c4eSMatthias Ringwald 
231bd021c4eSMatthias Ringwald     header[0] = sequence_nr | (acknowledgement_nr << 3) | (data_integrity_check_present << 6) | (reliable_packet << 7);
232bd021c4eSMatthias Ringwald     header[1] = packet_type | ((payload_length & 0x0f) << 4);
233bd021c4eSMatthias Ringwald     header[2] = payload_length >> 4;
234bd021c4eSMatthias Ringwald     header[3] = 0xff - (header[0] + header[1] + header[2]);
235bd021c4eSMatthias Ringwald }
236bd021c4eSMatthias Ringwald 
237bd021c4eSMatthias Ringwald static void hci_transport_link_send_control(const uint8_t * message, int message_len){
238bd021c4eSMatthias Ringwald     uint8_t header[4];
239bd021c4eSMatthias Ringwald     hci_transport_link_calc_header(header, 0, 0, 0, 0, LINK_CONTROL_PACKET_TYPE, message_len);
240bd021c4eSMatthias Ringwald     hci_transport_slip_send_frame(header, message, message_len);
241bd021c4eSMatthias Ringwald }
242bd021c4eSMatthias Ringwald 
243bd021c4eSMatthias Ringwald static void hci_transport_link_send_sync(void){
244bd021c4eSMatthias Ringwald     log_info("link: send sync");
245bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_sync, sizeof(link_control_sync));
246bd021c4eSMatthias Ringwald }
247bd021c4eSMatthias Ringwald 
248bd021c4eSMatthias Ringwald static void hci_transport_link_send_sync_response(void){
249bd021c4eSMatthias Ringwald     log_info("link: send sync response");
250bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_sync_response, sizeof(link_control_sync_response));
251bd021c4eSMatthias Ringwald }
252bd021c4eSMatthias Ringwald 
253bd021c4eSMatthias Ringwald static void hci_transport_link_send_config(void){
254bd021c4eSMatthias Ringwald     log_info("link: send config");
255bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_config, sizeof(link_control_config));
256bd021c4eSMatthias Ringwald }
257bd021c4eSMatthias Ringwald 
258bd021c4eSMatthias Ringwald static void hci_transport_link_send_config_response(void){
259bd021c4eSMatthias Ringwald     log_info("link: send config response");
260bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_config_response, sizeof(link_control_config_response));
261bd021c4eSMatthias Ringwald }
262bd021c4eSMatthias Ringwald 
263bd021c4eSMatthias Ringwald static void hci_transport_link_send_woken(void){
264bd021c4eSMatthias Ringwald     log_info("link: send woken");
265bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_woken, sizeof(link_control_woken));
266bd021c4eSMatthias Ringwald }
267bd021c4eSMatthias Ringwald 
268bd021c4eSMatthias Ringwald static void hci_transport_link_send_wakeup(void){
269bd021c4eSMatthias Ringwald     log_info("link: send wakeup");
270bd021c4eSMatthias Ringwald     hci_transport_link_send_control(link_control_wakeup, sizeof(link_control_wakeup));
271bd021c4eSMatthias Ringwald }
272bd021c4eSMatthias Ringwald 
27394764e99SMatthias Ringwald static void hci_transport_link_send_sleep(void){
27494764e99SMatthias Ringwald     log_info("link: send sleep");
27594764e99SMatthias Ringwald     hci_transport_link_send_control(link_control_sleep, sizeof(link_control_sleep));
27694764e99SMatthias Ringwald }
27794764e99SMatthias Ringwald 
278bd021c4eSMatthias Ringwald static void hci_transport_link_send_queued_packet(void){
279bd021c4eSMatthias Ringwald     log_info("hci_transport_link_send_queued_packet: seq %u, ack %u, size %u", link_seq_nr, link_ack_nr, hci_packet_size);
280bd021c4eSMatthias Ringwald     log_info_hexdump(hci_packet, hci_packet_size);
281bd021c4eSMatthias Ringwald 
282bd021c4eSMatthias Ringwald     uint8_t header[4];
283bd021c4eSMatthias Ringwald     hci_transport_link_calc_header(header, link_seq_nr, link_ack_nr, 0, 1, hci_packet_type, hci_packet_size);
284bd021c4eSMatthias Ringwald     hci_transport_slip_send_frame(header, hci_packet, hci_packet_size);
28594764e99SMatthias Ringwald 
28694764e99SMatthias Ringwald     // reset inactvitiy timer
28794764e99SMatthias Ringwald     hci_transport_inactivity_timer_set();
288bd021c4eSMatthias Ringwald }
289bd021c4eSMatthias Ringwald 
290bd021c4eSMatthias Ringwald static void hci_transport_link_send_ack_packet(void){
291bd021c4eSMatthias Ringwald     log_info("link: send ack %u", link_ack_nr);
292bd021c4eSMatthias Ringwald     uint8_t header[4];
293bd021c4eSMatthias Ringwald     hci_transport_link_calc_header(header, 0, link_ack_nr, 0, 0, LINK_ACKNOWLEDGEMENT_TYPE, 0);
294bd021c4eSMatthias Ringwald     hci_transport_slip_send_frame(header, NULL, 0);
295bd021c4eSMatthias Ringwald }
296bd021c4eSMatthias Ringwald 
297bd021c4eSMatthias Ringwald static void hci_transport_link_run(void){
298bd021c4eSMatthias Ringwald     // exit if outgoing active
299*6ae10533SMatthias Ringwald     if (slip_write_active) return;
300bd021c4eSMatthias Ringwald 
301bd021c4eSMatthias Ringwald     // process queued requests
302bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SYNC){
303bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SYNC;
304bd021c4eSMatthias Ringwald         hci_transport_link_send_sync();
305bd021c4eSMatthias Ringwald         return;
306bd021c4eSMatthias Ringwald     }
307bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE){
308bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE;
309bd021c4eSMatthias Ringwald         hci_transport_link_send_sync_response();
310bd021c4eSMatthias Ringwald         return;
311bd021c4eSMatthias Ringwald     }
312bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_CONFIG){
313bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_CONFIG;
314bd021c4eSMatthias Ringwald         hci_transport_link_send_config();
315bd021c4eSMatthias Ringwald         return;
316bd021c4eSMatthias Ringwald     }
317bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE){
318bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE;
319bd021c4eSMatthias Ringwald         hci_transport_link_send_config_response();
320bd021c4eSMatthias Ringwald         return;
321bd021c4eSMatthias Ringwald     }
322bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_WOKEN){
323bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_WOKEN;
324bd021c4eSMatthias Ringwald         hci_transport_link_send_woken();
325bd021c4eSMatthias Ringwald         return;
326bd021c4eSMatthias Ringwald     }
327bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_WAKEUP){
328bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_WAKEUP;
329bd021c4eSMatthias Ringwald         hci_transport_link_send_wakeup();
330bd021c4eSMatthias Ringwald         return;
331bd021c4eSMatthias Ringwald     }
332bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET){
333bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET;
334bd021c4eSMatthias Ringwald         // packet already contains ack, no need to send addtitional one
335bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_ACK_PACKET;
336bd021c4eSMatthias Ringwald         hci_transport_link_send_queued_packet();
337bd021c4eSMatthias Ringwald         return;
338bd021c4eSMatthias Ringwald     }
339bd021c4eSMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_ACK_PACKET){
340bd021c4eSMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_ACK_PACKET;
341bd021c4eSMatthias Ringwald         hci_transport_link_send_ack_packet();
342bd021c4eSMatthias Ringwald         return;
343bd021c4eSMatthias Ringwald     }
34494764e99SMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SLEEP){
34594764e99SMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SLEEP;
34694764e99SMatthias Ringwald         hci_transport_link_actions |=  HCI_TRANSPORT_LINK_ENTER_SLEEP;
34794764e99SMatthias Ringwald         link_peer_asleep = 1;
34894764e99SMatthias Ringwald         hci_transport_link_send_sleep();
34994764e99SMatthias Ringwald         return;
35094764e99SMatthias Ringwald     }
351bd021c4eSMatthias Ringwald }
352bd021c4eSMatthias Ringwald 
353bd021c4eSMatthias Ringwald static void hci_transport_link_set_timer(uint16_t timeout_ms){
354bd021c4eSMatthias Ringwald     btstack_run_loop_set_timer_handler(&link_timer, &hci_transport_link_timeout_handler);
355bd021c4eSMatthias Ringwald     btstack_run_loop_set_timer(&link_timer, timeout_ms);
356bd021c4eSMatthias Ringwald     btstack_run_loop_add_timer(&link_timer);
357bd021c4eSMatthias Ringwald }
358bd021c4eSMatthias Ringwald 
359bd021c4eSMatthias Ringwald static void hci_transport_link_timeout_handler(btstack_timer_source_t * timer){
360bd021c4eSMatthias Ringwald     switch (link_state){
361bd021c4eSMatthias Ringwald         case LINK_UNINITIALIZED:
362bd021c4eSMatthias Ringwald             hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC;
363bd021c4eSMatthias Ringwald             hci_transport_link_set_timer(LINK_PERIOD_MS);
364bd021c4eSMatthias Ringwald             break;
365bd021c4eSMatthias Ringwald         case LINK_INITIALIZED:
366bd021c4eSMatthias Ringwald             hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG;
367bd021c4eSMatthias Ringwald             hci_transport_link_set_timer(LINK_PERIOD_MS);
368bd021c4eSMatthias Ringwald             break;
369bd021c4eSMatthias Ringwald         case LINK_ACTIVE:
370bd021c4eSMatthias Ringwald             if (!hci_transport_link_have_outgoing_packet()){
371bd021c4eSMatthias Ringwald                 log_info("h5 timeout while active, but no outgoing packet");
372bd021c4eSMatthias Ringwald                 return;
373bd021c4eSMatthias Ringwald             }
374bd021c4eSMatthias Ringwald             if (link_peer_asleep){
375bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WAKEUP;
376bd021c4eSMatthias Ringwald                 hci_transport_link_set_timer(LINK_WAKEUP_MS);
377bd021c4eSMatthias Ringwald                 return;
378bd021c4eSMatthias Ringwald             }
379bd021c4eSMatthias Ringwald             // resend packet
380bd021c4eSMatthias Ringwald             hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET;
381bd021c4eSMatthias Ringwald             hci_transport_link_set_timer(link_resend_timeout_ms);
382bd021c4eSMatthias Ringwald             break;
383bd021c4eSMatthias Ringwald         default:
384bd021c4eSMatthias Ringwald             break;
385bd021c4eSMatthias Ringwald     }
386bd021c4eSMatthias Ringwald 
387bd021c4eSMatthias Ringwald     hci_transport_link_run();
388bd021c4eSMatthias Ringwald }
389bd021c4eSMatthias Ringwald 
390bd021c4eSMatthias Ringwald static void hci_transport_link_init(void){
391bd021c4eSMatthias Ringwald     link_state = LINK_UNINITIALIZED;
392bd021c4eSMatthias Ringwald     link_peer_asleep = 0;
393bd021c4eSMatthias Ringwald 
394bd021c4eSMatthias Ringwald     // get started
395bd021c4eSMatthias Ringwald     hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC;
396bd021c4eSMatthias Ringwald     hci_transport_link_set_timer(LINK_PERIOD_MS);
397bd021c4eSMatthias Ringwald     hci_transport_link_run();
398bd021c4eSMatthias Ringwald }
399bd021c4eSMatthias Ringwald 
400bd021c4eSMatthias Ringwald static int hci_transport_link_inc_seq_nr(int seq_nr){
401bd021c4eSMatthias Ringwald     return (seq_nr + 1) & 0x07;
402bd021c4eSMatthias Ringwald }
403bd021c4eSMatthias Ringwald 
404bd021c4eSMatthias Ringwald static int hci_transport_link_have_outgoing_packet(void){
405bd021c4eSMatthias Ringwald     return hci_packet != 0;
406bd021c4eSMatthias Ringwald }
407bd021c4eSMatthias Ringwald 
408bd021c4eSMatthias Ringwald static void hci_transport_link_clear_queue(void){
409bd021c4eSMatthias Ringwald     btstack_run_loop_remove_timer(&link_timer);
410bd021c4eSMatthias Ringwald     hci_packet = NULL;
411bd021c4eSMatthias Ringwald }
412bd021c4eSMatthias Ringwald 
413bd021c4eSMatthias Ringwald static void hci_transport_h5_queue_packet(uint8_t packet_type, uint8_t *packet, int size){
414bd021c4eSMatthias Ringwald     hci_packet = packet;
415bd021c4eSMatthias Ringwald     hci_packet_type = packet_type;
416bd021c4eSMatthias Ringwald     hci_packet_size = size;
417bd021c4eSMatthias Ringwald }
418bd021c4eSMatthias Ringwald 
419bd021c4eSMatthias Ringwald static void hci_transport_h5_process_frame(uint16_t frame_size){
420bd021c4eSMatthias Ringwald 
421bd021c4eSMatthias Ringwald     if (frame_size < 4) return;
422bd021c4eSMatthias Ringwald 
423bd021c4eSMatthias Ringwald     uint8_t * slip_header  = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE];
424bd021c4eSMatthias Ringwald     uint8_t * slip_payload = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 4];
425bd021c4eSMatthias Ringwald     int       frame_size_without_header = frame_size - 4;
426bd021c4eSMatthias Ringwald 
427bd021c4eSMatthias Ringwald     int      seq_nr =  slip_header[0] & 0x07;
428bd021c4eSMatthias Ringwald     int      ack_nr = (slip_header[0] >> 3)    & 0x07;
429bd021c4eSMatthias Ringwald     int      data_integrity_check_present = (slip_header[0] & 0x40) != 0;
430bd021c4eSMatthias Ringwald     int      reliable_packet  = (slip_header[0] & 0x80) != 0;
431bd021c4eSMatthias Ringwald     uint8_t  link_packet_type = slip_header[1] & 0x0f;
432bd021c4eSMatthias Ringwald     uint16_t link_payload_len = (slip_header[1] >> 4) | (slip_header[2] << 4);
433bd021c4eSMatthias Ringwald 
434bd021c4eSMatthias Ringwald     log_info("hci_transport_h5_process_frame, reliable %u, packet type %u, seq_nr %u, ack_nr %u", reliable_packet, link_packet_type, seq_nr, ack_nr);
435bd021c4eSMatthias Ringwald     log_info_hexdump(slip_header, 4);
436bd021c4eSMatthias Ringwald     log_info_hexdump(slip_payload, frame_size_without_header);
437bd021c4eSMatthias Ringwald 
438bd021c4eSMatthias Ringwald     // CSR 8811 does not seem to auto-detect H5 mode and sends data with even parity.
439bd021c4eSMatthias Ringwald     // if this byte sequence is detected, just enable even parity
440bd021c4eSMatthias Ringwald     const uint8_t sync_response_bcsp[] = {0x01, 0x7a, 0x06, 0x10};
441bd021c4eSMatthias Ringwald     if (memcmp(sync_response_bcsp, slip_header, 4) == 0){
442bd021c4eSMatthias Ringwald         log_info("h5: detected BSCP SYNC sent with Even Parity -> discard frame and enable Even Parity");
443bd021c4eSMatthias Ringwald         btstack_uart->set_parity(1);
444bd021c4eSMatthias Ringwald         return;
445bd021c4eSMatthias Ringwald     }
446bd021c4eSMatthias Ringwald 
447bd021c4eSMatthias Ringwald     // validate header checksum
448bd021c4eSMatthias Ringwald     uint8_t header_checksum = slip_header[0] + slip_header[1] + slip_header[2] + slip_header[3];
449bd021c4eSMatthias Ringwald     if (header_checksum != 0xff){
450bd021c4eSMatthias Ringwald         log_info("h5: header checksum 0x%02x (instead of 0xff)", header_checksum);
451bd021c4eSMatthias Ringwald         return;
452bd021c4eSMatthias Ringwald     }
453bd021c4eSMatthias Ringwald 
454bd021c4eSMatthias Ringwald     // validate payload length
455bd021c4eSMatthias Ringwald     int data_integrity_len = data_integrity_check_present ? 2 : 0;
456bd021c4eSMatthias Ringwald     uint16_t received_payload_len = frame_size_without_header - data_integrity_len;
457bd021c4eSMatthias Ringwald     if (link_payload_len != received_payload_len){
458bd021c4eSMatthias Ringwald         log_info("h5: expected payload len %u but got %u", link_payload_len, received_payload_len);
459bd021c4eSMatthias Ringwald         return;
460bd021c4eSMatthias Ringwald     }
461bd021c4eSMatthias Ringwald 
462bd021c4eSMatthias Ringwald     // (TODO data integrity check)
463bd021c4eSMatthias Ringwald 
464bd021c4eSMatthias Ringwald     switch (link_state){
465bd021c4eSMatthias Ringwald         case LINK_UNINITIALIZED:
466bd021c4eSMatthias Ringwald             if (link_packet_type != LINK_CONTROL_PACKET_TYPE) break;
467bd021c4eSMatthias Ringwald             if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){
468bd021c4eSMatthias Ringwald                 log_info("link: received sync");
469bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE;
470bd021c4eSMatthias Ringwald             }
471bd021c4eSMatthias Ringwald             if (memcmp(slip_payload, link_control_sync_response, sizeof(link_control_sync_response)) == 0){
472bd021c4eSMatthias Ringwald                 log_info("link: received sync response");
473bd021c4eSMatthias Ringwald                 link_state = LINK_INITIALIZED;
474bd021c4eSMatthias Ringwald                 btstack_run_loop_remove_timer(&link_timer);
475bd021c4eSMatthias Ringwald                 log_info("link initialized");
476bd021c4eSMatthias Ringwald                 //
477bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG;
478bd021c4eSMatthias Ringwald                 hci_transport_link_set_timer(LINK_PERIOD_MS);
479bd021c4eSMatthias Ringwald             }
480bd021c4eSMatthias Ringwald             break;
481bd021c4eSMatthias Ringwald         case LINK_INITIALIZED:
482bd021c4eSMatthias Ringwald             if (link_packet_type != LINK_CONTROL_PACKET_TYPE) break;
483bd021c4eSMatthias Ringwald             if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){
484bd021c4eSMatthias Ringwald                 log_info("link: received sync");
485bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE;
486bd021c4eSMatthias Ringwald             }
487cbc04f48SMatthias Ringwald             if (memcmp(slip_payload, link_control_config, link_control_config_prefix_len) == 0){
488bd021c4eSMatthias Ringwald                 log_info("link: received config");
489bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE;
490bd021c4eSMatthias Ringwald             }
491bd021c4eSMatthias Ringwald             if (memcmp(slip_payload, link_control_config_response, link_control_config_response_prefix_len) == 0){
492bd021c4eSMatthias Ringwald                 log_info("link: received config response");
493bd021c4eSMatthias Ringwald                 link_state = LINK_ACTIVE;
494bd021c4eSMatthias Ringwald                 btstack_run_loop_remove_timer(&link_timer);
495bd021c4eSMatthias Ringwald                 log_info("link activated");
496bd021c4eSMatthias Ringwald                 //
497bd021c4eSMatthias Ringwald                 link_seq_nr = 0;
498bd021c4eSMatthias Ringwald                 link_ack_nr = 0;
499bd021c4eSMatthias Ringwald                 // notify upper stack that it can start
500bd021c4eSMatthias Ringwald                 uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
501bd021c4eSMatthias Ringwald                 packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
502bd021c4eSMatthias Ringwald             }
503bd021c4eSMatthias Ringwald             break;
504bd021c4eSMatthias Ringwald         case LINK_ACTIVE:
505bd021c4eSMatthias Ringwald 
506bd021c4eSMatthias Ringwald             // validate packet sequence nr in reliable packets (check for out of sequence error)
507bd021c4eSMatthias Ringwald             if (reliable_packet){
508bd021c4eSMatthias Ringwald                 if (seq_nr != link_ack_nr){
509bd021c4eSMatthias Ringwald                     log_info("expected seq nr %u, but received %u", link_ack_nr, seq_nr);
510bd021c4eSMatthias Ringwald                     hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_ACK_PACKET;
511bd021c4eSMatthias Ringwald                     break;
512bd021c4eSMatthias Ringwald                 }
513bd021c4eSMatthias Ringwald                 // ack packet right away
514bd021c4eSMatthias Ringwald                 link_ack_nr = hci_transport_link_inc_seq_nr(link_ack_nr);
515bd021c4eSMatthias Ringwald                 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_ACK_PACKET;
516bd021c4eSMatthias Ringwald             }
517bd021c4eSMatthias Ringwald 
518bd021c4eSMatthias Ringwald             // Process ACKs in reliable packet and explicit ack packets
519bd021c4eSMatthias Ringwald             if (reliable_packet || link_packet_type == LINK_ACKNOWLEDGEMENT_TYPE){
520bd021c4eSMatthias Ringwald                 // our packet is good if the remote expects our seq nr + 1
521bd021c4eSMatthias Ringwald                 int next_seq_nr = hci_transport_link_inc_seq_nr(link_seq_nr);
522bd021c4eSMatthias Ringwald                 if (hci_transport_link_have_outgoing_packet() && next_seq_nr == ack_nr){
523bd021c4eSMatthias Ringwald                     log_info("h5: outoing packet with seq %u ack'ed", link_seq_nr);
524bd021c4eSMatthias Ringwald                     link_seq_nr = next_seq_nr;
525bd021c4eSMatthias Ringwald                     hci_transport_link_clear_queue();
526bd021c4eSMatthias Ringwald 
527bd021c4eSMatthias Ringwald                     // notify upper stack that it can send again
528bd021c4eSMatthias Ringwald                     uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
529bd021c4eSMatthias Ringwald                     packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
530bd021c4eSMatthias Ringwald                 }
531bd021c4eSMatthias Ringwald             }
532bd021c4eSMatthias Ringwald 
533bd021c4eSMatthias Ringwald             switch (link_packet_type){
534bd021c4eSMatthias Ringwald                 case LINK_CONTROL_PACKET_TYPE:
535bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_config, sizeof(link_control_config)) == 0){
536bd021c4eSMatthias Ringwald                         log_info("link: received config");
537bd021c4eSMatthias Ringwald                         hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE;
538bd021c4eSMatthias Ringwald                         break;
539bd021c4eSMatthias Ringwald                     }
540bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){
541bd021c4eSMatthias Ringwald                         log_info("link: received sync in ACTIVE STATE!");
542bd021c4eSMatthias Ringwald                         // TODO sync during active indicates peer reset -> full upper layer reset necessary
543bd021c4eSMatthias Ringwald                         break;
544bd021c4eSMatthias Ringwald                     }
545bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_sleep, sizeof(link_control_sleep)) == 0){
546d8e28fa3SMatthias Ringwald                         if (btstack_uart_sleep_mode){
547d8e28fa3SMatthias Ringwald                             log_info("link: received sleep message. Enabling UART Sleep.");
548d8e28fa3SMatthias Ringwald                             btstack_uart->set_sleep(btstack_uart_sleep_mode);
549d8e28fa3SMatthias Ringwald                         } else {
550d8e28fa3SMatthias Ringwald                             log_info("link: received sleep message. UART Sleep not supported");
551d8e28fa3SMatthias Ringwald                         }
552bd021c4eSMatthias Ringwald                         link_peer_asleep = 1;
553bd021c4eSMatthias Ringwald                         break;
554bd021c4eSMatthias Ringwald                     }
555bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_wakeup, sizeof(link_control_wakeup)) == 0){
556bd021c4eSMatthias Ringwald                         log_info("link: received wakupe message -> send woken");
557bd021c4eSMatthias Ringwald                         link_peer_asleep = 0;
558bd021c4eSMatthias Ringwald                         hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WOKEN;
559bd021c4eSMatthias Ringwald                         break;
560bd021c4eSMatthias Ringwald                     }
561bd021c4eSMatthias Ringwald                     if (memcmp(slip_payload, link_control_woken, sizeof(link_control_woken)) == 0){
562bd021c4eSMatthias Ringwald                         log_info("link: received woken message");
563bd021c4eSMatthias Ringwald                         link_peer_asleep = 0;
56494764e99SMatthias Ringwald                         // queued packet will be sent in hci_transport_link_run if needed
565bd021c4eSMatthias Ringwald                         break;
566bd021c4eSMatthias Ringwald                     }
567bd021c4eSMatthias Ringwald                     break;
568bd021c4eSMatthias Ringwald                 case HCI_EVENT_PACKET:
569bd021c4eSMatthias Ringwald                 case HCI_ACL_DATA_PACKET:
570bd021c4eSMatthias Ringwald                 case HCI_SCO_DATA_PACKET:
57194764e99SMatthias Ringwald                     // seems like peer is awake
57294764e99SMatthias Ringwald                     link_peer_asleep = 0;
57394764e99SMatthias Ringwald                     // forward packet to stack
574bd021c4eSMatthias Ringwald                     packet_handler(link_packet_type, slip_payload, link_payload_len);
57594764e99SMatthias Ringwald                     // reset inactvitiy timer
57694764e99SMatthias Ringwald                     hci_transport_inactivity_timer_set();
577bd021c4eSMatthias Ringwald                     break;
578bd021c4eSMatthias Ringwald             }
579bd021c4eSMatthias Ringwald 
580bd021c4eSMatthias Ringwald             break;
581bd021c4eSMatthias Ringwald         default:
582bd021c4eSMatthias Ringwald             break;
583bd021c4eSMatthias Ringwald     }
584bd021c4eSMatthias Ringwald 
585bd021c4eSMatthias Ringwald     hci_transport_link_run();
586bd021c4eSMatthias Ringwald }
587bd021c4eSMatthias Ringwald 
588bd021c4eSMatthias Ringwald // recommendet time until resend: 3 * time of largest packet
589bd021c4eSMatthias Ringwald static uint16_t hci_transport_link_calc_resend_timeout(uint32_t baudrate){
590bd021c4eSMatthias Ringwald     uint32_t max_packet_size_in_bit = (HCI_PACKET_BUFFER_SIZE + 6) << 3;
591bd021c4eSMatthias Ringwald     uint32_t t_max_x3_ms = max_packet_size_in_bit * 3000 / baudrate;
592bd021c4eSMatthias Ringwald     log_info("resend timeout for %u baud: %u ms", baudrate, t_max_x3_ms);
593bd021c4eSMatthias Ringwald     return t_max_x3_ms;
594bd021c4eSMatthias Ringwald }
595bd021c4eSMatthias Ringwald 
596bd021c4eSMatthias Ringwald static void hci_transport_link_update_resend_timeout(uint32_t baudrate){
597bd021c4eSMatthias Ringwald     link_resend_timeout_ms = hci_transport_link_calc_resend_timeout(baudrate);
598bd021c4eSMatthias Ringwald }
599bd021c4eSMatthias Ringwald 
600bd021c4eSMatthias Ringwald /// H5 Interface
601bd021c4eSMatthias Ringwald 
602bd021c4eSMatthias Ringwald static uint8_t hci_transport_link_read_byte;
603bd021c4eSMatthias Ringwald 
604bd021c4eSMatthias Ringwald static void hci_transport_h5_read_next_byte(void){
605cbc04f48SMatthias Ringwald     log_debug("h5: rx nxt");
606bd021c4eSMatthias Ringwald     btstack_uart->receive_block(&hci_transport_link_read_byte, 1);
607bd021c4eSMatthias Ringwald }
608bd021c4eSMatthias Ringwald 
609bd021c4eSMatthias Ringwald static void hci_transport_h5_block_received(){
610cbc04f48SMatthias Ringwald     log_debug("slip: process 0x%02x", hci_transport_link_read_byte);
611bd021c4eSMatthias Ringwald     btstack_slip_decoder_process(hci_transport_link_read_byte);
612bd021c4eSMatthias Ringwald     uint16_t frame_size = btstack_slip_decoder_frame_size();
613bd021c4eSMatthias Ringwald     if (frame_size) {
614bd021c4eSMatthias Ringwald         hci_transport_h5_process_frame(frame_size);
615bd021c4eSMatthias Ringwald         hci_transport_slip_init();
616bd021c4eSMatthias Ringwald     }
617bd021c4eSMatthias Ringwald     hci_transport_h5_read_next_byte();
618bd021c4eSMatthias Ringwald }
619bd021c4eSMatthias Ringwald 
620bd021c4eSMatthias Ringwald static void hci_transport_h5_block_sent(void){
621*6ae10533SMatthias Ringwald 
622*6ae10533SMatthias Ringwald     // check if more data to send
623*6ae10533SMatthias Ringwald     if (btstack_slip_encoder_has_data()){
624*6ae10533SMatthias Ringwald         hci_transport_slip_send_next_chunk();
625*6ae10533SMatthias Ringwald         return;
626*6ae10533SMatthias Ringwald     }
627*6ae10533SMatthias Ringwald 
628*6ae10533SMatthias Ringwald     // done
629*6ae10533SMatthias Ringwald     slip_write_active = 0;
63094764e99SMatthias Ringwald 
63194764e99SMatthias Ringwald     // enter sleep mode after sending sleep message
63294764e99SMatthias Ringwald     if (hci_transport_link_actions & HCI_TRANSPORT_LINK_ENTER_SLEEP){
63394764e99SMatthias Ringwald         hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_ENTER_SLEEP;
63494764e99SMatthias Ringwald         if (btstack_uart_sleep_mode){
63594764e99SMatthias Ringwald             log_info("link: sent sleep message. Enabling UART Sleep.");
63694764e99SMatthias Ringwald             btstack_uart->set_sleep(btstack_uart_sleep_mode);
63794764e99SMatthias Ringwald         } else {
63894764e99SMatthias Ringwald             log_info("link: sent sleep message. UART Sleep not supported");
63994764e99SMatthias Ringwald         }
64094764e99SMatthias Ringwald     }
64194764e99SMatthias Ringwald 
642bd021c4eSMatthias Ringwald     hci_transport_link_run();
643bd021c4eSMatthias Ringwald }
644bd021c4eSMatthias Ringwald 
645bd021c4eSMatthias Ringwald static void hci_transport_h5_init(const void * transport_config){
646bd021c4eSMatthias Ringwald     // check for hci_transport_config_uart_t
647bd021c4eSMatthias Ringwald     if (!transport_config) {
648bd021c4eSMatthias Ringwald         log_error("hci_transport_h5: no config!");
649bd021c4eSMatthias Ringwald         return;
650bd021c4eSMatthias Ringwald     }
651bd021c4eSMatthias Ringwald     if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) {
652bd021c4eSMatthias Ringwald         log_error("hci_transport_h5: config not of type != HCI_TRANSPORT_CONFIG_UART!");
653bd021c4eSMatthias Ringwald         return;
654bd021c4eSMatthias Ringwald     }
655bd021c4eSMatthias Ringwald 
656bd021c4eSMatthias Ringwald     // extract UART config from transport config
657bd021c4eSMatthias Ringwald     hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config;
658bd021c4eSMatthias Ringwald     uart_config.baudrate    = hci_transport_config_uart->baudrate_init;
659bd021c4eSMatthias Ringwald     uart_config.flowcontrol = hci_transport_config_uart->flowcontrol;
660bd021c4eSMatthias Ringwald     uart_config.device_name = hci_transport_config_uart->device_name;
661bd021c4eSMatthias Ringwald 
662bd021c4eSMatthias Ringwald     // setup UART driver
663bd021c4eSMatthias Ringwald     btstack_uart->init(&uart_config);
664bd021c4eSMatthias Ringwald     btstack_uart->set_block_received(&hci_transport_h5_block_received);
665bd021c4eSMatthias Ringwald     btstack_uart->set_block_sent(&hci_transport_h5_block_sent);
666bd021c4eSMatthias Ringwald }
667bd021c4eSMatthias Ringwald 
668bd021c4eSMatthias Ringwald static int hci_transport_h5_open(void){
669bd021c4eSMatthias Ringwald     int res = btstack_uart->open();
670bd021c4eSMatthias Ringwald     if (res){
671bd021c4eSMatthias Ringwald         return res;
672bd021c4eSMatthias Ringwald     }
673bd021c4eSMatthias Ringwald 
674d8e28fa3SMatthias Ringwald     // check if wake on RX can be used
675d8e28fa3SMatthias Ringwald     btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF;
676d8e28fa3SMatthias Ringwald     int supported_sleep_modes = 0;
677d8e28fa3SMatthias Ringwald     if (btstack_uart->get_supported_sleep_modes){
678d8e28fa3SMatthias Ringwald         supported_sleep_modes = btstack_uart->get_supported_sleep_modes();
679d8e28fa3SMatthias Ringwald     }
680d8e28fa3SMatthias Ringwald     if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){
681d8e28fa3SMatthias Ringwald         log_info("H5: using wake on RX");
682d8e28fa3SMatthias Ringwald         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE;
683d8e28fa3SMatthias Ringwald     } else {
684d8e28fa3SMatthias Ringwald         log_info("H5: UART driver does not provide compatible sleep mode");
685d8e28fa3SMatthias Ringwald     }
686d8e28fa3SMatthias Ringwald 
687bd021c4eSMatthias Ringwald     // setup resend timeout
688bd021c4eSMatthias Ringwald     hci_transport_link_update_resend_timeout(uart_config.baudrate);
689bd021c4eSMatthias Ringwald 
690bd021c4eSMatthias Ringwald     // init slip parser state machine
691bd021c4eSMatthias Ringwald     hci_transport_slip_init();
692bd021c4eSMatthias Ringwald 
693bd021c4eSMatthias Ringwald     // init link management - already starts syncing
694bd021c4eSMatthias Ringwald     hci_transport_link_init();
695bd021c4eSMatthias Ringwald 
696bd021c4eSMatthias Ringwald     // start receiving
697bd021c4eSMatthias Ringwald     hci_transport_h5_read_next_byte();
698bd021c4eSMatthias Ringwald 
699bd021c4eSMatthias Ringwald     return 0;
700bd021c4eSMatthias Ringwald }
701bd021c4eSMatthias Ringwald 
702bd021c4eSMatthias Ringwald static int hci_transport_h5_close(void){
703bd021c4eSMatthias Ringwald     return btstack_uart->close();
704bd021c4eSMatthias Ringwald     return 0;
705bd021c4eSMatthias Ringwald }
706bd021c4eSMatthias Ringwald 
707bd021c4eSMatthias Ringwald static void hci_transport_h5_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
708bd021c4eSMatthias Ringwald     packet_handler = handler;
709bd021c4eSMatthias Ringwald }
710bd021c4eSMatthias Ringwald 
711bd021c4eSMatthias Ringwald static int hci_transport_h5_can_send_packet_now(uint8_t packet_type){
71294764e99SMatthias Ringwald     int res = !hci_transport_link_have_outgoing_packet() && link_state == LINK_ACTIVE;
713*6ae10533SMatthias Ringwald     // log_info("hci_transport_h5_can_send_packet_now: %u", res);
71494764e99SMatthias Ringwald     return res;
715bd021c4eSMatthias Ringwald }
716bd021c4eSMatthias Ringwald 
717bd021c4eSMatthias Ringwald static int hci_transport_h5_send_packet(uint8_t packet_type, uint8_t *packet, int size){
718bd021c4eSMatthias Ringwald     if (!hci_transport_h5_can_send_packet_now(packet_type)){
719bd021c4eSMatthias Ringwald         log_error("hci_transport_h5_send_packet called but in state %u", link_state);
720bd021c4eSMatthias Ringwald         return -1;
721bd021c4eSMatthias Ringwald     }
722bd021c4eSMatthias Ringwald 
723bd021c4eSMatthias Ringwald     // store request
724bd021c4eSMatthias Ringwald     hci_transport_h5_queue_packet(packet_type, packet, size);
725bd021c4eSMatthias Ringwald 
726bd021c4eSMatthias Ringwald     // send wakeup first
727bd021c4eSMatthias Ringwald     if (link_peer_asleep){
728d8e28fa3SMatthias Ringwald         if (btstack_uart_sleep_mode){
729d8e28fa3SMatthias Ringwald             log_info("h5: disable UART sleep");
730d8e28fa3SMatthias Ringwald             btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
731d8e28fa3SMatthias Ringwald         }
732bd021c4eSMatthias Ringwald         hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WAKEUP;
733bd021c4eSMatthias Ringwald         hci_transport_link_set_timer(LINK_WAKEUP_MS);
734bd021c4eSMatthias Ringwald     } else {
735bd021c4eSMatthias Ringwald         hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET;
736bd021c4eSMatthias Ringwald         hci_transport_link_set_timer(link_resend_timeout_ms);
737bd021c4eSMatthias Ringwald     }
738bd021c4eSMatthias Ringwald     hci_transport_link_run();
739bd021c4eSMatthias Ringwald     return 0;
740bd021c4eSMatthias Ringwald }
741bd021c4eSMatthias Ringwald 
742bd021c4eSMatthias Ringwald static int hci_transport_h5_set_baudrate(uint32_t baudrate){
743bd021c4eSMatthias Ringwald 
744bd021c4eSMatthias Ringwald     log_info("hci_transport_h5_set_baudrate %u", baudrate);
745bd021c4eSMatthias Ringwald     int res = btstack_uart->set_baudrate(baudrate);
746bd021c4eSMatthias Ringwald 
747bd021c4eSMatthias Ringwald     if (res) return res;
748bd021c4eSMatthias Ringwald     hci_transport_link_update_resend_timeout(baudrate);
749bd021c4eSMatthias Ringwald     return 0;
750bd021c4eSMatthias Ringwald }
751bd021c4eSMatthias Ringwald 
752bd021c4eSMatthias Ringwald static void hci_transport_h5_reset_link(void){
753bd021c4eSMatthias Ringwald 
754bd021c4eSMatthias Ringwald     log_info("hci_transport_h5_reset_link");
755bd021c4eSMatthias Ringwald 
756bd021c4eSMatthias Ringwald     // clear outgoing queue
757bd021c4eSMatthias Ringwald     hci_transport_link_clear_queue();
758bd021c4eSMatthias Ringwald 
759bd021c4eSMatthias Ringwald     // init slip parser state machine
760bd021c4eSMatthias Ringwald     hci_transport_slip_init();
761bd021c4eSMatthias Ringwald 
762bd021c4eSMatthias Ringwald     // init link management - already starts syncing
763bd021c4eSMatthias Ringwald     hci_transport_link_init();
764bd021c4eSMatthias Ringwald }
765bd021c4eSMatthias Ringwald 
766bd021c4eSMatthias Ringwald static const hci_transport_t hci_transport_h5 = {
767bd021c4eSMatthias Ringwald     /* const char * name; */                                        "H5",
768bd021c4eSMatthias Ringwald     /* void   (*init) (const void *transport_config); */            &hci_transport_h5_init,
769bd021c4eSMatthias Ringwald     /* int    (*open)(void); */                                     &hci_transport_h5_open,
770bd021c4eSMatthias Ringwald     /* int    (*close)(void); */                                    &hci_transport_h5_close,
771bd021c4eSMatthias Ringwald     /* void   (*register_packet_handler)(void (*handler)(...); */   &hci_transport_h5_register_packet_handler,
772bd021c4eSMatthias Ringwald     /* int    (*can_send_packet_now)(uint8_t packet_type); */       &hci_transport_h5_can_send_packet_now,
773bd021c4eSMatthias Ringwald     /* int    (*send_packet)(...); */                               &hci_transport_h5_send_packet,
774bd021c4eSMatthias Ringwald     /* int    (*set_baudrate)(uint32_t baudrate); */                &hci_transport_h5_set_baudrate,
775bd021c4eSMatthias Ringwald     /* void   (*reset_link)(void); */                               &hci_transport_h5_reset_link,
776bd021c4eSMatthias Ringwald };
777bd021c4eSMatthias Ringwald 
778bd021c4eSMatthias Ringwald // configure and return h5 singleton
779bd021c4eSMatthias Ringwald const hci_transport_t * hci_transport_h5_instance(const btstack_uart_block_t * uart_driver) {
780bd021c4eSMatthias Ringwald     btstack_uart = uart_driver;
781bd021c4eSMatthias Ringwald     return &hci_transport_h5;
782bd021c4eSMatthias Ringwald }
78394764e99SMatthias Ringwald 
78494764e99SMatthias Ringwald void hci_transport_h5_set_auto_sleep(uint16_t inactivity_timeout_ms){
78594764e99SMatthias Ringwald     link_inactivity_timeout_ms = inactivity_timeout_ms;
78694764e99SMatthias Ringwald }
787