IIO Application Library

Dependents:  

Committer:
mahphalke
Date:
Mon Oct 05 12:28:13 2020 +0530
Revision:
1:182a6f54cab0
IIO application library- Initial Revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mahphalke 1:182a6f54cab0 1 /***************************************************************************//**
mahphalke 1:182a6f54cab0 2 * @file iio.c
mahphalke 1:182a6f54cab0 3 * @brief Implementation of iio.
mahphalke 1:182a6f54cab0 4 * This module implements read/write ops, required by libtinyiiod and further
mahphalke 1:182a6f54cab0 5 * calls show/store functions, corresponding to device/channel/attribute.
mahphalke 1:182a6f54cab0 6 * @author Cristian Pop (cristian.pop@analog.com)
mahphalke 1:182a6f54cab0 7 ********************************************************************************
mahphalke 1:182a6f54cab0 8 * Copyright 2019, 2020(c) Analog Devices, Inc.
mahphalke 1:182a6f54cab0 9 *
mahphalke 1:182a6f54cab0 10 * All rights reserved.
mahphalke 1:182a6f54cab0 11 *
mahphalke 1:182a6f54cab0 12 * Redistribution and use in source and binary forms, with or without
mahphalke 1:182a6f54cab0 13 * modification, are permitted provided that the following conditions are met:
mahphalke 1:182a6f54cab0 14 * - Redistributions of source code must retain the above copyright
mahphalke 1:182a6f54cab0 15 * notice, this list of conditions and the following disclaimer.
mahphalke 1:182a6f54cab0 16 * - Redistributions in binary form must reproduce the above copyright
mahphalke 1:182a6f54cab0 17 * notice, this list of conditions and the following disclaimer in
mahphalke 1:182a6f54cab0 18 * the documentation and/or other materials provided with the
mahphalke 1:182a6f54cab0 19 * distribution.
mahphalke 1:182a6f54cab0 20 * - Neither the name of Analog Devices, Inc. nor the names of its
mahphalke 1:182a6f54cab0 21 * contributors may be used to endorse or promote products derived
mahphalke 1:182a6f54cab0 22 * from this software without specific prior written permission.
mahphalke 1:182a6f54cab0 23 * - The use of this software may or may not infringe the patent rights
mahphalke 1:182a6f54cab0 24 * of one or more patent holders. This license does not release you
mahphalke 1:182a6f54cab0 25 * from the requirement that you obtain separate licenses from these
mahphalke 1:182a6f54cab0 26 * patent holders to use this software.
mahphalke 1:182a6f54cab0 27 * - Use of the software either in source or binary form, must be run
mahphalke 1:182a6f54cab0 28 * on or directly connected to an Analog Devices Inc. component.
mahphalke 1:182a6f54cab0 29 *
mahphalke 1:182a6f54cab0 30 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
mahphalke 1:182a6f54cab0 31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
mahphalke 1:182a6f54cab0 32 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
mahphalke 1:182a6f54cab0 33 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
mahphalke 1:182a6f54cab0 34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
mahphalke 1:182a6f54cab0 35 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
mahphalke 1:182a6f54cab0 36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mahphalke 1:182a6f54cab0 37 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mahphalke 1:182a6f54cab0 38 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mahphalke 1:182a6f54cab0 39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mahphalke 1:182a6f54cab0 40 *******************************************************************************/
mahphalke 1:182a6f54cab0 41
mahphalke 1:182a6f54cab0 42 /******************************************************************************/
mahphalke 1:182a6f54cab0 43 /***************************** Include Files **********************************/
mahphalke 1:182a6f54cab0 44 /******************************************************************************/
mahphalke 1:182a6f54cab0 45
mahphalke 1:182a6f54cab0 46 #include "iio.h"
mahphalke 1:182a6f54cab0 47 #include "ctype.h"
mahphalke 1:182a6f54cab0 48 #include "tinyiiod.h"
mahphalke 1:182a6f54cab0 49 #include "util.h"
mahphalke 1:182a6f54cab0 50 #include "error.h"
mahphalke 1:182a6f54cab0 51 #include "errno.h"
mahphalke 1:182a6f54cab0 52 #include "compat.h"
mahphalke 1:182a6f54cab0 53
mahphalke 1:182a6f54cab0 54 /******************************************************************************/
mahphalke 1:182a6f54cab0 55 /*************************** Types Declarations *******************************/
mahphalke 1:182a6f54cab0 56 /******************************************************************************/
mahphalke 1:182a6f54cab0 57
mahphalke 1:182a6f54cab0 58 /**
mahphalke 1:182a6f54cab0 59 * @struct iio_interfaces
mahphalke 1:182a6f54cab0 60 * @brief Structure containing all interfaces.
mahphalke 1:182a6f54cab0 61 */
mahphalke 1:182a6f54cab0 62 struct iio_interfaces {
mahphalke 1:182a6f54cab0 63 /** List containing all interfaces */
mahphalke 1:182a6f54cab0 64 struct iio_interface **interfaces;
mahphalke 1:182a6f54cab0 65 /** Number of Interfaces */
mahphalke 1:182a6f54cab0 66 uint8_t num_interfaces;
mahphalke 1:182a6f54cab0 67 };
mahphalke 1:182a6f54cab0 68
mahphalke 1:182a6f54cab0 69 /**
mahphalke 1:182a6f54cab0 70 * @struct element_info
mahphalke 1:182a6f54cab0 71 * @brief Structure informations about a specific parameter.
mahphalke 1:182a6f54cab0 72 */
mahphalke 1:182a6f54cab0 73 struct element_info {
mahphalke 1:182a6f54cab0 74 /** Device name */
mahphalke 1:182a6f54cab0 75 const char *device_name;
mahphalke 1:182a6f54cab0 76 /** Channel name */
mahphalke 1:182a6f54cab0 77 const char *channel_name;
mahphalke 1:182a6f54cab0 78 /** Attribute name */
mahphalke 1:182a6f54cab0 79 const char *attribute_name;
mahphalke 1:182a6f54cab0 80 /** If set, is an output channel */
mahphalke 1:182a6f54cab0 81 bool ch_out;
mahphalke 1:182a6f54cab0 82 };
mahphalke 1:182a6f54cab0 83
mahphalke 1:182a6f54cab0 84 /**
mahphalke 1:182a6f54cab0 85 * iio_read_attr(), iio_write_attr() functions, they need to know about iio_interfaces
mahphalke 1:182a6f54cab0 86 */
mahphalke 1:182a6f54cab0 87 static struct iio_interfaces *iio_interfaces = NULL;
mahphalke 1:182a6f54cab0 88
mahphalke 1:182a6f54cab0 89 /******************************************************************************/
mahphalke 1:182a6f54cab0 90 /************************ Functions Definitions *******************************/
mahphalke 1:182a6f54cab0 91 /******************************************************************************/
mahphalke 1:182a6f54cab0 92
mahphalke 1:182a6f54cab0 93 /**
mahphalke 1:182a6f54cab0 94 * @brief Get channel number.
mahphalke 1:182a6f54cab0 95 * @param ch - String containing channel name + channel number.
mahphalke 1:182a6f54cab0 96 * @return - Channel number. Ex: for "altvoltage0" return 0, for "voltage2"
mahphalke 1:182a6f54cab0 97 * return 2.
mahphalke 1:182a6f54cab0 98 */
mahphalke 1:182a6f54cab0 99 static int32_t iio_get_channel_number(const char *ch)
mahphalke 1:182a6f54cab0 100 {
mahphalke 1:182a6f54cab0 101 char *p = (char*)ch;
mahphalke 1:182a6f54cab0 102 int32_t ch_num = FAILURE;
mahphalke 1:182a6f54cab0 103
mahphalke 1:182a6f54cab0 104 while (*p) {
mahphalke 1:182a6f54cab0 105 if (isdigit(*p))
mahphalke 1:182a6f54cab0 106 ch_num = strtol(p, &p, 10);
mahphalke 1:182a6f54cab0 107 else
mahphalke 1:182a6f54cab0 108 p++;
mahphalke 1:182a6f54cab0 109 }
mahphalke 1:182a6f54cab0 110
mahphalke 1:182a6f54cab0 111 return ch_num;
mahphalke 1:182a6f54cab0 112 }
mahphalke 1:182a6f54cab0 113
mahphalke 1:182a6f54cab0 114 /**
mahphalke 1:182a6f54cab0 115 * @brief Get channel ID from a list of channels.
mahphalke 1:182a6f54cab0 116 * @param channel - Channel name.
mahphalke 1:182a6f54cab0 117 * @param channels - List of channels.
mahphalke 1:182a6f54cab0 118 * @param ch_out - If "true" is output channel, if "false" is input channel.
mahphalke 1:182a6f54cab0 119 * @return Channel ID, or negative value if attribute is not found.
mahphalke 1:182a6f54cab0 120 */
mahphalke 1:182a6f54cab0 121 static int16_t iio_get_channel_id(const char *channel,
mahphalke 1:182a6f54cab0 122 struct iio_channel **channels, bool ch_out)
mahphalke 1:182a6f54cab0 123 {
mahphalke 1:182a6f54cab0 124 int16_t i = 0;
mahphalke 1:182a6f54cab0 125
mahphalke 1:182a6f54cab0 126 if (!(*channels))
mahphalke 1:182a6f54cab0 127 return -EINVAL;
mahphalke 1:182a6f54cab0 128
mahphalke 1:182a6f54cab0 129 while (channels[i]) {
mahphalke 1:182a6f54cab0 130 if (!strcmp(channel, channels[i]->name) && (channels[i]->ch_out == ch_out))
mahphalke 1:182a6f54cab0 131 return i;
mahphalke 1:182a6f54cab0 132 i++;
mahphalke 1:182a6f54cab0 133 }
mahphalke 1:182a6f54cab0 134
mahphalke 1:182a6f54cab0 135 return -ENOENT;
mahphalke 1:182a6f54cab0 136 }
mahphalke 1:182a6f54cab0 137
mahphalke 1:182a6f54cab0 138 /**
mahphalke 1:182a6f54cab0 139 * @brief Get attribute ID from a list of attributes.
mahphalke 1:182a6f54cab0 140 * @param attr - Attribute name.
mahphalke 1:182a6f54cab0 141 * @param attributes - List of attributes.
mahphalke 1:182a6f54cab0 142 * @return - Attribute ID, or negative value if attribute is not found.
mahphalke 1:182a6f54cab0 143 */
mahphalke 1:182a6f54cab0 144 static int16_t iio_get_attribute_id(const char *attr,
mahphalke 1:182a6f54cab0 145 struct iio_attribute **attributes)
mahphalke 1:182a6f54cab0 146 {
mahphalke 1:182a6f54cab0 147 int16_t i = 0;
mahphalke 1:182a6f54cab0 148
mahphalke 1:182a6f54cab0 149 if (!(*attributes))
mahphalke 1:182a6f54cab0 150 return -EINVAL;
mahphalke 1:182a6f54cab0 151
mahphalke 1:182a6f54cab0 152 while (attributes[i]) {
mahphalke 1:182a6f54cab0 153 if (!strcmp(attr, attributes[i]->name))
mahphalke 1:182a6f54cab0 154 return i;
mahphalke 1:182a6f54cab0 155 i++;
mahphalke 1:182a6f54cab0 156 }
mahphalke 1:182a6f54cab0 157
mahphalke 1:182a6f54cab0 158 return -ENOENT;
mahphalke 1:182a6f54cab0 159 }
mahphalke 1:182a6f54cab0 160
mahphalke 1:182a6f54cab0 161 /**
mahphalke 1:182a6f54cab0 162 * @brief Find interface with "device_name".
mahphalke 1:182a6f54cab0 163 * @param device_name - Device name.
mahphalke 1:182a6f54cab0 164 * @param iio_interfaces - List of interfaces.
mahphalke 1:182a6f54cab0 165 * @return Interface pointer if interface is found, NULL otherwise.
mahphalke 1:182a6f54cab0 166 */
mahphalke 1:182a6f54cab0 167 static struct iio_interface *iio_get_interface(const char *device_name,
mahphalke 1:182a6f54cab0 168 struct iio_interfaces *iio_interfaces)
mahphalke 1:182a6f54cab0 169 {
mahphalke 1:182a6f54cab0 170 int16_t i;
mahphalke 1:182a6f54cab0 171
mahphalke 1:182a6f54cab0 172 if (!iio_interfaces)
mahphalke 1:182a6f54cab0 173 return NULL;
mahphalke 1:182a6f54cab0 174
mahphalke 1:182a6f54cab0 175 for (i = 0; i < iio_interfaces->num_interfaces; i++) {
mahphalke 1:182a6f54cab0 176 if (!strcmp(device_name, iio_interfaces->interfaces[i]->name))
mahphalke 1:182a6f54cab0 177 return iio_interfaces->interfaces[i];
mahphalke 1:182a6f54cab0 178 }
mahphalke 1:182a6f54cab0 179
mahphalke 1:182a6f54cab0 180 return NULL;
mahphalke 1:182a6f54cab0 181 }
mahphalke 1:182a6f54cab0 182
mahphalke 1:182a6f54cab0 183 /**
mahphalke 1:182a6f54cab0 184 * @brief Read all attributes from an attribute list.
mahphalke 1:182a6f54cab0 185 * @param device - Physical instance of a device.
mahphalke 1:182a6f54cab0 186 * @param buf - Buffer where values are read.
mahphalke 1:182a6f54cab0 187 * @param len - Maximum length of value to be stored in buf.
mahphalke 1:182a6f54cab0 188 * @param channel - Channel properties.
mahphalke 1:182a6f54cab0 189 * @param attributes - List of attributes to be read.
mahphalke 1:182a6f54cab0 190 * @return Number of bytes read or negative value in case of error.
mahphalke 1:182a6f54cab0 191 */
mahphalke 1:182a6f54cab0 192 static ssize_t iio_read_all_attr(void *device, char *buf, size_t len,
mahphalke 1:182a6f54cab0 193 const struct iio_ch_info *channel, struct iio_attribute **attributes)
mahphalke 1:182a6f54cab0 194 {
mahphalke 1:182a6f54cab0 195 int16_t i = 0, j = 0;
mahphalke 1:182a6f54cab0 196 char local_buf[256];
mahphalke 1:182a6f54cab0 197 ssize_t attr_length;
mahphalke 1:182a6f54cab0 198 uint32_t *pattr_length;
mahphalke 1:182a6f54cab0 199
mahphalke 1:182a6f54cab0 200 if (!attributes)
mahphalke 1:182a6f54cab0 201 return FAILURE;
mahphalke 1:182a6f54cab0 202
mahphalke 1:182a6f54cab0 203 if (!buf)
mahphalke 1:182a6f54cab0 204 return FAILURE;
mahphalke 1:182a6f54cab0 205
mahphalke 1:182a6f54cab0 206 while (attributes[i]) {
mahphalke 1:182a6f54cab0 207 attr_length = attributes[i]->show(device, local_buf, len, channel);
mahphalke 1:182a6f54cab0 208 pattr_length = (uint32_t *)(buf + j);
mahphalke 1:182a6f54cab0 209 *pattr_length = bswap_constant_32(attr_length);
mahphalke 1:182a6f54cab0 210 j += 4;
mahphalke 1:182a6f54cab0 211 if (attr_length >= 0) {
mahphalke 1:182a6f54cab0 212 sprintf(buf + j, "%s", local_buf);
mahphalke 1:182a6f54cab0 213 if (attr_length & 0x3) /* multiple of 4 */
mahphalke 1:182a6f54cab0 214 attr_length = ((attr_length >> 2) + 1) << 2;
mahphalke 1:182a6f54cab0 215 j += attr_length;
mahphalke 1:182a6f54cab0 216 }
mahphalke 1:182a6f54cab0 217 i++;
mahphalke 1:182a6f54cab0 218 }
mahphalke 1:182a6f54cab0 219
mahphalke 1:182a6f54cab0 220 return j;
mahphalke 1:182a6f54cab0 221 }
mahphalke 1:182a6f54cab0 222
mahphalke 1:182a6f54cab0 223 /**
mahphalke 1:182a6f54cab0 224 * @brief Write all attributes from an attribute list.
mahphalke 1:182a6f54cab0 225 * @param device - Physical instance of a device.
mahphalke 1:182a6f54cab0 226 * @param buf - Values to be written.
mahphalke 1:182a6f54cab0 227 * @param len - Length of buf.
mahphalke 1:182a6f54cab0 228 * @param channel - Channel properties.
mahphalke 1:182a6f54cab0 229 * @param attributes - List of attributes to be written.
mahphalke 1:182a6f54cab0 230 * @return Number of written bytes or negative value in case of error.
mahphalke 1:182a6f54cab0 231 */
mahphalke 1:182a6f54cab0 232 static ssize_t iio_write_all_attr(void *device, char *buf, size_t len,
mahphalke 1:182a6f54cab0 233 const struct iio_ch_info *channel, struct iio_attribute **attributes)
mahphalke 1:182a6f54cab0 234 {
mahphalke 1:182a6f54cab0 235 int16_t i = 0, j = 0;
mahphalke 1:182a6f54cab0 236 int16_t attr_length;
mahphalke 1:182a6f54cab0 237
mahphalke 1:182a6f54cab0 238 if (!attributes)
mahphalke 1:182a6f54cab0 239 return FAILURE;
mahphalke 1:182a6f54cab0 240
mahphalke 1:182a6f54cab0 241 if (!buf)
mahphalke 1:182a6f54cab0 242 return FAILURE;
mahphalke 1:182a6f54cab0 243
mahphalke 1:182a6f54cab0 244 while (attributes[i]) {
mahphalke 1:182a6f54cab0 245 attr_length = bswap_constant_32((uint32_t)(buf + j));
mahphalke 1:182a6f54cab0 246 j += 4;
mahphalke 1:182a6f54cab0 247 attributes[i]->store(device, (buf + j), attr_length, channel);
mahphalke 1:182a6f54cab0 248 j += attr_length;
mahphalke 1:182a6f54cab0 249 if (j & 0x3)
mahphalke 1:182a6f54cab0 250 j = ((j >> 2) + 1) << 2;
mahphalke 1:182a6f54cab0 251 i++;
mahphalke 1:182a6f54cab0 252 }
mahphalke 1:182a6f54cab0 253
mahphalke 1:182a6f54cab0 254 return len;
mahphalke 1:182a6f54cab0 255 }
mahphalke 1:182a6f54cab0 256
mahphalke 1:182a6f54cab0 257 /**
mahphalke 1:182a6f54cab0 258 * @brief Read/write channel attribute.
mahphalke 1:182a6f54cab0 259 * @param el_info - Structure describing element to be written.
mahphalke 1:182a6f54cab0 260 * @param buf - Read/write value.
mahphalke 1:182a6f54cab0 261 * @param len - Length of data in "buf" parameter.
mahphalke 1:182a6f54cab0 262 * @param channel - Structure describing channel attributes.
mahphalke 1:182a6f54cab0 263 * @param is_write -If it has value "1", writes attribute, otherwise reads
mahphalke 1:182a6f54cab0 264 * attribute.
mahphalke 1:182a6f54cab0 265 * @return Length of chars written/read or negative value in case of error.
mahphalke 1:182a6f54cab0 266 */
mahphalke 1:182a6f54cab0 267 static ssize_t iio_rd_wr_channel_attribute(struct element_info *el_info,
mahphalke 1:182a6f54cab0 268 char *buf, size_t len,
mahphalke 1:182a6f54cab0 269 struct iio_channel *channel, bool is_write)
mahphalke 1:182a6f54cab0 270 {
mahphalke 1:182a6f54cab0 271 int16_t attribute_id;
mahphalke 1:182a6f54cab0 272 struct iio_interface *iface = iio_get_interface(el_info->device_name,
mahphalke 1:182a6f54cab0 273 iio_interfaces);
mahphalke 1:182a6f54cab0 274 const struct iio_ch_info channel_info = {
mahphalke 1:182a6f54cab0 275 iio_get_channel_number(el_info->channel_name),
mahphalke 1:182a6f54cab0 276 el_info->ch_out
mahphalke 1:182a6f54cab0 277 };
mahphalke 1:182a6f54cab0 278
mahphalke 1:182a6f54cab0 279 if (!strcmp(el_info->attribute_name, "")) {
mahphalke 1:182a6f54cab0 280 /* read / write all channel attributes */
mahphalke 1:182a6f54cab0 281 if (is_write)
mahphalke 1:182a6f54cab0 282 return iio_write_all_attr(iface->dev_instance, buf, len, &channel_info,
mahphalke 1:182a6f54cab0 283 channel->attributes);
mahphalke 1:182a6f54cab0 284 else
mahphalke 1:182a6f54cab0 285 return iio_read_all_attr(iface->dev_instance, buf, len, &channel_info,
mahphalke 1:182a6f54cab0 286 channel->attributes);
mahphalke 1:182a6f54cab0 287 } else {
mahphalke 1:182a6f54cab0 288 /* read / write single channel attribute, if attribute found */
mahphalke 1:182a6f54cab0 289 attribute_id = iio_get_attribute_id(el_info->attribute_name,
mahphalke 1:182a6f54cab0 290 channel->attributes);
mahphalke 1:182a6f54cab0 291 if (attribute_id >= 0) {
mahphalke 1:182a6f54cab0 292 if (is_write)
mahphalke 1:182a6f54cab0 293 return channel->attributes[attribute_id]->store(iface->dev_instance,
mahphalke 1:182a6f54cab0 294 (char*)buf, len, &channel_info);
mahphalke 1:182a6f54cab0 295 else
mahphalke 1:182a6f54cab0 296 return channel->attributes[attribute_id]->show(iface->dev_instance, (char*)buf,
mahphalke 1:182a6f54cab0 297 len, &channel_info);
mahphalke 1:182a6f54cab0 298 }
mahphalke 1:182a6f54cab0 299 }
mahphalke 1:182a6f54cab0 300
mahphalke 1:182a6f54cab0 301 return -ENOENT;
mahphalke 1:182a6f54cab0 302 }
mahphalke 1:182a6f54cab0 303
mahphalke 1:182a6f54cab0 304 /**
mahphalke 1:182a6f54cab0 305 * @brief Read/write attribute.
mahphalke 1:182a6f54cab0 306 * @param el_info - Structure describing element to be written.
mahphalke 1:182a6f54cab0 307 * @param buf - Read/write value.
mahphalke 1:182a6f54cab0 308 * @param len - Length of data in "buf" parameter.
mahphalke 1:182a6f54cab0 309 * @param iio_device - Physical instance of a device.
mahphalke 1:182a6f54cab0 310 * @param is_write -If it has value "1", writes attribute, otherwise reads
mahphalke 1:182a6f54cab0 311 * attribute.
mahphalke 1:182a6f54cab0 312 * @return Length of chars written/read or negative value in case of error.
mahphalke 1:182a6f54cab0 313 */
mahphalke 1:182a6f54cab0 314 static ssize_t iio_rd_wr_attribute(struct element_info *el_info, char *buf,
mahphalke 1:182a6f54cab0 315 size_t len,
mahphalke 1:182a6f54cab0 316 struct iio_device *iio_device, bool is_write)
mahphalke 1:182a6f54cab0 317 {
mahphalke 1:182a6f54cab0 318 int16_t channel_id;
mahphalke 1:182a6f54cab0 319 int16_t attribute_id;
mahphalke 1:182a6f54cab0 320 struct iio_interface *iface;
mahphalke 1:182a6f54cab0 321
mahphalke 1:182a6f54cab0 322 if (!iio_device)
mahphalke 1:182a6f54cab0 323 return -ENOENT;
mahphalke 1:182a6f54cab0 324
mahphalke 1:182a6f54cab0 325 if (!strcmp(el_info->channel_name, "")) {
mahphalke 1:182a6f54cab0 326 /* it is attribute of a device */
mahphalke 1:182a6f54cab0 327 iface = iio_get_interface(el_info->device_name, iio_interfaces);
mahphalke 1:182a6f54cab0 328
mahphalke 1:182a6f54cab0 329 if (!strcmp(el_info->attribute_name, "")) {
mahphalke 1:182a6f54cab0 330 /* read / write all device attributes */
mahphalke 1:182a6f54cab0 331 if (is_write)
mahphalke 1:182a6f54cab0 332 return iio_write_all_attr(iface->dev_instance, buf, len, NULL,
mahphalke 1:182a6f54cab0 333 iio_device->attributes);
mahphalke 1:182a6f54cab0 334 else
mahphalke 1:182a6f54cab0 335 return iio_read_all_attr(iface->dev_instance, buf, len, NULL,
mahphalke 1:182a6f54cab0 336 iio_device->attributes);
mahphalke 1:182a6f54cab0 337 } else {
mahphalke 1:182a6f54cab0 338 /* read / write single device attribute, if attribute found */
mahphalke 1:182a6f54cab0 339 attribute_id = iio_get_attribute_id(el_info->attribute_name,
mahphalke 1:182a6f54cab0 340 iio_device->attributes);
mahphalke 1:182a6f54cab0 341 if (attribute_id < 0)
mahphalke 1:182a6f54cab0 342 return -ENOENT;
mahphalke 1:182a6f54cab0 343 if (is_write)
mahphalke 1:182a6f54cab0 344 return iio_device->attributes[attribute_id]->store(iface->dev_instance,
mahphalke 1:182a6f54cab0 345 (char*)buf, len, NULL);
mahphalke 1:182a6f54cab0 346 else
mahphalke 1:182a6f54cab0 347 return iio_device->attributes[attribute_id]->show(iface->dev_instance,
mahphalke 1:182a6f54cab0 348 (char*)buf, len, NULL);
mahphalke 1:182a6f54cab0 349 }
mahphalke 1:182a6f54cab0 350 } else {
mahphalke 1:182a6f54cab0 351 /* it is attribute of a channel */
mahphalke 1:182a6f54cab0 352 channel_id = iio_get_channel_id(el_info->channel_name, iio_device->channels,
mahphalke 1:182a6f54cab0 353 el_info->ch_out);
mahphalke 1:182a6f54cab0 354 return iio_rd_wr_channel_attribute(el_info, buf, len,
mahphalke 1:182a6f54cab0 355 iio_device->channels[channel_id], is_write);
mahphalke 1:182a6f54cab0 356 }
mahphalke 1:182a6f54cab0 357
mahphalke 1:182a6f54cab0 358 return -ENOENT;
mahphalke 1:182a6f54cab0 359 }
mahphalke 1:182a6f54cab0 360
mahphalke 1:182a6f54cab0 361 /**
mahphalke 1:182a6f54cab0 362 * @brief Check if device is supported.
mahphalke 1:182a6f54cab0 363 * @param device - Device name.
mahphalke 1:182a6f54cab0 364 * @return TRUE if device is supported, FALSE otherwise.
mahphalke 1:182a6f54cab0 365 */
mahphalke 1:182a6f54cab0 366 static bool iio_supported_dev(const char *device)
mahphalke 1:182a6f54cab0 367 {
mahphalke 1:182a6f54cab0 368 return (NULL != iio_get_interface(device, iio_interfaces));
mahphalke 1:182a6f54cab0 369 }
mahphalke 1:182a6f54cab0 370
mahphalke 1:182a6f54cab0 371 /**
mahphalke 1:182a6f54cab0 372 * @brief Read global attribute of a device.
mahphalke 1:182a6f54cab0 373 * @param device - String containing device name.
mahphalke 1:182a6f54cab0 374 * @param attr - String containing attribute name.
mahphalke 1:182a6f54cab0 375 * @param buf - Buffer where value is read.
mahphalke 1:182a6f54cab0 376 * @param len - Maximum length of value to be stored in buf.
mahphalke 1:182a6f54cab0 377 * @param debug - Read raw value if set.
mahphalke 1:182a6f54cab0 378 * @return Number of bytes read.
mahphalke 1:182a6f54cab0 379 */
mahphalke 1:182a6f54cab0 380 static ssize_t iio_read_attr(const char *device, const char *attr, char *buf,
mahphalke 1:182a6f54cab0 381 size_t len, bool debug)
mahphalke 1:182a6f54cab0 382 {
mahphalke 1:182a6f54cab0 383 struct iio_interface *iio_device;
mahphalke 1:182a6f54cab0 384 struct element_info el_info;
mahphalke 1:182a6f54cab0 385
mahphalke 1:182a6f54cab0 386 if (!iio_supported_dev(device))
mahphalke 1:182a6f54cab0 387 return FAILURE;
mahphalke 1:182a6f54cab0 388
mahphalke 1:182a6f54cab0 389 el_info.device_name = device;
mahphalke 1:182a6f54cab0 390 el_info.channel_name = ""; /* there is no channel here */
mahphalke 1:182a6f54cab0 391 el_info.attribute_name = attr;
mahphalke 1:182a6f54cab0 392
mahphalke 1:182a6f54cab0 393 iio_device = iio_get_interface(device, iio_interfaces);
mahphalke 1:182a6f54cab0 394 if (!iio_device)
mahphalke 1:182a6f54cab0 395 return FAILURE;
mahphalke 1:182a6f54cab0 396
mahphalke 1:182a6f54cab0 397 return iio_rd_wr_attribute(&el_info, buf, len, iio_device->iio, 0);
mahphalke 1:182a6f54cab0 398 }
mahphalke 1:182a6f54cab0 399
mahphalke 1:182a6f54cab0 400 /**
mahphalke 1:182a6f54cab0 401 * @brief Write global attribute of a device.
mahphalke 1:182a6f54cab0 402 * @param device - String containing device name.
mahphalke 1:182a6f54cab0 403 * @param attr - String containing attribute name.
mahphalke 1:182a6f54cab0 404 * @param buf - Value to be written.
mahphalke 1:182a6f54cab0 405 * @param len - Length of data.
mahphalke 1:182a6f54cab0 406 * @param debug - Write raw value if set.
mahphalke 1:182a6f54cab0 407 * @return Number of written bytes.
mahphalke 1:182a6f54cab0 408 */
mahphalke 1:182a6f54cab0 409 static ssize_t iio_write_attr(const char *device, const char *attr,
mahphalke 1:182a6f54cab0 410 const char *buf,
mahphalke 1:182a6f54cab0 411 size_t len, bool debug)
mahphalke 1:182a6f54cab0 412 {
mahphalke 1:182a6f54cab0 413 struct element_info el_info;
mahphalke 1:182a6f54cab0 414 struct iio_interface *iio_interface;
mahphalke 1:182a6f54cab0 415
mahphalke 1:182a6f54cab0 416 if (!iio_supported_dev(device))
mahphalke 1:182a6f54cab0 417 return -ENODEV;
mahphalke 1:182a6f54cab0 418
mahphalke 1:182a6f54cab0 419 el_info.device_name = device;
mahphalke 1:182a6f54cab0 420 el_info.channel_name = ""; /* there is no channel here */
mahphalke 1:182a6f54cab0 421 el_info.attribute_name = attr;
mahphalke 1:182a6f54cab0 422
mahphalke 1:182a6f54cab0 423 iio_interface = iio_get_interface(device, iio_interfaces);
mahphalke 1:182a6f54cab0 424 if (!iio_interface)
mahphalke 1:182a6f54cab0 425 return FAILURE;
mahphalke 1:182a6f54cab0 426
mahphalke 1:182a6f54cab0 427 return iio_rd_wr_attribute(&el_info, (char*)buf, len, iio_interface->iio, 1);
mahphalke 1:182a6f54cab0 428 }
mahphalke 1:182a6f54cab0 429
mahphalke 1:182a6f54cab0 430 /**
mahphalke 1:182a6f54cab0 431 * @brief Read channel attribute.
mahphalke 1:182a6f54cab0 432 * @param device - String containing device name.
mahphalke 1:182a6f54cab0 433 * @param channel - String containing channel name.
mahphalke 1:182a6f54cab0 434 * @param ch_out -Channel type input/output.
mahphalke 1:182a6f54cab0 435 * @param attr - String containing attribute name.
mahphalke 1:182a6f54cab0 436 * @param buf - Buffer where value is stored.
mahphalke 1:182a6f54cab0 437 * @param len - Maximum length of value to be stored in buf.
mahphalke 1:182a6f54cab0 438 * @return - Number of bytes read.
mahphalke 1:182a6f54cab0 439 */
mahphalke 1:182a6f54cab0 440 static ssize_t iio_ch_read_attr(const char *device, const char *channel,
mahphalke 1:182a6f54cab0 441 bool ch_out, const char *attr, char *buf, size_t len)
mahphalke 1:182a6f54cab0 442 {
mahphalke 1:182a6f54cab0 443 struct element_info el_info;
mahphalke 1:182a6f54cab0 444 struct iio_interface *iio_interface;
mahphalke 1:182a6f54cab0 445
mahphalke 1:182a6f54cab0 446 if (!iio_supported_dev(device))
mahphalke 1:182a6f54cab0 447 return FAILURE;
mahphalke 1:182a6f54cab0 448
mahphalke 1:182a6f54cab0 449 el_info.device_name = device;
mahphalke 1:182a6f54cab0 450 el_info.channel_name = channel;
mahphalke 1:182a6f54cab0 451 el_info.attribute_name = attr;
mahphalke 1:182a6f54cab0 452 el_info.ch_out = ch_out;
mahphalke 1:182a6f54cab0 453
mahphalke 1:182a6f54cab0 454 iio_interface = iio_get_interface(device, iio_interfaces);
mahphalke 1:182a6f54cab0 455 if (!device)
mahphalke 1:182a6f54cab0 456 return FAILURE;
mahphalke 1:182a6f54cab0 457
mahphalke 1:182a6f54cab0 458 return iio_rd_wr_attribute(&el_info, buf, len, iio_interface->iio, 0);
mahphalke 1:182a6f54cab0 459 }
mahphalke 1:182a6f54cab0 460
mahphalke 1:182a6f54cab0 461 /**
mahphalke 1:182a6f54cab0 462 * @brief Write channel attribute.
mahphalke 1:182a6f54cab0 463 * @param device - String containing device name.
mahphalke 1:182a6f54cab0 464 * @param channel - String containing channel name.
mahphalke 1:182a6f54cab0 465 * @param ch_out - Channel type input/output.
mahphalke 1:182a6f54cab0 466 * @param attr - String containing attribute name.
mahphalke 1:182a6f54cab0 467 * @param buf - Value to be written.
mahphalke 1:182a6f54cab0 468 * @param len - Length of data in "buf" parameter.
mahphalke 1:182a6f54cab0 469 * @return Number of written bytes.
mahphalke 1:182a6f54cab0 470 */
mahphalke 1:182a6f54cab0 471 static ssize_t iio_ch_write_attr(const char *device, const char *channel,
mahphalke 1:182a6f54cab0 472 bool ch_out, const char *attr, const char *buf, size_t len)
mahphalke 1:182a6f54cab0 473 {
mahphalke 1:182a6f54cab0 474 struct element_info el_info;
mahphalke 1:182a6f54cab0 475 struct iio_interface *iio_interface;
mahphalke 1:182a6f54cab0 476
mahphalke 1:182a6f54cab0 477 if (!iio_supported_dev(device))
mahphalke 1:182a6f54cab0 478 return -ENODEV;
mahphalke 1:182a6f54cab0 479
mahphalke 1:182a6f54cab0 480 el_info.device_name = device;
mahphalke 1:182a6f54cab0 481 el_info.channel_name = channel;
mahphalke 1:182a6f54cab0 482 el_info.attribute_name = attr;
mahphalke 1:182a6f54cab0 483 el_info.ch_out = ch_out;
mahphalke 1:182a6f54cab0 484 iio_interface = iio_get_interface(device, iio_interfaces);
mahphalke 1:182a6f54cab0 485
mahphalke 1:182a6f54cab0 486 if (!iio_interface)
mahphalke 1:182a6f54cab0 487 return -ENOENT;
mahphalke 1:182a6f54cab0 488
mahphalke 1:182a6f54cab0 489 return iio_rd_wr_attribute(&el_info, (char*)buf, len, iio_interface->iio, 1);
mahphalke 1:182a6f54cab0 490 }
mahphalke 1:182a6f54cab0 491
mahphalke 1:182a6f54cab0 492 /**
mahphalke 1:182a6f54cab0 493 * @brief Open device.
mahphalke 1:182a6f54cab0 494 * @param device - String containing device name.
mahphalke 1:182a6f54cab0 495 * @param sample_size - Sample size.
mahphalke 1:182a6f54cab0 496 * @param mask - Channels to be opened.
mahphalke 1:182a6f54cab0 497 * @return SUCCESS, negative value in case of failure.
mahphalke 1:182a6f54cab0 498 */
mahphalke 1:182a6f54cab0 499 static int32_t iio_open_dev(const char *device, size_t sample_size,
mahphalke 1:182a6f54cab0 500 uint32_t mask)
mahphalke 1:182a6f54cab0 501 {
mahphalke 1:182a6f54cab0 502 struct iio_interface *iface;
mahphalke 1:182a6f54cab0 503 uint32_t ch_mask;
mahphalke 1:182a6f54cab0 504
mahphalke 1:182a6f54cab0 505 if (!iio_supported_dev(device))
mahphalke 1:182a6f54cab0 506 return -ENODEV;
mahphalke 1:182a6f54cab0 507
mahphalke 1:182a6f54cab0 508 iface = iio_get_interface(device, iio_interfaces);
mahphalke 1:182a6f54cab0 509 ch_mask = 0xFFFFFFFF >> (32 - iface->iio->num_ch);
mahphalke 1:182a6f54cab0 510
mahphalke 1:182a6f54cab0 511 if (mask & ~ch_mask)
mahphalke 1:182a6f54cab0 512 return -ENOENT;
mahphalke 1:182a6f54cab0 513
mahphalke 1:182a6f54cab0 514 iface->ch_mask = mask;
mahphalke 1:182a6f54cab0 515
mahphalke 1:182a6f54cab0 516 return SUCCESS;
mahphalke 1:182a6f54cab0 517 }
mahphalke 1:182a6f54cab0 518
mahphalke 1:182a6f54cab0 519 /**
mahphalke 1:182a6f54cab0 520 * @brief Close device.
mahphalke 1:182a6f54cab0 521 * @param device - String containing device name.
mahphalke 1:182a6f54cab0 522 * @return SUCCESS, negative value in case of failure.
mahphalke 1:182a6f54cab0 523 */
mahphalke 1:182a6f54cab0 524 static int32_t iio_close_dev(const char *device)
mahphalke 1:182a6f54cab0 525 {
mahphalke 1:182a6f54cab0 526 struct iio_interface *iface;
mahphalke 1:182a6f54cab0 527
mahphalke 1:182a6f54cab0 528 if (!iio_supported_dev(device))
mahphalke 1:182a6f54cab0 529 return FAILURE;
mahphalke 1:182a6f54cab0 530 iface = iio_get_interface(device, iio_interfaces);
mahphalke 1:182a6f54cab0 531 iface->ch_mask = 0;
mahphalke 1:182a6f54cab0 532
mahphalke 1:182a6f54cab0 533 return SUCCESS;
mahphalke 1:182a6f54cab0 534 }
mahphalke 1:182a6f54cab0 535
mahphalke 1:182a6f54cab0 536 /**
mahphalke 1:182a6f54cab0 537 * @brief Get device mask, this specifies the channels that are used.
mahphalke 1:182a6f54cab0 538 * @param device - String containing device name.
mahphalke 1:182a6f54cab0 539 * @param mask - Channels that are opened.
mahphalke 1:182a6f54cab0 540 * @return SUCCESS, negative value in case of failure.
mahphalke 1:182a6f54cab0 541 */
mahphalke 1:182a6f54cab0 542 static int32_t iio_get_mask(const char *device, uint32_t *mask)
mahphalke 1:182a6f54cab0 543 {
mahphalke 1:182a6f54cab0 544 struct iio_interface *iface;
mahphalke 1:182a6f54cab0 545
mahphalke 1:182a6f54cab0 546 if (!iio_supported_dev(device))
mahphalke 1:182a6f54cab0 547 return -ENODEV;
mahphalke 1:182a6f54cab0 548
mahphalke 1:182a6f54cab0 549 iface = iio_get_interface(device, iio_interfaces);
mahphalke 1:182a6f54cab0 550 *mask = iface->ch_mask;
mahphalke 1:182a6f54cab0 551
mahphalke 1:182a6f54cab0 552 return SUCCESS;
mahphalke 1:182a6f54cab0 553 }
mahphalke 1:182a6f54cab0 554
mahphalke 1:182a6f54cab0 555 /**
mahphalke 1:182a6f54cab0 556 * @brief Transfer data from device into RAM.
mahphalke 1:182a6f54cab0 557 * @param device - String containing device name.
mahphalke 1:182a6f54cab0 558 * @param bytes_count - Number of bytes.
mahphalke 1:182a6f54cab0 559 * @return Bytes_count or negative value in case of error.
mahphalke 1:182a6f54cab0 560 */
mahphalke 1:182a6f54cab0 561 static ssize_t iio_transfer_dev_to_mem(const char *device, size_t bytes_count)
mahphalke 1:182a6f54cab0 562 {
mahphalke 1:182a6f54cab0 563 struct iio_interface *iio_interface = iio_get_interface(device, iio_interfaces);
mahphalke 1:182a6f54cab0 564
mahphalke 1:182a6f54cab0 565 if (iio_interface->transfer_dev_to_mem)
mahphalke 1:182a6f54cab0 566 return iio_interface->transfer_dev_to_mem(iio_interface->dev_instance,
mahphalke 1:182a6f54cab0 567 bytes_count, iio_interface->ch_mask);
mahphalke 1:182a6f54cab0 568
mahphalke 1:182a6f54cab0 569 return -ENOENT;
mahphalke 1:182a6f54cab0 570 }
mahphalke 1:182a6f54cab0 571
mahphalke 1:182a6f54cab0 572 /**
mahphalke 1:182a6f54cab0 573 * @brief Read chunk of data from RAM to pbuf. Call
mahphalke 1:182a6f54cab0 574 * "iio_transfer_dev_to_mem()" first.
mahphalke 1:182a6f54cab0 575 * This function is probably called multiple times by libtinyiiod after a
mahphalke 1:182a6f54cab0 576 * "iio_transfer_dev_to_mem" call, since we can only read "bytes_count" bytes.
mahphalke 1:182a6f54cab0 577 * @param device - String containing device name.
mahphalke 1:182a6f54cab0 578 * @param pbuf - Buffer where value is stored.
mahphalke 1:182a6f54cab0 579 * @param offset - Offset to the remaining data after reading n chunks.
mahphalke 1:182a6f54cab0 580 * @param bytes_count - Number of bytes to read.
mahphalke 1:182a6f54cab0 581 * @return: Bytes_count or negative value in case of error.
mahphalke 1:182a6f54cab0 582 */
mahphalke 1:182a6f54cab0 583 static ssize_t iio_read_dev(const char *device, char *pbuf, size_t offset,
mahphalke 1:182a6f54cab0 584 size_t bytes_count)
mahphalke 1:182a6f54cab0 585 {
mahphalke 1:182a6f54cab0 586 struct iio_interface *iio_interface = iio_get_interface(device, iio_interfaces);
mahphalke 1:182a6f54cab0 587
mahphalke 1:182a6f54cab0 588 if (iio_interface->read_data)
mahphalke 1:182a6f54cab0 589 return iio_interface->read_data(iio_interface->dev_instance, pbuf, offset,
mahphalke 1:182a6f54cab0 590 bytes_count, iio_interface->ch_mask);
mahphalke 1:182a6f54cab0 591
mahphalke 1:182a6f54cab0 592 return -ENOENT;
mahphalke 1:182a6f54cab0 593 }
mahphalke 1:182a6f54cab0 594
mahphalke 1:182a6f54cab0 595 /**
mahphalke 1:182a6f54cab0 596 * @brief Transfer memory to device.
mahphalke 1:182a6f54cab0 597 * @param device - String containing device name.
mahphalke 1:182a6f54cab0 598 * @param bytes_count - Number of bytes to transfer.
mahphalke 1:182a6f54cab0 599 * @return Bytes_count or negative value in case of error.
mahphalke 1:182a6f54cab0 600 */
mahphalke 1:182a6f54cab0 601 static ssize_t iio_transfer_mem_to_dev(const char *device, size_t bytes_count)
mahphalke 1:182a6f54cab0 602 {
mahphalke 1:182a6f54cab0 603 struct iio_interface *iio_interface = iio_get_interface(device, iio_interfaces);
mahphalke 1:182a6f54cab0 604
mahphalke 1:182a6f54cab0 605 if (iio_interface->transfer_mem_to_dev)
mahphalke 1:182a6f54cab0 606 return iio_interface->transfer_mem_to_dev(iio_interface->dev_instance,
mahphalke 1:182a6f54cab0 607 bytes_count, iio_interface->ch_mask);
mahphalke 1:182a6f54cab0 608
mahphalke 1:182a6f54cab0 609 return -ENOENT;
mahphalke 1:182a6f54cab0 610 }
mahphalke 1:182a6f54cab0 611
mahphalke 1:182a6f54cab0 612 /**
mahphalke 1:182a6f54cab0 613 * @brief Write chunk of data into RAM.
mahphalke 1:182a6f54cab0 614 * This function is probably called multiple times by libtinyiiod before a
mahphalke 1:182a6f54cab0 615 * "iio_transfer_mem_to_dev" call, since we can only write "bytes_count" bytes
mahphalke 1:182a6f54cab0 616 * at a time.
mahphalke 1:182a6f54cab0 617 * @param device - String containing device name.
mahphalke 1:182a6f54cab0 618 * @param buf - Values to write.
mahphalke 1:182a6f54cab0 619 * @param offset - Offset in memory after the nth chunk of data.
mahphalke 1:182a6f54cab0 620 * @param bytes_count - Number of bytes to write.
mahphalke 1:182a6f54cab0 621 * @return Bytes_count or negative value in case of error.
mahphalke 1:182a6f54cab0 622 */
mahphalke 1:182a6f54cab0 623 static ssize_t iio_write_dev(const char *device, const char *buf,
mahphalke 1:182a6f54cab0 624 size_t offset, size_t bytes_count)
mahphalke 1:182a6f54cab0 625 {
mahphalke 1:182a6f54cab0 626 struct iio_interface *iio_interface = iio_get_interface(device, iio_interfaces);
mahphalke 1:182a6f54cab0 627 if(iio_interface->write_data)
mahphalke 1:182a6f54cab0 628 return iio_interface->write_data(iio_interface->dev_instance, (char*)buf,
mahphalke 1:182a6f54cab0 629 offset, bytes_count, iio_interface->ch_mask);
mahphalke 1:182a6f54cab0 630
mahphalke 1:182a6f54cab0 631 return -ENOENT;
mahphalke 1:182a6f54cab0 632 }
mahphalke 1:182a6f54cab0 633
mahphalke 1:182a6f54cab0 634 /**
mahphalke 1:182a6f54cab0 635 * @brief Get a merged xml containing all devices.
mahphalke 1:182a6f54cab0 636 * @param outxml - Generated xml.
mahphalke 1:182a6f54cab0 637 * @return SUCCESS in case of success or negative value otherwise.
mahphalke 1:182a6f54cab0 638 */
mahphalke 1:182a6f54cab0 639 static ssize_t iio_get_xml(char **outxml)
mahphalke 1:182a6f54cab0 640 {
mahphalke 1:182a6f54cab0 641 char *xml, *tmp_xml, *tmp_xml2;
mahphalke 1:182a6f54cab0 642 uint32_t length;
mahphalke 1:182a6f54cab0 643 uint16_t i;
mahphalke 1:182a6f54cab0 644 ssize_t ret;
mahphalke 1:182a6f54cab0 645
mahphalke 1:182a6f54cab0 646 char header[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
mahphalke 1:182a6f54cab0 647 "<!DOCTYPE context ["
mahphalke 1:182a6f54cab0 648 "<!ELEMENT context (device | context-attribute)*>"
mahphalke 1:182a6f54cab0 649 "<!ELEMENT context-attribute EMPTY>"
mahphalke 1:182a6f54cab0 650 "<!ELEMENT device (channel | attribute | debug-attribute | buffer-attribute)*>"
mahphalke 1:182a6f54cab0 651 "<!ELEMENT channel (scan-element?, attribute*)>"
mahphalke 1:182a6f54cab0 652 "<!ELEMENT attribute EMPTY>"
mahphalke 1:182a6f54cab0 653 "<!ELEMENT scan-element EMPTY>"
mahphalke 1:182a6f54cab0 654 "<!ELEMENT debug-attribute EMPTY>"
mahphalke 1:182a6f54cab0 655 "<!ELEMENT buffer-attribute EMPTY>"
mahphalke 1:182a6f54cab0 656 "<!ATTLIST context name CDATA #REQUIRED description CDATA #IMPLIED>"
mahphalke 1:182a6f54cab0 657 "<!ATTLIST context-attribute name CDATA #REQUIRED value CDATA #REQUIRED>"
mahphalke 1:182a6f54cab0 658 "<!ATTLIST device id CDATA #REQUIRED name CDATA #IMPLIED>"
mahphalke 1:182a6f54cab0 659 "<!ATTLIST channel id CDATA #REQUIRED type (input|output) #REQUIRED name CDATA #IMPLIED>"
mahphalke 1:182a6f54cab0 660 "<!ATTLIST scan-element index CDATA #REQUIRED format CDATA #REQUIRED scale CDATA #IMPLIED>"
mahphalke 1:182a6f54cab0 661 "<!ATTLIST attribute name CDATA #REQUIRED filename CDATA #IMPLIED>"
mahphalke 1:182a6f54cab0 662 "<!ATTLIST debug-attribute name CDATA #REQUIRED>"
mahphalke 1:182a6f54cab0 663 "<!ATTLIST buffer-attribute name CDATA #REQUIRED>"
mahphalke 1:182a6f54cab0 664 "]>"
mahphalke 1:182a6f54cab0 665 "<context name=\"xml\" description=\"no-OS analog 1.1.0-g0000000 #1 Tue Nov 26 09:52:32 IST 2019 armv7l\" >"
mahphalke 1:182a6f54cab0 666 "<context-attribute name=\"no-OS\" value=\"1.1.0-g0000000\" />";
mahphalke 1:182a6f54cab0 667 char header_end[] = "</context>";
mahphalke 1:182a6f54cab0 668
mahphalke 1:182a6f54cab0 669 if (!outxml)
mahphalke 1:182a6f54cab0 670 return FAILURE;
mahphalke 1:182a6f54cab0 671
mahphalke 1:182a6f54cab0 672 xml = (char *)calloc(1, strlen(header) + 1);
mahphalke 1:182a6f54cab0 673 if (!xml)
mahphalke 1:182a6f54cab0 674 return FAILURE;
mahphalke 1:182a6f54cab0 675
mahphalke 1:182a6f54cab0 676 strcpy(xml, header);
mahphalke 1:182a6f54cab0 677 for (i = 0; i < iio_interfaces->num_interfaces; i++) {
mahphalke 1:182a6f54cab0 678 ret = iio_interfaces->interfaces[i]->get_xml(&tmp_xml,
mahphalke 1:182a6f54cab0 679 iio_interfaces->interfaces[i]->iio);
mahphalke 1:182a6f54cab0 680 if (ret < 0)
mahphalke 1:182a6f54cab0 681 goto error;
mahphalke 1:182a6f54cab0 682
mahphalke 1:182a6f54cab0 683 length = strlen(xml);
mahphalke 1:182a6f54cab0 684 tmp_xml2 = (char *)realloc(xml, strlen(xml) + strlen(tmp_xml) + 1);
mahphalke 1:182a6f54cab0 685 if (!tmp_xml2)
mahphalke 1:182a6f54cab0 686 goto error;
mahphalke 1:182a6f54cab0 687
mahphalke 1:182a6f54cab0 688 xml = tmp_xml2;
mahphalke 1:182a6f54cab0 689 strcpy((xml + length), tmp_xml);
mahphalke 1:182a6f54cab0 690 }
mahphalke 1:182a6f54cab0 691
mahphalke 1:182a6f54cab0 692 length = strlen(xml);
mahphalke 1:182a6f54cab0 693 tmp_xml = (char *)realloc(xml, strlen(xml) + strlen(header_end) + 1);
mahphalke 1:182a6f54cab0 694 if (!tmp_xml)
mahphalke 1:182a6f54cab0 695 goto error;
mahphalke 1:182a6f54cab0 696
mahphalke 1:182a6f54cab0 697 xml = tmp_xml;
mahphalke 1:182a6f54cab0 698 strcpy((xml + length), header_end);
mahphalke 1:182a6f54cab0 699
mahphalke 1:182a6f54cab0 700 *outxml = xml;
mahphalke 1:182a6f54cab0 701
mahphalke 1:182a6f54cab0 702 return SUCCESS;
mahphalke 1:182a6f54cab0 703 error:
mahphalke 1:182a6f54cab0 704 free(xml);
mahphalke 1:182a6f54cab0 705
mahphalke 1:182a6f54cab0 706 return FAILURE;
mahphalke 1:182a6f54cab0 707 }
mahphalke 1:182a6f54cab0 708
mahphalke 1:182a6f54cab0 709 /**
mahphalke 1:182a6f54cab0 710 * @brief Register interface.
mahphalke 1:182a6f54cab0 711 * @param iio_interface - Structure containing physical device instance and
mahphalke 1:182a6f54cab0 712 * device descriptor.
mahphalke 1:182a6f54cab0 713 * @return SUCCESS in case of success or negative value otherwise.
mahphalke 1:182a6f54cab0 714 */
mahphalke 1:182a6f54cab0 715 ssize_t iio_register(struct iio_interface *iio_interface)
mahphalke 1:182a6f54cab0 716 {
mahphalke 1:182a6f54cab0 717
mahphalke 1:182a6f54cab0 718 struct iio_interface **temp_interfaces;
mahphalke 1:182a6f54cab0 719
mahphalke 1:182a6f54cab0 720 if (!(iio_interfaces)) {
mahphalke 1:182a6f54cab0 721 iio_interfaces = (struct iio_interfaces *)calloc(1,
mahphalke 1:182a6f54cab0 722 sizeof(struct iio_interfaces));
mahphalke 1:182a6f54cab0 723 if (!iio_interfaces)
mahphalke 1:182a6f54cab0 724 return -ENOMEM;
mahphalke 1:182a6f54cab0 725
mahphalke 1:182a6f54cab0 726 iio_interfaces->num_interfaces = 1;
mahphalke 1:182a6f54cab0 727 iio_interfaces->interfaces = (struct iio_interface **)calloc(1,
mahphalke 1:182a6f54cab0 728 sizeof(struct iio_interface*));
mahphalke 1:182a6f54cab0 729 if (!iio_interfaces->interfaces)
mahphalke 1:182a6f54cab0 730 return -ENOMEM;
mahphalke 1:182a6f54cab0 731 } else {
mahphalke 1:182a6f54cab0 732 iio_interfaces->num_interfaces++;
mahphalke 1:182a6f54cab0 733 temp_interfaces = (struct iio_interface **)realloc(iio_interfaces->interfaces,
mahphalke 1:182a6f54cab0 734 iio_interfaces->num_interfaces * sizeof(struct iio_interface*));
mahphalke 1:182a6f54cab0 735 if (!temp_interfaces) {
mahphalke 1:182a6f54cab0 736 free(iio_interfaces->interfaces);
mahphalke 1:182a6f54cab0 737 return -ENOMEM;
mahphalke 1:182a6f54cab0 738 }
mahphalke 1:182a6f54cab0 739 iio_interfaces->interfaces = temp_interfaces;
mahphalke 1:182a6f54cab0 740 }
mahphalke 1:182a6f54cab0 741
mahphalke 1:182a6f54cab0 742 iio_interfaces->interfaces[iio_interfaces->num_interfaces - 1] = iio_interface;
mahphalke 1:182a6f54cab0 743
mahphalke 1:182a6f54cab0 744 return SUCCESS;
mahphalke 1:182a6f54cab0 745 }
mahphalke 1:182a6f54cab0 746
mahphalke 1:182a6f54cab0 747 /**
mahphalke 1:182a6f54cab0 748 * @brief Unregister interface.
mahphalke 1:182a6f54cab0 749 * @param iio_interface - Structure containing physical device instance and
mahphalke 1:182a6f54cab0 750 * device descriptor.
mahphalke 1:182a6f54cab0 751 * @return SUCCESS in case of success or negative value otherwise.
mahphalke 1:182a6f54cab0 752 */
mahphalke 1:182a6f54cab0 753 ssize_t iio_unregister(struct iio_interface *iio_interface)
mahphalke 1:182a6f54cab0 754 {
mahphalke 1:182a6f54cab0 755 struct iio_interfaces *interfaces;
mahphalke 1:182a6f54cab0 756 int16_t i, deleted = 0;
mahphalke 1:182a6f54cab0 757
mahphalke 1:182a6f54cab0 758 interfaces = (struct iio_interfaces *)calloc(1, sizeof(struct iio_interfaces));
mahphalke 1:182a6f54cab0 759 if (!interfaces)
mahphalke 1:182a6f54cab0 760 return FAILURE;
mahphalke 1:182a6f54cab0 761
mahphalke 1:182a6f54cab0 762 interfaces->interfaces = (struct iio_interface **)calloc(
mahphalke 1:182a6f54cab0 763 iio_interfaces->num_interfaces - 1,
mahphalke 1:182a6f54cab0 764 sizeof(struct iio_interface*));
mahphalke 1:182a6f54cab0 765 if (!interfaces->interfaces) {
mahphalke 1:182a6f54cab0 766 free(interfaces);
mahphalke 1:182a6f54cab0 767 return FAILURE;
mahphalke 1:182a6f54cab0 768 }
mahphalke 1:182a6f54cab0 769
mahphalke 1:182a6f54cab0 770 for(i = 0; i < iio_interfaces->num_interfaces; i++) {
mahphalke 1:182a6f54cab0 771 if (!strcmp(iio_interface->name, iio_interfaces->interfaces[i]->name)) {
mahphalke 1:182a6f54cab0 772 iio_interface = iio_interfaces->interfaces[i];
mahphalke 1:182a6f54cab0 773 deleted = 1;
mahphalke 1:182a6f54cab0 774 continue;
mahphalke 1:182a6f54cab0 775 }
mahphalke 1:182a6f54cab0 776 interfaces->interfaces[i - deleted] = iio_interfaces->interfaces[i];
mahphalke 1:182a6f54cab0 777 }
mahphalke 1:182a6f54cab0 778
mahphalke 1:182a6f54cab0 779 interfaces->num_interfaces = iio_interfaces->num_interfaces - 1;
mahphalke 1:182a6f54cab0 780 free(iio_interfaces);
mahphalke 1:182a6f54cab0 781
mahphalke 1:182a6f54cab0 782 return deleted ? SUCCESS : FAILURE;
mahphalke 1:182a6f54cab0 783 }
mahphalke 1:182a6f54cab0 784
mahphalke 1:182a6f54cab0 785 /**
mahphalke 1:182a6f54cab0 786 * @brief Set communication ops and read/write ops that will be called
mahphalke 1:182a6f54cab0 787 * from "libtinyiiod".
mahphalke 1:182a6f54cab0 788 * @param iiod - Structure containing new tinyiiod instance.
mahphalke 1:182a6f54cab0 789 * @param iio_server_ops - Structure containing read/write ops (Ex: read/write to
mahphalke 1:182a6f54cab0 790 * UART).
mahphalke 1:182a6f54cab0 791 * @return SUCCESS in case of success or negative value otherwise.
mahphalke 1:182a6f54cab0 792 */
mahphalke 1:182a6f54cab0 793 ssize_t iio_init(struct tinyiiod **iiod, struct iio_server_ops *iio_server_ops)
mahphalke 1:182a6f54cab0 794 {
mahphalke 1:182a6f54cab0 795 struct tinyiiod_ops *ops = (struct tinyiiod_ops *)calloc(1,
mahphalke 1:182a6f54cab0 796 sizeof(struct tinyiiod_ops));
mahphalke 1:182a6f54cab0 797
mahphalke 1:182a6f54cab0 798 if (!ops)
mahphalke 1:182a6f54cab0 799 return FAILURE;
mahphalke 1:182a6f54cab0 800
mahphalke 1:182a6f54cab0 801 /* device operations */
mahphalke 1:182a6f54cab0 802 ops->read_attr = iio_read_attr;
mahphalke 1:182a6f54cab0 803 ops->write_attr = iio_write_attr;
mahphalke 1:182a6f54cab0 804 ops->ch_read_attr = iio_ch_read_attr;
mahphalke 1:182a6f54cab0 805 ops->ch_write_attr = iio_ch_write_attr;
mahphalke 1:182a6f54cab0 806 ops->transfer_dev_to_mem = iio_transfer_dev_to_mem;
mahphalke 1:182a6f54cab0 807 ops->read_data = iio_read_dev;
mahphalke 1:182a6f54cab0 808 ops->transfer_mem_to_dev = iio_transfer_mem_to_dev;
mahphalke 1:182a6f54cab0 809 ops->write_data = iio_write_dev;
mahphalke 1:182a6f54cab0 810
mahphalke 1:182a6f54cab0 811 ops->open = iio_open_dev;
mahphalke 1:182a6f54cab0 812 ops->close = iio_close_dev;
mahphalke 1:182a6f54cab0 813 ops->get_mask = iio_get_mask;
mahphalke 1:182a6f54cab0 814
mahphalke 1:182a6f54cab0 815 ops->read = iio_server_ops->read;
mahphalke 1:182a6f54cab0 816 ops->write = iio_server_ops->write;
mahphalke 1:182a6f54cab0 817 ops->get_xml = iio_get_xml;
mahphalke 1:182a6f54cab0 818
mahphalke 1:182a6f54cab0 819 *iiod = tinyiiod_create(ops);
mahphalke 1:182a6f54cab0 820 if (!(*iiod)) {
mahphalke 1:182a6f54cab0 821 free(ops);
mahphalke 1:182a6f54cab0 822 return FAILURE;
mahphalke 1:182a6f54cab0 823 } else {
mahphalke 1:182a6f54cab0 824 return SUCCESS;
mahphalke 1:182a6f54cab0 825 }
mahphalke 1:182a6f54cab0 826 }
mahphalke 1:182a6f54cab0 827
mahphalke 1:182a6f54cab0 828 /**
mahphalke 1:182a6f54cab0 829 * @brief Free the resources allocated by "iio_init()".
mahphalke 1:182a6f54cab0 830 * @param iiod: Structure containing tinyiiod instance.
mahphalke 1:182a6f54cab0 831 * @return SUCCESS in case of success or negative value otherwise.
mahphalke 1:182a6f54cab0 832 */
mahphalke 1:182a6f54cab0 833 ssize_t iio_remove(struct tinyiiod *iiod)
mahphalke 1:182a6f54cab0 834 {
mahphalke 1:182a6f54cab0 835 uint8_t i;
mahphalke 1:182a6f54cab0 836
mahphalke 1:182a6f54cab0 837 for (i = 0; i < iio_interfaces->num_interfaces; i++)
mahphalke 1:182a6f54cab0 838 free(iio_interfaces->interfaces[i]);
mahphalke 1:182a6f54cab0 839
mahphalke 1:182a6f54cab0 840 free(iio_interfaces);
mahphalke 1:182a6f54cab0 841 tinyiiod_destroy(iiod);
mahphalke 1:182a6f54cab0 842
mahphalke 1:182a6f54cab0 843 return SUCCESS;
mahphalke 1:182a6f54cab0 844 }