blob: 5c11fcefe5c21c3c3bd7f83b25e7d8dc37446bee [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
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +020029#include <limits.h>
Paul Cercueile1311222014-03-12 15:46:16 +010030#include <stdint.h>
Paul Cercueil0b2ce712014-02-17 15:04:18 +010031#include <stdlib.h>
Paul Cercueil95347b92014-03-21 09:50:17 +010032#include <stddef.h>
Paul Cercueil0b2ce712014-02-17 15:04:18 +010033
Paul Cercueilc4d3fcb2016-09-16 18:08:08 +020034#if (defined(_WIN32) || defined(__MBED__))
35#ifndef _SSIZE_T_DEFINED
36typedef ptrdiff_t ssize_t;
Paul Cercueilbc2645b2015-12-11 11:29:21 +010037#define _SSIZE_T_DEFINED
Paul Cercueilb669f692014-04-04 13:42:24 +020038#endif
Paul Cercueilc4d3fcb2016-09-16 18:08:08 +020039#else
40#include <sys/types.h>
41#endif
Paul Cercueilb669f692014-04-04 13:42:24 +020042
Paul Cercueil3b1a57a2017-01-23 18:14:54 +010043#if defined(_MSC_VER) && (_MSC_VER < 1800) && !defined(__BOOL_DEFINED)
44#undef bool
45#undef false
46#undef true
47#define bool char
48#define false 0
49#define true 1
50#else
51#include <stdbool.h>
52#endif
53
Tim Harderb0babc22016-09-29 16:54:34 -040054#if defined(__GNUC__) && !defined(MATLAB_MEX_FILE) && !defined(MATLAB_LOADLIBRARY)
Paul Cercueil07396472016-04-25 18:32:28 +020055#ifndef __cnst
Paul Cercueil59f2aa32014-04-04 16:28:51 +020056#define __cnst __attribute__((const))
Paul Cercueil07396472016-04-25 18:32:28 +020057#endif
58#ifndef __pure
Paul Cercueil59f2aa32014-04-04 16:28:51 +020059#define __pure __attribute__((pure))
Paul Cercueil07396472016-04-25 18:32:28 +020060#endif
Paul Cercueil83f21892014-05-19 13:00:39 +020061#define __notused __attribute__((unused))
Paul Cercueil59f2aa32014-04-04 16:28:51 +020062#else
63#define __cnst
64#define __pure
Paul Cercueil83f21892014-05-19 13:00:39 +020065#define __notused
Paul Cercueil59f2aa32014-04-04 16:28:51 +020066#endif
67
68#ifdef _WIN32
69# ifdef LIBIIO_EXPORTS
Paul Cercueil59f2aa32014-04-04 16:28:51 +020070# define __api __declspec(dllexport)
Paul Cercueil7288c332014-04-07 16:25:11 +020071# else
72# define __api __declspec(dllimport)
Paul Cercueil59f2aa32014-04-04 16:28:51 +020073# endif
Tim Harderb0babc22016-09-29 16:54:34 -040074#elif __GNUC__ >= 4 && !defined(MATLAB_MEX_FILE) && !defined(MATLAB_LOADLIBRARY)
Paul Cercueil59f2aa32014-04-04 16:28:51 +020075# define __api __attribute__((visibility ("default")))
76#else
77# define __api
78#endif
79
Paul Cercueil0b2ce712014-02-17 15:04:18 +010080struct iio_context;
81struct iio_device;
82struct iio_channel;
Paul Cercueila689cd92014-03-20 16:37:25 +010083struct iio_buffer;
Paul Cercueil0b2ce712014-02-17 15:04:18 +010084
Lars-Peter Clausen08633732016-02-22 14:35:17 +010085struct iio_context_info;
86struct iio_scan_context;
87
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +020088/**
89 * @enum iio_chan_type
90 * @brief IIO channel type
91 *
92 * A IIO channel has a type specifying the type of data associated with the
93 * channel.
94 */
95enum iio_chan_type {
96 IIO_VOLTAGE,
97 IIO_CURRENT,
98 IIO_POWER,
99 IIO_ACCEL,
100 IIO_ANGL_VEL,
101 IIO_MAGN,
102 IIO_LIGHT,
103 IIO_INTENSITY,
104 IIO_PROXIMITY,
105 IIO_TEMP,
106 IIO_INCLI,
107 IIO_ROT,
108 IIO_ANGL,
109 IIO_TIMESTAMP,
110 IIO_CAPACITANCE,
111 IIO_ALTVOLTAGE,
112 IIO_CCT,
113 IIO_PRESSURE,
114 IIO_HUMIDITYRELATIVE,
115 IIO_ACTIVITY,
116 IIO_STEPS,
117 IIO_ENERGY,
118 IIO_DISTANCE,
119 IIO_VELOCITY,
120 IIO_CONCENTRATION,
121 IIO_RESISTANCE,
122 IIO_PH,
Andreas Brauchli6c5afdf2017-11-08 20:15:44 +0100123 IIO_UVINDEX,
124 IIO_ELECTRICALCONDUCTIVITY,
125 IIO_COUNT,
126 IIO_INDEX,
127 IIO_GRAVITY,
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +0200128 IIO_CHAN_TYPE_UNKNOWN = INT_MAX
129};
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200130
Lars-Peter Clausen093ae462016-06-21 11:43:54 +0200131/**
132 * @enum iio_modifier
133 * @brief IIO channel modifier
134 *
135 * In a addition to a type a IIO channel can optionally have a channel modifier
136 * further specifying the data type of of the channel.
137 */
138enum iio_modifier {
139 IIO_NO_MOD,
140 IIO_MOD_X,
141 IIO_MOD_Y,
142 IIO_MOD_Z,
143 IIO_MOD_X_AND_Y,
144 IIO_MOD_X_AND_Z,
145 IIO_MOD_Y_AND_Z,
146 IIO_MOD_X_AND_Y_AND_Z,
147 IIO_MOD_X_OR_Y,
148 IIO_MOD_X_OR_Z,
149 IIO_MOD_Y_OR_Z,
150 IIO_MOD_X_OR_Y_OR_Z,
151 IIO_MOD_LIGHT_BOTH,
152 IIO_MOD_LIGHT_IR,
153 IIO_MOD_ROOT_SUM_SQUARED_X_Y,
154 IIO_MOD_SUM_SQUARED_X_Y_Z,
155 IIO_MOD_LIGHT_CLEAR,
156 IIO_MOD_LIGHT_RED,
157 IIO_MOD_LIGHT_GREEN,
158 IIO_MOD_LIGHT_BLUE,
159 IIO_MOD_QUATERNION,
160 IIO_MOD_TEMP_AMBIENT,
161 IIO_MOD_TEMP_OBJECT,
162 IIO_MOD_NORTH_MAGN,
163 IIO_MOD_NORTH_TRUE,
164 IIO_MOD_NORTH_MAGN_TILT_COMP,
165 IIO_MOD_NORTH_TRUE_TILT_COMP,
166 IIO_MOD_RUNNING,
167 IIO_MOD_JOGGING,
168 IIO_MOD_WALKING,
169 IIO_MOD_STILL,
170 IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z,
171 IIO_MOD_I,
172 IIO_MOD_Q,
173 IIO_MOD_CO2,
174 IIO_MOD_VOC,
Andreas Brauchli6c5afdf2017-11-08 20:15:44 +0100175 IIO_MOD_LIGHT_UV,
Lars-Peter Clausen093ae462016-06-21 11:43:54 +0200176};
177
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200178/* ---------------------------------------------------------------------------*/
Lars-Peter Clausen08633732016-02-22 14:35:17 +0100179/* ------------------------- Scan functions ----------------------------------*/
180/** @defgroup Scan Functions for scanning available contexts
181 * @{
182 * @struct iio_scan_context
183 * @brief The scanning context
184 *
185 * @struct iio_context_info
186 * @brief The information related to a discovered context
187 */
188
189
190/** @brief Create a scan context
191 * @param backend A NULL-terminated string containing the backend to use for
192 * scanning. If NULL, all the available backends are used.
193 * @param flags Unused for now. Set to 0.
194 * @return on success, a pointer to a iio_scan_context structure
195 * @return On failure, NULL is returned and errno is set appropriately */
196__api struct iio_scan_context * iio_create_scan_context(
197 const char *backend, unsigned int flags);
198
199
200/** @brief Destroy the given scan context
201 * @param ctx A pointer to an iio_scan_context structure
202 *
203 * <b>NOTE:</b> After that function, the iio_scan_context pointer shall be invalid. */
204__api void iio_scan_context_destroy(struct iio_scan_context *ctx);
205
206
207/** @brief Enumerate available contexts
208 * @param ctx A pointer to an iio_scan_context structure
209 * @param info A pointer to a 'const struct iio_context_info **' typed variable.
210 * The pointed variable will be initialized on success.
211 * @returns On success, the number of contexts found.
212 * @returns On failure, a negative error number.
213 */
214__api ssize_t iio_scan_context_get_info_list(struct iio_scan_context *ctx,
215 struct iio_context_info ***info);
216
217
218/** @brief Free a context info list
219 * @param info A pointer to a 'const struct iio_context_info *' typed variable
220 */
221__api void iio_context_info_list_free(struct iio_context_info **info);
222
223
224/** @brief Get a description of a discovered context
225 * @param info A pointer to an iio_context_info structure
226 * @return A pointer to a static NULL-terminated string
227 */
228__api __pure const char * iio_context_info_get_description(
229 const struct iio_context_info *info);
230
231
232/** @brief Get the URI of a discovered context
233 * @param info A pointer to an iio_context_info structure
234 * @return A pointer to a static NULL-terminated string
235 */
236__api __pure const char * iio_context_info_get_uri(
237 const struct iio_context_info *info);
238
239
240/** @} *//* ------------------------------------------------------------------*/
Paul Cercueil53ed9432014-11-20 13:46:04 +0100241/* ------------------------- Top-level functions -----------------------------*/
242/** @defgroup TopLevel Top-level functions
243 * @{ */
244
245
246/** @brief Get the version of the libiio library
247 * @param major A pointer to an unsigned integer (NULL accepted)
248 * @param minor A pointer to an unsigned integer (NULL accepted)
249 * @param git_tag A pointer to a 8-characters buffer (NULL accepted) */
250__api void iio_library_get_version(unsigned int *major,
251 unsigned int *minor, char git_tag[8]);
252
253
Paul Cercueil3fa34382015-05-22 10:54:28 +0200254/** @brief Get a string description of an error code
255 * @param err The error code
256 * @param dst A pointer to the memory area where the NULL-terminated string
257 * corresponding to the error message will be stored
258 * @param len The available length of the memory area, in bytes */
259__api void iio_strerror(int err, char *dst, size_t len);
260
261
Paul Cercueil6780a982017-01-20 14:38:44 +0100262/** @brief Check if the specified backend is available
263 * @param backend The name of the backend to query
264 * @return True if the backend is available, false otherwise
265 *
266 * Introduced in version 0.9. */
267__api __cnst bool iio_has_backend(const char *backend);
268
269
270/** @brief Get the number of available backends
271 * @return The number of available backends
272 *
273 * Introduced in version 0.9. */
274__api __cnst unsigned int iio_get_backends_count(void);
275
276
277/** @brief Retrieve the name of a given backend
278 * @param index The index corresponding to the attribute
279 * @return On success, a pointer to a static NULL-terminated string
280 * @return If the index is invalid, NULL is returned
281 *
282 * Introduced in version 0.9. */
283__api __cnst const char * iio_get_backend(unsigned int index);
284
285
Paul Cercueil53ed9432014-11-20 13:46:04 +0100286/** @} *//* ------------------------------------------------------------------*/
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200287/* ------------------------- Context functions -------------------------------*/
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200288/** @defgroup Context Context
289 * @{
290 * @struct iio_context
291 * @brief Contains the representation of an IIO context */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200292
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200293
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100294/** @brief Create a context from local or remote IIO devices
295 * @return On success, A pointer to an iio_context structure
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100296 * @return On failure, NULL is returned and errno is set appropriately
Paul Cercueil8f56ea72014-10-28 15:18:18 +0100297 *
298 * <b>NOTE:</b> This function will create a network context if the IIOD_REMOTE
299 * environment variable is set to the hostname where the IIOD server runs. If
300 * set to an empty string, the server will be discovered using ZeroConf.
301 * If the environment variable is not set, a local context will be created
302 * instead. */
303__api struct iio_context * iio_create_default_context(void);
304
305
306/** @brief Create a context from local IIO devices (Linux only)
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200307 * @return On success, A pointer to an iio_context structure
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100308 * @return On failure, NULL is returned and errno is set appropriately */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200309__api struct iio_context * iio_create_local_context(void);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200310
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200311
312/** @brief Create a context from a XML file
313 * @param xml_file Path to the XML file to open
314 * @return On success, A pointer to an iio_context structure
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100315 * @return On failure, NULL is returned and errno is set appropriately
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200316 *
317 * <b>NOTE:</b> The format of the XML must comply to the one returned by
318 * iio_context_get_xml. */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200319__api struct iio_context * iio_create_xml_context(const char *xml_file);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200320
321
322/** @brief Create a context from XML data in memory
323 * @param xml Pointer to the XML data in memory
324 * @param len Length of the XML string in memory (excluding the final \0)
325 * @return On success, A pointer to an iio_context structure
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100326 * @return On failure, NULL is returned and errno is set appropriately
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200327 *
328 * <b>NOTE:</b> The format of the XML must comply to the one returned by
329 * iio_context_get_xml */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200330__api struct iio_context * iio_create_xml_context_mem(
331 const char *xml, size_t len);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200332
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200333
334/** @brief Create a context from the network
335 * @param host Hostname, IPv4 or IPv6 address where the IIO Daemon is running
336 * @return On success, a pointer to an iio_context structure
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100337 * @return On failure, NULL is returned and errno is set appropriately */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200338__api struct iio_context * iio_create_network_context(const char *host);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200339
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200340
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100341/** @brief Create a context from a URI description
342 * @param uri A URI describing the context location
343 * @return On success, a pointer to a iio_context structure
Edward Kigwana1f812432020-01-24 17:19:49 +0000344 * @return On failure, NULL is returned and errno is set appropriately
345 *
346 * <b>NOTE:</b> The following URIs are supported based on compile time backend
347 * support:
348 * - Local backend, "local:"\n
349 * Does not have an address part. For example <i>"local:"</i>
350 * - XML backend, "xml:"\n Requires a path to the XML file for the address part.
351 * For example <i>"xml:/home/user/file.xml"</i>
352 * - Network backend, "ip:"\n Requires a hostname, IPv4, or IPv6 to connect to
353 * a specific running IIO Daemon or no address part for automatic discovery
354 * when library is compiled with ZeroConf support. For example
355 * <i>"ip:192.168.2.1"</i>, <i>"ip:localhost"</i>, <i>"ip:"</i>
356 * - USB backend, "usb:"\n Requires bus, address, and interface parts separated
357 * with a dot. For example <i>"usb:3.32.5"</i>
358 * - Serial backend, "serial:"\n Requires a port, baud_rate, parity ('<b>n</b>'
359 * none, 'o' odd, 'e' even, 'm' mark, 's' space), bits (<b>8</b>), and flow
360 * ('<b>\0</b>' none, 'x' Xon Xoff, 'r' RTSCTS, 'd' DTRDSR) parts separated
361 * with a comma. For example <i>"serial:/dev/ttyUSB0,115200"</i>,
362 * <i>"serial:/dev/ttyUSB0,115200,n,8,1"</i>*/
Lars-Peter Clausen60f1c9a2016-02-22 13:20:40 +0100363__api struct iio_context * iio_create_context_from_uri(const char *uri);
364
365
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100366/** @brief Duplicate a pre-existing IIO context
367 * @param ctx A pointer to an iio_context structure
368 * @return On success, A pointer to an iio_context structure
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100369 * @return On failure, NULL is returned and errno is set appropriately */
Paul Cercueil63d5e7c2014-10-28 14:33:08 +0100370__api struct iio_context * iio_context_clone(const struct iio_context *ctx);
371
372
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200373/** @brief Destroy the given context
374 * @param ctx A pointer to an iio_context structure
375 *
376 * <b>NOTE:</b> After that function, the iio_context pointer shall be invalid. */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200377__api void iio_context_destroy(struct iio_context *ctx);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100378
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200379
Paul Cercueile45f8762014-05-02 11:19:26 +0200380/** @brief Get the version of the backend in use
381 * @param ctx A pointer to an iio_context structure
Paul Cercueild15d9952014-05-20 11:40:08 +0200382 * @param major A pointer to an unsigned integer (NULL accepted)
383 * @param minor A pointer to an unsigned integer (NULL accepted)
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200384 * @param git_tag A pointer to a 8-characters buffer (NULL accepted)
Paul Cercueile45f8762014-05-02 11:19:26 +0200385 * @return On success, 0 is returned
386 * @return On error, a negative errno code is returned */
387__api int iio_context_get_version(const struct iio_context *ctx,
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200388 unsigned int *major, unsigned int *minor, char git_tag[8]);
Paul Cercueile45f8762014-05-02 11:19:26 +0200389
390
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200391/** @brief Obtain a XML representation of the given context
392 * @param ctx A pointer to an iio_context structure
393 * @return A pointer to a static NULL-terminated string */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200394__api __pure const char * iio_context_get_xml(const struct iio_context *ctx);
395
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200396
397/** @brief Get the name of the given context
398 * @param ctx A pointer to an iio_context structure
399 * @return A pointer to a static NULL-terminated string
400 *
401 * <b>NOTE:</b>The returned string will be <b><i>local</i></b>,
402 * <b><i>xml</i></b> or <b><i>network</i></b> when the context has been
403 * created with the local, xml and network backends respectively.*/
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200404__api __pure const char * iio_context_get_name(const struct iio_context *ctx);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200405
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200406
Paul Cercueil3ac36c12015-01-08 14:44:00 +0100407/** @brief Get a description of the given context
408 * @param ctx A pointer to an iio_context structure
409 * @return A pointer to a static NULL-terminated string
410 *
411 * <b>NOTE:</b>The returned string will contain human-readable information about
412 * the current context. */
413__api __pure const char * iio_context_get_description(
414 const struct iio_context *ctx);
415
416
Paul Cercueil60b4d1b2016-11-15 15:55:41 +0100417/** @brief Get the number of context-specific attributes
418 * @param ctx A pointer to an iio_context structure
419 * @return The number of context-specific attributes
420 *
421 * Introduced in version 0.9. */
422__api __pure unsigned int iio_context_get_attrs_count(
423 const struct iio_context *ctx);
424
425
426/** @brief Retrieve the name and value of a context-specific attribute
427 * @param ctx A pointer to an iio_context structure
428 * @param index The index corresponding to the attribute
429 * @param name A pointer to a const char * pointer (NULL accepted)
430 * @param value A pointer to a const char * pointer (NULL accepted)
431 * @return On success, 0 is returned
432 * @return On error, a negative errno code is returned
433 *
434 * Introduced in version 0.9. */
435__api int iio_context_get_attr(
436 const struct iio_context *ctx, unsigned int index,
437 const char **name, const char **value);
438
439
440/** @brief Retrieve the value of a context-specific attribute
441 * @param ctx A pointer to an iio_context structure
442 * @param name The name of the context attribute to read
443 * @return On success, a pointer to a static NULL-terminated string
444 * @return If the name does not correspond to any attribute, NULL is
445 * returned
446 *
447 * Introduced in version 0.9. */
448__api const char * iio_context_get_attr_value(
449 const struct iio_context *ctx, const char *name);
450
451
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200452/** @brief Enumerate the devices found in the given context
453 * @param ctx A pointer to an iio_context structure
454 * @return The number of devices found */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200455__api __pure unsigned int iio_context_get_devices_count(
456 const struct iio_context *ctx);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200457
458
459/** @brief Get the device present at the given index
460 * @param ctx A pointer to an iio_context structure
461 * @param index The index corresponding to the device
462 * @return On success, a pointer to an iio_device structure
463 * @return If the index is invalid, NULL is returned */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200464__api __pure struct iio_device * iio_context_get_device(
465 const struct iio_context *ctx, unsigned int index);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200466
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200467
468/** @brief Try to find a device structure by its name of ID
469 * @param ctx A pointer to an iio_context structure
470 * @param name A NULL-terminated string corresponding to the name or the ID of
471 * the device to search for
472 * @return On success, a pointer to an iio_device structure
473 * @return If the name or ID does not correspond to any known device, NULL is
474 * returned */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200475__api __pure struct iio_device * iio_context_find_device(
476 const struct iio_context *ctx, const char *name);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100477
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200478
Paul Cercueil4ca73542014-06-10 16:23:29 +0200479/** @brief Set a timeout for I/O operations
480 * @param ctx A pointer to an iio_context structure
481 * @param timeout_ms A positive integer representing the time in milliseconds
Lars-Peter Clausenae7b7d92016-04-21 18:22:38 +0200482 * after which a timeout occurs. A value of 0 is used to specify that no
483 * timeout should occur.
Paul Cercueil4ca73542014-06-10 16:23:29 +0200484 * @return On success, 0 is returned
485 * @return On error, a negative errno code is returned */
486__api int iio_context_set_timeout(
487 struct iio_context *ctx, unsigned int timeout_ms);
488
489
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200490/** @} *//* ------------------------------------------------------------------*/
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200491/* ------------------------- Device functions --------------------------------*/
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200492/** @defgroup Device Device
493 * @{
494 * @struct iio_device
495 * @brief Represents a device in the IIO context */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200496
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200497
Paul Cercueil03b6c812015-04-14 16:49:06 +0200498/** @brief Retrieve a pointer to the iio_context structure
499 * @param dev A pointer to an iio_device structure
500 * @return A pointer to an iio_context structure */
501__api __pure const struct iio_context * iio_device_get_context(
502 const struct iio_device *dev);
503
504
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200505/** @brief Retrieve the device ID (e.g. <b><i>iio:device0</i></b>)
506 * @param dev A pointer to an iio_device structure
507 * @return A pointer to a static NULL-terminated string */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200508__api __pure const char * iio_device_get_id(const struct iio_device *dev);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200509
510
511/** @brief Retrieve the device name (e.g. <b><i>xadc</i></b>)
512 * @param dev A pointer to an iio_device structure
513 * @return A pointer to a static NULL-terminated string
514 *
515 * <b>NOTE:</b> if the device has no name, NULL is returned. */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200516__api __pure const char * iio_device_get_name(const struct iio_device *dev);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200517
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200518
519/** @brief Enumerate the channels of the given device
520 * @param dev A pointer to an iio_device structure
521 * @return The number of channels found */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200522__api __pure unsigned int iio_device_get_channels_count(
523 const struct iio_device *dev);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200524
525
526/** @brief Enumerate the device-specific attributes of the given device
527 * @param dev A pointer to an iio_device structure
528 * @return The number of device-specific attributes found */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200529__api __pure unsigned int iio_device_get_attrs_count(
530 const struct iio_device *dev);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200531
Matt Fornero81f04a52017-11-30 14:36:37 -0500532/** @brief Enumerate the buffer-specific attributes of the given device
533 * @param dev A pointer to an iio_device structure
534 * @return The number of buffer-specific attributes found */
535__api __pure unsigned int iio_device_get_buffer_attrs_count(
536 const struct iio_device *dev);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200537
538/** @brief Get the channel present at the given index
539 * @param dev A pointer to an iio_device structure
540 * @param index The index corresponding to the channel
541 * @return On success, a pointer to an iio_channel structure
542 * @return If the index is invalid, NULL is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200543__api __pure struct iio_channel * iio_device_get_channel(
544 const struct iio_device *dev, unsigned int index);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200545
546
547/** @brief Get the device-specific attribute present at the given index
548 * @param dev A pointer to an iio_device 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 Cercueil59f2aa32014-04-04 16:28:51 +0200552__api __pure const char * iio_device_get_attr(
553 const struct iio_device *dev, unsigned int index);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200554
Matt Fornero81f04a52017-11-30 14:36:37 -0500555/** @brief Get the buffer-specific attribute present at the given index
556 * @param dev A pointer to an iio_device structure
557 * @param index The index corresponding to the attribute
558 * @return On success, a pointer to a static NULL-terminated string
559 * @return If the index is invalid, NULL is returned */
560__api __pure const char * iio_device_get_buffer_attr(
561 const struct iio_device *dev, unsigned int index);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200562
563/** @brief Try to find a channel structure by its name of ID
564 * @param dev A pointer to an iio_device structure
565 * @param name A NULL-terminated string corresponding to the name or the ID of
566 * the channel to search for
567 * @param output True if the searched channel is output, False otherwise
568 * @return On success, a pointer to an iio_channel structure
569 * @return If the name or ID does not correspond to any known channel of the
570 * given device, NULL is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200571__api __pure struct iio_channel * iio_device_find_channel(
572 const struct iio_device *dev, const char *name, bool output);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200573
574
575/** @brief Try to find a device-specific attribute by its name
576 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200577 * @param name A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200578 * attribute
579 * @return On success, a pointer to a static NULL-terminated string
580 * @return If the name does not correspond to any known attribute of the given
581 * device, NULL is returned
582 *
583 * <b>NOTE:</b> This function is useful to detect the presence of an attribute.
584 * It can also be used to retrieve the name of an attribute as a pointer to a
585 * static string from a dynamically allocated string. */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200586__api __pure const char * iio_device_find_attr(
587 const struct iio_device *dev, const char *name);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200588
Matt Fornero81f04a52017-11-30 14:36:37 -0500589/** @brief Try to find a buffer-specific attribute by its name
590 * @param dev A pointer to an iio_device structure
591 * @param name A NULL-terminated string corresponding to the name of the
592 * attribute
593 * @return On success, a pointer to a static NULL-terminated string
594 * @return If the name does not correspond to any known attribute of the given
595 * device, NULL is returned
596 *
597 * <b>NOTE:</b> This function is useful to detect the presence of an attribute.
598 * It can also be used to retrieve the name of an attribute as a pointer to a
599 * static string from a dynamically allocated string. */
600__api __pure const char * iio_device_find_buffer_attr(
601 const struct iio_device *dev, const char *name);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200602
603/** @brief Read the content of the given device-specific attribute
604 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200605 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200606 * attribute
607 * @param dst A pointer to the memory area where the NULL-terminated string
608 * corresponding to the value read will be stored
609 * @param len The available length of the memory area, in bytes
610 * @return On success, the number of bytes written to the buffer
Paul Cercueil4ca6ddc2014-05-21 14:51:50 +0200611 * @return On error, a negative errno code is returned
612 *
613 * <b>NOTE:</b>By passing NULL as the "attr" argument to iio_device_attr_read,
614 * it is now possible to read all of the attributes of a device.
615 *
616 * The buffer is filled with one block of data per attribute of the device,
617 * by the order they appear in the iio_device structure.
618 *
619 * The first four bytes of one block correspond to a 32-bit signed value in
620 * network order. If negative, it corresponds to the errno code that were
621 * returned when reading the attribute; if positive, it corresponds to the
622 * length of the data read. In that case, the rest of the block contains
623 * the data. */
Robin Getz6b393c02020-01-27 08:48:32 -0500624__api ssize_t iio_device_attr_read(const struct iio_device *dev,
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200625 const char *attr, char *dst, size_t len);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200626
627
Paul Cercueil1b36a012014-06-05 14:39:31 +0200628/** @brief Read the content of all device-specific attributes
629 * @param dev A pointer to an iio_device structure
630 * @param cb A pointer to a callback function
631 * @param data A pointer that will be passed to the callback function
632 * @return On success, 0 is returned
633 * @return On error, a negative errno code is returned
634 *
635 * <b>NOTE:</b> This function is especially useful when used with the network
636 * backend, as all the device-specific attributes are read in one single
637 * command. */
638__api int iio_device_attr_read_all(struct iio_device *dev,
639 int (*cb)(struct iio_device *dev, const char *attr,
640 const char *value, size_t len, void *d),
641 void *data);
642
643
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200644/** @brief Read the content of the given device-specific attribute
645 * @param dev A pointer to an iio_device 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 bool 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_device_attr_read_bool(const struct iio_device *dev,
652 const char *attr, bool *val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200653
654
655/** @brief Read the content of the given device-specific attribute
656 * @param dev A pointer to an iio_device 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 val A pointer to a long long variable where the value should be stored
660 * @return On success, 0 is returned
661 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200662__api int iio_device_attr_read_longlong(const struct iio_device *dev,
663 const char *attr, long long *val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200664
665
666/** @brief Read the content of the given device-specific attribute
667 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200668 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200669 * attribute
670 * @param val A pointer to a double variable where the value should be stored
671 * @return On success, 0 is returned
672 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200673__api int iio_device_attr_read_double(const struct iio_device *dev,
674 const char *attr, double *val);
675
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200676
677/** @brief Set the value of the given device-specific attribute
678 * @param dev A pointer to an iio_device 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
681 * @param src A NULL-terminated string to set the attribute to
682 * @return On success, the number of bytes written
Paul Cercueil1da28122014-05-22 10:59:49 +0200683 * @return On error, a negative errno code is returned
684 *
685 * <b>NOTE:</b>By passing NULL as the "attr" argument to iio_device_attr_write,
686 * it is now possible to write all of the attributes of a device.
687 *
688 * The buffer must contain one block of data per attribute of the device,
689 * by the order they appear in the iio_device structure.
690 *
691 * The first four bytes of one block correspond to a 32-bit signed value in
692 * network order. If negative, the attribute is not written; if positive,
693 * it corresponds to the length of the data to write. In that case, the rest
694 * of the block must contain the data. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200695__api ssize_t iio_device_attr_write(const struct iio_device *dev,
696 const char *attr, const char *src);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200697
698
699/** @brief Set the value of the given device-specific attribute
700 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200701 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200702 * attribute
Paul Cercueilcecda352014-05-06 18:14:29 +0200703 * @param src A pointer to the data to be written
704 * @param len The number of bytes that should be written
705 * @return On success, the number of bytes written
706 * @return On error, a negative errno code is returned */
707__api ssize_t iio_device_attr_write_raw(const struct iio_device *dev,
708 const char *attr, const void *src, size_t len);
709
710
Paul Cercueil1b36a012014-06-05 14:39:31 +0200711/** @brief Set the values of all device-specific attributes
712 * @param dev A pointer to an iio_device structure
713 * @param cb A pointer to a callback function
714 * @param data A pointer that will be passed to the callback function
715 * @return On success, 0 is returned
716 * @return On error, a negative errno code is returned
717 *
718 * <b>NOTE:</b> This function is especially useful when used with the network
719 * backend, as all the device-specific attributes are written in one single
720 * command. */
721__api int iio_device_attr_write_all(struct iio_device *dev,
722 ssize_t (*cb)(struct iio_device *dev,
723 const char *attr, void *buf, size_t len, void *d),
724 void *data);
725
726
Paul Cercueilcecda352014-05-06 18:14:29 +0200727/** @brief Set the value of the given device-specific attribute
728 * @param dev A pointer to an iio_device structure
729 * @param attr A NULL-terminated string corresponding to the name of the
730 * attribute
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200731 * @param val A bool 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_device_attr_write_bool(const struct iio_device *dev,
735 const char *attr, bool val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200736
737
738/** @brief Set the value of the given device-specific attribute
739 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200740 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200741 * attribute
742 * @param val A long long value to set the attribute to
743 * @return On success, 0 is returned
744 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200745__api int iio_device_attr_write_longlong(const struct iio_device *dev,
746 const char *attr, long long val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200747
748
749/** @brief Set the value of the given device-specific attribute
750 * @param dev A pointer to an iio_device structure
Paul Cercueil30606d52014-04-14 16:04:15 +0200751 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200752 * attribute
753 * @param val A double value to set the attribute to
754 * @return On success, 0 is returned
755 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200756__api int iio_device_attr_write_double(const struct iio_device *dev,
757 const char *attr, double val);
758
Matt Fornero81f04a52017-11-30 14:36:37 -0500759/** @brief Read the content of the given buffer-specific attribute
760 * @param dev A pointer to an iio_device structure
761 * @param attr A NULL-terminated string corresponding to the name of the
762 * attribute
763 * @param dst A pointer to the memory area where the NULL-terminated string
764 * corresponding to the value read will be stored
765 * @param len The available length of the memory area, in bytes
766 * @return On success, the number of bytes written to the buffer
767 * @return On error, a negative errno code is returned
768 *
769 * <b>NOTE:</b>By passing NULL as the "attr" argument to
770 * iio_device_buffer_attr_read, it is now possible to read all of the attributes
771 * of a device.
772 *
773 * The buffer is filled with one block of data per attribute of the buffer,
774 * by the order they appear in the iio_device structure.
775 *
776 * The first four bytes of one block correspond to a 32-bit signed value in
777 * network order. If negative, it corresponds to the errno code that were
778 * returned when reading the attribute; if positive, it corresponds to the
779 * length of the data read. In that case, the rest of the block contains
780 * the data. */
Robin Getz6b393c02020-01-27 08:48:32 -0500781__api ssize_t iio_device_buffer_attr_read(const struct iio_device *dev,
Matt Fornero81f04a52017-11-30 14:36:37 -0500782 const char *attr, char *dst, size_t len);
783
784/** @brief Read the content of all buffer-specific attributes
785 * @param dev A pointer to an iio_device structure
786 * @param cb A pointer to a callback function
787 * @param data A pointer that will be passed to the callback function
788 * @return On success, 0 is returned
789 * @return On error, a negative errno code is returned
790 *
791 * <b>NOTE:</b> This function is especially useful when used with the network
792 * backend, as all the buffer-specific attributes are read in one single
793 * command. */
794__api int iio_device_buffer_attr_read_all(struct iio_device *dev,
795 int (*cb)(struct iio_device *dev, const char *attr,
796 const char *value, size_t len, void *d),
797 void *data);
798
799
800/** @brief Read the content of the given buffer-specific attribute
801 * @param dev A pointer to an iio_device structure
802 * @param attr A NULL-terminated string corresponding to the name of the
803 * attribute
804 * @param val A pointer to a bool variable where the value should be stored
805 * @return On success, 0 is returned
806 * @return On error, a negative errno code is returned */
807__api int iio_device_buffer_attr_read_bool(const struct iio_device *dev,
808 const char *attr, bool *val);
809
810
811/** @brief Read the content of the given buffer-specific attribute
812 * @param dev A pointer to an iio_device structure
813 * @param attr A NULL-terminated string corresponding to the name of the
814 * attribute
815 * @param val A pointer to a long long variable where the value should be stored
816 * @return On success, 0 is returned
817 * @return On error, a negative errno code is returned */
818__api int iio_device_buffer_attr_read_longlong(const struct iio_device *dev,
819 const char *attr, long long *val);
820
821
822/** @brief Read the content of the given buffer-specific attribute
823 * @param dev A pointer to an iio_device structure
824 * @param attr A NULL-terminated string corresponding to the name of the
825 * attribute
826 * @param val A pointer to a double variable where the value should be stored
827 * @return On success, 0 is returned
828 * @return On error, a negative errno code is returned */
829__api int iio_device_buffer_attr_read_double(const struct iio_device *dev,
830 const char *attr, double *val);
831
832
833/** @brief Set the value of the given buffer-specific attribute
834 * @param dev A pointer to an iio_device structure
835 * @param attr A NULL-terminated string corresponding to the name of the
836 * attribute
837 * @param src A NULL-terminated string to set the attribute to
838 * @return On success, the number of bytes written
839 * @return On error, a negative errno code is returned
840 *
841 * <b>NOTE:</b>By passing NULL as the "attr" argument to
842 * iio_device_buffer_attr_write, it is now possible to write all of the
843 * attributes of a device.
844 *
845 * The buffer must contain one block of data per attribute of the buffer,
846 * by the order they appear in the iio_device structure.
847 *
848 * The first four bytes of one block correspond to a 32-bit signed value in
849 * network order. If negative, the attribute is not written; if positive,
850 * it corresponds to the length of the data to write. In that case, the rest
851 * of the block must contain the data. */
852__api ssize_t iio_device_buffer_attr_write(const struct iio_device *dev,
853 const char *attr, const char *src);
854
855
856/** @brief Set the value of the given buffer-specific attribute
857 * @param dev A pointer to an iio_device structure
858 * @param attr A NULL-terminated string corresponding to the name of the
859 * attribute
860 * @param src A pointer to the data to be written
861 * @param len The number of bytes that should be written
862 * @return On success, the number of bytes written
863 * @return On error, a negative errno code is returned */
864__api ssize_t iio_device_buffer_attr_write_raw(const struct iio_device *dev,
865 const char *attr, const void *src, size_t len);
866
867
868/** @brief Set the values of all buffer-specific attributes
869 * @param dev A pointer to an iio_device structure
870 * @param cb A pointer to a callback function
871 * @param data A pointer that will be passed to the callback function
872 * @return On success, 0 is returned
873 * @return On error, a negative errno code is returned
874 *
875 * <b>NOTE:</b> This function is especially useful when used with the network
876 * backend, as all the buffer-specific attributes are written in one single
877 * command. */
878__api int iio_device_buffer_attr_write_all(struct iio_device *dev,
879 ssize_t (*cb)(struct iio_device *dev,
880 const char *attr, void *buf, size_t len, void *d),
881 void *data);
882
883
884/** @brief Set the value of the given buffer-specific attribute
885 * @param dev A pointer to an iio_device structure
886 * @param attr A NULL-terminated string corresponding to the name of the
887 * attribute
888 * @param val A bool value to set the attribute to
889 * @return On success, 0 is returned
890 * @return On error, a negative errno code is returned */
891__api int iio_device_buffer_attr_write_bool(const struct iio_device *dev,
892 const char *attr, bool val);
893
894
895/** @brief Set the value of the given buffer-specific attribute
896 * @param dev A pointer to an iio_device structure
897 * @param attr A NULL-terminated string corresponding to the name of the
898 * attribute
899 * @param val A long long value to set the attribute to
900 * @return On success, 0 is returned
901 * @return On error, a negative errno code is returned */
902__api int iio_device_buffer_attr_write_longlong(const struct iio_device *dev,
903 const char *attr, long long val);
904
905
906/** @brief Set the value of the given buffer-specific attribute
907 * @param dev A pointer to an iio_device structure
908 * @param attr A NULL-terminated string corresponding to the name of the
909 * attribute
910 * @param val A double value to set the attribute to
911 * @return On success, 0 is returned
912 * @return On error, a negative errno code is returned */
913__api int iio_device_buffer_attr_write_double(const struct iio_device *dev,
914 const char *attr, double val);
915
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200916
917/** @brief Associate a pointer to an iio_device structure
918 * @param dev A pointer to an iio_device structure
919 * @param data The pointer to be associated */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200920__api void iio_device_set_data(struct iio_device *dev, void *data);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200921
922
923/** @brief Retrieve a previously associated pointer of an iio_device structure
924 * @param dev A pointer to an iio_device structure
925 * @return The pointer previously associated if present, or NULL */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200926__api void * iio_device_get_data(const struct iio_device *dev);
927
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200928
929/** @brief Retrieve the trigger of a given device
930 * @param dev A pointer to an iio_device structure
931 * @param trigger a pointer to a pointer of an iio_device structure. The pointed
932 * pointer will be set to the address of the iio_device structure corresponding
933 * to the associated trigger device.
934 * @return On success, 0 is returned
935 * @return On error, a negative errno code is returned */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200936__api int iio_device_get_trigger(const struct iio_device *dev,
Paul Cercueil24ffa532014-03-10 12:39:58 +0100937 const struct iio_device **trigger);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200938
939
940/** @brief Associate a trigger to a given device
941 * @param dev A pointer to an iio_device structure
942 * @param trigger a pointer to the iio_device structure corresponding to the
943 * trigger that should be associated.
944 * @return On success, 0 is returned
945 * @return On error, a negative errno code is returned */
Paul Cercueil59f2aa32014-04-04 16:28:51 +0200946__api int iio_device_set_trigger(const struct iio_device *dev,
Paul Cercueil24ffa532014-03-10 12:39:58 +0100947 const struct iio_device *trigger);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100948
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200949
950/** @brief Return True if the given device is a trigger
951 * @param dev A pointer to an iio_device structure
952 * @return True if the device is a trigger, False otherwise */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200953__api __pure bool iio_device_is_trigger(const struct iio_device *dev);
954
Robin Getz4eb6b452020-01-27 09:09:45 -0500955/** @brief Configure the number of kernel buffers for a device
Romain Roffé6c385d92015-06-30 13:36:05 +0200956 *
957 * This function allows to change the number of buffers on kernel side.
958 * @param dev A pointer to an iio_device structure
959 * @param nb_buffers The number of buffers
960 * @return On success, 0 is returned
961 * @return On error, a negative errno code is returned */
962__api int iio_device_set_kernel_buffers_count(const struct iio_device *dev,
963 unsigned int nb_buffers);
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200964
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200965/** @} *//* ------------------------------------------------------------------*/
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200966/* ------------------------- Channel functions -------------------------------*/
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200967/** @defgroup Channel Channel
968 * @{
969 * @struct iio_channel
970 * @brief Represents an input or output channel of a device */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200971
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200972
Paul Cercueil03b6c812015-04-14 16:49:06 +0200973/** @brief Retrieve a pointer to the iio_device structure
974 * @param chn A pointer to an iio_channel structure
975 * @return A pointer to an iio_device structure */
976__api __pure const struct iio_device * iio_channel_get_device(
977 const struct iio_channel *chn);
978
979
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200980/** @brief Retrieve the channel ID (e.g. <b><i>voltage0</i></b>)
981 * @param chn A pointer to an iio_channel structure
982 * @return A pointer to a static NULL-terminated string */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200983__api __pure const char * iio_channel_get_id(const struct iio_channel *chn);
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200984
985
986/** @brief Retrieve the channel name (e.g. <b><i>vccint</i></b>)
987 * @param chn A pointer to an iio_channel structure
988 * @return A pointer to a static NULL-terminated string
989 *
990 * <b>NOTE:</b> if the channel has no name, NULL is returned. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200991__api __pure const char * iio_channel_get_name(const struct iio_channel *chn);
992
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200993
994/** @brief Return True if the given channel is an output channel
995 * @param chn A pointer to an iio_channel structure
996 * @return True if the channel is an output channel, False otherwise */
Paul Cercueilb4afdd92014-04-08 12:37:21 +0200997__api __pure bool iio_channel_is_output(const struct iio_channel *chn);
998
Paul Cercueil306cb1c2014-04-11 14:46:45 +0200999
Paul Cercueil85aaf482014-04-24 16:39:09 +02001000/** @brief Return True if the given channel is a scan element
1001 * @param chn A pointer to an iio_channel structure
1002 * @return True if the channel is a scan element, False otherwise
1003 *
1004 * <b>NOTE:</b> a channel that is a scan element is a channel that can
1005 * generate samples (for an input channel) or receive samples (for an output
1006 * channel) after being enabled. */
1007__api __pure bool iio_channel_is_scan_element(const struct iio_channel *chn);
1008
1009
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001010/** @brief Enumerate the channel-specific attributes of the given channel
1011 * @param chn A pointer to an iio_channel structure
1012 * @return The number of channel-specific attributes found */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001013__api __pure unsigned int iio_channel_get_attrs_count(
1014 const struct iio_channel *chn);
1015
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001016
1017/** @brief Get the channel-specific attribute present at the given index
1018 * @param chn A pointer to an iio_channel structure
1019 * @param index The index corresponding to the attribute
1020 * @return On success, a pointer to a static NULL-terminated string
1021 * @return If the index is invalid, NULL is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001022__api __pure const char * iio_channel_get_attr(
1023 const struct iio_channel *chn, unsigned int index);
1024
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001025
1026/** @brief Try to find a channel-specific attribute by its name
1027 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +02001028 * @param name A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001029 * attribute
1030 * @return On success, a pointer to a static NULL-terminated string
1031 * @return If the name does not correspond to any known attribute of the given
1032 * channel, NULL is returned
1033 *
1034 * <b>NOTE:</b> This function is useful to detect the presence of an attribute.
1035 * It can also be used to retrieve the name of an attribute as a pointer to a
1036 * static string from a dynamically allocated string. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001037__api __pure const char * iio_channel_find_attr(
1038 const struct iio_channel *chn, const char *name);
1039
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001040
Paul Cercueil6f8dbd42014-05-05 17:05:59 +02001041/** @brief Retrieve the filename of an attribute
1042 * @param chn A pointer to an iio_channel structure
1043 * @param attr a NULL-terminated string corresponding to the name of the
1044 * attribute
1045 * @return On success, a pointer to a static NULL-terminated string
1046 * @return If the attribute name is unknown, NULL is returned */
1047__api __pure const char * iio_channel_attr_get_filename(
1048 const struct iio_channel *chn, const char *attr);
1049
1050
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001051/** @brief Read the content of the given channel-specific attribute
1052 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +02001053 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001054 * attribute
1055 * @param dst A pointer to the memory area where the NULL-terminated string
1056 * corresponding to the value read will be stored
1057 * @param len The available length of the memory area, in bytes
1058 * @return On success, the number of bytes written to the buffer
Paul Cercueil4ca6ddc2014-05-21 14:51:50 +02001059 * @return On error, a negative errno code is returned
1060 *
1061 * <b>NOTE:</b>By passing NULL as the "attr" argument to iio_channel_attr_read,
1062 * it is now possible to read all of the attributes of a channel.
1063 *
1064 * The buffer is filled with one block of data per attribute of the channel,
1065 * by the order they appear in the iio_channel structure.
1066 *
1067 * The first four bytes of one block correspond to a 32-bit signed value in
1068 * network order. If negative, it corresponds to the errno code that were
1069 * returned when reading the attribute; if positive, it corresponds to the
1070 * length of the data read. In that case, the rest of the block contains
1071 * the data. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001072__api ssize_t iio_channel_attr_read(const struct iio_channel *chn,
1073 const char *attr, char *dst, size_t len);
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001074
1075
Paul Cercueil47d23d02014-06-05 14:46:20 +02001076/** @brief Read the content of all channel-specific attributes
1077 * @param chn A pointer to an iio_channel structure
1078 * @param cb A pointer to a callback function
1079 * @param data A pointer that will be passed to the callback function
1080 * @return On success, 0 is returned
1081 * @return On error, a negative errno code is returned
1082 *
1083 * <b>NOTE:</b> This function is especially useful when used with the network
1084 * backend, as all the channel-specific attributes are read in one single
1085 * command. */
1086__api int iio_channel_attr_read_all(struct iio_channel *chn,
1087 int (*cb)(struct iio_channel *chn,
1088 const char *attr, const char *val, size_t len, void *d),
1089 void *data);
1090
1091
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001092/** @brief Read the content of the given channel-specific attribute
1093 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +02001094 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001095 * attribute
1096 * @param val A pointer to a bool variable where the value should be stored
1097 * @return On success, 0 is returned
1098 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001099__api int iio_channel_attr_read_bool(const struct iio_channel *chn,
1100 const char *attr, bool *val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001101
1102
1103/** @brief Read the content of the given channel-specific attribute
1104 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +02001105 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001106 * attribute
1107 * @param val A pointer to a long long variable where the value should be stored
1108 * @return On success, 0 is returned
1109 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001110__api int iio_channel_attr_read_longlong(const struct iio_channel *chn,
1111 const char *attr, long long *val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001112
1113
1114/** @brief Read the content of the given channel-specific attribute
1115 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +02001116 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001117 * attribute
1118 * @param val A pointer to a double variable where the value should be stored
1119 * @return On success, 0 is returned
1120 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001121__api int iio_channel_attr_read_double(const struct iio_channel *chn,
1122 const char *attr, double *val);
1123
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001124
1125/** @brief Set the value of the given channel-specific attribute
1126 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +02001127 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001128 * attribute
1129 * @param src A NULL-terminated string to set the attribute to
1130 * @return On success, the number of bytes written
Paul Cercueil1da28122014-05-22 10:59:49 +02001131 * @return On error, a negative errno code is returned
1132 *
1133 * <b>NOTE:</b>By passing NULL as the "attr" argument to iio_channel_attr_write,
1134 * it is now possible to write all of the attributes of a channel.
1135 *
1136 * The buffer must contain one block of data per attribute of the channel,
1137 * by the order they appear in the iio_channel structure.
1138 *
1139 * The first four bytes of one block correspond to a 32-bit signed value in
1140 * network order. If negative, the attribute is not written; if positive,
1141 * it corresponds to the length of the data to write. In that case, the rest
1142 * of the block must contain the data. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001143__api ssize_t iio_channel_attr_write(const struct iio_channel *chn,
1144 const char *attr, const char *src);
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001145
1146
1147/** @brief Set the value of the given channel-specific attribute
1148 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +02001149 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001150 * attribute
Paul Cercueilcecda352014-05-06 18:14:29 +02001151 * @param src A pointer to the data to be written
1152 * @param len The number of bytes that should be written
1153 * @return On success, the number of bytes written
1154 * @return On error, a negative errno code is returned */
1155__api ssize_t iio_channel_attr_write_raw(const struct iio_channel *chn,
1156 const char *attr, const void *src, size_t len);
1157
1158
Paul Cercueil47d23d02014-06-05 14:46:20 +02001159/** @brief Set the values of all channel-specific attributes
1160 * @param chn A pointer to an iio_channel structure
1161 * @param cb A pointer to a callback function
1162 * @param data A pointer that will be passed to the callback function
1163 * @return On success, 0 is returned
1164 * @return On error, a negative errno code is returned
1165 *
1166 * <b>NOTE:</b> This function is especially useful when used with the network
1167 * backend, as all the channel-specific attributes are written in one single
1168 * command. */
1169__api int iio_channel_attr_write_all(struct iio_channel *chn,
1170 ssize_t (*cb)(struct iio_channel *chn,
1171 const char *attr, void *buf, size_t len, void *d),
1172 void *data);
1173
1174
Paul Cercueilcecda352014-05-06 18:14:29 +02001175/** @brief Set the value of the given channel-specific attribute
1176 * @param chn A pointer to an iio_channel structure
1177 * @param attr A NULL-terminated string corresponding to the name of the
1178 * attribute
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001179 * @param val A bool value to set the attribute to
1180 * @return On success, 0 is returned
1181 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001182__api int iio_channel_attr_write_bool(const struct iio_channel *chn,
1183 const char *attr, bool val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001184
1185
1186/** @brief Set the value of the given channel-specific attribute
1187 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +02001188 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001189 * attribute
1190 * @param val A long long value to set the attribute to
1191 * @return On success, 0 is returned
1192 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001193__api int iio_channel_attr_write_longlong(const struct iio_channel *chn,
1194 const char *attr, long long val);
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001195
1196
1197/** @brief Set the value of the given channel-specific attribute
1198 * @param chn A pointer to an iio_channel structure
Paul Cercueil30606d52014-04-14 16:04:15 +02001199 * @param attr A NULL-terminated string corresponding to the name of the
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001200 * attribute
1201 * @param val A double value to set the attribute to
1202 * @return On success, 0 is returned
1203 * @return On error, a negative errno code is returned */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001204__api int iio_channel_attr_write_double(const struct iio_channel *chn,
1205 const char *attr, double val);
1206
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001207
1208/** @brief Enable the given channel
1209 * @param chn A pointer to an iio_channel structure
1210 *
1211 * <b>NOTE:</b>Before creating an iio_buffer structure with
1212 * iio_device_create_buffer, it is required to enable at least one channel of
1213 * the device to read from. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001214__api void iio_channel_enable(struct iio_channel *chn);
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001215
1216
1217/** @brief Disable the given channel
1218 * @param chn A pointer to an iio_channel structure */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001219__api void iio_channel_disable(struct iio_channel *chn);
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001220
1221
1222/** @brief Returns True if the channel is enabled
1223 * @param chn A pointer to an iio_channel structure
1224 * @return True if the channel is enabled, False otherwise */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001225__api bool iio_channel_is_enabled(const struct iio_channel *chn);
1226
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001227
Robin Getz4eb6b452020-01-27 09:09:45 -05001228/** @brief Demultiplex the samples of a given channel
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001229 * @param chn A pointer to an iio_channel structure
1230 * @param buffer A pointer to an iio_buffer structure
1231 * @param dst A pointer to the memory area where the demultiplexed data will be
1232 * stored
1233 * @param len The available length of the memory area, in bytes
1234 * @return The size of the demultiplexed data, in bytes */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001235__api size_t iio_channel_read_raw(const struct iio_channel *chn,
1236 struct iio_buffer *buffer, void *dst, size_t len);
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001237
1238
Robin Getz4eb6b452020-01-27 09:09:45 -05001239/** @brief Demultiplex and convert the samples of a given channel
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001240 * @param chn A pointer to an iio_channel structure
1241 * @param buffer A pointer to an iio_buffer structure
1242 * @param dst A pointer to the memory area where the converted data will be
1243 * stored
1244 * @param len The available length of the memory area, in bytes
1245 * @return The size of the converted data, in bytes */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001246__api size_t iio_channel_read(const struct iio_channel *chn,
1247 struct iio_buffer *buffer, void *dst, size_t len);
1248
1249
Robin Getz4eb6b452020-01-27 09:09:45 -05001250/** @brief Multiplex the samples of a given channel
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001251 * @param chn A pointer to an iio_channel structure
1252 * @param buffer A pointer to an iio_buffer structure
1253 * @param src A pointer to the memory area where the sequential data will
1254 * be read from
1255 * @param len The length of the memory area, in bytes
1256 * @return The number of bytes actually multiplexed */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001257__api size_t iio_channel_write_raw(const struct iio_channel *chn,
1258 struct iio_buffer *buffer, const void *src, size_t len);
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001259
1260
Robin Getz4eb6b452020-01-27 09:09:45 -05001261/** @brief Convert and multiplex the samples of a given channel
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001262 * @param chn A pointer to an iio_channel structure
1263 * @param buffer A pointer to an iio_buffer structure
1264 * @param src A pointer to the memory area where the sequential data will
1265 * be read from
1266 * @param len The length of the memory area, in bytes
1267 * @return The number of bytes actually converted and multiplexed */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001268__api size_t iio_channel_write(const struct iio_channel *chn,
1269 struct iio_buffer *buffer, const void *src, size_t len);
1270
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001271
1272/** @brief Associate a pointer to an iio_channel structure
1273 * @param chn A pointer to an iio_channel structure
1274 * @param data The pointer to be associated */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001275__api void iio_channel_set_data(struct iio_channel *chn, void *data);
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001276
1277
1278/** @brief Retrieve a previously associated pointer of an iio_channel structure
1279 * @param chn A pointer to an iio_channel structure
1280 * @return The pointer previously associated if present, or NULL */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001281__api void * iio_channel_get_data(const struct iio_channel *chn);
1282
1283
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +02001284/** @brief Get the type of the given channel
1285 * @param chn A pointer to an iio_channel structure
1286 * @return The type of the channel */
1287__api __pure enum iio_chan_type iio_channel_get_type(
1288 const struct iio_channel *chn);
1289
1290
Lars-Peter Clausen093ae462016-06-21 11:43:54 +02001291/** @brief Get the modifier type of the given channel
1292 * @param chn A pointer to an iio_channel structure
1293 * @return The modifier type of the channel */
1294__api __pure enum iio_modifier iio_channel_get_modifier(
1295 const struct iio_channel *chn);
1296
1297
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001298/** @} *//* ------------------------------------------------------------------*/
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001299/* ------------------------- Buffer functions --------------------------------*/
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001300/** @defgroup Buffer Buffer
1301 * @{
1302 * @struct iio_buffer
1303 * @brief An input or output buffer, used to read or write samples */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001304
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001305
Paul Cercueil03b6c812015-04-14 16:49:06 +02001306/** @brief Retrieve a pointer to the iio_device structure
1307 * @param buf A pointer to an iio_buffer structure
1308 * @return A pointer to an iio_device structure */
1309__api __pure const struct iio_device * iio_buffer_get_device(
1310 const struct iio_buffer *buf);
1311
1312
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001313/** @brief Create an input or output buffer associated to the given device
1314 * @param dev A pointer to an iio_device structure
1315 * @param samples_count The number of samples that the buffer should contain
Paul Cercueil2004eaa2014-05-22 14:04:39 +02001316 * @param cyclic If True, enable cyclic mode
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001317 * @return On success, a pointer to an iio_buffer structure
Paul Cercueil3106e892014-04-30 11:29:51 +02001318 * @return On error, NULL is returned, and errno is set to the error code
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001319 *
1320 * <b>NOTE:</b> Channels that have to be written to / read from must be enabled
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001321 * before creating the buffer. */
1322__api struct iio_buffer * iio_device_create_buffer(const struct iio_device *dev,
Paul Cercueil2004eaa2014-05-22 14:04:39 +02001323 size_t samples_count, bool cyclic);
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001324
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001325
1326/** @brief Destroy the given buffer
1327 * @param buf A pointer to an iio_buffer structure
1328 *
1329 * <b>NOTE:</b> After that function, the iio_buffer pointer shall be invalid. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001330__api void iio_buffer_destroy(struct iio_buffer *buf);
1331
Romain Roffé6a881702015-06-30 16:25:43 +02001332/** @brief Get a pollable file descriptor
1333 *
1334 * Can be used to know when iio_buffer_refill() or iio_buffer_push() can be
1335 * called
1336 * @param buf A pointer to an iio_buffer structure
1337 * @return On success, valid file descriptor
1338 * @return On error, a negative errno code is returned
1339 */
1340__api int iio_buffer_get_poll_fd(struct iio_buffer *buf);
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001341
Romain Roffé0ea038d2015-06-30 13:35:38 +02001342/** @brief Make iio_buffer_refill() and iio_buffer_push() blocking or not
1343 *
Rémi Lefèvrea9ef6762016-12-19 16:29:38 +01001344 * After this function has been called with blocking == false,
Romain Roffé0ea038d2015-06-30 13:35:38 +02001345 * iio_buffer_refill() and iio_buffer_push() will return -EAGAIN if no data is
1346 * ready.
1347 * A device is blocking by default.
1348 * @param buf A pointer to an iio_buffer structure
1349 * @param blocking true if the buffer API should be blocking, else false
1350 * @return On success, 0
1351 * @return On error, a negative errno code is returned
1352 */
1353__api int iio_buffer_set_blocking_mode(struct iio_buffer *buf, bool blocking);
1354
1355
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001356/** @brief Fetch more samples from the hardware
1357 * @param buf A pointer to an iio_buffer structure
1358 * @return On success, the number of bytes read is returned
1359 * @return On error, a negative errno code is returned
1360 *
1361 * <b>NOTE:</b> Only valid for input buffers */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001362__api ssize_t iio_buffer_refill(struct iio_buffer *buf);
1363
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001364
1365/** @brief Send the samples to the hardware
1366 * @param buf A pointer to an iio_buffer structure
1367 * @return On success, the number of bytes written is returned
1368 * @return On error, a negative errno code is returned
1369 *
1370 * <b>NOTE:</b> Only valid for output buffers */
Paul Cercueil0183b6e2014-05-20 13:21:46 +02001371__api ssize_t iio_buffer_push(struct iio_buffer *buf);
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001372
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001373
Paul Cercueil8e7b4192015-08-05 13:03:35 +02001374/** @brief Send a given number of samples to the hardware
1375 * @param buf A pointer to an iio_buffer structure
1376 * @param samples_count The number of samples to submit
1377 * @return On success, the number of bytes written is returned
1378 * @return On error, a negative errno code is returned
1379 *
1380 * <b>NOTE:</b> Only valid for output buffers */
1381__api ssize_t iio_buffer_push_partial(struct iio_buffer *buf,
1382 size_t samples_count);
1383
Lars-Peter Clausen48c01602016-04-20 13:10:05 +02001384/** @brief Cancel all buffer operations
1385 * @param buf The buffer for which operations should be canceled
1386 *
1387 * This function cancels all outstanding buffer operations previously scheduled.
1388 * This means any pending iio_buffer_push() or iio_buffer_refill() operation
1389 * will abort and return immediately, any further invocations of these functions
1390 * on the same buffer will return immediately with an error.
1391 *
1392 * Usually iio_buffer_push() and iio_buffer_refill() will block until either all
1393 * data has been transferred or a timeout occurs. This can depending on the
1394 * configuration take a significant amount of time. iio_buffer_cancel() is
1395 * useful to bypass these conditions if the buffer operation is supposed to be
1396 * stopped in response to an external event (e.g. user input).
1397 *
1398 * To be able to capture additional data after calling this function the buffer
1399 * should be destroyed and then re-created.
1400 *
1401 * This function can be called multiple times for the same buffer, but all but
1402 * the first invocation will be without additional effect.
1403 *
1404 * This function is thread-safe, but not signal-safe, i.e. it must not be called
1405 * from a signal handler.
1406 */
1407__api void iio_buffer_cancel(struct iio_buffer *buf);
1408
Paul Cercueil8e7b4192015-08-05 13:03:35 +02001409
Paul Cercueil6d927162014-04-16 15:53:22 +02001410/** @brief Get the start address of the buffer
1411 * @param buf A pointer to an iio_buffer structure
1412 * @return A pointer corresponding to the start address of the buffer */
Paul Cercueil49106002015-03-04 13:19:20 +01001413__api void * iio_buffer_start(const struct iio_buffer *buf);
Paul Cercueil6d927162014-04-16 15:53:22 +02001414
1415
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001416/** @brief Find the first sample of a channel in a buffer
1417 * @param buf A pointer to an iio_buffer structure
1418 * @param chn A pointer to an iio_channel structure
1419 * @return A pointer to the first sample found, or to the end of the buffer if
1420 * no sample for the given channel is present in the buffer
1421 *
David Frey4cbd8772018-09-28 12:02:42 -07001422 * <b>NOTE:</b> This function, coupled with iio_buffer_step and iio_buffer_end,
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001423 * can be used to iterate on all the samples of a given channel present in the
1424 * buffer, doing the following:
1425 *
1426 * @verbatim
1427 for (void *ptr = iio_buffer_first(buffer, chn); ptr < iio_buffer_end(buffer); ptr += iio_buffer_step(buffer)) {
1428 ....
1429 }
1430 @endverbatim */
1431__api void * iio_buffer_first(const struct iio_buffer *buf,
1432 const struct iio_channel *chn);
1433
1434
1435/** @brief Get the step size between two samples of one channel
1436 * @param buf A pointer to an iio_buffer structure
1437 * @return the difference between the addresses of two consecutive samples of
1438 * one same channel */
1439__api ptrdiff_t iio_buffer_step(const struct iio_buffer *buf);
1440
1441
1442/** @brief Get the address that follows the last sample in a buffer
1443 * @param buf A pointer to an iio_buffer structure
1444 * @return A pointer corresponding to the address that follows the last sample
1445 * present in the buffer */
1446__api void * iio_buffer_end(const struct iio_buffer *buf);
1447
1448
1449/** @brief Call the supplied callback for each sample found in a buffer
1450 * @param buf A pointer to an iio_buffer structure
1451 * @param callback A pointer to a function to call for each sample found
1452 * @param data A user-specified pointer that will be passed to the callback
Lucas Magasweran9c1844e2016-08-31 11:45:08 -07001453 * @return number of bytes processed.
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001454 *
1455 * <b>NOTE:</b> The callback receives four arguments:
1456 * * A pointer to the iio_channel structure corresponding to the sample,
1457 * * A pointer to the sample itself,
1458 * * The length of the sample in bytes,
1459 * * The user-specified pointer passed to iio_buffer_foreach_sample. */
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001460__api ssize_t iio_buffer_foreach_sample(struct iio_buffer *buf,
1461 ssize_t (*callback)(const struct iio_channel *chn,
1462 void *src, size_t bytes, void *d), void *data);
1463
1464
Paul Cercueila2d4bad2014-05-27 10:15:29 +02001465/** @brief Associate a pointer to an iio_buffer structure
1466 * @param buf A pointer to an iio_buffer structure
1467 * @param data The pointer to be associated */
1468__api void iio_buffer_set_data(struct iio_buffer *buf, void *data);
1469
1470
1471/** @brief Retrieve a previously associated pointer of an iio_buffer structure
1472 * @param buf A pointer to an iio_buffer structure
1473 * @return The pointer previously associated if present, or NULL */
1474__api void * iio_buffer_get_data(const struct iio_buffer *buf);
1475
1476
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001477/** @} *//* ------------------------------------------------------------------*/
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001478/* ------------------------- Low-level functions -----------------------------*/
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001479/** @defgroup Debug Debug and low-level functions
1480 * @{
1481 * @struct iio_data_format
1482 * @brief Contains the format of a data sample.
1483 *
1484 * The different fields inform about the correct way to convert one sample from
1485 * its raw format (as read from / generated by the hardware) to its real-world
1486 * value.
1487 */
1488struct iio_data_format {
1489 /** @brief Total length of the sample, in bits */
1490 unsigned int length;
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001491
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001492 /** @brief Length of valuable data in the sample, in bits */
1493 unsigned int bits;
1494
1495 /** @brief Right-shift to apply when converting sample */
1496 unsigned int shift;
1497
1498 /** @brief Contains True if the sample is signed */
1499 bool is_signed;
1500
Michael Hennerichfa3c6f62014-08-13 11:21:23 +02001501 /** @brief Contains True if the sample is fully defined, sign extended, etc. */
1502 bool is_fully_defined;
1503
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001504 /** @brief Contains True if the sample is in big-endian format */
1505 bool is_be;
1506
1507 /** @brief Contains True if the sample should be scaled when converted */
1508 bool with_scale;
1509
1510 /** @brief Contains the scale to apply if with_scale is set */
1511 double scale;
Lucas Magasweran77fe2912016-08-29 13:47:29 -07001512
1513 /** @brief Number of times length repeats (added in v0.8) */
1514 unsigned int repeat;
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001515};
1516
1517
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001518/** @brief Get the current sample size
1519 * @param dev A pointer to an iio_device structure
1520 * @return On success, the sample size in bytes
1521 * @return On error, a negative errno code is returned
1522 *
1523 * <b>NOTE:</b> The sample size is not constant and will change when channels
1524 * get enabled or disabled. */
Paul Cercueil59f2aa32014-04-04 16:28:51 +02001525__api ssize_t iio_device_get_sample_size(const struct iio_device *dev);
Paul Cercueil1a474732014-03-17 11:38:34 +01001526
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001527
1528/** @brief Get the index of the given channel
1529 * @param chn A pointer to an iio_channel structure
1530 * @return On success, the index of the specified channel
1531 * @return On error, a negative errno code is returned */
Paul Cercueil59f2aa32014-04-04 16:28:51 +02001532__api __pure long iio_channel_get_index(const struct iio_channel *chn);
Paul Cercueilb4afdd92014-04-08 12:37:21 +02001533
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001534
1535/** @brief Get a pointer to a channel's data format structure
1536 * @param chn A pointer to an iio_channel structure
1537 * @return A pointer to the channel's iio_data_format structure */
Paul Cercueil59f2aa32014-04-04 16:28:51 +02001538__api __cnst const struct iio_data_format * iio_channel_get_data_format(
1539 const struct iio_channel *chn);
Paul Cercueil0b2ce712014-02-17 15:04:18 +01001540
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001541
1542/** @brief Convert the sample from hardware format to host format
1543 * @param chn A pointer to an iio_channel structure
1544 * @param dst A pointer to the destination buffer where the converted sample
1545 * should be written
1546 * @param src A pointer to the source buffer containing the sample */
Paul Cercueil59f2aa32014-04-04 16:28:51 +02001547__api void iio_channel_convert(const struct iio_channel *chn,
Paul Cercueil2917ffb2014-03-21 15:47:12 +01001548 void *dst, const void *src);
1549
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001550
1551/** @brief Convert the sample from host format to hardware format
1552 * @param chn A pointer to an iio_channel structure
1553 * @param dst A pointer to the destination buffer where the converted sample
1554 * should be written
1555 * @param src A pointer to the source buffer containing the sample */
Paul Cercueild840d4c2014-04-07 19:38:58 +02001556__api void iio_channel_convert_inverse(const struct iio_channel *chn,
1557 void *dst, const void *src);
1558
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001559
Paul Cercueil1ce35ef2014-04-15 12:28:40 +02001560/** @brief Enumerate the debug attributes of the given device
1561 * @param dev A pointer to an iio_device structure
1562 * @return The number of debug attributes found */
1563__api __pure unsigned int iio_device_get_debug_attrs_count(
1564 const struct iio_device *dev);
1565
1566
1567/** @brief Get the debug attribute present at the given index
1568 * @param dev A pointer to an iio_device structure
1569 * @param index The index corresponding to the debug attribute
1570 * @return On success, a pointer to a static NULL-terminated string
1571 * @return If the index is invalid, NULL is returned */
1572__api __pure const char * iio_device_get_debug_attr(
1573 const struct iio_device *dev, unsigned int index);
1574
1575
Paul Cercueil001d2152014-06-03 15:24:44 +02001576/** @brief Try to find a debug attribute by its name
1577 * @param dev A pointer to an iio_device structure
1578 * @param name A NULL-terminated string corresponding to the name of the
1579 * debug attribute
1580 * @return On success, a pointer to a static NULL-terminated string
1581 * @return If the name does not correspond to any known debug attribute of the
1582 * given device, NULL is returned
1583 *
1584 * <b>NOTE:</b> This function is useful to detect the presence of a debug
1585 * attribute.
1586 * It can also be used to retrieve the name of a debug attribute as a pointer
1587 * to a static string from a dynamically allocated string. */
1588__api __pure const char * iio_device_find_debug_attr(
1589 const struct iio_device *dev, const char *name);
1590
1591
Paul Cercueil99b07cc2014-04-14 16:07:32 +02001592/** @brief Read the content of the given debug attribute
1593 * @param dev A pointer to an iio_device structure
1594 * @param attr A NULL-terminated string corresponding to the name of the
1595 * debug attribute
1596 * @param dst A pointer to the memory area where the NULL-terminated string
1597 * corresponding to the value read will be stored
1598 * @param len The available length of the memory area, in bytes
1599 * @return On success, the number of bytes written to the buffer
Paul Cercueil4ca6ddc2014-05-21 14:51:50 +02001600 * @return On error, a negative errno code is returned
1601 *
1602 * <b>NOTE:</b>By passing NULL as the "attr" argument to
1603 * iio_device_debug_attr_read, it is now possible to read all of the debug
1604 * attributes of a device.
1605 *
1606 * The buffer is filled with one block of data per debug attribute of the
1607 * device, by the order they appear in the iio_device structure.
1608 *
1609 * The first four bytes of one block correspond to a 32-bit signed value in
1610 * network order. If negative, it corresponds to the errno code that were
1611 * returned when reading the debug attribute; if positive, it corresponds
1612 * to the length of the data read. In that case, the rest of the block contains
1613 * the data. */
Paul Cercueil99b07cc2014-04-14 16:07:32 +02001614__api ssize_t iio_device_debug_attr_read(const struct iio_device *dev,
1615 const char *attr, char *dst, size_t len);
1616
1617
Paul Cercueil1b36a012014-06-05 14:39:31 +02001618/** @brief Read the content of all debug attributes
1619 * @param dev A pointer to an iio_device structure
1620 * @param cb A pointer to a callback function
1621 * @param data A pointer that will be passed to the callback function
1622 * @return On success, 0 is returned
1623 * @return On error, a negative errno code is returned
1624 *
1625 * <b>NOTE:</b> This function is especially useful when used with the network
1626 * backend, as all the debug attributes are read in one single command. */
1627__api int iio_device_debug_attr_read_all(struct iio_device *dev,
1628 int (*cb)(struct iio_device *dev, const char *attr,
1629 const char *value, size_t len, void *d),
1630 void *data);
1631
1632
Paul Cercueil99b07cc2014-04-14 16:07:32 +02001633/** @brief Set the value of the given debug attribute
1634 * @param dev A pointer to an iio_device structure
1635 * @param attr A NULL-terminated string corresponding to the name of the
1636 * debug attribute
1637 * @param src A NULL-terminated string to set the debug attribute to
1638 * @return On success, the number of bytes written
Paul Cercueil1da28122014-05-22 10:59:49 +02001639 * @return On error, a negative errno code is returned
1640 *
1641 * <b>NOTE:</b>By passing NULL as the "attr" argument to
1642 * iio_device_debug_attr_write, it is now possible to write all of the
1643 * debug attributes of a device.
1644 *
1645 * The buffer must contain one block of data per debug attribute of the device,
1646 * by the order they appear in the iio_device structure.
1647 *
1648 * The first four bytes of one block correspond to a 32-bit signed value in
1649 * network order. If negative, the debug attribute is not written; if positive,
1650 * it corresponds to the length of the data to write. In that case, the rest
1651 * of the block must contain the data. */
Paul Cercueil99b07cc2014-04-14 16:07:32 +02001652__api ssize_t iio_device_debug_attr_write(const struct iio_device *dev,
1653 const char *attr, const char *src);
1654
Paul Cercueile3960742014-04-15 16:00:50 +02001655
Paul Cercueilcecda352014-05-06 18:14:29 +02001656/** @brief Set the value of the given debug attribute
1657 * @param dev A pointer to an iio_device structure
1658 * @param attr A NULL-terminated string corresponding to the name of the
1659 * debug attribute
1660 * @param src A pointer to the data to be written
1661 * @param len The number of bytes that should be written
1662 * @return On success, the number of bytes written
1663 * @return On error, a negative errno code is returned */
1664__api ssize_t iio_device_debug_attr_write_raw(const struct iio_device *dev,
1665 const char *attr, const void *src, size_t len);
1666
1667
Paul Cercueil1b36a012014-06-05 14:39:31 +02001668/** @brief Set the values of all debug attributes
1669 * @param dev A pointer to an iio_device structure
1670 * @param cb A pointer to a callback function
1671 * @param data A pointer that will be passed to the callback function
1672 * @return On success, 0 is returned
1673 * @return On error, a negative errno code is returned
1674 *
1675 * <b>NOTE:</b> This function is especially useful when used with the network
1676 * backend, as all the debug attributes are written in one single command. */
1677__api int iio_device_debug_attr_write_all(struct iio_device *dev,
1678 ssize_t (*cb)(struct iio_device *dev,
1679 const char *attr, void *buf, size_t len, void *d),
1680 void *data);
1681
1682
Paul Cercueile3960742014-04-15 16:00:50 +02001683/** @brief Read the content of the given debug attribute
1684 * @param dev A pointer to an iio_device structure
1685 * @param attr A NULL-terminated string corresponding to the name of the
1686 * debug attribute
1687 * @param val A pointer to a bool variable where the value should be stored
1688 * @return On success, 0 is returned
1689 * @return On error, a negative errno code is returned */
1690__api int iio_device_debug_attr_read_bool(const struct iio_device *dev,
1691 const char *attr, bool *val);
1692
1693
1694/** @brief Read the content of the given debug attribute
1695 * @param dev A pointer to an iio_device structure
1696 * @param attr A NULL-terminated string corresponding to the name of the
1697 * debug attribute
1698 * @param val A pointer to a long long variable where the value should be stored
1699 * @return On success, 0 is returned
1700 * @return On error, a negative errno code is returned */
1701__api int iio_device_debug_attr_read_longlong(const struct iio_device *dev,
1702 const char *attr, long long *val);
1703
1704
1705/** @brief Read the content of the given debug attribute
1706 * @param dev A pointer to an iio_device structure
1707 * @param attr A NULL-terminated string corresponding to the name of the
1708 * debug attribute
1709 * @param val A pointer to a double variable where the value should be stored
1710 * @return On success, 0 is returned
1711 * @return On error, a negative errno code is returned */
1712__api int iio_device_debug_attr_read_double(const struct iio_device *dev,
1713 const char *attr, double *val);
1714
1715
1716/** @brief Set the value of the given debug attribute
1717 * @param dev A pointer to an iio_device structure
1718 * @param attr A NULL-terminated string corresponding to the name of the
1719 * debug attribute
1720 * @param val A bool value to set the debug attribute to
1721 * @return On success, 0 is returned
1722 * @return On error, a negative errno code is returned */
1723__api int iio_device_debug_attr_write_bool(const struct iio_device *dev,
1724 const char *attr, bool val);
1725
1726
1727/** @brief Set the value of the given debug attribute
1728 * @param dev A pointer to an iio_device structure
1729 * @param attr A NULL-terminated string corresponding to the name of the
1730 * debug attribute
1731 * @param val A long long value to set the debug attribute to
1732 * @return On success, 0 is returned
1733 * @return On error, a negative errno code is returned */
1734__api int iio_device_debug_attr_write_longlong(const struct iio_device *dev,
1735 const char *attr, long long val);
1736
1737
1738/** @brief Set the value of the given debug attribute
1739 * @param dev A pointer to an iio_device structure
1740 * @param attr A NULL-terminated string corresponding to the name of the
1741 * debug attribute
1742 * @param val A double value to set the debug attribute to
1743 * @return On success, 0 is returned
1744 * @return On error, a negative errno code is returned */
1745__api int iio_device_debug_attr_write_double(const struct iio_device *dev,
1746 const char *attr, double val);
1747
Paul Cercueil108e0aa2014-05-06 14:45:14 +02001748
1749/** @brief Identify the channel or debug attribute corresponding to a filename
1750 * @param dev A pointer to an iio_device structure
1751 * @param filename A NULL-terminated string corresponding to the filename
1752 * @param chn A pointer to a pointer of an iio_channel structure. The pointed
1753 * pointer will be set to the address of the iio_channel structure if the
1754 * filename correspond to the attribute of a channel, or NULL otherwise.
1755 * @param attr A pointer to a NULL-terminated string. The pointer
1756 * pointer will be set to point to the name of the attribute corresponding to
1757 * the filename.
1758 * @return On success, 0 is returned, and *chn and *attr are modified.
1759 * @return On error, a negative errno code is returned. *chn and *attr are not
1760 * modified. */
1761__api int iio_device_identify_filename(const struct iio_device *dev,
1762 const char *filename, struct iio_channel **chn,
1763 const char **attr);
1764
1765
Paul Cercueil14405872014-05-07 14:00:32 +02001766/** @brief Set the value of a hardware register
1767 * @param dev A pointer to an iio_device structure
1768 * @param address The address of the register
1769 * @param value The value to set the register to
1770 * @return On success, 0 is returned
1771 * @return On error, a negative errno code is returned */
1772__api int iio_device_reg_write(struct iio_device *dev,
1773 uint32_t address, uint32_t value);
1774
1775
1776/** @brief Get the value of a hardware register
1777 * @param dev A pointer to an iio_device structure
1778 * @param address The address of the register
1779 * @param value A pointer to the variable where the value will be written
1780 * @return On success, 0 is returned
1781 * @return On error, a negative errno code is returned */
1782__api int iio_device_reg_read(struct iio_device *dev,
1783 uint32_t address, uint32_t *value);
1784
1785
Paul Cercueil306cb1c2014-04-11 14:46:45 +02001786/** @} */
1787
Paul Cercueila167e0c2014-04-08 14:50:41 +02001788#ifdef __cplusplus
1789}
1790#endif
1791
Paul Cercueilddf35ec2016-02-25 15:36:58 +01001792#undef __api
1793
Paul Cercueil0b2ce712014-02-17 15:04:18 +01001794#endif /* __IIO_H__ */