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 <fuchsia/bluetooth/le/cpp/fidl.h>
18 #include <lib/fidl/cpp/binding.h>
19 
20 #include "pw_bluetooth_sapphire/fuchsia/host/fidl/server_base.h"
21 #include "pw_bluetooth_sapphire/internal/host/common/weak_self.h"
22 #include "pw_bluetooth_sapphire/internal/host/iso/iso_common.h"
23 
24 namespace bthost {
25 class IsoStreamServer
26     : public ServerBase<fuchsia::bluetooth::le::IsochronousStream> {
27  public:
28   explicit IsoStreamServer(
29       fidl::InterfaceRequest<fuchsia::bluetooth::le::IsochronousStream> request,
30       fit::callback<void()> on_closed_cb);
31 
32   void OnStreamEstablished(
33       bt::iso::IsoStream::WeakPtr stream_ptr,
34       const bt::iso::CisEstablishedParameters& connection_params);
35 
36   void OnStreamEstablishmentFailed(pw::bluetooth::emboss::StatusCode status);
37 
38   void OnClosed();
39 
40   void Close(zx_status_t epitaph);
41 
42   using WeakPtr = WeakSelf<IsoStreamServer>::WeakPtr;
GetWeakPtr()43   WeakPtr GetWeakPtr() { return weak_self_.GetWeakPtr(); }
44 
45  private:
46   // fuchsia::bluetooth::le::IsochronousStream overrides:
47   void SetupDataPath(
48       fuchsia::bluetooth::le::IsochronousStreamSetupDataPathRequest parameters,
49       SetupDataPathCallback callback) override;
50   void Read(ReadCallback callback) override;
51   void handle_unknown_method(uint64_t ordinal, bool has_response) override;
52 
53   // Handler for new incoming data. Returns a value indicating if we were able
54   // to process the packet (on false, it should be queued by the caller for
55   // later retrieval). If we were able to process the frame, the caller should
56   // continue to send notifications as frames are received. Otherwise, the
57   // caller should not invoke this function until the next frame received after
58   // the caller's queue has been completely emptied.
59   bool OnIncomingDataAvailable(pw::span<const std::byte> packet);
60 
61   fit::callback<void()> on_closed_cb_;
62 
63   std::optional<bt::iso::IsoStream::WeakPtr> iso_stream_;
64 
65   WeakSelf<IsoStreamServer> weak_self_;
66 
67   BT_DISALLOW_COPY_ASSIGN_AND_MOVE(IsoStreamServer);
68 };
69 
70 }  // namespace bthost
71