gsm: add GetOperatorID method

Returns the ID of the operator that issued the SIM card.

Cleanups and get_mnc_length_done() by me (dcbw).
This commit is contained in:
Torgny Johansson
2010-06-22 16:50:21 -07:00
committed by Dan Williams
parent e442b3b7f1
commit 8873c0a7dc
9 changed files with 309 additions and 88 deletions

View File

@@ -28,6 +28,20 @@
</arg>
</method>
<method name="GetOperatorId">
<tp:docstring>
Returns the ID of the network operator that issued the SIM card,
formatted as a 5 or 6-digit MCC/MNC code (ex "310410").
</tp:docstring>
<annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
<annotation name="org.freedesktop.DBus.GLib.CSymbol" value="impl_gsm_modem_get_operator_id"/>
<arg name="imsi" type="s" direction="out">
<tp:docstring>
The operator ID formatted as an MCC/MNC code.
</tp:docstring>
</arg>
</method>
<method name="SendPuk">
<tp:docstring>
Send the PUK and a new PIN to unlock the SIM card.

View File

@@ -20,7 +20,9 @@ libmodem_helpers_la_SOURCES = \
mm-modem-helpers.c \
mm-modem-helpers.h \
mm-charsets.c \
mm-charsets.h
mm-charsets.h \
mm-utils.c \
mm-utils.h
sbin_PROGRAMS = modem-manager

View File

