xref: /aosp_15_r20/external/pigweed/pw_bluetooth_proxy/public/pw_bluetooth_proxy/basic_l2cap_channel.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 "pw_bluetooth_proxy/internal/l2cap_read_channel.h"
18 #include "pw_bluetooth_proxy/internal/l2cap_write_channel.h"
19 
20 namespace pw::bluetooth::proxy {
21 
22 class BasicL2capChannel : public L2capReadChannel, public L2capWriteChannel {
23  public:
24   // TODO: https://pwbug.dev/360929142 - Take the MTU. Signaling channels would
25   // provide MTU_SIG.
26   static pw::Result<BasicL2capChannel> Create(
27       L2capChannelManager& l2cap_channel_manager,
28       uint16_t connection_handle,
29       uint16_t local_cid,
30       uint16_t remote_cid,
31       pw::Function<void(pw::span<uint8_t> payload)>&&
32           payload_from_controller_fn);
33 
34   BasicL2capChannel(const BasicL2capChannel& other) = delete;
35   BasicL2capChannel& operator=(const BasicL2capChannel& other) = delete;
36   BasicL2capChannel(BasicL2capChannel&&) = default;
37   // Move assignment operator allows channels to be erased from pw_containers.
38   BasicL2capChannel& operator=(BasicL2capChannel&& other) = default;
39 
40   /// Send an L2CAP payload to the remote peer.
41   ///
42   /// @param[in] payload The L2CAP payload to be sent. Payload will be copied
43   ///                    before function completes.
44   ///
45   /// @returns @rst
46   ///
47   /// .. pw-status-codes::
48   ///  OK:                  If packet was successfully queued for send.
49   ///  UNAVAILABLE:         If channel could not acquire the resources to queue
50   ///                       the send at this time (transient error).
51   ///  INVALID_ARGUMENT:    If payload is too large.
52   /// @endrst
53   pw::Status Write(pw::span<const uint8_t> payload);
54 
55  protected:
56   explicit BasicL2capChannel(L2capChannelManager& l2cap_channel_manager,
57                              uint16_t connection_handle,
58                              uint16_t local_cid,
59                              uint16_t remote_cid,
60                              pw::Function<void(pw::span<uint8_t> payload)>&&
61                                  payload_from_controller_fn);
62 
63  protected:
64   bool HandlePduFromController(pw::span<uint8_t> bframe) override;
65   bool HandlePduFromHost(pw::span<uint8_t> bframe) override;
66 
67   // TODO: https://pwbug.dev/360929142 - Stop channel on errors.
68 };
69 
70 }  // namespace pw::bluetooth::proxy
71