162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci#ifndef _IIO_UTILS_H_
362306a36Sopenharmony_ci#define _IIO_UTILS_H_
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci/* IIO - useful set of util functionality
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright (c) 2008 Jonathan Cameron
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <stdint.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci/* Made up value to limit allocation sizes */
1362306a36Sopenharmony_ci#define IIO_MAX_NAME_LENGTH 64
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#define FORMAT_SCAN_ELEMENTS_DIR "%s/buffer%d"
1662306a36Sopenharmony_ci#define FORMAT_EVENTS_DIR "%s/events"
1762306a36Sopenharmony_ci#define FORMAT_TYPE_FILE "%s_type"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciextern const char *iio_dir;
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/**
2462306a36Sopenharmony_ci * struct iio_channel_info - information about a given channel
2562306a36Sopenharmony_ci * @name: channel name
2662306a36Sopenharmony_ci * @generic_name: general name for channel type
2762306a36Sopenharmony_ci * @scale: scale factor to be applied for conversion to si units
2862306a36Sopenharmony_ci * @offset: offset to be applied for conversion to si units
2962306a36Sopenharmony_ci * @index: the channel index in the buffer output
3062306a36Sopenharmony_ci * @bytes: number of bytes occupied in buffer output
3162306a36Sopenharmony_ci * @bits_used: number of valid bits of data
3262306a36Sopenharmony_ci * @shift: amount of bits to shift right data before applying bit mask
3362306a36Sopenharmony_ci * @mask: a bit mask for the raw output
3462306a36Sopenharmony_ci * @be: flag if data is big endian
3562306a36Sopenharmony_ci * @is_signed: is the raw value stored signed
3662306a36Sopenharmony_ci * @location: data offset for this channel inside the buffer (in bytes)
3762306a36Sopenharmony_ci **/
3862306a36Sopenharmony_cistruct iio_channel_info {
3962306a36Sopenharmony_ci	char *name;
4062306a36Sopenharmony_ci	char *generic_name;
4162306a36Sopenharmony_ci	float scale;
4262306a36Sopenharmony_ci	float offset;
4362306a36Sopenharmony_ci	unsigned index;
4462306a36Sopenharmony_ci	unsigned bytes;
4562306a36Sopenharmony_ci	unsigned bits_used;
4662306a36Sopenharmony_ci	unsigned shift;
4762306a36Sopenharmony_ci	uint64_t mask;
4862306a36Sopenharmony_ci	unsigned be;
4962306a36Sopenharmony_ci	unsigned is_signed;
5062306a36Sopenharmony_ci	unsigned location;
5162306a36Sopenharmony_ci};
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic inline int iioutils_check_suffix(const char *str, const char *suffix)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	return strlen(str) >= strlen(suffix) &&
5662306a36Sopenharmony_ci		strncmp(str+strlen(str)-strlen(suffix),
5762306a36Sopenharmony_ci			suffix, strlen(suffix)) == 0;
5862306a36Sopenharmony_ci}
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ciint iioutils_break_up_name(const char *full_name, char **generic_name);
6162306a36Sopenharmony_ciint iioutils_get_param_float(float *output, const char *param_name,
6262306a36Sopenharmony_ci			     const char *device_dir, const char *name,
6362306a36Sopenharmony_ci			     const char *generic_name);
6462306a36Sopenharmony_civoid bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt);
6562306a36Sopenharmony_ciint build_channel_array(const char *device_dir, int buffer_idx,
6662306a36Sopenharmony_ci			struct iio_channel_info **ci_array, int *counter);
6762306a36Sopenharmony_ciint find_type_by_name(const char *name, const char *type);
6862306a36Sopenharmony_ciint write_sysfs_int(const char *filename, const char *basedir, int val);
6962306a36Sopenharmony_ciint write_sysfs_int_and_verify(const char *filename, const char *basedir,
7062306a36Sopenharmony_ci			       int val);
7162306a36Sopenharmony_ciint write_sysfs_string_and_verify(const char *filename, const char *basedir,
7262306a36Sopenharmony_ci				  const char *val);
7362306a36Sopenharmony_ciint write_sysfs_string(const char *filename, const char *basedir,
7462306a36Sopenharmony_ci		       const char *val);
7562306a36Sopenharmony_ciint read_sysfs_posint(const char *filename, const char *basedir);
7662306a36Sopenharmony_ciint read_sysfs_float(const char *filename, const char *basedir, float *val);
7762306a36Sopenharmony_ciint read_sysfs_string(const char *filename, const char *basedir, char *str);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci#endif /* _IIO_UTILS_H_ */
80