-
Notifications
You must be signed in to change notification settings - Fork 317
/
xml.c
454 lines (396 loc) · 10.8 KB
/
xml.c
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2014 Analog Devices, Inc.
* Author: Paul Cercueil <paul.cercueil@analog.com>
*/
#include <errno.h>
#include <iio/iio-backend.h>
#include <iio/iio-debug.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <string.h>
#define XML_HEADER "<?xml version=\"1.0\""
static struct iio_context *
xml_create_context(const struct iio_context_params *params,
const char *xml_file);
static int add_attr_to_channel(struct iio_channel *chn, xmlNode *n)
{
const char *name = NULL, *filename = NULL;
xmlAttr *attr;
for (attr = n->properties; attr; attr = attr->next) {
if (!strcmp((const char *)attr->name, "name")) {
name = (const char *)attr->children->content;
} else if (!strcmp((const char *)attr->name, "filename")) {
filename = (const char *)attr->children->content;
} else {
chn_dbg(chn, "Unknown field \'%s\'\n", attr->name);
}
}
if (!name) {
chn_err(chn, "Incomplete attribute\n");
return -EINVAL;
}
return iio_channel_add_attr(chn, name, filename);
}
static int add_attr_to_device(struct iio_device *dev, xmlNode *n, enum iio_attr_type type)
{
xmlAttr *attr;
char *name = NULL;
for (attr = n->properties; attr; attr = attr->next) {
if (!strcmp((char *) attr->name, "name")) {
name = (char *) attr->children->content;
} else {
dev_dbg(dev, "Unknown field \'%s\'\n", attr->name);
}
}
if (!name) {
dev_err(dev, "Incomplete attribute\n");
return -EINVAL;
}
return iio_device_add_attr(dev, name, type);
}
static int setup_scan_element(const struct iio_device *dev,
xmlNode *n, long *index,
struct iio_data_format *fmt)
{
xmlAttr *attr;
int err;
for (attr = n->properties; attr; attr = attr->next) {
const char *name = (const char *) attr->name,
*content = (const char *) attr->children->content;
if (!strcmp(name, "index")) {
char *end;
long long value;
errno = 0;
value = strtoll(content, &end, 0);
if (end == content || value < 0 || errno == ERANGE)
return -EINVAL;
*index = (long)value;
} else if (!strcmp(name, "format")) {
char e, s;
if (strchr(content, 'X')) {
err = iio_sscanf(content, "%ce:%c%u/%uX%u>>%u",
#ifdef _MSC_BUILD
&e, (unsigned int)sizeof(e),
&s, (unsigned int)sizeof(s),
#else
&e, &s,
#endif
&fmt->bits,
&fmt->length,
&fmt->repeat,
&fmt->shift);
if (err != 6)
return -EINVAL;
} else {
fmt->repeat = 1;
err = iio_sscanf(content, "%ce:%c%u/%u>>%u",
#ifdef _MSC_BUILD
&e, (unsigned int)sizeof(e),
&s, (unsigned int)sizeof(s),
#else
&e, &s,
#endif
&fmt->bits,
&fmt->length,
&fmt->shift);
if (err != 5)
return -EINVAL;
}
fmt->is_be = e == 'b';
fmt->is_signed = (s == 's' || s == 'S');
fmt->is_fully_defined = (s == 'S' || s == 'U' ||
fmt->bits == fmt->length);
} else if (!strcmp(name, "scale")) {
char *end;
float value;
errno = 0;
value = strtof(content, &end);
if (end == content || errno == ERANGE) {
fmt->with_scale = false;
return -EINVAL;
}
fmt->with_scale = true;
fmt->scale = value;
} else {
dev_dbg(dev, "Unknown attribute \'%s\' in <scan-element>\n",
name);
}
}
return 0;
}
static int create_channel(struct iio_device *dev, xmlNode *node)
{
xmlAttr *attr;
struct iio_channel *chn;
int err = -ENOMEM;
char *name_ptr = NULL, *id_ptr = NULL;
bool output = false;
bool scan_element = false;
long index = -ENOENT;
struct iio_data_format format = { 0 };
xmlNode *n;
for (attr = node->properties; attr; attr = attr->next) {
const char *name = (const char *) attr->name,
*content = (const char *) attr->children->content;
if (!strcmp(name, "name")) {
name_ptr = iio_strdup(content);
if (!name_ptr)
goto err_free_name_id;
} else if (!strcmp(name, "id")) {
id_ptr = iio_strdup(content);
if (!id_ptr)
goto err_free_name_id;
} else if (!strcmp(name, "type")) {
if (!strcmp(content, "output"))
output = true;
else if (strcmp(content, "input"))
dev_dbg(dev, "Unknown channel type %s\n", content);
} else {
dev_dbg(dev, "Unknown attribute \'%s\' in <channel>\n",
name);
}
}
if (!id_ptr) {
dev_err(dev, "Incomplete <attribute>\n");
err = -EINVAL;
goto err_free_name_id;
}
for (n = node->children; n; n = n->next) {
if (!strcmp((char *) n->name, "scan-element")) {
scan_element = true;
err = setup_scan_element(dev, n, &index, &format);
if (err < 0)
goto err_free_name_id;
break;
}
}
chn = iio_device_add_channel(dev, index, id_ptr, name_ptr, output,
scan_element, &format);
if (!chn) {
err = -ENOMEM;
goto err_free_name_id;
}
free(name_ptr);
free(id_ptr);
for (n = node->children; n; n = n->next) {
if (!strcmp((char *) n->name, "attribute")) {
err = add_attr_to_channel(chn, n);
if (err < 0)
return err;
} else if (strcmp((char *) n->name, "scan-element")
&& strcmp((char *) n->name, "text")) {
chn_dbg(chn, "Unknown children \'%s\' in <channel>\n",
n->name);
continue;
}
}
return 0;
err_free_name_id:
free(name_ptr);
free(id_ptr);
return err;
}
static int create_device(struct iio_context *ctx, xmlNode *n)
{
xmlAttr *attr;
struct iio_device *dev;
int err = -ENOMEM;
char *name = NULL, *label = NULL, *id = NULL;
for (attr = n->properties; attr; attr = attr->next) {
if (!strcmp((char *) attr->name, "name")) {
name = iio_strdup((char *) attr->children->content);
if (!name)
goto err_free_name_label_id;
} else if (!strcmp((char *) attr->name, "label")) {
label = iio_strdup((char *) attr->children->content);
if (!label)
goto err_free_name_label_id;
} else if (!strcmp((char *) attr->name, "id")) {
id = iio_strdup((char *) attr->children->content);
if (!id)
goto err_free_name_label_id;
} else {
ctx_dbg(ctx, "Unknown attribute \'%s\' in <device>\n",
attr->name);
}
}
if (!id) {
ctx_err(ctx, "Unable to read device ID\n");
err = -EINVAL;
goto err_free_name_label_id;
}
dev = iio_context_add_device(ctx, id, name, label);
if (!dev)
goto err_free_name_label_id;
/* Those have been duplicated into the iio_device. */
free(name);
free(label);
free(id);
for (n = n->children; n; n = n->next) {
if (!strcmp((char *) n->name, "channel")) {
err = create_channel(dev, n);
if (err) {
dev_perror(dev, err, "Unable to create channel");
return err;
}
} else if (!strcmp((char *) n->name, "attribute")) {
err = add_attr_to_device(dev, n, IIO_ATTR_TYPE_DEVICE);
if (err < 0)
return err;
} else if (!strcmp((char *) n->name, "debug-attribute")) {
err = add_attr_to_device(dev, n, IIO_ATTR_TYPE_DEBUG);
if (err < 0)
return err;
} else if (!strcmp((char *) n->name, "buffer-attribute")) {
err = add_attr_to_device(dev, n, IIO_ATTR_TYPE_BUFFER);
if (err < 0)
return err;
} else if (strcmp((char *) n->name, "text")) {
dev_dbg(dev, "Unknown children \'%s\' in <device>\n",
n->name);
continue;
}
}
return 0;
err_free_name_label_id:
free(name);
free(label);
free(id);
return err;
}
static const struct iio_backend_ops xml_ops = {
.create = xml_create_context,
};
const struct iio_backend iio_xml_backend = {
.api_version = IIO_BACKEND_API_V1,
.name = "xml",
.uri_prefix = "xml:",
.ops = &xml_ops,
};
static int parse_context_attr(struct iio_context *ctx, xmlNode *n)
{
xmlAttr *attr;
const char *name = NULL, *value = NULL;
for (attr = n->properties; attr; attr = attr->next) {
if (!strcmp((const char *) attr->name, "name")) {
name = (const char *) attr->children->content;
} else if (!strcmp((const char *) attr->name, "value")) {
value = (const char *) attr->children->content;
}
}
if (!name || !value)
return -EINVAL;
else
return iio_context_add_attr(ctx, name, value);
}
static int iio_populate_xml_context_helper(struct iio_context *ctx, xmlNode *root)
{
xmlNode *n;
int err;
for (n = root->children; n; n = n->next) {
if (!strcmp((char *) n->name, "context-attribute")) {
err = parse_context_attr(ctx, n);
if (err)
return err;
continue;
} else if (strcmp((char *) n->name, "device")) {
if (strcmp((char *) n->name, "text"))
ctx_dbg(ctx, "Unknown children \'%s\' in "
"<context>\n", n->name);
continue;
}
err = create_device(ctx, n);
if (err) {
ctx_perror(ctx, err, "Unable to create device");
return err;
}
}
return 0;
}
static struct iio_context *
iio_create_xml_context_helper(const struct iio_context_params *params,
xmlDoc *doc)
{
const char *description = NULL, *git_tag = NULL, *content;
struct iio_context *ctx;
long major = 0, minor = 0;
xmlNode *root;
xmlAttr *attr;
char *end;
int err;
root = xmlDocGetRootElement(doc);
if (strcmp((char *) root->name, "context")) {
prm_err(params, "Unrecognized XML file\n");
return iio_ptr(-EINVAL);
}
for (attr = root->properties; attr; attr = attr->next) {
content = (const char *) attr->children->content;
if (!strcmp((char *) attr->name, "description")) {
description = content;
} else if (!strcmp((char *) attr->name, "version-major")) {
errno = 0;
major = strtol(content, &end, 10);
if (*end != '\0' || errno == ERANGE)
prm_warn(params, "invalid format for major version\n");
} else if (!strcmp((char *) attr->name, "version-minor")) {
errno = 0;
minor = strtol(content, &end, 10);
if (*end != '\0' || errno == ERANGE)
prm_warn(params, "invalid format for minor version\n");
} else if (!strcmp((char *) attr->name, "version-git")) {
git_tag = content;
} else if (strcmp((char *) attr->name, "name")) {
prm_dbg(params, "Unknown parameter \'%s\' in <context>\n",
content);
}
}
ctx = iio_context_create_from_backend(params, &iio_xml_backend,
description,
major, minor, git_tag);
err = iio_err(ctx);
if (err) {
prm_err(params, "Unable to allocate memory for context\n");
return iio_ptr(err);
}
err = iio_populate_xml_context_helper(ctx, root);
if (err) {
iio_context_destroy(ctx);
return iio_ptr(err);
}
return ctx;
}
static struct iio_context *
xml_create_context(const struct iio_context_params *params, const char *arg)
{
struct iio_context *ctx;
xmlDoc *doc;
LIBXML_TEST_VERSION;
if (!strncmp(arg, XML_HEADER, sizeof(XML_HEADER) - 1)) {
doc = xmlReadMemory(arg, (int) strlen(arg),
NULL, NULL, XML_PARSE_DTDVALID);
} else {
doc = xmlReadFile(arg, NULL, XML_PARSE_DTDVALID);
}
if (!doc) {
prm_err(params, "Unable to parse XML file\n");
return iio_ptr(-EINVAL);
}
ctx = iio_create_xml_context_helper(params, doc);
xmlFreeDoc(doc);
return ctx;
}
void libiio_cleanup_xml_backend(void)
{
/*
* This function will be called only when the libiio library is
* unloaded (e.g. when the program exits).
*
* Cleanup libxml2 so that memory analyzer tools like Valgrind won't
* detect a memory leak.
*/
xmlCleanupParser();
xmlMemoryDump();
}