diff --git a/CMakeLists.txt b/CMakeLists.txt index bef06091..83b24d59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,9 +19,9 @@ include(GNUInstallDirs) set (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH}) # Make a version file containing the current version from git. -include (GetGitRevisionDescription) -git_describe (VERSION "--tags") - +#include (GetGitRevisionDescription) +#git_describe (VERSION "--tags") +set (VERSION "v0.5.3") if ("x_${VERSION}" STREQUAL "x_GIT-NOTFOUND" OR "x_${VERSION}" STREQUAL "x_HEAD-HASH-NOTFOUND" OR "x_${VERSION}" STREQUAL "x_-128-NOTFOUND") message (WARNING " - Install git to compile a production libtinyb!") set (VERSION "v0.5.0-dirty") diff --git a/api/tinyb/BluetoothManager.hpp b/api/tinyb/BluetoothManager.hpp index fd833cfc..684a73d8 100644 --- a/api/tinyb/BluetoothManager.hpp +++ b/api/tinyb/BluetoothManager.hpp @@ -23,32 +23,40 @@ */ #pragma once + #include "BluetoothObject.hpp" #include "BluetoothEvent.hpp" #include #include +#include + +class tinyb::BluetoothManager : public BluetoothObject { + friend class BluetoothAdapter; + + friend class BluetoothDevice; + + friend class BluetoothGattService; + + friend class BluetoothGattCharacteristic; -class tinyb::BluetoothManager: public BluetoothObject -{ -friend class BluetoothAdapter; -friend class BluetoothDevice; -friend class BluetoothGattService; -friend class BluetoothGattCharacteristic; -friend class BluetoothGattDescriptor; -friend class BluetoothEventManager; + friend class BluetoothGattDescriptor; + + friend class BluetoothEventManager; private: - std::unique_ptr default_adapter; + std::unique_ptr default_adapter; static BluetoothManager *bluetooth_manager; - std::list> event_list; + std::list > event_list; + std::mutex mutex; BluetoothManager(); + BluetoothManager(const BluetoothManager &object); protected: - void handle_event(BluetoothType type, std::string *name, - std::string *identifier, BluetoothObject *parent, BluetoothObject &object); + void handle_event(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent, + BluetoothObject &object); public: @@ -57,14 +65,19 @@ friend class BluetoothEventManager; } static std::string get_api_version(); + static std::string get_library_version(); virtual std::string get_java_class() const; + virtual std::string get_class_name() const; + virtual std::string get_object_path() const; + virtual BluetoothType get_bluetooth_type() const; ~BluetoothManager(); + /** Returns an instance of BluetoothManager, to be used instead of constructor. * @return An initialized BluetoothManager instance. */ @@ -73,23 +86,29 @@ friend class BluetoothEventManager; /** Add event to checked against events generated by BlueZ. If an the event * matches an incoming event its' callback will be triggered. Events can be * the addition of a new Device, GattService, GattCharacteristic, etc. */ - void add_event(std::shared_ptr &event) { + void add_event(std::shared_ptr &event) { event_list.push_back(event); } /** Remove event to checked against events generated by BlueZ. */ - void remove_event(std::shared_ptr &event) { + void remove_event(std::shared_ptr &event) { + + event_list.remove(event); + + } void remove_event(BluetoothEvent &event) { - for(auto it = event_list.begin(); it != event_list.end(); ++it) { + std::lock_guard guard(mutex); + for (auto it = event_list.begin(); it != event_list.end(); ++it) { if ((*it).get() == &event) { event_list.remove(*it); break; } } + } @@ -110,11 +129,9 @@ friend class BluetoothEventManager; * timeout expires or event is canceled. */ template - std::unique_ptr find(std::string *name, - std::string* identifier, BluetoothObject *parent, - std::chrono::milliseconds timeout = std::chrono::milliseconds::zero()) - { - std::unique_ptr obj = find(T::class_type(), name, identifier, parent, timeout); + std::unique_ptr find(std::string *name, std::string *identifier, BluetoothObject *parent, + std::chrono::milliseconds timeout = std::chrono::milliseconds::zero()) { + std::unique_ptr obj = find(T::class_type(), name, identifier, parent, timeout); T *t = dynamic_cast(obj.release()); return std::unique_ptr(t); } @@ -138,9 +155,9 @@ friend class BluetoothEventManager; * @return An object matching the name, identifier, parent or null if not found before * timeout expires or event is canceled. */ - std::unique_ptr find(BluetoothType type, std::string *name, - std::string* identifier, BluetoothObject *parent, - std::chrono::milliseconds timeout = std::chrono::milliseconds::zero()); + std::unique_ptr + find(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent, + std::chrono::milliseconds timeout = std::chrono::milliseconds::zero()); /** Find a BluetoothObject of a type matching type. If parameters name, * identifier and parent are not null, the found object will have to @@ -159,10 +176,9 @@ friend class BluetoothEventManager; * value of zero means wait forever. If object is not found during this time null will be returned. * @return It returns the BluetoothEvent generated by this function, allowing to manage the parameters or cancel the event. */ - std::weak_ptr find(BluetoothType type, std::string *name, - std::string* identifier, BluetoothObject *parent, BluetoothCallback cb, - bool execute_once = true, - std::chrono::milliseconds timeout = std::chrono::milliseconds::zero()); + std::weak_ptr + find(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent, BluetoothCallback cb, + bool execute_once = true, std::chrono::milliseconds timeout = std::chrono::milliseconds::zero()); /** Return a BluetoothObject of a type matching type. If parameters name, * identifier and parent are not null, the returned object will have to @@ -178,8 +194,8 @@ friend class BluetoothEventManager; * waiting for * @return An object matching the name, identifier, parent or null if not found. */ - std::unique_ptr get_object(BluetoothType type, - std::string *name, std::string *identifier, BluetoothObject *parent); + std::unique_ptr + get_object(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent); /** Return a vector of BluetoothObject of a type matching type. If parameters name, * identifier and parent are not null, the returned object will have to @@ -195,53 +211,44 @@ friend class BluetoothEventManager; * waiting for * @return A vector of object matching the name, identifier, parent. */ - std::vector> get_objects( - BluetoothType type = BluetoothType::NONE, - std::string *name = nullptr, std::string *identifier = nullptr, - BluetoothObject *parent = nullptr); + std::vector > + get_objects(BluetoothType type = BluetoothType::NONE, std::string *name = nullptr, + std::string *identifier = nullptr, BluetoothObject *parent = nullptr); /** Returns a list of BluetoothAdapters available in the system * @return A list of BluetoothAdapters available in the system */ - std::vector> get_adapters( - ); + std::vector > get_adapters(); /** Returns a list of discovered BluetoothDevices * @return A list of discovered BluetoothDevices */ - std::vector> get_devices( - ); + std::vector > get_devices(); /** Returns a list of available BluetoothGattServices * @return A list of available BluetoothGattServices */ - std::vector> get_services( - ); + std::vector > get_services(); /** Sets a default adapter to use for discovery. * @return TRUE if the device was set */ - bool set_default_adapter( - BluetoothAdapter &adapter - ); + bool set_default_adapter(BluetoothAdapter &adapter); - std::unique_ptr get_default_adapter(); + std::unique_ptr get_default_adapter(); /** Turns on device discovery on the default adapter if it is disabled. * @return TRUE if discovery was successfully enabled */ - bool start_discovery( - ); + bool start_discovery(); /** Turns off device discovery on the default adapter if it is enabled. * @return TRUE if discovery was successfully disabled */ - bool stop_discovery( - ); + bool stop_discovery(); /** Returns if the discovers is running or not. * @return TRUE if discovery is running */ - bool get_discovering( - ); + bool get_discovering(); }; diff --git a/java/BluetoothManager.java b/java/BluetoothManager.java index cc8f9d3a..b823533a 100644 --- a/java/BluetoothManager.java +++ b/java/BluetoothManager.java @@ -303,7 +303,7 @@ public static synchronized BluetoothManager getBluetoothManager() throws Runtime { String nativeAPIVersion = getNativeAPIVersion(); String APIVersion = BluetoothManager.class.getPackage().getSpecificationVersion(); - if (APIVersion.equals(nativeAPIVersion) == false) { + if (APIVersion != null && APIVersion.equals(nativeAPIVersion) == false) { String[] nativeAPIVersionCode = nativeAPIVersion.split("\\D"); String[] APIVersionCode = APIVersion.split("\\D"); if (APIVersionCode[0].equals(nativeAPIVersionCode[0]) == false) { diff --git a/src/BluetoothManager.cpp b/src/BluetoothManager.cpp index 1a05776e..2dddc13c 100644 --- a/src/BluetoothManager.cpp +++ b/src/BluetoothManager.cpp @@ -39,8 +39,7 @@ using namespace tinyb; class tinyb::BluetoothEventManager { public: - static void on_interface_added (GDBusObject *object, - GDBusInterface *interface, gpointer user_data) { + static void on_interface_added(GDBusObject *object, GDBusInterface *interface, gpointer user_data) { GDBusInterfaceInfo *info = g_dbus_interface_get_info(interface); BluetoothType type = BluetoothType::NONE; BluetoothManager *manager = BluetoothManager::get_bluetooth_manager(); @@ -49,36 +48,32 @@ class tinyb::BluetoothEventManager { if (info == NULL) return; - if(IS_GATT_SERVICE1_PROXY(interface)) { + 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)) { + } 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)) { + } 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)) { + } 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)) { + } else if (IS_ADAPTER1_PROXY(interface)) { type = BluetoothType::ADAPTER; auto obj = new BluetoothAdapter(ADAPTER1(interface)); auto name = obj->get_name(); @@ -87,12 +82,12 @@ class tinyb::BluetoothEventManager { } } - static void on_object_added (GDBusObjectManager *manager, - GDBusObject *object, gpointer user_data) { + static void on_object_added(GDBusObjectManager *manager, GDBusObject *object, gpointer user_data) { 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); + 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); } @@ -101,24 +96,21 @@ class tinyb::BluetoothEventManager { GDBusObjectManager *gdbus_manager = NULL; GThread *manager_thread = NULL; -std::string BluetoothManager::get_class_name() const -{ +std::string BluetoothManager::get_class_name() const { return std::string("BluetoothManager"); } -std::string BluetoothManager::get_java_class() const -{ - return std::string(JAVA_PACKAGE "/BluetoothManager"); +std::string BluetoothManager::get_java_class() const { + return std::string(JAVA_PACKAGE + "/BluetoothManager"); } -std::string BluetoothManager::get_object_path() const -{ +std::string BluetoothManager::get_object_path() const { return std::string("/"); } -BluetoothType BluetoothManager::get_bluetooth_type() const -{ - return BluetoothType::NONE; +BluetoothType BluetoothManager::get_bluetooth_type() const { + return BluetoothType::NONE; } std::string BluetoothManager::get_api_version() { @@ -129,21 +121,17 @@ std::string BluetoothManager::get_library_version() { return std::string(gVERSION_SHORT); } -std::unique_ptr BluetoothManager::get_object( - BluetoothType type, std::string *name, std::string *identifier, - BluetoothObject *parent) -{ +std::unique_ptr +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(); return std::move(list.front()); } -std::vector> BluetoothManager::get_objects( - BluetoothType type, std::string *name, std::string *identifier, - BluetoothObject *parent) -{ - std::vector> vector; +std::vector > +BluetoothManager::get_objects(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent) { + std::vector > vector; GList *l, *objects = g_dbus_object_manager_get_objects(gdbus_manager); for (l = objects; l != NULL; l = l->next) { @@ -173,41 +161,37 @@ std::vector> BluetoothManager::get_objects( return vector; } -std::unique_ptr BluetoothManager::find(BluetoothType type, - std::string *name, std::string* identifier, BluetoothObject *parent, - std::chrono::milliseconds timeout) -{ - std::shared_ptr event(new BluetoothEvent(type, name, - identifier, parent)); +std::unique_ptr +BluetoothManager::find(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent, + std::chrono::milliseconds timeout) { + std::shared_ptr 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(event->get_result()); + object = std::unique_ptr(event->get_result()->clone()); } event->cancel(); return object; } -std::weak_ptr BluetoothManager::find(BluetoothType type, - std::string *name, std::string* identifier, BluetoothObject *parent, - BluetoothCallback cb, bool execute_once, - std::chrono::milliseconds timeout) -{ - std::shared_ptr event(new BluetoothEvent(type, name, - identifier, parent)); +std::weak_ptr +BluetoothManager::find(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent, + BluetoothCallback cb, bool execute_once, std::chrono::milliseconds timeout) { + std::shared_ptr event(new BluetoothEvent(type, name, identifier, parent)); add_event(event); return std::weak_ptr(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();) { +void +BluetoothManager::handle_event(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent, + BluetoothObject &object) { + std::lock_guard guard(mutex); + 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 */ @@ -228,47 +212,35 @@ void BluetoothManager::handle_event(BluetoothType type, std::string *name, continue; /* this event does not match */ } /* The event matches, execute and see if it needs to reexecute */ - if ((*it)->execute_callback(object)) + if ((*it)->execute_callback(object)) { it = event_list.erase(it); - else + } else ++it; } } -static gpointer init_manager_thread(void *data) -{ +static gpointer init_manager_thread(void *data) { GMainLoop *loop; GDBusObjectManager *gdbus_manager = (GDBusObjectManager *) data; loop = g_main_loop_new(NULL, FALSE); - g_signal_connect(gdbus_manager, - "interface-added", - G_CALLBACK(BluetoothEventManager::on_interface_added), - NULL); + 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_signal_connect(gdbus_manager, "object-added", G_CALLBACK(BluetoothEventManager::on_object_added), NULL); g_main_loop_run(loop); return NULL; } -BluetoothManager::BluetoothManager() : event_list() -{ +BluetoothManager::BluetoothManager() : event_list() { GError *error = NULL; GList *objects, *l; - 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); + 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: "); @@ -298,25 +270,21 @@ BluetoothManager::BluetoothManager() : event_list() } } -BluetoothManager *BluetoothManager::get_bluetooth_manager() -{ +BluetoothManager *BluetoothManager::get_bluetooth_manager() { static BluetoothManager bluetooth_manager; return &bluetooth_manager; } -BluetoothManager::BluetoothManager(const BluetoothManager &) -{ +BluetoothManager::BluetoothManager(const BluetoothManager &) { /* Should not be called */ } -BluetoothManager::~BluetoothManager() -{ +BluetoothManager::~BluetoothManager() { /* Should not be called */ } -std::vector> BluetoothManager::get_adapters() -{ - std::vector> vector; +std::vector > BluetoothManager::get_adapters() { + std::vector > vector; GList *l, *objects = g_dbus_object_manager_get_objects(gdbus_manager); for (l = objects; l != NULL; l = l->next) { @@ -331,9 +299,8 @@ std::vector> BluetoothManager::get_adapters() return vector; } -std::vector> BluetoothManager::get_devices() -{ - std::vector> vector; +std::vector > BluetoothManager::get_devices() { + std::vector > vector; GList *l, *objects = g_dbus_object_manager_get_objects(gdbus_manager); for (l = objects; l != NULL; l = l->next) { @@ -348,9 +315,8 @@ std::vector> BluetoothManager::get_devices() return vector; } -std::vector> BluetoothManager::get_services() -{ - std::vector> vector; +std::vector > BluetoothManager::get_services() { + std::vector > vector; GList *l, *objects = g_dbus_object_manager_get_objects(gdbus_manager); for (l = objects; l != NULL; l = l->next) { @@ -365,35 +331,30 @@ std::vector> BluetoothManager::get_service return vector; } -bool BluetoothManager::set_default_adapter(BluetoothAdapter &adapter) -{ +bool BluetoothManager::set_default_adapter(BluetoothAdapter &adapter) { default_adapter = std::unique_ptr(adapter.clone()); return true; } -std::unique_ptr BluetoothManager::get_default_adapter() -{ +std::unique_ptr BluetoothManager::get_default_adapter() { return std::unique_ptr(default_adapter->clone()); } -bool BluetoothManager::start_discovery() -{ +bool BluetoothManager::start_discovery() { if (default_adapter != nullptr) return default_adapter->start_discovery(); else return false; } -bool BluetoothManager::stop_discovery() -{ +bool BluetoothManager::stop_discovery() { if (default_adapter != NULL) return default_adapter->stop_discovery(); else return false; } -bool BluetoothManager::get_discovering() -{ +bool BluetoothManager::get_discovering() { if (default_adapter != NULL) return default_adapter->get_discovering(); else