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