1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* mm-cli -- 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) 2011 Aleksander Morgado <aleksander@gnu.org>
*/
#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"
#define PROGRAM_NAME "mmcli"
#define PROGRAM_VERSION PACKAGE_VERSION
/* Globals */
static GMainLoop *loop;
static gboolean keep_loop;
static GCancellable *cancellable;
/* Context */
static gboolean version_flag;
static gboolean async_flag;
static gboolean list_modems_flag;
static gboolean monitor_modems_flag;
static gboolean scan_modems_flag;
static gchar *set_logging_str;
static GOptionEntry entries[] = {
{ "version", 'V', 0, G_OPTION_ARG_NONE, &version_flag,
"Print version",
NULL
},
{ "set-logging", 'L', 0, G_OPTION_ARG_STRING, &set_logging_str,
"Set logging level in the ModemManager daemon",
"[ERR,WARN,INFO,DEBUG]",
},
{ "async", 'a', 0, G_OPTION_ARG_NONE, &async_flag,
"Use asynchronous methods",
NULL
},
{ "list-modems", 'l', 0, G_OPTION_ARG_NONE, &list_modems_flag,
"List available modems",
NULL
},
{ "monitor-modems", 'm', 0, G_OPTION_ARG_NONE, &monitor_modems_flag,
"List available modems and monitor additions and removals",
NULL
},
{ "scan-modems", 's', 0, G_OPTION_ARG_NONE, &scan_modems_flag,
"Request to re-scan looking for modems",
NULL
},
{ NULL }
};
static void
signals_handler (int signum)
{
if (cancellable) {
/* Ignore consecutive requests of cancellation */
if (!g_cancellable_is_cancelled (cancellable)) {
g_printerr ("%s\n",
"cancelling the operation...");
g_cancellable_cancel (cancellable);
}
return;
}
if (loop &&
g_main_loop_is_running (loop)) {
g_printerr ("%s\n",
"cancelling the main loop...");
g_main_loop_quit (loop);
}
}
static void
print_version_and_exit (void)
{
g_print ("\n"
PROGRAM_NAME " " PROGRAM_VERSION "\n"
"Copyright (2011) Aleksander Morgado\n"
"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n"
"\n");
exit (EXIT_SUCCESS);
}
static void
scan_devices_process_reply (gboolean result,
const GError *error)
{
if (!result) {
g_printerr ("couldn't request to scan devices: '%s'\n",
error ? error->message : "unknown error");
exit (EXIT_FAILURE);
}
g_print ("successfully requested to scan devices\n");
}
static void
scan_devices_ready (MMManager *manager,
GAsyncResult *result,
gpointer nothing)
{
gboolean operation_result;
GError *error = NULL;
operation_result = mm_manager_scan_devices_finish (manager,
result,
&error);
scan_devices_process_reply (operation_result, error);
if (cancellable) {
g_object_unref (cancellable);
cancellable = NULL;
}
if (!keep_loop)
g_main_loop_quit (loop);
}
static void
enumerate_devices_process_reply (const GStrv paths,
const GError *error)
{
if (error) {
g_printerr ("couldn't enumerate devices: '%s'\n",
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
enumerate_devices_ready (MMManager *manager,
GAsyncResult *result,
gpointer nothing)
{
GStrv paths;
GError *error = NULL;
paths = mm_manager_enumerate_devices_finish (manager, result, &error);
enumerate_devices_process_reply (paths, error);
g_strfreev (paths);
if (cancellable) {
g_object_unref (cancellable);
cancellable = NULL;
}
if (!keep_loop)
g_main_loop_quit (loop);
}
static void
device_added (MMManager *manager,
const gchar *path)
{
g_print ("%s: '%s'\n",
"Added modem",
path);
fflush (stdout);
}
static void
device_removed (MMManager *manager,
const gchar *path)
{
g_print ("%s: '%s'\n",
"Removed modem",
path);
fflush (stdout);
}
static void
asynchronous (MMManager *manager)
{
g_debug ("Running asynchronous operations...");
/* Setup global cancellable */
cancellable = g_cancellable_new ();
/* Request to scan modems? */
if (scan_modems_flag) {
mm_manager_scan_devices_async (manager,
cancellable,
(GAsyncReadyCallback)scan_devices_ready,
NULL);
return;
}
/* Request to monitor modems? */
if (monitor_modems_flag) {
g_signal_connect (manager,
"device-added",
G_CALLBACK (device_added),
NULL);
g_signal_connect (manager,
"device-removed",
G_CALLBACK (device_removed),
NULL);
}
/* Request to list modems? */
if (list_modems_flag) {
mm_manager_enumerate_devices_async (manager,
cancellable,
(GAsyncReadyCallback)enumerate_devices_ready,
NULL);
return;
}
}
static void
synchronous (MMManager *manager)
{
GError *error = NULL;
g_debug ("Running synchronous operations...");
/* Request to set log level? */
if (set_logging_str) {
MMLogLevel level;
if (g_strcmp0 (set_logging_str, "ERR") == 0)
level = MM_LOG_LEVEL_ERROR;
else if (g_strcmp0 (set_logging_str, "WARN") == 0)
level = MM_LOG_LEVEL_WARNING;
else if (g_strcmp0 (set_logging_str, "INFO") == 0)
level = MM_LOG_LEVEL_INFO;
else if (g_strcmp0 (set_logging_str, "DEBUG") == 0)
level = MM_LOG_LEVEL_DEBUG;
else {
g_printerr ("couldn't set unknown logging level: '%s'\n",
set_logging_str);
exit (EXIT_FAILURE);
}
if (mm_manager_set_logging (manager, level, &error)) {
g_printerr ("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;
}
/* Request to scan modems? */
if (scan_modems_flag) {
gboolean result;
result = mm_manager_scan_devices (manager, &error);
scan_devices_process_reply (result, error);
return;
}
/* Request to list modems? */
if (list_modems_flag) {
GStrv paths;
paths = mm_manager_enumerate_devices (manager, &error);
enumerate_devices_process_reply (paths, error);
g_strfreev (paths);
return;
}
}
static void
ensure_single_action (void)
{
guint n_actions;
n_actions = (scan_modems_flag +
list_modems_flag +
monitor_modems_flag +
(set_logging_str ? 1 : 0));
if (n_actions == 0)
print_version_and_exit ();
if (n_actions > 1) {
g_printerr ("error, too many actions requested\n");
exit (EXIT_FAILURE);
}
/* Additional fixes to the modem monitoring request */
if (monitor_modems_flag) {
/* Do not stop loop after listing initial modems */
keep_loop = TRUE;
/* Assume an implicit list modems request */
list_modems_flag = TRUE;
/* Monitoring always asynchronously */
async_flag = TRUE;
}
/* Additional fixes for the log level setting request */
if (set_logging_str) {
/* Log level setting always synchronously */
async_flag = FALSE;
/* Always stop loop after setting log level */
keep_loop = FALSE;
}
}
gint
main (gint argc, gchar **argv)
{
GDBusConnection *connection;
MMManager *manager;
GOptionContext *context;
GError *error = NULL;
setlocale (LC_ALL, "");
context = g_option_context_new ("- Control and monitor the ModemManager");
g_option_context_add_main_entries (context, entries, NULL);
g_option_context_parse (context, &argc, &argv, NULL);
if (version_flag)
print_version_and_exit ();
/* We must have exactly 1 action requested */
ensure_single_action ();
g_type_init ();
/* Setup signals */
signal (SIGINT, signals_handler);
signal (SIGHUP, signals_handler);
signal (SIGTERM, signals_handler);
/* Setup dbus connection to use */
connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
if (!connection) {
g_printerr ("couldn't get bus: %s\n",
error ? error->message : "unknown error");
exit (EXIT_FAILURE);
}
/* Create new manager */
manager = mm_manager_new (connection, NULL, &error);
if (!manager) {
g_printerr ("couldn't create manager: %s\n",
error ? error->message : "unknown error");
exit (EXIT_FAILURE);
}
if (async_flag) {
loop = g_main_loop_new (NULL, FALSE);
asynchronous (manager);
g_main_loop_run (loop);
g_main_loop_unref (loop);
}
else
synchronous (manager);
g_object_unref (manager);
g_object_unref (connection);
if (cancellable)
g_object_unref (cancellable);
g_option_context_free (context);
return EXIT_SUCCESS;
}
|