blob: 7d95a6fbda3aa1b1b21c48a167011360b0329b56 [file] [log] [blame]
Paul Cercueilbb4401d2014-02-28 16:10:49 +01001/*
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 Cercueil306cb1c2014-04-11 14:46:45 +020019/** @file iio.h
20 * @brief Public interface */
21
Paul Cercueil0b2ce712014-02-17 15:04:18 +010022#ifndef __IIO_H__
23#define __IIO_H__
24
Paul Cercueila167e0c2014-04-08 14:50:41 +020025#ifdef __cplusplus
26extern "C" {
27#endif
28
Paul Cercueil35a01312014-02-20 10:56:57 +010029#include <stdbool.h>
Paul Cercueile1311222014-03-12 15:46:16 +010030#include <stdint.h>
Paul Cercueil0b2ce712014-02-17 15:04:18 +010031#include <stdlib.h>
Paul Cercueilecbf4ed2014-03-17 15:02:43 +010032#include <sys/types.h>
Paul Cercueil95347b92014-03-21 09:50:17 +010033#include <stddef.h>
Paul Cercueil0b2ce712014-02-17 15:04:18 +010034
Paul Cercueilb669f692014-04-04 13:42:24 +020035#ifdef _MSC_BUILD
36/* Come on Microsoft, time to get some C99... */
37typedef long ssize_t;
Paul Cercueilb669f692014-04-04 13:42:24 +020038#endif
39
Paul Cercueil59f2aa32014-04-04 16:28:51 +020040#ifdef __GNUC__
41#define __cnst __attribute__((const))
42#define __pure __attribute__((pure))
Paul Cercueil83f21892014-05-19 13:00:39 +020043#define __notused __attribute__((unused))
Paul Cercueil59f2aa32014-04-04 16:28:51 +020044#else
45#define __cnst
46#define __pure
Paul Cercueil83f21892014-05-19 13:00:39 +020047#define __notused
Paul Cercueil59f2aa32014-04-04 16:28:51 +020048#endif
49
50#ifdef _WIN32
51# ifdef LIBIIO_EXPORTS
Paul Cercueil59f2aa32014-04-04 16:28:51 +020052# define __api __declspec(dllexport)
Paul Cercueil7288c332014-04-07 16:25:11 +020053# else
54# define __api __declspec(dllimport)
Paul Cercueil59f2aa32014-04-04 16:28:51 +020055# endif
56#elif __GNUC__ >= 4
57# define __api __attribute__((visibility ("default")))
58#else
59# define __api
60#endif
61
Paul Cercueil0b2ce712014-02-17 15:04:18 +010062struct iio_context;
63struct iio_device;
64struct iio_channel;
Paul Cercueila689cd92014-03-20 16:37:25 +010065struct iio_buffer;
Paul Cercueil0b2ce712014-02-17 15:04:18 +010066
Paul Cercueilb4afdd92014-04-08 12:37:21 +020067
Paul Cercueil306cb1c2014-04-11 14:46:45 +020068/* ---------------------------------------------------------------------------*/
Paul Cercueil53ed9432014-11-20 13:46:04 +010069/* ------------------------- Top-level functions -----------------------------*/
70/** @defgroup TopLevel Top-level functions
71 * @{ */
72
73
74/** @brief Get the version of the libiio library
75 * @param major A pointer to an unsigned integer (NULL accepted)
76 * @param minor A pointer to an unsigned integer (NULL accepted)
77 * @param git_tag A pointer to a 8-characters buffer (NULL accepted) */
78__api void iio_library_get_version(unsigned int *major,
79 unsigned int *minor, char git_tag[8]);
80
81
82/** @} *//* ------------------------------------------------------------------*/
Paul Cercueilb4afdd92014-04-08 12:37:21 +020083/* ------------------------- Context functions -------------------------------*/
Paul Cercueil306cb1c2014-04-11 14:46:45 +020084/** @defgroup Context Context
85 * @{
86 * @struct iio_context
87 * @brief Contains the representation of an IIO context */
Paul Cercueilb4afdd92014-04-08 12:37:21 +020088
Paul Cercueil306cb1c2014-04-11 14:46:45 +020089
Paul Cercueil8f56ea72014-10-28 15:18:18 +010090/** @brief Create a context from local or remote IIO devices
91 * @return On success, A pointer to an iio_context structure
92 * @return On failure, NULL is returned
93 *
94 * <b>NOTE:</b> This function will create a network context if the IIOD_REMOTE
95 * environment variable is set to the hostname where the IIOD server runs. If
96 * set to an empty string, the server will be discovered using ZeroConf.
97 * If the environment variable is not set, a local context will be created
98 * instead. */
99__api struct iio_context * iio_create_default_context(void);
100
101
102/** @brief Create a context from local IIO devices (Linux only)
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200103 * @return On success, A pointer to an iio_context structure
104 * @return On failure, NULL is returned */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200105__api struct iio_context * iio_create_local_context(void);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200106
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200107
108/** @brief Create a context from a XML file
109 * @param xml_file Path to the XML file to open
110 * @return On success, A pointer to an iio_context structure
111 * @return On failure, NULL is returned
112 *
113 * <b>NOTE:</b> The format of the XML must comply to the one returned by
114 * iio_context_get_xml. */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200115__api struct iio_context * iio_create_xml_context(const char *xml_file);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200116
117
118/** @brief Create a context from XML data in memory
119 * @param xml Pointer to the XML data in memory
120 * @param len Length of the XML string in memory (excluding the final \0)
121 * @return On success, A pointer to an iio_context structure
122 * @return On failure, NULL is returned
123 *
124 * <b>NOTE:</b> The format of the XML must comply to the one returned by
125 * iio_context_get_xml */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200126__api struct iio_context * iio_create_xml_context_mem(
127 const char *xml, size_t len);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200128
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200129
130/** @brief Create a context from the network
131 * @param host Hostname, IPv4 or IPv6 address where the IIO Daemon is running
132 * @return On success, a pointer to an iio_context structure
133 * @return On failure, NULL is returned */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200134__api struct iio_context * iio_create_network_context(const char *host);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200135
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200136
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100137/** @brief Duplicate a pre-existing IIO context
138 * @param ctx A pointer to an iio_context structure
139 * @return On success, A pointer to an iio_context structure
140 * @return On failure, NULL is returned */
141__api struct iio_context * iio_context_clone(const struct iio_context *ctx);
142
143
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200144/** @brief Destroy the given context
145 * @param ctx A pointer to an iio_context structure
146 *
147 * <b>NOTE:</b> After that function, the iio_context pointer shall be invalid. */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200148__api void iio_context_destroy(struct iio_context *ctx);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100149
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200150
Paul Cercueile45f8762014-05-02 11:19:26 +0200151/** @brief Get the version of the backend in use
152 * @param ctx A pointer to an iio_context structure
Paul Cercueild15d9952014-05-20 11:40:08 +0200153 * @param major A pointer to an unsigned integer (NULL accepted)
154 * @param minor A pointer to an unsigned integer (NULL accepted)
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200155 * @param git_tag A pointer to a 8-characters buffer (NULL accepted)
Paul Cercueile45f8762014-05-02 11:19:26 +0200156 * @return On success, 0 is returned
157 * @return On error, a negative errno code is returned */
158__api int iio_context_get_version(const struct iio_context *ctx,
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200159 unsigned int *major, unsigned int *minor, char git_tag[8]);
Paul Cercueile45f8762014-05-02 11:19:26 +0200160
161
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200162/** @brief Obtain a XML representation of the given context
163 * @param ctx A pointer to an iio_context structure
164 * @return A pointer to a static NULL-terminated string */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200165__api __pure const char * iio_context_get_xml(const struct iio_context *ctx);
166
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200167
168/** @brief Get the name of the given context
169 * @param ctx A pointer to an iio_context structure
170 * @return A pointer to a static NULL-terminated string
171 *
172 * <b>NOTE:</b>The returned string will be <b><i>local</i></b>,
173 * <b><i>xml</i></b> or <b><i>network</i></b> when the context has been
174 * created with the local, xml and network backends respectively.*/
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200175__api __pure const char * iio_context_get_name(const struct iio_context *ctx);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200176
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200177
Paul Cercueil3ac36c12015-01-08 14:44:00 +0100178/** @brief Get a description of the given context
179 * @param ctx A pointer to an iio_context structure
180 * @return A pointer to a static NULL-terminated string
181 *
182 * <b>NOTE:</b>The returned string will contain human-readable information about
183 * the current context. */
184__api __pure const char * iio_context_get_description(
185 const struct iio_context *ctx);
186
187
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200188/** @brief Enumerate the devices found in the given context
189 * @param ctx A pointer to an iio_context structure
190 * @return The number of devices found */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200191__api __pure unsigned int iio_context_get_devices_count(
192 const struct iio_context *ctx);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200193
194
195/** @brief Get the device present at the given index
196 * @param ctx A pointer to an iio_context structure
197 * @param index The index corresponding to the device
198 * @return On success, a pointer to an iio_device structure
199 * @return If the index is invalid, NULL is returned */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200200__api __pure struct iio_device * iio_context_get_device(
201 const struct iio_context *ctx, unsigned int index);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200202
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200203
204/** @brief Try to find a device structure by its name of ID
205 * @param ctx A pointer to an iio_context structure
206 * @param name A NULL-terminated string corresponding to the name or the ID of
207 * the device to search for
208 * @return On success, a pointer to an iio_device structure
209 * @return If the name or ID does not correspond to any known device, NULL is
210 * returned */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200211__api __pure struct iio_device * iio_context_find_device(
212 const struct iio_context *ctx, const char *name);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100213
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200214
Paul Cercueil4ca73542014-06-10 16:23:29 +0200215/** @brief Set a timeout for I/O operations
216 * @param ctx A pointer to an iio_context structure
217 * @param timeout_ms A positive integer representing the time in milliseconds
218 * after which a timeout occurs
219 * @return On success, 0 is returned
220 * @return On error, a negative errno code is returned */
221__api int iio_context_set_timeout(
222 struct iio_context *ctx, unsigned int timeout_ms);
223
224
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200225/** @} *//* ------------------------------------------------------------------*/
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200226/* ------------------------- Device functions --------------------------------*/
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200227/** @defgroup Device Device
228 * @{
229 * @struct iio_device
230 * @brief Represents a device in the IIO context */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200231
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200232
233/** @brief Retrieve the device ID (e.g. <b><i>iio:device0</i></b>)
234 * @param dev A pointer to an iio_device structure
235 * @return A pointer to a static NULL-terminated string */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200236__api __pure const char * iio_device_get_id(const struct iio_device *dev);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200237
238
239/** @brief Retrieve the device name (e.g. <b><i>xadc</i></b>)
240 * @param dev A pointer to an iio_device structure
241 * @return A pointer to a static NULL-terminated string
242 *
243 * <b>NOTE:</b> if the device has no name, NULL is returned. */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200244__api __pure const char * iio_device_get_name(const struct iio_device *dev);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200245
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200246
247/** @brief Enumerate the channels of the given device
248 * @param dev A pointer to an iio_device structure
249 * @return The number of channels found */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200250__api __pure unsigned int iio_device_get_channels_count(
251 const struct iio_device *dev);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200252
253
254/** @brief Enumerate the device-specific attributes of the given device
255 * @param dev A pointer to an iio_device structure
256 * @return The number of device-specific attributes found */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200257__api __pure unsigned int iio_device_get_attrs_count(
258 const struct iio_device *dev);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200259
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200260
261/** @brief Get the channel present at the given index
262 * @param dev A pointer to an iio_device structure
263 * @param index The index corresponding to the channel
264 * @return On success, a pointer to an iio_channel structure
265 * @return If the index is invalid, NULL is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200266__api __pure struct iio_channel * iio_device_get_channel(
267 const struct iio_device *dev, unsigned int index);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200268
269
270/** @brief Get the device-specific attribute present at the given index
271 * @param dev A pointer to an iio_device structure
272 * @param index The index corresponding to the attribute
273 * @return On success, a pointer to a static NULL-terminated string
274 * @return If the index is invalid, NULL is returned */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200275__api __pure const char * iio_device_get_attr(
276 const struct iio_device *dev, unsigned int index);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200277
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200278
279/** @brief Try to find a channel structure by its name of ID
280 * @param dev A pointer to an iio_device structure
281 * @param name A NULL-terminated string corresponding to the name or the ID of
282 * the channel to search for
283 * @param output True if the searched channel is output, False otherwise
284 * @return On success, a pointer to an iio_channel structure
285 * @return If the name or ID does not correspond to any known channel of the
286 * given device, NULL is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200287__api __pure struct iio_channel * iio_device_find_channel(
288 const struct iio_device *dev, const char *name, bool output);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200289
290
291/** @brief Try to find a device-specific attribute by its name
292 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200293 * @param name A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200294 * attribute
295 * @return On success, a pointer to a static NULL-terminated string
296 * @return If the name does not correspond to any known attribute of the given
297 * device, NULL is returned
298 *
299 * <b>NOTE:</b> This function is useful to detect the presence of an attribute.
300 * It can also be used to retrieve the name of an attribute as a pointer to a
301 * static string from a dynamically allocated string. */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200302__api __pure const char * iio_device_find_attr(
303 const struct iio_device *dev, const char *name);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200304
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200305
306/** @brief Read the content of the given device-specific attribute
307 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200308 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200309 * attribute
310 * @param dst A pointer to the memory area where the NULL-terminated string
311 * corresponding to the value read will be stored
312 * @param len The available length of the memory area, in bytes
313 * @return On success, the number of bytes written to the buffer
Paul Cercueil4ca6ddc2014-05-21 14:51:50 +0200314 * @return On error, a negative errno code is returned
315 *
316 * <b>NOTE:</b>By passing NULL as the "attr" argument to iio_device_attr_read,
317 * it is now possible to read all of the attributes of a device.
318 *
319 * The buffer is filled with one block of data per attribute of the device,
320 * by the order they appear in the iio_device structure.
321 *
322 * The first four bytes of one block correspond to a 32-bit signed value in
323 * network order. If negative, it corresponds to the errno code that were
324 * returned when reading the attribute; if positive, it corresponds to the
325 * length of the data read. In that case, the rest of the block contains
326 * the data. */
327 __api ssize_t iio_device_attr_read(const struct iio_device *dev,
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200328 const char *attr, char *dst, size_t len);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200329
330
Paul Cercueil1b36a012014-06-05 14:39:31 +0200331/** @brief Read the content of all device-specific attributes
332 * @param dev A pointer to an iio_device structure
333 * @param cb A pointer to a callback function
334 * @param data A pointer that will be passed to the callback function
335 * @return On success, 0 is returned
336 * @return On error, a negative errno code is returned
337 *
338 * <b>NOTE:</b> This function is especially useful when used with the network
339 * backend, as all the device-specific attributes are read in one single
340 * command. */
341__api int iio_device_attr_read_all(struct iio_device *dev,
342 int (*cb)(struct iio_device *dev, const char *attr,
343 const char *value, size_t len, void *d),
344 void *data);
345
346
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200347/** @brief Read the content of the given device-specific attribute
348 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200349 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200350 * attribute
351 * @param val A pointer to a bool variable where the value should be stored
352 * @return On success, 0 is returned
353 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200354__api int iio_device_attr_read_bool(const struct iio_device *dev,
355 const char *attr, bool *val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200356
357
358/** @brief Read the content of the given device-specific attribute
359 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200360 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200361 * attribute
362 * @param val A pointer to a long long variable where the value should be stored
363 * @return On success, 0 is returned
364 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200365__api int iio_device_attr_read_longlong(const struct iio_device *dev,
366 const char *attr, long long *val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200367
368
369/** @brief Read the content of the given device-specific attribute
370 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200371 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200372 * attribute
373 * @param val A pointer to a double variable where the value should be stored
374 * @return On success, 0 is returned
375 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200376__api int iio_device_attr_read_double(const struct iio_device *dev,
377 const char *attr, double *val);
378
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200379
380/** @brief Set the value of the given device-specific attribute
381 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200382 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200383 * attribute
384 * @param src A NULL-terminated string to set the attribute to
385 * @return On success, the number of bytes written
Paul Cercueil1da28122014-05-22 10:59:49 +0200386 * @return On error, a negative errno code is returned
387 *
388 * <b>NOTE:</b>By passing NULL as the "attr" argument to iio_device_attr_write,
389 * it is now possible to write all of the attributes of a device.
390 *
391 * The buffer must contain one block of data per attribute of the device,
392 * by the order they appear in the iio_device structure.
393 *
394 * The first four bytes of one block correspond to a 32-bit signed value in
395 * network order. If negative, the attribute is not written; if positive,
396 * it corresponds to the length of the data to write. In that case, the rest
397 * of the block must contain the data. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200398__api ssize_t iio_device_attr_write(const struct iio_device *dev,
399 const char *attr, const char *src);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200400
401
402/** @brief Set the value of the given device-specific attribute
403 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200404 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200405 * attribute
Paul Cercueilcecda352014-05-06 18:14:29 +0200406 * @param src A pointer to the data to be written
407 * @param len The number of bytes that should be written
408 * @return On success, the number of bytes written
409 * @return On error, a negative errno code is returned */
410__api ssize_t iio_device_attr_write_raw(const struct iio_device *dev,
411 const char *attr, const void *src, size_t len);
412
413
Paul Cercueil1b36a012014-06-05 14:39:31 +0200414/** @brief Set the values of all device-specific attributes
415 * @param dev A pointer to an iio_device structure
416 * @param cb A pointer to a callback function
417 * @param data A pointer that will be passed to the callback function
418 * @return On success, 0 is returned
419 * @return On error, a negative errno code is returned
420 *
421 * <b>NOTE:</b> This function is especially useful when used with the network
422 * backend, as all the device-specific attributes are written in one single
423 * command. */
424__api int iio_device_attr_write_all(struct iio_device *dev,
425 ssize_t (*cb)(struct iio_device *dev,
426 const char *attr, void *buf, size_t len, void *d),
427 void *data);
428
429
Paul Cercueilcecda352014-05-06 18:14:29 +0200430/** @brief Set the value of the given device-specific attribute
431 * @param dev A pointer to an iio_device structure
432 * @param attr A NULL-terminated string corresponding to the name of the
433 * attribute
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200434 * @param val A bool value to set the attribute to
435 * @return On success, 0 is returned
436 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200437__api int iio_device_attr_write_bool(const struct iio_device *dev,
438 const char *attr, bool val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200439
440
441/** @brief Set the value of the given device-specific attribute
442 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200443 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200444 * attribute
445 * @param val A long long value to set the attribute to
446 * @return On success, 0 is returned
447 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200448__api int iio_device_attr_write_longlong(const struct iio_device *dev,
449 const char *attr, long long val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200450
451
452/** @brief Set the value of the given device-specific attribute
453 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200454 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200455 * attribute
456 * @param val A double value to set the attribute to
457 * @return On success, 0 is returned
458 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200459__api int iio_device_attr_write_double(const struct iio_device *dev,
460 const char *attr, double val);
461
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200462
463/** @brief Associate a pointer to an iio_device structure
464 * @param dev A pointer to an iio_device structure
465 * @param data The pointer to be associated */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200466__api void iio_device_set_data(struct iio_device *dev, void *data);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200467
468
469/** @brief Retrieve a previously associated pointer of an iio_device structure
470 * @param dev A pointer to an iio_device structure
471 * @return The pointer previously associated if present, or NULL */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200472__api void * iio_device_get_data(const struct iio_device *dev);
473
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200474
475/** @brief Retrieve the trigger of a given device
476 * @param dev A pointer to an iio_device structure
477 * @param trigger a pointer to a pointer of an iio_device structure. The pointed
478 * pointer will be set to the address of the iio_device structure corresponding
479 * to the associated trigger device.
480 * @return On success, 0 is returned
481 * @return On error, a negative errno code is returned */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200482__api int iio_device_get_trigger(const struct iio_device *dev,
Paul Cercueil24ffa532014-03-10 12:39:58 +0100483 const struct iio_device **trigger);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200484
485
486/** @brief Associate a trigger to a given device
487 * @param dev A pointer to an iio_device structure
488 * @param trigger a pointer to the iio_device structure corresponding to the
489 * trigger that should be associated.
490 * @return On success, 0 is returned
491 * @return On error, a negative errno code is returned */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200492__api int iio_device_set_trigger(const struct iio_device *dev,
Paul Cercueil24ffa532014-03-10 12:39:58 +0100493 const struct iio_device *trigger);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100494
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200495
496/** @brief Return True if the given device is a trigger
497 * @param dev A pointer to an iio_device structure
498 * @return True if the device is a trigger, False otherwise */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200499__api __pure bool iio_device_is_trigger(const struct iio_device *dev);
500
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200501
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200502/** @} *//* ------------------------------------------------------------------*/
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200503/* ------------------------- Channel functions -------------------------------*/
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200504/** @defgroup Channel Channel
505 * @{
506 * @struct iio_channel
507 * @brief Represents an input or output channel of a device */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200508
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200509
510/** @brief Retrieve the channel ID (e.g. <b><i>voltage0</i></b>)
511 * @param chn A pointer to an iio_channel structure
512 * @return A pointer to a static NULL-terminated string */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200513__api __pure const char * iio_channel_get_id(const struct iio_channel *chn);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200514
515
516/** @brief Retrieve the channel name (e.g. <b><i>vccint</i></b>)
517 * @param chn A pointer to an iio_channel structure
518 * @return A pointer to a static NULL-terminated string
519 *
520 * <b>NOTE:</b> if the channel has no name, NULL is returned. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200521__api __pure const char * iio_channel_get_name(const struct iio_channel *chn);
522
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200523
524/** @brief Return True if the given channel is an output channel
525 * @param chn A pointer to an iio_channel structure
526 * @return True if the channel is an output channel, False otherwise */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200527__api __pure bool iio_channel_is_output(const struct iio_channel *chn);
528
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200529
Paul Cercueil85aaf482014-04-24 16:39:09 +0200530/** @brief Return True if the given channel is a scan element
531 * @param chn A pointer to an iio_channel structure
532 * @return True if the channel is a scan element, False otherwise
533 *
534 * <b>NOTE:</b> a channel that is a scan element is a channel that can
535 * generate samples (for an input channel) or receive samples (for an output
536 * channel) after being enabled. */
537__api __pure bool iio_channel_is_scan_element(const struct iio_channel *chn);
538
539
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200540/** @brief Enumerate the channel-specific attributes of the given channel
541 * @param chn A pointer to an iio_channel structure
542 * @return The number of channel-specific attributes found */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200543__api __pure unsigned int iio_channel_get_attrs_count(
544 const struct iio_channel *chn);
545
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200546
547/** @brief Get the channel-specific attribute present at the given index
548 * @param chn A pointer to an iio_channel structure
549 * @param index The index corresponding to the attribute
550 * @return On success, a pointer to a static NULL-terminated string
551 * @return If the index is invalid, NULL is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200552__api __pure const char * iio_channel_get_attr(
553 const struct iio_channel *chn, unsigned int index);
554
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200555
556/** @brief Try to find a channel-specific attribute by its name
557 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200558 * @param name A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200559 * attribute
560 * @return On success, a pointer to a static NULL-terminated string
561 * @return If the name does not correspond to any known attribute of the given
562 * channel, NULL is returned
563 *
564 * <b>NOTE:</b> This function is useful to detect the presence of an attribute.
565 * It can also be used to retrieve the name of an attribute as a pointer to a
566 * static string from a dynamically allocated string. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200567__api __pure const char * iio_channel_find_attr(
568 const struct iio_channel *chn, const char *name);
569
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200570
Paul Cercueil6f8dbd42014-05-05 17:05:59 +0200571/** @brief Retrieve the filename of an attribute
572 * @param chn A pointer to an iio_channel structure
573 * @param attr a NULL-terminated string corresponding to the name of the
574 * attribute
575 * @return On success, a pointer to a static NULL-terminated string
576 * @return If the attribute name is unknown, NULL is returned */
577__api __pure const char * iio_channel_attr_get_filename(
578 const struct iio_channel *chn, const char *attr);
579
580
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200581/** @brief Read the content of the given channel-specific attribute
582 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200583 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200584 * attribute
585 * @param dst A pointer to the memory area where the NULL-terminated string
586 * corresponding to the value read will be stored
587 * @param len The available length of the memory area, in bytes
588 * @return On success, the number of bytes written to the buffer
Paul Cercueil4ca6ddc2014-05-21 14:51:50 +0200589 * @return On error, a negative errno code is returned
590 *
591 * <b>NOTE:</b>By passing NULL as the "attr" argument to iio_channel_attr_read,
592 * it is now possible to read all of the attributes of a channel.
593 *
594 * The buffer is filled with one block of data per attribute of the channel,
595 * by the order they appear in the iio_channel structure.
596 *
597 * The first four bytes of one block correspond to a 32-bit signed value in
598 * network order. If negative, it corresponds to the errno code that were
599 * returned when reading the attribute; if positive, it corresponds to the
600 * length of the data read. In that case, the rest of the block contains
601 * the data. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200602__api ssize_t iio_channel_attr_read(const struct iio_channel *chn,
603 const char *attr, char *dst, size_t len);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200604
605
Paul Cercueil47d23d02014-06-05 14:46:20 +0200606/** @brief Read the content of all channel-specific attributes
607 * @param chn A pointer to an iio_channel structure
608 * @param cb A pointer to a callback function
609 * @param data A pointer that will be passed to the callback function
610 * @return On success, 0 is returned
611 * @return On error, a negative errno code is returned
612 *
613 * <b>NOTE:</b> This function is especially useful when used with the network
614 * backend, as all the channel-specific attributes are read in one single
615 * command. */
616__api int iio_channel_attr_read_all(struct iio_channel *chn,
617 int (*cb)(struct iio_channel *chn,
618 const char *attr, const char *val, size_t len, void *d),
619 void *data);
620
621
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200622/** @brief Read the content of the given channel-specific attribute
623 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200624 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200625 * attribute
626 * @param val A pointer to a bool variable where the value should be stored
627 * @return On success, 0 is returned
628 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200629__api int iio_channel_attr_read_bool(const struct iio_channel *chn,
630 const char *attr, bool *val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200631
632
633/** @brief Read the content of the given channel-specific attribute
634 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200635 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200636 * attribute
637 * @param val A pointer to a long long variable where the value should be stored
638 * @return On success, 0 is returned
639 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200640__api int iio_channel_attr_read_longlong(const struct iio_channel *chn,
641 const char *attr, long long *val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200642
643
644/** @brief Read the content of the given channel-specific attribute
645 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200646 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200647 * attribute
648 * @param val A pointer to a double variable where the value should be stored
649 * @return On success, 0 is returned
650 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200651__api int iio_channel_attr_read_double(const struct iio_channel *chn,
652 const char *attr, double *val);
653
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200654
655/** @brief Set the value of the given channel-specific attribute
656 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200657 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200658 * attribute
659 * @param src A NULL-terminated string to set the attribute to
660 * @return On success, the number of bytes written
Paul Cercueil1da28122014-05-22 10:59:49 +0200661 * @return On error, a negative errno code is returned
662 *
663 * <b>NOTE:</b>By passing NULL as the "attr" argument to iio_channel_attr_write,
664 * it is now possible to write all of the attributes of a channel.
665 *
666 * The buffer must contain one block of data per attribute of the channel,
667 * by the order they appear in the iio_channel structure.
668 *
669 * The first four bytes of one block correspond to a 32-bit signed value in
670 * network order. If negative, the attribute is not written; if positive,
671 * it corresponds to the length of the data to write. In that case, the rest
672 * of the block must contain the data. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200673__api ssize_t iio_channel_attr_write(const struct iio_channel *chn,
674 const char *attr, const char *src);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200675
676
677/** @brief Set the value of the given channel-specific attribute
678 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200679 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200680 * attribute
Paul Cercueilcecda352014-05-06 18:14:29 +0200681 * @param src A pointer to the data to be written
682 * @param len The number of bytes that should be written
683 * @return On success, the number of bytes written
684 * @return On error, a negative errno code is returned */
685__api ssize_t iio_channel_attr_write_raw(const struct iio_channel *chn,
686 const char *attr, const void *src, size_t len);
687
688
Paul Cercueil47d23d02014-06-05 14:46:20 +0200689/** @brief Set the values of all channel-specific attributes
690 * @param chn A pointer to an iio_channel structure
691 * @param cb A pointer to a callback function
692 * @param data A pointer that will be passed to the callback function
693 * @return On success, 0 is returned
694 * @return On error, a negative errno code is returned
695 *
696 * <b>NOTE:</b> This function is especially useful when used with the network
697 * backend, as all the channel-specific attributes are written in one single
698 * command. */
699__api int iio_channel_attr_write_all(struct iio_channel *chn,
700 ssize_t (*cb)(struct iio_channel *chn,
701 const char *attr, void *buf, size_t len, void *d),
702 void *data);
703
704
Paul Cercueilcecda352014-05-06 18:14:29 +0200705/** @brief Set the value of the given channel-specific attribute
706 * @param chn A pointer to an iio_channel structure
707 * @param attr A NULL-terminated string corresponding to the name of the
708 * attribute
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200709 * @param val A bool value to set the attribute to
710 * @return On success, 0 is returned
711 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200712__api int iio_channel_attr_write_bool(const struct iio_channel *chn,
713 const char *attr, bool val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200714
715
716/** @brief Set the value of the given channel-specific attribute
717 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200718 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200719 * attribute
720 * @param val A long long value to set the attribute to
721 * @return On success, 0 is returned
722 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200723__api int iio_channel_attr_write_longlong(const struct iio_channel *chn,
724 const char *attr, long long val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200725
726
727/** @brief Set the value of the given channel-specific attribute
728 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200729 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200730 * attribute
731 * @param val A double value to set the attribute to
732 * @return On success, 0 is returned
733 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200734__api int iio_channel_attr_write_double(const struct iio_channel *chn,
735 const char *attr, double val);
736
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200737
738/** @brief Enable the given channel
739 * @param chn A pointer to an iio_channel structure
740 *
741 * <b>NOTE:</b>Before creating an iio_buffer structure with
742 * iio_device_create_buffer, it is required to enable at least one channel of
743 * the device to read from. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200744__api void iio_channel_enable(struct iio_channel *chn);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200745
746
747/** @brief Disable the given channel
748 * @param chn A pointer to an iio_channel structure */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200749__api void iio_channel_disable(struct iio_channel *chn);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200750
751
752/** @brief Returns True if the channel is enabled
753 * @param chn A pointer to an iio_channel structure
754 * @return True if the channel is enabled, False otherwise */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200755__api bool iio_channel_is_enabled(const struct iio_channel *chn);
756
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200757
758/** Demultiplex the samples of a given channel
759 * @param chn A pointer to an iio_channel structure
760 * @param buffer A pointer to an iio_buffer structure
761 * @param dst A pointer to the memory area where the demultiplexed data will be
762 * stored
763 * @param len The available length of the memory area, in bytes
764 * @return The size of the demultiplexed data, in bytes */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200765__api size_t iio_channel_read_raw(const struct iio_channel *chn,
766 struct iio_buffer *buffer, void *dst, size_t len);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200767
768
769/** Demultiplex and convert the samples of a given channel
770 * @param chn A pointer to an iio_channel structure
771 * @param buffer A pointer to an iio_buffer structure
772 * @param dst A pointer to the memory area where the converted data will be
773 * stored
774 * @param len The available length of the memory area, in bytes
775 * @return The size of the converted data, in bytes */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200776__api size_t iio_channel_read(const struct iio_channel *chn,
777 struct iio_buffer *buffer, void *dst, size_t len);
778
779
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200780/** Multiplex the samples of a given channel
781 * @param chn A pointer to an iio_channel structure
782 * @param buffer A pointer to an iio_buffer structure
783 * @param src A pointer to the memory area where the sequential data will
784 * be read from
785 * @param len The length of the memory area, in bytes
786 * @return The number of bytes actually multiplexed */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200787__api size_t iio_channel_write_raw(const struct iio_channel *chn,
788 struct iio_buffer *buffer, const void *src, size_t len);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200789
790
791/** Convert and multiplex the samples of a given channel
792 * @param chn A pointer to an iio_channel structure
793 * @param buffer A pointer to an iio_buffer structure
794 * @param src A pointer to the memory area where the sequential data will
795 * be read from
796 * @param len The length of the memory area, in bytes
797 * @return The number of bytes actually converted and multiplexed */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200798__api size_t iio_channel_write(const struct iio_channel *chn,
799 struct iio_buffer *buffer, const void *src, size_t len);
800
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200801
802/** @brief Associate a pointer to an iio_channel structure
803 * @param chn A pointer to an iio_channel structure
804 * @param data The pointer to be associated */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200805__api void iio_channel_set_data(struct iio_channel *chn, void *data);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200806
807
808/** @brief Retrieve a previously associated pointer of an iio_channel structure
809 * @param chn A pointer to an iio_channel structure
810 * @return The pointer previously associated if present, or NULL */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200811__api void * iio_channel_get_data(const struct iio_channel *chn);
812
813
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200814/** @} *//* ------------------------------------------------------------------*/
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200815/* ------------------------- Buffer functions --------------------------------*/
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200816/** @defgroup Buffer Buffer
817 * @{
818 * @struct iio_buffer
819 * @brief An input or output buffer, used to read or write samples */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200820
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200821
822/** @brief Create an input or output buffer associated to the given device
823 * @param dev A pointer to an iio_device structure
824 * @param samples_count The number of samples that the buffer should contain
Paul Cercueil2004eaa2014-05-22 14:04:39 +0200825 * @param cyclic If True, enable cyclic mode
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200826 * @return On success, a pointer to an iio_buffer structure
Paul Cercueil3106e892014-04-30 11:29:51 +0200827 * @return On error, NULL is returned, and errno is set to the error code
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200828 *
829 * <b>NOTE:</b> Channels that have to be written to / read from must be enabled
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200830 * before creating the buffer. */
831__api struct iio_buffer * iio_device_create_buffer(const struct iio_device *dev,
Paul Cercueil2004eaa2014-05-22 14:04:39 +0200832 size_t samples_count, bool cyclic);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200833
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200834
835/** @brief Destroy the given buffer
836 * @param buf A pointer to an iio_buffer structure
837 *
838 * <b>NOTE:</b> After that function, the iio_buffer pointer shall be invalid. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200839__api void iio_buffer_destroy(struct iio_buffer *buf);
840
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200841
842/** @brief Fetch more samples from the hardware
843 * @param buf A pointer to an iio_buffer structure
844 * @return On success, the number of bytes read is returned
845 * @return On error, a negative errno code is returned
846 *
847 * <b>NOTE:</b> Only valid for input buffers */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200848__api ssize_t iio_buffer_refill(struct iio_buffer *buf);
849
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200850
851/** @brief Send the samples to the hardware
852 * @param buf A pointer to an iio_buffer structure
853 * @return On success, the number of bytes written is returned
854 * @return On error, a negative errno code is returned
855 *
856 * <b>NOTE:</b> Only valid for output buffers */
Paul Cercueil0183b6e2014-05-20 13:21:46 +0200857__api ssize_t iio_buffer_push(struct iio_buffer *buf);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200858
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200859
Paul Cercueil6d927162014-04-16 15:53:22 +0200860/** @brief Get the start address of the buffer
861 * @param buf A pointer to an iio_buffer structure
862 * @return A pointer corresponding to the start address of the buffer */
Paul Cercueil49106002015-03-04 13:19:20 +0100863__api void * iio_buffer_start(const struct iio_buffer *buf);
Paul Cercueil6d927162014-04-16 15:53:22 +0200864
865
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200866/** @brief Find the first sample of a channel in a buffer
867 * @param buf A pointer to an iio_buffer structure
868 * @param chn A pointer to an iio_channel structure
869 * @return A pointer to the first sample found, or to the end of the buffer if
870 * no sample for the given channel is present in the buffer
871 *
872 * <b>NOTE:</b> This fonction, coupled with iio_buffer_step and iio_buffer_end,
873 * can be used to iterate on all the samples of a given channel present in the
874 * buffer, doing the following:
875 *
876 * @verbatim
877 for (void *ptr = iio_buffer_first(buffer, chn); ptr < iio_buffer_end(buffer); ptr += iio_buffer_step(buffer)) {
878 ....
879 }
880 @endverbatim */
881__api void * iio_buffer_first(const struct iio_buffer *buf,
882 const struct iio_channel *chn);
883
884
885/** @brief Get the step size between two samples of one channel
886 * @param buf A pointer to an iio_buffer structure
887 * @return the difference between the addresses of two consecutive samples of
888 * one same channel */
889__api ptrdiff_t iio_buffer_step(const struct iio_buffer *buf);
890
891
892/** @brief Get the address that follows the last sample in a buffer
893 * @param buf A pointer to an iio_buffer structure
894 * @return A pointer corresponding to the address that follows the last sample
895 * present in the buffer */
896__api void * iio_buffer_end(const struct iio_buffer *buf);
897
898
899/** @brief Call the supplied callback for each sample found in a buffer
900 * @param buf A pointer to an iio_buffer structure
901 * @param callback A pointer to a function to call for each sample found
902 * @param data A user-specified pointer that will be passed to the callback
903 *
904 * <b>NOTE:</b> The callback receives four arguments:
905 * * A pointer to the iio_channel structure corresponding to the sample,
906 * * A pointer to the sample itself,
907 * * The length of the sample in bytes,
908 * * The user-specified pointer passed to iio_buffer_foreach_sample. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200909__api ssize_t iio_buffer_foreach_sample(struct iio_buffer *buf,
910 ssize_t (*callback)(const struct iio_channel *chn,
911 void *src, size_t bytes, void *d), void *data);
912
913
Paul Cercueila2d4bad2014-05-27 10:15:29 +0200914/** @brief Associate a pointer to an iio_buffer structure
915 * @param buf A pointer to an iio_buffer structure
916 * @param data The pointer to be associated */
917__api void iio_buffer_set_data(struct iio_buffer *buf, void *data);
918
919
920/** @brief Retrieve a previously associated pointer of an iio_buffer structure
921 * @param buf A pointer to an iio_buffer structure
922 * @return The pointer previously associated if present, or NULL */
923__api void * iio_buffer_get_data(const struct iio_buffer *buf);
924
925
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200926/** @} *//* ------------------------------------------------------------------*/
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200927/* ------------------------- Low-level functions -----------------------------*/
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200928/** @defgroup Debug Debug and low-level functions
929 * @{
930 * @struct iio_data_format
931 * @brief Contains the format of a data sample.
932 *
933 * The different fields inform about the correct way to convert one sample from
934 * its raw format (as read from / generated by the hardware) to its real-world
935 * value.
936 */
937struct iio_data_format {
938 /** @brief Total length of the sample, in bits */
939 unsigned int length;
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200940
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200941 /** @brief Length of valuable data in the sample, in bits */
942 unsigned int bits;
943
944 /** @brief Right-shift to apply when converting sample */
945 unsigned int shift;
946
947 /** @brief Contains True if the sample is signed */
948 bool is_signed;
949
Michael Hennerichfa3c6f62014-08-13 11:21:23 +0200950 /** @brief Contains True if the sample is fully defined, sign extended, etc. */
951 bool is_fully_defined;
952
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200953 /** @brief Contains True if the sample is in big-endian format */
954 bool is_be;
955
956 /** @brief Contains True if the sample should be scaled when converted */
957 bool with_scale;
958
959 /** @brief Contains the scale to apply if with_scale is set */
960 double scale;
961};
962
963
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200964/** @brief Get the current sample size
965 * @param dev A pointer to an iio_device structure
966 * @return On success, the sample size in bytes
967 * @return On error, a negative errno code is returned
968 *
969 * <b>NOTE:</b> The sample size is not constant and will change when channels
970 * get enabled or disabled. */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200971__api ssize_t iio_device_get_sample_size(const struct iio_device *dev);
Paul Cercueil1a474732014-03-17 11:38:34 +0100972
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200973
974/** @brief Get the index of the given channel
975 * @param chn A pointer to an iio_channel structure
976 * @return On success, the index of the specified channel
977 * @return On error, a negative errno code is returned */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200978__api __pure long iio_channel_get_index(const struct iio_channel *chn);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200979
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200980
981/** @brief Get a pointer to a channel's data format structure
982 * @param chn A pointer to an iio_channel structure
983 * @return A pointer to the channel's iio_data_format structure */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200984__api __cnst const struct iio_data_format * iio_channel_get_data_format(
985 const struct iio_channel *chn);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100986
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200987
988/** @brief Convert the sample from hardware format to host format
989 * @param chn A pointer to an iio_channel structure
990 * @param dst A pointer to the destination buffer where the converted sample
991 * should be written
992 * @param src A pointer to the source buffer containing the sample */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200993__api void iio_channel_convert(const struct iio_channel *chn,
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100994 void *dst, const void *src);
995
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200996
997/** @brief Convert the sample from host format to hardware format
998 * @param chn A pointer to an iio_channel structure
999 * @param dst A pointer to the destination buffer where the converted sample
1000 * should be written
1001 * @param src A pointer to the source buffer containing the sample */
Paul Cercueild840d4c2014-04-07 19:38:58 +02001002__api void iio_channel_convert_inverse(const struct iio_channel *chn,
1003 void *dst, const void *src);
1004
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001005
Paul Cercueil1ce35ef2014-04-15 12:28:40 +02001006/** @brief Enumerate the debug attributes of the given device
1007 * @param dev A pointer to an iio_device structure
1008 * @return The number of debug attributes found */
1009__api __pure unsigned int iio_device_get_debug_attrs_count(
1010 const struct iio_device *dev);
1011
1012
1013/** @brief Get the debug attribute present at the given index
1014 * @param dev A pointer to an iio_device structure
1015 * @param index The index corresponding to the debug attribute
1016 * @return On success, a pointer to a static NULL-terminated string
1017 * @return If the index is invalid, NULL is returned */
1018__api __pure const char * iio_device_get_debug_attr(
1019 const struct iio_device *dev, unsigned int index);
1020
1021
Paul Cercueil001d2152014-06-03 15:24:44 +02001022/** @brief Try to find a debug attribute by its name
1023 * @param dev A pointer to an iio_device structure
1024 * @param name A NULL-terminated string corresponding to the name of the
1025 * debug attribute
1026 * @return On success, a pointer to a static NULL-terminated string
1027 * @return If the name does not correspond to any known debug attribute of the
1028 * given device, NULL is returned
1029 *
1030 * <b>NOTE:</b> This function is useful to detect the presence of a debug
1031 * attribute.
1032 * It can also be used to retrieve the name of a debug attribute as a pointer
1033 * to a static string from a dynamically allocated string. */
1034__api __pure const char * iio_device_find_debug_attr(
1035 const struct iio_device *dev, const char *name);
1036
1037
Paul Cercueil99b07cc2014-04-14 16:07:32 +02001038/** @brief Read the content of the given debug attribute
1039 * @param dev A pointer to an iio_device structure
1040 * @param attr A NULL-terminated string corresponding to the name of the
1041 * debug attribute
1042 * @param dst A pointer to the memory area where the NULL-terminated string
1043 * corresponding to the value read will be stored
1044 * @param len The available length of the memory area, in bytes
1045 * @return On success, the number of bytes written to the buffer
Paul Cercueil4ca6ddc2014-05-21 14:51:50 +02001046 * @return On error, a negative errno code is returned
1047 *
1048 * <b>NOTE:</b>By passing NULL as the "attr" argument to
1049 * iio_device_debug_attr_read, it is now possible to read all of the debug
1050 * attributes of a device.
1051 *
1052 * The buffer is filled with one block of data per debug attribute of the
1053 * device, by the order they appear in the iio_device structure.
1054 *
1055 * The first four bytes of one block correspond to a 32-bit signed value in
1056 * network order. If negative, it corresponds to the errno code that were
1057 * returned when reading the debug attribute; if positive, it corresponds
1058 * to the length of the data read. In that case, the rest of the block contains
1059 * the data. */
Paul Cercueil99b07cc2014-04-14 16:07:32 +02001060__api ssize_t iio_device_debug_attr_read(const struct iio_device *dev,
1061 const char *attr, char *dst, size_t len);
1062
1063
Paul Cercueil1b36a012014-06-05 14:39:31 +02001064/** @brief Read the content of all debug attributes
1065 * @param dev A pointer to an iio_device structure
1066 * @param cb A pointer to a callback function
1067 * @param data A pointer that will be passed to the callback function
1068 * @return On success, 0 is returned
1069 * @return On error, a negative errno code is returned
1070 *
1071 * <b>NOTE:</b> This function is especially useful when used with the network
1072 * backend, as all the debug attributes are read in one single command. */
1073__api int iio_device_debug_attr_read_all(struct iio_device *dev,
1074 int (*cb)(struct iio_device *dev, const char *attr,
1075 const char *value, size_t len, void *d),
1076 void *data);
1077
1078
Paul Cercueil99b07cc2014-04-14 16:07:32 +02001079/** @brief Set the value of the given debug attribute
1080 * @param dev A pointer to an iio_device structure
1081 * @param attr A NULL-terminated string corresponding to the name of the
1082 * debug attribute
1083 * @param src A NULL-terminated string to set the debug attribute to
1084 * @return On success, the number of bytes written
Paul Cercueil1da28122014-05-22 10:59:49 +02001085 * @return On error, a negative errno code is returned
1086 *
1087 * <b>NOTE:</b>By passing NULL as the "attr" argument to
1088 * iio_device_debug_attr_write, it is now possible to write all of the
1089 * debug attributes of a device.
1090 *
1091 * The buffer must contain one block of data per debug attribute of the device,
1092 * by the order they appear in the iio_device structure.
1093 *
1094 * The first four bytes of one block correspond to a 32-bit signed value in
1095 * network order. If negative, the debug attribute is not written; if positive,
1096 * it corresponds to the length of the data to write. In that case, the rest
1097 * of the block must contain the data. */
Paul Cercueil99b07cc2014-04-14 16:07:32 +02001098__api ssize_t iio_device_debug_attr_write(const struct iio_device *dev,
1099 const char *attr, const char *src);
1100
Paul Cercueile3960742014-04-15 16:00:50 +02001101
Paul Cercueilcecda352014-05-06 18:14:29 +02001102/** @brief Set the value of the given debug attribute
1103 * @param dev A pointer to an iio_device structure
1104 * @param attr A NULL-terminated string corresponding to the name of the
1105 * debug attribute
1106 * @param src A pointer to the data to be written
1107 * @param len The number of bytes that should be written
1108 * @return On success, the number of bytes written
1109 * @return On error, a negative errno code is returned */
1110__api ssize_t iio_device_debug_attr_write_raw(const struct iio_device *dev,
1111 const char *attr, const void *src, size_t len);
1112
1113
Paul Cercueil1b36a012014-06-05 14:39:31 +02001114/** @brief Set the values of all debug attributes
1115 * @param dev A pointer to an iio_device structure
1116 * @param cb A pointer to a callback function
1117 * @param data A pointer that will be passed to the callback function
1118 * @return On success, 0 is returned
1119 * @return On error, a negative errno code is returned
1120 *
1121 * <b>NOTE:</b> This function is especially useful when used with the network
1122 * backend, as all the debug attributes are written in one single command. */
1123__api int iio_device_debug_attr_write_all(struct iio_device *dev,
1124 ssize_t (*cb)(struct iio_device *dev,
1125 const char *attr, void *buf, size_t len, void *d),
1126 void *data);
1127
1128
Paul Cercueile3960742014-04-15 16:00:50 +02001129/** @brief Read the content of the given debug attribute
1130 * @param dev A pointer to an iio_device structure
1131 * @param attr A NULL-terminated string corresponding to the name of the
1132 * debug attribute
1133 * @param val A pointer to a bool variable where the value should be stored
1134 * @return On success, 0 is returned
1135 * @return On error, a negative errno code is returned */
1136__api int iio_device_debug_attr_read_bool(const struct iio_device *dev,
1137 const char *attr, bool *val);
1138
1139
1140/** @brief Read the content of the given debug attribute
1141 * @param dev A pointer to an iio_device structure
1142 * @param attr A NULL-terminated string corresponding to the name of the
1143 * debug attribute
1144 * @param val A pointer to a long long variable where the value should be stored
1145 * @return On success, 0 is returned
1146 * @return On error, a negative errno code is returned */
1147__api int iio_device_debug_attr_read_longlong(const struct iio_device *dev,
1148 const char *attr, long long *val);
1149
1150
1151/** @brief Read the content of the given debug attribute
1152 * @param dev A pointer to an iio_device structure
1153 * @param attr A NULL-terminated string corresponding to the name of the
1154 * debug attribute
1155 * @param val A pointer to a double variable where the value should be stored
1156 * @return On success, 0 is returned
1157 * @return On error, a negative errno code is returned */
1158__api int iio_device_debug_attr_read_double(const struct iio_device *dev,
1159 const char *attr, double *val);
1160
1161
1162/** @brief Set the value of the given debug attribute
1163 * @param dev A pointer to an iio_device structure
1164 * @param attr A NULL-terminated string corresponding to the name of the
1165 * debug attribute
1166 * @param val A bool value to set the debug attribute to
1167 * @return On success, 0 is returned
1168 * @return On error, a negative errno code is returned */
1169__api int iio_device_debug_attr_write_bool(const struct iio_device *dev,
1170 const char *attr, bool val);
1171
1172
1173/** @brief Set the value of the given debug attribute
1174 * @param dev A pointer to an iio_device structure
1175 * @param attr A NULL-terminated string corresponding to the name of the
1176 * debug attribute
1177 * @param val A long long value to set the debug attribute to
1178 * @return On success, 0 is returned
1179 * @return On error, a negative errno code is returned */
1180__api int iio_device_debug_attr_write_longlong(const struct iio_device *dev,
1181 const char *attr, long long val);
1182
1183
1184/** @brief Set the value of the given debug attribute
1185 * @param dev A pointer to an iio_device structure
1186 * @param attr A NULL-terminated string corresponding to the name of the
1187 * debug attribute
1188 * @param val A double value to set the debug attribute to
1189 * @return On success, 0 is returned
1190 * @return On error, a negative errno code is returned */
1191__api int iio_device_debug_attr_write_double(const struct iio_device *dev,
1192 const char *attr, double val);
1193
Paul Cercueil108e0aa2014-05-06 14:45:14 +02001194
1195/** @brief Identify the channel or debug attribute corresponding to a filename
1196 * @param dev A pointer to an iio_device structure
1197 * @param filename A NULL-terminated string corresponding to the filename
1198 * @param chn A pointer to a pointer of an iio_channel structure. The pointed
1199 * pointer will be set to the address of the iio_channel structure if the
1200 * filename correspond to the attribute of a channel, or NULL otherwise.
1201 * @param attr A pointer to a NULL-terminated string. The pointer
1202 * pointer will be set to point to the name of the attribute corresponding to
1203 * the filename.
1204 * @return On success, 0 is returned, and *chn and *attr are modified.
1205 * @return On error, a negative errno code is returned. *chn and *attr are not
1206 * modified. */
1207__api int iio_device_identify_filename(const struct iio_device *dev,
1208 const char *filename, struct iio_channel **chn,
1209 const char **attr);
1210
1211
Paul Cercueil14405872014-05-07 14:00:32 +02001212/** @brief Set the value of a hardware register
1213 * @param dev A pointer to an iio_device structure
1214 * @param address The address of the register
1215 * @param value The value to set the register to
1216 * @return On success, 0 is returned
1217 * @return On error, a negative errno code is returned */
1218__api int iio_device_reg_write(struct iio_device *dev,
1219 uint32_t address, uint32_t value);
1220
1221
1222/** @brief Get the value of a hardware register
1223 * @param dev A pointer to an iio_device structure
1224 * @param address The address of the register
1225 * @param value A pointer to the variable where the value will be written
1226 * @return On success, 0 is returned
1227 * @return On error, a negative errno code is returned */
1228__api int iio_device_reg_read(struct iio_device *dev,
1229 uint32_t address, uint32_t *value);
1230
1231
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001232/** @} */
1233
Paul Cercueila167e0c2014-04-08 14:50:41 +02001234#ifdef __cplusplus
1235}
1236#endif
1237
Paul Cercueil0b2ce712014-02-17 15:04:18 +01001238#endif /* __IIO_H__ */