xref: /btstack/src/hci_transport_h4.c (revision 2cc827d4920e09c7219a79fceb0577d37efd4025)
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  void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler;
140 
141 // packet reader state machine
142 static  H4_STATE h4_state;
143 static uint16_t bytes_to_read;
144 static uint16_t read_pos;
145 
146 // incoming packet buffer
147 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)
148 static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE];
149 
150 // Baudrate change bugs in TI CC256x and CYW20704
151 #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
152 #define ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
153 static const uint8_t baud_rate_command_prefix[]   = { 0x01, 0x36, 0xff, 0x04};
154 #endif
155 
156 #ifdef ENABLE_CYPRESS_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
157 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
158 #error "Please enable either ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND or ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND"
159 #endif
160 #define ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
161 static const uint8_t baud_rate_command_prefix[]   = { 0x01, 0x18, 0xfc, 0x06};
162 #endif
163 
164 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
165 static const uint8_t local_version_event_prefix[] = { 0x04, 0x0e, 0x0c, 0x01, 0x01, 0x10};
166 static enum {
167     BAUDRATE_CHANGE_WORKAROUND_IDLE,
168     BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED,
169     BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT,
170     BAUDRATE_CHANGE_WORKAROUND_DONE
171 } baudrate_change_workaround_state;
172 #endif
173 
174 static int hci_transport_h4_set_baudrate(uint32_t baudrate){
175     log_info("hci_transport_h4_set_baudrate %"PRIu32, baudrate);
176     return btstack_uart->set_baudrate(baudrate);
177 }
178 
179 static void hci_transport_h4_reset_statemachine(void){
180     h4_state = H4_W4_PACKET_TYPE;
181     read_pos = 0;
182     bytes_to_read = 1;
183 }
184 
185 static void hci_transport_h4_trigger_next_read(void){
186     // log_info("hci_transport_h4_trigger_next_read: %u bytes", bytes_to_read);
187     btstack_uart->receive_block(&hci_packet[read_pos], bytes_to_read);
188 }
189 
190 static void hci_transport_h4_packet_complete(void){
191 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
192     if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_IDLE
193             && memcmp(hci_packet, local_version_event_prefix, sizeof(local_version_event_prefix)) == 0){
194 #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
195                 if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
196                     // detect TI CC256x controller based on manufacturer
197                     log_info("Detected CC256x controller");
198                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED;
199                 } else {
200                     // work around not needed
201                     log_info("Bluetooth controller not by TI");
202                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE;
203                 }
204 #endif
205 #ifdef ENABLE_CYPRESS_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
206                 if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){
207                     // detect Cypress controller based on manufacturer
208                     log_info("Detected Cypress controller");
209                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED;
210                 } else {
211                     // work around not needed
212                     log_info("Bluetooth controller not by Cypress");
213                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE;
214                 }
215 #endif
216             }
217 #endif
218     uint16_t packet_len = read_pos-1u;
219 
220     // reset state machine before delivering packet to stack as it might close the transport
221     hci_transport_h4_reset_statemachine();
222     packet_handler(hci_packet[0], &hci_packet[1], packet_len);
223 }
224 
225 static void hci_transport_h4_block_read(void){
226 
227     read_pos += bytes_to_read;
228 
229     switch (h4_state) {
230         case H4_W4_PACKET_TYPE:
231             switch (hci_packet[0]){
232                 case HCI_EVENT_PACKET:
233                     bytes_to_read = HCI_EVENT_HEADER_SIZE;
234                     h4_state = H4_W4_EVENT_HEADER;
235                     break;
236                 case HCI_ACL_DATA_PACKET:
237                     bytes_to_read = HCI_ACL_HEADER_SIZE;
238                     h4_state = H4_W4_ACL_HEADER;
239                     break;
240                 case HCI_SCO_DATA_PACKET:
241                     bytes_to_read = HCI_SCO_HEADER_SIZE;
242                     h4_state = H4_W4_SCO_HEADER;
243                     break;
244 #ifdef ENABLE_EHCILL
245                 case EHCILL_GO_TO_SLEEP_IND:
246                 case EHCILL_GO_TO_SLEEP_ACK:
247                 case EHCILL_WAKE_UP_IND:
248                 case EHCILL_WAKE_UP_ACK:
249                     hci_transport_h4_ehcill_handle_command(hci_packet[0]);
250                     hci_transport_h4_reset_statemachine();
251                     break;
252 #endif
253                 default:
254                     log_error("hci_transport_h4: invalid packet type 0x%02x", hci_packet[0]);
255                     hci_transport_h4_reset_statemachine();
256                     break;
257             }
258             break;
259 
260         case H4_W4_EVENT_HEADER:
261             bytes_to_read = hci_packet[2];
262             // check Event length
263             if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_EVENT_HEADER_SIZE)){
264                 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);
265                 hci_transport_h4_reset_statemachine();
266                 break;
267             }
268             h4_state = H4_W4_PAYLOAD;
269             break;
270 
271         case H4_W4_ACL_HEADER:
272             bytes_to_read = little_endian_read_16( hci_packet, 3);
273             // check ACL length
274             if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE)){
275                 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);
276                 hci_transport_h4_reset_statemachine();
277                 break;
278             }
279             h4_state = H4_W4_PAYLOAD;
280             break;
281 
282         case H4_W4_SCO_HEADER:
283             bytes_to_read = hci_packet[3];
284             // check SCO length
285             if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_SCO_HEADER_SIZE)){
286                 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);
287                 hci_transport_h4_reset_statemachine();
288                 break;
289             }
290             h4_state = H4_W4_PAYLOAD;
291             break;
292 
293         case H4_W4_PAYLOAD:
294             hci_transport_h4_packet_complete();
295             break;
296 
297         case H4_OFF:
298             bytes_to_read = 0;
299             break;
300         default:
301             btstack_assert(false);
302             break;
303     }
304 
305 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
306     if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT){
307         baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_IDLE;
308         // avoid flowcontrol problem by reading expected hci command complete event of 7 bytes in a single block read
309         h4_state = H4_W4_PAYLOAD;
310         bytes_to_read = 7;
311     }
312 #endif
313 
314     // forward packet if payload size == 0
315     if (h4_state == H4_W4_PAYLOAD && bytes_to_read == 0u) {
316         hci_transport_h4_packet_complete();
317     }
318 
319     if (h4_state != H4_OFF) {
320         hci_transport_h4_trigger_next_read();
321     }
322 }
323 
324 static void hci_transport_h4_block_sent(void){
325 
326     static const uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
327 
328     switch (tx_state){
329         case TX_W4_PACKET_SENT:
330             // packet fully sent, reset state
331 #ifdef ENABLE_EHCILL
332             ehcill_tx_len = 0;
333 #endif
334             tx_state = TX_IDLE;
335 
336 #ifdef ENABLE_EHCILL
337             // notify eHCILL engine
338             hci_transport_h4_ehcill_handle_packet_sent();
339 #endif
340             // notify upper stack that it can send again
341             packet_handler(HCI_EVENT_PACKET, (uint8_t *) &packet_sent_event[0], sizeof(packet_sent_event));
342             break;
343 
344 #ifdef ENABLE_EHCILL
345         case TX_W4_EHCILL_SENT:
346         case TX_W4_WAKEUP:
347             hci_transport_h4_ehcill_handle_ehcill_command_sent();
348             break;
349 #endif
350 
351         default:
352             break;
353     }
354 }
355 
356 static int hci_transport_h4_can_send_now(uint8_t packet_type){
357     UNUSED(packet_type);
358     return tx_state == TX_IDLE;
359 }
360 
361 static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){
362 
363     // store packet type before actual data and increase size
364     size++;
365     packet--;
366     *packet = packet_type;
367 
368 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
369     if ((baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED)
370     && (memcmp(packet, baud_rate_command_prefix, sizeof(baud_rate_command_prefix)) == 0)) {
371         log_info("Baud rate command detected, expect command complete event next");
372         baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT;
373     }
374 #endif
375 
376 #ifdef ENABLE_EHCILL
377     // store request for later
378     ehcill_tx_len   = size;
379     ehcill_tx_data  = packet;
380     switch (ehcill_state){
381         case EHCILL_STATE_SLEEP:
382             hci_transport_h4_ehcill_trigger_wakeup();
383             return 0;
384         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
385             log_info("eHILL: send next packet, state EHCILL_STATE_W2_SEND_SLEEP_ACK");
386             return 0;
387         default:
388             break;
389     }
390 #endif
391 
392     // start sending
393     tx_state = TX_W4_PACKET_SENT;
394     btstack_uart->send_block(packet, size);
395     return 0;
396 }
397 
398 static void hci_transport_h4_init(const void * transport_config){
399     // check for hci_transport_config_uart_t
400     if (!transport_config) {
401         log_error("hci_transport_h4: no config!");
402         return;
403     }
404     if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) {
405         log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!");
406         return;
407     }
408 
409     // extract UART config from transport config
410     hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config;
411     uart_config.baudrate    = hci_transport_config_uart->baudrate_init;
412     uart_config.flowcontrol = hci_transport_config_uart->flowcontrol;
413     uart_config.device_name = hci_transport_config_uart->device_name;
414 
415     // set state to off
416     tx_state = TX_OFF;
417     h4_state = H4_OFF;
418 
419     // setup UART driver
420     btstack_uart->init(&uart_config);
421     btstack_uart->set_block_received(&hci_transport_h4_block_read);
422     btstack_uart->set_block_sent(&hci_transport_h4_block_sent);
423 }
424 
425 static int hci_transport_h4_open(void){
426     // open uart driver
427     int res = btstack_uart->open();
428     if (res != 0){
429         return res;
430     }
431 
432     // init rx + tx state machines
433     hci_transport_h4_reset_statemachine();
434     hci_transport_h4_trigger_next_read();
435     tx_state = TX_IDLE;
436 
437 #ifdef ENABLE_EHCILL
438     hci_transport_h4_ehcill_open();
439 #endif
440     return 0;
441 }
442 
443 static int hci_transport_h4_close(void){
444     // set state to off
445     tx_state = TX_OFF;
446     h4_state = H4_OFF;
447 
448     // close uart driver
449     return btstack_uart->close();
450 }
451 
452 static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
453     packet_handler = handler;
454 }
455 
456 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
457     UNUSED(packet_type);
458     UNUSED(packet);
459     UNUSED(size);
460 }
461 
462 //
463 // --- main part of eHCILL implementation ---
464 //
465 
466 #ifdef ENABLE_EHCILL
467 
468 static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){
469     static int last_state = 0;
470     if (sleep_active == last_state) return;
471     last_state = sleep_active;
472 
473     log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active);
474     uint8_t event[3];
475     event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE;
476     event[1] = sizeof(event) - 2;
477     event[2] = sleep_active;
478     packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
479 }
480 
481 static void hci_transport_h4_ehcill_wakeup_handler(void){
482 #ifdef ENABLE_LOG_EHCILL
483     log_info("eHCILL: UART wakeup received");
484 #endif
485     hci_transport_h4_ehcill_handle_command(EHCILL_WAKEUP_SIGNAL);
486 }
487 
488 static void hci_transport_h4_ehcill_open(void){
489     hci_transport_h4_ehcill_reset_statemachine();
490 
491     // find best sleep mode to use: wake on CTS, wake on RX, none
492     btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF;
493     int supported_sleep_modes = 0;
494     if (btstack_uart->get_supported_sleep_modes){
495         supported_sleep_modes = btstack_uart->get_supported_sleep_modes();
496     }
497     if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){
498         log_info("eHCILL: using wake on CTS");
499         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE;
500     } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){
501         log_info("eHCILL: using wake on RX");
502         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE;
503     } else {
504         log_info("eHCILL: UART driver does not provide compatible sleep mode");
505     }
506     if (btstack_uart->set_wakeup_handler){
507         btstack_uart->set_wakeup_handler(&hci_transport_h4_ehcill_wakeup_handler);
508     }
509 }
510 
511 static void hci_transport_h4_echill_send_wakeup_ind(void){
512 #ifdef ENABLE_LOG_EHCILL
513     log_info("eHCILL: send WAKEUP_IND");
514 #endif
515     // update state
516     tx_state     = TX_W4_WAKEUP;
517     ehcill_state = EHCILL_STATE_W4_WAKEUP_IND_OR_ACK;
518     ehcill_command_to_send = EHCILL_WAKE_UP_IND;
519     btstack_uart->send_block(&ehcill_command_to_send, 1);
520 }
521 
522 static int hci_transport_h4_ehcill_outgoing_packet_ready(void){
523     return ehcill_tx_len != 0;
524 }
525 
526 static void hci_transport_h4_ehcill_reset_statemachine(void){
527     ehcill_state = EHCILL_STATE_AWAKE;
528 }
529 
530 static void hci_transport_h4_ehcill_send_ehcill_command(void){
531 #ifdef ENABLE_LOG_EHCILL
532     log_info("eHCILL: send command %02x", ehcill_command_to_send);
533 #endif
534     tx_state = TX_W4_EHCILL_SENT;
535     if (ehcill_command_to_send == EHCILL_GO_TO_SLEEP_ACK){
536         ehcill_state = EHCILL_STATE_SLEEP;
537     }
538     btstack_uart->send_block(&ehcill_command_to_send, 1);
539 }
540 
541 static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){
542 	UNUSED(timer);
543 #ifdef ENABLE_LOG_EHCILL
544     log_info("eHCILL: timer triggered");
545 #endif
546     hci_transport_h4_ehcill_send_ehcill_command();
547 }
548 
549 static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){
550     // setup timer
551 #ifdef ENABLE_LOG_EHCILL
552     log_info("eHCILL: set timer for sending command %02x", ehcill_command_to_send);
553 #endif
554     btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler);
555     btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50);
556     btstack_run_loop_add_timer(&ehcill_sleep_ack_timer);
557 }
558 
559 static void hci_transport_h4_ehcill_trigger_wakeup(void){
560     switch (tx_state){
561         case TX_W2_EHCILL_SEND:
562         case TX_W4_EHCILL_SENT:
563             // wake up / sleep ack in progress, nothing to do now
564             return;
565         case TX_IDLE:
566         default:
567             // all clear, prepare for wakeup
568             break;
569     }
570     // UART needed again
571     hci_transport_h4_ehcill_emit_sleep_state(0);
572     if (btstack_uart_sleep_mode){
573         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
574     }
575     hci_transport_h4_echill_send_wakeup_ind();
576 }
577 
578 static void hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command){
579 #ifdef ENABLE_LOG_EHCILL
580     log_info("eHCILL: schedule eHCILL command %02x", command);
581 #endif
582     ehcill_command_to_send = command;
583     switch (tx_state){
584         case TX_IDLE:
585             if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){
586                 // send right away
587                 hci_transport_h4_ehcill_send_ehcill_command();
588             } else {
589                 // change state so BTstack cannot send and setup timer
590                 tx_state = TX_W2_EHCILL_SEND;
591                 hci_transport_h4_ehcill_sleep_ack_timer_setup();
592             }
593             break;
594         default:
595             break;
596     }
597 }
598 
599 static void hci_transport_h4_ehcill_handle_command(uint8_t action){
600     // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size);
601     switch(ehcill_state){
602         case EHCILL_STATE_AWAKE:
603             switch(action){
604                 case EHCILL_GO_TO_SLEEP_IND:
605                     ehcill_state = EHCILL_STATE_W2_SEND_SLEEP_ACK;
606 #ifdef ENABLE_LOG_EHCILL
607                     log_info("eHCILL: Received GO_TO_SLEEP_IND RX");
608 #endif
609                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_GO_TO_SLEEP_ACK);
610                     break;
611                 default:
612                     break;
613             }
614             break;
615 
616         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
617             switch(action){
618                 case EHCILL_WAKE_UP_IND:
619                     ehcill_state = EHCILL_STATE_AWAKE;
620                     hci_transport_h4_ehcill_emit_sleep_state(0);
621                     if (btstack_uart_sleep_mode){
622                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
623                     }
624 #ifdef ENABLE_LOG_EHCILL
625                     log_info("eHCILL: Received WAKE_UP_IND RX");
626 #endif
627                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK);
628                     break;
629 
630                 default:
631                     break;
632             }
633             break;
634 
635         case EHCILL_STATE_SLEEP:
636             switch(action){
637                 case EHCILL_WAKEUP_SIGNAL:
638                     hci_transport_h4_ehcill_emit_sleep_state(0);
639                     if (btstack_uart_sleep_mode){
640                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
641                     }
642                     break;
643                 case EHCILL_WAKE_UP_IND:
644                     ehcill_state = EHCILL_STATE_AWAKE;
645                     hci_transport_h4_ehcill_emit_sleep_state(0);
646                     if (btstack_uart_sleep_mode){
647                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
648                     }
649 #ifdef ENABLE_LOG_EHCILL
650                     log_info("eHCILL: Received WAKE_UP_IND RX");
651 #endif
652                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK);
653                     break;
654 
655                 default:
656                     break;
657             }
658             break;
659 
660         case EHCILL_STATE_W4_WAKEUP_IND_OR_ACK:
661             switch(action){
662                 case EHCILL_WAKE_UP_IND:
663                 case EHCILL_WAKE_UP_ACK:
664 #ifdef ENABLE_LOG_EHCILL
665                     log_info("eHCILL: Received WAKE_UP (%02x)", action);
666 #endif
667                     tx_state = TX_W4_PACKET_SENT;
668                     ehcill_state = EHCILL_STATE_AWAKE;
669                     btstack_uart->send_block(ehcill_tx_data, ehcill_tx_len);
670                     break;
671                 default:
672                     break;
673             }
674             break;
675     }
676 }
677 
678 static void hci_transport_h4_ehcill_handle_packet_sent(void){
679 #ifdef ENABLE_LOG_EHCILL
680         log_info("eHCILL: handle packet sent, command to send %02x", ehcill_command_to_send);
681 #endif
682     // now, send pending ehcill command if neccessary
683     switch (ehcill_command_to_send){
684         case EHCILL_GO_TO_SLEEP_ACK:
685             hci_transport_h4_ehcill_sleep_ack_timer_setup();
686             break;
687         case EHCILL_WAKE_UP_IND:
688             hci_transport_h4_ehcill_send_ehcill_command();
689             break;
690         default:
691             break;
692     }
693 }
694 
695 static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){
696     tx_state = TX_IDLE;
697     int command = ehcill_command_to_send;
698     ehcill_command_to_send = 0;
699 
700 #ifdef ENABLE_LOG_EHCILL
701         log_info("eHCILL: handle eHCILL sent, command was %02x", command);
702 #endif
703 
704     if (command == EHCILL_GO_TO_SLEEP_ACK) {
705 #ifdef ENABLE_LOG_EHCILL
706         log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode");
707 #endif
708         // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent
709         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
710             btstack_uart->set_sleep(btstack_uart_sleep_mode);
711         }
712         hci_transport_h4_ehcill_emit_sleep_state(1);
713     }
714     // already packet ready? then start wakeup
715     if (hci_transport_h4_ehcill_outgoing_packet_ready()){
716         hci_transport_h4_ehcill_emit_sleep_state(0);
717         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
718             btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
719         }
720         if (command != EHCILL_WAKE_UP_IND){
721             hci_transport_h4_echill_send_wakeup_ind();
722         }
723     }
724 }
725 
726 #endif
727 // --- end of eHCILL implementation ---------
728 
729 
730 // configure and return h4 singleton
731 const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) {
732 
733     static const hci_transport_t hci_transport_h4 = {
734             /* const char * name; */                                        "H4",
735             /* void   (*init) (const void *transport_config); */            &hci_transport_h4_init,
736             /* int    (*open)(void); */                                     &hci_transport_h4_open,
737             /* int    (*close)(void); */                                    &hci_transport_h4_close,
738             /* void   (*register_packet_handler)(void (*handler)(...); */   &hci_transport_h4_register_packet_handler,
739             /* int    (*can_send_packet_now)(uint8_t packet_type); */       &hci_transport_h4_can_send_now,
740             /* int    (*send_packet)(...); */                               &hci_transport_h4_send_packet,
741             /* int    (*set_baudrate)(uint32_t baudrate); */                &hci_transport_h4_set_baudrate,
742             /* void   (*reset_link)(void); */                               NULL,
743             /* void   (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL,
744     };
745 
746     btstack_uart = uart_driver;
747     return &hci_transport_h4;
748 }
749