blob: b58179ae130ee352c3d63decd3be228b81976ab1 [file] [log] [blame]
Paul Cercueilbb4401d2014-02-28 16:10:49 +01001/*
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 Cercueil42090d12014-02-24 12:32:23 +010019#include "debug.h"
Paul Cercueil0b2ce712014-02-17 15:04:18 +010020#include "iio-private.h"
21
Paul Cercueil4c6729d2014-04-04 17:24:41 +020022#include <errno.h>
Paul Cercueil42090d12014-02-24 12:32:23 +010023#include <string.h>
24
Paul Cercueil8f56ea72014-10-28 15:18:18 +010025#ifdef _WIN32
26#define LOCAL_BACKEND 0
27#define NETWORK_BACKEND 1
28#endif
29
Paul Cercueil42090d12014-02-24 12:32:23 +010030static const char xml_header[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
31"<!DOCTYPE context ["
32"<!ELEMENT context (device)*>"
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020033"<!ELEMENT device (channel | attribute | debug-attribute)*>"
Paul Cercueila7e80022014-06-11 11:41:26 +020034"<!ELEMENT channel (scan-element?, attribute*)>"
Paul Cercueil42090d12014-02-24 12:32:23 +010035"<!ELEMENT attribute EMPTY>"
Paul Cercueila7e80022014-06-11 11:41:26 +020036"<!ELEMENT scan-element EMPTY>"
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020037"<!ELEMENT debug-attribute EMPTY>"
Paul Cercueil42090d12014-02-24 12:32:23 +010038"<!ATTLIST context name CDATA #REQUIRED>"
39"<!ATTLIST device id CDATA #REQUIRED name CDATA #IMPLIED>"
Paul Cercueila7e80022014-06-11 11:41:26 +020040"<!ATTLIST channel id CDATA #REQUIRED type (input|output) #REQUIRED name CDATA #IMPLIED>"
41"<!ATTLIST scan-element index CDATA #REQUIRED format CDATA #REQUIRED scale CDATA #IMPLIED>"
Paul Cercueil42d12352014-05-05 16:11:58 +020042"<!ATTLIST attribute name CDATA #REQUIRED filename CDATA #IMPLIED>"
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020043"<!ATTLIST debug-attribute name CDATA #REQUIRED>"
Paul Cercueil42090d12014-02-24 12:32:23 +010044"]>";
45
46/* Returns a string containing the XML representation of this context */
Paul Cercueilc1ed8482014-06-11 16:29:43 +020047char * iio_context_create_xml(const struct iio_context *ctx)
Paul Cercueil42090d12014-02-24 12:32:23 +010048{
Paul Cercueil9aab0192014-04-07 11:04:30 +020049 size_t len = strlen(ctx->name) + sizeof(xml_header) - 1 +
Paul Cercueil42090d12014-02-24 12:32:23 +010050 sizeof("<context name=\"\" ></context>");
Paul Cercueil5822ab62014-04-04 13:29:17 +020051 size_t *devices_len;
52 char *str, *ptr, **devices;
Paul Cercueil42090d12014-02-24 12:32:23 +010053 unsigned int i;
54
Paul Cercueil9aab0192014-04-07 11:04:30 +020055 if (!ctx->nb_devices) {
56 str = malloc(len);
57 if (str)
58 snprintf(str, len, "%s<context name=\"%s\" ></context>",
59 xml_header, ctx->name);
60 return str;
61 }
62
Paul Cercueil5822ab62014-04-04 13:29:17 +020063 devices_len = malloc(ctx->nb_devices * sizeof(*devices_len));
64 if (!devices_len)
65 return NULL;
66
67 devices = malloc(ctx->nb_devices * sizeof(*devices));
68 if (!devices)
69 goto err_free_devices_len;
70
Paul Cercueil42090d12014-02-24 12:32:23 +010071 for (i = 0; i < ctx->nb_devices; i++) {
72 char *xml = iio_device_get_xml(ctx->devices[i],
73 &devices_len[i]);
74 if (!xml)
75 goto err_free_devices;
76 devices[i] = xml;
77 len += devices_len[i];
78 }
79
80 str = malloc(len);
81 if (!str)
82 goto err_free_devices;
83
Paul Cercueil8c29e412014-04-07 09:46:45 +020084 snprintf(str, len, "%s<context name=\"%s\" >", xml_header, ctx->name);
Paul Cercueil42090d12014-02-24 12:32:23 +010085 ptr = strrchr(str, '\0');
86
87 for (i = 0; i < ctx->nb_devices; i++) {
88 strcpy(ptr, devices[i]);
89 ptr += devices_len[i];
90 free(devices[i]);
91 }
92
Paul Cercueil5822ab62014-04-04 13:29:17 +020093 free(devices);
94 free(devices_len);
Paul Cercueil42090d12014-02-24 12:32:23 +010095 strcpy(ptr, "</context>");
96 return str;
97
98err_free_devices:
99 while (i--)
100 free(devices[i]);
Paul Cercueil5822ab62014-04-04 13:29:17 +0200101 free(devices);
102err_free_devices_len:
103 free(devices_len);
Paul Cercueil42090d12014-02-24 12:32:23 +0100104 return NULL;
105}
106
Paul Cercueil4c6729d2014-04-04 17:24:41 +0200107const char * iio_context_get_xml(const struct iio_context *ctx)
108{
109 return ctx->xml;
110}
111
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100112const char * iio_context_get_name(const struct iio_context *ctx)
113{
114 return ctx->name;
115}
116
Paul Cercueil3ac36c12015-01-08 14:44:00 +0100117const char * iio_context_get_description(const struct iio_context *ctx)
118{
119 if (ctx->description)
120 return ctx->description;
121 else
122 return "";
123}
124
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100125void iio_context_destroy(struct iio_context *ctx)
126{
Paul Cercueil50363542014-02-18 16:04:28 +0100127 unsigned int i;
128 if (ctx->ops->shutdown)
129 ctx->ops->shutdown(ctx);
130
131 for (i = 0; i < ctx->nb_devices; i++)
132 free_device(ctx->devices[i]);
133 if (ctx->nb_devices)
134 free(ctx->devices);
Paul Cercueil4c6729d2014-04-04 17:24:41 +0200135 if (ctx->xml)
136 free(ctx->xml);
Paul Cercueil3ac36c12015-01-08 14:44:00 +0100137 if (ctx->description)
138 free(ctx->description);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100139 free(ctx);
140}
141
142unsigned int iio_context_get_devices_count(const struct iio_context *ctx)
143{
144 return ctx->nb_devices;
145}
146
147struct iio_device * iio_context_get_device(const struct iio_context *ctx,
148 unsigned int index)
149{
150 if (index >= ctx->nb_devices)
151 return NULL;
152 else
153 return ctx->devices[index];
154}
Paul Cercueilae88fde2014-03-12 11:47:10 +0100155
Paul Cercueil17512b02014-03-28 11:15:24 +0100156struct iio_device * iio_context_find_device(const struct iio_context *ctx,
157 const char *name)
158{
159 unsigned int i;
160 for (i = 0; i < ctx->nb_devices; i++) {
161 struct iio_device *dev = ctx->devices[i];
162 if (!strcmp(dev->id, name) ||
163 (dev->name && !strcmp(dev->name, name)))
164 return dev;
165 }
166 return NULL;
167}
168
Paul Cercueil1f7bc152014-03-14 13:48:43 +0100169static void reorder_channels(struct iio_device *dev)
170{
171 bool found;
172 unsigned int i;
173
174 /* Reorder channels by index */
175 do {
176 found = false;
177 for (i = 1; i < dev->nb_channels; i++) {
178 struct iio_channel **channels = dev->channels;
179 long ch1 = channels[i - 1]->index;
180 long ch2 = channels[i]->index;
181
182 if (ch2 >= 0 && ((ch1 > ch2) || ch1 < 0)) {
183 struct iio_channel *bak = channels[i];
184 channels[i] = channels[i - 1];
185 channels[i - 1] = bak;
186 found = true;
187 }
188 }
189 } while (found);
190}
191
Paul Cercueilc1ed8482014-06-11 16:29:43 +0200192void iio_context_init(struct iio_context *ctx)
Paul Cercueilae88fde2014-03-12 11:47:10 +0100193{
194 unsigned int i;
Paul Cercueila7e80022014-06-11 11:41:26 +0200195 for (i = 0; i < ctx->nb_devices; i++)
196 reorder_channels(ctx->devices[i]);
Paul Cercueilae88fde2014-03-12 11:47:10 +0100197}
Paul Cercueile45f8762014-05-02 11:19:26 +0200198
199int iio_context_get_version(const struct iio_context *ctx,
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200200 unsigned int *major, unsigned int *minor, char git_tag[8])
Paul Cercueile45f8762014-05-02 11:19:26 +0200201{
202 if (ctx->ops->get_version)
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200203 return ctx->ops->get_version(ctx, major, minor, git_tag);
Paul Cercueil53ed9432014-11-20 13:46:04 +0100204
205 iio_library_get_version(major, minor, git_tag);
Paul Cercueile45f8762014-05-02 11:19:26 +0200206 return 0;
207}
Paul Cercueil4ca73542014-06-10 16:23:29 +0200208
209int iio_context_set_timeout(struct iio_context *ctx, unsigned int timeout)
210{
211 if (ctx->ops->set_timeout)
212 return ctx->ops->set_timeout(ctx, timeout);
213 else
214 return -ENOSYS;
215}
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100216
217struct iio_context * iio_context_clone(const struct iio_context *ctx)
218{
219 if (ctx->ops->clone)
220 return ctx->ops->clone(ctx);
221 else
222 return NULL;
223}
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100224
225struct iio_context * iio_create_default_context(void)
226{
227#if NETWORK_BACKEND
228 char *hostname = getenv("IIOD_REMOTE");
229
230 if (hostname) {
231 /* If the environment variable is an empty string, we will
232 * discover the server using ZeroConf */
233 if (strlen(hostname) == 0)
234 hostname = NULL;
235
236 return iio_create_network_context(hostname);
237 }
238#endif
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100239 return iio_create_local_context();
Paul Cercueil63e52182014-12-11 12:52:48 +0100240}
241
242struct iio_context * iio_create_local_context(void)
243{
244#if LOCAL_BACKEND
245 return local_create_context();
246#else
247 return NULL;
248#endif
249}
250
251struct iio_context * iio_create_network_context(const char *hostname)
252{
253#if NETWORK_BACKEND
254 return network_create_context(hostname);
255#else
256 return NULL;
257#endif
258}
259
260struct iio_context * iio_create_xml_context_mem(const char *xml, size_t len)
261{
262#if NETWORK_BACKEND
263 return xml_create_context_mem(xml, len);
264#else
265 return NULL;
266#endif
267}
268
269struct iio_context * iio_create_xml_context(const char *xml_file)
270{
271#if NETWORK_BACKEND
272 return xml_create_context(xml_file);
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100273#else
274 return NULL;
275#endif
276}