bearer-list: new common method to disconnect one or all bearers
This commit is contained in:

committed by
Aleksander Morgado

parent
acc98e43ba
commit
200f827d38
@@ -29,7 +29,7 @@
|
||||
#include "mm-bearer-list.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 {
|
||||
PROP_0,
|
||||
@@ -190,33 +190,35 @@ mm_bearer_list_find_by_apn_type (MMBearerList *self,
|
||||
/*****************************************************************************/
|
||||
|
||||
typedef struct {
|
||||
GList *pending;
|
||||
gchar *bearer_path;
|
||||
GList *pending;
|
||||
MMBaseBearer *current;
|
||||
} DisconnectAllContext;
|
||||
} DisconnectBearersContext;
|
||||
|
||||
static void
|
||||
disconnect_all_context_free (DisconnectAllContext *ctx)
|
||||
disconnect_bearers_context_free (DisconnectBearersContext *ctx)
|
||||
{
|
||||
g_free (ctx->bearer_path);
|
||||
if (ctx->current)
|
||||
g_object_unref (ctx->current);
|
||||
g_list_free_full (ctx->pending, g_object_unref);
|
||||
g_free (ctx);
|
||||
g_slice_free (DisconnectBearersContext, ctx);
|
||||
}
|
||||
|
||||
gboolean
|
||||
mm_bearer_list_disconnect_all_bearers_finish (MMBearerList *self,
|
||||
GAsyncResult *res,
|
||||
GError **error)
|
||||
mm_bearer_list_disconnect_bearers_finish (MMBearerList *self,
|
||||
GAsyncResult *res,
|
||||
GError **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
|
||||
disconnect_ready (MMBaseBearer *bearer,
|
||||
GAsyncResult *res,
|
||||
GTask *task)
|
||||
bearer_disconnect_ready (MMBaseBearer *bearer,
|
||||
GAsyncResult *res,
|
||||
GTask *task)
|
||||
{
|
||||
GError *error = NULL;
|
||||
|
||||
@@ -225,18 +227,16 @@ disconnect_ready (MMBaseBearer *bearer,
|
||||
g_object_unref (task);
|
||||
return;
|
||||
}
|
||||
|
||||
disconnect_next_bearer (task);
|
||||
disconnect_bearers_next (task);
|
||||
}
|
||||
|
||||
static void
|
||||
disconnect_next_bearer (GTask *task)
|
||||
disconnect_bearers_next (GTask *task)
|
||||
{
|
||||
DisconnectAllContext *ctx;
|
||||
DisconnectBearersContext *ctx;
|
||||
|
||||
ctx = g_task_get_task_data (task);
|
||||
if (ctx->current)
|
||||
g_clear_object (&ctx->current);
|
||||
ctx = g_task_get_task_data (task);
|
||||
g_clear_object (&ctx->current);
|
||||
|
||||
/* No more bearers? all done! */
|
||||
if (!ctx->pending) {
|
||||
@@ -249,30 +249,46 @@ disconnect_next_bearer (GTask *task)
|
||||
ctx->pending = g_list_delete_link (ctx->pending, ctx->pending);
|
||||
|
||||
mm_base_bearer_disconnect (ctx->current,
|
||||
(GAsyncReadyCallback)disconnect_ready,
|
||||
(GAsyncReadyCallback)bearer_disconnect_ready,
|
||||
task);
|
||||
}
|
||||
|
||||
void
|
||||
mm_bearer_list_disconnect_all_bearers (MMBearerList *self,
|
||||
GAsyncReadyCallback callback,
|
||||
gpointer user_data)
|
||||
static void
|
||||
build_connected_bearer_list (MMBaseBearer *bearer,
|
||||
DisconnectBearersContext *ctx)
|
||||
{
|
||||
DisconnectAllContext *ctx;
|
||||
GTask *task;
|
||||
if (!ctx->bearer_path ||
|
||||
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);
|
||||
/* Get a copy of the list */
|
||||
ctx->pending = g_list_copy_deep (self->priv->bearers,
|
||||
(GCopyFunc)g_object_ref,
|
||||
NULL);
|
||||
void
|
||||
mm_bearer_list_disconnect_bearers (MMBearerList *self,
|
||||
const gchar *bearer_path,
|
||||
GAsyncReadyCallback callback,
|
||||
gpointer user_data)
|
||||
{
|
||||
GTask *task;
|
||||
DisconnectBearersContext *ctx;
|
||||
|
||||
task = g_task_new (self, NULL, callback, user_data);
|
||||
g_task_set_task_data (task,
|
||||
ctx,
|
||||
(GDestroyNotify)disconnect_all_context_free);
|
||||
ctx = g_slice_new0 (DisconnectBearersContext);
|
||||
ctx->bearer_path = g_strdup (bearer_path); /* may be NULL if disconnecting all */
|
||||
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);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
@@ -79,12 +79,13 @@ MMBaseBearer *mm_bearer_list_find_by_profile_id (MMBearerList *self,
|
||||
MMBaseBearer *mm_bearer_list_find_by_apn_type (MMBearerList *self,
|
||||
MMBearerApnType apn_type);
|
||||
|
||||
void mm_bearer_list_disconnect_all_bearers (MMBearerList *self,
|
||||
GAsyncReadyCallback callback,
|
||||
gpointer user_data);
|
||||
gboolean mm_bearer_list_disconnect_all_bearers_finish (MMBearerList *self,
|
||||
GAsyncResult *res,
|
||||
GError **error);
|
||||
void mm_bearer_list_disconnect_bearers (MMBearerList *self,
|
||||
const gchar *bearer_path,
|
||||
GAsyncReadyCallback callback,
|
||||
gpointer user_data);
|
||||
gboolean mm_bearer_list_disconnect_bearers_finish (MMBearerList *self,
|
||||
GAsyncResult *res,
|
||||
GError **error);
|
||||
|
||||
#if defined WITH_SUSPEND_RESUME
|
||||
|
||||
|
@@ -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)
|
||||
|
||||
static void
|
||||
bearer_list_disconnect_all_bearers_ready (MMBearerList *list,
|
||||
GAsyncResult *res,
|
||||
GTask *task)
|
||||
bearer_list_disconnect_bearers_ready (MMBearerList *list,
|
||||
GAsyncResult *res,
|
||||
GTask *task)
|
||||
{
|
||||
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_object_unref (task);
|
||||
return;
|
||||
@@ -11442,9 +11442,10 @@ disabling_step (GTask *task)
|
||||
return;
|
||||
}
|
||||
if (ctx->self->priv->modem_bearer_list) {
|
||||
mm_bearer_list_disconnect_all_bearers (
|
||||
mm_bearer_list_disconnect_bearers (
|
||||
ctx->self->priv->modem_bearer_list,
|
||||
(GAsyncReadyCallback)bearer_list_disconnect_all_bearers_ready,
|
||||
NULL, /* all bearers */
|
||||
(GAsyncReadyCallback)bearer_list_disconnect_bearers_ready,
|
||||
task);
|
||||
return;
|
||||
}
|
||||
|
@@ -974,8 +974,6 @@ typedef struct {
|
||||
MmGdbusModemSimple *skeleton;
|
||||
GDBusMethodInvocation *invocation;
|
||||
gchar *bearer_path;
|
||||
GList *bearers;
|
||||
MMBaseBearer *current;
|
||||
} DisconnectionContext;
|
||||
|
||||
static void
|
||||
@@ -985,61 +983,24 @@ disconnection_context_free (DisconnectionContext *ctx)
|
||||
g_object_unref (ctx->invocation);
|
||||
g_object_unref (ctx->self);
|
||||
g_free (ctx->bearer_path);
|
||||
g_clear_object (&ctx->current);
|
||||
g_list_free_full (ctx->bearers, g_object_unref);
|
||||
g_slice_free (DisconnectionContext, ctx);
|
||||
}
|
||||
|
||||
static void disconnect_next_bearer (DisconnectionContext *ctx);
|
||||
|
||||
static void
|
||||
disconnect_ready (MMBaseBearer *bearer,
|
||||
GAsyncResult *res,
|
||||
DisconnectionContext *ctx)
|
||||
bearer_list_disconnect_bearers_ready (MMBearerList *bearer_list,
|
||||
GAsyncResult *res,
|
||||
DisconnectionContext *ctx)
|
||||
{
|
||||
GError *error = NULL;
|
||||
|
||||
if (!mm_base_bearer_disconnect_finish (bearer, res, &error)) {
|
||||
mm_obj_warn (ctx->self, "failed to disconnect bearer '%s': %s",
|
||||
mm_base_bearer_get_path (bearer), error->message);
|
||||
if (!mm_bearer_list_disconnect_bearers_finish (bearer_list, res, &error)) {
|
||||
mm_obj_warn (ctx->self, "failed to disconnect bearers: %s", error->message);
|
||||
g_dbus_method_invocation_take_error (ctx->invocation, error);
|
||||
disconnection_context_free (ctx);
|
||||
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) {
|
||||
} else {
|
||||
mm_obj_info (ctx->self, "all requested bearers disconnected");
|
||||
mm_gdbus_modem_simple_complete_disconnect (ctx->skeleton, ctx->invocation);
|
||||
disconnection_context_free (ctx);
|
||||
return;
|
||||
}
|
||||
|
||||
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));
|
||||
disconnection_context_free (ctx);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -1083,24 +1044,10 @@ disconnect_auth_ready (MMBaseModem *self,
|
||||
else
|
||||
mm_obj_info (self, "processing user request to disconnect modem: all bearers");
|
||||
|
||||
mm_bearer_list_foreach (list,
|
||||
(MMBearerListForeachFunc)build_connected_bearer_list,
|
||||
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);
|
||||
mm_bearer_list_disconnect_bearers (list,
|
||||
ctx->bearer_path,
|
||||
(GAsyncReadyCallback)bearer_list_disconnect_bearers_ready,
|
||||
ctx);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
|
Reference in New Issue
Block a user