1 /* 2 * Copyright (C) 2016 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 /* 39 * hci_transport_h5.c 40 * 41 * HCI Transport API implementation for basic H5 protocol 42 * 43 * Created by Matthias Ringwald on 4/29/09. 44 */ 45 46 #include "hci.h" 47 #include "btstack_slip.h" 48 #include "btstack_debug.h" 49 #include "hci_transport.h" 50 #include "btstack_uart_block.h" 51 52 typedef enum { 53 LINK_UNINITIALIZED, 54 LINK_INITIALIZED, 55 LINK_ACTIVE 56 } hci_transport_link_state_t; 57 58 typedef enum { 59 HCI_TRANSPORT_LINK_SEND_SYNC = 1 << 0, 60 HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE = 1 << 1, 61 HCI_TRANSPORT_LINK_SEND_CONFIG = 1 << 2, 62 HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE = 1 << 3, 63 HCI_TRANSPORT_LINK_SEND_SLEEP = 1 << 4, 64 HCI_TRANSPORT_LINK_SEND_WOKEN = 1 << 5, 65 HCI_TRANSPORT_LINK_SEND_WAKEUP = 1 << 6, 66 HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET = 1 << 7, 67 HCI_TRANSPORT_LINK_SEND_ACK_PACKET = 1 << 8, 68 HCI_TRANSPORT_LINK_ENTER_SLEEP = 1 << 9, 69 70 } hci_transport_link_actions_t; 71 72 // Configuration Field. No packet buffers -> sliding window = 1, no OOF flow control, no data integrity check 73 #define LINK_CONFIG_SLIDING_WINDOW_SIZE 1 74 #define LINK_CONFIG_OOF_FLOW_CONTROL 0 75 #define LINK_CONFIG_DATA_INTEGRITY_CHECK 0 76 #define LINK_CONFIG_VERSION_NR 0 77 #define LINK_CONFIG_FIELD (LINK_CONFIG_SLIDING_WINDOW_SIZE | (LINK_CONFIG_OOF_FLOW_CONTROL << 3) | (LINK_CONFIG_DATA_INTEGRITY_CHECK << 4) | (LINK_CONFIG_VERSION_NR << 5)) 78 79 // periodic sending during link establishment 80 #define LINK_PERIOD_MS 250 81 82 // resend wakeup 83 #define LINK_WAKEUP_MS 50 84 85 // additional packet types 86 #define LINK_ACKNOWLEDGEMENT_TYPE 0x00 87 #define LINK_CONTROL_PACKET_TYPE 0x0f 88 89 // max size of write requests 90 #define LINK_SLIP_TX_CHUNK_LEN 64 91 92 // --- 93 static const uint8_t link_control_sync[] = { 0x01, 0x7e}; 94 static const uint8_t link_control_sync_response[] = { 0x02, 0x7d}; 95 static const uint8_t link_control_config[] = { 0x03, 0xfc, LINK_CONFIG_FIELD}; 96 static const uint8_t link_control_config_prefix_len = 2; 97 static const uint8_t link_control_config_response[] = { 0x04, 0x7b, LINK_CONFIG_FIELD}; 98 static const uint8_t link_control_config_response_prefix_len = 2; 99 static const uint8_t link_control_wakeup[] = { 0x05, 0xfa}; 100 static const uint8_t link_control_woken[] = { 0x06, 0xf9}; 101 static const uint8_t link_control_sleep[] = { 0x07, 0x78}; 102 103 // incoming pre-bufffer + 4 bytes H5 header + max(acl header + acl payload, event header + event data) + 2 bytes opt CRC 104 static uint8_t hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 6 + HCI_PACKET_BUFFER_SIZE]; 105 106 // outgoing slip encoded buffer. +1 to assert that last SOF fits in buffer 107 static uint8_t slip_outgoing_buffer[LINK_SLIP_TX_CHUNK_LEN+1]; 108 static int slip_write_active; 109 110 // H5 Link State 111 static hci_transport_link_state_t link_state; 112 static btstack_timer_source_t link_timer; 113 static uint8_t link_seq_nr; 114 static uint8_t link_ack_nr; 115 static uint16_t link_resend_timeout_ms; 116 static uint8_t link_peer_asleep; 117 118 // auto sleep-mode 119 static btstack_timer_source_t inactivity_timer; 120 static uint16_t link_inactivity_timeout_ms; // auto-sleep if set 121 122 // Outgoing packet 123 static uint8_t hci_packet_type; 124 static uint16_t hci_packet_size; 125 static uint8_t * hci_packet; 126 127 // hci packet handler 128 static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 129 130 static int hci_transport_link_actions; 131 132 // UART Driver + Config 133 static const btstack_uart_block_t * btstack_uart; 134 static btstack_uart_config_t uart_config; 135 static btstack_uart_sleep_mode_t btstack_uart_sleep_mode; 136 137 // Prototypes 138 static void hci_transport_h5_process_frame(uint16_t frame_size); 139 static int hci_transport_link_have_outgoing_packet(void); 140 static void hci_transport_link_send_queued_packet(void); 141 static void hci_transport_link_set_timer(uint16_t timeout_ms); 142 static void hci_transport_link_timeout_handler(btstack_timer_source_t * timer); 143 static void hci_transport_link_run(void); 144 static void hci_transport_slip_init(void); 145 146 // ----------------------------- 147 static void hci_transport_inactivity_timeout_handler(btstack_timer_source_t * ts){ 148 log_info("h5: inactivity timeout. link state %u, peer asleep %u, actions 0x%02x, outgoing packet %u", 149 link_state, link_peer_asleep, hci_transport_link_actions, hci_transport_link_have_outgoing_packet()); 150 if (hci_transport_link_have_outgoing_packet()) return; 151 if (link_state != LINK_ACTIVE) return; 152 if (hci_transport_link_actions) return; 153 if (link_peer_asleep) return; 154 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SLEEP; 155 hci_transport_link_run(); 156 } 157 158 static void hci_transport_inactivity_timer_set(void){ 159 if (!link_inactivity_timeout_ms) return; 160 btstack_run_loop_set_timer_handler(&inactivity_timer, &hci_transport_inactivity_timeout_handler); 161 btstack_run_loop_set_timer(&inactivity_timer, link_inactivity_timeout_ms); 162 btstack_run_loop_remove_timer(&inactivity_timer); 163 btstack_run_loop_add_timer(&inactivity_timer); 164 } 165 166 // ----------------------------- 167 // SLIP Outgoing 168 169 // Fill chunk and write 170 static void hci_transport_slip_encode_chunk_and_send(int pos){ 171 while (btstack_slip_encoder_has_data() & (pos < LINK_SLIP_TX_CHUNK_LEN)) { 172 slip_outgoing_buffer[pos++] = btstack_slip_encoder_get_byte(); 173 } 174 if (!btstack_slip_encoder_has_data()){ 175 // Start of Frame 176 slip_outgoing_buffer[pos++] = BTSTACK_SLIP_SOF; 177 } 178 slip_write_active = 1; 179 log_info("hci_transport_slip: send %u bytes", pos); 180 btstack_uart->send_block(slip_outgoing_buffer, pos); 181 } 182 183 static inline void hci_transport_slip_send_next_chunk(void){ 184 hci_transport_slip_encode_chunk_and_send(0); 185 } 186 187 // format: 0xc0 HEADER PACKER 0xc0 188 // @param uint8_t header[4] 189 static void hci_transport_slip_send_frame(const uint8_t * header, const uint8_t * packet, uint16_t packet_size){ 190 191 int pos = 0; 192 193 // Start of Frame 194 slip_outgoing_buffer[pos++] = BTSTACK_SLIP_SOF; 195 196 // Header 197 btstack_slip_encoder_start(header, 4); 198 while (btstack_slip_encoder_has_data()){ 199 slip_outgoing_buffer[pos++] = btstack_slip_encoder_get_byte(); 200 } 201 202 // Packet 203 btstack_slip_encoder_start(packet, packet_size); 204 205 // Fill rest of chunk from packet and send 206 hci_transport_slip_encode_chunk_and_send(pos); 207 } 208 209 // SLIP Incoming 210 211 static void hci_transport_slip_init(void){ 212 btstack_slip_decoder_init(&hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], 6 + HCI_PACKET_BUFFER_SIZE); 213 } 214 215 // H5 Three-Wire Implementation 216 217 static void hci_transport_link_calc_header(uint8_t * header, 218 uint8_t sequence_nr, 219 uint8_t acknowledgement_nr, 220 uint8_t data_integrity_check_present, 221 uint8_t reliable_packet, 222 uint8_t packet_type, 223 uint16_t payload_length){ 224 225 // reset data integrity flag 226 if (data_integrity_check_present){ 227 log_error("hci_transport_link_calc_header: data integrity not supported, dropping flag"); 228 data_integrity_check_present = 0; 229 } 230 231 header[0] = sequence_nr | (acknowledgement_nr << 3) | (data_integrity_check_present << 6) | (reliable_packet << 7); 232 header[1] = packet_type | ((payload_length & 0x0f) << 4); 233 header[2] = payload_length >> 4; 234 header[3] = 0xff - (header[0] + header[1] + header[2]); 235 } 236 237 static void hci_transport_link_send_control(const uint8_t * message, int message_len){ 238 uint8_t header[4]; 239 hci_transport_link_calc_header(header, 0, 0, 0, 0, LINK_CONTROL_PACKET_TYPE, message_len); 240 hci_transport_slip_send_frame(header, message, message_len); 241 } 242 243 static void hci_transport_link_send_sync(void){ 244 log_info("link: send sync"); 245 hci_transport_link_send_control(link_control_sync, sizeof(link_control_sync)); 246 } 247 248 static void hci_transport_link_send_sync_response(void){ 249 log_info("link: send sync response"); 250 hci_transport_link_send_control(link_control_sync_response, sizeof(link_control_sync_response)); 251 } 252 253 static void hci_transport_link_send_config(void){ 254 log_info("link: send config"); 255 hci_transport_link_send_control(link_control_config, sizeof(link_control_config)); 256 } 257 258 static void hci_transport_link_send_config_response(void){ 259 log_info("link: send config response"); 260 hci_transport_link_send_control(link_control_config_response, sizeof(link_control_config_response)); 261 } 262 263 static void hci_transport_link_send_woken(void){ 264 log_info("link: send woken"); 265 hci_transport_link_send_control(link_control_woken, sizeof(link_control_woken)); 266 } 267 268 static void hci_transport_link_send_wakeup(void){ 269 log_info("link: send wakeup"); 270 hci_transport_link_send_control(link_control_wakeup, sizeof(link_control_wakeup)); 271 } 272 273 static void hci_transport_link_send_sleep(void){ 274 log_info("link: send sleep"); 275 hci_transport_link_send_control(link_control_sleep, sizeof(link_control_sleep)); 276 } 277 278 static void hci_transport_link_send_queued_packet(void){ 279 log_info("hci_transport_link_send_queued_packet: seq %u, ack %u, size %u", link_seq_nr, link_ack_nr, hci_packet_size); 280 log_info_hexdump(hci_packet, hci_packet_size); 281 282 uint8_t header[4]; 283 hci_transport_link_calc_header(header, link_seq_nr, link_ack_nr, 0, 1, hci_packet_type, hci_packet_size); 284 hci_transport_slip_send_frame(header, hci_packet, hci_packet_size); 285 286 // reset inactvitiy timer 287 hci_transport_inactivity_timer_set(); 288 } 289 290 static void hci_transport_link_send_ack_packet(void){ 291 log_info("link: send ack %u", link_ack_nr); 292 uint8_t header[4]; 293 hci_transport_link_calc_header(header, 0, link_ack_nr, 0, 0, LINK_ACKNOWLEDGEMENT_TYPE, 0); 294 hci_transport_slip_send_frame(header, NULL, 0); 295 } 296 297 static void hci_transport_link_run(void){ 298 // exit if outgoing active 299 if (slip_write_active) return; 300 301 // process queued requests 302 if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SYNC){ 303 hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SYNC; 304 hci_transport_link_send_sync(); 305 return; 306 } 307 if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE){ 308 hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE; 309 hci_transport_link_send_sync_response(); 310 return; 311 } 312 if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_CONFIG){ 313 hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_CONFIG; 314 hci_transport_link_send_config(); 315 return; 316 } 317 if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE){ 318 hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE; 319 hci_transport_link_send_config_response(); 320 return; 321 } 322 if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_WOKEN){ 323 hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_WOKEN; 324 hci_transport_link_send_woken(); 325 return; 326 } 327 if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_WAKEUP){ 328 hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_WAKEUP; 329 hci_transport_link_send_wakeup(); 330 return; 331 } 332 if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET){ 333 hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET; 334 // packet already contains ack, no need to send addtitional one 335 hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_ACK_PACKET; 336 hci_transport_link_send_queued_packet(); 337 return; 338 } 339 if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_ACK_PACKET){ 340 hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_ACK_PACKET; 341 hci_transport_link_send_ack_packet(); 342 return; 343 } 344 if (hci_transport_link_actions & HCI_TRANSPORT_LINK_SEND_SLEEP){ 345 hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_SEND_SLEEP; 346 hci_transport_link_actions |= HCI_TRANSPORT_LINK_ENTER_SLEEP; 347 link_peer_asleep = 1; 348 hci_transport_link_send_sleep(); 349 return; 350 } 351 } 352 353 static void hci_transport_link_set_timer(uint16_t timeout_ms){ 354 btstack_run_loop_set_timer_handler(&link_timer, &hci_transport_link_timeout_handler); 355 btstack_run_loop_set_timer(&link_timer, timeout_ms); 356 btstack_run_loop_add_timer(&link_timer); 357 } 358 359 static void hci_transport_link_timeout_handler(btstack_timer_source_t * timer){ 360 switch (link_state){ 361 case LINK_UNINITIALIZED: 362 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC; 363 hci_transport_link_set_timer(LINK_PERIOD_MS); 364 break; 365 case LINK_INITIALIZED: 366 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG; 367 hci_transport_link_set_timer(LINK_PERIOD_MS); 368 break; 369 case LINK_ACTIVE: 370 if (!hci_transport_link_have_outgoing_packet()){ 371 log_info("h5 timeout while active, but no outgoing packet"); 372 return; 373 } 374 if (link_peer_asleep){ 375 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WAKEUP; 376 hci_transport_link_set_timer(LINK_WAKEUP_MS); 377 return; 378 } 379 // resend packet 380 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET; 381 hci_transport_link_set_timer(link_resend_timeout_ms); 382 break; 383 default: 384 break; 385 } 386 387 hci_transport_link_run(); 388 } 389 390 static void hci_transport_link_init(void){ 391 link_state = LINK_UNINITIALIZED; 392 link_peer_asleep = 0; 393 394 // get started 395 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC; 396 hci_transport_link_set_timer(LINK_PERIOD_MS); 397 hci_transport_link_run(); 398 } 399 400 static int hci_transport_link_inc_seq_nr(int seq_nr){ 401 return (seq_nr + 1) & 0x07; 402 } 403 404 static int hci_transport_link_have_outgoing_packet(void){ 405 return hci_packet != 0; 406 } 407 408 static void hci_transport_link_clear_queue(void){ 409 btstack_run_loop_remove_timer(&link_timer); 410 hci_packet = NULL; 411 } 412 413 static void hci_transport_h5_queue_packet(uint8_t packet_type, uint8_t *packet, int size){ 414 hci_packet = packet; 415 hci_packet_type = packet_type; 416 hci_packet_size = size; 417 } 418 419 static void hci_transport_h5_process_frame(uint16_t frame_size){ 420 421 if (frame_size < 4) return; 422 423 uint8_t * slip_header = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE]; 424 uint8_t * slip_payload = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 4]; 425 int frame_size_without_header = frame_size - 4; 426 427 int seq_nr = slip_header[0] & 0x07; 428 int ack_nr = (slip_header[0] >> 3) & 0x07; 429 int data_integrity_check_present = (slip_header[0] & 0x40) != 0; 430 int reliable_packet = (slip_header[0] & 0x80) != 0; 431 uint8_t link_packet_type = slip_header[1] & 0x0f; 432 uint16_t link_payload_len = (slip_header[1] >> 4) | (slip_header[2] << 4); 433 434 log_info("hci_transport_h5_process_frame, reliable %u, packet type %u, seq_nr %u, ack_nr %u", reliable_packet, link_packet_type, seq_nr, ack_nr); 435 log_info_hexdump(slip_header, 4); 436 log_info_hexdump(slip_payload, frame_size_without_header); 437 438 // CSR 8811 does not seem to auto-detect H5 mode and sends data with even parity. 439 // if this byte sequence is detected, just enable even parity 440 const uint8_t sync_response_bcsp[] = {0x01, 0x7a, 0x06, 0x10}; 441 if (memcmp(sync_response_bcsp, slip_header, 4) == 0){ 442 log_info("h5: detected BSCP SYNC sent with Even Parity -> discard frame and enable Even Parity"); 443 btstack_uart->set_parity(1); 444 return; 445 } 446 447 // validate header checksum 448 uint8_t header_checksum = slip_header[0] + slip_header[1] + slip_header[2] + slip_header[3]; 449 if (header_checksum != 0xff){ 450 log_info("h5: header checksum 0x%02x (instead of 0xff)", header_checksum); 451 return; 452 } 453 454 // validate payload length 455 int data_integrity_len = data_integrity_check_present ? 2 : 0; 456 uint16_t received_payload_len = frame_size_without_header - data_integrity_len; 457 if (link_payload_len != received_payload_len){ 458 log_info("h5: expected payload len %u but got %u", link_payload_len, received_payload_len); 459 return; 460 } 461 462 // (TODO data integrity check) 463 464 switch (link_state){ 465 case LINK_UNINITIALIZED: 466 if (link_packet_type != LINK_CONTROL_PACKET_TYPE) break; 467 if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){ 468 log_info("link: received sync"); 469 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE; 470 } 471 if (memcmp(slip_payload, link_control_sync_response, sizeof(link_control_sync_response)) == 0){ 472 log_info("link: received sync response"); 473 link_state = LINK_INITIALIZED; 474 btstack_run_loop_remove_timer(&link_timer); 475 log_info("link initialized"); 476 // 477 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG; 478 hci_transport_link_set_timer(LINK_PERIOD_MS); 479 } 480 break; 481 case LINK_INITIALIZED: 482 if (link_packet_type != LINK_CONTROL_PACKET_TYPE) break; 483 if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){ 484 log_info("link: received sync"); 485 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_SYNC_RESPONSE; 486 } 487 if (memcmp(slip_payload, link_control_config, link_control_config_prefix_len) == 0){ 488 log_info("link: received config"); 489 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE; 490 } 491 if (memcmp(slip_payload, link_control_config_response, link_control_config_response_prefix_len) == 0){ 492 log_info("link: received config response"); 493 link_state = LINK_ACTIVE; 494 btstack_run_loop_remove_timer(&link_timer); 495 log_info("link activated"); 496 // 497 link_seq_nr = 0; 498 link_ack_nr = 0; 499 // notify upper stack that it can start 500 uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 501 packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 502 } 503 break; 504 case LINK_ACTIVE: 505 506 // validate packet sequence nr in reliable packets (check for out of sequence error) 507 if (reliable_packet){ 508 if (seq_nr != link_ack_nr){ 509 log_info("expected seq nr %u, but received %u", link_ack_nr, seq_nr); 510 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_ACK_PACKET; 511 break; 512 } 513 // ack packet right away 514 link_ack_nr = hci_transport_link_inc_seq_nr(link_ack_nr); 515 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_ACK_PACKET; 516 } 517 518 // Process ACKs in reliable packet and explicit ack packets 519 if (reliable_packet || link_packet_type == LINK_ACKNOWLEDGEMENT_TYPE){ 520 // our packet is good if the remote expects our seq nr + 1 521 int next_seq_nr = hci_transport_link_inc_seq_nr(link_seq_nr); 522 if (hci_transport_link_have_outgoing_packet() && next_seq_nr == ack_nr){ 523 log_info("h5: outoing packet with seq %u ack'ed", link_seq_nr); 524 link_seq_nr = next_seq_nr; 525 hci_transport_link_clear_queue(); 526 527 // notify upper stack that it can send again 528 uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 529 packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 530 } 531 } 532 533 switch (link_packet_type){ 534 case LINK_CONTROL_PACKET_TYPE: 535 if (memcmp(slip_payload, link_control_config, sizeof(link_control_config)) == 0){ 536 log_info("link: received config"); 537 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_CONFIG_RESPONSE; 538 break; 539 } 540 if (memcmp(slip_payload, link_control_sync, sizeof(link_control_sync)) == 0){ 541 log_info("link: received sync in ACTIVE STATE!"); 542 // TODO sync during active indicates peer reset -> full upper layer reset necessary 543 break; 544 } 545 if (memcmp(slip_payload, link_control_sleep, sizeof(link_control_sleep)) == 0){ 546 if (btstack_uart_sleep_mode){ 547 log_info("link: received sleep message. Enabling UART Sleep."); 548 btstack_uart->set_sleep(btstack_uart_sleep_mode); 549 } else { 550 log_info("link: received sleep message. UART Sleep not supported"); 551 } 552 link_peer_asleep = 1; 553 break; 554 } 555 if (memcmp(slip_payload, link_control_wakeup, sizeof(link_control_wakeup)) == 0){ 556 log_info("link: received wakupe message -> send woken"); 557 link_peer_asleep = 0; 558 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WOKEN; 559 break; 560 } 561 if (memcmp(slip_payload, link_control_woken, sizeof(link_control_woken)) == 0){ 562 log_info("link: received woken message"); 563 link_peer_asleep = 0; 564 // queued packet will be sent in hci_transport_link_run if needed 565 break; 566 } 567 break; 568 case HCI_EVENT_PACKET: 569 case HCI_ACL_DATA_PACKET: 570 case HCI_SCO_DATA_PACKET: 571 // seems like peer is awake 572 link_peer_asleep = 0; 573 // forward packet to stack 574 packet_handler(link_packet_type, slip_payload, link_payload_len); 575 // reset inactvitiy timer 576 hci_transport_inactivity_timer_set(); 577 break; 578 } 579 580 break; 581 default: 582 break; 583 } 584 585 hci_transport_link_run(); 586 } 587 588 // recommendet time until resend: 3 * time of largest packet 589 static uint16_t hci_transport_link_calc_resend_timeout(uint32_t baudrate){ 590 uint32_t max_packet_size_in_bit = (HCI_PACKET_BUFFER_SIZE + 6) << 3; 591 uint32_t t_max_x3_ms = max_packet_size_in_bit * 3000 / baudrate; 592 log_info("resend timeout for %u baud: %u ms", baudrate, t_max_x3_ms); 593 return t_max_x3_ms; 594 } 595 596 static void hci_transport_link_update_resend_timeout(uint32_t baudrate){ 597 link_resend_timeout_ms = hci_transport_link_calc_resend_timeout(baudrate); 598 } 599 600 /// H5 Interface 601 602 static uint8_t hci_transport_link_read_byte; 603 604 static void hci_transport_h5_read_next_byte(void){ 605 log_debug("h5: rx nxt"); 606 btstack_uart->receive_block(&hci_transport_link_read_byte, 1); 607 } 608 609 static void hci_transport_h5_block_received(){ 610 log_debug("slip: process 0x%02x", hci_transport_link_read_byte); 611 btstack_slip_decoder_process(hci_transport_link_read_byte); 612 uint16_t frame_size = btstack_slip_decoder_frame_size(); 613 if (frame_size) { 614 hci_transport_h5_process_frame(frame_size); 615 hci_transport_slip_init(); 616 } 617 hci_transport_h5_read_next_byte(); 618 } 619 620 static void hci_transport_h5_block_sent(void){ 621 622 // check if more data to send 623 if (btstack_slip_encoder_has_data()){ 624 hci_transport_slip_send_next_chunk(); 625 return; 626 } 627 628 // done 629 slip_write_active = 0; 630 631 // enter sleep mode after sending sleep message 632 if (hci_transport_link_actions & HCI_TRANSPORT_LINK_ENTER_SLEEP){ 633 hci_transport_link_actions &= ~HCI_TRANSPORT_LINK_ENTER_SLEEP; 634 if (btstack_uart_sleep_mode){ 635 log_info("link: sent sleep message. Enabling UART Sleep."); 636 btstack_uart->set_sleep(btstack_uart_sleep_mode); 637 } else { 638 log_info("link: sent sleep message. UART Sleep not supported"); 639 } 640 } 641 642 hci_transport_link_run(); 643 } 644 645 static void hci_transport_h5_init(const void * transport_config){ 646 // check for hci_transport_config_uart_t 647 if (!transport_config) { 648 log_error("hci_transport_h5: no config!"); 649 return; 650 } 651 if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) { 652 log_error("hci_transport_h5: config not of type != HCI_TRANSPORT_CONFIG_UART!"); 653 return; 654 } 655 656 // extract UART config from transport config 657 hci_transport_config_uart_t * hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config; 658 uart_config.baudrate = hci_transport_config_uart->baudrate_init; 659 uart_config.flowcontrol = hci_transport_config_uart->flowcontrol; 660 uart_config.device_name = hci_transport_config_uart->device_name; 661 662 // setup UART driver 663 btstack_uart->init(&uart_config); 664 btstack_uart->set_block_received(&hci_transport_h5_block_received); 665 btstack_uart->set_block_sent(&hci_transport_h5_block_sent); 666 } 667 668 static int hci_transport_h5_open(void){ 669 int res = btstack_uart->open(); 670 if (res){ 671 return res; 672 } 673 674 // check if wake on RX can be used 675 btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_OFF; 676 int supported_sleep_modes = 0; 677 if (btstack_uart->get_supported_sleep_modes){ 678 supported_sleep_modes = btstack_uart->get_supported_sleep_modes(); 679 } 680 if (supported_sleep_modes & BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE){ 681 log_info("H5: using wake on RX"); 682 btstack_uart_sleep_mode = BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE; 683 } else { 684 log_info("H5: UART driver does not provide compatible sleep mode"); 685 } 686 687 // setup resend timeout 688 hci_transport_link_update_resend_timeout(uart_config.baudrate); 689 690 // init slip parser state machine 691 hci_transport_slip_init(); 692 693 // init link management - already starts syncing 694 hci_transport_link_init(); 695 696 // start receiving 697 hci_transport_h5_read_next_byte(); 698 699 return 0; 700 } 701 702 static int hci_transport_h5_close(void){ 703 return btstack_uart->close(); 704 return 0; 705 } 706 707 static void hci_transport_h5_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 708 packet_handler = handler; 709 } 710 711 static int hci_transport_h5_can_send_packet_now(uint8_t packet_type){ 712 int res = !hci_transport_link_have_outgoing_packet() && link_state == LINK_ACTIVE; 713 // log_info("hci_transport_h5_can_send_packet_now: %u", res); 714 return res; 715 } 716 717 static int hci_transport_h5_send_packet(uint8_t packet_type, uint8_t *packet, int size){ 718 if (!hci_transport_h5_can_send_packet_now(packet_type)){ 719 log_error("hci_transport_h5_send_packet called but in state %u", link_state); 720 return -1; 721 } 722 723 // store request 724 hci_transport_h5_queue_packet(packet_type, packet, size); 725 726 // send wakeup first 727 if (link_peer_asleep){ 728 if (btstack_uart_sleep_mode){ 729 log_info("h5: disable UART sleep"); 730 btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); 731 } 732 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_WAKEUP; 733 hci_transport_link_set_timer(LINK_WAKEUP_MS); 734 } else { 735 hci_transport_link_actions |= HCI_TRANSPORT_LINK_SEND_QUEUED_PACKET; 736 hci_transport_link_set_timer(link_resend_timeout_ms); 737 } 738 hci_transport_link_run(); 739 return 0; 740 } 741 742 static int hci_transport_h5_set_baudrate(uint32_t baudrate){ 743 744 log_info("hci_transport_h5_set_baudrate %u", baudrate); 745 int res = btstack_uart->set_baudrate(baudrate); 746 747 if (res) return res; 748 hci_transport_link_update_resend_timeout(baudrate); 749 return 0; 750 } 751 752 static void hci_transport_h5_reset_link(void){ 753 754 log_info("hci_transport_h5_reset_link"); 755 756 // clear outgoing queue 757 hci_transport_link_clear_queue(); 758 759 // init slip parser state machine 760 hci_transport_slip_init(); 761 762 // init link management - already starts syncing 763 hci_transport_link_init(); 764 } 765 766 static const hci_transport_t hci_transport_h5 = { 767 /* const char * name; */ "H5", 768 /* void (*init) (const void *transport_config); */ &hci_transport_h5_init, 769 /* int (*open)(void); */ &hci_transport_h5_open, 770 /* int (*close)(void); */ &hci_transport_h5_close, 771 /* void (*register_packet_handler)(void (*handler)(...); */ &hci_transport_h5_register_packet_handler, 772 /* int (*can_send_packet_now)(uint8_t packet_type); */ &hci_transport_h5_can_send_packet_now, 773 /* int (*send_packet)(...); */ &hci_transport_h5_send_packet, 774 /* int (*set_baudrate)(uint32_t baudrate); */ &hci_transport_h5_set_baudrate, 775 /* void (*reset_link)(void); */ &hci_transport_h5_reset_link, 776 }; 777 778 // configure and return h5 singleton 779 const hci_transport_t * hci_transport_h5_instance(const btstack_uart_block_t * uart_driver) { 780 btstack_uart = uart_driver; 781 return &hci_transport_h5; 782 } 783 784 void hci_transport_h5_set_auto_sleep(uint16_t inactivity_timeout_ms){ 785 link_inactivity_timeout_ms = inactivity_timeout_ms; 786 } 787