Skip to content

Commit

Permalink
Initialize BLE iterator on Matter glib context (#26540)
Browse files Browse the repository at this point in the history
This fixes Python bindings after changes in 0ada46a.
  • Loading branch information
arkq authored and pull[bot] committed May 25, 2023
1 parent bf332a8 commit 5205141
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/platform/Linux/bluez/AdapterIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/PlatformManager.h>

namespace chip {
namespace DeviceLayer {
Expand All @@ -45,30 +46,34 @@ 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)
{
ChipLogError(DeviceLayer, "DBus error: %s", error->message);
g_error_free(error);
}

return err;
}

bool AdapterIterator::Advance()
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion src/platform/Linux/bluez/AdapterIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#pragma once

#include "lib/core/CHIPError.h"

#include "Types.h"

#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
Expand Down Expand Up @@ -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.
///
Expand Down

0 comments on commit 5205141

Please sign in to comment.