diff options
author | Aleksander Morgado <aleksander@lanedo.com> | 2012-06-27 17:40:35 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@lanedo.com> | 2012-07-03 16:08:57 +0200 |
commit | c064bb5a446ecc0b5f6ebd3c71e9094463de34d7 (patch) | |
tree | ef953e5e5bfe38ca4229bcfa98746a48067941d8 | |
parent | 20c56ede36788e1a61e52538d8bbb4a9e1202a06 (diff) |
cli: new `--dms-change-pin' action
Allows to change the PIN, e.g (from 1234 to 4321):
$> sudo qmicli -d /dev/cdc-wdm0 \
--dms-change-pin="PIN,1234,4321"
-rw-r--r-- | cli/qmicli-dms.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/cli/qmicli-dms.c b/cli/qmicli-dms.c index 57eb1d3..2e65efe 100644 --- a/cli/qmicli-dms.c +++ b/cli/qmicli-dms.c @@ -51,6 +51,7 @@ static gboolean get_power_state_flag; static gchar *set_pin_protection_str; static gchar *verify_pin_str; static gchar *unblock_pin_str; +static gchar *change_pin_str; static gboolean noop_flag; static GOptionEntry entries[] = { @@ -94,6 +95,10 @@ static GOptionEntry entries[] = { "Unblock PIN", "[(PIN|PIN2),(PUK),(new PIN)]", }, + { "dms-change-pin", 0, 0, G_OPTION_ARG_STRING, &change_pin_str, + "Change PIN", + "[(PIN|PIN2),(old PIN),(new PIN)]", + }, { "dms-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag, "Just allocate or release a DMS client. Use with `--client-no-release-cid' and/or `--client-cid'", NULL @@ -135,6 +140,7 @@ qmicli_dms_options_enabled (void) !!set_pin_protection_str + !!verify_pin_str + !!unblock_pin_str + + !!change_pin_str + noop_flag); if (n_actions > 1) { @@ -724,6 +730,96 @@ unblock_pin_ready (QmiClientDms *client, shutdown (TRUE); } +static QmiMessageDmsChangePinInput * +change_pin_input_create (const gchar *str) +{ + QmiMessageDmsChangePinInput *input; + QmiMessageDmsChangePinInputInfo info; + gchar **split; + + /* Prepare inputs. + * Format of the string is: + * "[(PIN|PIN2),(old PIN),(new PIN)]" + */ + input = qmi_message_dms_change_pin_input_new (); + + split = g_strsplit (str, ",", -1); + + if (!split[0] || split[0][0] == '\0') { + g_printerr ("error: expected 'PIN' or 'PIN2', got: none\n"); + exit (EXIT_FAILURE); + } else if (g_str_equal (split[0], "PIN")) + info.pin_id = QMI_DMS_PIN_ID_PIN; + else if (g_str_equal (split[0], "PIN2")) + info.pin_id = QMI_DMS_PIN_ID_PIN2; + else { + g_printerr ("error: expected 'PIN' or 'PIN2', got: '%s'\n", + split[0]); + exit (EXIT_FAILURE); + } + + info.old_pin = split[1]; + if (!info.old_pin || info.old_pin[0] == '\0') { + g_printerr ("error: empty old PIN given\n"); + exit (EXIT_FAILURE); + } + + info.new_pin = split[2]; + if (!info.new_pin || info.new_pin[0] == '\0') { + g_printerr ("error: empty new PIN given\n"); + exit (EXIT_FAILURE); + } + + qmi_message_dms_change_pin_input_set_info (input, info, NULL); + g_strfreev (split); + + return input; +} + +static void +change_pin_ready (QmiClientDms *client, + GAsyncResult *res) +{ + QmiMessageDmsChangePinOutput *output; + GError *error = NULL; + + output = qmi_client_dms_change_pin_finish (client, res, &error); + if (!output) { + g_printerr ("error: operation failed: %s\n", error->message); + g_error_free (error); + shutdown (FALSE); + return; + } + + if (!qmi_message_dms_change_pin_output_get_result (output, &error)) { + QmiMessageDmsChangePinOutputPinRetriesStatus retry_status; + + g_printerr ("error: couldn't change PIN: %s\n", error->message); + g_error_free (error); + + if (qmi_message_dms_change_pin_output_get_pin_retries_status (output, + &retry_status, + NULL)) { + g_printerr ("[%s] Retries left:\n" + "\tVerify: %u\n" + "\tUnblock: %u\n", + qmi_device_get_path_display (ctx->device), + retry_status.verify_retries_left, + retry_status.unblock_retries_left); + } + + qmi_message_dms_change_pin_output_unref (output); + shutdown (FALSE); + return; + } + + g_print ("[%s] PIN changed successfully\n", + qmi_device_get_path_display (ctx->device)); + + qmi_message_dms_change_pin_output_unref (output); + shutdown (TRUE); +} + static gboolean noop_cb (gpointer unused) { @@ -875,6 +971,22 @@ qmicli_dms_run (QmiDevice *device, return; } + /* Request to change the PIN? */ + if (change_pin_str) { + QmiMessageDmsChangePinInput *input; + + g_debug ("Asynchronously changing PIN..."); + input = change_pin_input_create (change_pin_str); + qmi_client_dms_change_pin (ctx->client, + input, + 10, + ctx->cancellable, + (GAsyncReadyCallback)change_pin_ready, + NULL); + qmi_message_dms_change_pin_input_unref (input); + return; + } + /* Just client allocate/release? */ if (noop_flag) { g_idle_add (noop_cb, NULL); |