broadband-modem,ussd: port to GTask

This commit is contained in:
Aleksander Morgado
2018-08-06 22:17:11 +02:00
committed by Dan Williams
parent fc41c167ce
commit 6838a3f715

View File

@@ -175,7 +175,7 @@ struct _MMBroadbandModemPrivate {
GObject *modem_3gpp_ussd_dbus_skeleton; GObject *modem_3gpp_ussd_dbus_skeleton;
/* Implementation helpers */ /* Implementation helpers */
gboolean use_unencoded_ussd; gboolean use_unencoded_ussd;
GSimpleAsyncResult *pending_ussd_action; GTask *pending_ussd_action;
/*<--- Modem CDMA interface --->*/ /*<--- Modem CDMA interface --->*/
/* Properties */ /* Properties */
@@ -4898,25 +4898,27 @@ cancel_command_ready (MMBroadbandModem *self,
GError *error = NULL; GError *error = NULL;
mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error); mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error);
/* Complete the pending action, regardless of the CUSD result */
if (self->priv->pending_ussd_action) {
GTask *task;
task = self->priv->pending_ussd_action;
self->priv->pending_ussd_action = NULL;
g_task_return_new_error (task, MM_CORE_ERROR, MM_CORE_ERROR_CANCELLED,
"USSD session was cancelled");
g_object_unref (task);
}
mm_iface_modem_3gpp_ussd_update_state (MM_IFACE_MODEM_3GPP_USSD (self),
MM_MODEM_3GPP_USSD_SESSION_STATE_IDLE);
if (error) if (error)
g_task_return_error (task, error); g_task_return_error (task, error);
else else
g_task_return_boolean (task, TRUE); g_task_return_boolean (task, TRUE);
g_object_unref (task); g_object_unref (task);
/* Complete the pending action, if any */
if (self->priv->pending_ussd_action) {
g_simple_async_result_set_error (self->priv->pending_ussd_action,
MM_CORE_ERROR,
MM_CORE_ERROR_CANCELLED,
"USSD session was cancelled");
g_simple_async_result_complete_in_idle (self->priv->pending_ussd_action);
g_object_unref (self->priv->pending_ussd_action);
self->priv->pending_ussd_action = NULL;
}
mm_iface_modem_3gpp_ussd_update_state (MM_IFACE_MODEM_3GPP_USSD (self),
MM_MODEM_3GPP_USSD_SESSION_STATE_IDLE);
} }
static void static void
@@ -4940,8 +4942,6 @@ modem_3gpp_ussd_cancel (MMIfaceModem3gppUssd *self,
/* Send command (3GPP/USSD interface) */ /* Send command (3GPP/USSD interface) */
typedef struct { typedef struct {
MMBroadbandModem *self;
GSimpleAsyncResult *result;
gchar *command; gchar *command;
gboolean current_is_unencoded; gboolean current_is_unencoded;
gboolean encoded_used; gboolean encoded_used;
@@ -4949,104 +4949,93 @@ typedef struct {
} Modem3gppUssdSendContext; } Modem3gppUssdSendContext;
static void static void
modem_3gpp_ussd_send_context_complete_and_free (Modem3gppUssdSendContext *ctx) modem_3gpp_ussd_send_context_free (Modem3gppUssdSendContext *ctx)
{ {
/* We check for result, as we may have already set it in
* priv->pending_ussd_request */
if (ctx->result) {
g_simple_async_result_complete_in_idle (ctx->result);
g_object_unref (ctx->result);
}
g_object_unref (ctx->self);
g_free (ctx->command); g_free (ctx->command);
g_slice_free (Modem3gppUssdSendContext, ctx); g_slice_free (Modem3gppUssdSendContext, ctx);
} }
static const gchar * static gchar *
modem_3gpp_ussd_send_finish (MMIfaceModem3gppUssd *self, modem_3gpp_ussd_send_finish (MMIfaceModem3gppUssd *self,
GAsyncResult *res, GAsyncResult *res,
GError **error) GError **error)
{ {
if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error)) return g_task_propagate_pointer (G_TASK (res), error);
return NULL;
return g_strdup (g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res)));
} }
static void modem_3gpp_ussd_context_step (Modem3gppUssdSendContext *ctx); static void modem_3gpp_ussd_context_step (MMBroadbandModem *self);
static void cusd_process_string (MMBroadbandModem *self, static void cusd_process_string (MMBroadbandModem *self,
const gchar *str); const gchar *str);
static void static void
ussd_send_command_ready (MMBroadbandModem *self, ussd_send_command_ready (MMBaseModem *_self,
GAsyncResult *res, GAsyncResult *res)
Modem3gppUssdSendContext *ctx)
{ {
MMBroadbandModem *self;
Modem3gppUssdSendContext *ctx;
GError *error = NULL; GError *error = NULL;
const gchar *reply; const gchar *response;
g_assert (ctx->result == NULL); self = MM_BROADBAND_MODEM (_self);
reply = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error); response = mm_base_modem_at_command_finish (_self, res, &error);
if (error) { if (error) {
/* Some immediate error happened when sending the USSD request */ /* Some immediate error happened when sending the USSD request */
mm_dbg ("Error sending USSD request: '%s'", error->message); mm_dbg ("Error sending USSD request: '%s'", error->message);
g_error_free (error); g_error_free (error);
if (self->priv->pending_ussd_action) { if (self->priv->pending_ussd_action) {
/* Recover result */ modem_3gpp_ussd_context_step (self);
ctx->result = self->priv->pending_ussd_action;
self->priv->pending_ussd_action = NULL;
modem_3gpp_ussd_context_step (ctx);
return; return;
} }
}
/* So the USSD action was completed already... */ if (!self->priv->pending_ussd_action) {
mm_dbg ("USSD action already completed via URCs"); mm_dbg ("USSD operation finished already via URCs");
modem_3gpp_ussd_send_context_complete_and_free (ctx);
return; return;
} }
/* Cache the hint for the next time we send something */ /* Cache the hint for the next time we send something */
if (!ctx->self->priv->use_unencoded_ussd && ctx = g_task_get_task_data (self->priv->pending_ussd_action);
ctx->current_is_unencoded) { if (!self->priv->use_unencoded_ussd && ctx->current_is_unencoded) {
mm_dbg ("Will assume we want unencoded USSD commands"); mm_dbg ("Will assume we want unencoded USSD commands");
ctx->self->priv->use_unencoded_ussd = TRUE; self->priv->use_unencoded_ussd = TRUE;
} else if (ctx->self->priv->use_unencoded_ussd && } else if (self->priv->use_unencoded_ussd && !ctx->current_is_unencoded) {
!ctx->current_is_unencoded) {
mm_dbg ("Will assume we want encoded USSD commands"); mm_dbg ("Will assume we want encoded USSD commands");
ctx->self->priv->use_unencoded_ussd = FALSE; self->priv->use_unencoded_ussd = FALSE;
} }
if (!self->priv->pending_ussd_action) if (response && response[0]) {
mm_dbg ("USSD operation finished already via URCs"); response = mm_strip_tag (response, "+CUSD:");
else if (reply && reply[0]) { cusd_process_string (self, response);
reply = mm_strip_tag (reply, "+CUSD:");
cusd_process_string (ctx->self, reply);
} }
modem_3gpp_ussd_send_context_complete_and_free (ctx);
} }
static void static void
modem_3gpp_ussd_context_send_encoded (Modem3gppUssdSendContext *ctx) modem_3gpp_ussd_context_send_encoded (MMBroadbandModem *self)
{ {
Modem3gppUssdSendContext *ctx;
gchar *at_command = NULL; gchar *at_command = NULL;
GError *error = NULL; GError *error = NULL;
guint scheme = 0; guint scheme = 0;
gchar *encoded; gchar *encoded;
g_assert (self->priv->pending_ussd_action);
ctx = g_task_get_task_data (self->priv->pending_ussd_action);
/* Encode USSD command */ /* Encode USSD command */
encoded = mm_iface_modem_3gpp_ussd_encode (MM_IFACE_MODEM_3GPP_USSD (ctx->self), encoded = mm_iface_modem_3gpp_ussd_encode (MM_IFACE_MODEM_3GPP_USSD (self), ctx->command, &scheme, &error);
ctx->command,
&scheme,
&error);
if (!encoded) { if (!encoded) {
mm_iface_modem_3gpp_ussd_update_state (MM_IFACE_MODEM_3GPP_USSD (ctx->self), GTask *task;
MM_MODEM_3GPP_USSD_SESSION_STATE_IDLE);
g_simple_async_result_take_error (ctx->result, error); task = self->priv->pending_ussd_action;
modem_3gpp_ussd_send_context_complete_and_free (ctx); self->priv->pending_ussd_action = NULL;
mm_iface_modem_3gpp_ussd_update_state (MM_IFACE_MODEM_3GPP_USSD (self), MM_MODEM_3GPP_USSD_SESSION_STATE_IDLE);
g_task_return_error (task, error);
g_object_unref (task);
return; return;
} }
@@ -5056,26 +5045,24 @@ modem_3gpp_ussd_context_send_encoded (Modem3gppUssdSendContext *ctx)
at_command = g_strdup_printf ("+CUSD=1,\"%s\",%d", encoded, scheme); at_command = g_strdup_printf ("+CUSD=1,\"%s\",%d", encoded, scheme);
g_free (encoded); g_free (encoded);
/* Cache the action, as it may be completed via URCs. mm_base_modem_at_command (MM_BASE_MODEM (self),
* There shouldn't be any previous action pending. */
g_warn_if_fail (ctx->self->priv->pending_ussd_action == NULL);
ctx->self->priv->pending_ussd_action = ctx->result;
ctx->result = NULL;
mm_base_modem_at_command (MM_BASE_MODEM (ctx->self),
at_command, at_command,
10, 10,
FALSE, FALSE,
(GAsyncReadyCallback)ussd_send_command_ready, (GAsyncReadyCallback)ussd_send_command_ready,
ctx); NULL);
g_free (at_command); g_free (at_command);
} }
static void static void
modem_3gpp_ussd_context_send_unencoded (Modem3gppUssdSendContext *ctx) modem_3gpp_ussd_context_send_unencoded (MMBroadbandModem *self)
{ {
Modem3gppUssdSendContext *ctx;
gchar *at_command = NULL; gchar *at_command = NULL;
g_assert (self->priv->pending_ussd_action);
ctx = g_task_get_task_data (self->priv->pending_ussd_action);
/* Build AT command with action unencoded */ /* Build AT command with action unencoded */
ctx->unencoded_used = TRUE; ctx->unencoded_used = TRUE;
ctx->current_is_unencoded = TRUE; ctx->current_is_unencoded = TRUE;
@@ -5083,76 +5070,86 @@ modem_3gpp_ussd_context_send_unencoded (Modem3gppUssdSendContext *ctx)
ctx->command, ctx->command,
MM_MODEM_GSM_USSD_SCHEME_7BIT); MM_MODEM_GSM_USSD_SCHEME_7BIT);
/* Cache the action, as it may be completed via URCs. mm_base_modem_at_command (MM_BASE_MODEM (self),
* There shouldn't be any previous action pending. */
g_warn_if_fail (ctx->self->priv->pending_ussd_action == NULL);
ctx->self->priv->pending_ussd_action = ctx->result;
ctx->result = NULL;
mm_base_modem_at_command (MM_BASE_MODEM (ctx->self),
at_command, at_command,
10, 10,
FALSE, FALSE,
(GAsyncReadyCallback)ussd_send_command_ready, (GAsyncReadyCallback)ussd_send_command_ready,
ctx); NULL);
g_free (at_command); g_free (at_command);
} }
static void static void
modem_3gpp_ussd_context_step (Modem3gppUssdSendContext *ctx) modem_3gpp_ussd_context_step (MMBroadbandModem *self)
{ {
if (ctx->encoded_used && Modem3gppUssdSendContext *ctx;
ctx->unencoded_used) {
mm_iface_modem_3gpp_ussd_update_state (MM_IFACE_MODEM_3GPP_USSD (ctx->self), g_assert (self->priv->pending_ussd_action);
ctx = g_task_get_task_data (self->priv->pending_ussd_action);
if (ctx->encoded_used && ctx->unencoded_used) {
GTask *task;
task = self->priv->pending_ussd_action;
self->priv->pending_ussd_action = NULL;
mm_iface_modem_3gpp_ussd_update_state (MM_IFACE_MODEM_3GPP_USSD (self),
MM_MODEM_3GPP_USSD_SESSION_STATE_IDLE); MM_MODEM_3GPP_USSD_SESSION_STATE_IDLE);
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR, g_task_return_new_error (task, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
MM_CORE_ERROR_FAILED,
"Sending USSD command failed"); "Sending USSD command failed");
modem_3gpp_ussd_send_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
if (ctx->self->priv->use_unencoded_ussd) { if (self->priv->use_unencoded_ussd) {
if (!ctx->unencoded_used) if (!ctx->unencoded_used)
modem_3gpp_ussd_context_send_unencoded (ctx); modem_3gpp_ussd_context_send_unencoded (self);
else if (!ctx->encoded_used) else if (!ctx->encoded_used)
modem_3gpp_ussd_context_send_encoded (ctx); modem_3gpp_ussd_context_send_encoded (self);
else else
g_assert_not_reached (); g_assert_not_reached ();
} else { } else {
if (!ctx->encoded_used) if (!ctx->encoded_used)
modem_3gpp_ussd_context_send_encoded (ctx); modem_3gpp_ussd_context_send_encoded (self);
else if (!ctx->unencoded_used) else if (!ctx->unencoded_used)
modem_3gpp_ussd_context_send_unencoded (ctx); modem_3gpp_ussd_context_send_unencoded (self);
else else
g_assert_not_reached (); g_assert_not_reached ();
} }
} }
static void static void
modem_3gpp_ussd_send (MMIfaceModem3gppUssd *self, modem_3gpp_ussd_send (MMIfaceModem3gppUssd *_self,
const gchar *command, const gchar *command,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
MMBroadbandModem *self;
GTask *task;
Modem3gppUssdSendContext *ctx; Modem3gppUssdSendContext *ctx;
self = MM_BROADBAND_MODEM (_self);
task = g_task_new (self, NULL, callback, user_data);
ctx = g_slice_new0 (Modem3gppUssdSendContext); ctx = g_slice_new0 (Modem3gppUssdSendContext);
/* We're going to steal the string result in finish() so we must have a
* callback specified. */
g_assert (callback != NULL);
ctx->self = g_object_ref (self);
ctx->command = g_strdup (command); ctx->command = g_strdup (command);
ctx->result = g_simple_async_result_new (G_OBJECT (self), g_task_set_task_data (task, ctx, (GDestroyNotify) modem_3gpp_ussd_send_context_free);
callback,
user_data,
modem_3gpp_ussd_send);
mm_iface_modem_3gpp_ussd_update_state (self, /* Fail if there is an ongoing operation already */
MM_MODEM_3GPP_USSD_SESSION_STATE_ACTIVE); if (self->priv->pending_ussd_action) {
g_task_return_new_error (task, MM_CORE_ERROR, MM_CORE_ERROR_IN_PROGRESS,
"there is already an ongoing USSD operation");
g_object_unref (task);
return;
}
modem_3gpp_ussd_context_step (ctx); /* Cache the action, as it may be completed via URCs */
self->priv->pending_ussd_action = task;
mm_iface_modem_3gpp_ussd_update_state (_self, MM_MODEM_3GPP_USSD_SESSION_STATE_ACTIVE);
modem_3gpp_ussd_context_step (self);
} }
/*****************************************************************************/ /*****************************************************************************/
@@ -5257,117 +5254,97 @@ cusd_process_string (MMBroadbandModem *self,
const gchar *str) const gchar *str)
{ {
MMModem3gppUssdSessionState ussd_state = MM_MODEM_3GPP_USSD_SESSION_STATE_IDLE; MMModem3gppUssdSessionState ussd_state = MM_MODEM_3GPP_USSD_SESSION_STATE_IDLE;
GError *error = NULL;
gint status;
GTask *task;
gchar *converted = NULL;
/* If there is a pending action, it is ALWAYS completed here */
task = self->priv->pending_ussd_action;
self->priv->pending_ussd_action = NULL;
if (!str || !isdigit (*str)) { if (!str || !isdigit (*str)) {
if (self->priv->pending_ussd_action) error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
g_simple_async_result_set_error (self->priv->pending_ussd_action, "Invalid USSD message received: '%s'", str ? str : "(none)");
MM_CORE_ERROR, goto out;
MM_CORE_ERROR_FAILED, }
"Invalid USSD response received: '%s'",
str ? str : "(none)");
else
mm_warn ("Received invalid USSD network-initiated request: '%s'",
str ? str : "(none)");
} else {
gint status;
status = g_ascii_digit_value (*str); status = g_ascii_digit_value (*str);
switch (status) { switch (status) {
case 0: /* no further action required */ { case 0:
gchar *converted; /* no further action required */
GError *error = NULL;
converted = decode_ussd_response (self, str, &error); converted = decode_ussd_response (self, str, &error);
if (self->priv->pending_ussd_action) { if (!converted)
/* Response to the user's request */ break;
if (error)
g_simple_async_result_take_error (self->priv->pending_ussd_action, error); /* Response to the user's request? */
else if (task)
g_simple_async_result_set_op_res_gpointer (self->priv->pending_ussd_action, break;
converted,
g_free);
} else {
if (error) {
mm_warn ("Invalid network initiated USSD notification: %s",
error->message);
g_error_free (error);
} else {
/* Network-initiated USSD-Notify */ /* Network-initiated USSD-Notify */
mm_iface_modem_3gpp_ussd_update_network_notification ( mm_iface_modem_3gpp_ussd_update_network_notification (MM_IFACE_MODEM_3GPP_USSD (self), converted);
MM_IFACE_MODEM_3GPP_USSD (self), g_clear_pointer (&converted, g_free);
converted);
g_free (converted);
}
}
break; break;
}
case 1: /* further action required */ {
gchar *converted;
GError *error = NULL;
case 1:
/* further action required */
ussd_state = MM_MODEM_3GPP_USSD_SESSION_STATE_USER_RESPONSE; ussd_state = MM_MODEM_3GPP_USSD_SESSION_STATE_USER_RESPONSE;
converted = decode_ussd_response (self, str, &error); converted = decode_ussd_response (self, str, &error);
if (self->priv->pending_ussd_action) { if (!converted)
if (error) break;
g_simple_async_result_take_error (self->priv->pending_ussd_action, error);
else /* Response to the user's request? */
g_simple_async_result_set_op_res_gpointer (self->priv->pending_ussd_action, if (task)
converted, break;
g_free);
} else { /* Network-initiated USSD-Request */
if (error) { mm_iface_modem_3gpp_ussd_update_network_request (MM_IFACE_MODEM_3GPP_USSD (self), converted);
mm_warn ("Invalid network initiated USSD request: %s", g_clear_pointer (&converted, g_free);
error->message);
g_error_free (error);
} else {
/* Network-initiated USSD-Request */
mm_iface_modem_3gpp_ussd_update_network_request (
MM_IFACE_MODEM_3GPP_USSD (self),
converted);
g_free (converted);
}
}
break; break;
}
case 2: case 2:
if (self->priv->pending_ussd_action) /* Response to the user's request? */
g_simple_async_result_set_error (self->priv->pending_ussd_action, if (task)
MM_CORE_ERROR, error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_CANCELLED, "USSD terminated by network");
MM_CORE_ERROR_CANCELLED,
"USSD terminated by network.");
break; break;
case 4: case 4:
if (self->priv->pending_ussd_action) /* Response to the user's request? */
g_simple_async_result_set_error (self->priv->pending_ussd_action, if (task)
MM_CORE_ERROR, error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_CANCELLED, "Operation not supported");
MM_CORE_ERROR_UNSUPPORTED,
"Operation not supported.");
break; break;
default: default:
if (self->priv->pending_ussd_action) /* Response to the user's request? */
g_simple_async_result_set_error (self->priv->pending_ussd_action, if (task)
MM_CORE_ERROR, error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
MM_CORE_ERROR_FAILED, "Unhandled USSD reply: %s (%d)", str, status);
"Unhandled USSD reply: %s (%d)",
str,
status);
break; break;
} }
}
mm_iface_modem_3gpp_ussd_update_state (MM_IFACE_MODEM_3GPP_USSD (self), out:
ussd_state);
mm_iface_modem_3gpp_ussd_update_state (MM_IFACE_MODEM_3GPP_USSD (self), ussd_state);
/* Complete the pending action */ /* Complete the pending action */
if (self->priv->pending_ussd_action) { if (task) {
g_simple_async_result_complete_in_idle (self->priv->pending_ussd_action); if (error)
g_object_unref (self->priv->pending_ussd_action); g_task_return_error (task, error);
self->priv->pending_ussd_action = NULL; else if (converted)
g_task_return_pointer (task, converted, g_free);
else
g_assert_not_reached ();
return;
} }
/* If no pending task, just report the error */
if (error) {
mm_warn ("Invalid USSD message: %s", error->message);
g_error_free (error);
}
g_assert (!converted);
} }
static void static void