xref: /btstack/src/hci_transport_h4.c (revision 7741d2d0723f163597ad5845ffd7c2387d278ea1)
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 
38e501bae0SMatthias 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 
48bace42efSMatthias Ringwald #include <inttypes.h>
49bace42efSMatthias Ringwald 
50bd021c4eSMatthias Ringwald #include "btstack_config.h"
51bd021c4eSMatthias Ringwald 
52bd021c4eSMatthias Ringwald #include "btstack_debug.h"
53bd021c4eSMatthias Ringwald #include "hci.h"
54bd021c4eSMatthias Ringwald #include "hci_transport.h"
5539e7ee9fSMatthias Ringwald #include "bluetooth_company_id.h"
56bd021c4eSMatthias Ringwald #include "btstack_uart_block.h"
57bd021c4eSMatthias Ringwald 
589ecd1ba8SMatthias Ringwald #define ENABLE_LOG_EHCILL
599ecd1ba8SMatthias Ringwald 
60f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
61307a4fe3SMatthias Ringwald 
62307a4fe3SMatthias Ringwald // eHCILL commands
63c4e86052SMatthias Ringwald enum EHCILL_MESSAGES {
64c4e86052SMatthias Ringwald 	EHCILL_GO_TO_SLEEP_IND = 0x030,
65c4e86052SMatthias Ringwald 	EHCILL_GO_TO_SLEEP_ACK = 0x031,
66c4e86052SMatthias Ringwald 	EHCILL_WAKE_UP_IND     = 0x032,
67c4e86052SMatthias Ringwald 	EHCILL_WAKE_UP_ACK     = 0x033,
68b403028eSMatthias Ringwald 	EHCILL_WAKEUP_SIGNAL   = 0x034,
69c4e86052SMatthias Ringwald };
70307a4fe3SMatthias Ringwald 
718a23fc53SMatthias Ringwald static int  hci_transport_h4_ehcill_outgoing_packet_ready(void);
728a23fc53SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void);
738a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action);
748a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void);
758a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void);
768a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void);
77307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void);
78307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void);
79307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void);
808a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void);
81307a4fe3SMatthias Ringwald 
82307a4fe3SMatthias Ringwald typedef enum {
839ecd1ba8SMatthias Ringwald     EHCILL_STATE_W2_SEND_SLEEP_ACK,
84307a4fe3SMatthias Ringwald     EHCILL_STATE_SLEEP,
859ecd1ba8SMatthias Ringwald     EHCILL_STATE_W4_WAKEUP_IND_OR_ACK,
86307a4fe3SMatthias Ringwald     EHCILL_STATE_AWAKE
87307a4fe3SMatthias Ringwald } EHCILL_STATE;
88307a4fe3SMatthias Ringwald 
89307a4fe3SMatthias Ringwald // eHCILL state machine
90307a4fe3SMatthias Ringwald static EHCILL_STATE ehcill_state;
91307a4fe3SMatthias Ringwald static uint8_t      ehcill_command_to_send;
92307a4fe3SMatthias Ringwald 
93f6a20ec9SMatthias Ringwald static btstack_uart_sleep_mode_t btstack_uart_sleep_mode;
94f6a20ec9SMatthias Ringwald 
95307a4fe3SMatthias Ringwald // work around for eHCILL problem
96307a4fe3SMatthias Ringwald static btstack_timer_source_t ehcill_sleep_ack_timer;
97307a4fe3SMatthias Ringwald 
98bd021c4eSMatthias Ringwald #endif
99bd021c4eSMatthias Ringwald 
10082d05f16SMatthias Ringwald 
101bd021c4eSMatthias Ringwald // assert pre-buffer for packet type is available
102bd021c4eSMatthias Ringwald #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0)
103bd021c4eSMatthias Ringwald #error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h
104bd021c4eSMatthias Ringwald #endif
105bd021c4eSMatthias Ringwald 
106bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
107bd021c4eSMatthias Ringwald 
108bd021c4eSMatthias Ringwald typedef enum {
109bd021c4eSMatthias Ringwald     H4_W4_PACKET_TYPE,
110bd021c4eSMatthias Ringwald     H4_W4_EVENT_HEADER,
111bd021c4eSMatthias Ringwald     H4_W4_ACL_HEADER,
112bd021c4eSMatthias Ringwald     H4_W4_SCO_HEADER,
113bd021c4eSMatthias Ringwald     H4_W4_PAYLOAD,
114bd021c4eSMatthias Ringwald } H4_STATE;
115bd021c4eSMatthias Ringwald 
116307a4fe3SMatthias Ringwald typedef enum {
117307a4fe3SMatthias Ringwald     TX_IDLE = 1,
118307a4fe3SMatthias Ringwald     TX_W4_PACKET_SENT,
119f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
120307a4fe3SMatthias Ringwald     TX_W4_WAKEUP,
121307a4fe3SMatthias Ringwald     TX_W2_EHCILL_SEND,
122307a4fe3SMatthias Ringwald     TX_W4_EHCILL_SENT,
123307a4fe3SMatthias Ringwald #endif
124307a4fe3SMatthias Ringwald } TX_STATE;
125307a4fe3SMatthias Ringwald 
126bd021c4eSMatthias Ringwald // UART Driver + Config
127bd021c4eSMatthias Ringwald static const btstack_uart_block_t * btstack_uart;
128bd021c4eSMatthias Ringwald static btstack_uart_config_t uart_config;
129bd021c4eSMatthias Ringwald 
130307a4fe3SMatthias Ringwald // write state
1318a23fc53SMatthias Ringwald static TX_STATE tx_state;
1322e25934bSMatthias Ringwald #ifdef ENABLE_EHCILL
1332e25934bSMatthias Ringwald static uint8_t * ehcill_tx_data;
1342e25934bSMatthias Ringwald static uint16_t  ehcill_tx_len;   // 0 == no outgoing packet
1352e25934bSMatthias Ringwald #endif
1368a23fc53SMatthias Ringwald 
137307a4fe3SMatthias Ringwald static uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
138bd021c4eSMatthias Ringwald 
139bd021c4eSMatthias Ringwald static  void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler;
140bd021c4eSMatthias Ringwald 
141bd021c4eSMatthias Ringwald // packet reader state machine
142bd021c4eSMatthias Ringwald static  H4_STATE h4_state;
143bd021c4eSMatthias Ringwald static int bytes_to_read;
144bd021c4eSMatthias Ringwald static int read_pos;
145bd021c4eSMatthias Ringwald 
146bd021c4eSMatthias Ringwald // incoming packet buffer
147fc6cde64SMatthias Ringwald static uint8_t hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + HCI_INCOMING_PACKET_BUFFER_SIZE + 1]; // packet type + max(acl header + acl payload, event header + event data)
148bd021c4eSMatthias Ringwald static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE];
149bd021c4eSMatthias Ringwald 
15039e7ee9fSMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
15139e7ee9fSMatthias Ringwald static const uint8_t local_version_event_prefix[] = { 0x04, 0x0e, 0x0c, 0x01, 0x01, 0x10};
15239e7ee9fSMatthias Ringwald static const uint8_t baud_rate_command_prefix[]   = { 0x01, 0x36, 0xff, 0x04};
15339e7ee9fSMatthias Ringwald static enum {
154*7741d2d0SMatthias Ringwald     BAUDRATE_CHANGE_WORKAROUND_IDLE,
155*7741d2d0SMatthias Ringwald     BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED,
156*7741d2d0SMatthias Ringwald     BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT,
157*7741d2d0SMatthias Ringwald     BAUDRATE_CHANGE_WORKAROUND_DONE
158*7741d2d0SMatthias Ringwald } baudrate_change_workaround_state;
15939e7ee9fSMatthias Ringwald #endif
16039e7ee9fSMatthias Ringwald 
161bd021c4eSMatthias Ringwald static int hci_transport_h4_set_baudrate(uint32_t baudrate){
162bace42efSMatthias Ringwald     log_info("hci_transport_h4_set_baudrate %"PRIu32, baudrate);
163bd021c4eSMatthias Ringwald     return btstack_uart->set_baudrate(baudrate);
164bd021c4eSMatthias Ringwald }
165bd021c4eSMatthias Ringwald 
166bd021c4eSMatthias Ringwald static void hci_transport_h4_reset_statemachine(void){
167bd021c4eSMatthias Ringwald     h4_state = H4_W4_PACKET_TYPE;
168bd021c4eSMatthias Ringwald     read_pos = 0;
169bd021c4eSMatthias Ringwald     bytes_to_read = 1;
170bd021c4eSMatthias Ringwald }
171bd021c4eSMatthias Ringwald 
172bd021c4eSMatthias Ringwald static void hci_transport_h4_trigger_next_read(void){
1738a23fc53SMatthias Ringwald     // log_info("hci_transport_h4_trigger_next_read: %u bytes", bytes_to_read);
174bd021c4eSMatthias Ringwald     btstack_uart->receive_block(&hci_packet[read_pos], bytes_to_read);
175bd021c4eSMatthias Ringwald }
176bd021c4eSMatthias Ringwald 
177bd021c4eSMatthias Ringwald static void hci_transport_h4_block_read(void){
178bd021c4eSMatthias Ringwald 
179bd021c4eSMatthias Ringwald     read_pos += bytes_to_read;
180bd021c4eSMatthias Ringwald 
181bd021c4eSMatthias Ringwald     switch (h4_state) {
182bd021c4eSMatthias Ringwald         case H4_W4_PACKET_TYPE:
183bd021c4eSMatthias Ringwald             switch (hci_packet[0]){
184bd021c4eSMatthias Ringwald                 case HCI_EVENT_PACKET:
185bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_EVENT_HEADER_SIZE;
186bd021c4eSMatthias Ringwald                     h4_state = H4_W4_EVENT_HEADER;
187bd021c4eSMatthias Ringwald                     break;
188bd021c4eSMatthias Ringwald                 case HCI_ACL_DATA_PACKET:
189bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_ACL_HEADER_SIZE;
190bd021c4eSMatthias Ringwald                     h4_state = H4_W4_ACL_HEADER;
191bd021c4eSMatthias Ringwald                     break;
192bd021c4eSMatthias Ringwald                 case HCI_SCO_DATA_PACKET:
193bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_SCO_HEADER_SIZE;
194bd021c4eSMatthias Ringwald                     h4_state = H4_W4_SCO_HEADER;
195bd021c4eSMatthias Ringwald                     break;
196f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
197307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_IND:
198307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_ACK:
199307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
200307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_ACK:
2018a23fc53SMatthias Ringwald                     hci_transport_h4_ehcill_handle_command(hci_packet[0]);
2028a23fc53SMatthias Ringwald                     hci_transport_h4_reset_statemachine();
203307a4fe3SMatthias Ringwald                     break;
204307a4fe3SMatthias Ringwald #endif
205bd021c4eSMatthias Ringwald                 default:
2068a23fc53SMatthias Ringwald                     log_error("hci_transport_h4: invalid packet type 0x%02x", hci_packet[0]);
207bd021c4eSMatthias Ringwald                     hci_transport_h4_reset_statemachine();
208bd021c4eSMatthias Ringwald                     break;
209bd021c4eSMatthias Ringwald             }
210bd021c4eSMatthias Ringwald             break;
211bd021c4eSMatthias Ringwald 
212bd021c4eSMatthias Ringwald         case H4_W4_EVENT_HEADER:
213bd021c4eSMatthias Ringwald             bytes_to_read = hci_packet[2];
214e8b81068SMatthias Ringwald             // check Event length
215e8b81068SMatthias Ringwald             if (HCI_EVENT_HEADER_SIZE + bytes_to_read >  HCI_INCOMING_PACKET_BUFFER_SIZE){
216e8b81068SMatthias Ringwald                 log_error("hci_transport_h4: invalid Event len %d - only space for %u", bytes_to_read, HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_EVENT_HEADER_SIZE);
217e8b81068SMatthias Ringwald                 hci_transport_h4_reset_statemachine();
218e8b81068SMatthias Ringwald                 break;
219e8b81068SMatthias Ringwald             }
220bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
221bd021c4eSMatthias Ringwald             break;
222bd021c4eSMatthias Ringwald 
223bd021c4eSMatthias Ringwald         case H4_W4_ACL_HEADER:
224bd021c4eSMatthias Ringwald             bytes_to_read = little_endian_read_16( hci_packet, 3);
225bd021c4eSMatthias Ringwald             // check ACL length
226fc6cde64SMatthias Ringwald             if (HCI_ACL_HEADER_SIZE + bytes_to_read >  HCI_INCOMING_PACKET_BUFFER_SIZE){
227fc6cde64SMatthias Ringwald                 log_error("hci_transport_h4: invalid ACL payload len %d - only space for %u", bytes_to_read, HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE);
228bd021c4eSMatthias Ringwald                 hci_transport_h4_reset_statemachine();
229bd021c4eSMatthias Ringwald                 break;
230bd021c4eSMatthias Ringwald             }
231bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
232bd021c4eSMatthias Ringwald             break;
233bd021c4eSMatthias Ringwald 
234bd021c4eSMatthias Ringwald         case H4_W4_SCO_HEADER:
235bd021c4eSMatthias Ringwald             bytes_to_read = hci_packet[3];
236e8b81068SMatthias Ringwald             // check SCO length
237e8b81068SMatthias Ringwald             if (HCI_SCO_HEADER_SIZE + bytes_to_read >  HCI_INCOMING_PACKET_BUFFER_SIZE){
238e8b81068SMatthias Ringwald                 log_error("hci_transport_h4: invalid SCO payload len %d - only space for %u", bytes_to_read, HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_SCO_HEADER_SIZE);
239e8b81068SMatthias Ringwald                 hci_transport_h4_reset_statemachine();
240e8b81068SMatthias Ringwald                 break;
241e8b81068SMatthias Ringwald             }
242bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
243bd021c4eSMatthias Ringwald             break;
244bd021c4eSMatthias Ringwald 
245bd021c4eSMatthias Ringwald         case H4_W4_PAYLOAD:
24639e7ee9fSMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
247*7741d2d0SMatthias Ringwald             if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_IDLE
24839e7ee9fSMatthias Ringwald             && memcmp(hci_packet, local_version_event_prefix, sizeof(local_version_event_prefix)) == 0){
24939e7ee9fSMatthias Ringwald                 if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
25039e7ee9fSMatthias Ringwald                     // detect TI CC256x controller based on manufacturer
25139e7ee9fSMatthias Ringwald                     log_info("Detected CC256x controller");
252*7741d2d0SMatthias Ringwald                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED;
25339e7ee9fSMatthias Ringwald                 } else {
25439e7ee9fSMatthias Ringwald                     // work around not needed
25539e7ee9fSMatthias Ringwald                     log_info("Bluetooth controller not by TI");
256*7741d2d0SMatthias Ringwald                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE;
25739e7ee9fSMatthias Ringwald                 }
25839e7ee9fSMatthias Ringwald             }
25939e7ee9fSMatthias Ringwald #endif
260bd021c4eSMatthias Ringwald             packet_handler(hci_packet[0], &hci_packet[1], read_pos-1);
261bd021c4eSMatthias Ringwald             hci_transport_h4_reset_statemachine();
262bd021c4eSMatthias Ringwald             break;
263bd021c4eSMatthias Ringwald         default:
264bd021c4eSMatthias Ringwald             break;
265bd021c4eSMatthias Ringwald     }
26639e7ee9fSMatthias Ringwald 
26739e7ee9fSMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
268*7741d2d0SMatthias Ringwald     if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT){
269*7741d2d0SMatthias Ringwald         baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_IDLE;
27039e7ee9fSMatthias Ringwald         // avoid flowcontrol problem by reading expected hci command complete event of 7 bytes in a single block read
27139e7ee9fSMatthias Ringwald         h4_state = H4_W4_PAYLOAD;
27239e7ee9fSMatthias Ringwald         bytes_to_read = 7;
27339e7ee9fSMatthias Ringwald     }
27439e7ee9fSMatthias Ringwald #endif
27539e7ee9fSMatthias Ringwald 
276bd021c4eSMatthias Ringwald     hci_transport_h4_trigger_next_read();
277bd021c4eSMatthias Ringwald }
278bd021c4eSMatthias Ringwald 
279307a4fe3SMatthias Ringwald static void hci_transport_h4_block_sent(void){
280307a4fe3SMatthias Ringwald     switch (tx_state){
281307a4fe3SMatthias Ringwald         case TX_W4_PACKET_SENT:
282307a4fe3SMatthias Ringwald             // packet fully sent, reset state
2832e25934bSMatthias Ringwald #ifdef ENABLE_EHCILL
2842e25934bSMatthias Ringwald             ehcill_tx_len = 0;
2852e25934bSMatthias Ringwald #endif
2868a23fc53SMatthias Ringwald             tx_state = TX_IDLE;
287307a4fe3SMatthias Ringwald 
288f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
2898a23fc53SMatthias Ringwald             // notify eHCILL engine
2908a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_handle_packet_sent();
291307a4fe3SMatthias Ringwald #endif
292307a4fe3SMatthias Ringwald             // notify upper stack that it can send again
293307a4fe3SMatthias Ringwald             packet_handler(HCI_EVENT_PACKET, &packet_sent_event[0], sizeof(packet_sent_event));
294307a4fe3SMatthias Ringwald             break;
295307a4fe3SMatthias Ringwald 
296f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
2978a23fc53SMatthias Ringwald         case TX_W4_EHCILL_SENT:
298b403028eSMatthias Ringwald         case TX_W4_WAKEUP:
2998a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_handle_ehcill_command_sent();
300307a4fe3SMatthias Ringwald             break;
301307a4fe3SMatthias Ringwald #endif
302307a4fe3SMatthias Ringwald 
303307a4fe3SMatthias Ringwald         default:
304307a4fe3SMatthias Ringwald             break;
305307a4fe3SMatthias Ringwald     }
306307a4fe3SMatthias Ringwald }
307307a4fe3SMatthias Ringwald 
308307a4fe3SMatthias Ringwald static int hci_transport_h4_can_send_now(uint8_t packet_type){
309307a4fe3SMatthias Ringwald     return tx_state == TX_IDLE;
310307a4fe3SMatthias Ringwald }
311307a4fe3SMatthias Ringwald 
312307a4fe3SMatthias Ringwald static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){
31339e7ee9fSMatthias Ringwald 
314307a4fe3SMatthias Ringwald     // store packet type before actual data and increase size
315307a4fe3SMatthias Ringwald     size++;
316307a4fe3SMatthias Ringwald     packet--;
317307a4fe3SMatthias Ringwald     *packet = packet_type;
318307a4fe3SMatthias Ringwald 
31939e7ee9fSMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
320*7741d2d0SMatthias Ringwald     if ((baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED)
32139e7ee9fSMatthias Ringwald     && (memcmp(packet, baud_rate_command_prefix, sizeof(baud_rate_command_prefix)) == 0)) {
322*7741d2d0SMatthias Ringwald         log_info("Baud rate command detected, expect command complete event next");
323*7741d2d0SMatthias Ringwald         baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT;
32439e7ee9fSMatthias Ringwald     }
32539e7ee9fSMatthias Ringwald #endif
32639e7ee9fSMatthias Ringwald 
327f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
3282e25934bSMatthias Ringwald     // store request for later
3292e25934bSMatthias Ringwald     ehcill_tx_len   = size;
3302e25934bSMatthias Ringwald     ehcill_tx_data  = packet;
3319ecd1ba8SMatthias Ringwald     switch (ehcill_state){
3329ecd1ba8SMatthias Ringwald         case EHCILL_STATE_SLEEP:
3338a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_trigger_wakeup();
3348a23fc53SMatthias Ringwald             return 0;
3359ecd1ba8SMatthias Ringwald         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
336b403028eSMatthias Ringwald             log_info("eHILL: send next packet, state EHCILL_STATE_W2_SEND_SLEEP_ACK");
3379ecd1ba8SMatthias Ringwald             return 0;
3389ecd1ba8SMatthias Ringwald         default:
3399ecd1ba8SMatthias Ringwald             break;
3408a23fc53SMatthias Ringwald     }
3418a23fc53SMatthias Ringwald #endif
3428a23fc53SMatthias Ringwald 
3438a23fc53SMatthias Ringwald     // start sending
3448a23fc53SMatthias Ringwald     tx_state = TX_W4_PACKET_SENT;
345307a4fe3SMatthias Ringwald     btstack_uart->send_block(packet, size);
346307a4fe3SMatthias Ringwald     return 0;
347307a4fe3SMatthias Ringwald }
348307a4fe3SMatthias Ringwald 
349bd021c4eSMatthias Ringwald static void hci_transport_h4_init(const void * transport_config){
350bd021c4eSMatthias Ringwald     // check for hci_transport_config_uart_t
351bd021c4eSMatthias Ringwald     if (!transport_config) {
352bd021c4eSMatthias Ringwald         log_error("hci_transport_h4: no config!");
353bd021c4eSMatthias Ringwald         return;
354bd021c4eSMatthias Ringwald     }
355bd021c4eSMatthias Ringwald     if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) {
356bd021c4eSMatthias Ringwald         log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!");
357bd021c4eSMatthias Ringwald         return;
358bd021c4eSMatthias Ringwald     }
359bd021c4eSMatthias Ringwald 
360bd021c4eSMatthias Ringwald     // extract UART config from transport config
361bd021c4eSMatthias Ringwald     hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config;
362bd021c4eSMatthias Ringwald     uart_config.baudrate    = hci_transport_config_uart->baudrate_init;
363bd021c4eSMatthias Ringwald     uart_config.flowcontrol = hci_transport_config_uart->flowcontrol;
364bd021c4eSMatthias Ringwald     uart_config.device_name = hci_transport_config_uart->device_name;
365bd021c4eSMatthias Ringwald 
366bd021c4eSMatthias Ringwald     // setup UART driver
367bd021c4eSMatthias Ringwald     btstack_uart->init(&uart_config);
368bd021c4eSMatthias Ringwald     btstack_uart->set_block_received(&hci_transport_h4_block_read);
369bd021c4eSMatthias Ringwald     btstack_uart->set_block_sent(&hci_transport_h4_block_sent);
370bd021c4eSMatthias Ringwald }
371bd021c4eSMatthias Ringwald 
372bd021c4eSMatthias Ringwald static int hci_transport_h4_open(void){
373bd021c4eSMatthias Ringwald     int res = btstack_uart->open();
374bd021c4eSMatthias Ringwald     if (res){
375bd021c4eSMatthias Ringwald         return res;
376bd021c4eSMatthias Ringwald     }
377bd021c4eSMatthias Ringwald     hci_transport_h4_reset_statemachine();
378bd021c4eSMatthias Ringwald     hci_transport_h4_trigger_next_read();
379307a4fe3SMatthias Ringwald 
380307a4fe3SMatthias Ringwald     tx_state = TX_IDLE;
381307a4fe3SMatthias Ringwald 
382f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
3838a23fc53SMatthias Ringwald     hci_transport_h4_ehcill_open();
384307a4fe3SMatthias Ringwald #endif
385bd021c4eSMatthias Ringwald     return 0;
386bd021c4eSMatthias Ringwald }
387bd021c4eSMatthias Ringwald 
388bd021c4eSMatthias Ringwald static int hci_transport_h4_close(void){
389bd021c4eSMatthias Ringwald     return btstack_uart->close();
390bd021c4eSMatthias Ringwald }
391bd021c4eSMatthias Ringwald 
392bd021c4eSMatthias Ringwald static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
393bd021c4eSMatthias Ringwald     packet_handler = handler;
394bd021c4eSMatthias Ringwald }
395bd021c4eSMatthias Ringwald 
396bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
397bd021c4eSMatthias Ringwald }
398bd021c4eSMatthias Ringwald 
3998a23fc53SMatthias Ringwald //
400307a4fe3SMatthias Ringwald // --- main part of eHCILL implementation ---
4018a23fc53SMatthias Ringwald //
402307a4fe3SMatthias Ringwald 
403f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
404307a4fe3SMatthias Ringwald 
40562ca45d7SMatthias Ringwald static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){
40662ca45d7SMatthias Ringwald     static int last_state = 0;
40762ca45d7SMatthias Ringwald     if (sleep_active == last_state) return;
40862ca45d7SMatthias Ringwald     last_state = sleep_active;
40962ca45d7SMatthias Ringwald 
41062ca45d7SMatthias Ringwald     log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active);
41162ca45d7SMatthias Ringwald     uint8_t event[3];
41262ca45d7SMatthias Ringwald     event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE;
41362ca45d7SMatthias Ringwald     event[1] = sizeof(event) - 2;
41462ca45d7SMatthias Ringwald     event[2] = sleep_active;
41562ca45d7SMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
41662ca45d7SMatthias Ringwald }
41762ca45d7SMatthias Ringwald 
418b403028eSMatthias Ringwald static void hci_transport_h4_ehcill_wakeup_handler(void){
419b403028eSMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
420b403028eSMatthias Ringwald     log_info("eHCILL: UART wakeup received");
421b403028eSMatthias Ringwald #endif
422b403028eSMatthias Ringwald     hci_transport_h4_ehcill_handle_command(EHCILL_WAKEUP_SIGNAL);
423b403028eSMatthias Ringwald }
424b403028eSMatthias Ringwald 
4258a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void){
4268a23fc53SMatthias Ringwald     hci_transport_h4_ehcill_reset_statemachine();
4278a23fc53SMatthias Ringwald 
4288a23fc53SMatthias Ringwald     // find best sleep mode to use: wake on CTS, wake on RX, none
4298a23fc53SMatthias Ringwald     btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF;
4308a23fc53SMatthias Ringwald     int supported_sleep_modes = 0;
4318a23fc53SMatthias Ringwald     if (btstack_uart->get_supported_sleep_modes){
4328a23fc53SMatthias Ringwald         supported_sleep_modes = btstack_uart->get_supported_sleep_modes();
433307a4fe3SMatthias Ringwald     }
4348a23fc53SMatthias Ringwald     if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){
4358a23fc53SMatthias Ringwald         log_info("eHCILL: using wake on CTS");
4368a23fc53SMatthias Ringwald         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE;
4378a23fc53SMatthias Ringwald     } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){
4388a23fc53SMatthias Ringwald         log_info("eHCILL: using wake on RX");
4398a23fc53SMatthias Ringwald         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE;
4408a23fc53SMatthias Ringwald     } else {
4418a23fc53SMatthias Ringwald         log_info("eHCILL: UART driver does not provide compatible sleep mode");
4428a23fc53SMatthias Ringwald     }
443b403028eSMatthias Ringwald     if (btstack_uart->set_wakeup_handler){
444b403028eSMatthias Ringwald         btstack_uart->set_wakeup_handler(&hci_transport_h4_ehcill_wakeup_handler);
445b403028eSMatthias Ringwald     }
446307a4fe3SMatthias Ringwald }
447307a4fe3SMatthias Ringwald 
448307a4fe3SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void){
4499ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
4509ecd1ba8SMatthias Ringwald     log_info("eHCILL: send WAKEUP_IND");
4519ecd1ba8SMatthias Ringwald #endif
452307a4fe3SMatthias Ringwald     // update state
453307a4fe3SMatthias Ringwald     tx_state     = TX_W4_WAKEUP;
4549ecd1ba8SMatthias Ringwald     ehcill_state = EHCILL_STATE_W4_WAKEUP_IND_OR_ACK;
455307a4fe3SMatthias Ringwald     ehcill_command_to_send = EHCILL_WAKE_UP_IND;
456307a4fe3SMatthias Ringwald     btstack_uart->send_block(&ehcill_command_to_send, 1);
457307a4fe3SMatthias Ringwald }
458307a4fe3SMatthias Ringwald 
459307a4fe3SMatthias Ringwald static int hci_transport_h4_ehcill_outgoing_packet_ready(void){
4602e25934bSMatthias Ringwald     return ehcill_tx_len != 0;
461307a4fe3SMatthias Ringwald }
462307a4fe3SMatthias Ringwald 
463307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void){
464307a4fe3SMatthias Ringwald     ehcill_state = EHCILL_STATE_AWAKE;
465307a4fe3SMatthias Ringwald }
466307a4fe3SMatthias Ringwald 
467307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void){
4689ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
4699ecd1ba8SMatthias Ringwald     log_info("eHCILL: send command %02x", ehcill_command_to_send);
4709ecd1ba8SMatthias Ringwald #endif
471307a4fe3SMatthias Ringwald     tx_state = TX_W4_EHCILL_SENT;
4729ecd1ba8SMatthias Ringwald     if (ehcill_command_to_send == EHCILL_GO_TO_SLEEP_ACK){
4739ecd1ba8SMatthias Ringwald         ehcill_state = EHCILL_STATE_SLEEP;
4749ecd1ba8SMatthias Ringwald     }
475307a4fe3SMatthias Ringwald     btstack_uart->send_block(&ehcill_command_to_send, 1);
476307a4fe3SMatthias Ringwald }
477307a4fe3SMatthias Ringwald 
478307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){
479b403028eSMatthias Ringwald 	UNUSED(timer);
4809ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
4819ecd1ba8SMatthias Ringwald     log_info("eHCILL: timer triggered");
4829ecd1ba8SMatthias Ringwald #endif
483307a4fe3SMatthias Ringwald     hci_transport_h4_ehcill_send_ehcill_command();
484307a4fe3SMatthias Ringwald }
485307a4fe3SMatthias Ringwald 
486307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){
487307a4fe3SMatthias Ringwald     // setup timer
4889ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
4899ecd1ba8SMatthias Ringwald     log_info("eHCILL: set timer for sending command %02x", ehcill_command_to_send);
4909ecd1ba8SMatthias Ringwald #endif
4918a23fc53SMatthias Ringwald     btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler);
492307a4fe3SMatthias Ringwald     btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50);
493307a4fe3SMatthias Ringwald     btstack_run_loop_add_timer(&ehcill_sleep_ack_timer);
494307a4fe3SMatthias Ringwald }
495307a4fe3SMatthias Ringwald 
4968a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void){
4978a23fc53SMatthias Ringwald     switch (tx_state){
4988a23fc53SMatthias Ringwald         case TX_W2_EHCILL_SEND:
4998a23fc53SMatthias Ringwald         case TX_W4_EHCILL_SENT:
5008a23fc53SMatthias Ringwald             // wake up / sleep ack in progress, nothing to do now
5018a23fc53SMatthias Ringwald             return;
5028a23fc53SMatthias Ringwald         case TX_IDLE:
5038a23fc53SMatthias Ringwald         default:
5048a23fc53SMatthias Ringwald             // all clear, prepare for wakeup
5058a23fc53SMatthias Ringwald             break;
5068a23fc53SMatthias Ringwald     }
5078a23fc53SMatthias Ringwald     // UART needed again
50862ca45d7SMatthias Ringwald     hci_transport_h4_ehcill_emit_sleep_state(0);
5098a23fc53SMatthias Ringwald     if (btstack_uart_sleep_mode){
5108a23fc53SMatthias Ringwald         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
5118a23fc53SMatthias Ringwald     }
5128a23fc53SMatthias Ringwald     hci_transport_h4_echill_send_wakeup_ind();
5138a23fc53SMatthias Ringwald }
5148a23fc53SMatthias Ringwald 
5159ecd1ba8SMatthias Ringwald static void hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command){
5169ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5179ecd1ba8SMatthias Ringwald     log_info("eHCILL: schedule eHCILL command %02x", command);
5189ecd1ba8SMatthias Ringwald #endif
519307a4fe3SMatthias Ringwald     ehcill_command_to_send = command;
520307a4fe3SMatthias Ringwald     switch (tx_state){
521307a4fe3SMatthias Ringwald         case TX_IDLE:
522307a4fe3SMatthias Ringwald             if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){
523307a4fe3SMatthias Ringwald                 // send right away
524307a4fe3SMatthias Ringwald                 hci_transport_h4_ehcill_send_ehcill_command();
525307a4fe3SMatthias Ringwald             } else {
526307a4fe3SMatthias Ringwald                 // change state so BTstack cannot send and setup timer
527307a4fe3SMatthias Ringwald                 tx_state = TX_W2_EHCILL_SEND;
528307a4fe3SMatthias Ringwald                 hci_transport_h4_ehcill_sleep_ack_timer_setup();
529307a4fe3SMatthias Ringwald             }
530307a4fe3SMatthias Ringwald             break;
531307a4fe3SMatthias Ringwald         default:
532307a4fe3SMatthias Ringwald             break;
533307a4fe3SMatthias Ringwald     }
534307a4fe3SMatthias Ringwald }
535307a4fe3SMatthias Ringwald 
5368a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action){
537307a4fe3SMatthias Ringwald     // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size);
538307a4fe3SMatthias Ringwald     switch(ehcill_state){
539307a4fe3SMatthias Ringwald         case EHCILL_STATE_AWAKE:
540307a4fe3SMatthias Ringwald             switch(action){
541307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_IND:
5429ecd1ba8SMatthias Ringwald                     ehcill_state = EHCILL_STATE_W2_SEND_SLEEP_ACK;
5439ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5449ecd1ba8SMatthias Ringwald                     log_info("eHCILL: Received GO_TO_SLEEP_IND RX");
5459ecd1ba8SMatthias Ringwald #endif
5469ecd1ba8SMatthias Ringwald                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_GO_TO_SLEEP_ACK);
547307a4fe3SMatthias Ringwald                     break;
548307a4fe3SMatthias Ringwald                 default:
549307a4fe3SMatthias Ringwald                     break;
550307a4fe3SMatthias Ringwald             }
551307a4fe3SMatthias Ringwald             break;
552307a4fe3SMatthias Ringwald 
5539ecd1ba8SMatthias Ringwald         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
554307a4fe3SMatthias Ringwald             switch(action){
555307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
556307a4fe3SMatthias Ringwald                     ehcill_state = EHCILL_STATE_AWAKE;
55762ca45d7SMatthias Ringwald                     hci_transport_h4_ehcill_emit_sleep_state(0);
558b403028eSMatthias Ringwald                     if (btstack_uart_sleep_mode){
559b403028eSMatthias Ringwald                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
560b403028eSMatthias Ringwald                     }
561b403028eSMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
562b403028eSMatthias Ringwald                     log_info("eHCILL: Received WAKE_UP_IND RX");
563b403028eSMatthias Ringwald #endif
564b403028eSMatthias Ringwald                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK);
565b403028eSMatthias Ringwald                     break;
566b403028eSMatthias Ringwald 
567b403028eSMatthias Ringwald                 default:
568b403028eSMatthias Ringwald                     break;
569b403028eSMatthias Ringwald             }
570b403028eSMatthias Ringwald             break;
571b403028eSMatthias Ringwald 
572b403028eSMatthias Ringwald         case EHCILL_STATE_SLEEP:
573b403028eSMatthias Ringwald             switch(action){
574b403028eSMatthias Ringwald                 case EHCILL_WAKEUP_SIGNAL:
575b403028eSMatthias Ringwald                     hci_transport_h4_ehcill_emit_sleep_state(0);
576b403028eSMatthias Ringwald                     if (btstack_uart_sleep_mode){
577b403028eSMatthias Ringwald                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
578b403028eSMatthias Ringwald                     }
579b403028eSMatthias Ringwald                     break;
580b403028eSMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
581b403028eSMatthias Ringwald                     ehcill_state = EHCILL_STATE_AWAKE;
582b403028eSMatthias Ringwald                     hci_transport_h4_ehcill_emit_sleep_state(0);
583b403028eSMatthias Ringwald                     if (btstack_uart_sleep_mode){
584b403028eSMatthias Ringwald                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
585b403028eSMatthias Ringwald                     }
5869ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5879ecd1ba8SMatthias Ringwald                     log_info("eHCILL: Received WAKE_UP_IND RX");
5889ecd1ba8SMatthias Ringwald #endif
5899ecd1ba8SMatthias Ringwald                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK);
590307a4fe3SMatthias Ringwald                     break;
591307a4fe3SMatthias Ringwald 
592307a4fe3SMatthias Ringwald                 default:
593307a4fe3SMatthias Ringwald                     break;
594307a4fe3SMatthias Ringwald             }
595307a4fe3SMatthias Ringwald             break;
596307a4fe3SMatthias Ringwald 
5979ecd1ba8SMatthias Ringwald         case EHCILL_STATE_W4_WAKEUP_IND_OR_ACK:
598307a4fe3SMatthias Ringwald             switch(action){
599307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
600307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_ACK:
6019ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
6029ecd1ba8SMatthias Ringwald                     log_info("eHCILL: Received WAKE_UP (%02x)", action);
6039ecd1ba8SMatthias Ringwald #endif
604307a4fe3SMatthias Ringwald                     tx_state = TX_W4_PACKET_SENT;
605307a4fe3SMatthias Ringwald                     ehcill_state = EHCILL_STATE_AWAKE;
6062e25934bSMatthias Ringwald                     btstack_uart->send_block(ehcill_tx_data, ehcill_tx_len);
607307a4fe3SMatthias Ringwald                     break;
608307a4fe3SMatthias Ringwald                 default:
609307a4fe3SMatthias Ringwald                     break;
610307a4fe3SMatthias Ringwald             }
611307a4fe3SMatthias Ringwald             break;
612307a4fe3SMatthias Ringwald     }
613307a4fe3SMatthias Ringwald }
6148a23fc53SMatthias Ringwald 
6158a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void){
6169ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
6179ecd1ba8SMatthias Ringwald         log_info("eHCILL: handle packet sent, command to send %02x", ehcill_command_to_send);
6189ecd1ba8SMatthias Ringwald #endif
6198a23fc53SMatthias Ringwald     // now, send pending ehcill command if neccessary
6208a23fc53SMatthias Ringwald     switch (ehcill_command_to_send){
6218a23fc53SMatthias Ringwald         case EHCILL_GO_TO_SLEEP_ACK:
6228a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_sleep_ack_timer_setup();
6238a23fc53SMatthias Ringwald             break;
6248a23fc53SMatthias Ringwald         case EHCILL_WAKE_UP_IND:
6258a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_send_ehcill_command();
6268a23fc53SMatthias Ringwald             break;
6278a23fc53SMatthias Ringwald         default:
6288a23fc53SMatthias Ringwald             break;
6298a23fc53SMatthias Ringwald     }
6308a23fc53SMatthias Ringwald }
6318a23fc53SMatthias Ringwald 
6328a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){
6338a23fc53SMatthias Ringwald     tx_state = TX_IDLE;
6348a23fc53SMatthias Ringwald     int command = ehcill_command_to_send;
6358a23fc53SMatthias Ringwald     ehcill_command_to_send = 0;
6369ecd1ba8SMatthias Ringwald 
6379ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
6389ecd1ba8SMatthias Ringwald         log_info("eHCILL: handle eHCILL sent, command was %02x", command);
6399ecd1ba8SMatthias Ringwald #endif
6409ecd1ba8SMatthias Ringwald 
6418a23fc53SMatthias Ringwald     if (command == EHCILL_GO_TO_SLEEP_ACK) {
6429ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
6438a23fc53SMatthias Ringwald         log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode");
6449ecd1ba8SMatthias Ringwald #endif
6458a23fc53SMatthias Ringwald         // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent
6468a23fc53SMatthias Ringwald         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
6478a23fc53SMatthias Ringwald             btstack_uart->set_sleep(btstack_uart_sleep_mode);
6488a23fc53SMatthias Ringwald         }
64962ca45d7SMatthias Ringwald         hci_transport_h4_ehcill_emit_sleep_state(1);
6508a23fc53SMatthias Ringwald     }
6518a23fc53SMatthias Ringwald     // already packet ready? then start wakeup
6528a23fc53SMatthias Ringwald     if (hci_transport_h4_ehcill_outgoing_packet_ready()){
65362ca45d7SMatthias Ringwald         hci_transport_h4_ehcill_emit_sleep_state(0);
654b403028eSMatthias Ringwald         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
6558a23fc53SMatthias Ringwald             btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
6568a23fc53SMatthias Ringwald         }
6579ecd1ba8SMatthias Ringwald         if (command != EHCILL_WAKE_UP_IND){
6588a23fc53SMatthias Ringwald             hci_transport_h4_echill_send_wakeup_ind();
6598a23fc53SMatthias Ringwald         }
6608a23fc53SMatthias Ringwald     }
6619ecd1ba8SMatthias Ringwald }
6628a23fc53SMatthias Ringwald 
663307a4fe3SMatthias Ringwald #endif
664307a4fe3SMatthias Ringwald // --- end of eHCILL implementation ---------
665307a4fe3SMatthias Ringwald 
666bd021c4eSMatthias Ringwald static const hci_transport_t hci_transport_h4 = {
667bd021c4eSMatthias Ringwald     /* const char * name; */                                        "H4",
668bd021c4eSMatthias Ringwald     /* void   (*init) (const void *transport_config); */            &hci_transport_h4_init,
669bd021c4eSMatthias Ringwald     /* int    (*open)(void); */                                     &hci_transport_h4_open,
670bd021c4eSMatthias Ringwald     /* int    (*close)(void); */                                    &hci_transport_h4_close,
671bd021c4eSMatthias Ringwald     /* void   (*register_packet_handler)(void (*handler)(...); */   &hci_transport_h4_register_packet_handler,
672bd021c4eSMatthias Ringwald     /* int    (*can_send_packet_now)(uint8_t packet_type); */       &hci_transport_h4_can_send_now,
673bd021c4eSMatthias Ringwald     /* int    (*send_packet)(...); */                               &hci_transport_h4_send_packet,
674bd021c4eSMatthias Ringwald     /* int    (*set_baudrate)(uint32_t baudrate); */                &hci_transport_h4_set_baudrate,
675bd021c4eSMatthias Ringwald     /* void   (*reset_link)(void); */                               NULL,
676ee752bb8SMatthias Ringwald     /* void   (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL,
677bd021c4eSMatthias Ringwald };
678bd021c4eSMatthias Ringwald 
679bd021c4eSMatthias Ringwald // configure and return h4 singleton
680bd021c4eSMatthias Ringwald const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) {
681bd021c4eSMatthias Ringwald     btstack_uart = uart_driver;
682bd021c4eSMatthias Ringwald     return &hci_transport_h4;
683bd021c4eSMatthias Ringwald }
684