summaryrefslogtreecommitdiff
path: root/libsensors_iio/src/iio_utils.h
blob: bb0d7c3590642c7e4868d3dbbba153a56fe7e93d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* IIO - useful set of util functionality
 *
 * Copyright (c) 2008 Jonathan Cameron
 * Modified by Denis Ciocca <denis.ciocca@st.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */

#ifndef IIO_UTILS
#define IIO_UTILS

#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <stdint.h>
#include <dirent.h>
#include <errno.h>
#include <stdbool.h>


/* Made up value to limit allocation sizes */
#define IIO_MAX_NAME_LENGTH		(70)

#define FORMAT_SCAN_ELEMENTS_DIR	"%s/scan_elements"
#define FORMAT_TYPE_FILE		"%s_type"

#define IIO_DEFAULT_BUFFER_LEN		(2)

/**
 * struct iio_channel_info - information about a given channel
 * @name: channel name
 * @generic_name: general name for channel type
 * @scale: scale factor to be applied for conversion to si units
 * @offset: offset to be applied for conversion to si units
 * @index: the channel index in the buffer output
 * @bytes: number of bytes occupied in buffer output
 * @mask: a bit mask for the raw output
 * @is_signed: is the raw value stored signed
 * @enabled: is this channel enabled
 **/
struct iio_channel_info {
	char *name;
	char *generic_name;
	float scale;
	float offset;
	unsigned index;
	unsigned bytes;
	unsigned bits_used;
	unsigned shift;
	uint64_t mask;
	unsigned be;
	unsigned is_signed;
	unsigned enabled;
	unsigned location;
	bool isfloat;
	bool multi_data;
};


/**
 * build_channel_array() - function to figure out what channels are present
 * @device_dir: the IIO device directory in sysfs
 * @
 **/
int iio_utils_build_channel_array(const char *device_dir,
	struct iio_channel_info **ci_array, int *counter, bool read_offset);

/**
 * find_type_by_name() - function to match top level types by name
 * @name: top level type instance name
 * @type: the type of top level instance being sort
 *
 * Typical types this is used for are device and trigger.
 **/
int find_type_by_name(char *name, const char *type);

int write_sysfs_int(char *filename, char *basedir, int val);

int write_sysfs_int_and_verify(char *filename, char *basedir, int val);

int write_sysfs_ulong_and_verify(char *filename, char *basedir, unsigned long val);

int write_sysfs_float(char *filename, char *basedir, float val);

int write_sysfs_float_and_verify(char *filename, char *basedir, float val);





#define IIO_UTILS_MAX_SAMP_FREQ_AVAILABLE		(10)
#define IIO_UTILS_SCALE_AVAILABLE			(10)

struct iio_scale_available {
	float values[IIO_UTILS_SCALE_AVAILABLE];
	unsigned int num_available;
};

struct iio_sampling_frequency_available {
	unsigned int hz[IIO_UTILS_MAX_SAMP_FREQ_AVAILABLE];
	unsigned int num_available;
};

struct iio_device {
	unsigned int dev_num;
	char name[IIO_MAX_NAME_LENGTH];
};

int iio_utils_get_devices_name(struct iio_device devices[], unsigned int max_list);

int iio_utils_get_sampling_frequency_available(const char *device_dir,
					struct iio_sampling_frequency_available *sfa);

int iio_utils_get_scale_available(const char *device_dir, struct iio_scale_available *sa, int device_type);

int iio_utils_set_scale(const char *device_dir, float value, int device_type);

int iio_utils_get_hw_fifo_lenght(const char *device_dir);


int iio_utils_enable_sensor(const char *device_dir, bool enable);

/**
 * write_sysfs_string_and_verify() - string write, readback and verify
 * @filename: name of file to write to
 * @basedir: the sysfs directory in which the file is to be found
 * @val: the string to write
 **/
int write_sysfs_string_and_verify(char *filename, char *basedir, char *val);

int write_sysfs_string(char *filename, char *basedir, char *val);

int read_sysfs_posint(char *filename, char *basedir);

int read_sysfs_float(char *filename, char *basedir, float *val);

int read_sysfs_string(char *filename, char *basedir, char *str);

int read_sysfs_byte(char *filename, char *basedir, uint8_t *data, size_t len);

int write_sysfs_byte(char *filename, char *basedir, uint8_t *data, size_t len);

#endif /* IIO_UTILS */