• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 <memory>
20 
21 namespace bluetooth {
22 
23 class Packet;
24 
25 class PacketBuilder {
26  public:
27   virtual size_t size() const = 0;
28   virtual bool Serialize(const std::shared_ptr<Packet>& pkt) = 0;
29 
30   virtual ~PacketBuilder() = default;
31 
32  protected:
33   // Only Builders should be able to call these
34   void ReserveSpace(const std::shared_ptr<Packet>& pkt, size_t size);
AddPayloadOctets1(const std::shared_ptr<Packet> & pkt,uint8_t value)35   bool AddPayloadOctets1(const std::shared_ptr<Packet>& pkt, uint8_t value) {
36     return AddPayloadOctets(pkt, 1, value);
37   }
AddPayloadOctets2(const std::shared_ptr<Packet> & pkt,uint16_t value)38   bool AddPayloadOctets2(const std::shared_ptr<Packet>& pkt, uint16_t value) {
39     return AddPayloadOctets(pkt, 2, value);
40   }
AddPayloadOctets3(const std::shared_ptr<Packet> & pkt,uint32_t value)41   bool AddPayloadOctets3(const std::shared_ptr<Packet>& pkt, uint32_t value) {
42     return AddPayloadOctets(pkt, 3, value);
43   }
AddPayloadOctets4(const std::shared_ptr<Packet> & pkt,uint32_t value)44   bool AddPayloadOctets4(const std::shared_ptr<Packet>& pkt, uint32_t value) {
45     return AddPayloadOctets(pkt, 4, value);
46   }
AddPayloadOctets6(const std::shared_ptr<Packet> & pkt,uint64_t value)47   bool AddPayloadOctets6(const std::shared_ptr<Packet>& pkt, uint64_t value) {
48     return AddPayloadOctets(pkt, 6, value);
49   }
AddPayloadOctets8(const std::shared_ptr<Packet> & pkt,uint64_t value)50   bool AddPayloadOctets8(const std::shared_ptr<Packet>& pkt, uint64_t value) {
51     return AddPayloadOctets(pkt, 8, value);
52   }
53 
54  private:
55   // Add |octets| bytes to the payload.  Return true if:
56   // - the value of |value| fits in |octets| bytes and
57   // - the new size of the payload is still < |kMaxPayloadOctets|
58   bool AddPayloadOctets(const std::shared_ptr<Packet>& pkt, size_t octets,
59                         uint64_t value);
60 };
61 
62 }  // namespace bluetooth