xref: /aosp_15_r20/external/pigweed/pw_bluetooth_proxy/l2cap_aclu_signaling_channel.cc (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 #include "pw_bluetooth_proxy/internal/l2cap_aclu_signaling_channel.h"
16 
17 #include "pw_assert/check.h"
18 #include "pw_bluetooth/emboss_util.h"
19 #include "pw_bluetooth/l2cap_frames.emb.h"
20 #include "pw_log/log.h"
21 
22 namespace pw::bluetooth::proxy {
23 
L2capAclUSignalingChannel(L2capChannelManager & l2cap_channel_manager,uint16_t connection_handle)24 L2capAclUSignalingChannel::L2capAclUSignalingChannel(
25     L2capChannelManager& l2cap_channel_manager, uint16_t connection_handle)
26     : L2capSignalingChannel(
27           /*l2cap_channel_manager=*/l2cap_channel_manager,
28           /*connection_handle=*/connection_handle,
29           /*fixed_cid=*/
30           cpp23::to_underlying(emboss::L2capFixedCid::ACL_U_SIGNALING)) {}
31 
OnCFramePayload(pw::span<const uint8_t> cframe_payload)32 bool L2capAclUSignalingChannel::OnCFramePayload(
33     pw::span<const uint8_t> cframe_payload) {
34   bool all_consumed = false;
35 
36   do {
37     auto cmd = emboss::MakeL2capSignalingCommandView(cframe_payload.data(),
38                                                      cframe_payload.size());
39     if (!cmd.Ok()) {
40       PW_LOG_ERROR(
41           "Remaining buffer is too small for L2CAP command. So will forward to "
42           "host without processing.");
43 
44       // TODO: https://pwbug.dev/379172336 - Handle partially consumed ACL-U
45       // signaling command packets.
46       PW_CHECK(!all_consumed, "Consumed some commands.");
47       return false;
48     }
49     bool current_consumed = HandleL2capSignalingCommand(cmd);
50 
51     // TODO: https://pwbug.dev/379172336 - Handle partially consumed ACL-U
52     // signaling command packets.
53     PW_CHECK(all_consumed == current_consumed,
54              "Wasn't able to consume all commands, but don't yet support "
55              "passing on some of them");
56     all_consumed |= current_consumed;
57     cframe_payload = cframe_payload.subspan(cmd.SizeInBytes());
58   } while (!cframe_payload.empty());
59 
60   return all_consumed;
61 }
62 
63 }  // namespace pw::bluetooth::proxy
64