blob: 1777d24cda2b716a92802a250fb19ec5d52f1080 [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 Cercueil2814ed12016-08-25 17:08:18 +020020#include "iio-config.h"
Paul Cercueil0b2ce712014-02-17 15:04:18 +010021#include "iio-private.h"
22
Paul Cercueil4c6729d2014-04-04 17:24:41 +020023#include <errno.h>
Paul Cercueil42090d12014-02-24 12:32:23 +010024#include <string.h>
25
Paul Cercueil8f56ea72014-10-28 15:18:18 +010026#ifdef _WIN32
27#define LOCAL_BACKEND 0
28#define NETWORK_BACKEND 1
29#endif
30
Paul Cercueil42090d12014-02-24 12:32:23 +010031static const char xml_header[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
32"<!DOCTYPE context ["
Paul Cercueilc39540a2016-11-15 16:55:28 +010033"<!ELEMENT context (device | context-attribute)*>"
34"<!ELEMENT context-attribute EMPTY>"
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020035"<!ELEMENT device (channel | attribute | debug-attribute)*>"
Paul Cercueila7e80022014-06-11 11:41:26 +020036"<!ELEMENT channel (scan-element?, attribute*)>"
Paul Cercueil42090d12014-02-24 12:32:23 +010037"<!ELEMENT attribute EMPTY>"
Paul Cercueila7e80022014-06-11 11:41:26 +020038"<!ELEMENT scan-element EMPTY>"
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020039"<!ELEMENT debug-attribute EMPTY>"
Paul Cercueil87350372015-03-16 13:24:37 +010040"<!ATTLIST context name CDATA #REQUIRED description CDATA #IMPLIED>"
Paul Cercueilc39540a2016-11-15 16:55:28 +010041"<!ATTLIST context-attribute name CDATA #REQUIRED value CDATA #REQUIRED>"
Paul Cercueil42090d12014-02-24 12:32:23 +010042"<!ATTLIST device id CDATA #REQUIRED name CDATA #IMPLIED>"
Paul Cercueila7e80022014-06-11 11:41:26 +020043"<!ATTLIST channel id CDATA #REQUIRED type (input|output) #REQUIRED name CDATA #IMPLIED>"
44"<!ATTLIST scan-element index CDATA #REQUIRED format CDATA #REQUIRED scale CDATA #IMPLIED>"
Paul Cercueil42d12352014-05-05 16:11:58 +020045"<!ATTLIST attribute name CDATA #REQUIRED filename CDATA #IMPLIED>"
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020046"<!ATTLIST debug-attribute name CDATA #REQUIRED>"
Paul Cercueil42090d12014-02-24 12:32:23 +010047"]>";
48
49/* Returns a string containing the XML representation of this context */
Paul Cercueilc1ed8482014-06-11 16:29:43 +020050char * iio_context_create_xml(const struct iio_context *ctx)
Paul Cercueil42090d12014-02-24 12:32:23 +010051{
Paul Cercueilc39540a2016-11-15 16:55:28 +010052 size_t len, *devices_len = NULL;
53 char *str, *ptr, **devices = NULL;
Paul Cercueil42090d12014-02-24 12:32:23 +010054 unsigned int i;
55
Paul Cercueil87350372015-03-16 13:24:37 +010056 len = strlen(ctx->name) + sizeof(xml_header) - 1 +
57 sizeof("<context name=\"\" ></context>");
58 if (ctx->description)
59 len += strlen(ctx->description) +
60 sizeof(" description=\"\"") - 1;
61
Paul Cercueilc39540a2016-11-15 16:55:28 +010062 for (i = 0; i < ctx->nb_attrs; i++)
63 len += strlen(ctx->attrs[i]) +
64 strlen(ctx->values[i]) +
65 sizeof("<context-attribute name=\"\" value=\"\" />");
66
67 if (ctx->nb_devices) {
68 devices_len = malloc(ctx->nb_devices * sizeof(*devices_len));
69 if (!devices_len) {
Paul Cercueildfeca0d2015-03-16 17:18:00 +010070 errno = ENOMEM;
Paul Cercueil87350372015-03-16 13:24:37 +010071 return NULL;
Paul Cercueildfeca0d2015-03-16 17:18:00 +010072 }
Paul Cercueil87350372015-03-16 13:24:37 +010073
Paul Cercueilc39540a2016-11-15 16:55:28 +010074 devices = malloc(ctx->nb_devices * sizeof(*devices));
75 if (!devices)
76 goto err_free_devices_len;
Paul Cercueil9aab0192014-04-07 11:04:30 +020077
Paul Cercueilc39540a2016-11-15 16:55:28 +010078 for (i = 0; i < ctx->nb_devices; i++) {
79 char *xml = iio_device_get_xml(ctx->devices[i],
80 &devices_len[i]);
81 if (!xml)
82 goto err_free_devices;
83 devices[i] = xml;
84 len += devices_len[i];
85 }
Paul Cercueil42090d12014-02-24 12:32:23 +010086 }
87
88 str = malloc(len);
Paul Cercueildfeca0d2015-03-16 17:18:00 +010089 if (!str) {
90 errno = ENOMEM;
Paul Cercueil42090d12014-02-24 12:32:23 +010091 goto err_free_devices;
Paul Cercueildfeca0d2015-03-16 17:18:00 +010092 }
Paul Cercueil42090d12014-02-24 12:32:23 +010093
Paul Cercueil87350372015-03-16 13:24:37 +010094 if (ctx->description)
95 snprintf(str, len, "%s<context name=\"%s\" "
96 "description=\"%s\" >",
97 xml_header, ctx->name, ctx->description);
98 else
99 snprintf(str, len, "%s<context name=\"%s\" >",
100 xml_header, ctx->name);
Paul Cercueil42090d12014-02-24 12:32:23 +0100101 ptr = strrchr(str, '\0');
102
Paul Cercueilc39540a2016-11-15 16:55:28 +0100103 for (i = 0; i < ctx->nb_attrs; i++)
104 ptr += sprintf(ptr, "<context-attribute name=\"%s\" value=\"%s\" />",
105 ctx->attrs[i], ctx->values[i]);
106
107
Paul Cercueil42090d12014-02-24 12:32:23 +0100108 for (i = 0; i < ctx->nb_devices; i++) {
109 strcpy(ptr, devices[i]);
110 ptr += devices_len[i];
111 free(devices[i]);
112 }
113
Paul Cercueil5822ab62014-04-04 13:29:17 +0200114 free(devices);
115 free(devices_len);
Paul Cercueil42090d12014-02-24 12:32:23 +0100116 strcpy(ptr, "</context>");
117 return str;
118
119err_free_devices:
120 while (i--)
121 free(devices[i]);
Paul Cercueil5822ab62014-04-04 13:29:17 +0200122 free(devices);
123err_free_devices_len:
124 free(devices_len);
Paul Cercueil42090d12014-02-24 12:32:23 +0100125 return NULL;
126}
127
Paul Cercueil4c6729d2014-04-04 17:24:41 +0200128const char * iio_context_get_xml(const struct iio_context *ctx)
129{
130 return ctx->xml;
131}
132
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100133const char * iio_context_get_name(const struct iio_context *ctx)
134{
135 return ctx->name;
136}
137
Paul Cercueil3ac36c12015-01-08 14:44:00 +0100138const char * iio_context_get_description(const struct iio_context *ctx)
139{
140 if (ctx->description)
141 return ctx->description;
142 else
143 return "";
144}
145
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100146void iio_context_destroy(struct iio_context *ctx)
147{
Paul Cercueil50363542014-02-18 16:04:28 +0100148 unsigned int i;
149 if (ctx->ops->shutdown)
150 ctx->ops->shutdown(ctx);
151
Paul Cercueil60b4d1b2016-11-15 15:55:41 +0100152 for (i = 0; i < ctx->nb_attrs; i++) {
153 free(ctx->attrs[i]);
154 free(ctx->values[i]);
155 }
156 if (ctx->nb_attrs) {
157 free(ctx->attrs);
158 free(ctx->values);
159 }
Paul Cercueil50363542014-02-18 16:04:28 +0100160 for (i = 0; i < ctx->nb_devices; i++)
161 free_device(ctx->devices[i]);
162 if (ctx->nb_devices)
163 free(ctx->devices);
Paul Cercueil4c6729d2014-04-04 17:24:41 +0200164 if (ctx->xml)
165 free(ctx->xml);
Paul Cercueil3ac36c12015-01-08 14:44:00 +0100166 if (ctx->description)
167 free(ctx->description);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100168 free(ctx);
169}
170
171unsigned int iio_context_get_devices_count(const struct iio_context *ctx)
172{
173 return ctx->nb_devices;
174}
175
176struct iio_device * iio_context_get_device(const struct iio_context *ctx,
177 unsigned int index)
178{
179 if (index >= ctx->nb_devices)
180 return NULL;
181 else
182 return ctx->devices[index];
183}
Paul Cercueilae88fde2014-03-12 11:47:10 +0100184
Paul Cercueil17512b02014-03-28 11:15:24 +0100185struct iio_device * iio_context_find_device(const struct iio_context *ctx,
186 const char *name)
187{
188 unsigned int i;
189 for (i = 0; i < ctx->nb_devices; i++) {
190 struct iio_device *dev = ctx->devices[i];
191 if (!strcmp(dev->id, name) ||
192 (dev->name && !strcmp(dev->name, name)))
193 return dev;
194 }
195 return NULL;
196}
197
Paul Cercueil1f7bc152014-03-14 13:48:43 +0100198static void reorder_channels(struct iio_device *dev)
199{
200 bool found;
201 unsigned int i;
202
203 /* Reorder channels by index */
204 do {
205 found = false;
206 for (i = 1; i < dev->nb_channels; i++) {
207 struct iio_channel **channels = dev->channels;
208 long ch1 = channels[i - 1]->index;
209 long ch2 = channels[i]->index;
210
Lars-Peter Clausen75baa4d2016-02-01 17:58:57 +0100211 if (ch1 == ch2 && ch1 >= 0) {
212 ch1 = channels[i - 1]->format.shift;
213 ch2 = channels[i]->format.shift;
214 }
215
Paul Cercueil1f7bc152014-03-14 13:48:43 +0100216 if (ch2 >= 0 && ((ch1 > ch2) || ch1 < 0)) {
217 struct iio_channel *bak = channels[i];
218 channels[i] = channels[i - 1];
219 channels[i - 1] = bak;
220 found = true;
221 }
222 }
223 } while (found);
224}
225
Paul Cercueilfd387472015-08-05 10:34:19 +0200226int iio_context_init(struct iio_context *ctx)
Paul Cercueilae88fde2014-03-12 11:47:10 +0100227{
228 unsigned int i;
Paul Cercueilfd387472015-08-05 10:34:19 +0200229
Paul Cercueila7e80022014-06-11 11:41:26 +0200230 for (i = 0; i < ctx->nb_devices; i++)
231 reorder_channels(ctx->devices[i]);
Paul Cercueilfd387472015-08-05 10:34:19 +0200232
233 if (!ctx->xml) {
234 ctx->xml = iio_context_create_xml(ctx);
235 if (!ctx->xml)
236 return -ENOMEM;
237 }
238
239 return 0;
Paul Cercueilae88fde2014-03-12 11:47:10 +0100240}
Paul Cercueile45f8762014-05-02 11:19:26 +0200241
242int iio_context_get_version(const struct iio_context *ctx,
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200243 unsigned int *major, unsigned int *minor, char git_tag[8])
Paul Cercueile45f8762014-05-02 11:19:26 +0200244{
245 if (ctx->ops->get_version)
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200246 return ctx->ops->get_version(ctx, major, minor, git_tag);
Paul Cercueil53ed9432014-11-20 13:46:04 +0100247
248 iio_library_get_version(major, minor, git_tag);
Paul Cercueile45f8762014-05-02 11:19:26 +0200249 return 0;
250}
Paul Cercueil4ca73542014-06-10 16:23:29 +0200251
252int iio_context_set_timeout(struct iio_context *ctx, unsigned int timeout)
253{
254 if (ctx->ops->set_timeout)
255 return ctx->ops->set_timeout(ctx, timeout);
256 else
257 return -ENOSYS;
258}
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100259
260struct iio_context * iio_context_clone(const struct iio_context *ctx)
261{
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100262 if (ctx->ops->clone) {
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100263 return ctx->ops->clone(ctx);
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100264 } else {
265 errno = ENOSYS;
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100266 return NULL;
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100267 }
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100268}
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100269
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100270struct iio_context * iio_create_context_from_uri(const char *uri)
271{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200272#ifdef WITH_LOCAL_BACKEND
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100273 if (strcmp(uri, "local:") == 0) /* No address part */
274 return iio_create_local_context();
Paul Cercueilc399f512016-04-26 17:55:27 +0200275#endif
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100276
Paul Cercueil2814ed12016-08-25 17:08:18 +0200277#ifdef WITH_XML_BACKEND
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100278 if (strncmp(uri, "xml:", sizeof("xml:") - 1) == 0)
279 return iio_create_xml_context(uri + sizeof("xml:") - 1);
Paul Cercueilc399f512016-04-26 17:55:27 +0200280#endif
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100281
Paul Cercueil2814ed12016-08-25 17:08:18 +0200282#ifdef WITH_NETWORK_BACKEND
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100283 if (strncmp(uri, "ip:", sizeof("ip:") - 1) == 0)
284 return iio_create_network_context(uri+3);
Paul Cercueilc399f512016-04-26 17:55:27 +0200285#endif
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100286
Paul Cercueil2814ed12016-08-25 17:08:18 +0200287#ifdef WITH_USB_BACKEND
Paul Cercueilc399f512016-04-26 17:55:27 +0200288 if (strncmp(uri, "usb:", sizeof("usb:") - 1) == 0)
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100289 return usb_create_context_from_uri(uri);
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100290#endif
291
Paul Cercueil2814ed12016-08-25 17:08:18 +0200292#ifdef WITH_SERIAL_BACKEND
Paul Cercueilbcb04522016-03-22 17:03:29 +0100293 if (strncmp(uri, "serial:", sizeof("serial:") - 1) == 0)
294 return serial_create_context_from_uri(uri);
295#endif
296
Paul Cercueil10416462016-04-26 16:09:06 +0200297 errno = ENOSYS;
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100298 return NULL;
299}
300
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100301struct iio_context * iio_create_default_context(void)
302{
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100303 char *hostname = getenv("IIOD_REMOTE");
304
305 if (hostname) {
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100306 struct iio_context *ctx;
307
308 ctx = iio_create_context_from_uri(hostname);
309 if (ctx)
310 return ctx;
311
Paul Cercueil2814ed12016-08-25 17:08:18 +0200312#ifdef WITH_NETWORK_BACKEND
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100313 /* If the environment variable is an empty string, we will
314 * discover the server using ZeroConf */
315 if (strlen(hostname) == 0)
316 hostname = NULL;
317
318 return iio_create_network_context(hostname);
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100319#endif
Paul Cercueil3cce9652015-12-02 13:11:47 +0100320 }
321
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100322 return iio_create_local_context();
Paul Cercueil63e52182014-12-11 12:52:48 +0100323}
324
325struct iio_context * iio_create_local_context(void)
326{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200327#ifdef WITH_LOCAL_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100328 return local_create_context();
329#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100330 errno = ENOSYS;
Paul Cercueil63e52182014-12-11 12:52:48 +0100331 return NULL;
332#endif
333}
334
335struct iio_context * iio_create_network_context(const char *hostname)
336{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200337#ifdef WITH_NETWORK_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100338 return network_create_context(hostname);
339#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100340 errno = ENOSYS;
Paul Cercueil63e52182014-12-11 12:52:48 +0100341 return NULL;
342#endif
343}
344
345struct iio_context * iio_create_xml_context_mem(const char *xml, size_t len)
346{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200347#ifdef WITH_XML_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100348 return xml_create_context_mem(xml, len);
349#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100350 errno = ENOSYS;
Paul Cercueil63e52182014-12-11 12:52:48 +0100351 return NULL;
352#endif
353}
354
355struct iio_context * iio_create_xml_context(const char *xml_file)
356{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200357#ifdef WITH_XML_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100358 return xml_create_context(xml_file);
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100359#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100360 errno = ENOSYS;
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100361 return NULL;
362#endif
363}
Paul Cercueil60b4d1b2016-11-15 15:55:41 +0100364
365unsigned int iio_context_get_attrs_count(const struct iio_context *ctx)
366{
367 return ctx->nb_attrs;
368}
369
370int iio_context_get_attr(const struct iio_context *ctx, unsigned int index,
371 const char **name, const char **value)
372{
373 if (index >= ctx->nb_attrs)
374 return -EINVAL;
375
376 if (name)
377 *name = ctx->attrs[index];
378 if (value)
379 *value = ctx->values[index];
380 return 0;
381}
382
383const char * iio_context_get_attr_value(
384 const struct iio_context *ctx, const char *name)
385{
386 unsigned int i;
387
388 for (i = 0; i < ctx->nb_attrs; i++) {
389 if (!strcmp(name, ctx->attrs[i]))
390 return ctx->values[i];
391 }
392
393 return NULL;
394}
Paul Cercueila09970b2016-11-24 17:30:07 +0100395
396int iio_context_add_attr(struct iio_context *ctx,
397 const char *key, const char *value)
398{
399 char **attrs, **values, *new_key, *new_val;
400
401 attrs = realloc(ctx->attrs,
402 (ctx->nb_attrs + 1) * sizeof(*ctx->attrs));
403 if (!attrs)
404 return -ENOMEM;
405
406 ctx->attrs = attrs;
407
408 values = realloc(ctx->values,
409 (ctx->nb_attrs + 1) * sizeof(*ctx->values));
410 if (!values)
411 return -ENOMEM;
412
413 ctx->values = values;
414
415 new_key = iio_strdup(key);
416 if (!new_key)
417 return -ENOMEM;
418
419 new_val = iio_strdup(value);
420 if (!new_val) {
421 free(new_key);
422 return -ENOMEM;
423 }
424
425 ctx->attrs[ctx->nb_attrs] = new_key;
426 ctx->values[ctx->nb_attrs] = new_val;
427 ctx->nb_attrs++;
428 return 0;
429}