Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Linux] Fix sending reply to D-Bus release/confirm calls #32810

Merged
merged 4 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/platform/Linux/bluez/BluezAdvertisement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ gboolean BluezAdvertisement::BluezLEAdvertisement1Release(BluezLEAdvertisement1
// We can use it to update the state of the advertisement in the CHIP layer.
ChipLogDetail(DeviceLayer, "BLE advertisement stopped by BlueZ");
mIsAdvertising = false;
bluez_leadvertisement1_complete_release(aAdv, aInvocation);
BLEManagerImpl::NotifyBLEPeripheralAdvReleased();
return TRUE;
}
Expand Down
54 changes: 25 additions & 29 deletions src/platform/Linux/bluez/BluezEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,8 @@ gboolean BluezEndpoint::BluezCharacteristicReadValue(BluezGattCharacteristic1 *
return TRUE;
}

static void Bluez_gatt_characteristic1_complete_acquire_write_with_fd(GDBusMethodInvocation * invocation, int fd, guint16 mtu)
{
GUnixFDList * fd_list = g_unix_fd_list_new();
int index = g_unix_fd_list_append(fd_list, fd, nullptr);
g_dbus_method_invocation_return_value_with_unix_fd_list(invocation, g_variant_new("(@hq)", g_variant_new_handle(index), mtu),
fd_list);
g_object_unref(fd_list);
}

