Debian Codegraph @

src/qmi-firmware-update/qfu-device-selection.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * qmi-firmware-update -- Command line tool to update 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) 2017 Zodiac Inflight Innovations
19 * Copyright (C) 2017 Aleksander Morgado <[email protected]>
20 */
21
22 #include <config.h>
23 #include <string.h>
24
25 #include <gio/gio.h>
26
27 #include <libqmi-glib.h>
28
29 #include "qfu-device-selection.h"
30 #include "qfu-helpers.h"
31
32 G_DEFINE_TYPE (QfuDeviceSelection, qfu_device_selection, G_TYPE_OBJECT)
33
34 struct _QfuDeviceSelectionPrivate {
35 /* inputs */
36 gchar *preferred_devices[QFU_HELPERS_DEVICE_TYPE_LAST];
37 guint16 preferred_vid;
38 guint16 preferred_pid;
39 guint preferred_busnum;
40 guint preferred_devnum;
41
42 /* sysfs path */
43 gchar *sysfs_path;
44 /* peer port sysfs path */
45 gchar *peer_port;
46
47 #if defined WITH_UDEV
48 /* generic udev monitor */
49 QfuHelpersUdevGenericMonitor *monitor;
50 #endif
51 };
52
53 /******************************************************************************/
54
55 static GFile *
56 device_selection_get_single (QfuDeviceSelection *self,
57 QfuHelpersDeviceType device_type,
58 QfuHelpersDeviceMode device_mode)
59 {
60 GFile *first_selection = NULL;
61 GFile *preferred_selection = NULL;
62 GList *list, *l;
63 gchar *path;
64
65 g_debug ("[qfu,device-selection] single %s device requested in sysfs path '%s'",
66 qfu_helpers_device_type_to_string (device_type),
67 self->priv->sysfs_path);
68
69 list = qfu_helpers_list_devices (device_type, device_mode, self->priv->sysfs_path);
70 for (l = list; l; l = g_list_next (l)) {
71 path = g_file_get_path (G_FILE (l->data));
72 g_debug ("[qfu,device-selection] device found: %s", path);
73 if (!first_selection)
74 first_selection = g_object_ref (l->data);
75 if (!preferred_selection && !g_strcmp0 (path, self->priv->preferred_devices[device_type]))
76 preferred_selection = g_object_ref (l->data);
77 g_free (path);
78 }
79 g_list_free_full (list, g_object_unref);
80
81 if (preferred_selection) {
82 path = g_file_get_path (preferred_selection);
83 g_debug ("[qfu,device-selection] using preferred device: %s", path);
84 g_free (path);
85 if (first_selection)
86 g_object_unref (first_selection);
87 return preferred_selection;
88 }
89
90 if (first_selection) {
91 path = g_file_get_path (first_selection);
92 g_debug ("[qfu,device-selection] using automatically selected device: %s", path);
93 g_free (path);
94 return first_selection;
95 }
96
97 g_debug ("[qfu,device-selection] couldn't find any device to use");
98 return NULL;
99 }
100
101 GFile *
102 qfu_device_selection_get_single_cdc_wdm (QfuDeviceSelection *self)
103 {
104 return device_selection_get_single (self, QFU_HELPERS_DEVICE_TYPE_CDC_WDM, QFU_HELPERS_DEVICE_MODE_MODEM);
105 }
106
107 GFile *
108 qfu_device_selection_get_single_tty (QfuDeviceSelection *self,
109 QfuHelpersDeviceMode mode)
110 {
111 return device_selection_get_single (self, QFU_HELPERS_DEVICE_TYPE_TTY, mode);
112 }
113
114 /******************************************************************************/
115
116 static GList *
117 device_selection_get_multiple (QfuDeviceSelection *self,
118 QfuHelpersDeviceType device_type,
119 QfuHelpersDeviceMode device_mode)
120 {
121 GFile *preferred_selection = NULL;
122 GList *list, *l;
123 gchar *path;
124
125 g_debug ("[qfu,device-selection] multiple %s devices requested in sysfs path '%s'",
126 qfu_helpers_device_type_to_string (device_type),
127 self->priv->sysfs_path);
128
129 list = qfu_helpers_list_devices (device_type, device_mode, self->priv->sysfs_path);
130 for (l = list; l; l = g_list_next (l)) {
131 path = g_file_get_path (G_FILE (l->data));
132 g_debug ("[qfu,device-selection] device found: %s", path);
133 if (!preferred_selection && !g_strcmp0 (path, self->priv->preferred_devices[device_type]))
134 preferred_selection = g_object_ref (l->data);
135 g_free (path);
136 }
137
138 /* If we have a preferred device selected, we will only include that one in the output list */
139 if (preferred_selection) {
140 path = g_file_get_path (preferred_selection);
141 g_debug ("[qfu,device-selection] using only preferred device: %s", path);
142 g_free (path);
143 g_list_free_full (list, g_object_unref);
144 return g_list_append (NULL, preferred_selection);
145 }
146
147 if (list)
148 return list;
149
150 g_debug ("[qfu,device-selection] couldn't find any device to use");
151 return NULL;
152 }
153
154 GList *
155 qfu_device_selection_get_multiple_ttys (QfuDeviceSelection *self,
156 QfuHelpersDeviceMode mode)
157 {
158 return device_selection_get_multiple (self, QFU_HELPERS_DEVICE_TYPE_TTY, mode);
159 }
160
161 /******************************************************************************/
162
163 GFile *
164 qfu_device_selection_wait_for_cdc_wdm_finish (QfuDeviceSelection *self,
165 GAsyncResult *res,
166 GError **error)
167 {
168 return G_FILE (g_task_propagate_pointer (G_TASK (res), error));
169 }
170
171 GFile *
172 qfu_device_selection_wait_for_tty_finish (QfuDeviceSelection *self,
173 GAsyncResult *res,
174 GError **error)
175 {
176 return G_FILE (g_task_propagate_pointer (G_TASK (res), error));
177 }
178
179 static void
180 wait_for_device_ready (gpointer unused,
181 GAsyncResult *res,
182 GTask *task)
183 {
184 GError *error = NULL;
185 GFile *file;
186
187 file = qfu_helpers_wait_for_device_finish (res, &error);
188 if (!file)
189 g_task_return_error (task, error);
190 else
191 g_task_return_pointer (task, file, g_object_unref);
192 g_object_unref (task);
193 }
194
195 void
196 qfu_device_selection_wait_for_cdc_wdm (QfuDeviceSelection *self,
197 GCancellable *cancellable,
198 GAsyncReadyCallback callback,
199 gpointer user_data)
200 {
201 GTask *task;
202
203 task = g_task_new (self, cancellable, callback, user_data);
204 qfu_helpers_wait_for_device (QFU_HELPERS_DEVICE_TYPE_CDC_WDM,
205 QFU_HELPERS_DEVICE_MODE_MODEM,
206 self->priv->sysfs_path,
207 self->priv->peer_port,
208 cancellable,
209 (GAsyncReadyCallback) wait_for_device_ready,
210 task);
211 }
212
213 void
214 qfu_device_selection_wait_for_tty (QfuDeviceSelection *self,
215 QfuHelpersDeviceMode mode,
216 GCancellable *cancellable,
217 GAsyncReadyCallback callback,
218 gpointer user_data)
219 {
220 GTask *task;
221
222 task = g_task_new (self, cancellable, callback, user_data);
223 qfu_helpers_wait_for_device (QFU_HELPERS_DEVICE_TYPE_TTY,
224 mode,
225 self->priv->sysfs_path,
226 self->priv->peer_port,
227 cancellable,
228 (GAsyncReadyCallback) wait_for_device_ready,
229 task);
230 }
231
232 /******************************************************************************/
233
234 QfuDeviceSelection *
235 qfu_device_selection_new (const gchar *preferred_cdc_wdm,
236 const gchar *preferred_tty,
237 guint16 preferred_vid,
238 guint16 preferred_pid,
239 guint preferred_busnum,
240 guint preferred_devnum,
241 GError **error)
242 {
243 QfuDeviceSelection *self;
244 guint n_selections;
245
246 /* Note: pid and busnum may be zero */
247 n_selections = (!!preferred_cdc_wdm +
248 !!preferred_tty +
249 !!preferred_vid +
250 !!preferred_devnum);
251
252 if (!n_selections) {
253 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
254 "No device selected");
255 return NULL;
256 }
257
258 if (n_selections > 1) {
259 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
260 "Only one device selection option may be provided");
261 return NULL;
262 }
263
264 /* Selection valid, create object */
265
266 self = g_object_new (QFU_TYPE_DEVICE_SELECTION, NULL);
267
268 /* Store inputs */
269 self->priv->preferred_devices[QFU_HELPERS_DEVICE_TYPE_CDC_WDM] = g_strdup (preferred_cdc_wdm);
270 self->priv->preferred_devices[QFU_HELPERS_DEVICE_TYPE_TTY] = g_strdup (preferred_tty);
271 self->priv->preferred_vid = preferred_vid;
272 self->priv->preferred_pid = preferred_pid;
273 self->priv->preferred_busnum = preferred_busnum;
274 self->priv->preferred_devnum = preferred_devnum;
275
276 /* Initialize sysfs path from inputs */
277 if (preferred_vid || preferred_devnum)
278 self->priv->sysfs_path = qfu_helpers_find_by_device_info (preferred_vid, preferred_pid, preferred_busnum, preferred_devnum, error);
279 else if (preferred_cdc_wdm || preferred_tty)
280 self->priv->sysfs_path = qfu_helpers_find_by_file_path (preferred_cdc_wdm ? preferred_cdc_wdm : preferred_tty, error);
281 else
282 g_assert_not_reached ();
283
284 if (!self->priv->sysfs_path) {
285 g_object_unref (self);
286 return NULL;
287 }
288
289 /* look for a peer port */
290 self->priv->peer_port = qfu_helpers_find_peer_port (self->priv->sysfs_path, error);
291
292 #if defined WITH_UDEV
293 /* Initialize right away the generic udev monitor for this sysfs path */
294 self->priv->monitor = qfu_helpers_udev_generic_monitor_new (self->priv->sysfs_path);
295 #endif
296
297 return self;
298 }
299
300 static void
301 qfu_device_selection_init (QfuDeviceSelection *self)
302 {
303 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, QFU_TYPE_DEVICE_SELECTION, QfuDeviceSelectionPrivate);
304 }
305
306 static void
307 finalize (GObject *object)
308 {
309 QfuDeviceSelection *self = QFU_DEVICE_SELECTION (object);
310 guint i;
311
312 #if defined WITH_UDEV
313 if (self->priv->monitor)
314 qfu_helpers_udev_generic_monitor_free (self->priv->monitor);
315 #endif
316
317 g_free (self->priv->sysfs_path);
318 g_free (self->priv->peer_port);
319
320 for (i = 0; i < QFU_HELPERS_DEVICE_TYPE_LAST; i++)
321 g_free (self->priv->preferred_devices[i]);
322
323 G_OBJECT_CLASS (qfu_device_selection_parent_class)->finalize (object);
324 }
325
326 static void
327 qfu_device_selection_class_init (QfuDeviceSelectionClass *klass)
328 {
329 GObjectClass *object_class = G_OBJECT_CLASS (klass);
330
331 g_type_class_add_private (object_class, sizeof (QfuDeviceSelectionPrivate));
332
333 object_class->finalize = finalize;
334 }