libmm-common: added common utils from core

Moved the utils to play with binary to hex strings into libmm-common.
This commit is contained in:
Aleksander Morgado
2012-09-12 13:06:40 +02:00
parent fbe01c8d6a
commit a66f971a7c
24 changed files with 124 additions and 177 deletions

View File

@@ -10,6 +10,7 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details: * GNU General Public License for more details:
* *
* Copyright (C) 2010 - 2012 Red Hat, Inc.
* Copyright (C) 2011 - 2012 Google, Inc. * Copyright (C) 2011 - 2012 Google, Inc.
*/ */
@@ -805,3 +806,96 @@ mm_sms_delivery_state_get_string_extended (guint delivery_state)
* value */ * value */
return mm_sms_delivery_state_get_string ((MMSmsDeliveryState)delivery_state); return mm_sms_delivery_state_get_string ((MMSmsDeliveryState)delivery_state);
} }
/*****************************************************************************/
/* From hostap, Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi> */
static gint
hex2num (gchar 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;
}
gint
mm_utils_hex2byte (const gchar *hex)
{
gint a, b;
a = hex2num (*hex++);
if (a < 0)
return -1;
b = hex2num (*hex++);
if (b < 0)
return -1;
return (a << 4) | b;
}
gchar *
mm_utils_hexstr2bin (const gchar *hex, gsize *out_len)
{
const gchar *ipos = hex;
gchar *buf = NULL;
gsize i;
gint a;
gchar *opos;
gsize len;
len = strlen (hex);
/* 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 = mm_utils_hex2byte (ipos);
if (a < 0) {
g_free (buf);
return NULL;
}
*opos++ = a;
ipos += 2;
}
*out_len = len / 2;
return buf;
}
/* End from hostap */
gchar *
mm_utils_bin2hexstr (const guint8 *bin, gsize len)
{
GString *ret;
gsize i;
g_return_val_if_fail (bin != NULL, NULL);
ret = g_string_sized_new (len * 2 + 1);
for (i = 0; i < len; i++)
g_string_append_printf (ret, "%.2X", bin[i]);
return g_string_free (ret, FALSE);
}
gboolean
mm_utils_check_for_single_value (guint32 value)
{
gboolean found = FALSE;
guint32 i;
for (i = 1; i <= 32; i++) {
if (value & 0x1) {
if (found)
return FALSE; /* More than one bit set */
found = TRUE;
}
value >>= 1;
}
return TRUE;
}

View File

@@ -10,6 +10,7 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details: * GNU General Public License for more details:
* *
* Copyright (C) 2010 - 2012 Red Hat, Inc.
* Copyright (C) 2011 - 2012 Google, Inc. * Copyright (C) 2011 - 2012 Google, Inc.
* Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org> * Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org>
*/ */
@@ -89,4 +90,10 @@ gchar *mm_get_string_unquoted_from_match_info (GMatchInfo *match_info,
const gchar *mm_sms_delivery_state_get_string_extended (guint delivery_state); const gchar *mm_sms_delivery_state_get_string_extended (guint delivery_state);
gint mm_utils_hex2byte (const gchar *hex);
gchar *mm_utils_hexstr2bin (const gchar *hex, gsize *out_len);
gchar *mm_utils_bin2hexstr (const guint8 *bin, gsize len);
gboolean mm_utils_check_for_single_value (guint32 value);
#endif /* MM_COMMON_HELPERS_H */ #endif /* MM_COMMON_HELPERS_H */

View File

@@ -26,7 +26,6 @@
#include "ModemManager.h" #include "ModemManager.h"
#include "mm-log.h" #include "mm-log.h"
#include "mm-errors-types.h" #include "mm-errors-types.h"
#include "mm-utils.h"
#include "mm-common-helpers.h" #include "mm-common-helpers.h"
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"
#include "mm-base-modem-at.h" #include "mm-base-modem-at.h"
@@ -1096,7 +1095,7 @@ encode (MMIfaceModem3gppUssd *self,
} }
packed = gsm_pack (gsm, len, 0, &packed_len); packed = gsm_pack (gsm, len, 0, &packed_len);
hex = utils_bin2hexstr (packed, packed_len); hex = mm_utils_bin2hexstr (packed, packed_len);
g_free (packed); g_free (packed);
g_free (gsm); g_free (gsm);
@@ -1113,7 +1112,7 @@ decode (MMIfaceModem3gppUssd *self,
gsize bin_len; gsize bin_len;
guint32 unpacked_len; guint32 unpacked_len;
bin = utils_hexstr2bin (reply, &bin_len); bin = mm_utils_hexstr2bin (reply, &bin_len);
unpacked = gsm_unpack ((guint8*) bin, (bin_len * 8) / 7, 0, &unpacked_len); unpacked = gsm_unpack ((guint8*) bin, (bin_len * 8) / 7, 0, &unpacked_len);
/* if the last character in a 7-byte block is padding, then drop it */ /* if the last character in a 7-byte block is padding, then drop it */
if ((bin_len % 7 == 0) && (unpacked[unpacked_len - 1] == 0x0d)) if ((bin_len % 7 == 0) && (unpacked[unpacked_len - 1] == 0x0d))

View File

@@ -31,7 +31,6 @@
#include "mm-log.h" #include "mm-log.h"
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"
#include "mm-error-helpers.h" #include "mm-error-helpers.h"
#include "mm-utils.h"
G_DEFINE_TYPE (MMBroadbandBearerIcera, mm_broadband_bearer_icera, MM_TYPE_BROADBAND_BEARER); G_DEFINE_TYPE (MMBroadbandBearerIcera, mm_broadband_bearer_icera, MM_TYPE_BROADBAND_BEARER);

View File

@@ -25,7 +25,6 @@
#include "mm-bearer-iridium.h" #include "mm-bearer-iridium.h"
#include "mm-base-modem-at.h" #include "mm-base-modem-at.h"
#include "mm-utils.h"
#include "mm-log.h" #include "mm-log.h"
/* Allow up to 200s to get a proper IP connection */ /* Allow up to 200s to get a proper IP connection */

View File

@@ -38,7 +38,6 @@
#include "mm-broadband-bearer-mbm.h" #include "mm-broadband-bearer-mbm.h"
#include "mm-log.h" #include "mm-log.h"
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"
#include "mm-utils.h"
G_DEFINE_TYPE (MMBroadbandBearerMbm, mm_broadband_bearer_mbm, MM_TYPE_BROADBAND_BEARER); G_DEFINE_TYPE (MMBroadbandBearerMbm, mm_broadband_bearer_mbm, MM_TYPE_BROADBAND_BEARER);

View File

@@ -30,7 +30,6 @@
#include "mm-broadband-bearer-novatel-lte.h" #include "mm-broadband-bearer-novatel-lte.h"
#include "mm-log.h" #include "mm-log.h"
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"
#include "mm-utils.h"
#define CONNECTION_CHECK_TIMEOUT_SEC 5 #define CONNECTION_CHECK_TIMEOUT_SEC 5

View File

@@ -31,7 +31,6 @@
#include "mm-broadband-bearer-hso.h" #include "mm-broadband-bearer-hso.h"
#include "mm-log.h" #include "mm-log.h"
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"
#include "mm-utils.h"
G_DEFINE_TYPE (MMBroadbandBearerHso, mm_broadband_bearer_hso, MM_TYPE_BROADBAND_BEARER); G_DEFINE_TYPE (MMBroadbandBearerHso, mm_broadband_bearer_hso, MM_TYPE_BROADBAND_BEARER);

View File

@@ -30,7 +30,6 @@
#include "mm-broadband-bearer-sierra.h" #include "mm-broadband-bearer-sierra.h"
#include "mm-log.h" #include "mm-log.h"
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"
#include "mm-utils.h"
G_DEFINE_TYPE (MMBroadbandBearerSierra, mm_broadband_bearer_sierra, MM_TYPE_BROADBAND_BEARER); G_DEFINE_TYPE (MMBroadbandBearerSierra, mm_broadband_bearer_sierra, MM_TYPE_BROADBAND_BEARER);

View File

@@ -27,8 +27,6 @@ libmodem_helpers_la_SOURCES = \
mm-modem-helpers.h \ mm-modem-helpers.h \
mm-charsets.c \ mm-charsets.c \
mm-charsets.h \ mm-charsets.h \
mm-utils.c \
mm-utils.h \
mm-sms-part.h \ mm-sms-part.h \
mm-sms-part.c mm-sms-part.c

View File

@@ -26,7 +26,6 @@
#include <libmm-common.h> #include <libmm-common.h>
#include "mm-bearer-list.h" #include "mm-bearer-list.h"
#include "mm-utils.h"
#include "mm-log.h" #include "mm-log.h"
G_DEFINE_TYPE (MMBearerList, mm_bearer_list, G_TYPE_OBJECT); G_DEFINE_TYPE (MMBearerList, mm_bearer_list, G_TYPE_OBJECT);

View File

@@ -25,7 +25,6 @@
#include <libqmi-glib.h> #include <libqmi-glib.h>
#include "mm-bearer-qmi.h" #include "mm-bearer-qmi.h"
#include "mm-utils.h"
#include "mm-serial-enums-types.h" #include "mm-serial-enums-types.h"
#include "mm-log.h" #include "mm-log.h"

View File

@@ -32,7 +32,6 @@
#include "mm-bearer.h" #include "mm-bearer.h"
#include "mm-base-modem-at.h" #include "mm-base-modem-at.h"
#include "mm-base-modem.h" #include "mm-base-modem.h"
#include "mm-utils.h"
#include "mm-log.h" #include "mm-log.h"
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"

View File

@@ -30,7 +30,6 @@
#include "mm-iface-modem-3gpp.h" #include "mm-iface-modem-3gpp.h"
#include "mm-iface-modem-cdma.h" #include "mm-iface-modem-cdma.h"
#include "mm-base-modem-at.h" #include "mm-base-modem-at.h"
#include "mm-utils.h"
#include "mm-log.h" #include "mm-log.h"
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"
#include "mm-serial-enums-types.h" #include "mm-serial-enums-types.h"

View File

@@ -40,7 +40,6 @@
#include "mm-sms-list.h" #include "mm-sms-list.h"
#include "mm-sim.h" #include "mm-sim.h"
#include "mm-log.h" #include "mm-log.h"
#include "mm-utils.h"
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"
#include "mm-error-helpers.h" #include "mm-error-helpers.h"
#include "mm-qcdm-serial-port.h" #include "mm-qcdm-serial-port.h"
@@ -3581,7 +3580,7 @@ modem_3gpp_ussd_encode (MMIfaceModem3gppUssd *self,
broadband->priv->modem_current_charset)) { broadband->priv->modem_current_charset)) {
*scheme = MM_MODEM_GSM_USSD_SCHEME_7BIT; *scheme = MM_MODEM_GSM_USSD_SCHEME_7BIT;
/* convert to hex representation */ /* convert to hex representation */
hex = utils_bin2hexstr (ussd_command->data, ussd_command->len); hex = mm_utils_bin2hexstr (ussd_command->data, ussd_command->len);
} }
g_byte_array_free (ussd_command, TRUE); g_byte_array_free (ussd_command, TRUE);

