mbm: port common_send_pin_puk to use GTask

This commit is contained in:
Ben Chan
2017-09-26 05:29:45 -07:00
committed by Aleksander Morgado
parent 5769d52d3b
commit 9f573d3bcf

View File

@@ -34,20 +34,15 @@ G_DEFINE_TYPE (MMSimMbm, mm_sim_mbm, MM_TYPE_BASE_SIM)
/* SEND PIN/PUK (Generic implementation) */ /* SEND PIN/PUK (Generic implementation) */
typedef struct { typedef struct {
MMSimMbm *self;
MMBaseModem *modem; MMBaseModem *modem;
GSimpleAsyncResult *result;
MMModemLock expected; MMModemLock expected;
guint retries; guint retries;
} SendPinPukContext; } SendPinPukContext;
static void static void
send_pin_puk_context_complete_and_free (SendPinPukContext *ctx) send_pin_puk_context_free (SendPinPukContext *ctx)
{ {
g_simple_async_result_complete (ctx->result);
g_object_unref (ctx->result);
g_object_unref (ctx->modem); g_object_unref (ctx->modem);
g_object_unref (ctx->self);
g_slice_free (SendPinPukContext, ctx); g_slice_free (SendPinPukContext, ctx);
} }
@@ -56,15 +51,15 @@ common_send_pin_puk_finish (MMBaseSim *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 wait_for_unlocked_status (SendPinPukContext *ctx); static void wait_for_unlocked_status (GTask *task);
static void static void
cpin_query_ready (MMBaseModem *modem, cpin_query_ready (MMBaseModem *modem,
GAsyncResult *res, GAsyncResult *res,
SendPinPukContext *ctx) GTask *task)
{ {
const gchar *result; const gchar *result;
@@ -72,64 +67,73 @@ cpin_query_ready (MMBaseModem *modem,
result = mm_base_modem_at_command_finish (modem, res, NULL); result = mm_base_modem_at_command_finish (modem, res, NULL);
if (result && strstr (result, "READY")) { if (result && strstr (result, "READY")) {
/* All done! */ /* All done! */
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE); g_task_return_boolean (task, TRUE);
send_pin_puk_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
/* Need to recheck */ /* Need to recheck */
wait_for_unlocked_status (ctx); wait_for_unlocked_status (task);
} }
static gboolean static gboolean
cpin_query_cb (SendPinPukContext *ctx) cpin_query_cb (GTask *task)
{ {
SendPinPukContext *ctx;
ctx = g_task_get_task_data (task);
mm_base_modem_at_command (ctx->modem, mm_base_modem_at_command (ctx->modem,
"+CPIN?", "+CPIN?",
20, 20,
FALSE, FALSE,
(GAsyncReadyCallback)cpin_query_ready, (GAsyncReadyCallback)cpin_query_ready,
ctx); task);
return G_SOURCE_REMOVE; return G_SOURCE_REMOVE;
} }
static void static void
wait_for_unlocked_status (SendPinPukContext *ctx) wait_for_unlocked_status (GTask *task)
{ {
SendPinPukContext *ctx;
ctx = g_task_get_task_data (task);
/* Oops... :/ */ /* Oops... :/ */
if (ctx->retries == 0) { if (ctx->retries == 0) {
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,
"PIN was sent but modem didn't report unlocked"); "PIN was sent but modem didn't report unlocked");
send_pin_puk_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
/* Check status */ /* Check status */
ctx->retries--; ctx->retries--;
mm_dbg ("Scheduling lock state check..."); mm_dbg ("Scheduling lock state check...");
g_timeout_add_seconds (1, (GSourceFunc)cpin_query_cb, ctx); g_timeout_add_seconds (1, (GSourceFunc)cpin_query_cb, task);
} }
static void static void
send_pin_puk_ready (MMBaseModem *modem, send_pin_puk_ready (MMBaseModem *modem,
GAsyncResult *res, GAsyncResult *res,
SendPinPukContext *ctx) GTask *task)
{ {
SendPinPukContext *ctx;
GError *error = NULL; GError *error = NULL;
mm_base_modem_at_command_finish (modem, res, &error); mm_base_modem_at_command_finish (modem, res, &error);
if (error) { if (error) {
g_simple_async_result_take_error (ctx->result, error); g_task_return_error (task, error);
send_pin_puk_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
/* No explicit error sending the PIN/PUK, now check status until we have the /* No explicit error sending the PIN/PUK, now check status until we have the
* expected lock status */ * expected lock status */
ctx = g_task_get_task_data (task);
ctx->retries = 3; ctx->retries = 3;
wait_for_unlocked_status (ctx); wait_for_unlocked_status (task);
} }
static void static void
@@ -140,18 +144,17 @@ common_send_pin_puk (MMBaseSim *self,
gpointer user_data) gpointer user_data)
{ {
SendPinPukContext *ctx; SendPinPukContext *ctx;
GTask *task;
gchar *command; gchar *command;
ctx = g_slice_new (SendPinPukContext); ctx = g_slice_new (SendPinPukContext);
ctx->self = g_object_ref (self); g_object_get (self,
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
common_send_pin_puk);
g_object_get (ctx->self,
MM_BASE_SIM_MODEM, &ctx->modem, MM_BASE_SIM_MODEM, &ctx->modem,
NULL); NULL);
task = g_task_new (self, NULL, callback, user_data);
g_task_set_task_data (task, ctx, (GDestroyNotify)send_pin_puk_context_free);
command = (puk ? command = (puk ?
g_strdup_printf ("+CPIN=\"%s\",\"%s\"", puk, pin) : g_strdup_printf ("+CPIN=\"%s\",\"%s\"", puk, pin) :
g_strdup_printf ("+CPIN=\"%s\"", pin)); g_strdup_printf ("+CPIN=\"%s\"", pin));
@@ -160,7 +163,7 @@ common_send_pin_puk (MMBaseSim *self,
3, 3,
FALSE, FALSE,
(GAsyncReadyCallback)send_pin_puk_ready, (GAsyncReadyCallback)send_pin_puk_ready,
ctx); task);
g_free (command); g_free (command);
} }