From 08c09c51a3fc44075a19619d428a9901f80f6483 Mon Sep 17 00:00:00 2001 From: Jonathan Merkel Date: Thu, 23 May 2019 17:15:32 +0200 Subject: [PATCH 1/4] Added null check for api version --- java/BluetoothManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) { From adeea7f4f775cf44814dce2d15956a2a63f055fc Mon Sep 17 00:00:00 2001 From: Jonathan Merkel Date: Wed, 11 Sep 2019 16:22:27 +0200 Subject: [PATCH 2/4] Changed unique Pointer --- src/BluetoothManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BluetoothManager.cpp b/src/BluetoothManager.cpp index 1a05776e..8eca3861 100644 --- a/src/BluetoothManager.cpp +++ b/src/BluetoothManager.cpp @@ -185,7 +185,7 @@ std::unique_ptr BluetoothManager::find(BluetoothType type, if (object == nullptr) { event->wait(timeout); - object = std::unique_ptr(event->get_result()); + object = std::unique_ptr(event->get_result()->clone()); } event->cancel(); From 4c61a2c22f36c441250386230811c6e3ca6adae4 Mon Sep 17 00:00:00 2001 From: Jonathan Merkel Date: Thu, 12 Sep 2019 17:22:38 +0200 Subject: [PATCH 3/4] Added mutex, check if event is in event_list and log messages --- api/tinyb/BluetoothManager.hpp | 132 +++++++++++++++++---------- src/BluetoothManager.cpp | 161 +++++++++++++-------------------- 2 files changed, 144 insertions(+), 149 deletions(-) diff --git a/api/tinyb/BluetoothManager.hpp b/api/tinyb/BluetoothManager.hpp index fd833cfc..34cc43d3 100644 --- a/api/tinyb/BluetoothManager.hpp +++ b/api/tinyb/BluetoothManager.hpp @@ -23,32 +23,63 @@ */ #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; -friend class BluetoothGattDescriptor; -friend class BluetoothEventManager; +class tinyb::BluetoothManager : public BluetoothObject { + friend class BluetoothAdapter; + + friend class BluetoothDevice; + + friend class BluetoothGattService; + + friend class BluetoothGattCharacteristic; + + 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 event_list_lock; + BluetoothManager(); + BluetoothManager(const BluetoothManager &object); + /** + * Not threadsafe. Please ensure it through mutex locking. + * @return + */ + bool containsEvent(BluetoothEvent &event) { + for (auto it = event_list.begin(); it != event_list.end(); ++it) { + if ((*it).get() == &event) { + return true; + } + } + return false; + } + + bool containsEvent(std::shared_ptr &event) { + for (auto it = event_list.begin(); it != event_list.end(); ++it) { + if ((*it).get() == event.get()) { + return true; + } + } + return false; + } + 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 +88,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 +109,31 @@ 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) { - event_list.remove(event); + void remove_event(std::shared_ptr &event) { + event_list_lock.lock(); + if (containsEvent(event)) { + std::cout << "remove_event(std::shared_ptr &event) - removing event" << std::endl; + event_list.remove(event); + } + event_list_lock.unlock(); } void remove_event(BluetoothEvent &event) { - for(auto it = event_list.begin(); it != event_list.end(); ++it) { + event_list_lock.lock(); + for (auto it = event_list.begin(); it != event_list.end(); ++it) { if ((*it).get() == &event) { + std::cout << "remove_event(BluetoothEvent &event) - removing event" << std::endl; event_list.remove(*it); break; } } + event_list_lock.unlock(); } @@ -110,11 +154,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 +180,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 +201,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 +219,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 +236,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/src/BluetoothManager.cpp b/src/BluetoothManager.cpp index 8eca3861..6a98e9f8 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,20 @@ 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 +120,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,12 +160,10 @@ 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); @@ -192,22 +177,19 @@ std::unique_ptr BluetoothManager::find(BluetoothType type, 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) { + event_list_lock.lock(); + 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 +210,39 @@ 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)) - it = event_list.erase(it); - else + if ((*it)->execute_callback(object)) { + if (containsEvent(*it)) { + std::cout << "handle_event-erasing event from event_list" << std::endl; + it = event_list.erase(it); + } + } else ++it; } + event_list_lock.unlock(); } -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 +272,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 +301,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 +317,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 +333,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 From cd8fcd4e4c2232847cf0ecad8cbd94e7abb87aa2 Mon Sep 17 00:00:00 2001 From: Jonathan Merkel Date: Fri, 13 Sep 2019 14:11:36 +0200 Subject: [PATCH 4/4] changed version, added mutex, removed logs --- CMakeLists.txt | 6 ++--- api/tinyb/BluetoothManager.hpp | 43 +++++++--------------------------- src/BluetoothManager.cpp | 12 ++++------ 3 files changed, 17 insertions(+), 44 deletions(-) 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 34cc43d3..684a73d8 100644 --- a/api/tinyb/BluetoothManager.hpp +++ b/api/tinyb/BluetoothManager.hpp @@ -26,9 +26,9 @@ #include "BluetoothObject.hpp" #include "BluetoothEvent.hpp" -#include #include #include +#include class tinyb::BluetoothManager : public BluetoothObject { friend class BluetoothAdapter; @@ -47,35 +47,12 @@ class tinyb::BluetoothManager : public BluetoothObject { std::unique_ptr default_adapter; static BluetoothManager *bluetooth_manager; std::list > event_list; - std::mutex event_list_lock; - + std::mutex mutex; BluetoothManager(); BluetoothManager(const BluetoothManager &object); - /** - * Not threadsafe. Please ensure it through mutex locking. - * @return - */ - bool containsEvent(BluetoothEvent &event) { - for (auto it = event_list.begin(); it != event_list.end(); ++it) { - if ((*it).get() == &event) { - return true; - } - } - return false; - } - - bool containsEvent(std::shared_ptr &event) { - for (auto it = event_list.begin(); it != event_list.end(); ++it) { - if ((*it).get() == event.get()) { - return true; - } - } - return false; - } - protected: void handle_event(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent, @@ -116,24 +93,22 @@ class tinyb::BluetoothManager : public BluetoothObject { /** Remove event to checked against events generated by BlueZ. */ void remove_event(std::shared_ptr &event) { - event_list_lock.lock(); - if (containsEvent(event)) { - std::cout << "remove_event(std::shared_ptr &event) - removing event" << std::endl; - event_list.remove(event); - } - event_list_lock.unlock(); + + + event_list.remove(event); + + } void remove_event(BluetoothEvent &event) { - event_list_lock.lock(); + std::lock_guard guard(mutex); for (auto it = event_list.begin(); it != event_list.end(); ++it) { if ((*it).get() == &event) { - std::cout << "remove_event(BluetoothEvent &event) - removing event" << std::endl; event_list.remove(*it); break; } } - event_list_lock.unlock(); + } diff --git a/src/BluetoothManager.cpp b/src/BluetoothManager.cpp index 6a98e9f8..2dddc13c 100644 --- a/src/BluetoothManager.cpp +++ b/src/BluetoothManager.cpp @@ -101,7 +101,8 @@ std::string BluetoothManager::get_class_name() const { } std::string BluetoothManager::get_java_class() const { - return std::string(JAVA_PACKAGE("/BluetoothManager"); + return std::string(JAVA_PACKAGE + "/BluetoothManager"); } std::string BluetoothManager::get_object_path() const { @@ -188,8 +189,9 @@ BluetoothManager::find(BluetoothType type, std::string *name, std::string *ident void BluetoothManager::handle_event(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent, BluetoothObject &object) { - event_list_lock.lock(); + 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 */ @@ -211,14 +213,10 @@ BluetoothManager::handle_event(BluetoothType type, std::string *name, std::strin } /* The event matches, execute and see if it needs to reexecute */ if ((*it)->execute_callback(object)) { - if (containsEvent(*it)) { - std::cout << "handle_event-erasing event from event_list" << std::endl; - it = event_list.erase(it); - } + it = event_list.erase(it); } else ++it; } - event_list_lock.unlock(); } static gpointer init_manager_thread(void *data) {