linktop: new Linktop specific +CFUN? response parser

We handle all known CFUN? response values in the new parser, and report an error
if an unknown value is found.
This commit is contained in:
Aleksander Morgado
2016-10-11 17:49:43 +02:00
parent 74fd7da774
commit d7fdda2247
6 changed files with 214 additions and 38 deletions

1
.gitignore vendored
View File

@@ -168,6 +168,7 @@ Makefile.in
/plugins/test-modem-helpers-telit* /plugins/test-modem-helpers-telit*
/plugins/test-modem-helpers-thuraya* /plugins/test-modem-helpers-thuraya*
/plugins/test-modem-helpers-ublox* /plugins/test-modem-helpers-ublox*
/plugins/test-modem-helpers-linktop*
/plugins/test-service-* /plugins/test-service-*
/plugins/ublox/mm-ublox-enums-types.[ch] /plugins/ublox/mm-ublox-enums-types.[ch]

View File

@@ -518,6 +518,25 @@ libmm_plugin_anydata_la_LDFLAGS = $(PLUGIN_COMMON_LINKER_FLAGS)
# plugin: linktop cdma # plugin: linktop cdma
################################################################################ ################################################################################
noinst_LTLIBRARIES += libhelpers-linktop.la
libhelpers_linktop_la_SOURCES = \
linktop/mm-modem-helpers-linktop.c \
linktop/mm-modem-helpers-linktop.h \
$(NULL)
noinst_PROGRAMS += test-modem-helpers-linktop
test_modem_helpers_linktop_SOURCES = \
linktop/tests/test-modem-helpers-linktop.c \
$(NULL)
test_modem_helpers_linktop_CPPFLAGS = \
-I$(top_srcdir)/plugins/linktop \
$(NULL)
test_modem_helpers_linktop_LDADD = \
$(builddir)/libhelpers-linktop.la \
$(top_builddir)/src/libhelpers.la \
$(top_builddir)/libmm-glib/libmm-glib.la \
$(NULL)
pkglib_LTLIBRARIES += libmm-plugin-linktop.la pkglib_LTLIBRARIES += libmm-plugin-linktop.la
libmm_plugin_linktop_la_SOURCES = \ libmm_plugin_linktop_la_SOURCES = \
linktop/mm-plugin-linktop.c \ linktop/mm-plugin-linktop.c \
@@ -527,6 +546,7 @@ libmm_plugin_linktop_la_SOURCES = \
$(NULL) $(NULL)
libmm_plugin_linktop_la_CPPFLAGS = $(PLUGIN_COMMON_COMPILER_FLAGS) libmm_plugin_linktop_la_CPPFLAGS = $(PLUGIN_COMMON_COMPILER_FLAGS)
libmm_plugin_linktop_la_LDFLAGS = $(PLUGIN_COMMON_LINKER_FLAGS) libmm_plugin_linktop_la_LDFLAGS = $(PLUGIN_COMMON_LINKER_FLAGS)
libmm_plugin_linktop_la_LIBADD = $(builddir)/libhelpers-linktop.la
################################################################################ ################################################################################
# plugin: simtech # plugin: simtech

View File

@@ -33,10 +33,7 @@
#include "mm-iface-modem.h" #include "mm-iface-modem.h"
#include "mm-base-modem-at.h" #include "mm-base-modem-at.h"
#include "mm-broadband-modem-linktop.h" #include "mm-broadband-modem-linktop.h"
#include "mm-modem-helpers-linktop.h"
#define LINKTOP_MODE_ANY 1
#define LINKTOP_MODE_2G 5
#define LINKTOP_MODE_3G 6
static void iface_modem_init (MMIfaceModem *iface); static void iface_modem_init (MMIfaceModem *iface);
@@ -130,44 +127,13 @@ load_current_modes_finish (MMIfaceModem *self,
GError **error) GError **error)
{ {
const gchar *response; const gchar *response;
const gchar *str;
guint aux;
response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, error); response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, error);
if (!response) if (!response || !mm_linktop_parse_cfun_query_current_modes (response, allowed, error))
return FALSE; return FALSE;
str = mm_strip_tag (response, "CFUN:"); /* None preferred always */
if (!mm_get_uint_from_str (str, &aux)) {
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't parse CFUN? response: '%s'",
response);
return FALSE;
}
switch (aux) {
case LINKTOP_MODE_2G:
*allowed = MM_MODEM_MODE_2G;
*preferred = MM_MODEM_MODE_NONE; *preferred = MM_MODEM_MODE_NONE;
break;
case LINKTOP_MODE_3G:
*allowed = MM_MODEM_MODE_3G;
*preferred = MM_MODEM_MODE_NONE;
break;
case LINKTOP_MODE_ANY:
*allowed = (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G);
*preferred = MM_MODEM_MODE_NONE;
break;
default:
*allowed = MM_MODEM_MODE_ANY;
*preferred = MM_MODEM_MODE_NONE;
break;
}
return TRUE; return TRUE;
} }

