forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBluezObjectManager.cpp
323 lines (284 loc) · 12.4 KB
/
BluezObjectManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
*
* Copyright (c) 2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "BluezObjectManager.h"
#include <algorithm>
#include <memory>
#include <string>
#include <string_view>
#include <gio/gio.h>
#include <glib-object.h>
#include <glib.h>
#include <ble/Ble.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/ConfigurationManager.h>
#include <platform/GLibTypeDeleter.h>
#include <platform/Linux/dbus/bluez/DBusBluez.h>
#include <platform/PlatformManager.h>
#include "Types.h"
namespace chip {
namespace DeviceLayer {
namespace Internal {
namespace {
const char * GetAdapterObjectPath(BluezAdapter1 * aAdapter)
{
return g_dbus_proxy_get_object_path(reinterpret_cast<GDBusProxy *>(aAdapter));
}
} // namespace
CHIP_ERROR BluezObjectManager::Init()
{
return PlatformMgrImpl().GLibMatterContextInvokeSync(
+[](BluezObjectManager * self) {
ReturnErrorOnFailure(self->SetupDBusConnection());
ReturnErrorOnFailure(self->SetupObjectManager());
return CHIP_NO_ERROR;
},
this);
}
void BluezObjectManager::Shutdown()
{
// If the D-Bus connection or the object manager are not initialized,
// there is nothing to shutdown. This check prevents unnecessary call
// to the GLibMatterContextInvokeSync function.
VerifyOrReturn(mConnection || mObjectManager);
// Run endpoint cleanup on the CHIPoBluez thread. This is necessary because the
// cleanup function releases the D-Bus manager client object, which handles D-Bus
// signals. Otherwise, we will face race condition when the D-Bus signal is in
// the middle of being processed when the cleanup function is called.
PlatformMgrImpl().GLibMatterContextInvokeSync(
+[](BluezObjectManager * self) {
self->mConnection.reset();
self->mObjectManager.reset();
return CHIP_NO_ERROR;
},
this);
}
BluezAdapter1 * BluezObjectManager::GetAdapter(unsigned int aAdapterId)
{
char expectedPath[32];
snprintf(expectedPath, sizeof(expectedPath), BLUEZ_PATH "/hci%u", aAdapterId);
for (BluezObject & object : GetObjects())
{
GAutoPtr<BluezAdapter1> adapter(bluez_object_get_adapter1(&object));
if (adapter && strcmp(g_dbus_proxy_get_object_path(reinterpret_cast<GDBusProxy *>(adapter.get())), expectedPath) == 0)
{
SetupAdapter(adapter.get());
return adapter.release();
}
}
return nullptr;
}
BluezAdapter1 * BluezObjectManager::GetAdapter(const char * aAdapterAddress)
{
for (BluezObject & object : GetObjects())
{
GAutoPtr<BluezAdapter1> adapter(bluez_object_get_adapter1(&object));
if (adapter && strcmp(bluez_adapter1_get_address(adapter.get()), aAdapterAddress) == 0)
{
SetupAdapter(adapter.get());
return adapter.release();
}
}
return nullptr;
}
CHIP_ERROR BluezObjectManager::SubscribeDeviceNotifications(BluezAdapter1 * aAdapter,
BluezObjectManagerAdapterNotificationsDelegate * aDelegate)
{
std::lock_guard<std::mutex> lock(mSubscriptionsMutex);
mSubscriptions.emplace_back(GetAdapterObjectPath(aAdapter), aDelegate);
return CHIP_NO_ERROR;
}
CHIP_ERROR BluezObjectManager::UnsubscribeDeviceNotifications(BluezAdapter1 * aAdapter,
BluezObjectManagerAdapterNotificationsDelegate * aDelegate)
{
std::lock_guard<std::mutex> lock(mSubscriptionsMutex);
const auto item = std::make_pair(std::string(GetAdapterObjectPath(aAdapter)), aDelegate);
mSubscriptions.erase(std::remove(mSubscriptions.begin(), mSubscriptions.end(), item), mSubscriptions.end());
return CHIP_NO_ERROR;
}
CHIP_ERROR BluezObjectManager::SetupAdapter(BluezAdapter1 * aAdapter)
{
// Make sure the adapter is powered on.
bluez_adapter1_set_powered(aAdapter, TRUE);
// Setting "Discoverable" to False on the adapter and to True on the advertisement convinces
// BlueZ to set "BR/EDR Not Supported" flag. BlueZ doesn't provide API to do that explicitly
// and the flag is necessary to force using LE transport.
bluez_adapter1_set_discoverable(aAdapter, FALSE);
return CHIP_NO_ERROR;
}
void BluezObjectManager::NotifyAdapterAdded(BluezAdapter1 * aAdapter)
{
unsigned int adapterId = 0;
sscanf(GetAdapterObjectPath(aAdapter), BLUEZ_PATH "/hci%u", &adapterId);
// Notify the application that new adapter has been just added
BLEManagerImpl::NotifyBLEAdapterAdded(adapterId, bluez_adapter1_get_address(aAdapter));
}
void BluezObjectManager::NotifyAdapterRemoved(BluezAdapter1 * aAdapter)
{
unsigned int adapterId = 0;
sscanf(GetAdapterObjectPath(aAdapter), BLUEZ_PATH "/hci%u", &adapterId);
// Notify the application that the adapter is no longer available
BLEManagerImpl::NotifyBLEAdapterRemoved(adapterId, bluez_adapter1_get_address(aAdapter));
}
void BluezObjectManager::RemoveAdapterSubscriptions(BluezAdapter1 * aAdapter)
{
std::lock_guard<std::mutex> lock(mSubscriptionsMutex);
const auto adapterPath = GetAdapterObjectPath(aAdapter);
// Remove all device notification subscriptions for the given adapter
mSubscriptions.erase(std::remove_if(mSubscriptions.begin(), mSubscriptions.end(),
[adapterPath](const auto & subscription) { return subscription.first == adapterPath; }),
mSubscriptions.end());
}
CHIP_ERROR BluezObjectManager::SetupDBusConnection()
{
GAutoPtr<GError> err;
mConnection.reset(g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &err.GetReceiver()));
VerifyOrReturnError(mConnection != nullptr, CHIP_ERROR_INTERNAL,
ChipLogError(DeviceLayer, "FAIL: Get D-Bus system bus: %s", err->message));
return CHIP_NO_ERROR;
}
BluezObjectManager::NotificationsDelegates BluezObjectManager::GetDeviceNotificationsDelegates(BluezDevice1 * device)
{
const char * deviceAdapterPath = bluez_device1_get_adapter(device);
NotificationsDelegates delegates;
std::lock_guard<std::mutex> lock(mSubscriptionsMutex);
for (auto & [adapterPath, delegate] : mSubscriptions)
{
if (adapterPath == deviceAdapterPath)
{
delegates.push_back(delegate);
}
}
return delegates;
}
void BluezObjectManager::OnObjectAdded(GDBusObjectManager * aMgr, BluezObject * aObj)
{
GAutoPtr<BluezAdapter1> adapter(bluez_object_get_adapter1(aObj));
// Verify that the adapter is properly initialized - the class property must be set.
// BlueZ can export adapter objects on the bus before it is fully initialized. Such
// adapter objects are not usable and must be ignored.
//
// TODO: Find a better way to determine whether the adapter interface exposed by
// BlueZ D-Bus service is fully functional. The current approach is based on
// the assumption that the class property is non-zero, which is true only
// for BR/EDR + LE adapters. LE-only adapters do not have HCI command to read
// the class property and BlueZ sets it to 0 as a default value.
if (adapter && bluez_adapter1_get_class(adapter.get()) != 0)
{
NotifyAdapterAdded(adapter.get());
return;
}
GAutoPtr<BluezDevice1> device(bluez_object_get_device1(aObj));
if (device)
{
for (auto delegate : GetDeviceNotificationsDelegates(device.get()))
{
delegate->OnDeviceAdded(*device.get());
}
}
}
void BluezObjectManager::OnObjectRemoved(GDBusObjectManager * aMgr, BluezObject * aObj)
{
GAutoPtr<BluezAdapter1> adapter(bluez_object_get_adapter1(aObj));
if (adapter)
{
RemoveAdapterSubscriptions(adapter.get());
NotifyAdapterRemoved(adapter.get());
return;
}
GAutoPtr<BluezDevice1> device(bluez_object_get_device1(aObj));
if (device)
{
for (auto delegate : GetDeviceNotificationsDelegates(device.get()))
{
delegate->OnDeviceRemoved(*device.get());
}
}
}
void BluezObjectManager::OnInterfacePropertiesChanged(GDBusObjectManagerClient * aMgr, BluezObject * aObj, GDBusProxy * aIface,
GVariant * aChangedProps, const char * const * aInvalidatedProps)
{
uint32_t classValue = 0;
GAutoPtr<BluezAdapter1> adapter(bluez_object_get_adapter1(aObj));
// When the adapter's readonly class property is set, it means that the adapter has been
// fully initialized and is ready to be used. It's most likely that the adapter has been
// previously ignored in the OnObjectAdded callback, so now we can notify the application
// about the new adapter.
if (adapter && g_variant_lookup(aChangedProps, "Class", "u", &classValue) && classValue != 0)
{
NotifyAdapterAdded(adapter.get());
return;
}
GAutoPtr<BluezDevice1> device(bluez_object_get_device1(aObj));
if (device)
{
for (auto delegate : GetDeviceNotificationsDelegates(device.get()))
{
delegate->OnDevicePropertyChanged(*device.get(), aChangedProps, aInvalidatedProps);
}
}
}
CHIP_ERROR BluezObjectManager::SetupObjectManager()
{
// When connecting to signals, the thread default context must be initialized. Otherwise,
// all D-Bus signals will be delivered to the GLib global default main context.
VerifyOrDie(g_main_context_get_thread_default() != nullptr);
GAutoPtr<GError> err;
mObjectManager.reset(g_dbus_object_manager_client_new_sync(
mConnection.get(), G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE, BLUEZ_INTERFACE, "/",
bluez_object_manager_client_get_proxy_type, nullptr /* unused user data in the proxy type func */,
nullptr /* destroy notify */, nullptr /* cancellable */, &err.GetReceiver()));
VerifyOrReturnError(mObjectManager, CHIP_ERROR_INTERNAL,
ChipLogError(DeviceLayer, "FAIL: Get D-Bus object manager client: %s", err->message));
g_signal_connect(mObjectManager.get(), "object-added",
G_CALLBACK(+[](GDBusObjectManager * mgr, GDBusObject * obj, BluezObjectManager * self) {
return self->OnObjectAdded(mgr, reinterpret_cast<BluezObject *>(obj));
}),
this);
g_signal_connect(mObjectManager.get(), "object-removed",
G_CALLBACK(+[](GDBusObjectManager * mgr, GDBusObject * obj, BluezObjectManager * self) {
return self->OnObjectRemoved(mgr, reinterpret_cast<BluezObject *>(obj));
}),
this);
g_signal_connect(mObjectManager.get(), "interface-proxy-properties-changed",
G_CALLBACK(+[](GDBusObjectManagerClient * mgr, GDBusObjectProxy * obj, GDBusProxy * iface, GVariant * changed,
const char * const * invalidated, BluezObjectManager * self) {
return self->OnInterfacePropertiesChanged(mgr, reinterpret_cast<BluezObject *>(obj), iface, changed,
invalidated);
}),
this);
return CHIP_NO_ERROR;
}
CHIP_ERROR BluezCallToChipError(const GError * aError)
{
switch (aError->code)
{
// BlueZ crashed or the D-Bus connection is broken in both cases adapter is not available.
case G_DBUS_ERROR_NO_REPLY:
// BlueZ service is not available on the bus, hence the adapter is not available too.
case G_DBUS_ERROR_SERVICE_UNKNOWN:
// Requested D-Bus object is not available on the given path. This happens when the adapter
// was unplugged and unregistered from the BlueZ object manager.
case G_DBUS_ERROR_UNKNOWN_OBJECT:
return BLE_ERROR_ADAPTER_UNAVAILABLE;
default:
return CHIP_ERROR_INTERNAL;
}
}
} // namespace Internal
} // namespace DeviceLayer
} // namespace chip