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 namespace bluetooth {
25 namespace packet {
26
RawBuilder(size_t max_bytes)27 RawBuilder::RawBuilder(size_t max_bytes) : max_bytes_(max_bytes) {}
RawBuilder(std::vector<uint8_t> vec)28 RawBuilder::RawBuilder(std::vector<uint8_t> vec) : payload_(std::move(vec)) {}
29
AddOctets(size_t octets,const std::vector<uint8_t> & bytes)30 bool RawBuilder::AddOctets(size_t octets, const std::vector<uint8_t>& bytes) {
31 if (payload_.size() + octets > max_bytes_) {
32 return false;
33 }
34 if (octets != bytes.size()) {
35 return false;
36 }
37 payload_.insert(payload_.end(), bytes.begin(), bytes.end());
38
39 return true;
40 }
41
AddOctets(const std::vector<uint8_t> & bytes)42 bool RawBuilder::AddOctets(const std::vector<uint8_t>& bytes) {
43 return AddOctets(bytes.size(), bytes);
44 }
45
AddOctets(size_t octets,uint64_t value)46 bool RawBuilder::AddOctets(size_t octets, uint64_t value) {
47 std::vector<uint8_t> val_vector;
48
49 uint64_t v = value;
50
51 if (octets > sizeof(uint64_t)) {
52 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) {
60 return false;
61 }
62 return AddOctets(octets, val_vector);
63 }
64
AddOctets1(uint8_t value)65 bool RawBuilder::AddOctets1(uint8_t value) {
66 return AddOctets(1, value);
67 }
68
AddOctets2(uint16_t value)69 bool RawBuilder::AddOctets2(uint16_t value) {
70 return AddOctets(2, value);
71 }
72
AddOctets3(uint32_t value)73 bool RawBuilder::AddOctets3(uint32_t value) {
74 return AddOctets(3, value);
75 }
76
AddOctets4(uint32_t value)77 bool RawBuilder::AddOctets4(uint32_t value) {
78 return AddOctets(4, value);
79 }
80
AddOctets6(uint64_t value)81 bool RawBuilder::AddOctets6(uint64_t value) {
82 return AddOctets(6, value);
83 }
84
AddOctets8(uint64_t value)85 bool RawBuilder::AddOctets8(uint64_t value) {
86 return AddOctets(8, value);
87 }
88
CanAddOctets(size_t num_bytes) const89 bool RawBuilder::CanAddOctets(size_t num_bytes) const {
90 return payload_.size() + num_bytes <= max_bytes_;
91 }
92
Serialize(BitInserter & it) const93 void RawBuilder::Serialize(BitInserter& it) const {
94 for (const auto& val : payload_) {
95 insert(val, it);
96 }
97 }
98
size() const99 size_t RawBuilder::size() const {
100 return payload_.size();
101 }
102 } // namespace packet
103 } // namespace bluetooth
104