Skip to content

Commit

Permalink
staging:iio:Documentation: Rewrite example for new abi.
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Cameron <jic23@cam.ac.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
  • Loading branch information
Jonathan Cameron authored and Greg Kroah-Hartman committed May 11, 2010
1 parent e9124af commit 9d8ae6c
Show file tree
Hide file tree
Showing 2 changed files with 313 additions and 198 deletions.
277 changes: 162 additions & 115 deletions drivers/staging/iio/Documentation/iio_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,140 +7,173 @@
* the Free Software Foundation.
*/

/* Made up value to limit allocation sizes */
#include <string.h>
#include <stdlib.h>

#define IIO_MAX_NAME_LENGTH 30

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

const char *iio_dir = "/sys/bus/iio/devices/";

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)
/**
* 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 sort
*
* Typical types this is used for are device and trigger.
**/
inline int find_type_by_name(const char *name, const char *type)
{
const char *iio_dir = "/sys/bus/iio/devices/";
const struct dirent *ent;
int cnt, pos, pos2;
int number, numstrlen;

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

char *returnstring = NULL;
char thisname[IIO_MAX_NAME_LENGTH];
char *filename;
struct stat Stat;
pos = sprintf(temp, "%s", iio_dir);

dp = opendir(iio_dir);
if (dp == NULL) {
printf("No industrialio devices available");
return NULL;
return -ENODEV;
}

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;
}
}
strcmp(ent->d_name, "..") != 0 &&
strlen(ent->d_name) > strlen(type) &&
strncmp(ent->d_name, type, strlen(type)) == 0) {
numstrlen = sscanf(ent->d_name + strlen(type),
"%d",
&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)
+ 1
+ numstrlen
+ 1);
if (filename == NULL)
return -ENOMEM;
sprintf(filename, "%s%s%d/name",
iio_dir,
type,
number);
nameFile = fopen(filename, "r");
if (!nameFile)
continue;
free(filename);
fscanf(nameFile, "%s", thisname);
if (strcmp(name, thisname) == 0) {
returnstring = malloc(strlen(temp) + 1);
sprintf(temp + pos2, "");
strcpy(returnstring, temp);
return returnstring;
}
if (strcmp(name, thisname) == 0)
return number;
fclose(nameFile);

}
}
}
return -ENODEV;
}

int write_sysfs_int(char *filename, char *basedir, int val)
inline int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
{
int ret;
FILE *sysfsfp;
char temp[100];
sprintf(temp, "%s%s", basedir, filename);
FILE *sysfsfp;
int test;
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (temp == NULL)
return -ENOMEM;
sprintf(temp, "%s/%s", basedir, filename);
sysfsfp = fopen(temp, "w");
if (sysfsfp == NULL)
return -1;
if (sysfsfp == NULL) {
printf("failed to open %s\n", temp);
ret = -errno;
goto error_free;
}
fprintf(sysfsfp, "%d", val);
fclose(sysfsfp);
return 0;
if (verify) {
sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) {
printf("failed to open %s\n", temp);
ret = -errno;
goto error_free;
}
fscanf(sysfsfp, "%d", &test);
if (test != val) {
printf("Possible failure in int write %d to %s%s\n",
val,
basedir,
filename);
ret = -1;
}
}
error_free:
free(temp);
return ret;
}

int write_sysfs_int(char *filename, char *basedir, int val)
{
return _write_sysfs_int(filename, basedir, val, 0);
}

int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
{
return _write_sysfs_int(filename, basedir, val, 1);
}

int _write_sysfs_string(char *filename, char *basedir, char *val, int verify)
{
int ret;
FILE *sysfsfp;
char temp[100];
int test;

sprintf(temp, "%s%s", basedir, filename);
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (temp == NULL) {
printf("Memory allocation failed\n");
return -ENOMEM;
}
sprintf(temp, "%s/%s", basedir, filename);
sysfsfp = fopen(temp, "w");
if (sysfsfp == NULL)
return -1;
fprintf(sysfsfp, "%d", val);
if (sysfsfp == NULL) {
printf("Could not open %s\n", temp);
ret = -errno;
goto error_free;
}
fprintf(sysfsfp, "%s", val);
fclose(sysfsfp);

sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL)
return -1;
fscanf(sysfsfp, "%d", &test);
if (test != val) {
printf("Possible failure in int write %d to %s%s\n",
val,
basedir,
filename);
return -1;
if (verify) {
sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) {
ret = -errno;
goto error_free;
}
fscanf(sysfsfp, "%s", temp);
if (strcmp(temp, val) != 0) {
printf("Possible failure in string write of %s "
"Should be %s "
"writen to %s\%s\n",
temp,
val,
basedir,
filename);
ret = -1;
}
}
error_free:
free(temp);

return 0;
return ret;
}

/**
* write_sysfs_string_and_verify() - string write, readback and verify
* @filename: name of file to write to
Expand All @@ -149,40 +182,54 @@ int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
**/
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);
return _write_sysfs_string(filename, basedir, val, 1);
}

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 write_sysfs_string(char *filename, char *basedir, char *val)
{
return _write_sysfs_string(filename, basedir, val, 0);
}

int read_sysfs_posint(char *filename, char *basedir)
{
int ret;
FILE *sysfsfp;
char temp[100];
sprintf(temp, "%s%s", basedir, filename);
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (temp == NULL) {
printf("Memory allocation failed");
return -ENOMEM;
}
sprintf(temp, "%s/%s", basedir, filename);
sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL)
return -1;
if (sysfsfp == NULL) {
ret = -errno;
goto error_free;
}
fscanf(sysfsfp, "%d\n", &ret);
fclose(sysfsfp);
error_free:
free(temp);
return ret;
}

int read_sysfs_float(char *filename, char *basedir, float *val)
{
float ret = 0;
FILE *sysfsfp;
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (temp == NULL) {
printf("Memory allocation failed");
return -ENOMEM;
}
sprintf(temp, "%s/%s", basedir, filename);
sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) {
ret = -errno;
goto error_free;
}
fscanf(sysfsfp, "%f\n", val);
fclose(sysfsfp);
error_free:
free(temp);
return ret;
}
Loading

0 comments on commit 9d8ae6c

Please sign in to comment.