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_leu_signaling_channel.h"
16
17 #include "pw_bluetooth/l2cap_frames.emb.h"
18 #include "pw_log/log.h"
19
20 namespace pw::bluetooth::proxy {
21
L2capLeUSignalingChannel(L2capChannelManager & l2cap_channel_manager,uint16_t connection_handle)22 L2capLeUSignalingChannel::L2capLeUSignalingChannel(
23 L2capChannelManager& l2cap_channel_manager, uint16_t connection_handle)
24 : L2capSignalingChannel(
25 /*l2cap_channel_manager=*/l2cap_channel_manager,
26 /*connection_handle=*/connection_handle,
27 /*fixed_cid=*/
28 cpp23::to_underlying(emboss::L2capFixedCid::LE_U_SIGNALING)) {}
29
OnCFramePayload(pw::span<const uint8_t> cframe_payload)30 bool L2capLeUSignalingChannel::OnCFramePayload(
31 pw::span<const uint8_t> cframe_payload) {
32 emboss::L2capSignalingCommandHeaderView cmd_header =
33 emboss::MakeL2capSignalingCommandHeaderView(cframe_payload.data(),
34 cframe_payload.size());
35 if (!cmd_header.Ok()) {
36 PW_LOG_ERROR(
37 "C-frame does not contain a valid command. So will forward to host "
38 "without processing.");
39 return false;
40 }
41
42 // Core Spec v5.4 Vol 3, Part A, 4: "Examples of signaling packets that are
43 // not correctly formed include... A C-frame on fixed channel 0x0005 contains
44 // more than one signaling packet"
45 const size_t cmd_length =
46 emboss::L2capSignalingCommandHeader::IntrinsicSizeInBytes() +
47 cmd_header.data_length().Read();
48 if (cframe_payload.size() > cmd_length) {
49 PW_LOG_ERROR(
50 "Received C-frame on LE-U signaling channel with payload larger than "
51 "its command. So will forward to host without processing.");
52 return false;
53 }
54
55 emboss::L2capSignalingCommandView cmd = emboss::MakeL2capSignalingCommandView(
56 cframe_payload.data(), cframe_payload.size());
57
58 if (!cmd.Ok()) {
59 PW_LOG_ERROR(
60 "L2CAP PDU payload length not enough to accommodate signaling command. "
61 "So will forward to host without processing.");
62 return false;
63 }
64
65 return HandleL2capSignalingCommand(cmd);
66 }
67
68 } // namespace pw::bluetooth::proxy
69