• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #pragma once
16 #include <memory>
17 
18 #include "pw_bluetooth_sapphire/internal/host/common/macros.h"
19 #include "pw_bluetooth_sapphire/internal/host/hci-spec/constants.h"
20 #include "pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h"
21 #include "pw_bluetooth_sapphire/internal/host/transport/packet.h"
22 
23 namespace bt::hci::allocators {
24 
25 // Slab sizes for control (command/event) and ACL data packets used by the slab
26 // allocators. These are used by the CommandPacket, EventPacket, and
27 // ACLDataPacket classes.
28 
29 // TODO(armansito): The slab sizes below are arbitrary; fine tune them based on
30 // usage.
31 constexpr size_t kMaxControlSlabSize = 65536;  // 64K
32 constexpr size_t kMaxACLSlabSize = 65536;      // 64K
33 constexpr size_t kMaxScoSlabSize = 33024;  // exactly 128 max size SCO packets
34 constexpr size_t kMaxNumSlabs = 100;
35 
36 // The largest possible control packet size.
37 constexpr size_t kLargeControlPayloadSize =
38     hci_spec::kMaxCommandPacketPayloadSize;
39 constexpr size_t kLargeControlPacketSize =
40     sizeof(hci_spec::CommandHeader) + kLargeControlPayloadSize;
41 constexpr size_t kNumLargeControlPackets =
42     kMaxControlSlabSize / kLargeControlPacketSize;
43 
44 // The average HCI control packet payload size. Most packets are under 16 bytes.
45 constexpr size_t kSmallControlPayloadSize = 16;
46 constexpr size_t kSmallControlPacketSize =
47     sizeof(hci_spec::CommandHeader) + kSmallControlPayloadSize;
48 constexpr size_t kNumSmallControlPackets =
49     kMaxControlSlabSize / kSmallControlPacketSize;
50 
51 // Large, medium, and small buffer sizes for ACL data packets.
52 constexpr size_t kLargeACLDataPayloadSize = hci_spec::kMaxACLPayloadSize;
53 constexpr size_t kLargeACLDataPacketSize =
54     sizeof(hci_spec::ACLDataHeader) + kLargeACLDataPayloadSize;
55 constexpr size_t kNumLargeACLDataPackets =
56     kMaxACLSlabSize / kLargeACLDataPacketSize;
57 
58 constexpr size_t kMediumACLDataPayloadSize = 256;
59 constexpr size_t kMediumACLDataPacketSize =
60     sizeof(hci_spec::ACLDataHeader) + kMediumACLDataPayloadSize;
61 constexpr size_t kNumMediumACLDataPackets =
62     kMaxACLSlabSize / kMediumACLDataPacketSize;
63 
64 constexpr size_t kSmallACLDataPayloadSize = 64;
65 constexpr size_t kSmallACLDataPacketSize =
66     sizeof(hci_spec::ACLDataHeader) + kSmallACLDataPayloadSize;
67 constexpr size_t kNumSmallACLDataPackets =
68     kMaxACLSlabSize / kSmallACLDataPacketSize;
69 
70 constexpr size_t kMaxScoDataPayloadSize =
71     hci_spec::kMaxSynchronousDataPacketPayloadSize;
72 constexpr size_t kMaxScoDataPacketSize =
73     sizeof(hci_spec::SynchronousDataHeader) + kMaxScoDataPayloadSize;
74 constexpr size_t kNumMaxScoDataPackets =
75     kMaxScoSlabSize / kMaxScoDataPacketSize;
76 
77 namespace internal {
78 
79 template <size_t BufferSize>
80 class FixedSizePacketStorage {
81  protected:
82   StaticByteBuffer<BufferSize> buffer_;
83 };
84 
85 // A FixedSizePacket provides fixed-size buffer storage for Packets and is the
86 // basis for a slab-allocated Packet. Multiple inheritance is required to
87 // initialize the underlying buffer before PacketBase.
88 template <typename HeaderType, size_t BufferSize>
89 class FixedSizePacket : public FixedSizePacketStorage<BufferSize>,
90                         public Packet<HeaderType> {
91  public:
92   explicit FixedSizePacket(size_t payload_size = 0u)
93       : Packet<HeaderType>(
94             MutablePacketView<HeaderType>(&this->buffer_, payload_size)) {}
95 
96   ~FixedSizePacket() override = default;
97 
98   FixedSizePacket(const FixedSizePacket&) = delete;
99   FixedSizePacket& operator=(const FixedSizePacket&) = delete;
100 };
101 
102 }  // namespace internal
103 
104 }  // namespace bt::hci::allocators
105