diff --git a/src/platform/Linux/bluez/BluezEndpoint.cpp b/src/platform/Linux/bluez/BluezEndpoint.cpp index b4f519ced3ce9d..8fbfd81be4308a 100644 --- a/src/platform/Linux/bluez/BluezEndpoint.cpp +++ b/src/platform/Linux/bluez/BluezEndpoint.cpp @@ -71,6 +71,7 @@ #include #include "BluezConnection.h" +#include "Types.h" namespace chip { namespace DeviceLayer { @@ -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 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) diff --git a/src/platform/Linux/bluez/ChipDeviceScanner.cpp b/src/platform/Linux/bluez/ChipDeviceScanner.cpp index 4e1dba335779dc..050df86dabc9ef 100644 --- a/src/platform/Linux/bluez/ChipDeviceScanner.cpp +++ b/src/platform/Linux/bluez/ChipDeviceScanner.cpp @@ -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 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 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) @@ -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 device(bluez_object_get_device1(&object)); + if (device.get() != nullptr) { - self->RemoveDevice(*device); - g_object_unref(device); + self->RemoveDevice(*device.get()); } } diff --git a/src/platform/Linux/bluez/Types.h b/src/platform/Linux/bluez/Types.h index d96d4b1524febb..0153492b5b0e32 100644 --- a/src/platform/Linux/bluez/Types.h +++ b/src/platform/Linux/bluez/Types.h @@ -46,10 +46,20 @@ #pragma once #include +#include #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE +#include + namespace chip { + +template <> +struct GAutoPtrDeleter +{ + using deleter = GObjectDeleter; +}; + namespace DeviceLayer { namespace Internal {