xref: /aosp_15_r20/external/pigweed/pw_bluetooth_proxy/l2cap_write_channel.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 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_proxy/internal/l2cap_write_channel.h"
16 
17 #include <mutex>
18 
19 #include "pw_bluetooth/emboss_util.h"
20 #include "pw_bluetooth/hci_data.emb.h"
21 #include "pw_bluetooth/hci_h4.emb.h"
22 #include "pw_bluetooth/l2cap_frames.emb.h"
23 #include "pw_bluetooth_proxy/internal/l2cap_channel_manager.h"
24 #include "pw_log/log.h"
25 #include "pw_status/status.h"
26 #include "pw_status/try.h"
27 
28 namespace pw::bluetooth::proxy {
29 
QueuePacket(H4PacketWithH4 && packet)30 Status L2capWriteChannel::QueuePacket(H4PacketWithH4&& packet) {
31   Status status;
32   {
33     std::lock_guard lock(global_send_queue_mutex_);
34     if (send_queue_.full()) {
35       status = Status::Unavailable();
36     } else {
37       send_queue_.push(std::move(packet));
38       status = OkStatus();
39     }
40   }
41   ReportPacketsMayBeReadyToSend();
42   return status;
43 }
44 
DequeuePacket()45 std::optional<H4PacketWithH4> L2capWriteChannel::DequeuePacket() {
46   std::lock_guard lock(global_send_queue_mutex_);
47   if (send_queue_.empty()) {
48     return std::nullopt;
49   }
50   H4PacketWithH4 packet = std::move(send_queue_.front());
51   send_queue_.pop();
52   return packet;
53 }
54 
ClearQueue()55 void L2capWriteChannel::ClearQueue() {
56   std::lock_guard lock(global_send_queue_mutex_);
57   send_queue_.clear();
58 }
59 
L2capWriteChannel(L2capWriteChannel && other)60 L2capWriteChannel::L2capWriteChannel(L2capWriteChannel&& other)
61     : transport_(other.transport_),
62       connection_handle_(other.connection_handle_),
63       remote_cid_(other.remote_cid_),
64       l2cap_channel_manager_(other.l2cap_channel_manager_) {
65   l2cap_channel_manager_.ReleaseWriteChannel(other);
66   l2cap_channel_manager_.RegisterWriteChannel(*this);
67 }
68 
operator =(L2capWriteChannel && other)69 L2capWriteChannel& L2capWriteChannel::operator=(L2capWriteChannel&& other) {
70   if (this != &other) {
71     PW_CHECK(!l2cap_channel_manager_.ReleaseWriteChannel(*this),
72              "Move assignment operator called on channel that is still active "
73              "(still registered with L2capChannelManager).");
74     connection_handle_ = other.connection_handle();
75     remote_cid_ = other.remote_cid();
76     // All L2capWriteChannels share a static mutex, so only one lock needs to be
77     // acquired here.
78     // TODO: https://pwbug.dev/369849508 - Once mutex is no longer static,
79     // elide this operator or acquire a lock on both channels' mutexes.
80     std::lock_guard lock(global_send_queue_mutex_);
81     send_queue_ = std::move(other.send_queue_);
82     l2cap_channel_manager_.ReleaseWriteChannel(other);
83     l2cap_channel_manager_.RegisterWriteChannel(*this);
84   }
85   return *this;
86 }
87 
~L2capWriteChannel()88 L2capWriteChannel::~L2capWriteChannel() {
89   l2cap_channel_manager_.ReleaseWriteChannel(*this);
90   ClearQueue();
91 }
92 
L2capWriteChannel(L2capChannelManager & l2cap_channel_manager,uint16_t connection_handle,AclTransportType transport,uint16_t remote_cid)93 L2capWriteChannel::L2capWriteChannel(L2capChannelManager& l2cap_channel_manager,
94                                      uint16_t connection_handle,
95                                      AclTransportType transport,
96                                      uint16_t remote_cid)
97     : transport_(transport),
98       connection_handle_(connection_handle),
99       remote_cid_(remote_cid),
100       l2cap_channel_manager_(l2cap_channel_manager) {
101   l2cap_channel_manager_.RegisterWriteChannel(*this);
102 }
103 
AreValidParameters(uint16_t connection_handle,uint16_t remote_cid)104 bool L2capWriteChannel::AreValidParameters(uint16_t connection_handle,
105                                            uint16_t remote_cid) {
106   if (connection_handle > kMaxValidConnectionHandle) {
107     PW_LOG_ERROR(
108         "Invalid connection handle 0x%X. Maximum connection handle is 0x0EFF.",
109         connection_handle);
110     return false;
111   }
112   if (remote_cid == 0) {
113     PW_LOG_ERROR("L2CAP channel identifier 0 is not valid.");
114     return false;
115   }
116   return true;
117 }
118 
PopulateTxL2capPacket(uint16_t data_length)119 pw::Result<H4PacketWithH4> L2capWriteChannel::PopulateTxL2capPacket(
120     uint16_t data_length) {
121   const size_t l2cap_packet_size =
122       emboss::BasicL2capHeader::IntrinsicSizeInBytes() + data_length;
123   const size_t acl_packet_size =
124       emboss::AclDataFrameHeader::IntrinsicSizeInBytes() + l2cap_packet_size;
125   const size_t h4_packet_size = sizeof(emboss::H4PacketType) + acl_packet_size;
126 
127   pw::Result<H4PacketWithH4> h4_packet_res =
128       l2cap_channel_manager_.GetTxH4Packet(h4_packet_size);
129   if (!h4_packet_res.ok()) {
130     return h4_packet_res.status();
131   }
132   H4PacketWithH4 h4_packet = std::move(h4_packet_res.value());
133   h4_packet.SetH4Type(emboss::H4PacketType::ACL_DATA);
134 
135   PW_TRY_ASSIGN(
136       auto acl,
137       MakeEmbossWriter<emboss::AclDataFrameWriter>(h4_packet.GetHciSpan()));
138   acl.header().handle().Write(connection_handle_);
139   // TODO: https://pwbug.dev/360932103 - Support packet segmentation, so this
140   // value will not always be FIRST_NON_FLUSHABLE.
141   acl.header().packet_boundary_flag().Write(
142       emboss::AclDataPacketBoundaryFlag::FIRST_NON_FLUSHABLE);
143   acl.header().broadcast_flag().Write(
144       emboss::AclDataPacketBroadcastFlag::POINT_TO_POINT);
145   acl.data_total_length().Write(l2cap_packet_size);
146 
147   PW_TRY_ASSIGN(auto l2cap_header,
148                 MakeEmbossWriter<emboss::BasicL2capHeaderWriter>(
149                     acl.payload().BackingStorage().data(),
150                     emboss::BasicL2capHeader::IntrinsicSizeInBytes()));
151   l2cap_header.pdu_length().Write(data_length);
152   l2cap_header.channel_id().Write(remote_cid_);
153 
154   return h4_packet;
155 }
156 
MaxL2capPayloadSize() const157 uint16_t L2capWriteChannel::MaxL2capPayloadSize() const {
158   return l2cap_channel_manager_.GetH4BuffSize() - sizeof(emboss::H4PacketType) -
159          emboss::AclDataFrameHeader::IntrinsicSizeInBytes() -
160          emboss::BasicL2capHeader::IntrinsicSizeInBytes();
161 }
162 
ReportPacketsMayBeReadyToSend()163 void L2capWriteChannel::ReportPacketsMayBeReadyToSend() {
164   l2cap_channel_manager_.DrainWriteChannelQueues();
165 }
166 
167 }  // namespace pw::bluetooth::proxy
168