mmcli: new Time-specific actions

This commit is contained in:
Aleksander Morgado
2012-03-06 17:45:41 +01:00
parent a3e6faeaca
commit 1df1fe4f4a
4 changed files with 282 additions and 0 deletions

View File

@@ -21,6 +21,7 @@ mmcli_SOURCES = \
mmcli-modem-simple.c \ mmcli-modem-simple.c \
mmcli-modem-location.c \ mmcli-modem-location.c \
mmcli-modem-messaging.c \ mmcli-modem-messaging.c \
mmcli-modem-time.c \
mmcli-bearer.c \ mmcli-bearer.c \
mmcli-sim.c \ mmcli-sim.c \
mmcli-sms.c mmcli-sms.c

262
cli/mmcli-modem-time.c Normal file
View File

@@ -0,0 +1,262 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* mmcli -- Control modem status & access information from the command line
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2012 Google, Inc.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
#include <glib.h>
#include <gio/gio.h>
#include <libmm-glib.h>
#include "mmcli.h"
#include "mmcli-common.h"
/* Context */
typedef struct {
MMManager *manager;
GCancellable *cancellable;
MMObject *object;
MMModemTime *modem_time;
} Context;
static Context *ctx;
/* Options */
static gboolean time_flag;
static GOptionEntry entries[] = {
{ "time", 0, 0, G_OPTION_ARG_NONE, &time_flag,
"Get current network time",
NULL
},
{ NULL }
};
GOptionGroup *
mmcli_modem_time_get_option_group (void)
{
GOptionGroup *group;
group = g_option_group_new ("time",
"Time options",
"Show Time options",
NULL,
NULL);
g_option_group_add_entries (group, entries);
return group;
}
gboolean
mmcli_modem_time_options_enabled (void)
{
static guint n_actions = 0;
static gboolean checked = FALSE;
if (checked)
return !!n_actions;
n_actions = (time_flag);
if (n_actions > 1) {
g_printerr ("error: too many Time actions requested\n");
exit (EXIT_FAILURE);
}
checked = TRUE;
return !!n_actions;
}
static void
context_free (Context *ctx)
{
if (!ctx)
return;
if (ctx->cancellable)
g_object_unref (ctx->cancellable);
if (ctx->modem_time)
g_object_unref (ctx->modem_time);
if (ctx->object)
g_object_unref (ctx->object);
if (ctx->manager)
g_object_unref (ctx->manager);
g_free (ctx);
}
static void
ensure_modem_time (void)
{
if (ctx->modem_time)
return;
g_printerr ("error: modem has no time capabilities\n");
exit (EXIT_FAILURE);
}
void
mmcli_modem_time_shutdown (void)
{
context_free (ctx);
}
static void
get_network_time_process_reply (gchar *time_string,
MMNetworkTimezone *timezone,
const GError *error)
{
gchar *offset = NULL;
gchar *dst_offset = NULL;
gchar *leap_seconds = NULL;
if (error)
g_printerr ("error: couldn't get current network time: '%s'\n",
error->message);
/* Not the best thing to do, as we may be doing _get() calls twice, but
* easiest to maintain */
#undef VALIDATE
#define VALIDATE(str) (str ? str : "not available")
if (timezone) {
if (mm_network_timezone_get_offset (timezone) != MM_NETWORK_TIMEZONE_OFFSET_UNKNOWN)
offset = g_strdup_printf ("%" G_GINT32_FORMAT, mm_network_timezone_get_offset (timezone));
if (mm_network_timezone_get_dst_offset (timezone) != MM_NETWORK_TIMEZONE_OFFSET_UNKNOWN)
dst_offset = g_strdup_printf ("%" G_GINT32_FORMAT, mm_network_timezone_get_dst_offset (timezone));
if (mm_network_timezone_get_leap_seconds (timezone) != MM_NETWORK_TIMEZONE_LEAP_SECONDS_UNKNOWN)
leap_seconds = g_strdup_printf ("%" G_GINT32_FORMAT, mm_network_timezone_get_leap_seconds (timezone));
}
g_print ("\n"
"%s\n"
" ----------------------------\n"
" Time | Current: '%s'\n"
" ----------------------------\n"
" Timezone | Offset: '%s'\n"
" | DST offset: '%s'\n"
" | Leap seconds: '%s'\n",
mm_modem_time_get_path (ctx->modem_time),
VALIDATE (time_string),
VALIDATE (offset),
VALIDATE (dst_offset),
VALIDATE (leap_seconds));
g_free (offset);
g_free (dst_offset);
g_free (leap_seconds);
g_free (time_string);
if (error)
exit (EXIT_FAILURE);
}
static void
get_network_time_ready (MMModemTime *modem_time,
GAsyncResult *result)
{
MMNetworkTimezone *timezone;
gchar *time_string;
GError *error = NULL;
time_string = mm_modem_time_get_network_time_finish (modem_time, result, &error);
timezone = mm_modem_time_get_network_timezone (modem_time);
get_network_time_process_reply (time_string, timezone, error);
mmcli_async_operation_done ();
}
static void
get_modem_ready (GObject *source,
GAsyncResult *result,
gpointer none)
{
ctx->object = mmcli_get_modem_finish (result, &ctx->manager);
ctx->modem_time = mm_object_get_modem_time (ctx->object);
ensure_modem_time ();
/* Request to get network time from the modem? */
if (time_flag) {
g_debug ("Asynchronously getting network time from the modem...");
mm_modem_time_get_network_time (ctx->modem_time,
ctx->cancellable,
(GAsyncReadyCallback)get_network_time_ready,
NULL);
return;
}
g_warn_if_reached ();
}
void
mmcli_modem_time_run_asynchronous (GDBusConnection *connection,
GCancellable *cancellable)
{
/* Initialize context */
ctx = g_new0 (Context, 1);
if (cancellable)
ctx->cancellable = g_object_ref (cancellable);
/* Get proper modem */
mmcli_get_modem (connection,
mmcli_get_common_modem_string (),
cancellable,
(GAsyncReadyCallback)get_modem_ready,
NULL);
}
void
mmcli_modem_time_run_synchronous (GDBusConnection *connection)
{
GError *error = NULL;
/* Initialize context */
ctx = g_new0 (Context, 1);
ctx->object = mmcli_get_modem_sync (connection,
mmcli_get_common_modem_string (),
&ctx->manager);
ctx->modem_time = mm_object_get_modem_time (ctx->object);
ensure_modem_time ();
/* Request to get network time from the modem? */
if (time_flag) {
gchar *time_string;
MMNetworkTimezone *timezone;
g_debug ("Synchronously getting network time from the modem...");
time_string = mm_modem_time_get_network_time_sync (ctx->modem_time,
NULL,
&error);
timezone = mm_modem_time_get_network_timezone (ctx->modem_time);
get_network_time_process_reply (time_string, timezone, error);
return;
}
g_warn_if_reached ();
}

