bearer-qmi: common complete method for the connection attempt

Refactor the logic and setup a common complete_connect() method that
helps us complete the GTask associated to the connection attempt.
This commit is contained in:
Aleksander Morgado
2019-11-25 10:37:42 +01:00
parent 2790074c7e
commit ae53b0458b

View File

@@ -499,6 +499,21 @@ connect_finish (MMBaseBearer *self,
return g_task_propagate_pointer (G_TASK (res), error); return g_task_propagate_pointer (G_TASK (res), error);
} }
static void
complete_connect (GTask *task,
MMBearerConnectResult *result,
GError *error)
{
g_assert (result || error);
g_assert (!(result && error));
if (error)
g_task_return_error (task, error);
else
g_task_return_pointer (task, result, (GDestroyNotify)mm_bearer_connect_result_unref);
g_object_unref (task);
}
static void connect_context_step (GTask *task); static void connect_context_step (GTask *task);
static void static void
@@ -1169,8 +1184,10 @@ qmi_port_allocate_client_ready (MMPortQmi *qmi,
g_assert (!(ctx->running_ipv4 && ctx->running_ipv6)); g_assert (!(ctx->running_ipv4 && ctx->running_ipv6));
if (!mm_port_qmi_allocate_client_finish (qmi, res, &error)) { if (!mm_port_qmi_allocate_client_finish (qmi, res, &error)) {
g_task_return_error (task, error); g_prefix_error (&error, "Couldn't allocate %s client in QMI port %s: ",
g_object_unref (task); ctx->running_ipv4 ? "IPv4" : "IPv6",
mm_port_get_device (MM_PORT (qmi)));
complete_connect (task, NULL, error);
return; return;
} }
@@ -1197,8 +1214,9 @@ qmi_port_open_ready (MMPortQmi *qmi,
GError *error = NULL; GError *error = NULL;
if (!mm_port_qmi_open_finish (qmi, res, &error)) { if (!mm_port_qmi_open_finish (qmi, res, &error)) {
g_task_return_error (task, error); g_prefix_error (&error, "Couldn't open QMI port %s: ",
g_object_unref (task); mm_port_get_device (MM_PORT (qmi)));
complete_connect (task, NULL, error);
return; return;
} }
@@ -1216,14 +1234,18 @@ connect_context_step (GTask *task)
ConnectContext *ctx; ConnectContext *ctx;
GCancellable *cancellable; GCancellable *cancellable;
/* If cancelled, complete */ cancellable = g_task_get_cancellable (task);
if (g_task_return_error_if_cancelled (task)) {
g_object_unref (task); if (g_cancellable_is_cancelled (cancellable)) {
complete_connect (task,
NULL,
g_error_new (G_IO_ERROR,
G_IO_ERROR_CANCELLED,
"operation cancelled"));
return; return;
} }
ctx = g_task_get_task_data (task); ctx = g_task_get_task_data (task);
cancellable = g_task_get_cancellable (task);
switch (ctx->step) { switch (ctx->step) {
case CONNECT_STEP_FIRST: case CONNECT_STEP_FIRST:
@@ -1467,7 +1489,22 @@ connect_context_step (GTask *task)
case CONNECT_STEP_LAST: case CONNECT_STEP_LAST:
/* If one of IPv4 or IPv6 succeeds, we're connected */ /* If one of IPv4 or IPv6 succeeds, we're connected */
if (ctx->packet_data_handle_ipv4 || ctx->packet_data_handle_ipv6) { if (!ctx->packet_data_handle_ipv4 && !ctx->packet_data_handle_ipv6) {
GError *error;
/* No connection, set error. If both set, IPv4 error preferred */
if (ctx->error_ipv4) {
error = ctx->error_ipv4;
ctx->error_ipv4 = NULL;
} else {
error = ctx->error_ipv6;
ctx->error_ipv6 = NULL;
}
complete_connect (task, NULL, error);
return;
}
/* Port is connected; update the state */ /* Port is connected; update the state */
mm_port_set_connected (MM_PORT (ctx->data), TRUE); mm_port_set_connected (MM_PORT (ctx->data), TRUE);
@@ -1505,27 +1542,11 @@ connect_context_step (GTask *task)
ctx->self->priv->client_ipv6 = g_object_ref (ctx->client_ipv6); ctx->self->priv->client_ipv6 = g_object_ref (ctx->client_ipv6);
} }
/* Set operation result */ complete_connect (task,
g_task_return_pointer ( mm_bearer_connect_result_new (ctx->data,
task, ctx->ipv4_config,
mm_bearer_connect_result_new (ctx->data, ctx->ipv4_config, ctx->ipv6_config), ctx->ipv6_config),
(GDestroyNotify)mm_bearer_connect_result_unref); NULL);
} else {
GError *error;
/* No connection, set error. If both set, IPv4 error preferred */
if (ctx->error_ipv4) {
error = ctx->error_ipv4;
ctx->error_ipv4 = NULL;
} else {
error = ctx->error_ipv6;
ctx->error_ipv6 = NULL;
}
g_task_return_error (task, error);
}
g_object_unref (task);
return; return;
} }
} }