xref: /aosp_15_r20/external/pigweed/pw_bluetooth_sapphire/host/l2cap/scoped_channel.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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/l2cap/scoped_channel.h"
16 
17 namespace bt::l2cap {
18 
ScopedChannel(Channel::WeakPtr chan)19 ScopedChannel::ScopedChannel(Channel::WeakPtr chan) : chan_(std::move(chan)) {}
20 
ScopedChannel(ScopedChannel && other)21 ScopedChannel::ScopedChannel(ScopedChannel&& other)
22     : chan_(std::move(other.chan_)) {}
23 
~ScopedChannel()24 ScopedChannel::~ScopedChannel() { Close(); }
25 
Reset(Channel::WeakPtr new_channel)26 void ScopedChannel::Reset(Channel::WeakPtr new_channel) {
27   if (chan_.is_alive()) {
28     Close();
29   }
30   chan_ = std::move(new_channel);
31 }
32 
Close()33 void ScopedChannel::Close() {
34   if (chan_.is_alive()) {
35     chan_->Deactivate();
36     chan_ = Channel::WeakPtr();
37   }
38 }
39 
40 }  // namespace bt::l2cap
41