xref: /btstack/src/hci_transport_h4.c (revision b45b7749fd0a3efec18073ae84f893078d0216d0)
1bd021c4eSMatthias Ringwald /*
2bd021c4eSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3bd021c4eSMatthias Ringwald  *
4bd021c4eSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5bd021c4eSMatthias Ringwald  * modification, are permitted provided that the following conditions
6bd021c4eSMatthias Ringwald  * are met:
7bd021c4eSMatthias Ringwald  *
8bd021c4eSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9bd021c4eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10bd021c4eSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11bd021c4eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12bd021c4eSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13bd021c4eSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14bd021c4eSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15bd021c4eSMatthias Ringwald  *    from this software without specific prior written permission.
16bd021c4eSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17bd021c4eSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18bd021c4eSMatthias Ringwald  *    monetary gain.
19bd021c4eSMatthias Ringwald  *
20bd021c4eSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21bd021c4eSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22bd021c4eSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23bd021c4eSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24bd021c4eSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25bd021c4eSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26bd021c4eSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27bd021c4eSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28bd021c4eSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29bd021c4eSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30bd021c4eSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31bd021c4eSMatthias Ringwald  * SUCH DAMAGE.
32bd021c4eSMatthias Ringwald  *
33bd021c4eSMatthias Ringwald  * Please inquire about commercial licensing options at
34bd021c4eSMatthias Ringwald  * [email protected]
35bd021c4eSMatthias Ringwald  *
36bd021c4eSMatthias Ringwald  */
37bd021c4eSMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hci_transport_h4.c"
39ab2c6ae4SMatthias Ringwald 
40bd021c4eSMatthias Ringwald /*
41bd021c4eSMatthias Ringwald  *  hci_h4_transport.c
42bd021c4eSMatthias Ringwald  *
438a23fc53SMatthias Ringwald  *  HCI Transport API implementation for H4 protocol over POSIX with optional support for eHCILL
44bd021c4eSMatthias Ringwald  *
45bd021c4eSMatthias Ringwald  *  Created by Matthias Ringwald on 4/29/09.
46bd021c4eSMatthias Ringwald  */
47bd021c4eSMatthias Ringwald 
48bace42efSMatthias Ringwald 
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,
116bd021c4eSMatthias Ringwald     H4_W4_PAYLOAD,
117bd021c4eSMatthias Ringwald } H4_STATE;
118bd021c4eSMatthias Ringwald 
119307a4fe3SMatthias Ringwald typedef enum {
120c682b8ecSMatthias Ringwald     TX_OFF,
121c682b8ecSMatthias Ringwald     TX_IDLE,
122307a4fe3SMatthias Ringwald     TX_W4_PACKET_SENT,
123f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
124307a4fe3SMatthias Ringwald     TX_W4_WAKEUP,
125307a4fe3SMatthias Ringwald     TX_W2_EHCILL_SEND,
126307a4fe3SMatthias Ringwald     TX_W4_EHCILL_SENT,
127307a4fe3SMatthias Ringwald #endif
128307a4fe3SMatthias Ringwald } TX_STATE;
129307a4fe3SMatthias Ringwald 
130bd021c4eSMatthias Ringwald // UART Driver + Config
131f9bd6dd7SMatthias Ringwald static const btstack_uart_t * btstack_uart;
132bd021c4eSMatthias Ringwald static btstack_uart_config_t uart_config;
133bd021c4eSMatthias Ringwald 
134307a4fe3SMatthias Ringwald // write state
1358a23fc53SMatthias Ringwald static TX_STATE tx_state;
1362e25934bSMatthias Ringwald #ifdef ENABLE_EHCILL
1372e25934bSMatthias Ringwald static uint8_t * ehcill_tx_data;
1382e25934bSMatthias Ringwald static uint16_t  ehcill_tx_len;   // 0 == no outgoing packet
1392e25934bSMatthias Ringwald #endif
1408a23fc53SMatthias Ringwald 
141*b45b7749SMilanka Ringwald static void (*hci_transport_h4_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler;
142bd021c4eSMatthias Ringwald 
143bd021c4eSMatthias Ringwald // packet reader state machine
144bd021c4eSMatthias Ringwald static  H4_STATE h4_state;
145ea374553SMatthias Ringwald static uint16_t bytes_to_read;
146ea374553SMatthias Ringwald static uint16_t read_pos;
147bd021c4eSMatthias Ringwald 
148bd021c4eSMatthias Ringwald // incoming packet buffer
149fc6cde64SMatthias 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)
150bd021c4eSMatthias Ringwald static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE];
151bd021c4eSMatthias Ringwald 
15277291b13SMatthias Ringwald // Baudrate change bugs in TI CC256x and CYW20704
15339e7ee9fSMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
15477291b13SMatthias Ringwald #define ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
15539e7ee9fSMatthias Ringwald static const uint8_t baud_rate_command_prefix[]   = { 0x01, 0x36, 0xff, 0x04};
15677291b13SMatthias Ringwald #endif
15777291b13SMatthias Ringwald 
15877291b13SMatthias Ringwald #ifdef ENABLE_CYPRESS_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
15977291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
16077291b13SMatthias Ringwald #error "Please enable either ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND or ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND"
16177291b13SMatthias Ringwald #endif
16277291b13SMatthias Ringwald #define ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
16377291b13SMatthias Ringwald static const uint8_t baud_rate_command_prefix[]   = { 0x01, 0x18, 0xfc, 0x06};
16477291b13SMatthias Ringwald #endif
16577291b13SMatthias Ringwald 
16677291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
16777291b13SMatthias Ringwald static const uint8_t local_version_event_prefix[] = { 0x04, 0x0e, 0x0c, 0x01, 0x01, 0x10};
16839e7ee9fSMatthias Ringwald static enum {
1697741d2d0SMatthias Ringwald     BAUDRATE_CHANGE_WORKAROUND_IDLE,
1707741d2d0SMatthias Ringwald     BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED,
1717741d2d0SMatthias Ringwald     BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT,
1727741d2d0SMatthias Ringwald     BAUDRATE_CHANGE_WORKAROUND_DONE
1737741d2d0SMatthias Ringwald } baudrate_change_workaround_state;
17439e7ee9fSMatthias Ringwald #endif
17539e7ee9fSMatthias Ringwald 
176bd021c4eSMatthias Ringwald static int hci_transport_h4_set_baudrate(uint32_t baudrate){
177bace42efSMatthias Ringwald     log_info("hci_transport_h4_set_baudrate %"PRIu32, baudrate);
178bd021c4eSMatthias Ringwald     return btstack_uart->set_baudrate(baudrate);
179bd021c4eSMatthias Ringwald }
180bd021c4eSMatthias Ringwald 
181bd021c4eSMatthias Ringwald static void hci_transport_h4_reset_statemachine(void){
182bd021c4eSMatthias Ringwald     h4_state = H4_W4_PACKET_TYPE;
183bd021c4eSMatthias Ringwald     read_pos = 0;
184bd021c4eSMatthias Ringwald     bytes_to_read = 1;
185bd021c4eSMatthias Ringwald }
186bd021c4eSMatthias Ringwald 
187bd021c4eSMatthias Ringwald static void hci_transport_h4_trigger_next_read(void){
1888a23fc53SMatthias Ringwald     // log_info("hci_transport_h4_trigger_next_read: %u bytes", bytes_to_read);
189bd021c4eSMatthias Ringwald     btstack_uart->receive_block(&hci_packet[read_pos], bytes_to_read);
190bd021c4eSMatthias Ringwald }
191bd021c4eSMatthias Ringwald 
192bb806528SMatthias Ringwald static void hci_transport_h4_packet_complete(void){
193bb806528SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
194bb806528SMatthias Ringwald     if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_IDLE
195bb806528SMatthias Ringwald             && memcmp(hci_packet, local_version_event_prefix, sizeof(local_version_event_prefix)) == 0){
196bb806528SMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
197bb806528SMatthias Ringwald                 if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
198bb806528SMatthias Ringwald                     // detect TI CC256x controller based on manufacturer
199bb806528SMatthias Ringwald                     log_info("Detected CC256x controller");
200bb806528SMatthias Ringwald                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED;
201bb806528SMatthias Ringwald                 } else {
202bb806528SMatthias Ringwald                     // work around not needed
203bb806528SMatthias Ringwald                     log_info("Bluetooth controller not by TI");
204bb806528SMatthias Ringwald                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE;
205bb806528SMatthias Ringwald                 }
206bb806528SMatthias Ringwald #endif
207bb806528SMatthias Ringwald #ifdef ENABLE_CYPRESS_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
208bb806528SMatthias Ringwald                 if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){
209bb806528SMatthias Ringwald                     // detect Cypress controller based on manufacturer
210bb806528SMatthias Ringwald                     log_info("Detected Cypress controller");
211bb806528SMatthias Ringwald                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED;
212bb806528SMatthias Ringwald                 } else {
213bb806528SMatthias Ringwald                     // work around not needed
214bb806528SMatthias Ringwald                     log_info("Bluetooth controller not by Cypress");
215bb806528SMatthias Ringwald                     baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE;
216bb806528SMatthias Ringwald                 }
217bb806528SMatthias Ringwald #endif
218bb806528SMatthias Ringwald             }
219bb806528SMatthias Ringwald #endif
2204ea43905SMatthias Ringwald     uint16_t packet_len = read_pos-1u;
221bd021c4eSMatthias Ringwald 
222bb806528SMatthias Ringwald     // reset state machine before delivering packet to stack as it might close the transport
223bb806528SMatthias Ringwald     hci_transport_h4_reset_statemachine();
224*b45b7749SMilanka Ringwald     hci_transport_h4_packet_handler(hci_packet[0], &hci_packet[1], packet_len);
225bb806528SMatthias Ringwald }
226bb806528SMatthias Ringwald 
227bb806528SMatthias Ringwald static void hci_transport_h4_block_read(void){
228cab2c687SMatthias Ringwald 
229bd021c4eSMatthias Ringwald     read_pos += bytes_to_read;
230bd021c4eSMatthias Ringwald 
231bd021c4eSMatthias Ringwald     switch (h4_state) {
232bd021c4eSMatthias Ringwald         case H4_W4_PACKET_TYPE:
233bd021c4eSMatthias Ringwald             switch (hci_packet[0]){
234bd021c4eSMatthias Ringwald                 case HCI_EVENT_PACKET:
235bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_EVENT_HEADER_SIZE;
236bd021c4eSMatthias Ringwald                     h4_state = H4_W4_EVENT_HEADER;
237bd021c4eSMatthias Ringwald                     break;
238bd021c4eSMatthias Ringwald                 case HCI_ACL_DATA_PACKET:
239bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_ACL_HEADER_SIZE;
240bd021c4eSMatthias Ringwald                     h4_state = H4_W4_ACL_HEADER;
241bd021c4eSMatthias Ringwald                     break;
242bd021c4eSMatthias Ringwald                 case HCI_SCO_DATA_PACKET:
243bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_SCO_HEADER_SIZE;
244bd021c4eSMatthias Ringwald                     h4_state = H4_W4_SCO_HEADER;
245bd021c4eSMatthias Ringwald                     break;
246f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
247307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_IND:
248307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_ACK:
249307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
250307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_ACK:
2518a23fc53SMatthias Ringwald                     hci_transport_h4_ehcill_handle_command(hci_packet[0]);
2528a23fc53SMatthias Ringwald                     hci_transport_h4_reset_statemachine();
253307a4fe3SMatthias Ringwald                     break;
254307a4fe3SMatthias Ringwald #endif
255bd021c4eSMatthias Ringwald                 default:
2568a23fc53SMatthias Ringwald                     log_error("hci_transport_h4: invalid packet type 0x%02x", hci_packet[0]);
257bd021c4eSMatthias Ringwald                     hci_transport_h4_reset_statemachine();
258bd021c4eSMatthias Ringwald                     break;
259bd021c4eSMatthias Ringwald             }
260bd021c4eSMatthias Ringwald             break;
261bd021c4eSMatthias Ringwald 
262bd021c4eSMatthias Ringwald         case H4_W4_EVENT_HEADER:
263bd021c4eSMatthias Ringwald             bytes_to_read = hci_packet[2];
264e8b81068SMatthias Ringwald             // check Event length
265ea374553SMatthias Ringwald             if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_EVENT_HEADER_SIZE)){
266e8b81068SMatthias 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);
267e8b81068SMatthias Ringwald                 hci_transport_h4_reset_statemachine();
268e8b81068SMatthias Ringwald                 break;
269e8b81068SMatthias Ringwald             }
270bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
271bd021c4eSMatthias Ringwald             break;
272bd021c4eSMatthias Ringwald 
273bd021c4eSMatthias Ringwald         case H4_W4_ACL_HEADER:
274bd021c4eSMatthias Ringwald             bytes_to_read = little_endian_read_16( hci_packet, 3);
275bd021c4eSMatthias Ringwald             // check ACL length
276ea374553SMatthias Ringwald             if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE)){
277fc6cde64SMatthias 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);
278bd021c4eSMatthias Ringwald                 hci_transport_h4_reset_statemachine();
279bd021c4eSMatthias Ringwald                 break;
280bd021c4eSMatthias Ringwald             }
281bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
282bd021c4eSMatthias Ringwald             break;
283bd021c4eSMatthias Ringwald 
284bd021c4eSMatthias Ringwald         case H4_W4_SCO_HEADER:
285bd021c4eSMatthias Ringwald             bytes_to_read = hci_packet[3];
286e8b81068SMatthias Ringwald             // check SCO length
287ea374553SMatthias Ringwald             if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_SCO_HEADER_SIZE)){
288e8b81068SMatthias 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);
289e8b81068SMatthias Ringwald                 hci_transport_h4_reset_statemachine();
290e8b81068SMatthias Ringwald                 break;
291e8b81068SMatthias Ringwald             }
292bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
293bd021c4eSMatthias Ringwald             break;
294bd021c4eSMatthias Ringwald 
295bd021c4eSMatthias Ringwald         case H4_W4_PAYLOAD:
296bb806528SMatthias Ringwald             hci_transport_h4_packet_complete();
297bd021c4eSMatthias Ringwald             break;
298ad254f5dSMatthias Ringwald 
299ad254f5dSMatthias Ringwald         case H4_OFF:
300ad254f5dSMatthias Ringwald             bytes_to_read = 0;
301e80b814dSMatthias Ringwald             break;
3027bbeb3adSMilanka Ringwald         default:
3037bbeb3adSMilanka Ringwald             btstack_assert(false);
3047bbeb3adSMilanka Ringwald             break;
305bd021c4eSMatthias Ringwald     }
30639e7ee9fSMatthias Ringwald 
30777291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
3087741d2d0SMatthias Ringwald     if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT){
3097741d2d0SMatthias Ringwald         baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_IDLE;
31039e7ee9fSMatthias Ringwald         // avoid flowcontrol problem by reading expected hci command complete event of 7 bytes in a single block read
31139e7ee9fSMatthias Ringwald         h4_state = H4_W4_PAYLOAD;
31239e7ee9fSMatthias Ringwald         bytes_to_read = 7;
31339e7ee9fSMatthias Ringwald     }
31439e7ee9fSMatthias Ringwald #endif
31539e7ee9fSMatthias Ringwald 
316bb806528SMatthias Ringwald     // forward packet if payload size == 0
3174ea43905SMatthias Ringwald     if (h4_state == H4_W4_PAYLOAD && bytes_to_read == 0u) {
318bb806528SMatthias Ringwald         hci_transport_h4_packet_complete();
319bb806528SMatthias Ringwald     }
320bb806528SMatthias Ringwald 
321e80b814dSMatthias Ringwald     if (h4_state != H4_OFF) {
322bd021c4eSMatthias Ringwald         hci_transport_h4_trigger_next_read();
323bd021c4eSMatthias Ringwald     }
324e80b814dSMatthias Ringwald }
325bd021c4eSMatthias Ringwald 
326307a4fe3SMatthias Ringwald static void hci_transport_h4_block_sent(void){
3278334d3d8SMatthias Ringwald 
3288334d3d8SMatthias Ringwald     static const uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
3298334d3d8SMatthias Ringwald 
330307a4fe3SMatthias Ringwald     switch (tx_state){
331307a4fe3SMatthias Ringwald         case TX_W4_PACKET_SENT:
332307a4fe3SMatthias Ringwald             // packet fully sent, reset state
3332e25934bSMatthias Ringwald #ifdef ENABLE_EHCILL
3342e25934bSMatthias Ringwald             ehcill_tx_len = 0;
3352e25934bSMatthias Ringwald #endif
3368a23fc53SMatthias Ringwald             tx_state = TX_IDLE;
337307a4fe3SMatthias Ringwald 
338f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
3398a23fc53SMatthias Ringwald             // notify eHCILL engine
3408a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_handle_packet_sent();
341307a4fe3SMatthias Ringwald #endif
342307a4fe3SMatthias Ringwald             // notify upper stack that it can send again
343*b45b7749SMilanka Ringwald             hci_transport_h4_packet_handler(HCI_EVENT_PACKET, (uint8_t *) &packet_sent_event[0], sizeof(packet_sent_event));
344307a4fe3SMatthias Ringwald             break;
345307a4fe3SMatthias Ringwald 
346f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
3478a23fc53SMatthias Ringwald         case TX_W4_EHCILL_SENT:
348b403028eSMatthias Ringwald         case TX_W4_WAKEUP:
3498a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_handle_ehcill_command_sent();
350307a4fe3SMatthias Ringwald             break;
351307a4fe3SMatthias Ringwald #endif
352307a4fe3SMatthias Ringwald 
353307a4fe3SMatthias Ringwald         default:
354307a4fe3SMatthias Ringwald             break;
355307a4fe3SMatthias Ringwald     }
356307a4fe3SMatthias Ringwald }
357307a4fe3SMatthias Ringwald 
358307a4fe3SMatthias Ringwald static int hci_transport_h4_can_send_now(uint8_t packet_type){
359cebe3e9eSMatthias Ringwald     UNUSED(packet_type);
360307a4fe3SMatthias Ringwald     return tx_state == TX_IDLE;
361307a4fe3SMatthias Ringwald }
362307a4fe3SMatthias Ringwald 
363307a4fe3SMatthias Ringwald static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){
36439e7ee9fSMatthias Ringwald 
365307a4fe3SMatthias Ringwald     // store packet type before actual data and increase size
36641f9be70SMatthias Ringwald     uint8_t * buffer = &packet[-1];
36741f9be70SMatthias Ringwald     uint32_t  buffer_size = size + 1;
36841f9be70SMatthias Ringwald     buffer[0] = packet_type;
369307a4fe3SMatthias Ringwald 
37077291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND
3717741d2d0SMatthias Ringwald     if ((baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED)
37241f9be70SMatthias Ringwald     && (memcmp(buffer, baud_rate_command_prefix, sizeof(baud_rate_command_prefix)) == 0)) {
3737741d2d0SMatthias Ringwald         log_info("Baud rate command detected, expect command complete event next");
3747741d2d0SMatthias Ringwald         baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT;
37539e7ee9fSMatthias Ringwald     }
37639e7ee9fSMatthias Ringwald #endif
37739e7ee9fSMatthias Ringwald 
378f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
3792e25934bSMatthias Ringwald     // store request for later
38041f9be70SMatthias Ringwald     ehcill_tx_len   = buffer_size;
38141f9be70SMatthias Ringwald     ehcill_tx_data  = buffer;
3829ecd1ba8SMatthias Ringwald     switch (ehcill_state){
3839ecd1ba8SMatthias Ringwald         case EHCILL_STATE_SLEEP:
3848a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_trigger_wakeup();
3858a23fc53SMatthias Ringwald             return 0;
3869ecd1ba8SMatthias Ringwald         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
387b403028eSMatthias Ringwald             log_info("eHILL: send next packet, state EHCILL_STATE_W2_SEND_SLEEP_ACK");
3889ecd1ba8SMatthias Ringwald             return 0;
3899ecd1ba8SMatthias Ringwald         default:
3909ecd1ba8SMatthias Ringwald             break;
3918a23fc53SMatthias Ringwald     }
3928a23fc53SMatthias Ringwald #endif
3938a23fc53SMatthias Ringwald 
3948a23fc53SMatthias Ringwald     // start sending
3958a23fc53SMatthias Ringwald     tx_state = TX_W4_PACKET_SENT;
39641f9be70SMatthias Ringwald     btstack_uart->send_block(buffer, buffer_size);
397307a4fe3SMatthias Ringwald     return 0;
398307a4fe3SMatthias Ringwald }
399307a4fe3SMatthias Ringwald 
400bd021c4eSMatthias Ringwald static void hci_transport_h4_init(const void * transport_config){
401bd021c4eSMatthias Ringwald     // check for hci_transport_config_uart_t
402bd021c4eSMatthias Ringwald     if (!transport_config) {
403bd021c4eSMatthias Ringwald         log_error("hci_transport_h4: no config!");
404bd021c4eSMatthias Ringwald         return;
405bd021c4eSMatthias Ringwald     }
406bd021c4eSMatthias Ringwald     if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) {
407bd021c4eSMatthias Ringwald         log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!");
408bd021c4eSMatthias Ringwald         return;
409bd021c4eSMatthias Ringwald     }
410bd021c4eSMatthias Ringwald 
411bd021c4eSMatthias Ringwald     // extract UART config from transport config
412bd021c4eSMatthias Ringwald     hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config;
413bd021c4eSMatthias Ringwald     uart_config.baudrate    = hci_transport_config_uart->baudrate_init;
414bd021c4eSMatthias Ringwald     uart_config.flowcontrol = hci_transport_config_uart->flowcontrol;
415e76f5dd4SMatthias Ringwald     uart_config.parity      = hci_transport_config_uart->parity;
416bd021c4eSMatthias Ringwald     uart_config.device_name = hci_transport_config_uart->device_name;
417bd021c4eSMatthias Ringwald 
418c682b8ecSMatthias Ringwald     // set state to off
419c682b8ecSMatthias Ringwald     tx_state = TX_OFF;
420c682b8ecSMatthias Ringwald     h4_state = H4_OFF;
421c682b8ecSMatthias Ringwald 
422bd021c4eSMatthias Ringwald     // setup UART driver
423bd021c4eSMatthias Ringwald     btstack_uart->init(&uart_config);
424bd021c4eSMatthias Ringwald     btstack_uart->set_block_received(&hci_transport_h4_block_read);
425bd021c4eSMatthias Ringwald     btstack_uart->set_block_sent(&hci_transport_h4_block_sent);
426bd021c4eSMatthias Ringwald }
427bd021c4eSMatthias Ringwald 
428bd021c4eSMatthias Ringwald static int hci_transport_h4_open(void){
429c682b8ecSMatthias Ringwald     // open uart driver
430bd021c4eSMatthias Ringwald     int res = btstack_uart->open();
4319305033eSMatthias Ringwald     if (res != 0){
432bd021c4eSMatthias Ringwald         return res;
433bd021c4eSMatthias Ringwald     }
434c682b8ecSMatthias Ringwald 
435c682b8ecSMatthias Ringwald     // init rx + tx state machines
436bd021c4eSMatthias Ringwald     hci_transport_h4_reset_statemachine();
437bd021c4eSMatthias Ringwald     hci_transport_h4_trigger_next_read();
438307a4fe3SMatthias Ringwald     tx_state = TX_IDLE;
439307a4fe3SMatthias Ringwald 
440f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
4418a23fc53SMatthias Ringwald     hci_transport_h4_ehcill_open();
442307a4fe3SMatthias Ringwald #endif
443bd021c4eSMatthias Ringwald     return 0;
444bd021c4eSMatthias Ringwald }
445bd021c4eSMatthias Ringwald 
446bd021c4eSMatthias Ringwald static int hci_transport_h4_close(void){
447c682b8ecSMatthias Ringwald     // set state to off
448c682b8ecSMatthias Ringwald     tx_state = TX_OFF;
449c682b8ecSMatthias Ringwald     h4_state = H4_OFF;
450c682b8ecSMatthias Ringwald 
451c682b8ecSMatthias Ringwald     // close uart driver
452bd021c4eSMatthias Ringwald     return btstack_uart->close();
453bd021c4eSMatthias Ringwald }
454bd021c4eSMatthias Ringwald 
455bd021c4eSMatthias Ringwald static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
456*b45b7749SMilanka Ringwald     hci_transport_h4_packet_handler = handler;
457bd021c4eSMatthias Ringwald }
458bd021c4eSMatthias Ringwald 
459bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
460cebe3e9eSMatthias Ringwald     UNUSED(packet_type);
461cebe3e9eSMatthias Ringwald     UNUSED(packet);
462cebe3e9eSMatthias Ringwald     UNUSED(size);
463bd021c4eSMatthias Ringwald }
464bd021c4eSMatthias Ringwald 
4658a23fc53SMatthias Ringwald //
466307a4fe3SMatthias Ringwald // --- main part of eHCILL implementation ---
4678a23fc53SMatthias Ringwald //
468307a4fe3SMatthias Ringwald 
469f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
470307a4fe3SMatthias Ringwald 
47162ca45d7SMatthias Ringwald static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){
47262ca45d7SMatthias Ringwald     static int last_state = 0;
47362ca45d7SMatthias Ringwald     if (sleep_active == last_state) return;
47462ca45d7SMatthias Ringwald     last_state = sleep_active;
47562ca45d7SMatthias Ringwald 
47662ca45d7SMatthias Ringwald     log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active);
47762ca45d7SMatthias Ringwald     uint8_t event[3];
47862ca45d7SMatthias Ringwald     event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE;
47962ca45d7SMatthias Ringwald     event[1] = sizeof(event) - 2;
48062ca45d7SMatthias Ringwald     event[2] = sleep_active;
481*b45b7749SMilanka Ringwald     hci_transport_h4_packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
48262ca45d7SMatthias Ringwald }
48362ca45d7SMatthias Ringwald 
484b403028eSMatthias Ringwald static void hci_transport_h4_ehcill_wakeup_handler(void){
485b403028eSMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
486b403028eSMatthias Ringwald     log_info("eHCILL: UART wakeup received");
487b403028eSMatthias Ringwald #endif
488b403028eSMatthias Ringwald     hci_transport_h4_ehcill_handle_command(EHCILL_WAKEUP_SIGNAL);
489b403028eSMatthias Ringwald }
490b403028eSMatthias Ringwald 
4918a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void){
4928a23fc53SMatthias Ringwald     hci_transport_h4_ehcill_reset_statemachine();
4938a23fc53SMatthias Ringwald 
4948a23fc53SMatthias Ringwald     // find best sleep mode to use: wake on CTS, wake on RX, none
4958a23fc53SMatthias Ringwald     btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF;
4968a23fc53SMatthias Ringwald     int supported_sleep_modes = 0;
4978a23fc53SMatthias Ringwald     if (btstack_uart->get_supported_sleep_modes){
4988a23fc53SMatthias Ringwald         supported_sleep_modes = btstack_uart->get_supported_sleep_modes();
499307a4fe3SMatthias Ringwald     }
5008a23fc53SMatthias Ringwald     if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){
5018a23fc53SMatthias Ringwald         log_info("eHCILL: using wake on CTS");
5028a23fc53SMatthias Ringwald         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE;
5038a23fc53SMatthias Ringwald     } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){
5048a23fc53SMatthias Ringwald         log_info("eHCILL: using wake on RX");
5058a23fc53SMatthias Ringwald         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE;
5068a23fc53SMatthias Ringwald     } else {
5078a23fc53SMatthias Ringwald         log_info("eHCILL: UART driver does not provide compatible sleep mode");
5088a23fc53SMatthias Ringwald     }
509b403028eSMatthias Ringwald     if (btstack_uart->set_wakeup_handler){
510b403028eSMatthias Ringwald         btstack_uart->set_wakeup_handler(&hci_transport_h4_ehcill_wakeup_handler);
511b403028eSMatthias Ringwald     }
512307a4fe3SMatthias Ringwald }
513307a4fe3SMatthias Ringwald 
514307a4fe3SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void){
5159ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5169ecd1ba8SMatthias Ringwald     log_info("eHCILL: send WAKEUP_IND");
5179ecd1ba8SMatthias Ringwald #endif
518307a4fe3SMatthias Ringwald     // update state
519307a4fe3SMatthias Ringwald     tx_state     = TX_W4_WAKEUP;
5209ecd1ba8SMatthias Ringwald     ehcill_state = EHCILL_STATE_W4_WAKEUP_IND_OR_ACK;
521307a4fe3SMatthias Ringwald     ehcill_command_to_send = EHCILL_WAKE_UP_IND;
522307a4fe3SMatthias Ringwald     btstack_uart->send_block(&ehcill_command_to_send, 1);
523307a4fe3SMatthias Ringwald }
524307a4fe3SMatthias Ringwald 
525307a4fe3SMatthias Ringwald static int hci_transport_h4_ehcill_outgoing_packet_ready(void){
5262e25934bSMatthias Ringwald     return ehcill_tx_len != 0;
527307a4fe3SMatthias Ringwald }
528307a4fe3SMatthias Ringwald 
529307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void){
530307a4fe3SMatthias Ringwald     ehcill_state = EHCILL_STATE_AWAKE;
531307a4fe3SMatthias Ringwald }
532307a4fe3SMatthias Ringwald 
533307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void){
5349ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5359ecd1ba8SMatthias Ringwald     log_info("eHCILL: send command %02x", ehcill_command_to_send);
5369ecd1ba8SMatthias Ringwald #endif
537307a4fe3SMatthias Ringwald     tx_state = TX_W4_EHCILL_SENT;
5389ecd1ba8SMatthias Ringwald     if (ehcill_command_to_send == EHCILL_GO_TO_SLEEP_ACK){
5399ecd1ba8SMatthias Ringwald         ehcill_state = EHCILL_STATE_SLEEP;
5409ecd1ba8SMatthias Ringwald     }
541307a4fe3SMatthias Ringwald     btstack_uart->send_block(&ehcill_command_to_send, 1);
542307a4fe3SMatthias Ringwald }
543307a4fe3SMatthias Ringwald 
544307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){
545b403028eSMatthias Ringwald 	UNUSED(timer);
5469ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5479ecd1ba8SMatthias Ringwald     log_info("eHCILL: timer triggered");
5489ecd1ba8SMatthias Ringwald #endif
549307a4fe3SMatthias Ringwald     hci_transport_h4_ehcill_send_ehcill_command();
550307a4fe3SMatthias Ringwald }
551307a4fe3SMatthias Ringwald 
552307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){
553307a4fe3SMatthias Ringwald     // setup timer
5549ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5559ecd1ba8SMatthias Ringwald     log_info("eHCILL: set timer for sending command %02x", ehcill_command_to_send);
5569ecd1ba8SMatthias Ringwald #endif
5578a23fc53SMatthias Ringwald     btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler);
558307a4fe3SMatthias Ringwald     btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50);
559307a4fe3SMatthias Ringwald     btstack_run_loop_add_timer(&ehcill_sleep_ack_timer);
560307a4fe3SMatthias Ringwald }
561307a4fe3SMatthias Ringwald 
5628a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void){
5638a23fc53SMatthias Ringwald     switch (tx_state){
5648a23fc53SMatthias Ringwald         case TX_W2_EHCILL_SEND:
5658a23fc53SMatthias Ringwald         case TX_W4_EHCILL_SENT:
5668a23fc53SMatthias Ringwald             // wake up / sleep ack in progress, nothing to do now
5678a23fc53SMatthias Ringwald             return;
5688a23fc53SMatthias Ringwald         case TX_IDLE:
5698a23fc53SMatthias Ringwald         default:
5708a23fc53SMatthias Ringwald             // all clear, prepare for wakeup
5718a23fc53SMatthias Ringwald             break;
5728a23fc53SMatthias Ringwald     }
5738a23fc53SMatthias Ringwald     // UART needed again
57462ca45d7SMatthias Ringwald     hci_transport_h4_ehcill_emit_sleep_state(0);
5758a23fc53SMatthias Ringwald     if (btstack_uart_sleep_mode){
5768a23fc53SMatthias Ringwald         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
5778a23fc53SMatthias Ringwald     }
5788a23fc53SMatthias Ringwald     hci_transport_h4_echill_send_wakeup_ind();
5798a23fc53SMatthias Ringwald }
5808a23fc53SMatthias Ringwald 
5819ecd1ba8SMatthias Ringwald static void hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command){
5829ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
5839ecd1ba8SMatthias Ringwald     log_info("eHCILL: schedule eHCILL command %02x", command);
5849ecd1ba8SMatthias Ringwald #endif
585307a4fe3SMatthias Ringwald     ehcill_command_to_send = command;
586307a4fe3SMatthias Ringwald     switch (tx_state){
587307a4fe3SMatthias Ringwald         case TX_IDLE:
588307a4fe3SMatthias Ringwald             if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){
589307a4fe3SMatthias Ringwald                 // send right away
590307a4fe3SMatthias Ringwald                 hci_transport_h4_ehcill_send_ehcill_command();
591307a4fe3SMatthias Ringwald             } else {
592307a4fe3SMatthias Ringwald                 // change state so BTstack cannot send and setup timer
593307a4fe3SMatthias Ringwald                 tx_state = TX_W2_EHCILL_SEND;
594307a4fe3SMatthias Ringwald                 hci_transport_h4_ehcill_sleep_ack_timer_setup();
595307a4fe3SMatthias Ringwald             }
596307a4fe3SMatthias Ringwald             break;
597307a4fe3SMatthias Ringwald         default:
598307a4fe3SMatthias Ringwald             break;
599307a4fe3SMatthias Ringwald     }
600307a4fe3SMatthias Ringwald }
601307a4fe3SMatthias Ringwald 
6028a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action){
603307a4fe3SMatthias Ringwald     // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size);
604307a4fe3SMatthias Ringwald     switch(ehcill_state){
605307a4fe3SMatthias Ringwald         case EHCILL_STATE_AWAKE:
606307a4fe3SMatthias Ringwald             switch(action){
607307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_IND:
6089ecd1ba8SMatthias Ringwald                     ehcill_state = EHCILL_STATE_W2_SEND_SLEEP_ACK;
6099ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
6109ecd1ba8SMatthias Ringwald                     log_info("eHCILL: Received GO_TO_SLEEP_IND RX");
6119ecd1ba8SMatthias Ringwald #endif
6129ecd1ba8SMatthias Ringwald                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_GO_TO_SLEEP_ACK);
613307a4fe3SMatthias Ringwald                     break;
614307a4fe3SMatthias Ringwald                 default:
615307a4fe3SMatthias Ringwald                     break;
616307a4fe3SMatthias Ringwald             }
617307a4fe3SMatthias Ringwald             break;
618307a4fe3SMatthias Ringwald 
6199ecd1ba8SMatthias Ringwald         case EHCILL_STATE_W2_SEND_SLEEP_ACK:
620307a4fe3SMatthias Ringwald             switch(action){
621307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
622307a4fe3SMatthias Ringwald                     ehcill_state = EHCILL_STATE_AWAKE;
62362ca45d7SMatthias Ringwald                     hci_transport_h4_ehcill_emit_sleep_state(0);
624b403028eSMatthias Ringwald                     if (btstack_uart_sleep_mode){
625b403028eSMatthias Ringwald                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
626b403028eSMatthias Ringwald                     }
627b403028eSMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
628b403028eSMatthias Ringwald                     log_info("eHCILL: Received WAKE_UP_IND RX");
629b403028eSMatthias Ringwald #endif
630b403028eSMatthias Ringwald                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK);
631b403028eSMatthias Ringwald                     break;
632b403028eSMatthias Ringwald 
633b403028eSMatthias Ringwald                 default:
634b403028eSMatthias Ringwald                     break;
635b403028eSMatthias Ringwald             }
636b403028eSMatthias Ringwald             break;
637b403028eSMatthias Ringwald 
638b403028eSMatthias Ringwald         case EHCILL_STATE_SLEEP:
639b403028eSMatthias Ringwald             switch(action){
640b403028eSMatthias Ringwald                 case EHCILL_WAKEUP_SIGNAL:
641b403028eSMatthias 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                     break;
646b403028eSMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
647b403028eSMatthias Ringwald                     ehcill_state = EHCILL_STATE_AWAKE;
648b403028eSMatthias Ringwald                     hci_transport_h4_ehcill_emit_sleep_state(0);
649b403028eSMatthias Ringwald                     if (btstack_uart_sleep_mode){
650b403028eSMatthias Ringwald                         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
651b403028eSMatthias Ringwald                     }
6529ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
6539ecd1ba8SMatthias Ringwald                     log_info("eHCILL: Received WAKE_UP_IND RX");
6549ecd1ba8SMatthias Ringwald #endif
6559ecd1ba8SMatthias Ringwald                     hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK);
656307a4fe3SMatthias Ringwald                     break;
657307a4fe3SMatthias Ringwald 
658307a4fe3SMatthias Ringwald                 default:
659307a4fe3SMatthias Ringwald                     break;
660307a4fe3SMatthias Ringwald             }
661307a4fe3SMatthias Ringwald             break;
662307a4fe3SMatthias Ringwald 
6639ecd1ba8SMatthias Ringwald         case EHCILL_STATE_W4_WAKEUP_IND_OR_ACK:
664307a4fe3SMatthias Ringwald             switch(action){
665307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
666307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_ACK:
6679ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
6689ecd1ba8SMatthias Ringwald                     log_info("eHCILL: Received WAKE_UP (%02x)", action);
6699ecd1ba8SMatthias Ringwald #endif
670307a4fe3SMatthias Ringwald                     tx_state = TX_W4_PACKET_SENT;
671307a4fe3SMatthias Ringwald                     ehcill_state = EHCILL_STATE_AWAKE;
6722e25934bSMatthias Ringwald                     btstack_uart->send_block(ehcill_tx_data, ehcill_tx_len);
673307a4fe3SMatthias Ringwald                     break;
674307a4fe3SMatthias Ringwald                 default:
675307a4fe3SMatthias Ringwald                     break;
676307a4fe3SMatthias Ringwald             }
677307a4fe3SMatthias Ringwald             break;
678307a4fe3SMatthias Ringwald     }
679307a4fe3SMatthias Ringwald }
6808a23fc53SMatthias Ringwald 
6818a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void){
6829ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
6839ecd1ba8SMatthias Ringwald         log_info("eHCILL: handle packet sent, command to send %02x", ehcill_command_to_send);
6849ecd1ba8SMatthias Ringwald #endif
6858a23fc53SMatthias Ringwald     // now, send pending ehcill command if neccessary
6868a23fc53SMatthias Ringwald     switch (ehcill_command_to_send){
6878a23fc53SMatthias Ringwald         case EHCILL_GO_TO_SLEEP_ACK:
6888a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_sleep_ack_timer_setup();
6898a23fc53SMatthias Ringwald             break;
6908a23fc53SMatthias Ringwald         case EHCILL_WAKE_UP_IND:
6918a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_send_ehcill_command();
6928a23fc53SMatthias Ringwald             break;
6938a23fc53SMatthias Ringwald         default:
6948a23fc53SMatthias Ringwald             break;
6958a23fc53SMatthias Ringwald     }
6968a23fc53SMatthias Ringwald }
6978a23fc53SMatthias Ringwald 
6988a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){
6998a23fc53SMatthias Ringwald     tx_state = TX_IDLE;
7008a23fc53SMatthias Ringwald     int command = ehcill_command_to_send;
7018a23fc53SMatthias Ringwald     ehcill_command_to_send = 0;
7029ecd1ba8SMatthias Ringwald 
7039ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
7049ecd1ba8SMatthias Ringwald         log_info("eHCILL: handle eHCILL sent, command was %02x", command);
7059ecd1ba8SMatthias Ringwald #endif
7069ecd1ba8SMatthias Ringwald 
7078a23fc53SMatthias Ringwald     if (command == EHCILL_GO_TO_SLEEP_ACK) {
7089ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL
7098a23fc53SMatthias Ringwald         log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode");
7109ecd1ba8SMatthias Ringwald #endif
7118a23fc53SMatthias Ringwald         // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent
7128a23fc53SMatthias Ringwald         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
7138a23fc53SMatthias Ringwald             btstack_uart->set_sleep(btstack_uart_sleep_mode);
7148a23fc53SMatthias Ringwald         }
71562ca45d7SMatthias Ringwald         hci_transport_h4_ehcill_emit_sleep_state(1);
7168a23fc53SMatthias Ringwald     }
7178a23fc53SMatthias Ringwald     // already packet ready? then start wakeup
7188a23fc53SMatthias Ringwald     if (hci_transport_h4_ehcill_outgoing_packet_ready()){
71962ca45d7SMatthias Ringwald         hci_transport_h4_ehcill_emit_sleep_state(0);
720b403028eSMatthias Ringwald         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
7218a23fc53SMatthias Ringwald             btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
7228a23fc53SMatthias Ringwald         }
7239ecd1ba8SMatthias Ringwald         if (command != EHCILL_WAKE_UP_IND){
7248a23fc53SMatthias Ringwald             hci_transport_h4_echill_send_wakeup_ind();
7258a23fc53SMatthias Ringwald         }
7268a23fc53SMatthias Ringwald     }
7279ecd1ba8SMatthias Ringwald }
7288a23fc53SMatthias Ringwald 
729307a4fe3SMatthias Ringwald #endif
730307a4fe3SMatthias Ringwald // --- end of eHCILL implementation ---------
731307a4fe3SMatthias Ringwald 
7328334d3d8SMatthias Ringwald 
7338334d3d8SMatthias Ringwald // configure and return h4 singleton
734bd021c4eSMatthias Ringwald static const hci_transport_t hci_transport_h4 = {
735bd021c4eSMatthias Ringwald         /* const char * name; */                                        "H4",
736bd021c4eSMatthias Ringwald         /* void   (*init) (const void *transport_config); */            &hci_transport_h4_init,
737bd021c4eSMatthias Ringwald         /* int    (*open)(void); */                                     &hci_transport_h4_open,
738bd021c4eSMatthias Ringwald         /* int    (*close)(void); */                                    &hci_transport_h4_close,
739bd021c4eSMatthias Ringwald         /* void   (*register_packet_handler)(void (*handler)(...); */   &hci_transport_h4_register_packet_handler,
740bd021c4eSMatthias Ringwald         /* int    (*can_send_packet_now)(uint8_t packet_type); */       &hci_transport_h4_can_send_now,
741bd021c4eSMatthias Ringwald         /* int    (*send_packet)(...); */                               &hci_transport_h4_send_packet,
742bd021c4eSMatthias Ringwald         /* int    (*set_baudrate)(uint32_t baudrate); */                &hci_transport_h4_set_baudrate,
743bd021c4eSMatthias Ringwald         /* void   (*reset_link)(void); */                               NULL,
744ee752bb8SMatthias Ringwald         /* void   (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL,
745bd021c4eSMatthias Ringwald };
746bd021c4eSMatthias Ringwald 
747f9bd6dd7SMatthias Ringwald const hci_transport_t * hci_transport_h4_instance_for_uart(const btstack_uart_t * uart_driver){
748bd021c4eSMatthias Ringwald     btstack_uart = uart_driver;
749bd021c4eSMatthias Ringwald     return &hci_transport_h4;
750bd021c4eSMatthias Ringwald }
751f9bd6dd7SMatthias Ringwald 
752f9bd6dd7SMatthias Ringwald // @deprecated
753f9bd6dd7SMatthias Ringwald const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) {
754f9bd6dd7SMatthias Ringwald     btstack_uart = (const btstack_uart_t *) uart_driver;
755f9bd6dd7SMatthias Ringwald     return &hci_transport_h4;
756f9bd6dd7SMatthias Ringwald }
757