Skip to content

Commit 0a6626f

Browse files
authored
[Linux] Factor common code when connecting BLE device (project-chip#32502)
1 parent dfab05d commit 0a6626f

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

src/platform/Linux/bluez/BluezEndpoint.cpp

+13-32
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ CHIP_ERROR BluezEndpoint::RegisterGattApplicationImpl()
307307
/// Update the table of open BLE connections whenever a new device is spotted or its attributes have changed.
308308
void BluezEndpoint::UpdateConnectionTable(BluezDevice1 * apDevice)
309309
{
310-
const char * objectPath = g_dbus_proxy_get_object_path(G_DBUS_PROXY(apDevice));
310+
const char * objectPath = g_dbus_proxy_get_object_path(reinterpret_cast<GDBusProxy *>(apDevice));
311311
BluezConnection * connection = GetBluezConnection(objectPath);
312312

313313
if (connection != nullptr && !bluez_device1_get_connected(apDevice))
@@ -321,22 +321,9 @@ void BluezEndpoint::UpdateConnectionTable(BluezDevice1 * apDevice)
321321
return;
322322
}
323323

324-
if (connection == nullptr && !bluez_device1_get_connected(apDevice) && mIsCentral)
324+
if (connection == nullptr)
325325
{
326-
return;
327-
}
328-
329-
if (connection == nullptr && bluez_device1_get_connected(apDevice) &&
330-
(!mIsCentral || bluez_device1_get_services_resolved(apDevice)))
331-
{
332-
connection = chip::Platform::New<BluezConnection>(*this, apDevice);
333-
mpPeerDevicePath = g_strdup(objectPath);
334-
mConnMap[mpPeerDevicePath] = connection;
335-
336-
ChipLogDetail(DeviceLayer, "New BLE connection: conn %p, device %s, path %s", connection, connection->GetPeerAddress(),
337-
mpPeerDevicePath);
338-
339-
BLEManagerImpl::HandleNewConnection(connection);
326+
HandleNewDevice(apDevice);
340327
}
341328
}
342329

@@ -355,28 +342,22 @@ void BluezEndpoint::BluezSignalInterfacePropertiesChanged(GDBusObjectManagerClie
355342

356343
void BluezEndpoint::HandleNewDevice(BluezDevice1 * device)
357344
{
358-
VerifyOrReturn(!mIsCentral);
359-
360-
// We need to handle device connection both this function and BluezSignalInterfacePropertiesChanged
361-
// When a device is connected for first time, this function will be triggered.
362-
// The future connections for the same device will trigger ``Connect'' property change.
363-
// TODO: Factor common code in the two function.
364-
BluezConnection * conn;
365-
VerifyOrExit(bluez_device1_get_connected(device), ChipLogError(DeviceLayer, "FAIL: device is not connected"));
345+
VerifyOrReturn(bluez_device1_get_connected(device));
346+
VerifyOrReturn(!mIsCentral || bluez_device1_get_services_resolved(device));
366347

367-
conn = GetBluezConnection(g_dbus_proxy_get_object_path(G_DBUS_PROXY(device)));
368-
VerifyOrExit(conn == nullptr,
369-
ChipLogError(DeviceLayer, "FAIL: connection already tracked: conn: %p new device: %s", conn,
370-
g_dbus_proxy_get_object_path(G_DBUS_PROXY(device))));
348+
const char * objectPath = g_dbus_proxy_get_object_path(reinterpret_cast<GDBusProxy *>(device));
349+
BluezConnection * conn = GetBluezConnection(objectPath);
350+
VerifyOrReturn(conn == nullptr,
351+
ChipLogError(DeviceLayer, "FAIL: Connection already tracked: conn=%p device=%s path=%s", conn,
352+
conn->GetPeerAddress(), objectPath));
371353

372354
conn = chip::Platform::New<BluezConnection>(*this, device);
373-
mpPeerDevicePath = g_strdup(g_dbus_proxy_get_object_path(G_DBUS_PROXY(device)));
355+
mpPeerDevicePath = g_strdup(objectPath);
374356
mConnMap[mpPeerDevicePath] = conn;
375357

376-
ChipLogDetail(DeviceLayer, "BLE device connected: conn %p, device %s, path %s", conn, conn->GetPeerAddress(), mpPeerDevicePath);
358+
ChipLogDetail(DeviceLayer, "New BLE connection: conn=%p device=%s path=%s", conn, conn->GetPeerAddress(), objectPath);
377359

378-
exit:
379-
return;
360+
BLEManagerImpl::HandleNewConnection(conn);
380361
}
381362

382363
void BluezEndpoint::BluezSignalOnObjectAdded(GDBusObjectManager * aManager, GDBusObject * aObject)

0 commit comments

Comments
 (0)