1 /* IIO - useful set of util functionality
3 * Copyright (c) 2008 Jonathan Cameron
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
10 /* Made up value to limit allocation sizes */
14 #define IIO_MAX_NAME_LENGTH 30
16 #define IIO_EVENT_CODE_RING_50_FULL 200
17 #define IIO_EVENT_CODE_RING_75_FULL 201
18 #define IIO_EVENT_CODE_RING_100_FULL 202
20 const char *iio_dir
= "/sys/bus/iio/devices/";
22 struct iio_event_data
{
28 * find_type_by_name() - function to match top level types by name
29 * @name: top level type instance name
30 * @type: the type of top level instance being sort
32 * Typical types this is used for are device and trigger.
34 inline int find_type_by_name(const char *name
, const char *type
)
36 const struct dirent
*ent
;
37 int number
, numstrlen
;
41 char thisname
[IIO_MAX_NAME_LENGTH
];
45 dp
= opendir(iio_dir
);
47 printf("No industrialio devices available");
51 while (ent
= readdir(dp
), ent
!= NULL
) {
52 if (strcmp(ent
->d_name
, ".") != 0 &&
53 strcmp(ent
->d_name
, "..") != 0 &&
54 strlen(ent
->d_name
) > strlen(type
) &&
55 strncmp(ent
->d_name
, type
, strlen(type
)) == 0) {
56 numstrlen
= sscanf(ent
->d_name
+ strlen(type
),
59 /* verify the next character is not a colon */
60 if (strncmp(ent
->d_name
+ strlen(type
) + numstrlen
,
63 filename
= malloc(strlen(iio_dir
)
69 sprintf(filename
, "%s%s%d/name",
73 nameFile
= fopen(filename
, "r");
77 fscanf(nameFile
, "%s", thisname
);
78 if (strcmp(name
, thisname
) == 0)
87 inline int _write_sysfs_int(char *filename
, char *basedir
, int val
, int verify
)
92 char *temp
= malloc(strlen(basedir
) + strlen(filename
) + 2);
95 sprintf(temp
, "%s/%s", basedir
, filename
);
96 sysfsfp
= fopen(temp
, "w");
97 if (sysfsfp
== NULL
) {
98 printf("failed to open %s\n", temp
);
102 fprintf(sysfsfp
, "%d", val
);
105 sysfsfp
= fopen(temp
, "r");
106 if (sysfsfp
== NULL
) {
107 printf("failed to open %s\n", temp
);
111 fscanf(sysfsfp
, "%d", &test
);
113 printf("Possible failure in int write %d to %s%s\n",
125 int write_sysfs_int(char *filename
, char *basedir
, int val
)
127 return _write_sysfs_int(filename
, basedir
, val
, 0);
130 int write_sysfs_int_and_verify(char *filename
, char *basedir
, int val
)
132 return _write_sysfs_int(filename
, basedir
, val
, 1);
135 int _write_sysfs_string(char *filename
, char *basedir
, char *val
, int verify
)
139 char *temp
= malloc(strlen(basedir
) + strlen(filename
) + 2);
141 printf("Memory allocation failed\n");
144 sprintf(temp
, "%s/%s", basedir
, filename
);
145 sysfsfp
= fopen(temp
, "w");
146 if (sysfsfp
== NULL
) {
147 printf("Could not open %s\n", temp
);
151 fprintf(sysfsfp
, "%s", val
);
154 sysfsfp
= fopen(temp
, "r");
155 if (sysfsfp
== NULL
) {
159 fscanf(sysfsfp
, "%s", temp
);
160 if (strcmp(temp
, val
) != 0) {
161 printf("Possible failure in string write of %s "
177 * write_sysfs_string_and_verify() - string write, readback and verify
178 * @filename: name of file to write to
179 * @basedir: the sysfs directory in which the file is to be found
180 * @val: the string to write
182 int write_sysfs_string_and_verify(char *filename
, char *basedir
, char *val
)
184 return _write_sysfs_string(filename
, basedir
, val
, 1);
187 int write_sysfs_string(char *filename
, char *basedir
, char *val
)
189 return _write_sysfs_string(filename
, basedir
, val
, 0);
192 int read_sysfs_posint(char *filename
, char *basedir
)
196 char *temp
= malloc(strlen(basedir
) + strlen(filename
) + 2);
198 printf("Memory allocation failed");
201 sprintf(temp
, "%s/%s", basedir
, filename
);
202 sysfsfp
= fopen(temp
, "r");
203 if (sysfsfp
== NULL
) {
207 fscanf(sysfsfp
, "%d\n", &ret
);
214 int read_sysfs_float(char *filename
, char *basedir
, float *val
)
218 char *temp
= malloc(strlen(basedir
) + strlen(filename
) + 2);
220 printf("Memory allocation failed");
223 sprintf(temp
, "%s/%s", basedir
, filename
);
224 sysfsfp
= fopen(temp
, "r");
225 if (sysfsfp
== NULL
) {
229 fscanf(sysfsfp
, "%f\n", val
);