Release 4.10 tools/iio/iio_utils.c
/* 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.
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <dirent.h>
#include <errno.h>
#include <ctype.h>
#include "iio_utils.h"
const char *iio_dir = "/sys/bus/iio/devices/";
static char * const iio_direction[] = {
"in",
"out",
};
/**
* iioutils_break_up_name() - extract generic name from full channel name
* @full_name: the full channel name
* @generic_name: the output generic channel name
*
* Returns 0 on success, or a negative error code if string extraction failed.
**/
int iioutils_break_up_name(const char *full_name, char **generic_name)
{
char *current;
char *w, *r;
char *working, *prefix = "";
int i, ret;
for (i = 0; i < ARRAY_SIZE(iio_direction); i++)
if (!strncmp(full_name, iio_direction[i],
strlen(iio_direction[i]))) {
prefix = iio_direction[i];
break;
}
current = strdup(full_name + strlen(prefix) + 1);
if (!current)
return -ENOMEM;
working = strtok(current, "_\0");
if (!working) {
free(current);
return -EINVAL;
}
w = working;
r = working;
while (*r != '\0') {
if (!isdigit(*r)) {
*w = *r;
w++;
}
r++;
}
*w = '\0';
ret = asprintf(generic_name, "%s_%s", prefix, working);
free(current);
return (ret == -1) ? -ENOMEM : 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
jonathan cameron | jonathan cameron | 103 | 48.82% | 1 | 20.00% |
irina tirdea | irina tirdea | 68 | 32.23% | 1 | 20.00% |
hartmut knaack | hartmut knaack | 39 | 18.48% | 2 | 40.00% |
cristina opriceana | cristina opriceana | 1 | 0.47% | 1 | 20.00% |
| Total | 211 | 100.00% | 5 | 100.00% |
/**
* iioutils_get_type() - find and process _type attribute data
* @is_signed: output whether channel is signed
* @bytes: output how many bytes the channel storage occupies
* @bits_used: output number of valid bits of data
* @shift: output amount of bits to shift right data before applying bit mask
* @mask: output a bit mask for the raw data
* @be: output if data in big endian
* @device_dir: the IIO device directory
* @name: the channel name
* @generic_name: the channel type name
*
* Returns a value >= 0 on success, otherwise a negative error code.
**/
int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
unsigned *shift, uint64_t *mask, unsigned *be,
const char *device_dir, const char *name,
const char *generic_name)
{
FILE *sysfsfp;
int ret;
DIR *dp;
char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
char signchar, endianchar;
unsigned padint;
const struct dirent *ent;
ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
if (ret < 0)
return -ENOMEM;
ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
if (ret < 0) {
ret = -ENOMEM;
goto error_free_scan_el_dir;
}
ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
if (ret < 0) {
ret = -ENOMEM;
goto error_free_builtname;
}
dp = opendir(scan_el_dir);
if (!dp) {
ret = -errno;
goto error_free_builtname_generic;
}
ret = -ENOENT;
while (ent = readdir(dp), ent)
if ((strcmp(builtname, ent->d_name) == 0) ||
(strcmp(builtname_generic, ent->d_name) == 0)) {
ret = asprintf(&filename,
"%s/%s", scan_el_dir, ent->d_name);
if (ret < 0) {
ret = -ENOMEM;
goto error_closedir;
}
sysfsfp = fopen(filename, "r");
if (!sysfsfp) {
ret = -errno;
fprintf(stderr, "failed to open %s\n",
filename);
goto error_free_filename;
}
ret = fscanf(sysfsfp,
"%ce:%c%u/%u>>%u",
&endianchar,
&signchar,
bits_used,
&padint, shift);
if (ret < 0) {
ret = -errno;
fprintf(stderr,
"failed to pass scan type description\n");
goto error_close_sysfsfp;
} else if (ret != 5) {
ret = -EIO;
fprintf(stderr,
"scan type description didn't match\n");
goto error_close_sysfsfp;
}
*be = (endianchar == 'b');
*bytes = padint / 8;
if (*bits_used == 64)
*mask = ~0;
else
*mask = (1ULL << *bits_used) - 1;
*is_signed = (signchar == 's');
if (fclose(sysfsfp)) {
ret = -errno;
fprintf(stderr, "Failed to close %s\n",
filename);
goto error_free_filename;
}
sysfsfp = 0;
free(filename);
filename = 0;
/*
* Avoid having a more generic entry overwriting
* the settings.
*/
if (strcmp(builtname, ent->d_name) == 0)
break;
}
error_close_sysfsfp:
if (sysfsfp)
if (fclose(sysfsfp))
perror("iioutils_get_type(): Failed to close file");
error_free_filename:
if (filename)
free(filename);
error_closedir:
if (closedir(dp) == -1)
perror("iioutils_get_type(): Failed to close directory");
error_free_builtname_generic:
free(builtname_generic);
error_free_builtname:
free(builtname);
error_free_scan_el_dir:
free(scan_el_dir);
return ret;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
jonathan cameron | jonathan cameron | 408 | 75.70% | 4 | 25.00% |
hartmut knaack | hartmut knaack | 81 | 15.03% | 6 | 37.50% |
peter meerwald | peter meerwald | 18 | 3.34% | 1 | 6.25% |
matthew ranostay | matthew ranostay | 15 | 2.78% | 1 | 6.25% |
cristina opriceana | cristina opriceana | 14 | 2.60% | 2 | 12.50% |
michael hennerich | michael hennerich | 2 | 0.37% | 1 | 6.25% |
irina tirdea | irina tirdea | 1 | 0.19% | 1 | 6.25% |
| Total | 539 | 100.00% | 16 | 100.00% |
/**
* iioutils_get_param_float() - read a float value from a channel parameter
* @output: output the float value
* @param_name: the parameter name to read
* @device_dir: the IIO device directory in sysfs
* @name: the channel name
* @generic_name: the channel type name
*
* Returns a value >= 0 on success, otherwise a negative error code.
**/
int iioutils_get_param_float(float *output, const char *param_name,
const char *device_dir, const char *name,
const char *generic_name)
{
FILE *sysfsfp;
int ret;
DIR *dp;
char *builtname, *builtname_generic;
char *filename = NULL;
const struct dirent *ent;
ret = asprintf(&builtname, "%s_%s", name, param_name);
if (ret < 0)
return -ENOMEM;
ret = asprintf(&builtname_generic,
"%s_%s", generic_name, param_name);
if (ret < 0) {
ret = -ENOMEM;
goto error_free_builtname;
}
dp = opendir(device_dir);
if (!dp) {
ret = -errno;
goto error_free_builtname_generic;
}
ret = -ENOENT;
while (ent = readdir(dp), ent)
if ((strcmp(builtname, ent->d_name) == 0) ||
(strcmp(builtname_generic, ent->d_name) == 0)) {
ret = asprintf(&filename,
"%s/%s", device_dir, ent->d_name);
if (ret < 0) {
ret = -ENOMEM;
goto error_closedir;
}
sysfsfp = fopen(filename, "r");
if (!sysfsfp) {
ret = -errno;
goto error_free_filename;
}
errno = 0;
if (fscanf(sysfsfp, "%f", output) != 1)
ret = errno ? -errno : -ENODATA;
break;
}
error_free_filename:
if (filename)
free(filename);
error_closedir:
if (closedir(dp) == -1)
perror("iioutils_get_param_float(): Failed to close directory");
error_free_builtname_generic:
free(builtname_generic);
error_free_builtname:
free(builtname);
return ret;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
jonathan cameron | jonathan cameron | 274 | 88.39% | 1 | 25.00% |
hartmut knaack | hartmut knaack | 35 | 11.29% | 2 | 50.00% |
cristina opriceana | cristina opriceana | 1 | 0.32% | 1 | 25.00% |
| Total | 310 | 100.00% | 4 | 100.00% |
/**
* bsort_channel_array_by_index() - sort the array in index order
* @ci_array: the iio_channel_info array to be sorted
* @cnt: the amount of array elements
**/
void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt)
{
struct iio_channel_info temp;
int x, y;
for (x = 0; x < cnt; x++)
for (y = 0; y < (cnt - 1); y++)
if (ci_array[y].index > ci_array[y + 1].index) {
temp = ci_array[y + 1];
ci_array[y + 1] = ci_array[y];
ci_array[y] = temp;
}
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
michael hennerich | michael hennerich | 100 | 100.00% | 1 | 100.00% |
| Total | 100 | 100.00% | 1 | 100.00% |
/**
* build_channel_array() - function to figure out what channels are present
* @device_dir: the IIO device directory in sysfs
* @ci_array: output the resulting array of iio_channel_info
* @counter: output the amount of array elements
*
* Returns 0 on success, otherwise a negative error code.
**/
int build_channel_array(const char *device_dir,
struct iio_channel_info **ci_array, int *counter)
{
DIR *dp;
FILE *sysfsfp;
int count = 0, i;
struct iio_channel_info *current;
int ret;
const struct dirent *ent;
char *scan_el_dir;
char *filename;
*counter = 0;
ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
if (ret < 0)
return -ENOMEM;
dp = opendir(scan_el_dir);
if (!dp) {
ret = -errno;
goto error_free_name;
}
while (ent = readdir(dp), ent)
if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
"_en") == 0) {
ret = asprintf(&filename,
"%s/%s", scan_el_dir, ent->d_name);
if (ret < 0) {
ret = -ENOMEM;
goto error_close_dir;
}
sysfsfp = fopen(filename, "r");
if (!sysfsfp) {
ret = -errno;
free(filename);
goto error_close_dir;
}
errno = 0;
if (fscanf(sysfsfp, "%i", &ret) != 1) {
ret = errno ? -errno : -ENODATA;
if (fclose(sysfsfp))
perror("build_channel_array(): Failed to close file");
free(filename);
goto error_close_dir;
}
if (ret == 1)
(*counter)++;
if (fclose(sysfsfp)) {
ret = -errno;
free(filename);
goto error_close_dir;
}
free(filename);
}
*ci_array = malloc(sizeof(**ci_array) * (*counter));
if (!*ci_array) {
ret = -ENOMEM;
goto error_close_dir;
}
seekdir(dp, 0);
while (ent = readdir(dp), ent) {
if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
"_en") == 0) {
int current_enabled = 0;
current = &(*ci_array)[count++];
ret = asprintf(&filename,
"%s/%s", scan_el_dir, ent->d_name);
if (ret < 0) {
ret = -ENOMEM;
/* decrement count to avoid freeing name */
count--;
goto error_cleanup_array;
}
sysfsfp = fopen(filename, "r");
if (!sysfsfp) {
ret = -errno;
free(filename);
count--;
goto error_cleanup_array;
}
errno = 0;
if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) {
ret = errno ? -errno : -ENODATA;
free(filename);
count--;
goto error_cleanup_array;
}
if (fclose(sysfsfp)) {
ret = -errno;
free(filename);
count--;
goto error_cleanup_array;
}
if (!current_enabled) {
free(filename);
count--;
continue;
}
current->scale = 1.0;
current->offset = 0;
current->name = strndup(ent->d_name,
strlen(ent->d_name) -
strlen("_en"));
if (!current->name) {
free(filename);
ret = -ENOMEM;
count--;
goto error_cleanup_array;
}
/* Get the generic and specific name elements */
ret = iioutils_break_up_name(current->name,
¤t->generic_name);
if (ret) {
free(filename);
free(current->name);
count--;
goto error_cleanup_array;
}
ret = asprintf(&filename,
"%s/%s_index",
scan_el_dir,
current->name);
if (ret < 0) {
free(filename);
ret = -ENOMEM;
goto error_cleanup_array;
}
sysfsfp = fopen(filename, "r");
if (!sysfsfp) {
ret = -errno;
fprintf(stderr, "failed to open %s\n",
filename);
free(filename);
goto error_cleanup_array;
}
errno = 0;
if (fscanf(sysfsfp, "%u", ¤t->index) != 1) {
ret = errno ? -errno : -ENODATA;
if (fclose(sysfsfp))
perror("build_channel_array(): Failed to close file");
free(filename);
goto error_cleanup_array;
}
if (fclose(sysfsfp)) {
ret = -errno;
free(filename);
goto error_cleanup_array;
}
free(filename);
/* Find the scale */
ret = iioutils_get_param_float(¤t->scale,
"scale",
device_dir,
current->name,
current->generic_name);
if ((ret < 0) && (ret != -ENOENT))
goto error_cleanup_array;
ret = iioutils_get_param_float(¤t->offset,
"offset",
device_dir,
current->name,
current->generic_name);
if ((ret < 0) && (ret != -ENOENT))
goto error_cleanup_array;
ret = iioutils_get_type(¤t->is_signed,
¤t->bytes,
¤t->bits_used,
¤t->shift,
¤t->mask,
¤t->be,
device_dir,
current->name,
current->generic_name);
if (ret < 0)
goto error_cleanup_array;
}
}
if (closedir(dp) == -1) {
ret = -errno;
goto error_cleanup_array;
}
free(scan_el_dir);
/* reorder so that the array is in index order */
bsort_channel_array_by_index(*ci_array, *counter);
return 0;
error_cleanup_array:
for (i = count - 1; i >= 0; i--) {
free((*ci_array)[i].name);
free((*ci_array)[i].generic_name);
}
free(*ci_array);
*ci_array = NULL;
*counter = 0;
error_close_dir:
if (dp)
if (closedir(dp) == -1)
perror("build_channel_array(): Failed to close dir");
error_free_name:
free(scan_el_dir);
return ret;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
jonathan cameron | jonathan cameron | 712 | 67.17% | 4 | 20.00% |
hartmut knaack | hartmut knaack | 278 | 26.23% | 7 | 35.00% |
joo aun saw | joo aun saw | 29 | 2.74% | 3 | 15.00% |
michael hennerich | michael hennerich | 22 | 2.08% | 1 | 5.00% |
cristina opriceana | cristina opriceana | 9 | 0.85% | 2 | 10.00% |
craig markwardt | craig markwardt | 7 | 0.66% | 1 | 5.00% |
toralf foerster | toralf foerster | 2 | 0.19% | 1 | 5.00% |
roel kluin | roel kluin | 1 | 0.09% | 1 | 5.00% |
| Total | 1060 | 100.00% | 20 | 100.00% |
static int calc_digits(int num)
{
int count = 0;
while (num != 0) {
num /= 10;
count++;
}
return count;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
hartmut knaack | hartmut knaack | 31 | 96.88% | 1 | 50.00% |
joo aun saw | joo aun saw | 1 | 3.12% | 1 | 50.00% |
| Total | 32 | 100.00% | 2 | 100.00% |
/**
* 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 searched
*
* Returns the device number of a matched IIO device on success, otherwise a
* negative error code.
* Typical types this is used for are device and trigger.
**/
int find_type_by_name(const char *name, const char *type)
{
const struct dirent *ent;
int number, numstrlen, ret;
FILE *namefp;
DIR *dp;
char thisname[IIO_MAX_NAME_LENGTH];
char *filename;
dp = opendir(iio_dir);
if (!dp) {
fprintf(stderr, "No industrialio devices available\n");
return -ENODEV;
}
while (ent = readdir(dp), ent) {
if (strcmp(ent->d_name, ".") != 0 &&
strcmp(ent->d_name, "..") != 0 &&
strlen(ent->d_name) > strlen(type) &&
strncmp(ent->d_name, type, strlen(type)) == 0) {
errno = 0;
ret = sscanf(ent->d_name + strlen(type), "%d", &number);
if (ret < 0) {
ret = -errno;
fprintf(stderr,
"failed to read element number\n");
goto error_close_dir;
} else if (ret != 1) {
ret = -EIO;
fprintf(stderr,
"failed to match element number\n");
goto error_close_dir;
}
numstrlen = calc_digits(number);
/* verify the next character is not a colon */
if (strncmp(ent->d_name + strlen(type) + numstrlen,
":", 1) != 0) {
filename = malloc(strlen(iio_dir) + strlen(type)
+ numstrlen + 6);
if (!filename) {
ret = -ENOMEM;
goto error_close_dir;
}
ret = sprintf(filename, "%s%s%d/name", iio_dir,
type, number);
if (ret < 0) {
free(filename);
goto error_close_dir;
}
namefp = fopen(filename, "r");
if (!namefp) {
free(filename);
continue;
}
free(filename);
errno = 0;
if (fscanf(namefp, "%s", thisname) != 1) {
ret = errno ? -errno : -ENODATA;
goto error_close_dir;
}
if (fclose(namefp)) {
ret = -errno;
goto error_close_dir;
}
if (strcmp(name, thisname) == 0) {
if (closedir(dp) == -1)
return -errno;
return number;
}
}
}
}
if (closedir(dp) == -1)
return -errno;
return -ENODEV;
error_close_dir:
if (closedir(dp) == -1)
perror("find_type_by_name(): Failed to close directory");
return ret;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
jonathan cameron | jonathan cameron | 254 | 56.57% | 2 | 20.00% |
hartmut knaack | hartmut knaack | 159 | 35.41% | 3 | 30.00% |
peter meerwald | peter meerwald | 24 | 5.35% | 2 | 20.00% |
cristina opriceana | cristina opriceana | 11 | 2.45% | 2 | 20.00% |
barry song | barry song | 1 | 0.22% | 1 | 10.00% |
| Total | 449 | 100.00% | 10 | 100.00% |
static int _write_sysfs_int(const char *filename, const char *basedir, int val,
int verify)
{
int ret = 0;
FILE *sysfsfp;
int test;
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (!temp)
return -ENOMEM;
ret = sprintf(temp, "%s/%s", basedir, filename);
if (ret < 0)
goto error_free;
sysfsfp = fopen(temp, "w");
if (!sysfsfp) {
ret = -errno;
fprintf(stderr, "failed to open %s\n", temp);
goto error_free;
}
ret = fprintf(sysfsfp, "%d", val);
if (ret < 0) {
if (fclose(sysfsfp))
perror("_write_sysfs_int(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp)) {
ret = -errno;
goto error_free;
}
if (verify) {
sysfsfp = fopen(temp, "r");
if (!sysfsfp) {
ret = -errno;
fprintf(stderr, "failed to open %s\n", temp);
goto error_free;
}
if (fscanf(sysfsfp