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_async2/dispatcher.h"
18#include "pw_bluetooth_proxy/gatt_notify_channel.h"
19#include "pw_bluetooth_proxy/internal/acl_data_channel.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/internal/multibuf.h"
23#include "pw_bluetooth_proxy/l2cap_channel_common.h"
24#include "pw_bluetooth_proxy/l2cap_coc.h"
25#include "pw_bluetooth_proxy/l2cap_status_delegate.h"
26#include "pw_function/function.h"
27#include "pw_status/status.h"
28
29#if PW_BLUETOOTH_PROXY_ASYNC == 0
30#include "pw_bluetooth_proxy/internal/proxy_host_sync.h"
31#else
32#include "pw_bluetooth_proxy/internal/proxy_host_async.h"
33#endif // PW_BLUETOOTH_PROXY_ASYNC
34
36namespace pw::bluetooth::proxy {
37
39
42class ProxyHost {
43 public:
57 ProxyHost(pw::Function<void(H4PacketWithHci&& packet)>&& send_to_host_fn,
58 pw::Function<void(H4PacketWithH4&& packet)>&& send_to_controller_fn,
59 uint16_t le_acl_credits_to_reserve,
60 uint16_t br_edr_acl_credits_to_reserve,
61 pw::Allocator* allocator = nullptr);
62
63 ProxyHost() = delete;
64 ProxyHost(const ProxyHost&) = delete;
65 ProxyHost& operator=(const ProxyHost&) = delete;
66 ProxyHost(ProxyHost&&) = delete;
67 ProxyHost& operator=(ProxyHost&&) = delete;
71
72 // ##### Container API
73 // Containers are expected to call these functions (in addition to ctor).
74
90
120
131 void Reset();
132
133 // ##### Container async API
134
145
146 // ##### Client APIs
147
154 void RegisterL2capStatusDelegate(L2capStatusDelegate& delegate);
155
160 void UnregisterL2capStatusDelegate(L2capStatusDelegate& delegate);
161
193 MultiBufAllocator& rx_multibuf_allocator,
194 uint16_t connection_handle,
195 L2capCoc::CocConfig rx_config,
196 L2capCoc::CocConfig tx_config,
197 Function<void(FlatConstMultiBuf&& payload)>&& receive_fn,
198 ChannelEventCallback&& event_fn);
199
248 MultiBufAllocator& rx_multibuf_allocator,
249 uint16_t connection_handle,
250 uint16_t local_cid,
251 uint16_t remote_cid,
252 AclTransportType transport,
253 OptionalPayloadReceiveCallback&& payload_from_controller_fn,
254 OptionalPayloadReceiveCallback&& payload_from_host_fn,
255 ChannelEventCallback&& event_fn);
256
277 int16_t connection_handle,
278 uint16_t attribute_handle,
279 ChannelEventCallback&& event_fn);
280
285
290
293 uint16_t GetNumFreeLeAclPackets() const;
294
298
300 static constexpr size_t GetMaxNumAclConnections() {
301 return AclDataChannel::GetMaxNumAclConnections();
302 }
303
304 private:
305 friend class internal::ProxyHostImpl;
306
308 void DoHandleH4HciFromHost(H4PacketWithH4&& h4_packet);
309
311 void DoHandleH4HciFromController(H4PacketWithHci&& h4_packet);
312
314 void DoReset();
315
317 void DoRegisterL2capStatusDelegate(L2capStatusDelegate& delegate);
318
320 void DoUnregisterL2capStatusDelegate(L2capStatusDelegate& delegate);
321
323 pw::Result<L2capCoc> DoAcquireL2capCoc(
324 MultiBufAllocator& rx_multibuf_allocator,
325 uint16_t connection_handle,
326 L2capCoc::CocConfig rx_config,
327 L2capCoc::CocConfig tx_config,
328 Function<void(FlatConstMultiBuf&& payload)>&& receive_fn,
329 ChannelEventCallback&& event_fn);
330
332 pw::Result<BasicL2capChannel> DoAcquireBasicL2capChannel(
333 MultiBufAllocator& rx_multibuf_allocator,
334 uint16_t connection_handle,
335 uint16_t local_cid,
336 uint16_t remote_cid,
337 AclTransportType transport,
338 OptionalPayloadReceiveCallback&& payload_from_controller_fn,
339 OptionalPayloadReceiveCallback&& payload_from_host_fn,
340 ChannelEventCallback&& event_fn);
341
343 pw::Result<GattNotifyChannel> DoAcquireGattNotifyChannel(
344 int16_t connection_handle,
345 uint16_t attribute_handle,
346 ChannelEventCallback&& event_fn);
347
349 bool DoHasSendLeAclCapability() const;
350
352 bool DoHasSendBrEdrAclCapability() const;
353
355 uint16_t DoGetNumFreeLeAclPackets() const;
356
358 uint16_t DoGetNumFreeBrEdrAclPackets() const;
359
360 // Handle HCI Event packet from the controller.
361 void HandleEventFromController(H4PacketWithHci&& h4_packet);
362
363 // Handle HCI Event packet from the host.
364 void HandleEventFromHost(H4PacketWithH4&& h4_packet);
365
366 // Handle HCI ACL data packet from the controller.
367 void HandleAclFromController(H4PacketWithHci&& h4_packet);
368
369 // Process an LE_META_EVENT
370 void HandleLeMetaEvent(H4PacketWithHci&& h4_packet);
371
372 // Process a Command_Complete event.
373 void HandleCommandCompleteEvent(H4PacketWithHci&& h4_packet);
374
375 // Handle HCI Command packet from the host.
376 void HandleCommandFromHost(H4PacketWithH4&& h4_packet);
377
378 // Handle HCI ACL data packet from the host.
379 void HandleAclFromHost(H4PacketWithH4&& h4_packet);
380
381 // Called when any type of connection complete event is received.
382 void OnConnectionCompleteSuccess(uint16_t connection_handle,
383 AclTransportType transport);
384
385 // AclDataChannel callback for when new ACL TX credits are received and more
386 // L2CAP packets can be sent.
387 void OnAclTxCredits();
388
389 // Implementation-specific details that may vary between sync and async modes.
390 internal::ProxyHostImpl impl_;
391
392 // For sending non-ACL data to the host and controller. ACL traffic shall be
393 // sent through the `acl_data_channel_`.
394 HciTransport hci_transport_;
395
396 // Owns management of the LE ACL data channel.
397 AclDataChannel acl_data_channel_;
398
399 // Keeps track of the L2CAP-based channels managed by the proxy.
400 L2capChannelManager l2cap_channel_manager_;
401};
402
403} // namespace pw::bluetooth::proxy
Definition: allocator.h:43
Definition: multibuf_v2.h:185
Definition: result.h:143
Definition: status.h:120
Definition: dispatcher.h:53
H4PacketWithH4 is an H4Packet backed by an H4 buffer.
Definition: h4_packet.h:141
H4PacketWithHci is an H4Packet backed by an HCI buffer.
Definition: h4_packet.h:113
Definition: proxy_host.h:42
static constexpr size_t GetMaxNumAclConnections()
Returns the max number of simultaneous LE ACL connections supported.
Definition: proxy_host.h:300
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< BasicL2capChannel > AcquireBasicL2capChannel(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)
Status SetDispatcher(async2::Dispatcher &dispatcher)
uint16_t GetNumFreeLeAclPackets() const
uint16_t GetNumFreeBrEdrAclPackets() const
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=nullptr)
void UnregisterL2capStatusDelegate(L2capStatusDelegate &delegate)
pw::Result< L2capCoc > AcquireL2capCoc(MultiBufAllocator &rx_multibuf_allocator, uint16_t connection_handle, L2capCoc::CocConfig rx_config, L2capCoc::CocConfig tx_config, Function< void(FlatConstMultiBuf &&payload)> &&receive_fn, ChannelEventCallback &&event_fn)
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:24