gboolean BluezEndpoint::BluezCharacteristicAcquireWrite(BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInvocation,
GVariant * aOptions)
GUnixFDList * aFDList, GVariant * aOptions)
{
int fds[2] = { -1, -1 };
#if CHIP_ERROR_LOGGING
Expand Down Expand Up @@ -135,14 +126,15 @@ gboolean BluezEndpoint::BluezCharacteristicAcquireWrite(BluezGattCharacteristic1
conn->SetupWriteHandler(fds[0]);
bluez_gatt_characteristic1_set_write_acquired(aChar, TRUE);

Bluez_gatt_characteristic1_complete_acquire_write_with_fd(aInvocation, fds[1], conn->GetMTU());
close(fds[1]);
GUnixFDList * fdList = g_unix_fd_list_new_from_array(&fds[1], 1);
bluez_gatt_characteristic1_complete_acquire_write(aChar, aInvocation, fdList, g_variant_new_handle(0), conn->GetMTU());
g_object_unref(fdList);

return TRUE;
}

static gboolean BluezCharacteristicAcquireWriteError(BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInvocation,
GVariant * aOptions)
GUnixFDList * aFDList, GVariant * aOptions)
{
ChipLogDetail(DeviceLayer, "Received %s", __func__);
g_dbus_method_invocation_return_dbus_error(aInvocation, "org.bluez.Error.NotSupported",
Expand All @@ -151,7 +143,7 @@ static gboolean BluezCharacteristicAcquireWriteError(BluezGattCharacteristic1 *
}

gboolean BluezEndpoint::BluezCharacteristicAcquireNotify(BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInvocation,
GVariant * aOptions)
GUnixFDList * aFDList, GVariant * aOptions)
{
int fds[2] = { -1, -1 };
#if CHIP_ERROR_LOGGING
Expand Down Expand Up @@ -196,9 +188,9 @@ gboolean BluezEndpoint::BluezCharacteristicAcquireNotify(BluezGattCharacteristic
bluez_gatt_characteristic1_set_notify_acquired(aChar, TRUE);
conn->SetNotifyAcquired(true);

// same reply as for AcquireWrite
Bluez_gatt_characteristic1_complete_acquire_write_with_fd(aInvocation, fds[1], conn->GetMTU());
close(fds[1]);
GUnixFDList * fdList = g_unix_fd_list_new_from_array(&fds[1], 1);
bluez_gatt_characteristic1_complete_acquire_notify(aChar, aInvocation, fdList, g_variant_new_handle(0), conn->GetMTU());
g_object_unref(fdList);

BLEManagerImpl::HandleTXCharCCCDWrite(conn);

Expand All @@ -218,6 +210,7 @@ gboolean BluezEndpoint::BluezCharacteristicConfirm(BluezGattCharacteristic1 * aC
{
BluezConnection * conn = GetBluezConnectionViaDevice();
ChipLogDetail(Ble, "Indication confirmation, %p", conn);
bluez_gatt_characteristic1_complete_confirm(aChar, aInvocation);
BLEManagerImpl::HandleTXComplete(conn);
return TRUE;
}
Expand Down Expand Up @@ -453,10 +446,11 @@ void BluezEndpoint::SetupGattService()
G_CALLBACK(+[](BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GVariant * aOpt,
BluezEndpoint * self) { return self->BluezCharacteristicReadValue(aChar, aInv, aOpt); }),
this);
g_signal_connect(mC1.get(), "handle-acquire-write",
G_CALLBACK(+[](BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GVariant * aOpt,
BluezEndpoint * self) { return self->BluezCharacteristicAcquireWrite(aChar, aInv, aOpt); }),
this);
g_signal_connect(
mC1.get(), "handle-acquire-write",
G_CALLBACK(+[](BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GUnixFDList * aFDList, GVariant * aOpt,
BluezEndpoint * self) { return self->BluezCharacteristicAcquireWrite(aChar, aInv, aFDList, aOpt); }),
this);
g_signal_connect(mC1.get(), "handle-acquire-notify", G_CALLBACK(BluezCharacteristicAcquireNotifyError), nullptr);
g_signal_connect(mC1.get(), "handle-confirm", G_CALLBACK(BluezCharacteristicConfirmError), nullptr);

Expand All @@ -467,10 +461,11 @@ void BluezEndpoint::SetupGattService()
BluezEndpoint * self) { return self->BluezCharacteristicReadValue(aChar, aInv, aOpt); }),
this);
g_signal_connect(mC2.get(), "handle-acquire-write", G_CALLBACK(BluezCharacteristicAcquireWriteError), nullptr);
g_signal_connect(mC2.get(), "handle-acquire-notify",
G_CALLBACK(+[](BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GVariant * aOpt,
BluezEndpoint * self) { return self->BluezCharacteristicAcquireNotify(aChar, aInv, aOpt); }),
this);
g_signal_connect(
mC2.get(), "handle-acquire-notify",
G_CALLBACK(+[](BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GUnixFDList * aFDList, GVariant * aOpt,
BluezEndpoint * self) { return self->BluezCharacteristicAcquireNotify(aChar, aInv, aFDList, aOpt); }),
this);
g_signal_connect(mC2.get(), "handle-confirm",
G_CALLBACK(+[](BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, BluezEndpoint * self) {
return self->BluezCharacteristicConfirm(aChar, aInv);
Expand All @@ -489,10 +484,11 @@ void BluezEndpoint::SetupGattService()
BluezEndpoint * self) { return self->BluezCharacteristicReadValue(aChar, aInv, aOpt); }),
this);
g_signal_connect(mC3.get(), "handle-acquire-write", G_CALLBACK(BluezCharacteristicAcquireWriteError), nullptr);
g_signal_connect(mC3.get(), "handle-acquire-notify",
G_CALLBACK(+[](BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GVariant * aOpt,
BluezEndpoint * self) { return self->BluezCharacteristicAcquireNotify(aChar, aInv, aOpt); }),
this);
g_signal_connect(
mC3.get(), "handle-acquire-notify",
G_CALLBACK(+[](BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GUnixFDList * aFDList, GVariant * aOpt,
BluezEndpoint * self) { return self->BluezCharacteristicAcquireNotify(aChar, aInv, aFDList, aOpt); }),
this);
g_signal_connect(mC3.get(), "handle-confirm",
G_CALLBACK(+[](BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, BluezEndpoint * self) {
return self->BluezCharacteristicConfirm(aChar, aInv);
Expand Down
6 changes: 4 additions & 2 deletions src/platform/Linux/bluez/BluezEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ class BluezEndpoint : public BluezObjectManagerAdapterNotificationsDelegate
BluezConnection * GetBluezConnectionViaDevice();

gboolean BluezCharacteristicReadValue(BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GVariant * aOptions);
gboolean BluezCharacteristicAcquireWrite(BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GVariant * aOptions);
gboolean BluezCharacteristicAcquireNotify(BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GVariant * aOptions);
gboolean BluezCharacteristicAcquireWrite(BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GUnixFDList * aFDList,
GVariant * aOptions);
gboolean BluezCharacteristicAcquireNotify(BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GUnixFDList * aFDList,
GVariant * aOptions);
gboolean BluezCharacteristicConfirm(BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv);

void RegisterGattApplicationDone(GObject * aObject, GAsyncResult * aResult);
Expand Down
2 changes: 2 additions & 0 deletions src/platform/Linux/dbus/bluez/DbusBluez.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,13 @@
<arg name="options" type="a{sv}" direction="in"/>
</method>
<method name="AcquireWrite">
<annotation name="org.gtk.GDBus.C.UnixFD" value="true" />
<arg name="options" type="a{sv}" direction="in" />
<arg name="fd" type="h" direction="out" />
<arg name="mtu" type="q" direction="out" />
</method>
<method name="AcquireNotify">
<annotation name="org.gtk.GDBus.C.UnixFD" value="true" />
<arg name="options" type="a{sv}" direction="in" />
<arg name="fd" type="h" direction="out" />
<arg name="mtu" type="q" direction="out" />
Expand Down
Loading