Pigweed
 
Loading...
Searching...
No Matches
proxy_host.h
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#pragma once
16
17#include "pw_bluetooth_proxy/gatt_notify_channel.h"
18#include "pw_bluetooth_proxy/internal/acl_data_channel.h"
19#include "pw_bluetooth_proxy/internal/h4_storage.h"
20#include "pw_bluetooth_proxy/internal/hci_transport.h"
21#include "pw_bluetooth_proxy/internal/l2cap_channel_manager.h"
22#include "pw_bluetooth_proxy/l2cap_channel_common.h"
23#include "pw_bluetooth_proxy/l2cap_coc.h"
24#include "pw_bluetooth_proxy/l2cap_status_delegate.h"
25#include "pw_bluetooth_proxy/rfcomm_channel.h"
26#include "pw_status/status.h"
27
28namespace pw::bluetooth::proxy {
29
32class ProxyHost {
33 public:
43 ProxyHost(pw::Function<void(H4PacketWithHci&& packet)>&& send_to_host_fn,
44 pw::Function<void(H4PacketWithH4&& packet)>&& send_to_controller_fn,
45 uint16_t le_acl_credits_to_reserve,
46 uint16_t br_edr_acl_credits_to_reserve);
47
48 ProxyHost() = delete;
49 ProxyHost(const ProxyHost&) = delete;
50 ProxyHost& operator=(const ProxyHost&) = delete;
51 ProxyHost(ProxyHost&&) = delete;
52 ProxyHost& operator=(ProxyHost&&) = delete;
56
57 // ##### Container API
58 // Containers are expected to call these functions (in addition to ctor).
59
72
99
110 void Reset();
111
112 // ##### Client APIs
113
120 void RegisterL2capStatusDelegate(L2capStatusDelegate& delegate);
121
126 void UnregisterL2capStatusDelegate(L2capStatusDelegate& delegate);
127
157 pw::Result<L2capCoc> AcquireL2capCoc(
158 pw::multibuf::MultiBufAllocator& rx_multibuf_allocator,
159 uint16_t connection_handle,
160 L2capCoc::CocConfig rx_config,
161 L2capCoc::CocConfig tx_config,
162 Function<void(multibuf::MultiBuf&& payload)>&& receive_fn,
163 ChannelEventCallback&& event_fn);
164
168 pw::Status SendAdditionalRxCredits(uint16_t connection_handle,
169 uint16_t local_cid,
170 uint16_t additional_rx_credits);
171
212 pw::Result<BasicL2capChannel> AcquireBasicL2capChannel(
213 multibuf::MultiBufAllocator& rx_multibuf_allocator,
214 uint16_t connection_handle,
215 uint16_t local_cid,
216 uint16_t remote_cid,
217 AclTransportType transport,
218 OptionalPayloadReceiveCallback&& payload_from_controller_fn,
219 OptionalPayloadReceiveCallback&& payload_from_host_fn,
220 ChannelEventCallback&& event_fn);
221
242 pw::Result<GattNotifyChannel> AcquireGattNotifyChannel(
243 int16_t connection_handle,
244 uint16_t attribute_handle,
245 ChannelEventCallback&& event_fn);
246
269 // TODO: https://pwbug.dev/369709521 - Delete this once all downstreams
270 // have transitioned.
271 StatusWithMultiBuf SendGattNotify(uint16_t connection_handle,
272 uint16_t attribute_handle,
273 pw::multibuf::MultiBuf&& payload);
274
297 // TODO: https://pwbug.dev/379337272 - Delete this once all downstreams
298 // have transitioned.
299 pw::Status SendGattNotify(uint16_t connection_handle,
300 uint16_t attribute_handle,
301 pw::span<const uint8_t> attribute_value);
302
334 pw::Result<RfcommChannel> AcquireRfcommChannel(
335 multibuf::MultiBufAllocator& rx_multibuf_allocator,
336 uint16_t connection_handle,
337 RfcommChannel::Config rx_config,
338 RfcommChannel::Config tx_config,
339 uint8_t channel_number,
340 Function<void(multibuf::MultiBuf&& payload)>&& payload_from_controller_fn,
341 ChannelEventCallback&& event_fn);
342
347
352
355 uint16_t GetNumFreeLeAclPackets() const;
356
360
363 static constexpr size_t GetNumSimultaneousAclSendsSupported() {
364 return H4Storage::GetNumH4Buffs();
365 }
366
368 static constexpr size_t GetMaxAclSendSize() {
369 return H4Storage::GetH4BuffSize() - sizeof(emboss::H4PacketType);
370 }
371
373 static constexpr size_t GetMaxNumAclConnections() {
374 return AclDataChannel::GetMaxNumAclConnections();
375 }
376
377 private:
378 // Handle HCI Event packet from the controller.
379 void HandleEventFromController(H4PacketWithHci&& h4_packet);
380
381 // Handle HCI Event packet from the host.
382 void HandleEventFromHost(H4PacketWithH4&& h4_packet);
383
384 // Handle HCI ACL data packet from the controller.
385 void HandleAclFromController(H4PacketWithHci&& h4_packet);
386
387 // Process an LE_META_EVENT
388 void HandleLeMetaEvent(H4PacketWithHci&& h4_packet);
389
390 // Process a Command_Complete event.
391 void HandleCommandCompleteEvent(H4PacketWithHci&& h4_packet);
392
393 // Handle HCI Command packet from the host.
394 void HandleCommandFromHost(H4PacketWithH4&& h4_packet);
395
396 // Handle HCI ACL data packet from the host.
397 void HandleAclFromHost(H4PacketWithH4&& h4_packet);
398
399 // For sending non-ACL data to the host and controller. ACL traffic shall be
400 // sent through the `acl_data_channel_`.
401 HciTransport hci_transport_;
402
403 // Owns management of the LE ACL data channel.
404 AclDataChannel acl_data_channel_;
405
406 // Keeps track of the L2CAP-based channels managed by the proxy.
407 L2capChannelManager l2cap_channel_manager_;
408};
409
410} // namespace pw::bluetooth::proxy
Definition: status.h:85
H4PacketWithH4 is an H4Packet backed by an H4 buffer.
Definition: h4_packet.h:85
H4PacketWithHci is an H4Packet backed by an HCI buffer.
Definition: h4_packet.h:58
Definition: proxy_host.h:32
static constexpr size_t GetMaxNumAclConnections()
Returns the max number of simultaneous LE ACL connections supported.
Definition: proxy_host.h:373
pw::Result< RfcommChannel > AcquireRfcommChannel(multibuf::MultiBufAllocator &rx_multibuf_allocator, uint16_t connection_handle, RfcommChannel::Config rx_config, RfcommChannel::Config tx_config, uint8_t channel_number, Function< void(multibuf::MultiBuf &&payload)> &&payload_from_controller_fn, ChannelEventCallback &&event_fn)
pw::Result< GattNotifyChannel > AcquireGattNotifyChannel(int16_t connection_handle, uint16_t attribute_handle, ChannelEventCallback &&event_fn)
void HandleH4HciFromHost(H4PacketWithH4 &&h4_packet)
void RegisterL2capStatusDelegate(L2capStatusDelegate &delegate)
pw::Result< L2capCoc > AcquireL2capCoc(pw::multibuf::MultiBufAllocator &rx_multibuf_allocator, uint16_t connection_handle, L2capCoc::CocConfig rx_config, L2capCoc::CocConfig tx_config, Function< void(multibuf::MultiBuf &&payload)> &&receive_fn, ChannelEventCallback &&event_fn)
static constexpr size_t GetNumSimultaneousAclSendsSupported()
Definition: proxy_host.h:363
uint16_t GetNumFreeLeAclPackets() const
pw::Result< BasicL2capChannel > AcquireBasicL2capChannel(multibuf::MultiBufAllocator &rx_multibuf_allocator, uint16_t connection_handle, uint16_t local_cid, uint16_t remote_cid, AclTransportType transport, OptionalPayloadReceiveCallback &&payload_from_controller_fn, OptionalPayloadReceiveCallback &&payload_from_host_fn, ChannelEventCallback &&event_fn)
uint16_t GetNumFreeBrEdrAclPackets() const
pw::Status SendAdditionalRxCredits(uint16_t connection_handle, uint16_t local_cid, uint16_t additional_rx_credits)
static constexpr size_t GetMaxAclSendSize()
Returns the max LE ACL packet size supported to be sent.
Definition: proxy_host.h:368
void UnregisterL2capStatusDelegate(L2capStatusDelegate &delegate)
ProxyHost(pw::Function< void(H4PacketWithHci &&packet)> &&send_to_host_fn, pw::Function< void(H4PacketWithH4 &&packet)> &&send_to_controller_fn, uint16_t le_acl_credits_to_reserve, uint16_t br_edr_acl_credits_to_reserve)
StatusWithMultiBuf SendGattNotify(uint16_t connection_handle, uint16_t attribute_handle, pw::multibuf::MultiBuf &&payload)
void HandleH4HciFromController(H4PacketWithHci &&h4_packet)
pw::Status SendGattNotify(uint16_t connection_handle, uint16_t attribute_handle, pw::span< const uint8_t > attribute_value)
Definition: allocator.h:54
Definition: multibuf.h:245
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:74