Skip to content

Commit ac751b7

Browse files
rerickson1cfriedt
authored andcommitted
drivers: modem: hl7800: Query IMSI
Add API to query SIM card IMSI. Signed-off-by: Ryan Erickson <[email protected]>
1 parent d83aaef commit ac751b7

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

drivers/modem/hl7800.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ struct hl7800_iface_ctx {
505505
enum mdm_hl7800_radio_mode mdm_rat;
506506
char mdm_active_bands_string[MDM_HL7800_LTE_BAND_STR_SIZE];
507507
char mdm_bands_string[MDM_HL7800_LTE_BAND_STR_SIZE];
508+
char mdm_imsi[MDM_HL7800_IMSI_MAX_STR_SIZE];
508509
uint16_t mdm_bands_top;
509510
uint32_t mdm_bands_middle;
510511
uint32_t mdm_bands_bottom;
@@ -1517,6 +1518,42 @@ static bool on_cmd_atcmdinfo_iccid(struct net_buf **buf, uint16_t len)
15171518
return true;
15181519
}
15191520

1521+
static bool on_cmd_atcmdinfo_imsi(struct net_buf **buf, uint16_t len)
1522+
{
1523+
struct net_buf *frag = NULL;
1524+
size_t out_len;
1525+
1526+
/* The handler for the IMSI is based on the command.
1527+
* waiting for: <IMSI>\r\n
1528+
*/
1529+
wait_for_modem_data_and_newline(buf, net_buf_frags_len(*buf),
1530+
MDM_HL7800_IMSI_MIN_STR_SIZE);
1531+
1532+
frag = NULL;
1533+
len = net_buf_findcrlf(*buf, &frag);
1534+
if (!frag) {
1535+
LOG_ERR("Unable to find IMSI end");
1536+
goto done;
1537+
}
1538+
if (len > MDM_HL7800_IMSI_MAX_STRLEN) {
1539+
LOG_WRN("IMSI too long (len:%d)", len);
1540+
len = MDM_HL7800_IMSI_MAX_STRLEN;
1541+
}
1542+
1543+
out_len = net_buf_linearize(ictx.mdm_imsi, MDM_HL7800_IMSI_MAX_STR_SIZE,
1544+
*buf, 0, len);
1545+
ictx.mdm_imsi[out_len] = 0;
1546+
1547+
if (strstr(ictx.mdm_imsi, "ERROR") != NULL) {
1548+
LOG_ERR("Unable to read IMSI");
1549+
memset(ictx.mdm_imsi, 0, sizeof(ictx.mdm_imsi));
1550+
}
1551+
1552+
LOG_INF("IMSI: %s", log_strdup(ictx.mdm_imsi));
1553+
done:
1554+
return true;
1555+
}
1556+
15201557
static void dns_work_cb(struct k_work *work)
15211558
{
15221559
#if defined(CONFIG_DNS_RESOLVER) && !defined(CONFIG_DNS_SERVER_IP_ADDRESSES)
@@ -1556,6 +1593,11 @@ char *mdm_hl7800_get_fw_version(void)
15561593
return ictx.mdm_revision;
15571594
}
15581595

1596+
char *mdm_hl7800_get_imsi(void)
1597+
{
1598+
return ictx.mdm_imsi;
1599+
}
1600+
15591601
/* Handler: +CGCONTRDP: <cid>,<bearer_id>,<apn>,<local_addr and subnet_mask>,
15601602
* <gw_addr>,<DNS_prim_addr>,<DNS_sec_addr>
15611603
*/
@@ -3382,6 +3424,7 @@ static void hl7800_rx(void)
33823424
CMD_HANDLER("+CGDCONT: 1", atcmdinfo_pdp_context),
33833425
CMD_HANDLER("AT+CEREG?", network_report_query),
33843426
CMD_HANDLER("+KCARRIERCFG: ", operator_index_query),
3427+
CMD_HANDLER("AT+CIMI", atcmdinfo_imsi),
33853428
#ifdef CONFIG_NEWLIB_LIBC
33863429
CMD_HANDLER("+CCLK: ", rtc_query),
33873430
#endif
@@ -4079,6 +4122,10 @@ static int modem_reset_and_configure(void)
40794122
/* query SIM ICCID */
40804123
SEND_AT_CMD_IGNORE_ERROR("AT+CCID?");
40814124

4125+
/* query SIM IMSI */
4126+
(void)send_at_cmd(NULL, "AT+CIMI", MDM_CMD_SEND_TIMEOUT,
4127+
MDM_DEFAULT_AT_CMD_RETRIES, true);
4128+
40824129
/* An empty string is used here so that it doesn't conflict
40834130
* with the APN used in the +CGDCONT command.
40844131
*/

include/drivers/modem/hl7800.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ struct mdm_hl7800_apn {
6060
#define MDM_HL7800_OPERATOR_INDEX_SIZE 3
6161
#define MDM_HL7800_OPERATOR_INDEX_STRLEN (MDM_HL7800_OPERATOR_INDEX_SIZE - 1)
6262

63+
#define MDM_HL7800_IMSI_MIN_STR_SIZE 15
64+
#define MDM_HL7800_IMSI_MAX_STR_SIZE 16
65+
#define MDM_HL7800_IMSI_MAX_STRLEN (MDM_HL7800_IMSI_MAX_STR_SIZE - 1)
66+
6367
enum mdm_hl7800_radio_mode { MDM_RAT_CAT_M1 = 0, MDM_RAT_CAT_NB1 };
6468

6569
enum mdm_hl7800_event {
@@ -208,6 +212,12 @@ char *mdm_hl7800_get_imei(void);
208212
*/
209213
char *mdm_hl7800_get_fw_version(void);
210214

215+
/**
216+
* @brief Get the IMSI
217+
*
218+
*/
219+
char *mdm_hl7800_get_imsi(void);
220+
211221
/**
212222
* @brief Update the Access Point Name in the modem.
213223
*

0 commit comments

Comments
 (0)