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 | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 20 | #include "iio-private.h" |
| 21 | |
Paul Cercueil | 4c6729d | 2014-04-04 17:24:41 +0200 | [diff] [blame] | 22 | #include <errno.h> |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
Paul Cercueil | 8f56ea7 | 2014-10-28 15:18:18 +0100 | [diff] [blame] | 25 | #ifdef _WIN32 |
| 26 | #define LOCAL_BACKEND 0 |
| 27 | #define NETWORK_BACKEND 1 |
| 28 | #endif |
| 29 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 30 | static const char xml_header[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" |
| 31 | "<!DOCTYPE context [" |
| 32 | "<!ELEMENT context (device)*>" |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 33 | "<!ELEMENT device (channel | attribute | debug-attribute)*>" |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 34 | "<!ELEMENT channel (scan-element?, attribute*)>" |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 35 | "<!ELEMENT attribute EMPTY>" |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 36 | "<!ELEMENT scan-element EMPTY>" |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 37 | "<!ELEMENT debug-attribute EMPTY>" |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 38 | "<!ATTLIST context name CDATA #REQUIRED>" |
| 39 | "<!ATTLIST device id CDATA #REQUIRED name CDATA #IMPLIED>" |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 40 | "<!ATTLIST channel id CDATA #REQUIRED type (input|output) #REQUIRED name CDATA #IMPLIED>" |
| 41 | "<!ATTLIST scan-element index CDATA #REQUIRED format CDATA #REQUIRED scale CDATA #IMPLIED>" |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 42 | "<!ATTLIST attribute name CDATA #REQUIRED filename CDATA #IMPLIED>" |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 43 | "<!ATTLIST debug-attribute name CDATA #REQUIRED>" |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 44 | "]>"; |
| 45 | |
| 46 | /* Returns a string containing the XML representation of this context */ |
Paul Cercueil | c1ed848 | 2014-06-11 16:29:43 +0200 | [diff] [blame] | 47 | char * iio_context_create_xml(const struct iio_context *ctx) |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 48 | { |
Paul Cercueil | 9aab019 | 2014-04-07 11:04:30 +0200 | [diff] [blame] | 49 | size_t len = strlen(ctx->name) + sizeof(xml_header) - 1 + |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 50 | sizeof("<context name=\"\" ></context>"); |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 51 | size_t *devices_len; |
| 52 | char *str, *ptr, **devices; |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 53 | unsigned int i; |
| 54 | |
Paul Cercueil | 9aab019 | 2014-04-07 11:04:30 +0200 | [diff] [blame] | 55 | if (!ctx->nb_devices) { |
| 56 | str = malloc(len); |
| 57 | if (str) |
| 58 | snprintf(str, len, "%s<context name=\"%s\" ></context>", |
| 59 | xml_header, ctx->name); |
| 60 | return str; |
| 61 | } |
| 62 | |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 63 | devices_len = malloc(ctx->nb_devices * sizeof(*devices_len)); |
| 64 | if (!devices_len) |
| 65 | return NULL; |
| 66 | |
| 67 | devices = malloc(ctx->nb_devices * sizeof(*devices)); |
| 68 | if (!devices) |
| 69 | goto err_free_devices_len; |
| 70 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 71 | for (i = 0; i < ctx->nb_devices; i++) { |
| 72 | char *xml = iio_device_get_xml(ctx->devices[i], |
| 73 | &devices_len[i]); |
| 74 | if (!xml) |
| 75 | goto err_free_devices; |
| 76 | devices[i] = xml; |
| 77 | len += devices_len[i]; |
| 78 | } |
| 79 | |
| 80 | str = malloc(len); |
| 81 | if (!str) |
| 82 | goto err_free_devices; |
| 83 | |
Paul Cercueil | 8c29e41 | 2014-04-07 09:46:45 +0200 | [diff] [blame] | 84 | snprintf(str, len, "%s<context name=\"%s\" >", xml_header, ctx->name); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 85 | ptr = strrchr(str, '\0'); |
| 86 | |
| 87 | for (i = 0; i < ctx->nb_devices; i++) { |
| 88 | strcpy(ptr, devices[i]); |
| 89 | ptr += devices_len[i]; |
| 90 | free(devices[i]); |
| 91 | } |
| 92 | |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 93 | free(devices); |
| 94 | free(devices_len); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 95 | strcpy(ptr, "</context>"); |
| 96 | return str; |
| 97 | |
| 98 | err_free_devices: |
| 99 | while (i--) |
| 100 | free(devices[i]); |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 101 | free(devices); |
| 102 | err_free_devices_len: |
| 103 | free(devices_len); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 104 | return NULL; |
| 105 | } |
| 106 | |
Paul Cercueil | 4c6729d | 2014-04-04 17:24:41 +0200 | [diff] [blame] | 107 | const char * iio_context_get_xml(const struct iio_context *ctx) |
| 108 | { |
| 109 | return ctx->xml; |
| 110 | } |
| 111 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 112 | const char * iio_context_get_name(const struct iio_context *ctx) |
| 113 | { |
| 114 | return ctx->name; |
| 115 | } |
| 116 | |
| 117 | void iio_context_destroy(struct iio_context *ctx) |
| 118 | { |
Paul Cercueil | 5036354 | 2014-02-18 16:04:28 +0100 | [diff] [blame] | 119 | unsigned int i; |
| 120 | if (ctx->ops->shutdown) |
| 121 | ctx->ops->shutdown(ctx); |
| 122 | |
| 123 | for (i = 0; i < ctx->nb_devices; i++) |
| 124 | free_device(ctx->devices[i]); |
| 125 | if (ctx->nb_devices) |
| 126 | free(ctx->devices); |
Paul Cercueil | 4c6729d | 2014-04-04 17:24:41 +0200 | [diff] [blame] | 127 | if (ctx->xml) |
| 128 | free(ctx->xml); |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 129 | free(ctx); |
| 130 | } |
| 131 | |
| 132 | unsigned int iio_context_get_devices_count(const struct iio_context *ctx) |
| 133 | { |
| 134 | return ctx->nb_devices; |
| 135 | } |
| 136 | |
| 137 | struct iio_device * iio_context_get_device(const struct iio_context *ctx, |
| 138 | unsigned int index) |
| 139 | { |
| 140 | if (index >= ctx->nb_devices) |
| 141 | return NULL; |
| 142 | else |
| 143 | return ctx->devices[index]; |
| 144 | } |
Paul Cercueil | ae88fde | 2014-03-12 11:47:10 +0100 | [diff] [blame] | 145 | |
Paul Cercueil | 17512b0 | 2014-03-28 11:15:24 +0100 | [diff] [blame] | 146 | struct iio_device * iio_context_find_device(const struct iio_context *ctx, |
| 147 | const char *name) |
| 148 | { |
| 149 | unsigned int i; |
| 150 | for (i = 0; i < ctx->nb_devices; i++) { |
| 151 | struct iio_device *dev = ctx->devices[i]; |
| 152 | if (!strcmp(dev->id, name) || |
| 153 | (dev->name && !strcmp(dev->name, name))) |
| 154 | return dev; |
| 155 | } |
| 156 | return NULL; |
| 157 | } |
| 158 | |
Paul Cercueil | 1f7bc15 | 2014-03-14 13:48:43 +0100 | [diff] [blame] | 159 | static void reorder_channels(struct iio_device *dev) |
| 160 | { |
| 161 | bool found; |
| 162 | unsigned int i; |
| 163 | |
| 164 | /* Reorder channels by index */ |
| 165 | do { |
| 166 | found = false; |
| 167 | for (i = 1; i < dev->nb_channels; i++) { |
| 168 | struct iio_channel **channels = dev->channels; |
| 169 | long ch1 = channels[i - 1]->index; |
| 170 | long ch2 = channels[i]->index; |
| 171 | |
| 172 | if (ch2 >= 0 && ((ch1 > ch2) || ch1 < 0)) { |
| 173 | struct iio_channel *bak = channels[i]; |
| 174 | channels[i] = channels[i - 1]; |
| 175 | channels[i - 1] = bak; |
| 176 | found = true; |
| 177 | } |
| 178 | } |
| 179 | } while (found); |
| 180 | } |
| 181 | |
Paul Cercueil | c1ed848 | 2014-06-11 16:29:43 +0200 | [diff] [blame] | 182 | void iio_context_init(struct iio_context *ctx) |
Paul Cercueil | ae88fde | 2014-03-12 11:47:10 +0100 | [diff] [blame] | 183 | { |
| 184 | unsigned int i; |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 185 | for (i = 0; i < ctx->nb_devices; i++) |
| 186 | reorder_channels(ctx->devices[i]); |
Paul Cercueil | ae88fde | 2014-03-12 11:47:10 +0100 | [diff] [blame] | 187 | } |
Paul Cercueil | e45f876 | 2014-05-02 11:19:26 +0200 | [diff] [blame] | 188 | |
| 189 | int iio_context_get_version(const struct iio_context *ctx, |
Paul Cercueil | 9de9e9d | 2014-05-20 13:18:19 +0200 | [diff] [blame] | 190 | unsigned int *major, unsigned int *minor, char git_tag[8]) |
Paul Cercueil | e45f876 | 2014-05-02 11:19:26 +0200 | [diff] [blame] | 191 | { |
| 192 | if (ctx->ops->get_version) |
Paul Cercueil | 9de9e9d | 2014-05-20 13:18:19 +0200 | [diff] [blame] | 193 | return ctx->ops->get_version(ctx, major, minor, git_tag); |
Paul Cercueil | 53ed943 | 2014-11-20 13:46:04 +0100 | [diff] [blame^] | 194 | |
| 195 | iio_library_get_version(major, minor, git_tag); |
Paul Cercueil | e45f876 | 2014-05-02 11:19:26 +0200 | [diff] [blame] | 196 | return 0; |
| 197 | } |
Paul Cercueil | 4ca7354 | 2014-06-10 16:23:29 +0200 | [diff] [blame] | 198 | |
| 199 | int iio_context_set_timeout(struct iio_context *ctx, unsigned int timeout) |
| 200 | { |
| 201 | if (ctx->ops->set_timeout) |
| 202 | return ctx->ops->set_timeout(ctx, timeout); |
| 203 | else |
| 204 | return -ENOSYS; |
| 205 | } |
Paul Cercueil | 63d5e7c | 2014-10-28 14:33:08 +0100 | [diff] [blame] | 206 | |
| 207 | struct iio_context * iio_context_clone(const struct iio_context *ctx) |
| 208 | { |
| 209 | if (ctx->ops->clone) |
| 210 | return ctx->ops->clone(ctx); |
| 211 | else |
| 212 | return NULL; |
| 213 | } |
Paul Cercueil | 8f56ea7 | 2014-10-28 15:18:18 +0100 | [diff] [blame] | 214 | |
| 215 | struct iio_context * iio_create_default_context(void) |
| 216 | { |
| 217 | #if NETWORK_BACKEND |
| 218 | char *hostname = getenv("IIOD_REMOTE"); |
| 219 | |
| 220 | if (hostname) { |
| 221 | /* If the environment variable is an empty string, we will |
| 222 | * discover the server using ZeroConf */ |
| 223 | if (strlen(hostname) == 0) |
| 224 | hostname = NULL; |
| 225 | |
| 226 | return iio_create_network_context(hostname); |
| 227 | } |
| 228 | #endif |
| 229 | #if LOCAL_BACKEND |
| 230 | return iio_create_local_context(); |
| 231 | #else |
| 232 | return NULL; |
| 233 | #endif |
| 234 | } |