@@ -228,11 +228,6 @@ static gboolean BluezCharacteristicConfirmError(BluezGattCharacteristic1 * aChar
228
228
return TRUE ;
229
229
}
230
230
231
- static gboolean BluezIsDeviceOnAdapter (BluezDevice1 * aDevice, BluezAdapter1 * aAdapter)
232
- {
233
- return strcmp (bluez_device1_get_adapter (aDevice), g_dbus_proxy_get_object_path (G_DBUS_PROXY (aAdapter))) == 0 ? TRUE : FALSE ;
234
- }
235
-
236
231
BluezGattCharacteristic1 * BluezEndpoint::CreateGattCharacteristic (BluezGattService1 * aService, const char * aCharName,
237
232
const char * aUUID, const char * const * aFlags)
238
233
{
@@ -305,12 +300,12 @@ CHIP_ERROR BluezEndpoint::RegisterGattApplicationImpl()
305
300
}
306
301
307
302
// / Update the table of open BLE connections whenever a new device is spotted or its attributes have changed.
308
- void BluezEndpoint::UpdateConnectionTable (BluezDevice1 * apDevice )
303
+ void BluezEndpoint::UpdateConnectionTable (BluezDevice1 & aDevice )
309
304
{
310
- const char * objectPath = g_dbus_proxy_get_object_path (reinterpret_cast <GDBusProxy *>(apDevice ));
305
+ const char * objectPath = g_dbus_proxy_get_object_path (reinterpret_cast <GDBusProxy *>(&aDevice ));
311
306
BluezConnection * connection = GetBluezConnection (objectPath);
312
307
313
- if (connection != nullptr && !bluez_device1_get_connected (apDevice ))
308
+ if (connection != nullptr && !bluez_device1_get_connected (&aDevice ))
314
309
{
315
310
ChipLogDetail (DeviceLayer, " Bluez disconnected" );
316
311
BLEManagerImpl::CHIPoBluez_ConnectionClosed (connection);
@@ -323,35 +318,22 @@ void BluezEndpoint::UpdateConnectionTable(BluezDevice1 * apDevice)
323
318
324
319
if (connection == nullptr )
325
320
{
326
- HandleNewDevice (apDevice );
321
+ HandleNewDevice (aDevice );
327
322
}
328
323
}
329
324
330
- void BluezEndpoint::BluezSignalInterfacePropertiesChanged (GDBusObjectManagerClient * aManager, GDBusObjectProxy * aObject,
331
- GDBusProxy * aInterface, GVariant * aChangedProperties,
332
- const char * const * aInvalidatedProps)
325
+ void BluezEndpoint::HandleNewDevice (BluezDevice1 & aDevice)
333
326
{
334
- VerifyOrReturn (mAdapter , ChipLogError (DeviceLayer, " FAIL: NULL mAdapter in %s" , __func__));
335
- VerifyOrReturn (strcmp (g_dbus_proxy_get_interface_name (aInterface), DEVICE_INTERFACE) == 0 , );
336
-
337
- BluezDevice1 * device = BLUEZ_DEVICE1 (aInterface);
338
- VerifyOrReturn (BluezIsDeviceOnAdapter (device, mAdapter .get ()));
339
-
340
- UpdateConnectionTable (device);
341
- }
327
+ VerifyOrReturn (bluez_device1_get_connected (&aDevice));
328
+ VerifyOrReturn (!mIsCentral || bluez_device1_get_services_resolved (&aDevice));
342
329
343
- void BluezEndpoint::HandleNewDevice (BluezDevice1 * device)
344
- {
345
- VerifyOrReturn (bluez_device1_get_connected (device));
346
- VerifyOrReturn (!mIsCentral || bluez_device1_get_services_resolved (device));
347
-
348
- const char * objectPath = g_dbus_proxy_get_object_path (reinterpret_cast <GDBusProxy *>(device));
330
+ const char * objectPath = g_dbus_proxy_get_object_path (reinterpret_cast <GDBusProxy *>(&aDevice));
349
331
BluezConnection * conn = GetBluezConnection (objectPath);
350
332
VerifyOrReturn (conn == nullptr ,
351
333
ChipLogError (DeviceLayer, " FAIL: Connection already tracked: conn=%p device=%s path=%s" , conn,
352
334
conn->GetPeerAddress (), objectPath));
353
335
354
- conn = chip::Platform::New<BluezConnection>(*this , device );
336
+ conn = chip::Platform::New<BluezConnection>(*this , aDevice );
355
337
mpPeerDevicePath = g_strdup (objectPath);
356
338
mConnMap [mpPeerDevicePath] = conn;
357
339
@@ -360,24 +342,20 @@ void BluezEndpoint::HandleNewDevice(BluezDevice1 * device)
360
342
BLEManagerImpl::HandleNewConnection (conn);
361
343
}
362
344
363
- void BluezEndpoint::BluezSignalOnObjectAdded (GDBusObjectManager * aManager, GDBusObject * aObject )
345
+ void BluezEndpoint::OnDeviceAdded (BluezDevice1 & device )
364
346
{
365
- // TODO: right now we do not handle addition/removal of adapters
366
- // Primary focus here is to handle addition of a device
367
- GAutoPtr<BluezDevice1> device (bluez_object_get_device1 (reinterpret_cast <BluezObject *>(aObject)));
368
- VerifyOrReturn (device);
347
+ HandleNewDevice (device);
348
+ }
369
349
370
- if (BluezIsDeviceOnAdapter (device.get (), mAdapter .get ()) == TRUE )
371
- {
372
- HandleNewDevice (device.get ());
373
- }
350
+ void BluezEndpoint::OnDevicePropertyChanged (BluezDevice1 & device, GVariant * changedProps, const char * const * invalidatedProps)
351
+ {
352
+ UpdateConnectionTable (device);
374
353
}
375
354
376
- void BluezEndpoint::BluezSignalOnObjectRemoved (GDBusObjectManager * aManager, GDBusObject * aObject )
355
+ void BluezEndpoint::OnDeviceRemoved (BluezDevice1 & device )
377
356
{
378
- // TODO: for Device1, lookup connection, and call otPlatTobleHandleDisconnected
379
- // for Adapter1: unclear, crash if this pertains to our adapter? at least null out the self->mAdapter.
380
- // for Characteristic1, or GattService -- handle here via calling otPlatTobleHandleDisconnected, or ignore.
357
+ // Handling device removal is not necessary because disconnection is already handled
358
+ // in the OnDevicePropertyChanged() - we are checking for the "Connected" property.
381
359
}
382
360
383
361
BluezGattService1 * BluezEndpoint::CreateGattService (const char * aUUID)
@@ -545,30 +523,7 @@ void BluezEndpoint::SetupGattServer(GDBusConnection * aConn)
545
523
546
524
CHIP_ERROR BluezEndpoint::SetupEndpointBindings ()
547
525
{
548
- GAutoPtr<GError> err;
549
- GAutoPtr<GDBusConnection> conn (g_bus_get_sync (G_BUS_TYPE_SYSTEM, nullptr , &err.GetReceiver ()));
550
- VerifyOrReturnError (conn != nullptr , CHIP_ERROR_INTERNAL,
551
- ChipLogError (DeviceLayer, " FAIL: get bus sync in %s, error: %s" , __func__, err->message ));
552
-
553
- SetupGattServer (conn.get ());
554
-
555
- g_signal_connect (mObjectManager .GetObjectManager (), " object-added" ,
556
- G_CALLBACK (+[](GDBusObjectManager * aMgr, GDBusObject * aObj, BluezEndpoint * self) {
557
- return self->BluezSignalOnObjectAdded (aMgr, aObj);
558
- }),
559
- this );
560
- g_signal_connect (mObjectManager .GetObjectManager (), " object-removed" ,
561
- G_CALLBACK (+[](GDBusObjectManager * aMgr, GDBusObject * aObj, BluezEndpoint * self) {
562
- return self->BluezSignalOnObjectRemoved (aMgr, aObj);
563
- }),
564
- this );
565
- g_signal_connect (mObjectManager .GetObjectManager (), " interface-proxy-properties-changed" ,
566
- G_CALLBACK (+[](GDBusObjectManagerClient * aMgr, GDBusObjectProxy * aObj, GDBusProxy * aIface,
567
- GVariant * aChangedProps, const char * const * aInvalidatedProps, BluezEndpoint * self) {
568
- return self->BluezSignalInterfacePropertiesChanged (aMgr, aObj, aIface, aChangedProps, aInvalidatedProps);
569
- }),
570
- this );
571
-
526
+ SetupGattServer (mObjectManager .GetConnection ());
572
527
return CHIP_NO_ERROR;
573
528
}
574
529
@@ -586,7 +541,11 @@ CHIP_ERROR BluezEndpoint::Init(BluezAdapter1 * apAdapter, bool aIsCentral)
586
541
mAdapter .reset (reinterpret_cast <BluezAdapter1 *>(g_object_ref (apAdapter)));
587
542
mIsCentral = aIsCentral;
588
543
589
- CHIP_ERROR err = PlatformMgrImpl ().GLibMatterContextInvokeSync (
544
+ CHIP_ERROR err = mObjectManager .SubscribeDeviceNotifications (mAdapter .get (), this );
545
+ VerifyOrReturnError (err == CHIP_NO_ERROR, err,
546
+ ChipLogError (DeviceLayer, " Failed to subscribe for notifications: %" CHIP_ERROR_FORMAT, err.Format ()));
547
+
548
+ err = PlatformMgrImpl ().GLibMatterContextInvokeSync (
590
549
+[](BluezEndpoint * self) { return self->SetupEndpointBindings (); }, this );
591
550
VerifyOrReturnError (err == CHIP_NO_ERROR, err,
592
551
ChipLogError (DeviceLayer, " Failed to schedule endpoint initialization: %" CHIP_ERROR_FORMAT, err.Format ()));
0 commit comments