From 78a27efb55e1c509604fe9d96f8c421d6b1bda36 Mon Sep 17 00:00:00 2001 From: Serhii Kostiuk Date: Fri, 6 Jan 2023 18:48:25 +0200 Subject: [GP-1842] Update the Cellular code to use SIM IMSI for Verizon detection The certifications lab uses special SIM cards with non-Verizon ICCID numbers and Verizon IMSI numbers (MCC/MNC prefixes or PLMN IDs). Update the code to detect Verizon properly for such "special" SIM cards. --- src/atcmd.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/atcmd.h | 2 ++ src/global.h | 7 ++++- 3 files changed, 101 insertions(+), 5 deletions(-) diff --git a/src/atcmd.c b/src/atcmd.c index 0c4860a..d17c03f 100644 --- a/src/atcmd.c +++ b/src/atcmd.c @@ -1300,7 +1300,7 @@ int atcmd_plus_gmi_read(int fd) return 0; } -/* ICCID needed for LNA3 to determine carrier */ +/* ICCID needed to detect Verizon on multi-carrier radios */ int atcmd_plus_iccid_read(int fd) { char buf[ATCMD_LINE_SIZE]; @@ -1342,7 +1342,7 @@ int atcmd_plus_iccid_read(int fd) log_debug("buf=%s for %d", buf,strlen(buf)); token = atcmd_value_tok(&save); if (!token) { - log_debug("atcmd_value_tok model"); + log_debug("atcmd_value_tok iccid"); return -1; } else { log_debug("token is %s",token); @@ -1371,6 +1371,65 @@ int atcmd_plus_iccid_read(int fd) return 0; } +/* IMSI needed to detect Verizon when the ICCID is unrecognized */ +int atcmd_plus_imsi_read(int fd) +{ + char buf[ATCMD_LINE_SIZE]; + char *save; + char *token; + int tmp; + char *tmp_buf; + char* command = "AT+CIMI"; + + atcmd_writeline(fd, command); + //Swallow extra "\r\n" + tmp = atcmd_readline(fd, buf, sizeof(buf)); + if (tmp <= 0) { + log_debug("expected \\r\\n but it was not received"); + return -1; + } + + //Read imsi string + tmp = atcmd_readline(fd, buf, sizeof(buf)); + if (tmp <= 0) { + log_debug("expected IMSI but it was not received"); + /* Currently only LNA7/LNA7D/L4G1 models will need the IMSI */ + return -1; + } + + save = buf; + log_debug("buf=%s for %d", buf,strlen(buf)); + token = atcmd_value_tok(&save); + if (!token) { + log_debug("atcmd_value_tok imsi"); + return -1; + } else { + log_debug("token is %s",token); + } + tmp_buf = strrchr(token,' '); + if (tmp_buf) { + token = ++tmp_buf; + log_debug("Found blank, incrementing tmp"); + } else { + tmp_buf = strrchr(token,'\t'); + log_debug("Found tab, incrementing tmp"); + if(tmp_buf) + token = ++tmp_buf; + } + log_debug("token[0]=%2.2x token[1]=%2.2x",token[0],token[1]); + strncpy(Global.core.imsi, token, IMSI_LEN); + + log_debug("imsi: %s", Global.core.imsi); + + tmp = atcmd_expect_line(fd, buf, sizeof(buf), "OK"); + if (tmp <= 0) { + log_debug("expected OK but it was not received"); + return -1; + } + + return 0; +} + int atcmd_init(int fd, int read_timeout) { int tmp; @@ -1437,6 +1496,12 @@ static int sms_atcmd_init(int fd) return -1; } + tmp = atcmd_plus_imsi_read(fd); + if (tmp < 0) { + log_error("failed to read SIM IMSI"); + return -1; + } + tmp = atcmd_plus_cmgf_write(fd, SMS_PDU_MODE); if (tmp < 0) { return -1; @@ -1566,11 +1631,35 @@ int is_quectel_dual_format(void) !strncmp(Global.core.model, "EG25", MODEL_LEN)) { log_debug("Found Quectel radio with dual SMS format support"); - /* Verizon Wireless SIM */ + + /* Verizon Wireless SIM ICCID */ if (strncmp(Global.core.iccid,"891480",6) == 0) { - log_debug("Found VZW SIM"); + log_debug("Found VZW SIM by ICCID"); return 1; } + + /* Extract PLMN ID - the first 5 or 6 digits of IMSI. + Assume 6 digits for Verizon. + NOTE: Other carriers may use shorter codes */ + char plmn_id_str[PLMN_ID_SIZE_VZW] = {0}; + strncpy(plmn_id_str, Global.core.imsi, PLMN_ID_LEN_VZW); + + /* Parse as integer for easier comparison */ + int plmn_id_num = atoi(plmn_id_str); + + /* Verizon Wireless SIM MCC/MNC */ + switch (plmn_id_num) { + case 310590: + case 310890: + case 311270: + case 311480: + case 312770: + log_debug("Found VZW SIM by IMSI"); + return 1; + default: + /* ignore, not Verizon */ + break; + }; } return 0; } diff --git a/src/atcmd.h b/src/atcmd.h index 1f15e62..bbab2d7 100644 --- a/src/atcmd.h +++ b/src/atcmd.h @@ -117,6 +117,8 @@ int atcmd_plus_cpbr_test(int fd, struct phonebook_store *store); int atcmd_plus_gmm_read(int fd); int atcmd_plus_gmi_read(int fd); +int atcmd_plus_iccid_read(int fd); +int atcmd_plus_imsi_read(int fd); int atcmd_init(int fd, int read_timeout); int sms_device_close(int fd); diff --git a/src/global.h b/src/global.h index a6073dd..4b83cb5 100644 --- a/src/global.h +++ b/src/global.h @@ -14,6 +14,10 @@ #define ICCID_LEN 23 #define ICCID_SIZE (ICCID_LEN + 1) +#define IMSI_LEN 15 +#define IMSI_SIZE (IMSI_LEN + 1) +#define PLMN_ID_LEN_VZW 6 +#define PLMN_ID_SIZE_VZW (PLMN_ID_LEN_VZW + 1) #define MODEL_LEN 1023 #define MODEL_SIZE (MODEL_LEN + 1) #define MANUFACTURER_LEN 1023 @@ -37,7 +41,8 @@ struct global_core { char *msg_store_new; char *pb_store; char model[MODEL_SIZE]; - char iccid[ICCID_SIZE]; /* Needed for LNA3/Verizon */ + char iccid[ICCID_SIZE]; /* Needed for Verizon 3GPP2 SMS format detection */ + char imsi[IMSI_SIZE]; /* Needed for Verizon 3GPP2 SMS format detection */ char manufacturer[MANUFACTURER_SIZE]; /* Needed to pick proper manufacturer-specific commands */ char *editor; -- cgit v1.2.3