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