cli: added SMS-specific actions

This commit is contained in:
Aleksander Morgado
2012-03-15 14:15:01 +01:00
parent 2021ba80fd
commit 6ff1543c32
4 changed files with 273 additions and 1 deletions
+2 -1
View File
@@ -21,7 +21,8 @@ mmcli_SOURCES = \
mmcli-modem-simple.c \
mmcli-modem-location.c \
mmcli-bearer.c \
mmcli-sim.c
mmcli-sim.c \
mmcli-sms.c
mmcli_LDADD = \
$(MMCLI_LIBS) \
+252
View File
@@ -0,0 +1,252 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* mmcli -- Control sms 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;
MMObject *object;
GCancellable *cancellable;
MMSms *sms;
} Context;
static Context *ctx;
/* Options */
static gboolean info_flag; /* set when no action found */
static gboolean send_flag;
static GOptionEntry entries[] = {
{ "send", 0, 0, G_OPTION_ARG_NONE, &send_flag,
"Send SMS.",
NULL,
},
{ NULL }
};
GOptionGroup *
mmcli_sms_get_option_group (void)
{
GOptionGroup *group;
/* Status options */
group = g_option_group_new ("sms",
"SMS options",
"Show SMS options",
NULL,
NULL);
g_option_group_add_entries (group, entries);
return group;
}
gboolean
mmcli_sms_options_enabled (void)
{
static guint n_actions = 0;
static gboolean checked = FALSE;
if (checked)
return !!n_actions;
n_actions = (send_flag);
if (n_actions == 0 && mmcli_get_common_sms_string ()) {
/* default to info */
info_flag = TRUE;
n_actions++;
}
if (n_actions > 1) {
g_printerr ("error: too many SMS actions requested\n");
exit (EXIT_FAILURE);
}
if (info_flag)
mmcli_force_sync_operation ();
checked = TRUE;
return !!n_actions;
}
static void
context_free (Context *ctx)
{
if (!ctx)
return;
if (ctx->cancellable)
g_object_unref (ctx->cancellable);
if (ctx->sms)
g_object_unref (ctx->sms);
if (ctx->object)
g_object_unref (ctx->object);
if (ctx->manager)
g_object_unref (ctx->manager);
g_free (ctx);
}
void
mmcli_sms_shutdown (void)
{
context_free (ctx);
}
static void
print_sms_info (MMSms *sms)
{
/* 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 : "unknown")
g_print ("SMS '%s'\n",
mm_sms_get_path (sms));
g_print (" -------------------------\n"
" Content | text: '%s'\n"
" | number: '%s'\n"
" -------------------------\n"
" Properties | state: '%s'\n"
" | smsc: '%s'\n"
" | timestamp: '%s'\n"
" | validity: '%u'\n"
" | class: '%u'\n",
VALIDATE (mm_sms_get_text (sms)),
VALIDATE (mm_sms_get_number (sms)),
mm_sms_state_get_string (mm_sms_get_state (sms)),
VALIDATE (mm_sms_get_smsc (sms)),
VALIDATE (mm_sms_get_timestamp (sms)),
mm_sms_get_validity (sms),
mm_sms_get_class (sms));
}
static void
send_process_reply (gboolean result,
const GError *error)
{
if (!result) {
g_printerr ("error: couldn't send the SMS: '%s'\n",
error ? error->message : "unknown error");
exit (EXIT_FAILURE);
}
g_print ("successfully sent the SMS\n");
}
static void
send_ready (MMSms *sms,
GAsyncResult *result,
gpointer nothing)
{
gboolean operation_result;
GError *error = NULL;
operation_result = mm_sms_send_finish (sms, result, &error);
send_process_reply (operation_result, error);
mmcli_async_operation_done ();
}
static void
get_sms_ready (GObject *source,
GAsyncResult *result,
gpointer none)
{
ctx->sms = mmcli_get_sms_finish (result,
&ctx->manager,
&ctx->object);
if (info_flag)
g_assert_not_reached ();
/* Requesting to send the SMS? */
if (send_flag) {
mm_sms_send (ctx->sms,
ctx->cancellable,
(GAsyncReadyCallback)send_ready,
NULL);
return;
}
g_warn_if_reached ();
}
void
mmcli_sms_run_asynchronous (GDBusConnection *connection,
GCancellable *cancellable)
{
/* Initialize context */
ctx = g_new0 (Context, 1);
if (cancellable)
ctx->cancellable = g_object_ref (cancellable);
/* Get proper sms */
mmcli_get_sms (connection,
mmcli_get_common_sms_string (),
cancellable,
(GAsyncReadyCallback)get_sms_ready,
NULL);
}
void
mmcli_sms_run_synchronous (GDBusConnection *connection)
{
GError *error = NULL;
/* Initialize context */
ctx = g_new0 (Context, 1);
ctx->sms = mmcli_get_sms_sync (connection,
mmcli_get_common_sms_string (),
&ctx->manager,
&ctx->object);
/* Request to get info from SMS? */
if (info_flag) {
g_debug ("Printing SMS info...");
print_sms_info (ctx->sms);
return;
}
/* Requesting to send the SMS? */
if (send_flag) {
gboolean operation_result;
operation_result = mm_sms_send_sync (ctx->sms,
NULL,
&error);
send_process_reply (operation_result, error);
return;
}
g_warn_if_reached ();
}
+11
View File
@@ -194,6 +194,8 @@ main (gint argc, gchar **argv)
mmcli_sim_get_option_group ());
g_option_context_add_group (context,
mmcli_bearer_get_option_group ());
g_option_context_add_group (context,
mmcli_sms_get_option_group ());
g_option_context_add_main_entries (context, main_entries, NULL);
g_option_context_parse (context, &argc, &argv, NULL);
g_option_context_free (context);
@@ -249,6 +251,13 @@ main (gint argc, gchar **argv)
else
mmcli_bearer_run_synchronous (connection);
}
/* Sms options? */
else if (mmcli_sms_options_enabled ()) {
if (async_flag)
mmcli_sms_run_asynchronous (connection, cancellable);
else
mmcli_sms_run_synchronous (connection);
}
/* Modem 3GPP options? */
else if (mmcli_modem_3gpp_options_enabled ()) {
if (async_flag)
@@ -311,6 +320,8 @@ main (gint argc, gchar **argv)
mmcli_sim_shutdown ();
} else if (mmcli_bearer_options_enabled ()) {
mmcli_bearer_shutdown ();
} else if (mmcli_sms_options_enabled ()) {
mmcli_sms_shutdown ();
} else if (mmcli_modem_options_enabled ()) {
mmcli_modem_shutdown ();
}
+8
View File
@@ -92,4 +92,12 @@ void mmcli_sim_run_asynchronous (GDBusConnection *connection,
void mmcli_sim_run_synchronous (GDBusConnection *connection);
void mmcli_sim_shutdown (void);
/* SMS group */
GOptionGroup *mmcli_sms_get_option_group (void);
gboolean mmcli_sms_options_enabled (void);
void mmcli_sms_run_asynchronous (GDBusConnection *connection,
GCancellable *cancellable);
void mmcli_sms_run_synchronous (GDBusConnection *connection);
void mmcli_sms_shutdown (void);
#endif /* __MMCLI_H__ */