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/* Made up value to limit allocation sizes */ 11#include <string.h> 12#include <stdlib.h> 13 14#define IIO_MAX_NAME_LENGTH 30 15 16#define IIO_EVENT_CODE_RING_50_FULL 200 17#define IIO_EVENT_CODE_RING_75_FULL 201 18#define IIO_EVENT_CODE_RING_100_FULL 202 19 20const char *iio_dir = "/sys/bus/iio/devices/"; 21 22struct iio_event_data { 23 int id; 24 __s64 timestamp; 25}; 26 27/** 28 * find_type_by_name() - function to match top level types by name 29 * @name: top level type instance name 30 * @type: the type of top level instance being sort 31 * 32 * Typical types this is used for are device and trigger. 33 **/ 34inline int find_type_by_name(const char *name, const char *type) 35{ 36 const struct dirent *ent; 37 int number, numstrlen; 38 39 FILE *nameFile; 40 DIR *dp; 41 char thisname[IIO_MAX_NAME_LENGTH]; 42 char *filename; 43 struct stat Stat; 44 45 dp = opendir(iio_dir); 46 if (dp == NULL) { 47 printf("No industrialio devices available"); 48 return -ENODEV; 49 } 50 51 while (ent = readdir(dp), ent != NULL) { 52 if (strcmp(ent->d_name, ".") != 0 && 53 strcmp(ent->d_name, "..") != 0 && 54 strlen(ent->d_name) > strlen(type) && 55 strncmp(ent->d_name, type, strlen(type)) == 0) { 56 numstrlen = sscanf(ent->d_name + strlen(type), 57 "%d", 58 &number); 59 /* verify the next character is not a colon */ 60 if (strncmp(ent->d_name + strlen(type) + numstrlen, 61 ":", 62 1) != 0) { 63 filename = malloc(strlen(iio_dir) 64 + strlen(type) 65 + numstrlen 66 + 6); 67 if (filename == NULL) 68 return -ENOMEM; 69 sprintf(filename, "%s%s%d/name", 70 iio_dir, 71 type, 72 number); 73 nameFile = fopen(filename, "r"); 74 if (!nameFile) 75 continue; 76 free(filename); 77 fscanf(nameFile, "%s", thisname); 78 if (strcmp(name, thisname) == 0) 79 return number; 80 fclose(nameFile); 81 } 82 } 83 } 84 return -ENODEV; 85} 86 87inline int _write_sysfs_int(char *filename, char *basedir, int val, int verify) 88{ 89 int ret; 90 FILE *sysfsfp; 91 int test; 92 char *temp = malloc(strlen(basedir) + strlen(filename) + 2); 93 if (temp == NULL) 94 return -ENOMEM; 95 sprintf(temp, "%s/%s", basedir, filename); 96 sysfsfp = fopen(temp, "w"); 97 if (sysfsfp == NULL) { 98 printf("failed to open %s\n", temp); 99 ret = -errno; 100 goto error_free; 101 } 102 fprintf(sysfsfp, "%d", val); 103 fclose(sysfsfp); 104 if (verify) { 105 sysfsfp = fopen(temp, "r"); 106 if (sysfsfp == NULL) { 107 printf("failed to open %s\n", temp); 108 ret = -errno; 109 goto error_free; 110 } 111 fscanf(sysfsfp, "%d", &test); 112 if (test != val) { 113 printf("Possible failure in int write %d to %s%s\n", 114 val, 115 basedir, 116 filename); 117 ret = -1; 118 } 119 } 120error_free: 121 free(temp); 122 return ret; 123} 124 125int write_sysfs_int(char *filename, char *basedir, int val) 126{ 127 return _write_sysfs_int(filename, basedir, val, 0); 128} 129 130int write_sysfs_int_and_verify(char *filename, char *basedir, int val) 131{ 132 return _write_sysfs_int(filename, basedir, val, 1); 133} 134 135int _write_sysfs_string(char *filename, char *basedir, char *val, int verify) 136{ 137 int ret; 138 FILE *sysfsfp; 139 char *temp = malloc(strlen(basedir) + strlen(filename) + 2); 140 if (temp == NULL) { 141 printf("Memory allocation failed\n"); 142 return -ENOMEM; 143 } 144 sprintf(temp, "%s/%s", basedir, filename); 145 sysfsfp = fopen(temp, "w"); 146 if (sysfsfp == NULL) { 147 printf("Could not open %s\n", temp); 148 ret = -errno; 149 goto error_free; 150 } 151 fprintf(sysfsfp, "%s", val); 152 fclose(sysfsfp); 153 if (verify) { 154 sysfsfp = fopen(temp, "r"); 155 if (sysfsfp == NULL) { 156 ret = -errno; 157 goto error_free; 158 } 159 fscanf(sysfsfp, "%s", temp); 160 if (strcmp(temp, val) != 0) { 161 printf("Possible failure in string write of %s " 162 "Should be %s " 163 "writen to %s\%s\n", 164 temp, 165 val, 166 basedir, 167 filename); 168 ret = -1; 169 } 170 } 171error_free: 172 free(temp); 173 174 return ret; 175} 176/** 177 * write_sysfs_string_and_verify() - string write, readback and verify 178 * @filename: name of file to write to 179 * @basedir: the sysfs directory in which the file is to be found 180 * @val: the string to write 181 **/ 182int write_sysfs_string_and_verify(char *filename, char *basedir, char *val) 183{ 184 return _write_sysfs_string(filename, basedir, val, 1); 185} 186 187int write_sysfs_string(char *filename, char *basedir, char *val) 188{ 189 return _write_sysfs_string(filename, basedir, val, 0); 190} 191 192int read_sysfs_posint(char *filename, char *basedir) 193{ 194 int ret; 195 FILE *sysfsfp; 196 char *temp = malloc(strlen(basedir) + strlen(filename) + 2); 197 if (temp == NULL) { 198 printf("Memory allocation failed"); 199 return -ENOMEM; 200 } 201 sprintf(temp, "%s/%s", basedir, filename); 202 sysfsfp = fopen(temp, "r"); 203 if (sysfsfp == NULL) { 204 ret = -errno; 205 goto error_free; 206 } 207 fscanf(sysfsfp, "%d\n", &ret); 208 fclose(sysfsfp); 209error_free: 210 free(temp); 211 return ret; 212} 213 214int read_sysfs_float(char *filename, char *basedir, float *val) 215{ 216 float ret = 0; 217 FILE *sysfsfp; 218 char *temp = malloc(strlen(basedir) + strlen(filename) + 2); 219 if (temp == NULL) { 220 printf("Memory allocation failed"); 221 return -ENOMEM; 222 } 223 sprintf(temp, "%s/%s", basedir, filename); 224 sysfsfp = fopen(temp, "r"); 225 if (sysfsfp == NULL) { 226 ret = -errno; 227 goto error_free; 228 } 229 fscanf(sysfsfp, "%f\n", val); 230 fclose(sysfsfp); 231error_free: 232 free(temp); 233 return ret; 234} 235