Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 2 | #ifndef _IIO_UTILS_H_ |
| 3 | #define _IIO_UTILS_H_ |
| 4 | |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 5 | /* IIO - useful set of util functionality |
| 6 | * |
| 7 | * Copyright (c) 2008 Jonathan Cameron |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 10 | #include <stdint.h> |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 11 | |
Peter Meerwald | b42f2a0 | 2012-06-25 23:12:13 +0200 | [diff] [blame] | 12 | /* Made up value to limit allocation sizes */ |
Eugen Hristev | f80ac40 | 2017-05-04 15:13:20 +0300 | [diff] [blame] | 13 | #define IIO_MAX_NAME_LENGTH 64 |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 14 | |
Alexandru Ardelean | 8827faa | 2021-02-15 12:40:43 +0200 | [diff] [blame] | 15 | #define FORMAT_SCAN_ELEMENTS_DIR "%s/buffer%d" |
Linus Walleij | e1d392d | 2021-03-19 14:53:01 +0100 | [diff] [blame] | 16 | #define FORMAT_EVENTS_DIR "%s/events" |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 17 | #define FORMAT_TYPE_FILE "%s_type" |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 18 | |
Cristina Opriceana | 34cbea1 | 2015-07-13 16:20:11 +0300 | [diff] [blame] | 19 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) |
| 20 | |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 21 | extern const char *iio_dir; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * struct iio_channel_info - information about a given channel |
| 25 | * @name: channel name |
| 26 | * @generic_name: general name for channel type |
| 27 | * @scale: scale factor to be applied for conversion to si units |
| 28 | * @offset: offset to be applied for conversion to si units |
| 29 | * @index: the channel index in the buffer output |
| 30 | * @bytes: number of bytes occupied in buffer output |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 31 | * @bits_used: number of valid bits of data |
| 32 | * @shift: amount of bits to shift right data before applying bit mask |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 33 | * @mask: a bit mask for the raw output |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 34 | * @be: flag if data is big endian |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 35 | * @is_signed: is the raw value stored signed |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 36 | * @location: data offset for this channel inside the buffer (in bytes) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 37 | **/ |
| 38 | struct iio_channel_info { |
| 39 | char *name; |
| 40 | char *generic_name; |
| 41 | float scale; |
| 42 | float offset; |
| 43 | unsigned index; |
| 44 | unsigned bytes; |
| 45 | unsigned bits_used; |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 46 | unsigned shift; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 47 | uint64_t mask; |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 48 | unsigned be; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 49 | unsigned is_signed; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 50 | unsigned location; |
| 51 | }; |
| 52 | |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 53 | static inline int iioutils_check_suffix(const char *str, const char *suffix) |
| 54 | { |
| 55 | return strlen(str) >= strlen(suffix) && |
| 56 | strncmp(str+strlen(str)-strlen(suffix), |
| 57 | suffix, strlen(suffix)) == 0; |
| 58 | } |
| 59 | |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 60 | int iioutils_break_up_name(const char *full_name, char **generic_name); |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 61 | int iioutils_get_param_float(float *output, const char *param_name, |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 62 | const char *device_dir, const char *name, |
| 63 | const char *generic_name); |
Joo Aun Saw | 95ddd3f | 2015-07-25 01:23:29 +1000 | [diff] [blame] | 64 | void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt); |
Alexandru Ardelean | 8827faa | 2021-02-15 12:40:43 +0200 | [diff] [blame] | 65 | int build_channel_array(const char *device_dir, int buffer_idx, |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 66 | struct iio_channel_info **ci_array, int *counter); |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 67 | int find_type_by_name(const char *name, const char *type); |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 68 | int write_sysfs_int(const char *filename, const char *basedir, int val); |
| 69 | int write_sysfs_int_and_verify(const char *filename, const char *basedir, |
| 70 | int val); |
| 71 | int write_sysfs_string_and_verify(const char *filename, const char *basedir, |
| 72 | const char *val); |
| 73 | int write_sysfs_string(const char *filename, const char *basedir, |
| 74 | const char *val); |
| 75 | int read_sysfs_posint(const char *filename, const char *basedir); |
| 76 | int read_sysfs_float(const char *filename, const char *basedir, float *val); |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 77 | int read_sysfs_string(const char *filename, const char *basedir, char *str); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 78 | |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 79 | #endif /* _IIO_UTILS_H_ */ |