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 <lib/fit/function.h>
17 
18 #include "pw_bluetooth_sapphire/internal/host/common/byte_buffer.h"
19 #include "pw_bluetooth_sapphire/internal/host/common/packet_view.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 {
24 
25 // Packet template specialization for SCO data packets. ScoDataPacket does not
26 // have a public ctor, so clients should use its |New| factory methods to
27 // instantiate it.
28 using ScoDataPacket = Packet<hci_spec::SynchronousDataHeader>;
29 using ScoPacketHandler =
30     fit::function<void(std::unique_ptr<ScoDataPacket> data_packet)>;
31 
32 template <>
33 class Packet<hci_spec::SynchronousDataHeader>
34     : public PacketBase<hci_spec::SynchronousDataHeader, ScoDataPacket> {
35  public:
36   // Slab-allocates a new ScoDataPacket with the given payload size without
37   // initializing its contents.
38   static std::unique_ptr<ScoDataPacket> New(uint8_t payload_size);
39 
40   // Slab-allocates a new ScoDataPacket with the given payload size and
41   // initializes the packet's header field with the given data.
42   static std::unique_ptr<ScoDataPacket> New(
43       hci_spec::ConnectionHandle connection_handle, uint8_t payload_size);
44 
45   // Getters for the header fields.
46   hci_spec::ConnectionHandle connection_handle() const;
47   hci_spec::SynchronousDataPacketStatusFlag packet_status_flag() const;
48 
49   // Initializes the internal PacketView by reading the header portion of the
50   // underlying buffer.
51   void InitializeFromBuffer();
52 
53  protected:
54   using PacketBase<hci_spec::SynchronousDataHeader, ScoDataPacket>::PacketBase;
55 
56  private:
57   // Writes the given header fields into the underlying buffer.
58   void WriteHeader(hci_spec::ConnectionHandle connection_handle);
59 };
60 
61 }  // namespace bt::hci
62