huawei: new ^GETPORTMODE response parser

This commit is contained in:
Aleksander Morgado
2020-08-12 09:47:17 +02:00
parent b2621b4336
commit 4fdf28bb56
5 changed files with 311 additions and 2 deletions

2
.gitignore vendored
View File

@@ -165,8 +165,8 @@ Makefile.in
/plugins/test-service-* /plugins/test-service-*
/plugins/ublox/mm-ublox-enums-types.[ch] /plugins/ublox/mm-ublox-enums-types.[ch]
/plugins/telit/mm-telit-enums-types.[ch] /plugins/telit/mm-telit-enums-types.[ch]
/plugins/huawei/mm-huawei-enums-types.[ch]
/test/lsudev /test/lsudev
/test/mmtty /test/mmtty

View File

@@ -777,6 +777,35 @@ endif
if ENABLE_PLUGIN_HUAWEI if ENABLE_PLUGIN_HUAWEI
HUAWEI_ENUMS_INPUTS = \
$(top_srcdir)/plugins/huawei/mm-modem-helpers-huawei.h \
$(NULL)
HUAWEI_ENUMS_GENERATED = \
huawei/mm-huawei-enums-types.h \
huawei/mm-huawei-enums-types.c \
$(NULL)
BUILT_SOURCES += $(HUAWEI_ENUMS_GENERATED)
CLEANFILES += $(HUAWEI_ENUMS_GENERATED)
huawei/mm-huawei-enums-types.h: Makefile.am $(HUAWEI_ENUMS_INPUTS) $(top_srcdir)/build-aux/mm-enums-template.h
$(AM_V_GEN) \
$(MKDIR_P) huawei; \
$(GLIB_MKENUMS) \
--fhead "#include \"mm-modem-helpers-huawei.h\"\n#ifndef __MM_HUAWEI_ENUMS_TYPES_H__\n#define __MM_HUAWEI_ENUMS_TYPES_H__\n" \
--template $(top_srcdir)/build-aux/mm-enums-template.h \
--ftail "#endif /* __MM_HUAWEI_ENUMS_TYPES_H__ */\n" \
$(HUAWEI_ENUMS_INPUTS) > $@
huawei/mm-huawei-enums-types.c: Makefile.am $(top_srcdir)/build-aux/mm-enums-template.c huawei/mm-huawei-enums-types.h
$(AM_V_GEN) \
$(MKDIR_P) huawei; \
$(GLIB_MKENUMS) \
--fhead "#include \"mm-huawei-enums-types.h\"" \
--template $(top_srcdir)/build-aux/mm-enums-template.c \
$(HUAWEI_ENUMS_INPUTS) > $@
noinst_LTLIBRARIES += libhelpers-huawei.la noinst_LTLIBRARIES += libhelpers-huawei.la
libhelpers_huawei_la_SOURCES = \ libhelpers_huawei_la_SOURCES = \
huawei/mm-modem-helpers-huawei.c \ huawei/mm-modem-helpers-huawei.c \
@@ -784,7 +813,10 @@ libhelpers_huawei_la_SOURCES = \
$(NULL) $(NULL)
libhelpers_huawei_la_CPPFLAGS = \ libhelpers_huawei_la_CPPFLAGS = \
-DMM_MODULE_NAME=\"huawei\" \ -DMM_MODULE_NAME=\"huawei\" \
-I$(top_srcdir)/plugins/huawei \
-I$(top_builddir)/plugins/huawei \
$(NULL) $(NULL)
nodist_libhelpers_huawei_la_SOURCES = $(HUAWEI_ENUMS_GENERATED)
noinst_PROGRAMS += test-modem-helpers-huawei noinst_PROGRAMS += test-modem-helpers-huawei
test_modem_helpers_huawei_SOURCES = \ test_modem_helpers_huawei_SOURCES = \
@@ -792,6 +824,7 @@ test_modem_helpers_huawei_SOURCES = \
$(NULL) $(NULL)
test_modem_helpers_huawei_CPPFLAGS = \ test_modem_helpers_huawei_CPPFLAGS = \
-I$(top_srcdir)/plugins/huawei \ -I$(top_srcdir)/plugins/huawei \
-I$(top_builddir)/plugins/huawei \
$(NULL) $(NULL)
test_modem_helpers_huawei_LDADD = \ test_modem_helpers_huawei_LDADD = \
$(builddir)/libhelpers-huawei.la \ $(builddir)/libhelpers-huawei.la \
@@ -811,6 +844,8 @@ libmm_plugin_huawei_la_SOURCES = \
huawei/mm-broadband-bearer-huawei.h \ huawei/mm-broadband-bearer-huawei.h \
$(NULL) $(NULL)
libmm_plugin_huawei_la_CPPFLAGS = \ libmm_plugin_huawei_la_CPPFLAGS = \
-I$(top_srcdir)/plugins/huawei \
-I$(top_builddir)/plugins/huawei \
$(PLUGIN_COMMON_COMPILER_FLAGS) \ $(PLUGIN_COMMON_COMPILER_FLAGS) \
-DMM_MODULE_NAME=\"huawei\" \ -DMM_MODULE_NAME=\"huawei\" \
$(NULL) $(NULL)

