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