blob: 5c288b403f3cdf283f0cf6bc23d350d1d9c90852 [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 Cercueild4378e72014-05-30 13:37:34 +020026#ifndef _WIN32
27#define _strdup strdup
28#endif
29
Paul Cercueil5d9db302014-02-18 18:16:51 +010030static int add_attr_to_channel(struct iio_channel *chn, xmlNode *n)
31{
32 xmlAttr *attr;
Paul Cercueil42d12352014-05-05 16:11:58 +020033 char *name = NULL, *filename = NULL;
Paul Cercueilb34e0222014-05-05 15:32:38 +020034 struct iio_channel_attr *attrs;
Paul Cercueil5d9db302014-02-18 18:16:51 +010035
36 for (attr = n->properties; attr; attr = attr->next) {
37 if (!strcmp((char *) attr->name, "name")) {
Paul Cercueild4378e72014-05-30 13:37:34 +020038 name = _strdup((char *) attr->children->content);
Paul Cercueil42d12352014-05-05 16:11:58 +020039 } else if (!strcmp((char *) attr->name, "filename")) {
Paul Cercueild4378e72014-05-30 13:37:34 +020040 filename = _strdup((char *) attr->children->content);
Paul Cercueil5d9db302014-02-18 18:16:51 +010041 } else {
42 WARNING("Unknown field \'%s\' in channel %s\n",
43 attr->name, chn->id);
44 }
45 }
46
Paul Cercueil42d12352014-05-05 16:11:58 +020047 if (!name || !filename) {
Paul Cercueil5d9db302014-02-18 18:16:51 +010048 ERROR("Incomplete attribute in channel %s\n", chn->id);
49 goto err_free;
50 }
51
Paul Cercueilb34e0222014-05-05 15:32:38 +020052 attrs = realloc(chn->attrs, (1 + chn->nb_attrs) *
53 sizeof(struct iio_channel_attr));
Paul Cercueil5d9db302014-02-18 18:16:51 +010054 if (!attrs)
55 goto err_free;
56
Paul Cercueil42d12352014-05-05 16:11:58 +020057 attrs[chn->nb_attrs].filename = filename;
Paul Cercueilb34e0222014-05-05 15:32:38 +020058 attrs[chn->nb_attrs++].name = name;
Paul Cercueil5d9db302014-02-18 18:16:51 +010059 chn->attrs = attrs;
60 return 0;
61
Paul Cercueil5d9db302014-02-18 18:16:51 +010062err_free:
63 if (name)
64 free(name);
Paul Cercueil42d12352014-05-05 16:11:58 +020065 if (filename)
66 free(filename);
Paul Cercueil5d9db302014-02-18 18:16:51 +010067 return -1;
68}
69
Paul Cercueil501961b2014-04-15 11:23:30 +020070static int add_attr_to_device(struct iio_device *dev, xmlNode *n, bool is_debug)
Paul Cercueil5d9db302014-02-18 18:16:51 +010071{
72 xmlAttr *attr;
Paul Cercueil5cd260e2014-02-24 13:16:01 +010073 char **attrs, *name = NULL;
Paul Cercueil5d9db302014-02-18 18:16:51 +010074
75 for (attr = n->properties; attr; attr = attr->next) {
76 if (!strcmp((char *) attr->name, "name")) {
Paul Cercueild4378e72014-05-30 13:37:34 +020077 name = _strdup((char *) attr->children->content);
Paul Cercueil5d9db302014-02-18 18:16:51 +010078 } else {
79 WARNING("Unknown field \'%s\' in device %s\n",
80 attr->name, dev->id);
81 }
82 }
83
Paul Cercueil5cd260e2014-02-24 13:16:01 +010084 if (!name) {
Paul Cercueil5d9db302014-02-18 18:16:51 +010085 ERROR("Incomplete attribute in device %s\n", dev->id);
86 goto err_free;
87 }
88
Paul Cercueil501961b2014-04-15 11:23:30 +020089 if (is_debug)
90 attrs = realloc(dev->debug_attrs,
91 (1 + dev->nb_debug_attrs) * sizeof(char *));
92 else
93 attrs = realloc(dev->attrs,
94 (1 + dev->nb_attrs) * sizeof(char *));
Paul Cercueil5d9db302014-02-18 18:16:51 +010095 if (!attrs)
96 goto err_free;
97
Paul Cercueil501961b2014-04-15 11:23:30 +020098 if (is_debug) {
99 attrs[dev->nb_debug_attrs++] = name;
100 dev->debug_attrs = attrs;
101 } else {
102 attrs[dev->nb_attrs++] = name;
103 dev->attrs = attrs;
104 }
Paul Cercueil5d9db302014-02-18 18:16:51 +0100105 return 0;
106
Paul Cercueil5d9db302014-02-18 18:16:51 +0100107err_free:
108 if (name)
109 free(name);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100110 return -1;
111}
112
Paul Cercueila7e80022014-06-11 11:41:26 +0200113static void setup_scan_element(struct iio_channel *chn, xmlNode *n)
114{
115 xmlAttr *attr;
116
117 for (attr = n->properties; attr; attr = attr->next) {
118 const char *name = (const char *) attr->name,
119 *content = (const char *) attr->children->content;
120 if (!strcmp(name, "index")) {
121 chn->index = atol(content);
122 } else if (!strcmp(name, "format")) {
123 char e, s;
Paul Cercueil6e4bc7a2014-06-11 14:33:48 +0200124 sscanf(content, "%ce:%c%u/%u&gt;&gt;%u", &e, &s,
Paul Cercueila7e80022014-06-11 11:41:26 +0200125 &chn->format.bits,
126 &chn->format.length,
127 &chn->format.shift);
128 chn->format.is_be = e == 'b';
129 chn->format.is_signed = s == 's';
130 } else if (!strcmp(name, "scale")) {
131 chn->format.with_scale = true;
132 chn->format.scale = atof(content);
133 } else {
134 WARNING("Unknown attribute \'%s\' in <scan-element>\n",
135 name);
136 }
137 }
138}
139
Paul Cercueil5d9db302014-02-18 18:16:51 +0100140static struct iio_channel * create_channel(struct iio_device *dev, xmlNode *n)
141{
142 xmlAttr *attr;
143 struct iio_channel *chn = calloc(1, sizeof(*chn));
144 if (!chn)
145 return NULL;
146
147 chn->dev = dev;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100148
149 for (attr = n->properties; attr; attr = attr->next) {
Paul Cercueil35a01312014-02-20 10:56:57 +0100150 const char *name = (const char *) attr->name,
151 *content = (const char *) attr->children->content;
152 if (!strcmp(name, "name")) {
Paul Cercueild4378e72014-05-30 13:37:34 +0200153 chn->name = _strdup(content);
Paul Cercueil35a01312014-02-20 10:56:57 +0100154 } else if (!strcmp(name, "id")) {
Paul Cercueild4378e72014-05-30 13:37:34 +0200155 chn->id = _strdup(content);
Paul Cercueil35a01312014-02-20 10:56:57 +0100156 } else if (!strcmp(name, "type")) {
157 if (!strcmp(content, "output"))
158 chn->is_output = true;
159 else if (strcmp(content, "input"))
160 WARNING("Unknown channel type %s\n", content);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100161 } else {
162 WARNING("Unknown attribute \'%s\' in <channel>\n",
Paul Cercueil35a01312014-02-20 10:56:57 +0100163 name);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100164 }
165 }
166
167 if (!chn->id) {
168 ERROR("Incomplete <attribute>\n");
169 goto err_free_channel;
170 }
171
172 for (n = n->children; n; n = n->next) {
173 if (!strcmp((char *) n->name, "attribute")) {
174 if (add_attr_to_channel(chn, n) < 0)
175 goto err_free_channel;
Paul Cercueila7e80022014-06-11 11:41:26 +0200176 } else if (!strcmp((char *) n->name, "scan-element")) {
177 chn->is_scan_element = true;
178 setup_scan_element(chn, n);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100179 } else if (strcmp((char *) n->name, "text")) {
180 WARNING("Unknown children \'%s\' in <device>\n",
181 n->name);
182 continue;
183 }
184 }
185
186 return chn;
187
188err_free_channel:
189 free_channel(chn);
190 return NULL;
191}
192
193static struct iio_device * create_device(struct iio_context *ctx, xmlNode *n)
194{
195 xmlAttr *attr;
196 struct iio_device *dev = calloc(1, sizeof(*dev));
197 if (!dev)
198 return NULL;
199
200 dev->ctx = ctx;
201
202 for (attr = n->properties; attr; attr = attr->next) {
203 if (!strcmp((char *) attr->name, "name")) {
Paul Cercueild4378e72014-05-30 13:37:34 +0200204 dev->name = _strdup((char *) attr->children->content);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100205 } else if (!strcmp((char *) attr->name, "id")) {
Paul Cercueild4378e72014-05-30 13:37:34 +0200206 dev->id = _strdup((char *) attr->children->content);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100207 } else {
208 WARNING("Unknown attribute \'%s\' in <context>\n",
209 attr->name);
210 }
211 }
212
213 if (!dev->id) {
214 ERROR("Unable to read device ID\n");
215 goto err_free_device;
216 }
217
218 for (n = n->children; n; n = n->next) {
219 if (!strcmp((char *) n->name, "channel")) {
220 struct iio_channel **chns,
221 *chn = create_channel(dev, n);
222 if (!chn) {
223 ERROR("Unable to create channel\n");
224 goto err_free_device;
225 }
226
227 chns = realloc(dev->channels, (1 + dev->nb_channels) *
228 sizeof(struct iio_channel *));
229 if (!chns) {
230 ERROR("Unable to allocate memory\n");
231 free(chn);
232 goto err_free_device;
233 }
234
235 chns[dev->nb_channels++] = chn;
236 dev->channels = chns;
237 } else if (!strcmp((char *) n->name, "attribute")) {
Paul Cercueil501961b2014-04-15 11:23:30 +0200238 if (add_attr_to_device(dev, n, false) < 0)
239 goto err_free_device;
240 } else if (!strcmp((char *) n->name, "debug-attribute")) {
241 if (add_attr_to_device(dev, n, true) < 0)
Paul Cercueil5d9db302014-02-18 18:16:51 +0100242 goto err_free_device;
243 } else if (strcmp((char *) n->name, "text")) {
244 WARNING("Unknown children \'%s\' in <device>\n",
245 n->name);
246 continue;
247 }
248 }
249
250 return dev;
251
252err_free_device:
253 free_device(dev);
254 return NULL;
255}
256
Paul Cercueil5d9db302014-02-18 18:16:51 +0100257static struct iio_backend_ops xml_ops = {
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200258 .read = NULL, /* ISO C99 forbids empty initializer braces */
Paul Cercueil5d9db302014-02-18 18:16:51 +0100259};
260
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100261static struct iio_context * iio_create_xml_context_helper(xmlDoc *doc)
Paul Cercueil5d9db302014-02-18 18:16:51 +0100262{
263 unsigned int i;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100264 xmlNode *root, *n;
265 struct iio_context *ctx = calloc(1, sizeof(*ctx));
266 if (!ctx)
267 return NULL;
268
Paul Cercueil5d9db302014-02-18 18:16:51 +0100269 ctx->name = "xml";
270 ctx->ops = &xml_ops;
271
Paul Cercueil5d9db302014-02-18 18:16:51 +0100272 root = xmlDocGetRootElement(doc);
273 if (strcmp((char *) root->name, "context")) {
274 ERROR("Unrecognized XML file\n");
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100275 goto err_free_ctx;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100276 }
277
278 for (n = root->children; n; n = n->next) {
279 struct iio_device **devs, *dev;
280
281 if (strcmp((char *) n->name, "device")) {
282 if (strcmp((char *) n->name, "text"))
283 WARNING("Unknown children \'%s\' in "
284 "<context>\n", n->name);
285 continue;
286 }
287
288 dev = create_device(ctx, n);
289 if (!dev) {
290 ERROR("Unable to create device\n");
291 goto err_free_devices;
292 }
293
294 devs = realloc(ctx->devices, (1 + ctx->nb_devices) *
295 sizeof(struct iio_device *));
296 if (!devs) {
297 ERROR("Unable to allocate memory\n");
298 free(dev);
299 goto err_free_devices;
300 }
301
302 devs[ctx->nb_devices++] = dev;
303 ctx->devices = devs;
304 }
305
Paul Cercueil4c6729d2014-04-04 17:24:41 +0200306 if (!iio_context_init(ctx))
307 return ctx;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100308
309err_free_devices:
310 for (i = 0; i < ctx->nb_devices; i++)
311 free_device(ctx->devices[i]);
312 if (ctx->nb_devices)
313 free(ctx->devices);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100314err_free_ctx:
315 free(ctx);
316 return NULL;
317}
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100318
319struct iio_context * iio_create_xml_context(const char *xml_file)
320{
321 struct iio_context *ctx;
322 xmlDoc *doc;
323
324 LIBXML_TEST_VERSION;
325
326 doc = xmlReadFile(xml_file, NULL, XML_PARSE_DTDVALID);
327 if (!doc) {
328 ERROR("Unable to parse XML file\n");
329 return NULL;
330 }
331
332 ctx = iio_create_xml_context_helper(doc);
333 xmlFreeDoc(doc);
334 xmlCleanupParser();
335 return ctx;
336}
337
Paul Cercueildda33eb2014-03-03 15:00:11 +0100338struct iio_context * iio_create_xml_context_mem(const char *xml, size_t len)
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100339{
340 struct iio_context *ctx;
341 xmlDoc *doc;
342
343 LIBXML_TEST_VERSION;
344
Paul Cercueildda33eb2014-03-03 15:00:11 +0100345 doc = xmlReadMemory(xml, len, NULL, NULL, XML_PARSE_DTDVALID);
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100346 if (!doc) {
347 ERROR("Unable to parse XML file\n");
348 return NULL;
349 }
350
351 ctx = iio_create_xml_context_helper(doc);
352 xmlFreeDoc(doc);
353 xmlCleanupParser();
354 return ctx;
355}