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 */
17 #define IIO_MAX_NAME_LENGTH 30
19 #define IIO_EV_CLASS_BUFFER 0
20 #define IIO_BUFFER_EVENT_CODE(code) \
21 (IIO_EV_CLASS_BUFFER | (code << 8))
23 #define IIO_EVENT_CODE_RING_50_FULL IIO_BUFFER_EVENT_CODE(0)
24 #define IIO_EVENT_CODE_RING_75_FULL IIO_BUFFER_EVENT_CODE(1)
25 #define IIO_EVENT_CODE_RING_100_FULL IIO_BUFFER_EVENT_CODE(2)
28 #define FORMAT_SCAN_ELEMENTS_DIR "%s:buffer0/scan_elements"
29 #define FORMAT_TYPE_FILE "%s_type"
31 const char *iio_dir = "/sys/bus/iio/devices/";
33 struct iio_event_data {
39 * iioutils_break_up_name() - extract generic name from full channel name
40 * @full_name: the full channel name
41 * @generic_name: the output generic channel name
43 static int iioutils_break_up_name(const char *full_name,
49 current = strdup(full_name);
50 working = strtok(current, "_\0");
62 *generic_name = strdup(working);
69 * struct iio_channel_info - information about a given channel
71 * @generic_name: general name for channel type
72 * @scale: scale factor to be applied for conversion to si units
73 * @offset: offset to be applied for conversion to si units
74 * @index: the channel index in the buffer output
75 * @bytes: number of bytes occupied in buffer output
76 * @mask: a bit mask for the raw output
77 * @is_signed: is the raw value stored signed
78 * @enabled: is this channel enabled
80 struct iio_channel_info {
95 * iioutils_get_type() - find and process _type attribute data
96 * @is_signed: output whether channel is signed
97 * @bytes: output how many bytes the channel storage occupies
98 * @mask: output a bit mask for the raw data
99 * @device_dir: the iio device directory
100 * @name: the channel name
101 * @generic_name: the channel type name
103 inline int iioutils_get_type(unsigned *is_signed,
107 const char *device_dir,
109 const char *generic_name)
114 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
116 unsigned sizeint, padint;
117 const struct dirent *ent;
119 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
124 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
127 goto error_free_scan_el_dir;
129 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
132 goto error_free_builtname;
135 dp = opendir(scan_el_dir);
138 goto error_free_builtname_generic;
140 while (ent = readdir(dp), ent != NULL)
142 * Do we allow devices to override a generic name with
145 if ((strcmp(builtname, ent->d_name) == 0) ||
146 (strcmp(builtname_generic, ent->d_name) == 0)) {
147 ret = asprintf(&filename,
148 "%s/%s", scan_el_dir, ent->d_name);
153 sysfsfp = fopen(filename, "r");
154 if (sysfsfp == NULL) {
155 printf("failed to open %s\n", filename);
157 goto error_free_filename;
160 "%c%u/%u", &signchar, bits_used, &padint);
165 *mask = (1 << *bits_used) - 1;
176 error_free_builtname_generic:
177 free(builtname_generic);
178 error_free_builtname:
180 error_free_scan_el_dir:
186 inline int iioutils_get_param_float(float *output,
187 const char *param_name,
188 const char *device_dir,
190 const char *generic_name)
195 char *builtname, *builtname_generic;
196 char *filename = NULL;
197 const struct dirent *ent;
199 ret = asprintf(&builtname, "%s_%s", name, param_name);
204 ret = asprintf(&builtname_generic,
205 "%s_%s", generic_name, param_name);
208 goto error_free_builtname;
210 dp = opendir(device_dir);
213 goto error_free_builtname_generic;
215 while (ent = readdir(dp), ent != NULL)
216 if ((strcmp(builtname, ent->d_name) == 0) ||
217 (strcmp(builtname_generic, ent->d_name) == 0)) {
218 ret = asprintf(&filename,
219 "%s/%s", device_dir, ent->d_name);
224 sysfsfp = fopen(filename, "r");
227 goto error_free_filename;
229 fscanf(sysfsfp, "%f", output);
237 error_free_builtname_generic:
238 free(builtname_generic);
239 error_free_builtname:
247 * build_channel_array() - function to figure out what channels are present
248 * @device_dir: the IIO device directory in sysfs
251 inline int build_channel_array(const char *device_dir,
252 struct iio_channel_info **ci_array,
257 int count = 0, temp, i;
258 struct iio_channel_info *current;
260 const struct dirent *ent;
265 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
270 dp = opendir(scan_el_dir);
273 goto error_free_name;
275 while (ent = readdir(dp), ent != NULL)
276 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
278 ret = asprintf(&filename,
279 "%s/%s", scan_el_dir, ent->d_name);
282 goto error_close_dir;
284 sysfsfp = fopen(filename, "r");
285 if (sysfsfp == NULL) {
288 goto error_close_dir;
290 fscanf(sysfsfp, "%u", &ret);
296 *ci_array = malloc(sizeof(**ci_array)*(*counter));
297 if (*ci_array == NULL) {
299 goto error_close_dir;
302 while (ent = readdir(dp), ent != NULL) {
303 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
305 current = &(*ci_array)[count++];
306 ret = asprintf(&filename,
307 "%s/%s", scan_el_dir, ent->d_name);
310 /* decrement count to avoid freeing name */
312 goto error_cleanup_array;
314 sysfsfp = fopen(filename, "r");
315 if (sysfsfp == NULL) {
318 goto error_cleanup_array;
320 fscanf(sysfsfp, "%u", ¤t->enabled);
323 current->scale = 1.0;
325 current->name = strndup(ent->d_name,
326 strlen(ent->d_name) -
328 if (current->name == NULL) {
331 goto error_cleanup_array;
333 /* Get the generic and specific name elements */
334 ret = iioutils_break_up_name(current->name,
335 ¤t->generic_name);
338 goto error_cleanup_array;
340 ret = asprintf(&filename,
347 goto error_cleanup_array;
349 sysfsfp = fopen(filename, "r");
350 fscanf(sysfsfp, "%u", ¤t->index);
354 ret = iioutils_get_param_float(¤t->scale,
358 current->generic_name);
360 goto error_cleanup_array;
361 ret = iioutils_get_param_float(¤t->offset,
365 current->generic_name);
367 goto error_cleanup_array;
368 ret = iioutils_get_type(¤t->is_signed,
374 current->generic_name);
377 /* reorder so that the array is in index order*/
378 current = malloc(sizeof(**ci_array)**counter);
379 if (current == NULL) {
381 goto error_cleanup_array;
386 while (count < *counter)
387 for (i = 0; i < *counter; i++)
388 if ((*ci_array)[i].index == temp) {
389 memcpy(¤t[count++],
401 for (i = count - 1; i >= 0; i++)
402 free((*ci_array)[i].name);
413 * find_type_by_name() - function to match top level types by name
414 * @name: top level type instance name
415 * @type: the type of top level instance being sort
417 * Typical types this is used for are device and trigger.
419 inline int find_type_by_name(const char *name, const char *type)
421 const struct dirent *ent;
422 int number, numstrlen;
426 char thisname[IIO_MAX_NAME_LENGTH];
429 dp = opendir(iio_dir);
431 printf("No industrialio devices available");
435 while (ent = readdir(dp), ent != NULL) {
436 if (strcmp(ent->d_name, ".") != 0 &&
437 strcmp(ent->d_name, "..") != 0 &&
438 strlen(ent->d_name) > strlen(type) &&
439 strncmp(ent->d_name, type, strlen(type)) == 0) {
440 numstrlen = sscanf(ent->d_name + strlen(type),
443 /* verify the next character is not a colon */
444 if (strncmp(ent->d_name + strlen(type) + numstrlen,
447 filename = malloc(strlen(iio_dir)
451 if (filename == NULL)
453 sprintf(filename, "%s%s%d/name",
457 nameFile = fopen(filename, "r");
461 fscanf(nameFile, "%s", thisname);
462 if (strcmp(name, thisname) == 0)
471 inline int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
476 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
479 sprintf(temp, "%s/%s", basedir, filename);
480 sysfsfp = fopen(temp, "w");
481 if (sysfsfp == NULL) {
482 printf("failed to open %s\n", temp);
486 fprintf(sysfsfp, "%d", val);
489 sysfsfp = fopen(temp, "r");
490 if (sysfsfp == NULL) {
491 printf("failed to open %s\n", temp);
495 fscanf(sysfsfp, "%d", &test);
497 printf("Possible failure in int write %d to %s%s\n",
509 int write_sysfs_int(char *filename, char *basedir, int val)
511 return _write_sysfs_int(filename, basedir, val, 0);
514 int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
516 return _write_sysfs_int(filename, basedir, val, 1);
519 int _write_sysfs_string(char *filename, char *basedir, char *val, int verify)
523 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
525 printf("Memory allocation failed\n");
528 sprintf(temp, "%s/%s", basedir, filename);
529 sysfsfp = fopen(temp, "w");
530 if (sysfsfp == NULL) {
531 printf("Could not open %s\n", temp);
535 fprintf(sysfsfp, "%s", val);
538 sysfsfp = fopen(temp, "r");
539 if (sysfsfp == NULL) {
540 printf("could not open file to verify\n");
544 fscanf(sysfsfp, "%s", temp);
545 if (strcmp(temp, val) != 0) {
546 printf("Possible failure in string write of %s "
563 * write_sysfs_string_and_verify() - string write, readback and verify
564 * @filename: name of file to write to
565 * @basedir: the sysfs directory in which the file is to be found
566 * @val: the string to write
568 int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
570 return _write_sysfs_string(filename, basedir, val, 1);
573 int write_sysfs_string(char *filename, char *basedir, char *val)
575 return _write_sysfs_string(filename, basedir, val, 0);
578 int read_sysfs_posint(char *filename, char *basedir)
582 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
584 printf("Memory allocation failed");
587 sprintf(temp, "%s/%s", basedir, filename);
588 sysfsfp = fopen(temp, "r");
589 if (sysfsfp == NULL) {
593 fscanf(sysfsfp, "%d\n", &ret);
600 int read_sysfs_float(char *filename, char *basedir, float *val)
604 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
606 printf("Memory allocation failed");
609 sprintf(temp, "%s/%s", basedir, filename);
610 sysfsfp = fopen(temp, "r");
611 if (sysfsfp == NULL) {
615 fscanf(sysfsfp, "%f\n", val);