bearer-list: new common method to disconnect one or all bearers

This commit is contained in:
Aleksander Morgado
2023-04-17 16:09:43 +02:00
committed by Aleksander Morgado
parent acc98e43ba
commit 200f827d38
4 changed files with 77 additions and 112 deletions

View File

@@ -29,7 +29,7 @@
#include "mm-bearer-list.h" #include "mm-bearer-list.h"
#include "mm-log.h" #include "mm-log.h"
G_DEFINE_TYPE (MMBearerList, mm_bearer_list, G_TYPE_OBJECT); G_DEFINE_TYPE (MMBearerList, mm_bearer_list, G_TYPE_OBJECT)
enum { enum {
PROP_0, PROP_0,
@@ -190,33 +190,35 @@ mm_bearer_list_find_by_apn_type (MMBearerList *self,
/*****************************************************************************/ /*****************************************************************************/
typedef struct { typedef struct {
GList *pending; gchar *bearer_path;
GList *pending;
MMBaseBearer *current; MMBaseBearer *current;
} DisconnectAllContext; } DisconnectBearersContext;
static void static void
disconnect_all_context_free (DisconnectAllContext *ctx) disconnect_bearers_context_free (DisconnectBearersContext *ctx)
{ {
g_free (ctx->bearer_path);
if (ctx->current) if (ctx->current)
g_object_unref (ctx->current); g_object_unref (ctx->current);
g_list_free_full (ctx->pending, g_object_unref); g_list_free_full (ctx->pending, g_object_unref);
g_free (ctx); g_slice_free (DisconnectBearersContext, ctx);
} }
gboolean gboolean
mm_bearer_list_disconnect_all_bearers_finish (MMBearerList *self, mm_bearer_list_disconnect_bearers_finish (MMBearerList *self,
GAsyncResult *res, GAsyncResult *res,
GError **error) GError **error)
{ {
return g_task_propagate_boolean (G_TASK (res), error); return g_task_propagate_boolean (G_TASK (res), error);
} }
static void disconnect_next_bearer (GTask *task); static void disconnect_bearers_next (GTask *task);
static void static void
disconnect_ready (MMBaseBearer *bearer, bearer_disconnect_ready (MMBaseBearer *bearer,
GAsyncResult *res, GAsyncResult *res,
GTask *task) GTask *task)
{ {
GError *error = NULL; GError *error = NULL;
@@ -225,18 +227,16 @@ disconnect_ready (MMBaseBearer *bearer,
g_object_unref (task); g_object_unref (task);
return; return;
} }
disconnect_bearers_next (task);
disconnect_next_bearer (task);
} }
static void static void
disconnect_next_bearer (GTask *task) disconnect_bearers_next (GTask *task)
{ {
DisconnectAllContext *ctx; DisconnectBearersContext *ctx;
ctx = g_task_get_task_data (task); ctx = g_task_get_task_data (task);
if (ctx->current) g_clear_object (&ctx->current);
g_clear_object (&ctx->current);
/* No more bearers? all done! */ /* No more bearers? all done! */
if (!ctx->pending) { if (!ctx->pending) {
@@ -249,30 +249,46 @@ disconnect_next_bearer (GTask *task)
ctx->pending = g_list_delete_link (ctx->pending, ctx->pending); ctx->pending = g_list_delete_link (ctx->pending, ctx->pending);
mm_base_bearer_disconnect (ctx->current, mm_base_bearer_disconnect (ctx->current,
(GAsyncReadyCallback)disconnect_ready, (GAsyncReadyCallback)bearer_disconnect_ready,
task); task);
} }
void static void
mm_bearer_list_disconnect_all_bearers (MMBearerList *self, build_connected_bearer_list (MMBaseBearer *bearer,
GAsyncReadyCallback callback, DisconnectBearersContext *ctx)
gpointer user_data)
{ {
DisconnectAllContext *ctx; if (!ctx->bearer_path ||
GTask *task; g_str_equal (ctx->bearer_path, mm_base_bearer_get_path (bearer)))
ctx->pending = g_list_prepend (ctx->pending, g_object_ref (bearer));
}
ctx = g_new0 (DisconnectAllContext, 1); void
/* Get a copy of the list */ mm_bearer_list_disconnect_bearers (MMBearerList *self,
ctx->pending = g_list_copy_deep (self->priv->bearers, const gchar *bearer_path,
(GCopyFunc)g_object_ref, GAsyncReadyCallback callback,
NULL); gpointer user_data)
{
GTask *task;
DisconnectBearersContext *ctx;
task = g_task_new (self, NULL, callback, user_data); task = g_task_new (self, NULL, callback, user_data);
g_task_set_task_data (task, ctx = g_slice_new0 (DisconnectBearersContext);
ctx, ctx->bearer_path = g_strdup (bearer_path); /* may be NULL if disconnecting all */
(GDestroyNotify)disconnect_all_context_free); g_task_set_task_data (task, ctx, (GDestroyNotify)disconnect_bearers_context_free);
disconnect_next_bearer (task); /* If a given specific bearer is being disconnected, only add that one. Otherwise,
* disconnect all. */
mm_bearer_list_foreach (self, (MMBearerListForeachFunc)build_connected_bearer_list, ctx);
if (ctx->bearer_path && !ctx->pending) {
g_task_return_new_error (task, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS,
"Couldn't disconnect bearer '%s': not found",
ctx->bearer_path);
g_object_unref (task);
return;
}
disconnect_bearers_next (task);
} }
/*****************************************************************************/ /*****************************************************************************/

View File

@@ -79,12 +79,13 @@ MMBaseBearer *mm_bearer_list_find_by_profile_id (MMBearerList *self,
MMBaseBearer *mm_bearer_list_find_by_apn_type (MMBearerList *self, MMBaseBearer *mm_bearer_list_find_by_apn_type (MMBearerList *self,
MMBearerApnType apn_type); MMBearerApnType apn_type);
void mm_bearer_list_disconnect_all_bearers (MMBearerList *self, void mm_bearer_list_disconnect_bearers (MMBearerList *self,
GAsyncReadyCallback callback, const gchar *bearer_path,
gpointer user_data); GAsyncReadyCallback callback,
gboolean mm_bearer_list_disconnect_all_bearers_finish (MMBearerList *self, gpointer user_data);
GAsyncResult *res, gboolean mm_bearer_list_disconnect_bearers_finish (MMBearerList *self,
GError **error); GAsyncResult *res,
GError **error);
#if defined WITH_SUSPEND_RESUME #if defined WITH_SUSPEND_RESUME

View File

@@ -11339,14 +11339,14 @@ INTERFACE_DISABLE_READY_FN (iface_modem_time, MM_IFACE_MODEM_TIM
INTERFACE_DISABLE_READY_FN (iface_modem_oma, MM_IFACE_MODEM_OMA, FALSE) INTERFACE_DISABLE_READY_FN (iface_modem_oma, MM_IFACE_MODEM_OMA, FALSE)
static void static void
bearer_list_disconnect_all_bearers_ready (MMBearerList *list, bearer_list_disconnect_bearers_ready (MMBearerList *list,
GAsyncResult *res, GAsyncResult *res,
GTask *task) GTask *task)
{ {
DisablingContext *ctx; DisablingContext *ctx;
GError *error = NULL; GError *error = NULL;
if (!mm_bearer_list_disconnect_all_bearers_finish (list, res, &error)) { if (!mm_bearer_list_disconnect_bearers_finish (list, res, &error)) {
g_task_return_error (task, error); g_task_return_error (task, error);
g_object_unref (task); g_object_unref (task);
return; return;
@@ -11442,9 +11442,10 @@ disabling_step (GTask *task)
return; return;
} }
if (ctx->self->priv->modem_bearer_list) { if (ctx->self->priv->modem_bearer_list) {
mm_bearer_list_disconnect_all_bearers ( mm_bearer_list_disconnect_bearers (
ctx->self->priv->modem_bearer_list, ctx->self->priv->modem_bearer_list,
(GAsyncReadyCallback)bearer_list_disconnect_all_bearers_ready, NULL, /* all bearers */
(GAsyncReadyCallback)bearer_list_disconnect_bearers_ready,
task); task);
return; return;
} }

View File

@@ -974,8 +974,6 @@ typedef struct {
MmGdbusModemSimple *skeleton; MmGdbusModemSimple *skeleton;
GDBusMethodInvocation *invocation; GDBusMethodInvocation *invocation;
gchar *bearer_path; gchar *bearer_path;
GList *bearers;
MMBaseBearer *current;
} DisconnectionContext; } DisconnectionContext;
static void static void
@@ -985,61 +983,24 @@ disconnection_context_free (DisconnectionContext *ctx)
g_object_unref (ctx->invocation); g_object_unref (ctx->invocation);
g_object_unref (ctx->self); g_object_unref (ctx->self);
g_free (ctx->bearer_path); g_free (ctx->bearer_path);
g_clear_object (&ctx->current);
g_list_free_full (ctx->bearers, g_object_unref);
g_slice_free (DisconnectionContext, ctx); g_slice_free (DisconnectionContext, ctx);
} }
static void disconnect_next_bearer (DisconnectionContext *ctx);
static void static void
disconnect_ready (MMBaseBearer *bearer, bearer_list_disconnect_bearers_ready (MMBearerList *bearer_list,
GAsyncResult *res, GAsyncResult *res,
DisconnectionContext *ctx) DisconnectionContext *ctx)
{ {
GError *error = NULL; GError *error = NULL;
if (!mm_base_bearer_disconnect_finish (bearer, res, &error)) { if (!mm_bearer_list_disconnect_bearers_finish (bearer_list, res, &error)) {
mm_obj_warn (ctx->self, "failed to disconnect bearer '%s': %s", mm_obj_warn (ctx->self, "failed to disconnect bearers: %s", error->message);
mm_base_bearer_get_path (bearer), error->message);
g_dbus_method_invocation_take_error (ctx->invocation, error); g_dbus_method_invocation_take_error (ctx->invocation, error);
disconnection_context_free (ctx); } else {
return;
}
disconnect_next_bearer (ctx);
}
static void
disconnect_next_bearer (DisconnectionContext *ctx)
{
if (ctx->current)
g_clear_object (&ctx->current);
/* No more bearers? all done! */
if (!ctx->bearers) {
mm_obj_info (ctx->self, "all requested bearers disconnected"); mm_obj_info (ctx->self, "all requested bearers disconnected");
mm_gdbus_modem_simple_complete_disconnect (ctx->skeleton, ctx->invocation); mm_gdbus_modem_simple_complete_disconnect (ctx->skeleton, ctx->invocation);
disconnection_context_free (ctx);
return;
} }
disconnection_context_free (ctx);
ctx->current = MM_BASE_BEARER (ctx->bearers->data);
ctx->bearers = g_list_delete_link (ctx->bearers, ctx->bearers);
mm_obj_info (ctx->self, "disconnecting bearer '%s'...", mm_base_bearer_get_path (ctx->current));
mm_base_bearer_disconnect (ctx->current,
(GAsyncReadyCallback)disconnect_ready,
ctx);
}
static void
build_connected_bearer_list (MMBaseBearer *bearer,
DisconnectionContext *ctx)
{
if (!ctx->bearer_path ||
g_str_equal (ctx->bearer_path, mm_base_bearer_get_path (bearer)))
ctx->bearers = g_list_prepend (ctx->bearers, g_object_ref (bearer));
} }
static void static void
@@ -1083,24 +1044,10 @@ disconnect_auth_ready (MMBaseModem *self,
else else
mm_obj_info (self, "processing user request to disconnect modem: all bearers"); mm_obj_info (self, "processing user request to disconnect modem: all bearers");
mm_bearer_list_foreach (list, mm_bearer_list_disconnect_bearers (list,
(MMBearerListForeachFunc)build_connected_bearer_list, ctx->bearer_path,
ctx); (GAsyncReadyCallback)bearer_list_disconnect_bearers_ready,
ctx);
if (ctx->bearer_path && !ctx->bearers) {
mm_obj_warn (self, "failed to disconnect bearer '%s': not found", ctx->bearer_path);
g_dbus_method_invocation_return_error (
ctx->invocation,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't disconnect bearer '%s': not found",
ctx->bearer_path);
disconnection_context_free (ctx);
return;
}
/* Go on disconnecting bearers */
disconnect_next_bearer (ctx);
} }
static gboolean static gboolean