1bd021c4eSMatthias Ringwald /* 2bd021c4eSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3bd021c4eSMatthias Ringwald * 4bd021c4eSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5bd021c4eSMatthias Ringwald * modification, are permitted provided that the following conditions 6bd021c4eSMatthias Ringwald * are met: 7bd021c4eSMatthias Ringwald * 8bd021c4eSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9bd021c4eSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10bd021c4eSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11bd021c4eSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12bd021c4eSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13bd021c4eSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14bd021c4eSMatthias Ringwald * contributors may be used to endorse or promote products derived 15bd021c4eSMatthias Ringwald * from this software without specific prior written permission. 16bd021c4eSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17bd021c4eSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18bd021c4eSMatthias Ringwald * monetary gain. 19bd021c4eSMatthias Ringwald * 20bd021c4eSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21bd021c4eSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22bd021c4eSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23bd021c4eSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24bd021c4eSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25bd021c4eSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26bd021c4eSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27bd021c4eSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28bd021c4eSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29bd021c4eSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30bd021c4eSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31bd021c4eSMatthias Ringwald * SUCH DAMAGE. 32bd021c4eSMatthias Ringwald * 33bd021c4eSMatthias Ringwald * Please inquire about commercial licensing options at 34bd021c4eSMatthias Ringwald * [email protected] 35bd021c4eSMatthias Ringwald * 36bd021c4eSMatthias Ringwald */ 37bd021c4eSMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hci_transport_h4.c" 39ab2c6ae4SMatthias Ringwald 40bd021c4eSMatthias Ringwald /* 41bd021c4eSMatthias Ringwald * hci_h4_transport.c 42bd021c4eSMatthias Ringwald * 438a23fc53SMatthias Ringwald * HCI Transport API implementation for H4 protocol over POSIX with optional support for eHCILL 44bd021c4eSMatthias Ringwald * 45bd021c4eSMatthias Ringwald * Created by Matthias Ringwald on 4/29/09. 46bd021c4eSMatthias Ringwald */ 47bd021c4eSMatthias Ringwald 48bace42efSMatthias Ringwald #include <inttypes.h> 49bace42efSMatthias Ringwald 50bd021c4eSMatthias Ringwald #include "btstack_config.h" 51bd021c4eSMatthias Ringwald 52bd021c4eSMatthias Ringwald #include "btstack_debug.h" 53bd021c4eSMatthias Ringwald #include "hci.h" 54bd021c4eSMatthias Ringwald #include "hci_transport.h" 5539e7ee9fSMatthias Ringwald #include "bluetooth_company_id.h" 56bd021c4eSMatthias Ringwald #include "btstack_uart_block.h" 57bd021c4eSMatthias Ringwald 589ecd1ba8SMatthias Ringwald #define ENABLE_LOG_EHCILL 599ecd1ba8SMatthias Ringwald 60f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 61307a4fe3SMatthias Ringwald 62307a4fe3SMatthias Ringwald // eHCILL commands 63c4e86052SMatthias Ringwald enum EHCILL_MESSAGES { 64c4e86052SMatthias Ringwald EHCILL_GO_TO_SLEEP_IND = 0x030, 65c4e86052SMatthias Ringwald EHCILL_GO_TO_SLEEP_ACK = 0x031, 66c4e86052SMatthias Ringwald EHCILL_WAKE_UP_IND = 0x032, 67c4e86052SMatthias Ringwald EHCILL_WAKE_UP_ACK = 0x033, 68b403028eSMatthias Ringwald EHCILL_WAKEUP_SIGNAL = 0x034, 69c4e86052SMatthias Ringwald }; 70307a4fe3SMatthias Ringwald 718a23fc53SMatthias Ringwald static int hci_transport_h4_ehcill_outgoing_packet_ready(void); 728a23fc53SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void); 738a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action); 748a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void); 758a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void); 768a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void); 77307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void); 78307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void); 79307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void); 808a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void); 81307a4fe3SMatthias Ringwald 82307a4fe3SMatthias Ringwald typedef enum { 839ecd1ba8SMatthias Ringwald EHCILL_STATE_W2_SEND_SLEEP_ACK, 84307a4fe3SMatthias Ringwald EHCILL_STATE_SLEEP, 859ecd1ba8SMatthias Ringwald EHCILL_STATE_W4_WAKEUP_IND_OR_ACK, 86307a4fe3SMatthias Ringwald EHCILL_STATE_AWAKE 87307a4fe3SMatthias Ringwald } EHCILL_STATE; 88307a4fe3SMatthias Ringwald 89307a4fe3SMatthias Ringwald // eHCILL state machine 90307a4fe3SMatthias Ringwald static EHCILL_STATE ehcill_state; 91307a4fe3SMatthias Ringwald static uint8_t ehcill_command_to_send; 92307a4fe3SMatthias Ringwald 93f6a20ec9SMatthias Ringwald static btstack_uart_sleep_mode_t btstack_uart_sleep_mode; 94f6a20ec9SMatthias Ringwald 95307a4fe3SMatthias Ringwald // work around for eHCILL problem 96307a4fe3SMatthias Ringwald static btstack_timer_source_t ehcill_sleep_ack_timer; 97307a4fe3SMatthias Ringwald 98bd021c4eSMatthias Ringwald #endif 99bd021c4eSMatthias Ringwald 10082d05f16SMatthias Ringwald 101bd021c4eSMatthias Ringwald // assert pre-buffer for packet type is available 102bd021c4eSMatthias Ringwald #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0) 103bd021c4eSMatthias Ringwald #error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h 104bd021c4eSMatthias Ringwald #endif 105bd021c4eSMatthias Ringwald 106bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 107bd021c4eSMatthias Ringwald 108bd021c4eSMatthias Ringwald typedef enum { 109c682b8ecSMatthias Ringwald H4_OFF, 110bd021c4eSMatthias Ringwald H4_W4_PACKET_TYPE, 111bd021c4eSMatthias Ringwald H4_W4_EVENT_HEADER, 112bd021c4eSMatthias Ringwald H4_W4_ACL_HEADER, 113bd021c4eSMatthias Ringwald H4_W4_SCO_HEADER, 114bd021c4eSMatthias Ringwald H4_W4_PAYLOAD, 115bd021c4eSMatthias Ringwald } H4_STATE; 116bd021c4eSMatthias Ringwald 117307a4fe3SMatthias Ringwald typedef enum { 118c682b8ecSMatthias Ringwald TX_OFF, 119c682b8ecSMatthias Ringwald TX_IDLE, 120307a4fe3SMatthias Ringwald TX_W4_PACKET_SENT, 121f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 122307a4fe3SMatthias Ringwald TX_W4_WAKEUP, 123307a4fe3SMatthias Ringwald TX_W2_EHCILL_SEND, 124307a4fe3SMatthias Ringwald TX_W4_EHCILL_SENT, 125307a4fe3SMatthias Ringwald #endif 126307a4fe3SMatthias Ringwald } TX_STATE; 127307a4fe3SMatthias Ringwald 128bd021c4eSMatthias Ringwald // UART Driver + Config 129f9bd6dd7SMatthias Ringwald static const btstack_uart_t * btstack_uart; 130bd021c4eSMatthias Ringwald static btstack_uart_config_t uart_config; 131bd021c4eSMatthias Ringwald 132307a4fe3SMatthias Ringwald // write state 1338a23fc53SMatthias Ringwald static TX_STATE tx_state; 1342e25934bSMatthias Ringwald #ifdef ENABLE_EHCILL 1352e25934bSMatthias Ringwald static uint8_t * ehcill_tx_data; 1362e25934bSMatthias Ringwald static uint16_t ehcill_tx_len; // 0 == no outgoing packet 1372e25934bSMatthias Ringwald #endif 1388a23fc53SMatthias Ringwald 139bd021c4eSMatthias Ringwald static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler; 140bd021c4eSMatthias Ringwald 141bd021c4eSMatthias Ringwald // packet reader state machine 142bd021c4eSMatthias Ringwald static H4_STATE h4_state; 143ea374553SMatthias Ringwald static uint16_t bytes_to_read; 144ea374553SMatthias Ringwald static uint16_t read_pos; 145bd021c4eSMatthias Ringwald 146bd021c4eSMatthias Ringwald // incoming packet buffer 147fc6cde64SMatthias Ringwald static uint8_t hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + HCI_INCOMING_PACKET_BUFFER_SIZE + 1]; // packet type + max(acl header + acl payload, event header + event data) 148bd021c4eSMatthias Ringwald static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE]; 149bd021c4eSMatthias Ringwald 15077291b13SMatthias Ringwald // Baudrate change bugs in TI CC256x and CYW20704 15139e7ee9fSMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 15277291b13SMatthias Ringwald #define ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 15339e7ee9fSMatthias Ringwald static const uint8_t baud_rate_command_prefix[] = { 0x01, 0x36, 0xff, 0x04}; 15477291b13SMatthias Ringwald #endif 15577291b13SMatthias Ringwald 15677291b13SMatthias Ringwald #ifdef ENABLE_CYPRESS_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 15777291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 15877291b13SMatthias Ringwald #error "Please enable either ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND or ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND" 15977291b13SMatthias Ringwald #endif 16077291b13SMatthias Ringwald #define ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 16177291b13SMatthias Ringwald static const uint8_t baud_rate_command_prefix[] = { 0x01, 0x18, 0xfc, 0x06}; 16277291b13SMatthias Ringwald #endif 16377291b13SMatthias Ringwald 16477291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 16577291b13SMatthias Ringwald static const uint8_t local_version_event_prefix[] = { 0x04, 0x0e, 0x0c, 0x01, 0x01, 0x10}; 16639e7ee9fSMatthias Ringwald static enum { 1677741d2d0SMatthias Ringwald BAUDRATE_CHANGE_WORKAROUND_IDLE, 1687741d2d0SMatthias Ringwald BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED, 1697741d2d0SMatthias Ringwald BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT, 1707741d2d0SMatthias Ringwald BAUDRATE_CHANGE_WORKAROUND_DONE 1717741d2d0SMatthias Ringwald } baudrate_change_workaround_state; 17239e7ee9fSMatthias Ringwald #endif 17339e7ee9fSMatthias Ringwald 174bd021c4eSMatthias Ringwald static int hci_transport_h4_set_baudrate(uint32_t baudrate){ 175bace42efSMatthias Ringwald log_info("hci_transport_h4_set_baudrate %"PRIu32, baudrate); 176bd021c4eSMatthias Ringwald return btstack_uart->set_baudrate(baudrate); 177bd021c4eSMatthias Ringwald } 178bd021c4eSMatthias Ringwald 179bd021c4eSMatthias Ringwald static void hci_transport_h4_reset_statemachine(void){ 180bd021c4eSMatthias Ringwald h4_state = H4_W4_PACKET_TYPE; 181bd021c4eSMatthias Ringwald read_pos = 0; 182bd021c4eSMatthias Ringwald bytes_to_read = 1; 183bd021c4eSMatthias Ringwald } 184bd021c4eSMatthias Ringwald 185bd021c4eSMatthias Ringwald static void hci_transport_h4_trigger_next_read(void){ 1868a23fc53SMatthias Ringwald // log_info("hci_transport_h4_trigger_next_read: %u bytes", bytes_to_read); 187bd021c4eSMatthias Ringwald btstack_uart->receive_block(&hci_packet[read_pos], bytes_to_read); 188bd021c4eSMatthias Ringwald } 189bd021c4eSMatthias Ringwald 190bb806528SMatthias Ringwald static void hci_transport_h4_packet_complete(void){ 191bb806528SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 192bb806528SMatthias Ringwald if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_IDLE 193bb806528SMatthias Ringwald && memcmp(hci_packet, local_version_event_prefix, sizeof(local_version_event_prefix)) == 0){ 194bb806528SMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 195bb806528SMatthias Ringwald if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){ 196bb806528SMatthias Ringwald // detect TI CC256x controller based on manufacturer 197bb806528SMatthias Ringwald log_info("Detected CC256x controller"); 198bb806528SMatthias Ringwald baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED; 199bb806528SMatthias Ringwald } else { 200bb806528SMatthias Ringwald // work around not needed 201bb806528SMatthias Ringwald log_info("Bluetooth controller not by TI"); 202bb806528SMatthias Ringwald baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE; 203bb806528SMatthias Ringwald } 204bb806528SMatthias Ringwald #endif 205bb806528SMatthias Ringwald #ifdef ENABLE_CYPRESS_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 206bb806528SMatthias Ringwald if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){ 207bb806528SMatthias Ringwald // detect Cypress controller based on manufacturer 208bb806528SMatthias Ringwald log_info("Detected Cypress controller"); 209bb806528SMatthias Ringwald baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED; 210bb806528SMatthias Ringwald } else { 211bb806528SMatthias Ringwald // work around not needed 212bb806528SMatthias Ringwald log_info("Bluetooth controller not by Cypress"); 213bb806528SMatthias Ringwald baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE; 214bb806528SMatthias Ringwald } 215bb806528SMatthias Ringwald #endif 216bb806528SMatthias Ringwald } 217bb806528SMatthias Ringwald #endif 2184ea43905SMatthias Ringwald uint16_t packet_len = read_pos-1u; 219bd021c4eSMatthias Ringwald 220bb806528SMatthias Ringwald // reset state machine before delivering packet to stack as it might close the transport 221bb806528SMatthias Ringwald hci_transport_h4_reset_statemachine(); 222bb806528SMatthias Ringwald packet_handler(hci_packet[0], &hci_packet[1], packet_len); 223bb806528SMatthias Ringwald } 224bb806528SMatthias Ringwald 225bb806528SMatthias Ringwald static void hci_transport_h4_block_read(void){ 226cab2c687SMatthias Ringwald 227bd021c4eSMatthias Ringwald read_pos += bytes_to_read; 228bd021c4eSMatthias Ringwald 229bd021c4eSMatthias Ringwald switch (h4_state) { 230bd021c4eSMatthias Ringwald case H4_W4_PACKET_TYPE: 231bd021c4eSMatthias Ringwald switch (hci_packet[0]){ 232bd021c4eSMatthias Ringwald case HCI_EVENT_PACKET: 233bd021c4eSMatthias Ringwald bytes_to_read = HCI_EVENT_HEADER_SIZE; 234bd021c4eSMatthias Ringwald h4_state = H4_W4_EVENT_HEADER; 235bd021c4eSMatthias Ringwald break; 236bd021c4eSMatthias Ringwald case HCI_ACL_DATA_PACKET: 237bd021c4eSMatthias Ringwald bytes_to_read = HCI_ACL_HEADER_SIZE; 238bd021c4eSMatthias Ringwald h4_state = H4_W4_ACL_HEADER; 239bd021c4eSMatthias Ringwald break; 240bd021c4eSMatthias Ringwald case HCI_SCO_DATA_PACKET: 241bd021c4eSMatthias Ringwald bytes_to_read = HCI_SCO_HEADER_SIZE; 242bd021c4eSMatthias Ringwald h4_state = H4_W4_SCO_HEADER; 243bd021c4eSMatthias Ringwald break; 244f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 245307a4fe3SMatthias Ringwald case EHCILL_GO_TO_SLEEP_IND: 246307a4fe3SMatthias Ringwald case EHCILL_GO_TO_SLEEP_ACK: 247307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_IND: 248307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_ACK: 2498a23fc53SMatthias Ringwald hci_transport_h4_ehcill_handle_command(hci_packet[0]); 2508a23fc53SMatthias Ringwald hci_transport_h4_reset_statemachine(); 251307a4fe3SMatthias Ringwald break; 252307a4fe3SMatthias Ringwald #endif 253bd021c4eSMatthias Ringwald default: 2548a23fc53SMatthias Ringwald log_error("hci_transport_h4: invalid packet type 0x%02x", hci_packet[0]); 255bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 256bd021c4eSMatthias Ringwald break; 257bd021c4eSMatthias Ringwald } 258bd021c4eSMatthias Ringwald break; 259bd021c4eSMatthias Ringwald 260bd021c4eSMatthias Ringwald case H4_W4_EVENT_HEADER: 261bd021c4eSMatthias Ringwald bytes_to_read = hci_packet[2]; 262e8b81068SMatthias Ringwald // check Event length 263ea374553SMatthias Ringwald if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_EVENT_HEADER_SIZE)){ 264e8b81068SMatthias Ringwald log_error("hci_transport_h4: invalid Event len %d - only space for %u", bytes_to_read, HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_EVENT_HEADER_SIZE); 265e8b81068SMatthias Ringwald hci_transport_h4_reset_statemachine(); 266e8b81068SMatthias Ringwald break; 267e8b81068SMatthias Ringwald } 268bd021c4eSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 269bd021c4eSMatthias Ringwald break; 270bd021c4eSMatthias Ringwald 271bd021c4eSMatthias Ringwald case H4_W4_ACL_HEADER: 272bd021c4eSMatthias Ringwald bytes_to_read = little_endian_read_16( hci_packet, 3); 273bd021c4eSMatthias Ringwald // check ACL length 274ea374553SMatthias Ringwald if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE)){ 275fc6cde64SMatthias Ringwald log_error("hci_transport_h4: invalid ACL payload len %d - only space for %u", bytes_to_read, HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE); 276bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 277bd021c4eSMatthias Ringwald break; 278bd021c4eSMatthias Ringwald } 279bd021c4eSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 280bd021c4eSMatthias Ringwald break; 281bd021c4eSMatthias Ringwald 282bd021c4eSMatthias Ringwald case H4_W4_SCO_HEADER: 283bd021c4eSMatthias Ringwald bytes_to_read = hci_packet[3]; 284e8b81068SMatthias Ringwald // check SCO length 285ea374553SMatthias Ringwald if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_SCO_HEADER_SIZE)){ 286e8b81068SMatthias Ringwald log_error("hci_transport_h4: invalid SCO payload len %d - only space for %u", bytes_to_read, HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_SCO_HEADER_SIZE); 287e8b81068SMatthias Ringwald hci_transport_h4_reset_statemachine(); 288e8b81068SMatthias Ringwald break; 289e8b81068SMatthias Ringwald } 290bd021c4eSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 291bd021c4eSMatthias Ringwald break; 292bd021c4eSMatthias Ringwald 293bd021c4eSMatthias Ringwald case H4_W4_PAYLOAD: 294bb806528SMatthias Ringwald hci_transport_h4_packet_complete(); 295bd021c4eSMatthias Ringwald break; 296ad254f5dSMatthias Ringwald 297ad254f5dSMatthias Ringwald case H4_OFF: 298ad254f5dSMatthias Ringwald bytes_to_read = 0; 299e80b814dSMatthias Ringwald break; 3007bbeb3adSMilanka Ringwald default: 3017bbeb3adSMilanka Ringwald btstack_assert(false); 3027bbeb3adSMilanka Ringwald break; 303bd021c4eSMatthias Ringwald } 30439e7ee9fSMatthias Ringwald 30577291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 3067741d2d0SMatthias Ringwald if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT){ 3077741d2d0SMatthias Ringwald baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_IDLE; 30839e7ee9fSMatthias Ringwald // avoid flowcontrol problem by reading expected hci command complete event of 7 bytes in a single block read 30939e7ee9fSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 31039e7ee9fSMatthias Ringwald bytes_to_read = 7; 31139e7ee9fSMatthias Ringwald } 31239e7ee9fSMatthias Ringwald #endif 31339e7ee9fSMatthias Ringwald 314bb806528SMatthias Ringwald // forward packet if payload size == 0 3154ea43905SMatthias Ringwald if (h4_state == H4_W4_PAYLOAD && bytes_to_read == 0u) { 316bb806528SMatthias Ringwald hci_transport_h4_packet_complete(); 317bb806528SMatthias Ringwald } 318bb806528SMatthias Ringwald 319e80b814dSMatthias Ringwald if (h4_state != H4_OFF) { 320bd021c4eSMatthias Ringwald hci_transport_h4_trigger_next_read(); 321bd021c4eSMatthias Ringwald } 322e80b814dSMatthias Ringwald } 323bd021c4eSMatthias Ringwald 324307a4fe3SMatthias Ringwald static void hci_transport_h4_block_sent(void){ 3258334d3d8SMatthias Ringwald 3268334d3d8SMatthias Ringwald static const uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 3278334d3d8SMatthias Ringwald 328307a4fe3SMatthias Ringwald switch (tx_state){ 329307a4fe3SMatthias Ringwald case TX_W4_PACKET_SENT: 330307a4fe3SMatthias Ringwald // packet fully sent, reset state 3312e25934bSMatthias Ringwald #ifdef ENABLE_EHCILL 3322e25934bSMatthias Ringwald ehcill_tx_len = 0; 3332e25934bSMatthias Ringwald #endif 3348a23fc53SMatthias Ringwald tx_state = TX_IDLE; 335307a4fe3SMatthias Ringwald 336f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 3378a23fc53SMatthias Ringwald // notify eHCILL engine 3388a23fc53SMatthias Ringwald hci_transport_h4_ehcill_handle_packet_sent(); 339307a4fe3SMatthias Ringwald #endif 340307a4fe3SMatthias Ringwald // notify upper stack that it can send again 341d5fc1d92SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, (uint8_t *) &packet_sent_event[0], sizeof(packet_sent_event)); 342307a4fe3SMatthias Ringwald break; 343307a4fe3SMatthias Ringwald 344f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 3458a23fc53SMatthias Ringwald case TX_W4_EHCILL_SENT: 346b403028eSMatthias Ringwald case TX_W4_WAKEUP: 3478a23fc53SMatthias Ringwald hci_transport_h4_ehcill_handle_ehcill_command_sent(); 348307a4fe3SMatthias Ringwald break; 349307a4fe3SMatthias Ringwald #endif 350307a4fe3SMatthias Ringwald 351307a4fe3SMatthias Ringwald default: 352307a4fe3SMatthias Ringwald break; 353307a4fe3SMatthias Ringwald } 354307a4fe3SMatthias Ringwald } 355307a4fe3SMatthias Ringwald 356307a4fe3SMatthias Ringwald static int hci_transport_h4_can_send_now(uint8_t packet_type){ 357cebe3e9eSMatthias Ringwald UNUSED(packet_type); 358307a4fe3SMatthias Ringwald return tx_state == TX_IDLE; 359307a4fe3SMatthias Ringwald } 360307a4fe3SMatthias Ringwald 361307a4fe3SMatthias Ringwald static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 36239e7ee9fSMatthias Ringwald 363307a4fe3SMatthias Ringwald // store packet type before actual data and increase size 364307a4fe3SMatthias Ringwald size++; 365307a4fe3SMatthias Ringwald packet--; 366307a4fe3SMatthias Ringwald *packet = packet_type; 367307a4fe3SMatthias Ringwald 36877291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 3697741d2d0SMatthias Ringwald if ((baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED) 37039e7ee9fSMatthias Ringwald && (memcmp(packet, baud_rate_command_prefix, sizeof(baud_rate_command_prefix)) == 0)) { 3717741d2d0SMatthias Ringwald log_info("Baud rate command detected, expect command complete event next"); 3727741d2d0SMatthias Ringwald baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT; 37339e7ee9fSMatthias Ringwald } 37439e7ee9fSMatthias Ringwald #endif 37539e7ee9fSMatthias Ringwald 376f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 3772e25934bSMatthias Ringwald // store request for later 3782e25934bSMatthias Ringwald ehcill_tx_len = size; 3792e25934bSMatthias Ringwald ehcill_tx_data = packet; 3809ecd1ba8SMatthias Ringwald switch (ehcill_state){ 3819ecd1ba8SMatthias Ringwald case EHCILL_STATE_SLEEP: 3828a23fc53SMatthias Ringwald hci_transport_h4_ehcill_trigger_wakeup(); 3838a23fc53SMatthias Ringwald return 0; 3849ecd1ba8SMatthias Ringwald case EHCILL_STATE_W2_SEND_SLEEP_ACK: 385b403028eSMatthias Ringwald log_info("eHILL: send next packet, state EHCILL_STATE_W2_SEND_SLEEP_ACK"); 3869ecd1ba8SMatthias Ringwald return 0; 3879ecd1ba8SMatthias Ringwald default: 3889ecd1ba8SMatthias Ringwald break; 3898a23fc53SMatthias Ringwald } 3908a23fc53SMatthias Ringwald #endif 3918a23fc53SMatthias Ringwald 3928a23fc53SMatthias Ringwald // start sending 3938a23fc53SMatthias Ringwald tx_state = TX_W4_PACKET_SENT; 394307a4fe3SMatthias Ringwald btstack_uart->send_block(packet, size); 395307a4fe3SMatthias Ringwald return 0; 396307a4fe3SMatthias Ringwald } 397307a4fe3SMatthias Ringwald 398bd021c4eSMatthias Ringwald static void hci_transport_h4_init(const void * transport_config){ 399bd021c4eSMatthias Ringwald // check for hci_transport_config_uart_t 400bd021c4eSMatthias Ringwald if (!transport_config) { 401bd021c4eSMatthias Ringwald log_error("hci_transport_h4: no config!"); 402bd021c4eSMatthias Ringwald return; 403bd021c4eSMatthias Ringwald } 404bd021c4eSMatthias Ringwald if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) { 405bd021c4eSMatthias Ringwald log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!"); 406bd021c4eSMatthias Ringwald return; 407bd021c4eSMatthias Ringwald } 408bd021c4eSMatthias Ringwald 409bd021c4eSMatthias Ringwald // extract UART config from transport config 410bd021c4eSMatthias Ringwald hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config; 411bd021c4eSMatthias Ringwald uart_config.baudrate = hci_transport_config_uart->baudrate_init; 412bd021c4eSMatthias Ringwald uart_config.flowcontrol = hci_transport_config_uart->flowcontrol; 413*e76f5dd4SMatthias Ringwald uart_config.parity = hci_transport_config_uart->parity; 414bd021c4eSMatthias Ringwald uart_config.device_name = hci_transport_config_uart->device_name; 415bd021c4eSMatthias Ringwald 416c682b8ecSMatthias Ringwald // set state to off 417c682b8ecSMatthias Ringwald tx_state = TX_OFF; 418c682b8ecSMatthias Ringwald h4_state = H4_OFF; 419c682b8ecSMatthias Ringwald 420bd021c4eSMatthias Ringwald // setup UART driver 421bd021c4eSMatthias Ringwald btstack_uart->init(&uart_config); 422bd021c4eSMatthias Ringwald btstack_uart->set_block_received(&hci_transport_h4_block_read); 423bd021c4eSMatthias Ringwald btstack_uart->set_block_sent(&hci_transport_h4_block_sent); 424bd021c4eSMatthias Ringwald } 425bd021c4eSMatthias Ringwald 426bd021c4eSMatthias Ringwald static int hci_transport_h4_open(void){ 427c682b8ecSMatthias Ringwald // open uart driver 428bd021c4eSMatthias Ringwald int res = btstack_uart->open(); 4299305033eSMatthias Ringwald if (res != 0){ 430bd021c4eSMatthias Ringwald return res; 431bd021c4eSMatthias Ringwald } 432c682b8ecSMatthias Ringwald 433c682b8ecSMatthias Ringwald // init rx + tx state machines 434bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 435bd021c4eSMatthias Ringwald hci_transport_h4_trigger_next_read(); 436307a4fe3SMatthias Ringwald tx_state = TX_IDLE; 437307a4fe3SMatthias Ringwald 438f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 4398a23fc53SMatthias Ringwald hci_transport_h4_ehcill_open(); 440307a4fe3SMatthias Ringwald #endif 441bd021c4eSMatthias Ringwald return 0; 442bd021c4eSMatthias Ringwald } 443bd021c4eSMatthias Ringwald 444bd021c4eSMatthias Ringwald static int hci_transport_h4_close(void){ 445c682b8ecSMatthias Ringwald // set state to off 446c682b8ecSMatthias Ringwald tx_state = TX_OFF; 447c682b8ecSMatthias Ringwald h4_state = H4_OFF; 448c682b8ecSMatthias Ringwald 449c682b8ecSMatthias Ringwald // close uart driver 450bd021c4eSMatthias Ringwald return btstack_uart->close(); 451bd021c4eSMatthias Ringwald } 452bd021c4eSMatthias Ringwald 453bd021c4eSMatthias Ringwald static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 454bd021c4eSMatthias Ringwald packet_handler = handler; 455bd021c4eSMatthias Ringwald } 456bd021c4eSMatthias Ringwald 457bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 458cebe3e9eSMatthias Ringwald UNUSED(packet_type); 459cebe3e9eSMatthias Ringwald UNUSED(packet); 460cebe3e9eSMatthias Ringwald UNUSED(size); 461bd021c4eSMatthias Ringwald } 462bd021c4eSMatthias Ringwald 4638a23fc53SMatthias Ringwald // 464307a4fe3SMatthias Ringwald // --- main part of eHCILL implementation --- 4658a23fc53SMatthias Ringwald // 466307a4fe3SMatthias Ringwald 467f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 468307a4fe3SMatthias Ringwald 46962ca45d7SMatthias Ringwald static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){ 47062ca45d7SMatthias Ringwald static int last_state = 0; 47162ca45d7SMatthias Ringwald if (sleep_active == last_state) return; 47262ca45d7SMatthias Ringwald last_state = sleep_active; 47362ca45d7SMatthias Ringwald 47462ca45d7SMatthias Ringwald log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active); 47562ca45d7SMatthias Ringwald uint8_t event[3]; 47662ca45d7SMatthias Ringwald event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE; 47762ca45d7SMatthias Ringwald event[1] = sizeof(event) - 2; 47862ca45d7SMatthias Ringwald event[2] = sleep_active; 47962ca45d7SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 48062ca45d7SMatthias Ringwald } 48162ca45d7SMatthias Ringwald 482b403028eSMatthias Ringwald static void hci_transport_h4_ehcill_wakeup_handler(void){ 483b403028eSMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 484b403028eSMatthias Ringwald log_info("eHCILL: UART wakeup received"); 485b403028eSMatthias Ringwald #endif 486b403028eSMatthias Ringwald hci_transport_h4_ehcill_handle_command(EHCILL_WAKEUP_SIGNAL); 487b403028eSMatthias Ringwald } 488b403028eSMatthias Ringwald 4898a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void){ 4908a23fc53SMatthias Ringwald hci_transport_h4_ehcill_reset_statemachine(); 4918a23fc53SMatthias Ringwald 4928a23fc53SMatthias Ringwald // find best sleep mode to use: wake on CTS, wake on RX, none 4938a23fc53SMatthias Ringwald btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF; 4948a23fc53SMatthias Ringwald int supported_sleep_modes = 0; 4958a23fc53SMatthias Ringwald if (btstack_uart->get_supported_sleep_modes){ 4968a23fc53SMatthias Ringwald supported_sleep_modes = btstack_uart->get_supported_sleep_modes(); 497307a4fe3SMatthias Ringwald } 4988a23fc53SMatthias Ringwald if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){ 4998a23fc53SMatthias Ringwald log_info("eHCILL: using wake on CTS"); 5008a23fc53SMatthias Ringwald btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE; 5018a23fc53SMatthias Ringwald } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){ 5028a23fc53SMatthias Ringwald log_info("eHCILL: using wake on RX"); 5038a23fc53SMatthias Ringwald btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE; 5048a23fc53SMatthias Ringwald } else { 5058a23fc53SMatthias Ringwald log_info("eHCILL: UART driver does not provide compatible sleep mode"); 5068a23fc53SMatthias Ringwald } 507b403028eSMatthias Ringwald if (btstack_uart->set_wakeup_handler){ 508b403028eSMatthias Ringwald btstack_uart->set_wakeup_handler(&hci_transport_h4_ehcill_wakeup_handler); 509b403028eSMatthias Ringwald } 510307a4fe3SMatthias Ringwald } 511307a4fe3SMatthias Ringwald 512307a4fe3SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void){ 5139ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 5149ecd1ba8SMatthias Ringwald log_info("eHCILL: send WAKEUP_IND"); 5159ecd1ba8SMatthias Ringwald #endif 516307a4fe3SMatthias Ringwald // update state 517307a4fe3SMatthias Ringwald tx_state = TX_W4_WAKEUP; 5189ecd1ba8SMatthias Ringwald ehcill_state = EHCILL_STATE_W4_WAKEUP_IND_OR_ACK; 519307a4fe3SMatthias Ringwald ehcill_command_to_send = EHCILL_WAKE_UP_IND; 520307a4fe3SMatthias Ringwald btstack_uart->send_block(&ehcill_command_to_send, 1); 521307a4fe3SMatthias Ringwald } 522307a4fe3SMatthias Ringwald 523307a4fe3SMatthias Ringwald static int hci_transport_h4_ehcill_outgoing_packet_ready(void){ 5242e25934bSMatthias Ringwald return ehcill_tx_len != 0; 525307a4fe3SMatthias Ringwald } 526307a4fe3SMatthias Ringwald 527307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void){ 528307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 529307a4fe3SMatthias Ringwald } 530307a4fe3SMatthias Ringwald 531307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void){ 5329ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 5339ecd1ba8SMatthias Ringwald log_info("eHCILL: send command %02x", ehcill_command_to_send); 5349ecd1ba8SMatthias Ringwald #endif 535307a4fe3SMatthias Ringwald tx_state = TX_W4_EHCILL_SENT; 5369ecd1ba8SMatthias Ringwald if (ehcill_command_to_send == EHCILL_GO_TO_SLEEP_ACK){ 5379ecd1ba8SMatthias Ringwald ehcill_state = EHCILL_STATE_SLEEP; 5389ecd1ba8SMatthias Ringwald } 539307a4fe3SMatthias Ringwald btstack_uart->send_block(&ehcill_command_to_send, 1); 540307a4fe3SMatthias Ringwald } 541307a4fe3SMatthias Ringwald 542307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){ 543b403028eSMatthias Ringwald UNUSED(timer); 5449ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 5459ecd1ba8SMatthias Ringwald log_info("eHCILL: timer triggered"); 5469ecd1ba8SMatthias Ringwald #endif 547307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_send_ehcill_command(); 548307a4fe3SMatthias Ringwald } 549307a4fe3SMatthias Ringwald 550307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){ 551307a4fe3SMatthias Ringwald // setup timer 5529ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 5539ecd1ba8SMatthias Ringwald log_info("eHCILL: set timer for sending command %02x", ehcill_command_to_send); 5549ecd1ba8SMatthias Ringwald #endif 5558a23fc53SMatthias Ringwald btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler); 556307a4fe3SMatthias Ringwald btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50); 557307a4fe3SMatthias Ringwald btstack_run_loop_add_timer(&ehcill_sleep_ack_timer); 558307a4fe3SMatthias Ringwald } 559307a4fe3SMatthias Ringwald 5608a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void){ 5618a23fc53SMatthias Ringwald switch (tx_state){ 5628a23fc53SMatthias Ringwald case TX_W2_EHCILL_SEND: 5638a23fc53SMatthias Ringwald case TX_W4_EHCILL_SENT: 5648a23fc53SMatthias Ringwald // wake up / sleep ack in progress, nothing to do now 5658a23fc53SMatthias Ringwald return; 5668a23fc53SMatthias Ringwald case TX_IDLE: 5678a23fc53SMatthias Ringwald default: 5688a23fc53SMatthias Ringwald // all clear, prepare for wakeup 5698a23fc53SMatthias Ringwald break; 5708a23fc53SMatthias Ringwald } 5718a23fc53SMatthias Ringwald // UART needed again 57262ca45d7SMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(0); 5738a23fc53SMatthias Ringwald if (btstack_uart_sleep_mode){ 5748a23fc53SMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 5758a23fc53SMatthias Ringwald } 5768a23fc53SMatthias Ringwald hci_transport_h4_echill_send_wakeup_ind(); 5778a23fc53SMatthias Ringwald } 5788a23fc53SMatthias Ringwald 5799ecd1ba8SMatthias Ringwald static void hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command){ 5809ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 5819ecd1ba8SMatthias Ringwald log_info("eHCILL: schedule eHCILL command %02x", command); 5829ecd1ba8SMatthias Ringwald #endif 583307a4fe3SMatthias Ringwald ehcill_command_to_send = command; 584307a4fe3SMatthias Ringwald switch (tx_state){ 585307a4fe3SMatthias Ringwald case TX_IDLE: 586307a4fe3SMatthias Ringwald if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){ 587307a4fe3SMatthias Ringwald // send right away 588307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_send_ehcill_command(); 589307a4fe3SMatthias Ringwald } else { 590307a4fe3SMatthias Ringwald // change state so BTstack cannot send and setup timer 591307a4fe3SMatthias Ringwald tx_state = TX_W2_EHCILL_SEND; 592307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_sleep_ack_timer_setup(); 593307a4fe3SMatthias Ringwald } 594307a4fe3SMatthias Ringwald break; 595307a4fe3SMatthias Ringwald default: 596307a4fe3SMatthias Ringwald break; 597307a4fe3SMatthias Ringwald } 598307a4fe3SMatthias Ringwald } 599307a4fe3SMatthias Ringwald 6008a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action){ 601307a4fe3SMatthias Ringwald // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size); 602307a4fe3SMatthias Ringwald switch(ehcill_state){ 603307a4fe3SMatthias Ringwald case EHCILL_STATE_AWAKE: 604307a4fe3SMatthias Ringwald switch(action){ 605307a4fe3SMatthias Ringwald case EHCILL_GO_TO_SLEEP_IND: 6069ecd1ba8SMatthias Ringwald ehcill_state = EHCILL_STATE_W2_SEND_SLEEP_ACK; 6079ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 6089ecd1ba8SMatthias Ringwald log_info("eHCILL: Received GO_TO_SLEEP_IND RX"); 6099ecd1ba8SMatthias Ringwald #endif 6109ecd1ba8SMatthias Ringwald hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_GO_TO_SLEEP_ACK); 611307a4fe3SMatthias Ringwald break; 612307a4fe3SMatthias Ringwald default: 613307a4fe3SMatthias Ringwald break; 614307a4fe3SMatthias Ringwald } 615307a4fe3SMatthias Ringwald break; 616307a4fe3SMatthias Ringwald 6179ecd1ba8SMatthias Ringwald case EHCILL_STATE_W2_SEND_SLEEP_ACK: 618307a4fe3SMatthias Ringwald switch(action){ 619307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_IND: 620307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 62162ca45d7SMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(0); 622b403028eSMatthias Ringwald if (btstack_uart_sleep_mode){ 623b403028eSMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 624b403028eSMatthias Ringwald } 625b403028eSMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 626b403028eSMatthias Ringwald log_info("eHCILL: Received WAKE_UP_IND RX"); 627b403028eSMatthias Ringwald #endif 628b403028eSMatthias Ringwald hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK); 629b403028eSMatthias Ringwald break; 630b403028eSMatthias Ringwald 631b403028eSMatthias Ringwald default: 632b403028eSMatthias Ringwald break; 633b403028eSMatthias Ringwald } 634b403028eSMatthias Ringwald break; 635b403028eSMatthias Ringwald 636b403028eSMatthias Ringwald case EHCILL_STATE_SLEEP: 637b403028eSMatthias Ringwald switch(action){ 638b403028eSMatthias Ringwald case EHCILL_WAKEUP_SIGNAL: 639b403028eSMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(0); 640b403028eSMatthias Ringwald if (btstack_uart_sleep_mode){ 641b403028eSMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 642b403028eSMatthias Ringwald } 643b403028eSMatthias Ringwald break; 644b403028eSMatthias Ringwald case EHCILL_WAKE_UP_IND: 645b403028eSMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 646b403028eSMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(0); 647b403028eSMatthias Ringwald if (btstack_uart_sleep_mode){ 648b403028eSMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 649b403028eSMatthias Ringwald } 6509ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 6519ecd1ba8SMatthias Ringwald log_info("eHCILL: Received WAKE_UP_IND RX"); 6529ecd1ba8SMatthias Ringwald #endif 6539ecd1ba8SMatthias Ringwald hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK); 654307a4fe3SMatthias Ringwald break; 655307a4fe3SMatthias Ringwald 656307a4fe3SMatthias Ringwald default: 657307a4fe3SMatthias Ringwald break; 658307a4fe3SMatthias Ringwald } 659307a4fe3SMatthias Ringwald break; 660307a4fe3SMatthias Ringwald 6619ecd1ba8SMatthias Ringwald case EHCILL_STATE_W4_WAKEUP_IND_OR_ACK: 662307a4fe3SMatthias Ringwald switch(action){ 663307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_IND: 664307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_ACK: 6659ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 6669ecd1ba8SMatthias Ringwald log_info("eHCILL: Received WAKE_UP (%02x)", action); 6679ecd1ba8SMatthias Ringwald #endif 668307a4fe3SMatthias Ringwald tx_state = TX_W4_PACKET_SENT; 669307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 6702e25934bSMatthias Ringwald btstack_uart->send_block(ehcill_tx_data, ehcill_tx_len); 671307a4fe3SMatthias Ringwald break; 672307a4fe3SMatthias Ringwald default: 673307a4fe3SMatthias Ringwald break; 674307a4fe3SMatthias Ringwald } 675307a4fe3SMatthias Ringwald break; 676307a4fe3SMatthias Ringwald } 677307a4fe3SMatthias Ringwald } 6788a23fc53SMatthias Ringwald 6798a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void){ 6809ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 6819ecd1ba8SMatthias Ringwald log_info("eHCILL: handle packet sent, command to send %02x", ehcill_command_to_send); 6829ecd1ba8SMatthias Ringwald #endif 6838a23fc53SMatthias Ringwald // now, send pending ehcill command if neccessary 6848a23fc53SMatthias Ringwald switch (ehcill_command_to_send){ 6858a23fc53SMatthias Ringwald case EHCILL_GO_TO_SLEEP_ACK: 6868a23fc53SMatthias Ringwald hci_transport_h4_ehcill_sleep_ack_timer_setup(); 6878a23fc53SMatthias Ringwald break; 6888a23fc53SMatthias Ringwald case EHCILL_WAKE_UP_IND: 6898a23fc53SMatthias Ringwald hci_transport_h4_ehcill_send_ehcill_command(); 6908a23fc53SMatthias Ringwald break; 6918a23fc53SMatthias Ringwald default: 6928a23fc53SMatthias Ringwald break; 6938a23fc53SMatthias Ringwald } 6948a23fc53SMatthias Ringwald } 6958a23fc53SMatthias Ringwald 6968a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){ 6978a23fc53SMatthias Ringwald tx_state = TX_IDLE; 6988a23fc53SMatthias Ringwald int command = ehcill_command_to_send; 6998a23fc53SMatthias Ringwald ehcill_command_to_send = 0; 7009ecd1ba8SMatthias Ringwald 7019ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 7029ecd1ba8SMatthias Ringwald log_info("eHCILL: handle eHCILL sent, command was %02x", command); 7039ecd1ba8SMatthias Ringwald #endif 7049ecd1ba8SMatthias Ringwald 7058a23fc53SMatthias Ringwald if (command == EHCILL_GO_TO_SLEEP_ACK) { 7069ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 7078a23fc53SMatthias Ringwald log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode"); 7089ecd1ba8SMatthias Ringwald #endif 7098a23fc53SMatthias Ringwald // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent 7108a23fc53SMatthias Ringwald if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){ 7118a23fc53SMatthias Ringwald btstack_uart->set_sleep(btstack_uart_sleep_mode); 7128a23fc53SMatthias Ringwald } 71362ca45d7SMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(1); 7148a23fc53SMatthias Ringwald } 7158a23fc53SMatthias Ringwald // already packet ready? then start wakeup 7168a23fc53SMatthias Ringwald if (hci_transport_h4_ehcill_outgoing_packet_ready()){ 71762ca45d7SMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(0); 718b403028eSMatthias Ringwald if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){ 7198a23fc53SMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 7208a23fc53SMatthias Ringwald } 7219ecd1ba8SMatthias Ringwald if (command != EHCILL_WAKE_UP_IND){ 7228a23fc53SMatthias Ringwald hci_transport_h4_echill_send_wakeup_ind(); 7238a23fc53SMatthias Ringwald } 7248a23fc53SMatthias Ringwald } 7259ecd1ba8SMatthias Ringwald } 7268a23fc53SMatthias Ringwald 727307a4fe3SMatthias Ringwald #endif 728307a4fe3SMatthias Ringwald // --- end of eHCILL implementation --------- 729307a4fe3SMatthias Ringwald 7308334d3d8SMatthias Ringwald 7318334d3d8SMatthias Ringwald // configure and return h4 singleton 732bd021c4eSMatthias Ringwald static const hci_transport_t hci_transport_h4 = { 733bd021c4eSMatthias Ringwald /* const char * name; */ "H4", 734bd021c4eSMatthias Ringwald /* void (*init) (const void *transport_config); */ &hci_transport_h4_init, 735bd021c4eSMatthias Ringwald /* int (*open)(void); */ &hci_transport_h4_open, 736bd021c4eSMatthias Ringwald /* int (*close)(void); */ &hci_transport_h4_close, 737bd021c4eSMatthias Ringwald /* void (*register_packet_handler)(void (*handler)(...); */ &hci_transport_h4_register_packet_handler, 738bd021c4eSMatthias Ringwald /* int (*can_send_packet_now)(uint8_t packet_type); */ &hci_transport_h4_can_send_now, 739bd021c4eSMatthias Ringwald /* int (*send_packet)(...); */ &hci_transport_h4_send_packet, 740bd021c4eSMatthias Ringwald /* int (*set_baudrate)(uint32_t baudrate); */ &hci_transport_h4_set_baudrate, 741bd021c4eSMatthias Ringwald /* void (*reset_link)(void); */ NULL, 742ee752bb8SMatthias Ringwald /* void (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL, 743bd021c4eSMatthias Ringwald }; 744bd021c4eSMatthias Ringwald 745f9bd6dd7SMatthias Ringwald const hci_transport_t * hci_transport_h4_instance_for_uart(const btstack_uart_t * uart_driver){ 746bd021c4eSMatthias Ringwald btstack_uart = uart_driver; 747bd021c4eSMatthias Ringwald return &hci_transport_h4; 748bd021c4eSMatthias Ringwald } 749f9bd6dd7SMatthias Ringwald 750f9bd6dd7SMatthias Ringwald // @deprecated 751f9bd6dd7SMatthias Ringwald const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) { 752f9bd6dd7SMatthias Ringwald btstack_uart = (const btstack_uart_t *) uart_driver; 753f9bd6dd7SMatthias Ringwald return &hci_transport_h4; 754f9bd6dd7SMatthias Ringwald } 755