blob: 336752cade4fcef2bab666f23896b2cdc1180e4a [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +02002#ifndef _IIO_UTILS_H_
3#define _IIO_UTILS_H_
4
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +01005/* IIO - useful set of util functionality
6 *
7 * Copyright (c) 2008 Jonathan Cameron
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +01008 */
9
Jonathan Camerone58537c2010-10-08 12:14:14 +010010#include <stdint.h>
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +010011
Peter Meerwaldb42f2a02012-06-25 23:12:13 +020012/* Made up value to limit allocation sizes */
Eugen Hristevf80ac402017-05-04 15:13:20 +030013#define IIO_MAX_NAME_LENGTH 64
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +010014
Alexandru Ardelean8827faa2021-02-15 12:40:43 +020015#define FORMAT_SCAN_ELEMENTS_DIR "%s/buffer%d"
Jonathan Camerone58537c2010-10-08 12:14:14 +010016#define FORMAT_TYPE_FILE "%s_type"
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +010017
Cristina Opriceana34cbea12015-07-13 16:20:11 +030018#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
19
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020020extern const char *iio_dir;
Jonathan Camerone58537c2010-10-08 12:14:14 +010021
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
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020030 * @bits_used: number of valid bits of data
31 * @shift: amount of bits to shift right data before applying bit mask
Jonathan Camerone58537c2010-10-08 12:14:14 +010032 * @mask: a bit mask for the raw output
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020033 * @be: flag if data is big endian
Jonathan Camerone58537c2010-10-08 12:14:14 +010034 * @is_signed: is the raw value stored signed
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020035 * @location: data offset for this channel inside the buffer (in bytes)
Jonathan Camerone58537c2010-10-08 12:14:14 +010036 **/
37struct 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;
Jonathan Cameron52615d42011-05-18 14:41:19 +010045 unsigned shift;
Jonathan Camerone58537c2010-10-08 12:14:14 +010046 uint64_t mask;
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000047 unsigned be;
Jonathan Camerone58537c2010-10-08 12:14:14 +010048 unsigned is_signed;
Jonathan Camerone58537c2010-10-08 12:14:14 +010049 unsigned location;
50};
51
Linus Walleij5f991a92016-04-14 10:26:47 +020052static 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
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020059int iioutils_break_up_name(const char *full_name, char **generic_name);
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020060int iioutils_get_param_float(float *output, const char *param_name,
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020061 const char *device_dir, const char *name,
62 const char *generic_name);
Joo Aun Saw95ddd3f2015-07-25 01:23:29 +100063void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt);
Alexandru Ardelean8827faa2021-02-15 12:40:43 +020064int build_channel_array(const char *device_dir, int buffer_idx,
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020065 struct iio_channel_info **ci_array, int *counter);
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020066int find_type_by_name(const char *name, const char *type);
Hartmut Knaack9d475252015-05-31 14:40:36 +020067int write_sysfs_int(const char *filename, const char *basedir, int val);
68int write_sysfs_int_and_verify(const char *filename, const char *basedir,
69 int val);
70int write_sysfs_string_and_verify(const char *filename, const char *basedir,
71 const char *val);
72int write_sysfs_string(const char *filename, const char *basedir,
73 const char *val);
74int read_sysfs_posint(const char *filename, const char *basedir);
75int read_sysfs_float(const char *filename, const char *basedir, float *val);
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020076int read_sysfs_string(const char *filename, const char *basedir, char *str);
Jonathan Camerone58537c2010-10-08 12:14:14 +010077
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020078#endif /* _IIO_UTILS_H_ */