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 | d4378e7 | 2014-05-30 13:37:34 +0200 | [diff] [blame] | 26 | #ifndef _WIN32 |
| 27 | #define _strdup strdup |
| 28 | #endif |
| 29 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 30 | static int add_attr_to_channel(struct iio_channel *chn, xmlNode *n) |
| 31 | { |
| 32 | xmlAttr *attr; |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 33 | char *name = NULL, *filename = NULL; |
Paul Cercueil | b34e022 | 2014-05-05 15:32:38 +0200 | [diff] [blame] | 34 | struct iio_channel_attr *attrs; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 35 | |
| 36 | for (attr = n->properties; attr; attr = attr->next) { |
| 37 | if (!strcmp((char *) attr->name, "name")) { |
Paul Cercueil | d4378e7 | 2014-05-30 13:37:34 +0200 | [diff] [blame] | 38 | name = _strdup((char *) attr->children->content); |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 39 | } else if (!strcmp((char *) attr->name, "filename")) { |
Paul Cercueil | d4378e7 | 2014-05-30 13:37:34 +0200 | [diff] [blame] | 40 | filename = _strdup((char *) attr->children->content); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 41 | } else { |
| 42 | WARNING("Unknown field \'%s\' in channel %s\n", |
| 43 | attr->name, chn->id); |
| 44 | } |
| 45 | } |
| 46 | |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 47 | if (!name || !filename) { |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 48 | ERROR("Incomplete attribute in channel %s\n", chn->id); |
| 49 | goto err_free; |
| 50 | } |
| 51 | |
Paul Cercueil | b34e022 | 2014-05-05 15:32:38 +0200 | [diff] [blame] | 52 | attrs = realloc(chn->attrs, (1 + chn->nb_attrs) * |
| 53 | sizeof(struct iio_channel_attr)); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 54 | if (!attrs) |
| 55 | goto err_free; |
| 56 | |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 57 | attrs[chn->nb_attrs].filename = filename; |
Paul Cercueil | b34e022 | 2014-05-05 15:32:38 +0200 | [diff] [blame] | 58 | attrs[chn->nb_attrs++].name = name; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 59 | chn->attrs = attrs; |
| 60 | return 0; |
| 61 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 62 | err_free: |
| 63 | if (name) |
| 64 | free(name); |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 65 | if (filename) |
| 66 | free(filename); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 67 | return -1; |
| 68 | } |
| 69 | |
Paul Cercueil | 501961b | 2014-04-15 11:23:30 +0200 | [diff] [blame] | 70 | static int add_attr_to_device(struct iio_device *dev, xmlNode *n, bool is_debug) |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 71 | { |
| 72 | xmlAttr *attr; |
Paul Cercueil | 5cd260e | 2014-02-24 13:16:01 +0100 | [diff] [blame] | 73 | char **attrs, *name = NULL; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 74 | |
| 75 | for (attr = n->properties; attr; attr = attr->next) { |
| 76 | if (!strcmp((char *) attr->name, "name")) { |
Paul Cercueil | d4378e7 | 2014-05-30 13:37:34 +0200 | [diff] [blame] | 77 | name = _strdup((char *) attr->children->content); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 78 | } else { |
| 79 | WARNING("Unknown field \'%s\' in device %s\n", |
| 80 | attr->name, dev->id); |
| 81 | } |
| 82 | } |
| 83 | |
Paul Cercueil | 5cd260e | 2014-02-24 13:16:01 +0100 | [diff] [blame] | 84 | if (!name) { |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 85 | ERROR("Incomplete attribute in device %s\n", dev->id); |
| 86 | goto err_free; |
| 87 | } |
| 88 | |
Paul Cercueil | 501961b | 2014-04-15 11:23:30 +0200 | [diff] [blame] | 89 | 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 Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 95 | if (!attrs) |
| 96 | goto err_free; |
| 97 | |
Paul Cercueil | 501961b | 2014-04-15 11:23:30 +0200 | [diff] [blame] | 98 | 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 Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 105 | return 0; |
| 106 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 107 | err_free: |
| 108 | if (name) |
| 109 | free(name); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 110 | return -1; |
| 111 | } |
| 112 | |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 113 | static 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 Cercueil | 6e4bc7a | 2014-06-11 14:33:48 +0200 | [diff] [blame] | 124 | sscanf(content, "%ce:%c%u/%u>>%u", &e, &s, |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 125 | &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 Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 140 | static 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 Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 148 | |
Paul Cercueil | d8d5893 | 2014-06-12 10:08:36 +0200 | [diff] [blame^] | 149 | /* Set the default index value < 0 (== no index) */ |
| 150 | chn->index = -ENOENT; |
| 151 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 152 | for (attr = n->properties; attr; attr = attr->next) { |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 153 | const char *name = (const char *) attr->name, |
| 154 | *content = (const char *) attr->children->content; |
| 155 | if (!strcmp(name, "name")) { |
Paul Cercueil | d4378e7 | 2014-05-30 13:37:34 +0200 | [diff] [blame] | 156 | chn->name = _strdup(content); |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 157 | } else if (!strcmp(name, "id")) { |
Paul Cercueil | d4378e7 | 2014-05-30 13:37:34 +0200 | [diff] [blame] | 158 | chn->id = _strdup(content); |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 159 | } else if (!strcmp(name, "type")) { |
| 160 | if (!strcmp(content, "output")) |
| 161 | chn->is_output = true; |
| 162 | else if (strcmp(content, "input")) |
| 163 | WARNING("Unknown channel type %s\n", content); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 164 | } else { |
| 165 | WARNING("Unknown attribute \'%s\' in <channel>\n", |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 166 | name); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | if (!chn->id) { |
| 171 | ERROR("Incomplete <attribute>\n"); |
| 172 | goto err_free_channel; |
| 173 | } |
| 174 | |
| 175 | for (n = n->children; n; n = n->next) { |
| 176 | if (!strcmp((char *) n->name, "attribute")) { |
| 177 | if (add_attr_to_channel(chn, n) < 0) |
| 178 | goto err_free_channel; |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 179 | } else if (!strcmp((char *) n->name, "scan-element")) { |
| 180 | chn->is_scan_element = true; |
| 181 | setup_scan_element(chn, n); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 182 | } else if (strcmp((char *) n->name, "text")) { |
| 183 | WARNING("Unknown children \'%s\' in <device>\n", |
| 184 | n->name); |
| 185 | continue; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return chn; |
| 190 | |
| 191 | err_free_channel: |
| 192 | free_channel(chn); |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | static struct iio_device * create_device(struct iio_context *ctx, xmlNode *n) |
| 197 | { |
| 198 | xmlAttr *attr; |
| 199 | struct iio_device *dev = calloc(1, sizeof(*dev)); |
| 200 | if (!dev) |
| 201 | return NULL; |
| 202 | |
| 203 | dev->ctx = ctx; |
| 204 | |
| 205 | for (attr = n->properties; attr; attr = attr->next) { |
| 206 | if (!strcmp((char *) attr->name, "name")) { |
Paul Cercueil | d4378e7 | 2014-05-30 13:37:34 +0200 | [diff] [blame] | 207 | dev->name = _strdup((char *) attr->children->content); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 208 | } else if (!strcmp((char *) attr->name, "id")) { |
Paul Cercueil | d4378e7 | 2014-05-30 13:37:34 +0200 | [diff] [blame] | 209 | dev->id = _strdup((char *) attr->children->content); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 210 | } else { |
| 211 | WARNING("Unknown attribute \'%s\' in <context>\n", |
| 212 | attr->name); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if (!dev->id) { |
| 217 | ERROR("Unable to read device ID\n"); |
| 218 | goto err_free_device; |
| 219 | } |
| 220 | |
| 221 | for (n = n->children; n; n = n->next) { |
| 222 | if (!strcmp((char *) n->name, "channel")) { |
| 223 | struct iio_channel **chns, |
| 224 | *chn = create_channel(dev, n); |
| 225 | if (!chn) { |
| 226 | ERROR("Unable to create channel\n"); |
| 227 | goto err_free_device; |
| 228 | } |
| 229 | |
| 230 | chns = realloc(dev->channels, (1 + dev->nb_channels) * |
| 231 | sizeof(struct iio_channel *)); |
| 232 | if (!chns) { |
| 233 | ERROR("Unable to allocate memory\n"); |
| 234 | free(chn); |
| 235 | goto err_free_device; |
| 236 | } |
| 237 | |
| 238 | chns[dev->nb_channels++] = chn; |
| 239 | dev->channels = chns; |
| 240 | } else if (!strcmp((char *) n->name, "attribute")) { |
Paul Cercueil | 501961b | 2014-04-15 11:23:30 +0200 | [diff] [blame] | 241 | if (add_attr_to_device(dev, n, false) < 0) |
| 242 | goto err_free_device; |
| 243 | } else if (!strcmp((char *) n->name, "debug-attribute")) { |
| 244 | if (add_attr_to_device(dev, n, true) < 0) |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 245 | goto err_free_device; |
| 246 | } else if (strcmp((char *) n->name, "text")) { |
| 247 | WARNING("Unknown children \'%s\' in <device>\n", |
| 248 | n->name); |
| 249 | continue; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return dev; |
| 254 | |
| 255 | err_free_device: |
| 256 | free_device(dev); |
| 257 | return NULL; |
| 258 | } |
| 259 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 260 | static struct iio_backend_ops xml_ops = { |
Paul Cercueil | 6e7f79e | 2014-04-04 12:27:24 +0200 | [diff] [blame] | 261 | .read = NULL, /* ISO C99 forbids empty initializer braces */ |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 262 | }; |
| 263 | |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 264 | static struct iio_context * iio_create_xml_context_helper(xmlDoc *doc) |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 265 | { |
| 266 | unsigned int i; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 267 | xmlNode *root, *n; |
| 268 | struct iio_context *ctx = calloc(1, sizeof(*ctx)); |
| 269 | if (!ctx) |
| 270 | return NULL; |
| 271 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 272 | ctx->name = "xml"; |
| 273 | ctx->ops = &xml_ops; |
| 274 | |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 275 | root = xmlDocGetRootElement(doc); |
| 276 | if (strcmp((char *) root->name, "context")) { |
| 277 | ERROR("Unrecognized XML file\n"); |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 278 | goto err_free_ctx; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | for (n = root->children; n; n = n->next) { |
| 282 | struct iio_device **devs, *dev; |
| 283 | |
| 284 | if (strcmp((char *) n->name, "device")) { |
| 285 | if (strcmp((char *) n->name, "text")) |
| 286 | WARNING("Unknown children \'%s\' in " |
| 287 | "<context>\n", n->name); |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | dev = create_device(ctx, n); |
| 292 | if (!dev) { |
| 293 | ERROR("Unable to create device\n"); |
| 294 | goto err_free_devices; |
| 295 | } |
| 296 | |
| 297 | devs = realloc(ctx->devices, (1 + ctx->nb_devices) * |
| 298 | sizeof(struct iio_device *)); |
| 299 | if (!devs) { |
| 300 | ERROR("Unable to allocate memory\n"); |
| 301 | free(dev); |
| 302 | goto err_free_devices; |
| 303 | } |
| 304 | |
| 305 | devs[ctx->nb_devices++] = dev; |
| 306 | ctx->devices = devs; |
| 307 | } |
| 308 | |
Paul Cercueil | c1ed848 | 2014-06-11 16:29:43 +0200 | [diff] [blame] | 309 | iio_context_init(ctx); |
| 310 | return ctx; |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 311 | |
| 312 | err_free_devices: |
| 313 | for (i = 0; i < ctx->nb_devices; i++) |
| 314 | free_device(ctx->devices[i]); |
| 315 | if (ctx->nb_devices) |
| 316 | free(ctx->devices); |
Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 317 | err_free_ctx: |
| 318 | free(ctx); |
| 319 | return NULL; |
| 320 | } |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 321 | |
| 322 | struct iio_context * iio_create_xml_context(const char *xml_file) |
| 323 | { |
| 324 | struct iio_context *ctx; |
| 325 | xmlDoc *doc; |
| 326 | |
| 327 | LIBXML_TEST_VERSION; |
| 328 | |
| 329 | doc = xmlReadFile(xml_file, NULL, XML_PARSE_DTDVALID); |
| 330 | if (!doc) { |
| 331 | ERROR("Unable to parse XML file\n"); |
| 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | ctx = iio_create_xml_context_helper(doc); |
| 336 | xmlFreeDoc(doc); |
| 337 | xmlCleanupParser(); |
| 338 | return ctx; |
| 339 | } |
| 340 | |
Paul Cercueil | dda33eb | 2014-03-03 15:00:11 +0100 | [diff] [blame] | 341 | struct iio_context * iio_create_xml_context_mem(const char *xml, size_t len) |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 342 | { |
| 343 | struct iio_context *ctx; |
| 344 | xmlDoc *doc; |
| 345 | |
| 346 | LIBXML_TEST_VERSION; |
| 347 | |
Paul Cercueil | dda33eb | 2014-03-03 15:00:11 +0100 | [diff] [blame] | 348 | doc = xmlReadMemory(xml, len, NULL, NULL, XML_PARSE_DTDVALID); |
Paul Cercueil | 8b1f9d0 | 2014-02-21 16:24:51 +0100 | [diff] [blame] | 349 | if (!doc) { |
| 350 | ERROR("Unable to parse XML file\n"); |
| 351 | return NULL; |
| 352 | } |
| 353 | |
| 354 | ctx = iio_create_xml_context_helper(doc); |
| 355 | xmlFreeDoc(doc); |
| 356 | xmlCleanupParser(); |
| 357 | return ctx; |
| 358 | } |