[PATCH v2 1/3] mbim-common: turn __mbim_utils_str_hex into mbim_common_str_hex
Ben Chan
benchan at chromium.org
Fri Jul 28 02:45:53 UTC 2017
__mbim_utils_str_hex is a useful utility method for returning a hexadecimal
string representation of a sequence of bytes in memory. This patch turns it
into 'mbim_common_str_hex', which can be shared between libmbim-glib and
mbimcli.
---
configure.ac | 7 ++++
src/Makefile.am | 2 +-
src/common/Makefile.am | 15 +++++++
src/common/mbim-common.c | 61 ++++++++++++++++++++++++++++
src/common/mbim-common.h | 31 ++++++++++++++
src/libmbim-glib/Makefile.am | 2 +
src/libmbim-glib/mbim-device.c | 15 +++----
src/libmbim-glib/mbim-utils.c | 36 ----------------
src/libmbim-glib/mbim-utils.h | 3 --
src/libmbim-glib/test/Makefile.am | 4 ++
src/libmbim-glib/test/test-message-builder.c | 6 +--
src/libmbim-glib/test/test-message-parser.c | 6 +--
12 files changed, 135 insertions(+), 53 deletions(-)
create mode 100644 src/common/Makefile.am
create mode 100644 src/common/mbim-common.c
create mode 100644 src/common/mbim-common.h
diff --git a/configure.ac b/configure.ac
index b05333e..b3ecb7e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -67,6 +67,12 @@ AC_SUBST(MBIM_GLIB_LT_AGE)
dnl Required dependency versions
GLIB_MIN_VERSION=2.36
+dnl General dependencies for common
+PKG_CHECK_MODULES(MBIM_COMMON,
+ glib-2.0 >= $GLIB_MIN_VERSION)
+AC_SUBST(MBIM_COMMON_CFLAGS)
+AC_SUBST(MBIM_COMMON_LIBS)
+
dnl General dependencies for libmbim-glib
PKG_CHECK_MODULES(LIBMBIM_GLIB,
glib-2.0 >= $GLIB_MIN_VERSION
@@ -166,6 +172,7 @@ AC_CONFIG_FILES([Makefile
data/pkg-config/Makefile
data/pkg-config/mbim-glib.pc
src/Makefile
+ src/common/Makefile
src/libmbim-glib/Makefile
src/libmbim-glib/mbim-version.h
src/libmbim-glib/generated/Makefile
diff --git a/src/Makefile.am b/src/Makefile.am
index 6732c9e..068ad31 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,2 +1,2 @@
-SUBDIRS = libmbim-glib mbimcli mbim-proxy
+SUBDIRS = common libmbim-glib mbimcli mbim-proxy
diff --git a/src/common/Makefile.am b/src/common/Makefile.am
new file mode 100644
index 0000000..747d077
--- /dev/null
+++ b/src/common/Makefile.am
@@ -0,0 +1,15 @@
+SUBDIRS = .
+
+# common library, built as a noinst
+noinst_LTLIBRARIES = libmbim-common.la
+
+libmbim_common_la_CPPFLAGS = \
+ $(MBIM_COMMON_CFLAGS) \
+ -I$(top_srcdir) \
+ -I$(top_builddir) \
+ -I$(top_srcdir)/src/common
+libmbim_common_la_SOURCES = \
+ mbim-common.h \
+ mbim-common.c
+libmbim_common_la_LIBADD = \
+ $(MBIM_COMMON_LIBS)
diff --git a/src/common/mbim-common.c b/src/common/mbim-common.c
new file mode 100644
index 0000000..fd9bc86
--- /dev/null
+++ b/src/common/mbim-common.c
@@ -0,0 +1,61 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ *
+ * Copyright (C) 2013 - 2014 Aleksander Morgado <aleksander at aleksander.es>
+ * Copyright (C) 2017 Google Inc.
+ */
+
+#include <config.h>
+#include <stdio.h>
+
+#include "mbim-common.h"
+
+/*****************************************************************************/
+
+gchar *
+mbim_common_str_hex (gconstpointer mem,
+ gsize size,
+ gchar delimiter)
+{
+ const guint8 *data = mem;
+ gsize i;
+ gsize j;
+ gsize new_str_length;
+ gchar *new_str;
+
+ /* Get new string length. If input string has N bytes, we need:
+ * - 1 byte for last NUL char
+ * - 2N bytes for hexadecimal char representation of each byte...
+ * - N-1 bytes for the separator ':'
+ * So... a total of (1+2N+N-1) = 3N bytes are needed... */
+ new_str_length = 3 * size;
+
+ /* Allocate memory for new array and initialize contents to NUL */
+ new_str = g_malloc0 (new_str_length);
+
+ /* Print hexadecimal representation of each byte... */
+ for (i = 0, j = 0; i < size; i++, j += 3) {
+ /* Print character in output string... */
+ snprintf (&new_str[j], 3, "%02X", data[i]);
+ /* And if needed, add separator */
+ if (i != (size - 1) )
+ new_str[j + 2] = delimiter;
+ }
+
+ /* Set output string */
+ return new_str;
+}
diff --git a/src/common/mbim-common.h b/src/common/mbim-common.h
new file mode 100644
index 0000000..3b74dcd
--- /dev/null
+++ b/src/common/mbim-common.h
@@ -0,0 +1,31 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ *
+ * Copyright (C) 2013 - 2014 Aleksander Morgado <aleksander at aleksander.es>
+ * Copyright (C) 2017 Google Inc.
+ */
+
+#ifndef _COMMON_MBIM_COMMON_H_
+#define _COMMON_MBIM_COMMON_H_
+
+#include <glib.h>
+
+gchar *mbim_common_str_hex (gconstpointer mem,
+ gsize size,
+ gchar delimiter);
+
+#endif /* _COMMON_MBIM_COMMON_H_ */
diff --git a/src/libmbim-glib/Makefile.am b/src/libmbim-glib/Makefile.am
index d2a675d..49b2190 100644
--- a/src/libmbim-glib/Makefile.am
+++ b/src/libmbim-glib/Makefile.am
@@ -8,6 +8,7 @@ libmbim_glib_core_la_CPPFLAGS = \
$(GUDEV_CFLAGS) \
-I$(top_srcdir) \
-I$(top_builddir) \
+ -I$(top_srcdir)/src/common \
-I$(top_srcdir)/src/libmbim-glib \
-I$(top_srcdir)/src/libmbim-glib/generated \
-I$(top_builddir)/src/libmbim-glib \
@@ -36,6 +37,7 @@ libmbim_glib_la_SOURCES = \
libmbim_glib_la_LIBADD = \
libmbim-glib-core.la \
+ ${top_builddir}/src/common/libmbim-common.la \
${top_builddir}/src/libmbim-glib/generated/libmbim-glib-generated.la \
$(LIBMBIM_GLIB_LIBS) \
$(GUDEV_LIBS)
diff --git a/src/libmbim-glib/mbim-device.c b/src/libmbim-glib/mbim-device.c
index 612ffe1..68544af 100644
--- a/src/libmbim-glib/mbim-device.c
+++ b/src/libmbim-glib/mbim-device.c
@@ -43,6 +43,7 @@
# include <gudev/gudev.h>
#endif
+#include "mbim-common.h"
#include "mbim-utils.h"
#include "mbim-device.h"
#include "mbim-message.h"
@@ -495,9 +496,9 @@ process_message (MbimDevice *self,
if (mbim_utils_get_traces_enabled ()) {
gchar *printable;
- printable = __mbim_utils_str_hex (((GByteArray *)message)->data,
- ((GByteArray *)message)->len,
- ':');
+ printable = mbim_common_str_hex (((GByteArray *)message)->data,
+ ((GByteArray *)message)->len,
+ ':');
g_debug ("[%s] Received message...%s\n"
">>>>>> RAW:\n"
">>>>>> length = %u\n"
@@ -1812,7 +1813,7 @@ device_send (MbimDevice *self,
if (mbim_utils_get_traces_enabled ()) {
gchar *printable;
- printable = __mbim_utils_str_hex (raw_message, raw_message_len, ':');
+ printable = mbim_common_str_hex (raw_message, raw_message_len, ':');
g_debug ("[%s] Sent message...\n"
"<<<<<< RAW:\n"
"<<<<<< length = %u\n"
@@ -1847,9 +1848,9 @@ device_send (MbimDevice *self,
gchar *printable_fh;
gchar *printable_d;
- printable_h = __mbim_utils_str_hex (&fragments[i].header, sizeof (fragments[i].header), ':');
- printable_fh = __mbim_utils_str_hex (&fragments[i].fragment_header, sizeof (fragments[i].fragment_header), ':');
- printable_d = __mbim_utils_str_hex (fragments[i].data, fragments[i].data_length, ':');
+ printable_h = mbim_common_str_hex (&fragments[i].header, sizeof (fragments[i].header), ':');
+ printable_fh = mbim_common_str_hex (&fragments[i].fragment_header, sizeof (fragments[i].fragment_header), ':');
+ printable_d = mbim_common_str_hex (fragments[i].data, fragments[i].data_length, ':');
g_debug ("[%s] Sent fragment (%u)...\n"
"<<<<<< RAW:\n"
"<<<<<< length = %u\n"
diff --git a/src/libmbim-glib/mbim-utils.c b/src/libmbim-glib/mbim-utils.c
index a7541ae..f360542 100644
--- a/src/libmbim-glib/mbim-utils.c
+++ b/src/libmbim-glib/mbim-utils.c
@@ -38,42 +38,6 @@
* with the MBIM library.
**/
-/*****************************************************************************/
-
-gchar *
-__mbim_utils_str_hex (gconstpointer mem,
- gsize size,
- gchar delimiter)
-{
- const guint8 *data = mem;
- gsize i;
- gsize j;
- gsize new_str_length;
- gchar *new_str;
-
- /* Get new string length. If input string has N bytes, we need:
- * - 1 byte for last NUL char
- * - 2N bytes for hexadecimal char representation of each byte...
- * - N-1 bytes for the separator ':'
- * So... a total of (1+2N+N-1) = 3N bytes are needed... */
- new_str_length = 3 * size;
-
- /* Allocate memory for new array and initialize contents to NUL */
- new_str = g_malloc0 (new_str_length);
-
- /* Print hexadecimal representation of each byte... */
- for (i = 0, j = 0; i < size; i++, j += 3) {
- /* Print character in output string... */
- snprintf (&new_str[j], 3, "%02X", data[i]);
- /* And if needed, add separator */
- if (i != (size - 1) )
- new_str[j + 2] = delimiter;
- }
-
- /* Set output string */
- return new_str;
-}
-
/*****************************************************************************/
gboolean
__mbim_user_allowed (uid_t uid,
diff --git a/src/libmbim-glib/mbim-utils.h b/src/libmbim-glib/mbim-utils.h
index ee5632f..77ed4ca 100644
--- a/src/libmbim-glib/mbim-utils.h
+++ b/src/libmbim-glib/mbim-utils.h
@@ -39,9 +39,6 @@ void mbim_utils_set_traces_enabled (gboolean enabled);
/* Other private methods */
#if defined (LIBMBIM_GLIB_COMPILATION)
-gchar *__mbim_utils_str_hex (gconstpointer mem,
- gsize size,
- gchar delimiter);
gboolean __mbim_user_allowed (uid_t uid,
GError **error);
#endif
diff --git a/src/libmbim-glib/test/Makefile.am b/src/libmbim-glib/test/Makefile.am
index cddfb26..d492ed8 100644
--- a/src/libmbim-glib/test/Makefile.am
+++ b/src/libmbim-glib/test/Makefile.am
@@ -69,11 +69,13 @@ test_message_parser_SOURCES = \
test_message_parser_CPPFLAGS = \
$(LIBMBIM_GLIB_CFLAGS) \
-I$(top_srcdir) \
+ -I$(top_srcdir)/src/common \
-I$(top_srcdir)/src/libmbim-glib \
-I$(top_builddir)/src/libmbim-glib \
-I$(top_builddir)/src/libmbim-glib/generated \
-DLIBMBIM_GLIB_COMPILATION
test_message_parser_LDADD = \
+ $(top_builddir)/src/common/libmbim-common.la \
$(top_builddir)/src/libmbim-glib/libmbim-glib-core.la \
$(top_builddir)/src/libmbim-glib/generated/libmbim-glib-generated.la \
$(LIBMBIM_GLIB_LIBS)
@@ -83,11 +85,13 @@ test_message_builder_SOURCES = \
test_message_builder_CPPFLAGS = \
$(LIBMBIM_GLIB_CFLAGS) \
-I$(top_srcdir) \
+ -I$(top_srcdir)/src/common \
-I$(top_srcdir)/src/libmbim-glib \
-I$(top_builddir)/src/libmbim-glib \
-I$(top_builddir)/src/libmbim-glib/generated \
-DLIBMBIM_GLIB_COMPILATION
test_message_builder_LDADD = \
+ $(top_builddir)/src/common/libmbim-common.la \
$(top_builddir)/src/libmbim-glib/libmbim-glib-core.la \
$(top_builddir)/src/libmbim-glib/generated/libmbim-glib-generated.la \
$(LIBMBIM_GLIB_LIBS)
diff --git a/src/libmbim-glib/test/test-message-builder.c b/src/libmbim-glib/test/test-message-builder.c
index 18fd29d..c467eef 100644
--- a/src/libmbim-glib/test/test-message-builder.c
+++ b/src/libmbim-glib/test/test-message-builder.c
@@ -20,7 +20,7 @@
#include "mbim-message-private.h"
#include "mbim-cid.h"
#include "mbim-enums.h"
-#include "mbim-utils.h"
+#include "mbim-common.h"
#include "mbim-basic-connect.h"
#include "mbim-ussd.h"
#include "mbim-auth.h"
@@ -38,8 +38,8 @@ test_message_trace (const guint8 *computed,
gchar *message_str;
gchar *expected_str;
- message_str = __mbim_utils_str_hex (computed, computed_size, ':');
- expected_str = __mbim_utils_str_hex (expected, expected_size, ':');
+ message_str = mbim_common_str_hex (computed, computed_size, ':');
+ expected_str = mbim_common_str_hex (expected, expected_size, ':');
/* Dump all message contents */
g_print ("\n"
diff --git a/src/libmbim-glib/test/test-message-parser.c b/src/libmbim-glib/test/test-message-parser.c
index a71c8c5..3ba4946 100644
--- a/src/libmbim-glib/test/test-message-parser.c
+++ b/src/libmbim-glib/test/test-message-parser.c
@@ -24,7 +24,7 @@
#include "mbim-ms-firmware-id.h"
#include "mbim-message.h"
#include "mbim-cid.h"
-#include "mbim-utils.h"
+#include "mbim-common.h"
#if defined ENABLE_TEST_MESSAGE_TRACES
static void
@@ -36,8 +36,8 @@ test_message_trace (const guint8 *computed,
gchar *message_str;
gchar *expected_str;
- message_str = __mbim_utils_str_hex (computed, computed_size, ':');
- expected_str = __mbim_utils_str_hex (expected, expected_size, ':');
+ message_str = mbim_common_str_hex (computed, computed_size, ':');
+ expected_str = mbim_common_str_hex (expected, expected_size, ':');
/* Dump all message contents */
g_print ("\n"
--
2.14.0.rc0.400.g1c36432dff-goog
More information about the libmbim-devel
mailing list