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/acl_data_channel.h"
16
17 #include <cstdint>
18
19 #include "pw_log/log.h"
20
21 namespace pw::bluetooth::proxy {
22
23 template <class EventT>
ProcessSpecificLEReadBufferSizeCommandCompleteEvent(EventT read_buffer_event)24 void AclDataChannel::ProcessSpecificLEReadBufferSizeCommandCompleteEvent(
25 EventT read_buffer_event) {
26 if (initialized_) {
27 PW_LOG_WARN(
28 "AclDataChannel is already initialized, but encountered another "
29 "ReadBufferSizeCommandCompleteEvent.");
30 }
31
32 initialized_ = true;
33
34 uint16_t controller_max_le_acl_packets =
35 read_buffer_event.total_num_le_acl_data_packets().Read();
36 proxy_max_le_acl_packets_ =
37 std::min(controller_max_le_acl_packets, le_acl_credits_to_reserve_);
38 uint16_t host_max_le_acl_packets =
39 controller_max_le_acl_packets - proxy_max_le_acl_packets_;
40 read_buffer_event.total_num_le_acl_data_packets().Write(
41 host_max_le_acl_packets);
42 PW_LOG_INFO(
43 "Bluetooth Proxy reserved %d ACL data credits. Passed %d on to host.",
44 proxy_max_le_acl_packets_,
45 host_max_le_acl_packets);
46
47 if (proxy_max_le_acl_packets_ < le_acl_credits_to_reserve_) {
48 PW_LOG_ERROR(
49 "Only was able to reserve %d acl data credits rather than the "
50 "configured %d from the controller provided's data credits of %d. ",
51 proxy_max_le_acl_packets_,
52 le_acl_credits_to_reserve_,
53 controller_max_le_acl_packets);
54 }
55 }
56
57 template void
58 AclDataChannel::ProcessSpecificLEReadBufferSizeCommandCompleteEvent<
59 emboss::LEReadBufferSizeV1CommandCompleteEventWriter>(
60 emboss::LEReadBufferSizeV1CommandCompleteEventWriter read_buffer_event);
61
62 template void
63 AclDataChannel::ProcessSpecificLEReadBufferSizeCommandCompleteEvent<
64 emboss::LEReadBufferSizeV2CommandCompleteEventWriter>(
65 emboss::LEReadBufferSizeV2CommandCompleteEventWriter read_buffer_event);
66
GetLeAclCreditsToReserve() const67 uint16_t AclDataChannel::GetLeAclCreditsToReserve() const {
68 return le_acl_credits_to_reserve_;
69 }
70
GetNumFreeLeAclPackets() const71 uint16_t AclDataChannel::GetNumFreeLeAclPackets() const {
72 // TODO: https://pwbug.dev/326499611 - Subtract pending packets once we have
73 // them.
74 return proxy_max_le_acl_packets_;
75 }
76
77 } // namespace pw::bluetooth::proxy
78