blob: c1feef0ff9e39f09fe8fb319cfe8609577b0784e [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{
Paul Cercueil1f7bc152014-03-14 13:48:43 +0100205 unsigned int i;
206
207 /* Reorder channels by index */
Robin Getz57295ce2018-05-24 21:45:08 +0000208 qsort(dev->channels, dev->nb_channels, sizeof(struct iio_channel *),
Robin Getzb8634362018-10-24 12:39:11 +0000209 iio_channel_compare);
Paul Cercueil5e3b9cd2017-04-11 17:31:46 +0200210
Michael Hennerich7e70bcb2018-11-02 14:12:31 +0100211 for (i = 0; i < dev->nb_channels; i++)
212 dev->channels[i]->number = i;
Paul Cercueil1f7bc152014-03-14 13:48:43 +0100213}
214
Paul Cercueilfd387472015-08-05 10:34:19 +0200215int iio_context_init(struct iio_context *ctx)
Paul Cercueilae88fde2014-03-12 11:47:10 +0100216{
217 unsigned int i;
Paul Cercueilfd387472015-08-05 10:34:19 +0200218
Paul Cercueila7e80022014-06-11 11:41:26 +0200219 for (i = 0; i < ctx->nb_devices; i++)
220 reorder_channels(ctx->devices[i]);
Paul Cercueilfd387472015-08-05 10:34:19 +0200221
222 if (!ctx->xml) {
223 ctx->xml = iio_context_create_xml(ctx);
224 if (!ctx->xml)
225 return -ENOMEM;
226 }
227
228 return 0;
Paul Cercueilae88fde2014-03-12 11:47:10 +0100229}
Paul Cercueile45f8762014-05-02 11:19:26 +0200230
231int iio_context_get_version(const struct iio_context *ctx,
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200232 unsigned int *major, unsigned int *minor, char git_tag[8])
Paul Cercueile45f8762014-05-02 11:19:26 +0200233{
234 if (ctx->ops->get_version)
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200235 return ctx->ops->get_version(ctx, major, minor, git_tag);
Paul Cercueil53ed9432014-11-20 13:46:04 +0100236
237 iio_library_get_version(major, minor, git_tag);
Paul Cercueile45f8762014-05-02 11:19:26 +0200238 return 0;
239}
Paul Cercueil4ca73542014-06-10 16:23:29 +0200240
241int iio_context_set_timeout(struct iio_context *ctx, unsigned int timeout)
242{
243 if (ctx->ops->set_timeout)
244 return ctx->ops->set_timeout(ctx, timeout);
245 else
246 return -ENOSYS;
247}
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100248
249struct iio_context * iio_context_clone(const struct iio_context *ctx)
250{
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100251 if (ctx->ops->clone) {
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100252 return ctx->ops->clone(ctx);
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100253 } else {
254 errno = ENOSYS;
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100255 return NULL;
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100256 }
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100257}
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100258
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100259struct iio_context * iio_create_context_from_uri(const char *uri)
260{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200261#ifdef WITH_LOCAL_BACKEND
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100262 if (strcmp(uri, "local:") == 0) /* No address part */
263 return iio_create_local_context();
Paul Cercueilc399f512016-04-26 17:55:27 +0200264#endif
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100265
Paul Cercueil2814ed12016-08-25 17:08:18 +0200266#ifdef WITH_XML_BACKEND
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100267 if (strncmp(uri, "xml:", sizeof("xml:") - 1) == 0)
268 return iio_create_xml_context(uri + sizeof("xml:") - 1);
Paul Cercueilc399f512016-04-26 17:55:27 +0200269#endif
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100270
Paul Cercueil2814ed12016-08-25 17:08:18 +0200271#ifdef WITH_NETWORK_BACKEND
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100272 if (strncmp(uri, "ip:", sizeof("ip:") - 1) == 0)
273 return iio_create_network_context(uri+3);
Paul Cercueilc399f512016-04-26 17:55:27 +0200274#endif
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100275
Paul Cercueil2814ed12016-08-25 17:08:18 +0200276#ifdef WITH_USB_BACKEND
Paul Cercueilc399f512016-04-26 17:55:27 +0200277 if (strncmp(uri, "usb:", sizeof("usb:") - 1) == 0)
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100278 return usb_create_context_from_uri(uri);
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100279#endif
280
Paul Cercueil2814ed12016-08-25 17:08:18 +0200281#ifdef WITH_SERIAL_BACKEND
Paul Cercueilbcb04522016-03-22 17:03:29 +0100282 if (strncmp(uri, "serial:", sizeof("serial:") - 1) == 0)
283 return serial_create_context_from_uri(uri);
284#endif
285
Paul Cercueil10416462016-04-26 16:09:06 +0200286 errno = ENOSYS;
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100287 return NULL;
288}
289
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100290struct iio_context * iio_create_default_context(void)
291{
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100292 char *hostname = getenv("IIOD_REMOTE");
293
294 if (hostname) {
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100295 struct iio_context *ctx;
296
297 ctx = iio_create_context_from_uri(hostname);
298 if (ctx)
299 return ctx;
300
Paul Cercueil2814ed12016-08-25 17:08:18 +0200301#ifdef WITH_NETWORK_BACKEND
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100302 /* If the environment variable is an empty string, we will
303 * discover the server using ZeroConf */
304 if (strlen(hostname) == 0)
305 hostname = NULL;
306
307 return iio_create_network_context(hostname);
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100308#endif
Paul Cercueil3cce9652015-12-02 13:11:47 +0100309 }
310
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100311 return iio_create_local_context();
Paul Cercueil63e52182014-12-11 12:52:48 +0100312}
313
314struct iio_context * iio_create_local_context(void)
315{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200316#ifdef WITH_LOCAL_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100317 return local_create_context();
318#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100319 errno = ENOSYS;
Paul Cercueil63e52182014-12-11 12:52:48 +0100320 return NULL;
321#endif
322}
323
324struct iio_context * iio_create_network_context(const char *hostname)
325{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200326#ifdef WITH_NETWORK_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100327 return network_create_context(hostname);
328#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100329 errno = ENOSYS;
Paul Cercueil63e52182014-12-11 12:52:48 +0100330 return NULL;
331#endif
332}
333
334struct iio_context * iio_create_xml_context_mem(const char *xml, size_t len)
335{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200336#ifdef WITH_XML_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100337 return xml_create_context_mem(xml, len);
338#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100339 errno = ENOSYS;
Paul Cercueil63e52182014-12-11 12:52:48 +0100340 return NULL;
341#endif
342}
343
344struct iio_context * iio_create_xml_context(const char *xml_file)
345{
Paul Cercueil2814ed12016-08-25 17:08:18 +0200346#ifdef WITH_XML_BACKEND
Paul Cercueil63e52182014-12-11 12:52:48 +0100347 return xml_create_context(xml_file);
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100348#else
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100349 errno = ENOSYS;
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100350 return NULL;
351#endif
352}
Paul Cercueil60b4d1b2016-11-15 15:55:41 +0100353
354unsigned int iio_context_get_attrs_count(const struct iio_context *ctx)
355{
356 return ctx->nb_attrs;
357}
358
359int iio_context_get_attr(const struct iio_context *ctx, unsigned int index,
360 const char **name, const char **value)
361{
362 if (index >= ctx->nb_attrs)
363 return -EINVAL;
364
365 if (name)
366 *name = ctx->attrs[index];
367 if (value)
368 *value = ctx->values[index];
369 return 0;
370}
371
372const char * iio_context_get_attr_value(
373 const struct iio_context *ctx, const char *name)
374{
375 unsigned int i;
376
377 for (i = 0; i < ctx->nb_attrs; i++) {
378 if (!strcmp(name, ctx->attrs[i]))
379 return ctx->values[i];
380 }
381
382 return NULL;
383}
Paul Cercueila09970b2016-11-24 17:30:07 +0100384
385int iio_context_add_attr(struct iio_context *ctx,
386 const char *key, const char *value)
387{
388 char **attrs, **values, *new_key, *new_val;
389
390 attrs = realloc(ctx->attrs,
391 (ctx->nb_attrs + 1) * sizeof(*ctx->attrs));
392 if (!attrs)
393 return -ENOMEM;
394
395 ctx->attrs = attrs;
396
397 values = realloc(ctx->values,
398 (ctx->nb_attrs + 1) * sizeof(*ctx->values));
399 if (!values)
400 return -ENOMEM;
401
402 ctx->values = values;
403
404 new_key = iio_strdup(key);
405 if (!new_key)
406 return -ENOMEM;
407
408 new_val = iio_strdup(value);
409 if (!new_val) {
410 free(new_key);
411 return -ENOMEM;
412 }
413
414 ctx->attrs[ctx->nb_attrs] = new_key;
415 ctx->values[ctx->nb_attrs] = new_val;
416 ctx->nb_attrs++;
417 return 0;
418}