Skip to content

Commit

Permalink
[linux] Use BLE notification instead of indication (#17692)
Browse files Browse the repository at this point in the history
Linux BLE peripherals use GATT indications instead of
notifications as defined in the spec.
  • Loading branch information
Damian-Nordic authored and pull[bot] committed Aug 28, 2023
1 parent fbdfb5a commit 4797955
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions src/platform/Linux/bluez/Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,18 +627,6 @@ static gboolean BluezCharacteristicStopNotify(BluezGattCharacteristic1 * aChar,
return isSuccess ? TRUE : FALSE;
}

static gboolean BluezCharacteristicConfirm(BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInvocation,
gpointer apClosure)
{
BluezEndpoint * endpoint = static_cast<BluezEndpoint *>(apClosure);
BluezConnection * conn = GetBluezConnectionViaDevice(endpoint);

ChipLogDetail(Ble, "Indication confirmation, %p", conn);
BLEManagerImpl::HandleTXComplete(conn);

return TRUE;
}

static gboolean BluezCharacteristicStopNotifyError(BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInvocation)
{
g_dbus_method_invocation_return_dbus_error(aInvocation, "org.bluez.Error.Failed",
Expand Down Expand Up @@ -1235,7 +1223,7 @@ static void BluezPeripheralObjectsSetup(gpointer apClosure)
{

static const char * const c1_flags[] = { "write", nullptr };
static const char * const c2_flags[] = { "read", "indicate", nullptr };
static const char * const c2_flags[] = { "read", "notify", nullptr };
static const char * const c3_flags[] = { "read", nullptr };

BluezEndpoint * endpoint = static_cast<BluezEndpoint *>(apClosure);
Expand Down Expand Up @@ -1264,7 +1252,7 @@ static void BluezPeripheralObjectsSetup(gpointer apClosure)
g_signal_connect(endpoint->mpC2, "handle-acquire-notify", G_CALLBACK(BluezCharacteristicAcquireNotify), apClosure);
g_signal_connect(endpoint->mpC2, "handle-start-notify", G_CALLBACK(BluezCharacteristicStartNotify), apClosure);
g_signal_connect(endpoint->mpC2, "handle-stop-notify", G_CALLBACK(BluezCharacteristicStopNotify), apClosure);
g_signal_connect(endpoint->mpC2, "handle-confirm", G_CALLBACK(BluezCharacteristicConfirm), apClosure);
g_signal_connect(endpoint->mpC2, "handle-confirm", G_CALLBACK(BluezCharacteristicConfirmError), apClosure);

ChipLogDetail(DeviceLayer, "CHIP BTP C1 %s", bluez_gatt_characteristic1_get_service(endpoint->mpC1));
ChipLogDetail(DeviceLayer, "CHIP BTP C2 %s", bluez_gatt_characteristic1_get_service(endpoint->mpC2));
Expand All @@ -1281,7 +1269,7 @@ static void BluezPeripheralObjectsSetup(gpointer apClosure)
g_signal_connect(endpoint->mpC3, "handle-acquire-notify", G_CALLBACK(BluezCharacteristicAcquireNotify), apClosure);
g_signal_connect(endpoint->mpC3, "handle-start-notify", G_CALLBACK(BluezCharacteristicStartNotify), apClosure);
g_signal_connect(endpoint->mpC3, "handle-stop-notify", G_CALLBACK(BluezCharacteristicStopNotify), apClosure);
g_signal_connect(endpoint->mpC3, "handle-confirm", G_CALLBACK(BluezCharacteristicConfirm), apClosure);
g_signal_connect(endpoint->mpC3, "handle-confirm", G_CALLBACK(BluezCharacteristicConfirmError), apClosure);
// update the characteristic value
UpdateAdditionalDataCharacteristic(endpoint->mpC3);
ChipLogDetail(DeviceLayer, "CHIP BTP C3 %s", bluez_gatt_characteristic1_get_service(endpoint->mpC3));
Expand Down Expand Up @@ -1369,7 +1357,7 @@ static int StartupEndpointBindings(BluezEndpoint * endpoint)
return 0;
}

static gboolean BluezC2Indicate(ConnectionDataBundle * closure)
static gboolean BluezC2Notify(ConnectionDataBundle * closure)
{
BluezConnection * conn = nullptr;
GError * error = nullptr;
Expand All @@ -1381,7 +1369,7 @@ static gboolean BluezC2Indicate(ConnectionDataBundle * closure)

conn = closure->mpConn;
VerifyOrExit(conn != nullptr, ChipLogError(DeviceLayer, "BluezConnection is NULL in %s", __func__));
VerifyOrExit(conn->mpC2 != nullptr, ChipLogError(DeviceLayer, "FAIL: C2 Indicate: %s", "NULL C2"));
VerifyOrExit(conn->mpC2 != nullptr, ChipLogError(DeviceLayer, "FAIL: C2 Notify: %s", "NULL C2"));

if (bluez_gatt_characteristic1_get_notify_acquired(conn->mpC2) == TRUE)
{
Expand All @@ -1392,14 +1380,16 @@ static gboolean BluezC2Indicate(ConnectionDataBundle * closure)
g_variant_unref(closure->mpVal);
closure->mpVal = nullptr;

VerifyOrExit(status == G_IO_STATUS_NORMAL, ChipLogError(DeviceLayer, "FAIL: C2 Indicate: %s", error->message));
VerifyOrExit(status == G_IO_STATUS_NORMAL, ChipLogError(DeviceLayer, "FAIL: C2 Notify: %s", error->message));
}
else
{
bluez_gatt_characteristic1_set_value(conn->mpC2, closure->mpVal);
closure->mpVal = nullptr;
}

BLEManagerImpl::HandleTXComplete(conn);

exit:
if (closure != nullptr)
{
Expand Down Expand Up @@ -1430,7 +1420,7 @@ bool SendBluezIndication(BLE_CONNECTION_OBJECT apConn, chip::System::PacketBuffe

VerifyOrExit(!apBuf.IsNull(), ChipLogError(DeviceLayer, "apBuf is NULL in %s", __func__));

success = MainLoop::Instance().Schedule(BluezC2Indicate, MakeConnectionDataBundle(apConn, apBuf));
success = MainLoop::Instance().Schedule(BluezC2Notify, MakeConnectionDataBundle(apConn, apBuf));

exit:
return success;
Expand Down Expand Up @@ -1683,7 +1673,7 @@ static gboolean SubscribeCharacteristicImpl(BluezConnection * connection)
VerifyOrExit(connection->mpC2 != nullptr, ChipLogError(DeviceLayer, "C2 is NULL in %s", __func__));
c2 = BLUEZ_GATT_CHARACTERISTIC1(connection->mpC2);

// Get notifications on the TX characteristic change (e.g. indication is received)
// Get notifications on the TX characteristic change (e.g. notification is received)
g_signal_connect(c2, "g-properties-changed", G_CALLBACK(OnCharacteristicChanged), connection);
bluez_gatt_characteristic1_call_start_notify(connection->mpC2, nullptr, SubscribeCharacteristicDone, connection);

Expand Down

0 comments on commit 4797955

Please sign in to comment.