blob: fbb9adf56dd09c067747ce6d2e1b900b8d4cb4bd [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 Cercueil5cd260e2014-02-24 13:16:01 +010029 char **attrs, *name = NULL;
Paul Cercueil5d9db302014-02-18 18:16:51 +010030
31 for (attr = n->properties; attr; attr = attr->next) {
32 if (!strcmp((char *) attr->name, "name")) {
33 name = strdup((char *) attr->children->content);
Paul Cercueil5d9db302014-02-18 18:16:51 +010034 } else {
35 WARNING("Unknown field \'%s\' in channel %s\n",
36 attr->name, chn->id);
37 }
38 }
39
Paul Cercueil5cd260e2014-02-24 13:16:01 +010040 if (!name) {
Paul Cercueil5d9db302014-02-18 18:16:51 +010041 ERROR("Incomplete attribute in channel %s\n", chn->id);
42 goto err_free;
43 }
44
45 attrs = realloc(chn->attrs, (1 + chn->nb_attrs) * sizeof(char *));
46 if (!attrs)
47 goto err_free;
48
Paul Cercueil5d9db302014-02-18 18:16:51 +010049 attrs[chn->nb_attrs++] = name;
50 chn->attrs = attrs;
51 return 0;
52
Paul Cercueil5d9db302014-02-18 18:16:51 +010053err_free:
54 if (name)
55 free(name);
Paul Cercueil5d9db302014-02-18 18:16:51 +010056 return -1;
57}
58
Paul Cercueil501961b2014-04-15 11:23:30 +020059static int add_attr_to_device(struct iio_device *dev, xmlNode *n, bool is_debug)
Paul Cercueil5d9db302014-02-18 18:16:51 +010060{
61 xmlAttr *attr;
Paul Cercueil5cd260e2014-02-24 13:16:01 +010062 char **attrs, *name = NULL;
Paul Cercueil5d9db302014-02-18 18:16:51 +010063
64 for (attr = n->properties; attr; attr = attr->next) {
65 if (!strcmp((char *) attr->name, "name")) {
66 name = strdup((char *) attr->children->content);
Paul Cercueil5d9db302014-02-18 18:16:51 +010067 } else {
68 WARNING("Unknown field \'%s\' in device %s\n",
69 attr->name, dev->id);
70 }
71 }
72
Paul Cercueil5cd260e2014-02-24 13:16:01 +010073 if (!name) {
Paul Cercueil5d9db302014-02-18 18:16:51 +010074 ERROR("Incomplete attribute in device %s\n", dev->id);
75 goto err_free;
76 }
77
Paul Cercueil501961b2014-04-15 11:23:30 +020078 if (is_debug)
79 attrs = realloc(dev->debug_attrs,
80 (1 + dev->nb_debug_attrs) * sizeof(char *));
81 else
82 attrs = realloc(dev->attrs,
83 (1 + dev->nb_attrs) * sizeof(char *));
Paul Cercueil5d9db302014-02-18 18:16:51 +010084 if (!attrs)
85 goto err_free;
86
Paul Cercueil501961b2014-04-15 11:23:30 +020087 if (is_debug) {
88 attrs[dev->nb_debug_attrs++] = name;
89 dev->debug_attrs = attrs;
90 } else {
91 attrs[dev->nb_attrs++] = name;
92 dev->attrs = attrs;
93 }
Paul Cercueil5d9db302014-02-18 18:16:51 +010094 return 0;
95
Paul Cercueil5d9db302014-02-18 18:16:51 +010096err_free:
97 if (name)
98 free(name);
Paul Cercueil5d9db302014-02-18 18:16:51 +010099 return -1;
100}
101
102static struct iio_channel * create_channel(struct iio_device *dev, xmlNode *n)
103{
104 xmlAttr *attr;
105 struct iio_channel *chn = calloc(1, sizeof(*chn));
106 if (!chn)
107 return NULL;
108
109 chn->dev = dev;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100110
111 for (attr = n->properties; attr; attr = attr->next) {
Paul Cercueil35a01312014-02-20 10:56:57 +0100112 const char *name = (const char *) attr->name,
113 *content = (const char *) attr->children->content;
114 if (!strcmp(name, "name")) {
115 chn->name = strdup(content);
116 } else if (!strcmp(name, "id")) {
117 chn->id = strdup(content);
118 } else if (!strcmp(name, "type")) {
119 if (!strcmp(content, "output"))
120 chn->is_output = true;
121 else if (strcmp(content, "input"))
122 WARNING("Unknown channel type %s\n", content);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100123 } else {
124 WARNING("Unknown attribute \'%s\' in <channel>\n",
Paul Cercueil35a01312014-02-20 10:56:57 +0100125 name);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100126 }
127 }
128
129 if (!chn->id) {
130 ERROR("Incomplete <attribute>\n");
131 goto err_free_channel;
132 }
133
134 for (n = n->children; n; n = n->next) {
135 if (!strcmp((char *) n->name, "attribute")) {
136 if (add_attr_to_channel(chn, n) < 0)
137 goto err_free_channel;
138 } else if (strcmp((char *) n->name, "text")) {
139 WARNING("Unknown children \'%s\' in <device>\n",
140 n->name);
141 continue;
142 }
143 }
144
145 return chn;
146
147err_free_channel:
148 free_channel(chn);
149 return NULL;
150}
151
152static struct iio_device * create_device(struct iio_context *ctx, xmlNode *n)
153{
154 xmlAttr *attr;
155 struct iio_device *dev = calloc(1, sizeof(*dev));
156 if (!dev)
157 return NULL;
158
159 dev->ctx = ctx;
160
161 for (attr = n->properties; attr; attr = attr->next) {
162 if (!strcmp((char *) attr->name, "name")) {
163 dev->name = strdup((char *) attr->children->content);
164 } else if (!strcmp((char *) attr->name, "id")) {
165 dev->id = strdup((char *) attr->children->content);
166 } else {
167 WARNING("Unknown attribute \'%s\' in <context>\n",
168 attr->name);
169 }
170 }
171
172 if (!dev->id) {
173 ERROR("Unable to read device ID\n");
174 goto err_free_device;
175 }
176
177 for (n = n->children; n; n = n->next) {
178 if (!strcmp((char *) n->name, "channel")) {
179 struct iio_channel **chns,
180 *chn = create_channel(dev, n);
181 if (!chn) {
182 ERROR("Unable to create channel\n");
183 goto err_free_device;
184 }
185
186 chns = realloc(dev->channels, (1 + dev->nb_channels) *
187 sizeof(struct iio_channel *));
188 if (!chns) {
189 ERROR("Unable to allocate memory\n");
190 free(chn);
191 goto err_free_device;
192 }
193
194 chns[dev->nb_channels++] = chn;
195 dev->channels = chns;
196 } else if (!strcmp((char *) n->name, "attribute")) {
Paul Cercueil501961b2014-04-15 11:23:30 +0200197 if (add_attr_to_device(dev, n, false) < 0)
198 goto err_free_device;
199 } else if (!strcmp((char *) n->name, "debug-attribute")) {
200 if (add_attr_to_device(dev, n, true) < 0)
Paul Cercueil5d9db302014-02-18 18:16:51 +0100201 goto err_free_device;
202 } else if (strcmp((char *) n->name, "text")) {
203 WARNING("Unknown children \'%s\' in <device>\n",
204 n->name);
205 continue;
206 }
207 }
208
209 return dev;
210
211err_free_device:
212 free_device(dev);
213 return NULL;
214}
215
Paul Cercueil5d9db302014-02-18 18:16:51 +0100216static struct iio_backend_ops xml_ops = {
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200217 .read = NULL, /* ISO C99 forbids empty initializer braces */
Paul Cercueil5d9db302014-02-18 18:16:51 +0100218};
219
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100220static struct iio_context * iio_create_xml_context_helper(xmlDoc *doc)
Paul Cercueil5d9db302014-02-18 18:16:51 +0100221{
222 unsigned int i;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100223 xmlNode *root, *n;
224 struct iio_context *ctx = calloc(1, sizeof(*ctx));
225 if (!ctx)
226 return NULL;
227
Paul Cercueil5d9db302014-02-18 18:16:51 +0100228 ctx->name = "xml";
229 ctx->ops = &xml_ops;
230
Paul Cercueil5d9db302014-02-18 18:16:51 +0100231 root = xmlDocGetRootElement(doc);
232 if (strcmp((char *) root->name, "context")) {
233 ERROR("Unrecognized XML file\n");
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100234 goto err_free_ctx;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100235 }
236
237 for (n = root->children; n; n = n->next) {
238 struct iio_device **devs, *dev;
239
240 if (strcmp((char *) n->name, "device")) {
241 if (strcmp((char *) n->name, "text"))
242 WARNING("Unknown children \'%s\' in "
243 "<context>\n", n->name);
244 continue;
245 }
246
247 dev = create_device(ctx, n);
248 if (!dev) {
249 ERROR("Unable to create device\n");
250 goto err_free_devices;
251 }
252
253 devs = realloc(ctx->devices, (1 + ctx->nb_devices) *
254 sizeof(struct iio_device *));
255 if (!devs) {
256 ERROR("Unable to allocate memory\n");
257 free(dev);
258 goto err_free_devices;
259 }
260
261 devs[ctx->nb_devices++] = dev;
262 ctx->devices = devs;
263 }
264
Paul Cercueil4c6729d2014-04-04 17:24:41 +0200265 if (!iio_context_init(ctx))
266 return ctx;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100267
268err_free_devices:
269 for (i = 0; i < ctx->nb_devices; i++)
270 free_device(ctx->devices[i]);
271 if (ctx->nb_devices)
272 free(ctx->devices);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100273err_free_ctx:
274 free(ctx);
275 return NULL;
276}
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100277
278struct iio_context * iio_create_xml_context(const char *xml_file)
279{
280 struct iio_context *ctx;
281 xmlDoc *doc;
282
283 LIBXML_TEST_VERSION;
284
285 doc = xmlReadFile(xml_file, NULL, XML_PARSE_DTDVALID);
286 if (!doc) {
287 ERROR("Unable to parse XML file\n");
288 return NULL;
289 }
290
291 ctx = iio_create_xml_context_helper(doc);
292 xmlFreeDoc(doc);
293 xmlCleanupParser();
294 return ctx;
295}
296
Paul Cercueildda33eb2014-03-03 15:00:11 +0100297struct iio_context * iio_create_xml_context_mem(const char *xml, size_t len)
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100298{
299 struct iio_context *ctx;
300 xmlDoc *doc;
301
302 LIBXML_TEST_VERSION;
303
Paul Cercueildda33eb2014-03-03 15:00:11 +0100304 doc = xmlReadMemory(xml, len, NULL, NULL, XML_PARSE_DTDVALID);
Paul Cercueil8b1f9d02014-02-21 16:24:51 +0100305 if (!doc) {
306 ERROR("Unable to parse XML file\n");
307 return NULL;
308 }
309
310 ctx = iio_create_xml_context_helper(doc);
311 xmlFreeDoc(doc);
312 xmlCleanupParser();
313 return ctx;
314}