iio_utils.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. #ifndef _IIO_UTILS_H_
  3. #define _IIO_UTILS_H_
  4. /* IIO - useful set of util functionality
  5. *
  6. * Copyright (c) 2008 Jonathan Cameron
  7. */
  8. #include <stdint.h>
  9. /* Made up value to limit allocation sizes */
  10. #define IIO_MAX_NAME_LENGTH 64
  11. #define FORMAT_SCAN_ELEMENTS_DIR "%s/buffer%d"
  12. #define FORMAT_EVENTS_DIR "%s/events"
  13. #define FORMAT_TYPE_FILE "%s_type"
  14. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
  15. extern const char *iio_dir;
  16. /**
  17. * struct iio_channel_info - information about a given channel
  18. * @name: channel name
  19. * @generic_name: general name for channel type
  20. * @scale: scale factor to be applied for conversion to si units
  21. * @offset: offset to be applied for conversion to si units
  22. * @index: the channel index in the buffer output
  23. * @bytes: number of bytes occupied in buffer output
  24. * @bits_used: number of valid bits of data
  25. * @shift: amount of bits to shift right data before applying bit mask
  26. * @mask: a bit mask for the raw output
  27. * @be: flag if data is big endian
  28. * @is_signed: is the raw value stored signed
  29. * @location: data offset for this channel inside the buffer (in bytes)
  30. **/
  31. struct iio_channel_info {
  32. char *name;
  33. char *generic_name;
  34. float scale;
  35. float offset;
  36. unsigned index;
  37. unsigned bytes;
  38. unsigned bits_used;
  39. unsigned shift;
  40. uint64_t mask;
  41. unsigned be;
  42. unsigned is_signed;
  43. unsigned location;
  44. };
  45. static inline int iioutils_check_suffix(const char *str, const char *suffix)
  46. {
  47. return strlen(str) >= strlen(suffix) &&
  48. strncmp(str+strlen(str)-strlen(suffix),
  49. suffix, strlen(suffix)) == 0;
  50. }
  51. int iioutils_break_up_name(const char *full_name, char **generic_name);
  52. int iioutils_get_param_float(float *output, const char *param_name,
  53. const char *device_dir, const char *name,
  54. const char *generic_name);
  55. void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt);
  56. int build_channel_array(const char *device_dir, int buffer_idx,
  57. struct iio_channel_info **ci_array, int *counter);
  58. int find_type_by_name(const char *name, const char *type);
  59. int write_sysfs_int(const char *filename, const char *basedir, int val);
  60. int write_sysfs_int_and_verify(const char *filename, const char *basedir,
  61. int val);
  62. int write_sysfs_string_and_verify(const char *filename, const char *basedir,
  63. const char *val);
  64. int write_sysfs_string(const char *filename, const char *basedir,
  65. const char *val);
  66. int read_sysfs_posint(const char *filename, const char *basedir);
  67. int read_sysfs_float(const char *filename, const char *basedir, float *val);
  68. int read_sysfs_string(const char *filename, const char *basedir, char *str);
  69. #endif /* _IIO_UTILS_H_ */