summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@aleksander.es>2016-12-08 11:37:12 +0100
committerAleksander Morgado <aleksander@aleksander.es>2017-01-16 11:24:14 +0100
commitca44b38814fabbd3d8621acc1049157e64dd074c (patch)
tree739440140db74545dff1df90a736cd25ec3ff5c1
parent537fb19400c4b949c3b8b411340ca9af06c94cc8 (diff)
qmi-firmware-update: new parser for firmware/config/carrier strings
-rw-r--r--.gitignore2
-rw-r--r--configure.ac1
-rw-r--r--src/qmi-firmware-update/Makefile.am17
-rw-r--r--src/qmi-firmware-update/qfu-utils.c48
-rw-r--r--src/qmi-firmware-update/qfu-utils.h7
-rw-r--r--src/qmi-firmware-update/test/Makefile.am24
-rw-r--r--src/qmi-firmware-update/test/test-utils.c104
7 files changed, 203 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 79216de..99a4fe6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -79,3 +79,5 @@ Makefile.in
/utils/swi-update
/src/qmi-firmware-update/qfu-enum-types.*
+
+/src/qmi-firmware-update/test/test-utils
diff --git a/configure.ac b/configure.ac
index 311c10e..d4b6abf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -173,6 +173,7 @@ AC_CONFIG_FILES([Makefile
src/qmicli/test/Makefile
src/qmi-proxy/Makefile
src/qmi-firmware-update/Makefile
+ src/qmi-firmware-update/test/Makefile
utils/Makefile
docs/Makefile
docs/reference/Makefile
diff --git a/src/qmi-firmware-update/Makefile.am b/src/qmi-firmware-update/Makefile.am
index 86e857d..cbe8ed1 100644
--- a/src/qmi-firmware-update/Makefile.am
+++ b/src/qmi-firmware-update/Makefile.am
@@ -1,4 +1,20 @@
+SUBDIRS = . test
+
+noinst_LTLIBRARIES = libutils.la
+
+libutils_la_SOURCES = \
+ qfu-utils.h qfu-utils.c \
+ $(NULL)
+
+libutils_la_CPPFLAGS = \
+ $(GLIB_CFLAGS) \
+ $(NULL)
+
+libutils_la_LIBADD = \
+ $(GLIB_CFLAGS) \
+ $(NULL)
+
bin_PROGRAMS = qmi-firmware-update
qmi_firmware_update_CPPFLAGS = \
@@ -60,6 +76,7 @@ qmi_firmware_update_SOURCES = \
qmi_firmware_update_LDADD = \
$(GUDEV_LIBS) \
$(GLIB_LIBS) \
+ $(builddir)/libutils.la \
$(top_builddir)/src/libqmi-glib/libqmi-glib.la \
$(NULL)
diff --git a/src/qmi-firmware-update/qfu-utils.c b/src/qmi-firmware-update/qfu-utils.c
index 905e328..4e0b6e3 100644
--- a/src/qmi-firmware-update/qfu-utils.c
+++ b/src/qmi-firmware-update/qfu-utils.c
@@ -27,6 +27,7 @@
#include <string.h>
#include <glib.h>
+#include <gio/gio.h>
#include "qfu-utils.h"
@@ -165,3 +166,50 @@ qfu_utils_crc16 (const guint8 *buffer,
crc = crc_table[(crc ^ *buffer++) & 0xff] ^ (crc >> 8);
return ~crc;
}
+
+/******************************************************************************/
+
+gboolean
+qfu_utils_parse_cwe_version_string (const gchar *version,
+ gchar **firmware_version,
+ gchar **config_version,
+ gchar **carrier,
+ GError **error)
+{
+ GRegex *regex;
+ GMatchInfo *match_info = NULL;
+ gboolean result = FALSE;
+
+ regex = g_regex_new ("(?:.*)"
+ "_([0-9][0-9]\\.[0-9][0-9]\\.[0-9][0-9]\\.[0-9][0-9])" /* firmware version */
+ "(?:"
+ "(?:.*)"
+ "_([a-zA-Z\\-]+)" /* carrier */
+ "_([0-9][0-9][0-9]\\.[0-9][0-9][0-9]_[0-9][0-9][0-9])" /* config version */
+ ")?",
+ 0, 0, NULL);
+ g_assert (regex);
+
+ if (!g_regex_match_full (regex, version, strlen (version), 0, 0, &match_info, NULL)) {
+ g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+ "couldn't parse CWE version string '%s': didn't match",
+ version);
+ goto out;
+ }
+
+ if (firmware_version)
+ *firmware_version = g_match_info_fetch (match_info, 1);
+ if (carrier)
+ *carrier = (g_match_info_get_match_count (match_info) > 2 ? g_match_info_fetch (match_info, 2) : NULL);
+ if (config_version)
+ *config_version = (g_match_info_get_match_count (match_info) > 3 ? g_match_info_fetch (match_info, 3) : NULL);
+
+ result = TRUE;
+
+out:
+ if (match_info)
+ g_match_info_free (match_info);
+ g_regex_unref (regex);
+
+ return result;
+}
diff --git a/src/qmi-firmware-update/qfu-utils.h b/src/qmi-firmware-update/qfu-utils.h
index 2c66aad..8180a83 100644
--- a/src/qmi-firmware-update/qfu-utils.h
+++ b/src/qmi-firmware-update/qfu-utils.h
@@ -24,6 +24,7 @@
#define QFU_UTILS_H
#include <glib.h>
+#include <gio/gio.h>
G_BEGIN_DECLS
@@ -36,6 +37,12 @@ gchar *qfu_utils_get_firmware_image_unique_id_printable (const GArray *unique_id
guint16 qfu_utils_crc16 (const guint8 *buffer,
gsize len);
+gboolean qfu_utils_parse_cwe_version_string (const gchar *version,
+ gchar **firmware_version,
+ gchar **config_version,
+ gchar **carrier,
+ GError **error);
+
G_END_DECLS
#endif /* QFU_UTILS_H */
diff --git a/src/qmi-firmware-update/test/Makefile.am b/src/qmi-firmware-update/test/Makefile.am
new file mode 100644
index 0000000..7ef33d3
--- /dev/null
+++ b/src/qmi-firmware-update/test/Makefile.am
@@ -0,0 +1,24 @@
+include $(top_srcdir)/gtester.make
+
+noinst_PROGRAMS = \
+ test-utils \
+ $(NULL)
+
+TEST_PROGS += $(noinst_PROGRAMS)
+
+test_utils_SOURCES = \
+ test-utils.c \
+ $(NULL)
+
+test_utils_CPPFLAGS = \
+ $(GLIB_CFLAGS) \
+ -I$(top_srcdir) \
+ -I$(top_srcdir)/src/qmi-firmware-update \
+ -I$(top_builddir)/src/qmi-firmware-update \
+ -DLIBQMI_GLIB_COMPILATION \
+ $(NULL)
+
+test_utils_LDADD = \
+ $(top_builddir)/src/qmi-firmware-update/libutils.la \
+ $(GLIB_LIBS) \
+ $(NULL)
diff --git a/src/qmi-firmware-update/test/test-utils.c b/src/qmi-firmware-update/test/test-utils.c
new file mode 100644
index 0000000..7a77eeb
--- /dev/null
+++ b/src/qmi-firmware-update/test/test-utils.c
@@ -0,0 +1,104 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * qmi-firmware-update -- Command line tool to update firmware in 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) 2016 Zodiac Inflight Innovations
+ * Copyright (C) 2016 Aleksander Morgado <aleksander@aleksander.es>
+ */
+
+#include "qfu-utils.h"
+
+/******************************************************************************/
+
+static void
+common_version_parser_test (const gchar *version,
+ const gchar *expected_firmware_version,
+ const gchar *expected_config_version,
+ const gchar *expected_carrier)
+{
+ gboolean res;
+ GError *error = NULL;
+ gchar *firmware_version = NULL;
+ gchar *config_version = NULL;
+ gchar *carrier = NULL;
+
+ res = qfu_utils_parse_cwe_version_string (version,
+ &firmware_version,
+ &config_version,
+ &carrier,
+ &error);
+ g_assert_no_error (error);
+ g_assert (res);
+
+ g_assert_cmpstr (firmware_version, ==, expected_firmware_version);
+ g_assert_cmpstr (config_version, ==, expected_config_version);
+ g_assert_cmpstr (carrier, ==, expected_carrier);
+
+ g_free (firmware_version);
+ g_free (config_version);
+ g_free (carrier);
+}
+
+static void
+test_cwe_version_parser_mc7700 (void)
+{
+ common_version_parser_test ("9999999_9999999_9200_03.05.29.03_00_generic_000.000_001_SPKG_MC",
+ "03.05.29.03",
+ "000.000_001",
+ "generic");
+}
+
+static void
+test_cwe_version_parser_mc7354_cwe (void)
+{
+ common_version_parser_test ("INTERNAL_?_SWI9X15C_05.05.63.01_?_?_?_?",
+ "05.05.63.01",
+ NULL,
+ NULL);
+}
+
+static void
+test_cwe_version_parser_mc7354_nvu (void)
+{
+ common_version_parser_test ("9999999_9902350_SWI9X15C_05.05.63.01_00_SPRINT_005.037_000",
+ "05.05.63.01",
+ "005.037_000",
+ "SPRINT");
+}
+
+static void
+test_cwe_version_parser_mc7354b_spk (void)
+{
+ common_version_parser_test ("9999999_9902574_SWI9X15C_05.05.66.00_00_GENNA-UMTS_005.028_000",
+ "05.05.66.00",
+ "005.028_000",
+ "GENNA-UMTS");
+}
+
+/******************************************************************************/
+
+int main (int argc, char **argv)
+{
+ g_type_init ();
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/qmi-firmware-update/cwe-version-parser/mc7700", test_cwe_version_parser_mc7700);
+ g_test_add_func ("/qmi-firmware-update/cwe-version-parser/mc7354/cwe", test_cwe_version_parser_mc7354_cwe);
+ g_test_add_func ("/qmi-firmware-update/cwe-version-parser/mc7354/nvu", test_cwe_version_parser_mc7354_nvu);
+ g_test_add_func ("/qmi-firmware-update/cwe-version-parser/mc7354b/spk", test_cwe_version_parser_mc7354b_spk);
+
+ return g_test_run ();
+}