huawei: new helper to parse ^NDISSTATQRY responses

This commit is contained in:
Aleksander Morgado
2013-06-15 12:19:50 +02:00
parent 5e82707b3f
commit 8d0ec0fc24
5 changed files with 260 additions and 1 deletions

5
.gitignore vendored
View File

@@ -144,5 +144,10 @@ m4/xsize.m4
uml290/uml290mode uml290/uml290mode
plugins/test-modem-helpers-huawei
plugins/test-modem-helpers-huawei.log
plugins/test-modem-helpers-huawei.trs
plugins/test-suite.log
TAGS TAGS
ABOUT-NLS ABOUT-NLS

View File

@@ -1,3 +1,4 @@
include $(top_srcdir)/gtester.make
# Common CPPFLAGS and LDFLAGS # Common CPPFLAGS and LDFLAGS
@@ -30,6 +31,9 @@ endif
udevrulesdir = $(UDEV_BASE_DIR)/rules.d udevrulesdir = $(UDEV_BASE_DIR)/rules.d
udevrules_DATA = udevrules_DATA =
# Unit tests
noinst_PROGRAMS =
######################################## ########################################
# Icera-specific support # Icera-specific support
@@ -104,15 +108,28 @@ libmm_plugin_gobi_la_LDFLAGS = $(PLUGIN_COMMON_LINKER_FLAGS)
libmm_plugin_huawei_la_SOURCES = \ libmm_plugin_huawei_la_SOURCES = \
huawei/mm-plugin-huawei.c \ huawei/mm-plugin-huawei.c \
huawei/mm-plugin-huawei.h \ huawei/mm-plugin-huawei.h \
huawei/mm-modem-helpers-huawei.c \
huawei/mm-modem-helpers-huawei.h \
huawei/mm-broadband-modem-huawei.c \ huawei/mm-broadband-modem-huawei.c \
huawei/mm-broadband-modem-huawei.h \ huawei/mm-broadband-modem-huawei.h \
huawei/mm-broadband-bearer-huawei.c \ huawei/mm-broadband-bearer-huawei.c \
huawei/mm-broadband-bearer-huawei.h huawei/mm-broadband-bearer-huawei.h
libmm_plugin_huawei_la_CPPFLAGS = $(PLUGIN_COMMON_COMPILER_FLAGS) libmm_plugin_huawei_la_CPPFLAGS = $(PLUGIN_COMMON_COMPILER_FLAGS)
libmm_plugin_huawei_la_LDFLAGS = $(PLUGIN_COMMON_LINKER_FLAGS) libmm_plugin_huawei_la_LDFLAGS = $(PLUGIN_COMMON_LINKER_FLAGS)
udevrules_DATA += huawei/77-mm-huawei-net-port-types.rules udevrules_DATA += huawei/77-mm-huawei-net-port-types.rules
noinst_PROGRAMS += test-modem-helpers-huawei
test_modem_helpers_huawei_SOURCES = \
huawei/mm-modem-helpers-huawei.c \
huawei/mm-modem-helpers-huawei.h \
huawei/tests/test-modem-helpers-huawei.c
test_modem_helpers_huawei_CPPFLAGS = \
-I$(top_srcdir)/plugins/huawei \
$(PLUGIN_COMMON_COMPILER_FLAGS)
test_modem_helpers_huawei_LDFLAGS = $(top_builddir)/libmm-glib/libmm-glib.la
# MBM # MBM
libmm_plugin_mbm_la_SOURCES = \ libmm_plugin_mbm_la_SOURCES = \
mbm/mm-plugin-mbm.c \ mbm/mm-plugin-mbm.c \
@@ -353,6 +370,10 @@ libmm_plugin_telit_la_SOURCES = \
libmm_plugin_telit_la_CPPFLAGS = $(PLUGIN_COMMON_COMPILER_FLAGS) libmm_plugin_telit_la_CPPFLAGS = $(PLUGIN_COMMON_COMPILER_FLAGS)
libmm_plugin_telit_la_LDFLAGS = $(PLUGIN_COMMON_LINKER_FLAGS) libmm_plugin_telit_la_LDFLAGS = $(PLUGIN_COMMON_LINKER_FLAGS)
# Additional files to include in the distribution # Additional files to include in the distribution
EXTRA_DIST = \ EXTRA_DIST = \
$(udevrules_DATA) $(udevrules_DATA)
# Unit tests
TEST_PROGS += $(noinst_PROGRAMS)

