xref: /btstack/src/hci_transport_h4.c (revision ad254f5df72afc98c915b8e568d3f5533da8d0f3)
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             packet_handler(hci_packet[0], &hci_packet[1], read_pos-1);
289             hci_transport_h4_reset_statemachine();
290             break;
291 
292         case H4_OFF:
293             bytes_to_read = 0;
294             return;
295     }
296 
297 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
298     if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT){
299         baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_IDLE;
300         // avoid flowcontrol problem by reading expected hci command complete event of 7 bytes in a single block read
301         h4_state = H4_W4_PAYLOAD;
302         bytes_to_read = 7;
303     }
304 #endif
305 
306     hci_transport_h4_trigger_next_read();
307 }
308 
309 static void hci_transport_h4_block_sent(void){
310     switch (tx_state){
311         case TX_W4_PACKET_SENT:
312             // packet fully sent, reset state
313 #ifdef ENABLE_EHCILL
314             ehcill_tx_len = 0;
315 #endif
316             tx_state = TX_IDLE;
317 
318 #ifdef ENABLE_EHCILL
319             // notify eHCILL engine
320             hci_transport_h4_ehcill_handle_packet_sent();
321 #endif
322             // notify upper stack that it can send again
323             packet_handler(HCI_EVENT_PACKET, &packet_sent_event[0], sizeof(packet_sent_event));
324             break;
325 
326 #ifdef ENABLE_EHCILL
327         case TX_W4_EHCILL_SENT:
328         case TX_W4_WAKEUP:
329             hci_transport_h4_ehcill_handle_ehcill_command_sent();
330             break;
331 #endif
332 
333         default:
334             break;
335     }
336 }
337 
338 static int hci_transport_h4_can_send_now(uint8_t packet_type){
339     return tx_state == TX_IDLE;
340 }
341 
342 static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){
343 
344     // store packet type before actual data and increase size
345     size++;
346     packet--;
347     *packet = packet_type;
348 
349 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
350     if ((baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED)
351     && (memcmp(packet, baud_rate_command_prefix, sizeof(baud_rate_command_prefix)) == 0)) {
352         log_info("Baud rate command detected, expect command complete event next");
353         baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT;
354     }
355 #endif
356 
357 #ifdef ENABLE_EHCILL
358     // store request for later
359     ehcill_tx_len   = size;
360     ehcill_tx_data  = packet;
361     switch (ehcill_state){
362         case EHCILL_STATE_SLEEP:
363             hci_transport_h4_ehcill_trigger_wakeup();
364             return 0;
365         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
366             log_info("eHILL: send next packet, state EHCILL_STATE_W2_SEND_SLEEP_ACK");
367             return 0;
368         default:
369             break;
370     }
371 #endif
372 
373     // start sending
374     tx_state = TX_W4_PACKET_SENT;
375     btstack_uart->send_block(packet, size);
376     return 0;
377 }
378 
379 static void hci_transport_h4_init(const void * transport_config){
380     // check for hci_transport_config_uart_t
381     if (!transport_config) {
382         log_error("hci_transport_h4: no config!");
383         return;
384     }
385     if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) {
386         log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!");
387         return;
388     }
389 
390     // extract UART config from transport config
391     hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config;
392     uart_config.baudrate    = hci_transport_config_uart->baudrate_init;
393     uart_config.flowcontrol = hci_transport_config_uart->flowcontrol;
394     uart_config.device_name = hci_transport_config_uart->device_name;
395 
396     // set state to off
397     tx_state = TX_OFF;
398     h4_state = H4_OFF;
399 
400     // setup UART driver
401     btstack_uart->init(&uart_config);
402     btstack_uart->set_block_received(&hci_transport_h4_block_read);
403     btstack_uart->set_block_sent(&hci_transport_h4_block_sent);
404 }
405 
406 static int hci_transport_h4_open(void){
407     // open uart driver
408     int res = btstack_uart->open();
409     if (res){
410         return res;
411     }
412 
413     // init rx + tx state machines
414     hci_transport_h4_reset_statemachine();
415     hci_transport_h4_trigger_next_read();
416     tx_state = TX_IDLE;
417 
418 #ifdef ENABLE_EHCILL
419     hci_transport_h4_ehcill_open();
420 #endif
421     return 0;
422 }
423 
424 static int hci_transport_h4_close(void){
425     // set state to off
426     tx_state = TX_OFF;
427     h4_state = H4_OFF;
428 
429     // close uart driver
430     return btstack_uart->close();
431 }
432 
433 static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
434     packet_handler = handler;
435 }
436 
437 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
438 }
439 
440 //
441 // --- main part of eHCILL implementation ---
442 //
443 
444 #ifdef ENABLE_EHCILL
445 
446 static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){
447     static int last_state = 0;
448     if (sleep_active == last_state) return;
449     last_state = sleep_active;
450 
451     log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active);
452     uint8_t event[3];
453     event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE;
454     event[1] = sizeof(event) - 2;
455     event[2] = sleep_active;
456     packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
457 }
458 
459 static void hci_transport_h4_ehcill_wakeup_handler(void){
460 #ifdef ENABLE_LOG_EHCILL
461     log_info("eHCILL: UART wakeup received");
462 #endif
463     hci_transport_h4_ehcill_handle_command(EHCILL_WAKEUP_SIGNAL);
464 }
465 
466 static void hci_transport_h4_ehcill_open(void){
467     hci_transport_h4_ehcill_reset_statemachine();
468 
469     // find best sleep mode to use: wake on CTS, wake on RX, none
470     btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF;
471     int supported_sleep_modes = 0;
472     if (btstack_uart->get_supported_sleep_modes){
473         supported_sleep_modes = btstack_uart->get_supported_sleep_modes();
474     }
475     if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){
476         log_info("eHCILL: using wake on CTS");
477         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE;
478     } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){
479         log_info("eHCILL: using wake on RX");
480         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE;
481     } else {
482         log_info("eHCILL: UART driver does not provide compatible sleep mode");
483     }
484     if (btstack_uart->set_wakeup_handler){
485         btstack_uart->set_wakeup_handler(&hci_transport_h4_ehcill_wakeup_handler);
486     }
487 }
488 
489 static void hci_transport_h4_echill_send_wakeup_ind(void){
490 #ifdef ENABLE_LOG_EHCILL
491     log_info("eHCILL: send WAKEUP_IND");
492 #endif
493     // update state
494     tx_state     = TX_W4_WAKEUP;
495     ehcill_state = EHCILL_STATE_W4_WAKEUP_IND_OR_ACK;
496     ehcill_command_to_send = EHCILL_WAKE_UP_IND;
497     btstack_uart->send_block(&ehcill_command_to_send, 1);
498 }
499 
500 static int hci_transport_h4_ehcill_outgoing_packet_ready(void){
501     return ehcill_tx_len != 0;
502 }
503 
504 static void hci_transport_h4_ehcill_reset_statemachine(void){
505     ehcill_state = EHCILL_STATE_AWAKE;
506 }
507 
508 static void hci_transport_h4_ehcill_send_ehcill_command(void){
509 #ifdef ENABLE_LOG_EHCILL
510     log_info("eHCILL: send command %02x", ehcill_command_to_send);
511 #endif
512     tx_state = TX_W4_EHCILL_SENT;
513     if (ehcill_command_to_send == EHCILL_GO_TO_SLEEP_ACK){
514         ehcill_state = EHCILL_STATE_SLEEP;
515     }
516     btstack_uart->send_block(&ehcill_command_to_send, 1);
517 }
518 
519 static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){
520 	UNUSED(timer);
521 #ifdef ENABLE_LOG_EHCILL
522     log_info("eHCILL: timer triggered");
523 #endif
524     hci_transport_h4_ehcill_send_ehcill_command();
525 }
526 
527 static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){
528     // setup timer
529 #ifdef ENABLE_LOG_EHCILL
530     log_info("eHCILL: set timer for sending command %02x", ehcill_command_to_send);
531 #endif
532     btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler);
533     btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50);
534     btstack_run_loop_add_timer(&ehcill_sleep_ack_timer);
535 }
536 
537 static void hci_transport_h4_ehcill_trigger_wakeup(void){
538     switch (tx_state){
539         case TX_W2_EHCILL_SEND:
540         case TX_W4_EHCILL_SENT:
541             // wake up / sleep ack in progress, nothing to do now
542             return;
543         case TX_IDLE:
544         default:
545             // all clear, prepare for wakeup
546             break;
547     }
548     // UART needed again
549     hci_transport_h4_ehcill_emit_sleep_state(0);
550     if (btstack_uart_sleep_mode){
551         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
552     }
553     hci_transport_h4_echill_send_wakeup_ind();
554 }
555 
556 static void hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command){
557 #ifdef ENABLE_LOG_EHCILL
558     log_info("eHCILL: schedule eHCILL command %02x", command);
559 #endif
560     ehcill_command_to_send = command;
561     switch (tx_state){
562         case TX_IDLE:
563             if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){
564                 // send right away
565                 hci_transport_h4_ehcill_send_ehcill_command();
566             } else {
567                 // change state so BTstack cannot send and setup timer
568                 tx_state = TX_W2_EHCILL_SEND;
569                 hci_transport_h4_ehcill_sleep_ack_timer_setup();
570             }
571             break;
572         default:
573             break;
574     }
575 }
576 
577 static void hci_transport_h4_ehcill_handle_command(uint8_t action){
578     // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size);
579     switch(ehcill_state){
580         case EHCILL_STATE_AWAKE:
581             switch(action){
582                 case EHCILL_GO_TO_SLEEP_IND:
583                     ehcill_state = EHCILL_STATE_W2_SEND_SLEEP_ACK;
584 #ifdef ENABLE_LOG_EHCILL
585                     log_info("eHCILL: Received GO_TO_SLEEP_IND RX");
586 #endif
587                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_GO_TO_SLEEP_ACK);
588                     break;
589                 default:
590                     break;
591             }
592             break;
593 
594         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
595             switch(action){
596                 case EHCILL_WAKE_UP_IND:
597                     ehcill_state = EHCILL_STATE_AWAKE;
598                     hci_transport_h4_ehcill_emit_sleep_state(0);
599                     if (btstack_uart_sleep_mode){
600                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
601                     }
602 #ifdef ENABLE_LOG_EHCILL
603                     log_info("eHCILL: Received WAKE_UP_IND RX");
604 #endif
605                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK);
606                     break;
607 
608                 default:
609                     break;
610             }
611             break;
612 
613         case EHCILL_STATE_SLEEP:
614             switch(action){
615                 case EHCILL_WAKEUP_SIGNAL:
616                     hci_transport_h4_ehcill_emit_sleep_state(0);
617                     if (btstack_uart_sleep_mode){
618                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
619                     }
620                     break;
621                 case EHCILL_WAKE_UP_IND:
622                     ehcill_state = EHCILL_STATE_AWAKE;
623                     hci_transport_h4_ehcill_emit_sleep_state(0);
624                     if (btstack_uart_sleep_mode){
625                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
626                     }
627 #ifdef ENABLE_LOG_EHCILL
628                     log_info("eHCILL: Received WAKE_UP_IND RX");
629 #endif
630                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK);
631                     break;
632 
633                 default:
634                     break;
635             }
636             break;
637 
638         case EHCILL_STATE_W4_WAKEUP_IND_OR_ACK:
639             switch(action){
640                 case EHCILL_WAKE_UP_IND:
641                 case EHCILL_WAKE_UP_ACK:
642 #ifdef ENABLE_LOG_EHCILL
643                     log_info("eHCILL: Received WAKE_UP (%02x)", action);
644 #endif
645                     tx_state = TX_W4_PACKET_SENT;
646                     ehcill_state = EHCILL_STATE_AWAKE;
647                     btstack_uart->send_block(ehcill_tx_data, ehcill_tx_len);
648                     break;
649                 default:
650                     break;
651             }
652             break;
653     }
654 }
655 
656 static void hci_transport_h4_ehcill_handle_packet_sent(void){
657 #ifdef ENABLE_LOG_EHCILL
658         log_info("eHCILL: handle packet sent, command to send %02x", ehcill_command_to_send);
659 #endif
660     // now, send pending ehcill command if neccessary
661     switch (ehcill_command_to_send){
662         case EHCILL_GO_TO_SLEEP_ACK:
663             hci_transport_h4_ehcill_sleep_ack_timer_setup();
664             break;
665         case EHCILL_WAKE_UP_IND:
666             hci_transport_h4_ehcill_send_ehcill_command();
667             break;
668         default:
669             break;
670     }
671 }
672 
673 static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){
674     tx_state = TX_IDLE;
675     int command = ehcill_command_to_send;
676     ehcill_command_to_send = 0;
677 
678 #ifdef ENABLE_LOG_EHCILL
679         log_info("eHCILL: handle eHCILL sent, command was %02x", command);
680 #endif
681 
682     if (command == EHCILL_GO_TO_SLEEP_ACK) {
683 #ifdef ENABLE_LOG_EHCILL
684         log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode");
685 #endif
686         // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent
687         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
688             btstack_uart->set_sleep(btstack_uart_sleep_mode);
689         }
690         hci_transport_h4_ehcill_emit_sleep_state(1);
691     }
692     // already packet ready? then start wakeup
693     if (hci_transport_h4_ehcill_outgoing_packet_ready()){
694         hci_transport_h4_ehcill_emit_sleep_state(0);
695         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
696             btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
697         }
698         if (command != EHCILL_WAKE_UP_IND){
699             hci_transport_h4_echill_send_wakeup_ind();
700         }
701     }
702 }
703 
704 #endif
705 // --- end of eHCILL implementation ---------
706 
707 static const hci_transport_t hci_transport_h4 = {
708     /* const char * name; */                                        "H4",
709     /* void   (*init) (const void *transport_config); */            &hci_transport_h4_init,
710     /* int    (*open)(void); */                                     &hci_transport_h4_open,
711     /* int    (*close)(void); */                                    &hci_transport_h4_close,
712     /* void   (*register_packet_handler)(void (*handler)(...); */   &hci_transport_h4_register_packet_handler,
713     /* int    (*can_send_packet_now)(uint8_t packet_type); */       &hci_transport_h4_can_send_now,
714     /* int    (*send_packet)(...); */                               &hci_transport_h4_send_packet,
715     /* int    (*set_baudrate)(uint32_t baudrate); */                &hci_transport_h4_set_baudrate,
716     /* void   (*reset_link)(void); */                               NULL,
717     /* void   (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL,
718 };
719 
720 // configure and return h4 singleton
721 const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) {
722     btstack_uart = uart_driver;
723     return &hci_transport_h4;
724 }
725