View File

@@ -25,6 +25,7 @@
#include "mm-log-object.h" #include "mm-log-object.h"
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"
#include "mm-modem-helpers-huawei.h" #include "mm-modem-helpers-huawei.h"
#include "mm-huawei-enums-types.h"
/*****************************************************************************/ /*****************************************************************************/
/* ^NDISSTAT / ^NDISSTATQRY response parser */ /* ^NDISSTAT / ^NDISSTATQRY response parser */
@@ -1467,3 +1468,100 @@ mm_huawei_parse_cvoice_response (const gchar *response,
return ret; return ret;
} }
/*****************************************************************************/
/* ^GETPORTMODE response parser */
#define GETPORTMODE_PREFIX "^GETPORTMODE:"
GArray *
mm_huawei_parse_getportmode_response (const gchar *response,
gpointer log_object,
GError **error)
{
g_autoptr(GArray) modes = NULL;
g_auto(GStrv) split = NULL;
guint i;
gint n_items;
split = g_strsplit (response, ",", -1);
n_items = g_strv_length (split) - 1;
if (n_items < 1) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Unexpected number of items in response");
return NULL;
}
/* validate response prefix */
if (g_ascii_strncasecmp (split[0], GETPORTMODE_PREFIX, strlen (GETPORTMODE_PREFIX)) != 0) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Unexpected response prefix");
return NULL;
}
mm_obj_dbg (log_object, "processing ^GETPORTMODE response...");
modes = g_array_sized_new (FALSE, FALSE, sizeof (MMHuaweiPortMode), n_items);
/* iterate all port items found */
for (i = 1; split[i]; i++) {
MMHuaweiPortMode mode = MM_HUAWEI_PORT_MODE_NONE;
gchar *separator;
guint port_number;
separator = strchr (split[i], ':');
if (!separator)
continue;
/* the reported port number may start either by 0 or by 1; the important
* thing is therefore no the number itself, only that it's a number */
g_strstrip (&separator[1]);
if (!mm_get_uint_from_str (&separator[1], &port_number)) {
mm_obj_warn (log_object, " couldn't parse port number: %s", split[i]);
break;
}
*separator = '\0';
g_strstrip (split[i]);
if (g_ascii_strcasecmp (split[i], "pcui") == 0)
mode = MM_HUAWEI_PORT_MODE_PCUI;
else if ((g_ascii_strcasecmp (split[i], "mdm") == 0) ||
(g_ascii_strcasecmp (split[i], "modem") == 0) ||
(g_ascii_strcasecmp (split[i], "3g_modem") == 0))
mode = MM_HUAWEI_PORT_MODE_MODEM;
else if ((g_ascii_strcasecmp (split[i], "diag") == 0) ||
(g_ascii_strcasecmp (split[i], "3g_diag") == 0) ||
(g_ascii_strcasecmp (split[i], "4g_diag") == 0))
mode = MM_HUAWEI_PORT_MODE_DIAG;
else if (g_ascii_strcasecmp (split[i], "gps") == 0)
mode = MM_HUAWEI_PORT_MODE_GPS;
else if ((g_ascii_strcasecmp (split[i], "ndis") == 0) ||
(g_ascii_strcasecmp (split[i], "rndis") == 0) ||
(g_ascii_strcasecmp (split[i], "ncm") == 0) ||
(g_ascii_strcasecmp (split[i], "ecm") == 0))
mode = MM_HUAWEI_PORT_MODE_NET;
else if (g_ascii_strcasecmp (split[i], "cdrom") == 0)
mode = MM_HUAWEI_PORT_MODE_CDROM;
else if ((g_ascii_strcasecmp (split[i], "sd") == 0) ||
(g_ascii_strncasecmp (split[i], "mass", 4) == 0))
mode = MM_HUAWEI_PORT_MODE_SD;
else if (g_ascii_strcasecmp (split[i], "bt") == 0)
mode = MM_HUAWEI_PORT_MODE_BT;
else if ((g_ascii_strcasecmp (split[i], "a_shell") == 0) ||
(g_ascii_strcasecmp (split[i], "c_shell") == 0))
mode = MM_HUAWEI_PORT_MODE_SHELL;
mm_obj_dbg (log_object, " port mode %s reported at port number %u",
mm_huawei_port_mode_get_string (mode), port_number);
g_array_append_val (modes, mode);
}
if (!modes->len) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"No port modes loaded");
return NULL;
}
return g_steal_pointer (&modes);
}