@@ -20,6 +20,7 @@
#include <string.h>
#include "mm-charsets.h"
#include "mm-utils.h"
typedef struct {
const char *gsm_name;
@@ -150,60 +151,6 @@ mm_modem_charset_byte_array_append (GByteArray *array,
return TRUE;
}
/* From hostap, Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi> */
static int hex2num (char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
static int hex2byte (const char *hex)
{
int a, b;
a = hex2num(*hex++);
if (a < 0)
return -1;
b = hex2num(*hex++);
if (b < 0)
return -1;
return (a << 4) | b;
}
static char *
hexstr2bin (const char *hex, gsize *out_len)
{
size_t len = strlen (hex);
size_t i;
int a;
const char * ipos = hex;
char * buf = NULL;
char * opos;
/* Length must be a multiple of 2 */
g_return_val_if_fail ((len % 2) == 0, NULL);
opos = buf = g_malloc0 ((len / 2) + 1);
for (i = 0; i < len; i += 2) {
a = hex2byte (ipos);
if (a < 0) {
g_free (buf);
return NULL;
}
*opos++ = a;
ipos += 2;
}
*out_len = len / 2;
return buf;
}
/* End from hostap */
char *
mm_modem_charset_hex_to_utf8 (const char *src, MMModemCharset charset)
{
@@ -217,7 +164,7 @@ mm_modem_charset_hex_to_utf8 (const char *src, MMModemCharset charset)
iconv_from = charset_iconv_from (charset);
g_return_val_if_fail (iconv_from != NULL, FALSE);
unconverted = hexstr2bin (src, &unconverted_len);
unconverted = utils_hexstr2bin (src, &unconverted_len);
g_return_val_if_fail (unconverted != NULL, NULL);
if (charset == MM_MODEM_CHARSET_UTF8 || charset == MM_MODEM_CHARSET_IRA)

View File

@@ -12,7 +12,7 @@
*
* Copyright (C) 2008 - 2009 Novell, Inc.
* Copyright (C) 2009 - 2010 Red Hat, Inc.
* Copyright (C) 2009 Ericsson
* Copyright (C) 2009 - 2010 Ericsson
*/
#include <config.h>
@@ -20,6 +20,7 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "mm-generic-gsm.h"
#include "mm-modem-gsm-card.h"
#include "mm-modem-gsm-network.h"
@@ -33,6 +34,7 @@
#include "mm-modem-helpers.h"
#include "mm-options.h"
#include "mm-properties-changed-signal.h"
#include "mm-utils.h"
static void modem_init (MMModem *modem_class);
static void modem_gsm_card_init (MMModemGsmCard *gsm_card_class);
@@ -1175,6 +1177,110 @@ get_string_done (MMAtSerialPort *port,
mm_callback_info_schedule (info);
}
static void
get_mnc_length_done (MMAtSerialPort *port,
GString *response,
GError *error,
gpointer user_data)
{
MMCallbackInfo *info = (MMCallbackInfo *) user_data;
int sw1, sw2;
const char *imsi;
gboolean success = FALSE;
char hex[51];
char *bin;
if (error) {
info->error = g_error_copy (error);
goto done;
}
memset (hex, 0, sizeof (hex));
if (sscanf (response->str, "+CRSM:%d,%d,\"%50c\"", &sw1, &sw2, (char *) &hex) == 3)
success = TRUE;
else {
/* May not include quotes... */
if (sscanf (response->str, "+CRSM:%d,%d,%50c", &sw1, &sw2, (char *) &hex) == 3)
success = TRUE;
}
if (!success) {
info->error = g_error_new_literal (MM_MODEM_ERROR,
MM_MODEM_ERROR_GENERAL,
"Could not parse the CRSM response");
goto done;
}
if ((sw1 == 0x90 && sw2 == 0x00) || (sw1 == 0x91) || (sw1 == 0x92) || (sw1 == 0x9f)) {
gsize buflen = 0;
guint32 mnc_len;
/* Make sure the buffer is only hex characters */
while (buflen < sizeof (hex) && hex[buflen]) {
if (!isxdigit (hex[buflen])) {
hex[buflen] = 0x0;
break;
}
buflen++;
}
/* Convert hex string to binary */
bin = utils_hexstr2bin (hex, &buflen);
if (!bin || buflen < 4) {
info->error = g_error_new (MM_MODEM_ERROR,
MM_MODEM_ERROR_GENERAL,
"SIM returned malformed response '%s'",
hex);
goto done;
}
/* MNC length is byte 4 of this SIM file */
mnc_len = bin[3] & 0xFF;
if (mnc_len == 2 || mnc_len == 3) {
imsi = mm_callback_info_get_data (info, "imsi");
mm_callback_info_set_result (info, g_strndup (imsi, 3 + mnc_len), g_free);
} else {
info->error = g_error_new (MM_MODEM_ERROR,
MM_MODEM_ERROR_GENERAL,
"SIM returned invalid MNC length %d (should be either 2 or 3)",
mnc_len);
}
} else {
info->error = g_error_new (MM_MODEM_ERROR,
MM_MODEM_ERROR_GENERAL,
"SIM failed to handle CRSM request (sw1 %d sw2 %d)",
sw1, sw2);
}
done:
mm_callback_info_schedule (info);
}
static void
get_operator_id_imsi_done (MMModem *modem,
const char *result,
GError *error,
gpointer user_data)
{
MMGenericGsmPrivate *priv = MM_GENERIC_GSM_GET_PRIVATE (modem);
MMCallbackInfo *info = (MMCallbackInfo *) user_data;
if (error) {
info->error = g_error_copy (error);
mm_callback_info_schedule (info);
return;
}
mm_callback_info_set_data (info, "imsi", g_strdup (result), g_free);
/* READ BINARY of EFad (Administrative Data) ETSI 51.011 section 10.3.18 */
mm_at_serial_port_queue_command_cached (priv->primary,
"+CRSM=176,28589,0,0,4",
3,
get_mnc_length_done,
info);
}
static void
get_imei (MMModemGsmCard *modem,
MMModemStringFn callback,
@@ -1199,6 +1305,19 @@ get_imsi (MMModemGsmCard *modem,
mm_at_serial_port_queue_command_cached (priv->primary, "+CIMI", 3, get_string_done, info);
}
static void
get_operator_id (MMModemGsmCard *modem,
MMModemStringFn callback,
gpointer user_data)
{
MMCallbackInfo *info;
info = mm_callback_info_string_new (MM_MODEM (modem), callback, user_data);
mm_modem_gsm_card_get_imsi (MM_MODEM_GSM_CARD (modem),
get_operator_id_imsi_done,
info);
}
static void
get_card_info (MMModem *modem,
MMModemInfoFn callback,
@@ -3615,6 +3734,7 @@ modem_gsm_card_init (MMModemGsmCard *class)
{
class->get_imei = get_imei;
class->get_imsi = get_imsi;
class->get_operator_id = get_operator_id;
class->send_pin = send_pin;
class->send_puk = send_puk;
class->enable_pin = enable_pin;

View File

@@ -28,6 +28,9 @@ static void impl_gsm_modem_get_imei (MMModemGsmCard *modem,
static void impl_gsm_modem_get_imsi (MMModemGsmCard *modem,
DBusGMethodInvocation *context);
static void impl_gsm_modem_get_operator_id (MMModemGsmCard *modem,
DBusGMethodInvocation *context);
static void impl_gsm_modem_send_pin (MMModemGsmCard *modem,
const char *pin,
DBusGMethodInvocation *context);
@@ -158,6 +161,20 @@ void mm_modem_gsm_card_get_unlock_retries (MMModemGsmCard *self,
uint_call_not_supported (self, callback, user_data);
}
void
mm_modem_gsm_card_get_operator_id (MMModemGsmCard *self,
MMModemStringFn callback,
gpointer user_data)
{
g_return_if_fail (MM_IS_MODEM_GSM_CARD (self));
g_return_if_fail (callback != NULL);
if (MM_MODEM_GSM_CARD_GET_INTERFACE (self)->get_operator_id)
MM_MODEM_GSM_CARD_GET_INTERFACE (self)->get_operator_id (self, callback, user_data);
else
str_call_not_supported (self, callback, user_data);
}
void
mm_modem_gsm_card_send_puk (MMModemGsmCard *self,
const char *puk,
@@ -303,6 +320,43 @@ impl_gsm_modem_get_imsi (MMModemGsmCard *modem, DBusGMethodInvocation *context)
/*****************************************************************************/
static void
operator_id_auth_cb (MMAuthRequest *req,
GObject *owner,
DBusGMethodInvocation *context,
gpointer user_data)
{
MMModemGsmCard *self = MM_MODEM_GSM_CARD (owner);
GError *error = NULL;
/* Return any authorization error, otherwise get the operator id */
if (!mm_modem_auth_finish (MM_MODEM (self), req, &error)) {
dbus_g_method_return_error (context, error);
g_error_free (error);
} else
mm_modem_gsm_card_get_operator_id (self, str_call_done, context);
}
static void
impl_gsm_modem_get_operator_id (MMModemGsmCard *modem, DBusGMethodInvocation *context)
{
GError *error = NULL;
/* Make sure the caller is authorized to get the operator id */
if (!mm_modem_auth_request (MM_MODEM (modem),
MM_AUTHORIZATION_DEVICE_INFO,
context,
operator_id_auth_cb,
NULL,
NULL,
&error)) {
dbus_g_method_return_error (context, error);
g_error_free (error);
}
}
/*****************************************************************************/
typedef struct {
char *puk;
char *pin;

View File

@@ -53,6 +53,10 @@ struct _MMModemGsmCard {
MMModemUIntFn callback,
gpointer user_data);
void (*get_operator_id) (MMModemGsmCard *self,
MMModemStringFn callback,
gpointer user_data);
void (*send_puk) (MMModemGsmCard *self,
const char *puk,
const char *pin,
@@ -92,6 +96,10 @@ void mm_modem_gsm_card_get_unlock_retries (MMModemGsmCard *self,
MMModemUIntFn callback,
gpointer user_data);
void mm_modem_gsm_card_get_operator_id (MMModemGsmCard *self,
MMModemStringFn callback,
gpointer user_data);
void mm_modem_gsm_card_send_puk (MMModemGsmCard *self,
const char *puk,
const char *pin,

View File

@@ -31,6 +31,7 @@
#include "mm-serial-parsers.h"
#include "mm-errors.h"
#include "mm-marshal.h"
#include "mm-utils.h"
#include "libqcdm/src/commands.h"
#include "libqcdm/src/utils.h"
@@ -881,33 +882,6 @@ modem_destroyed (gpointer data, GObject *modem)
g_hash_table_remove_all (cached_caps);
}
/* From hostap, Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi> */
static int hex2num (char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
static int hex2byte (const char *hex)
{
int a, b;
a = hex2num(*hex++);
if (a < 0)
return -1;
b = hex2num(*hex++);
if (b < 0)
return -1;
return (a << 4) | b;
}
/* End from hostap */
gboolean
mm_plugin_base_get_device_ids (MMPluginBase *self,
const char *subsys,
@@ -940,8 +914,8 @@ mm_plugin_base_get_device_ids (MMPluginBase *self,
goto out;
if (vendor) {
*vendor = (guint16) (hex2byte (vid + 2) & 0xFF);
*vendor |= (guint16) ((hex2byte (vid) & 0xFF) << 8);
*vendor = (guint16) (utils_hex2byte (vid + 2) & 0xFF);
*vendor |= (guint16) ((utils_hex2byte (vid) & 0xFF) << 8);
}
pid = g_udev_device_get_property (device, "ID_MODEL_ID");
@@ -951,8 +925,8 @@ mm_plugin_base_get_device_ids (MMPluginBase *self,
}
if (product) {
*product = (guint16) (hex2byte (pid + 2) & 0xFF);
*product |= (guint16) ((hex2byte (pid) & 0xFF) << 8);
*product = (guint16) (utils_hex2byte (pid + 2) & 0xFF);
*product |= (guint16) ((utils_hex2byte (pid) & 0xFF) << 8);
}
success = TRUE;

78
src/mm-utils.c Normal file
View File

@@ -0,0 +1,78 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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 2 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:
*
* Copyright (C) 2010 Red Hat, Inc.
*/
#include <config.h>
#include <glib.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include "mm-utils.h"
/* From hostap, Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi> */
static int hex2num (char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
int utils_hex2byte (const char *hex)
{
int a, b;
a = hex2num(*hex++);
if (a < 0)
return -1;
b = hex2num(*hex++);
if (b < 0)
return -1;
return (a << 4) | b;
}
char *
utils_hexstr2bin (const char *hex, gsize *out_len)
{
size_t len = strlen (hex);
size_t i;
int a;
const char * ipos = hex;
char * buf = NULL;
char * opos;
/* Length must be a multiple of 2 */
g_return_val_if_fail ((len % 2) == 0, NULL);
opos = buf = g_malloc0 ((len / 2) + 1);
for (i = 0; i < len; i += 2) {
a = utils_hex2byte (ipos);
if (a < 0) {
g_free (buf);
return NULL;
}
*opos++ = a;
ipos += 2;
}
*out_len = len / 2;
return buf;
}
/* End from hostap */

24
src/mm-utils.h Normal file
View File

@@ -0,0 +1,24 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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 2 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:
*
* Copyright (C) 2010 Red Hat, Inc.
*/
#ifndef MM_UTILS_H
#define MM_UTILS_H
int utils_hex2byte (const char *hex);
char *utils_hexstr2bin (const char *hex, gsize *out_len);
#endif /* MM_UTILS_H */