iio_utils.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/scan_elements"
  12. #define FORMAT_TYPE_FILE "%s_type"
  13. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
  14. extern const char *iio_dir;
  15. /**
  16. * struct iio_channel_info - information about a given channel
  17. * @name: channel name
  18. * @generic_name: general name for channel type
  19. * @scale: scale factor to be applied for conversion to si units
  20. * @offset: offset to be applied for conversion to si units
  21. * @index: the channel index in the buffer output
  22. * @bytes: number of bytes occupied in buffer output
  23. * @bits_used: number of valid bits of data
  24. * @shift: amount of bits to shift right data before applying bit mask
  25. * @mask: a bit mask for the raw output
  26. * @be: flag if data is big endian
  27. * @is_signed: is the raw value stored signed
  28. * @location: data offset for this channel inside the buffer (in bytes)
  29. **/
  30. struct iio_channel_info {
  31. char *name;
  32. char *generic_name;
  33. float scale;
  34. float offset;
  35. unsigned index;
  36. unsigned bytes;
  37. unsigned bits_used;
  38. unsigned shift;
  39. uint64_t mask;
  40. unsigned be;
  41. unsigned is_signed;
  42. unsigned location;
  43. };
  44. static inline int iioutils_check_suffix(const char *str, const char *suffix)
  45. {
  46. return strlen(str) >= strlen(suffix) &&
  47. strncmp(str+strlen(str)-strlen(suffix),
  48. suffix, strlen(suffix)) == 0;
  49. }
  50. int iioutils_break_up_name(const char *full_name, char **generic_name);
  51. int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
  52. unsigned *shift, uint64_t *mask, unsigned *be,
  53. const char *device_dir, const char *name,
  54. const char *generic_name);
  55. int iioutils_get_param_float(float *output, const char *param_name,
  56. const char *device_dir, const char *name,
  57. const char *generic_name);
  58. void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt);
  59. int build_channel_array(const char *device_dir,
  60. struct iio_channel_info **ci_array, int *counter);
  61. int find_type_by_name(const char *name, const char *type);
  62. int write_sysfs_int(const char *filename, const char *basedir, int val);
  63. int write_sysfs_int_and_verify(const char *filename, const char *basedir,
  64. int val);
  65. int write_sysfs_string_and_verify(const char *filename, const char *basedir,
  66. const char *val);
  67. int write_sysfs_string(const char *filename, const char *basedir,
  68. const char *val);
  69. int read_sysfs_posint(const char *filename, const char *basedir);
  70. int read_sysfs_float(const char *filename, const char *basedir, float *val);
  71. int read_sysfs_string(const char *filename, const char *basedir, char *str);
  72. #endif /* _IIO_UTILS_H_ */