/** *iioutils_break_up_name()-extractgenericnamefromfullchannelname *@full_name:thefullchannelname *@generic_name:theoutputgenericchannelname * *Returns0onsuccess,oranegativeerrorcodeifstringextractionfailed.
**/ int iioutils_break_up_name(constchar *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++;
}
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;
}
}
/** *build_channel_array()-functiontofigureoutwhatchannelsarepresent *@device_dir:theIIOdevicedirectoryinsysfs *@buffer_idx:theIIObufferforthischannelarray *@ci_array:outputtheresultingarrayofiio_channel_info *@counter:outputtheamountofarrayelements * *Returns0onsuccess,otherwiseanegativeerrorcode.
**/ int build_channel_array(constchar *device_dir, int buffer_idx, struct iio_channel_info **ci_array, int *counter)
{
DIR *dp;
FILE *sysfsfp; int count = 0, i; struct iio_channel_info *current; int ret; conststruct dirent *ent; char *scan_el_dir; char *filename;
*counter = 0;
ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir, buffer_idx); 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");
free(filename); if (!sysfsfp) {
ret = -errno; 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");
goto error_close_dir;
} if (ret == 1)
(*counter)++;
if (fclose(sysfsfp)) {
ret = -errno; goto error_close_dir;
}
}
*ci_array = malloc(sizeof(**ci_array) * (*counter)); if (!*ci_array) {
ret = -ENOMEM; goto error_close_dir;
}
rewinddir(dp); 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");
free(filename); if (!sysfsfp) {
ret = -errno;
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(current->name);
count--; goto error_cleanup_array;
}
ret = asprintf(&filename, "%s/%s_index",
scan_el_dir,
current->name); if (ret < 0) {
ret = -ENOMEM; goto error_cleanup_array;
}
sysfsfp = fopen(filename, "r");
free(filename); if (!sysfsfp) {
ret = -errno;
fprintf(stderr, "failed to open %s/%s_index\n",
scan_el_dir, current->name); 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");
goto error_cleanup_array;
}
if (fclose(sysfsfp)) {
ret = -errno; goto error_cleanup_array;
}
/* 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,
buffer_idx,
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);
return0;
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;
}
staticint calc_digits(int num)
{ int count = 0;
/* It takes a digit to represent zero */ if (!num) return1;
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;
} elseif (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;
}
staticint _write_sysfs_string(constchar *filename, constchar *basedir, constchar *val, int verify)
{ int ret = 0;
FILE *sysfsfp; char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (!temp) {
fprintf(stderr, "Memory allocation failed\n"); 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, "Could not open %s\n", temp); goto error_free;
}
ret = fprintf(sysfsfp, "%s", val); if (ret < 0) { if (fclose(sysfsfp))
perror("_write_sysfs_string(): 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, "Could not open file to verify\n"); goto error_free;
}
if (fscanf(sysfsfp, "%s", temp) != 1) {
ret = errno ? -errno : -ENODATA; if (fclose(sysfsfp))
perror("_write_sysfs_string(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp)) {
ret = -errno; goto error_free;
}
if (strcmp(temp, val) != 0) {
fprintf(stderr, "Possible failure in string write of %s " "Should be %s written to %s/%s\n", temp, val,
basedir, filename);
ret = -1;
}
}
if (!temp) {
fprintf(stderr, "Memory allocation failed"); return -ENOMEM;
}
ret = sprintf(temp, "%s/%s", basedir, filename); if (ret < 0) goto error_free;
sysfsfp = fopen(temp, "r"); if (!sysfsfp) {
ret = -errno; goto error_free;
}
errno = 0; if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
ret = errno ? -errno : -ENODATA; if (fclose(sysfsfp))
perror("read_sysfs_posint(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp))
ret = -errno;
error_free:
free(temp);
return ret;
}
/** *read_sysfs_float()-readafloatvaluefromfile *@filename:nameoffiletoreadfrom *@basedir:thesysfsdirectoryinwhichthefileistobefound *@val:outputthereadfloatvalue * *Returnsavalue>=0onsuccess,otherwiseanegativeerrorcode.
**/ int read_sysfs_float(constchar *filename, constchar *basedir, float *val)
{ int ret = 0;
FILE *sysfsfp; char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (!temp) {
fprintf(stderr, "Memory allocation failed"); return -ENOMEM;
}
ret = sprintf(temp, "%s/%s", basedir, filename); if (ret < 0) goto error_free;
sysfsfp = fopen(temp, "r"); if (!sysfsfp) {
ret = -errno; goto error_free;
}
errno = 0; if (fscanf(sysfsfp, "%f\n", val) != 1) {
ret = errno ? -errno : -ENODATA; if (fclose(sysfsfp))
perror("read_sysfs_float(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp))
ret = -errno;
error_free:
free(temp);
return ret;
}
/** *read_sysfs_string()-readastringfromfile *@filename:nameoffiletoreadfrom *@basedir:thesysfsdirectoryinwhichthefileistobefound *@str:outputthereadstring * *Returnsavalue>=0onsuccess,otherwiseanegativeerrorcode.
**/ int read_sysfs_string(constchar *filename, constchar *basedir, char *str)
{ int ret = 0;
FILE *sysfsfp; char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (!temp) {
fprintf(stderr, "Memory allocation failed"); return -ENOMEM;
}
ret = sprintf(temp, "%s/%s", basedir, filename); if (ret < 0) goto error_free;
sysfsfp = fopen(temp, "r"); if (!sysfsfp) {
ret = -errno; goto error_free;
}
errno = 0; if (fscanf(sysfsfp, "%s\n", str) != 1) {
ret = errno ? -errno : -ENODATA; if (fclose(sysfsfp))
perror("read_sysfs_string(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp))
ret = -errno;
error_free:
free(temp);
return ret;
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.16 Sekunden
(vorverarbeitet am 2026-10-12)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.