C/C++ API Reference
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/hci_transport.h"
20#include "pw_bluetooth_proxy/internal/l2cap_channel_manager.h"
21#include "pw_bluetooth_proxy/l2cap_channel_common.h"
22#include "pw_bluetooth_proxy/l2cap_coc.h"
23#include "pw_bluetooth_proxy/l2cap_status_delegate.h"
24#include "pw_function/function.h"
25#include "pw_multibuf/multibuf.h"
26#include "pw_status/status.h"
27
28#if PW_BLUETOOTH_PROXY_ASYNC == 0
29#include "pw_bluetooth_proxy/internal/proxy_host_sync.h"
30
31// TODO: b/472522742 - Forward-declare the dispatcher until downstream support
32// for pw_async2 is resolved.
33namespace pw::async2 {
34class Dispatcher;
35} // namespace pw::async2
36#else
37#include "pw_async2/dispatcher.h"
38#include "pw_bluetooth_proxy/internal/proxy_host_async.h"
39#endif // PW_BLUETOOTH_PROXY_ASYNC
40
42namespace pw::bluetooth::proxy {
43
45
48class ProxyHost : public L2capChannelManagerInterface {
49 public:
64 // TODO: b/505912880 - Fix deadlock when release_fn sends packets.
73 ProxyHost(pw::Function<void(H4PacketWithHci&& packet)>&& send_to_host_fn,
74 pw::Function<void(H4PacketWithH4&& packet)>&& send_to_controller_fn,
75 pw::Allocator& allocator);
76
93 ProxyHost(pw::Function<void(H4PacketWithHci&& packet)>&& send_to_host_fn,
94 pw::Function<void(H4PacketWithH4&& packet)>&& send_to_controller_fn,
95 uint16_t le_acl_credits_to_reserve,
96 uint16_t br_edr_acl_credits_to_reserve,
97 pw::Allocator* allocator);
98
99 ProxyHost() = delete;
100 ProxyHost(const ProxyHost&) = delete;
101 ProxyHost& operator=(const ProxyHost&) = delete;
102 ProxyHost(ProxyHost&&) = delete;
103 ProxyHost& operator=(ProxyHost&&) = delete;
106 ~ProxyHost() override;
107
108 // ##### Container API
109 // Containers are expected to call these functions (in addition to ctor).
110
126
156
167 void Reset();
168
169 // ##### Container async API
170
181
182 // ##### Client APIs
183
190 void RegisterL2capStatusDelegate(L2capStatusDelegate& delegate);
191
196 void UnregisterL2capStatusDelegate(L2capStatusDelegate& delegate);
197
231 multibuf::MultiBufAllocator& rx_multibuf_allocator,
232 uint16_t connection_handle,
233 L2capCoc::CocConfig rx_config,
234 L2capCoc::CocConfig tx_config,
235 Function<void(multibuf::MultiBuf&& payload)>&& receive_fn,
236 ChannelEventCallback&& event_fn);
237
288 multibuf::MultiBufAllocator& rx_multibuf_allocator,
289 uint16_t connection_handle,
290 uint16_t local_cid,
291 uint16_t remote_cid,
292 AclTransportType transport,
293 OptionalPayloadReceiveCallback&& payload_from_controller_fn,
294 OptionalPayloadReceiveCallback&& payload_from_host_fn,
295 ChannelEventCallback&& event_fn);
296
317 int16_t connection_handle,
318 uint16_t attribute_handle,
319 ChannelEventCallback&& event_fn);
320
325
330
333 uint16_t GetNumFreeLeAclPackets() const;
334
338
340 static constexpr size_t GetMaxNumAclConnections() {
341 return AclDataChannel::GetMaxNumAclConnections();
342 }
343
344 private:
345 friend class internal::ProxyHostImpl;
346
348 void DoHandleH4HciFromHost(H4PacketWithH4&& h4_packet);
349
351 void DoHandleH4HciFromController(H4PacketWithHci&& h4_packet);
352
354 void DoReset();
355
357 void DoRegisterL2capStatusDelegate(L2capStatusDelegate& delegate);
358
360 void DoUnregisterL2capStatusDelegate(L2capStatusDelegate& delegate);
361
363 pw::Result<L2capCoc> DoAcquireL2capCoc(
364 multibuf::MultiBufAllocator& rx_multibuf_allocator,
365 uint16_t connection_handle,
366 L2capCoc::CocConfig rx_config,
367 L2capCoc::CocConfig tx_config,
368 Function<void(multibuf::MultiBuf&& payload)>&& receive_fn,
369 ChannelEventCallback&& event_fn);
370
372 pw::Result<BasicL2capChannel> DoAcquireBasicL2capChannel(
373 multibuf::MultiBufAllocator& rx_multibuf_allocator,
374 uint16_t connection_handle,
375 uint16_t local_cid,
376 uint16_t remote_cid,
377 AclTransportType transport,
378 OptionalPayloadReceiveCallback&& payload_from_controller_fn,
379 OptionalPayloadReceiveCallback&& payload_from_host_fn,
380 ChannelEventCallback&& event_fn);
381
383 pw::Result<GattNotifyChannel> DoAcquireGattNotifyChannel(
384 int16_t connection_handle,
385 uint16_t attribute_handle,
386 ChannelEventCallback&& event_fn);
387
389 bool DoHasSendLeAclCapability() const;
390
392 bool DoHasSendBrEdrAclCapability() const;
393
395 uint16_t DoGetNumFreeLeAclPackets() const;
396
398 uint16_t DoGetNumFreeBrEdrAclPackets() const;
399
400 // Handle HCI Event packet from the controller.
401 void HandleEventFromController(H4PacketWithHci&& h4_packet);
402
403 // Handle HCI Event packet from the host.
404 void HandleEventFromHost(H4PacketWithH4&& h4_packet);
405
406 // Handle HCI ACL data packet from the controller.
407 void HandleAclFromController(H4PacketWithHci&& h4_packet);
408
409 // Process an LE_META_EVENT
410 void HandleLeMetaEvent(H4PacketWithHci&& h4_packet);
411
412 // Process a Command_Complete event.
413 void HandleCommandCompleteEvent(H4PacketWithHci&& h4_packet);
414
415 // Handle HCI Command packet from the host.
416 void HandleCommandFromHost(H4PacketWithH4&& h4_packet);
417
418 // Handle HCI ACL data packet from the host.
419 void HandleAclFromHost(H4PacketWithH4&& h4_packet);
420
421 // Called when any type of connection complete event is received.
422 void OnConnectionCompleteSuccess(uint16_t connection_handle,
423 AclTransportType transport);
424
425 // AclDataChannel callback for when new ACL TX credits are received and more
426 // L2CAP packets can be sent.
427 void OnAclTxCredits();
428
429 // L2capChannelManagerInterface override:
430 Result<UniquePtr<ChannelProxy>> DoInterceptBasicModeChannel(
431 ConnectionHandle connection_handle,
432 uint16_t local_channel_id,
433 uint16_t remote_channel_id,
434 AclTransportType transport,
435 BufferReceiveFunction&& payload_from_controller_fn,
436 BufferReceiveFunction&& payload_from_host_fn,
437 ChannelEventCallback&& event_fn) override;
438
439 Result<UniquePtr<ChannelProxy>> InternalDoInterceptBasicModeChannel(
440 ConnectionHandle connection_handle,
441 uint16_t local_channel_id,
442 uint16_t remote_channel_id,
443 AclTransportType transport,
444 BufferReceiveFunction&& payload_from_controller_fn,
445 BufferReceiveFunction&& payload_from_host_fn,
446 ChannelEventCallback&& event_fn);
447
448 Result<UniquePtr<ChannelProxy>> DoInterceptCreditBasedFlowControlChannel(
449 ConnectionHandle connection_handle,
450 ConnectionOrientedChannelConfig rx_config,
451 ConnectionOrientedChannelConfig tx_config,
452 MultiBufReceiveFunction&& receive_fn,
453 ChannelEventCallback&& event_fn) override;
454
455 // Implementation-specific details that may vary between sync and async modes.
456 internal::ProxyHostImpl impl_;
457
458 // For sending non-ACL data to the host and controller. ACL traffic shall be
459 // sent through the `acl_data_channel_`.
460 HciTransport hci_transport_;
461
462 // Owns management of the LE ACL data channel.
463 AclDataChannel acl_data_channel_;
464
465 // Keeps track of the L2CAP-based channels managed by the proxy.
466 L2capChannelManager l2cap_channel_manager_;
467};
468
469} // namespace pw::bluetooth::proxy
Definition: allocator.h:42
Definition: result.h:145
Definition: status.h:120
Definition: dispatcher.h:74
H4PacketWithH4 is an H4Packet backed by an H4 buffer.
Definition: h4_packet.h:173
H4PacketWithHci is an H4Packet backed by an HCI buffer.
Definition: h4_packet.h:138
Definition: proxy_host.h:48
static constexpr size_t GetMaxNumAclConnections()
Returns the max number of simultaneous LE ACL connections supported.
Definition: proxy_host.h:340
pw::Result< GattNotifyChannel > AcquireGattNotifyChannel(int16_t connection_handle, uint16_t attribute_handle, ChannelEventCallback &&event_fn)
void HandleH4HciFromHost(H4PacketWithH4 &&h4_packet)
void RegisterL2capStatusDelegate(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, pw::Allocator *allocator)
Status SetDispatcher(async2::Dispatcher &dispatcher)
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::Result< L2capCoc > AcquireL2capCoc(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)
void UnregisterL2capStatusDelegate(L2capStatusDelegate &delegate)
ProxyHost(pw::Function< void(H4PacketWithHci &&packet)> &&send_to_host_fn, pw::Function< void(H4PacketWithH4 &&packet)> &&send_to_controller_fn, pw::Allocator &allocator)
void HandleH4HciFromController(H4PacketWithHci &&h4_packet)
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:73
Lightweight proxy for augmenting Bluetooth functionality.
Definition: h4_packet.h:27