diff options
author | Aleksander Morgado <aleksander@lanedo.com> | 2012-09-12 13:06:40 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@lanedo.com> | 2012-09-14 07:05:26 +0200 |
commit | a66f971a7cdea4bf1d9f6390e12f950f41ee5595 (patch) | |
tree | 91134bf4d00ffd509a8b7112effe2525342cfb88 /src | |
parent | fbe01c8d6a22cb95ff74ddefc907e663cdeee710 (diff) |
libmm-common: added common utils from core
Moved the utils to play with binary to hex strings into libmm-common.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/mm-bearer-list.c | 1 | ||||
-rw-r--r-- | src/mm-bearer-qmi.c | 1 | ||||
-rw-r--r-- | src/mm-bearer.c | 1 | ||||
-rw-r--r-- | src/mm-broadband-bearer.c | 1 | ||||
-rw-r--r-- | src/mm-broadband-modem.c | 3 | ||||
-rw-r--r-- | src/mm-charsets.c | 13 | ||||
-rw-r--r-- | src/mm-device.c | 11 | ||||
-rw-r--r-- | src/mm-sim.c | 5 | ||||
-rw-r--r-- | src/mm-sms-list.c | 1 | ||||
-rw-r--r-- | src/mm-sms-part.c | 3 | ||||
-rw-r--r-- | src/mm-sms.c | 3 | ||||
-rw-r--r-- | src/mm-utils.c | 109 | ||||
-rw-r--r-- | src/mm-utils.h | 28 | ||||
-rw-r--r-- | src/tests/test-sms-part.c | 7 |
15 files changed, 21 insertions, 168 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index c27b8534..768af7dd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,8 +27,6 @@ libmodem_helpers_la_SOURCES = \ mm-modem-helpers.h \ mm-charsets.c \ mm-charsets.h \ - mm-utils.c \ - mm-utils.h \ mm-sms-part.h \ mm-sms-part.c diff --git a/src/mm-bearer-list.c b/src/mm-bearer-list.c index 3c672f89..d5cb1e9a 100644 --- a/src/mm-bearer-list.c +++ b/src/mm-bearer-list.c @@ -26,7 +26,6 @@ #include <libmm-common.h> #include "mm-bearer-list.h" -#include "mm-utils.h" #include "mm-log.h" G_DEFINE_TYPE (MMBearerList, mm_bearer_list, G_TYPE_OBJECT); diff --git a/src/mm-bearer-qmi.c b/src/mm-bearer-qmi.c index b4d7d0c5..3d91ad74 100644 --- a/src/mm-bearer-qmi.c +++ b/src/mm-bearer-qmi.c @@ -25,7 +25,6 @@ #include <libqmi-glib.h> #include "mm-bearer-qmi.h" -#include "mm-utils.h" #include "mm-serial-enums-types.h" #include "mm-log.h" diff --git a/src/mm-bearer.c b/src/mm-bearer.c index 94be3588..9720fc87 100644 --- a/src/mm-bearer.c +++ b/src/mm-bearer.c @@ -32,7 +32,6 @@ #include "mm-bearer.h" #include "mm-base-modem-at.h" #include "mm-base-modem.h" -#include "mm-utils.h" #include "mm-log.h" #include "mm-modem-helpers.h" diff --git a/src/mm-broadband-bearer.c b/src/mm-broadband-bearer.c index d884cd0e..fa3ba664 100644 --- a/src/mm-broadband-bearer.c +++ b/src/mm-broadband-bearer.c @@ -30,7 +30,6 @@ #include "mm-iface-modem-3gpp.h" #include "mm-iface-modem-cdma.h" #include "mm-base-modem-at.h" -#include "mm-utils.h" #include "mm-log.h" #include "mm-modem-helpers.h" #include "mm-serial-enums-types.h" diff --git a/src/mm-broadband-modem.c b/src/mm-broadband-modem.c index 8e594d5c..77650490 100644 --- a/src/mm-broadband-modem.c +++ b/src/mm-broadband-modem.c @@ -40,7 +40,6 @@ #include "mm-sms-list.h" #include "mm-sim.h" #include "mm-log.h" -#include "mm-utils.h" #include "mm-modem-helpers.h" #include "mm-error-helpers.h" #include "mm-qcdm-serial-port.h" @@ -3581,7 +3580,7 @@ modem_3gpp_ussd_encode (MMIfaceModem3gppUssd *self, broadband->priv->modem_current_charset)) { *scheme = MM_MODEM_GSM_USSD_SCHEME_7BIT; /* 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); diff --git a/src/mm-charsets.c b/src/mm-charsets.c index 633acc19..b6937cbd 100644 --- a/src/mm-charsets.c +++ b/src/mm-charsets.c @@ -20,8 +20,9 @@ #include <string.h> #include <ctype.h> +#include <libmm-common.h> + #include "mm-charsets.h" -#include "mm-utils.h" typedef struct { 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); g_return_val_if_fail (iconv_from != NULL, FALSE); - unconverted = utils_hexstr2bin (src, &unconverted_len); + unconverted = mm_utils_hexstr2bin (src, &unconverted_len); if (!unconverted) return NULL; @@ -208,7 +209,7 @@ mm_modem_charset_utf8_to_hex (const char *src, MMModemCharset charset) } /* Get hex representation of the string */ - hex = utils_bin2hexstr ((guint8 *)converted, converted_len); + hex = mm_utils_bin2hexstr ((guint8 *)converted, converted_len); g_free (converted); return hex; } @@ -607,7 +608,7 @@ mm_charset_get_encoded_len (const char *utf8, for (e = &subset_table[0]; e->cs != charset && e->cs != MM_MODEM_CHARSET_UNKNOWN; e++); - g_return_val_if_fail (e->cs != MM_MODEM_CHARSET_UNKNOWN, 0); + g_return_val_if_fail (e->cs != MM_MODEM_CHARSET_UNKNOWN, 0); while (*p) { gunichar c; @@ -622,7 +623,7 @@ mm_charset_get_encoded_len (const char *utf8, end = p; while (*end++); } - + if (!e->func (c, p, (end - p), &clen)) unsupported++; len += clen; @@ -888,7 +889,7 @@ mm_utf8_take_and_convert_to_charset (gchar *str, } /* Get hex representation of the string */ - hex = utils_bin2hexstr ((guint8 *)encoded, encoded_len); + hex = mm_utils_bin2hexstr ((guint8 *)encoded, encoded_len); g_free (encoded); encoded = hex; g_free (str); diff --git a/src/mm-device.c b/src/mm-device.c index 06755722..83b23867 100644 --- a/src/mm-device.c +++ b/src/mm-device.c @@ -20,11 +20,10 @@ #include <string.h> #include <ModemManager.h> -#include <mm-errors-types.h> +#include <libmm-common.h> #include "mm-device.h" #include "mm-plugin.h" -#include "mm-utils.h" #include "mm-log.h" G_DEFINE_TYPE (MMDevice, mm_device, G_TYPE_OBJECT); @@ -166,8 +165,8 @@ get_device_ids (GUdevDevice *device, goto out; if (vendor) { - *vendor = (guint16) (utils_hex2byte (vid + 2) & 0xFF); - *vendor |= (guint16) ((utils_hex2byte (vid) & 0xFF) << 8); + *vendor = (guint16) (mm_utils_hex2byte (vid + 2) & 0xFF); + *vendor |= (guint16) ((mm_utils_hex2byte (vid) & 0xFF) << 8); } if (!pid) @@ -185,8 +184,8 @@ get_device_ids (GUdevDevice *device, } if (product) { - *product = (guint16) (utils_hex2byte (pid + 2) & 0xFF); - *product |= (guint16) ((utils_hex2byte (pid) & 0xFF) << 8); + *product = (guint16) (mm_utils_hex2byte (pid + 2) & 0xFF); + *product |= (guint16) ((mm_utils_hex2byte (pid) & 0xFF) << 8); } success = TRUE; diff --git a/src/mm-sim.c b/src/mm-sim.c index 7cdb356b..6503d342 100644 --- a/src/mm-sim.c +++ b/src/mm-sim.c @@ -29,7 +29,6 @@ #include "mm-sim.h" #include "mm-base-modem-at.h" #include "mm-base-modem.h" -#include "mm-utils.h" #include "mm-log.h" #include "mm-modem-helpers.h" #include "mm-marshal.h" @@ -1178,7 +1177,7 @@ parse_mnc_length (const gchar *response, } /* Convert hex string to binary */ - bin = utils_hexstr2bin (hex, &buflen); + bin = mm_utils_hexstr2bin (hex, &buflen); if (!bin || buflen < 4) { g_set_error (error, MM_CORE_ERROR, @@ -1315,7 +1314,7 @@ parse_spn (const gchar *response, } /* Convert hex string to binary */ - bin = utils_hexstr2bin (hex, &buflen); + bin = mm_utils_hexstr2bin (hex, &buflen); if (!bin) { g_set_error (error, MM_CORE_ERROR, diff --git a/src/mm-sms-list.c b/src/mm-sms-list.c index 9afe96f6..393101ba 100644 --- a/src/mm-sms-list.c +++ b/src/mm-sms-list.c @@ -27,7 +27,6 @@ #include "mm-marshal.h" #include "mm-sms-list.h" #include "mm-sms.h" -#include "mm-utils.h" #include "mm-log.h" G_DEFINE_TYPE (MMSmsList, mm_sms_list, G_TYPE_OBJECT); diff --git a/src/mm-sms-part.c b/src/mm-sms-part.c index cb1ee5fa..4d4f3a38 100644 --- a/src/mm-sms-part.c +++ b/src/mm-sms-part.c @@ -25,7 +25,6 @@ #include "mm-sms-part.h" #include "mm-charsets.h" -#include "mm-utils.h" #include "mm-log.h" #define PDU_SIZE 200 @@ -476,7 +475,7 @@ mm_sms_part_new_from_pdu (guint index, MMSmsPart *part; /* Convert PDU from hex to binary */ - pdu = (guint8 *) utils_hexstr2bin (hexpdu, &pdu_len); + pdu = (guint8 *) mm_utils_hexstr2bin (hexpdu, &pdu_len); if (!pdu) { g_set_error_literal (error, MM_CORE_ERROR, diff --git a/src/mm-sms.c b/src/mm-sms.c index 27d8ca26..90bfc68b 100644 --- a/src/mm-sms.c +++ b/src/mm-sms.c @@ -31,7 +31,6 @@ #include "mm-sms.h" #include "mm-base-modem-at.h" #include "mm-base-modem.h" -#include "mm-utils.h" #include "mm-log.h" #include "mm-modem-helpers.h" @@ -475,7 +474,7 @@ sms_get_store_or_send_command (MMSmsPart *part, return FALSE; /* Convert PDU to hex */ - hex = utils_bin2hexstr (pdu, pdulen); + hex = mm_utils_bin2hexstr (pdu, pdulen); g_free (pdu); if (!hex) { diff --git a/src/mm-utils.c b/src/mm-utils.c deleted file mode 100644 index 236c2d9f..00000000 --- a/src/mm-utils.c +++ /dev/null @@ -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; -} diff --git a/src/mm-utils.h b/src/mm-utils.h deleted file mode 100644 index be77f8a8..00000000 --- a/src/mm-utils.h +++ /dev/null @@ -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 */ - diff --git a/src/tests/test-sms-part.c b/src/tests/test-sms-part.c index 3f6f16da..f052913c 100644 --- a/src/tests/test-sms-part.c +++ b/src/tests/test-sms-part.c @@ -20,8 +20,9 @@ #include <stdio.h> #include <locale.h> +#include <libmm-common.h> + #include "mm-sms-part.h" -#include "mm-utils.h" #include "mm-log.h" /* If defined will print debugging traces */ @@ -96,7 +97,7 @@ common_test_part_from_pdu (const guint8 *pdu, { gchar *hexpdu; - hexpdu = utils_bin2hexstr (pdu, pdu_size); + hexpdu = mm_utils_bin2hexstr (pdu, pdu_size); common_test_part_from_hexpdu (hexpdu, expected_smsc, expected_number, @@ -353,7 +354,7 @@ test_pdu_insufficient_data (void) 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); g_assert (part == NULL); /* We don't care for the specific error type */ |