View File

@@ -0,0 +1,55 @@
/* -*- 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) 2008 - 2009 Novell, Inc.
* Copyright (C) 2009 - 2016 Red Hat, Inc.
* Copyright (C) 2016 Aleksander Morgado <aleksander@aleksander.es>
*/
#include "mm-log.h"
#include "mm-modem-helpers.h"
#include "mm-modem-helpers-linktop.h"
/*****************************************************************************/
gboolean
mm_linktop_parse_cfun_query_current_modes (const gchar *response,
MMModemMode *allowed,
GError **error)
{
guint state;
g_assert (allowed);
if (!mm_3gpp_parse_cfun_query_response (response, &state, error))
return FALSE;
switch (state) {
case LINKTOP_MODE_OFFLINE:
case LINKTOP_MODE_LOW_POWER:
*allowed = MM_MODEM_MODE_NONE;
return TRUE;
case LINKTOP_MODE_2G:
*allowed = MM_MODEM_MODE_2G;
return TRUE;
case LINKTOP_MODE_3G:
*allowed = MM_MODEM_MODE_3G;
return TRUE;
case LINKTOP_MODE_ANY:
*allowed = (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G);
return TRUE;
default:
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Unknown linktop +CFUN current mode: %u", state);
return FALSE;
}
}

View File

@@ -0,0 +1,40 @@
/* -*- 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) 2008 - 2009 Novell, Inc.
* Copyright (C) 2009 - 2016 Red Hat, Inc.
* Copyright (C) 2016 Aleksander Morgado <aleksander@aleksander.es>
*/
#ifndef MM_MODEM_HELPERS_LINKTOP_H
#define MM_MODEM_HELPERS_LINKTOP_H
#include <glib.h>
#include <ModemManager.h>
#define _LIBMM_INSIDE_MM
#include <libmm-glib.h>
typedef enum {
LINKTOP_MODE_OFFLINE = 0,
LINKTOP_MODE_ANY = 1,
LINKTOP_MODE_LOW_POWER = 4,
LINKTOP_MODE_2G = 5,
LINKTOP_MODE_3G = 6,
} MMLinktopMode;
/* AT+CFUN? response parsers */
gboolean mm_linktop_parse_cfun_query_current_modes (const gchar *response,
MMModemMode *allowed,
GError **error);
#endif /* MM_MODEM_HELPERS_LINKTOP_H */

View File

@@ -0,0 +1,94 @@
/* -*- 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) 2016 Aleksander Morgado <aleksander@aleksander.es>
*/
#include <glib.h>
#include <glib-object.h>
#include <locale.h>
#include <ModemManager.h>
#define _LIBMM_INSIDE_MM
#include <libmm-glib.h>
#include "mm-log.h"
#include "mm-modem-helpers.h"
#include "mm-modem-helpers-linktop.h"
/* #define ENABLE_TEST_MESSAGE_TRACES */
/*****************************************************************************/
typedef struct {
const gchar *str;
MMModemMode allowed;
} CfunQueryCurrentModeTest;
static const CfunQueryCurrentModeTest cfun_query_current_mode_tests[] = {
{ "+CFUN: 0", MM_MODEM_MODE_NONE },
{ "+CFUN: 1", MM_MODEM_MODE_2G | MM_MODEM_MODE_3G },
{ "+CFUN: 4", MM_MODEM_MODE_NONE },
{ "+CFUN: 5", MM_MODEM_MODE_2G },
{ "+CFUN: 6", MM_MODEM_MODE_3G },
};
static void
test_cfun_query_current_modes (void)
{
guint i;
for (i = 0; i < G_N_ELEMENTS (cfun_query_current_mode_tests); i++) {
GError *error = NULL;
gboolean success;
MMModemMode allowed = MM_MODEM_MODE_NONE;
success = mm_linktop_parse_cfun_query_current_modes (cfun_query_current_mode_tests[i].str, &allowed, &error);
g_assert_no_error (error);
g_assert (success);
g_assert_cmpuint (cfun_query_current_mode_tests[i].allowed, ==, allowed);
}
}
/*****************************************************************************/
void
_mm_log (const char *loc,
const char *func,
guint32 level,
const char *fmt,
...)
{
#if defined ENABLE_TEST_MESSAGE_TRACES
/* Dummy log function */
va_list args;
gchar *msg;
va_start (args, fmt);
msg = g_strdup_vprintf (fmt, args);
va_end (args);
g_print ("%s\n", msg);
g_free (msg);
#endif
}
int main (int argc, char **argv)
{
setlocale (LC_ALL, "");
g_type_init ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/MM/linktop/cfun/query/current-modes", test_cfun_query_current_modes);
return g_test_run ();
}