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