18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci#ifndef _IIO_UTILS_H_
38c2ecf20Sopenharmony_ci#define _IIO_UTILS_H_
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci/* IIO - useful set of util functionality
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (c) 2008 Jonathan Cameron
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <stdint.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/* Made up value to limit allocation sizes */
138c2ecf20Sopenharmony_ci#define IIO_MAX_NAME_LENGTH 64
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define FORMAT_SCAN_ELEMENTS_DIR "%s/scan_elements"
168c2ecf20Sopenharmony_ci#define FORMAT_TYPE_FILE "%s_type"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ciextern const char *iio_dir;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/**
238c2ecf20Sopenharmony_ci * struct iio_channel_info - information about a given channel
248c2ecf20Sopenharmony_ci * @name: channel name
258c2ecf20Sopenharmony_ci * @generic_name: general name for channel type
268c2ecf20Sopenharmony_ci * @scale: scale factor to be applied for conversion to si units
278c2ecf20Sopenharmony_ci * @offset: offset to be applied for conversion to si units
288c2ecf20Sopenharmony_ci * @index: the channel index in the buffer output
298c2ecf20Sopenharmony_ci * @bytes: number of bytes occupied in buffer output
308c2ecf20Sopenharmony_ci * @bits_used: number of valid bits of data
318c2ecf20Sopenharmony_ci * @shift: amount of bits to shift right data before applying bit mask
328c2ecf20Sopenharmony_ci * @mask: a bit mask for the raw output
338c2ecf20Sopenharmony_ci * @be: flag if data is big endian
348c2ecf20Sopenharmony_ci * @is_signed: is the raw value stored signed
358c2ecf20Sopenharmony_ci * @location: data offset for this channel inside the buffer (in bytes)
368c2ecf20Sopenharmony_ci **/
378c2ecf20Sopenharmony_cistruct iio_channel_info {
388c2ecf20Sopenharmony_ci	char *name;
398c2ecf20Sopenharmony_ci	char *generic_name;
408c2ecf20Sopenharmony_ci	float scale;
418c2ecf20Sopenharmony_ci	float offset;
428c2ecf20Sopenharmony_ci	unsigned index;
438c2ecf20Sopenharmony_ci	unsigned bytes;
448c2ecf20Sopenharmony_ci	unsigned bits_used;
458c2ecf20Sopenharmony_ci	unsigned shift;
468c2ecf20Sopenharmony_ci	uint64_t mask;
478c2ecf20Sopenharmony_ci	unsigned be;
488c2ecf20Sopenharmony_ci	unsigned is_signed;
498c2ecf20Sopenharmony_ci	unsigned location;
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic inline int iioutils_check_suffix(const char *str, const char *suffix)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	return strlen(str) >= strlen(suffix) &&
558c2ecf20Sopenharmony_ci		strncmp(str+strlen(str)-strlen(suffix),
568c2ecf20Sopenharmony_ci			suffix, strlen(suffix)) == 0;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciint iioutils_break_up_name(const char *full_name, char **generic_name);
608c2ecf20Sopenharmony_ciint iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
618c2ecf20Sopenharmony_ci		      unsigned *shift, uint64_t *mask, unsigned *be,
628c2ecf20Sopenharmony_ci		      const char *device_dir, const char *name,
638c2ecf20Sopenharmony_ci		      const char *generic_name);
648c2ecf20Sopenharmony_ciint iioutils_get_param_float(float *output, const char *param_name,
658c2ecf20Sopenharmony_ci			     const char *device_dir, const char *name,
668c2ecf20Sopenharmony_ci			     const char *generic_name);
678c2ecf20Sopenharmony_civoid bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt);
688c2ecf20Sopenharmony_ciint build_channel_array(const char *device_dir,
698c2ecf20Sopenharmony_ci			struct iio_channel_info **ci_array, int *counter);
708c2ecf20Sopenharmony_ciint find_type_by_name(const char *name, const char *type);
718c2ecf20Sopenharmony_ciint write_sysfs_int(const char *filename, const char *basedir, int val);
728c2ecf20Sopenharmony_ciint write_sysfs_int_and_verify(const char *filename, const char *basedir,
738c2ecf20Sopenharmony_ci			       int val);
748c2ecf20Sopenharmony_ciint write_sysfs_string_and_verify(const char *filename, const char *basedir,
758c2ecf20Sopenharmony_ci				  const char *val);
768c2ecf20Sopenharmony_ciint write_sysfs_string(const char *filename, const char *basedir,
778c2ecf20Sopenharmony_ci		       const char *val);
788c2ecf20Sopenharmony_ciint read_sysfs_posint(const char *filename, const char *basedir);
798c2ecf20Sopenharmony_ciint read_sysfs_float(const char *filename, const char *basedir, float *val);
808c2ecf20Sopenharmony_ciint read_sysfs_string(const char *filename, const char *basedir, char *str);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#endif /* _IIO_UTILS_H_ */
83