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 namespace bluetooth {
23 namespace packet {
24
RawBuilder(size_t max_bytes)25 RawBuilder::RawBuilder(size_t max_bytes) : max_bytes_(max_bytes) {}
RawBuilder(std::vector<uint8_t> vec)26 RawBuilder::RawBuilder(std::vector<uint8_t> vec) : payload_(std::move(vec)) {}
27
AddOctets(size_t octets,const std::vector<uint8_t> & bytes)28 bool RawBuilder::AddOctets(size_t octets, const std::vector<uint8_t>& bytes) {
29 if (payload_.size() + octets > max_bytes_) {
30 return false;
31 }
32 if (octets != bytes.size()) {
33 return false;
34 }
35 payload_.insert(payload_.end(), bytes.begin(), bytes.end());
36
37 return true;
38 }
39
AddOctets(const std::vector<uint8_t> & bytes)40 bool RawBuilder::AddOctets(const std::vector<uint8_t>& bytes) {
41 return AddOctets(bytes.size(), bytes);
42 }
43
AddOctets(size_t octets,uint64_t value)44 bool RawBuilder::AddOctets(size_t octets, uint64_t value) {
45 std::vector<uint8_t> val_vector;
46
47 uint64_t v = value;
48
49 if (octets > sizeof(uint64_t)) {
50 return false;
51 }
52 for (size_t i = 0; i < octets; i++) {
53 val_vector.push_back(v & 0xff);
54 v = v >> 8;
55 }
56
57 if (v != 0) {
58 return false;
59 }
60 return AddOctets(octets, val_vector);
61 }
62
AddOctets1(uint8_t value)63 bool RawBuilder::AddOctets1(uint8_t value) { return AddOctets(1, value); }
64
AddOctets2(uint16_t value)65 bool RawBuilder::AddOctets2(uint16_t value) { return AddOctets(2, value); }
66
AddOctets3(uint32_t value)67 bool RawBuilder::AddOctets3(uint32_t value) { return AddOctets(3, value); }
68
AddOctets4(uint32_t value)69 bool RawBuilder::AddOctets4(uint32_t value) { return AddOctets(4, value); }
70
AddOctets6(uint64_t value)71 bool RawBuilder::AddOctets6(uint64_t value) { return AddOctets(6, value); }
72
AddOctets8(uint64_t value)73 bool RawBuilder::AddOctets8(uint64_t value) { return AddOctets(8, value); }
74
CanAddOctets(size_t num_bytes) const75 bool RawBuilder::CanAddOctets(size_t num_bytes) const {
76 return payload_.size() + num_bytes <= max_bytes_;
77 }
78
Serialize(BitInserter & it) const79 void RawBuilder::Serialize(BitInserter& it) const {
80 for (const auto& val : payload_) {
81 insert(val, it);
82 }
83 }
84
size() const85 size_t RawBuilder::size() const { return payload_.size(); }
86 } // namespace packet
87 } // namespace bluetooth
88