Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame^] | 1 | /* IIO - useful set of util functionality |
| 2 | * |
| 3 | * Copyright (c) 2008 Jonathan Cameron |
| 4 | * |
| 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. |
| 8 | */ |
| 9 | |
| 10 | #define IIO_EVENT_CODE_RING_50_FULL 200 |
| 11 | #define IIO_EVENT_CODE_RING_75_FULL 201 |
| 12 | #define IIO_EVENT_CODE_RING_100_FULL 202 |
| 13 | |
| 14 | struct iio_event_data { |
| 15 | int id; |
| 16 | __s64 timestamp; |
| 17 | }; |
| 18 | |
| 19 | |
| 20 | inline char *find_ring_subelement(const char *directory, const char *subelement) |
| 21 | { |
| 22 | DIR *dp; |
| 23 | const struct dirent *ent; |
| 24 | int pos; |
| 25 | char temp[100]; |
| 26 | char *returnstring; |
| 27 | dp = opendir(directory); |
| 28 | if (dp == NULL) { |
| 29 | printf("could not directory: %s\n", directory); |
| 30 | return NULL; |
| 31 | } |
| 32 | while (ent = readdir(dp), ent != NULL) { |
| 33 | if (strcmp(ent->d_name, ".") != 0 && |
| 34 | strcmp(ent->d_name, "..") != 0) { |
| 35 | if (strncmp(ent->d_name, subelement, strlen(subelement)) == 0) { |
| 36 | int length = sprintf(temp, "%s%s%s", directory, ent->d_name, "/"); |
| 37 | returnstring = malloc(length+1); |
| 38 | strncpy(returnstring, temp, length+1); |
| 39 | return returnstring; |
| 40 | |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | char *find_type_by_name(const char *name, const char *type) |
| 49 | { |
| 50 | const char *iio_dir = "/sys/class/iio/"; |
| 51 | const struct dirent *ent; |
| 52 | int cnt, pos, pos2; |
| 53 | |
| 54 | FILE *nameFile; |
| 55 | DIR *dp; |
| 56 | char thisname[100]; |
| 57 | char temp[100]; |
| 58 | |
| 59 | char *returnstring = NULL; |
| 60 | struct stat Stat; |
| 61 | pos = sprintf(temp, "%s", iio_dir); |
| 62 | dp = opendir(iio_dir); |
| 63 | if (dp == NULL) { |
| 64 | printf("No industrialio devices available"); |
| 65 | return NULL; |
| 66 | } |
| 67 | while (ent = readdir(dp), ent != NULL) { |
| 68 | cnt++; |
| 69 | /*reject . and .. */ |
| 70 | if (strcmp(ent->d_name, ".") != 0 && |
| 71 | strcmp(ent->d_name, "..") != 0) { |
| 72 | /*make sure it isn't a trigger!*/ |
| 73 | if (strncmp(ent->d_name, type, strlen(type)) == 0) { |
| 74 | /* build full path to new file */ |
| 75 | pos2 = pos + sprintf(temp + pos, "%s/", ent->d_name); |
| 76 | sprintf(temp + pos2, "name"); |
| 77 | printf("search location %s\n", temp); |
| 78 | nameFile = fopen(temp, "r"); |
| 79 | if (!nameFile) { |
| 80 | sprintf(temp + pos2, "modalias", ent->d_name); |
| 81 | nameFile = fopen(temp, "r"); |
| 82 | if (!nameFile) { |
| 83 | printf("Failed to find a name for device\n"); |
| 84 | return NULL; |
| 85 | } |
| 86 | } |
| 87 | fscanf(nameFile, "%s", thisname); |
| 88 | if (strcmp(name, thisname) == 0) { |
| 89 | returnstring = malloc(strlen(temp) + 1); |
| 90 | sprintf(temp + pos2, ""); |
| 91 | strcpy(returnstring, temp); |
| 92 | return returnstring; |
| 93 | } |
| 94 | fclose(nameFile); |
| 95 | |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | int write_sysfs_int(char *filename, char *basedir, int val) |
| 102 | { |
| 103 | int ret; |
| 104 | FILE *sysfsfp; |
| 105 | char temp[100]; |
| 106 | sprintf(temp, "%s%s", basedir, filename); |
| 107 | sysfsfp = fopen(temp, "w"); |
| 108 | if (sysfsfp == NULL) |
| 109 | return -1; |
| 110 | fprintf(sysfsfp, "%d", val); |
| 111 | fclose(sysfsfp); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * write_sysfs_string_and_verify() - string write, readback and verify |
| 117 | * @filename: name of file to write to |
| 118 | * @basedir: the sysfs directory in which the file is to be found |
| 119 | * @val: the string to write |
| 120 | **/ |
| 121 | int write_sysfs_string_and_verify(char *filename, char *basedir, char *val) |
| 122 | { |
| 123 | int ret; |
| 124 | FILE *sysfsfp; |
| 125 | char temp[100]; |
| 126 | sprintf(temp, "%s%s", basedir, filename); |
| 127 | sysfsfp = fopen(temp, "w"); |
| 128 | if (sysfsfp == NULL) |
| 129 | return -1; |
| 130 | fprintf(sysfsfp, "%s", val); |
| 131 | fclose(sysfsfp); |
| 132 | |
| 133 | sysfsfp = fopen(temp, "r"); |
| 134 | if (sysfsfp == NULL) |
| 135 | return -1; |
| 136 | fscanf(sysfsfp, "%s", temp); |
| 137 | if (strcmp(temp, val) != 0) { |
| 138 | printf("Possible failure in string write %s to %s%s \n", |
| 139 | val, |
| 140 | basedir, |
| 141 | filename); |
| 142 | return -1; |
| 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | int read_sysfs_posint(char *filename, char *basedir) |
| 148 | { |
| 149 | int ret; |
| 150 | FILE *sysfsfp; |
| 151 | char temp[100]; |
| 152 | sprintf(temp, "%s%s", basedir, filename); |
| 153 | sysfsfp = fopen(temp, "r"); |
| 154 | if (sysfsfp == NULL) |
| 155 | return -1; |
| 156 | fscanf(sysfsfp, "%d\n", &ret); |
| 157 | fclose(sysfsfp); |
| 158 | return ret; |
| 159 | } |