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