diff options
-rw-r--r-- | src/mm-port-probe.c | 87 | ||||
-rw-r--r-- | src/mm-port-probe.h | 9 |
2 files changed, 95 insertions, 1 deletions
diff --git a/src/mm-port-probe.c b/src/mm-port-probe.c index 4ebb8f88..0f19972a 100644 --- a/src/mm-port-probe.c +++ b/src/mm-port-probe.c @@ -22,6 +22,11 @@ G_DEFINE_TYPE (MMPortProbe, mm_port_probe, G_TYPE_OBJECT) +typedef struct { + /* ---- Generic task context ---- */ + GSimpleAsyncResult *result; +} PortProbeRunTask; + struct _MMPortProbePrivate { /* Port and properties */ GUdevDevice *port; @@ -29,15 +34,92 @@ struct _MMPortProbePrivate { gchar *name; gchar *physdev_path; gchar *driver; + + /* Current probing task. Only one can be available at a time */ + PortProbeRunTask *task; }; +static void +port_probe_run_task_free (PortProbeRunTask *task) +{ + g_object_unref (task->result); + g_free (task); +} + +static void +port_probe_run_task_complete (PortProbeRunTask *task, + gboolean complete_in_idle, + gboolean result, + GError *error) +{ + if (error) + g_simple_async_result_take_error (task->result, error); + else + g_simple_async_result_set_op_res_gboolean (task->result, result); + + if (complete_in_idle) + g_simple_async_result_complete_in_idle (task->result); + else + g_simple_async_result_complete (task->result); +} + +gboolean +mm_port_probe_run_finish (MMPortProbe *self, + GAsyncResult *result, + GError **error) +{ + gboolean res; + + g_return_val_if_fail (MM_IS_PORT_PROBE (self), FALSE); + g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE); + + /* Propagate error, if any */ + if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error)) + res = FALSE; + else + res = g_simple_async_result_get_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (result)); + + /* Cleanup probing task */ + if (self->priv->task) { + port_probe_run_task_free (self->priv->task); + self->priv->task = NULL; + } + return res; +} + +void +mm_port_probe_run (MMPortProbe *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + PortProbeRunTask *task; + + g_return_if_fail (MM_IS_PORT_PROBE (self)); + g_return_if_fail (callback != NULL); + + /* Shouldn't schedule more than one probing at a time */ + g_assert (self->priv->task == NULL); + + task = g_new0 (PortProbeRunTask, 1); + task->result = g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + mm_port_probe_run); + + /* Store as current task */ + self->priv->task = task; + + /* For now, just set successful */ + port_probe_run_task_complete (task, TRUE, TRUE, NULL); +} + GUdevDevice * mm_port_probe_get_port (MMPortProbe *self) { g_return_val_if_fail (MM_IS_PORT_PROBE (self), NULL); return self->priv->port; -} +}; const gchar * mm_port_probe_get_port_name (MMPortProbe *self) @@ -101,6 +183,9 @@ finalize (GObject *object) { MMPortProbe *self = MM_PORT_PROBE (object); + /* We should never have a task here */ + g_assert (self->priv->task == NULL); + g_free (self->priv->subsys); g_free (self->priv->name); g_free (self->priv->physdev_path); diff --git a/src/mm-port-probe.h b/src/mm-port-probe.h index 9e54d608..7db56100 100644 --- a/src/mm-port-probe.h +++ b/src/mm-port-probe.h @@ -18,6 +18,7 @@ #include <glib.h> #include <glib-object.h> +#include <gio/gio.h> #define G_UDEV_API_IS_SUBJECT_TO_CHANGE #include <gudev/gudev.h> @@ -54,5 +55,13 @@ const gchar *mm_port_probe_get_port_subsys (MMPortProbe *self); const gchar *mm_port_probe_get_port_physdev (MMPortProbe *self); const gchar *mm_port_probe_get_port_driver (MMPortProbe *self); +/* Run probing */ +void mm_port_probe_run (MMPortProbe *self, + GAsyncReadyCallback callback, + gpointer user_data); +gboolean mm_port_probe_run_finish (MMPortProbe *self, + GAsyncResult *result, + GError **error); + #endif /* MM_PORT_PROBE_H */ |