• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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           /*transport=*/AclTransportType::kLe,
28           /*fixed_cid=*/
29           cpp23::to_underlying(emboss::L2capFixedCid::LE_U_SIGNALING)) {}
30 
OnCFramePayload(Direction direction,pw::span<const uint8_t> cframe_payload)31 bool L2capLeUSignalingChannel::OnCFramePayload(
32     Direction direction, pw::span<const uint8_t> cframe_payload) {
33   emboss::L2capSignalingCommandHeaderView cmd_header =
34       emboss::MakeL2capSignalingCommandHeaderView(cframe_payload.data(),
35                                                   cframe_payload.size());
36   if (!cmd_header.Ok()) {
37     PW_LOG_ERROR(
38         "C-frame does not contain a valid command. So will forward without "
39         "processing.");
40     return false;
41   }
42 
43   // Core Spec v5.4 Vol 3, Part A, 4: "Examples of signaling packets that are
44   // not correctly formed include... A C-frame on fixed channel 0x0005 contains
45   // more than one signaling packet"
46   const size_t cmd_length =
47       emboss::L2capSignalingCommandHeader::IntrinsicSizeInBytes() +
48       cmd_header.data_length().Read();
49   if (cframe_payload.size() > cmd_length) {
50     PW_LOG_ERROR(
51         "Received C-frame on LE-U signaling channel with payload larger than "
52         "its command. So will forward without processing.");
53     return false;
54   }
55 
56   emboss::L2capSignalingCommandView cmd = emboss::MakeL2capSignalingCommandView(
57       cframe_payload.data(), cframe_payload.size());
58 
59   if (!cmd.Ok()) {
60     PW_LOG_ERROR(
61         "L2CAP PDU payload length not enough to accommodate signaling command. "
62         "So will forward without processing.");
63     return false;
64   }
65 
66   return HandleL2capSignalingCommand(direction, cmd);
67 }
68 
69 }  // namespace pw::bluetooth::proxy
70