Loading system/hci/Android.mk +2 −1 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ LOCAL_PATH := $(call my-dir) # HCI static library for target # ======================================================== include $(CLEAR_VARS) LOCAL_CPP_EXTENSION := .cc LOCAL_SRC_FILES := \ src/btsnoop.c \ Loading @@ -18,7 +19,7 @@ LOCAL_SRC_FILES := \ src/hci_packet_factory.c \ src/hci_packet_parser.c \ src/low_power_manager.c \ src/packet_fragmenter.c \ src/packet_fragmenter.cc \ src/vendor.c LOCAL_C_INCLUDES += \ Loading system/hci/include/buffer_allocator.h +8 −0 Original line number Diff line number Diff line Loading @@ -20,4 +20,12 @@ #include "osi/include/allocator.h" #ifdef __cplusplus extern "C" { #endif const allocator_t *buffer_allocator_get_interface(); #ifdef __cplusplus } #endif system/hci/include/packet_fragmenter.h +8 −0 Original line number Diff line number Diff line Loading @@ -22,6 +22,10 @@ #include "bt_types.h" #include "hci_layer.h" #ifdef __cplusplus extern "C" { #endif typedef void (*transmit_finished_cb)(BT_HDR *packet, bool all_fragments_sent); typedef void (*packet_reassembled_cb)(BT_HDR *packet); typedef void (*packet_fragmented_cb)(BT_HDR *packet, bool send_transmit_finished); Loading Loading @@ -58,3 +62,7 @@ const packet_fragmenter_t *packet_fragmenter_get_interface(); const packet_fragmenter_t *packet_fragmenter_get_test_interface( const controller_t *controller_interface, const allocator_t *buffer_allocator_interface); #ifdef __cplusplus } #endif system/hci/src/packet_fragmenter.c→system/hci/src/packet_fragmenter.cc +16 −19 Original line number Diff line number Diff line Loading @@ -22,13 +22,12 @@ #include <assert.h> #include <string.h> #include <unordered_map> #include "bt_target.h" #include "buffer_allocator.h" #include "device/include/controller.h" #include "hci_internals.h" #include "osi/include/hash_functions.h" #include "osi/include/hash_map.h" #include "osi/include/log.h" #include "osi/include/osi.h" Loading @@ -42,25 +41,20 @@ #define CONTINUATION_PACKET_BOUNDARY 1 #define L2CAP_HEADER_SIZE 4 // TODO(zachoverflow): find good value for this #define NUMBER_OF_BUCKETS 42 // Our interface and callbacks static const packet_fragmenter_t interface; static const allocator_t *buffer_allocator; static const controller_t *controller; static const packet_fragmenter_callbacks_t *callbacks; static hash_map_t *partial_packets; static std::unordered_map<uint16_t /* handle */, BT_HDR*> partial_packets; static void init(const packet_fragmenter_callbacks_t *result_callbacks) { callbacks = result_callbacks; partial_packets = hash_map_new(NUMBER_OF_BUCKETS, hash_function_naive, NULL, NULL, NULL); } static void cleanup() { if (partial_packets) hash_map_free(partial_packets); partial_packets.clear(); } static void fragment_and_dispatch(BT_HDR *packet) { Loading Loading @@ -140,14 +134,14 @@ static void reassemble_and_dispatch(UNUSED_ATTR BT_HDR *packet) { uint8_t boundary_flag = GET_BOUNDARY_FLAG(handle); handle = handle & HANDLE_MASK; BT_HDR *partial_packet = (BT_HDR *)hash_map_get(partial_packets, (void *)(uintptr_t)handle); if (boundary_flag == START_PACKET_BOUNDARY) { if (partial_packet) { auto map_iter = partial_packets.find(handle); if (map_iter != partial_packets.end()) { LOG_WARN(LOG_TAG, "%s found unfinished packet for handle with start packet. Dropping old.", __func__); hash_map_erase(partial_packets, (void *)(uintptr_t)handle); buffer_allocator->free(partial_packet); BT_HDR *hdl = map_iter->second; partial_packets.erase(map_iter); buffer_allocator->free(hdl); } if (acl_length < L2CAP_HEADER_SIZE) { Loading Loading @@ -175,7 +169,7 @@ static void reassemble_and_dispatch(UNUSED_ATTR BT_HDR *packet) { return; } partial_packet = (BT_HDR *)buffer_allocator->alloc(full_length + sizeof(BT_HDR)); BT_HDR *partial_packet = (BT_HDR *)buffer_allocator->alloc(full_length + sizeof(BT_HDR)); partial_packet->event = packet->event; partial_packet->len = full_length; partial_packet->offset = packet->len; Loading @@ -187,15 +181,18 @@ static void reassemble_and_dispatch(UNUSED_ATTR BT_HDR *packet) { STREAM_SKIP_UINT16(stream); // skip the handle UINT16_TO_STREAM(stream, full_length - HCI_ACL_PREAMBLE_SIZE); hash_map_set(partial_packets, (void *)(uintptr_t)handle, partial_packet); partial_packets[handle] = partial_packet; // Free the old packet buffer, since we don't need it anymore buffer_allocator->free(packet); } else { if (!partial_packet) { auto map_iter = partial_packets.find(handle); if (map_iter == partial_packets.end()) { LOG_WARN(LOG_TAG, "%s got continuation for unknown packet. Dropping it.", __func__); buffer_allocator->free(packet); return; } BT_HDR *partial_packet = map_iter->second; packet->offset = HCI_ACL_PREAMBLE_SIZE; uint16_t projected_offset = partial_packet->offset + (packet->len - HCI_ACL_PREAMBLE_SIZE); Loading @@ -216,7 +213,7 @@ static void reassemble_and_dispatch(UNUSED_ATTR BT_HDR *packet) { partial_packet->offset = projected_offset; if (partial_packet->offset == partial_packet->len) { hash_map_erase(partial_packets, (void *)(uintptr_t)handle); partial_packets.erase(handle); partial_packet->offset = 0; callbacks->reassembled(partial_packet); } Loading Loading
system/hci/Android.mk +2 −1 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ LOCAL_PATH := $(call my-dir) # HCI static library for target # ======================================================== include $(CLEAR_VARS) LOCAL_CPP_EXTENSION := .cc LOCAL_SRC_FILES := \ src/btsnoop.c \ Loading @@ -18,7 +19,7 @@ LOCAL_SRC_FILES := \ src/hci_packet_factory.c \ src/hci_packet_parser.c \ src/low_power_manager.c \ src/packet_fragmenter.c \ src/packet_fragmenter.cc \ src/vendor.c LOCAL_C_INCLUDES += \ Loading
system/hci/include/buffer_allocator.h +8 −0 Original line number Diff line number Diff line Loading @@ -20,4 +20,12 @@ #include "osi/include/allocator.h" #ifdef __cplusplus extern "C" { #endif const allocator_t *buffer_allocator_get_interface(); #ifdef __cplusplus } #endif
system/hci/include/packet_fragmenter.h +8 −0 Original line number Diff line number Diff line Loading @@ -22,6 +22,10 @@ #include "bt_types.h" #include "hci_layer.h" #ifdef __cplusplus extern "C" { #endif typedef void (*transmit_finished_cb)(BT_HDR *packet, bool all_fragments_sent); typedef void (*packet_reassembled_cb)(BT_HDR *packet); typedef void (*packet_fragmented_cb)(BT_HDR *packet, bool send_transmit_finished); Loading Loading @@ -58,3 +62,7 @@ const packet_fragmenter_t *packet_fragmenter_get_interface(); const packet_fragmenter_t *packet_fragmenter_get_test_interface( const controller_t *controller_interface, const allocator_t *buffer_allocator_interface); #ifdef __cplusplus } #endif
system/hci/src/packet_fragmenter.c→system/hci/src/packet_fragmenter.cc +16 −19 Original line number Diff line number Diff line Loading @@ -22,13 +22,12 @@ #include <assert.h> #include <string.h> #include <unordered_map> #include "bt_target.h" #include "buffer_allocator.h" #include "device/include/controller.h" #include "hci_internals.h" #include "osi/include/hash_functions.h" #include "osi/include/hash_map.h" #include "osi/include/log.h" #include "osi/include/osi.h" Loading @@ -42,25 +41,20 @@ #define CONTINUATION_PACKET_BOUNDARY 1 #define L2CAP_HEADER_SIZE 4 // TODO(zachoverflow): find good value for this #define NUMBER_OF_BUCKETS 42 // Our interface and callbacks static const packet_fragmenter_t interface; static const allocator_t *buffer_allocator; static const controller_t *controller; static const packet_fragmenter_callbacks_t *callbacks; static hash_map_t *partial_packets; static std::unordered_map<uint16_t /* handle */, BT_HDR*> partial_packets; static void init(const packet_fragmenter_callbacks_t *result_callbacks) { callbacks = result_callbacks; partial_packets = hash_map_new(NUMBER_OF_BUCKETS, hash_function_naive, NULL, NULL, NULL); } static void cleanup() { if (partial_packets) hash_map_free(partial_packets); partial_packets.clear(); } static void fragment_and_dispatch(BT_HDR *packet) { Loading Loading @@ -140,14 +134,14 @@ static void reassemble_and_dispatch(UNUSED_ATTR BT_HDR *packet) { uint8_t boundary_flag = GET_BOUNDARY_FLAG(handle); handle = handle & HANDLE_MASK; BT_HDR *partial_packet = (BT_HDR *)hash_map_get(partial_packets, (void *)(uintptr_t)handle); if (boundary_flag == START_PACKET_BOUNDARY) { if (partial_packet) { auto map_iter = partial_packets.find(handle); if (map_iter != partial_packets.end()) { LOG_WARN(LOG_TAG, "%s found unfinished packet for handle with start packet. Dropping old.", __func__); hash_map_erase(partial_packets, (void *)(uintptr_t)handle); buffer_allocator->free(partial_packet); BT_HDR *hdl = map_iter->second; partial_packets.erase(map_iter); buffer_allocator->free(hdl); } if (acl_length < L2CAP_HEADER_SIZE) { Loading Loading @@ -175,7 +169,7 @@ static void reassemble_and_dispatch(UNUSED_ATTR BT_HDR *packet) { return; } partial_packet = (BT_HDR *)buffer_allocator->alloc(full_length + sizeof(BT_HDR)); BT_HDR *partial_packet = (BT_HDR *)buffer_allocator->alloc(full_length + sizeof(BT_HDR)); partial_packet->event = packet->event; partial_packet->len = full_length; partial_packet->offset = packet->len; Loading @@ -187,15 +181,18 @@ static void reassemble_and_dispatch(UNUSED_ATTR BT_HDR *packet) { STREAM_SKIP_UINT16(stream); // skip the handle UINT16_TO_STREAM(stream, full_length - HCI_ACL_PREAMBLE_SIZE); hash_map_set(partial_packets, (void *)(uintptr_t)handle, partial_packet); partial_packets[handle] = partial_packet; // Free the old packet buffer, since we don't need it anymore buffer_allocator->free(packet); } else { if (!partial_packet) { auto map_iter = partial_packets.find(handle); if (map_iter == partial_packets.end()) { LOG_WARN(LOG_TAG, "%s got continuation for unknown packet. Dropping it.", __func__); buffer_allocator->free(packet); return; } BT_HDR *partial_packet = map_iter->second; packet->offset = HCI_ACL_PREAMBLE_SIZE; uint16_t projected_offset = partial_packet->offset + (packet->len - HCI_ACL_PREAMBLE_SIZE); Loading @@ -216,7 +213,7 @@ static void reassemble_and_dispatch(UNUSED_ATTR BT_HDR *packet) { partial_packet->offset = projected_offset; if (partial_packet->offset == partial_packet->len) { hash_map_erase(partial_packets, (void *)(uintptr_t)handle); partial_packets.erase(handle); partial_packet->offset = 0; callbacks->reassembled(partial_packet); } Loading