• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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 
17 #include <cstdint>
18 
19 #include "pw_bluetooth_sapphire/internal/host/common/weak_self.h"
20 #include "pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h"
21 #include "pw_bluetooth_sapphire/internal/host/iso/iso_common.h"
22 #include "pw_bluetooth_sapphire/internal/host/transport/command_channel.h"
23 #include "pw_bluetooth_sapphire/internal/host/transport/transport.h"
24 #include "pw_bytes/span.h"
25 
26 namespace bt::iso {
27 
28 class IsoStream : public hci::IsoDataChannel::ConnectionInterface {
29  public:
30   ~IsoStream() override = default;
31 
32   // Handler for incoming HCI_LE_CIS_Established events. Returns a value
33   // indicating whether the vent was handled.
34   virtual bool OnCisEstablished(const hci::EventPacket& event) = 0;
35 
36   enum SetupDataPathError {
37     kSuccess,
38     kStreamAlreadyExists,
39     kCisNotEstablished,
40     kStreamRejectedByController,
41     kInvalidArgs,
42     kStreamClosed,
43   };
44 
45   using SetupDataPathCallback = pw::Callback<void(SetupDataPathError)>;
46   using IncomingDataHandler =
47       pw::Function<bool(const pw::span<const std::byte>&)>;
48 
49   virtual void SetupDataPath(
50       pw::bluetooth::emboss::DataPathDirection direction,
51       const bt::StaticPacket<pw::bluetooth::emboss::CodecIdWriter>& codec_id,
52       const std::optional<std::vector<uint8_t>>& codec_configuration,
53       uint32_t controller_delay_usecs,
54       SetupDataPathCallback&& on_complete_cb,
55       IncomingDataHandler&& on_incoming_data_available_cb) = 0;
56 
57   virtual hci_spec::ConnectionHandle cis_handle() const = 0;
58 
59   // Terminate this stream.
60   virtual void Close() = 0;
61 
62   static std::unique_ptr<IsoStream> Create(
63       uint8_t cig_id,
64       uint8_t cis_id,
65       hci_spec::ConnectionHandle cis_handle,
66       CisEstablishedCallback on_established_cb,
67       hci::CommandChannel::WeakPtr cmd,
68       pw::Callback<void()> on_closed_cb,
69       hci::IsoDataChannel* data_channel);
70 
71   // Used by the client to check for queued frames. If none are present the
72   // incoming data available callback will be called the next time a frame is
73   // available. This allows for a 'hanging get' style interface (request a frame
74   // whenever the client is ready to process one and then wait for a
75   // notification) or a client-buffered interface (every time the client wants
76   // more frames request them until it receives a nullptr, and then wait for a
77   // callback to indicate that the next frame(s) are available). It is important
78   // to note that the client cannot simply rely on notifications: until a read
79   // attempt is unfulfilled the stream will buffer frames waiting for a read
80   // from the client.
81   virtual std::unique_ptr<IsoDataPacket> ReadNextQueuedIncomingPacket() = 0;
82 
83   // Send a packet over the stream. If the packet is too large then it will be
84   // fragmented.
85   virtual void Send(pw::ConstByteSpan data) = 0;
86 
87   using WeakPtr = WeakSelf<IsoStream>::WeakPtr;
88   virtual WeakPtr GetWeakPtr() = 0;
89 };
90 
91 }  // namespace bt::iso
92