diff options
author | Ben Chan <benchan@chromium.org> | 2018-08-14 14:53:04 -0700 |
---|---|---|
committer | Ben Chan <benchan@chromium.org> | 2018-08-18 13:34:06 -0700 |
commit | eb327b4203601a6c09f6369927325767391b86e7 (patch) | |
tree | dfbe394d5c4cfabdec8982260c867591b0f326a8 | |
parent | 28e64d00181c83fd6cd420c46b6040f7b1afe5fb (diff) | |
download | ModemManager-eb327b4203601a6c09f6369927325767391b86e7.tar.gz |
libmm-glib,pco: add helpers for managing a list of MMPco
-rw-r--r-- | libmm-glib/mm-pco.c | 36 | ||||
-rw-r--r-- | libmm-glib/mm-pco.h | 4 | ||||
-rw-r--r-- | libmm-glib/tests/Makefile.am | 23 | ||||
-rw-r--r-- | libmm-glib/tests/test-pco.c | 95 |
4 files changed, 150 insertions, 8 deletions
diff --git a/libmm-glib/mm-pco.c b/libmm-glib/mm-pco.c index 07db363da..7ee347b02 100644 --- a/libmm-glib/mm-pco.c +++ b/libmm-glib/mm-pco.c @@ -224,6 +224,42 @@ mm_pco_to_variant (MMPco *self) /*****************************************************************************/ +void +mm_pco_list_free (GList *pco_list) +{ + g_list_free_full (pco_list, g_object_unref); +} + +GList * +mm_pco_list_add (GList *pco_list, + MMPco *pco) +{ + GList *iter; + guint32 session_id; + + g_return_val_if_fail (pco != NULL, pco_list); + + session_id = mm_pco_get_session_id (pco); + + for (iter = g_list_first (pco_list); iter; iter = g_list_next (iter)) { + MMPco *iter_pco = iter->data; + guint32 iter_session_id = mm_pco_get_session_id (iter_pco); + + if (iter_session_id < session_id) + continue; + else if (iter_session_id == session_id) { + iter->data = g_object_ref (pco); + g_object_unref (iter_pco); + return pco_list; + } else + break; + } + + return g_list_insert_before (pco_list, iter, g_object_ref (pco)); +} + +/*****************************************************************************/ + MMPco * mm_pco_new (void) { diff --git a/libmm-glib/mm-pco.h b/libmm-glib/mm-pco.h index e9c47445b..ee5c5b364 100644 --- a/libmm-glib/mm-pco.h +++ b/libmm-glib/mm-pco.h @@ -61,6 +61,10 @@ gboolean mm_pco_is_complete (MMPco *self); const guint8 *mm_pco_get_data (MMPco *self, gsize *data_size); +void mm_pco_list_free (GList *pco_list); +GList *mm_pco_list_add (GList *pco_list, + MMPco *pco); + /*****************************************************************************/ /* ModemManager/libmm-glib/mmcli specific methods */ diff --git a/libmm-glib/tests/Makefile.am b/libmm-glib/tests/Makefile.am index f98f9d7cb..8984d08d4 100644 --- a/libmm-glib/tests/Makefile.am +++ b/libmm-glib/tests/Makefile.am @@ -3,13 +3,7 @@ include $(top_srcdir)/gtester.make AM_CFLAGS = $(CODE_COVERAGE_CFLAGS) AM_LDFLAGS = $(CODE_COVERAGE_LDFLAGS) -noinst_PROGRAMS = test-common-helpers -TEST_PROGS += $(noinst_PROGRAMS) - -test_common_helpers_SOURCES = \ - test-common-helpers.c - -test_common_helpers_CPPFLAGS = \ +LIBMM_GLIB_TESTS_COMMON_CPPFLAGS = \ $(MM_CFLAGS) \ -I$(top_srcdir) \ -I$(top_srcdir)/include \ @@ -20,6 +14,19 @@ test_common_helpers_CPPFLAGS = \ -I${top_builddir}/libmm-glib/generated \ -DLIBMM_GLIB_COMPILATION -test_common_helpers_LDADD = \ +LIBMM_GLIB_TESTS_COMMON_LDADD = \ $(top_builddir)/libmm-glib/libmm-glib.la \ $(MM_LIBS) + +noinst_PROGRAMS = \ + test-common-helpers \ + test-pco +TEST_PROGS += $(noinst_PROGRAMS) + +test_common_helpers_SOURCES = test-common-helpers.c +test_common_helpers_CPPFLAGS = $(LIBMM_GLIB_TESTS_COMMON_CPPFLAGS) +test_common_helpers_LDADD = $(LIBMM_GLIB_TESTS_COMMON_LDADD) + +test_pco_SOURCES = test-pco.c +test_pco_CPPFLAGS = $(LIBMM_GLIB_TESTS_COMMON_CPPFLAGS) +test_pco_LDADD = $(LIBMM_GLIB_TESTS_COMMON_LDADD) diff --git a/libmm-glib/tests/test-pco.c b/libmm-glib/tests/test-pco.c new file mode 100644 index 000000000..c25606fc3 --- /dev/null +++ b/libmm-glib/tests/test-pco.c @@ -0,0 +1,95 @@ +/* -*- 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 2018 Google LLC. + */ + +#include <glib.h> +#include <libmm-glib.h> +#include <string.h> + +typedef struct { + guint32 session_id; + gboolean is_complete; + gsize pco_data_size; + guint8 pco_data[50]; +} TestPco; + +static const TestPco test_pco_list[] = { + { 3, TRUE, 8, { 0x27, 0x06, 0x80, 0x00, 0x10, 0x02, 0x05, 0x94 } }, + { 1, FALSE, 3, { 0x27, 0x01, 0x80 } }, + { 5, FALSE, 10, { 0x27, 0x08, 0x80, 0xFF, 0x00, 0x04, 0x13, 0x01, 0x84, 0x05 } }, + { 4, TRUE, 14, { 0x27, 0x0C, 0x80, 0x10, 0x02, 0x05, 0x94, 0xFF, 0x00, 0x04, 0x13, 0x01, 0x84, 0x05 } }, + { 3, FALSE, 10, { 0x27, 0x08, 0x80, 0x00, 0x0D, 0x04, 0xC6, 0xE0, 0xAD, 0x87 } }, +}; + +static const TestPco expected_pco_list[] = { + { 1, FALSE, 3, { 0x27, 0x01, 0x80 } }, + { 3, FALSE, 10, { 0x27, 0x08, 0x80, 0x00, 0x0D, 0x04, 0xC6, 0xE0, 0xAD, 0x87 } }, + { 4, TRUE, 14, { 0x27, 0x0C, 0x80, 0x10, 0x02, 0x05, 0x94, 0xFF, 0x00, 0x04, 0x13, 0x01, 0x84, 0x05 } }, + { 5, FALSE, 10, { 0x27, 0x08, 0x80, 0xFF, 0x00, 0x04, 0x13, 0x01, 0x84, 0x05 } }, +}; + +static void +test_pco_list_add (void) +{ + GList *list = NULL; + guint i; + + for (i = 0; i < G_N_ELEMENTS (test_pco_list); ++i) { + const TestPco *test_pco = &test_pco_list[i]; + MMPco *pco; + + pco = mm_pco_new (); + mm_pco_set_session_id (pco, test_pco->session_id); + mm_pco_set_complete (pco, test_pco->is_complete); + mm_pco_set_data (pco, test_pco->pco_data, test_pco->pco_data_size); + list = mm_pco_list_add (list, pco); + } + + g_assert (list != NULL); + g_assert_cmpuint (g_list_length (list), ==, G_N_ELEMENTS (expected_pco_list)); + + for (i = 0; i < G_N_ELEMENTS (expected_pco_list); ++i) { + GList *current; + MMPco *pco; + const TestPco *expected_pco; + gsize pco_data_size; + const guint8 *pco_data; + + current = g_list_nth (list, i); + pco = current->data; + expected_pco = &expected_pco_list[i]; + + g_assert (pco != NULL); + g_assert_cmpuint (mm_pco_get_session_id (pco), ==, expected_pco->session_id); + g_assert (mm_pco_is_complete (pco) == expected_pco->is_complete); + pco_data = mm_pco_get_data (pco, &pco_data_size); + g_assert (pco_data != NULL); + g_assert_cmpuint (pco_data_size, ==, expected_pco->pco_data_size); + g_assert_cmpmem (pco_data, pco_data_size, + expected_pco->pco_data, expected_pco->pco_data_size); + } + + mm_pco_list_free (list); +} + +/**************************************************************/ + +int main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/MM/Pco/pco-list-add", test_pco_list_add); + + return g_test_run (); +} |