diff options
author | Sven Gothel <[email protected]> | 2021-08-31 04:41:54 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-08-31 04:41:54 +0200 |
commit | aef10f3cdec0fd698cfa7d913725b40d1513ce30 (patch) | |
tree | e36d6b2796e40c5d4f40e66d9cbb6b7259bd42f6 /src | |
parent | 99ddbcee148144b3593adf4440d3fa3d06f229ab (diff) |
Direct-BT: Remove TinyB and Cleanup Java API (1)
Starting with version 2.3, the previously refactored *TinyB* has been removed completely.
Motivation was lack of detailed Bluetooth support, inclusive increasing diversion with *Direct-BT*.
Furthermore, work is underway for `BLE slave periphal and GATT server` support and its mapping to *BlueZ D-Bus* is questionable
and would be resource intensive.
Java API changed as follows:
- Objects no more Clonable
- Removed dead unsupported code
- Removed deprecated code
- Added 'GattCharPropertySet', representing property bit mask for BTGattChar,
replacing the string array.
Diffstat (limited to 'src')
-rw-r--r-- | src/tinyb/BluetoothAdapter.cpp | 461 | ||||
-rw-r--r-- | src/tinyb/BluetoothDevice.cpp | 644 | ||||
-rw-r--r-- | src/tinyb/BluetoothEvent.cpp | 111 | ||||
-rw-r--r-- | src/tinyb/BluetoothGattCharacteristic.cpp | 325 | ||||
-rw-r--r-- | src/tinyb/BluetoothGattDescriptor.cpp | 245 | ||||
-rw-r--r-- | src/tinyb/BluetoothGattService.cpp | 148 | ||||
-rw-r--r-- | src/tinyb/BluetoothManager.cpp | 420 | ||||
-rw-r--r-- | src/tinyb/BluetoothObject.cpp | 64 | ||||
-rw-r--r-- | src/tinyb/BluetoothUUID.cpp | 94 | ||||
-rw-r--r-- | src/tinyb/CMakeLists.txt | 88 | ||||
-rw-r--r-- | src/tinyb/org.bluez.xml | 195 | ||||
-rw-r--r-- | src/tinyb/tinyb.pc.cmake | 11 | ||||
-rw-r--r-- | src/tinyb/tinyb_utils.cpp | 95 |
13 files changed, 0 insertions, 2901 deletions
diff --git a/src/tinyb/BluetoothAdapter.cpp b/src/tinyb/BluetoothAdapter.cpp deleted file mode 100644 index c8ea3fbd..00000000 --- a/src/tinyb/BluetoothAdapter.cpp +++ /dev/null @@ -1,461 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * 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 "tinyb_utils.hpp" -#include "BluetoothNotificationHandler.hpp" -#include "BluetoothAdapter.hpp" -#include "BluetoothDevice.hpp" -#include "BluetoothManager.hpp" -#include "BluetoothException.hpp" - -using namespace tinyb; - -void BluetoothNotificationHandler::on_properties_changed_adapter(GDBusProxy *proxy, GVariant *changed_properties, GStrv invalidated_properties, gpointer userdata) { - (void) proxy; - (void) invalidated_properties; - - auto c = static_cast<BluetoothAdapter*>(userdata); - - if (!c->lock()) - return; - - if(g_variant_n_children(changed_properties) > 0) { - GVariantIter *iter = NULL; - - GVariant *value; - const gchar *key; - g_variant_get(changed_properties, "a{sv}", &iter); - while (iter != nullptr && g_variant_iter_loop(iter, "{&sv}", &key, &value)) { - auto powered_callback = c->powered_callback; - if (powered_callback != nullptr && g_ascii_strncasecmp(key, "powered", 8) == 0) { - bool new_value; - g_variant_get(value, "b", &new_value); - powered_callback(new_value); - continue; - } - auto discoverable_callback = c->discoverable_callback; - if (discoverable_callback != nullptr && g_ascii_strncasecmp(key, "discoverable", 13) == 0) { - bool new_value; - g_variant_get(value, "b", &new_value); - discoverable_callback(new_value); - continue; - } - auto pairable_callback = c->pairable_callback; - if (pairable_callback != nullptr && g_ascii_strncasecmp(key, "pairable", 9) == 0) { - bool new_value; - g_variant_get(value, "b", &new_value); - pairable_callback(new_value); - continue; - } - auto discovering_callback = c->discovering_callback; - if (discovering_callback != nullptr && g_ascii_strncasecmp(key, "discovering", 12) == 0) { - bool new_value; - g_variant_get(value, "b", &new_value); - discovering_callback(new_value); - continue; - } - } - g_variant_iter_free (iter); - } - - c->unlock(); -} - -std::string BluetoothAdapter::get_class_name() const -{ - return std::string("BluetoothAdapter"); -} - -std::string BluetoothAdapter::get_java_class() const -{ - return std::string(JAVA_DBUS_PACKAGE "/DBusAdapter"); -} - -std::string BluetoothAdapter::get_object_path() const -{ - return std::string(g_dbus_proxy_get_object_path(G_DBUS_PROXY(object))); -} - -BluetoothType BluetoothAdapter::get_bluetooth_type() const -{ - return BluetoothType::ADAPTER; -} - -BluetoothAdapter::BluetoothAdapter(Adapter1 *object_) -{ - this->object = object_; - g_object_ref(object_); - - g_signal_connect(G_DBUS_PROXY(object_), "g-properties-changed", - G_CALLBACK(BluetoothNotificationHandler::on_properties_changed_adapter), this); - valid = true; -} - -BluetoothAdapter::BluetoothAdapter(const BluetoothAdapter &object_) -{ - BluetoothAdapter(object_.object); -} - -BluetoothAdapter *BluetoothAdapter::clone() const -{ - return new BluetoothAdapter(object); -} - -BluetoothAdapter::~BluetoothAdapter() -{ - valid = false; - g_signal_handlers_disconnect_by_data(object, this); - lk.lock(); - - g_object_unref(object); -} - -std::unique_ptr<BluetoothAdapter> BluetoothAdapter::make(Object *object, - BluetoothType type, std::string *name, std::string *identifier, - BluetoothObject *parent) -{ - Adapter1 *adapter; - if((type == BluetoothType::NONE || type == BluetoothType::ADAPTER) && - (adapter = object_get_adapter1(object)) != NULL) { - - std::unique_ptr<BluetoothAdapter> p(new BluetoothAdapter(adapter)); - g_object_unref(adapter); - - if ((name == nullptr || *name == p->get_name()) && - (identifier == nullptr || *identifier == p->get_address()) && - (parent == nullptr)) - return p; - } - - return std::unique_ptr<BluetoothAdapter>(); -} - -std::vector<std::unique_ptr<BluetoothDevice>> BluetoothAdapter::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 *object2 = OBJECT(l->data); - - auto p = BluetoothDevice::make(object2, - BluetoothType::DEVICE, NULL, NULL, this); - if (p != nullptr) - vector.push_back(std::move(p)); - } - g_list_free_full(objects, g_object_unref); - - return vector; -} - -/* D-Bus method calls: */ -bool BluetoothAdapter::start_discovery () -{ - GError *error = NULL; - if (get_discovering() == true) - return true; - bool result = adapter1_call_start_discovery_sync( - object, - NULL, - &error - ); - handle_error(error); - return result; -} - -bool BluetoothAdapter::stop_discovery () -{ - GError *error = NULL; - if (get_discovering() == false) - return true; - bool result = adapter1_call_stop_discovery_sync( - object, - NULL, - &error - ); - handle_error(error); - return result; -} - -bool BluetoothAdapter::remove_device ( - const std::string &arg_device) -{ - GError *error = NULL; - bool result = adapter1_call_remove_device_sync( - object, - arg_device.c_str(), - NULL, - &error - ); - handle_error(error); - return result; -} - -bool BluetoothAdapter::set_discovery_filter (std::vector<BluetoothUUID> uuids, - int16_t rssi, uint16_t pathloss, const TransportType &transport) -{ - GError *error = NULL; - bool result = true; - GVariantDict dict; - g_variant_dict_init(&dict, NULL); - - if (uuids.size() > 0) - { - GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)")); - - for (std::vector<BluetoothUUID>::iterator i = uuids.begin(); i != uuids.end(); ++i) - g_variant_builder_add(builder, "(s)", (*i).get_string().c_str()); - - GVariant *uuids_variant = g_variant_new("a(s)", builder); - g_variant_builder_unref(builder); - g_variant_dict_insert_value(&dict, "UUIDs", uuids_variant); - } - - if (rssi != 0) - g_variant_dict_insert_value(&dict, "RSSI", g_variant_new_int16(rssi)); - - if (pathloss != 0) - g_variant_dict_insert_value(&dict, "Pathloss", g_variant_new_uint16(pathloss)); - - std::string transport_str; - - if (transport == TransportType::AUTO) - transport_str = "auto"; - else if (transport == TransportType::BREDR) - transport_str = "bredr"; - else if (transport == TransportType::LE) - transport_str = "le"; - - if (!transport_str.empty()) - g_variant_dict_insert_value(&dict, "Transport", g_variant_new_string(transport_str.c_str())); - - GVariant *variant = g_variant_dict_end(&dict); - - result = adapter1_call_set_discovery_filter_sync( - object, - variant, - NULL, - &error - ); - - handle_error(error); - return result; -} - -/* D-Bus property accessors: */ -std::string BluetoothAdapter::get_address () -{ - return std::string(adapter1_get_address (object)); -} - -std::string BluetoothAdapter::get_name () -{ - return std::string(adapter1_get_name (object)); -} - -std::string BluetoothAdapter::get_alias () -{ - return std::string(adapter1_get_alias (object)); -} - -void BluetoothAdapter::set_alias (const std::string &value) -{ - adapter1_set_alias (object, value.c_str()); -} - -unsigned int BluetoothAdapter::get_class () -{ - return adapter1_get_class (object); -} - -bool BluetoothAdapter::get_powered () -{ - return adapter1_get_powered (object); -} - -void BluetoothAdapter::enable_powered_notifications( - std::function<void(BluetoothAdapter &adapter, bool powered, void *userdata)> callback, - void *userdata) { - powered_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); -} -void BluetoothAdapter::enable_powered_notifications(std::function<void(bool powered)> callback) { - powered_callback = callback; -} -void BluetoothAdapter::disable_powered_notifications() { - powered_callback = nullptr; -} - -void BluetoothAdapter::set_powered (bool value) -{ - if (get_powered() != value) - adapter1_set_powered (object, value); -} - -bool BluetoothAdapter::get_discoverable () -{ - return adapter1_get_discoverable (object); -} - -void BluetoothAdapter::set_discoverable (bool value) -{ - adapter1_set_discoverable (object, value); -} - -void BluetoothAdapter::enable_discoverable_notifications( - std::function<void(BluetoothAdapter &adapter, bool discoverable, void *userdata)> callback, - void *userdata) { - discoverable_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); -} -void BluetoothAdapter::enable_discoverable_notifications(std::function<void(bool discoverable)> callback) { - discoverable_callback = callback; -} -void BluetoothAdapter::disable_discoverable_notifications() { - discoverable_callback = nullptr; -} - -unsigned int BluetoothAdapter::get_discoverable_timeout () -{ - return adapter1_get_discoverable_timeout (object); -} - -void BluetoothAdapter::set_discoverable_timeout (unsigned int value) -{ - adapter1_set_discoverable_timeout (object, value); -} - -bool BluetoothAdapter::get_pairable () -{ - return adapter1_get_pairable (object); -} - -void BluetoothAdapter::enable_pairable_notifications( - std::function<void(BluetoothAdapter &adapter, bool pairable, void *userdata)> callback, - void *userdata) { - pairable_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); -} -void BluetoothAdapter::enable_pairable_notifications(std::function<void(bool pairable)> callback) { - pairable_callback = callback; -} -void BluetoothAdapter::disable_pairable_notifications() { - pairable_callback = nullptr; -} - -void BluetoothAdapter::set_pairable (bool value) -{ - adapter1_set_pairable (object, value); -} - -unsigned int BluetoothAdapter::get_pairable_timeout () -{ - return adapter1_get_pairable_timeout (object); -} - -void BluetoothAdapter::set_pairable_timeout (unsigned int value) -{ - adapter1_set_pairable_timeout (object, value); -} - -bool BluetoothAdapter::get_discovering () -{ - return adapter1_get_discovering (object); -} - -void BluetoothAdapter::enable_discovering_notifications( - std::function<void(BluetoothAdapter &adapter, bool discovering, void *userdata)> callback, - void *userdata) { - discovering_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); -} -void BluetoothAdapter::enable_discovering_notifications(std::function<void(bool discovering)> callback) { - discovering_callback = callback; -} -void BluetoothAdapter::disable_discovering_notifications() { - discovering_callback = nullptr; -} - -std::vector<std::string> BluetoothAdapter::get_uuids () -{ - const char * const *uuids_c = adapter1_get_uuids (object); - std::vector<std::string> uuids; - for (int i = 0; uuids_c[i] != NULL ;i++) - uuids.push_back(std::string(uuids_c[i])); - return uuids; -} - -std::unique_ptr<std::string> BluetoothAdapter::get_modalias () -{ - const gchar *modalias= adapter1_get_modalias (object); - if (modalias == nullptr) - return std::unique_ptr<std::string>(); - return std::unique_ptr<std::string>(new std::string(modalias)); -} - -std::unique_ptr<BluetoothDevice> BluetoothAdapter::connect_device ( - const std::string &arg_address, const std::string &arg_address_type) -{ - bool result = true; - GError *error = NULL; - GVariantDict dict; - gchar *object_path = nullptr; - - g_variant_dict_init(&dict, NULL); - g_variant_dict_insert_value(&dict, "Address", g_variant_new_string(arg_address.c_str())); - g_variant_dict_insert_value(&dict, "AddressType", g_variant_new_string(arg_address_type.c_str())); - GVariant *variant = g_variant_dict_end(&dict); - - result = adapter1_call_connect_device_sync( - object, - variant, - &object_path, - NULL, - &error - ); - handle_error(error); - - if( result && nullptr != object_path ) { - - Device1 *device = device1_proxy_new_for_bus_sync( - G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, - "org.bluez", - object_path, - NULL, - &error); - g_free(object_path); - handle_error(error); - - if (device == nullptr) { - std::string error_msg("Error occured while instantiating device: "); - error_msg += error->message; - g_error_free(error); - throw BluetoothException(error_msg); - } - std::unique_ptr<BluetoothDevice> p(new BluetoothDevice(device)); - g_object_unref(device); - return p; - } - g_free(object_path); - return std::unique_ptr<BluetoothDevice>(); -} - diff --git a/src/tinyb/BluetoothDevice.cpp b/src/tinyb/BluetoothDevice.cpp deleted file mode 100644 index f8160a7d..00000000 --- a/src/tinyb/BluetoothDevice.cpp +++ /dev/null @@ -1,644 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * 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 "tinyb_utils.hpp" -#include "BluetoothNotificationHandler.hpp" -#include "BluetoothDevice.hpp" -#include "BluetoothGattService.hpp" -#include "BluetoothManager.hpp" -#include "BluetoothException.hpp" - -using namespace tinyb; - -void BluetoothNotificationHandler::on_properties_changed_device(GDBusProxy *proxy, GVariant *changed_properties, GStrv invalidated_properties, gpointer userdata) { - (void) proxy; - (void) invalidated_properties; - - auto c = static_cast<BluetoothDevice*>(userdata); - - if (!c->lock()) - return; - - if(g_variant_n_children(changed_properties) > 0) { - GVariantIter *iter = NULL; - - GVariant *value; - const gchar *key; - g_variant_get(changed_properties, "a{sv}", &iter); - while (iter != nullptr && g_variant_iter_loop(iter, "{&sv}", &key, &value)) { - auto rssi_callback = c->rssi_callback; - if (rssi_callback != nullptr && g_ascii_strncasecmp(key, "rssi", 5) == 0) { - int16_t new_value; - g_variant_get(value, "n", &new_value); - rssi_callback(new_value); - continue; - } - auto blocked_callback = c->blocked_callback; - if (blocked_callback != nullptr && g_ascii_strncasecmp(key, "blocked", 8) == 0) { - bool new_value; - g_variant_get(value, "b", &new_value); - blocked_callback(new_value); - continue; - } - auto trusted_callback = c->trusted_callback; - if (trusted_callback != nullptr && g_ascii_strncasecmp(key, "trusted", 8) == 0) { - bool new_value; - g_variant_get(value, "b", &new_value); - trusted_callback(new_value); - continue; - } - auto paired_callback = c->paired_callback; - if (paired_callback != nullptr && g_ascii_strncasecmp(key, "paired", 7) == 0) { - bool new_value; - g_variant_get(value, "b", &new_value); - paired_callback(new_value); - continue; - } - auto connected_callback = c->connected_callback; - if (connected_callback != nullptr && g_ascii_strncasecmp(key, "connected", 10) == 0) { - bool new_value; - g_variant_get(value, "b", &new_value); - 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); - } - - c->unlock(); -} - -std::string BluetoothDevice::get_class_name() const -{ - return std::string("BluetoothDevice"); -} - -std::string BluetoothDevice::get_java_class() const -{ - return std::string(JAVA_DBUS_PACKAGE "/DBusDevice"); -} - -std::string BluetoothDevice::get_object_path() const -{ - return std::string(g_dbus_proxy_get_object_path(G_DBUS_PROXY(object))); -} - -BluetoothType BluetoothDevice::get_bluetooth_type() const -{ - return BluetoothType::DEVICE; -} - -BluetoothDevice::BluetoothDevice(Device1 *object_) -{ - this->object = object_; - g_object_ref(object_); - - g_signal_connect(G_DBUS_PROXY(object_), "g-properties-changed", - G_CALLBACK(BluetoothNotificationHandler::on_properties_changed_device), this); - valid = true; -} - -BluetoothDevice::BluetoothDevice(const BluetoothDevice &object_) -{ - BluetoothDevice(object_.object); -} - -BluetoothDevice::~BluetoothDevice() -{ - valid = false; - g_signal_handlers_disconnect_by_data(object, this); - lk.lock(); - - g_object_unref(object); -} - -std::unique_ptr<BluetoothDevice> BluetoothDevice::make(Object *object, - BluetoothType type, std::string *name, std::string *identifier, - BluetoothObject *parent) -{ - Device1 *device; - if((type == BluetoothType::NONE || type == BluetoothType::DEVICE) && - (device = object_get_device1(object)) != NULL) { - - std::unique_ptr<BluetoothDevice> p(new BluetoothDevice(device)); - g_object_unref(device); - - if ((name == nullptr || *name == p->get_name()) && - (identifier == nullptr || *identifier == p->get_address()) && - (parent == nullptr || *parent == p->get_adapter())) - return p; - } - - return std::unique_ptr<BluetoothDevice>(); -} - -BluetoothDevice *BluetoothDevice::clone() const -{ - return new BluetoothDevice(object); -} - -std::vector<std::unique_ptr<BluetoothGattService>> BluetoothDevice::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 *object2 = OBJECT(l->data); - - auto p = BluetoothGattService::make(object2, - BluetoothType::GATT_SERVICE, NULL, NULL, this); - if (p != nullptr) - vector.push_back(std::move(p)); - } - g_list_free_full(objects, g_object_unref); - - return vector; -} - -/* D-Bus method calls: */ -bool BluetoothDevice::disconnect () -{ - GError *error = NULL; - bool result; - result = device1_call_disconnect_sync( - object, - NULL, - &error - ); - handle_error(error); - return result; -} - -void BluetoothDevice::connect_async_start () -{ - device1_call_connect ( - object, // Device1 *proxy, - NULL, // GCancellable *cancellable, - NULL, // GAsyncReadyCallback callback, - NULL // gpointer user_data) - ); -} - -bool BluetoothDevice::connect_async_finish () -{ - GError *error = NULL; - bool result; - result = device1_call_connect_finish ( - object, // Device1 *proxy, - NULL, // GAsyncResult *res, - &error // GError **error) - ); - handle_error(error); - return result; -} - -bool BluetoothDevice::connect () -{ - GError *error = NULL; - bool result; - result = device1_call_connect_sync( - object, - NULL, - &error - ); - handle_error(error); - return result; -} - -bool BluetoothDevice::connect_profile ( - const std::string &arg_UUID) -{ - GError *error = NULL; - bool result; - result = device1_call_connect_profile_sync( - object, - arg_UUID.c_str(), - NULL, - &error - ); - handle_error(error); - return result; -} - -bool BluetoothDevice::disconnect_profile ( - const std::string &arg_UUID) -{ - GError *error = NULL; - bool result; - result = device1_call_disconnect_profile_sync( - object, - arg_UUID.c_str(), - NULL, - &error - ); - handle_error(error); - return result; -} - -bool BluetoothDevice::pair () -{ - GError *error = NULL; - bool result; - result = device1_call_pair_sync( - object, - NULL, - &error - ); - handle_error(error); - return result; -} - -// Remove the device (like an unpair) -bool BluetoothDevice::remove_device(){ - BluetoothAdapter ba = get_adapter(); - return ba.remove_device(get_object_path()); -} - -bool BluetoothDevice::cancel_pairing () -{ - GError *error = NULL; - bool result; - result = device1_call_cancel_pairing_sync( - object, - NULL, - &error - ); - handle_error(error); - return result; -} - - - -/* D-Bus property accessors: */ -std::string BluetoothDevice::get_address () -{ - return std::string(device1_get_address (object)); -} - -std::string BluetoothDevice::get_name () -{ - const gchar *name = device1_get_name(object); - if (name == nullptr) - return std::string(device1_get_alias(object)); - return std::string(name); -} - -std::string BluetoothDevice::get_alias () -{ - return device1_get_alias (object); -} - -void BluetoothDevice::set_alias (const std::string &value) -{ - device1_set_alias (object, value.c_str()); -} - -unsigned int BluetoothDevice::get_class () -{ - return device1_get_class (object); -} - -uint16_t BluetoothDevice::get_appearance () -{ - return device1_get_appearance (object); -} - -std::unique_ptr<std::string> BluetoothDevice::get_icon () -{ - const gchar *icon = device1_get_icon (object); - if (icon == nullptr) - return std::unique_ptr<std::string>(); - return std::unique_ptr<std::string>(new std::string(icon)); -} - -bool BluetoothDevice::get_paired () -{ - return device1_get_paired (object); -} - -void BluetoothDevice::enable_paired_notifications( - std::function<void(BluetoothDevice &, bool, void *)> callback, - void *userdata) { - paired_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); -} -void BluetoothDevice::enable_paired_notifications( - std::function<void(bool)> callback) { - paired_callback = callback; -} -void BluetoothDevice::disable_paired_notifications() { - paired_callback = nullptr; -} - -bool BluetoothDevice::get_trusted () -{ - return device1_get_trusted (object); -} - -void BluetoothDevice::set_trusted (bool value) -{ - device1_set_trusted (object, value); -} - -void BluetoothDevice::enable_trusted_notifications( - std::function<void(BluetoothDevice &, bool, void *)> callback, - void *userdata) { - trusted_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); -} -void BluetoothDevice::enable_trusted_notifications( - std::function<void(bool)> callback) { - trusted_callback = callback; -} -void BluetoothDevice::disable_trusted_notifications() { - trusted_callback = nullptr; -} - -bool BluetoothDevice::get_blocked () -{ - return device1_get_blocked (object); -} - -void BluetoothDevice::set_blocked (bool value) -{ - device1_set_blocked (object, value); -} - -void BluetoothDevice::enable_blocked_notifications( - std::function<void(BluetoothDevice &, bool, void *)> callback, - void *userdata) { - blocked_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); -} -void BluetoothDevice::enable_blocked_notifications( - std::function<void(bool)> callback) { - blocked_callback = callback; -} -void BluetoothDevice::disable_blocked_notifications() { - blocked_callback = nullptr; -} - -bool BluetoothDevice::get_legacy_pairing () -{ - return device1_get_legacy_pairing (object); -} - -int16_t BluetoothDevice::get_rssi () -{ - return device1_get_rssi (object); -} - -void BluetoothDevice::enable_rssi_notifications( - std::function<void(BluetoothDevice &, int16_t, void *)> callback, - void *userdata) { - rssi_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); -} -void BluetoothDevice::enable_rssi_notifications( - std::function<void(int16_t)> callback) { - rssi_callback = callback; -} -void BluetoothDevice::disable_rssi_notifications() { - rssi_callback = nullptr; -} - -bool BluetoothDevice::get_connected () -{ - return device1_get_connected (object); -} - -void BluetoothDevice::enable_connected_notifications( - std::function<void(BluetoothDevice &, bool, void *)> callback, - void *userdata) { - connected_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); -} -void BluetoothDevice::enable_connected_notifications( - std::function<void(bool)> callback) { - connected_callback = callback; -} -void BluetoothDevice::disable_connected_notifications() { - connected_callback = nullptr; -} - -std::vector<std::string> BluetoothDevice::get_uuids () -{ - - const char * const *uuids_c = device1_get_uuids (object); - std::vector<std::string> uuids; - for (int i = 0; uuids_c[i] != NULL ;i++) - uuids.push_back(std::string(uuids_c[i])); - return uuids; -} - -std::unique_ptr<std::string> BluetoothDevice::get_modalias () -{ - const gchar *modalias= device1_get_modalias (object); - if (modalias == nullptr) - return std::unique_ptr<std::string>(); - return std::unique_ptr<std::string>(new std::string(modalias)); -} - -BluetoothAdapter BluetoothDevice::get_adapter () -{ - GError *error = NULL; - - Adapter1 *adapter = adapter1_proxy_new_for_bus_sync( - G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, - "org.bluez", - device1_get_adapter (object), - NULL, - &error); - - if (adapter == NULL) { - std::string error_msg("Error occured while instantiating adapter: "); - error_msg += error->message; - g_error_free(error); - throw BluetoothException(error_msg); - } - - auto res = BluetoothAdapter(adapter); - g_object_unref(adapter); - return res; -} - -std::map<uint16_t, std::vector<uint8_t>> BluetoothDevice::get_manufacturer_data() -{ - std::map<uint16_t, std::vector<uint8_t>> m_data; - GVariant *v = device1_dup_manufacturer_data (object); - - if (v == nullptr) - return m_data; - - GVariantIter *iter; - g_variant_get (v, "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)) { - m_data[key].push_back(val); - } - } - - g_variant_iter_free(iter); - g_variant_unref(v); - - 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; - GVariant *v = device1_dup_service_data (object); - - if (v == nullptr) - return m_data; - - GVariantIter *iter; - g_variant_get (v, "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)) { - m_data[key].push_back(val); - } - } - - g_variant_iter_free(iter); - g_variant_unref(v); - - 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); -} - -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; -} - diff --git a/src/tinyb/BluetoothEvent.cpp b/src/tinyb/BluetoothEvent.cpp deleted file mode 100644 index a92fa851..00000000 --- a/src/tinyb/BluetoothEvent.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * Copyright (c) 2016 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 "BluetoothEvent.hpp" -#include "BluetoothManager.hpp" - -void BluetoothEvent::generic_callback(BluetoothObject &object, void *data) -{ - - if (data == nullptr) - return; - - BluetoothConditionVariable *generic_data = static_cast<BluetoothConditionVariable *>(data); - - generic_data->result = object.clone(); - generic_data->notify(); -} - -BluetoothEvent::BluetoothEvent(BluetoothType type_, std::string *name_, - std::string *identifier_, BluetoothObject *parent_, bool execute_once_, - BluetoothCallback cb_, void *data_) -{ - canceled = false; - this->type = type_; - if (name_ != nullptr) - this->name = new std::string(*name_); - else - this->name = nullptr; - - if (identifier_ != nullptr) - this->identifier = new std::string(*identifier_); - else - this->identifier = nullptr; - - if (parent_ != nullptr) - this->parent = parent_->clone(); - else - this->parent = nullptr; - - this->execute_once = execute_once_; - - if (cb_ == nullptr) { - this->data = static_cast<void *>(&cv); - this->cb = generic_callback; - } - else { - this->cb = cb_; - this->data = data_; - } -} - -bool BluetoothEvent::execute_callback(BluetoothObject &object) -{ - if (has_callback()) { - cb(object, data); - cv.notify(); - return execute_once; - } - - return true; -} - -void BluetoothEvent::wait(std::chrono::milliseconds timeout) -{ - if (!canceled && execute_once == true) { - if (timeout == std::chrono::milliseconds::zero()) - cv.wait(); - else - cv.wait_for(timeout); - } -} - -void BluetoothEvent::cancel() -{ - BluetoothManager *manager = BluetoothManager::get_bluetooth_manager(); - manager->remove_event(*this); - - cv.notify(); -} - -BluetoothEvent::~BluetoothEvent() -{ - if (name != nullptr) - delete name; - if (identifier != nullptr) - delete identifier; - if (parent != nullptr) - delete parent; -} diff --git a/src/tinyb/BluetoothGattCharacteristic.cpp b/src/tinyb/BluetoothGattCharacteristic.cpp deleted file mode 100644 index b051cba6..00000000 --- a/src/tinyb/BluetoothGattCharacteristic.cpp +++ /dev/null @@ -1,325 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * 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 "tinyb_utils.hpp" -#include "BluetoothNotificationHandler.hpp" -#include "BluetoothGattCharacteristic.hpp" -#include "BluetoothGattService.hpp" -#include "BluetoothGattDescriptor.hpp" -#include "BluetoothException.hpp" - -using namespace tinyb; - -void BluetoothNotificationHandler::on_properties_changed_characteristic(GDBusProxy *proxy, GVariant *changed_properties, GStrv invalidated_properties, gpointer userdata) { - (void) proxy; - (void) invalidated_properties; - auto c = static_cast<BluetoothGattCharacteristic*>(userdata); - - if(g_variant_n_children(changed_properties) > 0) { - GVariantIter *iter = NULL; - - GVariant *value; - const gchar *key; - g_variant_get(changed_properties, "a{sv}", &iter); - while (iter != nullptr && g_variant_iter_loop(iter, "{&sv}", &key, &value)) { - auto value_callback = c->value_changed_callback; - if (value_callback != nullptr && g_ascii_strncasecmp(key, "value", 5) == 0) { - std::vector<unsigned char> new_value = from_iter_to_vector(value); - value_callback(new_value); - } - } - g_variant_iter_free (iter); - } -} - -std::string BluetoothGattCharacteristic::get_class_name() const -{ - return std::string("BluetoothGattCharacteristic"); -} - -std::string BluetoothGattCharacteristic::get_java_class() const -{ - return std::string(JAVA_DBUS_PACKAGE "/DBusGattCharacteristic"); -} - -std::string BluetoothGattCharacteristic::get_object_path() const -{ - return std::string(g_dbus_proxy_get_object_path(G_DBUS_PROXY(object))); -} - -BluetoothType BluetoothGattCharacteristic::get_bluetooth_type() const -{ - return BluetoothType::GATT_CHARACTERISTIC; -} - -BluetoothGattCharacteristic::BluetoothGattCharacteristic(GattCharacteristic1 *object_) -{ - this->object = object_; - g_object_ref(object_); - - g_signal_connect(G_DBUS_PROXY(object_), "g-properties-changed", - G_CALLBACK(BluetoothNotificationHandler::on_properties_changed_characteristic), this); -} - -BluetoothGattCharacteristic::BluetoothGattCharacteristic(const BluetoothGattCharacteristic &object_) -{ - BluetoothGattCharacteristic(object_.object); - -} - -BluetoothGattCharacteristic::~BluetoothGattCharacteristic() -{ - g_signal_handlers_disconnect_by_data(object, this); - g_object_unref(object); -} - -BluetoothGattCharacteristic *BluetoothGattCharacteristic::clone() const -{ - return new BluetoothGattCharacteristic(object); -} - -std::unique_ptr<BluetoothGattCharacteristic> BluetoothGattCharacteristic::make( - Object *object, BluetoothType type, std::string *name, - std::string *identifier, BluetoothObject *parent) -{ - GattCharacteristic1 *characteristic; - if((type == BluetoothType::NONE || type == BluetoothType::GATT_CHARACTERISTIC) && - (characteristic = object_get_gatt_characteristic1(object)) != NULL) { - - std::unique_ptr<BluetoothGattCharacteristic> p( - new BluetoothGattCharacteristic(characteristic)); - g_object_unref(characteristic); - - if ((name == nullptr) && - (identifier == nullptr || *identifier == p->get_uuid()) && - (parent == nullptr || *parent == p->get_service())) - return p; - } - - return std::unique_ptr<BluetoothGattCharacteristic>(); -} - -/* D-Bus method calls: */ -std::vector<unsigned char> BluetoothGattCharacteristic::read_value (uint16_t offset) -{ - GError *error = NULL; - GVariant *result_variant; - - GVariantDict dict; - g_variant_dict_init(&dict, NULL); - - if (offset != 0) - g_variant_dict_insert_value(&dict, "offset", g_variant_new_uint16(offset)); - - GVariant *variant = g_variant_dict_end(&dict); - - gatt_characteristic1_call_read_value_sync( - object, - variant, - &result_variant, - NULL, - &error - ); - - handle_error(error); - - GBytes *result_gbytes = g_variant_get_data_as_bytes(result_variant); - std::vector<unsigned char> result = from_gbytes_to_vector(result_gbytes); - - /* free the gbytes array */ - g_bytes_unref(result_gbytes); - - return result; -} - -bool BluetoothGattCharacteristic::write_value ( - const std::vector<unsigned char> &arg_value, uint16_t offset) -{ - GError *error = NULL; - bool result = true; - - gboolean trusted = true; - GBytes *arg_value_gbytes = from_vector_to_gbytes(arg_value); - GVariant *value = g_variant_new_from_bytes( - G_VARIANT_TYPE_BYTESTRING, arg_value_gbytes, trusted); - - GVariantDict dict; - g_variant_dict_init(&dict, NULL); - - if (offset != 0) - g_variant_dict_insert_value(&dict, "offset", g_variant_new_uint16(offset)); - - GVariant *variant = g_variant_dict_end(&dict); - - result = gatt_characteristic1_call_write_value_sync( - object, - value, - variant, - NULL, - &error - ); - - /* freeing the GBytes allocated inside from_vector_to_gbytes function */ - g_bytes_unref(arg_value_gbytes); - - handle_error(error); - - return result; -} - -bool BluetoothGattCharacteristic::enable_value_notifications( - std::function<void(BluetoothGattCharacteristic &, std::vector<unsigned char> &,void *)> callback, - void *userdata) -{ - value_changed_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); - start_notify(); - return true; -} - -bool BluetoothGattCharacteristic::enable_value_notifications( - std::function<void(std::vector<unsigned char> &)> callback) -{ - value_changed_callback = callback; - start_notify(); - return true; -} - -bool BluetoothGattCharacteristic::disable_value_notifications() -{ - stop_notify(); - value_changed_callback = nullptr; - return true; -} - - -bool BluetoothGattCharacteristic::start_notify () -{ - GError *error = NULL; - bool result; - result = gatt_characteristic1_call_start_notify_sync( - object, - NULL, - &error - ); - - handle_error(error); - return result; -} - -bool BluetoothGattCharacteristic::stop_notify () -{ - GError *error = NULL; - bool result; - result = gatt_characteristic1_call_stop_notify_sync( - object, - NULL, - &error - ); - handle_error(error); - return result; -} - -/* D-Bus property accessors: */ -std::string BluetoothGattCharacteristic::get_uuid () -{ - return std::string(gatt_characteristic1_get_uuid (object)); -} - -BluetoothGattService BluetoothGattCharacteristic::get_service () -{ - GError *error = NULL; - - GattService1 *service = gatt_service1_proxy_new_for_bus_sync( - G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, - "org.bluez", - gatt_characteristic1_get_service (object), - NULL, - &error); - - if (service == nullptr) { - std::string error_msg("Error occured while instantiating service: "); - error_msg += error->message; - g_error_free(error); - throw BluetoothException(error_msg); - } - - auto res = BluetoothGattService(service); - g_object_unref(service); - return res; -} - -std::vector<unsigned char> BluetoothGattCharacteristic::get_value () -{ - GVariant *value_variant = gatt_characteristic1_get_value (object); - GBytes *value_gbytes = g_variant_get_data_as_bytes(value_variant); - std::vector<unsigned char> result; - - try { - result = from_gbytes_to_vector(value_gbytes); - } catch (std::exception &e) { - g_bytes_unref(value_gbytes); - throw e; - } - - g_bytes_unref(value_gbytes); - - return result; -} - -bool BluetoothGattCharacteristic::get_notifying () -{ - return gatt_characteristic1_get_notifying (object); -} - -std::vector<std::string> BluetoothGattCharacteristic::get_flags () -{ - const char * const *flags_c = gatt_characteristic1_get_flags (object); - std::vector<std::string> flags; - for (int i = 0; flags_c[i] != NULL ;i++) - flags.push_back(std::string(flags_c[i])); - return flags; - -} - -std::vector<std::unique_ptr<BluetoothGattDescriptor>> BluetoothGattCharacteristic::get_descriptors () -{ - std::vector<std::unique_ptr<BluetoothGattDescriptor>> vector; - GList *l, *objects = g_dbus_object_manager_get_objects(gdbus_manager); - - for (l = objects; l != NULL; l = l->next) { - Object *object2 = OBJECT(l->data); - - auto p = BluetoothGattDescriptor::make(object2, - BluetoothType::GATT_DESCRIPTOR, NULL, NULL, this); - if (p != nullptr) - vector.push_back(std::move(p)); - } - g_list_free_full(objects, g_object_unref); - - return vector; -} - - diff --git a/src/tinyb/BluetoothGattDescriptor.cpp b/src/tinyb/BluetoothGattDescriptor.cpp deleted file mode 100644 index 48217114..00000000 --- a/src/tinyb/BluetoothGattDescriptor.cpp +++ /dev/null @@ -1,245 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * 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 "tinyb_utils.hpp" -#include "BluetoothNotificationHandler.hpp" -#include "BluetoothGattDescriptor.hpp" -#include "BluetoothGattCharacteristic.hpp" -#include "BluetoothException.hpp" - -using namespace tinyb; - -void BluetoothNotificationHandler::on_properties_changed_descriptor(GDBusProxy *proxy, GVariant *changed_properties, GStrv invalidated_properties, gpointer userdata) { - (void) proxy; - (void) invalidated_properties; - auto c = static_cast<BluetoothGattDescriptor*>(userdata); - - if(g_variant_n_children(changed_properties) > 0) { - GVariantIter *iter = NULL; - - GVariant *value; - const gchar *key; - g_variant_get(changed_properties, "a{sv}", &iter); - while (iter != nullptr && g_variant_iter_loop(iter, "{&sv}", &key, &value)) { - auto value_callback = c->value_changed_callback; - if (value_callback != nullptr && g_ascii_strncasecmp(key, "value", 5) == 0) { - std::vector<unsigned char> new_value = from_iter_to_vector(value); - value_callback(new_value); - } - } - g_variant_iter_free (iter); - } -} - -std::string BluetoothGattDescriptor::get_class_name() const -{ - return std::string("BluetoothGattDescriptor"); -} - -std::string BluetoothGattDescriptor::get_java_class() const -{ - return std::string(JAVA_DBUS_PACKAGE "/DBusGattDescriptor"); -} - -std::string BluetoothGattDescriptor::get_object_path() const -{ - return std::string(g_dbus_proxy_get_object_path(G_DBUS_PROXY(object))); -} - -BluetoothType BluetoothGattDescriptor::get_bluetooth_type() const -{ - return BluetoothType::GATT_DESCRIPTOR; -} - -BluetoothGattDescriptor::BluetoothGattDescriptor(GattDescriptor1 *object_) -{ - this->object = object_; - g_object_ref(object_); - - g_signal_connect(G_DBUS_PROXY(object_), "g-properties-changed", - G_CALLBACK(BluetoothNotificationHandler::on_properties_changed_descriptor), this); - valid = true; -} - -BluetoothGattDescriptor::BluetoothGattDescriptor(const BluetoothGattDescriptor &object_) -{ - BluetoothGattDescriptor(object_.object); -} - -BluetoothGattDescriptor::~BluetoothGattDescriptor() -{ - valid = false; - g_signal_handlers_disconnect_by_data(object, this); - lk.lock(); - - g_object_unref(object); -} - -std::unique_ptr<BluetoothGattDescriptor> BluetoothGattDescriptor::make( - Object *object, BluetoothType type, std::string *name, - std::string *identifier, BluetoothObject *parent) -{ - GattDescriptor1 *descriptor; - if((type == BluetoothType::NONE || type == BluetoothType::GATT_DESCRIPTOR) && - (descriptor = object_get_gatt_descriptor1(object)) != NULL) { - - std::unique_ptr<BluetoothGattDescriptor> p( - new BluetoothGattDescriptor(descriptor)); - g_object_unref(descriptor); - - if ((name == nullptr) && - (identifier == nullptr || *identifier == p->get_uuid()) && - (parent == nullptr || *parent == p->get_characteristic())) - return p; - } - - return std::unique_ptr<BluetoothGattDescriptor>(); -} - - - -BluetoothGattDescriptor *BluetoothGattDescriptor::clone() const -{ - return new BluetoothGattDescriptor(object); -} - -/* D-Bus method calls: */ -std::vector<unsigned char> BluetoothGattDescriptor::read_value (uint16_t offset) -{ - GError *error = NULL; - gchar *result_chars; - - GVariantDict dict; - g_variant_dict_init(&dict, NULL); - - if (offset != 0) - g_variant_dict_insert_value(&dict, "offset", g_variant_new_uint16(offset)); - - GVariant *variant = g_variant_dict_end(&dict); - - gatt_descriptor1_call_read_value_sync( - object, - variant, - &result_chars, - NULL, - &error - ); - handle_error(error); - - std::vector<unsigned char> result = from_chars_to_vector(result_chars); - - g_free(result_chars); - - return result; -} - -bool BluetoothGattDescriptor::write_value ( - const std::vector<unsigned char> &arg_value, uint16_t offset) -{ - GError *error = NULL; - bool result; - - gchar *arg_value_chars = const_cast<gchar*>(reinterpret_cast<const gchar*>(arg_value.data())); - - GVariantDict dict; - g_variant_dict_init(&dict, NULL); - - if (offset != 0) - g_variant_dict_insert_value(&dict, "offset", g_variant_new_uint16(offset)); - - GVariant *variant = g_variant_dict_end(&dict); - - result = gatt_descriptor1_call_write_value_sync( - object, - arg_value_chars, - variant, - NULL, - &error - ); - handle_error(error); - - return result; -} - -bool BluetoothGattDescriptor::enable_value_notifications( - std::function<void(BluetoothGattDescriptor &, std::vector<unsigned char> &,void *)> callback, - void *userdata) -{ - value_changed_callback = std::bind(callback, std::ref(*this), std::placeholders::_1, userdata); - return true; -} - -bool BluetoothGattDescriptor::enable_value_notifications( - std::function<void(std::vector<unsigned char> &)> callback) -{ - value_changed_callback = callback; - return true; -} - -bool BluetoothGattDescriptor::disable_value_notifications() -{ - value_changed_callback = nullptr; - return true; -} - -/* D-Bus property accessors: */ -std::string BluetoothGattDescriptor::get_uuid () -{ - return std::string(gatt_descriptor1_get_uuid (object)); -} - -BluetoothGattCharacteristic BluetoothGattDescriptor::get_characteristic () -{ - GError *error = NULL; - - GattCharacteristic1* characteristic = gatt_characteristic1_proxy_new_for_bus_sync( - G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, - "org.bluez", - gatt_descriptor1_get_characteristic (object), - NULL, - &error); - - if (characteristic == NULL) { - std::string error_msg("Error occured while instantiating characteristic: "); - error_msg += error->message; - g_error_free(error); - throw BluetoothException(error_msg); - } - - auto res = BluetoothGattCharacteristic(characteristic); - g_object_unref(characteristic); - return res; -} - -std::vector<unsigned char> BluetoothGattDescriptor::get_value () -{ - gchar *value_chars = const_cast<gchar *>(gatt_descriptor1_get_value (object)); - std::vector<unsigned char> result; - - result = from_chars_to_vector(value_chars); - - return result; -} diff --git a/src/tinyb/BluetoothGattService.cpp b/src/tinyb/BluetoothGattService.cpp deleted file mode 100644 index 0897ef4c..00000000 --- a/src/tinyb/BluetoothGattService.cpp +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * 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 "tinyb_utils.hpp" -#include "BluetoothGattService.hpp" -#include "BluetoothGattCharacteristic.hpp" -#include "BluetoothDevice.hpp" -#include "BluetoothException.hpp" - -using namespace tinyb; - -std::string BluetoothGattService::get_class_name() const -{ - return std::string("BluetoothGattService"); -} - -std::string BluetoothGattService::get_java_class() const -{ - return std::string(JAVA_DBUS_PACKAGE "/DBusGattService"); -} - -std::string BluetoothGattService::get_object_path() const -{ - return std::string(g_dbus_proxy_get_object_path(G_DBUS_PROXY(object))); -} - -BluetoothType BluetoothGattService::get_bluetooth_type() const -{ - return BluetoothType::GATT_SERVICE; -} - -BluetoothGattService::BluetoothGattService(GattService1 *object_) -{ - this->object = object_; - g_object_ref(object_); -} - -BluetoothGattService::BluetoothGattService(const BluetoothGattService &object_) -{ - BluetoothGattService(object_.object); -} - -BluetoothGattService::~BluetoothGattService() -{ - g_object_unref(object); -} - -std::unique_ptr<BluetoothGattService> BluetoothGattService::make( - Object *object, BluetoothType type, std::string *name, - std::string *identifier, BluetoothObject *parent) -{ - GattService1 *service; - if((type == BluetoothType::NONE || type == BluetoothType::GATT_SERVICE) && - (service = object_get_gatt_service1(object)) != NULL) { - - std::unique_ptr<BluetoothGattService> p( - new BluetoothGattService(service)); - g_object_unref(service); - - if ((name == nullptr) && - (identifier == nullptr || *identifier == p->get_uuid()) && - (parent == nullptr || *parent == p->get_device())) - return p; - } - - return std::unique_ptr<BluetoothGattService>(); -} - -BluetoothGattService *BluetoothGattService::clone() const -{ - return new BluetoothGattService(object); -} - -/* D-Bus property accessors: */ -std::string BluetoothGattService::get_uuid () -{ - return std::string(gatt_service1_get_uuid (object)); -} - -BluetoothDevice BluetoothGattService::get_device () -{ - GError *error = NULL; - - Device1 *device = device1_proxy_new_for_bus_sync( - G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, - "org.bluez", - gatt_service1_get_device (object), - NULL, - &error); - - if (device == nullptr) { - std::string error_msg("Error occured while instantiating device: "); - error_msg += error->message; - g_error_free(error); - throw BluetoothException(error_msg); - } - - auto res = BluetoothDevice(device); - g_object_unref(device); - return res; -} - -bool BluetoothGattService::get_primary () -{ - return gatt_service1_get_primary (object); -} - -std::vector<std::unique_ptr<BluetoothGattCharacteristic>> BluetoothGattService::get_characteristics () -{ - std::vector<std::unique_ptr<BluetoothGattCharacteristic>> vector; - GList *l, *objects = g_dbus_object_manager_get_objects(gdbus_manager); - - for (l = objects; l != NULL; l = l->next) { - Object *object2 = OBJECT(l->data); - - auto p = BluetoothGattCharacteristic::make(object2, - BluetoothType::GATT_CHARACTERISTIC, NULL, NULL, this); - if (p != nullptr) - vector.push_back(std::move(p)); - } - g_list_free_full(objects, g_object_unref); - - return vector; -} - diff --git a/src/tinyb/BluetoothManager.cpp b/src/tinyb/BluetoothManager.cpp deleted file mode 100644 index d4637308..00000000 --- a/src/tinyb/BluetoothManager.cpp +++ /dev/null @@ -1,420 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * 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; -} diff --git a/src/tinyb/BluetoothObject.cpp b/src/tinyb/BluetoothObject.cpp deleted file mode 100644 index 2e8ddaf4..00000000 --- a/src/tinyb/BluetoothObject.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Author: Petre Eftime <[email protected]> - * 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 "BluetoothObject.hpp" -#include <glib.h> - -using namespace tinyb; - -std::string BluetoothObject::get_java_class() const -{ - return std::string(JAVA_DBUS_PACKAGE "/DBusObject"); -} - -std::string BluetoothObject::get_class_name() const -{ - return std::string("BluetoothObject"); -} - -std::string BluetoothObject::get_object_path() const -{ - return std::string(); -} - -BluetoothType BluetoothObject::get_bluetooth_type() const -{ - return BluetoothType::NONE; -} - -BluetoothObject *BluetoothObject::clone() const -{ - return NULL; -} - -bool BluetoothObject::operator==(const BluetoothObject &other) const -{ - return (this->get_bluetooth_type() == other.get_bluetooth_type()) - && (this->get_object_path() == other.get_object_path()); -} - -bool BluetoothObject::operator!=(const BluetoothObject &other) const -{ - return !(*this == other); -} diff --git a/src/tinyb/BluetoothUUID.cpp b/src/tinyb/BluetoothUUID.cpp deleted file mode 100644 index 6d25e19e..00000000 --- a/src/tinyb/BluetoothUUID.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#include "BluetoothUUID.hpp" - -#include <cinttypes> -#include <cstring> -#include <iostream> - -using namespace tinyb; - -BluetoothUUID::BluetoothUUID(const BluetoothUUID &other) { - uuid[0] = other.uuid[0]; - uuid[1] = other.uuid[1]; -} - -BluetoothUUID::BluetoothUUID(const char str[]) { - int len = strlen(str); - const char *err_msg = "UUID does not have a valid format"; - - if (len == 4 || len == 8) { - /* 16bit or 32bit UUID: number + base UUID */ - const uint64_t strval = strtoul(str, NULL, 16); - uuid[0] = strval << 32 | 0x00001000ULL; - uuid[1] = 0x800000805f9b34fbULL; - } else if (len == 36) { - /* 128bit UUID */ - char u[37]; - strcpy(u, str); - - if (u[8] == '-') { - u[8] = ' '; - const uint64_t uval = strtoul(u + 0, NULL, 16); - uuid[0] = uval << 32; - } else { - throw std::invalid_argument(err_msg); - } - if (u[13] == '-') { - u[13] = ' '; - uuid[0] = uuid[0] | strtoul(u + 9, NULL, 16) << 16; - } else throw std::invalid_argument(err_msg); - if (u[18] == '-') { - u[18] = ' '; - uuid[0] = uuid[0] | strtoul(u + 14, NULL, 16); - } else throw std::invalid_argument(err_msg); - - if (u[23] == '-') { - u[23] = ' '; - const uint64_t uval = strtoul(u + 19, NULL, 16); - uuid[1] = uval << 48; - } else throw std::invalid_argument(err_msg); - - uuid[1] = uuid[1] | strtoul(u + 24, NULL, 16); - } else throw std::invalid_argument(err_msg); -} - -BluetoothUUID::BluetoothUUID(const std::string &str) : BluetoothUUID(str.c_str()) {} - -std::string BluetoothUUID::get_string() -{ - char u[37]; - snprintf(u, 37, "%08" PRIx64 "-%04" PRIx64 "-%04" PRIx64 "-%04" PRIx64 "-%012" PRIx64, - (uint64_t)(uuid[0] >> 32), - (uint64_t)((uuid[0] >> 16) & 0xFFFFULL), - (uint64_t)(uuid[0] & 0xFFFFULL), - (uint64_t)(uuid[1] >> 48), - (uint64_t)(uuid[1] & ~(0xFFFFULL << 48))); - return std::string(u); -} - -std::string BluetoothUUID::get_short_string() -{ - char u[12]; - if (is_short()) { - uint32_t suuid = get_short(); - if ( ( suuid & 0xFFFF ) == suuid ) - snprintf(u, sizeof(u), "%04ux", suuid); - else - snprintf(u, sizeof(u), "%08ux", suuid); - return std::string(u); - } else { - return get_string(); - } -} - -uint32_t BluetoothUUID::get_short() { - if (is_short()) - return uuid[0] >> 32; - return 0; -} - -bool BluetoothUUID::is_short() -{ - if ( ( uuid[1] == 0x800000805f9b34fbULL ) && uuid[0] & ( 0xffffffffULL == 0x00001000ULL ) ) - return true; - return false; -} diff --git a/src/tinyb/CMakeLists.txt b/src/tinyb/CMakeLists.txt deleted file mode 100644 index d87a1aab..00000000 --- a/src/tinyb/CMakeLists.txt +++ /dev/null @@ -1,88 +0,0 @@ -set (tinyb_LIB_INCLUDE_DIRS - ${PROJECT_SOURCE_DIR}/jaulib/include - ${PROJECT_SOURCE_DIR}/api - ${PROJECT_SOURCE_DIR}/api/tinyb - ${PROJECT_SOURCE_DIR}/include -) - -include_directories( - ${tinyb_LIB_INCLUDE_DIRS} - ${GLIB2_INCLUDE_DIRS} - ${GIO_INCLUDE_DIRS} - ${GIO-UNIX_INCLUDE_DIRS} - ${CMAKE_CURRENT_BINARY_DIR} -) - -set (tinyb_LIB_SRCS - ${PROJECT_SOURCE_DIR}/jaulib/src/dfa_utf8_decode.cpp - ${PROJECT_SOURCE_DIR}/jaulib/src/environment.cpp - ${PROJECT_SOURCE_DIR}/jaulib/src/debug.cpp - ${PROJECT_SOURCE_DIR}/jaulib/src/basic_types.cpp - ${PROJECT_SOURCE_DIR}/src/tinyb/BluetoothObject.cpp - ${PROJECT_SOURCE_DIR}/src/tinyb/BluetoothEvent.cpp - ${PROJECT_SOURCE_DIR}/src/tinyb/BluetoothManager.cpp - ${PROJECT_SOURCE_DIR}/src/tinyb/BluetoothAdapter.cpp - ${PROJECT_SOURCE_DIR}/src/tinyb/BluetoothDevice.cpp - ${PROJECT_SOURCE_DIR}/src/tinyb/BluetoothGattService.cpp - ${PROJECT_SOURCE_DIR}/src/tinyb/BluetoothGattCharacteristic.cpp - ${PROJECT_SOURCE_DIR}/src/tinyb/BluetoothGattDescriptor.cpp - ${PROJECT_SOURCE_DIR}/src/tinyb/BluetoothUUID.cpp - ${PROJECT_SOURCE_DIR}/src/tinyb/tinyb_utils.cpp -# autogenerated files - ${CMAKE_CURRENT_BINARY_DIR}/../version.c - ${CMAKE_CURRENT_BINARY_DIR}/orgbluez-dbus.c -) - -set (tinyb_LIB_GLOB_HEADERS - ${PROJECT_SOURCE_DIR}/api/tinyb.hpp -) - -find_program(GDBUS_CODEGEN NAMES gdbus-codegen) -if (NOT GDBUS_CODEGEN) - message(FATAL_ERROR "gdbus-codegen not found") -else () - message(STATUS "gdbus-codegen found") -endif () - -add_custom_command(OUTPUT orgbluez-dbus.c orgbluez-dbus.h - COMMAND ${GDBUS_CODEGEN} - --interface-prefix org.bluez - --generate-c-code orgbluez-dbus - --c-generate-object-manager ${PROJECT_SOURCE_DIR}/src/tinyb/org.bluez.xml - DEPENDS ${PROJECT_SOURCE_DIR}/src/tinyb/org.bluez.xml - COMMENT "Generating dbus code from org.bluez.xml") - -add_library (tinyb SHARED ${tinyb_LIB_SRCS}) -target_link_libraries ( - tinyb - unwind - ${CMAKE_THREAD_LIBS_INIT} - ${GLIB2_LIBRARIES} - ${GIO_LIBRARIES} -) - -if(USE_STRIP) -add_custom_command(TARGET tinyb POST_BUILD - COMMAND ${STRIP} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}tinyb${CMAKE_SHARED_LIBRARY_SUFFIX}.${tinyb_VERSION_STRING} - COMMENT "stripping tinyb" - VERBATIM - ) -endif(USE_STRIP) - -set_target_properties( - tinyb - PROPERTIES - SOVERSION ${tinyb_VERSION_MAJOR} - VERSION ${tinyb_VERSION_STRING} -) -install (FILES ${tinyb_LIB_GLOB_HEADERS} DESTINATION include/) -install (DIRECTORY ${PROJECT_SOURCE_DIR}/api/tinyb/ DESTINATION include/tinyb) - -macro (tinyb_CREATE_INSTALL_PKGCONFIG generated_file install_location) - configure_file (${generated_file}.cmake ${CMAKE_CURRENT_BINARY_DIR}/${generated_file} @ONLY) - install (FILES ${CMAKE_CURRENT_BINARY_DIR}/${generated_file} DESTINATION ${install_location}) -endmacro (tinyb_CREATE_INSTALL_PKGCONFIG) -tinyb_create_install_pkgconfig (tinyb.pc lib${LIB_SUFFIX}/pkgconfig) - -install(TARGETS tinyb LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) - diff --git a/src/tinyb/org.bluez.xml b/src/tinyb/org.bluez.xml deleted file mode 100644 index da200c6b..00000000 --- a/src/tinyb/org.bluez.xml +++ /dev/null @@ -1,195 +0,0 @@ -<?xml version="1.0"?> -<!-- -Author: Petre Eftime <[email protected]> -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. - -Author: Sven Gothel <[email protected]> -Copyright (c) 2020 Gothel Software e.K. - -Permission / License unchanged BSD style. - ---> -<node> - <interface name="org.bluez.Adapter1"> - <method name="StartDiscovery"/> - <method name="StopDiscovery"/> - <method name="RemoveDevice"> - <arg name="device" type="o" direction="in"/> - </method> - <method name="SetDiscoveryFilter"> - <arg name="filter" type="a{sv}" direction="in"/> - </method> - <method name="ConnectDevice"> - <arg name="value" type="o" direction="out"/> - <arg name="properties" type="a{sv}" direction="in"/> - </method> - <property name="Address" type="s" access="read"/> - <property name="Name" type="s" access="read"/> - <property name="Alias" type="s" access="readwrite"/> - <property name="Class" type="u" access="read"/> - <property name="Powered" type="b" access="readwrite"/> - <property name="Discoverable" type="b" access="readwrite"/> - <property name="DiscoverableTimeout" type="u" access="readwrite"/> - <property name="Pairable" type="b" access="readwrite"/> - <property name="PairableTimeout" type="u" access="readwrite"/> - <property name="Discovering" type="b" access="read"/> - <property name="UUIDs" type="as" access="read"/> - <property name="Modalias" type="s" access="read"/> - </interface> - - <interface name="org.bluez.Device1"> - <method name="Disconnect"/> - <method name="Connect"/> - <method name="ConnectProfile"> - <arg name="UUID" type="s" direction="in"/> - </method> - <method name="DisconnectProfile"> - <arg name="UUID" type="s" direction="in"/> - </method> - <method name="Pair"/> - <method name="CancelPairing"/> - <property name="Address" type="s" access="read"/> - <property name="Name" type="s" access="read"/> - <property name="Alias" type="s" access="readwrite"/> - <property name="Class" type="u" access="read"/> - <property name="Appearance" type="q" access="read"/> - <property name="Icon" type="s" access="read"/> - <property name="Paired" type="b" access="read"/> - <property name="Trusted" type="b" access="readwrite"/> - <property name="Blocked" type="b" access="readwrite"/> - <property name="LegacyPairing" type="b" access="read"/> - <property name="RSSI" type="n" access="read"/> - <property name="Connected" type="b" access="read"/> - <property name="UUIDs" type="as" access="read"/> - <property name="Modalias" type="s" access="read"/> - <property name="Adapter" type="o" access="read"/> - <property name="ManufacturerData" type="a{qv}" access="read"/> - <property name="ServiceData" type="a{sv}" access="read"/> - <property name="TxPower" type="n" access="read"/> - <property name="ServicesResolved" type="b" access="read"/> - </interface> - - <interface name="org.bluez.GattService1"> - <property name="UUID" type="s" access="read"/> - <property name="Device" type="o" access="read"/> - <property name="Primary" type="b" access="read"/> - <property name="Characteristics" type="ao" access="read"/> - </interface> - - <interface name="org.bluez.GattCharacteristic1"> - <method name="ReadValue"> - <arg name="value" type="ay" direction="out"> - <annotation name="org.gtk.GDBus.C.ForceGVariant" value="true"/> - </arg> - <arg name="options" type="a{sv}" direction="in"/> - </method> - <method name="WriteValue"> - <arg name="value" type="ay" byte_arrays="True" direction="in"> - <annotation name="org.gtk.GDBus.C.ForceGVariant" value="true"/> - </arg> - <arg name="options" type="a{sv}" direction="in"/> - </method> - <method name="StartNotify"/> - <method name="StopNotify"/> - <property name="UUID" type="s" access="read"/> - <property name="Service" type="o" access="read"/> - <property name="Value" type="ay" access="read"> - <annotation name="org.gtk.GDBus.C.ForceGVariant" value="true"/> - </property> - <property name="Notifying" type="b" access="read"/> - <property name="Flags" type="as" access="read"/> - <property name="Descriptors" type="ao" access="read"/> - </interface> - - <interface name="org.bluez.GattDescriptor1"> - <method name="ReadValue"> - <arg name="value" type="ay" direction="out"/> - <arg name="options" type="a{sv}" direction="in"/> - </method> - <method name="WriteValue"> - <arg name="value" type="ay" direction="in"/> - <arg name="options" type="a{sv}" direction="in"/> - </method> - <property name="UUID" type="s" access="read"/> - <property name="Characteristic" type="o" access="read"/> - <property name="Value" type="ay" access="read"/> - </interface> - - <interface name="org.bluez.LEAdvertisement1"> - <method name="Release"> - <annotation name="org.freedesktop.DBus.Method.NoReply" value="true"/> - </method> - <annotation name="org.freedesktop.DBus.Properties.PropertiesChanged" value="const"/> - <property name="Type" type="s" access="read"/> - <property name="ServiceUUIDs" type="as" access="read"/> - <property name="ManufacturerData" type="a{sv}" access="read"/> - <property name="SolicitUUIDs" type="as" access="read"/> - <property name="ServiceData" type="a{sv}" access="read"/> - <property name="IncludeTxPower" type="b" access="read"/> - </interface> - - <interface name="org.bluez.AgentManager1"> - <method name="RegisterAgent"> - <arg name="agent" type="o" direction="in" /> - <arg name="capability" type="s" direction="in" /> - </method> - <method name="UnregisterAgent"> - <arg name="agent" type="o" direction="in" /> - </method> - <method name="RequestDefaultAgent"> - <arg name="agent" type="o" direction="in" /> - </method> - </interface> - - <interface name="org.bluez.Agent1"> - <method name="Release"/> - <method name="RequestPinCode"> - <arg name="pincode" type="s" direction="out" /> - <arg name="device" type="o" direction="in" /> - </method> - <method name="DisplayPinCode"> - <arg name="device" type="o" direction="in" /> - <arg name="pincode" type="s" direction="in" /> - </method> - <method name="RequestPasskey"> - <arg name="passkey" type="u" direction="out" /> - <arg name="device" type="o" direction="in" /> - </method> - <method name="DisplayPasskey"> - <arg name="device" type="o" direction="in" /> - <arg name="passkey" type="u" direction="in" /> - <arg name="entered" type="q" direction="in" /> - </method> - <method name="RequestConfirmation"> - <arg name="device" type="o" direction="in" /> - <arg name="passkey" type="u" direction="in" /> - </method> - <method name="RequestAuthorization"> - <arg name="device" type="o" direction="in" /> - </method> - <method name="AuthorizeService"> - <arg name="device" type="o" direction="in" /> - <arg name="uuid" type="s" direction="in" /> - </method> - <method name="Cancel"/> - </interface> - -</node> diff --git a/src/tinyb/tinyb.pc.cmake b/src/tinyb/tinyb.pc.cmake deleted file mode 100644 index 1c9fbaf8..00000000 --- a/src/tinyb/tinyb.pc.cmake +++ /dev/null @@ -1,11 +0,0 @@ -prefix=@CMAKE_INSTALL_PREFIX@ -exec_prefix=${prefix} -libdir=${exec_prefix}/lib@LIB_SUFFIX@ -includedir=${prefix}/include - -Name: tinyb -Description: Tiny BLE library -Version: @tinyb_VERSION_STRING@ - -Libs: -L${libdir} -ltinyb -Cflags: -I${includedir} diff --git a/src/tinyb/tinyb_utils.cpp b/src/tinyb/tinyb_utils.cpp deleted file mode 100644 index 89c76b81..00000000 --- a/src/tinyb/tinyb_utils.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Author: Andrei Vasiliu <[email protected]> - * 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 "tinyb_utils.hpp" -#include "BluetoothException.hpp" - -std::vector<unsigned char> tinyb::from_chars_to_vector(const gchar *chars) -{ - std::vector<unsigned char>::size_type chars_size = strlen((const char*)chars); - - if (chars_size == 0) - throw std::runtime_error("Trying to read empty value"); - - std::vector<unsigned char> result(chars, chars + chars_size); - - return result; -} - -std::vector<unsigned char> tinyb::from_gbytes_to_vector(const GBytes *bytes) -{ - gsize result_size; - const unsigned char *aux_array = (const unsigned char *)g_bytes_get_data(const_cast<GBytes *>(bytes), &result_size); - - if (aux_array == nullptr || result_size == 0) - throw std::runtime_error("Trying to read empty value"); - - std::vector<unsigned char> result(result_size); - std::copy(aux_array, aux_array + result_size, result.begin()); - - return result; -} - -/* it allocates memory - the result that is being returned is from heap */ -GBytes *tinyb::from_vector_to_gbytes(const std::vector<unsigned char>& vector) -{ - unsigned int vector_size = vector.size(); - const unsigned char *vector_content = vector.data(); - - GBytes *result = g_bytes_new(vector_content, vector_size); - if (result == nullptr) - throw std::bad_alloc(); - - return result; -} - -std::vector<unsigned char> tinyb::from_iter_to_vector(GVariant *iter) -{ - GVariantIter *value_iter; - guchar value_byte; - - g_variant_get (iter, - "ay", - &value_iter); - - if (value_iter == nullptr) - throw std::invalid_argument("GVariant should be a container of an array of bytes"); - - std::vector<unsigned char> value; - while (g_variant_iter_loop(value_iter, "y", &value_byte)) { - value.push_back(value_byte); - } - - g_variant_iter_free(value_iter); - return value; -} - -void tinyb::handle_error(GError *error) -{ - if (error != nullptr) { - BluetoothException e(error->message); - g_error_free(error); - throw e; - } -} |