View File

@@ -17,7 +17,9 @@
#ifndef MM_MODEM_HELPERS_HUAWEI_H #ifndef MM_MODEM_HELPERS_HUAWEI_H
#define MM_MODEM_HELPERS_HUAWEI_H #define MM_MODEM_HELPERS_HUAWEI_H
#include "glib.h" #include <ModemManager.h>
#define _LIBMM_INSIDE_MM
#include <libmm-glib.h>
/*****************************************************************************/ /*****************************************************************************/
/* ^NDISSTAT / ^NDISSTATQRY response parser */ /* ^NDISSTAT / ^NDISSTATQRY response parser */
@@ -161,4 +163,31 @@ gboolean mm_huawei_parse_cvoice_response (const gchar *response,
guint *bits, guint *bits,
GError **error); GError **error);
/*****************************************************************************/
/* ^GETPORTMODE response parser */
typedef enum { /*< underscore_name=mm_huawei_port_mode >*/
MM_HUAWEI_PORT_MODE_NONE,
MM_HUAWEI_PORT_MODE_PCUI,
MM_HUAWEI_PORT_MODE_MODEM,
MM_HUAWEI_PORT_MODE_DIAG,
MM_HUAWEI_PORT_MODE_GPS,
MM_HUAWEI_PORT_MODE_NET,
MM_HUAWEI_PORT_MODE_CDROM,
MM_HUAWEI_PORT_MODE_SD,
MM_HUAWEI_PORT_MODE_BT,
MM_HUAWEI_PORT_MODE_SHELL,
} MMHuaweiPortMode;
#define MM_HUAWEI_PORT_MODE_IS_SERIAL(mode) \
(mode == MM_HUAWEI_PORT_MODE_PCUI || \
mode == MM_HUAWEI_PORT_MODE_MODEM || \
mode == MM_HUAWEI_PORT_MODE_DIAG || \
mode == MM_HUAWEI_PORT_MODE_GPS || \
mode == MM_HUAWEI_PORT_MODE_SHELL)
GArray *mm_huawei_parse_getportmode_response (const gchar *response,
gpointer log_object,
GError **error);
#endif /* MM_MODEM_HELPERS_HUAWEI_H */ #endif /* MM_MODEM_HELPERS_HUAWEI_H */

