1 /******************************************************************************
2 *
3 * Copyright 2018 The Android Open Source Project
4 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 ******************************************************************************/
18
19 #include "stack_test_packet_utils.h"
20
21 #include <bluetooth/log.h>
22
23 #include "osi/include/allocator.h"
24 #include "stack/include/bt_hdr.h"
25 #include "stack/include/l2cap_types.h"
26 #include "stack/include/l2cdefs.h"
27
28 namespace bluetooth {
29
CreateL2capDataPacket(uint16_t lcid,const std::vector<uint8_t> & data)30 std::vector<uint8_t> CreateL2capDataPacket(uint16_t lcid, const std::vector<uint8_t>& data) {
31 // Data in little endian order
32 std::vector<uint8_t> result;
33 auto data_size = static_cast<uint16_t>(data.size());
34 result.push_back(static_cast<uint8_t>(data_size));
35 result.push_back(static_cast<uint8_t>(data_size >> 8));
36 result.push_back(static_cast<uint8_t>(lcid));
37 result.push_back(static_cast<uint8_t>(lcid >> 8));
38 result.insert(result.end(), data.begin(), data.end());
39 return result;
40 }
41
CreateAclPacket(uint16_t handle,uint8_t pb,uint8_t bc,const std::vector<uint8_t> & data)42 std::vector<uint8_t> CreateAclPacket(uint16_t handle, uint8_t pb, uint8_t bc,
43 const std::vector<uint8_t>& data) {
44 // Data in little endian order
45 std::vector<uint8_t> result;
46 result.push_back(static_cast<uint8_t>(handle & 0x0F));
47 uint8_t second_byte = 0;
48 second_byte |= (bc << 6) & 0b11000000;
49 second_byte |= (pb << 4) & 0b00110000;
50 second_byte |= (handle >> 8) & 0b00001111;
51 result.push_back(second_byte);
52 auto data_size = static_cast<uint16_t>(data.size());
53 result.push_back(static_cast<uint8_t>(data_size));
54 result.push_back(static_cast<uint8_t>(data_size >> 8));
55 result.insert(result.end(), data.begin(), data.end());
56 return result;
57 }
58
AllocateWrappedIncomingL2capAclPacket(const uint8_t * acl_packet_bytes,size_t buffer_length)59 BT_HDR* AllocateWrappedIncomingL2capAclPacket(const uint8_t* acl_packet_bytes,
60 size_t buffer_length) {
61 size_t packet_size = buffer_length + BT_HDR_SIZE;
62 auto packet = reinterpret_cast<BT_HDR*>(osi_malloc(packet_size));
63 // Add ACL packet overhead + L2CAP packet overhead
64 packet->offset = 4 + L2CAP_PKT_OVERHEAD;
65 packet->len = static_cast<uint16_t>(buffer_length - 4 - L2CAP_PKT_OVERHEAD);
66 packet->layer_specific = 0;
67 packet->event = 0x1100; // MSG_HC_TO_STACK_HCI_ACL;
68 memcpy(packet->data, acl_packet_bytes, buffer_length);
69 return packet;
70 }
71
AllocateWrappedIncomingL2capAclPacket(const std::vector<uint8_t> & buffer)72 BT_HDR* AllocateWrappedIncomingL2capAclPacket(const std::vector<uint8_t>& buffer) {
73 return AllocateWrappedIncomingL2capAclPacket(buffer.data(), buffer.size());
74 }
75
AllocateWrappedOutgoingL2capAclPacket(const uint8_t * acl_packet_bytes,size_t buffer_length)76 BT_HDR* AllocateWrappedOutgoingL2capAclPacket(const uint8_t* acl_packet_bytes,
77 size_t buffer_length) {
78 size_t acl_l2cap_header_size = 4 + L2CAP_PKT_OVERHEAD;
79 log::assert_that(L2CAP_MIN_OFFSET >= static_cast<int>(acl_l2cap_header_size),
80 "invalid acl l2cap header size");
81 size_t packet_size = BT_HDR_SIZE + L2CAP_MIN_OFFSET + buffer_length - acl_l2cap_header_size;
82 auto packet = reinterpret_cast<BT_HDR*>(osi_malloc(packet_size));
83 packet->offset = L2CAP_MIN_OFFSET;
84 packet->len = static_cast<uint16_t>(buffer_length - acl_l2cap_header_size);
85 packet->layer_specific = 0;
86 packet->event = 0;
87 memcpy(packet->data + packet->offset - acl_l2cap_header_size, acl_packet_bytes, buffer_length);
88 return packet;
89 }
90
AllocateWrappedOutgoingL2capAclPacket(const std::vector<uint8_t> & buffer)91 BT_HDR* AllocateWrappedOutgoingL2capAclPacket(const std::vector<uint8_t>& buffer) {
92 return AllocateWrappedOutgoingL2capAclPacket(buffer.data(), buffer.size());
93 }
94
95 } // namespace bluetooth
96