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