View File

@@ -192,6 +192,8 @@ main (gint argc, gchar **argv)
mmcli_modem_location_get_option_group ()); mmcli_modem_location_get_option_group ());
g_option_context_add_group (context, g_option_context_add_group (context,
mmcli_modem_messaging_get_option_group ()); mmcli_modem_messaging_get_option_group ());
g_option_context_add_group (context,
mmcli_modem_time_get_option_group ());
g_option_context_add_group (context, g_option_context_add_group (context,
mmcli_sim_get_option_group ()); mmcli_sim_get_option_group ());
g_option_context_add_group (context, g_option_context_add_group (context,
@@ -295,6 +297,13 @@ main (gint argc, gchar **argv)
else else
mmcli_modem_messaging_run_synchronous (connection); mmcli_modem_messaging_run_synchronous (connection);
} }
/* Modem Time options? */
else if (mmcli_modem_time_options_enabled ()) {
if (async_flag)
mmcli_modem_time_run_asynchronous (connection, cancellable);
else
mmcli_modem_time_run_synchronous (connection);
}
/* Modem options? /* Modem options?
* NOTE: let this check be always the last one, as other groups also need * NOTE: let this check be always the last one, as other groups also need
* having a modem specified, and therefore if -m is set, modem options * having a modem specified, and therefore if -m is set, modem options
@@ -327,6 +336,8 @@ main (gint argc, gchar **argv)
mmcli_modem_location_shutdown (); mmcli_modem_location_shutdown ();
} else if (mmcli_modem_messaging_options_enabled ()) { } else if (mmcli_modem_messaging_options_enabled ()) {
mmcli_modem_messaging_shutdown (); mmcli_modem_messaging_shutdown ();
} else if (mmcli_modem_time_options_enabled ()) {
mmcli_modem_time_shutdown ();
} else if (mmcli_sim_options_enabled ()) { } else if (mmcli_sim_options_enabled ()) {
mmcli_sim_shutdown (); mmcli_sim_shutdown ();
} else if (mmcli_bearer_options_enabled ()) { } else if (mmcli_bearer_options_enabled ()) {

View File

@@ -84,6 +84,14 @@ void mmcli_modem_messaging_run_asynchronous (GDBusConnection *connect
void mmcli_modem_messaging_run_synchronous (GDBusConnection *connection); void mmcli_modem_messaging_run_synchronous (GDBusConnection *connection);
void mmcli_modem_messaging_shutdown (void); void mmcli_modem_messaging_shutdown (void);
/* Time group */
GOptionGroup *mmcli_modem_time_get_option_group (void);
gboolean mmcli_modem_time_options_enabled (void);
void mmcli_modem_time_run_asynchronous (GDBusConnection *connection,
GCancellable *cancellable);
void mmcli_modem_time_run_synchronous (GDBusConnection *connection);
void mmcli_modem_time_shutdown (void);
/* Bearer group */ /* Bearer group */
GOptionGroup *mmcli_bearer_get_option_group (void); GOptionGroup *mmcli_bearer_get_option_group (void);
gboolean mmcli_bearer_options_enabled (void); gboolean mmcli_bearer_options_enabled (void);