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 #pragma once
18 
19 #include <cstdint>
20 #include <vector>
21 
22 #include "hci/address.h"
23 #include "packet/bit_inserter.h"
24 #include "packet/packet_builder.h"
25 
26 namespace bluetooth {
27 namespace packet {
28 
29 class RawBuilder : public PacketBuilder<true> {
30  public:
31   RawBuilder() = default;
32   explicit RawBuilder(size_t max_bytes);
33   explicit RawBuilder(std::vector<uint8_t> vec);
34   virtual ~RawBuilder() = default;
35 
36   virtual size_t size() const override;
37 
38   virtual void Serialize(BitInserter& it) const;
39 
40   // Add |address| to the payload.  Return true if:
41   // - the new size of the payload is still <= |max_bytes_|
42   bool AddAddress(const hci::Address& address);
43 
44   // Return true if |num_bytes| can be added to the payload.
45   bool CanAddOctets(size_t num_bytes) const;
46 
47   // Add |octets| bytes to the payload.  Return true if:
48   // - the size of |bytes| is equal to |octets| and
49   // - the new size of the payload is still <= |max_bytes_|
50   bool AddOctets(size_t octets, const std::vector<uint8_t>& bytes);
51 
52   bool AddOctets(const std::vector<uint8_t>& bytes);
53 
54   bool AddOctets1(uint8_t value);
55   bool AddOctets2(uint16_t value);
56   bool AddOctets3(uint32_t value);
57   bool AddOctets4(uint32_t value);
58   bool AddOctets6(uint64_t value);
59   bool AddOctets8(uint64_t value);
60 
61  private:
62   // Add |octets| bytes to the payload.  Return true if:
63   // - the value of |value| fits in |octets| bytes and
64   // - the new size of the payload is still <= |max_bytes_|
65   bool AddOctets(size_t octets, uint64_t value);
66 
67   size_t max_bytes_{0xffff};
68 
69   // Underlying containers for storing the actual packet
70   std::vector<uint8_t> payload_;
71 };
72 
73 }  // namespace packet
74 }  // namespace bluetooth
75