View File

@@ -20,8 +20,9 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <libmm-common.h>
#include "mm-charsets.h" #include "mm-charsets.h"
#include "mm-utils.h"
typedef struct { typedef struct {
const char *gsm_name; const char *gsm_name;
@@ -160,7 +161,7 @@ mm_modem_charset_hex_to_utf8 (const char *src, MMModemCharset charset)
iconv_from = charset_iconv_from (charset); iconv_from = charset_iconv_from (charset);
g_return_val_if_fail (iconv_from != NULL, FALSE); g_return_val_if_fail (iconv_from != NULL, FALSE);
unconverted = utils_hexstr2bin (src, &unconverted_len); unconverted = mm_utils_hexstr2bin (src, &unconverted_len);
if (!unconverted) if (!unconverted)
return NULL; return NULL;
@@ -208,7 +209,7 @@ mm_modem_charset_utf8_to_hex (const char *src, MMModemCharset charset)
} }
/* Get hex representation of the string */ /* Get hex representation of the string */
hex = utils_bin2hexstr ((guint8 *)converted, converted_len); hex = mm_utils_bin2hexstr ((guint8 *)converted, converted_len);
g_free (converted); g_free (converted);
return hex; return hex;
} }
@@ -888,7 +889,7 @@ mm_utf8_take_and_convert_to_charset (gchar *str,
} }
/* Get hex representation of the string */ /* Get hex representation of the string */
hex = utils_bin2hexstr ((guint8 *)encoded, encoded_len); hex = mm_utils_bin2hexstr ((guint8 *)encoded, encoded_len);
g_free (encoded); g_free (encoded);
encoded = hex; encoded = hex;
g_free (str); g_free (str);

