broadband-modem: don't assume an AT port always exists in explicit registration

This commit is contained in:
Aleksander Morgado
2024-04-12 01:17:14 +00:00
committed by Dan Williams
parent ec8bbeeafc
commit b158c2e92a
+75 -47
View File
@@ -5003,6 +5003,19 @@ modem_3gpp_scan_networks (MMIfaceModem3gpp *self,
/*****************************************************************************/
/* Register in network (3GPP interface) */
typedef struct {
gchar *operator_id;
MMPortSerialAt *port;
} RegisterInNetworkContext;
static void
register_in_network_context_free (RegisterInNetworkContext *ctx)
{
g_free (ctx->operator_id);
g_object_unref (ctx->port);
g_slice_free (RegisterInNetworkContext, ctx);
}
static gboolean
modem_3gpp_register_in_network_finish (MMIfaceModem3gpp *self,
GAsyncResult *res,
@@ -5034,42 +5047,48 @@ cops_ascii_set_ready (MMBaseModem *_self,
g_autoptr(GError) error = NULL;
if (!mm_base_modem_at_command_full_finish (_self, res, &error)) {
/* If it failed with an unsupported error, retry with current modem charset */
if (g_error_matches (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_NOT_SUPPORTED)) {
g_autoptr(GError) enc_error = NULL;
g_autofree gchar *operator_id_enc = NULL;
gchar *operator_id;
RegisterInNetworkContext *ctx;
g_autoptr(GError) enc_error = NULL;
g_autofree gchar *operator_id_enc = NULL;
g_autofree gchar *command = NULL;
/* try to encode to current charset */
operator_id = g_task_get_task_data (task);
operator_id_enc = mm_modem_charset_str_from_utf8 (operator_id, self->priv->modem_current_charset, FALSE, &enc_error);
if (!operator_id_enc) {
mm_obj_dbg (self, "couldn't convert operator id to current charset: %s", enc_error->message);
g_task_return_error (task, g_steal_pointer (&error));
g_object_unref (task);
return;
}
/* retry only if encoded string is different to the non-encoded one */
if (g_strcmp0 (operator_id, operator_id_enc) != 0) {
g_autofree gchar *command = NULL;
command = g_strdup_printf ("+COPS=1,2,\"%s\"", operator_id_enc);
mm_base_modem_at_command_full (_self,
mm_base_modem_peek_best_at_port (_self, NULL),
command,
120,
FALSE,
FALSE, /* raw */
g_task_get_cancellable (task),
(GAsyncReadyCallback)cops_set_ready,
task);
return;
}
if (!g_error_matches (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_NOT_SUPPORTED)) {
g_task_return_error (task, g_steal_pointer (&error));
g_object_unref (task);
return;
}
g_task_return_error (task, g_steal_pointer (&error));
} else
g_task_return_boolean (task, TRUE);
/* If it failed with an unsupported error, retry with current modem charset */
ctx = g_task_get_task_data (task);
operator_id_enc = mm_modem_charset_str_from_utf8 (ctx->operator_id, self->priv->modem_current_charset, FALSE, &enc_error);
if (!operator_id_enc) {
mm_obj_dbg (self, "couldn't convert operator id to current charset: %s", enc_error->message);
g_task_return_error (task, g_steal_pointer (&error));
g_object_unref (task);
return;
}
/* retry only if encoded string is different to the non-encoded one */
if (g_strcmp0 (ctx->operator_id, operator_id_enc) == 0) {
g_task_return_error (task, g_steal_pointer (&error));
g_object_unref (task);
return;
}
command = g_strdup_printf ("+COPS=1,2,\"%s\"", operator_id_enc);
mm_base_modem_at_command_full (_self,
ctx->port,
command,
120,
FALSE,
FALSE, /* raw */
g_task_get_cancellable (task),
(GAsyncReadyCallback)cops_set_ready,
task);
return;
}
g_task_return_boolean (task, TRUE);
g_object_unref (task);
}
@@ -5080,17 +5099,31 @@ modem_3gpp_register_in_network (MMIfaceModem3gpp *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
gchar *command;
RegisterInNetworkContext *ctx;
GTask *task;
MMPortSerialAt *port;
GError *error = NULL;
g_autofree gchar *command = NULL;
task = g_task_new (self, cancellable, callback, user_data);
port = mm_base_modem_peek_best_at_port (MM_BASE_MODEM (self), &error);
if (!port) {
g_task_return_error (task, error);
g_object_unref (task);
return;
}
ctx = g_slice_new0 (RegisterInNetworkContext);
ctx->port = g_object_ref (port);
ctx->operator_id = g_strdup (operator_id);
g_task_set_task_data (task, ctx, (GDestroyNotify)register_in_network_context_free);
/* Trigger automatic network registration if no explicit operator id given */
if (!operator_id) {
/* Note that '+COPS=0,,' (same but with commas) won't work in some Nokia
* phones */
if (!ctx->operator_id) {
/* Note that '+COPS=0,,' (same but with commas) won't work in some Nokia phones */
mm_base_modem_at_command_full (MM_BASE_MODEM (self),
mm_base_modem_peek_best_at_port (MM_BASE_MODEM (self), NULL),
port,
"+COPS=0",
120,
FALSE,
@@ -5101,14 +5134,10 @@ modem_3gpp_register_in_network (MMIfaceModem3gpp *self,
return;
}
/* Store operator id in context, in case we need to retry with the current
* modem charset */
g_task_set_task_data (task, g_strdup (operator_id), g_free);
/* Use the operator id given in ASCII initially */
command = g_strdup_printf ("+COPS=1,2,\"%s\"", operator_id);
command = g_strdup_printf ("+COPS=1,2,\"%s\"", ctx->operator_id);
mm_base_modem_at_command_full (MM_BASE_MODEM (self),
mm_base_modem_peek_best_at_port (MM_BASE_MODEM (self), NULL),
port,
command,
120,
FALSE,
@@ -5116,7 +5145,6 @@ modem_3gpp_register_in_network (MMIfaceModem3gpp *self,
cancellable,
(GAsyncReadyCallback)cops_ascii_set_ready,
task);
g_free (command);
}
/*****************************************************************************/