diff options
-rw-r--r-- | api/tinyb/BluetoothDevice.hpp | 86 | ||||
-rw-r--r-- | src/BluetoothDevice.cpp | 92 |
2 files changed, 178 insertions, 0 deletions
diff --git a/api/tinyb/BluetoothDevice.hpp b/api/tinyb/BluetoothDevice.hpp index 0216cea4..68ad89eb 100644 --- a/api/tinyb/BluetoothDevice.hpp +++ b/api/tinyb/BluetoothDevice.hpp @@ -68,6 +68,9 @@ protected: 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: @@ -381,12 +384,67 @@ public: * @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). @@ -397,4 +455,32 @@ public: * @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(); + }; diff --git a/src/BluetoothDevice.cpp b/src/BluetoothDevice.cpp index a0832a43..9f3500a3 100644 --- a/src/BluetoothDevice.cpp +++ b/src/BluetoothDevice.cpp @@ -81,6 +81,59 @@ void BluetoothNotificationHandler::on_properties_changed_device(GDBusProxy *prox connected_callback(new_value); continue; } + auto mfg_callback = c->mfg_callback; + if (mfg_callback != nullptr && g_ascii_strncasecmp(key, "manufacturerdata", 16) == 0) { + std::map<uint16_t, std::vector<uint8_t>> new_value; + + GVariantIter *iter; + g_variant_get (value, "a{qv}", &iter); + + GVariant *array; + uint16_t key; + uint8_t val; + + while (g_variant_iter_loop(iter, "{qv}", &key, &array)) { + GVariantIter it_array; + g_variant_iter_init(&it_array, array); + while(g_variant_iter_loop(&it_array, "y", &val)) { + new_value[key].push_back(val); + } + } + + g_variant_iter_free(iter); + mfg_callback(new_value); + continue; + } + auto service_callback = c->service_callback; + if (service_callback != nullptr && g_ascii_strncasecmp(key, "servicedata", 11) == 0) { + std::map<std::string, std::vector<uint8_t>> new_value; + + GVariantIter *iter; + g_variant_get (value, "a{sv}", &iter); + + GVariant *array; + const char* key; + uint8_t val; + + while (g_variant_iter_loop(iter, "{sv}", &key, &array)) { + GVariantIter it_array; + g_variant_iter_init(&it_array, array); + while(g_variant_iter_loop(&it_array, "y", &val)) { + new_value[key].push_back(val); + } + } + + g_variant_iter_free(iter); + service_callback(new_value); + continue; + } + auto services_resolved_callback = c->services_resolved_callback; + if (services_resolved_callback != nullptr && g_ascii_strncasecmp(key, "servicesresolved", 16) == 0) { + bool new_value; + g_variant_get(value, "b", &new_value); + services_resolved_callback(new_value); + continue; + } } g_variant_iter_free (iter); } @@ -475,6 +528,19 @@ std::map<uint16_t, std::vector<uint8_t>> BluetoothDevice::get_manufacturer_data( return m_data; } +void BluetoothDevice::enable_manufacturer_data_notifications( + std::function<void(BluetoothDevice &, std::map<uint16_t, std::vector<uint8_t>> &, void *)> callback, + void *userdata) { + mfg_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); +} +void BluetoothDevice::enable_manufacturer_data_notifications( + std::function<void(std::map<uint16_t, std::vector<uint8_t>> &)> callback) { + mfg_callback = callback; +} +void BluetoothDevice::disable_manufacturer_data_notifications() { + mfg_callback = nullptr; +} + std::map<std::string, std::vector<uint8_t>> BluetoothDevice::get_service_data() { 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() return m_data; } +void BluetoothDevice::enable_service_data_notifications( + std::function<void(BluetoothDevice &, std::map<std::string, std::vector<uint8_t>> &, void *)> callback, + void *userdata) { + service_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); +} +void BluetoothDevice::enable_service_data_notifications( + std::function<void(std::map<std::string, std::vector<uint8_t>> &)> callback) { + service_callback = callback; +} +void BluetoothDevice::disable_service_data_notifications() { + service_callback = nullptr; +} + int16_t BluetoothDevice::get_tx_power () { return device1_get_tx_power (object); @@ -515,3 +594,16 @@ bool BluetoothDevice::get_services_resolved () return device1_get_services_resolved (object); } +void BluetoothDevice::enable_services_resolved_notifications( + std::function<void(BluetoothDevice &, bool, void *)> callback, + void *userdata) { + services_resolved_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); +} +void BluetoothDevice::enable_services_resolved_notifications( + std::function<void(bool)> callback) { + services_resolved_callback = callback; +} +void BluetoothDevice::disable_services_resolved_notifications() { + services_resolved_callback = nullptr; +} + |