aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/iio/Documentation/iio_utils.h
blob: 74d312473378492b92f0c22da1882dfea6765475 (plain) (blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
/* IIO - useful set of util functionality
 *
 * Copyright (c) 2008 Jonathan Cameron
 *
 * 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.
 */

#define IIO_EVENT_CODE_RING_50_FULL 200
#define IIO_EVENT_CODE_RING_75_FULL 201
#define IIO_EVENT_CODE_RING_100_FULL 202

struct iio_event_data {
	int id;
	__s64 timestamp;
};


inline char *find_ring_subelement(const char *directory, const char *subelement)
{
	DIR *dp;
	const struct dirent *ent;
	int pos;
	char temp[100];
	char *returnstring;
	dp = opendir(directory);
	if (dp == NULL) {
		printf("could not directory: %s\n", directory);
		return NULL;
	}
	while (ent = readdir(dp), ent != NULL) {
		if (strcmp(ent->d_name, ".") != 0 &&
		    strcmp(ent->d_name, "..") != 0)  {
			if (strncmp(ent->d_name, subelement, strlen(subelement)) == 0) {
				int length = sprintf(temp, "%s%s%s", directory, ent->d_name, "/");
				returnstring = malloc(length+1);
				strncpy(returnstring, temp, length+1);
				return returnstring;

			}
		}
	}
	return 0;
}


char *find_type_by_name(const char *name, const char *type)
{
	const char *iio_dir = "/sys/class/iio/";
	const struct dirent *ent;
	int cnt, pos, pos2;

	FILE *nameFile;
	DIR *dp;
	char thisname[100];
	char temp[100];

	char *returnstring = NULL;
	struct stat Stat;
	pos = sprintf(temp, "%s", iio_dir);
	dp = opendir(iio_dir);
	if (dp == NULL) {
		printf("No industrialio devices available");
		return NULL;
	}
	while (ent = readdir(dp), ent != NULL) {
		cnt++;
		/*reject . and .. */
		if (strcmp(ent->d_name, ".") != 0 &&
		    strcmp(ent->d_name, "..") != 0)  {
			/*make sure it isn't a trigger!*/
			if (strncmp(ent->d_name, type, strlen(type)) == 0) {
				/* build full path to new file */
				pos2 = pos + sprintf(temp + pos, "%s/", ent->d_name);
				sprintf(temp + pos2, "name");
				printf("search location %s\n", temp);
				nameFile = fopen(temp, "r");
				if (!nameFile) {
					sprintf(temp + pos2, "modalias", ent->d_name);
					nameFile = fopen(temp, "r");
					if (!nameFile) {
						printf("Failed to find a name for device\n");
						return NULL;
					}
				}
				fscanf(nameFile, "%s", thisname);
				if (strcmp(name, thisname) == 0) {
					returnstring = malloc(strlen(temp) + 1);
					sprintf(temp + pos2, "");
					strcpy(returnstring, temp);
					return returnstring;
				}
				fclose(nameFile);

			}
		}
	}
}

int write_sysfs_int(char *filename, char *basedir, int val)
{
	int ret;
	FILE  *sysfsfp;
	char temp[100];
	sprintf(temp, "%s%s", basedir, filename);
	sysfsfp = fopen(temp, "w");
	if (sysfsfp == NULL)
		return -1;
	fprintf(sysfsfp, "%d", val);
	fclose(sysfsfp);
	return 0;
}

/**
 * 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 ret;
	FILE  *sysfsfp;
	char temp[100];
	sprintf(temp, "%s%s", basedir, filename);
	sysfsfp = fopen(temp, "w");
	if (sysfsfp == NULL)
		return -1;
	fprintf(sysfsfp, "%s", val);
	fclose(sysfsfp);

	sysfsfp = fopen(temp, "r");
	if (sysfsfp == NULL)
		return -1;
	fscanf(sysfsfp, "%s", temp);
	if (strcmp(temp, val) != 0) {
		printf("Possible failure in string write %s to %s%s \n",
		       val,
		       basedir,
		       filename);
		return -1;
	}
	return 0;
}

int read_sysfs_posint(char *filename, char *basedir)
{
	int ret;
	FILE  *sysfsfp;
	char temp[100];
	sprintf(temp, "%s%s", basedir, filename);
	sysfsfp = fopen(temp, "r");
	if (sysfsfp == NULL)
		return -1;
	fscanf(sysfsfp, "%d\n", &ret);
	fclose(sysfsfp);
	return ret;
}