simtech: port load_current_modes to GTask

This commit is contained in:
Aleksander Morgado
2017-09-13 16:11:16 +02:00
parent 9c1f9023e3
commit bf83bb7577

View File

@@ -536,21 +536,10 @@ typedef struct {
} LoadCurrentModesResult; } LoadCurrentModesResult;
typedef struct { typedef struct {
MMBroadbandModemSimtech *self;
GSimpleAsyncResult *result;
gint acqord; gint acqord;
gint modepref; gint modepref;
} LoadCurrentModesContext; } LoadCurrentModesContext;
static void
load_current_modes_context_complete_and_free (LoadCurrentModesContext *ctx)
{
g_simple_async_result_complete (ctx->result);
g_object_unref (ctx->result);
g_object_unref (ctx->self);
g_free (ctx);
}
static gboolean static gboolean
load_current_modes_finish (MMIfaceModem *self, load_current_modes_finish (MMIfaceModem *self,
GAsyncResult *res, GAsyncResult *res,
@@ -560,41 +549,43 @@ load_current_modes_finish (MMIfaceModem *self,
{ {
LoadCurrentModesResult *result; LoadCurrentModesResult *result;
if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error)) result = g_task_propagate_pointer (G_TASK (res), error);
if (!result)
return FALSE; return FALSE;
result = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res));
*allowed = result->allowed; *allowed = result->allowed;
*preferred = result->preferred; *preferred = result->preferred;
g_free (result);
return TRUE; return TRUE;
} }
static void static void
cnmp_query_ready (MMBroadbandModemSimtech *self, cnmp_query_ready (MMBroadbandModemSimtech *self,
GAsyncResult *res, GAsyncResult *res,
LoadCurrentModesContext *ctx) GTask *task)
{ {
LoadCurrentModesContext *ctx;
LoadCurrentModesResult *result; LoadCurrentModesResult *result;
const gchar *response, *p; const gchar *response, *p;
GError *error = NULL; GError *error = NULL;
response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error); response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error);
if (!response) { if (!response) {
/* Let the error be critical. */ g_task_return_error (task, error);
g_simple_async_result_take_error (ctx->result, error); g_object_unref (task);
load_current_modes_context_complete_and_free (ctx);
return; return;
} }
ctx = g_task_get_task_data (task);
p = mm_strip_tag (response, "+CNMP:"); p = mm_strip_tag (response, "+CNMP:");
if (!p) { if (!p) {
g_simple_async_result_set_error ( g_task_return_new_error (task,
ctx->result,
MM_CORE_ERROR, MM_CORE_ERROR,
MM_CORE_ERROR_FAILED, MM_CORE_ERROR_FAILED,
"Failed to parse the mode preference response: '%s'", "Failed to parse the mode preference response: '%s'",
response); response);
load_current_modes_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
@@ -620,13 +611,13 @@ cnmp_query_ready (MMBroadbandModemSimtech *self,
result->preferred = MM_MODEM_MODE_3G; result->preferred = MM_MODEM_MODE_3G;
break; break;
default: default:
g_simple_async_result_set_error ( g_task_return_new_error (
ctx->result, task,
MM_CORE_ERROR, MM_CORE_ERROR,
MM_CORE_ERROR_FAILED, MM_CORE_ERROR_FAILED,
"Unknown acquisition order preference: '%d'", "Unknown acquisition order preference: '%d'",
ctx->acqord); ctx->acqord);
load_current_modes_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
break; break;
@@ -644,51 +635,50 @@ cnmp_query_ready (MMBroadbandModemSimtech *self,
break; break;
default: default:
g_simple_async_result_set_error ( g_task_return_new_error (
ctx->result, task,
MM_CORE_ERROR, MM_CORE_ERROR,
MM_CORE_ERROR_FAILED, MM_CORE_ERROR_FAILED,
"Unknown mode preference: '%d'", "Unknown mode preference: '%d'",
ctx->modepref); ctx->modepref);
load_current_modes_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
/* Set final result and complete */ g_task_return_pointer (task, result, g_free);
g_simple_async_result_set_op_res_gpointer (ctx->result, g_object_unref (task);
result,
g_free);
load_current_modes_context_complete_and_free (ctx);
} }
static void static void
cnaop_query_ready (MMBroadbandModemSimtech *self, cnaop_query_ready (MMBaseModem *self,
GAsyncResult *res, GAsyncResult *res,
LoadCurrentModesContext *ctx) GTask *task)
{ {
LoadCurrentModesContext *ctx;
const gchar *response, *p; const gchar *response, *p;
GError *error = NULL; GError *error = NULL;
response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error); response = mm_base_modem_at_command_finish (self, res, &error);
if (!response) { if (!response) {
/* Let the error be critical. */ g_task_return_error (task, error);
g_simple_async_result_take_error (ctx->result, error); g_object_unref (task);
load_current_modes_context_complete_and_free (ctx);
return; return;
} }
ctx = g_task_get_task_data (task);
p = mm_strip_tag (response, "+CNAOP:"); p = mm_strip_tag (response, "+CNAOP:");
if (p) if (p)
ctx->acqord = atoi (p); ctx->acqord = atoi (p);
if (ctx->acqord < 0 || ctx->acqord > 2) { if (ctx->acqord < 0 || ctx->acqord > 2) {
g_simple_async_result_set_error ( g_task_return_new_error (
ctx->result, task,
MM_CORE_ERROR, MM_CORE_ERROR,
MM_CORE_ERROR_FAILED, MM_CORE_ERROR_FAILED,
"Failed to parse the acquisition order response: '%s'", "Failed to parse the acquisition order response: '%s'",
response); response);
load_current_modes_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
@@ -698,7 +688,7 @@ cnaop_query_ready (MMBroadbandModemSimtech *self,
3, 3,
FALSE, FALSE,
(GAsyncReadyCallback)cnmp_query_ready, (GAsyncReadyCallback)cnmp_query_ready,
ctx); task);
} }
static void static void
@@ -706,24 +696,23 @@ load_current_modes (MMIfaceModem *self,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
GTask *task;
LoadCurrentModesContext *ctx; LoadCurrentModesContext *ctx;
ctx = g_new (LoadCurrentModesContext, 1); ctx = g_new (LoadCurrentModesContext, 1);
ctx->self = g_object_ref (self);
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
load_current_modes);
ctx->acqord = -1; ctx->acqord = -1;
ctx->modepref = -1; ctx->modepref = -1;
task = g_task_new (self, NULL, callback, user_data);
g_task_set_task_data (task, ctx, g_free);
mm_base_modem_at_command ( mm_base_modem_at_command (
MM_BASE_MODEM (self), MM_BASE_MODEM (self),
"+CNAOP?", "+CNAOP?",
3, 3,
FALSE, FALSE,
(GAsyncReadyCallback)cnaop_query_ready, (GAsyncReadyCallback)cnaop_query_ready,
ctx); task);
} }
/*****************************************************************************/ /*****************************************************************************/