hci.c (5e91d96c45df857d4e3123e2b4421a00fead486a) hci.c (1979f09cf045e87f55a9cd8067e8ef902cc8d78b)
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 669 unchanged lines hidden (view full) ---

678 // multiple packets could be send on a synchronous HCI transport
679 while (true){
680
681 log_debug("hci_send_acl_packet_fragments loop entered");
682
683 // get current data
684 const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4u;
685 int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos;
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 669 unchanged lines hidden (view full) ---

678 // multiple packets could be send on a synchronous HCI transport
679 while (true){
680
681 log_debug("hci_send_acl_packet_fragments loop entered");
682
683 // get current data
684 const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4u;
685 int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos;
686 int more_fragments = 0;
686 bool more_fragments = false;
687
688 // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length
689 if (current_acl_data_packet_length > max_acl_data_packet_length){
687
688 // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length
689 if (current_acl_data_packet_length > max_acl_data_packet_length){
690 more_fragments = 1;
690 more_fragments = true;
691 current_acl_data_packet_length = max_acl_data_packet_length;
692 }
693
694 // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent)
695 if (acl_header_pos > 0u){
696 uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
697 handle_and_flags = (handle_and_flags & 0xcfffu) | (1u << 12u);
698 little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags);
699 }
700
701 // update header len
702 little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2u, current_acl_data_packet_length);
703
704 // count packet
705 connection->num_packets_sent++;
691 current_acl_data_packet_length = max_acl_data_packet_length;
692 }
693
694 // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent)
695 if (acl_header_pos > 0u){
696 uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
697 handle_and_flags = (handle_and_flags & 0xcfffu) | (1u << 12u);
698 little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags);
699 }
700
701 // update header len
702 little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2u, current_acl_data_packet_length);
703
704 // count packet
705 connection->num_packets_sent++;
706 log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", more_fragments);
706 log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", (int) more_fragments);
707
708 // update state for next fragment (if any) as "transport done" might be sent during send_packet already
709 if (more_fragments){
710 // update start of next fragment to send
711 hci_stack->acl_fragmentation_pos += current_acl_data_packet_length;
712 } else {
713 // done
714 hci_stack->acl_fragmentation_pos = 0;
715 hci_stack->acl_fragmentation_total_size = 0;
716 }
717
718 // send packet
719 uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos];
720 const int size = current_acl_data_packet_length + 4;
721 hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size);
722 hci_stack->acl_fragmentation_tx_active = 1;
723 err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
724
707
708 // update state for next fragment (if any) as "transport done" might be sent during send_packet already
709 if (more_fragments){
710 // update start of next fragment to send
711 hci_stack->acl_fragmentation_pos += current_acl_data_packet_length;
712 } else {
713 // done
714 hci_stack->acl_fragmentation_pos = 0;
715 hci_stack->acl_fragmentation_total_size = 0;
716 }
717
718 // send packet
719 uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos];
720 const int size = current_acl_data_packet_length + 4;
721 hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size);
722 hci_stack->acl_fragmentation_tx_active = 1;
723 err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
724
725 log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", more_fragments);
725 log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", (int) more_fragments);
726
727 // done yet?
728 if (!more_fragments) break;
729
730 // can send more?
731 if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err;
732 }
733

--- 540 unchanged lines hidden (view full) ---

1274 hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM;
1275 hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]);
1276 break;
1277 }
1278 case HCI_INIT_CUSTOM_INIT:
1279 // Custom initialization
1280 if (hci_stack->chipset && hci_stack->chipset->next_command){
1281 hci_stack->chipset_result = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer);
726
727 // done yet?
728 if (!more_fragments) break;
729
730 // can send more?
731 if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err;
732 }
733

--- 540 unchanged lines hidden (view full) ---

1274 hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM;
1275 hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]);
1276 break;
1277 }
1278 case HCI_INIT_CUSTOM_INIT:
1279 // Custom initialization
1280 if (hci_stack->chipset && hci_stack->chipset->next_command){
1281 hci_stack->chipset_result = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer);
1282 int send_cmd = 0;
1282 bool send_cmd = false;
1283 switch (hci_stack->chipset_result){
1284 case BTSTACK_CHIPSET_VALID_COMMAND:
1283 switch (hci_stack->chipset_result){
1284 case BTSTACK_CHIPSET_VALID_COMMAND:
1285 send_cmd = 1;
1285 send_cmd = true;
1286 hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT;
1287 break;
1288 case BTSTACK_CHIPSET_WARMSTART_REQUIRED:
1286 hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT;
1287 break;
1288 case BTSTACK_CHIPSET_WARMSTART_REQUIRED:
1289 send_cmd = 1;
1289 send_cmd = true;
1290 // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete
1291 log_info("CSR Warm Boot");
1292 btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
1293 btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
1294 btstack_run_loop_add_timer(&hci_stack->timeout);
1295 if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO)
1296 && hci_stack->config
1297 && hci_stack->chipset

--- 4871 unchanged lines hidden ---
1290 // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete
1291 log_info("CSR Warm Boot");
1292 btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
1293 btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
1294 btstack_run_loop_add_timer(&hci_stack->timeout);
1295 if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO)
1296 && hci_stack->config
1297 && hci_stack->chipset

--- 4871 unchanged lines hidden ---