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

Initialize BLE iterator on Matter glib context #26540

Merged
merged 1 commit into from
May 12, 2023
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
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