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 <memory> 19 20 #include "pw_bluetooth_sapphire/internal/host/common/byte_buffer.h" 21 #include "pw_bluetooth_sapphire/internal/host/common/macros.h" 22 #include "pw_bluetooth_sapphire/internal/host/common/packet_view.h" 23 #include "pw_bluetooth_sapphire/internal/host/common/trace.h" 24 #include "pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h" 25 #include "pw_bluetooth_sapphire/internal/host/transport/packet.h" 26 27 namespace bt::hci { 28 29 // Packet template specialization for ACL data packets. This cannot be directly 30 // instantiated. Represents a HCI ACL data packet. 31 using ACLDataPacket = Packet<hci_spec::ACLDataHeader>; 32 using ACLDataPacketPtr = std::unique_ptr<ACLDataPacket>; 33 using ACLPacketHandler = fit::function<void(ACLDataPacketPtr data_packet)>; 34 35 template <> 36 class Packet<hci_spec::ACLDataHeader> 37 : public PacketBase<hci_spec::ACLDataHeader, ACLDataPacket> { 38 public: 39 // Slab-allocates a new ACLDataPacket with the given payload size without 40 // initializing its contents. 41 static ACLDataPacketPtr New(uint16_t payload_size); 42 43 // Slab-allocates a new ACLDataPacket with the given payload size and 44 // initializes the packet's header field with the given data. 45 static ACLDataPacketPtr New( 46 hci_spec::ConnectionHandle connection_handle, 47 hci_spec::ACLPacketBoundaryFlag packet_boundary_flag, 48 hci_spec::ACLBroadcastFlag broadcast_flag, 49 uint16_t payload_size = 0u); 50 51 // Getters for the header fields. 52 hci_spec::ConnectionHandle connection_handle() const; 53 hci_spec::ACLPacketBoundaryFlag packet_boundary_flag() const; 54 hci_spec::ACLBroadcastFlag broadcast_flag() const; 55 56 // Initializes the internal PacketView by reading the header portion of the 57 // underlying buffer. 58 void InitializeFromBuffer(); 59 60 // ID to trace packet flow set_trace_id(trace_flow_id_t id)61 void set_trace_id(trace_flow_id_t id) { async_id_ = id; } trace_id()62 trace_flow_id_t trace_id() { return async_id_; } 63 64 protected: 65 using PacketBase<hci_spec::ACLDataHeader, ACLDataPacket>::PacketBase; 66 67 private: 68 // Writes the given header fields into the underlying buffer. 69 void WriteHeader(hci_spec::ConnectionHandle connection_handle, 70 hci_spec::ACLPacketBoundaryFlag packet_boundary_flag, 71 hci_spec::ACLBroadcastFlag broadcast_flag); 72 73 trace_flow_id_t async_id_; 74 }; 75 76 } // namespace bt::hci 77