Skip to content

Commit 7fc24e2

Browse files
jlatusekDamMicSzm
andauthored
[Linux] Convert glib objects to GAutoPtr<> in BlueZ (#32617)
Co-authored-by: Damian Michalak-Szmaciński <d.michalak@samsung.com>
1 parent 997bb3f commit 7fc24e2

7 files changed

+114
-118
lines changed

src/platform/Linux/bluez/BluezAdvertisement.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ CHIP_ERROR BluezAdvertisement::StartImpl()
247247
GVariant * options;
248248

249249
VerifyOrExit(!mIsAdvertising, ChipLogError(DeviceLayer, "FAIL: Advertising has already been enabled in %s", __func__));
250-
VerifyOrExit(mAdapter.get() != nullptr, ChipLogError(DeviceLayer, "FAIL: NULL mAdapter in %s", __func__));
250+
VerifyOrExit(mAdapter, ChipLogError(DeviceLayer, "FAIL: NULL mAdapter in %s", __func__));
251251

252252
adapterObject = g_dbus_interface_get_object(G_DBUS_INTERFACE(mAdapter.get()));
253253
VerifyOrExit(adapterObject != nullptr, ChipLogError(DeviceLayer, "FAIL: NULL adapterObject in %s", __func__));
254254

255255
advMgr.reset(bluez_object_get_leadvertising_manager1(reinterpret_cast<BluezObject *>(adapterObject)));
256-
VerifyOrExit(advMgr.get() != nullptr, ChipLogError(DeviceLayer, "FAIL: NULL advMgr in %s", __func__));
256+
VerifyOrExit(advMgr, ChipLogError(DeviceLayer, "FAIL: NULL advMgr in %s", __func__));
257257

258258
g_variant_builder_init(&optionsBuilder, G_VARIANT_TYPE("a{sv}"));
259259
options = g_variant_builder_end(&optionsBuilder);
@@ -303,13 +303,13 @@ CHIP_ERROR BluezAdvertisement::StopImpl()
303303
GAutoPtr<BluezLEAdvertisingManager1> advMgr;
304304

305305
VerifyOrExit(mIsAdvertising, ChipLogError(DeviceLayer, "FAIL: Advertising has already been disabled in %s", __func__));
306-
VerifyOrExit(mAdapter.get() != nullptr, ChipLogError(DeviceLayer, "FAIL: NULL mAdapter in %s", __func__));
306+
VerifyOrExit(mAdapter, ChipLogError(DeviceLayer, "FAIL: NULL mAdapter in %s", __func__));
307307

308308
adapterObject = g_dbus_interface_get_object(G_DBUS_INTERFACE(mAdapter.get()));
309309
VerifyOrExit(adapterObject != nullptr, ChipLogError(DeviceLayer, "FAIL: NULL adapterObject in %s", __func__));
310310

311311
advMgr.reset(bluez_object_get_leadvertising_manager1(reinterpret_cast<BluezObject *>(adapterObject)));
312-
VerifyOrExit(advMgr.get() != nullptr, ChipLogError(DeviceLayer, "FAIL: NULL advMgr in %s", __func__));
312+
VerifyOrExit(advMgr, ChipLogError(DeviceLayer, "FAIL: NULL advMgr in %s", __func__));
313313

314314
bluez_leadvertising_manager1_call_unregister_advertisement(
315315
advMgr.get(), mAdvPath, nullptr,

src/platform/Linux/bluez/BluezAdvertisement.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@
2929
#include <platform/GLibTypeDeleter.h>
3030
#include <platform/Linux/dbus/bluez/DbusBluez.h>
3131

32+
#include "BluezEndpoint.h"
3233
#include "Types.h"
3334

3435
namespace chip {
3536
namespace DeviceLayer {
3637
namespace Internal {
3738

38-
class BluezEndpoint;
39-
4039
class BluezAdvertisement
4140
{
4241
public:

src/platform/Linux/bluez/BluezConnection.cpp

+29-44
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,11 @@ gboolean BluezIsCharOnService(BluezGattCharacteristic1 * aChar, BluezGattService
6161
} // namespace
6262

6363
BluezConnection::BluezConnection(const BluezEndpoint & aEndpoint, BluezDevice1 * apDevice) :
64-
mpDevice(reinterpret_cast<BluezDevice1 *>(g_object_ref(apDevice)))
64+
mDevice(reinterpret_cast<BluezDevice1 *>(g_object_ref(apDevice)))
6565
{
6666
Init(aEndpoint);
6767
}
6868

69-
BluezConnection::~BluezConnection()
70-
{
71-
g_object_unref(mpDevice);
72-
if (mpService)
73-
g_object_unref(mpService);
74-
if (mpC1)
75-
g_object_unref(mpC1);
76-
if (mpC2)
77-
g_object_unref(mpC2);
78-
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
79-
if (mpC3)
80-
g_object_unref(mpC2);
81-
#endif
82-
}
83-
8469
BluezConnection::IOChannel::~IOChannel()
8570
{
8671
if (mWatchSource != nullptr)
@@ -98,64 +83,64 @@ CHIP_ERROR BluezConnection::Init(const BluezEndpoint & aEndpoint)
9883
{
9984
if (!aEndpoint.mIsCentral)
10085
{
101-
mpService = reinterpret_cast<BluezGattService1 *>(g_object_ref(aEndpoint.mpService));
102-
mpC1 = reinterpret_cast<BluezGattCharacteristic1 *>(g_object_ref(aEndpoint.mpC1));
103-
mpC2 = reinterpret_cast<BluezGattCharacteristic1 *>(g_object_ref(aEndpoint.mpC2));
86+
mService.reset(reinterpret_cast<BluezGattService1 *>(g_object_ref(aEndpoint.mService.get())));
87+
mC1.reset(reinterpret_cast<BluezGattCharacteristic1 *>(g_object_ref(aEndpoint.mC1.get())));
88+
mC2.reset(reinterpret_cast<BluezGattCharacteristic1 *>(g_object_ref(aEndpoint.mC2.get())));
10489
}
10590
else
10691
{
107-
for (BluezObject & object : BluezObjectList(aEndpoint.mpObjMgr))
92+
for (BluezObject & object : BluezObjectList(aEndpoint.mObjMgr.get()))
10893
{
10994
BluezGattService1 * service = bluez_object_get_gatt_service1(&object);
11095
if (service != nullptr)
11196
{
112-
if ((BluezIsServiceOnDevice(service, mpDevice)) == TRUE &&
97+
if ((BluezIsServiceOnDevice(service, mDevice.get())) == TRUE &&
11398
(strcmp(bluez_gatt_service1_get_uuid(service), CHIP_BLE_UUID_SERVICE_STRING) == 0))
11499
{
115-
mpService = service;
100+
mService.reset(service);
116101
break;
117102
}
118103
g_object_unref(service);
119104
}
120105
}
121106

122-
VerifyOrExit(mpService != nullptr, ChipLogError(DeviceLayer, "FAIL: NULL service in %s", __func__));
107+
VerifyOrExit(mService, ChipLogError(DeviceLayer, "FAIL: NULL service in %s", __func__));
123108

124-
for (BluezObject & object : BluezObjectList(aEndpoint.mpObjMgr))
109+
for (BluezObject & object : BluezObjectList(aEndpoint.mObjMgr.get()))
125110
{
126111
BluezGattCharacteristic1 * char1 = bluez_object_get_gatt_characteristic1(&object);
127112
if (char1 != nullptr)
128113
{
129-
if ((BluezIsCharOnService(char1, mpService) == TRUE) &&
114+
if ((BluezIsCharOnService(char1, mService.get()) == TRUE) &&
130115
(strcmp(bluez_gatt_characteristic1_get_uuid(char1), CHIP_PLAT_BLE_UUID_C1_STRING) == 0))
131116
{
132-
mpC1 = char1;
117+
mC1.reset(char1);
133118
}
134-
else if ((BluezIsCharOnService(char1, mpService) == TRUE) &&
119+
else if ((BluezIsCharOnService(char1, mService.get()) == TRUE) &&
135120
(strcmp(bluez_gatt_characteristic1_get_uuid(char1), CHIP_PLAT_BLE_UUID_C2_STRING) == 0))
136121
{
137-
mpC2 = char1;
122+
mC2.reset(char1);
138123
}
139124
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
140-
else if ((BluezIsCharOnService(char1, mpService) == TRUE) &&
125+
else if ((BluezIsCharOnService(char1, mService.get()) == TRUE) &&
141126
(strcmp(bluez_gatt_characteristic1_get_uuid(char1), CHIP_PLAT_BLE_UUID_C3_STRING) == 0))
142127
{
143-
mpC3 = char1;
128+
mC3.reset(char1);
144129
}
145130
#endif
146131
else
147132
{
148133
g_object_unref(char1);
149134
}
150-
if ((mpC1 != nullptr) && (mpC2 != nullptr))
135+
if (mC1 && mC2)
151136
{
152137
break;
153138
}
154139
}
155140
}
156141

157-
VerifyOrExit(mpC1 != nullptr, ChipLogError(DeviceLayer, "FAIL: NULL C1 in %s", __func__));
158-
VerifyOrExit(mpC2 != nullptr, ChipLogError(DeviceLayer, "FAIL: NULL C2 in %s", __func__));
142+
VerifyOrExit(mC1, ChipLogError(DeviceLayer, "FAIL: NULL C1 in %s", __func__));
143+
VerifyOrExit(mC2, ChipLogError(DeviceLayer, "FAIL: NULL C2 in %s", __func__));
159144
}
160145

161146
exit:
@@ -169,7 +154,7 @@ CHIP_ERROR BluezConnection::BluezDisconnect(BluezConnection * conn)
169154

170155
ChipLogDetail(DeviceLayer, "%s peer=%s", __func__, conn->GetPeerAddress());
171156

172-
success = bluez_device1_call_disconnect_sync(conn->mpDevice, nullptr, &error.GetReceiver());
157+
success = bluez_device1_call_disconnect_sync(conn->mDevice.get(), nullptr, &error.GetReceiver());
173158
VerifyOrExit(success == TRUE, ChipLogError(DeviceLayer, "FAIL: Disconnect: %s", error->message));
174159

175160
exit:
@@ -183,7 +168,7 @@ CHIP_ERROR BluezConnection::CloseConnection()
183168

184169
const char * BluezConnection::GetPeerAddress() const
185170
{
186-
return bluez_device1_get_address(mpDevice);
171+
return bluez_device1_get_address(mDevice.get());
187172
}
188173

189174
gboolean BluezConnection::WriteHandlerCallback(GIOChannel * aChannel, GIOCondition aCond, BluezConnection * apConn)
@@ -205,7 +190,7 @@ gboolean BluezConnection::WriteHandlerCallback(GIOChannel * aChannel, GIOConditi
205190
// Casting len to size_t is safe, since we ensured that it's not negative.
206191
newVal = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, buf, static_cast<size_t>(len), sizeof(uint8_t));
207192

208-
bluez_gatt_characteristic1_set_value(apConn->mpC1, newVal);
193+
bluez_gatt_characteristic1_set_value(apConn->mC1.get(), newVal);
209194
BLEManagerImpl::HandleRXCharWrite(apConn, buf, static_cast<size_t>(len));
210195
isSuccess = true;
211196

@@ -267,7 +252,7 @@ CHIP_ERROR BluezConnection::SendIndicationImpl(ConnectionDataBundle * data)
267252
GAutoPtr<GError> error;
268253
size_t len, written;
269254

270-
if (bluez_gatt_characteristic1_get_notify_acquired(data->mConn.mpC2) == TRUE)
255+
if (bluez_gatt_characteristic1_get_notify_acquired(data->mConn.mC2.get()) == TRUE)
271256
{
272257
auto * buf = static_cast<const char *>(g_variant_get_fixed_array(data->mData.get(), &len, sizeof(uint8_t)));
273258
VerifyOrExit(len <= static_cast<size_t>(std::numeric_limits<gssize>::max()),
@@ -278,7 +263,7 @@ CHIP_ERROR BluezConnection::SendIndicationImpl(ConnectionDataBundle * data)
278263
}
279264
else
280265
{
281-
bluez_gatt_characteristic1_set_value(data->mConn.mpC2, data->mData.release());
266+
bluez_gatt_characteristic1_set_value(data->mConn.mC2.get(), data->mData.release());
282267
}
283268

284269
exit:
@@ -288,7 +273,7 @@ CHIP_ERROR BluezConnection::SendIndicationImpl(ConnectionDataBundle * data)
288273
CHIP_ERROR BluezConnection::SendIndication(chip::System::PacketBufferHandle apBuf)
289274
{
290275
VerifyOrReturnError(!apBuf.IsNull(), CHIP_ERROR_INVALID_ARGUMENT, ChipLogError(DeviceLayer, "apBuf is NULL in %s", __func__));
291-
VerifyOrReturnError(mpC2 != nullptr, CHIP_ERROR_INTERNAL, ChipLogError(DeviceLayer, "C2 is NULL in %s", __func__));
276+
VerifyOrReturnError(mC2, CHIP_ERROR_INTERNAL, ChipLogError(DeviceLayer, "C2 is NULL in %s", __func__));
292277

293278
ConnectionDataBundle bundle(*this, apBuf);
294279
return PlatformMgrImpl().GLibMatterContextInvokeSync(SendIndicationImpl, &bundle);
@@ -315,16 +300,16 @@ CHIP_ERROR BluezConnection::SendWriteRequestImpl(ConnectionDataBundle * data)
315300
g_variant_builder_add(&optionsBuilder, "{sv}", "type", g_variant_new_string("request"));
316301
auto options = g_variant_builder_end(&optionsBuilder);
317302

318-
bluez_gatt_characteristic1_call_write_value(data->mConn.mpC1, data->mData.release(), options, nullptr, SendWriteRequestDone,
319-
const_cast<BluezConnection *>(&data->mConn));
303+
bluez_gatt_characteristic1_call_write_value(data->mConn.mC1.get(), data->mData.release(), options, nullptr,
304+
SendWriteRequestDone, const_cast<BluezConnection *>(&data->mConn));
320305

321306
return CHIP_NO_ERROR;
322307
}
323308

324309
CHIP_ERROR BluezConnection::SendWriteRequest(chip::System::PacketBufferHandle apBuf)
325310
{
326311
VerifyOrReturnError(!apBuf.IsNull(), CHIP_ERROR_INVALID_ARGUMENT, ChipLogError(DeviceLayer, "apBuf is NULL in %s", __func__));
327-
VerifyOrReturnError(mpC1 != nullptr, CHIP_ERROR_INTERNAL, ChipLogError(DeviceLayer, "C1 is NULL in %s", __func__));
312+
VerifyOrReturnError(mC1, CHIP_ERROR_INTERNAL, ChipLogError(DeviceLayer, "C1 is NULL in %s", __func__));
328313

329314
ConnectionDataBundle bundle(*this, apBuf);
330315
return PlatformMgrImpl().GLibMatterContextInvokeSync(SendWriteRequestImpl, &bundle);
@@ -360,7 +345,7 @@ void BluezConnection::SubscribeCharacteristicDone(GObject * aObject, GAsyncResul
360345

361346
CHIP_ERROR BluezConnection::SubscribeCharacteristicImpl(BluezConnection * connection)
362347
{
363-
BluezGattCharacteristic1 * pC2 = connection->mpC2;
348+
BluezGattCharacteristic1 * pC2 = connection->mC2.get();
364349
VerifyOrExit(pC2 != nullptr, ChipLogError(DeviceLayer, "C2 is NULL in %s", __func__));
365350

366351
// Get notifications on the TX characteristic change (e.g. indication is received)
@@ -394,7 +379,7 @@ void BluezConnection::UnsubscribeCharacteristicDone(GObject * aObject, GAsyncRes
394379

395380
CHIP_ERROR BluezConnection::UnsubscribeCharacteristicImpl(BluezConnection * connection)
396381
{
397-
BluezGattCharacteristic1 * pC2 = connection->mpC2;
382+
BluezGattCharacteristic1 * pC2 = connection->mC2.get();
398383
VerifyOrExit(pC2 != nullptr, ChipLogError(DeviceLayer, "C2 is NULL in %s", __func__));
399384

400385
bluez_gatt_characteristic1_call_stop_notify(pC2, nullptr, UnsubscribeCharacteristicDone, connection);

src/platform/Linux/bluez/BluezConnection.h

+11-10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <platform/Linux/dbus/bluez/DbusBluez.h>
2929
#include <system/SystemPacketBuffer.h>
3030

31+
#include "Types.h"
32+
3133
namespace chip {
3234
namespace DeviceLayer {
3335
namespace Internal {
@@ -38,7 +40,7 @@ class BluezConnection
3840
{
3941
public:
4042
BluezConnection(const BluezEndpoint & aEndpoint, BluezDevice1 * apDevice);
41-
~BluezConnection();
43+
~BluezConnection() = default;
4244

4345
const char * GetPeerAddress() const;
4446

@@ -115,20 +117,19 @@ class BluezConnection
115117
static void UnsubscribeCharacteristicDone(GObject * aObject, GAsyncResult * aResult, gpointer apConn);
116118
static CHIP_ERROR UnsubscribeCharacteristicImpl(BluezConnection * apConn);
117119

118-
BluezDevice1 * mpDevice;
120+
GAutoPtr<BluezDevice1> mDevice;
121+
GAutoPtr<BluezGattService1> mService;
119122

120123
bool mNotifyAcquired = false;
121124
uint16_t mMtu = 0;
122125

123-
BluezGattService1 * mpService = nullptr;
124-
125-
BluezGattCharacteristic1 * mpC1 = nullptr;
126-
IOChannel mC1Channel = {};
127-
BluezGattCharacteristic1 * mpC2 = nullptr;
128-
IOChannel mC2Channel = {};
126+
GAutoPtr<BluezGattCharacteristic1> mC1;
127+
IOChannel mC1Channel = {};
128+
GAutoPtr<BluezGattCharacteristic1> mC2;
129+
IOChannel mC2Channel = {};
129130
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
130-
BluezGattCharacteristic1 * mpC3 = nullptr;
131-
IOChannel mC3Channel = {};
131+
GAutoPtr<BluezGattCharacteristic1> mC3;
132+
IOChannel mC3Channel = {};
132133
#endif
133134
};
134135

0 commit comments

Comments
 (0)