Paul Cercueil | 5d9db30 | 2014-02-18 18:16:51 +0100 | [diff] [blame] | 1 | #include "debug.h" |
| 2 | #include "iio-private.h" |
| 3 | |
| 4 | #include <errno.h> |
| 5 | #include <libxml/tree.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | struct value_map { |
| 9 | const struct iio_device *dev; |
| 10 | const struct iio_channel *chn; |
| 11 | const char *attr; |
| 12 | char *value; |
| 13 | }; |
| 14 | |
| 15 | struct xml_pdata { |
| 16 | struct value_map *values; |
| 17 | unsigned int nb_values; |
| 18 | }; |
| 19 | |
| 20 | static 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 | |
| 63 | err_update_attrs: |
| 64 | /* the first realloc succeeded so we must update chn->attrs |
| 65 | * even if an error occured later */ |
| 66 | chn->attrs = attrs; |
| 67 | err_free: |
| 68 | if (name) |
| 69 | free(name); |
| 70 | if (value) |
| 71 | free(value); |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | static 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 | |
| 118 | err_update_attrs: |
| 119 | /* the first realloc succeeded so we must update dev->attrs |
| 120 | * even if an error occured later */ |
| 121 | dev->attrs = attrs; |
| 122 | err_free: |
| 123 | if (name) |
| 124 | free(name); |
| 125 | if (value) |
| 126 | free(value); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | static 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 | |
| 179 | err_free_channel: |
| 180 | free_channel(chn); |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | static 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 | |
| 240 | err_free_device: |
| 241 | free_device(dev); |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static 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 | |
| 265 | static 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 | |
| 272 | static 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 | |
| 279 | static 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 | |
| 295 | static 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 | |
| 301 | struct 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"); |
| 313 | free(ctx); |
| 314 | return NULL; |
| 315 | } |
| 316 | |
| 317 | ctx->name = "xml"; |
| 318 | ctx->ops = &xml_ops; |
| 319 | |
| 320 | LIBXML_TEST_VERSION; |
| 321 | |
| 322 | doc = xmlReadFile(xml_file, NULL, 0); |
| 323 | if (!doc) { |
| 324 | ERROR("Unable to parse XML file\n"); |
| 325 | goto err_free_ctx; |
| 326 | } |
| 327 | |
| 328 | root = xmlDocGetRootElement(doc); |
| 329 | if (strcmp((char *) root->name, "context")) { |
| 330 | ERROR("Unrecognized XML file\n"); |
| 331 | goto err_free_doc; |
| 332 | } |
| 333 | |
| 334 | for (n = root->children; n; n = n->next) { |
| 335 | struct iio_device **devs, *dev; |
| 336 | |
| 337 | if (strcmp((char *) n->name, "device")) { |
| 338 | if (strcmp((char *) n->name, "text")) |
| 339 | WARNING("Unknown children \'%s\' in " |
| 340 | "<context>\n", n->name); |
| 341 | continue; |
| 342 | } |
| 343 | |
| 344 | dev = create_device(ctx, n); |
| 345 | if (!dev) { |
| 346 | ERROR("Unable to create device\n"); |
| 347 | goto err_free_devices; |
| 348 | } |
| 349 | |
| 350 | devs = realloc(ctx->devices, (1 + ctx->nb_devices) * |
| 351 | sizeof(struct iio_device *)); |
| 352 | if (!devs) { |
| 353 | ERROR("Unable to allocate memory\n"); |
| 354 | free(dev); |
| 355 | goto err_free_devices; |
| 356 | } |
| 357 | |
| 358 | devs[ctx->nb_devices++] = dev; |
| 359 | ctx->devices = devs; |
| 360 | } |
| 361 | |
| 362 | return ctx; |
| 363 | |
| 364 | err_free_devices: |
| 365 | for (i = 0; i < ctx->nb_devices; i++) |
| 366 | free_device(ctx->devices[i]); |
| 367 | if (ctx->nb_devices) |
| 368 | free(ctx->devices); |
| 369 | err_free_doc: |
| 370 | xmlFreeDoc(doc); |
| 371 | xmlCleanupParser(); |
| 372 | err_free_ctx: |
| 373 | free(ctx); |
| 374 | return NULL; |
| 375 | } |