xref: /btstack/src/hci_transport_h4.c (revision 8a23fc5312800685de9e8e4fa3907c561747a9c6)
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 
38bd021c4eSMatthias Ringwald /*
39bd021c4eSMatthias Ringwald  *  hci_h4_transport.c
40bd021c4eSMatthias Ringwald  *
41*8a23fc53SMatthias Ringwald  *  HCI Transport API implementation for H4 protocol over POSIX with optional support for eHCILL
42bd021c4eSMatthias Ringwald  *
43bd021c4eSMatthias Ringwald  *  Created by Matthias Ringwald on 4/29/09.
44bd021c4eSMatthias Ringwald  */
45bd021c4eSMatthias Ringwald 
46bd021c4eSMatthias Ringwald #include "btstack_config.h"
47bd021c4eSMatthias Ringwald 
48bd021c4eSMatthias Ringwald #include "btstack_debug.h"
49bd021c4eSMatthias Ringwald #include "hci.h"
50bd021c4eSMatthias Ringwald #include "hci_transport.h"
51bd021c4eSMatthias Ringwald #include "btstack_uart_block.h"
52bd021c4eSMatthias Ringwald 
53bd021c4eSMatthias Ringwald #ifdef HAVE_EHCILL
54307a4fe3SMatthias Ringwald 
55307a4fe3SMatthias Ringwald // eHCILL commands
56307a4fe3SMatthias Ringwald #define EHCILL_GO_TO_SLEEP_IND 0x030
57307a4fe3SMatthias Ringwald #define EHCILL_GO_TO_SLEEP_ACK 0x031
58307a4fe3SMatthias Ringwald #define EHCILL_WAKE_UP_IND     0x032
59307a4fe3SMatthias Ringwald #define EHCILL_WAKE_UP_ACK     0x033
60307a4fe3SMatthias Ringwald 
61*8a23fc53SMatthias Ringwald static int  hci_transport_h4_ehcill_outgoing_packet_ready(void);
62*8a23fc53SMatthias Ringwald static int  hci_transport_h4_ehcill_sleep_mode_active(void);
63*8a23fc53SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void);
64*8a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action);
65*8a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void);
66*8a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void);
67*8a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void);
68307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void);
69307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void);
70307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void);
71*8a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void);
72307a4fe3SMatthias Ringwald 
73307a4fe3SMatthias Ringwald typedef enum {
74307a4fe3SMatthias Ringwald     EHCILL_STATE_SLEEP,
75307a4fe3SMatthias Ringwald     EHCILL_STATE_W4_ACK,
76307a4fe3SMatthias Ringwald     EHCILL_STATE_AWAKE
77307a4fe3SMatthias Ringwald } EHCILL_STATE;
78307a4fe3SMatthias Ringwald 
79307a4fe3SMatthias Ringwald // eHCILL state machine
80307a4fe3SMatthias Ringwald static EHCILL_STATE ehcill_state;
81307a4fe3SMatthias Ringwald static uint8_t      ehcill_command_to_send;
82307a4fe3SMatthias Ringwald 
83307a4fe3SMatthias Ringwald // work around for eHCILL problem
84307a4fe3SMatthias Ringwald static btstack_timer_source_t ehcill_sleep_ack_timer;
85307a4fe3SMatthias Ringwald 
86bd021c4eSMatthias Ringwald #endif
87bd021c4eSMatthias Ringwald 
8882d05f16SMatthias Ringwald 
89bd021c4eSMatthias Ringwald // assert pre-buffer for packet type is available
90bd021c4eSMatthias Ringwald #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0)
91bd021c4eSMatthias Ringwald #error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h
92bd021c4eSMatthias Ringwald #endif
93bd021c4eSMatthias Ringwald 
94bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
95bd021c4eSMatthias Ringwald 
96bd021c4eSMatthias Ringwald typedef enum {
97bd021c4eSMatthias Ringwald     H4_W4_PACKET_TYPE,
98bd021c4eSMatthias Ringwald     H4_W4_EVENT_HEADER,
99bd021c4eSMatthias Ringwald     H4_W4_ACL_HEADER,
100bd021c4eSMatthias Ringwald     H4_W4_SCO_HEADER,
101bd021c4eSMatthias Ringwald     H4_W4_PAYLOAD,
102bd021c4eSMatthias Ringwald } H4_STATE;
103bd021c4eSMatthias Ringwald 
104307a4fe3SMatthias Ringwald typedef enum {
105307a4fe3SMatthias Ringwald     TX_IDLE = 1,
106307a4fe3SMatthias Ringwald     TX_W4_PACKET_SENT,
107307a4fe3SMatthias Ringwald #ifdef HAVE_EHCILL
108307a4fe3SMatthias Ringwald     TX_W4_WAKEUP,
109307a4fe3SMatthias Ringwald     TX_W2_EHCILL_SEND,
110307a4fe3SMatthias Ringwald     TX_W4_EHCILL_SENT,
111307a4fe3SMatthias Ringwald #endif
112307a4fe3SMatthias Ringwald } TX_STATE;
113307a4fe3SMatthias Ringwald 
114bd021c4eSMatthias Ringwald // UART Driver + Config
115bd021c4eSMatthias Ringwald static const btstack_uart_block_t * btstack_uart;
116*8a23fc53SMatthias Ringwald static btstack_uart_sleep_mode_t btstack_uart_sleep_mode;
117bd021c4eSMatthias Ringwald static btstack_uart_config_t uart_config;
118bd021c4eSMatthias Ringwald 
119307a4fe3SMatthias Ringwald // write state
120*8a23fc53SMatthias Ringwald static TX_STATE tx_state;
121*8a23fc53SMatthias Ringwald static uint8_t * tx_data;
122307a4fe3SMatthias Ringwald static uint16_t  tx_len;   // 0 == no outgoing packet
123*8a23fc53SMatthias Ringwald 
124307a4fe3SMatthias Ringwald static uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
125bd021c4eSMatthias Ringwald 
126bd021c4eSMatthias Ringwald static  void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler;
127bd021c4eSMatthias Ringwald 
128bd021c4eSMatthias Ringwald // packet reader state machine
129bd021c4eSMatthias Ringwald static  H4_STATE h4_state;
130bd021c4eSMatthias Ringwald static int bytes_to_read;
131bd021c4eSMatthias Ringwald static int read_pos;
132bd021c4eSMatthias Ringwald 
133bd021c4eSMatthias Ringwald // incoming packet buffer
134bd021c4eSMatthias Ringwald static uint8_t hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 1 + HCI_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, event header + event data)
135bd021c4eSMatthias Ringwald static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE];
136bd021c4eSMatthias Ringwald 
137bd021c4eSMatthias Ringwald static int hci_transport_h4_set_baudrate(uint32_t baudrate){
138bd021c4eSMatthias Ringwald     log_info("hci_transport_h4_set_baudrate %u", baudrate);
139bd021c4eSMatthias Ringwald     return btstack_uart->set_baudrate(baudrate);
140bd021c4eSMatthias Ringwald }
141bd021c4eSMatthias Ringwald 
142bd021c4eSMatthias Ringwald static void hci_transport_h4_reset_statemachine(void){
143bd021c4eSMatthias Ringwald     h4_state = H4_W4_PACKET_TYPE;
144bd021c4eSMatthias Ringwald     read_pos = 0;
145bd021c4eSMatthias Ringwald     bytes_to_read = 1;
146bd021c4eSMatthias Ringwald }
147bd021c4eSMatthias Ringwald 
148bd021c4eSMatthias Ringwald static void hci_transport_h4_trigger_next_read(void){
149*8a23fc53SMatthias Ringwald     // log_info("hci_transport_h4_trigger_next_read: %u bytes", bytes_to_read);
150bd021c4eSMatthias Ringwald     btstack_uart->receive_block(&hci_packet[read_pos], bytes_to_read);
151bd021c4eSMatthias Ringwald }
152bd021c4eSMatthias Ringwald 
153bd021c4eSMatthias Ringwald static void hci_transport_h4_block_read(void){
154bd021c4eSMatthias Ringwald 
155bd021c4eSMatthias Ringwald     read_pos += bytes_to_read;
156bd021c4eSMatthias Ringwald 
157bd021c4eSMatthias Ringwald     switch (h4_state) {
158bd021c4eSMatthias Ringwald         case H4_W4_PACKET_TYPE:
159bd021c4eSMatthias Ringwald             switch (hci_packet[0]){
160bd021c4eSMatthias Ringwald                 case HCI_EVENT_PACKET:
161bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_EVENT_HEADER_SIZE;
162bd021c4eSMatthias Ringwald                     h4_state = H4_W4_EVENT_HEADER;
163bd021c4eSMatthias Ringwald                     break;
164bd021c4eSMatthias Ringwald                 case HCI_ACL_DATA_PACKET:
165bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_ACL_HEADER_SIZE;
166bd021c4eSMatthias Ringwald                     h4_state = H4_W4_ACL_HEADER;
167bd021c4eSMatthias Ringwald                     break;
168bd021c4eSMatthias Ringwald                 case HCI_SCO_DATA_PACKET:
169bd021c4eSMatthias Ringwald                     bytes_to_read = HCI_SCO_HEADER_SIZE;
170bd021c4eSMatthias Ringwald                     h4_state = H4_W4_SCO_HEADER;
171bd021c4eSMatthias Ringwald                     break;
172307a4fe3SMatthias Ringwald #ifdef HAVE_EHCILL
173307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_IND:
174307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_ACK:
175307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
176307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_ACK:
177*8a23fc53SMatthias Ringwald                     hci_transport_h4_ehcill_handle_command(hci_packet[0]);
178*8a23fc53SMatthias Ringwald                     hci_transport_h4_reset_statemachine();
179307a4fe3SMatthias Ringwald                     break;
180307a4fe3SMatthias Ringwald #endif
181bd021c4eSMatthias Ringwald                 default:
182*8a23fc53SMatthias Ringwald                     log_error("hci_transport_h4: invalid packet type 0x%02x", hci_packet[0]);
183bd021c4eSMatthias Ringwald                     hci_transport_h4_reset_statemachine();
184bd021c4eSMatthias Ringwald                     break;
185bd021c4eSMatthias Ringwald             }
186bd021c4eSMatthias Ringwald             break;
187bd021c4eSMatthias Ringwald 
188bd021c4eSMatthias Ringwald         case H4_W4_EVENT_HEADER:
189bd021c4eSMatthias Ringwald             bytes_to_read = hci_packet[2];
190bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
191bd021c4eSMatthias Ringwald             break;
192bd021c4eSMatthias Ringwald 
193bd021c4eSMatthias Ringwald         case H4_W4_ACL_HEADER:
194bd021c4eSMatthias Ringwald             bytes_to_read = little_endian_read_16( hci_packet, 3);
195bd021c4eSMatthias Ringwald             // check ACL length
196bd021c4eSMatthias Ringwald             if (HCI_ACL_HEADER_SIZE + bytes_to_read >  HCI_PACKET_BUFFER_SIZE){
197*8a23fc53SMatthias Ringwald                 log_error("hci_transport_h4: invalid ACL payload len %u - only space for %u", bytes_to_read, HCI_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE);
198bd021c4eSMatthias Ringwald                 hci_transport_h4_reset_statemachine();
199bd021c4eSMatthias Ringwald                 break;
200bd021c4eSMatthias Ringwald             }
201bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
202bd021c4eSMatthias Ringwald             break;
203bd021c4eSMatthias Ringwald 
204bd021c4eSMatthias Ringwald         case H4_W4_SCO_HEADER:
205bd021c4eSMatthias Ringwald             bytes_to_read = hci_packet[3];
206bd021c4eSMatthias Ringwald             h4_state = H4_W4_PAYLOAD;
207bd021c4eSMatthias Ringwald             break;
208bd021c4eSMatthias Ringwald 
209bd021c4eSMatthias Ringwald         case H4_W4_PAYLOAD:
210bd021c4eSMatthias Ringwald             packet_handler(hci_packet[0], &hci_packet[1], read_pos-1);
211bd021c4eSMatthias Ringwald             hci_transport_h4_reset_statemachine();
212bd021c4eSMatthias Ringwald             break;
213bd021c4eSMatthias Ringwald         default:
214bd021c4eSMatthias Ringwald             break;
215bd021c4eSMatthias Ringwald     }
216bd021c4eSMatthias Ringwald     hci_transport_h4_trigger_next_read();
217bd021c4eSMatthias Ringwald }
218bd021c4eSMatthias Ringwald 
219307a4fe3SMatthias Ringwald static void hci_transport_h4_block_sent(void){
220307a4fe3SMatthias Ringwald     switch (tx_state){
221307a4fe3SMatthias Ringwald         case TX_W4_PACKET_SENT:
222307a4fe3SMatthias Ringwald             // packet fully sent, reset state
223307a4fe3SMatthias Ringwald             tx_len = 0;
224*8a23fc53SMatthias Ringwald             tx_state = TX_IDLE;
225307a4fe3SMatthias Ringwald 
226307a4fe3SMatthias Ringwald #ifdef HAVE_EHCILL
227*8a23fc53SMatthias Ringwald             // notify eHCILL engine
228*8a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_handle_packet_sent();
229307a4fe3SMatthias Ringwald #endif
230307a4fe3SMatthias Ringwald             // notify upper stack that it can send again
231307a4fe3SMatthias Ringwald             packet_handler(HCI_EVENT_PACKET, &packet_sent_event[0], sizeof(packet_sent_event));
232307a4fe3SMatthias Ringwald             break;
233307a4fe3SMatthias Ringwald 
234307a4fe3SMatthias Ringwald #ifdef HAVE_EHCILL
235*8a23fc53SMatthias Ringwald         case TX_W4_EHCILL_SENT:
236*8a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_handle_ehcill_command_sent();
237307a4fe3SMatthias Ringwald             break;
238307a4fe3SMatthias Ringwald #endif
239307a4fe3SMatthias Ringwald 
240307a4fe3SMatthias Ringwald         default:
241307a4fe3SMatthias Ringwald             break;
242307a4fe3SMatthias Ringwald     }
243307a4fe3SMatthias Ringwald }
244307a4fe3SMatthias Ringwald 
245307a4fe3SMatthias Ringwald static int hci_transport_h4_can_send_now(uint8_t packet_type){
246307a4fe3SMatthias Ringwald     return tx_state == TX_IDLE;
247307a4fe3SMatthias Ringwald }
248307a4fe3SMatthias Ringwald 
249307a4fe3SMatthias Ringwald static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){
250307a4fe3SMatthias Ringwald     // store packet type before actual data and increase size
251307a4fe3SMatthias Ringwald     size++;
252307a4fe3SMatthias Ringwald     packet--;
253307a4fe3SMatthias Ringwald     *packet = packet_type;
254307a4fe3SMatthias Ringwald 
255*8a23fc53SMatthias Ringwald     // store request
256*8a23fc53SMatthias Ringwald     tx_len   = size;
257*8a23fc53SMatthias Ringwald     tx_data  = packet;
258307a4fe3SMatthias Ringwald 
259*8a23fc53SMatthias Ringwald #ifdef HAVE_EHCILL
260*8a23fc53SMatthias Ringwald     if (hci_transport_h4_ehcill_sleep_mode_active()){
261*8a23fc53SMatthias Ringwald         hci_transport_h4_ehcill_trigger_wakeup();
262*8a23fc53SMatthias Ringwald         return 0;
263*8a23fc53SMatthias Ringwald     }
264*8a23fc53SMatthias Ringwald #endif
265*8a23fc53SMatthias Ringwald 
266*8a23fc53SMatthias Ringwald     // start sending
267*8a23fc53SMatthias Ringwald     tx_state = TX_W4_PACKET_SENT;
268307a4fe3SMatthias Ringwald     btstack_uart->send_block(packet, size);
269307a4fe3SMatthias Ringwald     return 0;
270307a4fe3SMatthias Ringwald }
271307a4fe3SMatthias Ringwald 
272bd021c4eSMatthias Ringwald static void hci_transport_h4_init(const void * transport_config){
273bd021c4eSMatthias Ringwald     // check for hci_transport_config_uart_t
274bd021c4eSMatthias Ringwald     if (!transport_config) {
275bd021c4eSMatthias Ringwald         log_error("hci_transport_h4: no config!");
276bd021c4eSMatthias Ringwald         return;
277bd021c4eSMatthias Ringwald     }
278bd021c4eSMatthias Ringwald     if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) {
279bd021c4eSMatthias Ringwald         log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!");
280bd021c4eSMatthias Ringwald         return;
281bd021c4eSMatthias Ringwald     }
282bd021c4eSMatthias Ringwald 
283bd021c4eSMatthias Ringwald     // extract UART config from transport config
284bd021c4eSMatthias Ringwald     hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config;
285bd021c4eSMatthias Ringwald     uart_config.baudrate    = hci_transport_config_uart->baudrate_init;
286bd021c4eSMatthias Ringwald     uart_config.flowcontrol = hci_transport_config_uart->flowcontrol;
287bd021c4eSMatthias Ringwald     uart_config.device_name = hci_transport_config_uart->device_name;
288bd021c4eSMatthias Ringwald 
289bd021c4eSMatthias Ringwald     // setup UART driver
290bd021c4eSMatthias Ringwald     btstack_uart->init(&uart_config);
291bd021c4eSMatthias Ringwald     btstack_uart->set_block_received(&hci_transport_h4_block_read);
292bd021c4eSMatthias Ringwald     btstack_uart->set_block_sent(&hci_transport_h4_block_sent);
293bd021c4eSMatthias Ringwald }
294bd021c4eSMatthias Ringwald 
295bd021c4eSMatthias Ringwald static int hci_transport_h4_open(void){
296bd021c4eSMatthias Ringwald     int res = btstack_uart->open();
297bd021c4eSMatthias Ringwald     if (res){
298bd021c4eSMatthias Ringwald         return res;
299bd021c4eSMatthias Ringwald     }
300bd021c4eSMatthias Ringwald     hci_transport_h4_reset_statemachine();
301bd021c4eSMatthias Ringwald     hci_transport_h4_trigger_next_read();
302307a4fe3SMatthias Ringwald 
303307a4fe3SMatthias Ringwald     tx_state = TX_IDLE;
304307a4fe3SMatthias Ringwald 
305307a4fe3SMatthias Ringwald #ifdef HAVE_EHCILL
306*8a23fc53SMatthias Ringwald     hci_transport_h4_ehcill_open();
307307a4fe3SMatthias Ringwald #endif
308bd021c4eSMatthias Ringwald     return 0;
309bd021c4eSMatthias Ringwald }
310bd021c4eSMatthias Ringwald 
311bd021c4eSMatthias Ringwald static int hci_transport_h4_close(void){
312bd021c4eSMatthias Ringwald     return btstack_uart->close();
313bd021c4eSMatthias Ringwald }
314bd021c4eSMatthias Ringwald 
315bd021c4eSMatthias Ringwald static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
316bd021c4eSMatthias Ringwald     packet_handler = handler;
317bd021c4eSMatthias Ringwald }
318bd021c4eSMatthias Ringwald 
319bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
320bd021c4eSMatthias Ringwald }
321bd021c4eSMatthias Ringwald 
322*8a23fc53SMatthias Ringwald //
323307a4fe3SMatthias Ringwald // --- main part of eHCILL implementation ---
324*8a23fc53SMatthias Ringwald //
325307a4fe3SMatthias Ringwald 
326307a4fe3SMatthias Ringwald #ifdef HAVE_EHCILL
327307a4fe3SMatthias Ringwald 
328*8a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void){
329*8a23fc53SMatthias Ringwald     hci_transport_h4_ehcill_reset_statemachine();
330*8a23fc53SMatthias Ringwald 
331*8a23fc53SMatthias Ringwald     // find best sleep mode to use: wake on CTS, wake on RX, none
332*8a23fc53SMatthias Ringwald     btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF;
333*8a23fc53SMatthias Ringwald     int supported_sleep_modes = 0;
334*8a23fc53SMatthias Ringwald     if (btstack_uart->get_supported_sleep_modes){
335*8a23fc53SMatthias Ringwald         supported_sleep_modes = btstack_uart->get_supported_sleep_modes();
336307a4fe3SMatthias Ringwald     }
337*8a23fc53SMatthias Ringwald     if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){
338*8a23fc53SMatthias Ringwald         log_info("eHCILL: using wake on CTS");
339*8a23fc53SMatthias Ringwald         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE;
340*8a23fc53SMatthias Ringwald     } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){
341*8a23fc53SMatthias Ringwald         log_info("eHCILL: using wake on RX");
342*8a23fc53SMatthias Ringwald         btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE;
343*8a23fc53SMatthias Ringwald     } else {
344*8a23fc53SMatthias Ringwald         log_info("eHCILL: UART driver does not provide compatible sleep mode");
345*8a23fc53SMatthias Ringwald     }
346307a4fe3SMatthias Ringwald }
347307a4fe3SMatthias Ringwald 
348307a4fe3SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void){
349307a4fe3SMatthias Ringwald     // update state
350307a4fe3SMatthias Ringwald     tx_state     = TX_W4_WAKEUP;
351307a4fe3SMatthias Ringwald     ehcill_state = EHCILL_STATE_W4_ACK;
352307a4fe3SMatthias Ringwald     ehcill_command_to_send = EHCILL_WAKE_UP_IND;
353307a4fe3SMatthias Ringwald     btstack_uart->send_block(&ehcill_command_to_send, 1);
354307a4fe3SMatthias Ringwald }
355307a4fe3SMatthias Ringwald 
356307a4fe3SMatthias Ringwald static int hci_transport_h4_ehcill_outgoing_packet_ready(void){
357307a4fe3SMatthias Ringwald     return tx_len != 0;
358307a4fe3SMatthias Ringwald }
359307a4fe3SMatthias Ringwald 
360*8a23fc53SMatthias Ringwald static int  hci_transport_h4_ehcill_sleep_mode_active(void){
361*8a23fc53SMatthias Ringwald     return ehcill_state == EHCILL_STATE_SLEEP;
362*8a23fc53SMatthias Ringwald }
363307a4fe3SMatthias Ringwald 
364307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void){
365307a4fe3SMatthias Ringwald     ehcill_state = EHCILL_STATE_AWAKE;
366307a4fe3SMatthias Ringwald }
367307a4fe3SMatthias Ringwald 
368307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void){
369*8a23fc53SMatthias Ringwald     log_debug("eHCILL: send command %02x", ehcill_command_to_send);
370307a4fe3SMatthias Ringwald     tx_state = TX_W4_EHCILL_SENT;
371307a4fe3SMatthias Ringwald     btstack_uart->send_block(&ehcill_command_to_send, 1);
372307a4fe3SMatthias Ringwald }
373307a4fe3SMatthias Ringwald 
374307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){
375*8a23fc53SMatthias Ringwald     log_debug("eHCILL: timer triggered");
376307a4fe3SMatthias Ringwald     hci_transport_h4_ehcill_send_ehcill_command();
377307a4fe3SMatthias Ringwald }
378307a4fe3SMatthias Ringwald 
379307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){
380307a4fe3SMatthias Ringwald     // setup timer
381*8a23fc53SMatthias Ringwald     log_debug("eHCILL: set timer for sending command");
382*8a23fc53SMatthias Ringwald     btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler);
383307a4fe3SMatthias Ringwald     btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50);
384307a4fe3SMatthias Ringwald     btstack_run_loop_add_timer(&ehcill_sleep_ack_timer);
385*8a23fc53SMatthias Ringwald 
386307a4fe3SMatthias Ringwald     // TODO: trigger run loop
387307a4fe3SMatthias Ringwald     // btstack_run_loop_embedded_trigger();
388307a4fe3SMatthias Ringwald }
389307a4fe3SMatthias Ringwald 
390*8a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void){
391*8a23fc53SMatthias Ringwald     switch (tx_state){
392*8a23fc53SMatthias Ringwald         case TX_W2_EHCILL_SEND:
393*8a23fc53SMatthias Ringwald         case TX_W4_EHCILL_SENT:
394*8a23fc53SMatthias Ringwald             // wake up / sleep ack in progress, nothing to do now
395*8a23fc53SMatthias Ringwald             return;
396*8a23fc53SMatthias Ringwald         case TX_IDLE:
397*8a23fc53SMatthias Ringwald         default:
398*8a23fc53SMatthias Ringwald             // all clear, prepare for wakeup
399*8a23fc53SMatthias Ringwald             break;
400*8a23fc53SMatthias Ringwald     }
401*8a23fc53SMatthias Ringwald     // UART needed again
402*8a23fc53SMatthias Ringwald     if (btstack_uart_sleep_mode){
403*8a23fc53SMatthias Ringwald         btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
404*8a23fc53SMatthias Ringwald     }
405*8a23fc53SMatthias Ringwald     hci_transport_h4_echill_send_wakeup_ind();
406*8a23fc53SMatthias Ringwald }
407*8a23fc53SMatthias Ringwald 
408307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_schedule_ecill_command(uint8_t command){
409307a4fe3SMatthias Ringwald     ehcill_command_to_send = command;
410307a4fe3SMatthias Ringwald     switch (tx_state){
411307a4fe3SMatthias Ringwald         case TX_IDLE:
412307a4fe3SMatthias Ringwald             if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){
413307a4fe3SMatthias Ringwald                 // send right away
414307a4fe3SMatthias Ringwald                 hci_transport_h4_ehcill_send_ehcill_command();
415307a4fe3SMatthias Ringwald             } else {
416307a4fe3SMatthias Ringwald                 // change state so BTstack cannot send and setup timer
417307a4fe3SMatthias Ringwald                 tx_state = TX_W2_EHCILL_SEND;
418307a4fe3SMatthias Ringwald                 hci_transport_h4_ehcill_sleep_ack_timer_setup();
419307a4fe3SMatthias Ringwald             }
420307a4fe3SMatthias Ringwald             break;
421307a4fe3SMatthias Ringwald         default:
422307a4fe3SMatthias Ringwald             break;
423307a4fe3SMatthias Ringwald     }
424307a4fe3SMatthias Ringwald }
425307a4fe3SMatthias Ringwald 
426*8a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action){
427307a4fe3SMatthias Ringwald     // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size);
428307a4fe3SMatthias Ringwald     switch(ehcill_state){
429307a4fe3SMatthias Ringwald         case EHCILL_STATE_AWAKE:
430307a4fe3SMatthias Ringwald             switch(action){
431307a4fe3SMatthias Ringwald                 case EHCILL_GO_TO_SLEEP_IND:
432307a4fe3SMatthias Ringwald                     ehcill_state = EHCILL_STATE_SLEEP;
433*8a23fc53SMatthias Ringwald                     log_info("eHCILL: GO_TO_SLEEP_IND RX");
434307a4fe3SMatthias Ringwald                     hci_transport_h4_ehcill_schedule_ecill_command(EHCILL_GO_TO_SLEEP_ACK);
435307a4fe3SMatthias Ringwald                     break;
436307a4fe3SMatthias Ringwald                 default:
437307a4fe3SMatthias Ringwald                     break;
438307a4fe3SMatthias Ringwald             }
439307a4fe3SMatthias Ringwald             break;
440307a4fe3SMatthias Ringwald 
441307a4fe3SMatthias Ringwald         case EHCILL_STATE_SLEEP:
442307a4fe3SMatthias Ringwald             switch(action){
443307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
444307a4fe3SMatthias Ringwald                     ehcill_state = EHCILL_STATE_AWAKE;
445*8a23fc53SMatthias Ringwald                     log_info("eHCILL: WAKE_UP_IND RX");
446307a4fe3SMatthias Ringwald                     hci_transport_h4_ehcill_schedule_ecill_command(EHCILL_WAKE_UP_ACK);
447307a4fe3SMatthias Ringwald                     break;
448307a4fe3SMatthias Ringwald 
449307a4fe3SMatthias Ringwald                 default:
450307a4fe3SMatthias Ringwald                     break;
451307a4fe3SMatthias Ringwald             }
452307a4fe3SMatthias Ringwald             break;
453307a4fe3SMatthias Ringwald 
454307a4fe3SMatthias Ringwald         case EHCILL_STATE_W4_ACK:
455307a4fe3SMatthias Ringwald             switch(action){
456307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_IND:
457307a4fe3SMatthias Ringwald                 case EHCILL_WAKE_UP_ACK:
458*8a23fc53SMatthias Ringwald                     log_info("eHCILL: WAKE_UP_IND or ACK");
459307a4fe3SMatthias Ringwald                     tx_state = TX_W4_PACKET_SENT;
460307a4fe3SMatthias Ringwald                     ehcill_state = EHCILL_STATE_AWAKE;
461307a4fe3SMatthias Ringwald                     btstack_uart->send_block(tx_data, tx_len);
462307a4fe3SMatthias Ringwald                     break;
463307a4fe3SMatthias Ringwald                 default:
464307a4fe3SMatthias Ringwald                     break;
465307a4fe3SMatthias Ringwald             }
466307a4fe3SMatthias Ringwald             break;
467307a4fe3SMatthias Ringwald     }
468307a4fe3SMatthias Ringwald }
469*8a23fc53SMatthias Ringwald 
470*8a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void){
471*8a23fc53SMatthias Ringwald     // now, send pending ehcill command if neccessary
472*8a23fc53SMatthias Ringwald     switch (ehcill_command_to_send){
473*8a23fc53SMatthias Ringwald         case EHCILL_GO_TO_SLEEP_ACK:
474*8a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_sleep_ack_timer_setup();
475*8a23fc53SMatthias Ringwald             break;
476*8a23fc53SMatthias Ringwald         case EHCILL_WAKE_UP_IND:
477*8a23fc53SMatthias Ringwald             hci_transport_h4_ehcill_send_ehcill_command();
478*8a23fc53SMatthias Ringwald             break;
479*8a23fc53SMatthias Ringwald         default:
480*8a23fc53SMatthias Ringwald             break;
481*8a23fc53SMatthias Ringwald     }
482*8a23fc53SMatthias Ringwald }
483*8a23fc53SMatthias Ringwald 
484*8a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){
485*8a23fc53SMatthias Ringwald     tx_state = TX_IDLE;
486*8a23fc53SMatthias Ringwald     int command = ehcill_command_to_send;
487*8a23fc53SMatthias Ringwald     ehcill_command_to_send = 0;
488*8a23fc53SMatthias Ringwald     if (command == EHCILL_GO_TO_SLEEP_ACK) {
489*8a23fc53SMatthias Ringwald         log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode");
490*8a23fc53SMatthias Ringwald         // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent
491*8a23fc53SMatthias Ringwald         if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){
492*8a23fc53SMatthias Ringwald             btstack_uart->set_sleep(btstack_uart_sleep_mode);
493*8a23fc53SMatthias Ringwald         }
494*8a23fc53SMatthias Ringwald     }
495*8a23fc53SMatthias Ringwald     // already packet ready? then start wakeup
496*8a23fc53SMatthias Ringwald     if (hci_transport_h4_ehcill_outgoing_packet_ready()){
497*8a23fc53SMatthias Ringwald         if (btstack_uart_sleep_mode){
498*8a23fc53SMatthias Ringwald             btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);
499*8a23fc53SMatthias Ringwald         }
500*8a23fc53SMatthias Ringwald         hci_transport_h4_echill_send_wakeup_ind();
501*8a23fc53SMatthias Ringwald     }
502*8a23fc53SMatthias Ringwald     // TODO: trigger run loop
503*8a23fc53SMatthias Ringwald     // btstack_run_loop_embedded_trigger();
504*8a23fc53SMatthias Ringwald }
505*8a23fc53SMatthias Ringwald 
506307a4fe3SMatthias Ringwald #endif
507307a4fe3SMatthias Ringwald // --- end of eHCILL implementation ---------
508307a4fe3SMatthias Ringwald 
509bd021c4eSMatthias Ringwald static const hci_transport_t hci_transport_h4 = {
510bd021c4eSMatthias Ringwald     /* const char * name; */                                        "H4",
511bd021c4eSMatthias Ringwald     /* void   (*init) (const void *transport_config); */            &hci_transport_h4_init,
512bd021c4eSMatthias Ringwald     /* int    (*open)(void); */                                     &hci_transport_h4_open,
513bd021c4eSMatthias Ringwald     /* int    (*close)(void); */                                    &hci_transport_h4_close,
514bd021c4eSMatthias Ringwald     /* void   (*register_packet_handler)(void (*handler)(...); */   &hci_transport_h4_register_packet_handler,
515bd021c4eSMatthias Ringwald     /* int    (*can_send_packet_now)(uint8_t packet_type); */       &hci_transport_h4_can_send_now,
516bd021c4eSMatthias Ringwald     /* int    (*send_packet)(...); */                               &hci_transport_h4_send_packet,
517bd021c4eSMatthias Ringwald     /* int    (*set_baudrate)(uint32_t baudrate); */                &hci_transport_h4_set_baudrate,
518bd021c4eSMatthias Ringwald     /* void   (*reset_link)(void); */                               NULL,
519bd021c4eSMatthias Ringwald };
520bd021c4eSMatthias Ringwald 
521bd021c4eSMatthias Ringwald // configure and return h4 singleton
522bd021c4eSMatthias Ringwald const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) {
523bd021c4eSMatthias Ringwald     btstack_uart = uart_driver;
524bd021c4eSMatthias Ringwald     return &hci_transport_h4;
525bd021c4eSMatthias Ringwald }
526