xref: /aosp_15_r20/external/pigweed/pw_bluetooth_sapphire/host/l2cap/dynamic_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/dynamic_channel.h"
16 
17 #include "pw_bluetooth_sapphire/internal/host/common/assert.h"
18 #include "pw_bluetooth_sapphire/internal/host/common/log.h"
19 #include "pw_bluetooth_sapphire/internal/host/l2cap/bredr_dynamic_channel.h"
20 #include "pw_bluetooth_sapphire/internal/host/l2cap/dynamic_channel_registry.h"
21 #include "pw_bluetooth_sapphire/internal/host/l2cap/l2cap_defs.h"
22 
23 namespace bt::l2cap::internal {
24 
DynamicChannel(DynamicChannelRegistry * registry,Psm psm,ChannelId local_cid,ChannelId remote_cid)25 DynamicChannel::DynamicChannel(DynamicChannelRegistry* registry,
26                                Psm psm,
27                                ChannelId local_cid,
28                                ChannelId remote_cid)
29     : registry_(registry),
30       psm_(psm),
31       local_cid_(local_cid),
32       remote_cid_(remote_cid),
33       opened_(false) {
34   PW_DCHECK(registry_);
35 }
36 
SetRemoteChannelId(ChannelId remote_cid)37 bool DynamicChannel::SetRemoteChannelId(ChannelId remote_cid) {
38   // do not allow duplicate remote CIDs
39   auto channel = registry_->FindChannelByRemoteId(remote_cid);
40   if (channel && channel != this) {
41     bt_log(WARN,
42            "l2cap",
43            "channel %#.4x: received remote channel id %#.4x that is already "
44            "set for channel %#.4x",
45            local_cid(),
46            remote_cid,
47            channel->local_cid());
48     return false;
49   }
50 
51   remote_cid_ = remote_cid;
52   return true;
53 }
54 
OnDisconnected()55 void DynamicChannel::OnDisconnected() {
56   registry_->OnChannelDisconnected(this);
57 }
58 
59 }  // namespace bt::l2cap::internal
60