blob: 2b5473094e12fda36c10493bfef728680daf3003 [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"
Robin Getz57295ce2018-05-24 21:45:08 +000022#include "sort.h"
Paul Cercueil0b2ce712014-02-17 15:04:18 +010023
Paul Cercueil4c6729d2014-04-04 17:24:41 +020024#include <errno.h>
Paul Cercueil42090d12014-02-24 12:32:23 +010025#include <string.h>
26
Paul Cercueil8f56ea72014-10-28 15:18:18 +010027#ifdef _WIN32
28#define LOCAL_BACKEND 0
29#define NETWORK_BACKEND 1
30#endif
31
Paul Cercueil42090d12014-02-24 12:32:23 +010032static const char xml_header[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
33"<!DOCTYPE context ["
Paul Cercueilc39540a2016-11-15 16:55:28 +010034"<!ELEMENT context (device | context-attribute)*>"
35"<!ELEMENT context-attribute EMPTY>"
Matt Fornero81f04a52017-11-30 14:36:37 -050036"<!ELEMENT device (channel | attribute | debug-attribute | buffer-attribute)*>"
Paul Cercueila7e80022014-06-11 11:41:26 +020037"<!ELEMENT channel (scan-element?, attribute*)>"
Paul Cercueil42090d12014-02-24 12:32:23 +010038"<!ELEMENT attribute EMPTY>"
Paul Cercueila7e80022014-06-11 11:41:26 +020039"<!ELEMENT scan-element EMPTY>"
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020040"<!ELEMENT debug-attribute EMPTY>"
Matt Fornero81f04a52017-11-30 14:36:37 -050041"<!ELEMENT buffer-attribute EMPTY>"
Paul Cercueil87350372015-03-16 13:24:37 +010042"<!ATTLIST context name CDATA #REQUIRED description CDATA #IMPLIED>"
Paul Cercueilc39540a2016-11-15 16:55:28 +010043"<!ATTLIST context-attribute name CDATA #REQUIRED value CDATA #REQUIRED>"
Paul Cercueil42090d12014-02-24 12:32:23 +010044"<!ATTLIST device id CDATA #REQUIRED name CDATA #IMPLIED>"
Paul Cercueila7e80022014-06-11 11:41:26 +020045"<!ATTLIST channel id CDATA #REQUIRED type (input|output) #REQUIRED name CDATA #IMPLIED>"
46"<!ATTLIST scan-element index CDATA #REQUIRED format CDATA #REQUIRED scale CDATA #IMPLIED>"
Paul Cercueil42d12352014-05-05 16:11:58 +020047"<!ATTLIST attribute name CDATA #REQUIRED filename CDATA #IMPLIED>"
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020048"<!ATTLIST debug-attribute name CDATA #REQUIRED>"
Matt Fornero81f04a52017-11-30 14:36:37 -050049"<!ATTLIST buffer-attribute name CDATA #REQUIRED>"
Paul Cercueil42090d12014-02-24 12:32:23 +010050"]>";
51
52/* Returns a string containing the XML representation of this context */
Paul Cercueilc1ed8482014-06-11 16:29:43 +020053char * iio_context_create_xml(const struct iio_context *ctx)
Paul Cercueil42090d12014-02-24 12:32:23 +010054{
Paul Cercueilc39540a2016-11-15 16:55:28 +010055 size_t len, *devices_len = NULL;
56 char *str, *ptr, **devices = NULL;
Paul Cercueil42090d12014-02-24 12:32:23 +010057 unsigned int i;
58
Paul Cercueil87350372015-03-16 13:24:37 +010059 len = strlen(ctx->name) + sizeof(xml_header) - 1 +
60 sizeof("<context name=\"\" ></context>");
61 if (ctx->description)
62 len += strlen(ctx->description) +
63 sizeof(" description=\"\"") - 1;
64
Paul Cercueilc39540a2016-11-15 16:55:28 +010065 for (i = 0; i < ctx->nb_attrs; i++)
66 len += strlen(ctx->attrs[i]) +
67 strlen(ctx->values[i]) +
68 sizeof("<context-attribute name=\"\" value=\"\" />");
69
70 if (ctx->nb_devices) {
Paul Cercueil002ba6e2017-02-02 11:43:52 +010071 devices_len = malloc(ctx->nb_devices * sizeof(*devices_len));
Paul Cercueilc39540a2016-11-15 16:55:28 +010072 if (!devices_len) {
Paul Cercueildfeca0d2015-03-16 17:18:00 +010073 errno = ENOMEM;
Paul Cercueil87350372015-03-16 13:24:37 +010074 return NULL;
Paul Cercueildfeca0d2015-03-16 17:18:00 +010075 }
Paul Cercueil87350372015-03-16 13:24:37 +010076
Paul Cercueil002ba6e2017-02-02 11:43:52 +010077 devices = calloc(ctx->nb_devices, sizeof(*devices));
Paul Cercueilc39540a2016-11-15 16:55:28 +010078 if (!devices)
79 goto err_free_devices_len;
Paul Cercueil9aab0192014-04-07 11:04:30 +020080
Paul Cercueilc39540a2016-11-15 16:55:28 +010081 for (i = 0; i < ctx->nb_devices; i++) {
82 char *xml = iio_device_get_xml(ctx->devices[i],
83 &devices_len[i]);
84 if (!xml)
85 goto err_free_devices;
86 devices[i] = xml;
87 len += devices_len[i];
88 }
Paul Cercueil42090d12014-02-24 12:32:23 +010089 }
90
91 str = malloc(len);
Paul Cercueildfeca0d2015-03-16 17:18:00 +010092 if (!str) {
93 errno = ENOMEM;
Paul Cercueil42090d12014-02-24 12:32:23 +010094 goto err_free_devices;
Paul Cercueildfeca0d2015-03-16 17:18:00 +010095 }
Paul Cercueil42090d12014-02-24 12:32:23 +010096
Paul Cercueil9c9a5562017-01-24 10:48:31 +010097 if (ctx->description) {
98 iio_snprintf(str, len, "%s<context name=\"%s\" "
Paul Cercueil87350372015-03-16 13:24:37 +010099 "description=\"%s\" >",
100 xml_header, ctx->name, ctx->description);
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100101 } else {
102 iio_snprintf(str, len, "%s<context name=\"%s\" >",
Paul Cercueil87350372015-03-16 13:24:37 +0100103 xml_header, ctx->name);
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100104 }
105
Paul Cercueil42090d12014-02-24 12:32:23 +0100106 ptr = strrchr(str, '\0');
107
Paul Cercueilc39540a2016-11-15 16:55:28 +0100108 for (i = 0; i < ctx->nb_attrs; i++)
109 ptr += sprintf(ptr, "<context-attribute name=\"%s\" value=\"%s\" />",
110 ctx->attrs[i], ctx->values[i]);
111
112
Paul Cercueil42090d12014-02-24 12:32:23 +0100113 for (i = 0; i < ctx->nb_devices; i++) {
114 strcpy(ptr, devices[i]);
115 ptr += devices_len[i];
116 free(devices[i]);
117 }
118
Paul Cercueil5822ab62014-04-04 13:29:17 +0200119 free(devices);
120 free(devices_len);
Paul Cercueil42090d12014-02-24 12:32:23 +0100121 strcpy(ptr, "</context>");
122 return str;
123
124err_free_devices:
Paul Cercueil8e80a192017-02-01 12:11:33 +0100125 for (i = 0; i < ctx->nb_devices; i++)
Paul Cercueil42090d12014-02-24 12:32:23 +0100126 free(devices[i]);
Paul Cercueil5822ab62014-04-04 13:29:17 +0200127 free(devices);
128err_free_devices_len:
129 free(devices_len);
Paul Cercueil42090d12014-02-24 12:32:23 +0100130 return NULL;
131}
132
Paul Cercueil4c6729d2014-04-04 17:24:41 +0200133const char * iio_context_get_xml(const struct iio_context *ctx)
134{
135 return ctx->xml;
136}
137
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100138const char * iio_context_get_name(const struct iio_context *ctx)
139{
140 return ctx->name;
141}
142
Paul Cercueil3ac36c12015-01-08 14:44:00 +0100143const char * iio_context_get_description(const struct iio_context *ctx)
144{
145 if (ctx->description)
146 return ctx->description;
147 else
148 return "";
149}
150
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100151void iio_context_destroy(struct iio_context *ctx)
152{
Paul Cercueil50363542014-02-18 16:04:28 +0100153 unsigned int i;
154 if (ctx->ops->shutdown)
155 ctx->ops->shutdown(ctx);
156
Paul Cercueil60b4d1b2016-11-15 15:55:41 +0100157 for (i = 0; i < ctx->nb_attrs; i++) {
158 free(ctx->attrs[i]);
159 free(ctx->values[i]);
160 }
161 if (ctx->nb_attrs) {
162 free(ctx->attrs);
163 free(ctx->values);
164 }
Paul Cercueil50363542014-02-18 16:04:28 +0100165 for (i = 0; i < ctx->nb_devices; i++)
166 free_device(ctx->devices[i]);
167 if (ctx->nb_devices)
168 free(ctx->devices);
Paul Cercueil4c6729d2014-04-04 17:24:41 +0200169 if (ctx->xml)
170 free(ctx->xml);
Paul Cercueil3ac36c12015-01-08 14:44:00 +0100171 if (ctx->description)
172 free(ctx->description);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100173 free(ctx);
174}
175
176unsigned int iio_context_get_devices_count(const struct iio_context *ctx)
177{
178 return ctx->nb_devices;
179}
180
181struct iio_device * iio_context_get_device(const struct iio_context *ctx,
182 unsigned int index)
183{
184 if (index >= ctx->nb_devices)
185 return NULL;
186 else
187 return ctx->devices[index];
188}
Paul Cercueilae88fde2014-03-12 11:47:10 +0100189
Paul Cercueil17512b02014-03-28 11:15:24 +0100190struct iio_device * iio_context_find_device(const struct iio_context *ctx,
191 const char *name)
192{
193 unsigned int i;
194 for (i = 0; i < ctx->nb_devices; i++) {
195 struct iio_device *dev = ctx->devices[i];
196 if (!strcmp(dev->id, name) ||
197 (dev->name && !strcmp(dev->name, name)))
198 return dev;
199 }
200 return NULL;
201}
202
Paul Cercueil1f7bc152014-03-14 13:48:43 +0100203static void reorder_channels(struct iio_device *dev)
204{
Adrian Suciu8748f322019-04-18 18:30:52 +0300205 bool found;
Paul Cercueil1f7bc152014-03-14 13:48:43 +0100206 unsigned int i;
207
208 /* Reorder channels by index */
Adrian Suciu8748f322019-04-18 18:30:52 +0300209 do {
210 found = false;
211 for (i = 1; i < dev->nb_channels; i++) {
212 struct iio_channel **channels = dev->channels;
213 long ch1 = channels[i - 1]->index;
214 long ch2 = channels[i]->index;
215
216 if (ch1 == ch2 && ch1 >= 0) {
217 ch1 = channels[i - 1]->format.shift;
218 ch2 = channels[i]->format.shift;
219 }
220
221 if (ch2 >= 0 && ((ch1 > ch2) || ch1 < 0)) {
222 struct iio_channel *bak = channels[i];
223 channels[i] = channels[i - 1];
224 channels[i - 1] = bak;
225 found = true;
226 }
227 }
228 } while (found);
Paul Cercueil5e3b9cd2017-04-11 17:31:46 +0200229
Michael Hennerich7e70bcb2018-11-02 14:12:31 +0100230 for (i = 0; i < dev->nb_channels; i++)
231 dev->channels[i]->number = i;
Paul Cercueil1f7bc152014-03-14 13:48:43 +0100232}
233
Paul Cercueilfd387472015-08-05 10:34:19 +0200234int iio_context_init(struct iio_context *ctx)
Paul Cercueilae88fde2014-03-12 11:47:10 +0100235{
236 unsigned int i;
Paul Cercueilfd387472015-08-05 10:34:19 +0200237
Paul Cercueila7e80022014-06-11 11:41:26 +0200238 for (i = 0; i < ctx->nb_devices; i++)
239 reorder_channels(ctx->devices[i]);
Paul Cercueilfd387472015-08-05 10:34:19 +0200240
241 if (!ctx->xml) {
242 ctx->xml = iio_context_create_xml(ctx);
243 if (!ctx->xml)
244 return -ENOMEM;
245 }
246
247 return 0;
Paul Cercueilae88fde2014-03-12 11:47:10 +0100248}
Paul Cercueile45f8762014-05-02 11:19:26 +0200249
250int iio_context_get_version(const struct iio_context *ctx,
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200251 unsigned int *major, unsigned int *minor, char git_tag[8])
Paul Cercueile45f8762014-05-02 11:19:26 +0200252{
253 if (ctx->ops->get_version)
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200254 return ctx->ops->get_version(ctx, major, minor, git_tag);
Paul Cercueil53ed9432014-11-20 13:46:04 +0100255
256 iio_library_get_version(major, minor, git_tag);
Paul Cercueile45f8762014-05-02 11:19:26 +0200257 return 0;
258}
Paul Cercueil4ca73542014-06-10 16:23:29 +0200259
260int iio_context_set_timeout(struct iio_context *ctx, unsigned int timeout)
261{
262 if (ctx->ops->set_timeout)
263 return ctx->ops->set_timeout(ctx, timeout);
264 else
265 return -ENOSYS;
266}
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100267
268struct iio_context * iio_context_clone(const struct iio_context *ctx)
269{
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100270 if (ctx->ops->clone) {
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100271 return ctx->ops->clone(ctx);
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100272 } else {
273 errno = ENOSYS;
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100274 return NULL;
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100275 }
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100276}
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100277
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100278struct iio_context * iio_create_context_from_uri(const char *uri)
279{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200280#ifdef WITH_LOCAL_BACKEND
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100281 if (strcmp(uri, "local:") == 0) /* No address part */
282 return iio_create_local_context();
Paul Cercueilc399f512016-04-26 17:55:27 +0200283#endif
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100284
Paul Cercueil2814ed12016-08-25 17:08:18 +0200285#ifdef WITH_XML_BACKEND
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100286 if (strncmp(uri, "xml:", sizeof("xml:") - 1) == 0)
287 return iio_create_xml_context(uri + sizeof("xml:") - 1);
Paul Cercueilc399f512016-04-26 17:55:27 +0200288#endif
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100289
Paul Cercueil2814ed12016-08-25 17:08:18 +0200290#ifdef WITH_NETWORK_BACKEND
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100291 if (strncmp(uri, "ip:", sizeof("ip:") - 1) == 0)
292 return iio_create_network_context(uri+3);
Paul Cercueilc399f512016-04-26 17:55:27 +0200293#endif
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100294
Paul Cercueil2814ed12016-08-25 17:08:18 +0200295#ifdef WITH_USB_BACKEND
Paul Cercueilc399f512016-04-26 17:55:27 +0200296 if (strncmp(uri, "usb:", sizeof("usb:") - 1) == 0)
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100297 return usb_create_context_from_uri(uri);
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100298#endif
299
Paul Cercueil2814ed12016-08-25 17:08:18 +0200300#ifdef WITH_SERIAL_BACKEND
Paul Cercueilbcb04522016-03-22 17:03:29 +0100301 if (strncmp(uri, "serial:", sizeof("serial:") - 1) == 0)
302 return serial_create_context_from_uri(uri);
303#endif
304
Paul Cercueil10416462016-04-26 16:09:06 +0200305 errno = ENOSYS;
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100306 return NULL;
307}
308
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100309struct iio_context * iio_create_default_context(void)
310{
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100311 char *hostname = getenv("IIOD_REMOTE");
312
313 if (hostname) {
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100314 struct iio_context *ctx;
315
316 ctx = iio_create_context_from_uri(hostname);
317 if (ctx)
318 return ctx;
319
Paul Cercueil2814ed12016-08-25 17:08:18 +0200320#ifdef WITH_NETWORK_BACKEND
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100321 /* If the environment variable is an empty string, we will
322 * discover the server using ZeroConf */
323 if (strlen(hostname) == 0)
324 hostname = NULL;
325
326 return iio_create_network_context(hostname);
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100327#endif
Paul Cercueil3cce9652015-12-02 13:11:47 +0100328 }
329
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100330 return iio_create_local_context();
Paul Cercueil63e52182014-12-11 12:52:48 +0100331}
332
333struct iio_context * iio_create_local_context(void)
334{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200335#ifdef WITH_LOCAL_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100336 return local_create_context();
337#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100338 errno = ENOSYS;
Paul Cercueil63e52182014-12-11 12:52:48 +0100339 return NULL;
340#endif
341}
342
343struct iio_context * iio_create_network_context(const char *hostname)
344{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200345#ifdef WITH_NETWORK_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100346 return network_create_context(hostname);
347#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100348 errno = ENOSYS;
Paul Cercueil63e52182014-12-11 12:52:48 +0100349 return NULL;
350#endif
351}
352
353struct iio_context * iio_create_xml_context_mem(const char *xml, size_t len)
354{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200355#ifdef WITH_XML_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100356 return xml_create_context_mem(xml, len);
357#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100358 errno = ENOSYS;
Paul Cercueil63e52182014-12-11 12:52:48 +0100359 return NULL;
360#endif
361}
362
363struct iio_context * iio_create_xml_context(const char *xml_file)
364{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200365#ifdef WITH_XML_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100366 return xml_create_context(xml_file);
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100367#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100368 errno = ENOSYS;
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100369 return NULL;
370#endif
371}
Paul Cercueil60b4d1b2016-11-15 15:55:41 +0100372
373unsigned int iio_context_get_attrs_count(const struct iio_context *ctx)
374{
375 return ctx->nb_attrs;
376}
377
378int iio_context_get_attr(const struct iio_context *ctx, unsigned int index,
379 const char **name, const char **value)
380{
381 if (index >= ctx->nb_attrs)
382 return -EINVAL;
383
384 if (name)
385 *name = ctx->attrs[index];
386 if (value)
387 *value = ctx->values[index];
388 return 0;
389}
390
391const char * iio_context_get_attr_value(
392 const struct iio_context *ctx, const char *name)
393{
394 unsigned int i;
395
396 for (i = 0; i < ctx->nb_attrs; i++) {
397 if (!strcmp(name, ctx->attrs[i]))
398 return ctx->values[i];
399 }
400
401 return NULL;
402}
Paul Cercueila09970b2016-11-24 17:30:07 +0100403
404int iio_context_add_attr(struct iio_context *ctx,
405 const char *key, const char *value)
406{
407 char **attrs, **values, *new_key, *new_val;
408
409 attrs = realloc(ctx->attrs,
410 (ctx->nb_attrs + 1) * sizeof(*ctx->attrs));
411 if (!attrs)
412 return -ENOMEM;
413
414 ctx->attrs = attrs;
415
416 values = realloc(ctx->values,
417 (ctx->nb_attrs + 1) * sizeof(*ctx->values));
418 if (!values)
419 return -ENOMEM;
420
421 ctx->values = values;
422
423 new_key = iio_strdup(key);
424 if (!new_key)
425 return -ENOMEM;
426
427 new_val = iio_strdup(value);
428 if (!new_val) {
429 free(new_key);
430 return -ENOMEM;
431 }
432
433 ctx->attrs[ctx->nb_attrs] = new_key;
434 ctx->values[ctx->nb_attrs] = new_val;
435 ctx->nb_attrs++;
436 return 0;
437}