mbm: new MBM specific +CFUN? response parsers

This commit is contained in:
Aleksander Morgado
2016-10-12 11:29:52 +02:00
parent aca6bb1c02
commit 74fd7da774
4 changed files with 165 additions and 63 deletions
+15 -60
View File
@@ -275,45 +275,22 @@ load_current_modes_finish (MMIfaceModem *_self,
{
MMBroadbandModemMbm *self = MM_BROADBAND_MODEM_MBM (_self);
const gchar *response;
guint a;
gint mbm_mode = -1;
g_assert (allowed);
g_assert (preferred);
response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, error);
if (!response)
if (!response || !mm_mbm_parse_cfun_query_current_modes (response, allowed, &mbm_mode, error))
return FALSE;
if (mm_get_uint_from_str (mm_strip_tag (response, "+CFUN:"), &a)) {
/* No settings to set preferred */
*preferred = MM_MODEM_MODE_NONE;
/* No settings to set preferred */
*preferred = MM_MODEM_MODE_NONE;
switch (a) {
case MBM_NETWORK_MODE_OFFLINE:
case MBM_NETWORK_MODE_LOW_POWER:
/* Do not update internal mbm_mode */
*allowed = MM_MODEM_MODE_NONE;
break;
case MBM_NETWORK_MODE_2G:
self->priv->mbm_mode = MBM_NETWORK_MODE_2G;
*allowed = MM_MODEM_MODE_2G;
break;
case MBM_NETWORK_MODE_3G:
self->priv->mbm_mode = MBM_NETWORK_MODE_3G;
*allowed = MM_MODEM_MODE_3G;
break;
default:
/* Do not update internal mbm_mode */
*allowed = (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G);
break;
}
if (mbm_mode != -1)
self->priv->mbm_mode = mbm_mode;
return TRUE;
}
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't parse +CFUN response: '%s'",
response);
return FALSE;
return TRUE;
}
static void
@@ -615,36 +592,14 @@ load_power_state_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
const gchar *response;
guint a;
const gchar *response;
MMModemPowerState state;
response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, error);
if (!response)
return FALSE;
if (!response || !mm_mbm_parse_cfun_query_power_state (response, &state, error))
return MM_MODEM_POWER_STATE_UNKNOWN;
if (mm_get_uint_from_str (mm_strip_tag (response, "+CFUN:"), &a)) {
switch (a) {
case MBM_NETWORK_MODE_OFFLINE:
return MM_MODEM_POWER_STATE_OFF;
case MBM_NETWORK_MODE_LOW_POWER:
return MM_MODEM_POWER_STATE_LOW;
case MBM_NETWORK_MODE_ANY:
case MBM_NETWORK_MODE_2G:
case MBM_NETWORK_MODE_3G:
return MM_MODEM_POWER_STATE_ON;
default:
break;
}
}
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't parse +CFUN response: '%s'",
response);
return MM_MODEM_POWER_STATE_UNKNOWN;
return state;
}
static void
+71
View File
@@ -263,3 +263,74 @@ mm_mbm_parse_cfun_test (const gchar *response,
*supported_mask = mask;
return !!mask;
}
/*****************************************************************************/
/* AT+CFUN? response parsers */
gboolean
mm_mbm_parse_cfun_query_power_state (const gchar *response,
MMModemPowerState *out_state,
GError **error)
{
guint state;
if (!mm_3gpp_parse_cfun_query_response (response, &state, error))
return FALSE;
switch (state) {
case MBM_NETWORK_MODE_OFFLINE:
*out_state = MM_MODEM_POWER_STATE_OFF;
return TRUE;
case MBM_NETWORK_MODE_LOW_POWER:
*out_state = MM_MODEM_POWER_STATE_LOW;
return TRUE;
case MBM_NETWORK_MODE_ANY:
case MBM_NETWORK_MODE_2G:
case MBM_NETWORK_MODE_3G:
*out_state = MM_MODEM_POWER_STATE_ON;
return TRUE;
default:
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Unknown +CFUN pòwer state: '%u'", state);
return FALSE;
}
}
gboolean
mm_mbm_parse_cfun_query_current_modes (const gchar *response,
MMModemMode *allowed,
gint *mbm_mode,
GError **error)
{
guint state;
g_assert (mbm_mode);
g_assert (allowed);
if (!mm_3gpp_parse_cfun_query_response (response, &state, error))
return FALSE;
switch (state) {
case MBM_NETWORK_MODE_OFFLINE:
case MBM_NETWORK_MODE_LOW_POWER:
/* Do not update mbm_mode */
*allowed = MM_MODEM_MODE_NONE;
return TRUE;
case MBM_NETWORK_MODE_2G:
*mbm_mode = MBM_NETWORK_MODE_2G;
*allowed = MM_MODEM_MODE_2G;
return TRUE;
case MBM_NETWORK_MODE_3G:
*mbm_mode = MBM_NETWORK_MODE_3G;
*allowed = MM_MODEM_MODE_3G;
return TRUE;
case MBM_NETWORK_MODE_ANY:
/* Do not update mbm_mode */
*allowed = (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G);
return TRUE;
default:
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Unknown +CFUN current mode: '%u'", state);
return FALSE;
}
}
+9
View File
@@ -38,4 +38,13 @@ gboolean mm_mbm_parse_cfun_test (const gchar *response,
guint32 *supported_mask,
GError **error);
/* AT+CFUN? response parsers */
gboolean mm_mbm_parse_cfun_query_power_state (const gchar *response,
MMModemPowerState *out_state,
GError **error);
gboolean mm_mbm_parse_cfun_query_current_modes (const gchar *response,
MMModemMode *allowed,
gint *mbm_mode,
GError **error);
#endif /* MM_MODEM_HELPERS_MBM_H */
+70 -3
View File
@@ -172,7 +172,7 @@ static const CfunTest cfun_tests[] = {
};
static void
test_cfun (void)
test_cfun_test (void)
{
guint i;
@@ -190,6 +190,71 @@ test_cfun (void)
/*****************************************************************************/
typedef struct {
const gchar *str;
MMModemPowerState state;
} CfunQueryPowerStateTest;
static const CfunQueryPowerStateTest cfun_query_power_state_tests[] = {
{ "+CFUN: 0", MM_MODEM_POWER_STATE_OFF },
{ "+CFUN: 1", MM_MODEM_POWER_STATE_ON },
{ "+CFUN: 4", MM_MODEM_POWER_STATE_LOW },
{ "+CFUN: 5", MM_MODEM_POWER_STATE_ON },
{ "+CFUN: 6", MM_MODEM_POWER_STATE_ON },
};
static void
test_cfun_query_power_state (void)
{
guint i;
for (i = 0; i < G_N_ELEMENTS (cfun_query_power_state_tests); i++) {
GError *error = NULL;
gboolean success;
MMModemPowerState state;
success = mm_mbm_parse_cfun_query_power_state (cfun_query_power_state_tests[i].str, &state, &error);
g_assert_no_error (error);
g_assert (success);
g_assert_cmpuint (cfun_query_power_state_tests[i].state, ==, state);
}
}
typedef struct {
const gchar *str;
MMModemMode allowed;
gint mbm_mode;
} CfunQueryCurrentModeTest;
static const CfunQueryCurrentModeTest cfun_query_current_mode_tests[] = {
{ "+CFUN: 0", MM_MODEM_MODE_NONE, -1 },
{ "+CFUN: 1", MM_MODEM_MODE_2G | MM_MODEM_MODE_3G, -1 },
{ "+CFUN: 4", MM_MODEM_MODE_NONE, -1 },
{ "+CFUN: 5", MM_MODEM_MODE_2G, MBM_NETWORK_MODE_2G },
{ "+CFUN: 6", MM_MODEM_MODE_3G, MBM_NETWORK_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;
gint mbm_mode = -1;
success = mm_mbm_parse_cfun_query_current_modes (cfun_query_current_mode_tests[i].str, &allowed, &mbm_mode, &error);
g_assert_no_error (error);
g_assert (success);
g_assert_cmpuint (cfun_query_current_mode_tests[i].allowed, ==, allowed);
g_assert_cmpint (cfun_query_current_mode_tests[i].mbm_mode, ==, mbm_mode);
}
}
/*****************************************************************************/
void
_mm_log (const char *loc,
const char *func,
@@ -217,8 +282,10 @@ int main (int argc, char **argv)
g_type_init ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/MM/mbm/e2ipcfg", test_e2ipcfg);
g_test_add_func ("/MM/mbm/cfun", test_cfun);
g_test_add_func ("/MM/mbm/e2ipcfg", test_e2ipcfg);
g_test_add_func ("/MM/mbm/cfun/test", test_cfun_test);
g_test_add_func ("/MM/mbm/cfun/query/power-state", test_cfun_query_power_state);
g_test_add_func ("/MM/mbm/cfun/query/current-modes", test_cfun_query_current_modes);
return g_test_run ();
}