mbm: wait for unlock reporting READY after PIN/PUK unlock

MBM modems will lie about the unlock status just after having sent a
correct PIN or PUK. So, explicitly wait to get in READY state after having
sent the PIN or PUK, before keeping on.

https://bugzilla.gnome.org/show_bug.cgi?id=696702
This commit is contained in:
Aleksander Morgado
2013-03-29 13:47:16 +01:00
parent 477623f0a0
commit b9b5ca39df
4 changed files with 317 additions and 1 deletions

View File

@@ -113,7 +113,9 @@ libmm_plugin_mbm_la_SOURCES = \
mbm/mm-broadband-modem-mbm.c \
mbm/mm-broadband-modem-mbm.h \
mbm/mm-broadband-bearer-mbm.c \
mbm/mm-broadband-bearer-mbm.h
mbm/mm-broadband-bearer-mbm.h \
mbm/mm-sim-mbm.c \
mbm/mm-sim-mbm.h
libmm_plugin_mbm_la_CPPFLAGS = $(PLUGIN_COMMON_COMPILER_FLAGS)
libmm_plugin_mbm_la_LDFLAGS = $(PLUGIN_COMMON_LINKER_FLAGS)
udevrules_DATA += mbm/77-mm-ericsson-mbm.rules

View File

@@ -37,6 +37,7 @@
#include "mm-modem-helpers.h"
#include "mm-broadband-modem-mbm.h"
#include "mm-broadband-bearer-mbm.h"
#include "mm-sim-mbm.h"
#include "mm-base-modem-at.h"
#include "mm-iface-modem.h"
#include "mm-iface-modem-3gpp.h"
@@ -130,6 +131,29 @@ modem_create_bearer (MMIfaceModem *self,
result);
}
/*****************************************************************************/
/* Create SIM (Modem interface) */
static MMSim *
create_sim_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return mm_sim_mbm_new_finish (res, error);
}
static void
create_sim (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
/* New MBM SIM */
mm_sim_mbm_new (MM_BASE_MODEM (self),
NULL, /* cancellable */
callback,
user_data);
}
/*****************************************************************************/
/* After SIM unlock (Modem interface) */
@@ -1206,6 +1230,8 @@ iface_modem_init (MMIfaceModem *iface)
{
iface->create_bearer = modem_create_bearer;
iface->create_bearer_finish = modem_create_bearer_finish;
iface->create_sim = create_sim;
iface->create_sim_finish = create_sim_finish;
iface->modem_after_sim_unlock = modem_after_sim_unlock;
iface->modem_after_sim_unlock_finish = modem_after_sim_unlock_finish;
iface->load_allowed_modes = load_allowed_modes;

237
plugins/mbm/mm-sim-mbm.c Normal file
View File

@@ -0,0 +1,237 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org>
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <ModemManager.h>
#define _LIBMM_INSIDE_MM
#include <libmm-glib.h>
#include "mm-log.h"
#include "mm-base-modem-at.h"
#include "mm-sim-mbm.h"
G_DEFINE_TYPE (MMSimMbm, mm_sim_mbm, MM_TYPE_SIM);
/*****************************************************************************/
/* SEND PIN/PUK (Generic implementation) */
typedef struct {
MMSimMbm *self;
MMBaseModem *modem;
GSimpleAsyncResult *result;
MMModemLock expected;
guint retries;
} SendPinPukContext;
static void
send_pin_puk_context_complete_and_free (SendPinPukContext *ctx)
{
g_simple_async_result_complete (ctx->result);
g_object_unref (ctx->result);
g_object_unref (ctx->modem);
g_object_unref (ctx->self);
g_slice_free (SendPinPukContext, ctx);
}
static gboolean
common_send_pin_puk_finish (MMSim *self,
GAsyncResult *res,
GError **error)
{
return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error);
}
static void wait_for_unlocked_status (SendPinPukContext *ctx);
static void
cpin_query_ready (MMBaseModem *modem,
GAsyncResult *res,
SendPinPukContext *ctx)
{
const gchar *result;
result = mm_base_modem_at_command_finish (modem, res, NULL);
if (result && strstr (result, "READY")) {
/* All done! */
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
send_pin_puk_context_complete_and_free (ctx);
return;
}
/* Need to recheck */
wait_for_unlocked_status (ctx);
}
static gboolean
cpin_query_cb (SendPinPukContext *ctx)
{
mm_base_modem_at_command (ctx->modem,
"+CPIN?",
20,
FALSE,
(GAsyncReadyCallback)cpin_query_ready,
ctx);
return FALSE;
}
static void
wait_for_unlocked_status (SendPinPukContext *ctx)
{
/* Oops... :/ */
if (ctx->retries == 0) {
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"PIN was sent but modem didn't report unlocked");
send_pin_puk_context_complete_and_free (ctx);
return;
}
/* Check status */
ctx->retries--;
mm_dbg ("Scheduling lock state check...");
g_timeout_add_seconds (1, (GSourceFunc)cpin_query_cb, ctx);
}
static void
send_pin_puk_ready (MMBaseModem *modem,
GAsyncResult *res,
SendPinPukContext *ctx)
{
GError *error = NULL;
mm_base_modem_at_command_finish (modem, res, &error);
if (error) {
g_simple_async_result_take_error (ctx->result, error);
send_pin_puk_context_complete_and_free (ctx);
return;
}
/* No explicit error sending the PIN/PUK, now check status until we have the
* expected lock status */
ctx->retries = 3;
wait_for_unlocked_status (ctx);
}
static void
common_send_pin_puk (MMSim *self,
const gchar *pin,
const gchar *puk,
GAsyncReadyCallback callback,
gpointer user_data)
{
SendPinPukContext *ctx;
gchar *command;
ctx = g_slice_new (SendPinPukContext);
ctx->self = g_object_ref (self);
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
common_send_pin_puk);
g_object_get (ctx->self,
MM_SIM_MODEM, &ctx->modem,
NULL);
command = (puk ?
g_strdup_printf ("+CPIN=\"%s\",\"%s\"", puk, pin) :
g_strdup_printf ("+CPIN=\"%s\"", pin));
mm_base_modem_at_command (ctx->modem,
command,
3,
FALSE,
(GAsyncReadyCallback)send_pin_puk_ready,
ctx);
g_free (command);
}
static void
send_puk (MMSim *self,
const gchar *puk,
const gchar *new_pin,
GAsyncReadyCallback callback,
gpointer user_data)
{
common_send_pin_puk (self, new_pin, puk, callback, user_data);
}
static void
send_pin (MMSim *self,
const gchar *pin,
GAsyncReadyCallback callback,
gpointer user_data)
{
common_send_pin_puk (self, pin, NULL, callback, user_data);
}
/*****************************************************************************/
MMSim *
mm_sim_mbm_new_finish (GAsyncResult *res,
GError **error)
{
GObject *source;
GObject *sim;
source = g_async_result_get_source_object (res);
sim = g_async_initable_new_finish (G_ASYNC_INITABLE (source), res, error);
g_object_unref (source);
if (!sim)
return NULL;
/* Only export valid SIMs */
mm_sim_export (MM_SIM (sim));
return MM_SIM (sim);
}
void
mm_sim_mbm_new (MMBaseModem *modem,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_async_initable_new_async (MM_TYPE_SIM_MBM,
G_PRIORITY_DEFAULT,
cancellable,
callback,
user_data,
MM_SIM_MODEM, modem,
NULL);
}
static void
mm_sim_mbm_init (MMSimMbm *self)
{
}
static void
mm_sim_mbm_class_init (MMSimMbmClass *klass)
{
MMSimClass *sim_class = MM_SIM_CLASS (klass);
sim_class->send_pin = send_pin;
sim_class->send_pin_finish = common_send_pin_puk_finish;
sim_class->send_puk = send_puk;
sim_class->send_puk_finish = common_send_pin_puk_finish;
}

