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