xref: /btstack/src/hci_transport_h4.c (revision 39e7ee9f5df7b74450032df357daa94e932e3106)
1bd021c4eSMatthias Ringwald /*
2bd021c4eSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3bd021c4eSMatthias Ringwald  *
4bd021c4eSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5bd021c4eSMatthias Ringwald  * modification, are permitted provided that the following conditions
6bd021c4eSMatthias Ringwald  * are met:
7bd021c4eSMatthias Ringwald  *
8bd021c4eSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9bd021c4eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10bd021c4eSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11bd021c4eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12bd021c4eSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13bd021c4eSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14bd021c4eSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15bd021c4eSMatthias Ringwald  *    from this software without specific prior written permission.
16bd021c4eSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17bd021c4eSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18bd021c4eSMatthias Ringwald  *    monetary gain.
19bd021c4eSMatthias Ringwald  *
20bd021c4eSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21bd021c4eSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22bd021c4eSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23bd021c4eSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24bd021c4eSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25bd021c4eSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26bd021c4eSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27bd021c4eSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28bd021c4eSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29bd021c4eSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30bd021c4eSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31bd021c4eSMatthias Ringwald  * SUCH DAMAGE.
32bd021c4eSMatthias Ringwald  *
33bd021c4eSMatthias Ringwald  * Please inquire about commercial licensing options at
34bd021c4eSMatthias Ringwald  * [email protected]
35bd021c4eSMatthias Ringwald  *
36bd021c4eSMatthias Ringwald  */
37bd021c4eSMatthias Ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hci_transport_h4.c"
39ab2c6ae4SMatthias Ringwald 
40bd021c4eSMatthias Ringwald /*
41bd021c4eSMatthias Ringwald  *  hci_h4_transport.c
42bd021c4eSMatthias Ringwald  *
438a23fc53SMatthias Ringwald  *  HCI Transport API implementation for H4 protocol over POSIX with optional support for eHCILL
44bd021c4eSMatthias Ringwald  *
45bd021c4eSMatthias Ringwald  *  Created by Matthias Ringwald on 4/29/09.
46bd021c4eSMatthias Ringwald  */
47bd021c4eSMatthias Ringwald 
48bd021c4eSMatthias Ringwald #include "btstack_config.h"
49bd021c4eSMatthias Ringwald 
50bd021c4eSMatthias Ringwald #include "btstack_debug.h"
51bd021c4eSMatthias Ringwald #include "hci.h"
52bd021c4eSMatthias Ringwald #include "hci_transport.h"
53*39e7ee9fSMatthias Ringwald #include "bluetooth_company_id.h"
54bd021c4eSMatthias Ringwald #include "btstack_uart_block.h"
55bd021c4eSMatthias Ringwald 
569ecd1ba8SMatthias Ringwald #define ENABLE_LOG_EHCILL
579ecd1ba8SMatthias Ringwald 
58f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
59307a4fe3SMatthias Ringwald 
60307a4fe3SMatthias Ringwald // eHCILL commands
619ecd1ba8SMatthias Ringwald static const uint8_t EHCILL_GO_TO_SLEEP_IND = 0x030;
629ecd1ba8SMatthias Ringwald static const uint8_t EHCILL_GO_TO_SLEEP_ACK = 0x031;
639ecd1ba8SMatthias Ringwald static const uint8_t EHCILL_WAKE_UP_IND     = 0x032;
649ecd1ba8SMatthias Ringwald static const uint8_t EHCILL_WAKE_UP_ACK     = 0x033;
65307a4fe3SMatthias Ringwald 
668a23fc53SMatthias Ringwald static int  hci_transport_h4_ehcill_outgoing_packet_ready(void);
678a23fc53SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void);
688a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action);
698a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void);
708a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void);
718a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void);
72307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void);
73307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void);
74307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void);
758a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void);
76307a4fe3SMatthias Ringwald 
77307a4fe3SMatthias Ringwald typedef enum {
789ecd1ba8SMatthias Ringwald     EHCILL_STATE_W2_SEND_SLEEP_ACK,
79307a4fe3SMatthias Ringwald     EHCILL_STATE_SLEEP,
809ecd1ba8SMatthias Ringwald     EHCILL_STATE_W4_WAKEUP_IND_OR_ACK,
81307a4fe3SMatthias Ringwald     EHCILL_STATE_AWAKE
82307a4fe3SMatthias Ringwald } EHCILL_STATE;
83307a4fe3SMatthias Ringwald 
84307a4fe3SMatthias Ringwald // eHCILL state machine
85307a4fe3SMatthias Ringwald static EHCILL_STATE ehcill_state;
86307a4fe3SMatthias Ringwald static uint8_t      ehcill_command_to_send;
87307a4fe3SMatthias Ringwald 
88f6a20ec9SMatthias Ringwald static btstack_uart_sleep_mode_t btstack_uart_sleep_mode;
89f6a20ec9SMatthias Ringwald 
90307a4fe3SMatthias Ringwald // work around for eHCILL problem
91307a4fe3SMatthias Ringwald static btstack_timer_source_t ehcill_sleep_ack_timer;
92307a4fe3SMatthias Ringwald 
93bd021c4eSMatthias Ringwald #endif
94bd021c4eSMatthias Ringwald 
9582d05f16SMatthias Ringwald 
96bd021c4eSMatthias Ringwald // assert pre-buffer for packet type is available
97bd021c4eSMatthias Ringwald #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0)
98bd021c4eSMatthias Ringwald #error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h
99bd021c4eSMatthias Ringwald #endif
100bd021c4eSMatthias Ringwald 
101bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
102bd021c4eSMatthias Ringwald 
103bd021c4eSMatthias Ringwald typedef enum {
104bd021c4eSMatthias Ringwald     H4_W4_PACKET_TYPE,
105bd021c4eSMatthias Ringwald     H4_W4_EVENT_HEADER,
106bd021c4eSMatthias Ringwald     H4_W4_ACL_HEADER,
107bd021c4eSMatthias Ringwald     H4_W4_SCO_HEADER,
108bd021c4eSMatthias Ringwald     H4_W4_PAYLOAD,
109bd021c4eSMatthias Ringwald } H4_STATE;
110bd021c4eSMatthias Ringwald 
111307a4fe3SMatthias Ringwald typedef enum {
112307a4fe3SMatthias Ringwald     TX_IDLE = 1,
113307a4fe3SMatthias Ringwald     TX_W4_PACKET_SENT,
114f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
115307a4fe3SMatthias Ringwald     TX_W4_WAKEUP,
116307a4fe3SMatthias Ringwald     TX_W2_EHCILL_SEND,
117307a4fe3SMatthias Ringwald     TX_W4_EHCILL_SENT,
118307a4fe3SMatthias Ringwald #endif
119307a4fe3SMatthias Ringwald } TX_STATE;
120307a4fe3SMatthias Ringwald 
121bd021c4eSMatthias Ringwald // UART Driver + Config
122bd021c4eSMatthias Ringwald static const btstack_uart_block_t * btstack_uart;
123bd021c4eSMatthias Ringwald static btstack_uart_config_t uart_config;
124bd021c4eSMatthias Ringwald 
125307a4fe3SMatthias Ringwald // write state
1268a23fc53SMatthias Ringwald static TX_STATE tx_state;
1278a23fc53SMatthias Ringwald static uint8_t * tx_data;
128307a4fe3SMatthias Ringwald static uint16_t  tx_len;   // 0 == no outgoing packet
1298a23fc53SMatthias Ringwald 
130307a4fe3SMatthias Ringwald static uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
131bd021c4eSMatthias Ringwald 
132bd021c4eSMatthias Ringwald static  void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler;
133bd021c4eSMatthias Ringwald 
134bd021c4eSMatthias Ringwald // packet reader state machine
135bd021c4eSMatthias Ringwald static  H4_STATE h4_state;
136bd021c4eSMatthias Ringwald static int bytes_to_read;
137bd021c4eSMatthias Ringwald static int read_pos;
138bd021c4eSMatthias Ringwald 
139bd021c4eSMatthias Ringwald // incoming packet buffer
140bd021c4eSMatthias Ringwald static uint8_t hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 1 + HCI_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, event header + event data)
141bd021c4eSMatthias Ringwald static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE];
142bd021c4eSMatthias Ringwald 
143*39e7ee9fSMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
144*39e7ee9fSMatthias Ringwald static const uint8_t local_version_event_prefix[] = { 0x04, 0x0e, 0x0c, 0x01, 0x01, 0x10};
145*39e7ee9fSMatthias Ringwald static const uint8_t baud_rate_command_prefix[]   = { 0x01, 0x36, 0xff, 0x04};
146*39e7ee9fSMatthias Ringwald static enum {
147*39e7ee9fSMatthias Ringwald     CC256X_WORKAROUND_IDLE,
148*39e7ee9fSMatthias Ringwald     CC256X_WORKAROUND_CHIPSET_DETECTED,
149*39e7ee9fSMatthias Ringwald     CC256X_WORKAROUND_BAUDRATE_COMMAND_SENT,
150*39e7ee9fSMatthias Ringwald     CC256X_WORKAROUND_DONE
151*39e7ee9fSMatthias Ringwald } cc256x_workaround_state;
152*39e7ee9fSMatthias Ringwald #endif
153*39e7ee9fSMatthias Ringwald 
154bd021c4eSMatthias Ringwald static int hci_transport_h4_set_baudrate(uint32_t baudrate){
155bd021c4eSMatthias Ringwald     log_info("hci_transport_h4_set_baudrate %u", baudrate);
156bd021c4eSMatthias Ringwald     return btstack_uart->set_baudrate(baudrate);
157bd021c4eSMatthias Ringwald }
158bd021c4eSMatthias Ringwald 
159bd021c4eSMatthias Ringwald static void hci_transport_h4_reset_statemachine(void){
160bd021c4eSMatthias Ringwald     h4_state = H4_W4_PACKET_TYPE;
161bd021c4eSMatthias Ringwald     read_pos = 0;
162bd021c4eSMatthias Ringwald     bytes_to_read = 1;
163bd021c4eSMatthias Ringwald }
164bd021c4eSMatthias Ringwald 
165bd021c4eSMatthias Ringwald static void hci_transport_h4_trigger_next_read(void){
1668a23fc53SMatthias Ringwald     // log_info("hci_transport_h4_trigger_next_read: %u bytes", bytes_to_read);
167bd021c4eSMatthias Ringwald     btstack_uart->receive_block(&hci_packet[read_pos], bytes_to_read);
168bd021c4eSMatthias Ringwald }
169bd021c4eSMatthias Ringwald 
170bd021c4eSMatthias Ringwald static void hci_transport_h4_block_read(void){
171bd021c4eSMatthias Ringwald 
172bd021c4eSMatthias Ringwald     read_pos += bytes_to_read;
173bd021c4eSMatthias Ringwald 
174bd021c4eSMatthias Ringwald     switch (h4_state) {
175bd021c4eSMatthias Ringwald         case H4_W4_PACKET_TYPE:
176bd021c4eSMatthias Ringwald             switch (hci_packet[0]){
177bd021c4eSMatthias Ringwald                 case HCI_EVENT_PACKET:
178bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_EVENT_HEADER_SIZE;
179bd021c4eSMatthias Ringwald                     h4_state = H4_W4_EVENT_HEADER;
180bd021c4eSMatthias Ringwald                     break;
181bd021c4eSMatthias Ringwald                 case HCI_ACL_DATA_PACKET:
182bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_ACL_HEADER_SIZE;
183bd021c4eSMatthias Ringwald                     h4_state = H4_W4_ACL_HEADER;
184bd021c4eSMatthias Ringwald                     break;
185bd021c4eSMatthias Ringwald                 case HCI_SCO_DATA_PACKET:
186bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_SCO_HEADER_SIZE;
187bd021c4eSMatthias Ringwald                     h4_state = H4_W4_SCO_HEADER;
188bd021c4eSMatthias Ringwald                     break;
189f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
190307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_IND:
191307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_ACK:
192307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
193307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_ACK:
1948a23fc53SMatthias Ringwald                     hci_transport_h4_ehcill_handle_command(hci_packet[0]);
1958a23fc53SMatthias Ringwald                     hci_transport_h4_reset_statemachine();
196307a4fe3SMatthias Ringwald                     break;
197307a4fe3SMatthias Ringwald #endif
198bd021c4eSMatthias Ringwald                 default:
1998a23fc53SMatthias Ringwald                     log_error("hci_transport_h4: invalid packet type 0x%02x", hci_packet[0]);
200bd021c4eSMatthias Ringwald                     hci_transport_h4_reset_statemachine();
201bd021c4eSMatthias Ringwald                     break;
202bd021c4eSMatthias Ringwald             }
203bd021c4eSMatthias Ringwald             break;
204bd021c4eSMatthias Ringwald 
205bd021c4eSMatthias Ringwald         case H4_W4_EVENT_HEADER:
206bd021c4eSMatthias Ringwald             bytes_to_read = hci_packet[2];
207bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
208bd021c4eSMatthias Ringwald             break;
209bd021c4eSMatthias Ringwald 
210bd021c4eSMatthias Ringwald         case H4_W4_ACL_HEADER:
211bd021c4eSMatthias Ringwald             bytes_to_read = little_endian_read_16( hci_packet, 3);
212bd021c4eSMatthias Ringwald             // check ACL length
213bd021c4eSMatthias Ringwald             if (HCI_ACL_HEADER_SIZE + bytes_to_read >  HCI_PACKET_BUFFER_SIZE){
214f04a0c31SMatthias Ringwald                 log_error("hci_transport_h4: invalid ACL payload len %d - only space for %u", bytes_to_read, HCI_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE);
215bd021c4eSMatthias Ringwald                 hci_transport_h4_reset_statemachine();
216bd021c4eSMatthias Ringwald                 break;
217bd021c4eSMatthias Ringwald             }
218bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
219bd021c4eSMatthias Ringwald             break;
220bd021c4eSMatthias Ringwald 
221bd021c4eSMatthias Ringwald         case H4_W4_SCO_HEADER:
222bd021c4eSMatthias Ringwald             bytes_to_read = hci_packet[3];
223bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
224bd021c4eSMatthias Ringwald             break;
225bd021c4eSMatthias Ringwald 
226bd021c4eSMatthias Ringwald         case H4_W4_PAYLOAD:
227*39e7ee9fSMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
228*39e7ee9fSMatthias Ringwald             if (cc256x_workaround_state == CC256X_WORKAROUND_IDLE
229*39e7ee9fSMatthias Ringwald             && memcmp(hci_packet, local_version_event_prefix, sizeof(local_version_event_prefix)) == 0){
230*39e7ee9fSMatthias Ringwald                 if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
231*39e7ee9fSMatthias Ringwald                     // detect TI CC256x controller based on manufacturer
232*39e7ee9fSMatthias Ringwald                     log_info("Detected CC256x controller");
233*39e7ee9fSMatthias Ringwald                     cc256x_workaround_state = CC256X_WORKAROUND_CHIPSET_DETECTED;
234*39e7ee9fSMatthias Ringwald                 } else {
235*39e7ee9fSMatthias Ringwald                     // work around not needed
236*39e7ee9fSMatthias Ringwald                     log_info("Bluetooth controller not by TI");
237*39e7ee9fSMatthias Ringwald                     cc256x_workaround_state = CC256X_WORKAROUND_DONE;
238*39e7ee9fSMatthias Ringwald                 }
239*39e7ee9fSMatthias Ringwald             }
240*39e7ee9fSMatthias Ringwald #endif
241bd021c4eSMatthias Ringwald             packet_handler(hci_packet[0], &hci_packet[1], read_pos-1);
242bd021c4eSMatthias Ringwald             hci_transport_h4_reset_statemachine();
243bd021c4eSMatthias Ringwald             break;
244bd021c4eSMatthias Ringwald         default:
245bd021c4eSMatthias Ringwald             break;
246bd021c4eSMatthias Ringwald     }
247*39e7ee9fSMatthias Ringwald 
248*39e7ee9fSMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
249*39e7ee9fSMatthias Ringwald     if (cc256x_workaround_state == CC256X_WORKAROUND_BAUDRATE_COMMAND_SENT){
250*39e7ee9fSMatthias Ringwald         cc256x_workaround_state = CC256X_WORKAROUND_IDLE;
251*39e7ee9fSMatthias Ringwald         // avoid flowcontrol problem by reading expected hci command complete event of 7 bytes in a single block read
252*39e7ee9fSMatthias Ringwald         h4_state = H4_W4_PAYLOAD;
253*39e7ee9fSMatthias Ringwald         bytes_to_read = 7;
254*39e7ee9fSMatthias Ringwald     }
255*39e7ee9fSMatthias Ringwald #endif
256*39e7ee9fSMatthias Ringwald 
257bd021c4eSMatthias Ringwald     hci_transport_h4_trigger_next_read();
258bd021c4eSMatthias Ringwald }
259bd021c4eSMatthias Ringwald 
260307a4fe3SMatthias Ringwald static void hci_transport_h4_block_sent(void){
261307a4fe3SMatthias Ringwald     switch (tx_state){
262307a4fe3SMatthias Ringwald         case TX_W4_PACKET_SENT:
263307a4fe3SMatthias Ringwald             // packet fully sent, reset state
264307a4fe3SMatthias Ringwald             tx_len = 0;
2658a23fc53SMatthias Ringwald             tx_state = TX_IDLE;
266307a4fe3SMatthias Ringwald 
267f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
2688a23fc53SMatthias Ringwald             // notify eHCILL engine
2698a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_handle_packet_sent();
270307a4fe3SMatthias Ringwald #endif
271307a4fe3SMatthias Ringwald             // notify upper stack that it can send again
272307a4fe3SMatthias Ringwald             packet_handler(HCI_EVENT_PACKET, &packet_sent_event[0], sizeof(packet_sent_event));
273307a4fe3SMatthias Ringwald             break;
274307a4fe3SMatthias Ringwald 
275f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
2768a23fc53SMatthias Ringwald         case TX_W4_EHCILL_SENT:
2778a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_handle_ehcill_command_sent();
278307a4fe3SMatthias Ringwald             break;
279307a4fe3SMatthias Ringwald #endif
280307a4fe3SMatthias Ringwald 
281307a4fe3SMatthias Ringwald         default:
282307a4fe3SMatthias Ringwald             break;
283307a4fe3SMatthias Ringwald     }
284307a4fe3SMatthias Ringwald }
285307a4fe3SMatthias Ringwald 
286307a4fe3SMatthias Ringwald static int hci_transport_h4_can_send_now(uint8_t packet_type){
287307a4fe3SMatthias Ringwald     return tx_state == TX_IDLE;
288307a4fe3SMatthias Ringwald }
289307a4fe3SMatthias Ringwald 
290307a4fe3SMatthias Ringwald static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){
291*39e7ee9fSMatthias Ringwald 
292307a4fe3SMatthias Ringwald     // store packet type before actual data and increase size
293307a4fe3SMatthias Ringwald     size++;
294307a4fe3SMatthias Ringwald     packet--;
295307a4fe3SMatthias Ringwald     *packet = packet_type;
296307a4fe3SMatthias Ringwald 
297*39e7ee9fSMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
298*39e7ee9fSMatthias Ringwald     if ((cc256x_workaround_state == CC256X_WORKAROUND_CHIPSET_DETECTED)
299*39e7ee9fSMatthias Ringwald     && (memcmp(packet, baud_rate_command_prefix, sizeof(baud_rate_command_prefix)) == 0)) {
300*39e7ee9fSMatthias Ringwald         log_info("CC256x baud rate command detected, expect command complete event next");
301*39e7ee9fSMatthias Ringwald         cc256x_workaround_state = CC256X_WORKAROUND_BAUDRATE_COMMAND_SENT;
302*39e7ee9fSMatthias Ringwald     }
303*39e7ee9fSMatthias Ringwald #endif
304*39e7ee9fSMatthias Ringwald 
3058a23fc53SMatthias Ringwald     // store request
3068a23fc53SMatthias Ringwald     tx_len   = size;
3078a23fc53SMatthias Ringwald     tx_data  = packet;
308307a4fe3SMatthias Ringwald 
309f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
3109ecd1ba8SMatthias Ringwald     switch (ehcill_state){
3119ecd1ba8SMatthias Ringwald         case EHCILL_STATE_SLEEP:
3128a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_trigger_wakeup();
3138a23fc53SMatthias Ringwald             return 0;
3149ecd1ba8SMatthias Ringwald         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
3159ecd1ba8SMatthias Ringwald             return 0;
3169ecd1ba8SMatthias Ringwald         default:
3179ecd1ba8SMatthias Ringwald             break;
3188a23fc53SMatthias Ringwald     }
3198a23fc53SMatthias Ringwald #endif
3208a23fc53SMatthias Ringwald 
3218a23fc53SMatthias Ringwald     // start sending
3228a23fc53SMatthias Ringwald     tx_state = TX_W4_PACKET_SENT;
323307a4fe3SMatthias Ringwald     btstack_uart->send_block(packet, size);
324307a4fe3SMatthias Ringwald     return 0;
325307a4fe3SMatthias Ringwald }
326307a4fe3SMatthias Ringwald 
327bd021c4eSMatthias Ringwald static void hci_transport_h4_init(const void * transport_config){
328bd021c4eSMatthias Ringwald     // check for hci_transport_config_uart_t
329bd021c4eSMatthias Ringwald     if (!transport_config) {
330bd021c4eSMatthias Ringwald         log_error("hci_transport_h4: no config!");
331bd021c4eSMatthias Ringwald         return;
332bd021c4eSMatthias Ringwald     }
333bd021c4eSMatthias Ringwald     if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) {
334bd021c4eSMatthias Ringwald         log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!");
335bd021c4eSMatthias Ringwald         return;
336bd021c4eSMatthias Ringwald     }
337bd021c4eSMatthias Ringwald 
338bd021c4eSMatthias Ringwald     // extract UART config from transport config
339bd021c4eSMatthias Ringwald     hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config;
340bd021c4eSMatthias Ringwald     uart_config.baudrate    = hci_transport_config_uart->baudrate_init;
341bd021c4eSMatthias Ringwald     uart_config.flowcontrol = hci_transport_config_uart->flowcontrol;
342bd021c4eSMatthias Ringwald     uart_config.device_name = hci_transport_config_uart->device_name;
343bd021c4eSMatthias Ringwald 
344bd021c4eSMatthias Ringwald     // setup UART driver
345bd021c4eSMatthias Ringwald     btstack_uart->init(&uart_config);
346bd021c4eSMatthias Ringwald     btstack_uart->set_block_received(&hci_transport_h4_block_read);
347bd021c4eSMatthias Ringwald     btstack_uart->set_block_sent(&hci_transport_h4_block_sent);
348bd021c4eSMatthias Ringwald }
349bd021c4eSMatthias Ringwald 
350bd021c4eSMatthias Ringwald static int hci_transport_h4_open(void){
351bd021c4eSMatthias Ringwald     int res = btstack_uart->open();
352bd021c4eSMatthias Ringwald     if (res){
353bd021c4eSMatthias Ringwald         return res;
354bd021c4eSMatthias Ringwald     }
355bd021c4eSMatthias Ringwald     hci_transport_h4_reset_statemachine();
356bd021c4eSMatthias Ringwald     hci_transport_h4_trigger_next_read();
357307a4fe3SMatthias Ringwald 
358307a4fe3SMatthias Ringwald     tx_state = TX_IDLE;
359307a4fe3SMatthias Ringwald 
360f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
3618a23fc53SMatthias Ringwald     hci_transport_h4_ehcill_open();
362307a4fe3SMatthias Ringwald #endif
363bd021c4eSMatthias Ringwald     return 0;
364bd021c4eSMatthias Ringwald }
365bd021c4eSMatthias Ringwald 
366bd021c4eSMatthias Ringwald static int hci_transport_h4_close(void){
367bd021c4eSMatthias Ringwald     return btstack_uart->close();
368bd021c4eSMatthias Ringwald }
369bd021c4eSMatthias Ringwald 
370bd021c4eSMatthias Ringwald static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
371bd021c4eSMatthias Ringwald     packet_handler = handler;
372bd021c4eSMatthias Ringwald }
373bd021c4eSMatthias Ringwald 
374bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
375bd021c4eSMatthias Ringwald }
376bd021c4eSMatthias Ringwald 
3778a23fc53SMatthias Ringwald //
378307a4fe3SMatthias Ringwald // --- main part of eHCILL implementation ---
3798a23fc53SMatthias Ringwald //
380307a4fe3SMatthias Ringwald 
381f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
382307a4fe3SMatthias Ringwald 
38362ca45d7SMatthias Ringwald static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){
38462ca45d7SMatthias Ringwald     static int last_state = 0;
38562ca45d7SMatthias Ringwald     if (sleep_active == last_state) return;
38662ca45d7SMatthias Ringwald     last_state = sleep_active;
38762ca45d7SMatthias Ringwald 
38862ca45d7SMatthias Ringwald     log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active);
38962ca45d7SMatthias Ringwald     uint8_t event[3];
39062ca45d7SMatthias Ringwald     event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE;
39162ca45d7SMatthias Ringwald     event[1] = sizeof(event) - 2;
39262ca45d7SMatthias Ringwald     event[2] = sleep_active;
39362ca45d7SMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
39462ca45d7SMatthias Ringwald }
39562ca45d7SMatthias Ringwald 
3968a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void){
3978a23fc53SMatthias Ringwald     hci_transport_h4_ehcill_reset_statemachine();
3988a23fc53SMatthias Ringwald 
3998a23fc53SMatthias Ringwald     // find best sleep mode to use: wake on CTS, wake on RX, none
4008a23fc53SMatthias Ringwald     btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF;
4018a23fc53SMatthias Ringwald     int supported_sleep_modes = 0;
4028a23fc53SMatthias Ringwald     if (btstack_uart->get_supported_sleep_modes){
4038a23fc53SMatthias Ringwald         supported_sleep_modes = btstack_uart->get_supported_sleep_modes();
404307a4fe3SMatthias Ringwald     }
4058a23fc53SMatthias Ringwald     if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){
4068a23fc53SMatthias Ringwald         log_info("eHCILL: using wake on CTS");
4078a23fc53SMatthias Ringwald         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE;
4088a23fc53SMatthias Ringwald     } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){
4098a23fc53SMatthias Ringwald         log_info("eHCILL: using wake on RX");
4108a23fc53SMatthias Ringwald         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE;
4118a23fc53SMatthias Ringwald     } else {
4128a23fc53SMatthias Ringwald         log_info("eHCILL: UART driver does not provide compatible sleep mode");
4138a23fc53SMatthias Ringwald     }
414307a4fe3SMatthias Ringwald }
415307a4fe3SMatthias Ringwald 
416307a4fe3SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void){
4179ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
4189ecd1ba8SMatthias Ringwald     log_info("eHCILL: send WAKEUP_IND");
4199ecd1ba8SMatthias Ringwald #endif
420307a4fe3SMatthias Ringwald     // update state
421307a4fe3SMatthias Ringwald     tx_state     = TX_W4_WAKEUP;
4229ecd1ba8SMatthias Ringwald     ehcill_state = EHCILL_STATE_W4_WAKEUP_IND_OR_ACK;
423307a4fe3SMatthias Ringwald     ehcill_command_to_send = EHCILL_WAKE_UP_IND;
424307a4fe3SMatthias Ringwald     btstack_uart->send_block(&ehcill_command_to_send, 1);
425307a4fe3SMatthias Ringwald }
426307a4fe3SMatthias Ringwald 
427307a4fe3SMatthias Ringwald static int hci_transport_h4_ehcill_outgoing_packet_ready(void){
428307a4fe3SMatthias Ringwald     return tx_len != 0;
429307a4fe3SMatthias Ringwald }
430307a4fe3SMatthias Ringwald 
431307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void){
432307a4fe3SMatthias Ringwald     ehcill_state = EHCILL_STATE_AWAKE;
433307a4fe3SMatthias Ringwald }
434307a4fe3SMatthias Ringwald 
435307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void){
4369ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
4379ecd1ba8SMatthias Ringwald     log_info("eHCILL: send command %02x", ehcill_command_to_send);
4389ecd1ba8SMatthias Ringwald #endif
439307a4fe3SMatthias Ringwald     tx_state = TX_W4_EHCILL_SENT;
4409ecd1ba8SMatthias Ringwald     if (ehcill_command_to_send == EHCILL_GO_TO_SLEEP_ACK){
4419ecd1ba8SMatthias Ringwald         ehcill_state = EHCILL_STATE_SLEEP;
4429ecd1ba8SMatthias Ringwald     }
443307a4fe3SMatthias Ringwald     btstack_uart->send_block(&ehcill_command_to_send, 1);
444307a4fe3SMatthias Ringwald }
445307a4fe3SMatthias Ringwald 
446307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){
4479ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
4489ecd1ba8SMatthias Ringwald     log_info("eHCILL: timer triggered");
4499ecd1ba8SMatthias Ringwald #endif
450307a4fe3SMatthias Ringwald     hci_transport_h4_ehcill_send_ehcill_command();
451307a4fe3SMatthias Ringwald }
452307a4fe3SMatthias Ringwald 
453307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){
454307a4fe3SMatthias Ringwald     // setup timer
4559ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
4569ecd1ba8SMatthias Ringwald     log_info("eHCILL: set timer for sending command %02x", ehcill_command_to_send);
4579ecd1ba8SMatthias Ringwald #endif
4588a23fc53SMatthias Ringwald     btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler);
459307a4fe3SMatthias Ringwald     btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50);
460307a4fe3SMatthias Ringwald     btstack_run_loop_add_timer(&ehcill_sleep_ack_timer);
461307a4fe3SMatthias Ringwald }
462307a4fe3SMatthias Ringwald 
4638a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void){
4648a23fc53SMatthias Ringwald     switch (tx_state){
4658a23fc53SMatthias Ringwald         case TX_W2_EHCILL_SEND:
4668a23fc53SMatthias Ringwald         case TX_W4_EHCILL_SENT:
4678a23fc53SMatthias Ringwald             // wake up / sleep ack in progress, nothing to do now
4688a23fc53SMatthias Ringwald             return;
4698a23fc53SMatthias Ringwald         case TX_IDLE:
4708a23fc53SMatthias Ringwald         default:
4718a23fc53SMatthias Ringwald             // all clear, prepare for wakeup
4728a23fc53SMatthias Ringwald             break;
4738a23fc53SMatthias Ringwald     }
4748a23fc53SMatthias Ringwald     // UART needed again
47562ca45d7SMatthias Ringwald     hci_transport_h4_ehcill_emit_sleep_state(0);
4768a23fc53SMatthias Ringwald     if (btstack_uart_sleep_mode){
4778a23fc53SMatthias Ringwald         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
4788a23fc53SMatthias Ringwald     }
4798a23fc53SMatthias Ringwald     hci_transport_h4_echill_send_wakeup_ind();
4808a23fc53SMatthias Ringwald }
4818a23fc53SMatthias Ringwald 
4829ecd1ba8SMatthias Ringwald static void hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command){
4839ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
4849ecd1ba8SMatthias Ringwald     log_info("eHCILL: schedule eHCILL command %02x", command);
4859ecd1ba8SMatthias Ringwald #endif
486307a4fe3SMatthias Ringwald     ehcill_command_to_send = command;
487307a4fe3SMatthias Ringwald     switch (tx_state){
488307a4fe3SMatthias Ringwald         case TX_IDLE:
489307a4fe3SMatthias Ringwald             if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){
490307a4fe3SMatthias Ringwald                 // send right away
491307a4fe3SMatthias Ringwald                 hci_transport_h4_ehcill_send_ehcill_command();
492307a4fe3SMatthias Ringwald             } else {
493307a4fe3SMatthias Ringwald                 // change state so BTstack cannot send and setup timer
494307a4fe3SMatthias Ringwald                 tx_state = TX_W2_EHCILL_SEND;
495307a4fe3SMatthias Ringwald                 hci_transport_h4_ehcill_sleep_ack_timer_setup();
496307a4fe3SMatthias Ringwald             }
497307a4fe3SMatthias Ringwald             break;
498307a4fe3SMatthias Ringwald         default:
499307a4fe3SMatthias Ringwald             break;
500307a4fe3SMatthias Ringwald     }
501307a4fe3SMatthias Ringwald }
502307a4fe3SMatthias Ringwald 
5038a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action){
504307a4fe3SMatthias Ringwald     // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size);
505307a4fe3SMatthias Ringwald     switch(ehcill_state){
506307a4fe3SMatthias Ringwald         case EHCILL_STATE_AWAKE:
507307a4fe3SMatthias Ringwald             switch(action){
508307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_IND:
5099ecd1ba8SMatthias Ringwald                     ehcill_state = EHCILL_STATE_W2_SEND_SLEEP_ACK;
5109ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5119ecd1ba8SMatthias Ringwald                     log_info("eHCILL: Received GO_TO_SLEEP_IND RX");
5129ecd1ba8SMatthias Ringwald #endif
5139ecd1ba8SMatthias Ringwald                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_GO_TO_SLEEP_ACK);
514307a4fe3SMatthias Ringwald                     break;
515307a4fe3SMatthias Ringwald                 default:
516307a4fe3SMatthias Ringwald                     break;
517307a4fe3SMatthias Ringwald             }
518307a4fe3SMatthias Ringwald             break;
519307a4fe3SMatthias Ringwald 
520307a4fe3SMatthias Ringwald         case EHCILL_STATE_SLEEP:
5219ecd1ba8SMatthias Ringwald         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
522307a4fe3SMatthias Ringwald             switch(action){
523307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
524307a4fe3SMatthias Ringwald                     ehcill_state = EHCILL_STATE_AWAKE;
52562ca45d7SMatthias Ringwald                     hci_transport_h4_ehcill_emit_sleep_state(0);
5269ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5279ecd1ba8SMatthias Ringwald                     log_info("eHCILL: Received WAKE_UP_IND RX");
5289ecd1ba8SMatthias Ringwald #endif
5299ecd1ba8SMatthias Ringwald                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK);
530307a4fe3SMatthias Ringwald                     break;
531307a4fe3SMatthias Ringwald 
532307a4fe3SMatthias Ringwald                 default:
533307a4fe3SMatthias Ringwald                     break;
534307a4fe3SMatthias Ringwald             }
535307a4fe3SMatthias Ringwald             break;
536307a4fe3SMatthias Ringwald 
5379ecd1ba8SMatthias Ringwald         case EHCILL_STATE_W4_WAKEUP_IND_OR_ACK:
538307a4fe3SMatthias Ringwald             switch(action){
539307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
540307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_ACK:
5419ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5429ecd1ba8SMatthias Ringwald                     log_info("eHCILL: Received WAKE_UP (%02x)", action);
5439ecd1ba8SMatthias Ringwald #endif
544307a4fe3SMatthias Ringwald                     tx_state = TX_W4_PACKET_SENT;
545307a4fe3SMatthias Ringwald                     ehcill_state = EHCILL_STATE_AWAKE;
546307a4fe3SMatthias Ringwald                     btstack_uart->send_block(tx_data, tx_len);
547307a4fe3SMatthias Ringwald                     break;
548307a4fe3SMatthias Ringwald                 default:
549307a4fe3SMatthias Ringwald                     break;
550307a4fe3SMatthias Ringwald             }
551307a4fe3SMatthias Ringwald             break;
552307a4fe3SMatthias Ringwald     }
553307a4fe3SMatthias Ringwald }
5548a23fc53SMatthias Ringwald 
5558a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void){
5569ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5579ecd1ba8SMatthias Ringwald         log_info("eHCILL: handle packet sent, command to send %02x", ehcill_command_to_send);
5589ecd1ba8SMatthias Ringwald #endif
5598a23fc53SMatthias Ringwald     // now, send pending ehcill command if neccessary
5608a23fc53SMatthias Ringwald     switch (ehcill_command_to_send){
5618a23fc53SMatthias Ringwald         case EHCILL_GO_TO_SLEEP_ACK:
5628a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_sleep_ack_timer_setup();
5638a23fc53SMatthias Ringwald             break;
5648a23fc53SMatthias Ringwald         case EHCILL_WAKE_UP_IND:
5658a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_send_ehcill_command();
5668a23fc53SMatthias Ringwald             break;
5678a23fc53SMatthias Ringwald         default:
5688a23fc53SMatthias Ringwald             break;
5698a23fc53SMatthias Ringwald     }
5708a23fc53SMatthias Ringwald }
5718a23fc53SMatthias Ringwald 
5728a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){
5738a23fc53SMatthias Ringwald     tx_state = TX_IDLE;
5748a23fc53SMatthias Ringwald     int command = ehcill_command_to_send;
5758a23fc53SMatthias Ringwald     ehcill_command_to_send = 0;
5769ecd1ba8SMatthias Ringwald 
5779ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5789ecd1ba8SMatthias Ringwald         log_info("eHCILL: handle eHCILL sent, command was %02x", command);
5799ecd1ba8SMatthias Ringwald #endif
5809ecd1ba8SMatthias Ringwald 
5818a23fc53SMatthias Ringwald     if (command == EHCILL_GO_TO_SLEEP_ACK) {
5829ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5838a23fc53SMatthias Ringwald         log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode");
5849ecd1ba8SMatthias Ringwald #endif
5858a23fc53SMatthias Ringwald         // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent
5868a23fc53SMatthias Ringwald         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
5878a23fc53SMatthias Ringwald             btstack_uart->set_sleep(btstack_uart_sleep_mode);
5888a23fc53SMatthias Ringwald         }
58962ca45d7SMatthias Ringwald         hci_transport_h4_ehcill_emit_sleep_state(1);
5908a23fc53SMatthias Ringwald     }
5918a23fc53SMatthias Ringwald     // already packet ready? then start wakeup
5928a23fc53SMatthias Ringwald     if (hci_transport_h4_ehcill_outgoing_packet_ready()){
59362ca45d7SMatthias Ringwald         hci_transport_h4_ehcill_emit_sleep_state(0);
5948a23fc53SMatthias Ringwald         if (btstack_uart_sleep_mode){
5958a23fc53SMatthias Ringwald             btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
5968a23fc53SMatthias Ringwald         }
5979ecd1ba8SMatthias Ringwald         if (command != EHCILL_WAKE_UP_IND){
5988a23fc53SMatthias Ringwald             hci_transport_h4_echill_send_wakeup_ind();
5998a23fc53SMatthias Ringwald         }
6008a23fc53SMatthias Ringwald     }
6019ecd1ba8SMatthias Ringwald }
6028a23fc53SMatthias Ringwald 
603307a4fe3SMatthias Ringwald #endif
604307a4fe3SMatthias Ringwald // --- end of eHCILL implementation ---------
605307a4fe3SMatthias Ringwald 
606bd021c4eSMatthias Ringwald static const hci_transport_t hci_transport_h4 = {
607bd021c4eSMatthias Ringwald     /* const char * name; */                                        "H4",
608bd021c4eSMatthias Ringwald     /* void   (*init) (const void *transport_config); */            &hci_transport_h4_init,
609bd021c4eSMatthias Ringwald     /* int    (*open)(void); */                                     &hci_transport_h4_open,
610bd021c4eSMatthias Ringwald     /* int    (*close)(void); */                                    &hci_transport_h4_close,
611bd021c4eSMatthias Ringwald     /* void   (*register_packet_handler)(void (*handler)(...); */   &hci_transport_h4_register_packet_handler,
612bd021c4eSMatthias Ringwald     /* int    (*can_send_packet_now)(uint8_t packet_type); */       &hci_transport_h4_can_send_now,
613bd021c4eSMatthias Ringwald     /* int    (*send_packet)(...); */                               &hci_transport_h4_send_packet,
614bd021c4eSMatthias Ringwald     /* int    (*set_baudrate)(uint32_t baudrate); */                &hci_transport_h4_set_baudrate,
615bd021c4eSMatthias Ringwald     /* void   (*reset_link)(void); */                               NULL,
616ee752bb8SMatthias Ringwald     /* void   (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL,
617bd021c4eSMatthias Ringwald };
618bd021c4eSMatthias Ringwald 
619bd021c4eSMatthias Ringwald // configure and return h4 singleton
620bd021c4eSMatthias Ringwald const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) {
621bd021c4eSMatthias Ringwald     btstack_uart = uart_driver;
622bd021c4eSMatthias Ringwald     return &hci_transport_h4;
623bd021c4eSMatthias Ringwald }
624