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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
/*
* 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.
*/
#pragma once
#include "BluetoothObject.hpp"
#include "BluetoothAdapter.hpp"
#include "BluetoothGattService.hpp"
#include "BluetoothManager.hpp"
#include <cstdint>
#include <vector>
#include <functional>
#include <map>
/* Forward declaration of types */
struct _Object;
typedef struct _Object Object;
struct _Device1;
typedef struct _Device1 Device1;
/**
* Provides access to Bluetooth devices. Follows the BlueZ adapter API
* available at: http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/device-api.txt
*/
class tinyb::BluetoothDevice: public BluetoothObject
{
friend class tinyb::BluetoothManager;
friend class tinyb::BluetoothEventManager;
friend class tinyb::BluetoothAdapter;
friend class tinyb::BluetoothGattService;
friend class tinyb::BluetoothNotificationHandler;
private:
Device1 *object;
protected:
BluetoothDevice(Device1 *object);
static std::unique_ptr<BluetoothDevice> make(Object *object,
BluetoothType type = BluetoothType::DEVICE,
std::string *name = nullptr,
std::string *identifier = nullptr,
BluetoothObject *parent = nullptr);
std::function<void(int16_t)> rssi_callback;
std::function<void(bool)> trusted_callback;
std::function<void(bool)> paired_callback;
std::function<void(bool)> connected_callback;
std::function<void(bool)> blocked_callback;
std::function<void(std::map<uint16_t, std::vector<uint8_t>> &)> mfg_callback;
std::function<void(std::map<std::string, std::vector<uint8_t>> &)> service_callback;
std::function<void(bool)> services_resolved_callback;
public:
static std::string java_class() {
return std::string(JAVA_DBUS_PACKAGE "/DBusDevice");
}
static BluetoothType class_type() { return BluetoothType::DEVICE; }
virtual std::string get_java_class() const;
virtual std::string get_class_name() const;
virtual std::string get_object_path() const;
virtual BluetoothType get_bluetooth_type() const;
BluetoothDevice(const BluetoothDevice &object);
~BluetoothDevice();
virtual BluetoothDevice *clone() const;
std::unique_ptr<BluetoothGattService> find(
std::string *identifier,
std::chrono::milliseconds timeout = std::chrono::milliseconds::zero())
{
BluetoothManager *manager = BluetoothManager::get_bluetooth_manager();
return manager->find<BluetoothGattService>(nullptr, identifier, this, timeout);
}
/* D-Bus method calls: */
/** The connection to this device is removed, removing all connected
* profiles.
* @return TRUE if the device disconnected
*/
bool disconnect (
);
/** An asynchronous connection to this device is initiated,
* connecting each profile flagged as auto-connectable.
*/
void connect_async_start ();
/** Completion of the initiated asynchronous connection.
*/
bool connect_async_finish ();
/** A connection to this device is established, connecting each profile
* flagged as auto-connectable.
* @return TRUE if the device connected
*/
bool connect (
);
/** Connects a specific profile available on the device, given by UUID
* @param arg_UUID The UUID of the profile to be connected
* @return TRUE if the profile connected successfully
*/
bool connect_profile (
const std::string &arg_UUID
);
/** Disconnects a specific profile available on the device, given by UUID
* @param arg_UUID The UUID of the profile to be disconnected
* @return TRUE if the profile disconnected successfully
*/
bool disconnect_profile (
const std::string &arg_UUID
);
/** A connection to this device is established, and the device is then
* paired.
* @return TRUE if the device connected and paired
*/
bool pair (
);
/** Remove the current device (like an unpair).
* @return true if the device has been removed from the system.
*/
bool remove_device(
);
/** Cancels an initiated pairing operation
* @return TRUE if the paring is cancelled successfully
*/
bool cancel_pairing (
);
/** Returns a list of BluetoothGattServices available on this device.
* @return A list of BluetoothGattServices available on this device,
* NULL if an error occurred
*/
std::vector<std::unique_ptr<BluetoothGattService>> get_services (
);
/* D-Bus property accessors: */
/** Returns the hardware address of this device.
* @return The hardware address of this device.
*/
std::string get_address ();
/** Returns the remote friendly name of this device.
* @return The remote friendly name of this device, or NULL if not set.
*/
std::string get_name ();
/** Returns an alternative friendly name of this device.
* @return The alternative friendly name of this device, or NULL if not set.
*/
std::string get_alias ();
/** Sets an alternative friendly name of this device.
*/
void set_alias (const std::string &value);
/** Returns the Bluetooth class of the device.
* @return The Bluetooth class of the device.
*/
unsigned int get_class ();
/** Returns the appearance of the device, as found by GAP service.
* @return The appearance of the device, as found by GAP service.
*/
uint16_t get_appearance ();
/** Returns the proposed icon name of the device.
* @return The proposed icon name, or NULL if not set.
*/
std::unique_ptr<std::string> get_icon ();
/** Returns the paired state the device.
* @return The paired state of the device.
*/
bool get_paired ();
/**
* Enables notifications for changes of the paired status of the device
* and triggers the callback when the value changes.
* Uninstalls the previous paired callback, if any was installed.
* @param callback A function of the form void(BluetoothDevice&, bool, void *), where
* BluetoothDevice& is the device for which the callback was set, bool will contain the
* new value of the paired property and void * contains optional, user set data
* @param userdata The data which will be delivered to the callback when it is triggered.
* Memory must be managed by user.
*/
void enable_paired_notifications(
std::function<void(BluetoothDevice &device, bool paired, void *userdata)> callback,
void *userdata);
/**
* Enables notifications for changes of the paired status of the device
* and triggers the callback when the value changes.
* Uninstalls the previous paired callback, if any was installed.
* @param callback A function of the form void(bool), where
* bool will contain the new value of the paired property
*/
void enable_paired_notifications(
std::function<void(bool paired)> callback);
/**
* Disables notifications for changes of the paired status of the device
* and uninstalls any callback.
*/
void disable_paired_notifications();
/** Returns the trusted state the device.
* @return The trusted state of the device.
*/
bool get_trusted ();
/** Sets the trusted state the device.
*/
void set_trusted (bool value);
/**
* Enables notifications for changes of the trusted status of the device
* and triggers the callback when the value changes.
* Uninstalls the previous trusted callback, if any was installed.
* @param callback A function of the form void(BluetoothDevice&, bool, void *), where
* BluetoothDevice& is the device for which the callback was set, bool will contain the
* new value of the trusted property and void * contains optional, user set data
* @param userdata The data which will be delivered to the callback when it is triggered.
* Memory must be managed by user.
*/
void enable_trusted_notifications(
std::function<void(BluetoothDevice &device, bool trusted, void *userdata)> callback,
void *userdata);
/**
* Enables notifications for changes of the trusted status of the device
* and triggers the callback when the value changes.
* Uninstalls the previous trusted callback, if any was installed.
* @param callback A function of the form void(bool), where
* bool will contain the new value of the trusted property
*/
void enable_trusted_notifications(
std::function<void(bool trusted)> callback);
/**
* Disables notifications for changes of the trusted status of the device
* and uninstalls any callback.
*/
void disable_trusted_notifications();
/** Returns the blocked state the device.
* @return The blocked state of the device.
*/
bool get_blocked ();
/** Sets the blocked state the device.
*/
void set_blocked (bool value);
/**
* Enables notifications for changes of the blocked status of the device
* and triggers the callback when the value changes.
* Uninstalls the previous blocked callback, if any was installed.
* @param callback A function of the form void(BluetoothDevice&, bool, void *), where
* BluetoothDevice& is the device for which the callback was set, bool will contain the
* new value of the blocked property and void * contains optional, user set data
* @param userdata The data which will be delivered to the callback when it is triggered.
* Memory must be managed by user.
*/
void enable_blocked_notifications(
std::function<void(BluetoothDevice &device, bool blocked, void *userdata)> callback,
void *userdata);
/**
* Enables notifications for changes of the blocked status of the device
* and triggers the callback when the value changes.
* Uninstalls the previous blocked callback, if any was installed.
* @param callback A function of the form void(bool), where
* bool will contain the new value of the blocked property
*/
void enable_blocked_notifications(
std::function<void(bool blocked)> callback);
/**
* Disables notifications for changes of the blocked status of the device
* and uninstalls any callback.
*/
void disable_blocked_notifications();
/** Returns if device uses only pre-Bluetooth 2.1 pairing mechanism.
* @return True if device uses only pre-Bluetooth 2.1 pairing mechanism.
*/
bool get_legacy_pairing ();
/** Returns the Received Signal Strength Indicator of the device (0 means unknown).
* @return The Received Signal Strength Indicator of the device (0 means unknown).
*/
int16_t get_rssi ();
/**
* Enables notifications for changes of the RSSI value of the device
* and triggers the callback when the value changes.
* Uninstalls the previous RSSI callback, if any was installed.
* @param callback A function of the form void(BluetoothDevice&, int16_t, void *), where
* BluetoothDevice& is the device for which the callback was set, bool will contain the
* new value of the RSSI property and void * contains optional, user set data
* @param userdata The data which will be delivered to the callback when it is triggered.
* Memory must be managed by user.
*/
void enable_rssi_notifications(
std::function<void(BluetoothDevice &device, int16_t rssi, void *userdata)> callback,
void *userdata = nullptr);
/**
* Enables notifications for changes of the RSSI value of the device
* and triggers the callback when the value changes.
* Uninstalls the previous RSSI callback, if any was installed.
* @param callback A function of the form void(int16_t), where
* bool will contain the new value of the RSSI property
*/
void enable_rssi_notifications(
std::function<void(int16_t rssi)> callback);
/**
* Disables notifications for changes of the RSSI value of the device
* and uninstalls any callback.
*/
void disable_rssi_notifications();
/** Returns the connected state of the device.
* @return The connected state of the device.
*/
bool get_connected ();
/**
* Enables notifications for changes of the connected status of the device
* and triggers the callback when the value changes.
* Uninstalls the previous connected callback, if any was installed.
* @param callback A function of the form void(BluetoothDevice&, bool, void *), where
* BluetoothDevice& is the device for which the callback was set, bool will contain the
* new value of the connected property and void * contains optional, user set data
* @param userdata The data which will be delivered to the callback when it is triggered.
* Memory must be managed by user.
*/
void enable_connected_notifications(
std::function<void(BluetoothDevice &device, bool connected, void *userdata)> callback,
void *userdata);
/**
* Enables notifications for changes of the connected status of the device
* and triggers the callback when the value changes.
* Uninstalls the previous connected callback, if any was installed.
* @param callback A function of the form void(bool), where
* bool will contain the new value of the connected property
*/
void enable_connected_notifications(
std::function<void(bool connected)> callback);
/**
* Disables notifications for changes of the connected status of the device
* and uninstalls any callback.
*/
void disable_connected_notifications();
/** Returns the UUIDs of the device.
* @return Array containing the UUIDs of the device, ends with NULL.
*/
std::vector<std::string> get_uuids ();
/** Returns the local ID of the adapter, or nullptr.
* @return The local ID of the adapter, or nullptr.
*/
std::unique_ptr<std::string> get_modalias ();
/** Returns the adapter on which this device was discovered or
* connected.
* @return The adapter.
*/
BluetoothAdapter get_adapter ();
/** Returns a map containing manufacturer specific advertisement data.
* An entry has a uint16_t key and an array of bytes.
* @return manufacturer specific advertisement data.
*/
std::map<uint16_t, std::vector<uint8_t>> get_manufacturer_data();
/**
* Enables notifications for changes of the manufacturer data of the device
* and triggers the callback when the value changes.
* Uninstalls the previous connected callback, if any was installed.
* @param callback A function of the form void(BluetoothDevice&, bool, void *), where
* BluetoothDevice& is the device for which the callback was set, bool will contain the
* new value of the connected property and void * contains optional, user set data
* @param userdata The data which will be delivered to the callback when it is triggered.
* Memory must be managed by user.
*/
void enable_manufacturer_data_notifications(
std::function<void(BluetoothDevice &device, std::map<uint16_t, std::vector<uint8_t>> &mfgdata, void *userdata)> callback,
void *userdata);
/**
* Enables notifications for changes in the manufacturer data of the device
* and triggers the callback when the value changes.
* Uninstalls the previous connected callback, if any was installed.
* @param callback A function of the form void(bool), where
* bool will contain the new value of the connected property
*/
void enable_manufacturer_data_notifications(
std::function<void(std::map<uint16_t, std::vector<uint8_t>> &mfgdata)> callback);
/**
* Disables notifications for changes in the manufacturer data of the device
* and uninstalls any callback.
*/
void disable_manufacturer_data_notifications();
/** Returns a map containing service advertisement data.
* An entry has a UUID string key and an array of bytes.
* @return service advertisement data.
*/
std::map<std::string, std::vector<uint8_t>> get_service_data();
/**
* Enables notifications for changes of the service data of the device
* and triggers the callback when the value changes.
* Uninstalls the previous connected callback, if any was installed.
* @param callback A function of the form void(BluetoothDevice&, bool, void *), where
* BluetoothDevice& is the device for which the callback was set, bool will contain the
* new value of the connected property and void * contains optional, user set data
* @param userdata The data which will be delivered to the callback when it is triggered.
* Memory must be managed by user.
*/
void enable_service_data_notifications(
std::function<void(BluetoothDevice &device, std::map<std::string, std::vector<uint8_t>> &servicedata, void *userdata)> callback,
void *userdata);
/**
* Enables notifications for changes in the manufacturer data of the device
* and triggers the callback when the value changes.
* Uninstalls the previous connected callback, if any was installed.
* @param callback A function of the form void(bool), where
* bool will contain the new value of the connected property
*/
void enable_service_data_notifications(
std::function<void(std::map<std::string, std::vector<uint8_t>> &servicedata)> callback);
/**
* Disables notifications for changes in the service data of the device
* and uninstalls any callback.
*/
void disable_service_data_notifications();
/** Returns the transmission power level (0 means unknown).
* @return the transmission power level (0 means unknown).
*/
int16_t get_tx_power ();
/** Returns true if service discovery has ended.
* @return true if the service discovery has ended.
*/
bool get_services_resolved ();
/**
* Enables notifications for changes of the services resolved status of the device
* and triggers the callback when the value changes.
* Uninstalls the previous services resolved callback, if any was installed.
* @param callback A function of the form void(BluetoothDevice&, bool, void *), where
* BluetoothDevice& is the device for which the callback was set, bool will contain the
* new value of the services resolved property and void * contains optional, user set data
* @param userdata The data which will be delivered to the callback when it is triggered.
* Memory must be managed by user.
*/
void enable_services_resolved_notifications(
std::function<void(BluetoothDevice &device, bool services_resolved, void *userdata)> callback,
void *userdata);
/**
* Enables notifications for changes of the services resolved status of the device
* and triggers the callback when the value changes.
* Uninstalls the previous services resolved callback, if any was installed.
* @param callback A function of the form void(bool), where
* bool will contain the new value of the services resolved property
*/
void enable_services_resolved_notifications(
std::function<void(bool connec)> callback);
/**
* Disables notifications for changes of the services resolved status of the device
* and uninstalls any callback.
*/
void disable_services_resolved_notifications();
};
|