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 { 109bd021c4eSMatthias Ringwald H4_W4_PACKET_TYPE, 110bd021c4eSMatthias Ringwald H4_W4_EVENT_HEADER, 111bd021c4eSMatthias Ringwald H4_W4_ACL_HEADER, 112bd021c4eSMatthias Ringwald H4_W4_SCO_HEADER, 113bd021c4eSMatthias Ringwald H4_W4_PAYLOAD, 114bd021c4eSMatthias Ringwald } H4_STATE; 115bd021c4eSMatthias Ringwald 116307a4fe3SMatthias Ringwald typedef enum { 117307a4fe3SMatthias Ringwald TX_IDLE = 1, 118307a4fe3SMatthias Ringwald TX_W4_PACKET_SENT, 119f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 120307a4fe3SMatthias Ringwald TX_W4_WAKEUP, 121307a4fe3SMatthias Ringwald TX_W2_EHCILL_SEND, 122307a4fe3SMatthias Ringwald TX_W4_EHCILL_SENT, 123307a4fe3SMatthias Ringwald #endif 124307a4fe3SMatthias Ringwald } TX_STATE; 125307a4fe3SMatthias Ringwald 126bd021c4eSMatthias Ringwald // UART Driver + Config 127bd021c4eSMatthias Ringwald static const btstack_uart_block_t * btstack_uart; 128bd021c4eSMatthias Ringwald static btstack_uart_config_t uart_config; 129bd021c4eSMatthias Ringwald 130307a4fe3SMatthias Ringwald // write state 1318a23fc53SMatthias Ringwald static TX_STATE tx_state; 1322e25934bSMatthias Ringwald #ifdef ENABLE_EHCILL 1332e25934bSMatthias Ringwald static uint8_t * ehcill_tx_data; 1342e25934bSMatthias Ringwald static uint16_t ehcill_tx_len; // 0 == no outgoing packet 1352e25934bSMatthias Ringwald #endif 1368a23fc53SMatthias Ringwald 137307a4fe3SMatthias Ringwald static uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 138bd021c4eSMatthias 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; 143*ea374553SMatthias Ringwald static uint16_t bytes_to_read; 144*ea374553SMatthias 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 190bd021c4eSMatthias Ringwald static void hci_transport_h4_block_read(void){ 191bd021c4eSMatthias Ringwald 192bd021c4eSMatthias Ringwald read_pos += bytes_to_read; 193bd021c4eSMatthias Ringwald 194bd021c4eSMatthias Ringwald switch (h4_state) { 195bd021c4eSMatthias Ringwald case H4_W4_PACKET_TYPE: 196bd021c4eSMatthias Ringwald switch (hci_packet[0]){ 197bd021c4eSMatthias Ringwald case HCI_EVENT_PACKET: 198bd021c4eSMatthias Ringwald bytes_to_read = HCI_EVENT_HEADER_SIZE; 199bd021c4eSMatthias Ringwald h4_state = H4_W4_EVENT_HEADER; 200bd021c4eSMatthias Ringwald break; 201bd021c4eSMatthias Ringwald case HCI_ACL_DATA_PACKET: 202bd021c4eSMatthias Ringwald bytes_to_read = HCI_ACL_HEADER_SIZE; 203bd021c4eSMatthias Ringwald h4_state = H4_W4_ACL_HEADER; 204bd021c4eSMatthias Ringwald break; 205bd021c4eSMatthias Ringwald case HCI_SCO_DATA_PACKET: 206bd021c4eSMatthias Ringwald bytes_to_read = HCI_SCO_HEADER_SIZE; 207bd021c4eSMatthias Ringwald h4_state = H4_W4_SCO_HEADER; 208bd021c4eSMatthias Ringwald break; 209f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 210307a4fe3SMatthias Ringwald case EHCILL_GO_TO_SLEEP_IND: 211307a4fe3SMatthias Ringwald case EHCILL_GO_TO_SLEEP_ACK: 212307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_IND: 213307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_ACK: 2148a23fc53SMatthias Ringwald hci_transport_h4_ehcill_handle_command(hci_packet[0]); 2158a23fc53SMatthias Ringwald hci_transport_h4_reset_statemachine(); 216307a4fe3SMatthias Ringwald break; 217307a4fe3SMatthias Ringwald #endif 218bd021c4eSMatthias Ringwald default: 2198a23fc53SMatthias Ringwald log_error("hci_transport_h4: invalid packet type 0x%02x", hci_packet[0]); 220bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 221bd021c4eSMatthias Ringwald break; 222bd021c4eSMatthias Ringwald } 223bd021c4eSMatthias Ringwald break; 224bd021c4eSMatthias Ringwald 225bd021c4eSMatthias Ringwald case H4_W4_EVENT_HEADER: 226bd021c4eSMatthias Ringwald bytes_to_read = hci_packet[2]; 227e8b81068SMatthias Ringwald // check Event length 228*ea374553SMatthias Ringwald if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_EVENT_HEADER_SIZE)){ 229e8b81068SMatthias 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); 230e8b81068SMatthias Ringwald hci_transport_h4_reset_statemachine(); 231e8b81068SMatthias Ringwald break; 232e8b81068SMatthias Ringwald } 233bd021c4eSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 234bd021c4eSMatthias Ringwald break; 235bd021c4eSMatthias Ringwald 236bd021c4eSMatthias Ringwald case H4_W4_ACL_HEADER: 237bd021c4eSMatthias Ringwald bytes_to_read = little_endian_read_16( hci_packet, 3); 238bd021c4eSMatthias Ringwald // check ACL length 239*ea374553SMatthias Ringwald if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE)){ 240fc6cde64SMatthias 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); 241bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 242bd021c4eSMatthias Ringwald break; 243bd021c4eSMatthias Ringwald } 244bd021c4eSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 245bd021c4eSMatthias Ringwald break; 246bd021c4eSMatthias Ringwald 247bd021c4eSMatthias Ringwald case H4_W4_SCO_HEADER: 248bd021c4eSMatthias Ringwald bytes_to_read = hci_packet[3]; 249e8b81068SMatthias Ringwald // check SCO length 250*ea374553SMatthias Ringwald if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_SCO_HEADER_SIZE)){ 251e8b81068SMatthias 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); 252e8b81068SMatthias Ringwald hci_transport_h4_reset_statemachine(); 253e8b81068SMatthias Ringwald break; 254e8b81068SMatthias Ringwald } 255bd021c4eSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 256bd021c4eSMatthias Ringwald break; 257bd021c4eSMatthias Ringwald 258bd021c4eSMatthias Ringwald case H4_W4_PAYLOAD: 25977291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 2607741d2d0SMatthias Ringwald if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_IDLE 26139e7ee9fSMatthias Ringwald && memcmp(hci_packet, local_version_event_prefix, sizeof(local_version_event_prefix)) == 0){ 26277291b13SMatthias Ringwald #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 26339e7ee9fSMatthias Ringwald if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){ 26439e7ee9fSMatthias Ringwald // detect TI CC256x controller based on manufacturer 26539e7ee9fSMatthias Ringwald log_info("Detected CC256x controller"); 2667741d2d0SMatthias Ringwald baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED; 26739e7ee9fSMatthias Ringwald } else { 26839e7ee9fSMatthias Ringwald // work around not needed 26939e7ee9fSMatthias Ringwald log_info("Bluetooth controller not by TI"); 2707741d2d0SMatthias Ringwald baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE; 27139e7ee9fSMatthias Ringwald } 27277291b13SMatthias Ringwald #endif 27377291b13SMatthias Ringwald #ifdef ENABLE_CYPRESS_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 27477291b13SMatthias Ringwald if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){ 27577291b13SMatthias Ringwald // detect Cypress controller based on manufacturer 27677291b13SMatthias Ringwald log_info("Detected Cypress controller"); 27777291b13SMatthias Ringwald baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED; 27877291b13SMatthias Ringwald } else { 27977291b13SMatthias Ringwald // work around not needed 28077291b13SMatthias Ringwald log_info("Bluetooth controller not by Cypress"); 28177291b13SMatthias Ringwald baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE; 28277291b13SMatthias Ringwald } 28377291b13SMatthias Ringwald #endif 28439e7ee9fSMatthias Ringwald } 28539e7ee9fSMatthias Ringwald #endif 286bd021c4eSMatthias Ringwald packet_handler(hci_packet[0], &hci_packet[1], read_pos-1); 287bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 288bd021c4eSMatthias Ringwald break; 289bd021c4eSMatthias Ringwald default: 290bd021c4eSMatthias Ringwald break; 291bd021c4eSMatthias Ringwald } 29239e7ee9fSMatthias Ringwald 29377291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 2947741d2d0SMatthias Ringwald if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT){ 2957741d2d0SMatthias Ringwald baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_IDLE; 29639e7ee9fSMatthias Ringwald // avoid flowcontrol problem by reading expected hci command complete event of 7 bytes in a single block read 29739e7ee9fSMatthias Ringwald h4_state = H4_W4_PAYLOAD; 29839e7ee9fSMatthias Ringwald bytes_to_read = 7; 29939e7ee9fSMatthias Ringwald } 30039e7ee9fSMatthias Ringwald #endif 30139e7ee9fSMatthias Ringwald 302bd021c4eSMatthias Ringwald hci_transport_h4_trigger_next_read(); 303bd021c4eSMatthias Ringwald } 304bd021c4eSMatthias Ringwald 305307a4fe3SMatthias Ringwald static void hci_transport_h4_block_sent(void){ 306307a4fe3SMatthias Ringwald switch (tx_state){ 307307a4fe3SMatthias Ringwald case TX_W4_PACKET_SENT: 308307a4fe3SMatthias Ringwald // packet fully sent, reset state 3092e25934bSMatthias Ringwald #ifdef ENABLE_EHCILL 3102e25934bSMatthias Ringwald ehcill_tx_len = 0; 3112e25934bSMatthias Ringwald #endif 3128a23fc53SMatthias Ringwald tx_state = TX_IDLE; 313307a4fe3SMatthias Ringwald 314f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 3158a23fc53SMatthias Ringwald // notify eHCILL engine 3168a23fc53SMatthias Ringwald hci_transport_h4_ehcill_handle_packet_sent(); 317307a4fe3SMatthias Ringwald #endif 318307a4fe3SMatthias Ringwald // notify upper stack that it can send again 319307a4fe3SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, &packet_sent_event[0], sizeof(packet_sent_event)); 320307a4fe3SMatthias Ringwald break; 321307a4fe3SMatthias Ringwald 322f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 3238a23fc53SMatthias Ringwald case TX_W4_EHCILL_SENT: 324b403028eSMatthias Ringwald case TX_W4_WAKEUP: 3258a23fc53SMatthias Ringwald hci_transport_h4_ehcill_handle_ehcill_command_sent(); 326307a4fe3SMatthias Ringwald break; 327307a4fe3SMatthias Ringwald #endif 328307a4fe3SMatthias Ringwald 329307a4fe3SMatthias Ringwald default: 330307a4fe3SMatthias Ringwald break; 331307a4fe3SMatthias Ringwald } 332307a4fe3SMatthias Ringwald } 333307a4fe3SMatthias Ringwald 334307a4fe3SMatthias Ringwald static int hci_transport_h4_can_send_now(uint8_t packet_type){ 335307a4fe3SMatthias Ringwald return tx_state == TX_IDLE; 336307a4fe3SMatthias Ringwald } 337307a4fe3SMatthias Ringwald 338307a4fe3SMatthias Ringwald static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 33939e7ee9fSMatthias Ringwald 340307a4fe3SMatthias Ringwald // store packet type before actual data and increase size 341307a4fe3SMatthias Ringwald size++; 342307a4fe3SMatthias Ringwald packet--; 343307a4fe3SMatthias Ringwald *packet = packet_type; 344307a4fe3SMatthias Ringwald 34577291b13SMatthias Ringwald #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 3467741d2d0SMatthias Ringwald if ((baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED) 34739e7ee9fSMatthias Ringwald && (memcmp(packet, baud_rate_command_prefix, sizeof(baud_rate_command_prefix)) == 0)) { 3487741d2d0SMatthias Ringwald log_info("Baud rate command detected, expect command complete event next"); 3497741d2d0SMatthias Ringwald baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT; 35039e7ee9fSMatthias Ringwald } 35139e7ee9fSMatthias Ringwald #endif 35239e7ee9fSMatthias Ringwald 353f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 3542e25934bSMatthias Ringwald // store request for later 3552e25934bSMatthias Ringwald ehcill_tx_len = size; 3562e25934bSMatthias Ringwald ehcill_tx_data = packet; 3579ecd1ba8SMatthias Ringwald switch (ehcill_state){ 3589ecd1ba8SMatthias Ringwald case EHCILL_STATE_SLEEP: 3598a23fc53SMatthias Ringwald hci_transport_h4_ehcill_trigger_wakeup(); 3608a23fc53SMatthias Ringwald return 0; 3619ecd1ba8SMatthias Ringwald case EHCILL_STATE_W2_SEND_SLEEP_ACK: 362b403028eSMatthias Ringwald log_info("eHILL: send next packet, state EHCILL_STATE_W2_SEND_SLEEP_ACK"); 3639ecd1ba8SMatthias Ringwald return 0; 3649ecd1ba8SMatthias Ringwald default: 3659ecd1ba8SMatthias Ringwald break; 3668a23fc53SMatthias Ringwald } 3678a23fc53SMatthias Ringwald #endif 3688a23fc53SMatthias Ringwald 3698a23fc53SMatthias Ringwald // start sending 3708a23fc53SMatthias Ringwald tx_state = TX_W4_PACKET_SENT; 371307a4fe3SMatthias Ringwald btstack_uart->send_block(packet, size); 372307a4fe3SMatthias Ringwald return 0; 373307a4fe3SMatthias Ringwald } 374307a4fe3SMatthias Ringwald 375bd021c4eSMatthias Ringwald static void hci_transport_h4_init(const void * transport_config){ 376bd021c4eSMatthias Ringwald // check for hci_transport_config_uart_t 377bd021c4eSMatthias Ringwald if (!transport_config) { 378bd021c4eSMatthias Ringwald log_error("hci_transport_h4: no config!"); 379bd021c4eSMatthias Ringwald return; 380bd021c4eSMatthias Ringwald } 381bd021c4eSMatthias Ringwald if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) { 382bd021c4eSMatthias Ringwald log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!"); 383bd021c4eSMatthias Ringwald return; 384bd021c4eSMatthias Ringwald } 385bd021c4eSMatthias Ringwald 386bd021c4eSMatthias Ringwald // extract UART config from transport config 387bd021c4eSMatthias Ringwald hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config; 388bd021c4eSMatthias Ringwald uart_config.baudrate = hci_transport_config_uart->baudrate_init; 389bd021c4eSMatthias Ringwald uart_config.flowcontrol = hci_transport_config_uart->flowcontrol; 390bd021c4eSMatthias Ringwald uart_config.device_name = hci_transport_config_uart->device_name; 391bd021c4eSMatthias Ringwald 392bd021c4eSMatthias Ringwald // setup UART driver 393bd021c4eSMatthias Ringwald btstack_uart->init(&uart_config); 394bd021c4eSMatthias Ringwald btstack_uart->set_block_received(&hci_transport_h4_block_read); 395bd021c4eSMatthias Ringwald btstack_uart->set_block_sent(&hci_transport_h4_block_sent); 396bd021c4eSMatthias Ringwald } 397bd021c4eSMatthias Ringwald 398bd021c4eSMatthias Ringwald static int hci_transport_h4_open(void){ 399bd021c4eSMatthias Ringwald int res = btstack_uart->open(); 400bd021c4eSMatthias Ringwald if (res){ 401bd021c4eSMatthias Ringwald return res; 402bd021c4eSMatthias Ringwald } 403bd021c4eSMatthias Ringwald hci_transport_h4_reset_statemachine(); 404bd021c4eSMatthias Ringwald hci_transport_h4_trigger_next_read(); 405307a4fe3SMatthias Ringwald 406307a4fe3SMatthias Ringwald tx_state = TX_IDLE; 407307a4fe3SMatthias Ringwald 408f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 4098a23fc53SMatthias Ringwald hci_transport_h4_ehcill_open(); 410307a4fe3SMatthias Ringwald #endif 411bd021c4eSMatthias Ringwald return 0; 412bd021c4eSMatthias Ringwald } 413bd021c4eSMatthias Ringwald 414bd021c4eSMatthias Ringwald static int hci_transport_h4_close(void){ 415bd021c4eSMatthias Ringwald return btstack_uart->close(); 416bd021c4eSMatthias Ringwald } 417bd021c4eSMatthias Ringwald 418bd021c4eSMatthias Ringwald static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 419bd021c4eSMatthias Ringwald packet_handler = handler; 420bd021c4eSMatthias Ringwald } 421bd021c4eSMatthias Ringwald 422bd021c4eSMatthias Ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 423bd021c4eSMatthias Ringwald } 424bd021c4eSMatthias Ringwald 4258a23fc53SMatthias Ringwald // 426307a4fe3SMatthias Ringwald // --- main part of eHCILL implementation --- 4278a23fc53SMatthias Ringwald // 428307a4fe3SMatthias Ringwald 429f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL 430307a4fe3SMatthias Ringwald 43162ca45d7SMatthias Ringwald static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){ 43262ca45d7SMatthias Ringwald static int last_state = 0; 43362ca45d7SMatthias Ringwald if (sleep_active == last_state) return; 43462ca45d7SMatthias Ringwald last_state = sleep_active; 43562ca45d7SMatthias Ringwald 43662ca45d7SMatthias Ringwald log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active); 43762ca45d7SMatthias Ringwald uint8_t event[3]; 43862ca45d7SMatthias Ringwald event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE; 43962ca45d7SMatthias Ringwald event[1] = sizeof(event) - 2; 44062ca45d7SMatthias Ringwald event[2] = sleep_active; 44162ca45d7SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 44262ca45d7SMatthias Ringwald } 44362ca45d7SMatthias Ringwald 444b403028eSMatthias Ringwald static void hci_transport_h4_ehcill_wakeup_handler(void){ 445b403028eSMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 446b403028eSMatthias Ringwald log_info("eHCILL: UART wakeup received"); 447b403028eSMatthias Ringwald #endif 448b403028eSMatthias Ringwald hci_transport_h4_ehcill_handle_command(EHCILL_WAKEUP_SIGNAL); 449b403028eSMatthias Ringwald } 450b403028eSMatthias Ringwald 4518a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_open(void){ 4528a23fc53SMatthias Ringwald hci_transport_h4_ehcill_reset_statemachine(); 4538a23fc53SMatthias Ringwald 4548a23fc53SMatthias Ringwald // find best sleep mode to use: wake on CTS, wake on RX, none 4558a23fc53SMatthias Ringwald btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF; 4568a23fc53SMatthias Ringwald int supported_sleep_modes = 0; 4578a23fc53SMatthias Ringwald if (btstack_uart->get_supported_sleep_modes){ 4588a23fc53SMatthias Ringwald supported_sleep_modes = btstack_uart->get_supported_sleep_modes(); 459307a4fe3SMatthias Ringwald } 4608a23fc53SMatthias Ringwald if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){ 4618a23fc53SMatthias Ringwald log_info("eHCILL: using wake on CTS"); 4628a23fc53SMatthias Ringwald btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE; 4638a23fc53SMatthias Ringwald } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){ 4648a23fc53SMatthias Ringwald log_info("eHCILL: using wake on RX"); 4658a23fc53SMatthias Ringwald btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE; 4668a23fc53SMatthias Ringwald } else { 4678a23fc53SMatthias Ringwald log_info("eHCILL: UART driver does not provide compatible sleep mode"); 4688a23fc53SMatthias Ringwald } 469b403028eSMatthias Ringwald if (btstack_uart->set_wakeup_handler){ 470b403028eSMatthias Ringwald btstack_uart->set_wakeup_handler(&hci_transport_h4_ehcill_wakeup_handler); 471b403028eSMatthias Ringwald } 472307a4fe3SMatthias Ringwald } 473307a4fe3SMatthias Ringwald 474307a4fe3SMatthias Ringwald static void hci_transport_h4_echill_send_wakeup_ind(void){ 4759ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 4769ecd1ba8SMatthias Ringwald log_info("eHCILL: send WAKEUP_IND"); 4779ecd1ba8SMatthias Ringwald #endif 478307a4fe3SMatthias Ringwald // update state 479307a4fe3SMatthias Ringwald tx_state = TX_W4_WAKEUP; 4809ecd1ba8SMatthias Ringwald ehcill_state = EHCILL_STATE_W4_WAKEUP_IND_OR_ACK; 481307a4fe3SMatthias Ringwald ehcill_command_to_send = EHCILL_WAKE_UP_IND; 482307a4fe3SMatthias Ringwald btstack_uart->send_block(&ehcill_command_to_send, 1); 483307a4fe3SMatthias Ringwald } 484307a4fe3SMatthias Ringwald 485307a4fe3SMatthias Ringwald static int hci_transport_h4_ehcill_outgoing_packet_ready(void){ 4862e25934bSMatthias Ringwald return ehcill_tx_len != 0; 487307a4fe3SMatthias Ringwald } 488307a4fe3SMatthias Ringwald 489307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_reset_statemachine(void){ 490307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 491307a4fe3SMatthias Ringwald } 492307a4fe3SMatthias Ringwald 493307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_send_ehcill_command(void){ 4949ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 4959ecd1ba8SMatthias Ringwald log_info("eHCILL: send command %02x", ehcill_command_to_send); 4969ecd1ba8SMatthias Ringwald #endif 497307a4fe3SMatthias Ringwald tx_state = TX_W4_EHCILL_SENT; 4989ecd1ba8SMatthias Ringwald if (ehcill_command_to_send == EHCILL_GO_TO_SLEEP_ACK){ 4999ecd1ba8SMatthias Ringwald ehcill_state = EHCILL_STATE_SLEEP; 5009ecd1ba8SMatthias Ringwald } 501307a4fe3SMatthias Ringwald btstack_uart->send_block(&ehcill_command_to_send, 1); 502307a4fe3SMatthias Ringwald } 503307a4fe3SMatthias Ringwald 504307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){ 505b403028eSMatthias Ringwald UNUSED(timer); 5069ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 5079ecd1ba8SMatthias Ringwald log_info("eHCILL: timer triggered"); 5089ecd1ba8SMatthias Ringwald #endif 509307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_send_ehcill_command(); 510307a4fe3SMatthias Ringwald } 511307a4fe3SMatthias Ringwald 512307a4fe3SMatthias Ringwald static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){ 513307a4fe3SMatthias Ringwald // setup timer 5149ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 5159ecd1ba8SMatthias Ringwald log_info("eHCILL: set timer for sending command %02x", ehcill_command_to_send); 5169ecd1ba8SMatthias Ringwald #endif 5178a23fc53SMatthias Ringwald btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler); 518307a4fe3SMatthias Ringwald btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50); 519307a4fe3SMatthias Ringwald btstack_run_loop_add_timer(&ehcill_sleep_ack_timer); 520307a4fe3SMatthias Ringwald } 521307a4fe3SMatthias Ringwald 5228a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_trigger_wakeup(void){ 5238a23fc53SMatthias Ringwald switch (tx_state){ 5248a23fc53SMatthias Ringwald case TX_W2_EHCILL_SEND: 5258a23fc53SMatthias Ringwald case TX_W4_EHCILL_SENT: 5268a23fc53SMatthias Ringwald // wake up / sleep ack in progress, nothing to do now 5278a23fc53SMatthias Ringwald return; 5288a23fc53SMatthias Ringwald case TX_IDLE: 5298a23fc53SMatthias Ringwald default: 5308a23fc53SMatthias Ringwald // all clear, prepare for wakeup 5318a23fc53SMatthias Ringwald break; 5328a23fc53SMatthias Ringwald } 5338a23fc53SMatthias Ringwald // UART needed again 53462ca45d7SMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(0); 5358a23fc53SMatthias Ringwald if (btstack_uart_sleep_mode){ 5368a23fc53SMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 5378a23fc53SMatthias Ringwald } 5388a23fc53SMatthias Ringwald hci_transport_h4_echill_send_wakeup_ind(); 5398a23fc53SMatthias Ringwald } 5408a23fc53SMatthias Ringwald 5419ecd1ba8SMatthias Ringwald static void hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command){ 5429ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 5439ecd1ba8SMatthias Ringwald log_info("eHCILL: schedule eHCILL command %02x", command); 5449ecd1ba8SMatthias Ringwald #endif 545307a4fe3SMatthias Ringwald ehcill_command_to_send = command; 546307a4fe3SMatthias Ringwald switch (tx_state){ 547307a4fe3SMatthias Ringwald case TX_IDLE: 548307a4fe3SMatthias Ringwald if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){ 549307a4fe3SMatthias Ringwald // send right away 550307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_send_ehcill_command(); 551307a4fe3SMatthias Ringwald } else { 552307a4fe3SMatthias Ringwald // change state so BTstack cannot send and setup timer 553307a4fe3SMatthias Ringwald tx_state = TX_W2_EHCILL_SEND; 554307a4fe3SMatthias Ringwald hci_transport_h4_ehcill_sleep_ack_timer_setup(); 555307a4fe3SMatthias Ringwald } 556307a4fe3SMatthias Ringwald break; 557307a4fe3SMatthias Ringwald default: 558307a4fe3SMatthias Ringwald break; 559307a4fe3SMatthias Ringwald } 560307a4fe3SMatthias Ringwald } 561307a4fe3SMatthias Ringwald 5628a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_command(uint8_t action){ 563307a4fe3SMatthias Ringwald // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size); 564307a4fe3SMatthias Ringwald switch(ehcill_state){ 565307a4fe3SMatthias Ringwald case EHCILL_STATE_AWAKE: 566307a4fe3SMatthias Ringwald switch(action){ 567307a4fe3SMatthias Ringwald case EHCILL_GO_TO_SLEEP_IND: 5689ecd1ba8SMatthias Ringwald ehcill_state = EHCILL_STATE_W2_SEND_SLEEP_ACK; 5699ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 5709ecd1ba8SMatthias Ringwald log_info("eHCILL: Received GO_TO_SLEEP_IND RX"); 5719ecd1ba8SMatthias Ringwald #endif 5729ecd1ba8SMatthias Ringwald hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_GO_TO_SLEEP_ACK); 573307a4fe3SMatthias Ringwald break; 574307a4fe3SMatthias Ringwald default: 575307a4fe3SMatthias Ringwald break; 576307a4fe3SMatthias Ringwald } 577307a4fe3SMatthias Ringwald break; 578307a4fe3SMatthias Ringwald 5799ecd1ba8SMatthias Ringwald case EHCILL_STATE_W2_SEND_SLEEP_ACK: 580307a4fe3SMatthias Ringwald switch(action){ 581307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_IND: 582307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 58362ca45d7SMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(0); 584b403028eSMatthias Ringwald if (btstack_uart_sleep_mode){ 585b403028eSMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 586b403028eSMatthias Ringwald } 587b403028eSMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 588b403028eSMatthias Ringwald log_info("eHCILL: Received WAKE_UP_IND RX"); 589b403028eSMatthias Ringwald #endif 590b403028eSMatthias Ringwald hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK); 591b403028eSMatthias Ringwald break; 592b403028eSMatthias Ringwald 593b403028eSMatthias Ringwald default: 594b403028eSMatthias Ringwald break; 595b403028eSMatthias Ringwald } 596b403028eSMatthias Ringwald break; 597b403028eSMatthias Ringwald 598b403028eSMatthias Ringwald case EHCILL_STATE_SLEEP: 599b403028eSMatthias Ringwald switch(action){ 600b403028eSMatthias Ringwald case EHCILL_WAKEUP_SIGNAL: 601b403028eSMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(0); 602b403028eSMatthias Ringwald if (btstack_uart_sleep_mode){ 603b403028eSMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 604b403028eSMatthias Ringwald } 605b403028eSMatthias Ringwald break; 606b403028eSMatthias Ringwald case EHCILL_WAKE_UP_IND: 607b403028eSMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 608b403028eSMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(0); 609b403028eSMatthias Ringwald if (btstack_uart_sleep_mode){ 610b403028eSMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 611b403028eSMatthias Ringwald } 6129ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 6139ecd1ba8SMatthias Ringwald log_info("eHCILL: Received WAKE_UP_IND RX"); 6149ecd1ba8SMatthias Ringwald #endif 6159ecd1ba8SMatthias Ringwald hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK); 616307a4fe3SMatthias Ringwald break; 617307a4fe3SMatthias Ringwald 618307a4fe3SMatthias Ringwald default: 619307a4fe3SMatthias Ringwald break; 620307a4fe3SMatthias Ringwald } 621307a4fe3SMatthias Ringwald break; 622307a4fe3SMatthias Ringwald 6239ecd1ba8SMatthias Ringwald case EHCILL_STATE_W4_WAKEUP_IND_OR_ACK: 624307a4fe3SMatthias Ringwald switch(action){ 625307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_IND: 626307a4fe3SMatthias Ringwald case EHCILL_WAKE_UP_ACK: 6279ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 6289ecd1ba8SMatthias Ringwald log_info("eHCILL: Received WAKE_UP (%02x)", action); 6299ecd1ba8SMatthias Ringwald #endif 630307a4fe3SMatthias Ringwald tx_state = TX_W4_PACKET_SENT; 631307a4fe3SMatthias Ringwald ehcill_state = EHCILL_STATE_AWAKE; 6322e25934bSMatthias Ringwald btstack_uart->send_block(ehcill_tx_data, ehcill_tx_len); 633307a4fe3SMatthias Ringwald break; 634307a4fe3SMatthias Ringwald default: 635307a4fe3SMatthias Ringwald break; 636307a4fe3SMatthias Ringwald } 637307a4fe3SMatthias Ringwald break; 638307a4fe3SMatthias Ringwald } 639307a4fe3SMatthias Ringwald } 6408a23fc53SMatthias Ringwald 6418a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_packet_sent(void){ 6429ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 6439ecd1ba8SMatthias Ringwald log_info("eHCILL: handle packet sent, command to send %02x", ehcill_command_to_send); 6449ecd1ba8SMatthias Ringwald #endif 6458a23fc53SMatthias Ringwald // now, send pending ehcill command if neccessary 6468a23fc53SMatthias Ringwald switch (ehcill_command_to_send){ 6478a23fc53SMatthias Ringwald case EHCILL_GO_TO_SLEEP_ACK: 6488a23fc53SMatthias Ringwald hci_transport_h4_ehcill_sleep_ack_timer_setup(); 6498a23fc53SMatthias Ringwald break; 6508a23fc53SMatthias Ringwald case EHCILL_WAKE_UP_IND: 6518a23fc53SMatthias Ringwald hci_transport_h4_ehcill_send_ehcill_command(); 6528a23fc53SMatthias Ringwald break; 6538a23fc53SMatthias Ringwald default: 6548a23fc53SMatthias Ringwald break; 6558a23fc53SMatthias Ringwald } 6568a23fc53SMatthias Ringwald } 6578a23fc53SMatthias Ringwald 6588a23fc53SMatthias Ringwald static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){ 6598a23fc53SMatthias Ringwald tx_state = TX_IDLE; 6608a23fc53SMatthias Ringwald int command = ehcill_command_to_send; 6618a23fc53SMatthias Ringwald ehcill_command_to_send = 0; 6629ecd1ba8SMatthias Ringwald 6639ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 6649ecd1ba8SMatthias Ringwald log_info("eHCILL: handle eHCILL sent, command was %02x", command); 6659ecd1ba8SMatthias Ringwald #endif 6669ecd1ba8SMatthias Ringwald 6678a23fc53SMatthias Ringwald if (command == EHCILL_GO_TO_SLEEP_ACK) { 6689ecd1ba8SMatthias Ringwald #ifdef ENABLE_LOG_EHCILL 6698a23fc53SMatthias Ringwald log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode"); 6709ecd1ba8SMatthias Ringwald #endif 6718a23fc53SMatthias Ringwald // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent 6728a23fc53SMatthias Ringwald if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){ 6738a23fc53SMatthias Ringwald btstack_uart->set_sleep(btstack_uart_sleep_mode); 6748a23fc53SMatthias Ringwald } 67562ca45d7SMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(1); 6768a23fc53SMatthias Ringwald } 6778a23fc53SMatthias Ringwald // already packet ready? then start wakeup 6788a23fc53SMatthias Ringwald if (hci_transport_h4_ehcill_outgoing_packet_ready()){ 67962ca45d7SMatthias Ringwald hci_transport_h4_ehcill_emit_sleep_state(0); 680b403028eSMatthias Ringwald if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){ 6818a23fc53SMatthias Ringwald btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 6828a23fc53SMatthias Ringwald } 6839ecd1ba8SMatthias Ringwald if (command != EHCILL_WAKE_UP_IND){ 6848a23fc53SMatthias Ringwald hci_transport_h4_echill_send_wakeup_ind(); 6858a23fc53SMatthias Ringwald } 6868a23fc53SMatthias Ringwald } 6879ecd1ba8SMatthias Ringwald } 6888a23fc53SMatthias Ringwald 689307a4fe3SMatthias Ringwald #endif 690307a4fe3SMatthias Ringwald // --- end of eHCILL implementation --------- 691307a4fe3SMatthias Ringwald 692bd021c4eSMatthias Ringwald static const hci_transport_t hci_transport_h4 = { 693bd021c4eSMatthias Ringwald /* const char * name; */ "H4", 694bd021c4eSMatthias Ringwald /* void (*init) (const void *transport_config); */ &hci_transport_h4_init, 695bd021c4eSMatthias Ringwald /* int (*open)(void); */ &hci_transport_h4_open, 696bd021c4eSMatthias Ringwald /* int (*close)(void); */ &hci_transport_h4_close, 697bd021c4eSMatthias Ringwald /* void (*register_packet_handler)(void (*handler)(...); */ &hci_transport_h4_register_packet_handler, 698bd021c4eSMatthias Ringwald /* int (*can_send_packet_now)(uint8_t packet_type); */ &hci_transport_h4_can_send_now, 699bd021c4eSMatthias Ringwald /* int (*send_packet)(...); */ &hci_transport_h4_send_packet, 700bd021c4eSMatthias Ringwald /* int (*set_baudrate)(uint32_t baudrate); */ &hci_transport_h4_set_baudrate, 701bd021c4eSMatthias Ringwald /* void (*reset_link)(void); */ NULL, 702ee752bb8SMatthias Ringwald /* void (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL, 703bd021c4eSMatthias Ringwald }; 704bd021c4eSMatthias Ringwald 705bd021c4eSMatthias Ringwald // configure and return h4 singleton 706bd021c4eSMatthias Ringwald const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) { 707bd021c4eSMatthias Ringwald btstack_uart = uart_driver; 708bd021c4eSMatthias Ringwald return &hci_transport_h4; 709bd021c4eSMatthias Ringwald } 710