iface-modem-oma: port mm_iface_modem_oma_enable to use GTask

This commit is contained in:
Ben Chan
2017-06-28 21:43:49 -07:00
committed by Aleksander Morgado
parent f23f3f2c3f
commit 193784ebf8

View File

@@ -794,7 +794,7 @@ mm_iface_modem_oma_disable (MMIfaceModemOma *self,
/*****************************************************************************/ /*****************************************************************************/
typedef struct _EnablingContext EnablingContext; typedef struct _EnablingContext EnablingContext;
static void interface_enabling_step (EnablingContext *ctx); static void interface_enabling_step (GTask *task);
typedef enum { typedef enum {
ENABLING_STEP_FIRST, ENABLING_STEP_FIRST,
@@ -805,94 +805,79 @@ typedef enum {
} EnablingStep; } EnablingStep;
struct _EnablingContext { struct _EnablingContext {
MMIfaceModemOma *self;
EnablingStep step; EnablingStep step;
GSimpleAsyncResult *result;
GCancellable *cancellable;
MmGdbusModemOma *skeleton; MmGdbusModemOma *skeleton;
}; };
static void static void
enabling_context_complete_and_free (EnablingContext *ctx) enabling_context_free (EnablingContext *ctx)
{ {
g_simple_async_result_complete_in_idle (ctx->result);
g_object_unref (ctx->self);
g_object_unref (ctx->result);
g_object_unref (ctx->cancellable);
if (ctx->skeleton) if (ctx->skeleton)
g_object_unref (ctx->skeleton); g_object_unref (ctx->skeleton);
g_free (ctx); g_free (ctx);
} }
static gboolean
enabling_context_complete_and_free_if_cancelled (EnablingContext *ctx)
{
if (!g_cancellable_is_cancelled (ctx->cancellable))
return FALSE;
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_CANCELLED,
"Interface enabling cancelled");
enabling_context_complete_and_free (ctx);
return TRUE;
}
gboolean gboolean
mm_iface_modem_oma_enable_finish (MMIfaceModemOma *self, mm_iface_modem_oma_enable_finish (MMIfaceModemOma *self,
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
load_features_ready (MMIfaceModemOma *self, load_features_ready (MMIfaceModemOma *self,
GAsyncResult *res, GAsyncResult *res,
EnablingContext *ctx) GTask *task)
{ {
EnablingContext *ctx;
GError *error = NULL; GError *error = NULL;
MMOmaFeature features; MMOmaFeature features;
features = MM_IFACE_MODEM_OMA_GET_INTERFACE (self)->load_features_finish (self, res, &error); features = MM_IFACE_MODEM_OMA_GET_INTERFACE (self)->load_features_finish (self, res, &error);
if (error) { if (error) {
g_simple_async_result_take_error (ctx->result, error); g_task_return_error (task, error);
enabling_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
ctx = g_task_get_task_data (task);
/* Update in the interface */ /* Update in the interface */
mm_gdbus_modem_oma_set_features (ctx->skeleton, features); mm_gdbus_modem_oma_set_features (ctx->skeleton, features);
/* Go on to next step */ /* Go on to next step */
ctx->step++; ctx->step++;
interface_enabling_step (ctx); interface_enabling_step (task);
} }
static void static void
setup_unsolicited_events_ready (MMIfaceModemOma *self, setup_unsolicited_events_ready (MMIfaceModemOma *self,
GAsyncResult *res, GAsyncResult *res,
EnablingContext *ctx) GTask *task)
{ {
EnablingContext *ctx;
GError *error = NULL; GError *error = NULL;
MM_IFACE_MODEM_OMA_GET_INTERFACE (self)->setup_unsolicited_events_finish (self, res, &error); MM_IFACE_MODEM_OMA_GET_INTERFACE (self)->setup_unsolicited_events_finish (self, res, &error);
if (error) { if (error) {
g_simple_async_result_take_error (ctx->result, error); g_task_return_error (task, error);
enabling_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
/* Go on to next step */ /* Go on to next step */
ctx = g_task_get_task_data (task);
ctx->step++; ctx->step++;
interface_enabling_step (ctx); interface_enabling_step (task);
} }
static void static void
enable_unsolicited_events_ready (MMIfaceModemOma *self, enable_unsolicited_events_ready (MMIfaceModemOma *self,
GAsyncResult *res, GAsyncResult *res,
EnablingContext *ctx) GTask *task)
{ {
EnablingContext *ctx;
GError *error = NULL; GError *error = NULL;
/* Not critical! */ /* Not critical! */
@@ -902,16 +887,25 @@ enable_unsolicited_events_ready (MMIfaceModemOma *self,
} }
/* Go on with next step */ /* Go on with next step */
ctx = g_task_get_task_data (task);
ctx->step++; ctx->step++;
interface_enabling_step (ctx); interface_enabling_step (task);
} }
static void static void
interface_enabling_step (EnablingContext *ctx) interface_enabling_step (GTask *task)
{ {
MMIfaceModemOma *self;
EnablingContext *ctx;
/* Don't run new steps if we're cancelled */ /* Don't run new steps if we're cancelled */
if (enabling_context_complete_and_free_if_cancelled (ctx)) if (g_task_return_error_if_cancelled (task)) {
g_object_unref (task);
return; return;
}
self = g_task_get_source_object (task);
ctx = g_task_get_task_data (task);
switch (ctx->step) { switch (ctx->step) {
case ENABLING_STEP_FIRST: case ENABLING_STEP_FIRST:
@@ -919,12 +913,12 @@ interface_enabling_step (EnablingContext *ctx)
ctx->step++; ctx->step++;
case ENABLING_STEP_LOAD_FEATURES: case ENABLING_STEP_LOAD_FEATURES:
if (MM_IFACE_MODEM_OMA_GET_INTERFACE (ctx->self)->load_features && if (MM_IFACE_MODEM_OMA_GET_INTERFACE (self)->load_features &&
MM_IFACE_MODEM_OMA_GET_INTERFACE (ctx->self)->load_features_finish) { MM_IFACE_MODEM_OMA_GET_INTERFACE (self)->load_features_finish) {
MM_IFACE_MODEM_OMA_GET_INTERFACE (ctx->self)->load_features ( MM_IFACE_MODEM_OMA_GET_INTERFACE (self)->load_features (
ctx->self, self,
(GAsyncReadyCallback)load_features_ready, (GAsyncReadyCallback)load_features_ready,
ctx); task);
return; return;
} }
/* Fall down to next step */ /* Fall down to next step */
@@ -932,12 +926,12 @@ interface_enabling_step (EnablingContext *ctx)
case ENABLING_STEP_SETUP_UNSOLICITED_EVENTS: case ENABLING_STEP_SETUP_UNSOLICITED_EVENTS:
/* Allow setting up unsolicited events */ /* Allow setting up unsolicited events */
if (MM_IFACE_MODEM_OMA_GET_INTERFACE (ctx->self)->setup_unsolicited_events && if (MM_IFACE_MODEM_OMA_GET_INTERFACE (self)->setup_unsolicited_events &&
MM_IFACE_MODEM_OMA_GET_INTERFACE (ctx->self)->setup_unsolicited_events_finish) { MM_IFACE_MODEM_OMA_GET_INTERFACE (self)->setup_unsolicited_events_finish) {
MM_IFACE_MODEM_OMA_GET_INTERFACE (ctx->self)->setup_unsolicited_events ( MM_IFACE_MODEM_OMA_GET_INTERFACE (self)->setup_unsolicited_events (
ctx->self, self,
(GAsyncReadyCallback)setup_unsolicited_events_ready, (GAsyncReadyCallback)setup_unsolicited_events_ready,
ctx); task);
return; return;
} }
/* Fall down to next step */ /* Fall down to next step */
@@ -945,12 +939,12 @@ interface_enabling_step (EnablingContext *ctx)
case ENABLING_STEP_ENABLE_UNSOLICITED_EVENTS: case ENABLING_STEP_ENABLE_UNSOLICITED_EVENTS:
/* Allow setting up unsolicited events */ /* Allow setting up unsolicited events */
if (MM_IFACE_MODEM_OMA_GET_INTERFACE (ctx->self)->enable_unsolicited_events && if (MM_IFACE_MODEM_OMA_GET_INTERFACE (self)->enable_unsolicited_events &&
MM_IFACE_MODEM_OMA_GET_INTERFACE (ctx->self)->enable_unsolicited_events_finish) { MM_IFACE_MODEM_OMA_GET_INTERFACE (self)->enable_unsolicited_events_finish) {
MM_IFACE_MODEM_OMA_GET_INTERFACE (ctx->self)->enable_unsolicited_events ( MM_IFACE_MODEM_OMA_GET_INTERFACE (self)->enable_unsolicited_events (
ctx->self, self,
(GAsyncReadyCallback)enable_unsolicited_events_ready, (GAsyncReadyCallback)enable_unsolicited_events_ready,
ctx); task);
return; return;
} }
/* Fall down to next step */ /* Fall down to next step */
@@ -958,8 +952,8 @@ interface_enabling_step (EnablingContext *ctx)
case ENABLING_STEP_LAST: case ENABLING_STEP_LAST:
/* We are done without errors! */ /* We are done without errors! */
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE); g_task_return_boolean (task, TRUE);
enabling_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
@@ -973,28 +967,27 @@ mm_iface_modem_oma_enable (MMIfaceModemOma *self,
gpointer user_data) gpointer user_data)
{ {
EnablingContext *ctx; EnablingContext *ctx;
GTask *task;
ctx = g_new0 (EnablingContext, 1); ctx = g_new0 (EnablingContext, 1);
ctx->self = g_object_ref (self);
ctx->cancellable = g_object_ref (cancellable);
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
mm_iface_modem_oma_enable);
ctx->step = ENABLING_STEP_FIRST; ctx->step = ENABLING_STEP_FIRST;
g_object_get (ctx->self,
task = g_task_new (self, cancellable, callback, user_data);
g_task_set_task_data (task, ctx, (GDestroyNotify)enabling_context_free);
g_object_get (self,
MM_IFACE_MODEM_OMA_DBUS_SKELETON, &ctx->skeleton, MM_IFACE_MODEM_OMA_DBUS_SKELETON, &ctx->skeleton,
NULL); NULL);
if (!ctx->skeleton) { if (!ctx->skeleton) {
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,
"Couldn't get interface skeleton"); "Couldn't get interface skeleton");
enabling_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
interface_enabling_step (ctx); interface_enabling_step (task);
} }
/*****************************************************************************/ /*****************************************************************************/