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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/*
* Author: Petre Eftime <petre.p.eftime@intel.com>
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "orgbluez-dbus.h"
#include "BluetoothManager.hpp"
#include "BluetoothAdapter.hpp"
#include "BluetoothDevice.hpp"
#include "BluetoothGattService.hpp"
#include "BluetoothGattCharacteristic.hpp"
#include "BluetoothGattDescriptor.hpp"
#include "BluetoothEvent.hpp"
#include "BluetoothException.hpp"
#include "version.h"
#include <cassert>
using namespace tinyb;
class tinyb::BluetoothEventManager {
public:
static void on_interface_added (GDBusObject *object,
GDBusInterface *interface, gpointer user_data) {
(void) object;
(void) user_data;
GDBusInterfaceInfo *info = g_dbus_interface_get_info(interface);
BluetoothType type = BluetoothType::NONE;
BluetoothManager *manager = BluetoothManager::get_bluetooth_manager();
/* Unknown interface, ignore */
if (info == NULL)
return;
if(IS_GATT_SERVICE1_PROXY(interface)) {
type = BluetoothType::GATT_SERVICE;
auto obj = new BluetoothGattService(GATT_SERVICE1(interface));
auto uuid = obj->get_uuid();
auto parent = obj->get_device();
manager->handle_event(type, nullptr, &uuid, &parent, *obj);
}
else if(IS_GATT_CHARACTERISTIC1_PROXY(interface)) {
type = BluetoothType::GATT_CHARACTERISTIC;
auto obj = new BluetoothGattCharacteristic(GATT_CHARACTERISTIC1(interface));
auto uuid = obj->get_uuid();
auto parent = obj->get_service();
manager->handle_event(type, nullptr, &uuid, &parent, *obj);
}
else if(IS_GATT_DESCRIPTOR1_PROXY(interface)) {
type = BluetoothType::GATT_DESCRIPTOR;
auto obj = new BluetoothGattDescriptor(GATT_DESCRIPTOR1(interface));
auto uuid = obj->get_uuid();
auto parent = obj->get_characteristic();
manager->handle_event(type, nullptr, &uuid, &parent, *obj);
}
else if(IS_DEVICE1_PROXY(interface)) {
type = BluetoothType::DEVICE;
auto obj = new BluetoothDevice(DEVICE1(interface));
auto name = obj->get_name();
auto uuid = obj->get_address();
auto parent = obj->get_adapter();
manager->handle_event(type, &name, &uuid, &parent, *obj);
}
else if(IS_ADAPTER1_PROXY(interface)) {
type = BluetoothType::ADAPTER;
auto obj = new BluetoothAdapter(ADAPTER1(interface));
auto name = obj->get_name();
auto uuid = obj->get_address();
manager->handle_event(type, &name, &uuid, nullptr, *obj);
}
}
static void on_object_added (GDBusObjectManager *manager,
GDBusObject *object, gpointer user_data) {
(void) manager;
GList *l, *interfaces = g_dbus_object_get_interfaces(object);
for(l = interfaces; l != NULL; l = l->next)
on_interface_added(object, (GDBusInterface *)l->data, user_data);
g_list_free_full(interfaces, g_object_unref);
}
};
GDBusObjectManager *gdbus_manager = NULL;
GMainContext *manager_context = NULL;
GThread *manager_thread = NULL;
std::string BluetoothManager::get_class_name() const
{
return std::string("BluetoothManager");
}
std::string BluetoothManager::get_java_class() const
{
return std::string(JAVA_DBUS_PACKAGE "/DBusManager");
}
std::string BluetoothManager::get_object_path() const
{
return std::string("/");
}
BluetoothType BluetoothManager::get_bluetooth_type() const
{
return BluetoothType::NONE;
}
std::string BluetoothManager::get_api_version() {
return std::string(gVERSION_API);
}
std::string BluetoothManager::get_library_version() {
return std::string(gVERSION_SHORT);
}
std::unique_ptr<BluetoothObject> BluetoothManager::get_object(
BluetoothType type, std::string *name, std::string *identifier,
BluetoothObject *parent)
{
auto list = get_objects(type, name, identifier, parent);
if (list.empty())
return std::unique_ptr<BluetoothObject>();
return std::move(list.front());
}
std::vector<std::unique_ptr<BluetoothObject>> BluetoothManager::get_objects(
BluetoothType type, std::string *name, std::string *identifier,
BluetoothObject *parent)
{
std::vector<std::unique_ptr<BluetoothObject>> vector;
GList *l, *objects = g_dbus_object_manager_get_objects(gdbus_manager);
for (l = objects; l != NULL; l = l->next) {
Object *object = OBJECT(l->data);
auto p_service = BluetoothGattService::make(object, type, name, identifier, parent);
if (p_service != nullptr)
vector.push_back(std::move(p_service));
auto p_characteristic = BluetoothGattCharacteristic::make(object, type, name, identifier, parent);
if (p_characteristic != nullptr)
vector.push_back(std::move(p_characteristic));
auto p_descriptor = BluetoothGattDescriptor::make(object, type, name, identifier, parent);
if (p_descriptor != nullptr)
vector.push_back(std::move(p_descriptor));
auto p_device = BluetoothDevice::make(object, type, name, identifier, parent);
if (p_device != nullptr)
vector.push_back(std::move(p_device));
auto p_adapter = BluetoothAdapter::make(object, type, name, identifier, parent);
if (p_adapter != nullptr)
vector.push_back(std::move(p_adapter));
}
g_list_free_full(objects, g_object_unref);
return vector;
}
std::unique_ptr<BluetoothObject> BluetoothManager::find(BluetoothType type,
std::string *name, std::string* identifier, BluetoothObject *parent,
std::chrono::milliseconds timeout)
{
std::shared_ptr<BluetoothEvent> event(new BluetoothEvent(type, name,
identifier, parent));
add_event(event);
auto object = get_object(type, name, identifier, parent);
if (object == nullptr) {
event->wait(timeout);
object = std::unique_ptr<BluetoothObject>(event->get_result());
}
event->cancel();
return object;
}
std::weak_ptr<BluetoothEvent> BluetoothManager::find(BluetoothType type,
std::string *name, std::string* identifier, BluetoothObject *parent,
BluetoothCallback cb, bool execute_once,
std::chrono::milliseconds timeout)
{
(void)cb;
(void)execute_once;
(void)timeout;
std::shared_ptr<BluetoothEvent> event(new BluetoothEvent(type, name,
identifier, parent));
add_event(event);
return std::weak_ptr<BluetoothEvent>(event);
}
void BluetoothManager::handle_event(BluetoothType type, std::string *name,
std::string *identifier, BluetoothObject *parent, BluetoothObject &object)
{
for (auto it = event_list.begin();
it != event_list.end();) {
if ((*it)->get_type() != BluetoothType::NONE && ((*it)->get_type()) != type) {
++it;
continue; /* this event does not match */
}
if ((*it)->get_name() != NULL)
if (name == NULL || *((*it)->get_name()) != *name) {
++it;
continue; /* this event does not match */
}
if ((*it)->get_identifier() != NULL)
if (identifier == NULL || *((*it)->get_identifier()) != *identifier) {
++it;
continue; /* this event does not match */
}
if ((*it)->get_parent() != NULL)
if (parent == NULL || *((*it)->get_parent()) != *parent) {
++it;
continue; /* this event does not match */
}
/* The event matches, execute and see if it needs to reexecute */
if ((*it)->execute_callback(object))
it = event_list.erase(it);
else
++it;
}
}
static gpointer init_manager_thread(void *data)
{
GMainLoop *loop;
GDBusObjectManager *_gdbus_manager = (GDBusObjectManager *) data;
g_main_context_push_thread_default(manager_context);
loop = g_main_loop_new(manager_context, FALSE);
g_signal_connect(_gdbus_manager,
"interface-added",
G_CALLBACK(BluetoothEventManager::on_interface_added),
NULL);
g_signal_connect(_gdbus_manager,
"object-added",
G_CALLBACK(BluetoothEventManager::on_object_added),
NULL);
g_main_context_pop_thread_default(manager_context);
g_main_loop_run(loop);
return NULL;
}
BluetoothManager::BluetoothManager() : event_list()
{
GError *error = NULL;
GList *objects, *l;
manager_context = g_main_context_new ();
g_main_context_push_thread_default(manager_context);
gdbus_manager = object_manager_client_new_for_bus_sync(
G_BUS_TYPE_SYSTEM,
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
"org.bluez",
"/",
NULL, /* GCancellable */
&error);
if (gdbus_manager == nullptr) {
std::string error_str("Error getting object manager client: ");
error_str += error->message;
g_error_free(error);
throw std::runtime_error(error_str);
}
g_main_context_pop_thread_default(manager_context);
manager_thread = g_thread_new("BluetoothManager-Thread", init_manager_thread, gdbus_manager);
objects = g_dbus_object_manager_get_objects(gdbus_manager);
default_adapter = nullptr;
for (l = objects; l != NULL; l = l->next) {
Object *object = (Object *) l->data;
Adapter1 *adapter = object_get_adapter1(object);
if (adapter != NULL) {
default_adapter = std::unique_ptr<BluetoothAdapter>(new BluetoothAdapter(adapter));
g_object_unref(adapter);
break;
}
}
g_list_free_full(objects, g_object_unref);
if (default_adapter == nullptr) {
throw BluetoothException("No adapter installed or not recognized by system");
}
}
BluetoothManager *BluetoothManager::get_bluetooth_manager()
{
static BluetoothManager bluetooth_manager;
return &bluetooth_manager;
}
BluetoothManager::BluetoothManager(const BluetoothManager &)
{
/* Should not be called */
}
BluetoothManager::~BluetoothManager()
{
/* Should not be called */
}
std::vector<std::unique_ptr<BluetoothAdapter>> BluetoothManager::get_adapters()
{
std::vector<std::unique_ptr<BluetoothAdapter>> vector;
GList *l, *objects = g_dbus_object_manager_get_objects(gdbus_manager);
for (l = objects; l != NULL; l = l->next) {
Object *object = OBJECT(l->data);
auto p = BluetoothAdapter::make(object);
if (p != nullptr)
vector.push_back(std::move(p));
}
g_list_free_full(objects, g_object_unref);
return vector;
}
std::vector<std::unique_ptr<BluetoothDevice>> BluetoothManager::get_devices()
{
std::vector<std::unique_ptr<BluetoothDevice>> vector;
GList *l, *objects = g_dbus_object_manager_get_objects(gdbus_manager);
for (l = objects; l != NULL; l = l->next) {
Object *object = OBJECT(l->data);
auto p = BluetoothDevice::make(object);
if (p != nullptr)
vector.push_back(std::move(p));
}
g_list_free_full(objects, g_object_unref);
return vector;
}
std::vector<std::unique_ptr<BluetoothGattService>> BluetoothManager::get_services()
{
std::vector<std::unique_ptr<BluetoothGattService>> vector;
GList *l, *objects = g_dbus_object_manager_get_objects(gdbus_manager);
for (l = objects; l != NULL; l = l->next) {
Object *object = OBJECT(l->data);
auto p = BluetoothGattService::make(object);
if (p != nullptr)
vector.push_back(std::move(p));
}
g_list_free_full(objects, g_object_unref);
return vector;
}
bool BluetoothManager::set_default_adapter(BluetoothAdapter &adapter)
{
default_adapter = std::unique_ptr<BluetoothAdapter>(adapter.clone());
return true;
}
std::unique_ptr<BluetoothAdapter> BluetoothManager::get_default_adapter()
{
return std::unique_ptr<BluetoothAdapter>(default_adapter->clone());
}
bool BluetoothManager::start_discovery()
{
if (default_adapter != nullptr)
return default_adapter->start_discovery();
else
return false;
}
bool BluetoothManager::stop_discovery()
{
if (default_adapter != NULL)
return default_adapter->stop_discovery();
else
return false;
}
bool BluetoothManager::get_discovering()
{
if (default_adapter != NULL)
return default_adapter->get_discovering();
else
return false;
}
|