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 "btstack_config.h" 49 50 #include "btstack_debug.h" 51 #include "hci.h" 52 #include "hci_transport.h" 53 #include "btstack_uart_block.h" 54 55 #define ENABLE_LOG_EHCILL 56 57 #ifdef ENABLE_EHCILL 58 59 // eHCILL commands 60 static const uint8_t EHCILL_GO_TO_SLEEP_IND = 0x030; 61 static const uint8_t EHCILL_GO_TO_SLEEP_ACK = 0x031; 62 static const uint8_t EHCILL_WAKE_UP_IND = 0x032; 63 static const uint8_t EHCILL_WAKE_UP_ACK = 0x033; 64 65 static int hci_transport_h4_ehcill_outgoing_packet_ready(void); 66 static void hci_transport_h4_echill_send_wakeup_ind(void); 67 static void hci_transport_h4_ehcill_handle_command(uint8_t action); 68 static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void); 69 static void hci_transport_h4_ehcill_handle_packet_sent(void); 70 static void hci_transport_h4_ehcill_open(void); 71 static void hci_transport_h4_ehcill_reset_statemachine(void); 72 static void hci_transport_h4_ehcill_send_ehcill_command(void); 73 static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void); 74 static void hci_transport_h4_ehcill_trigger_wakeup(void); 75 76 typedef enum { 77 EHCILL_STATE_W2_SEND_SLEEP_ACK, 78 EHCILL_STATE_SLEEP, 79 EHCILL_STATE_W4_WAKEUP_IND_OR_ACK, 80 EHCILL_STATE_AWAKE 81 } EHCILL_STATE; 82 83 // eHCILL state machine 84 static EHCILL_STATE ehcill_state; 85 static uint8_t ehcill_command_to_send; 86 87 static btstack_uart_sleep_mode_t btstack_uart_sleep_mode; 88 89 // work around for eHCILL problem 90 static btstack_timer_source_t ehcill_sleep_ack_timer; 91 92 #endif 93 94 95 // assert pre-buffer for packet type is available 96 #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0) 97 #error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h 98 #endif 99 100 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 101 102 typedef enum { 103 H4_W4_PACKET_TYPE, 104 H4_W4_EVENT_HEADER, 105 H4_W4_ACL_HEADER, 106 H4_W4_SCO_HEADER, 107 H4_W4_PAYLOAD, 108 } H4_STATE; 109 110 typedef enum { 111 TX_IDLE = 1, 112 TX_W4_PACKET_SENT, 113 #ifdef ENABLE_EHCILL 114 TX_W4_WAKEUP, 115 TX_W2_EHCILL_SEND, 116 TX_W4_EHCILL_SENT, 117 #endif 118 } TX_STATE; 119 120 // UART Driver + Config 121 static const btstack_uart_block_t * btstack_uart; 122 static btstack_uart_config_t uart_config; 123 124 // write state 125 static TX_STATE tx_state; 126 static uint8_t * tx_data; 127 static uint16_t tx_len; // 0 == no outgoing packet 128 129 static uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 130 131 static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler; 132 133 // packet reader state machine 134 static H4_STATE h4_state; 135 static int bytes_to_read; 136 static int read_pos; 137 138 // incoming packet buffer 139 static uint8_t hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 1 + HCI_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, event header + event data) 140 static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE]; 141 142 static int hci_transport_h4_set_baudrate(uint32_t baudrate){ 143 log_info("hci_transport_h4_set_baudrate %u", baudrate); 144 return btstack_uart->set_baudrate(baudrate); 145 } 146 147 static void hci_transport_h4_reset_statemachine(void){ 148 h4_state = H4_W4_PACKET_TYPE; 149 read_pos = 0; 150 bytes_to_read = 1; 151 } 152 153 static void hci_transport_h4_trigger_next_read(void){ 154 // log_info("hci_transport_h4_trigger_next_read: %u bytes", bytes_to_read); 155 btstack_uart->receive_block(&hci_packet[read_pos], bytes_to_read); 156 } 157 158 static void hci_transport_h4_block_read(void){ 159 160 read_pos += bytes_to_read; 161 162 switch (h4_state) { 163 case H4_W4_PACKET_TYPE: 164 switch (hci_packet[0]){ 165 case HCI_EVENT_PACKET: 166 bytes_to_read = HCI_EVENT_HEADER_SIZE; 167 h4_state = H4_W4_EVENT_HEADER; 168 break; 169 case HCI_ACL_DATA_PACKET: 170 bytes_to_read = HCI_ACL_HEADER_SIZE; 171 h4_state = H4_W4_ACL_HEADER; 172 break; 173 case HCI_SCO_DATA_PACKET: 174 bytes_to_read = HCI_SCO_HEADER_SIZE; 175 h4_state = H4_W4_SCO_HEADER; 176 break; 177 #ifdef ENABLE_EHCILL 178 case EHCILL_GO_TO_SLEEP_IND: 179 case EHCILL_GO_TO_SLEEP_ACK: 180 case EHCILL_WAKE_UP_IND: 181 case EHCILL_WAKE_UP_ACK: 182 hci_transport_h4_ehcill_handle_command(hci_packet[0]); 183 hci_transport_h4_reset_statemachine(); 184 break; 185 #endif 186 default: 187 log_error("hci_transport_h4: invalid packet type 0x%02x", hci_packet[0]); 188 hci_transport_h4_reset_statemachine(); 189 break; 190 } 191 break; 192 193 case H4_W4_EVENT_HEADER: 194 bytes_to_read = hci_packet[2]; 195 h4_state = H4_W4_PAYLOAD; 196 break; 197 198 case H4_W4_ACL_HEADER: 199 bytes_to_read = little_endian_read_16( hci_packet, 3); 200 // check ACL length 201 if (HCI_ACL_HEADER_SIZE + bytes_to_read > HCI_PACKET_BUFFER_SIZE){ 202 log_error("hci_transport_h4: invalid ACL payload len %d - only space for %u", bytes_to_read, HCI_PACKET_BUFFER_SIZE - HCI_ACL_HEADER_SIZE); 203 hci_transport_h4_reset_statemachine(); 204 break; 205 } 206 h4_state = H4_W4_PAYLOAD; 207 break; 208 209 case H4_W4_SCO_HEADER: 210 bytes_to_read = hci_packet[3]; 211 h4_state = H4_W4_PAYLOAD; 212 break; 213 214 case H4_W4_PAYLOAD: 215 packet_handler(hci_packet[0], &hci_packet[1], read_pos-1); 216 hci_transport_h4_reset_statemachine(); 217 break; 218 default: 219 break; 220 } 221 hci_transport_h4_trigger_next_read(); 222 } 223 224 static void hci_transport_h4_block_sent(void){ 225 switch (tx_state){ 226 case TX_W4_PACKET_SENT: 227 // packet fully sent, reset state 228 tx_len = 0; 229 tx_state = TX_IDLE; 230 231 #ifdef ENABLE_EHCILL 232 // notify eHCILL engine 233 hci_transport_h4_ehcill_handle_packet_sent(); 234 #endif 235 // notify upper stack that it can send again 236 packet_handler(HCI_EVENT_PACKET, &packet_sent_event[0], sizeof(packet_sent_event)); 237 break; 238 239 #ifdef ENABLE_EHCILL 240 case TX_W4_EHCILL_SENT: 241 hci_transport_h4_ehcill_handle_ehcill_command_sent(); 242 break; 243 #endif 244 245 default: 246 break; 247 } 248 } 249 250 static int hci_transport_h4_can_send_now(uint8_t packet_type){ 251 return tx_state == TX_IDLE; 252 } 253 254 static int hci_transport_h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 255 // store packet type before actual data and increase size 256 size++; 257 packet--; 258 *packet = packet_type; 259 260 // store request 261 tx_len = size; 262 tx_data = packet; 263 264 #ifdef ENABLE_EHCILL 265 switch (ehcill_state){ 266 case EHCILL_STATE_SLEEP: 267 hci_transport_h4_ehcill_trigger_wakeup(); 268 return 0; 269 case EHCILL_STATE_W2_SEND_SLEEP_ACK: 270 return 0; 271 default: 272 break; 273 } 274 #endif 275 276 // start sending 277 tx_state = TX_W4_PACKET_SENT; 278 btstack_uart->send_block(packet, size); 279 return 0; 280 } 281 282 static void hci_transport_h4_init(const void * transport_config){ 283 // check for hci_transport_config_uart_t 284 if (!transport_config) { 285 log_error("hci_transport_h4: no config!"); 286 return; 287 } 288 if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) { 289 log_error("hci_transport_h4: config not of type != HCI_TRANSPORT_CONFIG_UART!"); 290 return; 291 } 292 293 // extract UART config from transport config 294 hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config; 295 uart_config.baudrate = hci_transport_config_uart->baudrate_init; 296 uart_config.flowcontrol = hci_transport_config_uart->flowcontrol; 297 uart_config.device_name = hci_transport_config_uart->device_name; 298 299 // setup UART driver 300 btstack_uart->init(&uart_config); 301 btstack_uart->set_block_received(&hci_transport_h4_block_read); 302 btstack_uart->set_block_sent(&hci_transport_h4_block_sent); 303 } 304 305 static int hci_transport_h4_open(void){ 306 int res = btstack_uart->open(); 307 if (res){ 308 return res; 309 } 310 hci_transport_h4_reset_statemachine(); 311 hci_transport_h4_trigger_next_read(); 312 313 tx_state = TX_IDLE; 314 315 #ifdef ENABLE_EHCILL 316 hci_transport_h4_ehcill_open(); 317 #endif 318 return 0; 319 } 320 321 static int hci_transport_h4_close(void){ 322 return btstack_uart->close(); 323 } 324 325 static void hci_transport_h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 326 packet_handler = handler; 327 } 328 329 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 330 } 331 332 // 333 // --- main part of eHCILL implementation --- 334 // 335 336 #ifdef ENABLE_EHCILL 337 338 static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){ 339 static int last_state = 0; 340 if (sleep_active == last_state) return; 341 last_state = sleep_active; 342 343 log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active); 344 uint8_t event[3]; 345 event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE; 346 event[1] = sizeof(event) - 2; 347 event[2] = sleep_active; 348 packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 349 } 350 351 static void hci_transport_h4_ehcill_open(void){ 352 hci_transport_h4_ehcill_reset_statemachine(); 353 354 // find best sleep mode to use: wake on CTS, wake on RX, none 355 btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF; 356 int supported_sleep_modes = 0; 357 if (btstack_uart->get_supported_sleep_modes){ 358 supported_sleep_modes = btstack_uart->get_supported_sleep_modes(); 359 } 360 if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE){ 361 log_info("eHCILL: using wake on CTS"); 362 btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE; 363 } else if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){ 364 log_info("eHCILL: using wake on RX"); 365 btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE; 366 } else { 367 log_info("eHCILL: UART driver does not provide compatible sleep mode"); 368 } 369 } 370 371 static void hci_transport_h4_echill_send_wakeup_ind(void){ 372 #ifdef ENABLE_LOG_EHCILL 373 log_info("eHCILL: send WAKEUP_IND"); 374 #endif 375 // update state 376 tx_state = TX_W4_WAKEUP; 377 ehcill_state = EHCILL_STATE_W4_WAKEUP_IND_OR_ACK; 378 ehcill_command_to_send = EHCILL_WAKE_UP_IND; 379 btstack_uart->send_block(&ehcill_command_to_send, 1); 380 } 381 382 static int hci_transport_h4_ehcill_outgoing_packet_ready(void){ 383 return tx_len != 0; 384 } 385 386 static void hci_transport_h4_ehcill_reset_statemachine(void){ 387 ehcill_state = EHCILL_STATE_AWAKE; 388 } 389 390 static void hci_transport_h4_ehcill_send_ehcill_command(void){ 391 #ifdef ENABLE_LOG_EHCILL 392 log_info("eHCILL: send command %02x", ehcill_command_to_send); 393 #endif 394 tx_state = TX_W4_EHCILL_SENT; 395 if (ehcill_command_to_send == EHCILL_GO_TO_SLEEP_ACK){ 396 ehcill_state = EHCILL_STATE_SLEEP; 397 } 398 btstack_uart->send_block(&ehcill_command_to_send, 1); 399 } 400 401 static void hci_transport_h4_ehcill_sleep_ack_timer_handler(btstack_timer_source_t * timer){ 402 #ifdef ENABLE_LOG_EHCILL 403 log_info("eHCILL: timer triggered"); 404 #endif 405 hci_transport_h4_ehcill_send_ehcill_command(); 406 } 407 408 static void hci_transport_h4_ehcill_sleep_ack_timer_setup(void){ 409 // setup timer 410 #ifdef ENABLE_LOG_EHCILL 411 log_info("eHCILL: set timer for sending command %02x", ehcill_command_to_send); 412 #endif 413 btstack_run_loop_set_timer_handler(&ehcill_sleep_ack_timer, &hci_transport_h4_ehcill_sleep_ack_timer_handler); 414 btstack_run_loop_set_timer(&ehcill_sleep_ack_timer, 50); 415 btstack_run_loop_add_timer(&ehcill_sleep_ack_timer); 416 } 417 418 static void hci_transport_h4_ehcill_trigger_wakeup(void){ 419 switch (tx_state){ 420 case TX_W2_EHCILL_SEND: 421 case TX_W4_EHCILL_SENT: 422 // wake up / sleep ack in progress, nothing to do now 423 return; 424 case TX_IDLE: 425 default: 426 // all clear, prepare for wakeup 427 break; 428 } 429 // UART needed again 430 hci_transport_h4_ehcill_emit_sleep_state(0); 431 if (btstack_uart_sleep_mode){ 432 btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 433 } 434 hci_transport_h4_echill_send_wakeup_ind(); 435 } 436 437 static void hci_transport_h4_ehcill_schedule_ehcill_command(uint8_t command){ 438 #ifdef ENABLE_LOG_EHCILL 439 log_info("eHCILL: schedule eHCILL command %02x", command); 440 #endif 441 ehcill_command_to_send = command; 442 switch (tx_state){ 443 case TX_IDLE: 444 if (ehcill_command_to_send == EHCILL_WAKE_UP_ACK){ 445 // send right away 446 hci_transport_h4_ehcill_send_ehcill_command(); 447 } else { 448 // change state so BTstack cannot send and setup timer 449 tx_state = TX_W2_EHCILL_SEND; 450 hci_transport_h4_ehcill_sleep_ack_timer_setup(); 451 } 452 break; 453 default: 454 break; 455 } 456 } 457 458 static void hci_transport_h4_ehcill_handle_command(uint8_t action){ 459 // log_info("hci_transport_h4_ehcill_handle: %x, state %u, defer_rx %u", action, ehcill_state, ehcill_defer_rx_size); 460 switch(ehcill_state){ 461 case EHCILL_STATE_AWAKE: 462 switch(action){ 463 case EHCILL_GO_TO_SLEEP_IND: 464 ehcill_state = EHCILL_STATE_W2_SEND_SLEEP_ACK; 465 #ifdef ENABLE_LOG_EHCILL 466 log_info("eHCILL: Received GO_TO_SLEEP_IND RX"); 467 #endif 468 hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_GO_TO_SLEEP_ACK); 469 break; 470 default: 471 break; 472 } 473 break; 474 475 case EHCILL_STATE_SLEEP: 476 case EHCILL_STATE_W2_SEND_SLEEP_ACK: 477 switch(action){ 478 case EHCILL_WAKE_UP_IND: 479 ehcill_state = EHCILL_STATE_AWAKE; 480 hci_transport_h4_ehcill_emit_sleep_state(0); 481 #ifdef ENABLE_LOG_EHCILL 482 log_info("eHCILL: Received WAKE_UP_IND RX"); 483 #endif 484 hci_transport_h4_ehcill_schedule_ehcill_command(EHCILL_WAKE_UP_ACK); 485 break; 486 487 default: 488 break; 489 } 490 break; 491 492 case EHCILL_STATE_W4_WAKEUP_IND_OR_ACK: 493 switch(action){ 494 case EHCILL_WAKE_UP_IND: 495 case EHCILL_WAKE_UP_ACK: 496 #ifdef ENABLE_LOG_EHCILL 497 log_info("eHCILL: Received WAKE_UP (%02x)", action); 498 #endif 499 tx_state = TX_W4_PACKET_SENT; 500 ehcill_state = EHCILL_STATE_AWAKE; 501 btstack_uart->send_block(tx_data, tx_len); 502 break; 503 default: 504 break; 505 } 506 break; 507 } 508 } 509 510 static void hci_transport_h4_ehcill_handle_packet_sent(void){ 511 #ifdef ENABLE_LOG_EHCILL 512 log_info("eHCILL: handle packet sent, command to send %02x", ehcill_command_to_send); 513 #endif 514 // now, send pending ehcill command if neccessary 515 switch (ehcill_command_to_send){ 516 case EHCILL_GO_TO_SLEEP_ACK: 517 hci_transport_h4_ehcill_sleep_ack_timer_setup(); 518 break; 519 case EHCILL_WAKE_UP_IND: 520 hci_transport_h4_ehcill_send_ehcill_command(); 521 break; 522 default: 523 break; 524 } 525 } 526 527 static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){ 528 tx_state = TX_IDLE; 529 int command = ehcill_command_to_send; 530 ehcill_command_to_send = 0; 531 532 #ifdef ENABLE_LOG_EHCILL 533 log_info("eHCILL: handle eHCILL sent, command was %02x", command); 534 #endif 535 536 if (command == EHCILL_GO_TO_SLEEP_ACK) { 537 #ifdef ENABLE_LOG_EHCILL 538 log_info("eHCILL: GO_TO_SLEEP_ACK sent, enter sleep mode"); 539 #endif 540 // UART not needed after EHCILL_GO_TO_SLEEP_ACK was sent 541 if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){ 542 btstack_uart->set_sleep(btstack_uart_sleep_mode); 543 } 544 hci_transport_h4_ehcill_emit_sleep_state(1); 545 } 546 // already packet ready? then start wakeup 547 if (hci_transport_h4_ehcill_outgoing_packet_ready()){ 548 hci_transport_h4_ehcill_emit_sleep_state(0); 549 if (btstack_uart_sleep_mode){ 550 btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 551 } 552 if (command != EHCILL_WAKE_UP_IND){ 553 hci_transport_h4_echill_send_wakeup_ind(); 554 } 555 } 556 } 557 558 #endif 559 // --- end of eHCILL implementation --------- 560 561 static const hci_transport_t hci_transport_h4 = { 562 /* const char * name; */ "H4", 563 /* void (*init) (const void *transport_config); */ &hci_transport_h4_init, 564 /* int (*open)(void); */ &hci_transport_h4_open, 565 /* int (*close)(void); */ &hci_transport_h4_close, 566 /* void (*register_packet_handler)(void (*handler)(...); */ &hci_transport_h4_register_packet_handler, 567 /* int (*can_send_packet_now)(uint8_t packet_type); */ &hci_transport_h4_can_send_now, 568 /* int (*send_packet)(...); */ &hci_transport_h4_send_packet, 569 /* int (*set_baudrate)(uint32_t baudrate); */ &hci_transport_h4_set_baudrate, 570 /* void (*reset_link)(void); */ NULL, 571 /* void (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL, 572 }; 573 574 // configure and return h4 singleton 575 const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) { 576 btstack_uart = uart_driver; 577 return &hci_transport_h4; 578 } 579