From 109554889951629f646ca97a2a3f6f141f1458b8 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Fri, 12 May 2023 15:06:24 +0200 Subject: [PATCH] Initialize BLE iterator on Matter glib context (#26540) This fixes Python bindings after changes in 0ada46a. --- src/platform/Linux/bluez/AdapterIterator.cpp | 24 ++++++++++++-------- src/platform/Linux/bluez/AdapterIterator.h | 4 +++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/platform/Linux/bluez/AdapterIterator.cpp b/src/platform/Linux/bluez/AdapterIterator.cpp index 30825b6c372040..bc892247ac8986 100644 --- a/src/platform/Linux/bluez/AdapterIterator.cpp +++ b/src/platform/Linux/bluez/AdapterIterator.cpp @@ -21,6 +21,7 @@ #include #include +#include namespace chip { namespace DeviceLayer { @@ -45,23 +46,25 @@ AdapterIterator::~AdapterIterator() } } -void AdapterIterator::Initialize() +CHIP_ERROR AdapterIterator::Initialize(AdapterIterator * self) { // When creating D-Bus proxy object, the thread default context must be initialized. Otherwise, // all D-Bus signals will be delivered to the GLib global default main context. VerifyOrDie(g_main_context_get_thread_default() != nullptr); + CHIP_ERROR err = CHIP_NO_ERROR; GError * error = nullptr; - mManager = g_dbus_object_manager_client_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE, - BLUEZ_INTERFACE, "/", bluez_object_manager_client_get_proxy_type, - nullptr /* unused user data in the Proxy Type Func */, - nullptr /*destroy notify */, nullptr /* cancellable */, &error); + self->mManager = g_dbus_object_manager_client_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE, + BLUEZ_INTERFACE, "/", bluez_object_manager_client_get_proxy_type, + nullptr /* unused user data in the Proxy Type Func */, + nullptr /*destroy notify */, nullptr /* cancellable */, &error); - VerifyOrExit(mManager != nullptr, ChipLogError(DeviceLayer, "Failed to get DBUS object manager for listing adapters.")); + VerifyOrExit(self->mManager != nullptr, ChipLogError(DeviceLayer, "Failed to get DBUS object manager for listing adapters."); + err = CHIP_ERROR_INTERNAL); - mObjectList = g_dbus_object_manager_get_objects(mManager); - mCurrentListItem = mObjectList; + self->mObjectList = g_dbus_object_manager_get_objects(self->mManager); + self->mCurrentListItem = self->mObjectList; exit: if (error != nullptr) @@ -69,6 +72,8 @@ void AdapterIterator::Initialize() ChipLogError(DeviceLayer, "DBus error: %s", error->message); g_error_free(error); } + + return err; } bool AdapterIterator::Advance() @@ -124,7 +129,8 @@ bool AdapterIterator::Next() { if (mManager == nullptr) { - Initialize(); + CHIP_ERROR err = PlatformMgrImpl().GLibMatterContextInvokeSync(Initialize, this); + VerifyOrReturnError(err == CHIP_NO_ERROR, false, ChipLogError(DeviceLayer, "Failed to initialize adapter iterator")); } return Advance(); diff --git a/src/platform/Linux/bluez/AdapterIterator.h b/src/platform/Linux/bluez/AdapterIterator.h index be53995326f099..c06ea8cb97a73f 100644 --- a/src/platform/Linux/bluez/AdapterIterator.h +++ b/src/platform/Linux/bluez/AdapterIterator.h @@ -17,6 +17,8 @@ #pragma once +#include "lib/core/CHIPError.h" + #include "Types.h" #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE @@ -60,7 +62,7 @@ class AdapterIterator private: /// Sets up the DBUS manager and loads the list - void Initialize(); + static CHIP_ERROR Initialize(AdapterIterator * self); /// Loads the next value in the list. ///