View File

@@ -20,11 +20,10 @@
#include <string.h> #include <string.h>
#include <ModemManager.h> #include <ModemManager.h>
#include <mm-errors-types.h> #include <libmm-common.h>
#include "mm-device.h" #include "mm-device.h"
#include "mm-plugin.h" #include "mm-plugin.h"
#include "mm-utils.h"
#include "mm-log.h" #include "mm-log.h"
G_DEFINE_TYPE (MMDevice, mm_device, G_TYPE_OBJECT); G_DEFINE_TYPE (MMDevice, mm_device, G_TYPE_OBJECT);
@@ -166,8 +165,8 @@ get_device_ids (GUdevDevice *device,
goto out; goto out;
if (vendor) { if (vendor) {
*vendor = (guint16) (utils_hex2byte (vid + 2) & 0xFF); *vendor = (guint16) (mm_utils_hex2byte (vid + 2) & 0xFF);
*vendor |= (guint16) ((utils_hex2byte (vid) & 0xFF) << 8); *vendor |= (guint16) ((mm_utils_hex2byte (vid) & 0xFF) << 8);
} }
if (!pid) if (!pid)
@@ -185,8 +184,8 @@ get_device_ids (GUdevDevice *device,
} }
if (product) { if (product) {
*product = (guint16) (utils_hex2byte (pid + 2) & 0xFF); *product = (guint16) (mm_utils_hex2byte (pid + 2) & 0xFF);
*product |= (guint16) ((utils_hex2byte (pid) & 0xFF) << 8); *product |= (guint16) ((mm_utils_hex2byte (pid) & 0xFF) << 8);
} }
success = TRUE; success = TRUE;

