blob: 491c15ec9986275397ce2e996ac823aeb9f6c32f [file] [log] [blame]
Paul Cercueil42090d12014-02-24 12:32:23 +01001#include "debug.h"
Paul Cercueil0b2ce712014-02-17 15:04:18 +01002#include "iio-private.h"
3
Paul Cercueil42090d12014-02-24 12:32:23 +01004#include <string.h>
5
6static const char xml_header[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
7"<!DOCTYPE context ["
8"<!ELEMENT context (device)*>"
9"<!ELEMENT device (channel | attribute)*>"
10"<!ELEMENT channel (attribute)*>"
11"<!ELEMENT attribute EMPTY>"
12"<!ATTLIST context name CDATA #REQUIRED>"
13"<!ATTLIST device id CDATA #REQUIRED name CDATA #IMPLIED>"
14"<!ATTLIST channel id CDATA #REQUIRED type CDATA #REQUIRED name CDATA #IMPLIED>"
15"<!ATTLIST attribute name CDATA #REQUIRED>"
16"]>";
17
18/* Returns a string containing the XML representation of this context */
19char * iio_context_get_xml(const struct iio_context *ctx)
20{
21 size_t len = strlen(ctx->name) +
22 sizeof(xml_header) - 1 +
23 sizeof("<context name=\"\" ></context>");
24 char *str, *ptr, *devices[ctx->nb_devices];
25 size_t devices_len[ctx->nb_devices];
26 unsigned int i;
27
28 for (i = 0; i < ctx->nb_devices; i++) {
29 char *xml = iio_device_get_xml(ctx->devices[i],
30 &devices_len[i]);
31 if (!xml)
32 goto err_free_devices;
33 devices[i] = xml;
34 len += devices_len[i];
35 }
36
37 str = malloc(len);
38 if (!str)
39 goto err_free_devices;
40
41 sprintf(str, "%s<context name=\"%s\" >", xml_header, ctx->name);
42 ptr = strrchr(str, '\0');
43
44 for (i = 0; i < ctx->nb_devices; i++) {
45 strcpy(ptr, devices[i]);
46 ptr += devices_len[i];
47 free(devices[i]);
48 }
49
50 strcpy(ptr, "</context>");
51 return str;
52
53err_free_devices:
54 while (i--)
55 free(devices[i]);
56 return NULL;
57}
58
Paul Cercueil0b2ce712014-02-17 15:04:18 +010059const char * iio_context_get_name(const struct iio_context *ctx)
60{
61 return ctx->name;
62}
63
64void iio_context_destroy(struct iio_context *ctx)
65{
Paul Cercueil50363542014-02-18 16:04:28 +010066 unsigned int i;
67 if (ctx->ops->shutdown)
68 ctx->ops->shutdown(ctx);
69
70 for (i = 0; i < ctx->nb_devices; i++)
71 free_device(ctx->devices[i]);
72 if (ctx->nb_devices)
73 free(ctx->devices);
Paul Cercueil0b2ce712014-02-17 15:04:18 +010074 free(ctx);
75}
76
77unsigned int iio_context_get_devices_count(const struct iio_context *ctx)
78{
79 return ctx->nb_devices;
80}
81
82struct iio_device * iio_context_get_device(const struct iio_context *ctx,
83 unsigned int index)
84{
85 if (index >= ctx->nb_devices)
86 return NULL;
87 else
88 return ctx->devices[index];
89}