Skip to content

Commit

Permalink
Convert possible bluez object to GAutoPtr<> (#29987)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamMicSzm authored Oct 25, 2023
1 parent 0a0d909 commit 0876947
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
15 changes: 6 additions & 9 deletions src/platform/Linux/bluez/BluezEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
#include <system/SystemPacketBuffer.h>

#include "BluezConnection.h"
#include "Types.h"

namespace chip {
namespace DeviceLayer {
Expand Down Expand Up @@ -691,18 +692,14 @@ static void BluezSignalOnObjectAdded(GDBusObjectManager * aManager, GDBusObject
{
// TODO: right now we do not handle addition/removal of adapters
// Primary focus here is to handle addition of a device
BluezDevice1 * device = bluez_object_get_device1(BLUEZ_OBJECT(aObject));
if (device == nullptr)
{
return;
}
GAutoPtr<BluezDevice1> device(bluez_object_get_device1(BLUEZ_OBJECT(aObject)));

if (BluezIsDeviceOnAdapter(device, endpoint->mpAdapter) == TRUE)
VerifyOrReturn(device.get() != nullptr);

if (BluezIsDeviceOnAdapter(device.get(), endpoint->mpAdapter) == TRUE)
{
BluezHandleNewDevice(device, endpoint);
BluezHandleNewDevice(device.get(), endpoint);
}

g_object_unref(device);
}

static void BluezSignalOnObjectRemoved(GDBusObjectManager * aManager, GDBusObject * aObject, gpointer apClosure)
Expand Down
23 changes: 9 additions & 14 deletions src/platform/Linux/bluez/ChipDeviceScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,24 +223,20 @@ CHIP_ERROR ChipDeviceScanner::MainLoopStopScan(ChipDeviceScanner * self)

void ChipDeviceScanner::SignalObjectAdded(GDBusObjectManager * manager, GDBusObject * object, ChipDeviceScanner * self)
{
BluezDevice1 * device = bluez_object_get_device1(BLUEZ_OBJECT(object));
VerifyOrReturn(device != nullptr);
GAutoPtr<BluezDevice1> device(bluez_object_get_device1(BLUEZ_OBJECT(object)));
VerifyOrReturn(device.get() != nullptr);

self->ReportDevice(*device);

g_object_unref(device);
self->ReportDevice(*device.get());
}

void ChipDeviceScanner::SignalInterfaceChanged(GDBusObjectManagerClient * manager, GDBusObjectProxy * object,
GDBusProxy * aInterface, GVariant * aChangedProperties,
const gchar * const * aInvalidatedProps, ChipDeviceScanner * self)
{
BluezDevice1 * device = bluez_object_get_device1(BLUEZ_OBJECT(object));
VerifyOrReturn(device != nullptr);

self->ReportDevice(*device);
GAutoPtr<BluezDevice1> device(bluez_object_get_device1(BLUEZ_OBJECT(object)));
VerifyOrReturn(device.get() != nullptr);

g_object_unref(device);
self->ReportDevice(*device.get());
}

void ChipDeviceScanner::ReportDevice(BluezDevice1 & device)
Expand Down Expand Up @@ -295,11 +291,10 @@ CHIP_ERROR ChipDeviceScanner::MainLoopStartScan(ChipDeviceScanner * self)
ChipLogProgress(Ble, "BLE removing known devices.");
for (BluezObject & object : BluezObjectList(self->mManager))
{
BluezDevice1 * device = bluez_object_get_device1(&object);
if (device != nullptr)
GAutoPtr<BluezDevice1> device(bluez_object_get_device1(&object));
if (device.get() != nullptr)
{
self->RemoveDevice(*device);
g_object_unref(device);
self->RemoveDevice(*device.get());
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/platform/Linux/bluez/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,20 @@
#pragma once

#include <platform/CHIPDeviceConfig.h>
#include <platform/GLibTypeDeleter.h>

#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE

#include <platform/Linux/dbus/bluez/DbusBluez.h>

namespace chip {

template <>
struct GAutoPtrDeleter<BluezDevice1>
{
using deleter = GObjectDeleter;
};

namespace DeviceLayer {
namespace Internal {

Expand Down

0 comments on commit 0876947

Please sign in to comment.