cli: port Manager actions to use the new libmm-glib

The original command line interface was written based on a hand-made libmm. This
commit ports the Manager interface handling to the new gdbus-codegen-based
libmm-glib.
This commit is contained in:
Aleksander Morgado
2011-10-27 17:12:20 +02:00
parent c37949d7ba
commit 9e241b7062
3 changed files with 176 additions and 119 deletions

View File

@@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright (C) 2011 Aleksander Morgado <aleksander@gnu.org> * Copyright (C) 2011 Aleksander Morgado <aleksander@gnu.org>
* Copyright (C) 2011 Google, Inc.
*/ */
#include "config.h" #include "config.h"
@@ -35,8 +36,9 @@
/* Context */ /* Context */
typedef struct { typedef struct {
MMManager *manager; MMManager *manager;
GCancellable *cancellable;
} Context; } Context;
static Context ctxt; static Context *ctx;
/* Options */ /* Options */
static gboolean list_modems_flag; static gboolean list_modems_flag;
@@ -99,23 +101,51 @@ mmcli_manager_options_enabled (void)
} }
static void static void
init (GDBusConnection *connection) context_free (Context *ctx)
{ {
GError *error = NULL; if (!ctx)
return;
/* Create new manager */ if (ctx->manager)
ctxt.manager = mm_manager_new (connection, NULL, &error); g_object_unref (ctx->manager);
if (!ctxt.manager) { if (ctx->cancellable)
g_printerr ("error: couldn't create manager: %s\n", g_object_unref (ctx->cancellable);
error ? error->message : "unknown error"); g_free (ctx);
exit (EXIT_FAILURE);
}
} }
void void
mmcli_manager_shutdown (void) mmcli_manager_shutdown (void)
{ {
g_object_unref (ctxt.manager); context_free (ctx);
}
static void
set_logging_process_reply (gboolean result,
const GError *error)
{
if (!result) {
g_printerr ("error: couldn't set logging level: '%s'\n",
error ? error->message : "unknown error");
exit (EXIT_FAILURE);
}
g_print ("Successfully set logging level\n");
}
static void
set_logging_ready (MMManager *manager,
GAsyncResult *result,
gpointer nothing)
{
gboolean operation_result;
GError *error = NULL;
operation_result = mm_manager_set_logging_finish (manager,
result,
&error);
set_logging_process_reply (operation_result, error);
mmcli_async_operation_done ();
} }
static void static void
@@ -148,119 +178,143 @@ scan_devices_ready (MMManager *manager,
} }
static void static void
enumerate_devices_process_reply (const GStrv paths, device_added (GDBusObjectManager *manager,
const GError *error) GDBusObject *object)
{ {
if (error) { g_print ("Added modem [TODO: Print path]\n");
g_printerr ("error: couldn't enumerate devices: '%s'\n", fflush (stdout);
error ? error->message : "unknown error");
exit (EXIT_FAILURE);
}
g_print ("\n");
if (!paths) {
g_print ("No modems were found");
} else {
guint i;
for (i = 0; paths[i]; i++) {
g_print ("%s: '%s'\n",
"Found modem",
paths[i]);
}
}
g_print ("\n");
} }
static void static void
enumerate_devices_ready (MMManager *manager, device_removed (GDBusObjectManager *manager,
GAsyncResult *result, GDBusObject *object)
gpointer nothing)
{ {
GStrv paths; g_print ("Removed modem [TODO: Print path]\n");
GError *error = NULL; fflush (stdout);
}
paths = mm_manager_enumerate_devices_finish (manager, result, &error); static void
enumerate_devices_process_reply (paths, error); list_current_modems (MMManager *manager)
g_strfreev (paths); {
GList *modems;
modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (ctx->manager));
g_print ("\n");
if (!modems)
g_print ("No modems were found\n");
else {
GList *l;
g_print ("Found %u modems\n", g_list_length (modems));
for (l = modems; l; l = g_list_next (l)) {
g_print ("\t[TODO: Print path]\n");
g_object_unref (l->data);
}
g_list_free (modems);
}
}
static void
cancelled (GCancellable *cancellable)
{
mmcli_async_operation_done (); mmcli_async_operation_done ();
} }
static void static void
device_added (MMManager *manager, manager_new_ready (GObject *source,
const gchar *path) GAsyncResult *result,
gpointer none)
{ {
g_print ("%s: '%s'\n", gchar *name_owner;
"Added modem", GError *error = NULL;
path);
fflush (stdout);
}
static void ctx->manager = mm_manager_new_finish (result, &error);
device_removed (MMManager *manager, if (!ctx->manager) {
const gchar *path) g_printerr ("error: couldn't create manager: %s\n",
{ error ? error->message : "unknown error");
g_print ("%s: '%s'\n",
"Removed modem",
path);
fflush (stdout);
}
gboolean
mmcli_manager_run_asynchronous (GDBusConnection *connection,
GCancellable *cancellable)
{
gboolean keep_loop = FALSE;
if (set_logging_str) {
g_printerr ("error: logging level cannot be set asynchronously\n");
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
/* Initialize context */ name_owner = g_dbus_object_manager_client_get_name_owner (G_DBUS_OBJECT_MANAGER_CLIENT (ctx->manager));
init (connection); if (!name_owner) {
g_printerr ("error: couldn't find the ModemManager process in the bus\n");
exit (EXIT_FAILURE);
}
g_debug ("ModemManager process found at '%s'", name_owner);
g_free (name_owner);
/* Request to set log level? */
if (set_logging_str) {
mm_manager_set_logging (ctx->manager,
set_logging_str,
ctx->cancellable,
(GAsyncReadyCallback)set_logging_ready,
NULL);
return;
}
/* Request to scan modems? */ /* Request to scan modems? */
if (scan_modems_flag) { if (scan_modems_flag) {
mm_manager_scan_devices_async (ctxt.manager, mm_manager_scan_devices (ctx->manager,
cancellable, ctx->cancellable,
(GAsyncReadyCallback)scan_devices_ready, (GAsyncReadyCallback)scan_devices_ready,
NULL); NULL);
return keep_loop; return;
} }
/* Request to monitor modems? */ /* Request to monitor modems? */
if (monitor_modems_flag) { if (monitor_modems_flag) {
g_signal_connect (ctxt.manager, g_signal_connect (ctx->manager,
"device-added", "object-added",
G_CALLBACK (device_added), G_CALLBACK (device_added),
NULL); NULL);
g_signal_connect (ctxt.manager, g_signal_connect (ctx->manager,
"device-removed", "object-removed",
G_CALLBACK (device_removed), G_CALLBACK (device_removed),
NULL); NULL);
list_current_modems (ctx->manager);
/* We need to keep the loop */ /* If we get cancelled, operation done */
keep_loop = TRUE; g_cancellable_connect (ctx->cancellable,
G_CALLBACK (cancelled),
NULL,
NULL);
return;
} }
/* Request to list modems? */ /* Request to list modems? */
if (monitor_modems_flag || list_modems_flag) { if (list_modems_flag) {
mm_manager_enumerate_devices_async (ctxt.manager, list_current_modems (ctx->manager);
cancellable, mmcli_async_operation_done ();
(GAsyncReadyCallback)enumerate_devices_ready, return;
NULL);
return keep_loop;
} }
g_warn_if_reached (); g_warn_if_reached ();
return FALSE; }
void
mmcli_manager_run_asynchronous (GDBusConnection *connection,
GCancellable *cancellable)
{
/* Initialize context */
ctx = g_new0 (Context, 1);
if (cancellable)
ctx->cancellable = g_object_ref (cancellable);
/* Create a new Manager object asynchronously */
mm_manager_new (connection,
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
cancellable,
(GAsyncReadyCallback)manager_new_ready,
NULL);
} }
void void
mmcli_manager_run_synchronous (GDBusConnection *connection) mmcli_manager_run_synchronous (GDBusConnection *connection)
{ {
gchar *name_owner;
GError *error = NULL; GError *error = NULL;
if (monitor_modems_flag) { if (monitor_modems_flag) {
@@ -269,32 +323,35 @@ mmcli_manager_run_synchronous (GDBusConnection *connection)
} }
/* Initialize context */ /* Initialize context */
init (connection); ctx = g_new0 (Context, 1);
ctx->manager = mm_manager_new_sync (connection,
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
NULL,
&error);
if (!ctx->manager) {
g_printerr ("error: couldn't create manager: %s\n",
error ? error->message : "unknown error");
exit (EXIT_FAILURE);
}
name_owner = g_dbus_object_manager_client_get_name_owner (G_DBUS_OBJECT_MANAGER_CLIENT (ctx->manager));
if (!name_owner) {
g_printerr ("error: couldn't find the ModemManager process in the bus\n");
exit (EXIT_FAILURE);
}
g_debug ("ModemManager process found at '%s'", name_owner);
g_free (name_owner);
/* Request to set log level? */ /* Request to set log level? */
if (set_logging_str) { if (set_logging_str) {
MMLogLevel level; gboolean result;
if (g_strcmp0 (set_logging_str, "ERR") == 0) result = mm_manager_set_logging_sync (ctx->manager,
level = MM_LOG_LEVEL_ERROR; set_logging_str,
else if (g_strcmp0 (set_logging_str, "WARN") == 0) NULL,
level = MM_LOG_LEVEL_WARNING; &error);
else if (g_strcmp0 (set_logging_str, "INFO") == 0) set_logging_process_reply (result, error);
level = MM_LOG_LEVEL_INFO;
else if (g_strcmp0 (set_logging_str, "DEBUG") == 0)
level = MM_LOG_LEVEL_DEBUG;
else {
g_printerr ("error: couldn't set unknown logging level: '%s'\n",
set_logging_str);
exit (EXIT_FAILURE);
}
if (mm_manager_set_logging (ctxt.manager, level, &error)) {
g_printerr ("error: couldn't set logging level: '%s'\n",
error ? error->message : "unknown error");
exit (EXIT_FAILURE);
}
g_print ("successfully set log level '%s'\n", set_logging_str);
return; return;
} }
@@ -302,18 +359,16 @@ mmcli_manager_run_synchronous (GDBusConnection *connection)
if (scan_modems_flag) { if (scan_modems_flag) {
gboolean result; gboolean result;
result = mm_manager_scan_devices (ctxt.manager, &error); result = mm_manager_scan_devices_sync (ctx->manager,
NULL,
&error);
scan_devices_process_reply (result, error); scan_devices_process_reply (result, error);
return; return;
} }
/* Request to list modems? */ /* Request to list modems? */
if (list_modems_flag) { if (list_modems_flag) {
GStrv paths; list_current_modems (ctx->manager);
paths = mm_manager_enumerate_devices (ctxt.manager, &error);
enumerate_devices_process_reply (paths, error);
g_strfreev (paths);
return; return;
} }

View File

@@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright (C) 2011 Aleksander Morgado <aleksander@gnu.org> * Copyright (C) 2011 Aleksander Morgado <aleksander@gnu.org>
* Copyright (C) 2011 Google, Inc.
*/ */
#include "config.h" #include "config.h"
@@ -28,7 +29,7 @@
#include <glib.h> #include <glib.h>
#include <gio/gio.h> #include <gio/gio.h>
#include <libmm.h> #include <libmm-glib.h>
#include "mmcli.h" #include "mmcli.h"
@@ -37,7 +38,6 @@
/* Globals */ /* Globals */
static GMainLoop *loop; static GMainLoop *loop;
static gboolean keep_loop;
static GCancellable *cancellable; static GCancellable *cancellable;
/* Context */ /* Context */
@@ -98,8 +98,7 @@ mmcli_async_operation_done (void)
cancellable = NULL; cancellable = NULL;
} }
if (!keep_loop) g_main_loop_quit (loop);
g_main_loop_quit (loop);
} }
gint gint
@@ -147,9 +146,12 @@ main (gint argc, gchar **argv)
/* Manager options? */ /* Manager options? */
if (mmcli_manager_options_enabled ()) { if (mmcli_manager_options_enabled ()) {
if (async_flag) if (async_flag)
keep_loop = mmcli_manager_run_asynchronous (connection, cancellable); mmcli_manager_run_asynchronous (connection, cancellable);
else else
mmcli_manager_run_synchronous (connection); mmcli_manager_run_synchronous (connection);
} else {
g_printerr ("error: no actions specified\n");
exit (EXIT_FAILURE);
} }
/* Run loop only in async operations */ /* Run loop only in async operations */

View File

@@ -29,7 +29,7 @@ void mmcli_async_operation_done (void);
/* Manager group */ /* Manager group */
GOptionGroup *mmcli_manager_get_option_group (void); GOptionGroup *mmcli_manager_get_option_group (void);
gboolean mmcli_manager_options_enabled (void); gboolean mmcli_manager_options_enabled (void);
gboolean mmcli_manager_run_asynchronous (GDBusConnection *connection, void mmcli_manager_run_asynchronous (GDBusConnection *connection,
GCancellable *cancellable); GCancellable *cancellable);
void mmcli_manager_run_synchronous (GDBusConnection *connection); void mmcli_manager_run_synchronous (GDBusConnection *connection);
void mmcli_manager_shutdown (void); void mmcli_manager_shutdown (void);