xref: /btstack/src/hci_transport_h4.c (revision b338ca60490ae807c0f4ac455beaf464585bfb4c)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "hci_transport_h4.c"
39 
40 /*
41  *  hci_h4_transport.c
42  *
43  *  HCI Transport API implementation for H4 protocol over POSIX with optional support for eHCILL
44  *
45  *  Created by Matthias Ringwald on 4/29/09.
46  */
47 
48 #include <inttypes.h>
49 
50 #include "btstack_config.h"
51 
52 #include "btstack_debug.h"
53 #include "hci.h"
54 #include "hci_transport.h"
55 #include "bluetooth_company_id.h"
56 #include "btstack_uart_block.h"
57 
58 #define ENABLE_LOG_EHCILL
59 
60 #ifdef ENABLE_EHCILL
61 
62 // eHCILL commands
63 enum EHCILL_MESSAGES {
64 	EHCILL_GO_TO_SLEEP_IND = 0x030,
65 	EHCILL_GO_TO_SLEEP_ACK = 0x031,
66 	EHCILL_WAKE_UP_IND     = 0x032,
67 	EHCILL_WAKE_UP_ACK     = 0x033,
68 	EHCILL_WAKEUP_SIGNAL   = 0x034,
69 };
70 
71 static int  hci_transport_h4_ehcill_outgoing_packet_ready(void);
72 static void hci_transport_h4_echill_send_wakeup_ind(void);
73 static void hci_transport_h4_ehcill_handle_command(uint8_t action);
74 static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void);
75 static void hci_transport_h4_ehcill_handle_packet_sent(void);
76 static void hci_transport_h4_ehcill_open(void);
77 static void hci_transport_h4_ehcill_reset_statemachine(void);
78 static void hci_transport_h4_ehcill_send_ehcill_command(void);
79 static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void);
80 static void hci_transport_h4_ehcill_trigger_wakeup(void);
81 
82 typedef enum {
83     EHCILL_STATE_W2_SEND_SLEEP_ACK,
84     EHCILL_STATE_SLEEP,
85     EHCILL_STATE_W4_WAKEUP_IND_OR_ACK,
86     EHCILL_STATE_AWAKE
87 } EHCILL_STATE;
88 
89 // eHCILL state machine
90 static EHCILL_STATE ehcill_state;
91 static uint8_t      ehcill_command_to_send;
92 
93 static btstack_uart_sleep_mode_t btstack_uart_sleep_mode;
94 
95 // work around for eHCILL problem
96 static btstack_timer_source_t ehcill_sleep_ack_timer;
97 
98 #endif
99 
100 
101 // assert pre-buffer for packet type is available
102 #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0)
103 #error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h
104 #endif
105 
106 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
107 
108 typedef enum {
109     H4_OFF,
110     H4_W4_PACKET_TYPE,
111     H4_W4_EVENT_HEADER,
112     H4_W4_ACL_HEADER,
113     H4_W4_SCO_HEADER,
114     H4_W4_PAYLOAD,
115 } H4_STATE;
116 
117 typedef enum {
118     TX_OFF,
119     TX_IDLE,
120     TX_W4_PACKET_SENT,
121 #ifdef ENABLE_EHCILL
122     TX_W4_WAKEUP,
123     TX_W2_EHCILL_SEND,
124     TX_W4_EHCILL_SENT,
125 #endif
126 } TX_STATE;
127 
128 // UART Driver + Config
129 static const btstack_uart_block_t * btstack_uart;
130 static btstack_uart_config_t uart_config;
131 
132 // write state
133 static TX_STATE tx_state;
134 #ifdef ENABLE_EHCILL
135 static uint8_t * ehcill_tx_data;
136 static uint16_t  ehcill_tx_len;   // 0 == no outgoing packet
137 #endif
138 
139 static uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
140 
141 static  void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler;
142 
143 // packet reader state machine
144 static  H4_STATE h4_state;
145 static uint16_t bytes_to_read;
146 static uint16_t read_pos;
147 
148 // incoming packet buffer
149 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)
150 static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE];
151 
152 // Baudrate change bugs in TI CC256x and CYW20704
153 #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
154 #define ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
155 static const uint8_t baud_rate_command_prefix[]   = { 0x01, 0x36, 0xff, 0x04};
156 #endif
157 
158 #ifdef ENABLE_CYPRESS_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
159 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
160 #error "Please enable either ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND or ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND"
161 #endif
162 #define ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
163 static const uint8_t baud_rate_command_prefix[]   = { 0x01, 0x18, 0xfc, 0x06};
164 #endif
165 
166 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
167 static const uint8_t local_version_event_prefix[] = { 0x04, 0x0e, 0x0c, 0x01, 0x01, 0x10};
168 static enum {
169     BAUDRATE_CHANGE_WORKAROUND_IDLE,
170     BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED,
171     BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT,
172     BAUDRATE_CHANGE_WORKAROUND_DONE
173 } baudrate_change_workaround_state;
174 #endif
175 
176 static int hci_transport_h4_set_baudrate(uint32_t baudrate){
177     log_info("hci_transport_h4_set_baudrate %"PRIu32, baudrate);
178     return btstack_uart->set_baudrate(baudrate);
179 }
180 
181 static void hci_transport_h4_reset_statemachine(void){
182     h4_state = H4_W4_PACKET_TYPE;
183     read_pos = 0;
184     bytes_to_read = 1;
185 }
186 
187 static void hci_transport_h4_trigger_next_read(void){
188     // log_info("hci_transport_h4_trigger_next_read: %u bytes", bytes_to_read);
189     btstack_uart->receive_block(&hci_packet[read_pos], bytes_to_read);
190 }
191 
192 static void hci_transport_h4_block_read(void){
193 
194     read_pos += bytes_to_read;
195 
196     switch (h4_state) {
197         case H4_W4_PACKET_TYPE:
198             switch (hci_packet[0]){
199                 case HCI_EVENT_PACKET:
200                     bytes_to_read = HCI_EVENT_HEADER_SIZE;
201                     h4_state = H4_W4_EVENT_HEADER;
202                     break;
203                 case HCI_ACL_DATA_PACKET:
204                     bytes_to_read = HCI_ACL_HEADER_SIZE;
205                     h4_state = H4_W4_ACL_HEADER;
206                     break;
207                 case HCI_SCO_DATA_PACKET:
208                     bytes_to_read = HCI_SCO_HEADER_SIZE;
209                     h4_state = H4_W4_SCO_HEADER;
210                     break;
211 #ifdef ENABLE_EHCILL
212                 case EHCILL_GO_TO_SLEEP_IND:
213                 case EHCILL_GO_TO_SLEEP_ACK:
214                 case EHCILL_WAKE_UP_IND:
215                 case EHCILL_WAKE_UP_ACK:
216                     hci_transport_h4_ehcill_handle_command(hci_packet[0]);
217                     hci_transport_h4_reset_statemachine();
218                     break;
219 #endif
220                 default:
221                     log_error("hci_transport_h4: invalid packet type 0x%02x", hci_packet[0]);
222                     hci_transport_h4_reset_statemachine();
223                     break;
224             }
225             break;
226 
227         case H4_W4_EVENT_HEADER:
228             bytes_to_read = hci_packet[2];
229             // check Event length
230             if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_EVENT_HEADER_SIZE)){
231                 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);
232                 hci_transport_h4_reset_statemachine();
233                 break;
234             }
235             h4_state = H4_W4_PAYLOAD;
236             break;
237 
238         case H4_W4_ACL_HEADER:
239             bytes_to_read = little_endian_read_16( hci_packet, 3);
240             // check ACL length
241             if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE)){
242                 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);
243                 hci_transport_h4_reset_statemachine();
244                 break;
245             }
246             h4_state = H4_W4_PAYLOAD;
247             break;
248 
249         case H4_W4_SCO_HEADER:
250             bytes_to_read = hci_packet[3];
251             // check SCO length
252             if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_SCO_HEADER_SIZE)){
253                 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);
254                 hci_transport_h4_reset_statemachine();
255                 break;
256             }
257             h4_state = H4_W4_PAYLOAD;
258             break;
259 
260         case H4_W4_PAYLOAD:
261 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
262             if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_IDLE
263             && memcmp(hci_packet, local_version_event_prefix, sizeof(local_version_event_prefix)) == 0){
264 #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
265                 if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
266                     // detect TI CC256x controller based on manufacturer
267                     log_info("Detected CC256x controller");
268                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED;
269                 } else {
270                     // work around not needed
271                     log_info("Bluetooth controller not by TI");
272                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE;
273                 }
274 #endif
275 #ifdef ENABLE_CYPRESS_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
276                 if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){
277                     // detect Cypress controller based on manufacturer
278                     log_info("Detected Cypress controller");
279                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED;
280                 } else {
281                     // work around not needed
282                     log_info("Bluetooth controller not by Cypress");
283                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE;
284                 }
285 #endif
286             }
287 #endif
288             // reset state machine before delivering packet to stack as it might close the transport
289             hci_transport_h4_reset_statemachine();
290             packet_handler(hci_packet[0], &hci_packet[1], read_pos-1);
291             break;
292 
293         case H4_OFF:
294             bytes_to_read = 0;
295             break;
296     }
297 
298 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
299     if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT){
300         baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_IDLE;
301         // avoid flowcontrol problem by reading expected hci command complete event of 7 bytes in a single block read
302         h4_state = H4_W4_PAYLOAD;
303         bytes_to_read = 7;
304     }
305 #endif
306 
307     if (h4_state != H4_OFF) {
308         hci_transport_h4_trigger_next_read();
309     }
310 }
311 
312 static void hci_transport_h4_block_sent(void){
313     switch (tx_state){
314         case TX_W4_PACKET_SENT:
315             // packet fully sent, reset state
316 #ifdef ENABLE_EHCILL
317             ehcill_tx_len = 0;
318 #endif
319             tx_state = TX_IDLE;
320 
321 #ifdef ENABLE_EHCILL
322             // notify eHCILL engine
323             hci_transport_h4_ehcill_handle_packet_sent();
324 #endif
325             // notify upper stack that it can send again
326             packet_handler(HCI_EVENT_PACKET, &packet_sent_event[0], sizeof(packet_sent_event));
327             break;
328 
329 #ifdef ENABLE_EHCILL
330         case TX_W4_EHCILL_SENT:
331         case TX_W4_WAKEUP:
332             hci_transport_h4_ehcill_handle_ehcill_command_sent();
333             break;
334 #endif
335 
336         default:
337             break;
338     }
339 }
340 
341 static int hci_transport_h4_can_send_now(uint8_t packet_type){
342     return tx_state == TX_IDLE;
343 }
344 
345 static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){
346 
347     // store packet type before actual data and increase size
348     size++;
349     packet--;
350     *packet = packet_type;
351 
352 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
353     if ((baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED)
354     && (memcmp(packet, baud_rate_command_prefix, sizeof(baud_rate_command_prefix)) == 0)) {
355         log_info("Baud rate command detected, expect command complete event next");
356         baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT;
357     }
358 #endif
359 
360 #ifdef ENABLE_EHCILL
361     // store request for later
362     ehcill_tx_len   = size;
363     ehcill_tx_data  = packet;
364     switch (ehcill_state){
365         case EHCILL_STATE_SLEEP:
366             hci_transport_h4_ehcill_trigger_wakeup();
367             return 0;
368         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
369             log_info("eHILL: send next packet, state EHCILL_STATE_W2_SEND_SLEEP_ACK");
370             return 0;
371         default:
372             break;
373     }
374 #endif
375 
376     // start sending
377     tx_state = TX_W4_PACKET_SENT;
378     btstack_uart->send_block(packet, size);
379     return 0;
380 }
381 
382 static void hci_transport_h4_init(const void * transport_config){
383     // check for hci_transport_config_uart_t
384     if (!transport_config) {
385         log_error("hci_transport_h4: no config!");
386         return;
387     }
388     if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) {
389         log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!");
390         return;
391     }
392 
393     // extract UART config from transport config
394     hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config;
395     uart_config.baudrate    = hci_transport_config_uart->baudrate_init;
396     uart_config.flowcontrol = hci_transport_config_uart->flowcontrol;
397     uart_config.device_name = hci_transport_config_uart->device_name;
398 
399     // set state to off
400     tx_state = TX_OFF;
401     h4_state = H4_OFF;
402 
403     // setup UART driver
404     btstack_uart->init(&uart_config);
405     btstack_uart->set_block_received(&hci_transport_h4_block_read);
406     btstack_uart->set_block_sent(&hci_transport_h4_block_sent);
407 }
408 
409 static int hci_transport_h4_open(void){
410     // open uart driver
411     int res = btstack_uart->open();
412     if (res){
413         return res;
414     }
415 
416     // init rx + tx state machines
417     hci_transport_h4_reset_statemachine();
418     hci_transport_h4_trigger_next_read();
419     tx_state = TX_IDLE;
420 
421 #ifdef ENABLE_EHCILL
422     hci_transport_h4_ehcill_open();
423 #endif
424     return 0;
425 }
426 
427 static int hci_transport_h4_close(void){
428     // set state to off
429     tx_state = TX_OFF;
430     h4_state = H4_OFF;
431 
432     // close uart driver
433     return btstack_uart->close();
434 }
435 
436 static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
437     packet_handler = handler;
438 }
439 
440 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
441 }
442 
443 //
444 // --- main part of eHCILL implementation ---
445 //
446 
447 #ifdef ENABLE_EHCILL
448 
449 static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){
450     static int last_state = 0;
451     if (sleep_active == last_state) return;
452     last_state = sleep_active;
453 
454     log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active);
455     uint8_t event[3];
456     event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE;
457     event[1] = sizeof(event) - 2;
458     event[2] = sleep_active;
459     packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
460 }
461 
462 static void hci_transport_h4_ehcill_wakeup_handler(void){
463 #ifdef ENABLE_LOG_EHCILL
464     log_info("eHCILL: UART wakeup received");
465 #endif
466     hci_transport_h4_ehcill_handle_command(EHCILL_WAKEUP_SIGNAL);
467 }
468 
469 static void hci_transport_h4_ehcill_open(void){
470     hci_transport_h4_ehcill_reset_statemachine();
471 
472     // find best sleep mode to use: wake on CTS, wake on RX, none
473     btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF;
474     int supported_sleep_modes = 0;
475     if (btstack_uart->get_supported_sleep_modes){
476         supported_sleep_modes = btstack_uart->get_supported_sleep_modes();
477     }
478     if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){
479         log_info("eHCILL: using wake on CTS");
480         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE;
481     } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){
482         log_info("eHCILL: using wake on RX");
483         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE;
484     } else {
485         log_info("eHCILL: UART driver does not provide compatible sleep mode");
486     }
487     if (btstack_uart->set_wakeup_handler){
488         btstack_uart->set_wakeup_handler(&hci_transport_h4_ehcill_wakeup_handler);
489     }
490 }
491 
492 static void hci_transport_h4_echill_send_wakeup_ind(void){
493 #ifdef ENABLE_LOG_EHCILL
494     log_info("eHCILL: send WAKEUP_IND");
495 #endif
496     // update state
497     tx_state     = TX_W4_WAKEUP;
498     ehcill_state = EHCILL_STATE_W4_WAKEUP_IND_OR_ACK;
499     ehcill_command_to_send = EHCILL_WAKE_UP_IND;
500     btstack_uart->send_block(&ehcill_command_to_send, 1);
501 }
502 
503 static int hci_transport_h4_ehcill_outgoing_packet_ready(void){
504     return ehcill_tx_len != 0;
505 }
506 
507 static void hci_transport_h4_ehcill_reset_statemachine(void){
508     ehcill_state = EHCILL_STATE_AWAKE;
509 }
510 
511 static void hci_transport_h4_ehcill_send_ehcill_command(void){
512 #ifdef ENABLE_LOG_EHCILL
513     log_info("eHCILL: send command %02x", ehcill_command_to_send);
514 #endif
515     tx_state = TX_W4_EHCILL_SENT;
516     if (ehcill_command_to_send == EHCILL_GO_TO_SLEEP_ACK){
517         ehcill_state = EHCILL_STATE_SLEEP;
518     }
519     btstack_uart->send_block(&ehcill_command_to_send, 1);
520 }
521 
522 static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){
523 	UNUSED(timer);
524 #ifdef ENABLE_LOG_EHCILL
525     log_info("eHCILL: timer triggered");
526 #endif
527     hci_transport_h4_ehcill_send_ehcill_command();
528 }
529 
530 static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){
531     // setup timer
532 #ifdef ENABLE_LOG_EHCILL
533     log_info("eHCILL: set timer for sending command %02x", ehcill_command_to_send);
534 #endif
535     btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler);
536     btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50);
537     btstack_run_loop_add_timer(&ehcill_sleep_ack_timer);
538 }
539 
540 static void hci_transport_h4_ehcill_trigger_wakeup(void){
541     switch (tx_state){
542         case TX_W2_EHCILL_SEND:
543         case TX_W4_EHCILL_SENT:
544             // wake up / sleep ack in progress, nothing to do now
545             return;
546         case TX_IDLE:
547         default:
548             // all clear, prepare for wakeup
549             break;
550     }
551     // UART needed again
552     hci_transport_h4_ehcill_emit_sleep_state(0);
553     if (btstack_uart_sleep_mode){
554         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
555     }
556     hci_transport_h4_echill_send_wakeup_ind();
557 }
558 
559 static void hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command){
560 #ifdef ENABLE_LOG_EHCILL
561     log_info("eHCILL: schedule eHCILL command %02x", command);
562 #endif
563     ehcill_command_to_send = command;
564     switch (tx_state){
565         case TX_IDLE:
566             if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){
567                 // send right away
568                 hci_transport_h4_ehcill_send_ehcill_command();
569             } else {
570                 // change state so BTstack cannot send and setup timer
571                 tx_state = TX_W2_EHCILL_SEND;
572                 hci_transport_h4_ehcill_sleep_ack_timer_setup();
573             }
574             break;
575         default:
576             break;
577     }
578 }
579 
580 static void hci_transport_h4_ehcill_handle_command(uint8_t action){
581     // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size);
582     switch(ehcill_state){
583         case EHCILL_STATE_AWAKE:
584             switch(action){
585                 case EHCILL_GO_TO_SLEEP_IND:
586                     ehcill_state = EHCILL_STATE_W2_SEND_SLEEP_ACK;
587 #ifdef ENABLE_LOG_EHCILL
588                     log_info("eHCILL: Received GO_TO_SLEEP_IND RX");
589 #endif
590                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_GO_TO_SLEEP_ACK);
591                     break;
592                 default:
593                     break;
594             }
595             break;
596 
597         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
598             switch(action){
599                 case EHCILL_WAKE_UP_IND:
600                     ehcill_state = EHCILL_STATE_AWAKE;
601                     hci_transport_h4_ehcill_emit_sleep_state(0);
602                     if (btstack_uart_sleep_mode){
603                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
604                     }
605 #ifdef ENABLE_LOG_EHCILL
606                     log_info("eHCILL: Received WAKE_UP_IND RX");
607 #endif
608                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK);
609                     break;
610 
611                 default:
612                     break;
613             }
614             break;
615 
616         case EHCILL_STATE_SLEEP:
617             switch(action){
618                 case EHCILL_WAKEUP_SIGNAL:
619                     hci_transport_h4_ehcill_emit_sleep_state(0);
620                     if (btstack_uart_sleep_mode){
621                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
622                     }
623                     break;
624                 case EHCILL_WAKE_UP_IND:
625                     ehcill_state = EHCILL_STATE_AWAKE;
626                     hci_transport_h4_ehcill_emit_sleep_state(0);
627                     if (btstack_uart_sleep_mode){
628                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
629                     }
630 #ifdef ENABLE_LOG_EHCILL
631                     log_info("eHCILL: Received WAKE_UP_IND RX");
632 #endif
633                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK);
634                     break;
635 
636                 default:
637                     break;
638             }
639             break;
640 
641         case EHCILL_STATE_W4_WAKEUP_IND_OR_ACK:
642             switch(action){
643                 case EHCILL_WAKE_UP_IND:
644                 case EHCILL_WAKE_UP_ACK:
645 #ifdef ENABLE_LOG_EHCILL
646                     log_info("eHCILL: Received WAKE_UP (%02x)", action);
647 #endif
648                     tx_state = TX_W4_PACKET_SENT;
649                     ehcill_state = EHCILL_STATE_AWAKE;
650                     btstack_uart->send_block(ehcill_tx_data, ehcill_tx_len);
651                     break;
652                 default:
653                     break;
654             }
655             break;
656     }
657 }
658 
659 static void hci_transport_h4_ehcill_handle_packet_sent(void){
660 #ifdef ENABLE_LOG_EHCILL
661         log_info("eHCILL: handle packet sent, command to send %02x", ehcill_command_to_send);
662 #endif
663     // now, send pending ehcill command if neccessary
664     switch (ehcill_command_to_send){
665         case EHCILL_GO_TO_SLEEP_ACK:
666             hci_transport_h4_ehcill_sleep_ack_timer_setup();
667             break;
668         case EHCILL_WAKE_UP_IND:
669             hci_transport_h4_ehcill_send_ehcill_command();
670             break;
671         default:
672             break;
673     }
674 }
675 
676 static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){
677     tx_state = TX_IDLE;
678     int command = ehcill_command_to_send;
679     ehcill_command_to_send = 0;
680 
681 #ifdef ENABLE_LOG_EHCILL
682         log_info("eHCILL: handle eHCILL sent, command was %02x", command);
683 #endif
684 
685     if (command == EHCILL_GO_TO_SLEEP_ACK) {
686 #ifdef ENABLE_LOG_EHCILL
687         log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode");
688 #endif
689         // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent
690         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
691             btstack_uart->set_sleep(btstack_uart_sleep_mode);
692         }
693         hci_transport_h4_ehcill_emit_sleep_state(1);
694     }
695     // already packet ready? then start wakeup
696     if (hci_transport_h4_ehcill_outgoing_packet_ready()){
697         hci_transport_h4_ehcill_emit_sleep_state(0);
698         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
699             btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
700         }
701         if (command != EHCILL_WAKE_UP_IND){
702             hci_transport_h4_echill_send_wakeup_ind();
703         }
704     }
705 }
706 
707 #endif
708 // --- end of eHCILL implementation ---------
709 
710 static const hci_transport_t hci_transport_h4 = {
711     /* const char * name; */                                        "H4",
712     /* void   (*init) (const void *transport_config); */            &hci_transport_h4_init,
713     /* int    (*open)(void); */                                     &hci_transport_h4_open,
714     /* int    (*close)(void); */                                    &hci_transport_h4_close,
715     /* void   (*register_packet_handler)(void (*handler)(...); */   &hci_transport_h4_register_packet_handler,
716     /* int    (*can_send_packet_now)(uint8_t packet_type); */       &hci_transport_h4_can_send_now,
717     /* int    (*send_packet)(...); */                               &hci_transport_h4_send_packet,
718     /* int    (*set_baudrate)(uint32_t baudrate); */                &hci_transport_h4_set_baudrate,
719     /* void   (*reset_link)(void); */                               NULL,
720     /* void   (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL,
721 };
722 
723 // configure and return h4 singleton
724 const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) {
725     btstack_uart = uart_driver;
726     return &hci_transport_h4;
727 }
728