Paul Cercueil | bb4401d | 2014-02-28 16:10:49 +0100 | [diff] [blame] | 1 | /* |
| 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 Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 19 | #include "debug.h" |
| 20 | #include "iio-private.h" |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <libxml/tree.h> |
| 24 | #include <string.h> |
| 25 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 26 | static int add_attr_to_channel(struct iio_channel *chn, xmlNode *n) |
| 27 | { |
| 28 | xmlAttr *attr; |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 29 | char *name = NULL, *filename = NULL; |
Paul Cercueil | b34e022 | 2014-05-05 15:32:38 +0200 | [diff] [blame] | 30 | struct iio_channel_attr *attrs; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 31 | |
| 32 | for (attr = n->properties; attr; attr = attr->next) { |
| 33 | if (!strcmp((char *) attr->name, "name")) { |
Paul Cercueil | caf0e71 | 2016-08-25 17:27:02 +0200 | [diff] [blame] | 34 | name = iio_strdup((char *) attr->children->content); |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 35 | } else if (!strcmp((char *) attr->name, "filename")) { |
Paul Cercueil | caf0e71 | 2016-08-25 17:27:02 +0200 | [diff] [blame] | 36 | filename = iio_strdup((char *) attr->children->content); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 37 | } else { |
| 38 | WARNING("Unknown field \'%s\' in channel %s\n", |
| 39 | attr->name, chn->id); |
| 40 | } |
| 41 | } |
| 42 | |
Paul Cercueil | 7150c6e | 2015-04-14 16:26:21 +0200 | [diff] [blame] | 43 | if (!name) { |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 44 | ERROR("Incomplete attribute in channel %s\n", chn->id); |
| 45 | goto err_free; |
| 46 | } |
| 47 | |
Paul Cercueil | 7150c6e | 2015-04-14 16:26:21 +0200 | [diff] [blame] | 48 | if (!filename) { |
Paul Cercueil | caf0e71 | 2016-08-25 17:27:02 +0200 | [diff] [blame] | 49 | filename = iio_strdup(name); |
Paul Cercueil | 7150c6e | 2015-04-14 16:26:21 +0200 | [diff] [blame] | 50 | if (!filename) |
| 51 | goto err_free; |
| 52 | } |
| 53 | |
Paul Cercueil | b34e022 | 2014-05-05 15:32:38 +0200 | [diff] [blame] | 54 | attrs = realloc(chn->attrs, (1 + chn->nb_attrs) * |
| 55 | sizeof(struct iio_channel_attr)); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 56 | if (!attrs) |
| 57 | goto err_free; |
| 58 | |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 59 | attrs[chn->nb_attrs].filename = filename; |
Paul Cercueil | b34e022 | 2014-05-05 15:32:38 +0200 | [diff] [blame] | 60 | attrs[chn->nb_attrs++].name = name; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 61 | chn->attrs = attrs; |
| 62 | return 0; |
| 63 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 64 | err_free: |
| 65 | if (name) |
| 66 | free(name); |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 67 | if (filename) |
| 68 | free(filename); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | |
Matt Fornero | 81f04a5 | 2017-11-30 14:36:37 -0500 | [diff] [blame] | 72 | static int add_attr_to_device(struct iio_device *dev, xmlNode *n, enum iio_attr_type type) |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 73 | { |
| 74 | xmlAttr *attr; |
Paul Cercueil | 5cd260e | 2014-02-24 13:16:01 +0100 | [diff] [blame] | 75 | char **attrs, *name = NULL; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 76 | |
| 77 | for (attr = n->properties; attr; attr = attr->next) { |
| 78 | if (!strcmp((char *) attr->name, "name")) { |
Paul Cercueil | caf0e71 | 2016-08-25 17:27:02 +0200 | [diff] [blame] | 79 | name = iio_strdup((char *) attr->children->content); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 80 | } else { |
| 81 | WARNING("Unknown field \'%s\' in device %s\n", |
| 82 | attr->name, dev->id); |
| 83 | } |
| 84 | } |
| 85 | |
Paul Cercueil | 5cd260e | 2014-02-24 13:16:01 +0100 | [diff] [blame] | 86 | if (!name) { |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 87 | ERROR("Incomplete attribute in device %s\n", dev->id); |
| 88 | goto err_free; |
| 89 | } |
| 90 | |
Matt Fornero | 81f04a5 | 2017-11-30 14:36:37 -0500 | [diff] [blame] | 91 | switch(type) { |
| 92 | case IIO_ATTR_TYPE_DEBUG: |
| 93 | attrs = realloc(dev->debug_attrs, |
| 94 | (1 + dev->nb_debug_attrs) * sizeof(char *)); |
| 95 | break; |
| 96 | case IIO_ATTR_TYPE_DEVICE: |
| 97 | attrs = realloc(dev->attrs, |
| 98 | (1 + dev->nb_attrs) * sizeof(char *)); |
| 99 | break; |
| 100 | case IIO_ATTR_TYPE_BUFFER: |
| 101 | attrs = realloc(dev->buffer_attrs, |
| 102 | (1 + dev->nb_buffer_attrs) * sizeof(char *)); |
| 103 | break; |
| 104 | default: |
| 105 | attrs = NULL; |
| 106 | break; |
| 107 | } |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 108 | if (!attrs) |
| 109 | goto err_free; |
| 110 | |
Matt Fornero | 81f04a5 | 2017-11-30 14:36:37 -0500 | [diff] [blame] | 111 | switch(type) { |
| 112 | case IIO_ATTR_TYPE_DEBUG: |
| 113 | attrs[dev->nb_debug_attrs++] = name; |
| 114 | dev->debug_attrs = attrs; |
| 115 | break; |
| 116 | case IIO_ATTR_TYPE_DEVICE: |
| 117 | attrs[dev->nb_attrs++] = name; |
| 118 | dev->attrs = attrs; |
| 119 | break; |
| 120 | case IIO_ATTR_TYPE_BUFFER: |
| 121 | attrs[dev->nb_buffer_attrs++] = name; |
| 122 | dev->buffer_attrs = attrs; |
| 123 | break; |
Paul Cercueil | 501961b | 2014-04-15 11:23:30 +0200 | [diff] [blame] | 124 | } |
Matt Fornero | 81f04a5 | 2017-11-30 14:36:37 -0500 | [diff] [blame] | 125 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 126 | return 0; |
| 127 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 128 | err_free: |
| 129 | if (name) |
| 130 | free(name); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 131 | return -1; |
| 132 | } |
| 133 | |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 134 | static void setup_scan_element(struct iio_channel *chn, xmlNode *n) |
| 135 | { |
| 136 | xmlAttr *attr; |
| 137 | |
| 138 | for (attr = n->properties; attr; attr = attr->next) { |
| 139 | const char *name = (const char *) attr->name, |
| 140 | *content = (const char *) attr->children->content; |
| 141 | if (!strcmp(name, "index")) { |
| 142 | chn->index = atol(content); |
| 143 | } else if (!strcmp(name, "format")) { |
| 144 | char e, s; |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 145 | if (strchr(content, 'X')) { |
| 146 | sscanf(content, "%ce:%c%u/%uX%u>>%u", &e, &s, |
| 147 | &chn->format.bits, |
| 148 | &chn->format.length, |
| 149 | &chn->format.repeat, |
| 150 | &chn->format.shift); |
| 151 | } else { |
| 152 | chn->format.repeat = 1; |
| 153 | sscanf(content, "%ce:%c%u/%u>>%u", &e, &s, |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 154 | &chn->format.bits, |
| 155 | &chn->format.length, |
| 156 | &chn->format.shift); |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 157 | } |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 158 | chn->format.is_be = e == 'b'; |
Michael Hennerich | fa3c6f6 | 2014-08-13 11:21:23 +0200 | [diff] [blame] | 159 | chn->format.is_signed = (s == 's' || s == 'S'); |
| 160 | chn->format.is_fully_defined = (s == 'S' || s == 'U' || |
| 161 | chn->format.bits == chn->format.length); |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 162 | } else if (!strcmp(name, "scale")) { |
| 163 | chn->format.with_scale = true; |
| 164 | chn->format.scale = atof(content); |
| 165 | } else { |
| 166 | WARNING("Unknown attribute \'%s\' in <scan-element>\n", |
| 167 | name); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 172 | static struct iio_channel * create_channel(struct iio_device *dev, xmlNode *n) |
| 173 | { |
| 174 | xmlAttr *attr; |
Lars-Peter Clausen | d1be838 | 2016-02-24 11:13:45 +0100 | [diff] [blame] | 175 | struct iio_channel *chn = zalloc(sizeof(*chn)); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 176 | if (!chn) |
| 177 | return NULL; |
| 178 | |
| 179 | chn->dev = dev; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 180 | |
Paul Cercueil | d8d5893 | 2014-06-12 10:08:36 +0200 | [diff] [blame] | 181 | /* Set the default index value < 0 (== no index) */ |
| 182 | chn->index = -ENOENT; |
| 183 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 184 | for (attr = n->properties; attr; attr = attr->next) { |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 185 | const char *name = (const char *) attr->name, |
| 186 | *content = (const char *) attr->children->content; |
| 187 | if (!strcmp(name, "name")) { |
Paul Cercueil | caf0e71 | 2016-08-25 17:27:02 +0200 | [diff] [blame] | 188 | chn->name = iio_strdup(content); |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 189 | } else if (!strcmp(name, "id")) { |
Paul Cercueil | caf0e71 | 2016-08-25 17:27:02 +0200 | [diff] [blame] | 190 | chn->id = iio_strdup(content); |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 191 | } else if (!strcmp(name, "type")) { |
| 192 | if (!strcmp(content, "output")) |
| 193 | chn->is_output = true; |
| 194 | else if (strcmp(content, "input")) |
| 195 | WARNING("Unknown channel type %s\n", content); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 196 | } else { |
| 197 | WARNING("Unknown attribute \'%s\' in <channel>\n", |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 198 | name); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | if (!chn->id) { |
| 203 | ERROR("Incomplete <attribute>\n"); |
| 204 | goto err_free_channel; |
| 205 | } |
| 206 | |
| 207 | for (n = n->children; n; n = n->next) { |
| 208 | if (!strcmp((char *) n->name, "attribute")) { |
| 209 | if (add_attr_to_channel(chn, n) < 0) |
| 210 | goto err_free_channel; |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 211 | } else if (!strcmp((char *) n->name, "scan-element")) { |
| 212 | chn->is_scan_element = true; |
| 213 | setup_scan_element(chn, n); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 214 | } else if (strcmp((char *) n->name, "text")) { |
Paul Cercueil | f736190 | 2015-04-16 16:31:49 +0200 | [diff] [blame] | 215 | WARNING("Unknown children \'%s\' in <channel>\n", |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 216 | n->name); |
| 217 | continue; |
| 218 | } |
| 219 | } |
| 220 | |
Lars-Peter Clausen | c6f8592 | 2016-04-20 15:03:49 +0200 | [diff] [blame] | 221 | iio_channel_init_finalize(chn); |
| 222 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 223 | return chn; |
| 224 | |
| 225 | err_free_channel: |
| 226 | free_channel(chn); |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | static struct iio_device * create_device(struct iio_context *ctx, xmlNode *n) |
| 231 | { |
| 232 | xmlAttr *attr; |
Lars-Peter Clausen | d1be838 | 2016-02-24 11:13:45 +0100 | [diff] [blame] | 233 | struct iio_device *dev = zalloc(sizeof(*dev)); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 234 | if (!dev) |
| 235 | return NULL; |
| 236 | |
| 237 | dev->ctx = ctx; |
| 238 | |
| 239 | for (attr = n->properties; attr; attr = attr->next) { |
| 240 | if (!strcmp((char *) attr->name, "name")) { |
Paul Cercueil | caf0e71 | 2016-08-25 17:27:02 +0200 | [diff] [blame] | 241 | dev->name = iio_strdup( |
| 242 | (char *) attr->children->content); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 243 | } else if (!strcmp((char *) attr->name, "id")) { |
Paul Cercueil | caf0e71 | 2016-08-25 17:27:02 +0200 | [diff] [blame] | 244 | dev->id = iio_strdup((char *) attr->children->content); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 245 | } else { |
Paul Cercueil | f736190 | 2015-04-16 16:31:49 +0200 | [diff] [blame] | 246 | WARNING("Unknown attribute \'%s\' in <device>\n", |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 247 | attr->name); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | if (!dev->id) { |
| 252 | ERROR("Unable to read device ID\n"); |
| 253 | goto err_free_device; |
| 254 | } |
| 255 | |
| 256 | for (n = n->children; n; n = n->next) { |
| 257 | if (!strcmp((char *) n->name, "channel")) { |
| 258 | struct iio_channel **chns, |
| 259 | *chn = create_channel(dev, n); |
| 260 | if (!chn) { |
| 261 | ERROR("Unable to create channel\n"); |
| 262 | goto err_free_device; |
| 263 | } |
| 264 | |
| 265 | chns = realloc(dev->channels, (1 + dev->nb_channels) * |
| 266 | sizeof(struct iio_channel *)); |
| 267 | if (!chns) { |
| 268 | ERROR("Unable to allocate memory\n"); |
| 269 | free(chn); |
| 270 | goto err_free_device; |
| 271 | } |
| 272 | |
| 273 | chns[dev->nb_channels++] = chn; |
| 274 | dev->channels = chns; |
| 275 | } else if (!strcmp((char *) n->name, "attribute")) { |
Matt Fornero | 81f04a5 | 2017-11-30 14:36:37 -0500 | [diff] [blame] | 276 | if (add_attr_to_device(dev, n, IIO_ATTR_TYPE_DEVICE) < 0) |
Paul Cercueil | 501961b | 2014-04-15 11:23:30 +0200 | [diff] [blame] | 277 | goto err_free_device; |
| 278 | } else if (!strcmp((char *) n->name, "debug-attribute")) { |
Matt Fornero | 81f04a5 | 2017-11-30 14:36:37 -0500 | [diff] [blame] | 279 | if (add_attr_to_device(dev, n, IIO_ATTR_TYPE_DEBUG) < 0) |
| 280 | goto err_free_device; |
| 281 | } else if (!strcmp((char *) n->name, "buffer-attribute")) { |
| 282 | if (add_attr_to_device(dev, n, IIO_ATTR_TYPE_BUFFER) < 0) |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 283 | goto err_free_device; |
| 284 | } else if (strcmp((char *) n->name, "text")) { |
| 285 | WARNING("Unknown children \'%s\' in <device>\n", |
| 286 | n->name); |
| 287 | continue; |
| 288 | } |
| 289 | } |
| 290 | |
Paul Cercueil | edec1cb | 2015-04-20 10:40:28 +0200 | [diff] [blame] | 291 | dev->words = (dev->nb_channels + 31) / 32; |
| 292 | if (dev->words) { |
| 293 | dev->mask = calloc(dev->words, sizeof(*dev->mask)); |
| 294 | if (!dev->mask) { |
| 295 | errno = ENOMEM; |
| 296 | goto err_free_device; |
| 297 | } |
| 298 | } |
| 299 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 300 | return dev; |
| 301 | |
| 302 | err_free_device: |
| 303 | free_device(dev); |
| 304 | return NULL; |
| 305 | } |
| 306 | |
Paul Cercueil | 927818d | 2014-10-28 14:38:24 +0100 | [diff] [blame] | 307 | static struct iio_context * xml_clone(const struct iio_context *ctx) |
| 308 | { |
Paul Cercueil | 63e5218 | 2014-12-11 12:52:48 +0100 | [diff] [blame] | 309 | return xml_create_context_mem(ctx->xml, strlen(ctx->xml)); |
Paul Cercueil | 927818d | 2014-10-28 14:38:24 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Lars-Peter Clausen | 09a59d7 | 2016-02-03 15:27:04 +0100 | [diff] [blame] | 312 | static const struct iio_backend_ops xml_ops = { |
Paul Cercueil | 927818d | 2014-10-28 14:38:24 +0100 | [diff] [blame] | 313 | .clone = xml_clone, |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 314 | }; |
| 315 | |
Paul Cercueil | 00dc215 | 2016-11-15 16:37:35 +0100 | [diff] [blame] | 316 | static int parse_context_attr(struct iio_context *ctx, xmlNode *n) |
| 317 | { |
| 318 | xmlAttr *attr; |
| 319 | const char *name = NULL, *value = NULL; |
| 320 | |
| 321 | for (attr = n->properties; attr; attr = attr->next) { |
| 322 | if (!strcmp((const char *) attr->name, "name")) { |
| 323 | name = (const char *) attr->children->content; |
| 324 | } else if (!strcmp((const char *) attr->name, "value")) { |
| 325 | value = (const char *) attr->children->content; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (!name || !value) |
| 330 | return -EINVAL; |
| 331 | else |
| 332 | return iio_context_add_attr(ctx, name, value); |
| 333 | } |
| 334 | |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 335 | static struct iio_context * iio_create_xml_context_helper(xmlDoc *doc) |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 336 | { |
| 337 | unsigned int i; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 338 | xmlNode *root, *n; |
Paul Cercueil | 60ac1f6 | 2015-03-16 14:09:54 +0100 | [diff] [blame] | 339 | xmlAttr *attr; |
Paul Cercueil | 4679334 | 2016-12-15 16:54:44 +0100 | [diff] [blame] | 340 | int err = -ENOMEM; |
Lars-Peter Clausen | d1be838 | 2016-02-24 11:13:45 +0100 | [diff] [blame] | 341 | struct iio_context *ctx = zalloc(sizeof(*ctx)); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 342 | if (!ctx) |
Paul Cercueil | 8a85d3e | 2015-03-16 17:11:49 +0100 | [diff] [blame] | 343 | goto err_set_errno; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 344 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 345 | ctx->name = "xml"; |
| 346 | ctx->ops = &xml_ops; |
Harvey Yang | bc3f48e | 2019-10-22 11:17:28 +0800 | [diff] [blame] | 347 | ctx->client_id = -1; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 348 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 349 | root = xmlDocGetRootElement(doc); |
| 350 | if (strcmp((char *) root->name, "context")) { |
| 351 | ERROR("Unrecognized XML file\n"); |
Paul Cercueil | 4679334 | 2016-12-15 16:54:44 +0100 | [diff] [blame] | 352 | err = -EINVAL; |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 353 | goto err_free_ctx; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Paul Cercueil | 60ac1f6 | 2015-03-16 14:09:54 +0100 | [diff] [blame] | 356 | for (attr = root->properties; attr; attr = attr->next) { |
| 357 | if (!strcmp((char *) attr->name, "description")) |
Paul Cercueil | caf0e71 | 2016-08-25 17:27:02 +0200 | [diff] [blame] | 358 | ctx->description = iio_strdup( |
Paul Cercueil | 60ac1f6 | 2015-03-16 14:09:54 +0100 | [diff] [blame] | 359 | (char *) attr->children->content); |
| 360 | else if (strcmp((char *) attr->name, "name")) |
| 361 | WARNING("Unknown parameter \'%s\' in <context>\n", |
| 362 | (char *) attr->children->content); |
| 363 | } |
| 364 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 365 | for (n = root->children; n; n = n->next) { |
| 366 | struct iio_device **devs, *dev; |
| 367 | |
Paul Cercueil | 00dc215 | 2016-11-15 16:37:35 +0100 | [diff] [blame] | 368 | if (!strcmp((char *) n->name, "context-attribute")) { |
| 369 | err = parse_context_attr(ctx, n); |
| 370 | if (err) |
| 371 | goto err_free_devices; |
| 372 | else |
| 373 | continue; |
| 374 | } else if (strcmp((char *) n->name, "device")) { |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 375 | if (strcmp((char *) n->name, "text")) |
| 376 | WARNING("Unknown children \'%s\' in " |
| 377 | "<context>\n", n->name); |
| 378 | continue; |
| 379 | } |
| 380 | |
| 381 | dev = create_device(ctx, n); |
| 382 | if (!dev) { |
| 383 | ERROR("Unable to create device\n"); |
| 384 | goto err_free_devices; |
| 385 | } |
| 386 | |
| 387 | devs = realloc(ctx->devices, (1 + ctx->nb_devices) * |
| 388 | sizeof(struct iio_device *)); |
| 389 | if (!devs) { |
| 390 | ERROR("Unable to allocate memory\n"); |
| 391 | free(dev); |
| 392 | goto err_free_devices; |
| 393 | } |
| 394 | |
| 395 | devs[ctx->nb_devices++] = dev; |
| 396 | ctx->devices = devs; |
| 397 | } |
| 398 | |
Paul Cercueil | fd38747 | 2015-08-05 10:34:19 +0200 | [diff] [blame] | 399 | err = iio_context_init(ctx); |
| 400 | if (err) |
| 401 | goto err_free_devices; |
| 402 | |
Paul Cercueil | c1ed848 | 2014-06-11 16:29:43 +0200 | [diff] [blame] | 403 | return ctx; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 404 | |
| 405 | err_free_devices: |
| 406 | for (i = 0; i < ctx->nb_devices; i++) |
| 407 | free_device(ctx->devices[i]); |
| 408 | if (ctx->nb_devices) |
| 409 | free(ctx->devices); |
Paul Cercueil | 00dc215 | 2016-11-15 16:37:35 +0100 | [diff] [blame] | 410 | for (i = 0; i < ctx->nb_attrs; i++) { |
| 411 | free(ctx->attrs[i]); |
| 412 | free(ctx->values[i]); |
| 413 | } |
| 414 | free(ctx->attrs); |
| 415 | free(ctx->values); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 416 | err_free_ctx: |
| 417 | free(ctx); |
Paul Cercueil | 8a85d3e | 2015-03-16 17:11:49 +0100 | [diff] [blame] | 418 | err_set_errno: |
Paul Cercueil | 4679334 | 2016-12-15 16:54:44 +0100 | [diff] [blame] | 419 | errno = -err; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 420 | return NULL; |
| 421 | } |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 422 | |
Paul Cercueil | 63e5218 | 2014-12-11 12:52:48 +0100 | [diff] [blame] | 423 | struct iio_context * xml_create_context(const char *xml_file) |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 424 | { |
| 425 | struct iio_context *ctx; |
| 426 | xmlDoc *doc; |
| 427 | |
| 428 | LIBXML_TEST_VERSION; |
| 429 | |
| 430 | doc = xmlReadFile(xml_file, NULL, XML_PARSE_DTDVALID); |
| 431 | if (!doc) { |
| 432 | ERROR("Unable to parse XML file\n"); |
Paul Cercueil | 9efa26f | 2016-12-15 16:56:15 +0100 | [diff] [blame] | 433 | errno = EINVAL; |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 434 | return NULL; |
| 435 | } |
| 436 | |
| 437 | ctx = iio_create_xml_context_helper(doc); |
| 438 | xmlFreeDoc(doc); |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 439 | return ctx; |
| 440 | } |
| 441 | |
Paul Cercueil | 63e5218 | 2014-12-11 12:52:48 +0100 | [diff] [blame] | 442 | struct iio_context * xml_create_context_mem(const char *xml, size_t len) |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 443 | { |
| 444 | struct iio_context *ctx; |
| 445 | xmlDoc *doc; |
| 446 | |
| 447 | LIBXML_TEST_VERSION; |
| 448 | |
Paul Cercueil | 4012cff | 2015-05-11 10:47:40 +0200 | [diff] [blame] | 449 | doc = xmlReadMemory(xml, (int) len, NULL, NULL, XML_PARSE_DTDVALID); |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 450 | if (!doc) { |
| 451 | ERROR("Unable to parse XML file\n"); |
Paul Cercueil | 9efa26f | 2016-12-15 16:56:15 +0100 | [diff] [blame] | 452 | errno = EINVAL; |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 453 | return NULL; |
| 454 | } |
| 455 | |
| 456 | ctx = iio_create_xml_context_helper(doc); |
| 457 | xmlFreeDoc(doc); |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 458 | return ctx; |
| 459 | } |