Debian Codegraph @

src/qmi-firmware-update/qfu-operation-reset.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * qmi-firmware-reseter -- Command line tool to reseter firmware in QMI devices
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Copyright (C) 2016 Zodiac Inflight Innovations
19 * Copyright (C) 2016-2017 Aleksander Morgado <[email protected]>
20 */
21
22 #include <glib-object.h>
23 #include <gio/gio.h>
24 #include <glib-unix.h>
25
26 #include "qfu-operation.h"
27 #include "qfu-reseter.h"
28
29 typedef struct {
30 GMainLoop *loop;
31 GCancellable *cancellable;
32 gboolean result;
33 } ReseterOperation;
34
35 static gboolean
36 signal_handler (ReseterOperation *operation)
37 {
38 operation->result = FALSE;
39
40 /* Ignore consecutive requests of cancellation */
41 if (!g_cancellable_is_cancelled (operation->cancellable)) {
42 g_printerr ("cancelling the operation...\n");
43 g_cancellable_cancel (operation->cancellable);
44 /* Reset the signal handler to allow main loop cancellation on
45 * second signal */
46 return G_SOURCE_CONTINUE;
47 }
48
49 if (g_main_loop_is_running (operation->loop)) {
50 g_printerr ("cancelling the main loop...\n");
51 g_main_loop_quit (operation->loop);
52 }
53
54 return G_SOURCE_REMOVE;
55 }
56
57 static void
58 run_ready (QfuReseter *reseter,
59 GAsyncResult *res,
60 ReseterOperation *operation)
61 {
62 GError *error = NULL;
63
64 if (!qfu_reseter_run_finish (reseter, res, &error)) {
65 g_printerr ("error: reseter operation finished: %s\n", error->message);
66 g_error_free (error);
67 } else {
68 g_print ("reseter operation finished successfully\n");
69 operation->result = TRUE;
70 }
71
72 g_idle_add ((GSourceFunc) g_main_loop_quit, operation->loop);
73 }
74
75 static gboolean
76 operation_reseter_run (QfuReseter *reseter)
77 {
78 ReseterOperation operation = {
79 .loop = NULL,
80 .cancellable = NULL,
81 .result = FALSE,
82 };
83
84 g_assert (QFU_IS_RESETER (reseter));
85
86 /* Create runtime context */
87 operation.loop = g_main_loop_new (NULL, FALSE);
88 operation.cancellable = g_cancellable_new ();
89
90 /* Setup signals */
91 g_unix_signal_add (SIGINT, (GSourceFunc) signal_handler, &operation);
92 g_unix_signal_add (SIGHUP, (GSourceFunc) signal_handler, &operation);
93 g_unix_signal_add (SIGTERM, (GSourceFunc) signal_handler, &operation);
94
95 /* Run! */
96 qfu_reseter_run (reseter, operation.cancellable, (GAsyncReadyCallback) run_ready, &operation);
97 g_main_loop_run (operation.loop);
98
99 if (operation.cancellable)
100 g_object_unref (operation.cancellable);
101 if (operation.loop)
102 g_main_loop_unref (operation.loop);
103
104 return operation.result;
105 }
106
107 gboolean
108 qfu_operation_reset_run (QfuDeviceSelection *device_selection,
109 QmiDeviceOpenFlags device_open_flags)
110 {
111 QfuReseter *reseter = NULL;
112 gboolean result;
113
114 reseter = qfu_reseter_new (device_selection, NULL, device_open_flags);
115 result = operation_reseter_run (reseter);
116 g_object_unref (reseter);
117 return result;
118 }