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