aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore5
-rw-r--r--cli/Makefile.am5
-rw-r--r--cli/qmicli-helpers.c102
-rw-r--r--cli/qmicli-helpers.h30
-rw-r--r--cli/test/Makefile.am23
-rw-r--r--cli/test/test-helpers.c125
-rw-r--r--configure.ac1
7 files changed, 291 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 2f0e464..998d62e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -57,5 +57,10 @@ libqmi-glib/test/test-utils
cli/.deps
cli/.libs
cli/qmicli
+cli/test/.libs
+cli/test/.deps
+cli/test/Makefile
+cli/test/Makefile.in
+cli/test/test-helpers
build-aux/qmi-codegen/*.pyc
diff --git a/cli/Makefile.am b/cli/Makefile.am
index 65fd0db..a5db8b3 100644
--- a/cli/Makefile.am
+++ b/cli/Makefile.am
@@ -1,3 +1,6 @@
+
+SUBDIRS = . test
+
bin_PROGRAMS = qmicli
qmicli_CPPFLAGS = \
@@ -9,6 +12,8 @@ qmicli_CPPFLAGS = \
qmicli_SOURCES = \
qmicli.c \
qmicli.h \
+ qmicli-helpers.c \
+ qmicli-helpers.h \
qmicli-dms.c \
qmicli-wds.c
diff --git a/cli/qmicli-helpers.c b/cli/qmicli-helpers.c
new file mode 100644
index 0000000..032a9e5
--- /dev/null
+++ b/cli/qmicli-helpers.c
@@ -0,0 +1,102 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * qmicli -- Command line interface to control QMI devices
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org>
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "qmicli-helpers.h"
+
+gchar *
+qmicli_get_raw_data_printable (const GArray *data,
+ gsize max_line_length,
+ const gchar *line_prefix)
+{
+ gsize i;
+ gsize j;
+ gsize k;
+ gsize new_str_length;
+ gchar *new_str;
+ gsize prefix_len;
+ guint n_lines;
+ gboolean is_new_line;
+
+ g_return_val_if_fail (max_line_length >= 3, NULL);
+
+ if (!data)
+ return g_strdup ("");
+
+ /* 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 * data->len;
+
+ /* Effective max line length needs to be multiple of 3, we don't want to
+ * split in half a given byte representation */
+ while (max_line_length % 3 != 0)
+ max_line_length--;
+
+ /* Prefix len includes the newline character plus the length of the input
+ * prefix */
+ prefix_len = strlen (line_prefix) + 1;
+ /* We don't consider the last NUL byte when counting lines to generate */
+ n_lines = (new_str_length - 1) / max_line_length;
+ if ((new_str_length - 1) % max_line_length != 0)
+ n_lines++;
+
+ /* Build new str length expected when we prefix the string and we limit the
+ * line length */
+ new_str_length += (n_lines * prefix_len);
+
+ /* Allocate memory for new array and initialize contents to NUL */
+ new_str = g_malloc0 (new_str_length);
+
+ /* Print hexadecimal representation of each byte... */
+ is_new_line = TRUE;
+ for (i = 0, j = 0, k = 0; i < data->len; i++) {
+ if (is_new_line) {
+ strcpy (&new_str[j], line_prefix);
+ j += strlen (line_prefix);
+ is_new_line = FALSE;
+ }
+
+ /* Print character in output string... */
+ snprintf (&new_str[j], 3, "%02X", g_array_index (data, guint8, i));
+ j+=2;
+ k+=2;
+
+ if (i != (data->len - 1) ) {
+ new_str[j] = ':';
+ j++;
+ k++;
+ }
+
+ if (k % max_line_length == 0 ||
+ i == (data->len -1)) {
+ new_str[j] = '\n';
+ j++;
+ is_new_line = TRUE;
+ }
+ }
+
+ /* Set output string */
+ return new_str;
+}
diff --git a/cli/qmicli-helpers.h b/cli/qmicli-helpers.h
new file mode 100644
index 0000000..80a48d6
--- /dev/null
+++ b/cli/qmicli-helpers.h
@@ -0,0 +1,30 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * qmicli -- Command line interface to control QMI devices
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org>
+ */
+
+#include <glib.h>
+
+#ifndef __QMICLI_HELPERS_H__
+#define __QMICLI_HELPERS_H__
+
+gchar *qmicli_get_raw_data_printable (const GArray *data,
+ gsize max_line_length,
+ const gchar *new_line_prefix);
+
+#endif /* __QMICLI_H__ */
diff --git a/cli/test/Makefile.am b/cli/test/Makefile.am
new file mode 100644
index 0000000..b3be3c0
--- /dev/null
+++ b/cli/test/Makefile.am
@@ -0,0 +1,23 @@
+
+noinst_PROGRAMS = \
+ test-helpers
+
+test_helpers_SOURCES = \
+ test-helpers.c \
+ $(top_srcdir)/cli/qmicli-helpers.h \
+ $(top_srcdir)/cli/qmicli-helpers.c
+
+test_helpers_CPPFLAGS = \
+ $(QMICLI_CFLAGS) \
+ -I$(top_srcdir) \
+ -I$(top_srcdir)/cli
+
+test_helpers_LDADD = \
+ $(QMICLI_LIBS)
+
+if WITH_TESTS
+
+check-local: test-helpers
+ $(abs_builddir)/test-helpers
+
+endif
diff --git a/cli/test/test-helpers.c b/cli/test/test-helpers.c
new file mode 100644
index 0000000..824ed3e
--- /dev/null
+++ b/cli/test/test-helpers.c
@@ -0,0 +1,125 @@
+/* -*- 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) 2012 Aleksander Morgado <aleksander@gnu.org>
+ */
+
+#include <glib.h>
+#include "qmicli-helpers.h"
+
+static void
+test_helpers_raw_printable_1 (void)
+{
+ GArray *array;
+ gchar *printable;
+ static guint8 buffer[8] = {
+ 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00
+ };
+ static const gchar *expected =
+ "0F:\n"
+ "50:\n"
+ "EB:\n"
+ "E2:\n"
+ "B6:\n"
+ "00:\n"
+ "00:\n"
+ "00\n";
+
+ array = g_array_sized_new (FALSE, FALSE, 1, 8);
+ g_array_insert_vals (array, 0, buffer, 8);
+
+ printable = qmicli_get_raw_data_printable (array, 3, "");
+
+ g_assert_cmpstr (printable, ==, expected);
+ g_free (printable);
+ g_array_unref (array);
+}
+
+static void
+test_helpers_raw_printable_2 (void)
+{
+ GArray *array;
+ gchar *printable;
+ static guint8 buffer[8] = {
+ 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00
+ };
+ static const gchar *expected =
+ "\t0F:50:\n"
+ "\tEB:E2:\n"
+ "\tB6:00:\n"
+ "\t00:00\n";
+
+ array = g_array_sized_new (FALSE, FALSE, 1, 8);
+ g_array_insert_vals (array, 0, buffer, 8);
+
+ /* When passing 7, we'll be really getting 6 (the closest lower multiple of 3) */
+ printable = qmicli_get_raw_data_printable (array, 7, "\t");
+
+ g_assert_cmpstr (printable, ==, expected);
+ g_free (printable);
+ g_array_unref (array);
+}
+
+static void
+test_helpers_raw_printable_3 (void)
+{
+ GArray *array;
+ gchar *printable;
+ static guint8 buffer[8] = {
+ 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00
+ };
+ static const gchar *expected =
+ "\t\t\t0F:50:EB:E2:\n"
+ "\t\t\tB6:00:00:00\n";
+
+ array = g_array_sized_new (FALSE, FALSE, 1, 8);
+ g_array_insert_vals (array, 0, buffer, 8);
+
+ printable = qmicli_get_raw_data_printable (array, 12, "\t\t\t");
+
+ g_assert_cmpstr (printable, ==, expected);
+ g_free (printable);
+ g_array_unref (array);
+}
+
+static void
+test_helpers_raw_printable_4 (void)
+{
+ GArray *array;
+ gchar *printable;
+ static guint8 buffer[8] = {
+ 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00
+ };
+ static const gchar *expected =
+ "\t0F:50:EB:E2:B6:00:00:00\n";
+
+ array = g_array_sized_new (FALSE, FALSE, 1, 8);
+ g_array_insert_vals (array, 0, buffer, 8);
+
+ printable = qmicli_get_raw_data_printable (array, 24, "\t");
+
+ g_assert_cmpstr (printable, ==, expected);
+ g_free (printable);
+ g_array_unref (array);
+}
+
+int main (int argc, char **argv)
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/qmicli/helpers/raw-printable/1", test_helpers_raw_printable_1);
+ g_test_add_func ("/qmicli/helpers/raw-printable/2", test_helpers_raw_printable_2);
+ g_test_add_func ("/qmicli/helpers/raw-printable/3", test_helpers_raw_printable_3);
+ g_test_add_func ("/qmicli/helpers/raw-printable/4", test_helpers_raw_printable_4);
+
+ return g_test_run ();
+}
diff --git a/configure.ac b/configure.ac
index bcc7699..eed20cb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -67,6 +67,7 @@ AC_CONFIG_FILES([Makefile
libqmi-glib/Makefile
libqmi-glib/test/Makefile
cli/Makefile
+ cli/test/Makefile
utils/Makefile])
AC_OUTPUT