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