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 | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 19 | #include "debug.h" |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 20 | #include "iio-private.h" |
| 21 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 22 | #include <string.h> |
| 23 | |
| 24 | static const char xml_header[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" |
| 25 | "<!DOCTYPE context [" |
| 26 | "<!ELEMENT context (device)*>" |
| 27 | "<!ELEMENT device (channel | attribute)*>" |
| 28 | "<!ELEMENT channel (attribute)*>" |
| 29 | "<!ELEMENT attribute EMPTY>" |
| 30 | "<!ATTLIST context name CDATA #REQUIRED>" |
| 31 | "<!ATTLIST device id CDATA #REQUIRED name CDATA #IMPLIED>" |
| 32 | "<!ATTLIST channel id CDATA #REQUIRED type CDATA #REQUIRED name CDATA #IMPLIED>" |
| 33 | "<!ATTLIST attribute name CDATA #REQUIRED>" |
| 34 | "]>"; |
| 35 | |
| 36 | /* Returns a string containing the XML representation of this context */ |
| 37 | char * iio_context_get_xml(const struct iio_context *ctx) |
| 38 | { |
| 39 | size_t len = strlen(ctx->name) + |
| 40 | sizeof(xml_header) - 1 + |
| 41 | sizeof("<context name=\"\" ></context>"); |
| 42 | char *str, *ptr, *devices[ctx->nb_devices]; |
| 43 | size_t devices_len[ctx->nb_devices]; |
| 44 | unsigned int i; |
| 45 | |
| 46 | for (i = 0; i < ctx->nb_devices; i++) { |
| 47 | char *xml = iio_device_get_xml(ctx->devices[i], |
| 48 | &devices_len[i]); |
| 49 | if (!xml) |
| 50 | goto err_free_devices; |
| 51 | devices[i] = xml; |
| 52 | len += devices_len[i]; |
| 53 | } |
| 54 | |
| 55 | str = malloc(len); |
| 56 | if (!str) |
| 57 | goto err_free_devices; |
| 58 | |
| 59 | sprintf(str, "%s<context name=\"%s\" >", xml_header, ctx->name); |
| 60 | ptr = strrchr(str, '\0'); |
| 61 | |
| 62 | for (i = 0; i < ctx->nb_devices; i++) { |
| 63 | strcpy(ptr, devices[i]); |
| 64 | ptr += devices_len[i]; |
| 65 | free(devices[i]); |
| 66 | } |
| 67 | |
| 68 | strcpy(ptr, "</context>"); |
| 69 | return str; |
| 70 | |
| 71 | err_free_devices: |
| 72 | while (i--) |
| 73 | free(devices[i]); |
| 74 | return NULL; |
| 75 | } |
| 76 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 77 | const char * iio_context_get_name(const struct iio_context *ctx) |
| 78 | { |
| 79 | return ctx->name; |
| 80 | } |
| 81 | |
| 82 | void iio_context_destroy(struct iio_context *ctx) |
| 83 | { |
Paul Cercueil | 5036354 | 2014-02-18 16:04:28 +0100 | [diff] [blame] | 84 | unsigned int i; |
| 85 | if (ctx->ops->shutdown) |
| 86 | ctx->ops->shutdown(ctx); |
| 87 | |
| 88 | for (i = 0; i < ctx->nb_devices; i++) |
| 89 | free_device(ctx->devices[i]); |
| 90 | if (ctx->nb_devices) |
| 91 | free(ctx->devices); |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 92 | free(ctx); |
| 93 | } |
| 94 | |
| 95 | unsigned int iio_context_get_devices_count(const struct iio_context *ctx) |
| 96 | { |
| 97 | return ctx->nb_devices; |
| 98 | } |
| 99 | |
| 100 | struct iio_device * iio_context_get_device(const struct iio_context *ctx, |
| 101 | unsigned int index) |
| 102 | { |
| 103 | if (index >= ctx->nb_devices) |
| 104 | return NULL; |
| 105 | else |
| 106 | return ctx->devices[index]; |
| 107 | } |