blob: b124801c0e4febcd6ca934878eab78e3cfd210a3 [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>"
Matt Fornero81f04a52017-11-30 14:36:37 -050035"<!ELEMENT device (channel | attribute | debug-attribute | buffer-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>"
Matt Fornero81f04a52017-11-30 14:36:37 -050040"<!ELEMENT buffer-attribute EMPTY>"
Paul Cercueil87350372015-03-16 13:24:37 +010041"<!ATTLIST context name CDATA #REQUIRED description CDATA #IMPLIED>"
Paul Cercueilc39540a2016-11-15 16:55:28 +010042"<!ATTLIST context-attribute name CDATA #REQUIRED value CDATA #REQUIRED>"
Paul Cercueil42090d12014-02-24 12:32:23 +010043"<!ATTLIST device id CDATA #REQUIRED name CDATA #IMPLIED>"
Paul Cercueila7e80022014-06-11 11:41:26 +020044"<!ATTLIST channel id CDATA #REQUIRED type (input|output) #REQUIRED name CDATA #IMPLIED>"
45"<!ATTLIST scan-element index CDATA #REQUIRED format CDATA #REQUIRED scale CDATA #IMPLIED>"
Paul Cercueil42d12352014-05-05 16:11:58 +020046"<!ATTLIST attribute name CDATA #REQUIRED filename CDATA #IMPLIED>"
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020047"<!ATTLIST debug-attribute name CDATA #REQUIRED>"
Matt Fornero81f04a52017-11-30 14:36:37 -050048"<!ATTLIST buffer-attribute name CDATA #REQUIRED>"
Paul Cercueil42090d12014-02-24 12:32:23 +010049"]>";
50
51/* Returns a string containing the XML representation of this context */
Paul Cercueilc1ed8482014-06-11 16:29:43 +020052char * iio_context_create_xml(const struct iio_context *ctx)
Paul Cercueil42090d12014-02-24 12:32:23 +010053{
Paul Cercueilc39540a2016-11-15 16:55:28 +010054 size_t len, *devices_len = NULL;
55 char *str, *ptr, **devices = NULL;
Paul Cercueil42090d12014-02-24 12:32:23 +010056 unsigned int i;
57
Paul Cercueil87350372015-03-16 13:24:37 +010058 len = strlen(ctx->name) + sizeof(xml_header) - 1 +
59 sizeof("<context name=\"\" ></context>");
60 if (ctx->description)
61 len += strlen(ctx->description) +
62 sizeof(" description=\"\"") - 1;
63
Paul Cercueilc39540a2016-11-15 16:55:28 +010064 for (i = 0; i < ctx->nb_attrs; i++)
65 len += strlen(ctx->attrs[i]) +
66 strlen(ctx->values[i]) +
67 sizeof("<context-attribute name=\"\" value=\"\" />");
68
69 if (ctx->nb_devices) {
Paul Cercueil002ba6e2017-02-02 11:43:52 +010070 devices_len = malloc(ctx->nb_devices * sizeof(*devices_len));
Paul Cercueilc39540a2016-11-15 16:55:28 +010071 if (!devices_len) {
Paul Cercueildfeca0d2015-03-16 17:18:00 +010072 errno = ENOMEM;
Paul Cercueil87350372015-03-16 13:24:37 +010073 return NULL;
Paul Cercueildfeca0d2015-03-16 17:18:00 +010074 }
Paul Cercueil87350372015-03-16 13:24:37 +010075
Paul Cercueil002ba6e2017-02-02 11:43:52 +010076 devices = calloc(ctx->nb_devices, sizeof(*devices));
Paul Cercueilc39540a2016-11-15 16:55:28 +010077 if (!devices)
78 goto err_free_devices_len;
Paul Cercueil9aab0192014-04-07 11:04:30 +020079
Paul Cercueilc39540a2016-11-15 16:55:28 +010080 for (i = 0; i < ctx->nb_devices; i++) {
81 char *xml = iio_device_get_xml(ctx->devices[i],
82 &devices_len[i]);
83 if (!xml)
84 goto err_free_devices;
85 devices[i] = xml;
86 len += devices_len[i];
87 }
Paul Cercueil42090d12014-02-24 12:32:23 +010088 }
89
90 str = malloc(len);
Paul Cercueildfeca0d2015-03-16 17:18:00 +010091 if (!str) {
92 errno = ENOMEM;
Paul Cercueil42090d12014-02-24 12:32:23 +010093 goto err_free_devices;
Paul Cercueildfeca0d2015-03-16 17:18:00 +010094 }
Paul Cercueil42090d12014-02-24 12:32:23 +010095
Paul Cercueil9c9a5562017-01-24 10:48:31 +010096 if (ctx->description) {
97 iio_snprintf(str, len, "%s<context name=\"%s\" "
Paul Cercueil87350372015-03-16 13:24:37 +010098 "description=\"%s\" >",
99 xml_header, ctx->name, ctx->description);
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100100 } else {
101 iio_snprintf(str, len, "%s<context name=\"%s\" >",
Paul Cercueil87350372015-03-16 13:24:37 +0100102 xml_header, ctx->name);
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100103 }
104
Paul Cercueil42090d12014-02-24 12:32:23 +0100105 ptr = strrchr(str, '\0');
106
Paul Cercueilc39540a2016-11-15 16:55:28 +0100107 for (i = 0; i < ctx->nb_attrs; i++)
108 ptr += sprintf(ptr, "<context-attribute name=\"%s\" value=\"%s\" />",
109 ctx->attrs[i], ctx->values[i]);
110
111
Paul Cercueil42090d12014-02-24 12:32:23 +0100112 for (i = 0; i < ctx->nb_devices; i++) {
113 strcpy(ptr, devices[i]);
114 ptr += devices_len[i];
115 free(devices[i]);
116 }
117
Paul Cercueil5822ab62014-04-04 13:29:17 +0200118 free(devices);
119 free(devices_len);
Paul Cercueil42090d12014-02-24 12:32:23 +0100120 strcpy(ptr, "</context>");
121 return str;
122
123err_free_devices:
Paul Cercueil8e80a192017-02-01 12:11:33 +0100124 for (i = 0; i < ctx->nb_devices; i++)
Paul Cercueil42090d12014-02-24 12:32:23 +0100125 free(devices[i]);
Paul Cercueil5822ab62014-04-04 13:29:17 +0200126 free(devices);
127err_free_devices_len:
128 free(devices_len);
Paul Cercueil42090d12014-02-24 12:32:23 +0100129 return NULL;
130}
131
Paul Cercueil4c6729d2014-04-04 17:24:41 +0200132const char * iio_context_get_xml(const struct iio_context *ctx)
133{
134 return ctx->xml;
135}
136
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100137const char * iio_context_get_name(const struct iio_context *ctx)
138{
139 return ctx->name;
140}
141
Paul Cercueil3ac36c12015-01-08 14:44:00 +0100142const char * iio_context_get_description(const struct iio_context *ctx)
143{
144 if (ctx->description)
145 return ctx->description;
146 else
147 return "";
148}
149
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100150void iio_context_destroy(struct iio_context *ctx)
151{
Paul Cercueil50363542014-02-18 16:04:28 +0100152 unsigned int i;
153 if (ctx->ops->shutdown)
154 ctx->ops->shutdown(ctx);
155
Paul Cercueil60b4d1b2016-11-15 15:55:41 +0100156 for (i = 0; i < ctx->nb_attrs; i++) {
157 free(ctx->attrs[i]);
158 free(ctx->values[i]);
159 }
160 if (ctx->nb_attrs) {
161 free(ctx->attrs);
162 free(ctx->values);
163 }
Paul Cercueil50363542014-02-18 16:04:28 +0100164 for (i = 0; i < ctx->nb_devices; i++)
165 free_device(ctx->devices[i]);
166 if (ctx->nb_devices)
167 free(ctx->devices);
Paul Cercueil4c6729d2014-04-04 17:24:41 +0200168 if (ctx->xml)
169 free(ctx->xml);
Paul Cercueil3ac36c12015-01-08 14:44:00 +0100170 if (ctx->description)
171 free(ctx->description);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100172 free(ctx);
173}
174
175unsigned int iio_context_get_devices_count(const struct iio_context *ctx)
176{
177 return ctx->nb_devices;
178}
179
180struct iio_device * iio_context_get_device(const struct iio_context *ctx,
181 unsigned int index)
182{
183 if (index >= ctx->nb_devices)
184 return NULL;
185 else
186 return ctx->devices[index];
187}
Paul Cercueilae88fde2014-03-12 11:47:10 +0100188
Paul Cercueil17512b02014-03-28 11:15:24 +0100189struct iio_device * iio_context_find_device(const struct iio_context *ctx,
190 const char *name)
191{
192 unsigned int i;
193 for (i = 0; i < ctx->nb_devices; i++) {
194 struct iio_device *dev = ctx->devices[i];
195 if (!strcmp(dev->id, name) ||
196 (dev->name && !strcmp(dev->name, name)))
197 return dev;
198 }
199 return NULL;
200}
201
Paul Cercueil1f7bc152014-03-14 13:48:43 +0100202static void reorder_channels(struct iio_device *dev)
203{
204 bool found;
205 unsigned int i;
206
207 /* Reorder channels by index */
208 do {
209 found = false;
210 for (i = 1; i < dev->nb_channels; i++) {
211 struct iio_channel **channels = dev->channels;
212 long ch1 = channels[i - 1]->index;
213 long ch2 = channels[i]->index;
214
Lars-Peter Clausen75baa4d2016-02-01 17:58:57 +0100215 if (ch1 == ch2 && ch1 >= 0) {
216 ch1 = channels[i - 1]->format.shift;
217 ch2 = channels[i]->format.shift;
218 }
219
Paul Cercueil1f7bc152014-03-14 13:48:43 +0100220 if (ch2 >= 0 && ((ch1 > ch2) || ch1 < 0)) {
221 struct iio_channel *bak = channels[i];
222 channels[i] = channels[i - 1];
223 channels[i - 1] = bak;
224 found = true;
225 }
226 }
227 } while (found);
Paul Cercueil5e3b9cd2017-04-11 17:31:46 +0200228
229 for (i = 0; i < dev->nb_channels; i++)
230 dev->channels[i]->number = i;
Paul Cercueil1f7bc152014-03-14 13:48:43 +0100231}
232
Paul Cercueilfd387472015-08-05 10:34:19 +0200233int iio_context_init(struct iio_context *ctx)
Paul Cercueilae88fde2014-03-12 11:47:10 +0100234{
235 unsigned int i;
Paul Cercueilfd387472015-08-05 10:34:19 +0200236
Paul Cercueila7e80022014-06-11 11:41:26 +0200237 for (i = 0; i < ctx->nb_devices; i++)
238 reorder_channels(ctx->devices[i]);
Paul Cercueilfd387472015-08-05 10:34:19 +0200239
240 if (!ctx->xml) {
241 ctx->xml = iio_context_create_xml(ctx);
242 if (!ctx->xml)
243 return -ENOMEM;
244 }
245
246 return 0;
Paul Cercueilae88fde2014-03-12 11:47:10 +0100247}
Paul Cercueile45f8762014-05-02 11:19:26 +0200248
249int iio_context_get_version(const struct iio_context *ctx,
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200250 unsigned int *major, unsigned int *minor, char git_tag[8])
Paul Cercueile45f8762014-05-02 11:19:26 +0200251{
252 if (ctx->ops->get_version)
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200253 return ctx->ops->get_version(ctx, major, minor, git_tag);
Paul Cercueil53ed9432014-11-20 13:46:04 +0100254
255 iio_library_get_version(major, minor, git_tag);
Paul Cercueile45f8762014-05-02 11:19:26 +0200256 return 0;
257}
Paul Cercueil4ca73542014-06-10 16:23:29 +0200258
259int iio_context_set_timeout(struct iio_context *ctx, unsigned int timeout)
260{
261 if (ctx->ops->set_timeout)
262 return ctx->ops->set_timeout(ctx, timeout);
263 else
264 return -ENOSYS;
265}
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100266
267struct iio_context * iio_context_clone(const struct iio_context *ctx)
268{
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100269 if (ctx->ops->clone) {
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100270 return ctx->ops->clone(ctx);
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100271 } else {
272 errno = ENOSYS;
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100273 return NULL;
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100274 }
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100275}
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100276
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100277struct iio_context * iio_create_context_from_uri(const char *uri)
278{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200279#ifdef WITH_LOCAL_BACKEND
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100280 if (strcmp(uri, "local:") == 0) /* No address part */
281 return iio_create_local_context();
Paul Cercueilc399f512016-04-26 17:55:27 +0200282#endif
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100283
Paul Cercueil2814ed12016-08-25 17:08:18 +0200284#ifdef WITH_XML_BACKEND
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100285 if (strncmp(uri, "xml:", sizeof("xml:") - 1) == 0)
286 return iio_create_xml_context(uri + sizeof("xml:") - 1);
Paul Cercueilc399f512016-04-26 17:55:27 +0200287#endif
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100288
Paul Cercueil2814ed12016-08-25 17:08:18 +0200289#ifdef WITH_NETWORK_BACKEND
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100290 if (strncmp(uri, "ip:", sizeof("ip:") - 1) == 0)
291 return iio_create_network_context(uri+3);
Paul Cercueilc399f512016-04-26 17:55:27 +0200292#endif
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100293
Paul Cercueil2814ed12016-08-25 17:08:18 +0200294#ifdef WITH_USB_BACKEND
Paul Cercueilc399f512016-04-26 17:55:27 +0200295 if (strncmp(uri, "usb:", sizeof("usb:") - 1) == 0)
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100296 return usb_create_context_from_uri(uri);
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100297#endif
298
Paul Cercueil2814ed12016-08-25 17:08:18 +0200299#ifdef WITH_SERIAL_BACKEND
Paul Cercueilbcb04522016-03-22 17:03:29 +0100300 if (strncmp(uri, "serial:", sizeof("serial:") - 1) == 0)
301 return serial_create_context_from_uri(uri);
302#endif
303
Paul Cercueil10416462016-04-26 16:09:06 +0200304 errno = ENOSYS;
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100305 return NULL;
306}
307
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100308struct iio_context * iio_create_default_context(void)
309{
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100310 char *hostname = getenv("IIOD_REMOTE");
311
312 if (hostname) {
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100313 struct iio_context *ctx;
314
315 ctx = iio_create_context_from_uri(hostname);
316 if (ctx)
317 return ctx;
318
Paul Cercueil2814ed12016-08-25 17:08:18 +0200319#ifdef WITH_NETWORK_BACKEND
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100320 /* If the environment variable is an empty string, we will
321 * discover the server using ZeroConf */
322 if (strlen(hostname) == 0)
323 hostname = NULL;
324
325 return iio_create_network_context(hostname);
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100326#endif
Paul Cercueil3cce9652015-12-02 13:11:47 +0100327 }
328
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100329 return iio_create_local_context();
Paul Cercueil63e52182014-12-11 12:52:48 +0100330}
331
332struct iio_context * iio_create_local_context(void)
333{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200334#ifdef WITH_LOCAL_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100335 return local_create_context();
336#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100337 errno = ENOSYS;
Paul Cercueil63e52182014-12-11 12:52:48 +0100338 return NULL;
339#endif
340}
341
342struct iio_context * iio_create_network_context(const char *hostname)
343{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200344#ifdef WITH_NETWORK_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100345 return network_create_context(hostname);
346#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100347 errno = ENOSYS;
Paul Cercueil63e52182014-12-11 12:52:48 +0100348 return NULL;
349#endif
350}
351
352struct iio_context * iio_create_xml_context_mem(const char *xml, size_t len)
353{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200354#ifdef WITH_XML_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100355 return xml_create_context_mem(xml, len);
356#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100357 errno = ENOSYS;
Paul Cercueil63e52182014-12-11 12:52:48 +0100358 return NULL;
359#endif
360}
361
362struct iio_context * iio_create_xml_context(const char *xml_file)
363{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200364#ifdef WITH_XML_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100365 return xml_create_context(xml_file);
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100366#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100367 errno = ENOSYS;
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100368 return NULL;
369#endif
370}
Paul Cercueil60b4d1b2016-11-15 15:55:41 +0100371
372unsigned int iio_context_get_attrs_count(const struct iio_context *ctx)
373{
374 return ctx->nb_attrs;
375}
376
377int iio_context_get_attr(const struct iio_context *ctx, unsigned int index,
378 const char **name, const char **value)
379{
380 if (index >= ctx->nb_attrs)
381 return -EINVAL;
382
383 if (name)
384 *name = ctx->attrs[index];
385 if (value)
386 *value = ctx->values[index];
387 return 0;
388}
389
390const char * iio_context_get_attr_value(
391 const struct iio_context *ctx, const char *name)
392{
393 unsigned int i;
394
395 for (i = 0; i < ctx->nb_attrs; i++) {
396 if (!strcmp(name, ctx->attrs[i]))
397 return ctx->values[i];
398 }
399
400 return NULL;
401}
Paul Cercueila09970b2016-11-24 17:30:07 +0100402
403int iio_context_add_attr(struct iio_context *ctx,
404 const char *key, const char *value)
405{
406 char **attrs, **values, *new_key, *new_val;
407
408 attrs = realloc(ctx->attrs,
409 (ctx->nb_attrs + 1) * sizeof(*ctx->attrs));
410 if (!attrs)
411 return -ENOMEM;
412
413 ctx->attrs = attrs;
414
415 values = realloc(ctx->values,
416 (ctx->nb_attrs + 1) * sizeof(*ctx->values));
417 if (!values)
418 return -ENOMEM;
419
420 ctx->values = values;
421
422 new_key = iio_strdup(key);
423 if (!new_key)
424 return -ENOMEM;
425
426 new_val = iio_strdup(value);
427 if (!new_val) {
428 free(new_key);
429 return -ENOMEM;
430 }
431
432 ctx->attrs[ctx->nb_attrs] = new_key;
433 ctx->values[ctx->nb_attrs] = new_val;
434 ctx->nb_attrs++;
435 return 0;
436}