View File

@@ -0,0 +1,100 @@
/* -*- 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) 2013 Huawei Technologies Co., Ltd
* Copyright (C) 2013 Aleksander Morgado <aleksander@gnu.org>
*/
#include <string.h>
#include <ModemManager.h>
#define _LIBMM_INSIDE_MM
#include <libmm-glib.h>
#include "mm-modem-helpers-huawei.h"
/*****************************************************************************/
/* ^NDISSTATQRY response parser */
gboolean
mm_huawei_parse_ndisstatqry_response (const gchar *response,
gboolean *ipv4_available,
gboolean *ipv4_connected,
gboolean *ipv6_available,
gboolean *ipv6_connected,
GError **error)
{
GRegex *r;
GMatchInfo *match_info;
GError *inner_error = NULL;
if (!response || !g_str_has_prefix (response, "^NDISSTATQRY:")) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Missing ^NDISSTATQRY prefix");
return FALSE;
}
*ipv4_available = FALSE;
*ipv6_available = FALSE;
/* The response maybe as:
* <CR><LF>^NDISSTATQRY: 1,,,IPV4<CR><LF>^NDISSTATQRY: 0,33,,IPV6<CR><LF>
* <CR><LF><CR><LF>OK<CR><LF>
* So we have to split the status for IPv4 and IPv6. For now, we only care
* about IPv4.
*/
r = g_regex_new ("\\^NDISSTATQRY:\\s*(\\d),(.*),(.*),(.*)(\\r\\n)?",
G_REGEX_DOLLAR_ENDONLY | G_REGEX_RAW,
0, NULL);
g_assert (r != NULL);
g_regex_match_full (r, response, strlen (response), 0, 0, &match_info, &inner_error);
while (!inner_error && g_match_info_matches (match_info)) {
gchar *ip_type_str;
guint connected;
/* Read values */
ip_type_str = mm_get_string_unquoted_from_match_info (match_info, 4);
if (!ip_type_str ||
!mm_get_uint_from_match_info (match_info, 1, &connected) ||
(connected != 0 && connected != 1)) {
inner_error = g_error_new (MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't parse ^NDISSTATQRY fields");
} else if (g_ascii_strcasecmp (ip_type_str, "IPV4") == 0) {
*ipv4_available = TRUE;
*ipv4_connected = (gboolean)connected;
} else if (g_ascii_strcasecmp (ip_type_str, "IPV6") == 0) {
*ipv6_available = TRUE;
*ipv6_connected = (gboolean)connected;
}
g_free (ip_type_str);
if (!inner_error)
g_match_info_next (match_info, &inner_error);
}
g_match_info_free (match_info);
g_regex_unref (r);
if (!ipv4_available && !ipv6_available) {
inner_error = g_error_new (MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't find IPv4 or IPv6 info in ^NDISSTATQRY response");
}
if (inner_error) {
g_propagate_error (error, inner_error);
return FALSE;
}
return TRUE;
}

View File

@@ -0,0 +1,30 @@
/* -*- 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) 2013 Huawei Technologies Co., Ltd
* Copyright (C) 2013 Aleksander Morgado <aleksander@gnu.org>
*/
#ifndef MM_MODEM_HELPERS_HUAWEI_H
#define MM_MODEM_HELPERS_HUAWEI_H
#include "glib.h"
/* ^NDISSTATQRY response parser */
gboolean mm_huawei_parse_ndisstatqry_response (const gchar *response,
gboolean *ipv4_available,
gboolean *ipv4_connected,
gboolean *ipv6_available,
gboolean *ipv6_connected,
GError **error);
#endif /* MM_MODEM_HELPERS_HUAWEI_H */

View File

@@ -0,0 +1,103 @@
/* -*- 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) 2013 Aleksander Morgado <aleksander@gnu.org>
*/
#include <glib.h>
#include <glib-object.h>
#include <locale.h>
#include "mm-modem-helpers-huawei.h"
/*****************************************************************************/
/* Test ^NDISSTATQRY responses */
typedef struct {
const gchar *str;
gboolean expected_ipv4_available;
gboolean expected_ipv4_connected;
gboolean expected_ipv6_available;
gboolean expected_ipv6_connected;
} NdisstatqryTest;
static const NdisstatqryTest ndisstatqry_tests[] = {
{ "^NDISSTATQRY: 1,,,IPV4\r\n", TRUE, TRUE, FALSE, FALSE },
{ "^NDISSTATQRY: 0,,,IPV4\r\n", TRUE, FALSE, FALSE, FALSE },
{ "^NDISSTATQRY: 1,,,IPV6\r\n", FALSE, FALSE, TRUE, TRUE },
{ "^NDISSTATQRY: 0,,,IPV6\r\n", FALSE, FALSE, TRUE, FALSE },
{ "^NDISSTATQRY: 1,,,IPV4\r\n"
"^NDISSTATQRY: 1,,,IPV6\r\n", TRUE, TRUE, TRUE, TRUE },
{ "^NDISSTATQRY: 1,,,IPV4\r\n"
"^NDISSTATQRY: 0,,,IPV6\r\n", TRUE, TRUE, TRUE, FALSE },
{ "^NDISSTATQRY: 0,,,IPV4\r\n"
"^NDISSTATQRY: 1,,,IPV6\r\n", TRUE, FALSE, TRUE, TRUE },
{ "^NDISSTATQRY: 0,,,IPV4\r\n"
"^NDISSTATQRY: 0,,,IPV6\r\n", TRUE, FALSE, TRUE, FALSE },
{ "^NDISSTATQRY: 1,,,IPV4", TRUE, TRUE, FALSE, FALSE },
{ "^NDISSTATQRY: 0,,,IPV4", TRUE, FALSE, FALSE, FALSE },
{ "^NDISSTATQRY: 1,,,IPV6", FALSE, FALSE, TRUE, TRUE },
{ "^NDISSTATQRY: 0,,,IPV6", FALSE, FALSE, TRUE, FALSE },
{ "^NDISSTATQRY: 1,,,IPV4\r\n"
"^NDISSTATQRY: 1,,,IPV6", TRUE, TRUE, TRUE, TRUE },
{ "^NDISSTATQRY: 1,,,IPV4\r\n"
"^NDISSTATQRY: 0,,,IPV6", TRUE, TRUE, TRUE, FALSE },
{ "^NDISSTATQRY: 0,,,IPV4\r\n"
"^NDISSTATQRY: 1,,,IPV6", TRUE, FALSE, TRUE, TRUE },
{ "^NDISSTATQRY: 0,,,IPV4\r\n"
"^NDISSTATQRY: 0,,,IPV6", TRUE, FALSE, TRUE, FALSE },
{ NULL, FALSE, FALSE, FALSE, FALSE }
};
static void
test_ndisstatqry (void)
{
guint i;
for (i = 0; ndisstatqry_tests[i].str; i++) {
GError *error = NULL;
gboolean ipv4_available;
gboolean ipv4_connected;
gboolean ipv6_available;
gboolean ipv6_connected;
g_assert (mm_huawei_parse_ndisstatqry_response (
ndisstatqry_tests[i].str,
&ipv4_available,
&ipv4_connected,
&ipv6_available,
&ipv6_connected,
&error) == TRUE);
g_assert_no_error (error);
g_assert (ipv4_available == ndisstatqry_tests[i].expected_ipv4_available);
if (ipv4_available)
g_assert (ipv4_connected == ndisstatqry_tests[i].expected_ipv4_connected);
g_assert (ipv6_available == ndisstatqry_tests[i].expected_ipv6_available);
if (ipv6_available)
g_assert (ipv6_connected == ndisstatqry_tests[i].expected_ipv6_connected);
}
}
/*****************************************************************************/
int main (int argc, char **argv)
{
setlocale (LC_ALL, "");
g_type_init ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/MM/huawei/ndisstatqry", test_ndisstatqry);
return g_test_run ();
}