1 /*
2 * Copyright 2019 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 #include "packet/raw_builder.h"
18
19 #include <algorithm>
20 #include <utility>
21
22 #include "os/log.h"
23
24 using bluetooth::hci::Address;
25 using std::vector;
26
27 namespace bluetooth {
28 namespace packet {
29
RawBuilder(size_t max_bytes)30 RawBuilder::RawBuilder(size_t max_bytes) : max_bytes_(max_bytes) {}
RawBuilder(std::vector<uint8_t> vec)31 RawBuilder::RawBuilder(std::vector<uint8_t> vec) : payload_(std::move(vec)) {}
32
AddOctets(size_t octets,const vector<uint8_t> & bytes)33 bool RawBuilder::AddOctets(size_t octets, const vector<uint8_t>& bytes) {
34 if (payload_.size() + octets > max_bytes_) return false;
35
36 if (octets != bytes.size()) return false;
37
38 payload_.insert(payload_.end(), bytes.begin(), bytes.end());
39
40 return true;
41 }
42
AddOctets(const vector<uint8_t> & bytes)43 bool RawBuilder::AddOctets(const vector<uint8_t>& bytes) {
44 return AddOctets(bytes.size(), bytes);
45 }
46
AddOctets(size_t octets,uint64_t value)47 bool RawBuilder::AddOctets(size_t octets, uint64_t value) {
48 vector<uint8_t> val_vector;
49
50 uint64_t v = value;
51
52 if (octets > sizeof(uint64_t)) return false;
53
54 for (size_t i = 0; i < octets; i++) {
55 val_vector.push_back(v & 0xff);
56 v = v >> 8;
57 }
58
59 if (v != 0) return false;
60
61 return AddOctets(octets, val_vector);
62 }
63
AddAddress(const Address & address)64 bool RawBuilder::AddAddress(const Address& address) {
65 if (payload_.size() + Address::kLength > max_bytes_) return false;
66
67 for (size_t i = 0; i < Address::kLength; i++) {
68 payload_.push_back(address.address[i]);
69 }
70 return true;
71 }
72
AddOctets1(uint8_t value)73 bool RawBuilder::AddOctets1(uint8_t value) {
74 return AddOctets(1, value);
75 }
76
AddOctets2(uint16_t value)77 bool RawBuilder::AddOctets2(uint16_t value) {
78 return AddOctets(2, value);
79 }
80
AddOctets3(uint32_t value)81 bool RawBuilder::AddOctets3(uint32_t value) {
82 return AddOctets(3, value);
83 }
84
AddOctets4(uint32_t value)85 bool RawBuilder::AddOctets4(uint32_t value) {
86 return AddOctets(4, value);
87 }
88
AddOctets6(uint64_t value)89 bool RawBuilder::AddOctets6(uint64_t value) {
90 return AddOctets(6, value);
91 }
92
AddOctets8(uint64_t value)93 bool RawBuilder::AddOctets8(uint64_t value) {
94 return AddOctets(8, value);
95 }
96
CanAddOctets(size_t num_bytes) const97 bool RawBuilder::CanAddOctets(size_t num_bytes) const {
98 return payload_.size() + num_bytes <= max_bytes_;
99 }
100
Serialize(BitInserter & it) const101 void RawBuilder::Serialize(BitInserter& it) const {
102 for (const auto& val : payload_) {
103 insert(val, it);
104 }
105 }
106
size() const107 size_t RawBuilder::size() const {
108 return payload_.size();
109 }
110 } // namespace packet
111 } // namespace bluetooth
112