51
plugins/mbm/mm-sim-mbm.h Normal file
View File

@@ -0,0 +1,51 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2013 Aleksander Morgado <aleksander@gnu.org>
*/
#ifndef MM_SIM_MBM_H
#define MM_SIM_MBM_H
#include <glib.h>
#include <glib-object.h>
#include "mm-sim.h"
#define MM_TYPE_SIM_MBM (mm_sim_mbm_get_type ())
#define MM_SIM_MBM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_SIM_MBM, MMSimMbm))
#define MM_SIM_MBM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_SIM_MBM, MMSimMbmClass))
#define MM_IS_SIM_MBM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_SIM_MBM))
#define MM_IS_SIM_MBM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_SIM_MBM))
#define MM_SIM_MBM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_SIM_MBM, MMSimMbmClass))
typedef struct _MMSimMbm MMSimMbm;
typedef struct _MMSimMbmClass MMSimMbmClass;
struct _MMSimMbm {
MMSim parent;
};
struct _MMSimMbmClass {
MMSimClass parent;
};
GType mm_sim_mbm_get_type (void);
void mm_sim_mbm_new (MMBaseModem *modem,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
MMSim *mm_sim_mbm_new_finish (GAsyncResult *res,
GError **error);
#endif /* MM_SIM_MBM_H */