Skip to content
This repository was archived by the owner on Jan 4, 2023. It is now read-only.

Commit 52a0b06

Browse files
committed
Add notifications for manufacturer data, service data and services resolved
Signed-off-by: Petre Eftime <[email protected]>
1 parent 2c3706f commit 52a0b06

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

api/tinyb/BluetoothDevice.hpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ friend class tinyb::BluetoothNotificationHandler;
6868
std::function<void(bool)> paired_callback;
6969
std::function<void(bool)> connected_callback;
7070
std::function<void(bool)> blocked_callback;
71+
std::function<void(std::map<uint16_t, std::vector<uint8_t>> &)> mfg_callback;
72+
std::function<void(std::map<std::string, std::vector<uint8_t>> &)> service_callback;
73+
std::function<void(bool)> services_resolved_callback;
7174

7275
public:
7376

@@ -381,12 +384,67 @@ friend class tinyb::BluetoothNotificationHandler;
381384
* @return manufacturer specific advertisement data.
382385
*/
383386
std::map<uint16_t, std::vector<uint8_t>> get_manufacturer_data();
387+
/**
388+
* Enables notifications for changes of the manufacturer data of the device
389+
* and triggers the callback when the value changes.
390+
* Uninstalls the previous connected callback, if any was installed.
391+
* @param callback A function of the form void(BluetoothDevice&, bool, void *), where
392+
* BluetoothDevice& is the device for which the callback was set, bool will contain the
393+
* new value of the connected property and void * contains optional, user set data
394+
* @param userdata The data which will be delivered to the callback when it is triggered.
395+
* Memory must be managed by user.
396+
*/
397+
void enable_manufacturer_data_notifications(
398+
std::function<void(BluetoothDevice &device, std::map<uint16_t, std::vector<uint8_t>> &mfgdata, void *userdata)> callback,
399+
void *userdata);
400+
/**
401+
* Enables notifications for changes in the manufacturer data of the device
402+
* and triggers the callback when the value changes.
403+
* Uninstalls the previous connected callback, if any was installed.
404+
* @param callback A function of the form void(bool), where
405+
* bool will contain the new value of the connected property
406+
*/
407+
void enable_manufacturer_data_notifications(
408+
std::function<void(std::map<uint16_t, std::vector<uint8_t>> &mfgdata)> callback);
409+
/**
410+
* Disables notifications for changes in the manufacturer data of the device
411+
* and uninstalls any callback.
412+
*/
413+
void disable_manufacturer_data_notifications();
414+
384415

385416
/** Returns a map containing service advertisement data.
386417
* An entry has a UUID string key and an array of bytes.
387418
* @return service advertisement data.
388419
*/
389420
std::map<std::string, std::vector<uint8_t>> get_service_data();
421+
/**
422+
* Enables notifications for changes of the service data of the device
423+
* and triggers the callback when the value changes.
424+
* Uninstalls the previous connected callback, if any was installed.
425+
* @param callback A function of the form void(BluetoothDevice&, bool, void *), where
426+
* BluetoothDevice& is the device for which the callback was set, bool will contain the
427+
* new value of the connected property and void * contains optional, user set data
428+
* @param userdata The data which will be delivered to the callback when it is triggered.
429+
* Memory must be managed by user.
430+
*/
431+
void enable_service_data_notifications(
432+
std::function<void(BluetoothDevice &device, std::map<std::string, std::vector<uint8_t>> &servicedata, void *userdata)> callback,
433+
void *userdata);
434+
/**
435+
* Enables notifications for changes in the manufacturer data of the device
436+
* and triggers the callback when the value changes.
437+
* Uninstalls the previous connected callback, if any was installed.
438+
* @param callback A function of the form void(bool), where
439+
* bool will contain the new value of the connected property
440+
*/
441+
void enable_service_data_notifications(
442+
std::function<void(std::map<std::string, std::vector<uint8_t>> &servicedata)> callback);
443+
/**
444+
* Disables notifications for changes in the service data of the device
445+
* and uninstalls any callback.
446+
*/
447+
void disable_service_data_notifications();
390448

391449
/** Returns the transmission power level (0 means unknown).
392450
* @return the transmission power level (0 means unknown).
@@ -397,4 +455,32 @@ friend class tinyb::BluetoothNotificationHandler;
397455
* @return true if the service discovery has ended.
398456
*/
399457
bool get_services_resolved ();
458+
/**
459+
* Enables notifications for changes of the services resolved status of the device
460+
* and triggers the callback when the value changes.
461+
* Uninstalls the previous services resolved callback, if any was installed.
462+
* @param callback A function of the form void(BluetoothDevice&, bool, void *), where
463+
* BluetoothDevice& is the device for which the callback was set, bool will contain the
464+
* new value of the services resolved property and void * contains optional, user set data
465+
* @param userdata The data which will be delivered to the callback when it is triggered.
466+
* Memory must be managed by user.
467+
*/
468+
void enable_services_resolved_notifications(
469+
std::function<void(BluetoothDevice &device, bool services_resolved, void *userdata)> callback,
470+
void *userdata);
471+
/**
472+
* Enables notifications for changes of the services resolved status of the device
473+
* and triggers the callback when the value changes.
474+
* Uninstalls the previous services resolved callback, if any was installed.
475+
* @param callback A function of the form void(bool), where
476+
* bool will contain the new value of the services resolved property
477+
*/
478+
void enable_services_resolved_notifications(
479+
std::function<void(bool connec)> callback);
480+
/**
481+
* Disables notifications for changes of the services resolved status of the device
482+
* and uninstalls any callback.
483+
*/
484+
void disable_services_resolved_notifications();
485+
400486
};