View File

@@ -29,7 +29,6 @@
#include "mm-sim.h" #include "mm-sim.h"
#include "mm-base-modem-at.h" #include "mm-base-modem-at.h"
#include "mm-base-modem.h" #include "mm-base-modem.h"
#include "mm-utils.h"
#include "mm-log.h" #include "mm-log.h"
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"
#include "mm-marshal.h" #include "mm-marshal.h"
@@ -1178,7 +1177,7 @@ parse_mnc_length (const gchar *response,
} }
/* Convert hex string to binary */ /* Convert hex string to binary */
bin = utils_hexstr2bin (hex, &buflen); bin = mm_utils_hexstr2bin (hex, &buflen);
if (!bin || buflen < 4) { if (!bin || buflen < 4) {
g_set_error (error, g_set_error (error,
MM_CORE_ERROR, MM_CORE_ERROR,
@@ -1315,7 +1314,7 @@ parse_spn (const gchar *response,
} }
/* Convert hex string to binary */ /* Convert hex string to binary */
bin = utils_hexstr2bin (hex, &buflen); bin = mm_utils_hexstr2bin (hex, &buflen);
if (!bin) { if (!bin) {
g_set_error (error, g_set_error (error,
MM_CORE_ERROR, MM_CORE_ERROR,

View File

@@ -27,7 +27,6 @@
#include "mm-marshal.h" #include "mm-marshal.h"
#include "mm-sms-list.h" #include "mm-sms-list.h"
#include "mm-sms.h" #include "mm-sms.h"
#include "mm-utils.h"
#include "mm-log.h" #include "mm-log.h"
G_DEFINE_TYPE (MMSmsList, mm_sms_list, G_TYPE_OBJECT); G_DEFINE_TYPE (MMSmsList, mm_sms_list, G_TYPE_OBJECT);

View File

@@ -25,7 +25,6 @@
#include "mm-sms-part.h" #include "mm-sms-part.h"
#include "mm-charsets.h" #include "mm-charsets.h"
#include "mm-utils.h"
#include "mm-log.h" #include "mm-log.h"
#define PDU_SIZE 200 #define PDU_SIZE 200
@@ -476,7 +475,7 @@ mm_sms_part_new_from_pdu (guint index,
MMSmsPart *part; MMSmsPart *part;
/* Convert PDU from hex to binary */ /* Convert PDU from hex to binary */
pdu = (guint8 *) utils_hexstr2bin (hexpdu, &pdu_len); pdu = (guint8 *) mm_utils_hexstr2bin (hexpdu, &pdu_len);
if (!pdu) { if (!pdu) {
g_set_error_literal (error, g_set_error_literal (error,
MM_CORE_ERROR, MM_CORE_ERROR,

View File

@@ -31,7 +31,6 @@
#include "mm-sms.h" #include "mm-sms.h"
#include "mm-base-modem-at.h" #include "mm-base-modem-at.h"
#include "mm-base-modem.h" #include "mm-base-modem.h"
#include "mm-utils.h"
#include "mm-log.h" #include "mm-log.h"
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"
@@ -475,7 +474,7 @@ sms_get_store_or_send_command (MMSmsPart *part,
return FALSE; return FALSE;
/* Convert PDU to hex */ /* Convert PDU to hex */
hex = utils_bin2hexstr (pdu, pdulen); hex = mm_utils_bin2hexstr (pdu, pdulen);
g_free (pdu); g_free (pdu);
if (!hex) { if (!hex) {

View File

@@ -1,109 +0,0 @@
/* -*- 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 */
char *
utils_bin2hexstr (const guint8 *bin, gsize len)
{
GString *ret;
gsize i;
g_return_val_if_fail (bin != NULL, NULL);
ret = g_string_sized_new (len * 2 + 1);
for (i = 0; i < len; i++)
g_string_append_printf (ret, "%.2X", bin[i]);
return g_string_free (ret, FALSE);
}
gboolean
utils_check_for_single_value (guint32 value)
{
gboolean found = FALSE;
guint32 i;
for (i = 1; i <= 32; i++) {
if (value & 0x1) {
if (found)
return FALSE; /* More than one bit set */
found = TRUE;
}
value >>= 1;
}
return TRUE;
}

View File

@@ -1,28 +0,0 @@
/* -*- 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);
char *utils_bin2hexstr (const guint8 *bin, gsize len);
gboolean utils_check_for_single_value (guint32 value);
#endif /* MM_UTILS_H */

View File

@@ -20,8 +20,9 @@
#include <stdio.h> #include <stdio.h>
#include <locale.h> #include <locale.h>
#include <libmm-common.h>
#include "mm-sms-part.h" #include "mm-sms-part.h"
#include "mm-utils.h"
#include "mm-log.h" #include "mm-log.h"
/* If defined will print debugging traces */ /* If defined will print debugging traces */
@@ -96,7 +97,7 @@ common_test_part_from_pdu (const guint8 *pdu,
{ {
gchar *hexpdu; gchar *hexpdu;
hexpdu = utils_bin2hexstr (pdu, pdu_size); hexpdu = mm_utils_bin2hexstr (pdu, pdu_size);
common_test_part_from_hexpdu (hexpdu, common_test_part_from_hexpdu (hexpdu,
expected_smsc, expected_smsc,
expected_number, expected_number,
@@ -353,7 +354,7 @@ test_pdu_insufficient_data (void)
0x97, 0xd9, 0xec, 0x37 0x97, 0xd9, 0xec, 0x37
}; };
hexpdu = utils_bin2hexstr (pdu, sizeof (pdu)); hexpdu = mm_utils_bin2hexstr (pdu, sizeof (pdu));
part = mm_sms_part_new_from_pdu (0, hexpdu, &error); part = mm_sms_part_new_from_pdu (0, hexpdu, &error);
g_assert (part == NULL); g_assert (part == NULL);
/* We don't care for the specific error type */ /* We don't care for the specific error type */