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 | 167d311 | 2014-02-18 12:23:53 +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 | 167d311 | 2014-02-18 12:23:53 +0100 | [diff] [blame] | 22 | #include <errno.h> |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 23 | #include <stdio.h> |
Paul Cercueil | 167d311 | 2014-02-18 12:23:53 +0100 | [diff] [blame] | 24 | #include <string.h> |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 25 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 26 | static char *get_attr_xml(const char *attr, size_t *length) |
| 27 | { |
| 28 | size_t len = sizeof("<attribute name=\"\" />") + strlen(attr); |
| 29 | char *str = malloc(len); |
| 30 | if (!str) { |
| 31 | ERROR("Unable to allocate memory\n"); |
| 32 | return NULL; |
| 33 | } |
| 34 | |
| 35 | *length = len - 1; /* Skip the \0 */ |
| 36 | sprintf(str, "<attribute name=\"%s\" />", attr); |
| 37 | return str; |
| 38 | } |
| 39 | |
| 40 | /* Returns a string containing the XML representation of this channel */ |
| 41 | char * iio_channel_get_xml(const struct iio_channel *chn, size_t *length) |
| 42 | { |
| 43 | size_t len = sizeof("<channel id=\"\" name=\"\" " |
| 44 | "type=\"output\" ></channel>"); |
| 45 | char *ptr, *str, *attrs[chn->nb_attrs]; |
| 46 | size_t attrs_len[chn->nb_attrs]; |
| 47 | unsigned int i; |
| 48 | |
| 49 | for (i = 0; i < chn->nb_attrs; i++) { |
| 50 | char *xml = get_attr_xml(chn->attrs[i], &attrs_len[i]); |
| 51 | if (!xml) |
| 52 | goto err_free_attrs; |
| 53 | attrs[i] = xml; |
| 54 | len += attrs_len[i]; |
| 55 | } |
| 56 | |
| 57 | len += strlen(chn->id); |
| 58 | if (chn->name) |
| 59 | len += strlen(chn->name); |
| 60 | |
| 61 | str = malloc(len); |
| 62 | if (!str) |
| 63 | goto err_free_attrs; |
| 64 | |
| 65 | sprintf(str, "<channel id=\"%s\"", chn->id); |
| 66 | ptr = strrchr(str, '\0'); |
| 67 | |
| 68 | if (chn->name) { |
| 69 | sprintf(ptr, " name=\"%s\"", chn->name); |
| 70 | ptr = strrchr(ptr, '\0'); |
| 71 | } |
| 72 | |
| 73 | sprintf(ptr, " type=\"%s\" >", chn->is_output ? "output" : "input"); |
| 74 | ptr = strrchr(ptr, '\0'); |
| 75 | |
| 76 | for (i = 0; i < chn->nb_attrs; i++) { |
| 77 | strcpy(ptr, attrs[i]); |
| 78 | ptr += attrs_len[i]; |
| 79 | free(attrs[i]); |
| 80 | } |
| 81 | |
| 82 | strcpy(ptr, "</channel>"); |
| 83 | *length = ptr - str + sizeof("</channel>") - 1; |
| 84 | return str; |
| 85 | |
| 86 | err_free_attrs: |
| 87 | while (i--) |
| 88 | free(attrs[i]); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 92 | const char * iio_channel_get_id(const struct iio_channel *chn) |
| 93 | { |
| 94 | return chn->id; |
| 95 | } |
| 96 | |
| 97 | const char * iio_channel_get_name(const struct iio_channel *chn) |
| 98 | { |
| 99 | return chn->name; |
| 100 | } |
| 101 | |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 102 | bool iio_channel_is_output(const struct iio_channel *chn) |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 103 | { |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 104 | return chn->is_output; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | unsigned int iio_channel_get_attrs_count(const struct iio_channel *chn) |
| 108 | { |
| 109 | return chn->nb_attrs; |
| 110 | } |
| 111 | |
| 112 | const char * iio_channel_get_attr(const struct iio_channel *chn, |
| 113 | unsigned int index) |
| 114 | { |
| 115 | if (index >= chn->nb_attrs) |
| 116 | return NULL; |
| 117 | else |
| 118 | return chn->attrs[index]; |
| 119 | } |
| 120 | |
| 121 | ssize_t iio_channel_attr_read(const struct iio_channel *chn, |
| 122 | const char *attr, char *dst, size_t len) |
| 123 | { |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 124 | if (chn->dev->ctx->ops->read_channel_attr) |
| 125 | return chn->dev->ctx->ops->read_channel_attr(chn, |
| 126 | attr, dst, len); |
| 127 | else |
| 128 | return -ENOSYS; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | ssize_t iio_channel_attr_write(const struct iio_channel *chn, |
| 132 | const char *attr, const char *src) |
| 133 | { |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 134 | if (chn->dev->ctx->ops->write_channel_attr) |
| 135 | return chn->dev->ctx->ops->write_channel_attr(chn, attr, src); |
| 136 | else |
| 137 | return -ENOSYS; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 138 | } |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 139 | |
| 140 | void free_channel(struct iio_channel *chn) |
| 141 | { |
| 142 | unsigned int i; |
| 143 | for (i = 0; i < chn->nb_attrs; i++) |
| 144 | free((char *) chn->attrs[i]); |
| 145 | if (chn->nb_attrs) |
| 146 | free(chn->attrs); |
| 147 | if (chn->name) |
| 148 | free((char *) chn->name); |
| 149 | if (chn->id) |
| 150 | free((char *) chn->id); |
| 151 | free(chn); |
| 152 | } |