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_read_channel.h"
16
17 #include "pw_bluetooth_proxy/internal/l2cap_channel_manager.h"
18 #include "pw_log/log.h"
19
20 namespace pw::bluetooth::proxy {
21
L2capReadChannel(L2capReadChannel && other)22 L2capReadChannel::L2capReadChannel(L2capReadChannel&& other)
23 : connection_handle_(other.connection_handle()),
24 local_cid_(other.local_cid()),
25 payload_from_controller_fn_(std::move(other.payload_from_controller_fn_)),
26 l2cap_channel_manager_(other.l2cap_channel_manager_) {
27 l2cap_channel_manager_.ReleaseReadChannel(other);
28 l2cap_channel_manager_.RegisterReadChannel(*this);
29 }
30
operator =(L2capReadChannel && other)31 L2capReadChannel& L2capReadChannel::operator=(L2capReadChannel&& other) {
32 if (this != &other) {
33 PW_CHECK(!l2cap_channel_manager_.ReleaseReadChannel(*this),
34 "Move assignment operator called on channel that is still active "
35 "(still registered with L2capChannelManager).");
36 connection_handle_ = other.connection_handle();
37 local_cid_ = other.local_cid();
38 payload_from_controller_fn_ = std::move(other.payload_from_controller_fn_);
39 l2cap_channel_manager_.ReleaseReadChannel(other);
40 l2cap_channel_manager_.RegisterReadChannel(*this);
41 }
42 return *this;
43 }
44
~L2capReadChannel()45 L2capReadChannel::~L2capReadChannel() {
46 l2cap_channel_manager_.ReleaseReadChannel(*this);
47 }
48
OnFragmentedPduReceived()49 void L2capReadChannel::OnFragmentedPduReceived() {
50 PW_LOG_ERROR(
51 "(CID 0x%X) Fragmented L2CAP frame received, which is not yet supported.",
52 local_cid());
53 }
54
L2capReadChannel(L2capChannelManager & l2cap_channel_manager,pw::Function<void (pw::span<uint8_t> payload)> && payload_from_controller_fn,uint16_t connection_handle,uint16_t local_cid)55 L2capReadChannel::L2capReadChannel(
56 L2capChannelManager& l2cap_channel_manager,
57 pw::Function<void(pw::span<uint8_t> payload)>&& payload_from_controller_fn,
58 uint16_t connection_handle,
59 uint16_t local_cid)
60 : connection_handle_(connection_handle),
61 local_cid_(local_cid),
62 payload_from_controller_fn_(std::move(payload_from_controller_fn)),
63 l2cap_channel_manager_(l2cap_channel_manager) {
64 l2cap_channel_manager_.RegisterReadChannel(*this);
65 }
66
AreValidParameters(uint16_t connection_handle,uint16_t local_cid)67 bool L2capReadChannel::AreValidParameters(uint16_t connection_handle,
68 uint16_t local_cid) {
69 if (connection_handle > kMaxValidConnectionHandle) {
70 PW_LOG_ERROR(
71 "Invalid connection handle 0x%X. Maximum connection handle is 0x0EFF.",
72 connection_handle);
73 return false;
74 }
75 if (local_cid == 0) {
76 PW_LOG_ERROR("L2CAP channel identifier 0 is not valid.");
77 return false;
78 }
79 return true;
80 }
81
82 } // namespace pw::bluetooth::proxy
83