blob: 1724fcf0a06b6b876b6f48a667284690169fe87d [file] [log] [blame]
Paul Cercueil5d9db302014-02-18 18:16:51 +01001#include "debug.h"
2#include "iio-private.h"
3
4#include <errno.h>
5#include <libxml/tree.h>
6#include <string.h>
7
8struct value_map {
9 const struct iio_device *dev;
10 const struct iio_channel *chn;
11 const char *attr;
12 char *value;
13};
14
15struct xml_pdata {
16 struct value_map *values;
17 unsigned int nb_values;
18};
19
20static int add_attr_to_channel(struct iio_channel *chn, xmlNode *n)
21{
22 xmlAttr *attr;
23 char *name = NULL, *value = NULL;
24 const char **attrs;
25 struct xml_pdata *pdata = chn->dev->ctx->backend_data;
26 struct value_map *values;
27
28 for (attr = n->properties; attr; attr = attr->next) {
29 if (!strcmp((char *) attr->name, "name")) {
30 name = strdup((char *) attr->children->content);
31 } else if (!strcmp((char *) attr->name, "value")) {
32 value = strdup((char *) attr->children->content);
33 } else {
34 WARNING("Unknown field \'%s\' in channel %s\n",
35 attr->name, chn->id);
36 }
37 }
38
39 if (!name || !value) {
40 ERROR("Incomplete attribute in channel %s\n", chn->id);
41 goto err_free;
42 }
43
44 attrs = realloc(chn->attrs, (1 + chn->nb_attrs) * sizeof(char *));
45 if (!attrs)
46 goto err_free;
47
48 values = realloc(pdata->values,
49 (1 + pdata->nb_values) * sizeof(struct value_map));
50 if (!values)
51 goto err_update_attrs;
52
53 values[pdata->nb_values].dev = NULL;
54 values[pdata->nb_values].chn = chn;
55 values[pdata->nb_values].attr = name;
56 values[pdata->nb_values++].value = value;
57 pdata->values = values;
58
59 attrs[chn->nb_attrs++] = name;
60 chn->attrs = attrs;
61 return 0;
62
63err_update_attrs:
64 /* the first realloc succeeded so we must update chn->attrs
65 * even if an error occured later */
66 chn->attrs = attrs;
67err_free:
68 if (name)
69 free(name);
70 if (value)
71 free(value);
72 return -1;
73}
74
75static int add_attr_to_device(struct iio_device *dev, xmlNode *n)
76{
77 xmlAttr *attr;
78 char *name = NULL, *value = NULL;
79 const char **attrs;
80 struct xml_pdata *pdata = dev->ctx->backend_data;
81 struct value_map *values;
82
83 for (attr = n->properties; attr; attr = attr->next) {
84 if (!strcmp((char *) attr->name, "name")) {
85 name = strdup((char *) attr->children->content);
86 } else if (!strcmp((char *) attr->name, "value")) {
87 value = strdup((char *) attr->children->content);
88 } else {
89 WARNING("Unknown field \'%s\' in device %s\n",
90 attr->name, dev->id);
91 }
92 }
93
94 if (!name || !value) {
95 ERROR("Incomplete attribute in device %s\n", dev->id);
96 goto err_free;
97 }
98
99 attrs = realloc(dev->attrs, (1 + dev->nb_attrs) * sizeof(char *));
100 if (!attrs)
101 goto err_free;
102
103 values = realloc(pdata->values,
104 (1 + pdata->nb_values) * sizeof(struct value_map));
105 if (!values)
106 goto err_update_attrs;
107
108 values[pdata->nb_values].dev = dev;
109 values[pdata->nb_values].chn = NULL;
110 values[pdata->nb_values].attr = name;
111 values[pdata->nb_values++].value = value;
112 pdata->values = values;
113
114 attrs[dev->nb_attrs++] = name;
115 dev->attrs = attrs;
116 return 0;
117
118err_update_attrs:
119 /* the first realloc succeeded so we must update dev->attrs
120 * even if an error occured later */
121 dev->attrs = attrs;
122err_free:
123 if (name)
124 free(name);
125 if (value)
126 free(value);
127 return -1;
128}
129
130static struct iio_channel * create_channel(struct iio_device *dev, xmlNode *n)
131{
132 xmlAttr *attr;
133 struct iio_channel *chn = calloc(1, sizeof(*chn));
134 if (!chn)
135 return NULL;
136
137 chn->dev = dev;
138 chn->type = IIO_CHANNEL_UNKNOWN;
139
140 for (attr = n->properties; attr; attr = attr->next) {
141 if (!strcmp((char *) attr->name, "name")) {
142 chn->name = strdup((char *) attr->children->content);
143 } else if (!strcmp((char *) attr->name, "id")) {
144 chn->id = strdup((char *) attr->children->content);
145 } else if (!strcmp((char *) attr->name, "type")) {
146 if (!strcmp((char *) attr->children->content, "input"))
147 chn->type = IIO_CHANNEL_INPUT;
148 else if (!strcmp((char *) attr->children->content,
149 "output")) {
150 chn->type = IIO_CHANNEL_OUTPUT;
151 } else {
152 WARNING("Unknown channel type %s\n", (char *)
153 attr->children->content);
154 }
155 } else {
156 WARNING("Unknown attribute \'%s\' in <channel>\n",
157 attr->name);
158 }
159 }
160
161 if (!chn->id) {
162 ERROR("Incomplete <attribute>\n");
163 goto err_free_channel;
164 }
165
166 for (n = n->children; n; n = n->next) {
167 if (!strcmp((char *) n->name, "attribute")) {
168 if (add_attr_to_channel(chn, n) < 0)
169 goto err_free_channel;
170 } else if (strcmp((char *) n->name, "text")) {
171 WARNING("Unknown children \'%s\' in <device>\n",
172 n->name);
173 continue;
174 }
175 }
176
177 return chn;
178
179err_free_channel:
180 free_channel(chn);
181 return NULL;
182}
183
184static struct iio_device * create_device(struct iio_context *ctx, xmlNode *n)
185{
186 xmlAttr *attr;
187 struct iio_device *dev = calloc(1, sizeof(*dev));
188 if (!dev)
189 return NULL;
190
191 dev->ctx = ctx;
192
193 for (attr = n->properties; attr; attr = attr->next) {
194 if (!strcmp((char *) attr->name, "name")) {
195 dev->name = strdup((char *) attr->children->content);
196 } else if (!strcmp((char *) attr->name, "id")) {
197 dev->id = strdup((char *) attr->children->content);
198 } else {
199 WARNING("Unknown attribute \'%s\' in <context>\n",
200 attr->name);
201 }
202 }
203
204 if (!dev->id) {
205 ERROR("Unable to read device ID\n");
206 goto err_free_device;
207 }
208
209 for (n = n->children; n; n = n->next) {
210 if (!strcmp((char *) n->name, "channel")) {
211 struct iio_channel **chns,
212 *chn = create_channel(dev, n);
213 if (!chn) {
214 ERROR("Unable to create channel\n");
215 goto err_free_device;
216 }
217
218 chns = realloc(dev->channels, (1 + dev->nb_channels) *
219 sizeof(struct iio_channel *));
220 if (!chns) {
221 ERROR("Unable to allocate memory\n");
222 free(chn);
223 goto err_free_device;
224 }
225
226 chns[dev->nb_channels++] = chn;
227 dev->channels = chns;
228 } else if (!strcmp((char *) n->name, "attribute")) {
229 if (add_attr_to_device(dev, n) < 0)
230 goto err_free_device;
231 } else if (strcmp((char *) n->name, "text")) {
232 WARNING("Unknown children \'%s\' in <device>\n",
233 n->name);
234 continue;
235 }
236 }
237
238 return dev;
239
240err_free_device:
241 free_device(dev);
242 return NULL;
243}
244
245static ssize_t xml_read_attr_helper(struct xml_pdata *pdata,
246 const struct iio_device *dev,
247 const struct iio_channel *chn,
248 const char *path, char *dst, size_t len)
249{
250 unsigned int i;
251 for (i = 0; i < pdata->nb_values; i++) {
252 struct value_map *map = &pdata->values[i];
253
254 if (dev == map->dev && chn == map->chn
255 && !strcmp(path, map->attr)) {
256 size_t value_len = strlen(map->value);
257 strncpy(dst, map->value, len);
258 return value_len + 1;
259 }
260 }
261
262 return -ENOENT;
263}
264
265static ssize_t xml_read_dev_attr(const struct iio_device *dev,
266 const char *path, char *dst, size_t len)
267{
268 struct xml_pdata *pdata = dev->ctx->backend_data;
269 return xml_read_attr_helper(pdata, dev, NULL, path, dst, len);
270}
271
272static ssize_t xml_read_chn_attr(const struct iio_channel *chn,
273 const char *path, char *dst, size_t len)
274{
275 struct xml_pdata *pdata = chn->dev->ctx->backend_data;
276 return xml_read_attr_helper(pdata, NULL, chn, path, dst, len);
277}
278
279static void xml_shutdown(struct iio_context *ctx)
280{
281 struct xml_pdata *pdata = ctx->backend_data;
282 unsigned int i;
283 for (i = 0; i < pdata->nb_values; i++) {
284 struct value_map *map = &pdata->values[i];
285
286 /* note: map->attr and map->dev are freed elsewhere */
287 free(map->value);
288 }
289
290 if (pdata->nb_values)
291 free(pdata->values);
292 free(pdata);
293}
294
295static struct iio_backend_ops xml_ops = {
296 .read_device_attr = xml_read_dev_attr,
297 .read_channel_attr = xml_read_chn_attr,
298 .shutdown = xml_shutdown,
299};
300
301struct iio_context * iio_create_xml_context(const char *xml_file)
302{
303 unsigned int i;
304 xmlDoc *doc;
305 xmlNode *root, *n;
306 struct iio_context *ctx = calloc(1, sizeof(*ctx));
307 if (!ctx)
308 return NULL;
309
310 ctx->backend_data = calloc(1, sizeof(struct xml_pdata));
311 if (!ctx->backend_data) {
312 ERROR("Unable to allocate memory\n");
Paul Cercueil92de9f22014-02-19 14:07:24 +0100313 goto err_free_ctx;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100314 }
315
316 ctx->name = "xml";
317 ctx->ops = &xml_ops;
318
319 LIBXML_TEST_VERSION;
320
Paul Cercueil92de9f22014-02-19 14:07:24 +0100321 doc = xmlReadFile(xml_file, NULL, XML_PARSE_DTDVALID);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100322 if (!doc) {
323 ERROR("Unable to parse XML file\n");
Paul Cercueil92de9f22014-02-19 14:07:24 +0100324 goto err_free_bdata;
Paul Cercueil5d9db302014-02-18 18:16:51 +0100325 }
326
327 root = xmlDocGetRootElement(doc);
328 if (strcmp((char *) root->name, "context")) {
329 ERROR("Unrecognized XML file\n");
330 goto err_free_doc;
331 }
332
333 for (n = root->children; n; n = n->next) {
334 struct iio_device **devs, *dev;
335
336 if (strcmp((char *) n->name, "device")) {
337 if (strcmp((char *) n->name, "text"))
338 WARNING("Unknown children \'%s\' in "
339 "<context>\n", n->name);
340 continue;
341 }
342
343 dev = create_device(ctx, n);
344 if (!dev) {
345 ERROR("Unable to create device\n");
346 goto err_free_devices;
347 }
348
349 devs = realloc(ctx->devices, (1 + ctx->nb_devices) *
350 sizeof(struct iio_device *));
351 if (!devs) {
352 ERROR("Unable to allocate memory\n");
353 free(dev);
354 goto err_free_devices;
355 }
356
357 devs[ctx->nb_devices++] = dev;
358 ctx->devices = devs;
359 }
360
Paul Cercueil92de9f22014-02-19 14:07:24 +0100361 xmlFreeDoc(doc);
362 xmlCleanupParser();
Paul Cercueil5d9db302014-02-18 18:16:51 +0100363 return ctx;
364
365err_free_devices:
366 for (i = 0; i < ctx->nb_devices; i++)
367 free_device(ctx->devices[i]);
368 if (ctx->nb_devices)
369 free(ctx->devices);
370err_free_doc:
371 xmlFreeDoc(doc);
372 xmlCleanupParser();
Paul Cercueil92de9f22014-02-19 14:07:24 +0100373err_free_bdata:
374 free(ctx->backend_data);
Paul Cercueil5d9db302014-02-18 18:16:51 +0100375err_free_ctx:
376 free(ctx);
377 return NULL;
378}