1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 2817020cfSRoberta Dobrescu #ifndef _IIO_UTILS_H_ 3817020cfSRoberta Dobrescu #define _IIO_UTILS_H_ 4817020cfSRoberta Dobrescu 5817020cfSRoberta Dobrescu /* IIO - useful set of util functionality 6817020cfSRoberta Dobrescu * 7817020cfSRoberta Dobrescu * Copyright (c) 2008 Jonathan Cameron 8817020cfSRoberta Dobrescu */ 9817020cfSRoberta Dobrescu 10817020cfSRoberta Dobrescu #include <stdint.h> 11817020cfSRoberta Dobrescu 12817020cfSRoberta Dobrescu /* Made up value to limit allocation sizes */ 13f80ac400SEugen Hristev #define IIO_MAX_NAME_LENGTH 64 14817020cfSRoberta Dobrescu 15*8827faabSAlexandru Ardelean #define FORMAT_SCAN_ELEMENTS_DIR "%s/buffer%d" 16817020cfSRoberta Dobrescu #define FORMAT_TYPE_FILE "%s_type" 17817020cfSRoberta Dobrescu 1834cbea19SCristina Opriceana #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) 1934cbea19SCristina Opriceana 20817020cfSRoberta Dobrescu extern const char *iio_dir; 21817020cfSRoberta Dobrescu 22817020cfSRoberta Dobrescu /** 23817020cfSRoberta Dobrescu * struct iio_channel_info - information about a given channel 24817020cfSRoberta Dobrescu * @name: channel name 25817020cfSRoberta Dobrescu * @generic_name: general name for channel type 26817020cfSRoberta Dobrescu * @scale: scale factor to be applied for conversion to si units 27817020cfSRoberta Dobrescu * @offset: offset to be applied for conversion to si units 28817020cfSRoberta Dobrescu * @index: the channel index in the buffer output 29817020cfSRoberta Dobrescu * @bytes: number of bytes occupied in buffer output 305dc65d79SHartmut Knaack * @bits_used: number of valid bits of data 315dc65d79SHartmut Knaack * @shift: amount of bits to shift right data before applying bit mask 32817020cfSRoberta Dobrescu * @mask: a bit mask for the raw output 335dc65d79SHartmut Knaack * @be: flag if data is big endian 34817020cfSRoberta Dobrescu * @is_signed: is the raw value stored signed 355dc65d79SHartmut Knaack * @location: data offset for this channel inside the buffer (in bytes) 36817020cfSRoberta Dobrescu **/ 37817020cfSRoberta Dobrescu struct iio_channel_info { 38817020cfSRoberta Dobrescu char *name; 39817020cfSRoberta Dobrescu char *generic_name; 40817020cfSRoberta Dobrescu float scale; 41817020cfSRoberta Dobrescu float offset; 42817020cfSRoberta Dobrescu unsigned index; 43817020cfSRoberta Dobrescu unsigned bytes; 44817020cfSRoberta Dobrescu unsigned bits_used; 45817020cfSRoberta Dobrescu unsigned shift; 46817020cfSRoberta Dobrescu uint64_t mask; 47817020cfSRoberta Dobrescu unsigned be; 48817020cfSRoberta Dobrescu unsigned is_signed; 49817020cfSRoberta Dobrescu unsigned location; 50817020cfSRoberta Dobrescu }; 51817020cfSRoberta Dobrescu 525f991a92SLinus Walleij static inline int iioutils_check_suffix(const char *str, const char *suffix) 535f991a92SLinus Walleij { 545f991a92SLinus Walleij return strlen(str) >= strlen(suffix) && 555f991a92SLinus Walleij strncmp(str+strlen(str)-strlen(suffix), 565f991a92SLinus Walleij suffix, strlen(suffix)) == 0; 575f991a92SLinus Walleij } 585f991a92SLinus Walleij 59817020cfSRoberta Dobrescu int iioutils_break_up_name(const char *full_name, char **generic_name); 60817020cfSRoberta Dobrescu int iioutils_get_param_float(float *output, const char *param_name, 61817020cfSRoberta Dobrescu const char *device_dir, const char *name, 62817020cfSRoberta Dobrescu const char *generic_name); 6395ddd3f4SJoo Aun Saw void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt); 64*8827faabSAlexandru Ardelean int build_channel_array(const char *device_dir, int buffer_idx, 65817020cfSRoberta Dobrescu struct iio_channel_info **ci_array, int *counter); 66817020cfSRoberta Dobrescu int find_type_by_name(const char *name, const char *type); 679d475254SHartmut Knaack int write_sysfs_int(const char *filename, const char *basedir, int val); 689d475254SHartmut Knaack int write_sysfs_int_and_verify(const char *filename, const char *basedir, 699d475254SHartmut Knaack int val); 709d475254SHartmut Knaack int write_sysfs_string_and_verify(const char *filename, const char *basedir, 719d475254SHartmut Knaack const char *val); 729d475254SHartmut Knaack int write_sysfs_string(const char *filename, const char *basedir, 739d475254SHartmut Knaack const char *val); 749d475254SHartmut Knaack int read_sysfs_posint(const char *filename, const char *basedir); 759d475254SHartmut Knaack int read_sysfs_float(const char *filename, const char *basedir, float *val); 76817020cfSRoberta Dobrescu int read_sysfs_string(const char *filename, const char *basedir, char *str); 77817020cfSRoberta Dobrescu 78817020cfSRoberta Dobrescu #endif /* _IIO_UTILS_H_ */ 79