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_block_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-1; 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 } 301 302 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 303 if (baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT){ 304 baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_IDLE; 305 // avoid flowcontrol problem by reading expected hci command complete event of 7 bytes in a single block read 306 h4_state = H4_W4_PAYLOAD; 307 bytes_to_read = 7; 308 } 309 #endif 310 311 // forward packet if payload size == 0 312 if (h4_state == H4_W4_PAYLOAD && bytes_to_read == 0) { 313 hci_transport_h4_packet_complete(); 314 } 315 316 if (h4_state != H4_OFF) { 317 hci_transport_h4_trigger_next_read(); 318 } 319 } 320 321 static void hci_transport_h4_block_sent(void){ 322 323 static const uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 324 325 switch (tx_state){ 326 case TX_W4_PACKET_SENT: 327 // packet fully sent, reset state 328 #ifdef ENABLE_EHCILL 329 ehcill_tx_len = 0; 330 #endif 331 tx_state = TX_IDLE; 332 333 #ifdef ENABLE_EHCILL 334 // notify eHCILL engine 335 hci_transport_h4_ehcill_handle_packet_sent(); 336 #endif 337 // notify upper stack that it can send again 338 packet_handler(HCI_EVENT_PACKET, (uint8_t *) &packet_sent_event[0], sizeof(packet_sent_event)); 339 break; 340 341 #ifdef ENABLE_EHCILL 342 case TX_W4_EHCILL_SENT: 343 case TX_W4_WAKEUP: 344 hci_transport_h4_ehcill_handle_ehcill_command_sent(); 345 break; 346 #endif 347 348 default: 349 break; 350 } 351 } 352 353 static int hci_transport_h4_can_send_now(uint8_t packet_type){ 354 UNUSED(packet_type); 355 return tx_state == TX_IDLE; 356 } 357 358 static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 359 360 // store packet type before actual data and increase size 361 size++; 362 packet--; 363 *packet = packet_type; 364 365 #ifdef ENABLE_BAUDRATE_CHANGE_FLOWCONTROL_BUG_WORKAROUND 366 if ((baudrate_change_workaround_state == BAUDRATE_CHANGE_WORKAROUND_CHIPSET_DETECTED) 367 && (memcmp(packet, baud_rate_command_prefix, sizeof(baud_rate_command_prefix)) == 0)) { 368 log_info("Baud rate command detected, expect command complete event next"); 369 baudrate_change_workaround_state = BAUDRATE_CHANGE_WORKAROUND_BAUDRATE_COMMAND_SENT; 370 } 371 #endif 372 373 #ifdef ENABLE_EHCILL 374 // store request for later 375 ehcill_tx_len = size; 376 ehcill_tx_data = packet; 377 switch (ehcill_state){ 378 case EHCILL_STATE_SLEEP: 379 hci_transport_h4_ehcill_trigger_wakeup(); 380 return 0; 381 case EHCILL_STATE_W2_SEND_SLEEP_ACK: 382 log_info("eHILL: send next packet, state EHCILL_STATE_W2_SEND_SLEEP_ACK"); 383 return 0; 384 default: 385 break; 386 } 387 #endif 388 389 // start sending 390 tx_state = TX_W4_PACKET_SENT; 391 btstack_uart->send_block(packet, size); 392 return 0; 393 } 394 395 static void hci_transport_h4_init(const void * transport_config){ 396 // check for hci_transport_config_uart_t 397 if (!transport_config) { 398 log_error("hci_transport_h4: no config!"); 399 return; 400 } 401 if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) { 402 log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!"); 403 return; 404 } 405 406 // extract UART config from transport config 407 hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config; 408 uart_config.baudrate = hci_transport_config_uart->baudrate_init; 409 uart_config.flowcontrol = hci_transport_config_uart->flowcontrol; 410 uart_config.device_name = hci_transport_config_uart->device_name; 411 412 // set state to off 413 tx_state = TX_OFF; 414 h4_state = H4_OFF; 415 416 // setup UART driver 417 btstack_uart->init(&uart_config); 418 btstack_uart->set_block_received(&hci_transport_h4_block_read); 419 btstack_uart->set_block_sent(&hci_transport_h4_block_sent); 420 } 421 422 static int hci_transport_h4_open(void){ 423 // open uart driver 424 int res = btstack_uart->open(); 425 if (res){ 426 return res; 427 } 428 429 // init rx + tx state machines 430 hci_transport_h4_reset_statemachine(); 431 hci_transport_h4_trigger_next_read(); 432 tx_state = TX_IDLE; 433 434 #ifdef ENABLE_EHCILL 435 hci_transport_h4_ehcill_open(); 436 #endif 437 return 0; 438 } 439 440 static int hci_transport_h4_close(void){ 441 // set state to off 442 tx_state = TX_OFF; 443 h4_state = H4_OFF; 444 445 // close uart driver 446 return btstack_uart->close(); 447 } 448 449 static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 450 packet_handler = handler; 451 } 452 453 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 454 UNUSED(packet_type); 455 UNUSED(packet); 456 UNUSED(size); 457 } 458 459 // 460 // --- main part of eHCILL implementation --- 461 // 462 463 #ifdef ENABLE_EHCILL 464 465 static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){ 466 static int last_state = 0; 467 if (sleep_active == last_state) return; 468 last_state = sleep_active; 469 470 log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active); 471 uint8_t event[3]; 472 event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE; 473 event[1] = sizeof(event) - 2; 474 event[2] = sleep_active; 475 packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 476 } 477 478 static void hci_transport_h4_ehcill_wakeup_handler(void){ 479 #ifdef ENABLE_LOG_EHCILL 480 log_info("eHCILL: UART wakeup received"); 481 #endif 482 hci_transport_h4_ehcill_handle_command(EHCILL_WAKEUP_SIGNAL); 483 } 484 485 static void hci_transport_h4_ehcill_open(void){ 486 hci_transport_h4_ehcill_reset_statemachine(); 487 488 // find best sleep mode to use: wake on CTS, wake on RX, none 489 btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF; 490 int supported_sleep_modes = 0; 491 if (btstack_uart->get_supported_sleep_modes){ 492 supported_sleep_modes = btstack_uart->get_supported_sleep_modes(); 493 } 494 if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){ 495 log_info("eHCILL: using wake on CTS"); 496 btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE; 497 } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){ 498 log_info("eHCILL: using wake on RX"); 499 btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE; 500 } else { 501 log_info("eHCILL: UART driver does not provide compatible sleep mode"); 502 } 503 if (btstack_uart->set_wakeup_handler){ 504 btstack_uart->set_wakeup_handler(&hci_transport_h4_ehcill_wakeup_handler); 505 } 506 } 507 508 static void hci_transport_h4_echill_send_wakeup_ind(void){ 509 #ifdef ENABLE_LOG_EHCILL 510 log_info("eHCILL: send WAKEUP_IND"); 511 #endif 512 // update state 513 tx_state = TX_W4_WAKEUP; 514 ehcill_state = EHCILL_STATE_W4_WAKEUP_IND_OR_ACK; 515 ehcill_command_to_send = EHCILL_WAKE_UP_IND; 516 btstack_uart->send_block(&ehcill_command_to_send, 1); 517 } 518 519 static int hci_transport_h4_ehcill_outgoing_packet_ready(void){ 520 return ehcill_tx_len != 0; 521 } 522 523 static void hci_transport_h4_ehcill_reset_statemachine(void){ 524 ehcill_state = EHCILL_STATE_AWAKE; 525 } 526 527 static void hci_transport_h4_ehcill_send_ehcill_command(void){ 528 #ifdef ENABLE_LOG_EHCILL 529 log_info("eHCILL: send command %02x", ehcill_command_to_send); 530 #endif 531 tx_state = TX_W4_EHCILL_SENT; 532 if (ehcill_command_to_send == EHCILL_GO_TO_SLEEP_ACK){ 533 ehcill_state = EHCILL_STATE_SLEEP; 534 } 535 btstack_uart->send_block(&ehcill_command_to_send, 1); 536 } 537 538 static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){ 539 UNUSED(timer); 540 #ifdef ENABLE_LOG_EHCILL 541 log_info("eHCILL: timer triggered"); 542 #endif 543 hci_transport_h4_ehcill_send_ehcill_command(); 544 } 545 546 static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){ 547 // setup timer 548 #ifdef ENABLE_LOG_EHCILL 549 log_info("eHCILL: set timer for sending command %02x", ehcill_command_to_send); 550 #endif 551 btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler); 552 btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50); 553 btstack_run_loop_add_timer(&ehcill_sleep_ack_timer); 554 } 555 556 static void hci_transport_h4_ehcill_trigger_wakeup(void){ 557 switch (tx_state){ 558 case TX_W2_EHCILL_SEND: 559 case TX_W4_EHCILL_SENT: 560 // wake up / sleep ack in progress, nothing to do now 561 return; 562 case TX_IDLE: 563 default: 564 // all clear, prepare for wakeup 565 break; 566 } 567 // UART needed again 568 hci_transport_h4_ehcill_emit_sleep_state(0); 569 if (btstack_uart_sleep_mode){ 570 btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 571 } 572 hci_transport_h4_echill_send_wakeup_ind(); 573 } 574 575 static void hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command){ 576 #ifdef ENABLE_LOG_EHCILL 577 log_info("eHCILL: schedule eHCILL command %02x", command); 578 #endif 579 ehcill_command_to_send = command; 580 switch (tx_state){ 581 case TX_IDLE: 582 if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){ 583 // send right away 584 hci_transport_h4_ehcill_send_ehcill_command(); 585 } else { 586 // change state so BTstack cannot send and setup timer 587 tx_state = TX_W2_EHCILL_SEND; 588 hci_transport_h4_ehcill_sleep_ack_timer_setup(); 589 } 590 break; 591 default: 592 break; 593 } 594 } 595 596 static void hci_transport_h4_ehcill_handle_command(uint8_t action){ 597 // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size); 598 switch(ehcill_state){ 599 case EHCILL_STATE_AWAKE: 600 switch(action){ 601 case EHCILL_GO_TO_SLEEP_IND: 602 ehcill_state = EHCILL_STATE_W2_SEND_SLEEP_ACK; 603 #ifdef ENABLE_LOG_EHCILL 604 log_info("eHCILL: Received GO_TO_SLEEP_IND RX"); 605 #endif 606 hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_GO_TO_SLEEP_ACK); 607 break; 608 default: 609 break; 610 } 611 break; 612 613 case EHCILL_STATE_W2_SEND_SLEEP_ACK: 614 switch(action){ 615 case EHCILL_WAKE_UP_IND: 616 ehcill_state = EHCILL_STATE_AWAKE; 617 hci_transport_h4_ehcill_emit_sleep_state(0); 618 if (btstack_uart_sleep_mode){ 619 btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 620 } 621 #ifdef ENABLE_LOG_EHCILL 622 log_info("eHCILL: Received WAKE_UP_IND RX"); 623 #endif 624 hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK); 625 break; 626 627 default: 628 break; 629 } 630 break; 631 632 case EHCILL_STATE_SLEEP: 633 switch(action){ 634 case EHCILL_WAKEUP_SIGNAL: 635 hci_transport_h4_ehcill_emit_sleep_state(0); 636 if (btstack_uart_sleep_mode){ 637 btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 638 } 639 break; 640 case EHCILL_WAKE_UP_IND: 641 ehcill_state = EHCILL_STATE_AWAKE; 642 hci_transport_h4_ehcill_emit_sleep_state(0); 643 if (btstack_uart_sleep_mode){ 644 btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 645 } 646 #ifdef ENABLE_LOG_EHCILL 647 log_info("eHCILL: Received WAKE_UP_IND RX"); 648 #endif 649 hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK); 650 break; 651 652 default: 653 break; 654 } 655 break; 656 657 case EHCILL_STATE_W4_WAKEUP_IND_OR_ACK: 658 switch(action){ 659 case EHCILL_WAKE_UP_IND: 660 case EHCILL_WAKE_UP_ACK: 661 #ifdef ENABLE_LOG_EHCILL 662 log_info("eHCILL: Received WAKE_UP (%02x)", action); 663 #endif 664 tx_state = TX_W4_PACKET_SENT; 665 ehcill_state = EHCILL_STATE_AWAKE; 666 btstack_uart->send_block(ehcill_tx_data, ehcill_tx_len); 667 break; 668 default: 669 break; 670 } 671 break; 672 } 673 } 674 675 static void hci_transport_h4_ehcill_handle_packet_sent(void){ 676 #ifdef ENABLE_LOG_EHCILL 677 log_info("eHCILL: handle packet sent, command to send %02x", ehcill_command_to_send); 678 #endif 679 // now, send pending ehcill command if neccessary 680 switch (ehcill_command_to_send){ 681 case EHCILL_GO_TO_SLEEP_ACK: 682 hci_transport_h4_ehcill_sleep_ack_timer_setup(); 683 break; 684 case EHCILL_WAKE_UP_IND: 685 hci_transport_h4_ehcill_send_ehcill_command(); 686 break; 687 default: 688 break; 689 } 690 } 691 692 static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){ 693 tx_state = TX_IDLE; 694 int command = ehcill_command_to_send; 695 ehcill_command_to_send = 0; 696 697 #ifdef ENABLE_LOG_EHCILL 698 log_info("eHCILL: handle eHCILL sent, command was %02x", command); 699 #endif 700 701 if (command == EHCILL_GO_TO_SLEEP_ACK) { 702 #ifdef ENABLE_LOG_EHCILL 703 log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode"); 704 #endif 705 // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent 706 if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){ 707 btstack_uart->set_sleep(btstack_uart_sleep_mode); 708 } 709 hci_transport_h4_ehcill_emit_sleep_state(1); 710 } 711 // already packet ready? then start wakeup 712 if (hci_transport_h4_ehcill_outgoing_packet_ready()){ 713 hci_transport_h4_ehcill_emit_sleep_state(0); 714 if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){ 715 btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 716 } 717 if (command != EHCILL_WAKE_UP_IND){ 718 hci_transport_h4_echill_send_wakeup_ind(); 719 } 720 } 721 } 722 723 #endif 724 // --- end of eHCILL implementation --------- 725 726 727 // configure and return h4 singleton 728 const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) { 729 730 static const hci_transport_t hci_transport_h4 = { 731 /* const char * name; */ "H4", 732 /* void (*init) (const void *transport_config); */ &hci_transport_h4_init, 733 /* int (*open)(void); */ &hci_transport_h4_open, 734 /* int (*close)(void); */ &hci_transport_h4_close, 735 /* void (*register_packet_handler)(void (*handler)(...); */ &hci_transport_h4_register_packet_handler, 736 /* int (*can_send_packet_now)(uint8_t packet_type); */ &hci_transport_h4_can_send_now, 737 /* int (*send_packet)(...); */ &hci_transport_h4_send_packet, 738 /* int (*set_baudrate)(uint32_t baudrate); */ &hci_transport_h4_set_baudrate, 739 /* void (*reset_link)(void); */ NULL, 740 /* void (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL, 741 }; 742 743 btstack_uart = uart_driver; 744 return &hci_transport_h4; 745 } 746