diff options
author | Aleksander Morgado <aleksander@lanedo.com> | 2013-11-10 21:14:00 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@lanedo.com> | 2013-12-09 23:07:19 +0100 |
commit | 4e5c35a69fd8e9aab7c7b2b3f7d14298487c2de6 (patch) | |
tree | 8922a365189b3370ea63ae57a8f6db801df9e54c | |
parent | 8cc3eff661ea8cfa61c2c7d5960e82d0c411e8e1 (diff) |
huawei: new ^PREFMODE? response parser
-rw-r--r-- | plugins/huawei/mm-modem-helpers-huawei.c | 46 | ||||
-rw-r--r-- | plugins/huawei/mm-modem-helpers-huawei.h | 7 | ||||
-rw-r--r-- | plugins/huawei/tests/test-modem-helpers-huawei.c | 56 |
3 files changed, 109 insertions, 0 deletions
diff --git a/plugins/huawei/mm-modem-helpers-huawei.c b/plugins/huawei/mm-modem-helpers-huawei.c index 0957d6a5..91866e6d 100644 --- a/plugins/huawei/mm-modem-helpers-huawei.c +++ b/plugins/huawei/mm-modem-helpers-huawei.c @@ -367,6 +367,52 @@ mm_huawei_parse_prefmode_test (const gchar *response, } /*****************************************************************************/ +/* ^PREFMODE response parser */ + +const MMHuaweiPrefmodeCombination * +mm_huawei_parse_prefmode_response (const gchar *response, + const GArray *supported_mode_combinations, + GError **error) +{ + gint mode; + guint i; + + /* Format: + * + * ^PREFMODE: <mode> + */ + + response = mm_strip_tag (response, "^PREFMODE:"); + if (!sscanf (response, "%d", &mode)) { + /* Dump error to upper layer */ + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Unexpected PREFMODE response: '%s'", + response); + return NULL; + } + + /* Look for current modes among the supported ones */ + for (i = 0; i < supported_mode_combinations->len; i++) { + const MMHuaweiPrefmodeCombination *combination; + + combination = &g_array_index (supported_mode_combinations, + MMHuaweiPrefmodeCombination, + i); + if (mode == combination->prefmode) + return combination; + } + + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "No PREFMODE combination found matching the current one (%d)", + mode); + return NULL; +} + +/*****************************************************************************/ /* ^SYSCFG test parser */ static gchar ** diff --git a/plugins/huawei/mm-modem-helpers-huawei.h b/plugins/huawei/mm-modem-helpers-huawei.h index 687c82d6..1c1f1be1 100644 --- a/plugins/huawei/mm-modem-helpers-huawei.h +++ b/plugins/huawei/mm-modem-helpers-huawei.h @@ -64,6 +64,13 @@ GArray *mm_huawei_parse_prefmode_test (const gchar *response, GError **error); /*****************************************************************************/ +/* ^PREFMODE response parser */ + +const MMHuaweiPrefmodeCombination *mm_huawei_parse_prefmode_response (const gchar *response, + const GArray *supported_mode_combinations, + GError **error); + +/*****************************************************************************/ /* ^SYSCFG test parser */ typedef struct { diff --git a/plugins/huawei/tests/test-modem-helpers-huawei.c b/plugins/huawei/tests/test-modem-helpers-huawei.c index b1105605..fd35ec28 100644 --- a/plugins/huawei/tests/test-modem-helpers-huawei.c +++ b/plugins/huawei/tests/test-modem-helpers-huawei.c @@ -374,6 +374,61 @@ test_prefmode (void) } /*****************************************************************************/ +/* Test ^PREFMODE? responses */ + +typedef struct { + const gchar *str; + const gchar *format; + MMModemMode allowed; + MMModemMode preferred; +} PrefmodeResponseTest; + +static const PrefmodeResponseTest prefmode_response_tests[] = { + { + .str = "^PREFMODE:2\r\n", + .format = "^PREFMODE:(2,4,8)\r\n", + .allowed = (MM_MODEM_MODE_3G | MM_MODEM_MODE_2G), + .preferred = MM_MODEM_MODE_2G + }, + { + .str = "^PREFMODE:4\r\n", + .format = "^PREFMODE:(2,4,8)\r\n", + .allowed = (MM_MODEM_MODE_3G | MM_MODEM_MODE_2G), + .preferred = MM_MODEM_MODE_3G + }, + { + .str = "^PREFMODE:8\r\n", + .format = "^PREFMODE:(2,4,8)\r\n", + .allowed = (MM_MODEM_MODE_3G | MM_MODEM_MODE_2G), + .preferred = MM_MODEM_MODE_NONE + } +}; + +static void +test_prefmode_response (void) +{ + guint i; + + for (i = 0; i < G_N_ELEMENTS (prefmode_response_tests); i++) { + GArray *combinations = NULL; + const MMHuaweiPrefmodeCombination *found; + GError *error = NULL; + + combinations = mm_huawei_parse_prefmode_test (prefmode_response_tests[i].format, NULL); + g_assert (combinations != NULL); + + found = mm_huawei_parse_prefmode_response (prefmode_response_tests[i].str, + combinations, + &error); + g_assert (found != NULL); + g_assert_cmpuint (found->allowed, ==, prefmode_response_tests[i].allowed); + g_assert_cmpuint (found->preferred, ==, prefmode_response_tests[i].preferred); + + g_array_unref (combinations); + } +} + +/*****************************************************************************/ /* Test ^SYSCFG=? responses */ #define MAX_SYSCFG_COMBINATIONS 5 @@ -739,6 +794,7 @@ int main (int argc, char **argv) g_test_add_func ("/MM/huawei/sysinfo", test_sysinfo); g_test_add_func ("/MM/huawei/sysinfoex", test_sysinfoex); g_test_add_func ("/MM/huawei/prefmode", test_prefmode); + g_test_add_func ("/MM/huawei/prefmode/response", test_prefmode_response); g_test_add_func ("/MM/huawei/syscfg", test_syscfg); g_test_add_func ("/MM/huawei/syscfgex", test_syscfgex); |