diff options
author | Riccardo Vangelisti <riccardo.vangelisti@sadel.it> | 2015-04-27 11:41:59 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2015-08-02 10:39:12 +0200 |
commit | c3bec1537b96b3b3071c2de787126ba11479ed88 (patch) | |
tree | ef976c104cc2d071cef97f7783fbf65829618f3b | |
parent | 0bf4ad6b66b1ea7626df28dc2417686b9e2d03f7 (diff) |
mmcli: added Modem.Voice and Call support
-rw-r--r-- | cli/Makefile.am | 4 | ||||
-rw-r--r-- | cli/mmcli-call.c | 357 | ||||
-rw-r--r-- | cli/mmcli-common.c | 281 | ||||
-rw-r--r-- | cli/mmcli-common.h | 15 | ||||
-rw-r--r-- | cli/mmcli-modem-voice.c | 438 | ||||
-rw-r--r-- | cli/mmcli.c | 22 | ||||
-rw-r--r-- | cli/mmcli.h | 16 |
7 files changed, 1132 insertions, 1 deletions
diff --git a/cli/Makefile.am b/cli/Makefile.am index 1d36d91f..51d71c42 100644 --- a/cli/Makefile.am +++ b/cli/Makefile.am @@ -21,13 +21,15 @@ mmcli_SOURCES = \ mmcli-modem-simple.c \ mmcli-modem-location.c \ mmcli-modem-messaging.c \ + mmcli-modem-voice.c \ mmcli-modem-time.c \ mmcli-modem-firmware.c \ mmcli-modem-signal.c \ mmcli-modem-oma.c \ mmcli-bearer.c \ mmcli-sim.c \ - mmcli-sms.c + mmcli-sms.c \ + mmcli-call.c mmcli_LDADD = \ $(MMCLI_LIBS) \ diff --git a/cli/mmcli-call.c b/cli/mmcli-call.c new file mode 100644 index 00000000..8137b4fd --- /dev/null +++ b/cli/mmcli-call.c @@ -0,0 +1,357 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * mmcli -- Control call 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) 2015 Riccardo Vangelisti <riccardo.vangelisti@sadel.it> + */ + +#include "config.h" + +#include <stdio.h> +#include <stdlib.h> +#include <locale.h> +#include <string.h> + +#include <glib.h> +#include <gio/gio.h> + +#define _LIBMM_INSIDE_MMCLI +#include <libmm-glib.h> + +#include "mmcli.h" +#include "mmcli-common.h" + +/* Context */ +typedef struct { + MMManager *manager; + MMObject *object; + GCancellable *cancellable; + MMCall *call; +} Context; +static Context *ctx; + +/* Options */ +static gboolean info_flag; /* set when no action found */ +static gboolean start_flag; +static gboolean accept_flag; +static gboolean hangup_flag; + +static GOptionEntry entries[] = { + { "start", 0, 0, G_OPTION_ARG_NONE, &start_flag, + "Start the call.", + NULL, + }, + { "accept", 0, 0, G_OPTION_ARG_NONE, &accept_flag, + "Accept the incoming call", + NULL, + }, + { "hangup", 0, 0, G_OPTION_ARG_NONE, &hangup_flag, + "Hangup the call", + NULL, + }, + { NULL } +}; + +GOptionGroup * +mmcli_call_get_option_group (void) +{ + GOptionGroup *group; + + /* Status options */ + group = g_option_group_new ("call", + "Call options", + "Show call options", + NULL, + NULL); + g_option_group_add_entries (group, entries); + + return group; +} + +gboolean +mmcli_call_options_enabled (void) +{ + static guint n_actions = 0; + static gboolean checked = FALSE; + + if (checked) + return !!n_actions; + + n_actions = (start_flag + + accept_flag + + hangup_flag ); + + if (n_actions == 0 && mmcli_get_common_call_string ()) { + /* default to info */ + info_flag = TRUE; + n_actions++; + } + + if (n_actions > 1) { + g_printerr ("error: too many call 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->call) + g_object_unref (ctx->call); + if (ctx->object) + g_object_unref (ctx->object); + if (ctx->manager) + g_object_unref (ctx->manager); + g_free (ctx); +} + +void +mmcli_call_shutdown (void) +{ + context_free (ctx); +} + +static void +print_call_info (MMCall *call) +{ + /* 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 ("CALL '%s'\n", mm_call_get_path (call)); + g_print (" -------------------------------\n" + " Global | number: '%s'\n", VALIDATE (mm_call_get_number (call))); + g_print (" | direction: '%s'\n", mm_call_direction_get_string( mm_call_get_direction (call) ) ); + + g_print (" -------------------------------\n" + " Properties | state: '%s'\n", mm_call_state_get_string (mm_call_get_state (call))); + + if (mm_call_get_state_reason(call) != MM_CALL_STATE_REASON_UNKNOWN) + g_print (" | state reason: '%s'\n", + mm_call_state_reason_get_string(mm_call_get_state_reason(call))); +} + +static void +start_process_reply (gboolean result, + const GError *error) +{ + if (!result) { + g_printerr ("error: couldn't start the call: '%s'\n", + error ? error->message : "unknown error"); + exit (EXIT_FAILURE); + } + + g_print ("successfully start the call\n"); +} + +static void +start_ready (MMCall *call, + GAsyncResult *result, + gpointer nothing) +{ + gboolean operation_result; + GError *error = NULL; + + operation_result = mm_call_start_finish (call, result, &error); + start_process_reply (operation_result, error); + + mmcli_async_operation_done (); +} + +static void +accept_process_reply (gboolean result, + const GError *error) +{ + if (!result) { + g_printerr ("error: couldn't accept the call: '%s'\n", + error ? error->message : "unknown error"); + exit (EXIT_FAILURE); + } + + g_print ("successfully accepted the call\n"); +} + +static void +accept_ready (MMCall *call, + GAsyncResult *result, + gpointer nothing) +{ + gboolean operation_result; + GError *error = NULL; + + operation_result = mm_call_accept_finish (call, result, &error); + accept_process_reply (operation_result, error); + + mmcli_async_operation_done (); +} + +static void +hangup_process_reply (gboolean result, + const GError *error) +{ + if (!result) { + g_printerr ("error: couldn't hangup the call: '%s'\n", + error ? error->message : "unknown error"); + exit (EXIT_FAILURE); + } + + g_print ("successfully hungup'd the call\n"); +} + +static void +hangup_ready (MMCall *call, + GAsyncResult *result, + gpointer nothing) +{ + gboolean operation_result; + GError *error = NULL; + + operation_result = mm_call_hangup_finish (call, result, &error); + hangup_process_reply (operation_result, error); + + mmcli_async_operation_done (); +} + +static void +get_call_ready (GObject *source, + GAsyncResult *result, + gpointer none) +{ + ctx->call = mmcli_get_call_finish (result, + &ctx->manager, + &ctx->object); + /* Setup operation timeout */ + mmcli_force_operation_timeout (G_DBUS_PROXY (ctx->call)); + + if (info_flag) + g_assert_not_reached (); + + /* Requesting to start the call? */ + if (start_flag) { + mm_call_start(ctx->call, + ctx->cancellable, + (GAsyncReadyCallback)start_ready, + NULL); + return; + } + + /* Requesting to accept the call? */ + if (accept_flag) { + mm_call_accept(ctx->call, + ctx->cancellable, + (GAsyncReadyCallback)accept_ready, + NULL); + return; + } + + /* Requesting to hangup the call? */ + if (hangup_flag) { + mm_call_hangup(ctx->call, + ctx->cancellable, + (GAsyncReadyCallback)hangup_ready, + NULL); + return; + } + + g_warn_if_reached (); +} + +void +mmcli_call_run_asynchronous (GDBusConnection *connection, + GCancellable *cancellable) +{ + /* Initialize context */ + ctx = g_new0 (Context, 1); + if (cancellable) + ctx->cancellable = g_object_ref (cancellable); + + /* Get proper call */ + mmcli_get_call (connection, + mmcli_get_common_call_string (), + cancellable, + (GAsyncReadyCallback)get_call_ready, + NULL); +} + +void +mmcli_call_run_synchronous (GDBusConnection *connection) +{ + GError *error = NULL; + + /* Initialize context */ + ctx = g_new0 (Context, 1); + ctx->call = mmcli_get_call_sync (connection, + mmcli_get_common_call_string (), + &ctx->manager, + &ctx->object); + + /* Setup operation timeout */ + mmcli_force_operation_timeout (G_DBUS_PROXY (ctx->call)); + + /* Request to get info from CALL? */ + if (info_flag) { + g_debug ("Printing call info..."); + print_call_info (ctx->call); + return; + } + + /* Requesting to start the call? */ + if (start_flag) { + gboolean operation_result; + + operation_result = mm_call_start_sync (ctx->call, + NULL, + &error); + start_process_reply (operation_result, error); + return; + } + + /* Requesting to accept the call? */ + if (accept_flag) { + gboolean operation_result; + + operation_result = mm_call_accept_sync (ctx->call, + NULL, + &error); + accept_process_reply (operation_result, error); + return; + } + + /* Requesting to hangup the call? */ + if (hangup_flag) { + gboolean operation_result; + + operation_result = mm_call_hangup_sync (ctx->call, + NULL, + &error); + hangup_process_reply (operation_result, error); + return; + } + + g_warn_if_reached (); +} diff --git a/cli/mmcli-common.c b/cli/mmcli-common.c index 2d7f66ea..0b1df167 100644 --- a/cli/mmcli-common.c +++ b/cli/mmcli-common.c @@ -1071,11 +1071,282 @@ mmcli_get_state_reason_string (MMModemStateChangeReason reason) return NULL; } +typedef struct { + GSimpleAsyncResult *result; + GCancellable *cancellable; + gchar *call_path; + MMManager *manager; + GList *modems; + MMObject *current; + MMCall *call; +} GetVoiceContext; + +static void +get_voice_context_free (GetVoiceContext *ctx) +{ + if (ctx->current) + g_object_unref (ctx->current); + if (ctx->cancellable) + g_object_unref (ctx->cancellable); + if (ctx->manager) + g_object_unref (ctx->manager); + if (ctx->call) + g_object_unref (ctx->call); + g_list_free_full (ctx->modems, (GDestroyNotify) g_object_unref); + g_free (ctx->call_path); + g_free (ctx); +} + +static void +get_voice_context_complete (GetVoiceContext *ctx) +{ + g_simple_async_result_complete (ctx->result); + g_object_unref (ctx->result); + ctx->result = NULL; +} + + +static void look_for_call_in_modem (GetVoiceContext *ctx); + +static MMCall * +find_call_in_list (GList *list, + const gchar *call_path) +{ + GList *l; + + for (l = list; l; l = g_list_next (l)) { + MMCall *call = MM_CALL (l->data); + + if (g_str_equal (mm_call_get_path (call), call_path)) { + g_debug ("Call found at '%s'\n", call_path); + return g_object_ref (call); + } + } + + return NULL; +} + +static void +list_call_ready (MMModemVoice *modem, + GAsyncResult *res, + GetVoiceContext *ctx) +{ + GList *call_list; + GError *error = NULL; + + call_list = mm_modem_voice_list_call_finish (modem, res, &error); + if (error) { + g_printerr ("error: couldn't list call at '%s': '%s'\n", + mm_modem_voice_get_path (modem), + error->message); + exit (EXIT_FAILURE); + } + + ctx->call = find_call_in_list (call_list, ctx->call_path); + g_list_free_full (call_list, (GDestroyNotify) g_object_unref); + + /* Found! */ + if (ctx->call) { + g_simple_async_result_set_op_res_gpointer ( + ctx->result, + ctx, + (GDestroyNotify)get_voice_context_free); + get_voice_context_complete (ctx); + return; + } + + /* Not found, try with next modem */ + look_for_call_in_modem (ctx); +} + +static void +look_for_call_in_modem (GetVoiceContext *ctx) +{ + MMModemVoice *modem; + + if (!ctx->modems) { + g_printerr ("error: couldn't find call at '%s': 'not found in any modem'\n", + ctx->call_path); + exit (EXIT_FAILURE); + } + + /* Loop looking for the call in each modem found */ + ctx->current = MM_OBJECT (ctx->modems->data); + ctx->modems = g_list_delete_link (ctx->modems, ctx->modems); + + modem = mm_object_get_modem_voice (ctx->current); + if (modem) { + g_debug ("Looking for call '%s' in modem '%s'...", + ctx->call_path, + mm_object_get_path (ctx->current)); + mm_modem_voice_list_call (modem, + ctx->cancellable, + (GAsyncReadyCallback)list_call_ready, + ctx); + g_object_unref (modem); + return; + } + + /* Current modem has no messaging capabilities, try with next modem */ + look_for_call_in_modem (ctx); +} + +static void +get_voice_manager_ready (GDBusConnection *connection, + GAsyncResult *res, + GetVoiceContext *ctx) +{ + ctx->manager = mmcli_get_manager_finish (res); + ctx->modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (ctx->manager)); + if (!ctx->modems) { + g_printerr ("error: couldn't find call at '%s': 'no modems found'\n", + ctx->call_path); + exit (EXIT_FAILURE); + } + + look_for_call_in_modem (ctx); +} + +static gchar * +get_call_path (const gchar *path_or_index) +{ + gchar *call_path; + + /* We must have a given call specified */ + if (!path_or_index) { + g_printerr ("error: no call was specified\n"); + exit (EXIT_FAILURE); + } + + /* Call path may come in two ways: full DBus path or just call index. + * If it is a call index, we'll need to generate the DBus path ourselves */ + if (g_str_has_prefix (path_or_index, MM_DBUS_CALL_PREFIX)) { + g_debug ("Assuming '%s' is the full call path", path_or_index); + call_path = g_strdup (path_or_index); + } else if (g_ascii_isdigit (path_or_index[0])) { + g_debug ("Assuming '%s' is the call index", path_or_index); + call_path = g_strdup_printf (MM_DBUS_CALL_PREFIX "/%s", path_or_index); + } else { + g_printerr ("error: invalid path or index string specified: '%s'\n", + path_or_index); + exit (EXIT_FAILURE); + } + + return call_path; +} + +MMCall * +mmcli_get_call_finish (GAsyncResult *res, + MMManager **o_manager, + MMObject **o_object) +{ + GetVoiceContext *ctx; + + ctx = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res)); + if (o_manager) + *o_manager = g_object_ref (ctx->manager); + if (o_object) + *o_object = g_object_ref (ctx->current); + return g_object_ref (ctx->call); +} + +void mmcli_get_call (GDBusConnection *connection, + const gchar *path_or_index, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) { + + GetVoiceContext *ctx; + + ctx = g_new0 (GetVoiceContext, 1); + ctx->call_path = get_call_path (path_or_index); + if (cancellable) + ctx->cancellable = g_object_ref (cancellable); + ctx->result = g_simple_async_result_new (G_OBJECT (connection), + callback, + user_data, + mmcli_get_call); + mmcli_get_manager (connection, + cancellable, + (GAsyncReadyCallback)get_voice_manager_ready, + ctx); +} + +MMCall *mmcli_get_call_sync (GDBusConnection *connection, + const gchar *path_or_index, + MMManager **o_manager, + MMObject **o_object) { + + MMManager *manager; + GList *modems; + GList *l; + MMCall *found = NULL; + gchar *call_path; + + call_path = get_call_path (path_or_index); + + manager = mmcli_get_manager_sync (connection); + modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (manager)); + if (!modems) { + g_printerr ("error: couldn't find sms at '%s': 'no modems found'\n", + call_path); + exit (EXIT_FAILURE); + } + + for (l = modems; !found && l; l = g_list_next (l)) { + GError *error = NULL; + MMObject *object; + MMModemVoice *voice; + GList *call_list; + + object = MM_OBJECT (l->data); + voice = mm_object_get_modem_voice (object); + + /* If doesn't implement voice, continue to next one */ + if (!voice) + continue; + + call_list = mm_modem_voice_list_call_sync (voice, NULL, &error); + if (error) { + g_printerr ("error: couldn't list call at '%s': '%s'\n", + mm_modem_voice_get_path (voice), + error->message); + exit (EXIT_FAILURE); + } + + found = find_call_in_list (call_list, call_path); + g_list_free_full (call_list, (GDestroyNotify) g_object_unref); + + if (found && o_object) + *o_object = g_object_ref (object); + + g_object_unref (voice); + } + + if (!found) { + g_printerr ("error: couldn't find call at '%s': 'not found in any modem'\n", + call_path); + exit (EXIT_FAILURE); + } + + g_list_free_full (modems, (GDestroyNotify) g_object_unref); + g_free (call_path); + + if (o_manager) + *o_manager = manager; + else + g_object_unref (manager); + + return found; +} + + /* Common options */ static gchar *modem_str; static gchar *bearer_str; static gchar *sim_str; static gchar *sms_str; +static gchar *call_str; static GOptionEntry entries[] = { { "modem", 'm', 0, G_OPTION_ARG_STRING, &modem_str, @@ -1094,6 +1365,10 @@ static GOptionEntry entries[] = { "Specify SMS by path or index. Shows SMS information if no action specified.", "[PATH|INDEX]" }, + { "call", 'o', 0, G_OPTION_ARG_STRING, &call_str, + "Specify Call by path or index. Shows Call information if no action specified.", + "[PATH|INDEX]" + }, { NULL } }; @@ -1137,6 +1412,12 @@ mmcli_get_common_sms_string (void) return sms_str; } +const gchar * +mmcli_get_common_call_string (void) +{ + return call_str; +} + gchar * mmcli_prefix_newlines (const gchar *prefix, const gchar *str) diff --git a/cli/mmcli-common.h b/cli/mmcli-common.h index 615f5218..2b807b04 100644 --- a/cli/mmcli-common.h +++ b/cli/mmcli-common.h @@ -79,6 +79,20 @@ MMSms *mmcli_get_sms_sync (GDBusConnection *connection, MMManager **manager, MMObject **object); +void mmcli_get_call (GDBusConnection *connection, + const gchar *path_or_index, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +MMCall *mmcli_get_call_finish (GAsyncResult *res, + MMManager **manager, + MMObject **object); +MMCall *mmcli_get_call_sync (GDBusConnection *connection, + const gchar *path_or_index, + MMManager **manager, + MMObject **object); + + const gchar *mmcli_get_state_reason_string (MMModemStateChangeReason reason); GOptionGroup *mmcli_get_common_option_group (void); @@ -86,6 +100,7 @@ const gchar *mmcli_get_common_modem_string (void); const gchar *mmcli_get_common_bearer_string (void); const gchar *mmcli_get_common_sim_string (void); const gchar *mmcli_get_common_sms_string (void); +const gchar *mmcli_get_common_call_string (void); gchar *mmcli_prefix_newlines (const gchar *prefix, const gchar *str); diff --git a/cli/mmcli-modem-voice.c b/cli/mmcli-modem-voice.c new file mode 100644 index 00000000..8622a1f9 --- /dev/null +++ b/cli/mmcli-modem-voice.c @@ -0,0 +1,438 @@ +/* -*- 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. + * Copyright (C) 2015 Riccardo Vangelisti <riccardo.vangelisti@sadel.it> + */ + +#include "config.h" + +#include <stdio.h> +#include <stdlib.h> +#include <locale.h> +#include <string.h> + +#include <glib.h> +#include <gio/gio.h> + +#define _LIBMM_INSIDE_MMCLI +#include <libmm-glib.h> + +#include "mmcli.h" +#include "mmcli-common.h" + +/* Context */ +typedef struct { + GDBusConnection *connection; + MMManager *manager; + GCancellable *cancellable; + MMObject *object; + MMModemVoice *modem_voice; +} Context; +static Context *ctx; + +/* Options */ +static gboolean list_flag; +static gchar *create_str; +static gchar *delete_str; + +static GOptionEntry entries[] = { + { "voice-list-call", 0, 0, G_OPTION_ARG_NONE, &list_flag, + "List call available in a given modem", + NULL + }, + { "voice-create-call", 0, 0, G_OPTION_ARG_STRING, &create_str, + "Create a new call in a given modem", + "[\"key=value,...\"]" + }, + { "voice-delete-call", 0, 0, G_OPTION_ARG_STRING, &delete_str, + "Delete a call from a given modem", + "[PATH|INDEX]" + }, + { NULL } +}; + +GOptionGroup * +mmcli_modem_voice_get_option_group (void) +{ + GOptionGroup *group; + + group = g_option_group_new ("voice", + "Voice options", + "Show Voice options", + NULL, + NULL); + g_option_group_add_entries (group, entries); + + return group; +} + +gboolean +mmcli_modem_voice_options_enabled (void) +{ + static guint n_actions = 0; + static gboolean checked = FALSE; + + if (checked) + return !!n_actions; + + n_actions = (list_flag + + !!create_str + + !!delete_str); + + if (n_actions > 1) { + g_printerr ("error: too many Voice 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_voice) + g_object_unref (ctx->modem_voice); + if (ctx->object) + g_object_unref (ctx->object); + if (ctx->manager) + g_object_unref (ctx->manager); + if (ctx->connection) + g_object_unref (ctx->connection); + g_free (ctx); +} + +static void +ensure_modem_voice (void) +{ + if (mm_modem_get_state (mm_object_peek_modem (ctx->object)) < MM_MODEM_STATE_ENABLED) { + g_printerr ("error: modem not enabled yet\n"); + exit (EXIT_FAILURE); + } + + if (!ctx->modem_voice) { + g_printerr ("error: modem has no voice capabilities\n"); + exit (EXIT_FAILURE); + } + + /* Success */ +} + +void +mmcli_modem_voice_shutdown (void) +{ + context_free (ctx); +} + +static MMCallProperties * +build_call_properties_from_input (const gchar *properties_string) +{ + GError *error = NULL; + MMCallProperties *properties; + + properties = mm_call_properties_new_from_string (properties_string, &error); + if (!properties) { + g_printerr ("error: cannot parse properties string: '%s'\n", error->message); + exit (EXIT_FAILURE); + } + + return properties; +} + +static void +print_call_short_info (MMCall *call) +{ + g_print ("\t%s %s (%s)\n", + mm_call_get_path (call), + mm_call_direction_get_string (mm_call_get_direction (call)), + mm_call_state_get_string (mm_call_get_state (call))); +} + +static void +list_process_reply (GList *result, + const GError *error) +{ + if (error) { + g_printerr ("error: couldn't list call: '%s'\n", + error->message); + exit (EXIT_FAILURE); + } + + g_print ("\n"); + if (!result) { + g_print ("No call were found\n"); + } else { + GList *l; + + g_print ("Found %u calls:\n", g_list_length (result)); + for (l = result; l; l = g_list_next (l)) { + MMCall *call = MM_CALL (l->data); + + print_call_short_info (call); + g_object_unref (call); + } + g_list_free (result); + } +} + +static void +list_ready (MMModemVoice *modem, + GAsyncResult *result, + gpointer nothing) +{ + GList *operation_result; + GError *error = NULL; + + operation_result = mm_modem_voice_list_call_finish (modem, result, &error); + list_process_reply (operation_result, error); + + mmcli_async_operation_done (); +} + +static void +create_process_reply (MMCall *call, + const GError *error) +{ + if (!call) { + g_printerr ("error: couldn't create new call: '%s'\n", + error ? error->message : "unknown error"); + exit (EXIT_FAILURE); + } + + g_print ("Successfully created new call:\n"); + print_call_short_info (call); + g_object_unref (call); +} + +static void +create_ready (MMModemVoice *modem, + GAsyncResult *result, + gpointer nothing) +{ + MMCall *call; + GError *error = NULL; + + call = mm_modem_voice_create_call_finish (modem, result, &error); + create_process_reply (call, error); + + mmcli_async_operation_done (); +} + +static void +delete_process_reply (gboolean result, + const GError *error) +{ + if (!result) { + g_printerr ("error: couldn't delete call: '%s'\n", + error ? error->message : "unknown error"); + exit (EXIT_FAILURE); + } + + g_print ("successfully deleted call from modem\n"); +} + +static void +delete_ready (MMModemVoice *modem, + GAsyncResult *result, + gpointer nothing) +{ + gboolean operation_result; + GError *error = NULL; + + operation_result = mm_modem_voice_delete_call_finish (modem, result, &error); + delete_process_reply (operation_result, error); + + mmcli_async_operation_done (); +} + +static void +get_call_to_delete_ready (GDBusConnection *connection, + GAsyncResult *res) +{ + MMCall *call; + MMObject *obj = NULL; + + call = mmcli_get_call_finish (res, NULL, &obj); + if (!g_str_equal (mm_object_get_path (obj), mm_modem_voice_get_path (ctx->modem_voice))) { + g_printerr ("error: call '%s' not owned by modem '%s'", + mm_call_get_path (call), + mm_modem_voice_get_path (ctx->modem_voice)); + exit (EXIT_FAILURE); + } + + mm_modem_voice_delete_call (ctx->modem_voice, + mm_call_get_path (call), + ctx->cancellable, + (GAsyncReadyCallback)delete_ready, + NULL); + g_object_unref (call); + g_object_unref (obj); +} + +static void +get_modem_ready (GObject *source, + GAsyncResult *result, + gpointer none) +{ + ctx->object = mmcli_get_modem_finish (result, &ctx->manager); + ctx->modem_voice = mm_object_get_modem_voice (ctx->object); + + /* Setup operation timeout */ + if (ctx->modem_voice) + mmcli_force_operation_timeout (G_DBUS_PROXY (ctx->modem_voice)); + + ensure_modem_voice (); + + /* Request to list call? */ + if (list_flag) { + g_debug ("Asynchronously listing call in modem..."); + mm_modem_voice_list_call (ctx->modem_voice, + ctx->cancellable, + (GAsyncReadyCallback)list_ready, + NULL); + return; + } + + /* Request to create a new call? */ + if (create_str) { + MMCallProperties *properties; + + properties = build_call_properties_from_input (create_str); + g_debug ("Asynchronously creating new call in modem..."); + mm_modem_voice_create_call (ctx->modem_voice, + properties, + ctx->cancellable, + (GAsyncReadyCallback)create_ready, + NULL); + g_object_unref (properties); + return; + } + + /* Request to delete a given call? */ + if (delete_str) { + mmcli_get_call (ctx->connection, + delete_str, + ctx->cancellable, + (GAsyncReadyCallback)get_call_to_delete_ready, + NULL); + return; + } + + g_warn_if_reached (); +} + +void +mmcli_modem_voice_run_asynchronous (GDBusConnection *connection, + GCancellable *cancellable) +{ + /* Initialize context */ + ctx = g_new0 (Context, 1); + if (cancellable) + ctx->cancellable = g_object_ref (cancellable); + ctx->connection = g_object_ref (connection); + + /* Get proper modem */ + mmcli_get_modem (connection, + mmcli_get_common_modem_string (), + cancellable, + (GAsyncReadyCallback)get_modem_ready, + NULL); +} + +void +mmcli_modem_voice_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_voice = mm_object_get_modem_voice (ctx->object); + + /* Setup operation timeout */ + if (ctx->modem_voice) + mmcli_force_operation_timeout (G_DBUS_PROXY (ctx->modem_voice)); + + ensure_modem_voice (); + + /* Request to list the call? */ + if (list_flag) { + GList *result; + + g_debug ("Synchronously listing call..."); + result = mm_modem_voice_list_call_sync (ctx->modem_voice, NULL, &error); + list_process_reply (result, error); + return; + } + + /* Request to create a new call? */ + if (create_str) { + MMCall *call; + GError *error = NULL; + MMCallProperties *properties; + + properties = build_call_properties_from_input (create_str); + + g_debug ("Synchronously creating new call in modem..."); + call = mm_modem_voice_create_call_sync (ctx->modem_voice, + properties, + NULL, + &error); + g_object_unref (properties); + + create_process_reply (call, error); + return; + } + + /* Request to delete a given call? */ + if (delete_str) { + gboolean result; + MMCall *call; + MMObject *obj = NULL; + + call = mmcli_get_call_sync (connection, + delete_str, + NULL, + &obj); + if (!g_str_equal (mm_object_get_path (obj), mm_modem_voice_get_path (ctx->modem_voice))) { + g_printerr ("error: call '%s' not owned by modem '%s'", + mm_call_get_path (call), + mm_modem_voice_get_path (ctx->modem_voice)); + exit (EXIT_FAILURE); + } + + result = mm_modem_voice_delete_call_sync (ctx->modem_voice, + mm_call_get_path (call), + NULL, + &error); + g_object_unref (call); + g_object_unref (obj); + + delete_process_reply (result, error); + return; + } + + g_warn_if_reached (); +} diff --git a/cli/mmcli.c b/cli/mmcli.c index a4e6a4f5..6784a704 100644 --- a/cli/mmcli.c +++ b/cli/mmcli.c @@ -205,6 +205,8 @@ main (gint argc, gchar **argv) g_option_context_add_group (context, mmcli_modem_messaging_get_option_group ()); g_option_context_add_group (context, + mmcli_modem_voice_get_option_group ()); + g_option_context_add_group (context, mmcli_modem_time_get_option_group ()); g_option_context_add_group (context, mmcli_modem_firmware_get_option_group ()); @@ -218,6 +220,8 @@ main (gint argc, gchar **argv) mmcli_bearer_get_option_group ()); g_option_context_add_group (context, mmcli_sms_get_option_group ()); + g_option_context_add_group (context, + mmcli_call_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); @@ -280,6 +284,13 @@ main (gint argc, gchar **argv) else mmcli_sms_run_synchronous (connection); } + /* Call options? */ + else if (mmcli_call_options_enabled ()) { + if (async_flag) + mmcli_call_run_asynchronous (connection, cancellable); + else + mmcli_call_run_synchronous (connection); + } /* Modem 3GPP options? */ else if (mmcli_modem_3gpp_options_enabled ()) { if (async_flag) @@ -315,6 +326,13 @@ main (gint argc, gchar **argv) else mmcli_modem_messaging_run_synchronous (connection); } + /* Voice options? */ + else if (mmcli_modem_voice_options_enabled ()) { + if (async_flag) + mmcli_modem_voice_run_asynchronous (connection, cancellable); + else + mmcli_modem_voice_run_synchronous (connection); + } /* Modem Time options? */ else if (mmcli_modem_time_options_enabled ()) { if (async_flag) @@ -375,6 +393,8 @@ main (gint argc, gchar **argv) mmcli_modem_location_shutdown (); } else if (mmcli_modem_messaging_options_enabled ()) { mmcli_modem_messaging_shutdown (); + } else if (mmcli_modem_voice_options_enabled ()) { + mmcli_modem_voice_shutdown (); } else if (mmcli_modem_time_options_enabled ()) { mmcli_modem_time_shutdown (); } else if (mmcli_modem_firmware_options_enabled ()) { @@ -389,6 +409,8 @@ main (gint argc, gchar **argv) mmcli_bearer_shutdown (); } else if (mmcli_sms_options_enabled ()) { mmcli_sms_shutdown (); + } else if (mmcli_call_options_enabled ()) { + mmcli_call_shutdown (); } else if (mmcli_modem_options_enabled ()) { mmcli_modem_shutdown (); } diff --git a/cli/mmcli.h b/cli/mmcli.h index 6b7b5536..a367be4c 100644 --- a/cli/mmcli.h +++ b/cli/mmcli.h @@ -85,6 +85,14 @@ void mmcli_modem_messaging_run_asynchronous (GDBusConnection *connect void mmcli_modem_messaging_run_synchronous (GDBusConnection *connection); void mmcli_modem_messaging_shutdown (void); +/* Voice group */ +GOptionGroup *mmcli_modem_voice_get_option_group (void); +gboolean mmcli_modem_voice_options_enabled (void); +void mmcli_modem_voice_run_asynchronous (GDBusConnection *connection, + GCancellable *cancellable); +void mmcli_modem_voice_run_synchronous (GDBusConnection *connection); +void mmcli_modem_voice_shutdown (void); + /* Time group */ GOptionGroup *mmcli_modem_time_get_option_group (void); gboolean mmcli_modem_time_options_enabled (void); @@ -141,4 +149,12 @@ void mmcli_sms_run_asynchronous (GDBusConnection *connection, void mmcli_sms_run_synchronous (GDBusConnection *connection); void mmcli_sms_shutdown (void); +/* Call group */ +GOptionGroup *mmcli_call_get_option_group (void); +gboolean mmcli_call_options_enabled (void); +void mmcli_call_run_asynchronous (GDBusConnection *connection, + GCancellable *cancellable); +void mmcli_call_run_synchronous (GDBusConnection *connection); +void mmcli_call_shutdown (void); + #endif /* __MMCLI_H__ */ |