aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/Makefile.am3
-rw-r--r--cli/mmcli-common.c247
-rw-r--r--cli/mmcli-common.h14
-rw-r--r--cli/mmcli-modem.c62
-rw-r--r--cli/mmcli-sim.c475
-rw-r--r--cli/mmcli.c13
-rw-r--r--cli/mmcli.h8
7 files changed, 762 insertions, 60 deletions
diff --git a/cli/Makefile.am b/cli/Makefile.am
index 6c9bc51d..305353d0 100644
--- a/cli/Makefile.am
+++ b/cli/Makefile.am
@@ -17,7 +17,8 @@ mmcli_SOURCES = \
mmcli-manager.c \
mmcli-modem.c \
mmcli-modem-3gpp.c \
- mmcli-bearer.c
+ mmcli-bearer.c \
+ mmcli-sim.c
mmcli_LDADD = \
$(MMCLI_LIBS) \
diff --git a/cli/mmcli-common.c b/cli/mmcli-common.c
index 89e48323..33127699 100644
--- a/cli/mmcli-common.c
+++ b/cli/mmcli-common.c
@@ -268,26 +268,6 @@ mmcli_get_modem_sync (GDBusConnection *connection,
return found;
}
-static MMBearer *
-find_bearer_in_list (GList *list,
- const gchar *bearer_path)
-{
- GList *l;
-
- for (l = list; l; l = g_list_next (l)) {
- MMBearer *bearer = MM_BEARER (l->data);
-
- if (g_str_equal (mm_bearer_get_path (bearer), bearer_path)) {
- g_debug ("Bearer found at '%s'\n", bearer_path);
- return g_object_ref (bearer);
- }
- }
-
- g_printerr ("error: couldn't find bearer at '%s'\n", bearer_path);
- exit (EXIT_FAILURE);
- return NULL;
-}
-
typedef struct {
GSimpleAsyncResult *result;
GCancellable *cancellable;
@@ -340,6 +320,26 @@ mmcli_get_bearer_finish (GAsyncResult *res,
static void look_for_bearer_in_modem (GetBearerContext *ctx);
+static MMBearer *
+find_bearer_in_list (GList *list,
+ const gchar *bearer_path)
+{
+ GList *l;
+
+ for (l = list; l; l = g_list_next (l)) {
+ MMBearer *bearer = MM_BEARER (l->data);
+
+ if (g_str_equal (mm_bearer_get_path (bearer), bearer_path)) {
+ g_debug ("Bearer found at '%s'\n", bearer_path);
+ return g_object_ref (bearer);
+ }
+ }
+
+ g_printerr ("error: couldn't find bearer at '%s'\n", bearer_path);
+ exit (EXIT_FAILURE);
+ return NULL;
+}
+
static void
list_bearers_ready (MMModem *modem,
GAsyncResult *res,
@@ -495,6 +495,202 @@ mmcli_get_bearer_sync (GDBusConnection *connection,
return found;
}
+typedef struct {
+ GSimpleAsyncResult *result;
+ GCancellable *cancellable;
+ gchar *sim_path;
+ MMManager *manager;
+ MMObject *modem;
+ MMSim *sim;
+} GetSimContext;
+
+static void
+get_sim_context_free (GetSimContext *ctx)
+{
+ if (ctx->modem)
+ g_object_unref (ctx->modem);
+ if (ctx->cancellable)
+ g_object_unref (ctx->cancellable);
+ if (ctx->manager)
+ g_object_unref (ctx->manager);
+ if (ctx->sim)
+ g_object_unref (ctx->sim);
+ g_free (ctx->sim_path);
+ g_free (ctx);
+}
+
+static void
+get_sim_context_complete (GetSimContext *ctx)
+{
+ g_simple_async_result_complete (ctx->result);
+ g_object_unref (ctx->result);
+ ctx->result = NULL;
+}
+
+MMSim *
+mmcli_get_sim_finish (GAsyncResult *res,
+ MMManager **o_manager,
+ MMObject **o_object)
+{
+ GetSimContext *ctx;
+
+ ctx = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res));
+ if (o_manager)
+ *o_manager = g_object_ref (ctx->manager);
+ if (o_object)
+ *o_object = g_object_ref (ctx->modem);
+ return g_object_ref (ctx->sim);
+}
+
+static void
+get_sim_ready (MMModem *modem,
+ GAsyncResult *res,
+ GetSimContext *ctx)
+{
+ GError *error = NULL;
+
+ ctx->sim = mm_modem_get_sim_finish (modem, res, &error);
+ if (error) {
+ g_printerr ("error: couldn't get sim '%s' at '%s': '%s'\n",
+ ctx->sim_path,
+ mm_modem_get_path (modem),
+ error->message);
+ exit (EXIT_FAILURE);
+ }
+
+ g_simple_async_result_set_op_res_gpointer (
+ ctx->result,
+ ctx,
+ (GDestroyNotify)get_sim_context_free);
+ get_sim_context_complete (ctx);
+}
+
+static void
+get_sim_manager_ready (GDBusConnection *connection,
+ GAsyncResult *res,
+ GetSimContext *ctx)
+{
+ GList *l;
+ GList *modems;
+
+ ctx->manager = mmcli_get_manager_finish (res);
+
+ modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (ctx->manager));
+ if (!modems) {
+ g_printerr ("error: couldn't find sim at '%s': 'no modems found'\n",
+ ctx->sim_path);
+ exit (EXIT_FAILURE);
+ }
+
+ for (l = modems; l; l = g_list_next (l)) {
+ MMObject *object;
+ MMModem *modem;
+
+ object = MM_OBJECT (l->data);
+ modem = mm_object_get_modem (object);
+ if (g_str_equal (ctx->sim_path, mm_modem_get_sim_path (modem))) {
+ ctx->modem = g_object_ref (object);
+ mm_modem_get_sim (modem,
+ ctx->cancellable,
+ (GAsyncReadyCallback)get_sim_ready,
+ ctx);
+ break;
+ }
+ g_object_unref (modem);
+ }
+
+ if (!ctx->modem) {
+ g_printerr ("error: couldn't find sim at '%s'\n",
+ ctx->sim_path);
+ exit (EXIT_FAILURE);
+ }
+
+ g_list_foreach (modems, (GFunc)g_object_unref, NULL);
+ g_list_free (modems);
+}
+
+void
+mmcli_get_sim (GDBusConnection *connection,
+ const gchar *sim_path,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GetSimContext *ctx;
+
+ ctx = g_new0 (GetSimContext, 1);
+ ctx->sim_path = g_strdup (sim_path);
+ if (cancellable)
+ ctx->cancellable = g_object_ref (cancellable);
+ ctx->result = g_simple_async_result_new (G_OBJECT (connection),
+ callback,
+ user_data,
+ mmcli_get_modem);
+ mmcli_get_manager (connection,
+ cancellable,
+ (GAsyncReadyCallback)get_sim_manager_ready,
+ ctx);
+}
+
+MMSim *
+mmcli_get_sim_sync (GDBusConnection *connection,
+ const gchar *sim_path,
+ MMManager **o_manager,
+ MMObject **o_object)
+{
+ MMManager *manager;
+ GList *modems;
+ GList *l;
+ MMSim *found = NULL;
+
+ manager = mmcli_get_manager_sync (connection);
+ modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (manager));
+ if (!modems) {
+ g_printerr ("error: couldn't find sim at '%s': 'no modems found'\n",
+ sim_path);
+ exit (EXIT_FAILURE);
+ }
+
+ for (l = modems; !found && l; l = g_list_next (l)) {
+ GError *error = NULL;
+ MMObject *object;
+ MMModem *modem;
+
+ object = MM_OBJECT (l->data);
+ modem = mm_object_get_modem (object);
+ if (g_str_equal (sim_path, mm_modem_get_sim_path (modem))) {
+ found = mm_modem_get_sim_sync (modem, NULL, &error);
+ if (error) {
+ g_printerr ("error: couldn't get sim '%s' in modem '%s': '%s'\n",
+ sim_path,
+ mm_modem_get_path (modem),
+ error->message);
+ exit (EXIT_FAILURE);
+ }
+
+ if (o_object)
+ *o_object = g_object_ref (object);
+ }
+
+ g_object_unref (modem);
+ }
+
+ if (!found) {
+ g_printerr ("error: couldn't find sim at '%s'\n", sim_path);
+ exit (EXIT_FAILURE);
+ }
+
+ g_list_foreach (modems, (GFunc)g_object_unref, NULL);
+ g_list_free (modems);
+
+ if (o_manager)
+ *o_manager = manager;
+ else
+ g_object_unref (manager);
+
+ return found;
+}
+
const gchar *
mmcli_get_bearer_ip_method_string (MMBearerIpMethod method)
{
@@ -579,6 +775,7 @@ mmcli_get_3gpp_registration_state_string (MMModem3gppRegistrationState state)
/* Common options */
static gchar *modem_str;
static gchar *bearer_str;
+static gchar *sim_str;
static GOptionEntry entries[] = {
{ "modem", 'm', 0, G_OPTION_ARG_STRING, &modem_str,
@@ -589,6 +786,10 @@ static GOptionEntry entries[] = {
"Specify bearer by path. Shows bearer information if no action specified.",
"[PATH]"
},
+ { "sim", 's', 0, G_OPTION_ARG_STRING, &sim_str,
+ "Specify SIM by path. Shows SIM information if no action specified.",
+ "[PATH]"
+ },
{ NULL }
};
@@ -619,3 +820,9 @@ mmcli_get_common_bearer_string (void)
{
return bearer_str;
}
+
+const gchar *
+mmcli_get_common_sim_string (void)
+{
+ return sim_str;
+}
diff --git a/cli/mmcli-common.h b/cli/mmcli-common.h
index a0df66b3..9add4889 100644
--- a/cli/mmcli-common.h
+++ b/cli/mmcli-common.h
@@ -51,6 +51,19 @@ MMBearer *mmcli_get_bearer_sync (GDBusConnection *connection,
MMManager **manager,
MMObject **object);
+void mmcli_get_sim (GDBusConnection *connection,
+ const gchar *sim_path,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+MMSim *mmcli_get_sim_finish (GAsyncResult *res,
+ MMManager **manager,
+ MMObject **object);
+MMSim *mmcli_get_sim_sync (GDBusConnection *connection,
+ const gchar *sim_path,
+ MMManager **manager,
+ MMObject **object);
+
const gchar *mmcli_get_bearer_ip_method_string (MMBearerIpMethod method);
const gchar *mmcli_get_state_string (MMModemState state);
const gchar *mmcli_get_state_reason_string (MMModemStateChangeReason reason);
@@ -61,5 +74,6 @@ const gchar *mmcli_get_3gpp_registration_state_string (MMModem3gppRegistrationSt
GOptionGroup *mmcli_get_common_option_group (void);
const gchar *mmcli_get_common_modem_string (void);
const gchar *mmcli_get_common_bearer_string (void);
+const gchar *mmcli_get_common_sim_string (void);
#endif /* _MMCLI_COMMON_H_ */
diff --git a/cli/mmcli-modem.c b/cli/mmcli-modem.c
index 29a1d017..a4637317 100644
--- a/cli/mmcli-modem.c
+++ b/cli/mmcli-modem.c
@@ -223,8 +223,6 @@ print_bearer_short_info (MMBearer *bearer)
static void
print_modem_info (void)
{
- GError *error = NULL;
- MMSim *sim;
MMModemLock unlock_required;
gchar *prefixed_revision;
gchar *unlock;
@@ -233,8 +231,10 @@ print_modem_info (void)
/* Not the best thing to do, as we may be doing _get() calls twice, but
* easiest to maintain */
-#undef VALIDATE
-#define VALIDATE(str) (str ? str : "unknown")
+#undef VALIDATE_UNKNOWN
+#define VALIDATE_UNKNOWN(str) (str ? str : "unknown")
+#undef VALIDATE_NONE
+#define VALIDATE_NONE(str) (str ? str : "none")
/* Strings with mixed properties */
unlock_required = mm_modem_get_unlock_required (ctx->modem);
@@ -265,8 +265,8 @@ print_modem_info (void)
/* Global IDs */
g_print ("\n"
"%s (device id '%s')\n",
- VALIDATE (mm_modem_get_path (ctx->modem)),
- VALIDATE (mm_modem_get_device_identifier (ctx->modem)));
+ VALIDATE_UNKNOWN (mm_modem_get_path (ctx->modem)),
+ VALIDATE_UNKNOWN (mm_modem_get_device_identifier (ctx->modem)));
/* Hardware related stuff */
g_print (" -------------------------\n"
@@ -275,29 +275,29 @@ print_modem_info (void)
" | revision: '%s'\n"
" | capabilities: '%s'\n"
" | equipment id: '%s'\n",
- VALIDATE (mm_modem_get_manufacturer (ctx->modem)),
- VALIDATE (mm_modem_get_model (ctx->modem)),
- VALIDATE (prefixed_revision),
- VALIDATE (capabilities_string),
- VALIDATE (mm_modem_get_equipment_identifier (ctx->modem)));
+ VALIDATE_UNKNOWN (mm_modem_get_manufacturer (ctx->modem)),
+ VALIDATE_UNKNOWN (mm_modem_get_model (ctx->modem)),
+ VALIDATE_UNKNOWN (prefixed_revision),
+ VALIDATE_UNKNOWN (capabilities_string),
+ VALIDATE_UNKNOWN (mm_modem_get_equipment_identifier (ctx->modem)));
/* System related stuff */
g_print (" -------------------------\n"
" System | device: '%s'\n"
" | driver: '%s'\n"
" | plugin: '%s'\n",
- VALIDATE (mm_modem_get_device (ctx->modem)),
- VALIDATE (mm_modem_get_driver (ctx->modem)),
- VALIDATE (mm_modem_get_plugin (ctx->modem)));
+ VALIDATE_UNKNOWN (mm_modem_get_device (ctx->modem)),
+ VALIDATE_UNKNOWN (mm_modem_get_driver (ctx->modem)),
+ VALIDATE_UNKNOWN (mm_modem_get_plugin (ctx->modem)));
/* Status related stuff */
g_print (" -------------------------\n"
" Status | unlock: '%s'\n"
" | state: '%s'\n"
" | access tech: '%s'\n",
- VALIDATE (unlock),
- VALIDATE (mmcli_get_state_string (mm_modem_get_state (ctx->modem))),
- VALIDATE (access_technologies_string));
+ VALIDATE_UNKNOWN (unlock),
+ VALIDATE_UNKNOWN (mmcli_get_state_string (mm_modem_get_state (ctx->modem))),
+ VALIDATE_UNKNOWN (access_technologies_string));
/* If available, 3GPP related stuff */
if (ctx->modem_3gpp) {
@@ -306,31 +306,17 @@ print_modem_info (void)
" | operator id: '%s'\n"
" | operator name: '%s'\n"
" | registration: '%s'\n",
- VALIDATE (mm_modem_3gpp_get_imei (ctx->modem_3gpp)),
- VALIDATE (mm_modem_3gpp_get_operator_code (ctx->modem_3gpp)),
- VALIDATE (mm_modem_3gpp_get_operator_name (ctx->modem_3gpp)),
+ VALIDATE_UNKNOWN (mm_modem_3gpp_get_imei (ctx->modem_3gpp)),
+ VALIDATE_UNKNOWN (mm_modem_3gpp_get_operator_code (ctx->modem_3gpp)),
+ VALIDATE_UNKNOWN (mm_modem_3gpp_get_operator_name (ctx->modem_3gpp)),
mmcli_get_3gpp_registration_state_string (
mm_modem_3gpp_get_registration_state ((ctx->modem_3gpp))));
}
- /* SIM related stuff */
- sim = mm_modem_get_sim_sync (ctx->modem, NULL, &error);
- if (error) {
- g_warning ("Couldn't get SIM: '%s'", error->message);
- g_error_free (error);
- }
- if (sim) {
- g_print (" -------------------------\n"
- " SIM | imsi : '%s'\n"
- " | id : '%s'\n"
- " | operator id : '%s'\n"
- " | operator name : '%s'\n",
- VALIDATE (mm_sim_get_imsi (sim)),
- VALIDATE (mm_sim_get_identifier (sim)),
- VALIDATE (mm_sim_get_operator_identifier (sim)),
- VALIDATE (mm_sim_get_operator_name (sim)));
- g_object_unref (sim);
- }
+ /* SIM */
+ g_print (" -------------------------\n"
+ " SIM | path: '%s'\n",
+ VALIDATE_NONE (mm_modem_get_sim_path (ctx->modem)));
g_print ("\n");
g_free (access_technologies_string);
diff --git a/cli/mmcli-sim.c b/cli/mmcli-sim.c
new file mode 100644
index 00000000..aa39a91c
--- /dev/null
+++ b/cli/mmcli-sim.c
@@ -0,0 +1,475 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * mmcli -- Control sim status & access information from the command line
+ *
+ * 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 3 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (C) 2011 Aleksander Morgado <aleksander@gnu.org>
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <locale.h>
+#include <string.h>
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include <libmm-glib.h>
+
+#include "mmcli.h"
+#include "mmcli-common.h"
+
+/* Context */
+typedef struct {
+ MMManager *manager;
+ MMObject *object;
+ GCancellable *cancellable;
+ MMSim *sim;
+} Context;
+static Context *ctx;
+
+/* Options */
+static gboolean info_flag; /* set when no action found */
+static gchar *pin_str;
+static gchar *puk_str;
+static gboolean enable_pin_flag;
+static gboolean disable_pin_flag;
+static gchar *change_pin_str;
+
+static GOptionEntry entries[] = {
+ { "pin", 0, 0, G_OPTION_ARG_STRING, &pin_str,
+ "Send PIN code to a given SIM.",
+ "[PIN]"
+ },
+ { "puk", 0, 0, G_OPTION_ARG_STRING, &puk_str,
+ "Send PUK code to a given SIM (must send the new PIN with --pin).",
+ "[PUK]"
+ },
+ { "enable-pin", 0, 0, G_OPTION_ARG_NONE, &enable_pin_flag,
+ "Enable PIN request in a given SIM (must send the current PIN with --pin).",
+ NULL
+ },
+ { "disable-pin", 0, 0, G_OPTION_ARG_NONE, &disable_pin_flag,
+ "Disable PIN request in a given SIM (must send the current PIN with --pin).",
+ NULL
+ },
+ { "change-pin", 0, 0, G_OPTION_ARG_STRING, &change_pin_str,
+ "Change the PIN in a given SIM (must send the current PIN with --pin).",
+ "[New PIN]"
+ },
+ { NULL }
+};
+
+GOptionGroup *
+mmcli_sim_get_option_group (void)
+{
+ GOptionGroup *group;
+
+ /* Status options */
+ group = g_option_group_new ("sim",
+ "SIM options",
+ "Show SIM options",
+ NULL,
+ NULL);
+ g_option_group_add_entries (group, entries);
+
+ return group;
+}
+
+gboolean
+mmcli_sim_options_enabled (void)
+{
+ static guint n_actions = 0;
+ static gboolean checked = FALSE;
+
+ if (checked)
+ return !!n_actions;
+
+ n_actions = (!!puk_str +
+ enable_pin_flag +
+ disable_pin_flag +
+ !!change_pin_str);
+
+ if (n_actions == 1) {
+ if (!pin_str) {
+ g_printerr ("error: action requires also the PIN code\n");
+ exit (EXIT_FAILURE);
+ }
+ } else if (n_actions == 0)
+ n_actions += !!pin_str;
+
+ if (n_actions == 0 && mmcli_get_common_sim_string ()) {
+ /* default to info */
+ info_flag = TRUE;
+ n_actions++;
+ }
+
+ if (n_actions > 1) {
+ g_printerr ("error: too many sim actions requested\n");
+ exit (EXIT_FAILURE);
+ }
+
+ checked = TRUE;
+ return !!n_actions;
+}
+
+static void
+context_free (Context *ctx)
+{
+ if (!ctx)
+ return;
+
+ if (ctx->cancellable)
+ g_object_unref (ctx->cancellable);
+ if (ctx->sim)
+ g_object_unref (ctx->sim);
+ if (ctx->object)
+ g_object_unref (ctx->object);
+ if (ctx->manager)
+ g_object_unref (ctx->manager);
+ g_free (ctx);
+}
+
+void
+mmcli_sim_shutdown (void)
+{
+ context_free (ctx);
+}
+
+static void
+print_sim_info (MMSim *sim)
+{
+ /* Not the best thing to do, as we may be doing _get() calls twice, but
+ * easiest to maintain */
+#undef VALIDATE
+#define VALIDATE(str) (str ? str : "unknown")
+
+ g_print ("SIM '%s'\n",
+ mm_sim_get_path (sim));
+ g_print (" -------------------------\n"
+ " Properties | imsi : '%s'\n"
+ " | id : '%s'\n"
+ " | operator id : '%s'\n"
+ " | operator name : '%s'\n",
+ VALIDATE (mm_sim_get_imsi (sim)),
+ VALIDATE (mm_sim_get_identifier (sim)),
+ VALIDATE (mm_sim_get_operator_identifier (sim)),
+ VALIDATE (mm_sim_get_operator_name (sim)));
+}
+
+static void
+send_pin_process_reply (gboolean result,
+ const GError *error)
+{
+ if (!result) {
+ g_printerr ("error: couldn't send PIN code to the SIM: '%s'\n",
+ error ? error->message : "unknown error");
+ exit (EXIT_FAILURE);
+ }
+
+ g_print ("successfully sent PIN code to the SIM\n");
+}
+
+static void
+send_pin_ready (MMSim *sim,
+ GAsyncResult *result,
+ gpointer nothing)
+{
+ gboolean operation_result;
+ GError *error = NULL;
+
+ operation_result = mm_sim_send_pin_finish (sim, result, &error);
+ send_pin_process_reply (operation_result, error);
+
+ mmcli_async_operation_done ();
+}
+
+static void
+send_puk_process_reply (gboolean result,
+ const GError *error)
+{
+ if (!result) {
+ g_printerr ("error: couldn't send PUK code to the SIM: '%s'\n",
+ error ? error->message : "unknown error");
+ exit (EXIT_FAILURE);
+ }
+
+ g_print ("successfully sent PUK code to the SIM\n");
+}
+
+static void
+send_puk_ready (MMSim *sim,
+ GAsyncResult *result,
+ gpointer nothing)
+{
+ gboolean operation_result;
+ GError *error = NULL;
+
+ operation_result = mm_sim_send_puk_finish (sim, result, &error);
+ send_puk_process_reply (operation_result, error);
+
+ mmcli_async_operation_done ();
+}
+
+static void
+enable_pin_process_reply (gboolean result,
+ const GError *error)
+{
+ if (!result) {
+ g_printerr ("error: couldn't enable PIN code request in the SIM: '%s'\n",
+ error ? error->message : "unknown error");
+ exit (EXIT_FAILURE);
+ }
+
+ g_print ("successfully enabled PIN code request in the SIM\n");
+}
+
+static void
+enable_pin_ready (MMSim *sim,
+ GAsyncResult *result,
+ gpointer nothing)
+{
+ gboolean operation_result;
+ GError *error = NULL;
+
+ operation_result = mm_sim_enable_pin_finish (sim, result, &error);
+ enable_pin_process_reply (operation_result, error);
+
+ mmcli_async_operation_done ();
+}
+
+static void
+disable_pin_process_reply (gboolean result,
+ const GError *error)
+{
+ if (!result) {
+ g_printerr ("error: couldn't disable PIN code request in the SIM: '%s'\n",
+ error ? error->message : "unknown error");
+ exit (EXIT_FAILURE);
+ }
+
+ g_print ("successfully disabled PIN code request in the SIM\n");
+}
+
+static void
+disable_pin_ready (MMSim *sim,
+ GAsyncResult *result,
+ gpointer nothing)
+{
+ gboolean operation_result;
+ GError *error = NULL;
+
+ operation_result = mm_sim_disable_pin_finish (sim, result, &error);
+ disable_pin_process_reply (operation_result, error);
+
+ mmcli_async_operation_done ();
+}
+
+static void
+change_pin_process_reply (gboolean result,
+ const GError *error)
+{
+ if (!result) {
+ g_printerr ("error: couldn't change PIN code in the SIM: '%s'\n",
+ error ? error->message : "unknown error");
+ exit (EXIT_FAILURE);
+ }
+
+ g_print ("successfully changed PIN code in the SIM\n");
+}
+
+static void
+change_pin_ready (MMSim *sim,
+ GAsyncResult *result,
+ gpointer nothing)
+{
+ gboolean operation_result;
+ GError *error = NULL;
+
+ operation_result = mm_sim_change_pin_finish (sim, result, &error);
+ change_pin_process_reply (operation_result, error);
+
+ mmcli_async_operation_done ();
+}
+
+static void
+get_sim_ready (GObject *source,
+ GAsyncResult *result,
+ gpointer none)
+{
+ ctx->sim = mmcli_get_sim_finish (result,
+ &ctx->manager,
+ &ctx->object);
+
+ if (info_flag)
+ g_assert_not_reached ();
+
+ /* Requesting to enable PIN? */
+ if (enable_pin_flag) {
+ mm_sim_enable_pin (ctx->sim,
+ pin_str,
+ ctx->cancellable,
+ (GAsyncReadyCallback)enable_pin_ready,
+ NULL);
+ return;
+ }
+
+ /* Requesting to disable PIN? */
+ if (disable_pin_flag) {
+ mm_sim_disable_pin (ctx->sim,
+ pin_str,
+ ctx->cancellable,
+ (GAsyncReadyCallback)disable_pin_ready,
+ NULL);
+ return;
+ }
+
+ /* Requesting to change PIN? */
+ if (change_pin_str) {
+ mm_sim_change_pin (ctx->sim,
+ pin_str, /* current */
+ change_pin_str, /* new */
+ ctx->cancellable,
+ (GAsyncReadyCallback)change_pin_ready,
+ NULL);
+ return;
+ }
+
+ /* Requesting to send PUK? */
+ if (puk_str) {
+ mm_sim_send_puk (ctx->sim,
+ puk_str,
+ pin_str,
+ ctx->cancellable,
+ (GAsyncReadyCallback)send_puk_ready,
+ NULL);
+ return;
+ }
+
+ /* Requesting to send PIN? (always LAST check!) */
+ if (pin_str) {
+ mm_sim_send_pin (ctx->sim,
+ pin_str,
+ ctx->cancellable,
+ (GAsyncReadyCallback)send_pin_ready,
+ NULL);
+ return;
+ }
+
+ g_warn_if_reached ();
+}
+
+void
+mmcli_sim_run_asynchronous (GDBusConnection *connection,
+ GCancellable *cancellable)
+{
+ /* Initialize context */
+ ctx = g_new0 (Context, 1);
+ if (cancellable)
+ ctx->cancellable = g_object_ref (cancellable);
+
+ /* Get proper sim */
+ mmcli_get_sim (connection,
+ mmcli_get_common_sim_string (),
+ cancellable,
+ (GAsyncReadyCallback)get_sim_ready,
+ NULL);
+}
+
+void
+mmcli_sim_run_synchronous (GDBusConnection *connection)
+{
+ GError *error = NULL;
+
+ /* Initialize context */
+ ctx = g_new0 (Context, 1);
+ ctx->sim = mmcli_get_sim_sync (connection,
+ mmcli_get_common_sim_string (),
+ &ctx->manager,
+ &ctx->object);
+
+ /* Request to get info from SIM? */
+ if (info_flag) {
+ g_debug ("Printing sim info...");
+ print_sim_info (ctx->sim);
+ return;
+ }
+
+ /* Requesting to enable PIN? */
+ if (enable_pin_flag) {
+ gboolean operation_result;
+
+ operation_result = mm_sim_enable_pin_sync (ctx->sim,
+ pin_str,
+ NULL,
+ &error);
+ enable_pin_process_reply (operation_result, error);
+ return;
+ }
+
+ /* Requesting to disable PIN? */
+ if (disable_pin_flag) {
+ gboolean operation_result;
+
+ operation_result = mm_sim_disable_pin_sync (ctx->sim,
+ pin_str,
+ NULL,
+ &error);
+ disable_pin_process_reply (operation_result, error);
+ return;
+ }
+
+ /* Requesting to change PIN? */
+ if (change_pin_str) {
+ gboolean operation_result;
+
+ operation_result = mm_sim_change_pin_sync (ctx->sim,
+ pin_str, /* current */
+ change_pin_str, /* new */
+ NULL,
+ &error);
+ change_pin_process_reply (operation_result, error);
+ return;
+ }
+
+ /* Requesting to send PUK? */
+ if (puk_str) {
+ gboolean operation_result;
+
+ operation_result = mm_sim_send_puk_sync (ctx->sim,
+ puk_str,
+ pin_str,
+ NULL,
+ &error);
+ send_puk_process_reply (operation_result, error);
+ return;
+ }
+
+ /* Requesting to send PIN? (always LAST check!) */
+ if (pin_str) {
+ gboolean operation_result;
+
+ operation_result = mm_sim_send_pin_sync (ctx->sim,
+ pin_str,
+ NULL,
+ &error);
+ send_pin_process_reply (operation_result, error);
+ return;
+ }
+
+ g_warn_if_reached ();
+}
diff --git a/cli/mmcli.c b/cli/mmcli.c
index 8bb5fc29..1ff7f938 100644
--- a/cli/mmcli.c
+++ b/cli/mmcli.c
@@ -185,6 +185,8 @@ main (gint argc, gchar **argv)
g_option_context_add_group (context,
mmcli_modem_3gpp_get_option_group ());
g_option_context_add_group (context,
+ mmcli_sim_get_option_group ());
+ g_option_context_add_group (context,
mmcli_bearer_get_option_group ());
g_option_context_add_main_entries (context, main_entries, NULL);
g_option_context_parse (context, &argc, &argv, NULL);
@@ -227,6 +229,13 @@ main (gint argc, gchar **argv)
else
mmcli_manager_run_synchronous (connection);
}
+ /* Sim options? */
+ else if (mmcli_sim_options_enabled ()) {
+ if (async_flag)
+ mmcli_sim_run_asynchronous (connection, cancellable);
+ else
+ mmcli_sim_run_synchronous (connection);
+ }
/* Bearer options? */
else if (mmcli_bearer_options_enabled ()) {
if (async_flag)
@@ -267,7 +276,9 @@ main (gint argc, gchar **argv)
mmcli_modem_shutdown ();
} else if (mmcli_modem_3gpp_options_enabled ()) {
mmcli_modem_3gpp_shutdown ();
- } else if (mmcli_bearer_options_enabled ()) {
+ } else if (mmcli_sim_options_enabled ()) {
+ mmcli_sim_shutdown ();
+ } else if (mmcli_bearer_options_enabled ()) {
mmcli_bearer_shutdown ();
}
diff --git a/cli/mmcli.h b/cli/mmcli.h
index e2323b59..3c241588 100644
--- a/cli/mmcli.h
+++ b/cli/mmcli.h
@@ -60,4 +60,12 @@ void mmcli_bearer_run_asynchronous (GDBusConnection *connection,
void mmcli_bearer_run_synchronous (GDBusConnection *connection);
void mmcli_bearer_shutdown (void);
+/* SIM group */
+GOptionGroup *mmcli_sim_get_option_group (void);
+gboolean mmcli_sim_options_enabled (void);
+void mmcli_sim_run_asynchronous (GDBusConnection *connection,
+ GCancellable *cancellable);
+void mmcli_sim_run_synchronous (GDBusConnection *connection);
+void mmcli_sim_shutdown (void);
+
#endif /* __MMCLI_H__ */