Debian Codegraph @

src/qmi-firmware-update/qfu-dload-message.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) 2016 Bjørn Mork <[email protected]>
19 * Copyright (C) 2016 Zodiac Inflight Innovations
20 * Copyright (C) 2016-2017 Aleksander Morgado <[email protected]>
21 */
22
23 #include <string.h>
24
25 #include <glib-object.h>
26 #include <gio/gio.h>
27
28 #include "qfu-dload-message.h"
29 #include "qfu-utils.h"
30 #include "qfu-enum-types.h"
31
32 /******************************************************************************/
33 /* DLOAD SDP */
34
35 /* Generic message for operations that just require the command id */
36 typedef struct _DloadSdpReq DloadSdpReq;
37 struct _DloadSdpReq {
38 guint8 cmd;
39 guint16 reserved;
40 } __attribute__ ((packed));
41
42 G_STATIC_ASSERT (sizeof (DloadSdpReq) <= QFU_DLOAD_MESSAGE_MAX_SIZE);
43
44 gsize
45 qfu_dload_request_sdp_build (guint8 *buffer,
46 gsize buffer_len)
47 {
48 DloadSdpReq *req;
49
50 g_assert (buffer_len >= sizeof (DloadSdpReq));
51
52 /* Create request */
53 req = (DloadSdpReq *) buffer;
54 memset (buffer, 0, sizeof (DloadSdpReq));
55 req->cmd = (guint8) QFU_DLOAD_CMD_SDP;
56
57 g_debug ("[qfu,dload-message] sent %s:", qfu_dload_cmd_get_string ((QfuDloadCmd) req->cmd));
58
59 return (sizeof (DloadSdpReq));
60 }
61
62 /******************************************************************************/
63 /* DLOAD Ack */
64
65 typedef struct _DloadAckRsp DloadAckRsp;
66 struct _DloadAckRsp {
67 guint8 cmd; /* 0x2a */
68 } __attribute__ ((packed));
69
70 G_STATIC_ASSERT (sizeof (DloadAckRsp) <= QFU_DLOAD_MESSAGE_MAX_SIZE);
71
72 gboolean
73 qfu_dload_response_ack_parse (const guint8 *buffer,
74 gsize buffer_len,
75 GError **error)
76 {
77 DloadAckRsp *rsp;
78
79 if (buffer_len != sizeof (DloadAckRsp)) {
80 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
81 "message size mismatch: %" G_GSIZE_FORMAT " != %" G_GSIZE_FORMAT,
82 buffer_len, sizeof (DloadAckRsp));
83 return FALSE;
84 }
85
86 rsp = (DloadAckRsp *) buffer;
87 g_assert (rsp->cmd == QFU_DLOAD_CMD_ACK);
88
89 g_debug ("[qfu,dload-message] received %s:", qfu_dload_cmd_get_string ((QfuDloadCmd) rsp->cmd));
90
91 return TRUE;
92 }