src/BluetoothDevice.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,59 @@ void BluetoothNotificationHandler::on_properties_changed_device(GDBusProxy *prox
8181
connected_callback(new_value);
8282
continue;
8383
}
84+
auto mfg_callback = c->mfg_callback;
85+
if (mfg_callback != nullptr && g_ascii_strncasecmp(key, "manufacturerdata", 16) == 0) {
86+
std::map<uint16_t, std::vector<uint8_t>> new_value;
87+
88+
GVariantIter *iter;
89+
g_variant_get (value, "a{qv}", &iter);
90+
91+
GVariant *array;
92+
uint16_t key;
93+
uint8_t val;
94+
95+
while (g_variant_iter_loop(iter, "{qv}", &key, &array)) {
96+
GVariantIter it_array;
97+
g_variant_iter_init(&it_array, array);
98+
while(g_variant_iter_loop(&it_array, "y", &val)) {
99+
new_value[key].push_back(val);
100+
}
101+
}
102+
103+
g_variant_iter_free(iter);
104+
mfg_callback(new_value);
105+
continue;
106+
}
107+
auto service_callback = c->service_callback;
108+
if (service_callback != nullptr && g_ascii_strncasecmp(key, "servicedata", 11) == 0) {
109+
std::map<std::string, std::vector<uint8_t>> new_value;
110+
111+
GVariantIter *iter;
112+
g_variant_get (value, "a{sv}", &iter);
113+
114+
GVariant *array;
115+
const char* key;
116+
uint8_t val;
117+
118+
while (g_variant_iter_loop(iter, "{sv}", &key, &array)) {
119+
GVariantIter it_array;
120+
g_variant_iter_init(&it_array, array);
121+
while(g_variant_iter_loop(&it_array, "y", &val)) {
122+
new_value[key].push_back(val);
123+
}
124+
}
125+
126+
g_variant_iter_free(iter);
127+
service_callback(new_value);
128+
continue;
129+
}
130+
auto services_resolved_callback = c->services_resolved_callback;
131+
if (services_resolved_callback != nullptr && g_ascii_strncasecmp(key, "servicesresolved", 16) == 0) {
132+
bool new_value;
133+
g_variant_get(value, "b", &new_value);
134+
services_resolved_callback(new_value);
135+
continue;
136+
}
84137
}
85138
g_variant_iter_free (iter);
86139
}
@@ -475,6 +528,19 @@ std::map<uint16_t, std::vector<uint8_t>> BluetoothDevice::get_manufacturer_data(
475528
return m_data;
476529
}
477530

531+
void BluetoothDevice::enable_manufacturer_data_notifications(
532+
std::function<void(BluetoothDevice &, std::map<uint16_t, std::vector<uint8_t>> &, void *)> callback,
533+
void *userdata) {
534+
mfg_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata);
535+
}
536+
void BluetoothDevice::enable_manufacturer_data_notifications(
537+
std::function<void(std::map<uint16_t, std::vector<uint8_t>> &)> callback) {
538+
mfg_callback = callback;
539+
}
540+
void BluetoothDevice::disable_manufacturer_data_notifications() {
541+
mfg_callback = nullptr;
542+
}
543+
478544
std::map<std::string, std::vector<uint8_t>> BluetoothDevice::get_service_data()
479545
{
480546
std::map<std::string, std::vector<uint8_t>> m_data;
@@ -505,6 +571,19 @@ std::map<std::string, std::vector<uint8_t>> BluetoothDevice::get_service_data()
505571
return m_data;
506572
}
507573

574+
void BluetoothDevice::enable_service_data_notifications(
575+
std::function<void(BluetoothDevice &, std::map<std::string, std::vector<uint8_t>> &, void *)> callback,
576+
void *userdata) {
577+
service_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata);
578+
}
579+
void BluetoothDevice::enable_service_data_notifications(
580+
std::function<void(std::map<std::string, std::vector<uint8_t>> &)> callback) {
581+
service_callback = callback;
582+
}
583+
void BluetoothDevice::disable_service_data_notifications() {
584+
service_callback = nullptr;
585+
}
586+
508587
int16_t BluetoothDevice::get_tx_power ()
509588
{
510589
return device1_get_tx_power (object);
@@ -515,3 +594,16 @@ bool BluetoothDevice::get_services_resolved ()
515594
return device1_get_services_resolved (object);
516595
}
517596

597+
void BluetoothDevice::enable_services_resolved_notifications(
598+
std::function<void(BluetoothDevice &, bool, void *)> callback,
599+
void *userdata) {
600+
services_resolved_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata);
601+
}
602+
void BluetoothDevice::enable_services_resolved_notifications(
603+
std::function<void(bool)> callback) {
604+
services_resolved_callback = callback;
605+
}
606+
void BluetoothDevice::disable_services_resolved_notifications() {
607+
services_resolved_callback = nullptr;
608+
}
609+

0 commit comments

Comments
 (0)