View File

@@ -1249,6 +1249,152 @@ test_hcsq (void)
} }
} }
/*****************************************************************************/
/* Test ^GETPORTMODE response */
typedef struct {
const gchar *str;
guint n_modes;
MMHuaweiPortMode modes[8];
} GetportmodeTest;
static const GetportmodeTest getportmode_tests[] = {
{
"^GETPORTMODE: TYPE: WCDMA: huawei,PCUI:0,MDM:1",
2, { MM_HUAWEI_PORT_MODE_PCUI,
MM_HUAWEI_PORT_MODE_MODEM }
},
{
"^GETPORTMODE: TYPE: WCDMA: huawei,MDM:0,PCUI:1,NDIS:2,CDROM:3,SD:4,",
5, { MM_HUAWEI_PORT_MODE_MODEM,
MM_HUAWEI_PORT_MODE_PCUI,
MM_HUAWEI_PORT_MODE_NET,
MM_HUAWEI_PORT_MODE_CDROM,
MM_HUAWEI_PORT_MODE_SD }
},
{
"^GETPORTMODE: TYPE: WCDMA: huawei,MDM:0,PCUI:1,NDIS:2,GPS:3,BT:4,",
5, { MM_HUAWEI_PORT_MODE_MODEM,
MM_HUAWEI_PORT_MODE_PCUI,
MM_HUAWEI_PORT_MODE_NET,
MM_HUAWEI_PORT_MODE_GPS,
MM_HUAWEI_PORT_MODE_BT
}
},
{
"^GETPORTMODE: TYPE: WCDMA: huawei,PCUI:0,MDM:1,NDIS:2,CDROM:3,SD:4,GPS:5,BT:6",
7, { MM_HUAWEI_PORT_MODE_PCUI,
MM_HUAWEI_PORT_MODE_MODEM,
MM_HUAWEI_PORT_MODE_NET,
MM_HUAWEI_PORT_MODE_CDROM,
MM_HUAWEI_PORT_MODE_SD,
MM_HUAWEI_PORT_MODE_GPS,
MM_HUAWEI_PORT_MODE_BT
}
},
{
"^getportmode:type:WCDMA:Qualcomm,NDIS:0,DIAG:1,PCUI:2,MDM:3,SD:4",
5, { MM_HUAWEI_PORT_MODE_NET,
MM_HUAWEI_PORT_MODE_DIAG,
MM_HUAWEI_PORT_MODE_PCUI,
MM_HUAWEI_PORT_MODE_MODEM,
MM_HUAWEI_PORT_MODE_SD
}
},
{
"^GETPORTMODE: TYPE: WCDMA: ,pcui:1,modem:2,ncm:3,mass:4,mass_two:5,",
5, { MM_HUAWEI_PORT_MODE_PCUI,
MM_HUAWEI_PORT_MODE_MODEM,
MM_HUAWEI_PORT_MODE_NET,
MM_HUAWEI_PORT_MODE_SD,
MM_HUAWEI_PORT_MODE_SD
}
},
{
"^GETPORTMODE: TYPE: WCDMA: huawei ,, rndis: 0, pcui: 1, c_shell: 2, a_shell: 3,3g_diag: 4, gps: 5, 4g_diag: 6, mass_two: 7",
8, { MM_HUAWEI_PORT_MODE_NET,
MM_HUAWEI_PORT_MODE_PCUI,
MM_HUAWEI_PORT_MODE_SHELL,
MM_HUAWEI_PORT_MODE_SHELL,
MM_HUAWEI_PORT_MODE_DIAG,
MM_HUAWEI_PORT_MODE_GPS,
MM_HUAWEI_PORT_MODE_DIAG,
MM_HUAWEI_PORT_MODE_SD
}
},
{
"^GETPORTMODE: TYPE: WCDMA: huawei,ecm:1,pcui:2,c_shell:3,a_shell:4,3g_diag:5,gps:6,4g_diag:7,mass:8,",
8, { MM_HUAWEI_PORT_MODE_NET,
MM_HUAWEI_PORT_MODE_PCUI,
MM_HUAWEI_PORT_MODE_SHELL,
MM_HUAWEI_PORT_MODE_SHELL,
MM_HUAWEI_PORT_MODE_DIAG,
MM_HUAWEI_PORT_MODE_GPS,
MM_HUAWEI_PORT_MODE_DIAG,
MM_HUAWEI_PORT_MODE_SD
}
},
{
"^GETPORTMODE: TYPE: WCDMA: huawei,rndis:1,pcui:2,c_shell:3,a_shell:4,3g_diag:5,gps:6,4g_diag:7,mass:8,",
8, { MM_HUAWEI_PORT_MODE_NET,
MM_HUAWEI_PORT_MODE_PCUI,
MM_HUAWEI_PORT_MODE_SHELL,
MM_HUAWEI_PORT_MODE_SHELL,
MM_HUAWEI_PORT_MODE_DIAG,
MM_HUAWEI_PORT_MODE_GPS,
MM_HUAWEI_PORT_MODE_DIAG,
MM_HUAWEI_PORT_MODE_SD
}
},
{
"^GETPORTMODE: TYPE: WCDMA: huawei,,pcui:0,3g_modem:1,ncm:2,mass:3,mass_two:4",
5, { MM_HUAWEI_PORT_MODE_PCUI,
MM_HUAWEI_PORT_MODE_MODEM,
MM_HUAWEI_PORT_MODE_NET,
MM_HUAWEI_PORT_MODE_SD,
MM_HUAWEI_PORT_MODE_SD
}
},
{
"^GETPORTMODE:TYPE:WCDMA:Qualcomm,MDM:0,NDIS:1,DIAG:2,PCUI:3,CDROM:4,SD:5",
6, { MM_HUAWEI_PORT_MODE_MODEM,
MM_HUAWEI_PORT_MODE_NET,
MM_HUAWEI_PORT_MODE_DIAG,
MM_HUAWEI_PORT_MODE_PCUI,
MM_HUAWEI_PORT_MODE_CDROM,
MM_HUAWEI_PORT_MODE_SD
}
},
{
"^GETPORTMODE: TYPE: WCDMA: Huawei Technologies Co.,Ltd.,",
0
},
};
static void
test_getportmode (void)
{
guint i;
for (i = 0; i < G_N_ELEMENTS (getportmode_tests); i++) {
g_autoptr(GArray) modes = NULL;
g_autoptr(GError) error = NULL;
mm_obj_dbg (NULL, "testing ^GETPORTMODE response: '%s'", getportmode_tests[i].str);
modes = mm_huawei_parse_getportmode_response (getportmode_tests[i].str, NULL, &error);
if (modes) {
guint j;
g_assert_no_error (error);
g_assert_cmpuint (modes->len, ==, getportmode_tests[i].n_modes);
for (j = 0; j < getportmode_tests[i].n_modes; j++)
g_assert_cmpuint (g_array_index (modes, MMHuaweiPortMode, j), ==, getportmode_tests[i].modes[j]);
} else
g_assert (error);
}
}
/*****************************************************************************/ /*****************************************************************************/
int main (int argc, char **argv) int main (int argc, char **argv)
@@ -1270,6 +1416,7 @@ int main (int argc, char **argv)
g_test_add_func ("/MM/huawei/nwtime", test_nwtime); g_test_add_func ("/MM/huawei/nwtime", test_nwtime);
g_test_add_func ("/MM/huawei/time", test_time); g_test_add_func ("/MM/huawei/time", test_time);
g_test_add_func ("/MM/huawei/hcsq", test_hcsq); g_test_add_func ("/MM/huawei/hcsq", test_hcsq);
g_test_add_func ("/MM/huawei/getportmode", test_getportmode);
return g_test_run (); return g_test_run ();
} }