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 38*ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hci_transport_h4.c" 39*ab2c6ae4SMatthias 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 48bd021c4eSMatthias Ringwald #include "btstack_config.h" 49bd021c4eSMatthias Ringwald 50bd021c4eSMatthias Ringwald #include "btstack_debug.h" 51bd021c4eSMatthias Ringwald #include "hci.h" 52bd021c4eSMatthias Ringwald #include "hci_transport.h" 53bd021c4eSMatthias Ringwald #include "btstack_uart_block.h" 54bd021c4eSMatthias Ringwald 559ecd1ba8SMatthias Ringwald #define ENABLE_LOG_EHCILL 569ecd1ba8SMatthias Ringwald 57f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 58307a4fe3SMatthias Ringwald 59307a4fe3SMatthias Ringwald // eHCILL commands 609ecd1ba8SMatthias Ringwald static const uint8_t EHCILL_GO_TO_SLEEP_IND = 0x030; 619ecd1ba8SMatthias Ringwald static const uint8_t EHCILL_GO_TO_SLEEP_ACK = 0x031; 629ecd1ba8SMatthias Ringwald static const uint8_t EHCILL_WAKE_UP_IND = 0x032; 639ecd1ba8SMatthias Ringwald static const uint8_t EHCILL_WAKE_UP_ACK = 0x033; 64307a4fe3SMatthias Ringwald 658a23fc53SMatthias Ringwald static int hci_transport_h4_ehcill_outgoing_packet_ready(void); 668a23fc53SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void); 678a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action); 688a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void); 698a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void); 708a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void); 71307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void); 72307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void); 73307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void); 748a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void); 75307a4fe3SMatthias Ringwald 76307a4fe3SMatthias Ringwald typedef enum { 779ecd1ba8SMatthias Ringwald EHCILL_STATE_W2_SEND_SLEEP_ACK, 78307a4fe3SMatthias Ringwald EHCILL_STATE_SLEEP, 799ecd1ba8SMatthias Ringwald EHCILL_STATE_W4_WAKEUP_IND_OR_ACK, 80307a4fe3SMatthias Ringwald EHCILL_STATE_AWAKE 81307a4fe3SMatthias Ringwald } EHCILL_STATE; 82307a4fe3SMatthias Ringwald 83307a4fe3SMatthias Ringwald // eHCILL state machine 84307a4fe3SMatthias Ringwald static EHCILL_STATE ehcill_state; 85307a4fe3SMatthias Ringwald static uint8_t ehcill_command_to_send; 86307a4fe3SMatthias Ringwald 87f6a20ec9SMatthias Ringwald static btstack_uart_sleep_mode_t btstack_uart_sleep_mode; 88f6a20ec9SMatthias Ringwald 89307a4fe3SMatthias Ringwald // work around for eHCILL problem 90307a4fe3SMatthias Ringwald static btstack_timer_source_t ehcill_sleep_ack_timer; 91307a4fe3SMatthias Ringwald 92bd021c4eSMatthias Ringwald #endif 93bd021c4eSMatthias Ringwald 9482d05f16SMatthias Ringwald 95bd021c4eSMatthias Ringwald // assert pre-buffer for packet type is available 96bd021c4eSMatthias Ringwald #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0) 97bd021c4eSMatthias Ringwald #error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h 98bd021c4eSMatthias Ringwald #endif 99bd021c4eSMatthias Ringwald 100bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 101bd021c4eSMatthias Ringwald 102bd021c4eSMatthias Ringwald typedef enum { 103bd021c4eSMatthias Ringwald H4_W4_PACKET_TYPE, 104bd021c4eSMatthias Ringwald H4_W4_EVENT_HEADER, 105bd021c4eSMatthias Ringwald H4_W4_ACL_HEADER, 106bd021c4eSMatthias Ringwald H4_W4_SCO_HEADER, 107bd021c4eSMatthias Ringwald H4_W4_PAYLOAD, 108bd021c4eSMatthias Ringwald } H4_STATE; 109bd021c4eSMatthias Ringwald 110307a4fe3SMatthias Ringwald typedef enum { 111307a4fe3SMatthias Ringwald TX_IDLE = 1, 112307a4fe3SMatthias Ringwald TX_W4_PACKET_SENT, 113f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 114307a4fe3SMatthias Ringwald TX_W4_WAKEUP, 115307a4fe3SMatthias Ringwald TX_W2_EHCILL_SEND, 116307a4fe3SMatthias Ringwald TX_W4_EHCILL_SENT, 117307a4fe3SMatthias Ringwald #endif 118307a4fe3SMatthias Ringwald } TX_STATE; 119307a4fe3SMatthias Ringwald 120bd021c4eSMatthias Ringwald // UART Driver + Config 121bd021c4eSMatthias Ringwald static const btstack_uart_block_t * btstack_uart; 122bd021c4eSMatthias Ringwald static btstack_uart_config_t uart_config; 123bd021c4eSMatthias Ringwald 124307a4fe3SMatthias Ringwald // write state 1258a23fc53SMatthias Ringwald static TX_STATE tx_state; 1268a23fc53SMatthias Ringwald static uint8_t * tx_data; 127307a4fe3SMatthias Ringwald static uint16_t tx_len; // 0 == no outgoing packet 1288a23fc53SMatthias Ringwald 129307a4fe3SMatthias Ringwald static uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 130bd021c4eSMatthias Ringwald 131bd021c4eSMatthias Ringwald static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler; 132bd021c4eSMatthias Ringwald 133bd021c4eSMatthias Ringwald // packet reader state machine 134bd021c4eSMatthias Ringwald static H4_STATE h4_state; 135bd021c4eSMatthias Ringwald static int bytes_to_read; 136bd021c4eSMatthias Ringwald static int read_pos; 137bd021c4eSMatthias Ringwald 138bd021c4eSMatthias Ringwald // incoming packet buffer 139bd021c4eSMatthias 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) 140bd021c4eSMatthias Ringwald static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE]; 141bd021c4eSMatthias Ringwald 142bd021c4eSMatthias Ringwald static int hci_transport_h4_set_baudrate(uint32_t baudrate){ 143bd021c4eSMatthias Ringwald log_info("hci_transport_h4_set_baudrate %u", baudrate); 144bd021c4eSMatthias Ringwald return btstack_uart->set_baudrate(baudrate); 145bd021c4eSMatthias Ringwald } 146bd021c4eSMatthias Ringwald 147bd021c4eSMatthias Ringwald static void hci_transport_h4_reset_statemachine(void){ 148bd021c4eSMatthias Ringwald h4_state = H4_W4_PACKET_TYPE; 149bd021c4eSMatthias Ringwald read_pos = 0; 150bd021c4eSMatthias Ringwald bytes_to_read = 1; 151bd021c4eSMatthias Ringwald } 152bd021c4eSMatthias Ringwald 153bd021c4eSMatthias Ringwald static void hci_transport_h4_trigger_next_read(void){ 1548a23fc53SMatthias Ringwald // log_info("hci_transport_h4_trigger_next_read: %u bytes", bytes_to_read); 155bd021c4eSMatthias Ringwald btstack_uart->receive_block(&hci_packet[read_pos], bytes_to_read); 156bd021c4eSMatthias Ringwald } 157bd021c4eSMatthias Ringwald 158bd021c4eSMatthias Ringwald static void hci_transport_h4_block_read(void){ 159bd021c4eSMatthias Ringwald 160bd021c4eSMatthias Ringwald read_pos += bytes_to_read; 161bd021c4eSMatthias Ringwald 162bd021c4eSMatthias Ringwald switch (h4_state) { 163bd021c4eSMatthias Ringwald case H4_W4_PACKET_TYPE: 164bd021c4eSMatthias Ringwald switch (hci_packet[0]){ 165bd021c4eSMatthias Ringwald case HCI_EVENT_PACKET: 166bd021c4eSMatthias Ringwald bytes_to_read = HCI_EVENT_HEADER_SIZE; 167bd021c4eSMatthias Ringwald h4_state = H4_W4_EVENT_HEADER; 168bd021c4eSMatthias Ringwald break; 169bd021c4eSMatthias Ringwald case HCI_ACL_DATA_PACKET: 170bd021c4eSMatthias Ringwald bytes_to_read = HCI_ACL_HEADER_SIZE; 171bd021c4eSMatthias Ringwald h4_state = H4_W4_ACL_HEADER; 172bd021c4eSMatthias Ringwald break; 173bd021c4eSMatthias Ringwald case HCI_SCO_DATA_PACKET: 174bd021c4eSMatthias Ringwald bytes_to_read = HCI_SCO_HEADER_SIZE; 175bd021c4eSMatthias Ringwald h4_state = H4_W4_SCO_HEADER; 176bd021c4eSMatthias Ringwald break; 177f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 178307a4fe3SMatthias Ringwald case EHCILL_GO_TO_SLEEP_IND: 179307a4fe3SMatthias Ringwald case EHCILL_GO_TO_SLEEP_ACK: 180307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_IND: 181307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_ACK: 1828a23fc53SMatthias Ringwald hci_transport_h4_ehcill_handle_command(hci_packet[0]); 1838a23fc53SMatthias Ringwald hci_transport_h4_reset_statemachine(); 184307a4fe3SMatthias Ringwald break; 185307a4fe3SMatthias Ringwald #endif 186bd021c4eSMatthias Ringwald default: 1878a23fc53SMatthias Ringwald log_error("hci_transport_h4: invalid packet type 0x%02x", hci_packet[0]); 188bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 189bd021c4eSMatthias Ringwald break; 190bd021c4eSMatthias Ringwald } 191bd021c4eSMatthias Ringwald break; 192bd021c4eSMatthias Ringwald 193bd021c4eSMatthias Ringwald case H4_W4_EVENT_HEADER: 194bd021c4eSMatthias Ringwald bytes_to_read = hci_packet[2]; 195bd021c4eSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 196bd021c4eSMatthias Ringwald break; 197bd021c4eSMatthias Ringwald 198bd021c4eSMatthias Ringwald case H4_W4_ACL_HEADER: 199bd021c4eSMatthias Ringwald bytes_to_read = little_endian_read_16( hci_packet, 3); 200bd021c4eSMatthias Ringwald // check ACL length 201bd021c4eSMatthias Ringwald if (HCI_ACL_HEADER_SIZE + bytes_to_read > HCI_PACKET_BUFFER_SIZE){ 202f04a0c31SMatthias Ringwald log_error("hci_transport_h4: invalid ACL payload len %d - only space for %u", bytes_to_read, HCI_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE); 203bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 204bd021c4eSMatthias Ringwald break; 205bd021c4eSMatthias Ringwald } 206bd021c4eSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 207bd021c4eSMatthias Ringwald break; 208bd021c4eSMatthias Ringwald 209bd021c4eSMatthias Ringwald case H4_W4_SCO_HEADER: 210bd021c4eSMatthias Ringwald bytes_to_read = hci_packet[3]; 211bd021c4eSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 212bd021c4eSMatthias Ringwald break; 213bd021c4eSMatthias Ringwald 214bd021c4eSMatthias Ringwald case H4_W4_PAYLOAD: 215bd021c4eSMatthias Ringwald packet_handler(hci_packet[0], &hci_packet[1], read_pos-1); 216bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 217bd021c4eSMatthias Ringwald break; 218bd021c4eSMatthias Ringwald default: 219bd021c4eSMatthias Ringwald break; 220bd021c4eSMatthias Ringwald } 221bd021c4eSMatthias Ringwald hci_transport_h4_trigger_next_read(); 222bd021c4eSMatthias Ringwald } 223bd021c4eSMatthias Ringwald 224307a4fe3SMatthias Ringwald static void hci_transport_h4_block_sent(void){ 225307a4fe3SMatthias Ringwald switch (tx_state){ 226307a4fe3SMatthias Ringwald case TX_W4_PACKET_SENT: 227307a4fe3SMatthias Ringwald // packet fully sent, reset state 228307a4fe3SMatthias Ringwald tx_len = 0; 2298a23fc53SMatthias Ringwald tx_state = TX_IDLE; 230307a4fe3SMatthias Ringwald 231f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 2328a23fc53SMatthias Ringwald // notify eHCILL engine 2338a23fc53SMatthias Ringwald hci_transport_h4_ehcill_handle_packet_sent(); 234307a4fe3SMatthias Ringwald #endif 235307a4fe3SMatthias Ringwald // notify upper stack that it can send again 236307a4fe3SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, &packet_sent_event[0], sizeof(packet_sent_event)); 237307a4fe3SMatthias Ringwald break; 238307a4fe3SMatthias Ringwald 239f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 2408a23fc53SMatthias Ringwald case TX_W4_EHCILL_SENT: 2418a23fc53SMatthias Ringwald hci_transport_h4_ehcill_handle_ehcill_command_sent(); 242307a4fe3SMatthias Ringwald break; 243307a4fe3SMatthias Ringwald #endif 244307a4fe3SMatthias Ringwald 245307a4fe3SMatthias Ringwald default: 246307a4fe3SMatthias Ringwald break; 247307a4fe3SMatthias Ringwald } 248307a4fe3SMatthias Ringwald } 249307a4fe3SMatthias Ringwald 250307a4fe3SMatthias Ringwald static int hci_transport_h4_can_send_now(uint8_t packet_type){ 251307a4fe3SMatthias Ringwald return tx_state == TX_IDLE; 252307a4fe3SMatthias Ringwald } 253307a4fe3SMatthias Ringwald 254307a4fe3SMatthias Ringwald static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 255307a4fe3SMatthias Ringwald // store packet type before actual data and increase size 256307a4fe3SMatthias Ringwald size++; 257307a4fe3SMatthias Ringwald packet--; 258307a4fe3SMatthias Ringwald *packet = packet_type; 259307a4fe3SMatthias Ringwald 2608a23fc53SMatthias Ringwald // store request 2618a23fc53SMatthias Ringwald tx_len = size; 2628a23fc53SMatthias Ringwald tx_data = packet; 263307a4fe3SMatthias Ringwald 264f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 2659ecd1ba8SMatthias Ringwald switch (ehcill_state){ 2669ecd1ba8SMatthias Ringwald case EHCILL_STATE_SLEEP: 2678a23fc53SMatthias Ringwald hci_transport_h4_ehcill_trigger_wakeup(); 2688a23fc53SMatthias Ringwald return 0; 2699ecd1ba8SMatthias Ringwald case EHCILL_STATE_W2_SEND_SLEEP_ACK: 2709ecd1ba8SMatthias Ringwald return 0; 2719ecd1ba8SMatthias Ringwald default: 2729ecd1ba8SMatthias Ringwald break; 2738a23fc53SMatthias Ringwald } 2748a23fc53SMatthias Ringwald #endif 2758a23fc53SMatthias Ringwald 2768a23fc53SMatthias Ringwald // start sending 2778a23fc53SMatthias Ringwald tx_state = TX_W4_PACKET_SENT; 278307a4fe3SMatthias Ringwald btstack_uart->send_block(packet, size); 279307a4fe3SMatthias Ringwald return 0; 280307a4fe3SMatthias Ringwald } 281307a4fe3SMatthias Ringwald 282bd021c4eSMatthias Ringwald static void hci_transport_h4_init(const void * transport_config){ 283bd021c4eSMatthias Ringwald // check for hci_transport_config_uart_t 284bd021c4eSMatthias Ringwald if (!transport_config) { 285bd021c4eSMatthias Ringwald log_error("hci_transport_h4: no config!"); 286bd021c4eSMatthias Ringwald return; 287bd021c4eSMatthias Ringwald } 288bd021c4eSMatthias Ringwald if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) { 289bd021c4eSMatthias Ringwald log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!"); 290bd021c4eSMatthias Ringwald return; 291bd021c4eSMatthias Ringwald } 292bd021c4eSMatthias Ringwald 293bd021c4eSMatthias Ringwald // extract UART config from transport config 294bd021c4eSMatthias Ringwald hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config; 295bd021c4eSMatthias Ringwald uart_config.baudrate = hci_transport_config_uart->baudrate_init; 296bd021c4eSMatthias Ringwald uart_config.flowcontrol = hci_transport_config_uart->flowcontrol; 297bd021c4eSMatthias Ringwald uart_config.device_name = hci_transport_config_uart->device_name; 298bd021c4eSMatthias Ringwald 299bd021c4eSMatthias Ringwald // setup UART driver 300bd021c4eSMatthias Ringwald btstack_uart->init(&uart_config); 301bd021c4eSMatthias Ringwald btstack_uart->set_block_received(&hci_transport_h4_block_read); 302bd021c4eSMatthias Ringwald btstack_uart->set_block_sent(&hci_transport_h4_block_sent); 303bd021c4eSMatthias Ringwald } 304bd021c4eSMatthias Ringwald 305bd021c4eSMatthias Ringwald static int hci_transport_h4_open(void){ 306bd021c4eSMatthias Ringwald int res = btstack_uart->open(); 307bd021c4eSMatthias Ringwald if (res){ 308bd021c4eSMatthias Ringwald return res; 309bd021c4eSMatthias Ringwald } 310bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 311bd021c4eSMatthias Ringwald hci_transport_h4_trigger_next_read(); 312307a4fe3SMatthias Ringwald 313307a4fe3SMatthias Ringwald tx_state = TX_IDLE; 314307a4fe3SMatthias Ringwald 315f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 3168a23fc53SMatthias Ringwald hci_transport_h4_ehcill_open(); 317307a4fe3SMatthias Ringwald #endif 318bd021c4eSMatthias Ringwald return 0; 319bd021c4eSMatthias Ringwald } 320bd021c4eSMatthias Ringwald 321bd021c4eSMatthias Ringwald static int hci_transport_h4_close(void){ 322bd021c4eSMatthias Ringwald return btstack_uart->close(); 323bd021c4eSMatthias Ringwald } 324bd021c4eSMatthias Ringwald 325bd021c4eSMatthias Ringwald static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 326bd021c4eSMatthias Ringwald packet_handler = handler; 327bd021c4eSMatthias Ringwald } 328bd021c4eSMatthias Ringwald 329bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 330bd021c4eSMatthias Ringwald } 331bd021c4eSMatthias Ringwald 3328a23fc53SMatthias Ringwald // 333307a4fe3SMatthias Ringwald // --- main part of eHCILL implementation --- 3348a23fc53SMatthias Ringwald // 335307a4fe3SMatthias Ringwald 336f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 337307a4fe3SMatthias Ringwald 33862ca45d7SMatthias Ringwald static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){ 33962ca45d7SMatthias Ringwald static int last_state = 0; 34062ca45d7SMatthias Ringwald if (sleep_active == last_state) return; 34162ca45d7SMatthias Ringwald last_state = sleep_active; 34262ca45d7SMatthias Ringwald 34362ca45d7SMatthias Ringwald log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active); 34462ca45d7SMatthias Ringwald uint8_t event[3]; 34562ca45d7SMatthias Ringwald event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE; 34662ca45d7SMatthias Ringwald event[1] = sizeof(event) - 2; 34762ca45d7SMatthias Ringwald event[2] = sleep_active; 34862ca45d7SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 34962ca45d7SMatthias Ringwald } 35062ca45d7SMatthias Ringwald 3518a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void){ 3528a23fc53SMatthias Ringwald hci_transport_h4_ehcill_reset_statemachine(); 3538a23fc53SMatthias Ringwald 3548a23fc53SMatthias Ringwald // find best sleep mode to use: wake on CTS, wake on RX, none 3558a23fc53SMatthias Ringwald btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF; 3568a23fc53SMatthias Ringwald int supported_sleep_modes = 0; 3578a23fc53SMatthias Ringwald if (btstack_uart->get_supported_sleep_modes){ 3588a23fc53SMatthias Ringwald supported_sleep_modes = btstack_uart->get_supported_sleep_modes(); 359307a4fe3SMatthias Ringwald } 3608a23fc53SMatthias Ringwald if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){ 3618a23fc53SMatthias Ringwald log_info("eHCILL: using wake on CTS"); 3628a23fc53SMatthias Ringwald btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE; 3638a23fc53SMatthias Ringwald } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){ 3648a23fc53SMatthias Ringwald log_info("eHCILL: using wake on RX"); 3658a23fc53SMatthias Ringwald btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE; 3668a23fc53SMatthias Ringwald } else { 3678a23fc53SMatthias Ringwald log_info("eHCILL: UART driver does not provide compatible sleep mode"); 3688a23fc53SMatthias Ringwald } 369307a4fe3SMatthias Ringwald } 370307a4fe3SMatthias Ringwald 371307a4fe3SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void){ 3729ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 3739ecd1ba8SMatthias Ringwald log_info("eHCILL: send WAKEUP_IND"); 3749ecd1ba8SMatthias Ringwald #endif 375307a4fe3SMatthias Ringwald // update state 376307a4fe3SMatthias Ringwald tx_state = TX_W4_WAKEUP; 3779ecd1ba8SMatthias Ringwald ehcill_state = EHCILL_STATE_W4_WAKEUP_IND_OR_ACK; 378307a4fe3SMatthias Ringwald ehcill_command_to_send = EHCILL_WAKE_UP_IND; 379307a4fe3SMatthias Ringwald btstack_uart->send_block(&ehcill_command_to_send, 1); 380307a4fe3SMatthias Ringwald } 381307a4fe3SMatthias Ringwald 382307a4fe3SMatthias Ringwald static int hci_transport_h4_ehcill_outgoing_packet_ready(void){ 383307a4fe3SMatthias Ringwald return tx_len != 0; 384307a4fe3SMatthias Ringwald } 385307a4fe3SMatthias Ringwald 386307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void){ 387307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 388307a4fe3SMatthias Ringwald } 389307a4fe3SMatthias Ringwald 390307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void){ 3919ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 3929ecd1ba8SMatthias Ringwald log_info("eHCILL: send command %02x", ehcill_command_to_send); 3939ecd1ba8SMatthias Ringwald #endif 394307a4fe3SMatthias Ringwald tx_state = TX_W4_EHCILL_SENT; 3959ecd1ba8SMatthias Ringwald if (ehcill_command_to_send == EHCILL_GO_TO_SLEEP_ACK){ 3969ecd1ba8SMatthias Ringwald ehcill_state = EHCILL_STATE_SLEEP; 3979ecd1ba8SMatthias Ringwald } 398307a4fe3SMatthias Ringwald btstack_uart->send_block(&ehcill_command_to_send, 1); 399307a4fe3SMatthias Ringwald } 400307a4fe3SMatthias Ringwald 401307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){ 4029ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 4039ecd1ba8SMatthias Ringwald log_info("eHCILL: timer triggered"); 4049ecd1ba8SMatthias Ringwald #endif 405307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_send_ehcill_command(); 406307a4fe3SMatthias Ringwald } 407307a4fe3SMatthias Ringwald 408307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){ 409307a4fe3SMatthias Ringwald // setup timer 4109ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 4119ecd1ba8SMatthias Ringwald log_info("eHCILL: set timer for sending command %02x", ehcill_command_to_send); 4129ecd1ba8SMatthias Ringwald #endif 4138a23fc53SMatthias Ringwald btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler); 414307a4fe3SMatthias Ringwald btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50); 415307a4fe3SMatthias Ringwald btstack_run_loop_add_timer(&ehcill_sleep_ack_timer); 416307a4fe3SMatthias Ringwald } 417307a4fe3SMatthias Ringwald 4188a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void){ 4198a23fc53SMatthias Ringwald switch (tx_state){ 4208a23fc53SMatthias Ringwald case TX_W2_EHCILL_SEND: 4218a23fc53SMatthias Ringwald case TX_W4_EHCILL_SENT: 4228a23fc53SMatthias Ringwald // wake up / sleep ack in progress, nothing to do now 4238a23fc53SMatthias Ringwald return; 4248a23fc53SMatthias Ringwald case TX_IDLE: 4258a23fc53SMatthias Ringwald default: 4268a23fc53SMatthias Ringwald // all clear, prepare for wakeup 4278a23fc53SMatthias Ringwald break; 4288a23fc53SMatthias Ringwald } 4298a23fc53SMatthias Ringwald // UART needed again 43062ca45d7SMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(0); 4318a23fc53SMatthias Ringwald if (btstack_uart_sleep_mode){ 4328a23fc53SMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 4338a23fc53SMatthias Ringwald } 4348a23fc53SMatthias Ringwald hci_transport_h4_echill_send_wakeup_ind(); 4358a23fc53SMatthias Ringwald } 4368a23fc53SMatthias Ringwald 4379ecd1ba8SMatthias Ringwald static void hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command){ 4389ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 4399ecd1ba8SMatthias Ringwald log_info("eHCILL: schedule eHCILL command %02x", command); 4409ecd1ba8SMatthias Ringwald #endif 441307a4fe3SMatthias Ringwald ehcill_command_to_send = command; 442307a4fe3SMatthias Ringwald switch (tx_state){ 443307a4fe3SMatthias Ringwald case TX_IDLE: 444307a4fe3SMatthias Ringwald if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){ 445307a4fe3SMatthias Ringwald // send right away 446307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_send_ehcill_command(); 447307a4fe3SMatthias Ringwald } else { 448307a4fe3SMatthias Ringwald // change state so BTstack cannot send and setup timer 449307a4fe3SMatthias Ringwald tx_state = TX_W2_EHCILL_SEND; 450307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_sleep_ack_timer_setup(); 451307a4fe3SMatthias Ringwald } 452307a4fe3SMatthias Ringwald break; 453307a4fe3SMatthias Ringwald default: 454307a4fe3SMatthias Ringwald break; 455307a4fe3SMatthias Ringwald } 456307a4fe3SMatthias Ringwald } 457307a4fe3SMatthias Ringwald 4588a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action){ 459307a4fe3SMatthias Ringwald // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size); 460307a4fe3SMatthias Ringwald switch(ehcill_state){ 461307a4fe3SMatthias Ringwald case EHCILL_STATE_AWAKE: 462307a4fe3SMatthias Ringwald switch(action){ 463307a4fe3SMatthias Ringwald case EHCILL_GO_TO_SLEEP_IND: 4649ecd1ba8SMatthias Ringwald ehcill_state = EHCILL_STATE_W2_SEND_SLEEP_ACK; 4659ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 4669ecd1ba8SMatthias Ringwald log_info("eHCILL: Received GO_TO_SLEEP_IND RX"); 4679ecd1ba8SMatthias Ringwald #endif 4689ecd1ba8SMatthias Ringwald hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_GO_TO_SLEEP_ACK); 469307a4fe3SMatthias Ringwald break; 470307a4fe3SMatthias Ringwald default: 471307a4fe3SMatthias Ringwald break; 472307a4fe3SMatthias Ringwald } 473307a4fe3SMatthias Ringwald break; 474307a4fe3SMatthias Ringwald 475307a4fe3SMatthias Ringwald case EHCILL_STATE_SLEEP: 4769ecd1ba8SMatthias Ringwald case EHCILL_STATE_W2_SEND_SLEEP_ACK: 477307a4fe3SMatthias Ringwald switch(action){ 478307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_IND: 479307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 48062ca45d7SMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(0); 4819ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 4829ecd1ba8SMatthias Ringwald log_info("eHCILL: Received WAKE_UP_IND RX"); 4839ecd1ba8SMatthias Ringwald #endif 4849ecd1ba8SMatthias Ringwald hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK); 485307a4fe3SMatthias Ringwald break; 486307a4fe3SMatthias Ringwald 487307a4fe3SMatthias Ringwald default: 488307a4fe3SMatthias Ringwald break; 489307a4fe3SMatthias Ringwald } 490307a4fe3SMatthias Ringwald break; 491307a4fe3SMatthias Ringwald 4929ecd1ba8SMatthias Ringwald case EHCILL_STATE_W4_WAKEUP_IND_OR_ACK: 493307a4fe3SMatthias Ringwald switch(action){ 494307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_IND: 495307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_ACK: 4969ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 4979ecd1ba8SMatthias Ringwald log_info("eHCILL: Received WAKE_UP (%02x)", action); 4989ecd1ba8SMatthias Ringwald #endif 499307a4fe3SMatthias Ringwald tx_state = TX_W4_PACKET_SENT; 500307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 501307a4fe3SMatthias Ringwald btstack_uart->send_block(tx_data, tx_len); 502307a4fe3SMatthias Ringwald break; 503307a4fe3SMatthias Ringwald default: 504307a4fe3SMatthias Ringwald break; 505307a4fe3SMatthias Ringwald } 506307a4fe3SMatthias Ringwald break; 507307a4fe3SMatthias Ringwald } 508307a4fe3SMatthias Ringwald } 5098a23fc53SMatthias Ringwald 5108a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void){ 5119ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 5129ecd1ba8SMatthias Ringwald log_info("eHCILL: handle packet sent, command to send %02x", ehcill_command_to_send); 5139ecd1ba8SMatthias Ringwald #endif 5148a23fc53SMatthias Ringwald // now, send pending ehcill command if neccessary 5158a23fc53SMatthias Ringwald switch (ehcill_command_to_send){ 5168a23fc53SMatthias Ringwald case EHCILL_GO_TO_SLEEP_ACK: 5178a23fc53SMatthias Ringwald hci_transport_h4_ehcill_sleep_ack_timer_setup(); 5188a23fc53SMatthias Ringwald break; 5198a23fc53SMatthias Ringwald case EHCILL_WAKE_UP_IND: 5208a23fc53SMatthias Ringwald hci_transport_h4_ehcill_send_ehcill_command(); 5218a23fc53SMatthias Ringwald break; 5228a23fc53SMatthias Ringwald default: 5238a23fc53SMatthias Ringwald break; 5248a23fc53SMatthias Ringwald } 5258a23fc53SMatthias Ringwald } 5268a23fc53SMatthias Ringwald 5278a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){ 5288a23fc53SMatthias Ringwald tx_state = TX_IDLE; 5298a23fc53SMatthias Ringwald int command = ehcill_command_to_send; 5308a23fc53SMatthias Ringwald ehcill_command_to_send = 0; 5319ecd1ba8SMatthias Ringwald 5329ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 5339ecd1ba8SMatthias Ringwald log_info("eHCILL: handle eHCILL sent, command was %02x", command); 5349ecd1ba8SMatthias Ringwald #endif 5359ecd1ba8SMatthias Ringwald 5368a23fc53SMatthias Ringwald if (command == EHCILL_GO_TO_SLEEP_ACK) { 5379ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 5388a23fc53SMatthias Ringwald log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode"); 5399ecd1ba8SMatthias Ringwald #endif 5408a23fc53SMatthias Ringwald // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent 5418a23fc53SMatthias Ringwald if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){ 5428a23fc53SMatthias Ringwald btstack_uart->set_sleep(btstack_uart_sleep_mode); 5438a23fc53SMatthias Ringwald } 54462ca45d7SMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(1); 5458a23fc53SMatthias Ringwald } 5468a23fc53SMatthias Ringwald // already packet ready? then start wakeup 5478a23fc53SMatthias Ringwald if (hci_transport_h4_ehcill_outgoing_packet_ready()){ 54862ca45d7SMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(0); 5498a23fc53SMatthias Ringwald if (btstack_uart_sleep_mode){ 5508a23fc53SMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 5518a23fc53SMatthias Ringwald } 5529ecd1ba8SMatthias Ringwald if (command != EHCILL_WAKE_UP_IND){ 5538a23fc53SMatthias Ringwald hci_transport_h4_echill_send_wakeup_ind(); 5548a23fc53SMatthias Ringwald } 5558a23fc53SMatthias Ringwald } 5569ecd1ba8SMatthias Ringwald } 5578a23fc53SMatthias Ringwald 558307a4fe3SMatthias Ringwald #endif 559307a4fe3SMatthias Ringwald // --- end of eHCILL implementation --------- 560307a4fe3SMatthias Ringwald 561bd021c4eSMatthias Ringwald static const hci_transport_t hci_transport_h4 = { 562bd021c4eSMatthias Ringwald /* const char * name; */ "H4", 563bd021c4eSMatthias Ringwald /* void (*init) (const void *transport_config); */ &hci_transport_h4_init, 564bd021c4eSMatthias Ringwald /* int (*open)(void); */ &hci_transport_h4_open, 565bd021c4eSMatthias Ringwald /* int (*close)(void); */ &hci_transport_h4_close, 566bd021c4eSMatthias Ringwald /* void (*register_packet_handler)(void (*handler)(...); */ &hci_transport_h4_register_packet_handler, 567bd021c4eSMatthias Ringwald /* int (*can_send_packet_now)(uint8_t packet_type); */ &hci_transport_h4_can_send_now, 568bd021c4eSMatthias Ringwald /* int (*send_packet)(...); */ &hci_transport_h4_send_packet, 569bd021c4eSMatthias Ringwald /* int (*set_baudrate)(uint32_t baudrate); */ &hci_transport_h4_set_baudrate, 570bd021c4eSMatthias Ringwald /* void (*reset_link)(void); */ NULL, 571ee752bb8SMatthias Ringwald /* void (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL, 572bd021c4eSMatthias Ringwald }; 573bd021c4eSMatthias Ringwald 574bd021c4eSMatthias Ringwald // configure and return h4 singleton 575bd021c4eSMatthias Ringwald const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) { 576bd021c4eSMatthias Ringwald btstack_uart = uart_driver; 577bd021c4eSMatthias Ringwald return &hci_transport_h4; 578bd021c4eSMatthias Ringwald } 579