port-probe: setup an asynchronous method for port probing
The new method `mm_port_probe_run()' will run the whole probing process asynchronously. Result of the probing can be later obtained with `mm_port_probe_run_finish()'.
This commit is contained in:
committed by
Aleksander Morgado
parent
1b84994feb
commit
f227572d8f
+86
-1
@@ -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);
|
||||
|
||||
@@ -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 */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user