• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "acl_packet"
18 
19 #include "acl_packet.h"
20 
21 #include "base/logging.h"
22 #include "osi/include/log.h"
23 #include "stack/include/hcidefs.h"
24 
25 using std::vector;
26 
27 namespace test_vendor_lib {
28 
AclPacket(uint16_t channel,AclPacket::PacketBoundaryFlags packet_boundary,AclPacket::BroadcastFlags broadcast)29 AclPacket::AclPacket(uint16_t channel,
30                      AclPacket::PacketBoundaryFlags packet_boundary,
31                      AclPacket::BroadcastFlags broadcast)
32     : raw_packet_({static_cast<uint8_t>(channel & 0xff),
33                    static_cast<uint8_t>(((channel >> 8) & 0x0f) |
34                                         ((packet_boundary & 0x3) << 4) |
35                                         (broadcast & 0x3) << 6),
36                    0x00, 0x00}) {}
37 
AddPayloadOctets(size_t octets,const vector<uint8_t> & bytes)38 void AclPacket::AddPayloadOctets(size_t octets, const vector<uint8_t>& bytes) {
39   CHECK(bytes.size() == octets);
40 
41   raw_packet_.insert(raw_packet_.end(), bytes.begin(), bytes.end());
42 
43   raw_packet_[2] =
44       static_cast<uint8_t>((raw_packet_.size() - kHeaderSize) & 0xff);
45   raw_packet_[3] =
46       static_cast<uint8_t>(((raw_packet_.size() - kHeaderSize) >> 8) & 0xff);
47 }
48 
AddPayloadOctets(size_t octets,uint64_t value)49 void AclPacket::AddPayloadOctets(size_t octets, uint64_t value) {
50   vector<uint8_t> val_vector;
51 
52   uint64_t v = value;
53 
54   CHECK(octets <= sizeof(uint64_t));
55 
56   for (size_t i = 0; i < octets; i++) {
57     val_vector.push_back(v & 0xff);
58     v = v >> 8;
59   }
60 
61   CHECK(v == 0);
62 
63   AddPayloadOctets(octets, val_vector);
64 }
65 
GetPacketSize() const66 size_t AclPacket::GetPacketSize() const { return raw_packet_.size(); }
67 
GetPacket() const68 const std::vector<uint8_t>& AclPacket::GetPacket() const { return raw_packet_; }
69 
70 }  // namespace test_vendor_lib
71