Paul Cercueil | bb4401d | 2014-02-28 16:10:49 +0100 | [diff] [blame] | 1 | /* |
| 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 Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 19 | #include "debug.h" |
Paul Cercueil | 2814ed1 | 2016-08-25 17:08:18 +0200 | [diff] [blame] | 20 | #include "iio-config.h" |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 21 | #include "iio-private.h" |
| 22 | |
Paul Cercueil | 4c6729d | 2014-04-04 17:24:41 +0200 | [diff] [blame] | 23 | #include <errno.h> |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 24 | #include <string.h> |
| 25 | |
Paul Cercueil | 8f56ea7 | 2014-10-28 15:18:18 +0100 | [diff] [blame] | 26 | #ifdef _WIN32 |
| 27 | #define LOCAL_BACKEND 0 |
| 28 | #define NETWORK_BACKEND 1 |
| 29 | #endif |
| 30 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 31 | static const char xml_header[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" |
| 32 | "<!DOCTYPE context [" |
| 33 | "<!ELEMENT context (device)*>" |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 34 | "<!ELEMENT device (channel | attribute | debug-attribute)*>" |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 35 | "<!ELEMENT channel (scan-element?, attribute*)>" |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 36 | "<!ELEMENT attribute EMPTY>" |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 37 | "<!ELEMENT scan-element EMPTY>" |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 38 | "<!ELEMENT debug-attribute EMPTY>" |
Paul Cercueil | 8735037 | 2015-03-16 13:24:37 +0100 | [diff] [blame] | 39 | "<!ATTLIST context name CDATA #REQUIRED description CDATA #IMPLIED>" |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 40 | "<!ATTLIST device id CDATA #REQUIRED name CDATA #IMPLIED>" |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 41 | "<!ATTLIST channel id CDATA #REQUIRED type (input|output) #REQUIRED name CDATA #IMPLIED>" |
| 42 | "<!ATTLIST scan-element index CDATA #REQUIRED format CDATA #REQUIRED scale CDATA #IMPLIED>" |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 43 | "<!ATTLIST attribute name CDATA #REQUIRED filename CDATA #IMPLIED>" |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 44 | "<!ATTLIST debug-attribute name CDATA #REQUIRED>" |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 45 | "]>"; |
| 46 | |
| 47 | /* Returns a string containing the XML representation of this context */ |
Paul Cercueil | c1ed848 | 2014-06-11 16:29:43 +0200 | [diff] [blame] | 48 | char * iio_context_create_xml(const struct iio_context *ctx) |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 49 | { |
Paul Cercueil | 8735037 | 2015-03-16 13:24:37 +0100 | [diff] [blame] | 50 | size_t len, *devices_len; |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 51 | char *str, *ptr, **devices; |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 52 | unsigned int i; |
| 53 | |
Paul Cercueil | 8735037 | 2015-03-16 13:24:37 +0100 | [diff] [blame] | 54 | len = strlen(ctx->name) + sizeof(xml_header) - 1 + |
| 55 | sizeof("<context name=\"\" ></context>"); |
| 56 | if (ctx->description) |
| 57 | len += strlen(ctx->description) + |
| 58 | sizeof(" description=\"\"") - 1; |
| 59 | |
Paul Cercueil | 9aab019 | 2014-04-07 11:04:30 +0200 | [diff] [blame] | 60 | if (!ctx->nb_devices) { |
| 61 | str = malloc(len); |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 62 | if (!str) { |
| 63 | errno = ENOMEM; |
Paul Cercueil | 8735037 | 2015-03-16 13:24:37 +0100 | [diff] [blame] | 64 | return NULL; |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 65 | } |
Paul Cercueil | 8735037 | 2015-03-16 13:24:37 +0100 | [diff] [blame] | 66 | |
| 67 | if (ctx->description) |
| 68 | snprintf(str, len, "%s<context name=\"%s\" " |
| 69 | "description=\"%s\" ></context>", |
| 70 | xml_header, ctx->name, |
| 71 | ctx->description); |
| 72 | else |
Paul Cercueil | 9aab019 | 2014-04-07 11:04:30 +0200 | [diff] [blame] | 73 | snprintf(str, len, "%s<context name=\"%s\" ></context>", |
| 74 | xml_header, ctx->name); |
| 75 | return str; |
| 76 | } |
| 77 | |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 78 | devices_len = malloc(ctx->nb_devices * sizeof(*devices_len)); |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 79 | if (!devices_len) { |
| 80 | errno = ENOMEM; |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 81 | return NULL; |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 82 | } |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 83 | |
| 84 | devices = malloc(ctx->nb_devices * sizeof(*devices)); |
| 85 | if (!devices) |
| 86 | goto err_free_devices_len; |
| 87 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 88 | for (i = 0; i < ctx->nb_devices; i++) { |
| 89 | char *xml = iio_device_get_xml(ctx->devices[i], |
| 90 | &devices_len[i]); |
| 91 | if (!xml) |
| 92 | goto err_free_devices; |
| 93 | devices[i] = xml; |
| 94 | len += devices_len[i]; |
| 95 | } |
| 96 | |
| 97 | str = malloc(len); |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 98 | if (!str) { |
| 99 | errno = ENOMEM; |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 100 | goto err_free_devices; |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 101 | } |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 102 | |
Paul Cercueil | 8735037 | 2015-03-16 13:24:37 +0100 | [diff] [blame] | 103 | if (ctx->description) |
| 104 | snprintf(str, len, "%s<context name=\"%s\" " |
| 105 | "description=\"%s\" >", |
| 106 | xml_header, ctx->name, ctx->description); |
| 107 | else |
| 108 | snprintf(str, len, "%s<context name=\"%s\" >", |
| 109 | xml_header, ctx->name); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 110 | ptr = strrchr(str, '\0'); |
| 111 | |
| 112 | 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 Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 118 | free(devices); |
| 119 | free(devices_len); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 120 | strcpy(ptr, "</context>"); |
| 121 | return str; |
| 122 | |
| 123 | err_free_devices: |
| 124 | while (i--) |
| 125 | free(devices[i]); |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 126 | free(devices); |
| 127 | err_free_devices_len: |
| 128 | free(devices_len); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 129 | return NULL; |
| 130 | } |
| 131 | |
Paul Cercueil | 4c6729d | 2014-04-04 17:24:41 +0200 | [diff] [blame] | 132 | const char * iio_context_get_xml(const struct iio_context *ctx) |
| 133 | { |
| 134 | return ctx->xml; |
| 135 | } |
| 136 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 137 | const char * iio_context_get_name(const struct iio_context *ctx) |
| 138 | { |
| 139 | return ctx->name; |
| 140 | } |
| 141 | |
Paul Cercueil | 3ac36c1 | 2015-01-08 14:44:00 +0100 | [diff] [blame] | 142 | const 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 Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 150 | void iio_context_destroy(struct iio_context *ctx) |
| 151 | { |
Paul Cercueil | 5036354 | 2014-02-18 16:04:28 +0100 | [diff] [blame] | 152 | unsigned int i; |
| 153 | if (ctx->ops->shutdown) |
| 154 | ctx->ops->shutdown(ctx); |
| 155 | |
Paul Cercueil | 60b4d1b | 2016-11-15 15:55:41 +0100 | [diff] [blame^] | 156 | 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 Cercueil | 5036354 | 2014-02-18 16:04:28 +0100 | [diff] [blame] | 164 | for (i = 0; i < ctx->nb_devices; i++) |
| 165 | free_device(ctx->devices[i]); |
| 166 | if (ctx->nb_devices) |
| 167 | free(ctx->devices); |
Paul Cercueil | 4c6729d | 2014-04-04 17:24:41 +0200 | [diff] [blame] | 168 | if (ctx->xml) |
| 169 | free(ctx->xml); |
Paul Cercueil | 3ac36c1 | 2015-01-08 14:44:00 +0100 | [diff] [blame] | 170 | if (ctx->description) |
| 171 | free(ctx->description); |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 172 | free(ctx); |
| 173 | } |
| 174 | |
| 175 | unsigned int iio_context_get_devices_count(const struct iio_context *ctx) |
| 176 | { |
| 177 | return ctx->nb_devices; |
| 178 | } |
| 179 | |
| 180 | struct 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 Cercueil | ae88fde | 2014-03-12 11:47:10 +0100 | [diff] [blame] | 188 | |
Paul Cercueil | 17512b0 | 2014-03-28 11:15:24 +0100 | [diff] [blame] | 189 | struct 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 Cercueil | 1f7bc15 | 2014-03-14 13:48:43 +0100 | [diff] [blame] | 202 | static 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 Clausen | 75baa4d | 2016-02-01 17:58:57 +0100 | [diff] [blame] | 215 | if (ch1 == ch2 && ch1 >= 0) { |
| 216 | ch1 = channels[i - 1]->format.shift; |
| 217 | ch2 = channels[i]->format.shift; |
| 218 | } |
| 219 | |
Paul Cercueil | 1f7bc15 | 2014-03-14 13:48:43 +0100 | [diff] [blame] | 220 | 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); |
| 228 | } |
| 229 | |
Paul Cercueil | fd38747 | 2015-08-05 10:34:19 +0200 | [diff] [blame] | 230 | int iio_context_init(struct iio_context *ctx) |
Paul Cercueil | ae88fde | 2014-03-12 11:47:10 +0100 | [diff] [blame] | 231 | { |
| 232 | unsigned int i; |
Paul Cercueil | fd38747 | 2015-08-05 10:34:19 +0200 | [diff] [blame] | 233 | |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 234 | for (i = 0; i < ctx->nb_devices; i++) |
| 235 | reorder_channels(ctx->devices[i]); |
Paul Cercueil | fd38747 | 2015-08-05 10:34:19 +0200 | [diff] [blame] | 236 | |
| 237 | if (!ctx->xml) { |
| 238 | ctx->xml = iio_context_create_xml(ctx); |
| 239 | if (!ctx->xml) |
| 240 | return -ENOMEM; |
| 241 | } |
| 242 | |
| 243 | return 0; |
Paul Cercueil | ae88fde | 2014-03-12 11:47:10 +0100 | [diff] [blame] | 244 | } |
Paul Cercueil | e45f876 | 2014-05-02 11:19:26 +0200 | [diff] [blame] | 245 | |
| 246 | int iio_context_get_version(const struct iio_context *ctx, |
Paul Cercueil | 9de9e9d | 2014-05-20 13:18:19 +0200 | [diff] [blame] | 247 | unsigned int *major, unsigned int *minor, char git_tag[8]) |
Paul Cercueil | e45f876 | 2014-05-02 11:19:26 +0200 | [diff] [blame] | 248 | { |
| 249 | if (ctx->ops->get_version) |
Paul Cercueil | 9de9e9d | 2014-05-20 13:18:19 +0200 | [diff] [blame] | 250 | return ctx->ops->get_version(ctx, major, minor, git_tag); |
Paul Cercueil | 53ed943 | 2014-11-20 13:46:04 +0100 | [diff] [blame] | 251 | |
| 252 | iio_library_get_version(major, minor, git_tag); |
Paul Cercueil | e45f876 | 2014-05-02 11:19:26 +0200 | [diff] [blame] | 253 | return 0; |
| 254 | } |
Paul Cercueil | 4ca7354 | 2014-06-10 16:23:29 +0200 | [diff] [blame] | 255 | |
| 256 | int iio_context_set_timeout(struct iio_context *ctx, unsigned int timeout) |
| 257 | { |
| 258 | if (ctx->ops->set_timeout) |
| 259 | return ctx->ops->set_timeout(ctx, timeout); |
| 260 | else |
| 261 | return -ENOSYS; |
| 262 | } |
Paul Cercueil | 63d5e7c | 2014-10-28 14:33:08 +0100 | [diff] [blame] | 263 | |
| 264 | struct iio_context * iio_context_clone(const struct iio_context *ctx) |
| 265 | { |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 266 | if (ctx->ops->clone) { |
Paul Cercueil | 63d5e7c | 2014-10-28 14:33:08 +0100 | [diff] [blame] | 267 | return ctx->ops->clone(ctx); |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 268 | } else { |
| 269 | errno = ENOSYS; |
Paul Cercueil | 63d5e7c | 2014-10-28 14:33:08 +0100 | [diff] [blame] | 270 | return NULL; |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 271 | } |
Paul Cercueil | 63d5e7c | 2014-10-28 14:33:08 +0100 | [diff] [blame] | 272 | } |
Paul Cercueil | 8f56ea7 | 2014-10-28 15:18:18 +0100 | [diff] [blame] | 273 | |
Lars-Peter Clausen | 60f1c9a | 2016-02-22 13:20:40 +0100 | [diff] [blame] | 274 | struct iio_context * iio_create_context_from_uri(const char *uri) |
| 275 | { |
Paul Cercueil | 2814ed1 | 2016-08-25 17:08:18 +0200 | [diff] [blame] | 276 | #ifdef WITH_LOCAL_BACKEND |
Lars-Peter Clausen | 60f1c9a | 2016-02-22 13:20:40 +0100 | [diff] [blame] | 277 | if (strcmp(uri, "local:") == 0) /* No address part */ |
| 278 | return iio_create_local_context(); |
Paul Cercueil | c399f51 | 2016-04-26 17:55:27 +0200 | [diff] [blame] | 279 | #endif |
Lars-Peter Clausen | 60f1c9a | 2016-02-22 13:20:40 +0100 | [diff] [blame] | 280 | |
Paul Cercueil | 2814ed1 | 2016-08-25 17:08:18 +0200 | [diff] [blame] | 281 | #ifdef WITH_XML_BACKEND |
Lars-Peter Clausen | 60f1c9a | 2016-02-22 13:20:40 +0100 | [diff] [blame] | 282 | if (strncmp(uri, "xml:", sizeof("xml:") - 1) == 0) |
| 283 | return iio_create_xml_context(uri + sizeof("xml:") - 1); |
Paul Cercueil | c399f51 | 2016-04-26 17:55:27 +0200 | [diff] [blame] | 284 | #endif |
Lars-Peter Clausen | 60f1c9a | 2016-02-22 13:20:40 +0100 | [diff] [blame] | 285 | |
Paul Cercueil | 2814ed1 | 2016-08-25 17:08:18 +0200 | [diff] [blame] | 286 | #ifdef WITH_NETWORK_BACKEND |
Lars-Peter Clausen | 60f1c9a | 2016-02-22 13:20:40 +0100 | [diff] [blame] | 287 | if (strncmp(uri, "ip:", sizeof("ip:") - 1) == 0) |
| 288 | return iio_create_network_context(uri+3); |
Paul Cercueil | c399f51 | 2016-04-26 17:55:27 +0200 | [diff] [blame] | 289 | #endif |
Lars-Peter Clausen | 60f1c9a | 2016-02-22 13:20:40 +0100 | [diff] [blame] | 290 | |
Paul Cercueil | 2814ed1 | 2016-08-25 17:08:18 +0200 | [diff] [blame] | 291 | #ifdef WITH_USB_BACKEND |
Paul Cercueil | c399f51 | 2016-04-26 17:55:27 +0200 | [diff] [blame] | 292 | if (strncmp(uri, "usb:", sizeof("usb:") - 1) == 0) |
Lars-Peter Clausen | 60f1c9a | 2016-02-22 13:20:40 +0100 | [diff] [blame] | 293 | return usb_create_context_from_uri(uri); |
Lars-Peter Clausen | 60f1c9a | 2016-02-22 13:20:40 +0100 | [diff] [blame] | 294 | #endif |
| 295 | |
Paul Cercueil | 2814ed1 | 2016-08-25 17:08:18 +0200 | [diff] [blame] | 296 | #ifdef WITH_SERIAL_BACKEND |
Paul Cercueil | bcb0452 | 2016-03-22 17:03:29 +0100 | [diff] [blame] | 297 | if (strncmp(uri, "serial:", sizeof("serial:") - 1) == 0) |
| 298 | return serial_create_context_from_uri(uri); |
| 299 | #endif |
| 300 | |
Paul Cercueil | 1041646 | 2016-04-26 16:09:06 +0200 | [diff] [blame] | 301 | errno = ENOSYS; |
Lars-Peter Clausen | 60f1c9a | 2016-02-22 13:20:40 +0100 | [diff] [blame] | 302 | return NULL; |
| 303 | } |
| 304 | |
Paul Cercueil | 8f56ea7 | 2014-10-28 15:18:18 +0100 | [diff] [blame] | 305 | struct iio_context * iio_create_default_context(void) |
| 306 | { |
Paul Cercueil | 8f56ea7 | 2014-10-28 15:18:18 +0100 | [diff] [blame] | 307 | char *hostname = getenv("IIOD_REMOTE"); |
| 308 | |
| 309 | if (hostname) { |
Lars-Peter Clausen | 60f1c9a | 2016-02-22 13:20:40 +0100 | [diff] [blame] | 310 | struct iio_context *ctx; |
| 311 | |
| 312 | ctx = iio_create_context_from_uri(hostname); |
| 313 | if (ctx) |
| 314 | return ctx; |
| 315 | |
Paul Cercueil | 2814ed1 | 2016-08-25 17:08:18 +0200 | [diff] [blame] | 316 | #ifdef WITH_NETWORK_BACKEND |
Paul Cercueil | 8f56ea7 | 2014-10-28 15:18:18 +0100 | [diff] [blame] | 317 | /* If the environment variable is an empty string, we will |
| 318 | * discover the server using ZeroConf */ |
| 319 | if (strlen(hostname) == 0) |
| 320 | hostname = NULL; |
| 321 | |
| 322 | return iio_create_network_context(hostname); |
Paul Cercueil | 8f56ea7 | 2014-10-28 15:18:18 +0100 | [diff] [blame] | 323 | #endif |
Paul Cercueil | 3cce965 | 2015-12-02 13:11:47 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Paul Cercueil | 8f56ea7 | 2014-10-28 15:18:18 +0100 | [diff] [blame] | 326 | return iio_create_local_context(); |
Paul Cercueil | 63e5218 | 2014-12-11 12:52:48 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | struct iio_context * iio_create_local_context(void) |
| 330 | { |
Paul Cercueil | 2814ed1 | 2016-08-25 17:08:18 +0200 | [diff] [blame] | 331 | #ifdef WITH_LOCAL_BACKEND |
Paul Cercueil | 63e5218 | 2014-12-11 12:52:48 +0100 | [diff] [blame] | 332 | return local_create_context(); |
| 333 | #else |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 334 | errno = ENOSYS; |
Paul Cercueil | 63e5218 | 2014-12-11 12:52:48 +0100 | [diff] [blame] | 335 | return NULL; |
| 336 | #endif |
| 337 | } |
| 338 | |
| 339 | struct iio_context * iio_create_network_context(const char *hostname) |
| 340 | { |
Paul Cercueil | 2814ed1 | 2016-08-25 17:08:18 +0200 | [diff] [blame] | 341 | #ifdef WITH_NETWORK_BACKEND |
Paul Cercueil | 63e5218 | 2014-12-11 12:52:48 +0100 | [diff] [blame] | 342 | return network_create_context(hostname); |
| 343 | #else |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 344 | errno = ENOSYS; |
Paul Cercueil | 63e5218 | 2014-12-11 12:52:48 +0100 | [diff] [blame] | 345 | return NULL; |
| 346 | #endif |
| 347 | } |
| 348 | |
| 349 | struct iio_context * iio_create_xml_context_mem(const char *xml, size_t len) |
| 350 | { |
Paul Cercueil | 2814ed1 | 2016-08-25 17:08:18 +0200 | [diff] [blame] | 351 | #ifdef WITH_XML_BACKEND |
Paul Cercueil | 63e5218 | 2014-12-11 12:52:48 +0100 | [diff] [blame] | 352 | return xml_create_context_mem(xml, len); |
| 353 | #else |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 354 | errno = ENOSYS; |
Paul Cercueil | 63e5218 | 2014-12-11 12:52:48 +0100 | [diff] [blame] | 355 | return NULL; |
| 356 | #endif |
| 357 | } |
| 358 | |
| 359 | struct iio_context * iio_create_xml_context(const char *xml_file) |
| 360 | { |
Paul Cercueil | 2814ed1 | 2016-08-25 17:08:18 +0200 | [diff] [blame] | 361 | #ifdef WITH_XML_BACKEND |
Paul Cercueil | 63e5218 | 2014-12-11 12:52:48 +0100 | [diff] [blame] | 362 | return xml_create_context(xml_file); |
Paul Cercueil | 8f56ea7 | 2014-10-28 15:18:18 +0100 | [diff] [blame] | 363 | #else |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 364 | errno = ENOSYS; |
Paul Cercueil | 8f56ea7 | 2014-10-28 15:18:18 +0100 | [diff] [blame] | 365 | return NULL; |
| 366 | #endif |
| 367 | } |
Paul Cercueil | 60b4d1b | 2016-11-15 15:55:41 +0100 | [diff] [blame^] | 368 | |
| 369 | unsigned int iio_context_get_attrs_count(const struct iio_context *ctx) |
| 370 | { |
| 371 | return ctx->nb_attrs; |
| 372 | } |
| 373 | |
| 374 | int iio_context_get_attr(const struct iio_context *ctx, unsigned int index, |
| 375 | const char **name, const char **value) |
| 376 | { |
| 377 | if (index >= ctx->nb_attrs) |
| 378 | return -EINVAL; |
| 379 | |
| 380 | if (name) |
| 381 | *name = ctx->attrs[index]; |
| 382 | if (value) |
| 383 | *value = ctx->values[index]; |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | const char * iio_context_get_attr_value( |
| 388 | const struct iio_context *ctx, const char *name) |
| 389 | { |
| 390 | unsigned int i; |
| 391 | |
| 392 | for (i = 0; i < ctx->nb_attrs; i++) { |
| 393 | if (!strcmp(name, ctx->attrs[i])) |
| 394 | return ctx->values[i]; |
| 395 | } |
| 396 | |
| 397 | return NULL; |
| 398 | } |