blob: ec2ff686ab094fc5d0c62facac00ca4abdf7e4a8 [file] [log] [blame]
Myles Watson0ead5972019-04-01 13:21:25 -07001/*
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>
Myles Watson44b68e22019-11-22 10:34:11 -080020#include <utility>
Myles Watson0ead5972019-04-01 13:21:25 -070021
22#include "os/log.h"
23
Martin Brabham94db40c2019-03-29 10:24:52 -070024using bluetooth::hci::Address;
Myles Watson0ead5972019-04-01 13:21:25 -070025using std::vector;
Myles Watson0ead5972019-04-01 13:21:25 -070026
27namespace bluetooth {
28namespace packet {
29
30RawBuilder::RawBuilder(size_t max_bytes) : max_bytes_(max_bytes) {}
Myles Watson44b68e22019-11-22 10:34:11 -080031RawBuilder::RawBuilder(std::vector<uint8_t> vec) : payload_(std::move(vec)) {}
Myles Watson0ead5972019-04-01 13:21:25 -070032
33bool 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
43bool RawBuilder::AddOctets(const vector<uint8_t>& bytes) {
44 return AddOctets(bytes.size(), bytes);
45}
46
47bool 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
64bool 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
73bool RawBuilder::AddOctets1(uint8_t value) {
74 return AddOctets(1, value);
75}
76
77bool RawBuilder::AddOctets2(uint16_t value) {
78 return AddOctets(2, value);
79}
80
81bool RawBuilder::AddOctets3(uint32_t value) {
82 return AddOctets(3, value);
83}
84
85bool RawBuilder::AddOctets4(uint32_t value) {
86 return AddOctets(4, value);
87}
88
89bool RawBuilder::AddOctets6(uint64_t value) {
90 return AddOctets(6, value);
91}
92
93bool RawBuilder::AddOctets8(uint64_t value) {
94 return AddOctets(8, value);
95}
96
97bool RawBuilder::CanAddOctets(size_t num_bytes) const {
98 return payload_.size() + num_bytes <= max_bytes_;
99}
100
101void RawBuilder::Serialize(BitInserter& it) const {
102 for (const auto& val : payload_) {
103 insert(val, it);
104 }
105}
106
107size_t RawBuilder::size() const {
108 return payload_.size();
109}
110} // namespace packet
111} // namespace bluetooth