Debian Codegraph @

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

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Qfu-firmware-update -- Command line tool to update firmware in QFU 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 <errno.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <termios.h>
28 #include <unistd.h>
29
30 #include <glib-object.h>
31 #include <gio/gio.h>
32
33 #include <libqmi-glib.h>
34
35 #include "qfu-log.h"
36 #include "qfu-at-device.h"
37 #include "qfu-utils.h"
38
39 #define QFU_AT_BUFFER_SIZE 128
40
41 static void initable_iface_init (GInitableIface *iface);
42
43 G_DEFINE_TYPE_EXTENDED (QfuAtDevice, qfu_at_device, G_TYPE_OBJECT, 0,
44 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init))
45
46 enum {
47 PROP_0,
48 PROP_FILE,
49 PROP_LAST
50 };
51
52 static GParamSpec *properties[PROP_LAST];
53
54 struct _QfuAtDevicePrivate {
55 GFile *file;
56 gchar *name;
57 gint fd;
58 GByteArray *buffer;
59 };
60
61 /******************************************************************************/
62 /* Send */
63
64 static gboolean
65 send_request (QfuAtDevice *self,
66 const gchar *request,
67 GCancellable *cancellable,
68 GError **error)
69 {
70 gssize wlen;
71 fd_set wr;
72 gint aux;
73 struct timeval tv = {
74 .tv_sec = 2,
75 .tv_usec = 0,
76 };
77
78 /* Wait for the fd to be writable and don't wait forever */
79 FD_ZERO (&wr);
80 FD_SET (self->priv->fd, &wr);
81 aux = select (self->priv->fd + 1, NULL, &wr, NULL, &tv);
82
83 if (g_cancellable_set_error_if_cancelled (cancellable, error))
84 return FALSE;
85
86 if (aux < 0) {
87 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
88 "error waiting to write: %s",
89 g_strerror (errno));
90 return FALSE;
91 }
92
93 if (aux == 0) {
94 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
95 "timed out waiting to write");
96 return FALSE;
97 }
98
99 /* Debug output */
100 if (qfu_log_get_verbose ())
101 g_debug ("[qfu-at-device,%s] >> %s", self->priv->name, request);
102
103 wlen = write (self->priv->fd, request, strlen (request));
104 if (wlen < 0) {
105 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
106 "error writting: %s",
107 g_strerror (errno));
108 return FALSE;
109 }
110
111 /* We treat EINTR as an error, so we also treat as an error if not all bytes
112 * were wlen */
113 if ((gsize)wlen != strlen (request)) {
114 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
115 "error writing: only %" G_GSSIZE_FORMAT "/%" G_GSIZE_FORMAT " bytes written",
116 wlen, strlen (request));
117 return FALSE;
118 }
119
120 wlen = write (self->priv->fd, "\r", 1);
121 if (wlen < 0) {
122 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
123 "error writting <CR>: %s",
124 g_strerror (errno));
125 return FALSE;
126 }
127
128 return TRUE;
129 }
130
131 /******************************************************************************/
132 /* Receive */
133
134 static gssize
135 receive_response (QfuAtDevice *self,
136 guint timeout_secs,
137 gchar **response,
138 GCancellable *cancellable,
139 GError **error)
140 {
141 fd_set rd;
142 struct timeval tv;
143 gint aux;
144 gssize rlen;
145 gchar *start;
146
147 /* Use requested timeout */
148 tv.tv_sec = timeout_secs;
149 tv.tv_usec = 0;
150
151 FD_ZERO (&rd);
152 FD_SET (self->priv->fd, &rd);
153 aux = select (self->priv->fd + 1, &rd, NULL, NULL, &tv);
154
155 if (g_cancellable_set_error_if_cancelled (cancellable, error))
156 return -1;
157
158 if (aux < 0) {
159 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
160 "error waiting to read response: %s",
161 g_strerror (errno));
162 return -1;
163 }
164
165 if (aux == 0) {
166 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
167 "timed out waiting for the response");
168 return -1;
169 }
170
171 /* Receive in the primary buffer */
172 rlen = read (self->priv->fd, self->priv->buffer->data, self->priv->buffer->len);
173 if (rlen < 0) {
174 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
175 "couldn't read response: %s",
176 g_strerror (errno));
177 return -1;
178 }
179
180 if (rlen == 0) {
181 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
182 "couldn't read response: HUP detected");
183 return -1;
184 }
185
186 /* Force end of string */
187 start = (gchar *) &self->priv->buffer->data[0];
188 start[rlen] = '\0';
189
190 /* Remove trailing LFs and CRs */
191 while (rlen > 0 && (start[rlen - 1] == '\r' || start[rlen - 1] == '\n'))
192 start[--rlen] = '\0';
193
194 /* Remove heading LFs and CRs */
195 while (rlen > 0 && (start[0] == '\r' || start[0] == '\n')) {
196 start++;
197 rlen--;
198 }
199
200 /* Debug output */
201 if (qfu_log_get_verbose ())
202 g_debug ("[qfu-at-device,%s] << %s", self->priv->name, start);
203
204 if (response)
205 *response = start;
206
207 return rlen;
208 }
209
210 /******************************************************************************/
211 /* Send/receive */
212
213 static gssize
214 send_receive (QfuAtDevice *self,
215 const gchar *request,
216 guint response_timeout_secs,
217 gchar **response,
218 GCancellable *cancellable,
219 GError **error)
220 {
221 gboolean sent;
222
223 if (self->priv->fd < 0) {
224 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "device is closed");
225 return FALSE;
226 }
227
228 sent = send_request (self, request, cancellable, error);
229 if (!sent)
230 return -1;
231
232 if (!response)
233 return 0;
234
235 while (1) {
236 gssize rsplen;
237
238 rsplen = receive_response (self, response_timeout_secs, response, cancellable, error);
239 if (rsplen > 0 && g_str_has_prefix (*response, request)) {
240 *response += strlen (request);
241 rsplen -= strlen (request);
242 if (rsplen == 0)
243 continue;
244 }
245
246 return rsplen;
247 }
248 }
249
250 /******************************************************************************/
251
252 gboolean
253 qfu_at_device_boothold (QfuAtDevice *self,
254 GCancellable *cancellable,
255 GError **error)
256 {
257 gssize rsplen;
258 gchar *rsp = NULL;
259
260 rsplen = send_receive (self, "AT!BOOTHOLD", 3, &rsp, cancellable, error);
261 if (rsplen < 0)
262 return FALSE;
263
264 if (strstr (rsp, "OK"))
265 return TRUE;
266
267 if (strstr (rsp, "ERROR"))
268 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "unknown command");
269 else
270 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "unknown error");
271 return FALSE;
272 }
273
274 /******************************************************************************/
275
276 static gboolean
277 initable_init (GInitable *initable,
278 GCancellable *cancellable,
279 GError **error)
280 {
281 QfuAtDevice *self;
282 struct termios terminal_data;
283 gchar *path = NULL;
284 GError *inner_error = NULL;
285
286 self = QFU_AT_DEVICE (initable);
287
288 if (g_cancellable_set_error_if_cancelled (cancellable, &inner_error))
289 goto out;
290
291 path = g_file_get_path (self->priv->file);
292 g_debug ("[qfu-at-device,%s] opening TTY", self->priv->name);
293
294 self->priv->fd = open (path, O_RDWR | O_NOCTTY);
295 if (self->priv->fd < 0) {
296 inner_error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
297 "error opening serial device: %s",
298 g_strerror (errno));
299 goto out;
300 }
301
302 g_debug ("[qfu-at-device,%s] setting up serial port...", self->priv->name);
303 if (tcgetattr (self->priv->fd, &terminal_data) < 0) {
304 inner_error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
305 "error getting serial port attributes: %s",
306 g_strerror (errno));
307 goto out;
308 }
309
310 terminal_data.c_iflag &= ~(IGNCR | ICRNL | IUCLC | INPCK | IXON | IXANY );
311 terminal_data.c_oflag &= ~(OPOST | OLCUC | OCRNL | ONLCR | ONLRET);
312 terminal_data.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL);
313 terminal_data.c_cc[VMIN] = 1;
314 terminal_data.c_cc[VTIME] = 0;
315 terminal_data.c_cc[VEOF] = 1;
316 terminal_data.c_iflag |= (IXON | IXOFF | IXANY | IGNPAR);
317 terminal_data.c_cflag &= ~(CBAUD | CSIZE | CSTOPB | PARENB | CRTSCTS);
318 terminal_data.c_cflag |= (CS8 | CREAD | CLOCAL); /* 8N1 */
319
320 errno = 0;
321 if (cfsetispeed (&terminal_data, B115200) != 0) {
322 inner_error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
323 "failed to set serial port input speed: %s",
324 g_strerror (errno));
325 goto out;
326 }
327
328 errno = 0;
329 if (cfsetospeed (&terminal_data, B115200) != 0) {
330 inner_error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
331 "failed to set serial port output speed: %s",
332 g_strerror (errno));
333 goto out;
334 }
335
336 if (tcsetattr (self->priv->fd, TCSANOW, &terminal_data) < 0) {
337 inner_error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
338 "error setting serial port attributes: %s",
339 g_strerror (errno));
340 goto out;
341 }
342
343 out:
344 g_free (path);
345
346 if (inner_error) {
347 if (!(self->priv->fd < 0)) {
348 close (self->priv->fd);
349 self->priv->fd = -1;
350 }
351 g_propagate_error (error, inner_error);
352 return FALSE;
353 }
354
355 return TRUE;
356 }
357
358 /******************************************************************************/
359
360 const gchar *
361 qfu_at_device_get_name (QfuAtDevice *self)
362 {
363 return self->priv->name;
364 }
365
366 /******************************************************************************/
367
368 QfuAtDevice *
369 qfu_at_device_new (GFile *file,
370 GCancellable *cancellable,
371 GError **error)
372 {
373 g_return_val_if_fail (G_IS_FILE (file), NULL);
374
375 return QFU_AT_DEVICE (g_initable_new (QFU_TYPE_AT_DEVICE,
376 cancellable,
377 error,
378 "file", file,
379 NULL));
380 }
381
382
383 static void
384 qfu_at_device_init (QfuAtDevice *self)
385 {
386 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, QFU_TYPE_AT_DEVICE, QfuAtDevicePrivate);
387 self->priv->fd = -1;
388 /* Long buffer for I/O */
389 self->priv->buffer = g_byte_array_new ();
390 g_byte_array_set_size (self->priv->buffer, QFU_AT_BUFFER_SIZE);
391 }
392
393 static void
394 set_property (GObject *object,
395 guint prop_id,
396 const GValue *value,
397 GParamSpec *pspec)
398 {
399 QfuAtDevice *self = QFU_AT_DEVICE (object);
400
401 switch (prop_id) {
402 case PROP_FILE:
403 self->priv->file = g_value_dup_object (value);
404 if (self->priv->file)
405 self->priv->name = g_file_get_basename (self->priv->file);
406 break;
407 default:
408 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
409 break;
410 }
411 }
412
413 static void
414 get_property (GObject *object,
415 guint prop_id,
416 GValue *value,
417 GParamSpec *pspec)
418 {
419 QfuAtDevice *self = QFU_AT_DEVICE (object);
420
421 switch (prop_id) {
422 case PROP_FILE:
423 g_value_set_object (value, self->priv->file);
424 break;
425 default:
426 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
427 break;
428 }
429 }
430
431 static void
432 dispose (GObject *object)
433 {
434 QfuAtDevice *self = QFU_AT_DEVICE (object);
435
436 if (!(self->priv->fd < 0)) {
437 close (self->priv->fd);
438 self->priv->fd = -1;
439 }
440 g_clear_pointer (&self->priv->name, g_free);
441 g_clear_pointer (&self->priv->buffer, g_byte_array_unref);
442 g_clear_object (&self->priv->file);
443
444 G_OBJECT_CLASS (qfu_at_device_parent_class)->dispose (object);
445 }
446
447 static void
448 initable_iface_init (GInitableIface *iface)
449 {
450 iface->init = initable_init;
451 }
452
453 static void
454 qfu_at_device_class_init (QfuAtDeviceClass *klass)
455 {
456 GObjectClass *object_class = G_OBJECT_CLASS (klass);
457
458 g_type_class_add_private (object_class, sizeof (QfuAtDevicePrivate));
459
460 object_class->dispose = dispose;
461 object_class->get_property = get_property;
462 object_class->set_property = set_property;
463
464 properties[PROP_FILE] =
465 g_param_spec_object ("file",
466 "File",
467 "File object",
468 G_TYPE_FILE,
469 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
470 g_object_class_install_property (object_class, PROP_FILE, properties[PROP_FILE]);
471 }