iface-modem: run checks when trying to set allowed bands
Allowed bands must be a subset of the supported bands.
This commit is contained in:
@@ -953,6 +953,25 @@ handle_factory_reset (MmGdbusModem *skeleton,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
/* ALLOWED BANDS */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
MMIfaceModem *self;
|
||||||
|
MmGdbusModem *skeleton;
|
||||||
|
GSimpleAsyncResult *result;
|
||||||
|
GArray *allowed_bands_array;
|
||||||
|
} SetAllowedBandsContext;
|
||||||
|
|
||||||
|
static void
|
||||||
|
set_allowed_bands_context_complete_and_free (SetAllowedBandsContext *ctx)
|
||||||
|
{
|
||||||
|
g_simple_async_result_complete_in_idle (ctx->result);
|
||||||
|
g_object_unref (ctx->result);
|
||||||
|
g_object_unref (ctx->self);
|
||||||
|
g_object_unref (ctx->skeleton);
|
||||||
|
g_array_unref (ctx->allowed_bands_array);
|
||||||
|
g_free (ctx);
|
||||||
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
mm_iface_modem_set_allowed_bands_finish (MMIfaceModem *self,
|
mm_iface_modem_set_allowed_bands_finish (MMIfaceModem *self,
|
||||||
@@ -965,31 +984,35 @@ mm_iface_modem_set_allowed_bands_finish (MMIfaceModem *self,
|
|||||||
static void
|
static void
|
||||||
set_allowed_bands_ready (MMIfaceModem *self,
|
set_allowed_bands_ready (MMIfaceModem *self,
|
||||||
GAsyncResult *res,
|
GAsyncResult *res,
|
||||||
GSimpleAsyncResult *simple)
|
SetAllowedBandsContext *ctx)
|
||||||
{
|
{
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
|
|
||||||
if (!MM_IFACE_MODEM_GET_INTERFACE (self)->set_allowed_bands_finish (self, res, &error))
|
if (!MM_IFACE_MODEM_GET_INTERFACE (self)->set_allowed_bands_finish (self, res, &error))
|
||||||
g_simple_async_result_take_error (simple, error);
|
g_simple_async_result_take_error (ctx->result, error);
|
||||||
else
|
else {
|
||||||
g_simple_async_result_set_op_res_gboolean (simple, TRUE);
|
mm_gdbus_modem_set_allowed_bands (ctx->skeleton,
|
||||||
g_simple_async_result_complete (simple);
|
mm_common_bands_garray_to_variant (ctx->allowed_bands_array));
|
||||||
g_object_unref (simple);
|
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
set_allowed_bands_context_complete_and_free (ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
validate_allowed_bands (GArray *bands_array,
|
validate_allowed_bands (const GArray *supported_bands_array,
|
||||||
|
const GArray *allowed_bands_array,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
/* When the array has more than one element, there MUST NOT include ANY or
|
/* When the array has more than one element, there MUST NOT include ANY or
|
||||||
* UNKNOWN */
|
* UNKNOWN */
|
||||||
if (bands_array->len > 1) {
|
if (allowed_bands_array->len > 1) {
|
||||||
guint i;
|
guint i;
|
||||||
|
|
||||||
for (i = 0; i < bands_array->len; i++) {
|
for (i = 0; i < allowed_bands_array->len; i++) {
|
||||||
MMModemBand band;
|
MMModemBand band;
|
||||||
|
|
||||||
band = g_array_index (bands_array, MMModemBand, i);
|
band = g_array_index (allowed_bands_array, MMModemBand, i);
|
||||||
if (band == MM_MODEM_BAND_UNKNOWN ||
|
if (band == MM_MODEM_BAND_UNKNOWN ||
|
||||||
band == MM_MODEM_BAND_ANY) {
|
band == MM_MODEM_BAND_ANY) {
|
||||||
GEnumClass *enum_class;
|
GEnumClass *enum_class;
|
||||||
@@ -1006,6 +1029,39 @@ validate_allowed_bands (GArray *bands_array,
|
|||||||
g_type_class_unref (enum_class);
|
g_type_class_unref (enum_class);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (supported_bands_array->len > 1 ||
|
||||||
|
g_array_index (supported_bands_array, MMModemBand, 0) != MM_MODEM_BAND_ANY) {
|
||||||
|
gboolean found = FALSE;
|
||||||
|
guint j;
|
||||||
|
|
||||||
|
/* The band given in allowed MUST be available in supported */
|
||||||
|
for (j = 0; !found && j < supported_bands_array->len; j++) {
|
||||||
|
if (band == g_array_index (supported_bands_array, MMModemBand, j))
|
||||||
|
found = TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
GEnumClass *enum_class;
|
||||||
|
GEnumValue *value;
|
||||||
|
gchar *supported_bands_str;
|
||||||
|
|
||||||
|
supported_bands_str = (mm_common_get_bands_string (
|
||||||
|
(const MMModemBand *)supported_bands_array->data,
|
||||||
|
supported_bands_array->len));
|
||||||
|
enum_class = G_ENUM_CLASS (g_type_class_ref (MM_TYPE_MODEM_BAND));
|
||||||
|
value = g_enum_get_value (enum_class, band);
|
||||||
|
g_set_error (error,
|
||||||
|
MM_CORE_ERROR,
|
||||||
|
MM_CORE_ERROR_INVALID_ARGS,
|
||||||
|
"Given allowed band (%s) is not supported (%s)",
|
||||||
|
value->value_nick,
|
||||||
|
supported_bands_str);
|
||||||
|
g_type_class_unref (enum_class);
|
||||||
|
g_free (supported_bands_str);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
@@ -1017,7 +1073,8 @@ mm_iface_modem_set_allowed_bands (MMIfaceModem *self,
|
|||||||
GAsyncReadyCallback callback,
|
GAsyncReadyCallback callback,
|
||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
GSimpleAsyncResult *result;
|
SetAllowedBandsContext *ctx;
|
||||||
|
GArray *supported_bands_array;
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
|
|
||||||
/* If setting allowed bands is not implemented, report an error */
|
/* If setting allowed bands is not implemented, report an error */
|
||||||
@@ -1032,23 +1089,39 @@ mm_iface_modem_set_allowed_bands (MMIfaceModem *self,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Setup context */
|
||||||
|
ctx = g_new0 (SetAllowedBandsContext, 1);
|
||||||
|
ctx->self = g_object_ref (self);
|
||||||
|
ctx->result = g_simple_async_result_new (G_OBJECT (self),
|
||||||
|
callback,
|
||||||
|
user_data,
|
||||||
|
mm_iface_modem_set_allowed_bands);
|
||||||
|
g_object_get (self,
|
||||||
|
MM_IFACE_MODEM_DBUS_SKELETON, &ctx->skeleton,
|
||||||
|
NULL);
|
||||||
|
ctx->allowed_bands_array = g_array_ref (bands_array);
|
||||||
|
|
||||||
|
/* Get list of supported bands */
|
||||||
|
supported_bands_array = (mm_common_bands_variant_to_garray (
|
||||||
|
mm_gdbus_modem_get_supported_bands (ctx->skeleton)));
|
||||||
|
|
||||||
/* Validate input list of bands */
|
/* Validate input list of bands */
|
||||||
if (!validate_allowed_bands (bands_array, &error)) {
|
if (!validate_allowed_bands (supported_bands_array,
|
||||||
g_simple_async_report_take_gerror_in_idle (G_OBJECT (self),
|
ctx->allowed_bands_array,
|
||||||
callback,
|
&error)) {
|
||||||
user_data,
|
g_array_unref (supported_bands_array);
|
||||||
error);
|
g_simple_async_result_take_error (ctx->result, error);
|
||||||
|
set_allowed_bands_context_complete_and_free (ctx);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = g_simple_async_result_new (G_OBJECT (self),
|
MM_IFACE_MODEM_GET_INTERFACE (self)->set_allowed_bands (
|
||||||
callback,
|
self,
|
||||||
user_data,
|
bands_array,
|
||||||
mm_iface_modem_set_allowed_bands);
|
(GAsyncReadyCallback)set_allowed_bands_ready,
|
||||||
MM_IFACE_MODEM_GET_INTERFACE (self)->set_allowed_bands (self,
|
ctx);
|
||||||
bands_array,
|
|
||||||
(GAsyncReadyCallback)set_allowed_bands_ready,
|
g_array_unref (supported_bands_array);
|
||||||
result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -1058,9 +1131,7 @@ handle_set_allowed_bands_ready (MMIfaceModem *self,
|
|||||||
{
|
{
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
|
|
||||||
if (!MM_IFACE_MODEM_GET_INTERFACE (self)->set_allowed_bands_finish (self,
|
if (!mm_iface_modem_set_allowed_bands_finish (self, res, &error))
|
||||||
res,
|
|
||||||
&error))
|
|
||||||
g_dbus_method_invocation_take_error (ctx->invocation, error);
|
g_dbus_method_invocation_take_error (ctx->invocation, error);
|
||||||
else
|
else
|
||||||
mm_gdbus_modem_complete_set_allowed_bands (ctx->skeleton,
|
mm_gdbus_modem_complete_set_allowed_bands (ctx->skeleton,
|
||||||
|
Reference in New Issue
Block a user