aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetre Eftime <[email protected]>2016-01-11 19:16:26 +0200
committerPetre Eftime <[email protected]>2016-03-07 11:48:42 +0200
commit890cad0c7bc32a179bfa36f76946bc11393bcf0d (patch)
tree8909113f0aec5e5ea39ac8f99e3fcf23592ee3d5
parentc0184f27d502320fb8f6d3776bb3847238483d10 (diff)
Implement handle_event, fix some issues
Signed-off-by: Petre Eftime <[email protected]>
-rw-r--r--api/tinyb/BluetoothEvent.hpp7
-rw-r--r--api/tinyb/BluetoothManager.hpp6
-rw-r--r--src/BluetoothEvent.cpp25
-rw-r--r--src/BluetoothManager.cpp58
4 files changed, 73 insertions, 23 deletions
diff --git a/api/tinyb/BluetoothEvent.hpp b/api/tinyb/BluetoothEvent.hpp
index 47d32766..75cca633 100644
--- a/api/tinyb/BluetoothEvent.hpp
+++ b/api/tinyb/BluetoothEvent.hpp
@@ -54,14 +54,13 @@ class BluetoothConditionVariable {
std::atomic_uint waiting;
BluetoothConditionVariable() : cv(), lock() {
- result = NULL;
+ result = nullptr;
waiting = 0;
triggered = false;
}
BluetoothObject *wait() {
-
- if (result != NULL)
+ if (result != nullptr)
return result;
if (!triggered) {
@@ -75,7 +74,7 @@ class BluetoothConditionVariable {
}
BluetoothObject *wait_for(std::chrono::milliseconds timeout) {
- if (result != NULL)
+ if (result != nullptr)
return result;
if (!triggered) {
diff --git a/api/tinyb/BluetoothManager.hpp b/api/tinyb/BluetoothManager.hpp
index b1ef81ef..82d5c290 100644
--- a/api/tinyb/BluetoothManager.hpp
+++ b/api/tinyb/BluetoothManager.hpp
@@ -36,6 +36,7 @@ friend class BluetoothDevice;
friend class BluetoothGattService;
friend class BluetoothGattCharacteristic;
friend class BluetoothGattDescriptor;
+friend class BluetoothEventManager;
private:
std::unique_ptr<BluetoothAdapter> default_adapter;
@@ -48,10 +49,7 @@ private:
protected:
void handle_event(BluetoothType type, std::string *name,
- std::string *identifier, BluetoothObject &object);
- void handle_event(const BluetoothEvent &event, BluetoothObject &object) {
- handle_event(event.get_type(), event.get_name(), event.get_identifier(), object);
- }
+ std::string *identifier, BluetoothObject *parent, BluetoothObject &object);
public:
diff --git a/src/BluetoothEvent.cpp b/src/BluetoothEvent.cpp
index f4e9f3da..196053f8 100644
--- a/src/BluetoothEvent.cpp
+++ b/src/BluetoothEvent.cpp
@@ -4,11 +4,12 @@
void BluetoothEvent::generic_callback(BluetoothObject &object, void *data)
{
- BluetoothConditionVariable *generic_data = static_cast<BluetoothConditionVariable *>(data);
- if (data == NULL)
+ if (data == nullptr)
return;
+ BluetoothConditionVariable *generic_data = static_cast<BluetoothConditionVariable *>(data);
+
generic_data->result = object.clone();
generic_data->notify();
}
@@ -19,25 +20,28 @@ BluetoothEvent::BluetoothEvent(BluetoothType type, std::string *name,
{
canceled = false;
this->type = type;
- if (name != NULL)
+ if (name != nullptr)
this->name = new std::string(*name);
else
- this->name = NULL;
+ this->name = nullptr;
- if (identifier != NULL)
+ if (identifier != nullptr)
this->identifier = new std::string(*identifier);
else
- this->identifier = NULL;
+ this->identifier = nullptr;
- if (parent != NULL)
+ if (parent != nullptr)
this->parent = parent->clone();
else
- this->parent = NULL;
+ this->parent = nullptr;
this->execute_once = execute_once;
this->cb = cb;
- this->data = data;
+ if (cb == generic_callback)
+ this->data = static_cast<void *>(&cv);
+ else
+ this->data = data;
}
bool BluetoothEvent::execute_callback(BluetoothObject &object)
@@ -53,7 +57,6 @@ bool BluetoothEvent::execute_callback(BluetoothObject &object)
void BluetoothEvent::wait(std::chrono::milliseconds timeout)
{
- BluetoothConditionVariable *generic_data = static_cast<BluetoothConditionVariable *>(data);
if (!canceled && execute_once == true) {
if (timeout == std::chrono::milliseconds::zero())
cv.wait();
@@ -72,8 +75,6 @@ void BluetoothEvent::cancel()
BluetoothEvent::~BluetoothEvent()
{
- cancel();
-
if (name != nullptr)
delete name;
if (identifier != nullptr)
diff --git a/src/BluetoothManager.cpp b/src/BluetoothManager.cpp
index b0eef764..8d7cfdca 100644
--- a/src/BluetoothManager.cpp
+++ b/src/BluetoothManager.cpp
@@ -44,28 +44,53 @@ public:
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));
+ g_print("Object %s - Interface added: %s\n", g_dbus_object_get_object_path(object), info->name);
+ 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));
+ g_print("Object %s - Interface added: %s\n", g_dbus_object_get_object_path(object), info->name);
+ 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));
+ g_print("Object %s - Interface added: %s\n", g_dbus_object_get_object_path(object), info->name);
+ 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));
+ g_print("Object %s - Interface added: %s\n", g_dbus_object_get_object_path(object), info->name);
+ 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));
+ g_print("Object %s - Interface added: %s\n", g_dbus_object_get_object_path(object), info->name);
+ auto name = obj->get_name();
+ auto uuid = obj->get_address();
+ manager->handle_event(type, &name, &uuid, nullptr, *obj);
}
-
- if (type == BluetoothType::NONE) /* It does not match anything */
- return;
}
static void on_object_added (GDBusObjectManager *manager,
@@ -174,6 +199,33 @@ std::weak_ptr<BluetoothEvent> BluetoothManager::find(BluetoothType type,
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 */
+ }
+
+ /* 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)
{