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