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/l2cap_frames.emb.h"
18 #include "pw_bluetooth_proxy/basic_l2cap_channel.h"
19 
20 namespace pw::bluetooth::proxy {
21 
22 // Interface for L2CAP signaling channels, which can be either ACL-U signaling
23 // channels or LE-U signaling channels.
24 class L2capSignalingChannel : public BasicL2capChannel {
25  public:
26   explicit L2capSignalingChannel(L2capChannelManager& l2cap_channel_manager,
27                                  uint16_t connection_handle,
28                                  uint16_t fixed_cid);
29 
30   L2capSignalingChannel& operator=(L2capSignalingChannel&& other);
31 
32   // Process the payload of a CFrame. Implementations should return true if the
33   // CFrame was consumed by the channel. Otherwise, return false and the PDU
34   // containing this CFrame will be forwarded by `ProxyHost` on to the Bluetooth
35   // host.
36   virtual bool OnCFramePayload(pw::span<const uint8_t> cframe_payload) = 0;
37 
38   // Process an individual signaling command.
39   //
40   // Returns false if the command is not processed, either because it is not
41   // directed to a channel managed by `L2capChannelManager` or because we do
42   // not listen for that type of command.
43   bool HandleL2capSignalingCommand(emboss::L2capSignalingCommandView cmd);
44 
45   // Handle L2CAP_FLOW_CONTROL_CREDIT_IND.
46   //
47   // Returns false if the packet is invalid or not directed to a channel managed
48   // by `L2capChannelManager`.
49   bool HandleFlowControlCreditInd(emboss::L2capFlowControlCreditIndView cmd);
50 
51   // Send L2CAP_FLOW_CONTROL_CREDIT_IND to indicate local endpoint `cid` is
52   // capable of receiving a number of additional K-frames (`credits`).
53   //
54   // Returns PW_STATUS_INVALID_ARGUMENT if CID is invalid.
55   // Returns PW_STATUS_UNAVAILABLE if send could not be queued.
56   Status SendFlowControlCreditInd(uint16_t cid, uint16_t credits);
57 
58  protected:
59   // Process a C-frame.
60   //
61   // Returns false if the C-frame is to be forwarded on to the Bluetooth host,
62   // either because the command is not directed towards a channel managed by
63   // `L2capChannelManager` or because the C-frame is invalid and should be
64   // handled by the Bluetooth host.
65   bool HandlePduFromController(pw::span<uint8_t> cframe) override;
66 
67   bool HandlePduFromHost(pw::span<uint8_t> cframe) override;
68 
69   L2capChannelManager& l2cap_channel_manager_;
70 };
71 
72 }  // namespace pw::bluetooth::proxy
73