1 // Copyright 2023 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_sapphire/internal/host/sm/pairing_channel.h"
16
17 #include "pw_bluetooth_sapphire/internal/host/common/assert.h"
18 #include "pw_bluetooth_sapphire/internal/host/common/byte_buffer.h"
19 #include "pw_bluetooth_sapphire/internal/host/l2cap/channel.h"
20 #include "pw_bluetooth_sapphire/internal/host/l2cap/scoped_channel.h"
21 #include "pw_bluetooth_sapphire/internal/host/sm/smp.h"
22
23 namespace bt::sm {
24
PairingChannel(l2cap::Channel::WeakPtr chan,fit::closure timer_resetter)25 PairingChannel::PairingChannel(l2cap::Channel::WeakPtr chan,
26 fit::closure timer_resetter)
27 : chan_(std::move(chan)),
28 reset_timer_(std::move(timer_resetter)),
29 weak_self_(this) {
30 PW_CHECK(chan_);
31 if (chan_->link_type() == bt::LinkType::kLE) {
32 PW_CHECK(chan_->id() == l2cap::kLESMPChannelId);
33 } else if (chan_->link_type() == bt::LinkType::kACL) {
34 PW_CHECK(chan_->id() == l2cap::kSMPChannelId);
35 } else {
36 BT_PANIC("unsupported link type for SMP!");
37 }
38 auto self = weak_self_.GetWeakPtr();
39 chan_->Activate(
40 [self](ByteBufferPtr sdu) {
41 if (self.is_alive()) {
42 self->OnRxBFrame(std::move(sdu));
43 } else {
44 bt_log(WARN, "sm", "dropped packet on SM channel!");
45 }
46 },
47 [self]() {
48 if (self.is_alive()) {
49 self->OnChannelClosed();
50 }
51 });
52 // The SMP fixed channel's MTU must be >=23 bytes (kNoSecureConnectionsMTU)
53 // per spec V5.0 Vol. 3 Part H 3.2. As SMP operates on a fixed channel, there
54 // is no way to configure this MTU, so we expect that L2CAP always provides a
55 // channel with a sufficiently large MTU. This assertion serves as an explicit
56 // acknowledgement of that contract between L2CAP and SMP.
57 PW_CHECK(chan_->max_tx_sdu_size() >= kNoSecureConnectionsMtu &&
58 chan_->max_rx_sdu_size() >= kNoSecureConnectionsMtu);
59 }
60
PairingChannel(l2cap::Channel::WeakPtr chan)61 PairingChannel::PairingChannel(l2cap::Channel::WeakPtr chan)
62 : PairingChannel(std::move(chan), []() {}) {}
63
SetChannelHandler(Handler::WeakPtr new_handler)64 void PairingChannel::SetChannelHandler(Handler::WeakPtr new_handler) {
65 PW_CHECK(new_handler.is_alive());
66 bt_log(TRACE, "sm", "changing pairing channel handler");
67 handler_ = std::move(new_handler);
68 }
69
OnRxBFrame(ByteBufferPtr sdu)70 void PairingChannel::OnRxBFrame(ByteBufferPtr sdu) {
71 if (handler_.is_alive()) {
72 handler_->OnRxBFrame(std::move(sdu));
73 } else {
74 bt_log(WARN, "sm", "no handler to receive L2CAP packet callback!");
75 }
76 }
77
OnChannelClosed()78 void PairingChannel::OnChannelClosed() {
79 if (handler_.is_alive()) {
80 handler_->OnChannelClosed();
81 } else {
82 bt_log(WARN, "sm", "no handler to receive L2CAP channel closed callback!");
83 }
84 }
85
86 } // namespace bt::sm
87