port-serial: port mm_port_serial_reopen to use GTask

This commit is contained in:
Ben Chan
2018-09-27 00:27:50 -07:00
committed by Aleksander Morgado
parent 2a1a0b88fb
commit f3e1e699cf

View File

@@ -112,7 +112,7 @@ struct _MMPortSerialPrivate {
guint connected_id; guint connected_id;
gpointer flash_ctx; gpointer flash_ctx;
gpointer reopen_ctx; GTask *reopen_task;
}; };
/*****************************************************************************/ /*****************************************************************************/
@@ -1166,7 +1166,7 @@ mm_port_serial_open (MMPortSerial *self, GError **error)
return FALSE; return FALSE;
} }
if (self->priv->reopen_ctx) { if (self->priv->reopen_task) {
g_set_error (error, g_set_error (error,
MM_SERIAL_ERROR, MM_SERIAL_ERROR,
MM_SERIAL_ERROR_OPEN_FAILED, MM_SERIAL_ERROR_OPEN_FAILED,
@@ -1516,20 +1516,15 @@ port_serial_close_force (MMPortSerial *self)
/* Reopen */ /* Reopen */
typedef struct { typedef struct {
MMPortSerial *self;
GSimpleAsyncResult *result;
guint initial_open_count; guint initial_open_count;
guint reopen_id; guint reopen_id;
} ReopenContext; } ReopenContext;
static void static void
reopen_context_complete_and_free (ReopenContext *ctx) reopen_context_free (ReopenContext *ctx)
{ {
if (ctx->reopen_id) if (ctx->reopen_id)
g_source_remove (ctx->reopen_id); g_source_remove (ctx->reopen_id);
g_simple_async_result_complete_in_idle (ctx->result);
g_object_unref (ctx->result);
g_object_unref (ctx->self);
g_slice_free (ReopenContext, ctx); g_slice_free (ReopenContext, ctx);
} }
@@ -1538,54 +1533,56 @@ mm_port_serial_reopen_finish (MMPortSerial *port,
GAsyncResult *res, GAsyncResult *res,
GError **error) GError **error)
{ {
return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error); return g_task_propagate_boolean (G_TASK (res), error);
} }
static void static void
port_serial_reopen_cancel (MMPortSerial *self) port_serial_reopen_cancel (MMPortSerial *self)
{ {
ReopenContext *ctx; GTask *task;
if (!self->priv->reopen_ctx) if (!self->priv->reopen_task)
return; return;
/* Recover context */ /* Recover task */
ctx = (ReopenContext *)self->priv->reopen_ctx; task = self->priv->reopen_task;
self->priv->reopen_ctx = NULL; self->priv->reopen_task = NULL;
g_simple_async_result_set_error (ctx->result, g_task_return_new_error (task,
MM_CORE_ERROR, MM_CORE_ERROR,
MM_CORE_ERROR_CANCELLED, MM_CORE_ERROR_CANCELLED,
"Reopen cancelled"); "Reopen cancelled");
reopen_context_complete_and_free (ctx); g_object_unref (task);
} }
static gboolean static gboolean
reopen_do (MMPortSerial *self) reopen_do (MMPortSerial *self)
{ {
GTask *task;
ReopenContext *ctx; ReopenContext *ctx;
GError *error = NULL; GError *error = NULL;
guint i; guint i;
/* Recover context */ /* Recover task */
g_assert (self->priv->reopen_ctx != NULL); g_assert (self->priv->reopen_task != NULL);
ctx = (ReopenContext *)self->priv->reopen_ctx; task = self->priv->reopen_task;
self->priv->reopen_ctx = NULL; self->priv->reopen_task = NULL;
ctx = g_task_get_task_data (task);
ctx->reopen_id = 0; ctx->reopen_id = 0;
for (i = 0; i < ctx->initial_open_count; i++) { for (i = 0; i < ctx->initial_open_count; i++) {
if (!mm_port_serial_open (ctx->self, &error)) { if (!mm_port_serial_open (self, &error)) {
g_prefix_error (&error, "Couldn't reopen port (%u): ", i); g_prefix_error (&error, "Couldn't reopen port (%u): ", i);
break; break;
} }
} }
if (error) if (error)
g_simple_async_result_take_error (ctx->result, error); g_task_return_error (task, error);
else else
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE); g_task_return_boolean (task, TRUE);
reopen_context_complete_and_free (ctx); g_object_unref (task);
return G_SOURCE_REMOVE; return G_SOURCE_REMOVE;
} }
@@ -1597,35 +1594,34 @@ mm_port_serial_reopen (MMPortSerial *self,
gpointer user_data) gpointer user_data)
{ {
ReopenContext *ctx; ReopenContext *ctx;
GTask *task;
guint i; guint i;
g_return_if_fail (MM_IS_PORT_SERIAL (self)); g_return_if_fail (MM_IS_PORT_SERIAL (self));
/* Setup context */ /* Setup context */
ctx = g_slice_new0 (ReopenContext); ctx = g_slice_new0 (ReopenContext);
ctx->self = g_object_ref (self);
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
mm_port_serial_reopen);
ctx->initial_open_count = self->priv->open_count; ctx->initial_open_count = self->priv->open_count;
task = g_task_new (self, NULL, callback, user_data);
g_task_set_task_data (task, ctx, (GDestroyNotify)reopen_context_free);
if (self->priv->forced_close) { if (self->priv->forced_close) {
g_simple_async_result_set_error (ctx->result, g_task_return_new_error (task,
MM_CORE_ERROR, MM_CORE_ERROR,
MM_CORE_ERROR_FAILED, MM_CORE_ERROR_FAILED,
"Serial port has been forced close."); "Serial port has been forced close.");
reopen_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
/* If already reopening, halt */ /* If already reopening, halt */
if (self->priv->reopen_ctx) { if (self->priv->reopen_task) {
g_simple_async_result_set_error (ctx->result, g_task_return_new_error (task,
MM_CORE_ERROR, MM_CORE_ERROR,
MM_CORE_ERROR_IN_PROGRESS, MM_CORE_ERROR_IN_PROGRESS,
"Modem is already being reopened"); "Modem is already being reopened");
reopen_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
@@ -1642,7 +1638,7 @@ mm_port_serial_reopen (MMPortSerial *self,
ctx->reopen_id = g_idle_add ((GSourceFunc)reopen_do, self); ctx->reopen_id = g_idle_add ((GSourceFunc)reopen_do, self);
/* Store context in private info */ /* Store context in private info */
self->priv->reopen_ctx = ctx; self->priv->reopen_task = task;
} }
static gboolean static gboolean