1 /******************************************************************************
3 * Copyright (C) 2014 Google, Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 #define LOG_TAG "bt_hci_packet_fragmenter"
23 #include "buffer_allocator.h"
24 #include "device/include/controller.h"
25 #include "osi/include/hash_map.h"
26 #include "hci_internals.h"
27 #include "hci_layer.h"
28 #include "packet_fragmenter.h"
29 #include "osi/include/osi.h"
30 #include "osi/include/hash_functions.h"
31 #include "osi/include/log.h"
33 #define APPLY_CONTINUATION_FLAG(handle) (((handle) & 0xCFFF) | 0x1000)
34 #define APPLY_START_FLAG(handle) (((handle) & 0xCFFF) | 0x2000)
35 #define SUB_EVENT(event) ((event) & MSG_SUB_EVT_MASK)
36 #define GET_BOUNDARY_FLAG(handle) (((handle) >> 12) & 0x0003)
38 #define HANDLE_MASK 0x0FFF
39 #define START_PACKET_BOUNDARY 2
40 #define CONTINUATION_PACKET_BOUNDARY 1
41 #define L2CAP_HEADER_SIZE 4
43 // TODO(zachoverflow): find good value for this
44 #define NUMBER_OF_BUCKETS 42
46 // Our interface and callbacks
47 static const packet_fragmenter_t interface;
48 static const allocator_t *buffer_allocator;
49 static const controller_t *controller;
50 static const packet_fragmenter_callbacks_t *callbacks;
52 static hash_map_t *partial_packets;
54 static void init(const packet_fragmenter_callbacks_t *result_callbacks) {
55 callbacks = result_callbacks;
56 partial_packets = hash_map_new(NUMBER_OF_BUCKETS, hash_function_naive, NULL, NULL, NULL);
59 static void cleanup() {
61 hash_map_free(partial_packets);
64 static void fragment_and_dispatch(BT_HDR *packet) {
65 assert(packet != NULL);
67 uint16_t event = packet->event & MSG_EVT_MASK;
68 uint8_t *stream = packet->data + packet->offset;
70 // We only fragment ACL packets
71 if (event != MSG_STACK_TO_HC_HCI_ACL) {
72 callbacks->fragmented(packet, true);
76 uint16_t max_data_size =
77 SUB_EVENT(packet->event) == LOCAL_BR_EDR_CONTROLLER_ID ?
78 controller->get_acl_data_size_classic() :
79 controller->get_acl_data_size_ble();
81 uint16_t max_packet_size = max_data_size + HCI_ACL_PREAMBLE_SIZE;
82 uint16_t remaining_length = packet->len;
84 uint16_t continuation_handle;
85 STREAM_TO_UINT16(continuation_handle, stream);
86 continuation_handle = APPLY_CONTINUATION_FLAG(continuation_handle);
88 while (remaining_length > max_packet_size) {
89 // Make sure we use the right ACL packet size
90 stream = packet->data + packet->offset;
91 STREAM_SKIP_UINT16(stream);
92 UINT16_TO_STREAM(stream, max_data_size);
94 packet->len = max_packet_size;
95 callbacks->fragmented(packet, false);
97 packet->offset += max_data_size;
98 remaining_length -= max_data_size;
99 packet->len = remaining_length;
101 // Write the ACL header for the next fragment
102 stream = packet->data + packet->offset;
103 UINT16_TO_STREAM(stream, continuation_handle);
104 UINT16_TO_STREAM(stream, remaining_length - HCI_ACL_PREAMBLE_SIZE);
106 // Apparently L2CAP can set layer_specific to a max number of segments to transmit
107 if (packet->layer_specific) {
108 packet->layer_specific--;
110 if (packet->layer_specific == 0) {
111 packet->event = MSG_HC_TO_STACK_L2C_SEG_XMIT;
112 callbacks->transmit_finished(packet, false);
118 callbacks->fragmented(packet, true);
121 static void reassemble_and_dispatch(UNUSED_ATTR BT_HDR *packet) {
122 if ((packet->event & MSG_EVT_MASK) == MSG_HC_TO_STACK_HCI_ACL) {
123 uint8_t *stream = packet->data;
125 uint16_t l2cap_length;
128 STREAM_TO_UINT16(handle, stream);
129 STREAM_TO_UINT16(acl_length, stream);
130 STREAM_TO_UINT16(l2cap_length, stream);
132 assert(acl_length == packet->len - HCI_ACL_PREAMBLE_SIZE);
134 uint8_t boundary_flag = GET_BOUNDARY_FLAG(handle);
135 handle = handle & HANDLE_MASK;
137 BT_HDR *partial_packet = (BT_HDR *)hash_map_get(partial_packets, (void *)(uintptr_t)handle);
139 if (boundary_flag == START_PACKET_BOUNDARY) {
140 if (partial_packet) {
141 LOG_WARN("%s found unfinished packet for handle with start packet. Dropping old.", __func__);
143 hash_map_erase(partial_packets, (void *)(uintptr_t)handle);
144 buffer_allocator->free(partial_packet);
147 uint16_t full_length = l2cap_length + L2CAP_HEADER_SIZE + HCI_ACL_PREAMBLE_SIZE;
148 if (full_length <= packet->len) {
149 if (full_length < packet->len)
150 LOG_WARN("%s found l2cap full length %d less than the hci length %d.", __func__, l2cap_length, packet->len);
152 callbacks->reassembled(packet);
156 partial_packet = (BT_HDR *)buffer_allocator->alloc(full_length + sizeof(BT_HDR));
157 partial_packet->event = packet->event;
158 partial_packet->len = full_length;
159 partial_packet->offset = packet->len;
161 memcpy(partial_packet->data, packet->data, packet->len);
163 // Update the ACL data size to indicate the full expected length
164 stream = partial_packet->data;
165 STREAM_SKIP_UINT16(stream); // skip the handle
166 UINT16_TO_STREAM(stream, full_length - HCI_ACL_PREAMBLE_SIZE);
168 hash_map_set(partial_packets, (void *)(uintptr_t)handle, partial_packet);
169 // Free the old packet buffer, since we don't need it anymore
170 buffer_allocator->free(packet);
172 if (!partial_packet) {
173 LOG_WARN("%s got continuation for unknown packet. Dropping it.", __func__);
174 buffer_allocator->free(packet);
178 packet->offset = HCI_ACL_PREAMBLE_SIZE;
179 uint16_t projected_offset = partial_packet->offset + (packet->len - HCI_ACL_PREAMBLE_SIZE);
180 if (projected_offset > partial_packet->len) { // len stores the expected length
181 LOG_WARN("%s got packet which would exceed expected length of %d. Truncating.", __func__, partial_packet->len);
182 packet->len = partial_packet->len - partial_packet->offset;
183 projected_offset = partial_packet->len;
187 partial_packet->data + partial_packet->offset,
188 packet->data + packet->offset,
189 packet->len - packet->offset
192 // Free the old packet buffer, since we don't need it anymore
193 buffer_allocator->free(packet);
194 partial_packet->offset = projected_offset;
196 if (partial_packet->offset == partial_packet->len) {
197 hash_map_erase(partial_packets, (void *)(uintptr_t)handle);
198 partial_packet->offset = 0;
199 callbacks->reassembled(partial_packet);
203 callbacks->reassembled(packet);
207 static const packet_fragmenter_t interface = {
211 fragment_and_dispatch,
212 reassemble_and_dispatch
215 const packet_fragmenter_t *packet_fragmenter_get_interface() {
216 controller = controller_get_interface();
217 buffer_allocator = buffer_allocator_get_interface();
221 const packet_fragmenter_t *packet_fragmenter_get_test_interface(
222 const controller_t *controller_interface,
223 const allocator_t *buffer_allocator_interface) {
224 controller = controller_interface;
225 buffer_allocator = buffer_allocator_interface;