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:
61 // TODO: b/505912880 - Fix deadlock when release_fn sends packets.
70 ProxyHost(pw::Function<void(H4PacketWithHci&& packet)>&& send_to_host_fn,
71 pw::Function<void(H4PacketWithH4&& packet)>&& send_to_controller_fn,
72 pw::Allocator& allocator);
73
90 ProxyHost(pw::Function<void(H4PacketWithHci&& packet)>&& send_to_host_fn,
91 pw::Function<void(H4PacketWithH4&& packet)>&& send_to_controller_fn,
92 uint16_t le_acl_credits_to_reserve,
93 uint16_t br_edr_acl_credits_to_reserve,
94 pw::Allocator* allocator);
95
96 ProxyHost() = delete;
97 ProxyHost(const ProxyHost&) = delete;
98 ProxyHost& operator=(const ProxyHost&) = delete;
99 ProxyHost(ProxyHost&&) = delete;
100 ProxyHost& operator=(ProxyHost&&) = delete;
103 ~ProxyHost() override;
104
105 // ##### Container API
106 // Containers are expected to call these functions (in addition to ctor).
107
123
153
164 void Reset();
165
166 // ##### Container async API
167
178
179 // ##### Client APIs
180
187 void RegisterL2capStatusDelegate(L2capStatusDelegate& delegate);
188
193 void UnregisterL2capStatusDelegate(L2capStatusDelegate& delegate);
194
228 multibuf::MultiBufAllocator& rx_multibuf_allocator,
229 uint16_t connection_handle,
230 L2capCoc::CocConfig rx_config,
231 L2capCoc::CocConfig tx_config,
232 Function<void(multibuf::MultiBuf&& payload)>&& receive_fn,
233 ChannelEventCallback&& event_fn);
234
285 multibuf::MultiBufAllocator& rx_multibuf_allocator,
286 uint16_t connection_handle,
287 uint16_t local_cid,
288 uint16_t remote_cid,
289 AclTransportType transport,
290 OptionalPayloadReceiveCallback&& payload_from_controller_fn,
291 OptionalPayloadReceiveCallback&& payload_from_host_fn,
292 ChannelEventCallback&& event_fn);
293
314 int16_t connection_handle,
315 uint16_t attribute_handle,
316 ChannelEventCallback&& event_fn);
317
322
327
330 uint16_t GetNumFreeLeAclPackets() const;
331
335
337 static constexpr size_t GetMaxNumAclConnections() {
338 return AclDataChannel::GetMaxNumAclConnections();
339 }
340
341 private:
342 friend class internal::ProxyHostImpl;
343
345 void DoHandleH4HciFromHost(H4PacketWithH4&& h4_packet);
346
348 void DoHandleH4HciFromController(H4PacketWithHci&& h4_packet);
349
351 void DoReset();
352
354 void DoRegisterL2capStatusDelegate(L2capStatusDelegate& delegate);
355
357 void DoUnregisterL2capStatusDelegate(L2capStatusDelegate& delegate);
358
360 pw::Result<L2capCoc> DoAcquireL2capCoc(
361 multibuf::MultiBufAllocator& rx_multibuf_allocator,
362 uint16_t connection_handle,
363 L2capCoc::CocConfig rx_config,
364 L2capCoc::CocConfig tx_config,
365 Function<void(multibuf::MultiBuf&& payload)>&& receive_fn,
366 ChannelEventCallback&& event_fn);
367
369 pw::Result<BasicL2capChannel> DoAcquireBasicL2capChannel(
370 multibuf::MultiBufAllocator& rx_multibuf_allocator,
371 uint16_t connection_handle,
372 uint16_t local_cid,
373 uint16_t remote_cid,
374 AclTransportType transport,
375 OptionalPayloadReceiveCallback&& payload_from_controller_fn,
376 OptionalPayloadReceiveCallback&& payload_from_host_fn,
377 ChannelEventCallback&& event_fn);
378
380 pw::Result<GattNotifyChannel> DoAcquireGattNotifyChannel(
381 int16_t connection_handle,
382 uint16_t attribute_handle,
383 ChannelEventCallback&& event_fn);
384
386 bool DoHasSendLeAclCapability() const;
387
389 bool DoHasSendBrEdrAclCapability() const;
390
392 uint16_t DoGetNumFreeLeAclPackets() const;
393
395 uint16_t DoGetNumFreeBrEdrAclPackets() const;
396
397 // Handle HCI Event packet from the controller.
398 void HandleEventFromController(H4PacketWithHci&& h4_packet);
399
400 // Handle HCI Event packet from the host.
401 void HandleEventFromHost(H4PacketWithH4&& h4_packet);
402
403 // Handle HCI ACL data packet from the controller.
404 void HandleAclFromController(H4PacketWithHci&& h4_packet);
405
406 // Process an LE_META_EVENT
407 void HandleLeMetaEvent(H4PacketWithHci&& h4_packet);
408
409 // Process a Command_Complete event.
410 void HandleCommandCompleteEvent(H4PacketWithHci&& h4_packet);
411
412 // Handle HCI Command packet from the host.
413 void HandleCommandFromHost(H4PacketWithH4&& h4_packet);
414
415 // Handle HCI ACL data packet from the host.
416 void HandleAclFromHost(H4PacketWithH4&& h4_packet);
417
418 // Called when any type of connection complete event is received.
419 void OnConnectionCompleteSuccess(uint16_t connection_handle,
420 AclTransportType transport);
421
422 // AclDataChannel callback for when new ACL TX credits are received and more
423 // L2CAP packets can be sent.
424 void OnAclTxCredits();
425
426 // L2capChannelManagerInterface override:
427 Result<UniquePtr<ChannelProxy>> DoInterceptBasicModeChannel(
428 ConnectionHandle connection_handle,
429 uint16_t local_channel_id,
430 uint16_t remote_channel_id,
431 AclTransportType transport,
432 BufferReceiveFunction&& payload_from_controller_fn,
433 BufferReceiveFunction&& payload_from_host_fn,
434 ChannelEventCallback&& event_fn) override;
435
436 Result<UniquePtr<ChannelProxy>> InternalDoInterceptBasicModeChannel(
437 ConnectionHandle connection_handle,
438 uint16_t local_channel_id,
439 uint16_t remote_channel_id,
440 AclTransportType transport,
441 BufferReceiveFunction&& payload_from_controller_fn,
442 BufferReceiveFunction&& payload_from_host_fn,
443 ChannelEventCallback&& event_fn);
444
445 Result<UniquePtr<ChannelProxy>> DoInterceptCreditBasedFlowControlChannel(
446 ConnectionHandle connection_handle,
447 ConnectionOrientedChannelConfig rx_config,
448 ConnectionOrientedChannelConfig tx_config,
449 MultiBufReceiveFunction&& receive_fn,
450 ChannelEventCallback&& event_fn) override;
451
452 // Implementation-specific details that may vary between sync and async modes.
453 internal::ProxyHostImpl impl_;
454
455 // For sending non-ACL data to the host and controller. ACL traffic shall be
456 // sent through the `acl_data_channel_`.
457 HciTransport hci_transport_;
458
459 // Owns management of the LE ACL data channel.
460 AclDataChannel acl_data_channel_;
461
462 // Keeps track of the L2CAP-based channels managed by the proxy.
463 L2capChannelManager l2cap_channel_manager_;
464};
465
466} // 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:337
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