Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 1 | #include "debug.h" |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 2 | #include "iio-private.h" |
| 3 | |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 4 | #include <errno.h> |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 5 | #include <stdio.h> |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 6 | #include <string.h> |
| 7 | |
| 8 | static char *get_attr_xml(const char *attr, size_t *length) |
| 9 | { |
| 10 | size_t len = sizeof("<attribute name=\"\" />") + strlen(attr); |
| 11 | char *str = malloc(len); |
| 12 | if (!str) { |
| 13 | ERROR("Unable to allocate memory\n"); |
| 14 | return NULL; |
| 15 | } |
| 16 | |
| 17 | *length = len - 1; /* Skip the \0 */ |
| 18 | sprintf(str, "<attribute name=\"%s\" />", attr); |
| 19 | return str; |
| 20 | } |
| 21 | |
| 22 | /* Returns a string containing the XML representation of this device */ |
| 23 | char * iio_device_get_xml(const struct iio_device *dev, size_t *length) |
| 24 | { |
| 25 | size_t len = sizeof("<device id=\"\" name=\"\" ></device>"); |
| 26 | char *ptr, *str, *attrs[dev->nb_attrs], *channels[dev->nb_channels]; |
| 27 | size_t attrs_len[dev->nb_attrs], channels_len[dev->nb_channels]; |
| 28 | unsigned int i, j; |
| 29 | |
| 30 | for (i = 0; i < dev->nb_attrs; i++) { |
| 31 | char *xml = get_attr_xml(dev->attrs[i], &attrs_len[i]); |
| 32 | if (!xml) |
| 33 | goto err_free_attrs; |
| 34 | attrs[i] = xml; |
| 35 | len += attrs_len[i]; |
| 36 | } |
| 37 | |
| 38 | for (j = 0; j < dev->nb_channels; j++) { |
| 39 | char *xml = iio_channel_get_xml(dev->channels[j], |
| 40 | &channels_len[j]); |
| 41 | if (!xml) |
| 42 | goto err_free_channels; |
| 43 | channels[j] = xml; |
| 44 | len += channels_len[j]; |
| 45 | } |
| 46 | |
| 47 | len += strlen(dev->id); |
| 48 | if (dev->name) |
| 49 | len += strlen(dev->name); |
| 50 | |
| 51 | str = malloc(len); |
| 52 | if (!str) |
| 53 | goto err_free_channels; |
| 54 | |
| 55 | sprintf(str, "<device id=\"%s\"", dev->id); |
| 56 | ptr = strrchr(str, '\0'); |
| 57 | |
| 58 | if (dev->name) { |
| 59 | sprintf(ptr, " name=\"%s\"", dev->name); |
| 60 | ptr = strrchr(ptr, '\0'); |
| 61 | } |
| 62 | |
| 63 | strcpy(ptr, " >"); |
| 64 | ptr += 2; |
| 65 | |
| 66 | for (i = 0; i < dev->nb_channels; i++) { |
| 67 | strcpy(ptr, channels[i]); |
| 68 | ptr += channels_len[i]; |
| 69 | free(channels[i]); |
| 70 | } |
| 71 | |
| 72 | for (i = 0; i < dev->nb_attrs; i++) { |
| 73 | strcpy(ptr, attrs[i]); |
| 74 | ptr += attrs_len[i]; |
| 75 | free(attrs[i]); |
| 76 | } |
| 77 | |
| 78 | strcpy(ptr, "</device>"); |
| 79 | *length = ptr - str + sizeof("</device>") - 1; |
| 80 | return str; |
| 81 | |
| 82 | err_free_channels: |
| 83 | while (j--) |
| 84 | free(channels[j]); |
| 85 | err_free_attrs: |
| 86 | while (i--) |
| 87 | free(attrs[i]); |
| 88 | return NULL; |
| 89 | } |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 90 | |
| 91 | const char * iio_device_get_id(const struct iio_device *dev) |
| 92 | { |
| 93 | return dev->id; |
| 94 | } |
| 95 | |
| 96 | const char * iio_device_get_name(const struct iio_device *dev) |
| 97 | { |
| 98 | return dev->name; |
| 99 | } |
| 100 | |
| 101 | unsigned int iio_device_get_channels_count(const struct iio_device *dev) |
| 102 | { |
| 103 | return dev->nb_channels; |
| 104 | } |
| 105 | |
| 106 | struct iio_channel * iio_device_get_channel(const struct iio_device *dev, |
| 107 | unsigned int index) |
| 108 | { |
| 109 | if (index >= dev->nb_channels) |
| 110 | return NULL; |
| 111 | else |
| 112 | return dev->channels[index]; |
| 113 | } |
| 114 | |
| 115 | unsigned int iio_device_get_attrs_count(const struct iio_device *dev) |
| 116 | { |
| 117 | return dev->nb_attrs; |
| 118 | } |
| 119 | |
| 120 | const char * iio_device_get_attr(const struct iio_device *dev, |
| 121 | unsigned int index) |
| 122 | { |
| 123 | if (index >= dev->nb_attrs) |
| 124 | return NULL; |
| 125 | else |
| 126 | return dev->attrs[index]; |
| 127 | } |
| 128 | |
Paul Cercueil | ec1760d | 2014-02-21 11:31:20 +0100 | [diff] [blame] | 129 | int iio_device_open(const struct iio_device *dev) |
| 130 | { |
| 131 | if (dev->ctx->ops->open) |
| 132 | return dev->ctx->ops->open(dev); |
| 133 | else |
| 134 | return -ENOSYS; |
| 135 | } |
| 136 | |
| 137 | int iio_device_close(const struct iio_device *dev) |
| 138 | { |
| 139 | if (dev->ctx->ops->close) |
| 140 | return dev->ctx->ops->close(dev); |
| 141 | else |
| 142 | return -ENOSYS; |
| 143 | } |
| 144 | |
| 145 | ssize_t iio_device_read_raw(const struct iio_device *dev, |
| 146 | void *dst, size_t len) |
| 147 | { |
| 148 | if (dev->ctx->ops->read) |
| 149 | return dev->ctx->ops->read(dev, dst, len); |
| 150 | else |
| 151 | return -ENOSYS; |
| 152 | } |
| 153 | |
| 154 | ssize_t iio_device_write_raw(const struct iio_device *dev, |
| 155 | const void *src, size_t len) |
| 156 | { |
| 157 | if (dev->ctx->ops->write) |
| 158 | return dev->ctx->ops->write(dev, src, len); |
| 159 | else |
| 160 | return -ENOSYS; |
| 161 | } |
| 162 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 163 | ssize_t iio_device_attr_read(const struct iio_device *dev, |
| 164 | const char *attr, char *dst, size_t len) |
| 165 | { |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 166 | if (dev->ctx->ops->read_device_attr) |
| 167 | return dev->ctx->ops->read_device_attr(dev, attr, dst, len); |
| 168 | else |
| 169 | return -ENOSYS; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | ssize_t iio_device_attr_write(const struct iio_device *dev, |
| 173 | const char *attr, const char *src) |
| 174 | { |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 175 | if (dev->ctx->ops->write_device_attr) |
| 176 | return dev->ctx->ops->write_device_attr(dev, attr, src); |
| 177 | else |
| 178 | return -ENOSYS; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 179 | } |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 180 | |
| 181 | void free_device(struct iio_device *dev) |
| 182 | { |
| 183 | unsigned int i; |
| 184 | for (i = 0; i < dev->nb_attrs; i++) |
| 185 | free((char *) dev->attrs[i]); |
| 186 | if (dev->nb_attrs) |
| 187 | free(dev->attrs); |
| 188 | for (i = 0; i < dev->nb_channels; i++) |
| 189 | free_channel(dev->channels[i]); |
| 190 | if (dev->nb_channels) |
| 191 | free(dev->channels); |
| 192 | if (dev->name) |
| 193 | free((char *) dev->name); |
| 194 | if (dev->id) |
| 195 | free((char *) dev->id); |
| 196 | free(dev); |
| 197 | } |