uim: add --uim-get-sim-state
authorDaniel Golle <daniel@makrotopia.org>
Sat, 6 Nov 2021 02:02:00 +0000 (02:02 +0000)
committerDaniel Golle <daniel@makrotopia.org>
Sat, 6 Nov 2021 02:42:50 +0000 (02:42 +0000)
Provide same output as --get-pin-status for SIM/modem which require
using uim instead of dms command for that.
For now only the first present SIM card with only the first application
on the card is supported (which should cover the vast majority of
cases and allows to remain compatible with --get-pin-status output)

Suggested-by: Arjun AK <arjunak234@gmail.com>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
commands-dms.c
commands-dms.h
commands-uim.c
commands-uim.h

index 8cc7bc5017f3eee78a0082c96fe6ab25d15e1e7c..954dacad510d01ff5e6d5149b328252532383302 100644 (file)
@@ -80,7 +80,7 @@ cmd_dms_get_capabilities_prepare(struct qmi_dev *qmi, struct qmi_request *req, s
        return QMI_CMD_REQUEST;
 }
 
-static const char *get_pin_status(int status)
+const char *get_pin_status(int status)
 {
        static const char *pin_status[] = {
                [QMI_DMS_UIM_PIN_STATUS_NOT_INITIALIZED] = "not_initialized",
index 82ae592d885668cf63cc24f80effdbd60657924c..eea830877ce1fdd2433329d3207db640c9e410ce 100644 (file)
@@ -73,3 +73,4 @@
                "                                     mode_only_low_power)\n" \
                "  --fcc-auth:                       Set FCC authentication\n" \
 
+const char *get_pin_status(int status);
index d99bce9fd7d0d0e6e0ededb04009d0ffa64a3c59..3bd8a7449942d6e4f7cef791db640c7b65f06f1d 100644 (file)
@@ -54,3 +54,62 @@ cmd_uim_verify_pin2_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct
        qmi_set_uim_verify_pin_request(msg, &data);
        return QMI_CMD_REQUEST;
 }
+
+static void cmd_uim_get_sim_state_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
+{
+       struct qmi_uim_get_card_status_response res;
+       void * const card_table = blobmsg_open_table(&status, NULL);
+
+       qmi_parse_uim_get_card_status_response(msg, &res);
+
+       for (int i = 0; i < res.data.card_status.cards_n; ++i) {
+               if (res.data.card_status.cards[i].card_state != QMI_UIM_CARD_STATE_PRESENT)
+                       continue;
+
+               uint8_t pin1_state = res.data.card_status.cards[i].upin_state;
+               uint8_t pin1_retries = res.data.card_status.cards[i].upin_retries;
+               uint8_t puk1_retries = res.data.card_status.cards[i].upuk_retries;
+               uint8_t pin2_state;
+               uint8_t pin2_retries;
+               uint8_t puk2_retries;
+               bool has_pin2 = false;
+
+               for (int j = 0; j < res.data.card_status.cards[i].applications_n; ++j) {
+                       if (res.data.card_status.cards[i].applications[j].type == QMI_UIM_CARD_APPLICATION_TYPE_UNKNOWN)
+                               continue;
+
+                       if (!res.data.card_status.cards[i].applications[j].upin_replaces_pin1) {
+                               pin1_state = res.data.card_status.cards[i].applications[j].pin1_state;
+                               pin1_retries = res.data.card_status.cards[i].applications[j].pin1_retries;
+                               puk1_retries = res.data.card_status.cards[i].applications[j].puk1_retries;
+                       }
+
+                       pin2_state = res.data.card_status.cards[i].applications[j].pin2_state;
+                       pin2_retries = res.data.card_status.cards[i].applications[j].pin2_retries;
+                       puk2_retries = res.data.card_status.cards[i].applications[j].puk2_retries;
+                       has_pin2 = true;
+
+                       break; /* handle first application only for now */
+               }
+
+               blobmsg_add_string(&status, "pin1_status", get_pin_status(pin1_state));
+               blobmsg_add_u32(&status, "pin1_verify_tries", pin1_retries);
+               blobmsg_add_u32(&status, "pin1_unlock_tries", puk1_retries);
+               if (has_pin2) {
+                       blobmsg_add_string(&status, "pin2_status", get_pin_status(pin2_state));
+                       blobmsg_add_u32(&status, "pin2_verify_tries", pin2_retries);
+                       blobmsg_add_u32(&status, "pin2_unlock_tries", puk2_retries);
+               }
+
+               break; /* handle only first preset SIM card for now */
+       }
+
+       blobmsg_close_table(&status, card_table);
+}
+
+static enum qmi_cmd_result
+cmd_uim_get_sim_state_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
+{
+       qmi_set_uim_get_card_status_request(msg);
+       return QMI_CMD_REQUEST;
+}
index 86ebae4555b8d3ab08dad6a2efba7f48e68efcea..02a49b11161444872c3bd1e9f7fe5f72d58144ef 100644 (file)
 
 #define __uqmi_uim_commands                                                                                            \
        __uqmi_command(uim_verify_pin1, uim-verify-pin1, required, QMI_SERVICE_UIM), \
-       __uqmi_command(uim_verify_pin2, uim-verify-pin2, required, QMI_SERVICE_UIM) \
+       __uqmi_command(uim_verify_pin2, uim-verify-pin2, required, QMI_SERVICE_UIM), \
+       __uqmi_command(uim_get_sim_state, uim-get-sim-state, no, QMI_SERVICE_UIM) \
 
 
 #define uim_helptext \
                "  --uim-verify-pin1 <pin>:          Verify PIN1 (new devices)\n" \
                "  --uim-verify-pin2 <pin>:          Verify PIN2 (new devices)\n" \
+               "  --uim-get-sim-state:                  Get current SIM state\n" \