iface-modem: port mm_iface_modem_enable to use GTask

This commit is contained in:
Ben Chan
2017-06-30 12:00:09 +02:00
committed by Aleksander Morgado
parent 66f10d61a2
commit 00a092db2e
+71 -75
View File
@@ -3501,7 +3501,7 @@ mm_iface_modem_disable (MMIfaceModem *self,
/* MODEM ENABLING */
typedef struct _EnablingContext EnablingContext;
static void interface_enabling_step (EnablingContext *ctx);
static void interface_enabling_step (GTask *task);
typedef enum {
ENABLING_STEP_FIRST,
@@ -3513,93 +3513,79 @@ typedef enum {
} EnablingStep;
struct _EnablingContext {
MMIfaceModem *self;
EnablingStep step;
MMModemCharset supported_charsets;
const MMModemCharset *current_charset;
GSimpleAsyncResult *result;
GCancellable *cancellable;
MmGdbusModem *skeleton;
};
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)
g_object_unref (ctx->skeleton);
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
mm_iface_modem_enable_finish (MMIfaceModem *self,
GAsyncResult *res,
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
enabling_set_power_state_ready (MMIfaceModem *self,
GAsyncResult *res,
EnablingContext *ctx)
GTask *task)
{
EnablingContext *ctx;
GError *error = NULL;
if (!mm_iface_modem_set_power_state_finish (self, res, &error)) {
g_simple_async_result_take_error (ctx->result, error);
enabling_context_complete_and_free (ctx);
g_task_return_error (task, error);
g_object_unref (task);
return;
}
/* Go on to next step */
ctx = g_task_get_task_data (task);
ctx->step++;
interface_enabling_step (ctx);
interface_enabling_step (task);
}
static void
setup_flow_control_ready (MMIfaceModem *self,
GAsyncResult *res,
EnablingContext *ctx)
GTask *task)
{
EnablingContext *ctx;
GError *error = NULL;
MM_IFACE_MODEM_GET_INTERFACE (self)->setup_flow_control_finish (self, res, &error);
if (error) {
g_simple_async_result_take_error (ctx->result, error);
enabling_context_complete_and_free (ctx);
g_task_return_error (task, error);
g_object_unref (task);
return;
}
/* Go on to next step */
ctx = g_task_get_task_data (task);
ctx->step++;
interface_enabling_step (ctx);
interface_enabling_step (task);
}
static void
load_supported_charsets_ready (MMIfaceModem *self,
GAsyncResult *res,
EnablingContext *ctx)
GTask *task)
{
EnablingContext *ctx;
GError *error = NULL;
ctx = g_task_get_task_data (task);
ctx->supported_charsets =
MM_IFACE_MODEM_GET_INTERFACE (self)->load_supported_charsets_finish (self, res, &error);
if (error) {
@@ -3609,16 +3595,19 @@ load_supported_charsets_ready (MMIfaceModem *self,
/* Go on to next step */
ctx->step++;
interface_enabling_step (ctx);
interface_enabling_step (task);
}
static void
setup_charset_ready (MMIfaceModem *self,
GAsyncResult *res,
EnablingContext *ctx)
GTask *task)
{
EnablingContext *ctx;
GError *error = NULL;
ctx = g_task_get_task_data (task);
if (!MM_IFACE_MODEM_GET_INTERFACE (self)->setup_charset_finish (self, res, &error)) {
mm_dbg ("couldn't set charset '%s': '%s'",
mm_modem_charset_to_string (*ctx->current_charset),
@@ -3630,7 +3619,7 @@ setup_charset_ready (MMIfaceModem *self,
/* Done, Go on to next step */
ctx->step++;
interface_enabling_step (ctx);
interface_enabling_step (task);
}
static const MMModemCharset best_charsets[] = {
@@ -3643,11 +3632,19 @@ static const MMModemCharset best_charsets[] = {
};
static void
interface_enabling_step (EnablingContext *ctx)
interface_enabling_step (GTask *task)
{
MMIfaceModem *self;
EnablingContext *ctx;
/* 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;
}
self = g_task_get_source_object (task);
ctx = g_task_get_task_data (task);
switch (ctx->step) {
case ENABLING_STEP_FIRST:
@@ -3655,31 +3652,31 @@ interface_enabling_step (EnablingContext *ctx)
ctx->step++;
case ENABLING_STEP_SET_POWER_STATE:
mm_iface_modem_set_power_state (ctx->self,
mm_iface_modem_set_power_state (self,
MM_MODEM_POWER_STATE_ON,
(GAsyncReadyCallback)enabling_set_power_state_ready,
ctx);
task);
return;
case ENABLING_STEP_FLOW_CONTROL:
if (MM_IFACE_MODEM_GET_INTERFACE (ctx->self)->setup_flow_control &&
MM_IFACE_MODEM_GET_INTERFACE (ctx->self)->setup_flow_control_finish) {
MM_IFACE_MODEM_GET_INTERFACE (ctx->self)->setup_flow_control (
ctx->self,
if (MM_IFACE_MODEM_GET_INTERFACE (self)->setup_flow_control &&
MM_IFACE_MODEM_GET_INTERFACE (self)->setup_flow_control_finish) {
MM_IFACE_MODEM_GET_INTERFACE (self)->setup_flow_control (
self,
(GAsyncReadyCallback)setup_flow_control_ready,
ctx);
task);
return;
}
/* Fall down to next step */
ctx->step++;
case ENABLING_STEP_SUPPORTED_CHARSETS:
if (MM_IFACE_MODEM_GET_INTERFACE (ctx->self)->load_supported_charsets &&
MM_IFACE_MODEM_GET_INTERFACE (ctx->self)->load_supported_charsets_finish) {
MM_IFACE_MODEM_GET_INTERFACE (ctx->self)->load_supported_charsets (
ctx->self,
if (MM_IFACE_MODEM_GET_INTERFACE (self)->load_supported_charsets &&
MM_IFACE_MODEM_GET_INTERFACE (self)->load_supported_charsets_finish) {
MM_IFACE_MODEM_GET_INTERFACE (self)->load_supported_charsets (
self,
(GAsyncReadyCallback)load_supported_charsets_ready,
ctx);
task);
return;
}
/* Fall down to next step */
@@ -3688,8 +3685,8 @@ interface_enabling_step (EnablingContext *ctx)
case ENABLING_STEP_CHARSET:
/* Only try to set charsets if we were able to load supported ones */
if (ctx->supported_charsets > 0 &&
MM_IFACE_MODEM_GET_INTERFACE (ctx->self)->setup_charset &&
MM_IFACE_MODEM_GET_INTERFACE (ctx->self)->setup_charset_finish) {
MM_IFACE_MODEM_GET_INTERFACE (self)->setup_charset &&
MM_IFACE_MODEM_GET_INTERFACE (self)->setup_charset_finish) {
gboolean next_to_try = FALSE;
while (!next_to_try) {
@@ -3708,19 +3705,19 @@ interface_enabling_step (EnablingContext *ctx)
}
if (next_to_try) {
MM_IFACE_MODEM_GET_INTERFACE (ctx->self)->setup_charset (
ctx->self,
MM_IFACE_MODEM_GET_INTERFACE (self)->setup_charset (
self,
*ctx->current_charset,
(GAsyncReadyCallback)setup_charset_ready,
ctx);
task);
return;
}
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Failed to find a usable modem character set");
enabling_context_complete_and_free (ctx);
g_task_return_new_error (task,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Failed to find a usable modem character set");
g_object_unref (task);
return;
}
/* Fall down to next step */
@@ -3728,8 +3725,8 @@ interface_enabling_step (EnablingContext *ctx)
case ENABLING_STEP_LAST:
/* We are done without errors! */
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
enabling_context_complete_and_free (ctx);
g_task_return_boolean (task, TRUE);
g_object_unref (task);
return;
}
@@ -3743,28 +3740,27 @@ mm_iface_modem_enable (MMIfaceModem *self,
gpointer user_data)
{
EnablingContext *ctx;
GTask *task;
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_enable);
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_DBUS_SKELETON, &ctx->skeleton,
NULL);
if (!ctx->skeleton) {
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't get interface skeleton");
enabling_context_complete_and_free (ctx);
g_task_return_new_error (task,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't get interface skeleton");
g_object_unref (task);
return;
}
interface_enabling_step (ctx);
interface_enabling_step (task);
}
/*****************************************************************************/