ublox: new +UBANDSEL=X command builder

This commit is contained in:
Aleksander Morgado
2016-08-12 18:03:32 +02:00
parent c165f24514
commit 1c69a847ae
3 changed files with 120 additions and 0 deletions

View File

@@ -731,6 +731,61 @@ out:
return array; return array;
} }
/*****************************************************************************/
/* UBANDSEL=X command builder */
static gint
ubandsel_num_cmp (const guint *a, const guint *b)
{
return (*a - *b);
}
gchar *
mm_ublox_build_ubandsel_set_command (GArray *bands,
GError **error)
{
GString *command = NULL;
GArray *ubandsel_nums;
guint i;
if (bands->len == 1 && g_array_index (bands, MMModemBand, 0) == MM_MODEM_BAND_ANY)
return g_strdup ("+UBANDSEL=0");
ubandsel_nums = g_array_sized_new (FALSE, FALSE, sizeof (guint), G_N_ELEMENTS (band_configuration));
for (i = 0; i < G_N_ELEMENTS (band_configuration); i++) {
guint j;
for (j = 0; j < bands->len; j++) {
MMModemBand band;
band = g_array_index (bands, MMModemBand, j);
if (band == band_configuration[i].bands_2g[0] || band == band_configuration[i].bands_2g[1] ||
band == band_configuration[i].bands_3g[0] || band == band_configuration[i].bands_3g[1] ||
band == band_configuration[i].bands_4g[0] || band == band_configuration[i].bands_4g[1]) {
g_array_append_val (ubandsel_nums, band_configuration[i].ubandsel_value);
break;
}
}
}
if (ubandsel_nums->len == 0) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
"Given band combination is unsupported");
g_array_unref (ubandsel_nums);
return NULL;
}
if (ubandsel_nums->len > 1)
g_array_sort (ubandsel_nums, (GCompareFunc) ubandsel_num_cmp);
/* Build command */
command = g_string_new ("+UBANDSEL=");
for (i = 0; i < ubandsel_nums->len; i++)
g_string_append_printf (command, "%s%u", i == 0 ? "" : ",", g_array_index (ubandsel_nums, guint, i));
return g_string_free (command, FALSE);
}
/*****************************************************************************/ /*****************************************************************************/
/* Get mode to apply when ANY */ /* Get mode to apply when ANY */

View File

@@ -100,6 +100,12 @@ GArray *mm_ublox_get_supported_bands (const gchar *model,
GArray *mm_ublox_parse_ubandsel_response (const gchar *response, GArray *mm_ublox_parse_ubandsel_response (const gchar *response,
GError **error); GError **error);
/*****************************************************************************/
/* UBANDSEL=X command builder */
gchar *mm_ublox_build_ubandsel_set_command (GArray *bands,
GError **error);
/*****************************************************************************/ /*****************************************************************************/
/* Get mode to apply when ANY */ /* Get mode to apply when ANY */

View File

@@ -669,6 +669,61 @@ test_ubandsel_response_one (void)
common_validate_ubandsel_response ("+UBANDSEL: 850\r\n", expected_bands, G_N_ELEMENTS (expected_bands)); common_validate_ubandsel_response ("+UBANDSEL: 850\r\n", expected_bands, G_N_ELEMENTS (expected_bands));
} }
/*****************************************************************************/
/* +UBANDSEL=x command builder */
static void
common_validate_ubandsel_request (const MMModemBand *bands,
guint n_bands,
const gchar *expected_request)
{
GError *error = NULL;
GArray *bands_array;
gchar *request;
bands_array = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), n_bands);
g_array_append_vals (bands_array, bands, n_bands);
request = mm_ublox_build_ubandsel_set_command (bands_array, &error);
g_assert_no_error (error);
g_assert (request);
g_assert_cmpstr (request, ==, expected_request);
g_array_unref (bands_array);
g_free (request);
}
static void
test_ubandsel_request_any (void)
{
const MMModemBand bands[] = {
MM_MODEM_BAND_ANY
};
common_validate_ubandsel_request (bands, G_N_ELEMENTS (bands), "+UBANDSEL=0");
}
static void
test_ubandsel_request_2g (void)
{
const MMModemBand bands[] = {
MM_MODEM_BAND_G850, MM_MODEM_BAND_EGSM, MM_MODEM_BAND_DCS, MM_MODEM_BAND_PCS
};
common_validate_ubandsel_request (bands, G_N_ELEMENTS (bands), "+UBANDSEL=850,900,1800,1900");
}
static void
test_ubandsel_request_1800 (void)
{
const MMModemBand bands[] = {
MM_MODEM_BAND_DCS, MM_MODEM_BAND_U1800, MM_MODEM_BAND_EUTRAN_III
};
common_validate_ubandsel_request (bands, G_N_ELEMENTS (bands), "+UBANDSEL=1800");
}
/*****************************************************************************/ /*****************************************************************************/
void void
@@ -721,5 +776,9 @@ int main (int argc, char **argv)
g_test_add_func ("/MM/ublox/ubandsel/response/three", test_ubandsel_response_three); g_test_add_func ("/MM/ublox/ubandsel/response/three", test_ubandsel_response_three);
g_test_add_func ("/MM/ublox/ubandsel/response/four", test_ubandsel_response_four); g_test_add_func ("/MM/ublox/ubandsel/response/four", test_ubandsel_response_four);
g_test_add_func ("/MM/ublox/ubandsel/request/any", test_ubandsel_request_any);
g_test_add_func ("/MM/ublox/ubandsel/request/2g", test_ubandsel_request_2g);
g_test_add_func ("/MM/ublox/ubandsel/request/1800", test_ubandsel_request_1800);
return g_test_run (); return g_test_run ();
} }