Skip to content

Commit

Permalink
pw_bluetooth_proxy: Support event_fn in GATT channels
Browse files Browse the repository at this point in the history
Tested in later CL that tests all channel types

Bug: 369709521
Bug: 379337272
Change-Id: I08d42a49a5bf5458a9b4118e6f50566d19d9c5eb
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/267517
Reviewed-by: Austin Foxley <[email protected]>
Pigweed-Auto-Submit: David Rees <[email protected]>
Lint: Lint 🤖 <[email protected]>
Docs-Not-Needed: David Rees <[email protected]>
Commit-Queue: David Rees <[email protected]>
  • Loading branch information
studgeek authored and CQ Bot Account committed Feb 13, 2025
1 parent 8237d75 commit 4474c41
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
16 changes: 10 additions & 6 deletions pw_bluetooth_proxy/gatt_notify_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ pw::Status GattNotifyChannel::Write(pw::span<const uint8_t> attribute_value) {
pw::Result<GattNotifyChannel> GattNotifyChannel::Create(
L2capChannelManager& l2cap_channel_manager,
uint16_t connection_handle,
uint16_t attribute_handle) {
uint16_t attribute_handle,
Function<void(L2capChannelEvent event)>&& event_fn) {
if (!AreValidParameters(/*connection_handle=*/connection_handle,
/*local_cid=*/kAttributeProtocolCID,
/*remote_cid=*/kAttributeProtocolCID)) {
Expand All @@ -91,12 +92,15 @@ pw::Result<GattNotifyChannel> GattNotifyChannel::Create(
}
return GattNotifyChannel(/*l2cap_channel_manager=*/l2cap_channel_manager,
/*connection_handle=*/connection_handle,
/*attribute_handle=*/attribute_handle);
/*attribute_handle=*/attribute_handle,
std::move(event_fn));
}

GattNotifyChannel::GattNotifyChannel(L2capChannelManager& l2cap_channel_manager,
uint16_t connection_handle,
uint16_t attribute_handle)
GattNotifyChannel::GattNotifyChannel(
L2capChannelManager& l2cap_channel_manager,
uint16_t connection_handle,
uint16_t attribute_handle,
Function<void(L2capChannelEvent event)>&& event_fn)
: L2capChannel(/*l2cap_channel_manager=*/l2cap_channel_manager,
/*rx_multibuf_allocator*/ nullptr,
/*connection_handle=*/connection_handle,
Expand All @@ -105,7 +109,7 @@ GattNotifyChannel::GattNotifyChannel(L2capChannelManager& l2cap_channel_manager,
/*remote_cid=*/kAttributeProtocolCID,
/*payload_from_controller_fn=*/nullptr,
/*payload_from_host_fn=*/nullptr,
/*event_fn=*/nullptr),
/*event_fn=*/std::move(event_fn)),
attribute_handle_(attribute_handle) {}

} // namespace pw::bluetooth::proxy
12 changes: 7 additions & 5 deletions pw_bluetooth_proxy/proxy_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -399,22 +399,24 @@ pw::Result<BasicL2capChannel> ProxyHost::AcquireBasicL2capChannel(
pw::Result<GattNotifyChannel> ProxyHost::AcquireGattNotifyChannel(
int16_t connection_handle,
uint16_t attribute_handle,
[[maybe_unused]] Function<void(L2capChannelEvent event)>&& event_fn) {
Function<void(L2capChannelEvent event)>&& event_fn) {
Status status = acl_data_channel_.CreateAclConnection(connection_handle,
AclTransportType::kLe);
if (status != OkStatus() && status != Status::AlreadyExists()) {
return pw::Status::Unavailable();
}
return GattNotifyChannelInternal::Create(
l2cap_channel_manager_, connection_handle, attribute_handle);
return GattNotifyChannelInternal::Create(l2cap_channel_manager_,
connection_handle,
attribute_handle,
std::move(event_fn));
}

StatusWithMultiBuf ProxyHost::SendGattNotify(uint16_t connection_handle,
uint16_t attribute_handle,
pw::multibuf::MultiBuf&& payload) {
// TODO: https://pwbug.dev/369709521 - Migrate clients to channel API.
pw::Result<GattNotifyChannel> channel_result =
AcquireGattNotifyChannel(connection_handle, attribute_handle);
AcquireGattNotifyChannel(connection_handle, attribute_handle, nullptr);
if (!channel_result.ok()) {
return {channel_result.status(), std::move(payload)};
}
Expand All @@ -426,7 +428,7 @@ pw::Status ProxyHost::SendGattNotify(uint16_t connection_handle,
pw::span<const uint8_t> attribute_value) {
// TODO: https://pwbug.dev/369709521 - Migrate clients to channel API.
pw::Result<GattNotifyChannel> channel_result =
AcquireGattNotifyChannel(connection_handle, attribute_handle);
AcquireGattNotifyChannel(connection_handle, attribute_handle, nullptr);
if (!channel_result.ok()) {
return channel_result.status();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class GattNotifyChannel : public L2capChannel {
static pw::Result<GattNotifyChannel> Create(
L2capChannelManager& l2cap_channel_manager,
uint16_t connection_handle,
uint16_t attribute_handle);
uint16_t attribute_handle,
Function<void(L2capChannelEvent event)>&& event_fn);

bool DoHandlePduFromController(pw::span<uint8_t>) override {
// Forward all packets to host.
Expand All @@ -58,9 +59,11 @@ class GattNotifyChannel : public L2capChannel {
// TODO: https://pwbug.dev/349602172 - Define ATT CID in pw_bluetooth.
static constexpr uint16_t kAttributeProtocolCID = 0x0004;

explicit GattNotifyChannel(L2capChannelManager& l2cap_channel_manager,
uint16_t connection_handle,
uint16_t attribute_handle);
explicit GattNotifyChannel(
L2capChannelManager& l2cap_channel_manager,
uint16_t connection_handle,
uint16_t attribute_handle,
Function<void(L2capChannelEvent event)>&& event_fn);

uint16_t attribute_handle_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ class GattNotifyChannelInternal final : public GattNotifyChannel {
static pw::Result<GattNotifyChannel> Create(
L2capChannelManager& l2cap_channel_manager,
uint16_t connection_handle,
uint16_t attribute_handle) {
return GattNotifyChannel::Create(
l2cap_channel_manager, connection_handle, attribute_handle);
uint16_t attribute_handle,
Function<void(L2capChannelEvent event)>&& event_fn) {
return GattNotifyChannel::Create(l2cap_channel_manager,
connection_handle,
attribute_handle,
std::move(event_fn));
}
};

Expand Down
3 changes: 1 addition & 2 deletions pw_bluetooth_proxy/public/pw_bluetooth_proxy/proxy_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ class ProxyHost {
pw::Result<GattNotifyChannel> AcquireGattNotifyChannel(
int16_t connection_handle,
uint16_t attribute_handle,
// TODO: https://pwbug.dev/369709521 - Add event_fn support.
Function<void(L2capChannelEvent event)>&& event_fn = nullptr);
Function<void(L2capChannelEvent event)>&& event_fn);

/// Send a GATT Notify to the indicated connection.
///
Expand Down

0 comments on commit 4474c41

Please sign in to comment.