1817020cfSRoberta Dobrescu #ifndef _IIO_UTILS_H_ 2817020cfSRoberta Dobrescu #define _IIO_UTILS_H_ 3817020cfSRoberta Dobrescu 4817020cfSRoberta Dobrescu /* IIO - useful set of util functionality 5817020cfSRoberta Dobrescu * 6817020cfSRoberta Dobrescu * Copyright (c) 2008 Jonathan Cameron 7817020cfSRoberta Dobrescu * 8817020cfSRoberta Dobrescu * This program is free software; you can redistribute it and/or modify it 9817020cfSRoberta Dobrescu * under the terms of the GNU General Public License version 2 as published by 10817020cfSRoberta Dobrescu * the Free Software Foundation. 11817020cfSRoberta Dobrescu */ 12817020cfSRoberta Dobrescu 13817020cfSRoberta Dobrescu #include <stdint.h> 14817020cfSRoberta Dobrescu 15817020cfSRoberta Dobrescu /* Made up value to limit allocation sizes */ 16817020cfSRoberta Dobrescu #define IIO_MAX_NAME_LENGTH 30 17817020cfSRoberta Dobrescu 18817020cfSRoberta Dobrescu #define FORMAT_SCAN_ELEMENTS_DIR "%s/scan_elements" 19817020cfSRoberta Dobrescu #define FORMAT_TYPE_FILE "%s_type" 20817020cfSRoberta Dobrescu 2134cbea19SCristina Opriceana #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) 2234cbea19SCristina Opriceana 23817020cfSRoberta Dobrescu extern const char *iio_dir; 24817020cfSRoberta Dobrescu 25817020cfSRoberta Dobrescu /** 26817020cfSRoberta Dobrescu * struct iio_channel_info - information about a given channel 27817020cfSRoberta Dobrescu * @name: channel name 28817020cfSRoberta Dobrescu * @generic_name: general name for channel type 29817020cfSRoberta Dobrescu * @scale: scale factor to be applied for conversion to si units 30817020cfSRoberta Dobrescu * @offset: offset to be applied for conversion to si units 31817020cfSRoberta Dobrescu * @index: the channel index in the buffer output 32817020cfSRoberta Dobrescu * @bytes: number of bytes occupied in buffer output 335dc65d79SHartmut Knaack * @bits_used: number of valid bits of data 345dc65d79SHartmut Knaack * @shift: amount of bits to shift right data before applying bit mask 35817020cfSRoberta Dobrescu * @mask: a bit mask for the raw output 365dc65d79SHartmut Knaack * @be: flag if data is big endian 37817020cfSRoberta Dobrescu * @is_signed: is the raw value stored signed 385dc65d79SHartmut Knaack * @location: data offset for this channel inside the buffer (in bytes) 39817020cfSRoberta Dobrescu **/ 40817020cfSRoberta Dobrescu struct iio_channel_info { 41817020cfSRoberta Dobrescu char *name; 42817020cfSRoberta Dobrescu char *generic_name; 43817020cfSRoberta Dobrescu float scale; 44817020cfSRoberta Dobrescu float offset; 45817020cfSRoberta Dobrescu unsigned index; 46817020cfSRoberta Dobrescu unsigned bytes; 47817020cfSRoberta Dobrescu unsigned bits_used; 48817020cfSRoberta Dobrescu unsigned shift; 49817020cfSRoberta Dobrescu uint64_t mask; 50817020cfSRoberta Dobrescu unsigned be; 51817020cfSRoberta Dobrescu unsigned is_signed; 52817020cfSRoberta Dobrescu unsigned location; 53817020cfSRoberta Dobrescu }; 54817020cfSRoberta Dobrescu 555f991a92SLinus Walleij static inline int iioutils_check_suffix(const char *str, const char *suffix) 565f991a92SLinus Walleij { 575f991a92SLinus Walleij return strlen(str) >= strlen(suffix) && 585f991a92SLinus Walleij strncmp(str+strlen(str)-strlen(suffix), 595f991a92SLinus Walleij suffix, strlen(suffix)) == 0; 605f991a92SLinus Walleij } 615f991a92SLinus Walleij 62817020cfSRoberta Dobrescu int iioutils_break_up_name(const char *full_name, char **generic_name); 637663a4aaSHartmut Knaack int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used, 647663a4aaSHartmut Knaack unsigned *shift, uint64_t *mask, unsigned *be, 65817020cfSRoberta Dobrescu const char *device_dir, const char *name, 66817020cfSRoberta Dobrescu const char *generic_name); 67817020cfSRoberta Dobrescu int iioutils_get_param_float(float *output, const char *param_name, 68817020cfSRoberta Dobrescu const char *device_dir, const char *name, 69817020cfSRoberta Dobrescu const char *generic_name); 7095ddd3f4SJoo Aun Saw void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt); 71817020cfSRoberta Dobrescu int build_channel_array(const char *device_dir, 72817020cfSRoberta Dobrescu struct iio_channel_info **ci_array, int *counter); 73817020cfSRoberta Dobrescu int find_type_by_name(const char *name, const char *type); 749d475254SHartmut Knaack int write_sysfs_int(const char *filename, const char *basedir, int val); 759d475254SHartmut Knaack int write_sysfs_int_and_verify(const char *filename, const char *basedir, 769d475254SHartmut Knaack int val); 779d475254SHartmut Knaack int write_sysfs_string_and_verify(const char *filename, const char *basedir, 789d475254SHartmut Knaack const char *val); 799d475254SHartmut Knaack int write_sysfs_string(const char *filename, const char *basedir, 809d475254SHartmut Knaack const char *val); 819d475254SHartmut Knaack int read_sysfs_posint(const char *filename, const char *basedir); 829d475254SHartmut Knaack int read_sysfs_float(const char *filename, const char *basedir, float *val); 83817020cfSRoberta Dobrescu int read_sysfs_string(const char *filename, const char *basedir, char *str); 84817020cfSRoberta Dobrescu 85817020cfSRoberta Dobrescu #endif /* _IIO_UTILS_H_ */ 86