Enrico Granata | 19d8880 | 2019-04-29 16:28:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * libiio - Library for interfacing industrial I/O (IIO) devices |
| 3 | * |
| 4 | * Copyright (C) 2014 Analog Devices, Inc. |
| 5 | * Author: Paul Cercueil <paul.cercueil@analog.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * */ |
| 18 | |
| 19 | /** @file iio.h |
| 20 | * @brief Public interface */ |
| 21 | |
| 22 | #ifndef __IIO_H__ |
| 23 | #define __IIO_H__ |
| 24 | |
| 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
| 29 | #include <limits.h> |
| 30 | #include <stdint.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <stddef.h> |
| 33 | |
| 34 | #if (defined(_WIN32) || defined(__MBED__)) |
| 35 | #ifndef _SSIZE_T_DEFINED |
| 36 | typedef ptrdiff_t ssize_t; |
| 37 | #define _SSIZE_T_DEFINED |
| 38 | #endif |
| 39 | #else |
| 40 | #include <sys/types.h> |
| 41 | #endif |
| 42 | |
| 43 | #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 | |
| 54 | #if defined(__GNUC__) && !defined(MATLAB_MEX_FILE) && !defined(MATLAB_LOADLIBRARY) |
| 55 | #ifndef __cnst |
| 56 | #define __cnst __attribute__((const)) |
| 57 | #endif |
| 58 | #ifndef __pure |
| 59 | #define __pure __attribute__((pure)) |
| 60 | #endif |
| 61 | #define __notused __attribute__((unused)) |
| 62 | #else |
| 63 | #define __cnst |
| 64 | #define __pure |
| 65 | #define __notused |
| 66 | #endif |
| 67 | |
| 68 | #ifdef _WIN32 |
| 69 | # ifdef LIBIIO_EXPORTS |
| 70 | # define __api __declspec(dllexport) |
| 71 | # else |
| 72 | # define __api __declspec(dllimport) |
| 73 | # endif |
| 74 | #elif __GNUC__ >= 4 && !defined(MATLAB_MEX_FILE) && !defined(MATLAB_LOADLIBRARY) |
| 75 | # define __api __attribute__((visibility ("default"))) |
| 76 | #else |
| 77 | # define __api |
| 78 | #endif |
| 79 | |
| 80 | struct iio_context; |
| 81 | struct iio_device; |
| 82 | struct iio_channel; |
| 83 | struct iio_buffer; |
| 84 | |
| 85 | struct iio_context_info; |
| 86 | struct iio_scan_context; |
| 87 | |
| 88 | /** |
| 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 | */ |
| 95 | enum 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, |
| 123 | IIO_UVINDEX, |
| 124 | IIO_ELECTRICALCONDUCTIVITY, |
| 125 | IIO_COUNT, |
| 126 | IIO_INDEX, |
| 127 | IIO_GRAVITY, |
| 128 | IIO_CHAN_TYPE_UNKNOWN = INT_MAX |
| 129 | }; |
| 130 | |
| 131 | /** |
| 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 | */ |
| 138 | enum 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, |
| 175 | IIO_MOD_LIGHT_UV, |
| 176 | }; |
| 177 | |
| 178 | /* ---------------------------------------------------------------------------*/ |
| 179 | /* ------------------------- 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 | /** @} *//* ------------------------------------------------------------------*/ |
| 241 | /* ------------------------- 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 | |
| 254 | /** @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 | |
| 262 | /** @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 | |
| 286 | /** @} *//* ------------------------------------------------------------------*/ |
| 287 | /* ------------------------- Context functions -------------------------------*/ |
| 288 | /** @defgroup Context Context |
| 289 | * @{ |
| 290 | * @struct iio_context |
| 291 | * @brief Contains the representation of an IIO context */ |
| 292 | |
| 293 | |
| 294 | /** @brief Create a context from local or remote IIO devices |
| 295 | * @return On success, A pointer to an iio_context structure |
| 296 | * @return On failure, NULL is returned and errno is set appropriately |
| 297 | * |
| 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) |
| 307 | * @return On success, A pointer to an iio_context structure |
| 308 | * @return On failure, NULL is returned and errno is set appropriately */ |
| 309 | __api struct iio_context * iio_create_local_context(void); |
| 310 | |
| 311 | |
| 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 |
| 315 | * @return On failure, NULL is returned and errno is set appropriately |
| 316 | * |
| 317 | * <b>NOTE:</b> The format of the XML must comply to the one returned by |
| 318 | * iio_context_get_xml. */ |
| 319 | __api struct iio_context * iio_create_xml_context(const char *xml_file); |
| 320 | |
| 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 |
| 326 | * @return On failure, NULL is returned and errno is set appropriately |
| 327 | * |
| 328 | * <b>NOTE:</b> The format of the XML must comply to the one returned by |
| 329 | * iio_context_get_xml */ |
| 330 | __api struct iio_context * iio_create_xml_context_mem( |
| 331 | const char *xml, size_t len); |
| 332 | |
| 333 | |
| 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 |
| 337 | * @return On failure, NULL is returned and errno is set appropriately */ |
| 338 | __api struct iio_context * iio_create_network_context(const char *host); |
| 339 | |
| 340 | |
| 341 | /** @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 |
| 344 | * @return On failure, NULL is returned and errno is set appropriately */ |
| 345 | __api struct iio_context * iio_create_context_from_uri(const char *uri); |
| 346 | |
| 347 | |
| 348 | /** @brief Duplicate a pre-existing IIO context |
| 349 | * @param ctx A pointer to an iio_context structure |
| 350 | * @return On success, A pointer to an iio_context structure |
| 351 | * @return On failure, NULL is returned and errno is set appropriately */ |
| 352 | __api struct iio_context * iio_context_clone(const struct iio_context *ctx); |
| 353 | |
| 354 | |
| 355 | /** @brief Destroy the given context |
| 356 | * @param ctx A pointer to an iio_context structure |
| 357 | * |
| 358 | * <b>NOTE:</b> After that function, the iio_context pointer shall be invalid. */ |
| 359 | __api void iio_context_destroy(struct iio_context *ctx); |
| 360 | |
| 361 | |
| 362 | /** @brief Get the version of the backend in use |
| 363 | * @param ctx A pointer to an iio_context structure |
| 364 | * @param major A pointer to an unsigned integer (NULL accepted) |
| 365 | * @param minor A pointer to an unsigned integer (NULL accepted) |
| 366 | * @param git_tag A pointer to a 8-characters buffer (NULL accepted) |
| 367 | * @return On success, 0 is returned |
| 368 | * @return On error, a negative errno code is returned */ |
| 369 | __api int iio_context_get_version(const struct iio_context *ctx, |
| 370 | unsigned int *major, unsigned int *minor, char git_tag[8]); |
| 371 | |
| 372 | |
| 373 | /** @brief Obtain a XML representation of the given context |
| 374 | * @param ctx A pointer to an iio_context structure |
| 375 | * @return A pointer to a static NULL-terminated string */ |
| 376 | __api __pure const char * iio_context_get_xml(const struct iio_context *ctx); |
| 377 | |
| 378 | |
| 379 | /** @brief Get the name of the given context |
| 380 | * @param ctx A pointer to an iio_context structure |
| 381 | * @return A pointer to a static NULL-terminated string |
| 382 | * |
| 383 | * <b>NOTE:</b>The returned string will be <b><i>local</i></b>, |
| 384 | * <b><i>xml</i></b> or <b><i>network</i></b> when the context has been |
| 385 | * created with the local, xml and network backends respectively.*/ |
| 386 | __api __pure const char * iio_context_get_name(const struct iio_context *ctx); |
| 387 | |
| 388 | |
| 389 | /** @brief Get a description of the given context |
| 390 | * @param ctx A pointer to an iio_context structure |
| 391 | * @return A pointer to a static NULL-terminated string |
| 392 | * |
| 393 | * <b>NOTE:</b>The returned string will contain human-readable information about |
| 394 | * the current context. */ |
| 395 | __api __pure const char * iio_context_get_description( |
| 396 | const struct iio_context *ctx); |
| 397 | |
| 398 | |
| 399 | /** @brief Get the number of context-specific attributes |
| 400 | * @param ctx A pointer to an iio_context structure |
| 401 | * @return The number of context-specific attributes |
| 402 | * |
| 403 | * Introduced in version 0.9. */ |
| 404 | __api __pure unsigned int iio_context_get_attrs_count( |
| 405 | const struct iio_context *ctx); |
| 406 | |
| 407 | |
| 408 | /** @brief Retrieve the name and value of a context-specific attribute |
| 409 | * @param ctx A pointer to an iio_context structure |
| 410 | * @param index The index corresponding to the attribute |
| 411 | * @param name A pointer to a const char * pointer (NULL accepted) |
| 412 | * @param value A pointer to a const char * pointer (NULL accepted) |
| 413 | * @return On success, 0 is returned |
| 414 | * @return On error, a negative errno code is returned |
| 415 | * |
| 416 | * Introduced in version 0.9. */ |
| 417 | __api int iio_context_get_attr( |
| 418 | const struct iio_context *ctx, unsigned int index, |
| 419 | const char **name, const char **value); |
| 420 | |
| 421 | |
| 422 | /** @brief Retrieve the value of a context-specific attribute |
| 423 | * @param ctx A pointer to an iio_context structure |
| 424 | * @param name The name of the context attribute to read |
| 425 | * @return On success, a pointer to a static NULL-terminated string |
| 426 | * @return If the name does not correspond to any attribute, NULL is |
| 427 | * returned |
| 428 | * |
| 429 | * Introduced in version 0.9. */ |
| 430 | __api const char * iio_context_get_attr_value( |
| 431 | const struct iio_context *ctx, const char *name); |
| 432 | |
| 433 | |
| 434 | /** @brief Enumerate the devices found in the given context |
| 435 | * @param ctx A pointer to an iio_context structure |
| 436 | * @return The number of devices found */ |
| 437 | __api __pure unsigned int iio_context_get_devices_count( |
| 438 | const struct iio_context *ctx); |
| 439 | |
| 440 | |
| 441 | /** @brief Get the device present at the given index |
| 442 | * @param ctx A pointer to an iio_context structure |
| 443 | * @param index The index corresponding to the device |
| 444 | * @return On success, a pointer to an iio_device structure |
| 445 | * @return If the index is invalid, NULL is returned */ |
| 446 | __api __pure struct iio_device * iio_context_get_device( |
| 447 | const struct iio_context *ctx, unsigned int index); |
| 448 | |
| 449 | |
| 450 | /** @brief Try to find a device structure by its name of ID |
| 451 | * @param ctx A pointer to an iio_context structure |
| 452 | * @param name A NULL-terminated string corresponding to the name or the ID of |
| 453 | * the device to search for |
| 454 | * @return On success, a pointer to an iio_device structure |
| 455 | * @return If the name or ID does not correspond to any known device, NULL is |
| 456 | * returned */ |
| 457 | __api __pure struct iio_device * iio_context_find_device( |
| 458 | const struct iio_context *ctx, const char *name); |
| 459 | |
| 460 | |
| 461 | /** @brief Set a timeout for I/O operations |
| 462 | * @param ctx A pointer to an iio_context structure |
| 463 | * @param timeout_ms A positive integer representing the time in milliseconds |
| 464 | * after which a timeout occurs. A value of 0 is used to specify that no |
| 465 | * timeout should occur. |
| 466 | * @return On success, 0 is returned |
| 467 | * @return On error, a negative errno code is returned */ |
| 468 | __api int iio_context_set_timeout( |
| 469 | struct iio_context *ctx, unsigned int timeout_ms); |
| 470 | |
| 471 | |
| 472 | /** @} *//* ------------------------------------------------------------------*/ |
| 473 | /* ------------------------- Device functions --------------------------------*/ |
| 474 | /** @defgroup Device Device |
| 475 | * @{ |
| 476 | * @struct iio_device |
| 477 | * @brief Represents a device in the IIO context */ |
| 478 | |
| 479 | |
| 480 | /** @brief Retrieve a pointer to the iio_context structure |
| 481 | * @param dev A pointer to an iio_device structure |
| 482 | * @return A pointer to an iio_context structure */ |
| 483 | __api __pure const struct iio_context * iio_device_get_context( |
| 484 | const struct iio_device *dev); |
| 485 | |
| 486 | |
| 487 | /** @brief Retrieve the device ID (e.g. <b><i>iio:device0</i></b>) |
| 488 | * @param dev A pointer to an iio_device structure |
| 489 | * @return A pointer to a static NULL-terminated string */ |
| 490 | __api __pure const char * iio_device_get_id(const struct iio_device *dev); |
| 491 | |
| 492 | |
| 493 | /** @brief Retrieve the device name (e.g. <b><i>xadc</i></b>) |
| 494 | * @param dev A pointer to an iio_device structure |
| 495 | * @return A pointer to a static NULL-terminated string |
| 496 | * |
| 497 | * <b>NOTE:</b> if the device has no name, NULL is returned. */ |
| 498 | __api __pure const char * iio_device_get_name(const struct iio_device *dev); |
| 499 | |
| 500 | |
| 501 | /** @brief Enumerate the channels of the given device |
| 502 | * @param dev A pointer to an iio_device structure |
| 503 | * @return The number of channels found */ |
| 504 | __api __pure unsigned int iio_device_get_channels_count( |
| 505 | const struct iio_device *dev); |
| 506 | |
| 507 | |
| 508 | /** @brief Enumerate the device-specific attributes of the given device |
| 509 | * @param dev A pointer to an iio_device structure |
| 510 | * @return The number of device-specific attributes found */ |
| 511 | __api __pure unsigned int iio_device_get_attrs_count( |
| 512 | const struct iio_device *dev); |
| 513 | |
| 514 | /** @brief Enumerate the buffer-specific attributes of the given device |
| 515 | * @param dev A pointer to an iio_device structure |
| 516 | * @return The number of buffer-specific attributes found */ |
| 517 | __api __pure unsigned int iio_device_get_buffer_attrs_count( |
| 518 | const struct iio_device *dev); |
| 519 | |
| 520 | /** @brief Get the channel present at the given index |
| 521 | * @param dev A pointer to an iio_device structure |
| 522 | * @param index The index corresponding to the channel |
| 523 | * @return On success, a pointer to an iio_channel structure |
| 524 | * @return If the index is invalid, NULL is returned */ |
| 525 | __api __pure struct iio_channel * iio_device_get_channel( |
| 526 | const struct iio_device *dev, unsigned int index); |
| 527 | |
| 528 | |
| 529 | /** @brief Get the device-specific attribute present at the given index |
| 530 | * @param dev A pointer to an iio_device structure |
| 531 | * @param index The index corresponding to the attribute |
| 532 | * @return On success, a pointer to a static NULL-terminated string |
| 533 | * @return If the index is invalid, NULL is returned */ |
| 534 | __api __pure const char * iio_device_get_attr( |
| 535 | const struct iio_device *dev, unsigned int index); |
| 536 | |
| 537 | /** @brief Get the buffer-specific attribute present at the given index |
| 538 | * @param dev A pointer to an iio_device structure |
| 539 | * @param index The index corresponding to the attribute |
| 540 | * @return On success, a pointer to a static NULL-terminated string |
| 541 | * @return If the index is invalid, NULL is returned */ |
| 542 | __api __pure const char * iio_device_get_buffer_attr( |
| 543 | const struct iio_device *dev, unsigned int index); |
| 544 | |
| 545 | /** @brief Try to find a channel structure by its name of ID |
| 546 | * @param dev A pointer to an iio_device structure |
| 547 | * @param name A NULL-terminated string corresponding to the name or the ID of |
| 548 | * the channel to search for |
| 549 | * @param output True if the searched channel is output, False otherwise |
| 550 | * @return On success, a pointer to an iio_channel structure |
| 551 | * @return If the name or ID does not correspond to any known channel of the |
| 552 | * given device, NULL is returned */ |
| 553 | __api __pure struct iio_channel * iio_device_find_channel( |
| 554 | const struct iio_device *dev, const char *name, bool output); |
| 555 | |
| 556 | |
| 557 | /** @brief Try to find a device-specific attribute by its name |
| 558 | * @param dev A pointer to an iio_device structure |
| 559 | * @param name A NULL-terminated string corresponding to the name of the |
| 560 | * attribute |
| 561 | * @return On success, a pointer to a static NULL-terminated string |
| 562 | * @return If the name does not correspond to any known attribute of the given |
| 563 | * device, NULL is returned |
| 564 | * |
| 565 | * <b>NOTE:</b> This function is useful to detect the presence of an attribute. |
| 566 | * It can also be used to retrieve the name of an attribute as a pointer to a |
| 567 | * static string from a dynamically allocated string. */ |
| 568 | __api __pure const char * iio_device_find_attr( |
| 569 | const struct iio_device *dev, const char *name); |
| 570 | |
| 571 | /** @brief Try to find a buffer-specific attribute by its name |
| 572 | * @param dev A pointer to an iio_device structure |
| 573 | * @param name A NULL-terminated string corresponding to the name of the |
| 574 | * attribute |
| 575 | * @return On success, a pointer to a static NULL-terminated string |
| 576 | * @return If the name does not correspond to any known attribute of the given |
| 577 | * device, NULL is returned |
| 578 | * |
| 579 | * <b>NOTE:</b> This function is useful to detect the presence of an attribute. |
| 580 | * It can also be used to retrieve the name of an attribute as a pointer to a |
| 581 | * static string from a dynamically allocated string. */ |
| 582 | __api __pure const char * iio_device_find_buffer_attr( |
| 583 | const struct iio_device *dev, const char *name); |
| 584 | |
| 585 | /** @brief Read the content of the given device-specific attribute |
| 586 | * @param dev A pointer to an iio_device structure |
| 587 | * @param attr A NULL-terminated string corresponding to the name of the |
| 588 | * attribute |
| 589 | * @param dst A pointer to the memory area where the NULL-terminated string |
| 590 | * corresponding to the value read will be stored |
| 591 | * @param len The available length of the memory area, in bytes |
| 592 | * @return On success, the number of bytes written to the buffer |
| 593 | * @return On error, a negative errno code is returned |
| 594 | * |
| 595 | * <b>NOTE:</b>By passing NULL as the "attr" argument to iio_device_attr_read, |
| 596 | * it is now possible to read all of the attributes of a device. |
| 597 | * |
| 598 | * The buffer is filled with one block of data per attribute of the device, |
| 599 | * by the order they appear in the iio_device structure. |
| 600 | * |
| 601 | * The first four bytes of one block correspond to a 32-bit signed value in |
| 602 | * network order. If negative, it corresponds to the errno code that were |
| 603 | * returned when reading the attribute; if positive, it corresponds to the |
| 604 | * length of the data read. In that case, the rest of the block contains |
| 605 | * the data. */ |
| 606 | __api ssize_t iio_device_attr_read(const struct iio_device *dev, |
| 607 | const char *attr, char *dst, size_t len); |
| 608 | |
| 609 | |
| 610 | /** @brief Read the content of all device-specific attributes |
| 611 | * @param dev A pointer to an iio_device structure |
| 612 | * @param cb A pointer to a callback function |
| 613 | * @param data A pointer that will be passed to the callback function |
| 614 | * @return On success, 0 is returned |
| 615 | * @return On error, a negative errno code is returned |
| 616 | * |
| 617 | * <b>NOTE:</b> This function is especially useful when used with the network |
| 618 | * backend, as all the device-specific attributes are read in one single |
| 619 | * command. */ |
| 620 | __api int iio_device_attr_read_all(struct iio_device *dev, |
| 621 | int (*cb)(struct iio_device *dev, const char *attr, |
| 622 | const char *value, size_t len, void *d), |
| 623 | void *data); |
| 624 | |
| 625 | |
| 626 | /** @brief Read the content of the given device-specific attribute |
| 627 | * @param dev A pointer to an iio_device structure |
| 628 | * @param attr A NULL-terminated string corresponding to the name of the |
| 629 | * attribute |
| 630 | * @param val A pointer to a bool variable where the value should be stored |
| 631 | * @return On success, 0 is returned |
| 632 | * @return On error, a negative errno code is returned */ |
| 633 | __api int iio_device_attr_read_bool(const struct iio_device *dev, |
| 634 | const char *attr, bool *val); |
| 635 | |
| 636 | |
| 637 | /** @brief Read the content of the given device-specific attribute |
| 638 | * @param dev A pointer to an iio_device structure |
| 639 | * @param attr A NULL-terminated string corresponding to the name of the |
| 640 | * attribute |
| 641 | * @param val A pointer to a long long variable where the value should be stored |
| 642 | * @return On success, 0 is returned |
| 643 | * @return On error, a negative errno code is returned */ |
| 644 | __api int iio_device_attr_read_longlong(const struct iio_device *dev, |
| 645 | const char *attr, long long *val); |
| 646 | |
| 647 | |
| 648 | /** @brief Read the content of the given device-specific attribute |
| 649 | * @param dev A pointer to an iio_device structure |
| 650 | * @param attr A NULL-terminated string corresponding to the name of the |
| 651 | * attribute |
| 652 | * @param val A pointer to a double variable where the value should be stored |
| 653 | * @return On success, 0 is returned |
| 654 | * @return On error, a negative errno code is returned */ |
| 655 | __api int iio_device_attr_read_double(const struct iio_device *dev, |
| 656 | const char *attr, double *val); |
| 657 | |
| 658 | |
| 659 | /** @brief Set the value of the given device-specific attribute |
| 660 | * @param dev A pointer to an iio_device structure |
| 661 | * @param attr A NULL-terminated string corresponding to the name of the |
| 662 | * attribute |
| 663 | * @param src A NULL-terminated string to set the attribute to |
| 664 | * @return On success, the number of bytes written |
| 665 | * @return On error, a negative errno code is returned |
| 666 | * |
| 667 | * <b>NOTE:</b>By passing NULL as the "attr" argument to iio_device_attr_write, |
| 668 | * it is now possible to write all of the attributes of a device. |
| 669 | * |
| 670 | * The buffer must contain one block of data per attribute of the device, |
| 671 | * by the order they appear in the iio_device structure. |
| 672 | * |
| 673 | * The first four bytes of one block correspond to a 32-bit signed value in |
| 674 | * network order. If negative, the attribute is not written; if positive, |
| 675 | * it corresponds to the length of the data to write. In that case, the rest |
| 676 | * of the block must contain the data. */ |
| 677 | __api ssize_t iio_device_attr_write(const struct iio_device *dev, |
| 678 | const char *attr, const char *src); |
| 679 | |
| 680 | |
| 681 | /** @brief Set the value of the given device-specific attribute |
| 682 | * @param dev A pointer to an iio_device structure |
| 683 | * @param attr A NULL-terminated string corresponding to the name of the |
| 684 | * attribute |
| 685 | * @param src A pointer to the data to be written |
| 686 | * @param len The number of bytes that should be written |
| 687 | * @return On success, the number of bytes written |
| 688 | * @return On error, a negative errno code is returned */ |
| 689 | __api ssize_t iio_device_attr_write_raw(const struct iio_device *dev, |
| 690 | const char *attr, const void *src, size_t len); |
| 691 | |
| 692 | |
| 693 | /** @brief Set the values of all device-specific attributes |
| 694 | * @param dev A pointer to an iio_device structure |
| 695 | * @param cb A pointer to a callback function |
| 696 | * @param data A pointer that will be passed to the callback function |
| 697 | * @return On success, 0 is returned |
| 698 | * @return On error, a negative errno code is returned |
| 699 | * |
| 700 | * <b>NOTE:</b> This function is especially useful when used with the network |
| 701 | * backend, as all the device-specific attributes are written in one single |
| 702 | * command. */ |
| 703 | __api int iio_device_attr_write_all(struct iio_device *dev, |
| 704 | ssize_t (*cb)(struct iio_device *dev, |
| 705 | const char *attr, void *buf, size_t len, void *d), |
| 706 | void *data); |
| 707 | |
| 708 | |
| 709 | /** @brief Set the value of the given device-specific attribute |
| 710 | * @param dev A pointer to an iio_device structure |
| 711 | * @param attr A NULL-terminated string corresponding to the name of the |
| 712 | * attribute |
| 713 | * @param val A bool value to set the attribute to |
| 714 | * @return On success, 0 is returned |
| 715 | * @return On error, a negative errno code is returned */ |
| 716 | __api int iio_device_attr_write_bool(const struct iio_device *dev, |
| 717 | const char *attr, bool val); |
| 718 | |
| 719 | |
| 720 | /** @brief Set the value of the given device-specific attribute |
| 721 | * @param dev A pointer to an iio_device structure |
| 722 | * @param attr A NULL-terminated string corresponding to the name of the |
| 723 | * attribute |
| 724 | * @param val A long long value to set the attribute to |
| 725 | * @return On success, 0 is returned |
| 726 | * @return On error, a negative errno code is returned */ |
| 727 | __api int iio_device_attr_write_longlong(const struct iio_device *dev, |
| 728 | const char *attr, long long val); |
| 729 | |
| 730 | |
| 731 | /** @brief Set the value of the given device-specific attribute |
| 732 | * @param dev A pointer to an iio_device structure |
| 733 | * @param attr A NULL-terminated string corresponding to the name of the |
| 734 | * attribute |
| 735 | * @param val A double value to set the attribute to |
| 736 | * @return On success, 0 is returned |
| 737 | * @return On error, a negative errno code is returned */ |
| 738 | __api int iio_device_attr_write_double(const struct iio_device *dev, |
| 739 | const char *attr, double val); |
| 740 | |
| 741 | /** @brief Read the content of the given buffer-specific attribute |
| 742 | * @param dev A pointer to an iio_device structure |
| 743 | * @param attr A NULL-terminated string corresponding to the name of the |
| 744 | * attribute |
| 745 | * @param dst A pointer to the memory area where the NULL-terminated string |
| 746 | * corresponding to the value read will be stored |
| 747 | * @param len The available length of the memory area, in bytes |
| 748 | * @return On success, the number of bytes written to the buffer |
| 749 | * @return On error, a negative errno code is returned |
| 750 | * |
| 751 | * <b>NOTE:</b>By passing NULL as the "attr" argument to |
| 752 | * iio_device_buffer_attr_read, it is now possible to read all of the attributes |
| 753 | * of a device. |
| 754 | * |
| 755 | * The buffer is filled with one block of data per attribute of the buffer, |
| 756 | * by the order they appear in the iio_device structure. |
| 757 | * |
| 758 | * The first four bytes of one block correspond to a 32-bit signed value in |
| 759 | * network order. If negative, it corresponds to the errno code that were |
| 760 | * returned when reading the attribute; if positive, it corresponds to the |
| 761 | * length of the data read. In that case, the rest of the block contains |
| 762 | * the data. */ |
| 763 | __api ssize_t iio_device_buffer_attr_read(const struct iio_device *dev, |
| 764 | const char *attr, char *dst, size_t len); |
| 765 | |
| 766 | /** @brief Read the content of all buffer-specific attributes |
| 767 | * @param dev A pointer to an iio_device structure |
| 768 | * @param cb A pointer to a callback function |
| 769 | * @param data A pointer that will be passed to the callback function |
| 770 | * @return On success, 0 is returned |
| 771 | * @return On error, a negative errno code is returned |
| 772 | * |
| 773 | * <b>NOTE:</b> This function is especially useful when used with the network |
| 774 | * backend, as all the buffer-specific attributes are read in one single |
| 775 | * command. */ |
| 776 | __api int iio_device_buffer_attr_read_all(struct iio_device *dev, |
| 777 | int (*cb)(struct iio_device *dev, const char *attr, |
| 778 | const char *value, size_t len, void *d), |
| 779 | void *data); |
| 780 | |
| 781 | |
| 782 | /** @brief Read the content of the given buffer-specific attribute |
| 783 | * @param dev A pointer to an iio_device structure |
| 784 | * @param attr A NULL-terminated string corresponding to the name of the |
| 785 | * attribute |
| 786 | * @param val A pointer to a bool variable where the value should be stored |
| 787 | * @return On success, 0 is returned |
| 788 | * @return On error, a negative errno code is returned */ |
| 789 | __api int iio_device_buffer_attr_read_bool(const struct iio_device *dev, |
| 790 | const char *attr, bool *val); |
| 791 | |
| 792 | |
| 793 | /** @brief Read the content of the given buffer-specific attribute |
| 794 | * @param dev A pointer to an iio_device structure |
| 795 | * @param attr A NULL-terminated string corresponding to the name of the |
| 796 | * attribute |
| 797 | * @param val A pointer to a long long variable where the value should be stored |
| 798 | * @return On success, 0 is returned |
| 799 | * @return On error, a negative errno code is returned */ |
| 800 | __api int iio_device_buffer_attr_read_longlong(const struct iio_device *dev, |
| 801 | const char *attr, long long *val); |
| 802 | |
| 803 | |
| 804 | /** @brief Read the content of the given buffer-specific attribute |
| 805 | * @param dev A pointer to an iio_device structure |
| 806 | * @param attr A NULL-terminated string corresponding to the name of the |
| 807 | * attribute |
| 808 | * @param val A pointer to a double variable where the value should be stored |
| 809 | * @return On success, 0 is returned |
| 810 | * @return On error, a negative errno code is returned */ |
| 811 | __api int iio_device_buffer_attr_read_double(const struct iio_device *dev, |
| 812 | const char *attr, double *val); |
| 813 | |
| 814 | |
| 815 | /** @brief Set the value of the given buffer-specific attribute |
| 816 | * @param dev A pointer to an iio_device structure |
| 817 | * @param attr A NULL-terminated string corresponding to the name of the |
| 818 | * attribute |
| 819 | * @param src A NULL-terminated string to set the attribute to |
| 820 | * @return On success, the number of bytes written |
| 821 | * @return On error, a negative errno code is returned |
| 822 | * |
| 823 | * <b>NOTE:</b>By passing NULL as the "attr" argument to |
| 824 | * iio_device_buffer_attr_write, it is now possible to write all of the |
| 825 | * attributes of a device. |
| 826 | * |
| 827 | * The buffer must contain one block of data per attribute of the buffer, |
| 828 | * by the order they appear in the iio_device structure. |
| 829 | * |
| 830 | * The first four bytes of one block correspond to a 32-bit signed value in |
| 831 | * network order. If negative, the attribute is not written; if positive, |
| 832 | * it corresponds to the length of the data to write. In that case, the rest |
| 833 | * of the block must contain the data. */ |
| 834 | __api ssize_t iio_device_buffer_attr_write(const struct iio_device *dev, |
| 835 | const char *attr, const char *src); |
| 836 | |
| 837 | |
| 838 | /** @brief Set the value of the given buffer-specific attribute |
| 839 | * @param dev A pointer to an iio_device structure |
| 840 | * @param attr A NULL-terminated string corresponding to the name of the |
| 841 | * attribute |
| 842 | * @param src A pointer to the data to be written |
| 843 | * @param len The number of bytes that should be written |
| 844 | * @return On success, the number of bytes written |
| 845 | * @return On error, a negative errno code is returned */ |
| 846 | __api ssize_t iio_device_buffer_attr_write_raw(const struct iio_device *dev, |
| 847 | const char *attr, const void *src, size_t len); |
| 848 | |
| 849 | |
| 850 | /** @brief Set the values of all buffer-specific attributes |
| 851 | * @param dev A pointer to an iio_device structure |
| 852 | * @param cb A pointer to a callback function |
| 853 | * @param data A pointer that will be passed to the callback function |
| 854 | * @return On success, 0 is returned |
| 855 | * @return On error, a negative errno code is returned |
| 856 | * |
| 857 | * <b>NOTE:</b> This function is especially useful when used with the network |
| 858 | * backend, as all the buffer-specific attributes are written in one single |
| 859 | * command. */ |
| 860 | __api int iio_device_buffer_attr_write_all(struct iio_device *dev, |
| 861 | ssize_t (*cb)(struct iio_device *dev, |
| 862 | const char *attr, void *buf, size_t len, void *d), |
| 863 | void *data); |
| 864 | |
| 865 | |
| 866 | /** @brief Set the value of the given buffer-specific attribute |
| 867 | * @param dev A pointer to an iio_device structure |
| 868 | * @param attr A NULL-terminated string corresponding to the name of the |
| 869 | * attribute |
| 870 | * @param val A bool value to set the attribute to |
| 871 | * @return On success, 0 is returned |
| 872 | * @return On error, a negative errno code is returned */ |
| 873 | __api int iio_device_buffer_attr_write_bool(const struct iio_device *dev, |
| 874 | const char *attr, bool val); |
| 875 | |
| 876 | |
| 877 | /** @brief Set the value of the given buffer-specific attribute |
| 878 | * @param dev A pointer to an iio_device structure |
| 879 | * @param attr A NULL-terminated string corresponding to the name of the |
| 880 | * attribute |
| 881 | * @param val A long long value to set the attribute to |
| 882 | * @return On success, 0 is returned |
| 883 | * @return On error, a negative errno code is returned */ |
| 884 | __api int iio_device_buffer_attr_write_longlong(const struct iio_device *dev, |
| 885 | const char *attr, long long val); |
| 886 | |
| 887 | |
| 888 | /** @brief Set the value of the given buffer-specific attribute |
| 889 | * @param dev A pointer to an iio_device structure |
| 890 | * @param attr A NULL-terminated string corresponding to the name of the |
| 891 | * attribute |
| 892 | * @param val A double value to set the attribute to |
| 893 | * @return On success, 0 is returned |
| 894 | * @return On error, a negative errno code is returned */ |
| 895 | __api int iio_device_buffer_attr_write_double(const struct iio_device *dev, |
| 896 | const char *attr, double val); |
| 897 | |
| 898 | |
| 899 | /** @brief Associate a pointer to an iio_device structure |
| 900 | * @param dev A pointer to an iio_device structure |
| 901 | * @param data The pointer to be associated */ |
| 902 | __api void iio_device_set_data(struct iio_device *dev, void *data); |
| 903 | |
| 904 | |
| 905 | /** @brief Retrieve a previously associated pointer of an iio_device structure |
| 906 | * @param dev A pointer to an iio_device structure |
| 907 | * @return The pointer previously associated if present, or NULL */ |
| 908 | __api void * iio_device_get_data(const struct iio_device *dev); |
| 909 | |
| 910 | |
| 911 | /** @brief Retrieve the trigger of a given device |
| 912 | * @param dev A pointer to an iio_device structure |
| 913 | * @param trigger a pointer to a pointer of an iio_device structure. The pointed |
| 914 | * pointer will be set to the address of the iio_device structure corresponding |
| 915 | * to the associated trigger device. |
| 916 | * @return On success, 0 is returned |
| 917 | * @return On error, a negative errno code is returned */ |
| 918 | __api int iio_device_get_trigger(const struct iio_device *dev, |
| 919 | const struct iio_device **trigger); |
| 920 | |
| 921 | |
| 922 | /** @brief Associate a trigger to a given device |
| 923 | * @param dev A pointer to an iio_device structure |
| 924 | * @param trigger a pointer to the iio_device structure corresponding to the |
| 925 | * trigger that should be associated. |
| 926 | * @return On success, 0 is returned |
| 927 | * @return On error, a negative errno code is returned */ |
| 928 | __api int iio_device_set_trigger(const struct iio_device *dev, |
| 929 | const struct iio_device *trigger); |
| 930 | |
| 931 | |
| 932 | /** @brief Return True if the given device is a trigger |
| 933 | * @param dev A pointer to an iio_device structure |
| 934 | * @return True if the device is a trigger, False otherwise */ |
| 935 | __api __pure bool iio_device_is_trigger(const struct iio_device *dev); |
| 936 | |
| 937 | /** |
| 938 | * @brief Configure the number of kernel buffers for a device |
| 939 | * |
| 940 | * This function allows to change the number of buffers on kernel side. |
| 941 | * @param dev A pointer to an iio_device structure |
| 942 | * @param nb_buffers The number of buffers |
| 943 | * @return On success, 0 is returned |
| 944 | * @return On error, a negative errno code is returned */ |
| 945 | __api int iio_device_set_kernel_buffers_count(const struct iio_device *dev, |
| 946 | unsigned int nb_buffers); |
| 947 | |
| 948 | /** @} *//* ------------------------------------------------------------------*/ |
| 949 | /* ------------------------- Channel functions -------------------------------*/ |
| 950 | /** @defgroup Channel Channel |
| 951 | * @{ |
| 952 | * @struct iio_channel |
| 953 | * @brief Represents an input or output channel of a device */ |
| 954 | |
| 955 | |
| 956 | /** @brief Retrieve a pointer to the iio_device structure |
| 957 | * @param chn A pointer to an iio_channel structure |
| 958 | * @return A pointer to an iio_device structure */ |
| 959 | __api __pure const struct iio_device * iio_channel_get_device( |
| 960 | const struct iio_channel *chn); |
| 961 | |
| 962 | |
| 963 | /** @brief Retrieve the channel ID (e.g. <b><i>voltage0</i></b>) |
| 964 | * @param chn A pointer to an iio_channel structure |
| 965 | * @return A pointer to a static NULL-terminated string */ |
| 966 | __api __pure const char * iio_channel_get_id(const struct iio_channel *chn); |
| 967 | |
| 968 | |
| 969 | /** @brief Retrieve the channel name (e.g. <b><i>vccint</i></b>) |
| 970 | * @param chn A pointer to an iio_channel structure |
| 971 | * @return A pointer to a static NULL-terminated string |
| 972 | * |
| 973 | * <b>NOTE:</b> if the channel has no name, NULL is returned. */ |
| 974 | __api __pure const char * iio_channel_get_name(const struct iio_channel *chn); |
| 975 | |
| 976 | |
| 977 | /** @brief Return True if the given channel is an output channel |
| 978 | * @param chn A pointer to an iio_channel structure |
| 979 | * @return True if the channel is an output channel, False otherwise */ |
| 980 | __api __pure bool iio_channel_is_output(const struct iio_channel *chn); |
| 981 | |
| 982 | |
| 983 | /** @brief Return True if the given channel is a scan element |
| 984 | * @param chn A pointer to an iio_channel structure |
| 985 | * @return True if the channel is a scan element, False otherwise |
| 986 | * |
| 987 | * <b>NOTE:</b> a channel that is a scan element is a channel that can |
| 988 | * generate samples (for an input channel) or receive samples (for an output |
| 989 | * channel) after being enabled. */ |
| 990 | __api __pure bool iio_channel_is_scan_element(const struct iio_channel *chn); |
| 991 | |
| 992 | |
| 993 | /** @brief Enumerate the channel-specific attributes of the given channel |
| 994 | * @param chn A pointer to an iio_channel structure |
| 995 | * @return The number of channel-specific attributes found */ |
| 996 | __api __pure unsigned int iio_channel_get_attrs_count( |
| 997 | const struct iio_channel *chn); |
| 998 | |
| 999 | |
| 1000 | /** @brief Get the channel-specific attribute present at the given index |
| 1001 | * @param chn A pointer to an iio_channel structure |
| 1002 | * @param index The index corresponding to the attribute |
| 1003 | * @return On success, a pointer to a static NULL-terminated string |
| 1004 | * @return If the index is invalid, NULL is returned */ |
| 1005 | __api __pure const char * iio_channel_get_attr( |
| 1006 | const struct iio_channel *chn, unsigned int index); |
| 1007 | |
| 1008 | |
| 1009 | /** @brief Try to find a channel-specific attribute by its name |
| 1010 | * @param chn A pointer to an iio_channel structure |
| 1011 | * @param name A NULL-terminated string corresponding to the name of the |
| 1012 | * attribute |
| 1013 | * @return On success, a pointer to a static NULL-terminated string |
| 1014 | * @return If the name does not correspond to any known attribute of the given |
| 1015 | * channel, NULL is returned |
| 1016 | * |
| 1017 | * <b>NOTE:</b> This function is useful to detect the presence of an attribute. |
| 1018 | * It can also be used to retrieve the name of an attribute as a pointer to a |
| 1019 | * static string from a dynamically allocated string. */ |
| 1020 | __api __pure const char * iio_channel_find_attr( |
| 1021 | const struct iio_channel *chn, const char *name); |
| 1022 | |
| 1023 | |
| 1024 | /** @brief Retrieve the filename of an attribute |
| 1025 | * @param chn A pointer to an iio_channel structure |
| 1026 | * @param attr a NULL-terminated string corresponding to the name of the |
| 1027 | * attribute |
| 1028 | * @return On success, a pointer to a static NULL-terminated string |
| 1029 | * @return If the attribute name is unknown, NULL is returned */ |
| 1030 | __api __pure const char * iio_channel_attr_get_filename( |
| 1031 | const struct iio_channel *chn, const char *attr); |
| 1032 | |
| 1033 | |
| 1034 | /** @brief Read the content of the given channel-specific attribute |
| 1035 | * @param chn A pointer to an iio_channel structure |
| 1036 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1037 | * attribute |
| 1038 | * @param dst A pointer to the memory area where the NULL-terminated string |
| 1039 | * corresponding to the value read will be stored |
| 1040 | * @param len The available length of the memory area, in bytes |
| 1041 | * @return On success, the number of bytes written to the buffer |
| 1042 | * @return On error, a negative errno code is returned |
| 1043 | * |
| 1044 | * <b>NOTE:</b>By passing NULL as the "attr" argument to iio_channel_attr_read, |
| 1045 | * it is now possible to read all of the attributes of a channel. |
| 1046 | * |
| 1047 | * The buffer is filled with one block of data per attribute of the channel, |
| 1048 | * by the order they appear in the iio_channel structure. |
| 1049 | * |
| 1050 | * The first four bytes of one block correspond to a 32-bit signed value in |
| 1051 | * network order. If negative, it corresponds to the errno code that were |
| 1052 | * returned when reading the attribute; if positive, it corresponds to the |
| 1053 | * length of the data read. In that case, the rest of the block contains |
| 1054 | * the data. */ |
| 1055 | __api ssize_t iio_channel_attr_read(const struct iio_channel *chn, |
| 1056 | const char *attr, char *dst, size_t len); |
| 1057 | |
| 1058 | |
| 1059 | /** @brief Read the content of all channel-specific attributes |
| 1060 | * @param chn A pointer to an iio_channel structure |
| 1061 | * @param cb A pointer to a callback function |
| 1062 | * @param data A pointer that will be passed to the callback function |
| 1063 | * @return On success, 0 is returned |
| 1064 | * @return On error, a negative errno code is returned |
| 1065 | * |
| 1066 | * <b>NOTE:</b> This function is especially useful when used with the network |
| 1067 | * backend, as all the channel-specific attributes are read in one single |
| 1068 | * command. */ |
| 1069 | __api int iio_channel_attr_read_all(struct iio_channel *chn, |
| 1070 | int (*cb)(struct iio_channel *chn, |
| 1071 | const char *attr, const char *val, size_t len, void *d), |
| 1072 | void *data); |
| 1073 | |
| 1074 | |
| 1075 | /** @brief Read the content of the given channel-specific attribute |
| 1076 | * @param chn A pointer to an iio_channel structure |
| 1077 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1078 | * attribute |
| 1079 | * @param val A pointer to a bool variable where the value should be stored |
| 1080 | * @return On success, 0 is returned |
| 1081 | * @return On error, a negative errno code is returned */ |
| 1082 | __api int iio_channel_attr_read_bool(const struct iio_channel *chn, |
| 1083 | const char *attr, bool *val); |
| 1084 | |
| 1085 | |
| 1086 | /** @brief Read the content of the given channel-specific attribute |
| 1087 | * @param chn A pointer to an iio_channel structure |
| 1088 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1089 | * attribute |
| 1090 | * @param val A pointer to a long long variable where the value should be stored |
| 1091 | * @return On success, 0 is returned |
| 1092 | * @return On error, a negative errno code is returned */ |
| 1093 | __api int iio_channel_attr_read_longlong(const struct iio_channel *chn, |
| 1094 | const char *attr, long long *val); |
| 1095 | |
| 1096 | |
| 1097 | /** @brief Read the content of the given channel-specific attribute |
| 1098 | * @param chn A pointer to an iio_channel structure |
| 1099 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1100 | * attribute |
| 1101 | * @param val A pointer to a double variable where the value should be stored |
| 1102 | * @return On success, 0 is returned |
| 1103 | * @return On error, a negative errno code is returned */ |
| 1104 | __api int iio_channel_attr_read_double(const struct iio_channel *chn, |
| 1105 | const char *attr, double *val); |
| 1106 | |
| 1107 | |
| 1108 | /** @brief Set the value of the given channel-specific attribute |
| 1109 | * @param chn A pointer to an iio_channel structure |
| 1110 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1111 | * attribute |
| 1112 | * @param src A NULL-terminated string to set the attribute to |
| 1113 | * @return On success, the number of bytes written |
| 1114 | * @return On error, a negative errno code is returned |
| 1115 | * |
| 1116 | * <b>NOTE:</b>By passing NULL as the "attr" argument to iio_channel_attr_write, |
| 1117 | * it is now possible to write all of the attributes of a channel. |
| 1118 | * |
| 1119 | * The buffer must contain one block of data per attribute of the channel, |
| 1120 | * by the order they appear in the iio_channel structure. |
| 1121 | * |
| 1122 | * The first four bytes of one block correspond to a 32-bit signed value in |
| 1123 | * network order. If negative, the attribute is not written; if positive, |
| 1124 | * it corresponds to the length of the data to write. In that case, the rest |
| 1125 | * of the block must contain the data. */ |
| 1126 | __api ssize_t iio_channel_attr_write(const struct iio_channel *chn, |
| 1127 | const char *attr, const char *src); |
| 1128 | |
| 1129 | |
| 1130 | /** @brief Set the value of the given channel-specific attribute |
| 1131 | * @param chn A pointer to an iio_channel structure |
| 1132 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1133 | * attribute |
| 1134 | * @param src A pointer to the data to be written |
| 1135 | * @param len The number of bytes that should be written |
| 1136 | * @return On success, the number of bytes written |
| 1137 | * @return On error, a negative errno code is returned */ |
| 1138 | __api ssize_t iio_channel_attr_write_raw(const struct iio_channel *chn, |
| 1139 | const char *attr, const void *src, size_t len); |
| 1140 | |
| 1141 | |
| 1142 | /** @brief Set the values of all channel-specific attributes |
| 1143 | * @param chn A pointer to an iio_channel structure |
| 1144 | * @param cb A pointer to a callback function |
| 1145 | * @param data A pointer that will be passed to the callback function |
| 1146 | * @return On success, 0 is returned |
| 1147 | * @return On error, a negative errno code is returned |
| 1148 | * |
| 1149 | * <b>NOTE:</b> This function is especially useful when used with the network |
| 1150 | * backend, as all the channel-specific attributes are written in one single |
| 1151 | * command. */ |
| 1152 | __api int iio_channel_attr_write_all(struct iio_channel *chn, |
| 1153 | ssize_t (*cb)(struct iio_channel *chn, |
| 1154 | const char *attr, void *buf, size_t len, void *d), |
| 1155 | void *data); |
| 1156 | |
| 1157 | |
| 1158 | /** @brief Set the value of the given channel-specific attribute |
| 1159 | * @param chn A pointer to an iio_channel structure |
| 1160 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1161 | * attribute |
| 1162 | * @param val A bool value to set the attribute to |
| 1163 | * @return On success, 0 is returned |
| 1164 | * @return On error, a negative errno code is returned */ |
| 1165 | __api int iio_channel_attr_write_bool(const struct iio_channel *chn, |
| 1166 | const char *attr, bool val); |
| 1167 | |
| 1168 | |
| 1169 | /** @brief Set the value of the given channel-specific attribute |
| 1170 | * @param chn A pointer to an iio_channel structure |
| 1171 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1172 | * attribute |
| 1173 | * @param val A long long value to set the attribute to |
| 1174 | * @return On success, 0 is returned |
| 1175 | * @return On error, a negative errno code is returned */ |
| 1176 | __api int iio_channel_attr_write_longlong(const struct iio_channel *chn, |
| 1177 | const char *attr, long long val); |
| 1178 | |
| 1179 | |
| 1180 | /** @brief Set the value of the given channel-specific attribute |
| 1181 | * @param chn A pointer to an iio_channel structure |
| 1182 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1183 | * attribute |
| 1184 | * @param val A double value to set the attribute to |
| 1185 | * @return On success, 0 is returned |
| 1186 | * @return On error, a negative errno code is returned */ |
| 1187 | __api int iio_channel_attr_write_double(const struct iio_channel *chn, |
| 1188 | const char *attr, double val); |
| 1189 | |
| 1190 | |
| 1191 | /** @brief Enable the given channel |
| 1192 | * @param chn A pointer to an iio_channel structure |
| 1193 | * |
| 1194 | * <b>NOTE:</b>Before creating an iio_buffer structure with |
| 1195 | * iio_device_create_buffer, it is required to enable at least one channel of |
| 1196 | * the device to read from. */ |
| 1197 | __api void iio_channel_enable(struct iio_channel *chn); |
| 1198 | |
| 1199 | |
| 1200 | /** @brief Disable the given channel |
| 1201 | * @param chn A pointer to an iio_channel structure */ |
| 1202 | __api void iio_channel_disable(struct iio_channel *chn); |
| 1203 | |
| 1204 | |
| 1205 | /** @brief Returns True if the channel is enabled |
| 1206 | * @param chn A pointer to an iio_channel structure |
| 1207 | * @return True if the channel is enabled, False otherwise */ |
| 1208 | __api bool iio_channel_is_enabled(const struct iio_channel *chn); |
| 1209 | |
| 1210 | |
| 1211 | /** Demultiplex the samples of a given channel |
| 1212 | * @param chn A pointer to an iio_channel structure |
| 1213 | * @param buffer A pointer to an iio_buffer structure |
| 1214 | * @param dst A pointer to the memory area where the demultiplexed data will be |
| 1215 | * stored |
| 1216 | * @param len The available length of the memory area, in bytes |
| 1217 | * @return The size of the demultiplexed data, in bytes */ |
| 1218 | __api size_t iio_channel_read_raw(const struct iio_channel *chn, |
| 1219 | struct iio_buffer *buffer, void *dst, size_t len); |
| 1220 | |
| 1221 | |
| 1222 | /** Demultiplex and convert the samples of a given channel |
| 1223 | * @param chn A pointer to an iio_channel structure |
| 1224 | * @param buffer A pointer to an iio_buffer structure |
| 1225 | * @param dst A pointer to the memory area where the converted data will be |
| 1226 | * stored |
| 1227 | * @param len The available length of the memory area, in bytes |
| 1228 | * @return The size of the converted data, in bytes */ |
| 1229 | __api size_t iio_channel_read(const struct iio_channel *chn, |
| 1230 | struct iio_buffer *buffer, void *dst, size_t len); |
| 1231 | |
| 1232 | |
| 1233 | /** Multiplex the samples of a given channel |
| 1234 | * @param chn A pointer to an iio_channel structure |
| 1235 | * @param buffer A pointer to an iio_buffer structure |
| 1236 | * @param src A pointer to the memory area where the sequential data will |
| 1237 | * be read from |
| 1238 | * @param len The length of the memory area, in bytes |
| 1239 | * @return The number of bytes actually multiplexed */ |
| 1240 | __api size_t iio_channel_write_raw(const struct iio_channel *chn, |
| 1241 | struct iio_buffer *buffer, const void *src, size_t len); |
| 1242 | |
| 1243 | |
| 1244 | /** Convert and multiplex the samples of a given channel |
| 1245 | * @param chn A pointer to an iio_channel structure |
| 1246 | * @param buffer A pointer to an iio_buffer structure |
| 1247 | * @param src A pointer to the memory area where the sequential data will |
| 1248 | * be read from |
| 1249 | * @param len The length of the memory area, in bytes |
| 1250 | * @return The number of bytes actually converted and multiplexed */ |
| 1251 | __api size_t iio_channel_write(const struct iio_channel *chn, |
| 1252 | struct iio_buffer *buffer, const void *src, size_t len); |
| 1253 | |
| 1254 | |
| 1255 | /** @brief Associate a pointer to an iio_channel structure |
| 1256 | * @param chn A pointer to an iio_channel structure |
| 1257 | * @param data The pointer to be associated */ |
| 1258 | __api void iio_channel_set_data(struct iio_channel *chn, void *data); |
| 1259 | |
| 1260 | |
| 1261 | /** @brief Retrieve a previously associated pointer of an iio_channel structure |
| 1262 | * @param chn A pointer to an iio_channel structure |
| 1263 | * @return The pointer previously associated if present, or NULL */ |
| 1264 | __api void * iio_channel_get_data(const struct iio_channel *chn); |
| 1265 | |
| 1266 | |
| 1267 | /** @brief Get the type of the given channel |
| 1268 | * @param chn A pointer to an iio_channel structure |
| 1269 | * @return The type of the channel */ |
| 1270 | __api __pure enum iio_chan_type iio_channel_get_type( |
| 1271 | const struct iio_channel *chn); |
| 1272 | |
| 1273 | |
| 1274 | /** @brief Get the modifier type of the given channel |
| 1275 | * @param chn A pointer to an iio_channel structure |
| 1276 | * @return The modifier type of the channel */ |
| 1277 | __api __pure enum iio_modifier iio_channel_get_modifier( |
| 1278 | const struct iio_channel *chn); |
| 1279 | |
| 1280 | |
| 1281 | /** @} *//* ------------------------------------------------------------------*/ |
| 1282 | /* ------------------------- Buffer functions --------------------------------*/ |
| 1283 | /** @defgroup Buffer Buffer |
| 1284 | * @{ |
| 1285 | * @struct iio_buffer |
| 1286 | * @brief An input or output buffer, used to read or write samples */ |
| 1287 | |
| 1288 | |
| 1289 | /** @brief Retrieve a pointer to the iio_device structure |
| 1290 | * @param buf A pointer to an iio_buffer structure |
| 1291 | * @return A pointer to an iio_device structure */ |
| 1292 | __api __pure const struct iio_device * iio_buffer_get_device( |
| 1293 | const struct iio_buffer *buf); |
| 1294 | |
| 1295 | |
| 1296 | /** @brief Create an input or output buffer associated to the given device |
| 1297 | * @param dev A pointer to an iio_device structure |
| 1298 | * @param samples_count The number of samples that the buffer should contain |
| 1299 | * @param cyclic If True, enable cyclic mode |
| 1300 | * @return On success, a pointer to an iio_buffer structure |
| 1301 | * @return On error, NULL is returned, and errno is set to the error code |
| 1302 | * |
| 1303 | * <b>NOTE:</b> Channels that have to be written to / read from must be enabled |
| 1304 | * before creating the buffer. */ |
| 1305 | __api struct iio_buffer * iio_device_create_buffer(const struct iio_device *dev, |
| 1306 | size_t samples_count, bool cyclic); |
| 1307 | |
| 1308 | |
| 1309 | /** @brief Destroy the given buffer |
| 1310 | * @param buf A pointer to an iio_buffer structure |
| 1311 | * |
| 1312 | * <b>NOTE:</b> After that function, the iio_buffer pointer shall be invalid. */ |
| 1313 | __api void iio_buffer_destroy(struct iio_buffer *buf); |
| 1314 | |
| 1315 | /** @brief Get a pollable file descriptor |
| 1316 | * |
| 1317 | * Can be used to know when iio_buffer_refill() or iio_buffer_push() can be |
| 1318 | * called |
| 1319 | * @param buf A pointer to an iio_buffer structure |
| 1320 | * @return On success, valid file descriptor |
| 1321 | * @return On error, a negative errno code is returned |
| 1322 | */ |
| 1323 | __api int iio_buffer_get_poll_fd(struct iio_buffer *buf); |
| 1324 | |
| 1325 | /** @brief Make iio_buffer_refill() and iio_buffer_push() blocking or not |
| 1326 | * |
| 1327 | * After this function has been called with blocking == false, |
| 1328 | * iio_buffer_refill() and iio_buffer_push() will return -EAGAIN if no data is |
| 1329 | * ready. |
| 1330 | * A device is blocking by default. |
| 1331 | * @param buf A pointer to an iio_buffer structure |
| 1332 | * @param blocking true if the buffer API should be blocking, else false |
| 1333 | * @return On success, 0 |
| 1334 | * @return On error, a negative errno code is returned |
| 1335 | */ |
| 1336 | __api int iio_buffer_set_blocking_mode(struct iio_buffer *buf, bool blocking); |
| 1337 | |
| 1338 | |
| 1339 | /** @brief Fetch more samples from the hardware |
| 1340 | * @param buf A pointer to an iio_buffer structure |
| 1341 | * @return On success, the number of bytes read is returned |
| 1342 | * @return On error, a negative errno code is returned |
| 1343 | * |
| 1344 | * <b>NOTE:</b> Only valid for input buffers */ |
| 1345 | __api ssize_t iio_buffer_refill(struct iio_buffer *buf); |
| 1346 | |
| 1347 | |
| 1348 | /** @brief Send the samples to the hardware |
| 1349 | * @param buf A pointer to an iio_buffer structure |
| 1350 | * @return On success, the number of bytes written is returned |
| 1351 | * @return On error, a negative errno code is returned |
| 1352 | * |
| 1353 | * <b>NOTE:</b> Only valid for output buffers */ |
| 1354 | __api ssize_t iio_buffer_push(struct iio_buffer *buf); |
| 1355 | |
| 1356 | |
| 1357 | /** @brief Send a given number of samples to the hardware |
| 1358 | * @param buf A pointer to an iio_buffer structure |
| 1359 | * @param samples_count The number of samples to submit |
| 1360 | * @return On success, the number of bytes written is returned |
| 1361 | * @return On error, a negative errno code is returned |
| 1362 | * |
| 1363 | * <b>NOTE:</b> Only valid for output buffers */ |
| 1364 | __api ssize_t iio_buffer_push_partial(struct iio_buffer *buf, |
| 1365 | size_t samples_count); |
| 1366 | |
| 1367 | /** @brief Cancel all buffer operations |
| 1368 | * @param buf The buffer for which operations should be canceled |
| 1369 | * |
| 1370 | * This function cancels all outstanding buffer operations previously scheduled. |
| 1371 | * This means any pending iio_buffer_push() or iio_buffer_refill() operation |
| 1372 | * will abort and return immediately, any further invocations of these functions |
| 1373 | * on the same buffer will return immediately with an error. |
| 1374 | * |
| 1375 | * Usually iio_buffer_push() and iio_buffer_refill() will block until either all |
| 1376 | * data has been transferred or a timeout occurs. This can depending on the |
| 1377 | * configuration take a significant amount of time. iio_buffer_cancel() is |
| 1378 | * useful to bypass these conditions if the buffer operation is supposed to be |
| 1379 | * stopped in response to an external event (e.g. user input). |
| 1380 | * |
| 1381 | * To be able to capture additional data after calling this function the buffer |
| 1382 | * should be destroyed and then re-created. |
| 1383 | * |
| 1384 | * This function can be called multiple times for the same buffer, but all but |
| 1385 | * the first invocation will be without additional effect. |
| 1386 | * |
| 1387 | * This function is thread-safe, but not signal-safe, i.e. it must not be called |
| 1388 | * from a signal handler. |
| 1389 | */ |
| 1390 | __api void iio_buffer_cancel(struct iio_buffer *buf); |
| 1391 | |
| 1392 | |
| 1393 | /** @brief Get the start address of the buffer |
| 1394 | * @param buf A pointer to an iio_buffer structure |
| 1395 | * @return A pointer corresponding to the start address of the buffer */ |
| 1396 | __api void * iio_buffer_start(const struct iio_buffer *buf); |
| 1397 | |
| 1398 | |
| 1399 | /** @brief Find the first sample of a channel in a buffer |
| 1400 | * @param buf A pointer to an iio_buffer structure |
| 1401 | * @param chn A pointer to an iio_channel structure |
| 1402 | * @return A pointer to the first sample found, or to the end of the buffer if |
| 1403 | * no sample for the given channel is present in the buffer |
| 1404 | * |
Ryo Hashimoto | 8fcb4fb | 2020-02-03 18:17:14 +0900 | [diff] [blame] | 1405 | * <b>NOTE:</b> This function, coupled with iio_buffer_step and iio_buffer_end, |
Enrico Granata | 19d8880 | 2019-04-29 16:28:35 -0700 | [diff] [blame] | 1406 | * can be used to iterate on all the samples of a given channel present in the |
| 1407 | * buffer, doing the following: |
| 1408 | * |
| 1409 | * @verbatim |
| 1410 | for (void *ptr = iio_buffer_first(buffer, chn); ptr < iio_buffer_end(buffer); ptr += iio_buffer_step(buffer)) { |
| 1411 | .... |
| 1412 | } |
| 1413 | @endverbatim */ |
| 1414 | __api void * iio_buffer_first(const struct iio_buffer *buf, |
| 1415 | const struct iio_channel *chn); |
| 1416 | |
| 1417 | |
| 1418 | /** @brief Get the step size between two samples of one channel |
| 1419 | * @param buf A pointer to an iio_buffer structure |
| 1420 | * @return the difference between the addresses of two consecutive samples of |
| 1421 | * one same channel */ |
| 1422 | __api ptrdiff_t iio_buffer_step(const struct iio_buffer *buf); |
| 1423 | |
| 1424 | |
| 1425 | /** @brief Get the address that follows the last sample in a buffer |
| 1426 | * @param buf A pointer to an iio_buffer structure |
| 1427 | * @return A pointer corresponding to the address that follows the last sample |
| 1428 | * present in the buffer */ |
| 1429 | __api void * iio_buffer_end(const struct iio_buffer *buf); |
| 1430 | |
| 1431 | |
| 1432 | /** @brief Call the supplied callback for each sample found in a buffer |
| 1433 | * @param buf A pointer to an iio_buffer structure |
| 1434 | * @param callback A pointer to a function to call for each sample found |
| 1435 | * @param data A user-specified pointer that will be passed to the callback |
| 1436 | * @return number of bytes processed. |
| 1437 | * |
| 1438 | * <b>NOTE:</b> The callback receives four arguments: |
| 1439 | * * A pointer to the iio_channel structure corresponding to the sample, |
| 1440 | * * A pointer to the sample itself, |
| 1441 | * * The length of the sample in bytes, |
| 1442 | * * The user-specified pointer passed to iio_buffer_foreach_sample. */ |
| 1443 | __api ssize_t iio_buffer_foreach_sample(struct iio_buffer *buf, |
| 1444 | ssize_t (*callback)(const struct iio_channel *chn, |
| 1445 | void *src, size_t bytes, void *d), void *data); |
| 1446 | |
| 1447 | |
| 1448 | /** @brief Associate a pointer to an iio_buffer structure |
| 1449 | * @param buf A pointer to an iio_buffer structure |
| 1450 | * @param data The pointer to be associated */ |
| 1451 | __api void iio_buffer_set_data(struct iio_buffer *buf, void *data); |
| 1452 | |
| 1453 | |
| 1454 | /** @brief Retrieve a previously associated pointer of an iio_buffer structure |
| 1455 | * @param buf A pointer to an iio_buffer structure |
| 1456 | * @return The pointer previously associated if present, or NULL */ |
| 1457 | __api void * iio_buffer_get_data(const struct iio_buffer *buf); |
| 1458 | |
| 1459 | |
| 1460 | /** @} *//* ------------------------------------------------------------------*/ |
| 1461 | /* ------------------------- Low-level functions -----------------------------*/ |
| 1462 | /** @defgroup Debug Debug and low-level functions |
| 1463 | * @{ |
| 1464 | * @struct iio_data_format |
| 1465 | * @brief Contains the format of a data sample. |
| 1466 | * |
| 1467 | * The different fields inform about the correct way to convert one sample from |
| 1468 | * its raw format (as read from / generated by the hardware) to its real-world |
| 1469 | * value. |
| 1470 | */ |
| 1471 | struct iio_data_format { |
| 1472 | /** @brief Total length of the sample, in bits */ |
| 1473 | unsigned int length; |
| 1474 | |
| 1475 | /** @brief Length of valuable data in the sample, in bits */ |
| 1476 | unsigned int bits; |
| 1477 | |
| 1478 | /** @brief Right-shift to apply when converting sample */ |
| 1479 | unsigned int shift; |
| 1480 | |
| 1481 | /** @brief Contains True if the sample is signed */ |
| 1482 | bool is_signed; |
| 1483 | |
| 1484 | /** @brief Contains True if the sample is fully defined, sign extended, etc. */ |
| 1485 | bool is_fully_defined; |
| 1486 | |
| 1487 | /** @brief Contains True if the sample is in big-endian format */ |
| 1488 | bool is_be; |
| 1489 | |
| 1490 | /** @brief Contains True if the sample should be scaled when converted */ |
| 1491 | bool with_scale; |
| 1492 | |
| 1493 | /** @brief Contains the scale to apply if with_scale is set */ |
| 1494 | double scale; |
| 1495 | |
| 1496 | /** @brief Number of times length repeats (added in v0.8) */ |
| 1497 | unsigned int repeat; |
| 1498 | }; |
| 1499 | |
| 1500 | |
| 1501 | /** @brief Get the current sample size |
| 1502 | * @param dev A pointer to an iio_device structure |
| 1503 | * @return On success, the sample size in bytes |
| 1504 | * @return On error, a negative errno code is returned |
| 1505 | * |
| 1506 | * <b>NOTE:</b> The sample size is not constant and will change when channels |
| 1507 | * get enabled or disabled. */ |
| 1508 | __api ssize_t iio_device_get_sample_size(const struct iio_device *dev); |
| 1509 | |
| 1510 | |
| 1511 | /** @brief Get the index of the given channel |
| 1512 | * @param chn A pointer to an iio_channel structure |
| 1513 | * @return On success, the index of the specified channel |
| 1514 | * @return On error, a negative errno code is returned */ |
| 1515 | __api __pure long iio_channel_get_index(const struct iio_channel *chn); |
| 1516 | |
| 1517 | |
| 1518 | /** @brief Get a pointer to a channel's data format structure |
| 1519 | * @param chn A pointer to an iio_channel structure |
| 1520 | * @return A pointer to the channel's iio_data_format structure */ |
| 1521 | __api __cnst const struct iio_data_format * iio_channel_get_data_format( |
| 1522 | const struct iio_channel *chn); |
| 1523 | |
| 1524 | |
| 1525 | /** @brief Convert the sample from hardware format to host format |
| 1526 | * @param chn A pointer to an iio_channel structure |
| 1527 | * @param dst A pointer to the destination buffer where the converted sample |
| 1528 | * should be written |
| 1529 | * @param src A pointer to the source buffer containing the sample */ |
| 1530 | __api void iio_channel_convert(const struct iio_channel *chn, |
| 1531 | void *dst, const void *src); |
| 1532 | |
| 1533 | |
| 1534 | /** @brief Convert the sample from host format to hardware format |
| 1535 | * @param chn A pointer to an iio_channel structure |
| 1536 | * @param dst A pointer to the destination buffer where the converted sample |
| 1537 | * should be written |
| 1538 | * @param src A pointer to the source buffer containing the sample */ |
| 1539 | __api void iio_channel_convert_inverse(const struct iio_channel *chn, |
| 1540 | void *dst, const void *src); |
| 1541 | |
| 1542 | |
| 1543 | /** @brief Enumerate the debug attributes of the given device |
| 1544 | * @param dev A pointer to an iio_device structure |
| 1545 | * @return The number of debug attributes found */ |
| 1546 | __api __pure unsigned int iio_device_get_debug_attrs_count( |
| 1547 | const struct iio_device *dev); |
| 1548 | |
| 1549 | |
| 1550 | /** @brief Get the debug attribute present at the given index |
| 1551 | * @param dev A pointer to an iio_device structure |
| 1552 | * @param index The index corresponding to the debug attribute |
| 1553 | * @return On success, a pointer to a static NULL-terminated string |
| 1554 | * @return If the index is invalid, NULL is returned */ |
| 1555 | __api __pure const char * iio_device_get_debug_attr( |
| 1556 | const struct iio_device *dev, unsigned int index); |
| 1557 | |
| 1558 | |
| 1559 | /** @brief Try to find a debug attribute by its name |
| 1560 | * @param dev A pointer to an iio_device structure |
| 1561 | * @param name A NULL-terminated string corresponding to the name of the |
| 1562 | * debug attribute |
| 1563 | * @return On success, a pointer to a static NULL-terminated string |
| 1564 | * @return If the name does not correspond to any known debug attribute of the |
| 1565 | * given device, NULL is returned |
| 1566 | * |
| 1567 | * <b>NOTE:</b> This function is useful to detect the presence of a debug |
| 1568 | * attribute. |
| 1569 | * It can also be used to retrieve the name of a debug attribute as a pointer |
| 1570 | * to a static string from a dynamically allocated string. */ |
| 1571 | __api __pure const char * iio_device_find_debug_attr( |
| 1572 | const struct iio_device *dev, const char *name); |
| 1573 | |
| 1574 | |
| 1575 | /** @brief Read the content of the given debug attribute |
| 1576 | * @param dev A pointer to an iio_device structure |
| 1577 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1578 | * debug attribute |
| 1579 | * @param dst A pointer to the memory area where the NULL-terminated string |
| 1580 | * corresponding to the value read will be stored |
| 1581 | * @param len The available length of the memory area, in bytes |
| 1582 | * @return On success, the number of bytes written to the buffer |
| 1583 | * @return On error, a negative errno code is returned |
| 1584 | * |
| 1585 | * <b>NOTE:</b>By passing NULL as the "attr" argument to |
| 1586 | * iio_device_debug_attr_read, it is now possible to read all of the debug |
| 1587 | * attributes of a device. |
| 1588 | * |
| 1589 | * The buffer is filled with one block of data per debug attribute of the |
| 1590 | * device, by the order they appear in the iio_device structure. |
| 1591 | * |
| 1592 | * The first four bytes of one block correspond to a 32-bit signed value in |
| 1593 | * network order. If negative, it corresponds to the errno code that were |
| 1594 | * returned when reading the debug attribute; if positive, it corresponds |
| 1595 | * to the length of the data read. In that case, the rest of the block contains |
| 1596 | * the data. */ |
| 1597 | __api ssize_t iio_device_debug_attr_read(const struct iio_device *dev, |
| 1598 | const char *attr, char *dst, size_t len); |
| 1599 | |
| 1600 | |
| 1601 | /** @brief Read the content of all debug attributes |
| 1602 | * @param dev A pointer to an iio_device structure |
| 1603 | * @param cb A pointer to a callback function |
| 1604 | * @param data A pointer that will be passed to the callback function |
| 1605 | * @return On success, 0 is returned |
| 1606 | * @return On error, a negative errno code is returned |
| 1607 | * |
| 1608 | * <b>NOTE:</b> This function is especially useful when used with the network |
| 1609 | * backend, as all the debug attributes are read in one single command. */ |
| 1610 | __api int iio_device_debug_attr_read_all(struct iio_device *dev, |
| 1611 | int (*cb)(struct iio_device *dev, const char *attr, |
| 1612 | const char *value, size_t len, void *d), |
| 1613 | void *data); |
| 1614 | |
| 1615 | |
| 1616 | /** @brief Set the value of the given debug attribute |
| 1617 | * @param dev A pointer to an iio_device structure |
| 1618 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1619 | * debug attribute |
| 1620 | * @param src A NULL-terminated string to set the debug attribute to |
| 1621 | * @return On success, the number of bytes written |
| 1622 | * @return On error, a negative errno code is returned |
| 1623 | * |
| 1624 | * <b>NOTE:</b>By passing NULL as the "attr" argument to |
| 1625 | * iio_device_debug_attr_write, it is now possible to write all of the |
| 1626 | * debug attributes of a device. |
| 1627 | * |
| 1628 | * The buffer must contain one block of data per debug attribute of the device, |
| 1629 | * by the order they appear in the iio_device structure. |
| 1630 | * |
| 1631 | * The first four bytes of one block correspond to a 32-bit signed value in |
| 1632 | * network order. If negative, the debug attribute is not written; if positive, |
| 1633 | * it corresponds to the length of the data to write. In that case, the rest |
| 1634 | * of the block must contain the data. */ |
| 1635 | __api ssize_t iio_device_debug_attr_write(const struct iio_device *dev, |
| 1636 | const char *attr, const char *src); |
| 1637 | |
| 1638 | |
| 1639 | /** @brief Set the value of the given debug attribute |
| 1640 | * @param dev A pointer to an iio_device structure |
| 1641 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1642 | * debug attribute |
| 1643 | * @param src A pointer to the data to be written |
| 1644 | * @param len The number of bytes that should be written |
| 1645 | * @return On success, the number of bytes written |
| 1646 | * @return On error, a negative errno code is returned */ |
| 1647 | __api ssize_t iio_device_debug_attr_write_raw(const struct iio_device *dev, |
| 1648 | const char *attr, const void *src, size_t len); |
| 1649 | |
| 1650 | |
| 1651 | /** @brief Set the values of all debug attributes |
| 1652 | * @param dev A pointer to an iio_device structure |
| 1653 | * @param cb A pointer to a callback function |
| 1654 | * @param data A pointer that will be passed to the callback function |
| 1655 | * @return On success, 0 is returned |
| 1656 | * @return On error, a negative errno code is returned |
| 1657 | * |
| 1658 | * <b>NOTE:</b> This function is especially useful when used with the network |
| 1659 | * backend, as all the debug attributes are written in one single command. */ |
| 1660 | __api int iio_device_debug_attr_write_all(struct iio_device *dev, |
| 1661 | ssize_t (*cb)(struct iio_device *dev, |
| 1662 | const char *attr, void *buf, size_t len, void *d), |
| 1663 | void *data); |
| 1664 | |
| 1665 | |
| 1666 | /** @brief Read the content of the given debug attribute |
| 1667 | * @param dev A pointer to an iio_device structure |
| 1668 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1669 | * debug attribute |
| 1670 | * @param val A pointer to a bool variable where the value should be stored |
| 1671 | * @return On success, 0 is returned |
| 1672 | * @return On error, a negative errno code is returned */ |
| 1673 | __api int iio_device_debug_attr_read_bool(const struct iio_device *dev, |
| 1674 | const char *attr, bool *val); |
| 1675 | |
| 1676 | |
| 1677 | /** @brief Read the content of the given debug attribute |
| 1678 | * @param dev A pointer to an iio_device structure |
| 1679 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1680 | * debug attribute |
| 1681 | * @param val A pointer to a long long variable where the value should be stored |
| 1682 | * @return On success, 0 is returned |
| 1683 | * @return On error, a negative errno code is returned */ |
| 1684 | __api int iio_device_debug_attr_read_longlong(const struct iio_device *dev, |
| 1685 | const char *attr, long long *val); |
| 1686 | |
| 1687 | |
| 1688 | /** @brief Read the content of the given debug attribute |
| 1689 | * @param dev A pointer to an iio_device structure |
| 1690 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1691 | * debug attribute |
| 1692 | * @param val A pointer to a double variable where the value should be stored |
| 1693 | * @return On success, 0 is returned |
| 1694 | * @return On error, a negative errno code is returned */ |
| 1695 | __api int iio_device_debug_attr_read_double(const struct iio_device *dev, |
| 1696 | const char *attr, double *val); |
| 1697 | |
| 1698 | |
| 1699 | /** @brief Set the value of the given debug attribute |
| 1700 | * @param dev A pointer to an iio_device structure |
| 1701 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1702 | * debug attribute |
| 1703 | * @param val A bool value to set the debug attribute to |
| 1704 | * @return On success, 0 is returned |
| 1705 | * @return On error, a negative errno code is returned */ |
| 1706 | __api int iio_device_debug_attr_write_bool(const struct iio_device *dev, |
| 1707 | const char *attr, bool val); |
| 1708 | |
| 1709 | |
| 1710 | /** @brief Set the value of the given debug attribute |
| 1711 | * @param dev A pointer to an iio_device structure |
| 1712 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1713 | * debug attribute |
| 1714 | * @param val A long long value to set the debug attribute to |
| 1715 | * @return On success, 0 is returned |
| 1716 | * @return On error, a negative errno code is returned */ |
| 1717 | __api int iio_device_debug_attr_write_longlong(const struct iio_device *dev, |
| 1718 | const char *attr, long long val); |
| 1719 | |
| 1720 | |
| 1721 | /** @brief Set the value of the given debug attribute |
| 1722 | * @param dev A pointer to an iio_device structure |
| 1723 | * @param attr A NULL-terminated string corresponding to the name of the |
| 1724 | * debug attribute |
| 1725 | * @param val A double value to set the debug attribute to |
| 1726 | * @return On success, 0 is returned |
| 1727 | * @return On error, a negative errno code is returned */ |
| 1728 | __api int iio_device_debug_attr_write_double(const struct iio_device *dev, |
| 1729 | const char *attr, double val); |
| 1730 | |
| 1731 | |
| 1732 | /** @brief Identify the channel or debug attribute corresponding to a filename |
| 1733 | * @param dev A pointer to an iio_device structure |
| 1734 | * @param filename A NULL-terminated string corresponding to the filename |
| 1735 | * @param chn A pointer to a pointer of an iio_channel structure. The pointed |
| 1736 | * pointer will be set to the address of the iio_channel structure if the |
| 1737 | * filename correspond to the attribute of a channel, or NULL otherwise. |
| 1738 | * @param attr A pointer to a NULL-terminated string. The pointer |
| 1739 | * pointer will be set to point to the name of the attribute corresponding to |
| 1740 | * the filename. |
| 1741 | * @return On success, 0 is returned, and *chn and *attr are modified. |
| 1742 | * @return On error, a negative errno code is returned. *chn and *attr are not |
| 1743 | * modified. */ |
| 1744 | __api int iio_device_identify_filename(const struct iio_device *dev, |
| 1745 | const char *filename, struct iio_channel **chn, |
| 1746 | const char **attr); |
| 1747 | |
| 1748 | |
| 1749 | /** @brief Set the value of a hardware register |
| 1750 | * @param dev A pointer to an iio_device structure |
| 1751 | * @param address The address of the register |
| 1752 | * @param value The value to set the register to |
| 1753 | * @return On success, 0 is returned |
| 1754 | * @return On error, a negative errno code is returned */ |
| 1755 | __api int iio_device_reg_write(struct iio_device *dev, |
| 1756 | uint32_t address, uint32_t value); |
| 1757 | |
| 1758 | |
| 1759 | /** @brief Get the value of a hardware register |
| 1760 | * @param dev A pointer to an iio_device structure |
| 1761 | * @param address The address of the register |
| 1762 | * @param value A pointer to the variable where the value will be written |
| 1763 | * @return On success, 0 is returned |
| 1764 | * @return On error, a negative errno code is returned */ |
| 1765 | __api int iio_device_reg_read(struct iio_device *dev, |
| 1766 | uint32_t address, uint32_t *value); |
| 1767 | |
| 1768 | |
| 1769 | /** @} */ |
| 1770 | |
| 1771 | #ifdef __cplusplus |
| 1772 | } |
| 1773 | #endif |
| 1774 | |
| 1775 | #undef __api |
| 1776 | |
| 1777 | #endif /* __IIO_H__ */ |