1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "hci_transport_h4.c" 39 40 /* 41 * hci_h4_transport.c 42 * 43 * HCI Transport API implementation for H4 protocol over POSIX with optional support for eHCILL 44 * 45 * Created by Matthias Ringwald on 4/29/09. 46 */ 47 48 49 #include "btstack_config.h" 50 #include "hci_transport_h4.h" 51 52 #include "btstack_debug.h" 53 #include "hci.h" 54 #include "hci_transport.h" 55 #include "bluetooth_company_id.h" 56 #include "btstack_uart_block.h" 57 58 #include <inttypes.h> 59 60 #define ENABLE_LOG_EHCILL 61 62 #ifdef ENABLE_EHCILL 63 64 // eHCILL commands 65 enum EHCILL_MESSAGES { 66 EHCILL_GO_TO_SLEEP_IND = 0x030, 67 EHCILL_GO_TO_SLEEP_ACK = 0x031, 68 EHCILL_WAKE_UP_IND = 0x032, 69 EHCILL_WAKE_UP_ACK = 0x033, 70 EHCILL_WAKEUP_SIGNAL = 0x034, 71 }; 72 73 static int hci_transport_h4_ehcill_outgoing_packet_ready(void); 74 static void hci_transport_h4_echill_send_wakeup_ind(void); 75 static void hci_transport_h4_ehcill_handle_command(uint8_t action); 76 static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void); 77 static void hci_transport_h4_ehcill_handle_packet_sent(void); 78 static void hci_transport_h4_ehcill_open(void); 79 static void hci_transport_h4_ehcill_reset_statemachine(void); 80 static void hci_transport_h4_ehcill_send_ehcill_command(void); 81 static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void); 82 static void hci_transport_h4_ehcill_trigger_wakeup(void); 83 84 typedef enum { 85 EHCILL_STATE_W2_SEND_SLEEP_ACK, 86 EHCILL_STATE_SLEEP, 87 EHCILL_STATE_W4_WAKEUP_IND_OR_ACK, 88 EHCILL_STATE_AWAKE 89 } EHCILL_STATE; 90 91 // eHCILL state machine 92 static EHCILL_STATE ehcill_state; 93 static uint8_t ehcill_command_to_send; 94 95 static btstack_uart_sleep_mode_t btstack_uart_sleep_mode; 96 97 // work around for eHCILL problem 98 static btstack_timer_source_t ehcill_sleep_ack_timer; 99 100 #endif 101 102 103 // assert pre-buffer for packet type is available 104 #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0) 105 #error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h 106 #endif 107 108 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 109 110 typedef enum { 111 H4_OFF, 112 H4_W4_PACKET_TYPE, 113 H4_W4_EVENT_HEADER, 114 H4_W4_ACL_HEADER, 115 H4_W4_SCO_HEADER, 116 H4_W4_ISO_HEADER, 117 H4_W4_PAYLOAD, 118 } H4_STATE; 119 120 typedef enum { 121 TX_OFF, 122 TX_IDLE, 123 TX_W4_PACKET_SENT, 124 #ifdef ENABLE_EHCILL 125 TX_W4_WAKEUP, 126 TX_W2_EHCILL_SEND, 127 TX_W4_EHCILL_SENT, 128 #endif 129 } TX_STATE; 130 131 // UART Driver + Config 132 static const btstack_uart_t * btstack_uart; 133 static btstack_uart_config_t hci_transport_h4_uart_config; 134 135 // write state 136 static TX_STATE tx_state; 137 #ifdef ENABLE_EHCILL 138 static uint8_t * ehcill_tx_data; 139 static uint16_t ehcill_tx_len; // 0 == no outgoing packet 140 #endif 141 142 static void (*hci_transport_h4_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler; 143 144 // packet reader state machine 145 static H4_STATE h4_state; 146 static uint16_t bytes_to_read; 147 static uint16_t read_pos; 148 149 // incoming packet buffer 150 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) 151 static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE]; 152 153 // Baudrate change bugs in TI CC256x and CYW20704 154 #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 155 #define ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 156 static const uint8_t baud_rate_command_prefix[] = { 0x01, 0x36, 0xff, 0x04}; 157 #endif 158 159 #ifdef ENABLE_CYPRESS_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 160 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 161 #error "Please enable either ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND or ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND" 162 #endif 163 #define ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 164 static const uint8_t baud_rate_command_prefix[] = { 0x01, 0x18, 0xfc, 0x06}; 165 #endif 166 167 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 168 static const uint8_t local_version_event_prefix[] = { 0x04, 0x0e, 0x0c, 0x01, 0x01, 0x10}; 169 static enum { 170 BAUDRATE_CHANGE_WORKAROUND_IDLE, 171 BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED, 172 BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT, 173 BAUDRATE_CHANGE_WORKAROUND_DONE 174 } baudrate_change_workaround_state; 175 #endif 176 177 static int hci_transport_h4_set_baudrate(uint32_t baudrate){ 178 log_info("hci_transport_h4_set_baudrate %"PRIu32, baudrate); 179 return btstack_uart->set_baudrate(baudrate); 180 } 181 182 static void hci_transport_h4_reset_statemachine(void){ 183 h4_state = H4_W4_PACKET_TYPE; 184 read_pos = 0; 185 bytes_to_read = 1; 186 } 187 188 static void hci_transport_h4_trigger_next_read(void){ 189 // log_info("hci_transport_h4_trigger_next_read: %u bytes", bytes_to_read); 190 btstack_uart->receive_block(&hci_packet[read_pos], bytes_to_read); 191 } 192 193 static void hci_transport_h4_packet_complete(void){ 194 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 195 if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_IDLE 196 && memcmp(hci_packet, local_version_event_prefix, sizeof(local_version_event_prefix)) == 0){ 197 #ifdef ENABLE_CC256X_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 198 if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){ 199 // detect TI CC256x controller based on manufacturer 200 log_info("Detected CC256x controller"); 201 baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED; 202 } else { 203 // work around not needed 204 log_info("Bluetooth controller not by TI"); 205 baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE; 206 } 207 #endif 208 #ifdef ENABLE_CYPRESS_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 209 if (little_endian_read_16(hci_packet, 11) == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){ 210 // detect Cypress controller based on manufacturer 211 log_info("Detected Cypress controller"); 212 baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED; 213 } else { 214 // work around not needed 215 log_info("Bluetooth controller not by Cypress"); 216 baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_DONE; 217 } 218 #endif 219 } 220 #endif 221 uint16_t packet_len = read_pos-1u; 222 223 // reset state machine before delivering packet to stack as it might close the transport 224 hci_transport_h4_reset_statemachine(); 225 hci_transport_h4_packet_handler(hci_packet[0], &hci_packet[1], packet_len); 226 } 227 228 static void hci_transport_h4_block_read(void){ 229 230 read_pos += bytes_to_read; 231 232 switch (h4_state) { 233 case H4_W4_PACKET_TYPE: 234 switch (hci_packet[0]){ 235 case HCI_EVENT_PACKET: 236 bytes_to_read = HCI_EVENT_HEADER_SIZE; 237 h4_state = H4_W4_EVENT_HEADER; 238 break; 239 case HCI_ACL_DATA_PACKET: 240 bytes_to_read = HCI_ACL_HEADER_SIZE; 241 h4_state = H4_W4_ACL_HEADER; 242 break; 243 case HCI_SCO_DATA_PACKET: 244 bytes_to_read = HCI_SCO_HEADER_SIZE; 245 h4_state = H4_W4_SCO_HEADER; 246 break; 247 case HCI_ISO_DATA_PACKET: 248 bytes_to_read = HCI_ISO_HEADER_SIZE; 249 h4_state = H4_W4_SCO_HEADER; 250 break; 251 #ifdef ENABLE_EHCILL 252 case EHCILL_GO_TO_SLEEP_IND: 253 case EHCILL_GO_TO_SLEEP_ACK: 254 case EHCILL_WAKE_UP_IND: 255 case EHCILL_WAKE_UP_ACK: 256 hci_transport_h4_ehcill_handle_command(hci_packet[0]); 257 hci_transport_h4_reset_statemachine(); 258 break; 259 #endif 260 default: 261 log_error("hci_transport_h4: invalid packet type 0x%02x", hci_packet[0]); 262 hci_transport_h4_reset_statemachine(); 263 break; 264 } 265 break; 266 267 case H4_W4_EVENT_HEADER: 268 bytes_to_read = hci_packet[2]; 269 // check Event length 270 if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_EVENT_HEADER_SIZE)){ 271 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); 272 hci_transport_h4_reset_statemachine(); 273 break; 274 } 275 h4_state = H4_W4_PAYLOAD; 276 break; 277 278 case H4_W4_ACL_HEADER: 279 bytes_to_read = little_endian_read_16( hci_packet, 3); 280 // check ACL length 281 if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE)){ 282 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); 283 hci_transport_h4_reset_statemachine(); 284 break; 285 } 286 h4_state = H4_W4_PAYLOAD; 287 break; 288 289 case H4_W4_SCO_HEADER: 290 bytes_to_read = hci_packet[3]; 291 // check SCO length 292 if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_SCO_HEADER_SIZE)){ 293 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); 294 hci_transport_h4_reset_statemachine(); 295 break; 296 } 297 h4_state = H4_W4_PAYLOAD; 298 break; 299 300 case H4_W4_ISO_HEADER: 301 bytes_to_read = little_endian_read_16( hci_packet, 3) & 0x3fff; 302 // check ISO length 303 if (bytes_to_read > (HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE)){ 304 log_error("hci_transport_h4: invalid ISO payload len %d - only space for %u", bytes_to_read, HCI_INCOMING_PACKET_BUFFER_SIZE - HCI_ISO_HEADER_SIZE); 305 hci_transport_h4_reset_statemachine(); 306 break; 307 } 308 h4_state = H4_W4_PAYLOAD; 309 break; 310 311 case H4_W4_PAYLOAD: 312 hci_transport_h4_packet_complete(); 313 break; 314 315 case H4_OFF: 316 bytes_to_read = 0; 317 break; 318 default: 319 btstack_assert(false); 320 break; 321 } 322 323 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 324 if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT){ 325 baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_IDLE; 326 // avoid flowcontrol problem by reading expected hci command complete event of 7 bytes in a single block read 327 h4_state = H4_W4_PAYLOAD; 328 bytes_to_read = 7; 329 } 330 #endif 331 332 // forward packet if payload size == 0 333 if (h4_state == H4_W4_PAYLOAD && bytes_to_read == 0u) { 334 hci_transport_h4_packet_complete(); 335 } 336 337 if (h4_state != H4_OFF) { 338 hci_transport_h4_trigger_next_read(); 339 } 340 } 341 342 static void hci_transport_h4_block_sent(void){ 343 344 static const uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 345 346 switch (tx_state){ 347 case TX_W4_PACKET_SENT: 348 // packet fully sent, reset state 349 #ifdef ENABLE_EHCILL 350 ehcill_tx_len = 0; 351 #endif 352 tx_state = TX_IDLE; 353 354 #ifdef ENABLE_EHCILL 355 // notify eHCILL engine 356 hci_transport_h4_ehcill_handle_packet_sent(); 357 #endif 358 // notify upper stack that it can send again 359 hci_transport_h4_packet_handler(HCI_EVENT_PACKET, (uint8_t *) &packet_sent_event[0], sizeof(packet_sent_event)); 360 break; 361 362 #ifdef ENABLE_EHCILL 363 case TX_W4_EHCILL_SENT: 364 case TX_W4_WAKEUP: 365 hci_transport_h4_ehcill_handle_ehcill_command_sent(); 366 break; 367 #endif 368 369 default: 370 break; 371 } 372 } 373 374 static int hci_transport_h4_can_send_now(uint8_t packet_type){ 375 UNUSED(packet_type); 376 return tx_state == TX_IDLE; 377 } 378 379 static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 380 381 // store packet type before actual data and increase size 382 uint8_t * buffer = &packet[-1]; 383 uint32_t buffer_size = size + 1; 384 buffer[0] = packet_type; 385 386 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 387 if ((baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED) 388 && (memcmp(buffer, baud_rate_command_prefix, sizeof(baud_rate_command_prefix)) == 0)) { 389 log_info("Baud rate command detected, expect command complete event next"); 390 baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT; 391 } 392 #endif 393 394 #ifdef ENABLE_EHCILL 395 // store request for later 396 ehcill_tx_len = buffer_size; 397 ehcill_tx_data = buffer; 398 switch (ehcill_state){ 399 case EHCILL_STATE_SLEEP: 400 hci_transport_h4_ehcill_trigger_wakeup(); 401 return 0; 402 case EHCILL_STATE_W2_SEND_SLEEP_ACK: 403 log_info("eHILL: send next packet, state EHCILL_STATE_W2_SEND_SLEEP_ACK"); 404 return 0; 405 default: 406 break; 407 } 408 #endif 409 410 // start sending 411 tx_state = TX_W4_PACKET_SENT; 412 btstack_uart->send_block(buffer, buffer_size); 413 return 0; 414 } 415 416 static void hci_transport_h4_init(const void * transport_config){ 417 // check for hci_transport_config_uart_t 418 if (!transport_config) { 419 log_error("hci_transport_h4: no config!"); 420 return; 421 } 422 if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) { 423 log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!"); 424 return; 425 } 426 427 // extract UART config from transport config 428 hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config; 429 hci_transport_h4_uart_config.baudrate = hci_transport_config_uart->baudrate_init; 430 hci_transport_h4_uart_config.flowcontrol = hci_transport_config_uart->flowcontrol; 431 hci_transport_h4_uart_config.parity = hci_transport_config_uart->parity; 432 hci_transport_h4_uart_config.device_name = hci_transport_config_uart->device_name; 433 434 // set state to off 435 tx_state = TX_OFF; 436 h4_state = H4_OFF; 437 438 // setup UART driver 439 btstack_uart->init(&hci_transport_h4_uart_config); 440 btstack_uart->set_block_received(&hci_transport_h4_block_read); 441 btstack_uart->set_block_sent(&hci_transport_h4_block_sent); 442 } 443 444 static int hci_transport_h4_open(void){ 445 // open uart driver 446 int res = btstack_uart->open(); 447 if (res != 0){ 448 return res; 449 } 450 451 // init rx + tx state machines 452 hci_transport_h4_reset_statemachine(); 453 hci_transport_h4_trigger_next_read(); 454 tx_state = TX_IDLE; 455 456 #ifdef ENABLE_EHCILL 457 hci_transport_h4_ehcill_open(); 458 #endif 459 return 0; 460 } 461 462 static int hci_transport_h4_close(void){ 463 // set state to off 464 tx_state = TX_OFF; 465 h4_state = H4_OFF; 466 467 // close uart driver 468 return btstack_uart->close(); 469 } 470 471 static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 472 hci_transport_h4_packet_handler = handler; 473 } 474 475 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 476 UNUSED(packet_type); 477 UNUSED(packet); 478 UNUSED(size); 479 } 480 481 // 482 // --- main part of eHCILL implementation --- 483 // 484 485 #ifdef ENABLE_EHCILL 486 487 static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){ 488 static int last_state = 0; 489 if (sleep_active == last_state) return; 490 last_state = sleep_active; 491 492 log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active); 493 uint8_t event[3]; 494 event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE; 495 event[1] = sizeof(event) - 2; 496 event[2] = sleep_active; 497 hci_transport_h4_packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 498 } 499 500 static void hci_transport_h4_ehcill_wakeup_handler(void){ 501 #ifdef ENABLE_LOG_EHCILL 502 log_info("eHCILL: UART wakeup received"); 503 #endif 504 hci_transport_h4_ehcill_handle_command(EHCILL_WAKEUP_SIGNAL); 505 } 506 507 static void hci_transport_h4_ehcill_open(void){ 508 hci_transport_h4_ehcill_reset_statemachine(); 509 510 // find best sleep mode to use: wake on CTS, wake on RX, none 511 btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF; 512 int supported_sleep_modes = 0; 513 if (btstack_uart->get_supported_sleep_modes){ 514 supported_sleep_modes = btstack_uart->get_supported_sleep_modes(); 515 } 516 if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){ 517 log_info("eHCILL: using wake on CTS"); 518 btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE; 519 } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){ 520 log_info("eHCILL: using wake on RX"); 521 btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE; 522 } else { 523 log_info("eHCILL: UART driver does not provide compatible sleep mode"); 524 } 525 if (btstack_uart->set_wakeup_handler){ 526 btstack_uart->set_wakeup_handler(&hci_transport_h4_ehcill_wakeup_handler); 527 } 528 } 529 530 static void hci_transport_h4_echill_send_wakeup_ind(void){ 531 #ifdef ENABLE_LOG_EHCILL 532 log_info("eHCILL: send WAKEUP_IND"); 533 #endif 534 // update state 535 tx_state = TX_W4_WAKEUP; 536 ehcill_state = EHCILL_STATE_W4_WAKEUP_IND_OR_ACK; 537 ehcill_command_to_send = EHCILL_WAKE_UP_IND; 538 btstack_uart->send_block(&ehcill_command_to_send, 1); 539 } 540 541 static int hci_transport_h4_ehcill_outgoing_packet_ready(void){ 542 return ehcill_tx_len != 0; 543 } 544 545 static void hci_transport_h4_ehcill_reset_statemachine(void){ 546 ehcill_state = EHCILL_STATE_AWAKE; 547 } 548 549 static void hci_transport_h4_ehcill_send_ehcill_command(void){ 550 #ifdef ENABLE_LOG_EHCILL 551 log_info("eHCILL: send command %02x", ehcill_command_to_send); 552 #endif 553 tx_state = TX_W4_EHCILL_SENT; 554 if (ehcill_command_to_send == EHCILL_GO_TO_SLEEP_ACK){ 555 ehcill_state = EHCILL_STATE_SLEEP; 556 } 557 btstack_uart->send_block(&ehcill_command_to_send, 1); 558 } 559 560 static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){ 561 UNUSED(timer); 562 #ifdef ENABLE_LOG_EHCILL 563 log_info("eHCILL: timer triggered"); 564 #endif 565 hci_transport_h4_ehcill_send_ehcill_command(); 566 } 567 568 static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){ 569 // setup timer 570 #ifdef ENABLE_LOG_EHCILL 571 log_info("eHCILL: set timer for sending command %02x", ehcill_command_to_send); 572 #endif 573 btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler); 574 btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50); 575 btstack_run_loop_add_timer(&ehcill_sleep_ack_timer); 576 } 577 578 static void hci_transport_h4_ehcill_trigger_wakeup(void){ 579 switch (tx_state){ 580 case TX_W2_EHCILL_SEND: 581 case TX_W4_EHCILL_SENT: 582 // wake up / sleep ack in progress, nothing to do now 583 return; 584 case TX_IDLE: 585 default: 586 // all clear, prepare for wakeup 587 break; 588 } 589 // UART needed again 590 hci_transport_h4_ehcill_emit_sleep_state(0); 591 if (btstack_uart_sleep_mode){ 592 btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 593 } 594 hci_transport_h4_echill_send_wakeup_ind(); 595 } 596 597 static void hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command){ 598 #ifdef ENABLE_LOG_EHCILL 599 log_info("eHCILL: schedule eHCILL command %02x", command); 600 #endif 601 ehcill_command_to_send = command; 602 switch (tx_state){ 603 case TX_IDLE: 604 if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){ 605 // send right away 606 hci_transport_h4_ehcill_send_ehcill_command(); 607 } else { 608 // change state so BTstack cannot send and setup timer 609 tx_state = TX_W2_EHCILL_SEND; 610 hci_transport_h4_ehcill_sleep_ack_timer_setup(); 611 } 612 break; 613 default: 614 break; 615 } 616 } 617 618 static void hci_transport_h4_ehcill_handle_command(uint8_t action){ 619 // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size); 620 switch(ehcill_state){ 621 case EHCILL_STATE_AWAKE: 622 switch(action){ 623 case EHCILL_GO_TO_SLEEP_IND: 624 ehcill_state = EHCILL_STATE_W2_SEND_SLEEP_ACK; 625 #ifdef ENABLE_LOG_EHCILL 626 log_info("eHCILL: Received GO_TO_SLEEP_IND RX"); 627 #endif 628 hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_GO_TO_SLEEP_ACK); 629 break; 630 default: 631 break; 632 } 633 break; 634 635 case EHCILL_STATE_W2_SEND_SLEEP_ACK: 636 switch(action){ 637 case EHCILL_WAKE_UP_IND: 638 ehcill_state = EHCILL_STATE_AWAKE; 639 hci_transport_h4_ehcill_emit_sleep_state(0); 640 if (btstack_uart_sleep_mode){ 641 btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 642 } 643 #ifdef ENABLE_LOG_EHCILL 644 log_info("eHCILL: Received WAKE_UP_IND RX"); 645 #endif 646 hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK); 647 break; 648 649 default: 650 break; 651 } 652 break; 653 654 case EHCILL_STATE_SLEEP: 655 switch(action){ 656 case EHCILL_WAKEUP_SIGNAL: 657 hci_transport_h4_ehcill_emit_sleep_state(0); 658 if (btstack_uart_sleep_mode){ 659 btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 660 } 661 break; 662 case EHCILL_WAKE_UP_IND: 663 ehcill_state = EHCILL_STATE_AWAKE; 664 hci_transport_h4_ehcill_emit_sleep_state(0); 665 if (btstack_uart_sleep_mode){ 666 btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 667 } 668 #ifdef ENABLE_LOG_EHCILL 669 log_info("eHCILL: Received WAKE_UP_IND RX"); 670 #endif 671 hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK); 672 break; 673 674 default: 675 break; 676 } 677 break; 678 679 case EHCILL_STATE_W4_WAKEUP_IND_OR_ACK: 680 switch(action){ 681 case EHCILL_WAKE_UP_IND: 682 case EHCILL_WAKE_UP_ACK: 683 #ifdef ENABLE_LOG_EHCILL 684 log_info("eHCILL: Received WAKE_UP (%02x)", action); 685 #endif 686 tx_state = TX_W4_PACKET_SENT; 687 ehcill_state = EHCILL_STATE_AWAKE; 688 btstack_uart->send_block(ehcill_tx_data, ehcill_tx_len); 689 break; 690 default: 691 break; 692 } 693 break; 694 } 695 } 696 697 static void hci_transport_h4_ehcill_handle_packet_sent(void){ 698 #ifdef ENABLE_LOG_EHCILL 699 log_info("eHCILL: handle packet sent, command to send %02x", ehcill_command_to_send); 700 #endif 701 // now, send pending ehcill command if neccessary 702 switch (ehcill_command_to_send){ 703 case EHCILL_GO_TO_SLEEP_ACK: 704 hci_transport_h4_ehcill_sleep_ack_timer_setup(); 705 break; 706 case EHCILL_WAKE_UP_IND: 707 hci_transport_h4_ehcill_send_ehcill_command(); 708 break; 709 default: 710 break; 711 } 712 } 713 714 static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){ 715 tx_state = TX_IDLE; 716 int command = ehcill_command_to_send; 717 ehcill_command_to_send = 0; 718 719 #ifdef ENABLE_LOG_EHCILL 720 log_info("eHCILL: handle eHCILL sent, command was %02x", command); 721 #endif 722 723 if (command == EHCILL_GO_TO_SLEEP_ACK) { 724 #ifdef ENABLE_LOG_EHCILL 725 log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode"); 726 #endif 727 // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent 728 if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){ 729 btstack_uart->set_sleep(btstack_uart_sleep_mode); 730 } 731 hci_transport_h4_ehcill_emit_sleep_state(1); 732 } 733 // already packet ready? then start wakeup 734 if (hci_transport_h4_ehcill_outgoing_packet_ready()){ 735 hci_transport_h4_ehcill_emit_sleep_state(0); 736 if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){ 737 btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 738 } 739 if (command != EHCILL_WAKE_UP_IND){ 740 hci_transport_h4_echill_send_wakeup_ind(); 741 } 742 } 743 } 744 745 #endif 746 // --- end of eHCILL implementation --------- 747 748 749 // configure and return h4 singleton 750 static const hci_transport_t hci_transport_h4 = { 751 /* const char * name; */ "H4", 752 /* void (*init) (const void *transport_config); */ &hci_transport_h4_init, 753 /* int (*open)(void); */ &hci_transport_h4_open, 754 /* int (*close)(void); */ &hci_transport_h4_close, 755 /* void (*register_packet_handler)(void (*handler)(...); */ &hci_transport_h4_register_packet_handler, 756 /* int (*can_send_packet_now)(uint8_t packet_type); */ &hci_transport_h4_can_send_now, 757 /* int (*send_packet)(...); */ &hci_transport_h4_send_packet, 758 /* int (*set_baudrate)(uint32_t baudrate); */ &hci_transport_h4_set_baudrate, 759 /* void (*reset_link)(void); */ NULL, 760 /* void (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL, 761 }; 762 763 const hci_transport_t * hci_transport_h4_instance_for_uart(const btstack_uart_t * uart_driver){ 764 btstack_uart = uart_driver; 765 return &hci_transport_h4; 766 } 767 768 // @deprecated 769 const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) { 770 btstack_uart = (const btstack_uart_t *) uart_driver; 771 return &hci_transport_h4; 772 } 773