blob: f06af7e14e0371577cc2f35dc65952532309ca32 [file] [log] [blame]
Paul Cercueilbb4401d2014-02-28 16:10:49 +01001/*
2 * libiio - Library for interfacing industrial I/O (IIO) devices
3 *
4 * Copyright (C) 2014 Analog Devices, Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * */
18
Paul Cercueil5d9db302014-02-18 18:16:51 +010019#include "debug.h"
20#include "iio-private.h"
21
22#include <errno.h>
23#include <libxml/tree.h>
24#include <string.h>
25
Paul Cercueil5d9db302014-02-18 18:16:51 +010026static int add_attr_to_channel(struct iio_channel *chn, xmlNode *n)
27{
28 xmlAttr *attr;
Paul Cercueil42d12352014-05-05 16:11:58 +020029 char *name = NULL, *filename = NULL;
Paul Cercueilb34e0222014-05-05 15:32:38 +020030 struct iio_channel_attr *attrs;
Paul Cercueil5d9db302014-02-18 18:16:51 +010031
32 for (attr = n->properties; attr; attr = attr->next) {
33 if (!strcmp((char *) attr->name, "name")) {
Paul Cercueilcaf0e712016-08-25 17:27:02 +020034 name = iio_strdup((char *) attr->children->content);
Paul Cercueil42d12352014-05-05 16:11:58 +020035 } else if (!strcmp((char *) attr->name, "filename")) {
Paul Cercueilcaf0e712016-08-25 17:27:02 +020036 filename = iio_strdup((char *) attr->children->content);
Paul Cercueil5d9db302014-02-18 18:16:51 +010037 } else {
38 WARNING("Unknown field \'%s\' in channel %s\n",
39 attr->name, chn->id);
40 }
41 }
42
Paul Cercueil7150c6e2015-04-14 16:26:21 +020043 if (!name) {
Paul Cercueil5d9db302014-02-18 18:16:51 +010044 ERROR("Incomplete attribute in channel %s\n", chn->id);
45 goto err_free;
46 }
47
Paul Cercueil7150c6e2015-04-14 16:26:21 +020048 if (!filename) {
Paul Cercueilcaf0e712016-08-25 17:27:02 +020049 filename = iio_strdup(name);
Paul Cercueil7150c6e2015-04-14 16:26:21 +020050 if (!filename)
51 goto err_free;
52 }
53
Paul Cercueilb34e0222014-05-05 15:32:38 +020054 attrs = realloc(chn->attrs, (1 + chn->nb_attrs) *
55 sizeof(struct iio_channel_attr));
Paul Cercueil5d9db302014-02-18 18:16:51 +010056 if (!attrs)
57 goto err_free;
58
Paul Cercueil42d12352014-05-05 16:11:58 +020059 attrs[chn->nb_attrs].filename = filename;
Paul Cercueilb34e0222014-05-05 15:32:38 +020060 attrs[chn->nb_attrs++].name = name;
Paul Cercueil5d9db302014-02-18 18:16:51 +010061 chn->attrs = attrs;
62 return 0;
63
Paul Cercueil5d9db302014-02-18 18:16:51 +010064err_free:
65 if (name)
66 free(name);
Paul Cercueil42d12352014-05-05 16:11:58 +020067 if (filename)
68 free(filename);
Paul Cercueil5d9db302014-02-18 18:16:51 +010069 return -1;
70}
71
Paul Cercueil501961b2014-04-15 11:23:30 +020072static int add_attr_to_device(struct iio_device *dev, xmlNode *n, bool is_debug)
Paul Cercueil5d9db302014-02-18 18:16:51 +010073{
74 xmlAttr *attr;
Paul Cercueil5cd260e2014-02-24 13:16:01 +010075 char **attrs, *name = NULL;
Paul Cercueil5d9db302014-02-18 18:16:51 +010076
77 for (attr = n->properties; attr; attr = attr->next) {
78 if (!strcmp((char *) attr->name, "name")) {
Paul Cercueilcaf0e712016-08-25 17:27:02 +020079 name = iio_strdup((char *) attr->children->content);
Paul Cercueil5d9db302014-02-18 18:16:51 +010080 } else {
81 WARNING("Unknown field \'%s\' in device %s\n",
82 attr->name, dev->id);
83 }
84 }
85
Paul Cercueil5cd260e2014-02-24 13:16:01 +010086 if (!name) {
Paul Cercueil5d9db302014-02-18 18:16:51 +010087 ERROR("Incomplete attribute in device %s\n", dev->id);
88 goto err_free;
89 }
90
Paul Cercueil501961b2014-04-15 11:23:30 +020091 if (is_debug)
92 attrs = realloc(dev->debug_attrs,
93 (1 + dev->nb_debug_attrs) * sizeof(char *));
94 else
95 attrs = realloc(dev->attrs,
96 (1 + dev->nb_attrs) * sizeof(char *));
Paul Cercueil5d9db302014-02-18 18:16:51 +010097 if (!attrs)
98 goto err_free;
99
Paul Cercueil501961b2014-04-15 11:23:30 +0200100 if (is_debug) {
101 attrs[dev->nb_debug_attrs++] = name;
102 dev->debug_attrs = attrs;
103 } else {
104 attrs[dev->nb_attrs++] = name;
105 dev->attrs = attrs;
106 }
Paul Cercueil5d9db302014-02-18 18:16:51 +0100107 return 0;
108
Paul Cercueil5d9db302014-02-18 18:16:51 +0100109err_free:
110 if (name)
111 free(name);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100112 return -1;
113}
114
Paul Cercueila7e80022014-06-11 11:41:26 +0200115static void setup_scan_element(struct iio_channel *chn, xmlNode *n)
116{
117 xmlAttr *attr;
118
119 for (attr = n->properties; attr; attr = attr->next) {
120 const char *name = (const char *) attr->name,
121 *content = (const char *) attr->children->content;
122 if (!strcmp(name, "index")) {
123 chn->index = atol(content);
124 } else if (!strcmp(name, "format")) {
125 char e, s;
Paul Cercueil04030a72014-09-03 11:27:21 +0200126 sscanf(content, "%ce:%c%u/%u>>%u", &e, &s,
Paul Cercueila7e80022014-06-11 11:41:26 +0200127 &chn->format.bits,
128 &chn->format.length,
129 &chn->format.shift);
130 chn->format.is_be = e == 'b';
Michael Hennerichfa3c6f62014-08-13 11:21:23 +0200131 chn->format.is_signed = (s == 's' || s == 'S');
132 chn->format.is_fully_defined = (s == 'S' || s == 'U' ||
133 chn->format.bits == chn->format.length);
Paul Cercueila7e80022014-06-11 11:41:26 +0200134 } else if (!strcmp(name, "scale")) {
135 chn->format.with_scale = true;
136 chn->format.scale = atof(content);
137 } else {
138 WARNING("Unknown attribute \'%s\' in <scan-element>\n",
139 name);
140 }
141 }
142}
143
Paul Cercueil5d9db302014-02-18 18:16:51 +0100144static struct iio_channel * create_channel(struct iio_device *dev, xmlNode *n)
145{
146 xmlAttr *attr;
Lars-Peter Clausend1be8382016-02-24 11:13:45 +0100147 struct iio_channel *chn = zalloc(sizeof(*chn));
Paul Cercueil5d9db302014-02-18 18:16:51 +0100148 if (!chn)
149 return NULL;
150
151 chn->dev = dev;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100152
Paul Cercueild8d58932014-06-12 10:08:36 +0200153 /* Set the default index value < 0 (== no index) */
154 chn->index = -ENOENT;
155
Paul Cercueil5d9db302014-02-18 18:16:51 +0100156 for (attr = n->properties; attr; attr = attr->next) {
Paul Cercueil35a01312014-02-20 10:56:57 +0100157 const char *name = (const char *) attr->name,
158 *content = (const char *) attr->children->content;
159 if (!strcmp(name, "name")) {
Paul Cercueilcaf0e712016-08-25 17:27:02 +0200160 chn->name = iio_strdup(content);
Paul Cercueil35a01312014-02-20 10:56:57 +0100161 } else if (!strcmp(name, "id")) {
Paul Cercueilcaf0e712016-08-25 17:27:02 +0200162 chn->id = iio_strdup(content);
Paul Cercueil35a01312014-02-20 10:56:57 +0100163 } else if (!strcmp(name, "type")) {
164 if (!strcmp(content, "output"))
165 chn->is_output = true;
166 else if (strcmp(content, "input"))
167 WARNING("Unknown channel type %s\n", content);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100168 } else {
169 WARNING("Unknown attribute \'%s\' in <channel>\n",
Paul Cercueil35a01312014-02-20 10:56:57 +0100170 name);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100171 }
172 }
173
174 if (!chn->id) {
175 ERROR("Incomplete <attribute>\n");
176 goto err_free_channel;
177 }
178
179 for (n = n->children; n; n = n->next) {
180 if (!strcmp((char *) n->name, "attribute")) {
181 if (add_attr_to_channel(chn, n) < 0)
182 goto err_free_channel;
Paul Cercueila7e80022014-06-11 11:41:26 +0200183 } else if (!strcmp((char *) n->name, "scan-element")) {
184 chn->is_scan_element = true;
185 setup_scan_element(chn, n);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100186 } else if (strcmp((char *) n->name, "text")) {
Paul Cercueilf7361902015-04-16 16:31:49 +0200187 WARNING("Unknown children \'%s\' in <channel>\n",
Paul Cercueil5d9db302014-02-18 18:16:51 +0100188 n->name);
189 continue;
190 }
191 }
192
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +0200193 iio_channel_init_finalize(chn);
194
Paul Cercueil5d9db302014-02-18 18:16:51 +0100195 return chn;
196
197err_free_channel:
198 free_channel(chn);
199 return NULL;
200}
201
202static struct iio_device * create_device(struct iio_context *ctx, xmlNode *n)
203{
204 xmlAttr *attr;
Lars-Peter Clausend1be8382016-02-24 11:13:45 +0100205 struct iio_device *dev = zalloc(sizeof(*dev));
Paul Cercueil5d9db302014-02-18 18:16:51 +0100206 if (!dev)
207 return NULL;
208
209 dev->ctx = ctx;
210
211 for (attr = n->properties; attr; attr = attr->next) {
212 if (!strcmp((char *) attr->name, "name")) {
Paul Cercueilcaf0e712016-08-25 17:27:02 +0200213 dev->name = iio_strdup(
214 (char *) attr->children->content);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100215 } else if (!strcmp((char *) attr->name, "id")) {
Paul Cercueilcaf0e712016-08-25 17:27:02 +0200216 dev->id = iio_strdup((char *) attr->children->content);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100217 } else {
Paul Cercueilf7361902015-04-16 16:31:49 +0200218 WARNING("Unknown attribute \'%s\' in <device>\n",
Paul Cercueil5d9db302014-02-18 18:16:51 +0100219 attr->name);
220 }
221 }
222
223 if (!dev->id) {
224 ERROR("Unable to read device ID\n");
225 goto err_free_device;
226 }
227
228 for (n = n->children; n; n = n->next) {
229 if (!strcmp((char *) n->name, "channel")) {
230 struct iio_channel **chns,
231 *chn = create_channel(dev, n);
232 if (!chn) {
233 ERROR("Unable to create channel\n");
234 goto err_free_device;
235 }
236
237 chns = realloc(dev->channels, (1 + dev->nb_channels) *
238 sizeof(struct iio_channel *));
239 if (!chns) {
240 ERROR("Unable to allocate memory\n");
241 free(chn);
242 goto err_free_device;
243 }
244
245 chns[dev->nb_channels++] = chn;
246 dev->channels = chns;
247 } else if (!strcmp((char *) n->name, "attribute")) {
Paul Cercueil501961b2014-04-15 11:23:30 +0200248 if (add_attr_to_device(dev, n, false) < 0)
249 goto err_free_device;
250 } else if (!strcmp((char *) n->name, "debug-attribute")) {
251 if (add_attr_to_device(dev, n, true) < 0)
Paul Cercueil5d9db302014-02-18 18:16:51 +0100252 goto err_free_device;
253 } else if (strcmp((char *) n->name, "text")) {
254 WARNING("Unknown children \'%s\' in <device>\n",
255 n->name);
256 continue;
257 }
258 }
259
Paul Cercueiledec1cb2015-04-20 10:40:28 +0200260 dev->words = (dev->nb_channels + 31) / 32;
261 if (dev->words) {
262 dev->mask = calloc(dev->words, sizeof(*dev->mask));
263 if (!dev->mask) {
264 errno = ENOMEM;
265 goto err_free_device;
266 }
267 }
268
Paul Cercueil5d9db302014-02-18 18:16:51 +0100269 return dev;
270
271err_free_device:
272 free_device(dev);
273 return NULL;
274}
275
Paul Cercueil927818d2014-10-28 14:38:24 +0100276static struct iio_context * xml_clone(const struct iio_context *ctx)
277{
Paul Cercueil63e52182014-12-11 12:52:48 +0100278 return xml_create_context_mem(ctx->xml, strlen(ctx->xml));
Paul Cercueil927818d2014-10-28 14:38:24 +0100279}
280
Lars-Peter Clausen09a59d72016-02-03 15:27:04 +0100281static const struct iio_backend_ops xml_ops = {
Paul Cercueil927818d2014-10-28 14:38:24 +0100282 .clone = xml_clone,
Paul Cercueil5d9db302014-02-18 18:16:51 +0100283};
284
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100285static struct iio_context * iio_create_xml_context_helper(xmlDoc *doc)
Paul Cercueil5d9db302014-02-18 18:16:51 +0100286{
287 unsigned int i;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100288 xmlNode *root, *n;
Paul Cercueil60ac1f62015-03-16 14:09:54 +0100289 xmlAttr *attr;
Paul Cercueil8a85d3e2015-03-16 17:11:49 +0100290 int err = ENOMEM;
Lars-Peter Clausend1be8382016-02-24 11:13:45 +0100291 struct iio_context *ctx = zalloc(sizeof(*ctx));
Paul Cercueil5d9db302014-02-18 18:16:51 +0100292 if (!ctx)
Paul Cercueil8a85d3e2015-03-16 17:11:49 +0100293 goto err_set_errno;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100294
Paul Cercueil5d9db302014-02-18 18:16:51 +0100295 ctx->name = "xml";
296 ctx->ops = &xml_ops;
297
Paul Cercueil5d9db302014-02-18 18:16:51 +0100298 root = xmlDocGetRootElement(doc);
299 if (strcmp((char *) root->name, "context")) {
300 ERROR("Unrecognized XML file\n");
Paul Cercueil8a85d3e2015-03-16 17:11:49 +0100301 err = EINVAL;
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100302 goto err_free_ctx;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100303 }
304
Paul Cercueil60ac1f62015-03-16 14:09:54 +0100305 for (attr = root->properties; attr; attr = attr->next) {
306 if (!strcmp((char *) attr->name, "description"))
Paul Cercueilcaf0e712016-08-25 17:27:02 +0200307 ctx->description = iio_strdup(
Paul Cercueil60ac1f62015-03-16 14:09:54 +0100308 (char *) attr->children->content);
309 else if (strcmp((char *) attr->name, "name"))
310 WARNING("Unknown parameter \'%s\' in <context>\n",
311 (char *) attr->children->content);
312 }
313
Paul Cercueil5d9db302014-02-18 18:16:51 +0100314 for (n = root->children; n; n = n->next) {
315 struct iio_device **devs, *dev;
316
317 if (strcmp((char *) n->name, "device")) {
318 if (strcmp((char *) n->name, "text"))
319 WARNING("Unknown children \'%s\' in "
320 "<context>\n", n->name);
321 continue;
322 }
323
324 dev = create_device(ctx, n);
325 if (!dev) {
326 ERROR("Unable to create device\n");
327 goto err_free_devices;
328 }
329
330 devs = realloc(ctx->devices, (1 + ctx->nb_devices) *
331 sizeof(struct iio_device *));
332 if (!devs) {
333 ERROR("Unable to allocate memory\n");
334 free(dev);
335 goto err_free_devices;
336 }
337
338 devs[ctx->nb_devices++] = dev;
339 ctx->devices = devs;
340 }
341
Paul Cercueilfd387472015-08-05 10:34:19 +0200342 err = iio_context_init(ctx);
343 if (err)
344 goto err_free_devices;
345
Paul Cercueilc1ed8482014-06-11 16:29:43 +0200346 return ctx;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100347
348err_free_devices:
349 for (i = 0; i < ctx->nb_devices; i++)
350 free_device(ctx->devices[i]);
351 if (ctx->nb_devices)
352 free(ctx->devices);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100353err_free_ctx:
354 free(ctx);
Paul Cercueil8a85d3e2015-03-16 17:11:49 +0100355err_set_errno:
356 errno = err;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100357 return NULL;
358}
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100359
Paul Cercueil63e52182014-12-11 12:52:48 +0100360struct iio_context * xml_create_context(const char *xml_file)
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100361{
362 struct iio_context *ctx;
363 xmlDoc *doc;
364
365 LIBXML_TEST_VERSION;
366
367 doc = xmlReadFile(xml_file, NULL, XML_PARSE_DTDVALID);
368 if (!doc) {
369 ERROR("Unable to parse XML file\n");
370 return NULL;
371 }
372
373 ctx = iio_create_xml_context_helper(doc);
374 xmlFreeDoc(doc);
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100375 return ctx;
376}
377
Paul Cercueil63e52182014-12-11 12:52:48 +0100378struct iio_context * xml_create_context_mem(const char *xml, size_t len)
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100379{
380 struct iio_context *ctx;
381 xmlDoc *doc;
382
383 LIBXML_TEST_VERSION;
384
Paul Cercueil4012cff2015-05-11 10:47:40 +0200385 doc = xmlReadMemory(xml, (int) len, NULL, NULL, XML_PARSE_DTDVALID);
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100386 if (!doc) {
387 ERROR("Unable to parse XML file\n");
388 return NULL;
389 }
390
391 ctx = iio_create_xml_context_helper(doc);
392 xmlFreeDoc(doc);
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100393 return ctx;
394}