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 * 41bd021c4eSMatthias Ringwald * HCI Transport API implementation for basic H4 protocol over POSIX 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 53*307a4fe3SMatthias Ringwald // #ifdef HAVE_EHCILL 54*307a4fe3SMatthias Ringwald // #error "HCI Transport H4 POSIX does not support eHCILL yet. Please remove HAVE_EHCILL from your btstack-config.h" 55*307a4fe3SMatthias Ringwald // #endif 56*307a4fe3SMatthias Ringwald 57bd021c4eSMatthias Ringwald #ifdef HAVE_EHCILL 58*307a4fe3SMatthias Ringwald 59*307a4fe3SMatthias Ringwald // eHCILL commands 60*307a4fe3SMatthias Ringwald #define EHCILL_GO_TO_SLEEP_IND 0x030 61*307a4fe3SMatthias Ringwald #define EHCILL_GO_TO_SLEEP_ACK 0x031 62*307a4fe3SMatthias Ringwald #define EHCILL_WAKE_UP_IND 0x032 63*307a4fe3SMatthias Ringwald #define EHCILL_WAKE_UP_ACK 0x033 64*307a4fe3SMatthias Ringwald 65*307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_handle(uint8_t action); 66*307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void); 67*307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void); 68*307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void); 69*307a4fe3SMatthias Ringwald static int hci_transport_h4_ehcill_outgoing_packet_ready(void); 70*307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reactivate_rx(void); 71*307a4fe3SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void); 72*307a4fe3SMatthias Ringwald 73*307a4fe3SMatthias Ringwald typedef enum { 74*307a4fe3SMatthias Ringwald EHCILL_STATE_SLEEP, 75*307a4fe3SMatthias Ringwald EHCILL_STATE_W4_ACK, 76*307a4fe3SMatthias Ringwald EHCILL_STATE_AWAKE 77*307a4fe3SMatthias Ringwald } EHCILL_STATE; 78*307a4fe3SMatthias Ringwald 79*307a4fe3SMatthias Ringwald // eHCILL state machine 80*307a4fe3SMatthias Ringwald static EHCILL_STATE ehcill_state; 81*307a4fe3SMatthias Ringwald static uint8_t ehcill_command_to_send; 82*307a4fe3SMatthias Ringwald static uint8_t * ehcill_defer_rx_buffer; 83*307a4fe3SMatthias Ringwald static uint16_t ehcill_defer_rx_size = 0; 84*307a4fe3SMatthias Ringwald 85*307a4fe3SMatthias Ringwald // work around for eHCILL problem 86*307a4fe3SMatthias Ringwald static btstack_timer_source_t ehcill_sleep_ack_timer; 87*307a4fe3SMatthias Ringwald 88bd021c4eSMatthias Ringwald #endif 89bd021c4eSMatthias Ringwald 90bd021c4eSMatthias Ringwald // assert pre-buffer for packet type is available 91bd021c4eSMatthias Ringwald #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0) 92bd021c4eSMatthias Ringwald #error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h 93bd021c4eSMatthias Ringwald #endif 94bd021c4eSMatthias Ringwald 95bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 96bd021c4eSMatthias Ringwald 97bd021c4eSMatthias Ringwald typedef enum { 98bd021c4eSMatthias Ringwald H4_W4_PACKET_TYPE, 99bd021c4eSMatthias Ringwald H4_W4_EVENT_HEADER, 100bd021c4eSMatthias Ringwald H4_W4_ACL_HEADER, 101bd021c4eSMatthias Ringwald H4_W4_SCO_HEADER, 102bd021c4eSMatthias Ringwald H4_W4_PAYLOAD, 103bd021c4eSMatthias Ringwald } H4_STATE; 104bd021c4eSMatthias Ringwald 105*307a4fe3SMatthias Ringwald typedef enum { 106*307a4fe3SMatthias Ringwald TX_IDLE = 1, 107*307a4fe3SMatthias Ringwald TX_W4_PACKET_SENT, 108*307a4fe3SMatthias Ringwald #ifdef HAVE_EHCILL 109*307a4fe3SMatthias Ringwald TX_W4_WAKEUP, 110*307a4fe3SMatthias Ringwald TX_W2_EHCILL_SEND, 111*307a4fe3SMatthias Ringwald TX_W4_EHCILL_SENT, 112*307a4fe3SMatthias Ringwald #endif 113*307a4fe3SMatthias Ringwald } TX_STATE; 114*307a4fe3SMatthias Ringwald 115bd021c4eSMatthias Ringwald // UART Driver + Config 116bd021c4eSMatthias Ringwald static const btstack_uart_block_t * btstack_uart; 117bd021c4eSMatthias Ringwald static btstack_uart_config_t uart_config; 118bd021c4eSMatthias Ringwald 119*307a4fe3SMatthias Ringwald // write state 120*307a4fe3SMatthias Ringwald static TX_STATE tx_state; // updated from block_sent callback 121*307a4fe3SMatthias Ringwald static uint8_t * tx_data; 122*307a4fe3SMatthias Ringwald static uint16_t tx_len; // 0 == no outgoing packet 123*307a4fe3SMatthias Ringwald static uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 124bd021c4eSMatthias Ringwald 125bd021c4eSMatthias Ringwald static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler; 126bd021c4eSMatthias Ringwald 127bd021c4eSMatthias Ringwald // packet reader state machine 128bd021c4eSMatthias Ringwald static H4_STATE h4_state; 129bd021c4eSMatthias Ringwald static int bytes_to_read; 130bd021c4eSMatthias Ringwald static int read_pos; 131bd021c4eSMatthias Ringwald 132bd021c4eSMatthias Ringwald // incoming packet buffer 133bd021c4eSMatthias 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) 134bd021c4eSMatthias Ringwald static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE]; 135bd021c4eSMatthias Ringwald 136bd021c4eSMatthias Ringwald static int hci_transport_h4_set_baudrate(uint32_t baudrate){ 137bd021c4eSMatthias Ringwald log_info("hci_transport_h4_set_baudrate %u", baudrate); 138bd021c4eSMatthias Ringwald return btstack_uart->set_baudrate(baudrate); 139bd021c4eSMatthias Ringwald } 140bd021c4eSMatthias Ringwald 141bd021c4eSMatthias Ringwald static void hci_transport_h4_reset_statemachine(void){ 142bd021c4eSMatthias Ringwald h4_state = H4_W4_PACKET_TYPE; 143bd021c4eSMatthias Ringwald read_pos = 0; 144bd021c4eSMatthias Ringwald bytes_to_read = 1; 145bd021c4eSMatthias Ringwald } 146bd021c4eSMatthias Ringwald 147bd021c4eSMatthias Ringwald static void hci_transport_h4_trigger_next_read(void){ 148bd021c4eSMatthias Ringwald // trigger next read 149bd021c4eSMatthias Ringwald btstack_uart->receive_block(&hci_packet[read_pos], bytes_to_read); 150bd021c4eSMatthias Ringwald } 151bd021c4eSMatthias Ringwald 152bd021c4eSMatthias Ringwald static void hci_transport_h4_block_read(void){ 153bd021c4eSMatthias Ringwald 154bd021c4eSMatthias Ringwald read_pos += bytes_to_read; 155bd021c4eSMatthias Ringwald 156bd021c4eSMatthias Ringwald switch (h4_state) { 157bd021c4eSMatthias Ringwald case H4_W4_PACKET_TYPE: 158bd021c4eSMatthias Ringwald switch (hci_packet[0]){ 159bd021c4eSMatthias Ringwald case HCI_EVENT_PACKET: 160bd021c4eSMatthias Ringwald bytes_to_read = HCI_EVENT_HEADER_SIZE; 161bd021c4eSMatthias Ringwald h4_state = H4_W4_EVENT_HEADER; 162bd021c4eSMatthias Ringwald break; 163bd021c4eSMatthias Ringwald case HCI_ACL_DATA_PACKET: 164bd021c4eSMatthias Ringwald bytes_to_read = HCI_ACL_HEADER_SIZE; 165bd021c4eSMatthias Ringwald h4_state = H4_W4_ACL_HEADER; 166bd021c4eSMatthias Ringwald break; 167bd021c4eSMatthias Ringwald case HCI_SCO_DATA_PACKET: 168bd021c4eSMatthias Ringwald bytes_to_read = HCI_SCO_HEADER_SIZE; 169bd021c4eSMatthias Ringwald h4_state = H4_W4_SCO_HEADER; 170bd021c4eSMatthias Ringwald break; 171*307a4fe3SMatthias Ringwald #ifdef HAVE_EHCILL 172*307a4fe3SMatthias Ringwald case EHCILL_GO_TO_SLEEP_IND: 173*307a4fe3SMatthias Ringwald case EHCILL_GO_TO_SLEEP_ACK: 174*307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_IND: 175*307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_ACK: 176*307a4fe3SMatthias Ringwald bytes_to_read = 1; 177*307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_handle(hci_packet[0]); 178*307a4fe3SMatthias Ringwald break; 179*307a4fe3SMatthias Ringwald #endif 180bd021c4eSMatthias Ringwald default: 181bd021c4eSMatthias Ringwald log_error("h4_process: invalid packet type 0x%02x", hci_packet[0]); 182bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 183bd021c4eSMatthias Ringwald break; 184bd021c4eSMatthias Ringwald } 185bd021c4eSMatthias Ringwald break; 186bd021c4eSMatthias Ringwald 187bd021c4eSMatthias Ringwald case H4_W4_EVENT_HEADER: 188bd021c4eSMatthias Ringwald bytes_to_read = hci_packet[2]; 189bd021c4eSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 190bd021c4eSMatthias Ringwald break; 191bd021c4eSMatthias Ringwald 192bd021c4eSMatthias Ringwald case H4_W4_ACL_HEADER: 193bd021c4eSMatthias Ringwald bytes_to_read = little_endian_read_16( hci_packet, 3); 194bd021c4eSMatthias Ringwald // check ACL length 195bd021c4eSMatthias Ringwald if (HCI_ACL_HEADER_SIZE + bytes_to_read > HCI_PACKET_BUFFER_SIZE){ 196bd021c4eSMatthias Ringwald log_error("h4_process: invalid ACL payload len %u - only space for %u", bytes_to_read, HCI_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE); 197bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 198bd021c4eSMatthias Ringwald break; 199bd021c4eSMatthias Ringwald } 200bd021c4eSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 201bd021c4eSMatthias Ringwald break; 202bd021c4eSMatthias Ringwald 203bd021c4eSMatthias Ringwald case H4_W4_SCO_HEADER: 204bd021c4eSMatthias Ringwald bytes_to_read = hci_packet[3]; 205bd021c4eSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 206bd021c4eSMatthias Ringwald break; 207bd021c4eSMatthias Ringwald 208bd021c4eSMatthias Ringwald case H4_W4_PAYLOAD: 209bd021c4eSMatthias Ringwald packet_handler(hci_packet[0], &hci_packet[1], read_pos-1); 210bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 211bd021c4eSMatthias Ringwald break; 212bd021c4eSMatthias Ringwald default: 213bd021c4eSMatthias Ringwald break; 214bd021c4eSMatthias Ringwald } 215bd021c4eSMatthias Ringwald hci_transport_h4_trigger_next_read(); 216bd021c4eSMatthias Ringwald } 217bd021c4eSMatthias Ringwald 218*307a4fe3SMatthias Ringwald static void hci_transport_h4_block_sent(void){ 219*307a4fe3SMatthias Ringwald tx_state = TX_IDLE; 220*307a4fe3SMatthias Ringwald switch (tx_state){ 221*307a4fe3SMatthias Ringwald case TX_W4_PACKET_SENT: 222*307a4fe3SMatthias Ringwald // packet fully sent, reset state 223*307a4fe3SMatthias Ringwald tx_len = 0; 224*307a4fe3SMatthias Ringwald 225*307a4fe3SMatthias Ringwald #ifdef HAVE_EHCILL 226*307a4fe3SMatthias Ringwald // now, send pending ehcill command if neccessary 227*307a4fe3SMatthias Ringwald switch (ehcill_command_to_send){ 228*307a4fe3SMatthias Ringwald case EHCILL_GO_TO_SLEEP_ACK: 229*307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_sleep_ack_timer_setup(); 230*307a4fe3SMatthias Ringwald break; 231*307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_IND: 232*307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_send_ehcill_command(); 233*307a4fe3SMatthias Ringwald break; 234*307a4fe3SMatthias Ringwald default: 235*307a4fe3SMatthias Ringwald break; 236*307a4fe3SMatthias Ringwald } 237*307a4fe3SMatthias Ringwald #endif 238*307a4fe3SMatthias Ringwald // notify upper stack that it can send again 239*307a4fe3SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, &packet_sent_event[0], sizeof(packet_sent_event)); 240*307a4fe3SMatthias Ringwald break; 241*307a4fe3SMatthias Ringwald 242*307a4fe3SMatthias Ringwald #ifdef HAVE_EHCILL 243*307a4fe3SMatthias Ringwald case TX_W4_EHCILL_SENT: { 244*307a4fe3SMatthias Ringwald int command = ehcill_command_to_send; 245*307a4fe3SMatthias Ringwald ehcill_command_to_send = 0; 246*307a4fe3SMatthias Ringwald if (command == EHCILL_GO_TO_SLEEP_ACK) { 247*307a4fe3SMatthias Ringwald // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent 248*307a4fe3SMatthias Ringwald if (btstack_uart->get_supported_sleep_modes() & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){ 249*307a4fe3SMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE); 250*307a4fe3SMatthias Ringwald } else if (btstack_uart->get_supported_sleep_modes() & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){ 251*307a4fe3SMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE); 252*307a4fe3SMatthias Ringwald } 253*307a4fe3SMatthias Ringwald } 254*307a4fe3SMatthias Ringwald if (hci_transport_h4_ehcill_outgoing_packet_ready()){ 255*307a4fe3SMatthias Ringwald // already packet ready? then start wakeup 256*307a4fe3SMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 257*307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_reactivate_rx(); 258*307a4fe3SMatthias Ringwald hci_transport_h4_echill_send_wakeup_ind(); 259*307a4fe3SMatthias Ringwald } 260*307a4fe3SMatthias Ringwald // TODO: trigger run loop 261*307a4fe3SMatthias Ringwald // btstack_run_loop_embedded_trigger(); 262*307a4fe3SMatthias Ringwald break; 263*307a4fe3SMatthias Ringwald } 264*307a4fe3SMatthias Ringwald #endif 265*307a4fe3SMatthias Ringwald 266*307a4fe3SMatthias Ringwald default: 267*307a4fe3SMatthias Ringwald break; 268*307a4fe3SMatthias Ringwald } 269*307a4fe3SMatthias Ringwald } 270*307a4fe3SMatthias Ringwald 271*307a4fe3SMatthias Ringwald static int hci_transport_h4_can_send_now(uint8_t packet_type){ 272*307a4fe3SMatthias Ringwald return tx_state == TX_IDLE; 273*307a4fe3SMatthias Ringwald } 274*307a4fe3SMatthias Ringwald 275*307a4fe3SMatthias Ringwald static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 276*307a4fe3SMatthias Ringwald // store packet type before actual data and increase size 277*307a4fe3SMatthias Ringwald size++; 278*307a4fe3SMatthias Ringwald packet--; 279*307a4fe3SMatthias Ringwald *packet = packet_type; 280*307a4fe3SMatthias Ringwald 281*307a4fe3SMatthias Ringwald tx_state = TX_W4_PACKET_SENT; 282*307a4fe3SMatthias Ringwald 283*307a4fe3SMatthias Ringwald btstack_uart->send_block(packet, size); 284*307a4fe3SMatthias Ringwald return 0; 285*307a4fe3SMatthias Ringwald } 286*307a4fe3SMatthias Ringwald 287bd021c4eSMatthias Ringwald static void hci_transport_h4_init(const void * transport_config){ 288bd021c4eSMatthias Ringwald // check for hci_transport_config_uart_t 289bd021c4eSMatthias Ringwald if (!transport_config) { 290bd021c4eSMatthias Ringwald log_error("hci_transport_h4: no config!"); 291bd021c4eSMatthias Ringwald return; 292bd021c4eSMatthias Ringwald } 293bd021c4eSMatthias Ringwald if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) { 294bd021c4eSMatthias Ringwald log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!"); 295bd021c4eSMatthias Ringwald return; 296bd021c4eSMatthias Ringwald } 297bd021c4eSMatthias Ringwald 298bd021c4eSMatthias Ringwald // extract UART config from transport config 299bd021c4eSMatthias Ringwald hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config; 300bd021c4eSMatthias Ringwald uart_config.baudrate = hci_transport_config_uart->baudrate_init; 301bd021c4eSMatthias Ringwald uart_config.flowcontrol = hci_transport_config_uart->flowcontrol; 302bd021c4eSMatthias Ringwald uart_config.device_name = hci_transport_config_uart->device_name; 303bd021c4eSMatthias Ringwald 304bd021c4eSMatthias Ringwald // setup UART driver 305bd021c4eSMatthias Ringwald btstack_uart->init(&uart_config); 306bd021c4eSMatthias Ringwald btstack_uart->set_block_received(&hci_transport_h4_block_read); 307bd021c4eSMatthias Ringwald btstack_uart->set_block_sent(&hci_transport_h4_block_sent); 308bd021c4eSMatthias Ringwald } 309bd021c4eSMatthias Ringwald 310bd021c4eSMatthias Ringwald static int hci_transport_h4_open(void){ 311bd021c4eSMatthias Ringwald int res = btstack_uart->open(); 312bd021c4eSMatthias Ringwald if (res){ 313bd021c4eSMatthias Ringwald return res; 314bd021c4eSMatthias Ringwald } 315bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 316bd021c4eSMatthias Ringwald hci_transport_h4_trigger_next_read(); 317*307a4fe3SMatthias Ringwald 318*307a4fe3SMatthias Ringwald tx_state = TX_IDLE; 319*307a4fe3SMatthias Ringwald 320*307a4fe3SMatthias Ringwald #ifdef HAVE_EHCILL 321*307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_reset_statemachine(); 322*307a4fe3SMatthias Ringwald #endif 323bd021c4eSMatthias Ringwald return 0; 324bd021c4eSMatthias Ringwald } 325bd021c4eSMatthias Ringwald 326bd021c4eSMatthias Ringwald static int hci_transport_h4_close(void){ 327bd021c4eSMatthias Ringwald return btstack_uart->close(); 328bd021c4eSMatthias Ringwald } 329bd021c4eSMatthias Ringwald 330bd021c4eSMatthias Ringwald static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 331bd021c4eSMatthias Ringwald packet_handler = handler; 332bd021c4eSMatthias Ringwald } 333bd021c4eSMatthias Ringwald 334bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 335bd021c4eSMatthias Ringwald } 336bd021c4eSMatthias Ringwald 337*307a4fe3SMatthias Ringwald // --- main part of eHCILL implementation --- 338*307a4fe3SMatthias Ringwald 339*307a4fe3SMatthias Ringwald #ifdef HAVE_EHCILL 340*307a4fe3SMatthias Ringwald 341*307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reactivate_rx(void){ 342*307a4fe3SMatthias Ringwald if (!ehcill_defer_rx_size){ 343*307a4fe3SMatthias Ringwald log_error("EHCILL: NO RX REQUEST PENDING"); 344*307a4fe3SMatthias Ringwald return; 345*307a4fe3SMatthias Ringwald } 346*307a4fe3SMatthias Ringwald log_info ("EHCILL: Re-activate rx"); 347*307a4fe3SMatthias Ringwald // receive request, clears RTS 348*307a4fe3SMatthias Ringwald int rx_size = ehcill_defer_rx_size; 349*307a4fe3SMatthias Ringwald ehcill_defer_rx_size = 0; 350*307a4fe3SMatthias Ringwald btstack_uart->receive_block(ehcill_defer_rx_buffer, rx_size); 351*307a4fe3SMatthias Ringwald } 352*307a4fe3SMatthias Ringwald 353*307a4fe3SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void){ 354*307a4fe3SMatthias Ringwald // update state 355*307a4fe3SMatthias Ringwald tx_state = TX_W4_WAKEUP; 356*307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_W4_ACK; 357*307a4fe3SMatthias Ringwald ehcill_command_to_send = EHCILL_WAKE_UP_IND; 358*307a4fe3SMatthias Ringwald btstack_uart->send_block(&ehcill_command_to_send, 1); 359*307a4fe3SMatthias Ringwald } 360*307a4fe3SMatthias Ringwald 361*307a4fe3SMatthias Ringwald static int hci_transport_h4_ehcill_outgoing_packet_ready(void){ 362*307a4fe3SMatthias Ringwald return tx_len != 0; 363*307a4fe3SMatthias Ringwald } 364*307a4fe3SMatthias Ringwald 365*307a4fe3SMatthias Ringwald // static int ehcill_sleep_mode_active(void){ 366*307a4fe3SMatthias Ringwald // return ehcill_state == EHCILL_STATE_SLEEP; 367*307a4fe3SMatthias Ringwald // } 368*307a4fe3SMatthias Ringwald 369*307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void){ 370*307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 371*307a4fe3SMatthias Ringwald } 372*307a4fe3SMatthias Ringwald 373*307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void){ 374*307a4fe3SMatthias Ringwald tx_state = TX_W4_EHCILL_SENT; 375*307a4fe3SMatthias Ringwald btstack_uart->send_block(&ehcill_command_to_send, 1); 376*307a4fe3SMatthias Ringwald } 377*307a4fe3SMatthias Ringwald 378*307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){ 379*307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_send_ehcill_command(); 380*307a4fe3SMatthias Ringwald } 381*307a4fe3SMatthias Ringwald 382*307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){ 383*307a4fe3SMatthias Ringwald // setup timer 384*307a4fe3SMatthias Ringwald ehcill_sleep_ack_timer.process = &hci_transport_h4_ehcill_sleep_ack_timer_handler; 385*307a4fe3SMatthias Ringwald btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50); 386*307a4fe3SMatthias Ringwald btstack_run_loop_add_timer(&ehcill_sleep_ack_timer); 387*307a4fe3SMatthias Ringwald // TODO: trigger run loop 388*307a4fe3SMatthias Ringwald // btstack_run_loop_embedded_trigger(); 389*307a4fe3SMatthias Ringwald } 390*307a4fe3SMatthias Ringwald 391*307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_schedule_ecill_command(uint8_t command){ 392*307a4fe3SMatthias Ringwald ehcill_command_to_send = command; 393*307a4fe3SMatthias Ringwald switch (tx_state){ 394*307a4fe3SMatthias Ringwald case TX_IDLE: 395*307a4fe3SMatthias Ringwald if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){ 396*307a4fe3SMatthias Ringwald // send right away 397*307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_send_ehcill_command(); 398*307a4fe3SMatthias Ringwald } else { 399*307a4fe3SMatthias Ringwald // change state so BTstack cannot send and setup timer 400*307a4fe3SMatthias Ringwald tx_state = TX_W2_EHCILL_SEND; 401*307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_sleep_ack_timer_setup(); 402*307a4fe3SMatthias Ringwald } 403*307a4fe3SMatthias Ringwald break; 404*307a4fe3SMatthias Ringwald default: 405*307a4fe3SMatthias Ringwald break; 406*307a4fe3SMatthias Ringwald } 407*307a4fe3SMatthias Ringwald } 408*307a4fe3SMatthias Ringwald 409*307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_handle(uint8_t action){ 410*307a4fe3SMatthias Ringwald // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size); 411*307a4fe3SMatthias Ringwald switch(ehcill_state){ 412*307a4fe3SMatthias Ringwald case EHCILL_STATE_AWAKE: 413*307a4fe3SMatthias Ringwald switch(action){ 414*307a4fe3SMatthias Ringwald case EHCILL_GO_TO_SLEEP_IND: 415*307a4fe3SMatthias Ringwald // 1. set RTS high - already done by BT RX ISR 416*307a4fe3SMatthias Ringwald // 2. enable CTS IRQ - CTS always enabled 417*307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_SLEEP; 418*307a4fe3SMatthias Ringwald log_info("EHCILL: GO_TO_SLEEP_IND RX"); 419*307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_schedule_ecill_command(EHCILL_GO_TO_SLEEP_ACK); 420*307a4fe3SMatthias Ringwald break; 421*307a4fe3SMatthias Ringwald default: 422*307a4fe3SMatthias Ringwald break; 423*307a4fe3SMatthias Ringwald } 424*307a4fe3SMatthias Ringwald break; 425*307a4fe3SMatthias Ringwald 426*307a4fe3SMatthias Ringwald case EHCILL_STATE_SLEEP: 427*307a4fe3SMatthias Ringwald switch(action){ 428*307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_IND: 429*307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 430*307a4fe3SMatthias Ringwald log_info("EHCILL: WAKE_UP_IND RX"); 431*307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_schedule_ecill_command(EHCILL_WAKE_UP_ACK); 432*307a4fe3SMatthias Ringwald break; 433*307a4fe3SMatthias Ringwald 434*307a4fe3SMatthias Ringwald default: 435*307a4fe3SMatthias Ringwald break; 436*307a4fe3SMatthias Ringwald } 437*307a4fe3SMatthias Ringwald break; 438*307a4fe3SMatthias Ringwald 439*307a4fe3SMatthias Ringwald case EHCILL_STATE_W4_ACK: 440*307a4fe3SMatthias Ringwald switch(action){ 441*307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_IND: 442*307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_ACK: 443*307a4fe3SMatthias Ringwald log_info("EHCILL: WAKE_UP_IND or ACK"); 444*307a4fe3SMatthias Ringwald tx_state = TX_W4_PACKET_SENT; 445*307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 446*307a4fe3SMatthias Ringwald btstack_uart->send_block(tx_data, tx_len); 447*307a4fe3SMatthias Ringwald break; 448*307a4fe3SMatthias Ringwald default: 449*307a4fe3SMatthias Ringwald break; 450*307a4fe3SMatthias Ringwald } 451*307a4fe3SMatthias Ringwald break; 452*307a4fe3SMatthias Ringwald } 453*307a4fe3SMatthias Ringwald } 454*307a4fe3SMatthias Ringwald #endif 455*307a4fe3SMatthias Ringwald // --- end of eHCILL implementation --------- 456*307a4fe3SMatthias Ringwald 457bd021c4eSMatthias Ringwald static const hci_transport_t hci_transport_h4 = { 458bd021c4eSMatthias Ringwald /* const char * name; */ "H4", 459bd021c4eSMatthias Ringwald /* void (*init) (const void *transport_config); */ &hci_transport_h4_init, 460bd021c4eSMatthias Ringwald /* int (*open)(void); */ &hci_transport_h4_open, 461bd021c4eSMatthias Ringwald /* int (*close)(void); */ &hci_transport_h4_close, 462bd021c4eSMatthias Ringwald /* void (*register_packet_handler)(void (*handler)(...); */ &hci_transport_h4_register_packet_handler, 463bd021c4eSMatthias Ringwald /* int (*can_send_packet_now)(uint8_t packet_type); */ &hci_transport_h4_can_send_now, 464bd021c4eSMatthias Ringwald /* int (*send_packet)(...); */ &hci_transport_h4_send_packet, 465bd021c4eSMatthias Ringwald /* int (*set_baudrate)(uint32_t baudrate); */ &hci_transport_h4_set_baudrate, 466bd021c4eSMatthias Ringwald /* void (*reset_link)(void); */ NULL, 467bd021c4eSMatthias Ringwald }; 468bd021c4eSMatthias Ringwald 469bd021c4eSMatthias Ringwald // configure and return h4 singleton 470bd021c4eSMatthias Ringwald const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) { 471bd021c4eSMatthias Ringwald btstack_uart = uart_driver; 472bd021c4eSMatthias Ringwald return &hci_transport_h4; 473bd021c4eSMatthias Ringwald } 474