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