xref: /btstack/src/hci_transport_h4.c (revision d8910fa82b24f5941815f70baed665e14b519848)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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 
49bd021c4eSMatthias Ringwald #include "btstack_config.h"
50c8dfe071SMatthias Ringwald #include "hci_transport_h4.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 
58c8dfe071SMatthias Ringwald #include <inttypes.h>
59c8dfe071SMatthias Ringwald 
609ecd1ba8SMatthias Ringwald #define ENABLE_LOG_EHCILL
619ecd1ba8SMatthias Ringwald 
62f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
63307a4fe3SMatthias Ringwald 
64307a4fe3SMatthias Ringwald // eHCILL commands
65c4e86052SMatthias Ringwald enum EHCILL_MESSAGES {
66c4e86052SMatthias Ringwald 	EHCILL_GO_TO_SLEEP_IND = 0x030,
67c4e86052SMatthias Ringwald 	EHCILL_GO_TO_SLEEP_ACK = 0x031,
68c4e86052SMatthias Ringwald 	EHCILL_WAKE_UP_IND     = 0x032,
69c4e86052SMatthias Ringwald 	EHCILL_WAKE_UP_ACK     = 0x033,
70b403028eSMatthias Ringwald 	EHCILL_WAKEUP_SIGNAL   = 0x034,
71c4e86052SMatthias Ringwald };
72307a4fe3SMatthias Ringwald 
738a23fc53SMatthias Ringwald static int  hci_transport_h4_ehcill_outgoing_packet_ready(void);
748a23fc53SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void);
758a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action);
768a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void);
778a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void);
788a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void);
79307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void);
80307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void);
81307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void);
828a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void);
83307a4fe3SMatthias Ringwald 
84307a4fe3SMatthias Ringwald typedef enum {
859ecd1ba8SMatthias Ringwald     EHCILL_STATE_W2_SEND_SLEEP_ACK,
86307a4fe3SMatthias Ringwald     EHCILL_STATE_SLEEP,
879ecd1ba8SMatthias Ringwald     EHCILL_STATE_W4_WAKEUP_IND_OR_ACK,
88307a4fe3SMatthias Ringwald     EHCILL_STATE_AWAKE
89307a4fe3SMatthias Ringwald } EHCILL_STATE;
90307a4fe3SMatthias Ringwald 
91307a4fe3SMatthias Ringwald // eHCILL state machine
92307a4fe3SMatthias Ringwald static EHCILL_STATE ehcill_state;
93307a4fe3SMatthias Ringwald static uint8_t      ehcill_command_to_send;
94307a4fe3SMatthias Ringwald 
95f6a20ec9SMatthias Ringwald static btstack_uart_sleep_mode_t btstack_uart_sleep_mode;
96f6a20ec9SMatthias Ringwald 
97307a4fe3SMatthias Ringwald // work around for eHCILL problem
98307a4fe3SMatthias Ringwald static btstack_timer_source_t ehcill_sleep_ack_timer;
99307a4fe3SMatthias Ringwald 
100bd021c4eSMatthias Ringwald #endif
101bd021c4eSMatthias Ringwald 
10282d05f16SMatthias Ringwald 
103bd021c4eSMatthias Ringwald // assert pre-buffer for packet type is available
104bd021c4eSMatthias Ringwald #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0)
105bd021c4eSMatthias Ringwald #error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h
106bd021c4eSMatthias Ringwald #endif
107bd021c4eSMatthias Ringwald 
108bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
109bd021c4eSMatthias Ringwald 
110bd021c4eSMatthias Ringwald typedef enum {
111c682b8ecSMatthias Ringwald     H4_OFF,
112bd021c4eSMatthias Ringwald     H4_W4_PACKET_TYPE,
113bd021c4eSMatthias Ringwald     H4_W4_EVENT_HEADER,
114bd021c4eSMatthias Ringwald     H4_W4_ACL_HEADER,
115bd021c4eSMatthias Ringwald     H4_W4_SCO_HEADER,
1160d5b1bafSMatthias Ringwald     H4_W4_ISO_HEADER,
117bd021c4eSMatthias Ringwald     H4_W4_PAYLOAD,
118bd021c4eSMatthias Ringwald } H4_STATE;
119bd021c4eSMatthias Ringwald 
120307a4fe3SMatthias Ringwald typedef enum {
121c682b8ecSMatthias Ringwald     TX_OFF,
122c682b8ecSMatthias Ringwald     TX_IDLE,
123307a4fe3SMatthias Ringwald     TX_W4_PACKET_SENT,
124f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
125307a4fe3SMatthias Ringwald     TX_W4_WAKEUP,
126307a4fe3SMatthias Ringwald     TX_W2_EHCILL_SEND,
127307a4fe3SMatthias Ringwald     TX_W4_EHCILL_SENT,
128307a4fe3SMatthias Ringwald #endif
129307a4fe3SMatthias Ringwald } TX_STATE;
130307a4fe3SMatthias Ringwald 
131bd021c4eSMatthias Ringwald // UART Driver + Config
132f9bd6dd7SMatthias Ringwald static const btstack_uart_t * btstack_uart;
13348cdff9cSMilanka Ringwald static btstack_uart_config_t hci_transport_h4_uart_config;
134bd021c4eSMatthias Ringwald 
135307a4fe3SMatthias Ringwald // write state
1368a23fc53SMatthias Ringwald static TX_STATE tx_state;
1372e25934bSMatthias Ringwald #ifdef ENABLE_EHCILL
1382e25934bSMatthias Ringwald static uint8_t * ehcill_tx_data;
1392e25934bSMatthias Ringwald static uint16_t  ehcill_tx_len;   // 0 == no outgoing packet
1402e25934bSMatthias Ringwald #endif
1418a23fc53SMatthias Ringwald 
142b45b7749SMilanka Ringwald static void (*hci_transport_h4_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler;
143bd021c4eSMatthias Ringwald 
144bd021c4eSMatthias Ringwald // packet reader state machine
145bd021c4eSMatthias Ringwald static  H4_STATE h4_state;
146ea374553SMatthias Ringwald static uint16_t bytes_to_read;
147ea374553SMatthias Ringwald static uint16_t read_pos;
148bd021c4eSMatthias Ringwald 
149bd021c4eSMatthias Ringwald // incoming packet buffer
150fc6cde64SMatthias 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)
151bd021c4eSMatthias Ringwald static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE];
152bd021c4eSMatthias Ringwald 
15377291b13SMatthias Ringwald // Baudrate change bugs in TI CC256x and CYW20704
15439e7ee9fSMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
15577291b13SMatthias Ringwald #define ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
15639e7ee9fSMatthias Ringwald static const uint8_t baud_rate_command_prefix[]   = { 0x01, 0x36, 0xff, 0x04};
15777291b13SMatthias Ringwald #endif
15877291b13SMatthias Ringwald 
15977291b13SMatthias Ringwald #ifdef ENABLE_CYPRESS_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
16077291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
16177291b13SMatthias Ringwald #error "Please enable either ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND or ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND"
16277291b13SMatthias Ringwald #endif
16377291b13SMatthias Ringwald #define ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
16477291b13SMatthias Ringwald static const uint8_t baud_rate_command_prefix[]   = { 0x01, 0x18, 0xfc, 0x06};
16577291b13SMatthias Ringwald #endif
16677291b13SMatthias Ringwald 
16777291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
16877291b13SMatthias Ringwald static const uint8_t local_version_event_prefix[] = { 0x04, 0x0e, 0x0c, 0x01, 0x01, 0x10};
16939e7ee9fSMatthias Ringwald static enum {
1707741d2d0SMatthias Ringwald     BAUDRATE_CHANGE_WORKAROUND_IDLE,
1717741d2d0SMatthias Ringwald     BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED,
1727741d2d0SMatthias Ringwald     BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT,
1737741d2d0SMatthias Ringwald     BAUDRATE_CHANGE_WORKAROUND_DONE
1747741d2d0SMatthias Ringwald } baudrate_change_workaround_state;
17539e7ee9fSMatthias Ringwald #endif
17639e7ee9fSMatthias Ringwald 
hci_transport_h4_set_baudrate(uint32_t baudrate)177bd021c4eSMatthias Ringwald static int hci_transport_h4_set_baudrate(uint32_t baudrate){
178bace42efSMatthias Ringwald     log_info("hci_transport_h4_set_baudrate %"PRIu32, baudrate);
179bd021c4eSMatthias Ringwald     return btstack_uart->set_baudrate(baudrate);
180bd021c4eSMatthias Ringwald }
181bd021c4eSMatthias Ringwald 
hci_transport_h4_reset_statemachine(void)182bd021c4eSMatthias Ringwald static void hci_transport_h4_reset_statemachine(void){
183bd021c4eSMatthias Ringwald     h4_state = H4_W4_PACKET_TYPE;
184bd021c4eSMatthias Ringwald     read_pos = 0;
185bd021c4eSMatthias Ringwald     bytes_to_read = 1;
186bd021c4eSMatthias Ringwald }
187bd021c4eSMatthias Ringwald 
hci_transport_h4_trigger_next_read(void)188bd021c4eSMatthias Ringwald static void hci_transport_h4_trigger_next_read(void){
1898a23fc53SMatthias Ringwald     // log_info("hci_transport_h4_trigger_next_read: %u bytes", bytes_to_read);
190bd021c4eSMatthias Ringwald     btstack_uart->receive_block(&hci_packet[read_pos], bytes_to_read);
191bd021c4eSMatthias Ringwald }
192bd021c4eSMatthias Ringwald 
hci_transport_h4_packet_complete(void)193bb806528SMatthias Ringwald static void hci_transport_h4_packet_complete(void){
194bb806528SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
195bb806528SMatthias Ringwald     if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_IDLE
196bb806528SMatthias Ringwald             && memcmp(hci_packet, local_version_event_prefix, sizeof(local_version_event_prefix)) == 0){
197bb806528SMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
198bb806528SMatthias Ringwald                 if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
199bb806528SMatthias Ringwald                     // detect TI CC256x controller based on manufacturer
200bb806528SMatthias Ringwald                     log_info("Detected CC256x controller");
201bb806528SMatthias Ringwald                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED;
202bb806528SMatthias Ringwald                 } else {
203bb806528SMatthias Ringwald                     // work around not needed
204bb806528SMatthias Ringwald                     log_info("Bluetooth controller not by TI");
205bb806528SMatthias Ringwald                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE;
206bb806528SMatthias Ringwald                 }
207bb806528SMatthias Ringwald #endif
208bb806528SMatthias Ringwald #ifdef ENABLE_CYPRESS_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
209bb806528SMatthias Ringwald                 if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){
210bb806528SMatthias Ringwald                     // detect Cypress controller based on manufacturer
211bb806528SMatthias Ringwald                     log_info("Detected Cypress controller");
212bb806528SMatthias Ringwald                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED;
213bb806528SMatthias Ringwald                 } else {
214bb806528SMatthias Ringwald                     // work around not needed
215bb806528SMatthias Ringwald                     log_info("Bluetooth controller not by Cypress");
216bb806528SMatthias Ringwald                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE;
217bb806528SMatthias Ringwald                 }
218bb806528SMatthias Ringwald #endif
219bb806528SMatthias Ringwald             }
220bb806528SMatthias Ringwald #endif
2214ea43905SMatthias Ringwald     uint16_t packet_len = read_pos-1u;
222bd021c4eSMatthias Ringwald 
223bb806528SMatthias Ringwald     // reset state machine before delivering packet to stack as it might close the transport
224bb806528SMatthias Ringwald     hci_transport_h4_reset_statemachine();
225b45b7749SMilanka Ringwald     hci_transport_h4_packet_handler(hci_packet[0], &hci_packet[1], packet_len);
226bb806528SMatthias Ringwald }
227bb806528SMatthias Ringwald 
hci_transport_h4_block_read(void)228bb806528SMatthias Ringwald static void hci_transport_h4_block_read(void){
229cab2c687SMatthias Ringwald 
230bd021c4eSMatthias Ringwald     read_pos += bytes_to_read;
231bd021c4eSMatthias Ringwald 
232bd021c4eSMatthias Ringwald     switch (h4_state) {
233bd021c4eSMatthias Ringwald         case H4_W4_PACKET_TYPE:
234bd021c4eSMatthias Ringwald             switch (hci_packet[0]){
235bd021c4eSMatthias Ringwald                 case HCI_EVENT_PACKET:
236bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_EVENT_HEADER_SIZE;
237bd021c4eSMatthias Ringwald                     h4_state = H4_W4_EVENT_HEADER;
238bd021c4eSMatthias Ringwald                     break;
239bd021c4eSMatthias Ringwald                 case HCI_ACL_DATA_PACKET:
240bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_ACL_HEADER_SIZE;
241bd021c4eSMatthias Ringwald                     h4_state = H4_W4_ACL_HEADER;
242bd021c4eSMatthias Ringwald                     break;
243bd021c4eSMatthias Ringwald                 case HCI_SCO_DATA_PACKET:
244bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_SCO_HEADER_SIZE;
245bd021c4eSMatthias Ringwald                     h4_state = H4_W4_SCO_HEADER;
246bd021c4eSMatthias Ringwald                     break;
2470a8cccd5SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS
2480d5b1bafSMatthias Ringwald                 case HCI_ISO_DATA_PACKET:
2490d5b1bafSMatthias Ringwald                     bytes_to_read = HCI_ISO_HEADER_SIZE;
250*d8910fa8SMatthias Ringwald                     h4_state = H4_W4_ISO_HEADER;
2510d5b1bafSMatthias Ringwald                     break;
2520a8cccd5SMatthias Ringwald #endif
253f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
254307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_IND:
255307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_ACK:
256307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
257307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_ACK:
2588a23fc53SMatthias Ringwald                     hci_transport_h4_ehcill_handle_command(hci_packet[0]);
2598a23fc53SMatthias Ringwald                     hci_transport_h4_reset_statemachine();
260307a4fe3SMatthias Ringwald                     break;
261307a4fe3SMatthias Ringwald #endif
262bd021c4eSMatthias Ringwald                 default:
2638a23fc53SMatthias Ringwald                     log_error("hci_transport_h4: invalid packet type 0x%02x", hci_packet[0]);
264bd021c4eSMatthias Ringwald                     hci_transport_h4_reset_statemachine();
265bd021c4eSMatthias Ringwald                     break;
266bd021c4eSMatthias Ringwald             }
267bd021c4eSMatthias Ringwald             break;
268bd021c4eSMatthias Ringwald 
269bd021c4eSMatthias Ringwald         case H4_W4_EVENT_HEADER:
270bd021c4eSMatthias Ringwald             bytes_to_read = hci_packet[2];
271e8b81068SMatthias Ringwald             // check Event length
272ea374553SMatthias Ringwald             if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_EVENT_HEADER_SIZE)){
273e8b81068SMatthias 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);
274e8b81068SMatthias Ringwald                 hci_transport_h4_reset_statemachine();
275e8b81068SMatthias Ringwald                 break;
276e8b81068SMatthias Ringwald             }
277bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
278bd021c4eSMatthias Ringwald             break;
279bd021c4eSMatthias Ringwald 
280bd021c4eSMatthias Ringwald         case H4_W4_ACL_HEADER:
281bd021c4eSMatthias Ringwald             bytes_to_read = little_endian_read_16( hci_packet, 3);
282bd021c4eSMatthias Ringwald             // check ACL length
283ea374553SMatthias Ringwald             if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE)){
284fc6cde64SMatthias 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);
285bd021c4eSMatthias Ringwald                 hci_transport_h4_reset_statemachine();
286bd021c4eSMatthias Ringwald                 break;
287bd021c4eSMatthias Ringwald             }
288bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
289bd021c4eSMatthias Ringwald             break;
290bd021c4eSMatthias Ringwald 
291bd021c4eSMatthias Ringwald         case H4_W4_SCO_HEADER:
292bd021c4eSMatthias Ringwald             bytes_to_read = hci_packet[3];
293e8b81068SMatthias Ringwald             // check SCO length
294ea374553SMatthias Ringwald             if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_SCO_HEADER_SIZE)){
295e8b81068SMatthias 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);
296e8b81068SMatthias Ringwald                 hci_transport_h4_reset_statemachine();
297e8b81068SMatthias Ringwald                 break;
298e8b81068SMatthias Ringwald             }
299bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
300bd021c4eSMatthias Ringwald             break;
301bd021c4eSMatthias Ringwald 
3020d5b1bafSMatthias Ringwald         case H4_W4_ISO_HEADER:
3030d5b1bafSMatthias Ringwald             bytes_to_read = little_endian_read_16( hci_packet, 3) & 0x3fff;
3040d5b1bafSMatthias Ringwald             // check ISO length
3050d5b1bafSMatthias Ringwald             if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE)){
3060d5b1bafSMatthias Ringwald                 log_error("hci_transport_h4: invalid ISO payload len %d - only space for %u", bytes_to_read, HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_ISO_HEADER_SIZE);
3070d5b1bafSMatthias Ringwald                 hci_transport_h4_reset_statemachine();
3080d5b1bafSMatthias Ringwald                 break;
3090d5b1bafSMatthias Ringwald             }
3100d5b1bafSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
3110d5b1bafSMatthias Ringwald             break;
3120d5b1bafSMatthias Ringwald 
313bd021c4eSMatthias Ringwald         case H4_W4_PAYLOAD:
314bb806528SMatthias Ringwald             hci_transport_h4_packet_complete();
315bd021c4eSMatthias Ringwald             break;
316ad254f5dSMatthias Ringwald 
317ad254f5dSMatthias Ringwald         case H4_OFF:
318ad254f5dSMatthias Ringwald             bytes_to_read = 0;
319e80b814dSMatthias Ringwald             break;
3207bbeb3adSMilanka Ringwald         default:
3217bbeb3adSMilanka Ringwald             btstack_assert(false);
3227bbeb3adSMilanka Ringwald             break;
323bd021c4eSMatthias Ringwald     }
32439e7ee9fSMatthias Ringwald 
32577291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
3267741d2d0SMatthias Ringwald     if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT){
3277741d2d0SMatthias Ringwald         baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_IDLE;
32839e7ee9fSMatthias Ringwald         // avoid flowcontrol problem by reading expected hci command complete event of 7 bytes in a single block read
32939e7ee9fSMatthias Ringwald         h4_state = H4_W4_PAYLOAD;
33039e7ee9fSMatthias Ringwald         bytes_to_read = 7;
33139e7ee9fSMatthias Ringwald     }
33239e7ee9fSMatthias Ringwald #endif
33339e7ee9fSMatthias Ringwald 
334bb806528SMatthias Ringwald     // forward packet if payload size == 0
3354ea43905SMatthias Ringwald     if (h4_state == H4_W4_PAYLOAD && bytes_to_read == 0u) {
336bb806528SMatthias Ringwald         hci_transport_h4_packet_complete();
337bb806528SMatthias Ringwald     }
338bb806528SMatthias Ringwald 
339e80b814dSMatthias Ringwald     if (h4_state != H4_OFF) {
340bd021c4eSMatthias Ringwald         hci_transport_h4_trigger_next_read();
341bd021c4eSMatthias Ringwald     }
342e80b814dSMatthias Ringwald }
343bd021c4eSMatthias Ringwald 
hci_transport_h4_block_sent(void)344307a4fe3SMatthias Ringwald static void hci_transport_h4_block_sent(void){
3458334d3d8SMatthias Ringwald 
3468334d3d8SMatthias Ringwald     static const uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
3478334d3d8SMatthias Ringwald 
348307a4fe3SMatthias Ringwald     switch (tx_state){
349307a4fe3SMatthias Ringwald         case TX_W4_PACKET_SENT:
350307a4fe3SMatthias Ringwald             // packet fully sent, reset state
3512e25934bSMatthias Ringwald #ifdef ENABLE_EHCILL
3522e25934bSMatthias Ringwald             ehcill_tx_len = 0;
3532e25934bSMatthias Ringwald #endif
3548a23fc53SMatthias Ringwald             tx_state = TX_IDLE;
355307a4fe3SMatthias Ringwald 
356f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
3578a23fc53SMatthias Ringwald             // notify eHCILL engine
3588a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_handle_packet_sent();
359307a4fe3SMatthias Ringwald #endif
360307a4fe3SMatthias Ringwald             // notify upper stack that it can send again
361b45b7749SMilanka Ringwald             hci_transport_h4_packet_handler(HCI_EVENT_PACKET, (uint8_t *) &packet_sent_event[0], sizeof(packet_sent_event));
362307a4fe3SMatthias Ringwald             break;
363307a4fe3SMatthias Ringwald 
364f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
3658a23fc53SMatthias Ringwald         case TX_W4_EHCILL_SENT:
366b403028eSMatthias Ringwald         case TX_W4_WAKEUP:
3678a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_handle_ehcill_command_sent();
368307a4fe3SMatthias Ringwald             break;
369307a4fe3SMatthias Ringwald #endif
370307a4fe3SMatthias Ringwald 
371307a4fe3SMatthias Ringwald         default:
372307a4fe3SMatthias Ringwald             break;
373307a4fe3SMatthias Ringwald     }
374307a4fe3SMatthias Ringwald }
375307a4fe3SMatthias Ringwald 
hci_transport_h4_can_send_now(uint8_t packet_type)376307a4fe3SMatthias Ringwald static int hci_transport_h4_can_send_now(uint8_t packet_type){
377cebe3e9eSMatthias Ringwald     UNUSED(packet_type);
378307a4fe3SMatthias Ringwald     return tx_state == TX_IDLE;
379307a4fe3SMatthias Ringwald }
380307a4fe3SMatthias Ringwald 
hci_transport_h4_send_packet(uint8_t packet_type,uint8_t * packet,int size)381307a4fe3SMatthias Ringwald static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){
38239e7ee9fSMatthias Ringwald 
383307a4fe3SMatthias Ringwald     // store packet type before actual data and increase size
38441f9be70SMatthias Ringwald     uint8_t * buffer = &packet[-1];
38541f9be70SMatthias Ringwald     uint32_t  buffer_size = size + 1;
38641f9be70SMatthias Ringwald     buffer[0] = packet_type;
387307a4fe3SMatthias Ringwald 
38877291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
3897741d2d0SMatthias Ringwald     if ((baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED)
39041f9be70SMatthias Ringwald     && (memcmp(buffer, baud_rate_command_prefix, sizeof(baud_rate_command_prefix)) == 0)) {
3917741d2d0SMatthias Ringwald         log_info("Baud rate command detected, expect command complete event next");
3927741d2d0SMatthias Ringwald         baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT;
39339e7ee9fSMatthias Ringwald     }
39439e7ee9fSMatthias Ringwald #endif
39539e7ee9fSMatthias Ringwald 
396f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
3972e25934bSMatthias Ringwald     // store request for later
39841f9be70SMatthias Ringwald     ehcill_tx_len   = buffer_size;
39941f9be70SMatthias Ringwald     ehcill_tx_data  = buffer;
4009ecd1ba8SMatthias Ringwald     switch (ehcill_state){
4019ecd1ba8SMatthias Ringwald         case EHCILL_STATE_SLEEP:
4028a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_trigger_wakeup();
4038a23fc53SMatthias Ringwald             return 0;
4049ecd1ba8SMatthias Ringwald         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
405b403028eSMatthias Ringwald             log_info("eHILL: send next packet, state EHCILL_STATE_W2_SEND_SLEEP_ACK");
4069ecd1ba8SMatthias Ringwald             return 0;
4079ecd1ba8SMatthias Ringwald         default:
4089ecd1ba8SMatthias Ringwald             break;
4098a23fc53SMatthias Ringwald     }
4108a23fc53SMatthias Ringwald #endif
4118a23fc53SMatthias Ringwald 
4128a23fc53SMatthias Ringwald     // start sending
4138a23fc53SMatthias Ringwald     tx_state = TX_W4_PACKET_SENT;
41441f9be70SMatthias Ringwald     btstack_uart->send_block(buffer, buffer_size);
415307a4fe3SMatthias Ringwald     return 0;
416307a4fe3SMatthias Ringwald }
417307a4fe3SMatthias Ringwald 
hci_transport_h4_init(const void * transport_config)418bd021c4eSMatthias Ringwald static void hci_transport_h4_init(const void * transport_config){
419bd021c4eSMatthias Ringwald     // check for hci_transport_config_uart_t
420bd021c4eSMatthias Ringwald     if (!transport_config) {
421bd021c4eSMatthias Ringwald         log_error("hci_transport_h4: no config!");
422bd021c4eSMatthias Ringwald         return;
423bd021c4eSMatthias Ringwald     }
424bd021c4eSMatthias Ringwald     if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) {
425bd021c4eSMatthias Ringwald         log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!");
426bd021c4eSMatthias Ringwald         return;
427bd021c4eSMatthias Ringwald     }
428bd021c4eSMatthias Ringwald 
429bd021c4eSMatthias Ringwald     // extract UART config from transport config
430bd021c4eSMatthias Ringwald     hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config;
43148cdff9cSMilanka Ringwald     hci_transport_h4_uart_config.baudrate    = hci_transport_config_uart->baudrate_init;
43248cdff9cSMilanka Ringwald     hci_transport_h4_uart_config.flowcontrol = hci_transport_config_uart->flowcontrol;
43348cdff9cSMilanka Ringwald     hci_transport_h4_uart_config.parity      = hci_transport_config_uart->parity;
43448cdff9cSMilanka Ringwald     hci_transport_h4_uart_config.device_name = hci_transport_config_uart->device_name;
435bd021c4eSMatthias Ringwald 
436c682b8ecSMatthias Ringwald     // set state to off
437c682b8ecSMatthias Ringwald     tx_state = TX_OFF;
438c682b8ecSMatthias Ringwald     h4_state = H4_OFF;
439c682b8ecSMatthias Ringwald 
440bd021c4eSMatthias Ringwald     // setup UART driver
44148cdff9cSMilanka Ringwald     btstack_uart->init(&hci_transport_h4_uart_config);
442bd021c4eSMatthias Ringwald     btstack_uart->set_block_received(&hci_transport_h4_block_read);
443bd021c4eSMatthias Ringwald     btstack_uart->set_block_sent(&hci_transport_h4_block_sent);
444bd021c4eSMatthias Ringwald }
445bd021c4eSMatthias Ringwald 
hci_transport_h4_open(void)446bd021c4eSMatthias Ringwald static int hci_transport_h4_open(void){
447c682b8ecSMatthias Ringwald     // open uart driver
448bd021c4eSMatthias Ringwald     int res = btstack_uart->open();
4499305033eSMatthias Ringwald     if (res != 0){
450bd021c4eSMatthias Ringwald         return res;
451bd021c4eSMatthias Ringwald     }
452c682b8ecSMatthias Ringwald 
453c682b8ecSMatthias Ringwald     // init rx + tx state machines
454bd021c4eSMatthias Ringwald     hci_transport_h4_reset_statemachine();
455bd021c4eSMatthias Ringwald     hci_transport_h4_trigger_next_read();
456307a4fe3SMatthias Ringwald     tx_state = TX_IDLE;
457307a4fe3SMatthias Ringwald 
458f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
4598a23fc53SMatthias Ringwald     hci_transport_h4_ehcill_open();
460307a4fe3SMatthias Ringwald #endif
461bd021c4eSMatthias Ringwald     return 0;
462bd021c4eSMatthias Ringwald }
463bd021c4eSMatthias Ringwald 
hci_transport_h4_close(void)464bd021c4eSMatthias Ringwald static int hci_transport_h4_close(void){
465c682b8ecSMatthias Ringwald     // set state to off
466c682b8ecSMatthias Ringwald     tx_state = TX_OFF;
467c682b8ecSMatthias Ringwald     h4_state = H4_OFF;
468c682b8ecSMatthias Ringwald 
469c682b8ecSMatthias Ringwald     // close uart driver
470bd021c4eSMatthias Ringwald     return btstack_uart->close();
471bd021c4eSMatthias Ringwald }
472bd021c4eSMatthias Ringwald 
hci_transport_h4_register_packet_handler(void (* handler)(uint8_t packet_type,uint8_t * packet,uint16_t size))473bd021c4eSMatthias Ringwald static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
474b45b7749SMilanka Ringwald     hci_transport_h4_packet_handler = handler;
475bd021c4eSMatthias Ringwald }
476bd021c4eSMatthias Ringwald 
dummy_handler(uint8_t packet_type,uint8_t * packet,uint16_t size)477bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
478cebe3e9eSMatthias Ringwald     UNUSED(packet_type);
479cebe3e9eSMatthias Ringwald     UNUSED(packet);
480cebe3e9eSMatthias Ringwald     UNUSED(size);
481bd021c4eSMatthias Ringwald }
482bd021c4eSMatthias Ringwald 
4838a23fc53SMatthias Ringwald //
484307a4fe3SMatthias Ringwald // --- main part of eHCILL implementation ---
4858a23fc53SMatthias Ringwald //
486307a4fe3SMatthias Ringwald 
487f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
488307a4fe3SMatthias Ringwald 
hci_transport_h4_ehcill_emit_sleep_state(int sleep_active)48962ca45d7SMatthias Ringwald static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){
49062ca45d7SMatthias Ringwald     static int last_state = 0;
49162ca45d7SMatthias Ringwald     if (sleep_active == last_state) return;
49262ca45d7SMatthias Ringwald     last_state = sleep_active;
49362ca45d7SMatthias Ringwald 
49462ca45d7SMatthias Ringwald     log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active);
49562ca45d7SMatthias Ringwald     uint8_t event[3];
49662ca45d7SMatthias Ringwald     event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE;
49762ca45d7SMatthias Ringwald     event[1] = sizeof(event) - 2;
49862ca45d7SMatthias Ringwald     event[2] = sleep_active;
499b45b7749SMilanka Ringwald     hci_transport_h4_packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
50062ca45d7SMatthias Ringwald }
50162ca45d7SMatthias Ringwald 
hci_transport_h4_ehcill_wakeup_handler(void)502b403028eSMatthias Ringwald static void hci_transport_h4_ehcill_wakeup_handler(void){
503b403028eSMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
504b403028eSMatthias Ringwald     log_info("eHCILL: UART wakeup received");
505b403028eSMatthias Ringwald #endif
506b403028eSMatthias Ringwald     hci_transport_h4_ehcill_handle_command(EHCILL_WAKEUP_SIGNAL);
507b403028eSMatthias Ringwald }
508b403028eSMatthias Ringwald 
hci_transport_h4_ehcill_open(void)5098a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void){
5108a23fc53SMatthias Ringwald     hci_transport_h4_ehcill_reset_statemachine();
5118a23fc53SMatthias Ringwald 
5128a23fc53SMatthias Ringwald     // find best sleep mode to use: wake on CTS, wake on RX, none
5138a23fc53SMatthias Ringwald     btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF;
5148a23fc53SMatthias Ringwald     int supported_sleep_modes = 0;
5158a23fc53SMatthias Ringwald     if (btstack_uart->get_supported_sleep_modes){
5168a23fc53SMatthias Ringwald         supported_sleep_modes = btstack_uart->get_supported_sleep_modes();
517307a4fe3SMatthias Ringwald     }
5188a23fc53SMatthias Ringwald     if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){
5198a23fc53SMatthias Ringwald         log_info("eHCILL: using wake on CTS");
5208a23fc53SMatthias Ringwald         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE;
5218a23fc53SMatthias Ringwald     } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){
5228a23fc53SMatthias Ringwald         log_info("eHCILL: using wake on RX");
5238a23fc53SMatthias Ringwald         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE;
5248a23fc53SMatthias Ringwald     } else {
5258a23fc53SMatthias Ringwald         log_info("eHCILL: UART driver does not provide compatible sleep mode");
5268a23fc53SMatthias Ringwald     }
527b403028eSMatthias Ringwald     if (btstack_uart->set_wakeup_handler){
528b403028eSMatthias Ringwald         btstack_uart->set_wakeup_handler(&hci_transport_h4_ehcill_wakeup_handler);
529b403028eSMatthias Ringwald     }
530307a4fe3SMatthias Ringwald }
531307a4fe3SMatthias Ringwald 
hci_transport_h4_echill_send_wakeup_ind(void)532307a4fe3SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void){
5339ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5349ecd1ba8SMatthias Ringwald     log_info("eHCILL: send WAKEUP_IND");
5359ecd1ba8SMatthias Ringwald #endif
536307a4fe3SMatthias Ringwald     // update state
537307a4fe3SMatthias Ringwald     tx_state     = TX_W4_WAKEUP;
5389ecd1ba8SMatthias Ringwald     ehcill_state = EHCILL_STATE_W4_WAKEUP_IND_OR_ACK;
539307a4fe3SMatthias Ringwald     ehcill_command_to_send = EHCILL_WAKE_UP_IND;
540307a4fe3SMatthias Ringwald     btstack_uart->send_block(&ehcill_command_to_send, 1);
541307a4fe3SMatthias Ringwald }
542307a4fe3SMatthias Ringwald 
hci_transport_h4_ehcill_outgoing_packet_ready(void)543307a4fe3SMatthias Ringwald static int hci_transport_h4_ehcill_outgoing_packet_ready(void){
5442e25934bSMatthias Ringwald     return ehcill_tx_len != 0;
545307a4fe3SMatthias Ringwald }
546307a4fe3SMatthias Ringwald 
hci_transport_h4_ehcill_reset_statemachine(void)547307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void){
548307a4fe3SMatthias Ringwald     ehcill_state = EHCILL_STATE_AWAKE;
549307a4fe3SMatthias Ringwald }
550307a4fe3SMatthias Ringwald 
hci_transport_h4_ehcill_send_ehcill_command(void)551307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void){
5529ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5539ecd1ba8SMatthias Ringwald     log_info("eHCILL: send command %02x", ehcill_command_to_send);
5549ecd1ba8SMatthias Ringwald #endif
555307a4fe3SMatthias Ringwald     tx_state = TX_W4_EHCILL_SENT;
5569ecd1ba8SMatthias Ringwald     if (ehcill_command_to_send == EHCILL_GO_TO_SLEEP_ACK){
5579ecd1ba8SMatthias Ringwald         ehcill_state = EHCILL_STATE_SLEEP;
5589ecd1ba8SMatthias Ringwald     }
559307a4fe3SMatthias Ringwald     btstack_uart->send_block(&ehcill_command_to_send, 1);
560307a4fe3SMatthias Ringwald }
561307a4fe3SMatthias Ringwald 
hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer)562307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){
563b403028eSMatthias Ringwald 	UNUSED(timer);
5649ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5659ecd1ba8SMatthias Ringwald     log_info("eHCILL: timer triggered");
5669ecd1ba8SMatthias Ringwald #endif
567307a4fe3SMatthias Ringwald     hci_transport_h4_ehcill_send_ehcill_command();
568307a4fe3SMatthias Ringwald }
569307a4fe3SMatthias Ringwald 
hci_transport_h4_ehcill_sleep_ack_timer_setup(void)570307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){
571307a4fe3SMatthias Ringwald     // setup timer
5729ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5739ecd1ba8SMatthias Ringwald     log_info("eHCILL: set timer for sending command %02x", ehcill_command_to_send);
5749ecd1ba8SMatthias Ringwald #endif
5758a23fc53SMatthias Ringwald     btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler);
576307a4fe3SMatthias Ringwald     btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50);
577307a4fe3SMatthias Ringwald     btstack_run_loop_add_timer(&ehcill_sleep_ack_timer);
578307a4fe3SMatthias Ringwald }
579307a4fe3SMatthias Ringwald 
hci_transport_h4_ehcill_trigger_wakeup(void)5808a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void){
5818a23fc53SMatthias Ringwald     switch (tx_state){
5828a23fc53SMatthias Ringwald         case TX_W2_EHCILL_SEND:
5838a23fc53SMatthias Ringwald         case TX_W4_EHCILL_SENT:
5848a23fc53SMatthias Ringwald             // wake up / sleep ack in progress, nothing to do now
5858a23fc53SMatthias Ringwald             return;
5868a23fc53SMatthias Ringwald         case TX_IDLE:
5878a23fc53SMatthias Ringwald         default:
5888a23fc53SMatthias Ringwald             // all clear, prepare for wakeup
5898a23fc53SMatthias Ringwald             break;
5908a23fc53SMatthias Ringwald     }
5918a23fc53SMatthias Ringwald     // UART needed again
59262ca45d7SMatthias Ringwald     hci_transport_h4_ehcill_emit_sleep_state(0);
5938a23fc53SMatthias Ringwald     if (btstack_uart_sleep_mode){
5948a23fc53SMatthias Ringwald         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
5958a23fc53SMatthias Ringwald     }
5968a23fc53SMatthias Ringwald     hci_transport_h4_echill_send_wakeup_ind();
5978a23fc53SMatthias Ringwald }
5988a23fc53SMatthias Ringwald 
hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command)5999ecd1ba8SMatthias Ringwald static void hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command){
6009ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
6019ecd1ba8SMatthias Ringwald     log_info("eHCILL: schedule eHCILL command %02x", command);
6029ecd1ba8SMatthias Ringwald #endif
603307a4fe3SMatthias Ringwald     ehcill_command_to_send = command;
604307a4fe3SMatthias Ringwald     switch (tx_state){
605307a4fe3SMatthias Ringwald         case TX_IDLE:
606307a4fe3SMatthias Ringwald             if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){
607307a4fe3SMatthias Ringwald                 // send right away
608307a4fe3SMatthias Ringwald                 hci_transport_h4_ehcill_send_ehcill_command();
609307a4fe3SMatthias Ringwald             } else {
610307a4fe3SMatthias Ringwald                 // change state so BTstack cannot send and setup timer
611307a4fe3SMatthias Ringwald                 tx_state = TX_W2_EHCILL_SEND;
612307a4fe3SMatthias Ringwald                 hci_transport_h4_ehcill_sleep_ack_timer_setup();
613307a4fe3SMatthias Ringwald             }
614307a4fe3SMatthias Ringwald             break;
615307a4fe3SMatthias Ringwald         default:
616307a4fe3SMatthias Ringwald             break;
617307a4fe3SMatthias Ringwald     }
618307a4fe3SMatthias Ringwald }
619307a4fe3SMatthias Ringwald 
hci_transport_h4_ehcill_handle_command(uint8_t action)6208a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action){
621307a4fe3SMatthias Ringwald     // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size);
622307a4fe3SMatthias Ringwald     switch(ehcill_state){
623307a4fe3SMatthias Ringwald         case EHCILL_STATE_AWAKE:
624307a4fe3SMatthias Ringwald             switch(action){
625307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_IND:
6269ecd1ba8SMatthias Ringwald                     ehcill_state = EHCILL_STATE_W2_SEND_SLEEP_ACK;
6279ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
6289ecd1ba8SMatthias Ringwald                     log_info("eHCILL: Received GO_TO_SLEEP_IND RX");
6299ecd1ba8SMatthias Ringwald #endif
6309ecd1ba8SMatthias Ringwald                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_GO_TO_SLEEP_ACK);
631307a4fe3SMatthias Ringwald                     break;
632307a4fe3SMatthias Ringwald                 default:
633307a4fe3SMatthias Ringwald                     break;
634307a4fe3SMatthias Ringwald             }
635307a4fe3SMatthias Ringwald             break;
636307a4fe3SMatthias Ringwald 
6379ecd1ba8SMatthias Ringwald         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
638307a4fe3SMatthias Ringwald             switch(action){
639307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
640307a4fe3SMatthias Ringwald                     ehcill_state = EHCILL_STATE_AWAKE;
64162ca45d7SMatthias Ringwald                     hci_transport_h4_ehcill_emit_sleep_state(0);
642b403028eSMatthias Ringwald                     if (btstack_uart_sleep_mode){
643b403028eSMatthias Ringwald                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
644b403028eSMatthias Ringwald                     }
645b403028eSMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
646b403028eSMatthias Ringwald                     log_info("eHCILL: Received WAKE_UP_IND RX");
647b403028eSMatthias Ringwald #endif
648b403028eSMatthias Ringwald                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK);
649b403028eSMatthias Ringwald                     break;
650b403028eSMatthias Ringwald 
651b403028eSMatthias Ringwald                 default:
652b403028eSMatthias Ringwald                     break;
653b403028eSMatthias Ringwald             }
654b403028eSMatthias Ringwald             break;
655b403028eSMatthias Ringwald 
656b403028eSMatthias Ringwald         case EHCILL_STATE_SLEEP:
657b403028eSMatthias Ringwald             switch(action){
658b403028eSMatthias Ringwald                 case EHCILL_WAKEUP_SIGNAL:
659b403028eSMatthias Ringwald                     hci_transport_h4_ehcill_emit_sleep_state(0);
660b403028eSMatthias Ringwald                     if (btstack_uart_sleep_mode){
661b403028eSMatthias Ringwald                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
662b403028eSMatthias Ringwald                     }
663b403028eSMatthias Ringwald                     break;
664b403028eSMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
665b403028eSMatthias Ringwald                     ehcill_state = EHCILL_STATE_AWAKE;
666b403028eSMatthias Ringwald                     hci_transport_h4_ehcill_emit_sleep_state(0);
667b403028eSMatthias Ringwald                     if (btstack_uart_sleep_mode){
668b403028eSMatthias Ringwald                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
669b403028eSMatthias Ringwald                     }
6709ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
6719ecd1ba8SMatthias Ringwald                     log_info("eHCILL: Received WAKE_UP_IND RX");
6729ecd1ba8SMatthias Ringwald #endif
6739ecd1ba8SMatthias Ringwald                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK);
674307a4fe3SMatthias Ringwald                     break;
675307a4fe3SMatthias Ringwald 
676307a4fe3SMatthias Ringwald                 default:
677307a4fe3SMatthias Ringwald                     break;
678307a4fe3SMatthias Ringwald             }
679307a4fe3SMatthias Ringwald             break;
680307a4fe3SMatthias Ringwald 
6819ecd1ba8SMatthias Ringwald         case EHCILL_STATE_W4_WAKEUP_IND_OR_ACK:
682307a4fe3SMatthias Ringwald             switch(action){
683307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
684307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_ACK:
6859ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
6869ecd1ba8SMatthias Ringwald                     log_info("eHCILL: Received WAKE_UP (%02x)", action);
6879ecd1ba8SMatthias Ringwald #endif
688307a4fe3SMatthias Ringwald                     tx_state = TX_W4_PACKET_SENT;
689307a4fe3SMatthias Ringwald                     ehcill_state = EHCILL_STATE_AWAKE;
6902e25934bSMatthias Ringwald                     btstack_uart->send_block(ehcill_tx_data, ehcill_tx_len);
691307a4fe3SMatthias Ringwald                     break;
692307a4fe3SMatthias Ringwald                 default:
693307a4fe3SMatthias Ringwald                     break;
694307a4fe3SMatthias Ringwald             }
695307a4fe3SMatthias Ringwald             break;
696307a4fe3SMatthias Ringwald     }
697307a4fe3SMatthias Ringwald }
6988a23fc53SMatthias Ringwald 
hci_transport_h4_ehcill_handle_packet_sent(void)6998a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void){
7009ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
7019ecd1ba8SMatthias Ringwald         log_info("eHCILL: handle packet sent, command to send %02x", ehcill_command_to_send);
7029ecd1ba8SMatthias Ringwald #endif
7038a23fc53SMatthias Ringwald     // now, send pending ehcill command if neccessary
7048a23fc53SMatthias Ringwald     switch (ehcill_command_to_send){
7058a23fc53SMatthias Ringwald         case EHCILL_GO_TO_SLEEP_ACK:
7068a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_sleep_ack_timer_setup();
7078a23fc53SMatthias Ringwald             break;
7088a23fc53SMatthias Ringwald         case EHCILL_WAKE_UP_IND:
7098a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_send_ehcill_command();
7108a23fc53SMatthias Ringwald             break;
7118a23fc53SMatthias Ringwald         default:
7128a23fc53SMatthias Ringwald             break;
7138a23fc53SMatthias Ringwald     }
7148a23fc53SMatthias Ringwald }
7158a23fc53SMatthias Ringwald 
hci_transport_h4_ehcill_handle_ehcill_command_sent(void)7168a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){
7178a23fc53SMatthias Ringwald     tx_state = TX_IDLE;
7188a23fc53SMatthias Ringwald     int command = ehcill_command_to_send;
7198a23fc53SMatthias Ringwald     ehcill_command_to_send = 0;
7209ecd1ba8SMatthias Ringwald 
7219ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
7229ecd1ba8SMatthias Ringwald         log_info("eHCILL: handle eHCILL sent, command was %02x", command);
7239ecd1ba8SMatthias Ringwald #endif
7249ecd1ba8SMatthias Ringwald 
7258a23fc53SMatthias Ringwald     if (command == EHCILL_GO_TO_SLEEP_ACK) {
7269ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
7278a23fc53SMatthias Ringwald         log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode");
7289ecd1ba8SMatthias Ringwald #endif
7298a23fc53SMatthias Ringwald         // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent
7308a23fc53SMatthias Ringwald         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
7318a23fc53SMatthias Ringwald             btstack_uart->set_sleep(btstack_uart_sleep_mode);
7328a23fc53SMatthias Ringwald         }
73362ca45d7SMatthias Ringwald         hci_transport_h4_ehcill_emit_sleep_state(1);
7348a23fc53SMatthias Ringwald     }
7358a23fc53SMatthias Ringwald     // already packet ready? then start wakeup
7368a23fc53SMatthias Ringwald     if (hci_transport_h4_ehcill_outgoing_packet_ready()){
73762ca45d7SMatthias Ringwald         hci_transport_h4_ehcill_emit_sleep_state(0);
738b403028eSMatthias Ringwald         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
7398a23fc53SMatthias Ringwald             btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
7408a23fc53SMatthias Ringwald         }
7419ecd1ba8SMatthias Ringwald         if (command != EHCILL_WAKE_UP_IND){
7428a23fc53SMatthias Ringwald             hci_transport_h4_echill_send_wakeup_ind();
7438a23fc53SMatthias Ringwald         }
7448a23fc53SMatthias Ringwald     }
7459ecd1ba8SMatthias Ringwald }
7468a23fc53SMatthias Ringwald 
747307a4fe3SMatthias Ringwald #endif
748307a4fe3SMatthias Ringwald // --- end of eHCILL implementation ---------
749307a4fe3SMatthias Ringwald 
7508334d3d8SMatthias Ringwald 
7518334d3d8SMatthias Ringwald // configure and return h4 singleton
752bd021c4eSMatthias Ringwald static const hci_transport_t hci_transport_h4 = {
753bd021c4eSMatthias Ringwald         /* const char * name; */                                        "H4",
754bd021c4eSMatthias Ringwald         /* void   (*init) (const void *transport_config); */            &hci_transport_h4_init,
755bd021c4eSMatthias Ringwald         /* int    (*open)(void); */                                     &hci_transport_h4_open,
756bd021c4eSMatthias Ringwald         /* int    (*close)(void); */                                    &hci_transport_h4_close,
757bd021c4eSMatthias Ringwald         /* void   (*register_packet_handler)(void (*handler)(...); */   &hci_transport_h4_register_packet_handler,
758bd021c4eSMatthias Ringwald         /* int    (*can_send_packet_now)(uint8_t packet_type); */       &hci_transport_h4_can_send_now,
759bd021c4eSMatthias Ringwald         /* int    (*send_packet)(...); */                               &hci_transport_h4_send_packet,
760bd021c4eSMatthias Ringwald         /* int    (*set_baudrate)(uint32_t baudrate); */                &hci_transport_h4_set_baudrate,
761bd021c4eSMatthias Ringwald         /* void   (*reset_link)(void); */                               NULL,
762ee752bb8SMatthias Ringwald         /* void   (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL,
763bd021c4eSMatthias Ringwald };
764bd021c4eSMatthias Ringwald 
hci_transport_h4_instance_for_uart(const btstack_uart_t * uart_driver)765f9bd6dd7SMatthias Ringwald const hci_transport_t * hci_transport_h4_instance_for_uart(const btstack_uart_t * uart_driver){
766bd021c4eSMatthias Ringwald     btstack_uart = uart_driver;
767bd021c4eSMatthias Ringwald     return &hci_transport_h4;
768bd021c4eSMatthias Ringwald }
769f9bd6dd7SMatthias Ringwald 
770f9bd6dd7SMatthias Ringwald // @deprecated
hci_transport_h4_instance(const btstack_uart_block_t * uart_driver)771f9bd6dd7SMatthias Ringwald const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) {
772f9bd6dd7SMatthias Ringwald     btstack_uart = (const btstack_uart_t *) uart_driver;
773f9bd6dd7SMatthias Ringwald     return &hci_transport_h4;
774f9bd6dd7SMatthias Ringwald }
775