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
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Copyright (C) 2010 Red Hat, Inc.
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation
*
* 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/>.
*/
#include <string.h>
#include "commands.h"
#include "error.h"
#include "dm-commands.h"
#include "nv-items.h"
#include "result-private.h"
#include "utils.h"
/*
* utils_bin2hexstr
*
* Convert a byte-array into a hexadecimal string.
*
* Code originally by Alex Larsson <alexl@redhat.com> and
* copyright Red Hat, Inc. under terms of the LGPL.
*
*/
static char *
bin2hexstr (const guint8 *bytes, int len)
{
static char hex_digits[] = "0123456789abcdef";
char *result;
int i;
gsize buflen = (len * 2) + 1;
g_return_val_if_fail (bytes != NULL, NULL);
g_return_val_if_fail (len > 0, NULL);
g_return_val_if_fail (len < 4096, NULL); /* Arbitrary limit */
result = g_malloc0 (buflen);
for (i = 0; i < len; i++) {
result[2*i] = hex_digits[(bytes[i] >> 4) & 0xf];
result[2*i+1] = hex_digits[bytes[i] & 0xf];
}
result[buflen - 1] = '\0';
return result;
}
static gboolean
check_command (const char *buf, gsize len, guint8 cmd, gsize min_len, GError **error)
{
if (len < 1) {
g_set_error (error, QCDM_COMMAND_ERROR, QCDM_COMMAND_MALFORMED_RESPONSE,
"DM command response malformed (must be at least 1 byte in length)");
return FALSE;
}
switch (buf[0]) {
case DIAG_CMD_BAD_CMD:
g_set_error (error, QCDM_COMMAND_ERROR, QCDM_COMMAND_BAD_COMMAND,
"DM command %d unknown or unimplemented by the device",
cmd);
return FALSE;
case DIAG_CMD_BAD_PARM:
g_set_error (error, QCDM_COMMAND_ERROR, QCDM_COMMAND_BAD_PARAMETER,
"DM command %d contained invalid parameter",
cmd);
return FALSE;
case DIAG_CMD_BAD_LEN:
g_set_error (error, QCDM_COMMAND_ERROR, QCDM_COMMAND_BAD_LENGTH,
"DM command %d was the wrong size",
cmd);
return FALSE;
case DIAG_CMD_BAD_DEV:
g_set_error (error, QCDM_COMMAND_ERROR, QCDM_COMMAND_NOT_ACCEPTED,
"DM command %d was not accepted by the device",
cmd);
return FALSE;
case DIAG_CMD_BAD_MODE:
g_set_error (error, QCDM_COMMAND_ERROR, QCDM_COMMAND_BAD_MODE,
"DM command %d not allowed in the current device mode",
cmd);
return FALSE;
default:
break;
}
if (buf[0] != cmd) {
g_set_error (error, QCDM_COMMAND_ERROR, QCDM_COMMAND_UNEXPECTED,
"Unexpected DM command response (expected %d, got %d)",
cmd, buf[0]);
return FALSE;
}
if (len < min_len) {
g_set_error (error, QCDM_COMMAND_ERROR, QCDM_COMMAND_BAD_LENGTH,
"DM command %d response not long enough (got %zu, expected "
"at least %zu).", cmd, len, min_len);
return FALSE;
}
return TRUE;
}
/**********************************************************************/
gsize
qcdm_cmd_version_info_new (char *buf, gsize len, GError **error)
{
char cmdbuf[3];
DMCmdHeader *cmd = (DMCmdHeader *) &cmdbuf[0];
g_return_val_if_fail (buf != NULL, 0);
g_return_val_if_fail (len >= sizeof (*cmd) + DIAG_TRAILER_LEN, 0);
memset (cmd, 0, sizeof (cmd));
cmd->code = DIAG_CMD_VERSION_INFO;
return dm_encapsulate_buffer (cmdbuf, sizeof (*cmd), sizeof (cmdbuf), buf, len);
}
QCDMResult *
qcdm_cmd_version_info_result (const char *buf, gsize len, GError **error)
{
QCDMResult *result = NULL;
DMCmdVersionInfoRsp *rsp = (DMCmdVersionInfoRsp *) buf;
char tmp[12];
g_return_val_if_fail (buf != NULL, NULL);
if (!check_command (buf, len, DIAG_CMD_VERSION_INFO, sizeof (DMCmdVersionInfoRsp), error))
return NULL;
result = qcdm_result_new ();
memset (tmp, 0, sizeof (tmp));
g_assert (sizeof (rsp->comp_date) <= sizeof (tmp));
memcpy (tmp, rsp->comp_date, sizeof (rsp->comp_date));
qcdm_result_add_string (result, QCDM_CMD_VERSION_INFO_ITEM_COMP_DATE, tmp);
memset (tmp, 0, sizeof (tmp));
g_assert (sizeof (rsp->comp_time) <= sizeof (tmp));
memcpy (tmp, rsp->comp_time, sizeof (rsp->comp_time));
qcdm_result_add_string (result, QCDM_CMD_VERSION_INFO_ITEM_COMP_TIME, tmp);
memset (tmp, 0, sizeof (tmp));
g_assert (sizeof (rsp->rel_date) <= sizeof (tmp));
memcpy (tmp, rsp->rel_date, sizeof (rsp->rel_date));
qcdm_result_add_string (result, QCDM_CMD_VERSION_INFO_ITEM_RELEASE_DATE, tmp);
memset (tmp, 0, sizeof (tmp));
g_assert (sizeof (rsp->rel_time) <= sizeof (tmp));
memcpy (tmp, rsp->rel_time, sizeof (rsp->rel_time));
qcdm_result_add_string (result, QCDM_CMD_VERSION_INFO_ITEM_RELEASE_TIME, tmp);
memset (tmp, 0, sizeof (tmp));
g_assert (sizeof (rsp->model) <= sizeof (tmp));
memcpy (tmp, rsp->model, sizeof (rsp->model));
qcdm_result_add_string (result, QCDM_CMD_VERSION_INFO_ITEM_MODEL, tmp);
return result;
}
/**********************************************************************/
gsize
qcdm_cmd_esn_new (char *buf, gsize len, GError **error)
{
char cmdbuf[3];
DMCmdHeader *cmd = (DMCmdHeader *) &cmdbuf[0];
g_return_val_if_fail (buf != NULL, 0);
g_return_val_if_fail (len >= sizeof (*cmd) + DIAG_TRAILER_LEN, 0);
memset (cmd, 0, sizeof (cmd));
cmd->code = DIAG_CMD_ESN;
return dm_encapsulate_buffer (cmdbuf, sizeof (*cmd), sizeof (cmdbuf), buf, len);
}
QCDMResult *
qcdm_cmd_esn_result (const char *buf, gsize len, GError **error)
{
QCDMResult *result = NULL;
DMCmdEsnRsp *rsp = (DMCmdEsnRsp *) buf;
char *tmp;
guint8 swapped[4];
g_return_val_if_fail (buf != NULL, NULL);
if (!check_command (buf, len, DIAG_CMD_ESN, sizeof (DMCmdEsnRsp), error))
return NULL;
result = qcdm_result_new ();
/* Convert the ESN from binary to a hex string; it's LE so we have to
* swap it to get the correct ordering.
*/
swapped[0] = rsp->esn[3];
swapped[1] = rsp->esn[2];
swapped[2] = rsp->esn[1];
swapped[3] = rsp->esn[0];
tmp = bin2hexstr (&swapped[0], sizeof (swapped));
qcdm_result_add_string (result, QCDM_CMD_ESN_ITEM_ESN, tmp);
g_free (tmp);
return result;
}
/**********************************************************************/
gsize
qcdm_cmd_nv_get_mdn_new (char *buf, gsize len, guint8 profile, GError **error)
{
char cmdbuf[sizeof (DMCmdNVReadWrite) + DIAG_TRAILER_LEN];
DMCmdNVReadWrite *cmd = (DMCmdNVReadWrite *) &cmdbuf[0];
DMNVItemMdn *req;
g_return_val_if_fail (buf != NULL, 0);
g_return_val_if_fail (len >= sizeof (*cmd) + DIAG_TRAILER_LEN, 0);
memset (cmd, 0, sizeof (*cmd));
cmd->code = DIAG_CMD_NV_READ;
cmd->nv_item = GUINT16_TO_LE (DIAG_NV_DIR_NUMBER);
req = (DMNVItemMdn *) &cmd->data[0];
req->profile = profile;
return dm_encapsulate_buffer (cmdbuf, sizeof (*cmd), sizeof (cmdbuf), buf, len);
}
QCDMResult *
qcdm_cmd_nv_get_mdn_result (const char *buf, gsize len, GError **error)
{
QCDMResult *result = NULL;
DMCmdNVReadWrite *rsp = (DMCmdNVReadWrite *) buf;
DMNVItemMdn *mdn;
char tmp[11];
g_return_val_if_fail (buf != NULL, NULL);
if (!check_command (buf, len, DIAG_CMD_NV_READ, sizeof (DMCmdNVReadWrite), error))
return NULL;
mdn = (DMNVItemMdn *) &rsp->data[0];
result = qcdm_result_new ();
qcdm_result_add_uint8 (result, QCDM_CMD_NV_GET_MDN_ITEM_PROFILE, mdn->profile);
memset (tmp, 0, sizeof (tmp));
g_assert (sizeof (mdn->mdn) <= sizeof (tmp));
memcpy (tmp, mdn->mdn, sizeof (mdn->mdn));
qcdm_result_add_string (result, QCDM_CMD_NV_GET_MDN_ITEM_MDN, tmp);
return result;
}
/**********************************************************************/
|