summaryrefslogtreecommitdiffstats
path: root/tools/iio
diff options
context:
space:
mode:
authorHartmut Knaack <knaack.h@gmx.de>2015-06-10 15:51:20 -0400
committerJonathan Cameron <jic23@kernel.org>2015-06-13 13:49:43 -0400
commit7663a4aac625268674060d78ee00f97a36a03af7 (patch)
tree36e0b59f93717880690914e5265976f3a477878e /tools/iio
parent4f3ca89351e62f72144ca592c1c482e529d31539 (diff)
tools:iio: adjust coding style
Fix various coding style issues, including: * have spaces around operators * indentation * consolidate parameters in same line * required braces * adjust/drop comments * multiline comment style * delete unnecessary empty lines * add empty lines to visualize logial code blocks * typos Signed-off-by: Hartmut Knaack <knaack.h@gmx.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'tools/iio')
-rw-r--r--tools/iio/generic_buffer.c46
-rw-r--r--tools/iio/iio_event_monitor.c13
-rw-r--r--tools/iio/iio_utils.c111
-rw-r--r--tools/iio/iio_utils.h15
-rw-r--r--tools/iio/lsiio.c15
5 files changed, 118 insertions, 82 deletions
diff --git a/tools/iio/generic_buffer.c b/tools/iio/generic_buffer.c
index 4eebb6616e5c..fc362d2ff983 100644
--- a/tools/iio/generic_buffer.c
+++ b/tools/iio/generic_buffer.c
@@ -51,11 +51,13 @@ int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
51 if (bytes % channels[i].bytes == 0) 51 if (bytes % channels[i].bytes == 0)
52 channels[i].location = bytes; 52 channels[i].location = bytes;
53 else 53 else
54 channels[i].location = bytes - bytes%channels[i].bytes 54 channels[i].location = bytes - bytes % channels[i].bytes
55 + channels[i].bytes; 55 + channels[i].bytes;
56
56 bytes = channels[i].location + channels[i].bytes; 57 bytes = channels[i].location + channels[i].bytes;
57 i++; 58 i++;
58 } 59 }
60
59 return bytes; 61 return bytes;
60} 62}
61 63
@@ -136,9 +138,9 @@ void print8byte(uint64_t input, struct iio_channel_info *info)
136/** 138/**
137 * process_scan() - print out the values in SI units 139 * process_scan() - print out the values in SI units
138 * @data: pointer to the start of the scan 140 * @data: pointer to the start of the scan
139 * @channels: information about the channels. Note 141 * @channels: information about the channels.
140 * size_from_channelarray must have been called first to fill the 142 * Note: size_from_channelarray must have been called first
141 * location offsets. 143 * to fill the location offsets.
142 * @num_channels: number of channels 144 * @num_channels: number of channels
143 **/ 145 **/
144void process_scan(char *data, 146void process_scan(char *data,
@@ -213,6 +215,7 @@ int main(int argc, char **argv)
213 num_loops = strtoul(optarg, &dummy, 10); 215 num_loops = strtoul(optarg, &dummy, 10);
214 if (errno) 216 if (errno)
215 return -errno; 217 return -errno;
218
216 break; 219 break;
217 case 'e': 220 case 'e':
218 noevents = 1; 221 noevents = 1;
@@ -225,6 +228,7 @@ int main(int argc, char **argv)
225 buf_len = strtoul(optarg, &dummy, 10); 228 buf_len = strtoul(optarg, &dummy, 10);
226 if (errno) 229 if (errno)
227 return -errno; 230 return -errno;
231
228 break; 232 break;
229 case 'n': 233 case 'n':
230 device_name = optarg; 234 device_name = optarg;
@@ -257,6 +261,7 @@ int main(int argc, char **argv)
257 printf("Failed to find the %s\n", device_name); 261 printf("Failed to find the %s\n", device_name);
258 return dev_num; 262 return dev_num;
259 } 263 }
264
260 printf("iio device number being used is %d\n", dev_num); 265 printf("iio device number being used is %d\n", dev_num);
261 266
262 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); 267 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
@@ -285,9 +290,11 @@ int main(int argc, char **argv)
285 ret = trig_num; 290 ret = trig_num;
286 goto error_free_triggername; 291 goto error_free_triggername;
287 } 292 }
293
288 printf("iio trigger number being used is %d\n", trig_num); 294 printf("iio trigger number being used is %d\n", trig_num);
289 } else 295 } else {
290 printf("trigger-less mode selected\n"); 296 printf("trigger-less mode selected\n");
297 }
291 298
292 /* 299 /*
293 * Parse the files in scan_elements to identify what channels are 300 * Parse the files in scan_elements to identify what channels are
@@ -314,8 +321,10 @@ int main(int argc, char **argv)
314 321
315 if (!notrigger) { 322 if (!notrigger) {
316 printf("%s %s\n", dev_dir_name, trigger_name); 323 printf("%s %s\n", dev_dir_name, trigger_name);
317 /* Set the device trigger to be the data ready trigger found 324 /*
318 * above */ 325 * Set the device trigger to be the data ready trigger found
326 * above
327 */
319 ret = write_sysfs_string_and_verify("trigger/current_trigger", 328 ret = write_sysfs_string_and_verify("trigger/current_trigger",
320 dev_dir_name, 329 dev_dir_name,
321 trigger_name); 330 trigger_name);
@@ -334,8 +343,9 @@ int main(int argc, char **argv)
334 ret = write_sysfs_int("enable", buf_dir_name, 1); 343 ret = write_sysfs_int("enable", buf_dir_name, 1);
335 if (ret < 0) 344 if (ret < 0)
336 goto error_free_buf_dir_name; 345 goto error_free_buf_dir_name;
346
337 scan_size = size_from_channelarray(channels, num_channels); 347 scan_size = size_from_channelarray(channels, num_channels);
338 data = malloc(scan_size*buf_len); 348 data = malloc(scan_size * buf_len);
339 if (!data) { 349 if (!data) {
340 ret = -ENOMEM; 350 ret = -ENOMEM;
341 goto error_free_buf_dir_name; 351 goto error_free_buf_dir_name;
@@ -349,13 +359,12 @@ int main(int argc, char **argv)
349 359
350 /* Attempt to open non blocking the access dev */ 360 /* Attempt to open non blocking the access dev */
351 fp = open(buffer_access, O_RDONLY | O_NONBLOCK); 361 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
352 if (fp == -1) { /* If it isn't there make the node */ 362 if (fp == -1) { /* TODO: If it isn't there make the node */
353 ret = -errno; 363 ret = -errno;
354 printf("Failed to open %s\n", buffer_access); 364 printf("Failed to open %s\n", buffer_access);
355 goto error_free_buffer_access; 365 goto error_free_buffer_access;
356 } 366 }
357 367
358 /* Wait for events 10 times */
359 for (j = 0; j < num_loops; j++) { 368 for (j = 0; j < num_loops; j++) {
360 if (!noevents) { 369 if (!noevents) {
361 struct pollfd pfd = { 370 struct pollfd pfd = {
@@ -372,25 +381,22 @@ int main(int argc, char **argv)
372 } 381 }
373 382
374 toread = buf_len; 383 toread = buf_len;
375
376 } else { 384 } else {
377 usleep(timedelay); 385 usleep(timedelay);
378 toread = 64; 386 toread = 64;
379 } 387 }
380 388
381 read_size = read(fp, 389 read_size = read(fp, data, toread * scan_size);
382 data,
383 toread*scan_size);
384 if (read_size < 0) { 390 if (read_size < 0) {
385 if (errno == EAGAIN) { 391 if (errno == EAGAIN) {
386 printf("nothing available\n"); 392 printf("nothing available\n");
387 continue; 393 continue;
388 } else 394 } else {
389 break; 395 break;
396 }
390 } 397 }
391 for (i = 0; i < read_size/scan_size; i++) 398 for (i = 0; i < read_size / scan_size; i++)
392 process_scan(data + scan_size*i, 399 process_scan(data + scan_size * i, channels,
393 channels,
394 num_channels); 400 num_channels);
395 } 401 }
396 402
@@ -409,6 +415,7 @@ int main(int argc, char **argv)
409error_close_buffer_access: 415error_close_buffer_access:
410 if (close(fp) == -1) 416 if (close(fp) == -1)
411 perror("Failed to close buffer"); 417 perror("Failed to close buffer");
418
412error_free_buffer_access: 419error_free_buffer_access:
413 free(buffer_access); 420 free(buffer_access);
414error_free_data: 421error_free_data:
@@ -424,6 +431,7 @@ error_free_channels:
424error_free_triggername: 431error_free_triggername:
425 if (datardytrigger) 432 if (datardytrigger)
426 free(trigger_name); 433 free(trigger_name);
434
427error_free_dev_dir_name: 435error_free_dev_dir_name:
428 free(dev_dir_name); 436 free(dev_dir_name);
429 437
diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c
index 016760e769c0..a4ab6bb8f872 100644
--- a/tools/iio/iio_event_monitor.c
+++ b/tools/iio/iio_event_monitor.c
@@ -13,7 +13,6 @@
13 * 13 *
14 * Usage: 14 * Usage:
15 * iio_event_monitor <device_name> 15 * iio_event_monitor <device_name>
16 *
17 */ 16 */
18 17
19#include <unistd.h> 18#include <unistd.h>
@@ -209,7 +208,8 @@ static void print_event(struct iio_event_data *event)
209 208
210 if (!event_is_known(event)) { 209 if (!event_is_known(event)) {
211 printf("Unknown event: time: %lld, id: %llx\n", 210 printf("Unknown event: time: %lld, id: %llx\n",
212 event->timestamp, event->id); 211 event->timestamp, event->id);
212
213 return; 213 return;
214 } 214 }
215 215
@@ -229,6 +229,7 @@ static void print_event(struct iio_event_data *event)
229 229
230 if (dir != IIO_EV_DIR_NONE) 230 if (dir != IIO_EV_DIR_NONE)
231 printf(", direction: %s", iio_ev_dir_text[dir]); 231 printf(", direction: %s", iio_ev_dir_text[dir]);
232
232 printf("\n"); 233 printf("\n");
233} 234}
234 235
@@ -251,14 +252,16 @@ int main(int argc, char **argv)
251 dev_num = find_type_by_name(device_name, "iio:device"); 252 dev_num = find_type_by_name(device_name, "iio:device");
252 if (dev_num >= 0) { 253 if (dev_num >= 0) {
253 printf("Found IIO device with name %s with device number %d\n", 254 printf("Found IIO device with name %s with device number %d\n",
254 device_name, dev_num); 255 device_name, dev_num);
255 ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num); 256 ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
256 if (ret < 0) { 257 if (ret < 0) {
257 return -ENOMEM; 258 return -ENOMEM;
258 } 259 }
259 } else { 260 } else {
260 /* If we can't find a IIO device by name assume device_name is a 261 /*
261 IIO chrdev */ 262 * If we can't find an IIO device by name assume device_name is
263 * an IIO chrdev
264 */
262 chrdev_name = strdup(device_name); 265 chrdev_name = strdup(device_name);
263 if (!chrdev_name) 266 if (!chrdev_name)
264 return -ENOMEM; 267 return -ENOMEM;
diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c
index ec9ab7f9ae4c..4eac11ad38c4 100644
--- a/tools/iio/iio_utils.c
+++ b/tools/iio/iio_utils.c
@@ -32,8 +32,7 @@ static char * const iio_direction[] = {
32 * 32 *
33 * Returns 0 on success, or a negative error code if string extraction failed. 33 * Returns 0 on success, or a negative error code if string extraction failed.
34 **/ 34 **/
35int iioutils_break_up_name(const char *full_name, 35int iioutils_break_up_name(const char *full_name, char **generic_name)
36 char **generic_name)
37{ 36{
38 char *current; 37 char *current;
39 char *w, *r; 38 char *w, *r;
@@ -65,6 +64,7 @@ int iioutils_break_up_name(const char *full_name,
65 *w = *r; 64 *w = *r;
66 w++; 65 w++;
67 } 66 }
67
68 r++; 68 r++;
69 } 69 }
70 *w = '\0'; 70 *w = '\0';
@@ -88,15 +88,10 @@ int iioutils_break_up_name(const char *full_name,
88 * 88 *
89 * Returns a value >= 0 on success, otherwise a negative error code. 89 * Returns a value >= 0 on success, otherwise a negative error code.
90 **/ 90 **/
91int iioutils_get_type(unsigned *is_signed, 91int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
92 unsigned *bytes, 92 unsigned *shift, uint64_t *mask, unsigned *be,
93 unsigned *bits_used, 93 const char *device_dir, const char *name,
94 unsigned *shift, 94 const char *generic_name)
95 uint64_t *mask,
96 unsigned *be,
97 const char *device_dir,
98 const char *name,
99 const char *generic_name)
100{ 95{
101 FILE *sysfsfp; 96 FILE *sysfsfp;
102 int ret; 97 int ret;
@@ -126,6 +121,7 @@ int iioutils_get_type(unsigned *is_signed,
126 ret = -errno; 121 ret = -errno;
127 goto error_free_builtname_generic; 122 goto error_free_builtname_generic;
128 } 123 }
124
129 ret = -ENOENT; 125 ret = -ENOENT;
130 while (ent = readdir(dp), ent != NULL) 126 while (ent = readdir(dp), ent != NULL)
131 /* 127 /*
@@ -140,6 +136,7 @@ int iioutils_get_type(unsigned *is_signed,
140 ret = -ENOMEM; 136 ret = -ENOMEM;
141 goto error_closedir; 137 goto error_closedir;
142 } 138 }
139
143 sysfsfp = fopen(filename, "r"); 140 sysfsfp = fopen(filename, "r");
144 if (sysfsfp == NULL) { 141 if (sysfsfp == NULL) {
145 ret = -errno; 142 ret = -errno;
@@ -162,12 +159,14 @@ int iioutils_get_type(unsigned *is_signed,
162 printf("scan type description didn't match\n"); 159 printf("scan type description didn't match\n");
163 goto error_close_sysfsfp; 160 goto error_close_sysfsfp;
164 } 161 }
162
165 *be = (endianchar == 'b'); 163 *be = (endianchar == 'b');
166 *bytes = padint / 8; 164 *bytes = padint / 8;
167 if (*bits_used == 64) 165 if (*bits_used == 64)
168 *mask = ~0; 166 *mask = ~0;
169 else 167 else
170 *mask = (1 << *bits_used) - 1; 168 *mask = (1 << *bits_used) - 1;
169
171 *is_signed = (signchar == 's'); 170 *is_signed = (signchar == 's');
172 if (fclose(sysfsfp)) { 171 if (fclose(sysfsfp)) {
173 ret = -errno; 172 ret = -errno;
@@ -177,9 +176,9 @@ int iioutils_get_type(unsigned *is_signed,
177 176
178 sysfsfp = 0; 177 sysfsfp = 0;
179 free(filename); 178 free(filename);
180
181 filename = 0; 179 filename = 0;
182 } 180 }
181
183error_close_sysfsfp: 182error_close_sysfsfp:
184 if (sysfsfp) 183 if (sysfsfp)
185 if (fclose(sysfsfp)) 184 if (fclose(sysfsfp))
@@ -188,6 +187,7 @@ error_close_sysfsfp:
188error_free_filename: 187error_free_filename:
189 if (filename) 188 if (filename)
190 free(filename); 189 free(filename);
190
191error_closedir: 191error_closedir:
192 if (closedir(dp) == -1) 192 if (closedir(dp) == -1)
193 perror("iioutils_get_type(): Failed to close directory"); 193 perror("iioutils_get_type(): Failed to close directory");
@@ -212,11 +212,9 @@ error_free_scan_el_dir:
212 * 212 *
213 * Returns a value >= 0 on success, otherwise a negative error code. 213 * Returns a value >= 0 on success, otherwise a negative error code.
214 **/ 214 **/
215int iioutils_get_param_float(float *output, 215int iioutils_get_param_float(float *output, const char *param_name,
216 const char *param_name, 216 const char *device_dir, const char *name,
217 const char *device_dir, 217 const char *generic_name)
218 const char *name,
219 const char *generic_name)
220{ 218{
221 FILE *sysfsfp; 219 FILE *sysfsfp;
222 int ret; 220 int ret;
@@ -235,11 +233,13 @@ int iioutils_get_param_float(float *output,
235 ret = -ENOMEM; 233 ret = -ENOMEM;
236 goto error_free_builtname; 234 goto error_free_builtname;
237 } 235 }
236
238 dp = opendir(device_dir); 237 dp = opendir(device_dir);
239 if (dp == NULL) { 238 if (dp == NULL) {
240 ret = -errno; 239 ret = -errno;
241 goto error_free_builtname_generic; 240 goto error_free_builtname_generic;
242 } 241 }
242
243 ret = -ENOENT; 243 ret = -ENOENT;
244 while (ent = readdir(dp), ent != NULL) 244 while (ent = readdir(dp), ent != NULL)
245 if ((strcmp(builtname, ent->d_name) == 0) || 245 if ((strcmp(builtname, ent->d_name) == 0) ||
@@ -250,11 +250,13 @@ int iioutils_get_param_float(float *output,
250 ret = -ENOMEM; 250 ret = -ENOMEM;
251 goto error_closedir; 251 goto error_closedir;
252 } 252 }
253
253 sysfsfp = fopen(filename, "r"); 254 sysfsfp = fopen(filename, "r");
254 if (!sysfsfp) { 255 if (!sysfsfp) {
255 ret = -errno; 256 ret = -errno;
256 goto error_free_filename; 257 goto error_free_filename;
257 } 258 }
259
258 errno = 0; 260 errno = 0;
259 if (fscanf(sysfsfp, "%f", output) != 1) 261 if (fscanf(sysfsfp, "%f", output) != 1)
260 ret = errno ? -errno : -ENODATA; 262 ret = errno ? -errno : -ENODATA;
@@ -264,6 +266,7 @@ int iioutils_get_param_float(float *output,
264error_free_filename: 266error_free_filename:
265 if (filename) 267 if (filename)
266 free(filename); 268 free(filename);
269
267error_closedir: 270error_closedir:
268 if (closedir(dp) == -1) 271 if (closedir(dp) == -1)
269 perror("iioutils_get_param_float(): Failed to close directory"); 272 perror("iioutils_get_param_float(): Failed to close directory");
@@ -282,16 +285,14 @@ error_free_builtname:
282 * @cnt: the amount of array elements 285 * @cnt: the amount of array elements
283 **/ 286 **/
284 287
285void bsort_channel_array_by_index(struct iio_channel_info **ci_array, 288void bsort_channel_array_by_index(struct iio_channel_info **ci_array, int cnt)
286 int cnt)
287{ 289{
288
289 struct iio_channel_info temp; 290 struct iio_channel_info temp;
290 int x, y; 291 int x, y;
291 292
292 for (x = 0; x < cnt; x++) 293 for (x = 0; x < cnt; x++)
293 for (y = 0; y < (cnt - 1); y++) 294 for (y = 0; y < (cnt - 1); y++)
294 if ((*ci_array)[y].index > (*ci_array)[y+1].index) { 295 if ((*ci_array)[y].index > (*ci_array)[y + 1].index) {
295 temp = (*ci_array)[y + 1]; 296 temp = (*ci_array)[y + 1];
296 (*ci_array)[y + 1] = (*ci_array)[y]; 297 (*ci_array)[y + 1] = (*ci_array)[y];
297 (*ci_array)[y] = temp; 298 (*ci_array)[y] = temp;
@@ -307,8 +308,7 @@ void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
307 * Returns 0 on success, otherwise a negative error code. 308 * Returns 0 on success, otherwise a negative error code.
308 **/ 309 **/
309int build_channel_array(const char *device_dir, 310int build_channel_array(const char *device_dir,
310 struct iio_channel_info **ci_array, 311 struct iio_channel_info **ci_array, int *counter)
311 int *counter)
312{ 312{
313 DIR *dp; 313 DIR *dp;
314 FILE *sysfsfp; 314 FILE *sysfsfp;
@@ -329,6 +329,7 @@ int build_channel_array(const char *device_dir,
329 ret = -errno; 329 ret = -errno;
330 goto error_free_name; 330 goto error_free_name;
331 } 331 }
332
332 while (ent = readdir(dp), ent != NULL) 333 while (ent = readdir(dp), ent != NULL)
333 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), 334 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
334 "_en") == 0) { 335 "_en") == 0) {
@@ -338,12 +339,14 @@ int build_channel_array(const char *device_dir,
338 ret = -ENOMEM; 339 ret = -ENOMEM;
339 goto error_close_dir; 340 goto error_close_dir;
340 } 341 }
342
341 sysfsfp = fopen(filename, "r"); 343 sysfsfp = fopen(filename, "r");
342 if (sysfsfp == NULL) { 344 if (sysfsfp == NULL) {
343 ret = -errno; 345 ret = -errno;
344 free(filename); 346 free(filename);
345 goto error_close_dir; 347 goto error_close_dir;
346 } 348 }
349
347 errno = 0; 350 errno = 0;
348 if (fscanf(sysfsfp, "%i", &ret) != 1) { 351 if (fscanf(sysfsfp, "%i", &ret) != 1) {
349 ret = errno ? -errno : -ENODATA; 352 ret = errno ? -errno : -ENODATA;
@@ -353,9 +356,9 @@ int build_channel_array(const char *device_dir,
353 free(filename); 356 free(filename);
354 goto error_close_dir; 357 goto error_close_dir;
355 } 358 }
356
357 if (ret == 1) 359 if (ret == 1)
358 (*counter)++; 360 (*counter)++;
361
359 if (fclose(sysfsfp)) { 362 if (fclose(sysfsfp)) {
360 ret = -errno; 363 ret = -errno;
361 free(filename); 364 free(filename);
@@ -364,11 +367,13 @@ int build_channel_array(const char *device_dir,
364 367
365 free(filename); 368 free(filename);
366 } 369 }
370
367 *ci_array = malloc(sizeof(**ci_array) * (*counter)); 371 *ci_array = malloc(sizeof(**ci_array) * (*counter));
368 if (*ci_array == NULL) { 372 if (*ci_array == NULL) {
369 ret = -ENOMEM; 373 ret = -ENOMEM;
370 goto error_close_dir; 374 goto error_close_dir;
371 } 375 }
376
372 seekdir(dp, 0); 377 seekdir(dp, 0);
373 while (ent = readdir(dp), ent != NULL) { 378 while (ent = readdir(dp), ent != NULL) {
374 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), 379 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
@@ -384,6 +389,7 @@ int build_channel_array(const char *device_dir,
384 count--; 389 count--;
385 goto error_cleanup_array; 390 goto error_cleanup_array;
386 } 391 }
392
387 sysfsfp = fopen(filename, "r"); 393 sysfsfp = fopen(filename, "r");
388 if (sysfsfp == NULL) { 394 if (sysfsfp == NULL) {
389 ret = -errno; 395 ret = -errno;
@@ -391,6 +397,7 @@ int build_channel_array(const char *device_dir,
391 count--; 397 count--;
392 goto error_cleanup_array; 398 goto error_cleanup_array;
393 } 399 }
400
394 errno = 0; 401 errno = 0;
395 if (fscanf(sysfsfp, "%i", &current_enabled) != 1) { 402 if (fscanf(sysfsfp, "%i", &current_enabled) != 1) {
396 ret = errno ? -errno : -ENODATA; 403 ret = errno ? -errno : -ENODATA;
@@ -423,6 +430,7 @@ int build_channel_array(const char *device_dir,
423 count--; 430 count--;
424 goto error_cleanup_array; 431 goto error_cleanup_array;
425 } 432 }
433
426 /* Get the generic and specific name elements */ 434 /* Get the generic and specific name elements */
427 ret = iioutils_break_up_name(current->name, 435 ret = iioutils_break_up_name(current->name,
428 &current->generic_name); 436 &current->generic_name);
@@ -432,6 +440,7 @@ int build_channel_array(const char *device_dir,
432 count--; 440 count--;
433 goto error_cleanup_array; 441 goto error_cleanup_array;
434 } 442 }
443
435 ret = asprintf(&filename, 444 ret = asprintf(&filename,
436 "%s/%s_index", 445 "%s/%s_index",
437 scan_el_dir, 446 scan_el_dir,
@@ -441,6 +450,7 @@ int build_channel_array(const char *device_dir,
441 ret = -ENOMEM; 450 ret = -ENOMEM;
442 goto error_cleanup_array; 451 goto error_cleanup_array;
443 } 452 }
453
444 sysfsfp = fopen(filename, "r"); 454 sysfsfp = fopen(filename, "r");
445 if (sysfsfp == NULL) { 455 if (sysfsfp == NULL) {
446 ret = -errno; 456 ret = -errno;
@@ -474,6 +484,7 @@ int build_channel_array(const char *device_dir,
474 current->generic_name); 484 current->generic_name);
475 if (ret < 0) 485 if (ret < 0)
476 goto error_cleanup_array; 486 goto error_cleanup_array;
487
477 ret = iioutils_get_param_float(&current->offset, 488 ret = iioutils_get_param_float(&current->offset,
478 "offset", 489 "offset",
479 device_dir, 490 device_dir,
@@ -481,6 +492,7 @@ int build_channel_array(const char *device_dir,
481 current->generic_name); 492 current->generic_name);
482 if (ret < 0) 493 if (ret < 0)
483 goto error_cleanup_array; 494 goto error_cleanup_array;
495
484 ret = iioutils_get_type(&current->is_signed, 496 ret = iioutils_get_type(&current->is_signed,
485 &current->bytes, 497 &current->bytes,
486 &current->bits_used, 498 &current->bits_used,
@@ -562,9 +574,9 @@ int find_type_by_name(const char *name, const char *type)
562 574
563 while (ent = readdir(dp), ent != NULL) { 575 while (ent = readdir(dp), ent != NULL) {
564 if (strcmp(ent->d_name, ".") != 0 && 576 if (strcmp(ent->d_name, ".") != 0 &&
565 strcmp(ent->d_name, "..") != 0 && 577 strcmp(ent->d_name, "..") != 0 &&
566 strlen(ent->d_name) > strlen(type) && 578 strlen(ent->d_name) > strlen(type) &&
567 strncmp(ent->d_name, type, strlen(type)) == 0) { 579 strncmp(ent->d_name, type, strlen(type)) == 0) {
568 errno = 0; 580 errno = 0;
569 ret = sscanf(ent->d_name + strlen(type), "%d", &number); 581 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
570 if (ret < 0) { 582 if (ret < 0) {
@@ -580,12 +592,9 @@ int find_type_by_name(const char *name, const char *type)
580 numstrlen = calc_digits(number); 592 numstrlen = calc_digits(number);
581 /* verify the next character is not a colon */ 593 /* verify the next character is not a colon */
582 if (strncmp(ent->d_name + strlen(type) + numstrlen, 594 if (strncmp(ent->d_name + strlen(type) + numstrlen,
583 ":", 595 ":", 1) != 0) {
584 1) != 0) { 596 filename = malloc(strlen(iio_dir) + strlen(type)
585 filename = malloc(strlen(iio_dir) 597 + numstrlen + 6);
586 + strlen(type)
587 + numstrlen
588 + 6);
589 if (filename == NULL) { 598 if (filename == NULL) {
590 ret = -ENOMEM; 599 ret = -ENOMEM;
591 goto error_close_dir; 600 goto error_close_dir;
@@ -603,6 +612,7 @@ int find_type_by_name(const char *name, const char *type)
603 free(filename); 612 free(filename);
604 continue; 613 continue;
605 } 614 }
615
606 free(filename); 616 free(filename);
607 errno = 0; 617 errno = 0;
608 if (fscanf(nameFile, "%s", thisname) != 1) { 618 if (fscanf(nameFile, "%s", thisname) != 1) {
@@ -618,6 +628,7 @@ int find_type_by_name(const char *name, const char *type)
618 if (strcmp(name, thisname) == 0) { 628 if (strcmp(name, thisname) == 0) {
619 if (closedir(dp) == -1) 629 if (closedir(dp) == -1)
620 return -errno; 630 return -errno;
631
621 return number; 632 return number;
622 } 633 }
623 } 634 }
@@ -631,6 +642,7 @@ int find_type_by_name(const char *name, const char *type)
631error_close_dir: 642error_close_dir:
632 if (closedir(dp) == -1) 643 if (closedir(dp) == -1)
633 perror("find_type_by_name(): Failed to close directory"); 644 perror("find_type_by_name(): Failed to close directory");
645
634 return ret; 646 return ret;
635} 647}
636 648
@@ -644,6 +656,7 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val,
644 656
645 if (temp == NULL) 657 if (temp == NULL)
646 return -ENOMEM; 658 return -ENOMEM;
659
647 ret = sprintf(temp, "%s/%s", basedir, filename); 660 ret = sprintf(temp, "%s/%s", basedir, filename);
648 if (ret < 0) 661 if (ret < 0)
649 goto error_free; 662 goto error_free;
@@ -654,6 +667,7 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val,
654 printf("failed to open %s\n", temp); 667 printf("failed to open %s\n", temp);
655 goto error_free; 668 goto error_free;
656 } 669 }
670
657 ret = fprintf(sysfsfp, "%d", val); 671 ret = fprintf(sysfsfp, "%d", val);
658 if (ret < 0) { 672 if (ret < 0) {
659 if (fclose(sysfsfp)) 673 if (fclose(sysfsfp))
@@ -674,6 +688,7 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val,
674 printf("failed to open %s\n", temp); 688 printf("failed to open %s\n", temp);
675 goto error_free; 689 goto error_free;
676 } 690 }
691
677 if (fscanf(sysfsfp, "%d", &test) != 1) { 692 if (fscanf(sysfsfp, "%d", &test) != 1) {
678 ret = errno ? -errno : -ENODATA; 693 ret = errno ? -errno : -ENODATA;
679 if (fclose(sysfsfp)) 694 if (fclose(sysfsfp))
@@ -688,13 +703,12 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val,
688 } 703 }
689 704
690 if (test != val) { 705 if (test != val) {
691 printf("Possible failure in int write %d to %s%s\n", 706 printf("Possible failure in int write %d to %s/%s\n",
692 val, 707 val, basedir, filename);
693 basedir,
694 filename);
695 ret = -1; 708 ret = -1;
696 } 709 }
697 } 710 }
711
698error_free: 712error_free:
699 free(temp); 713 free(temp);
700 return ret; 714 return ret;
@@ -739,6 +753,7 @@ static int _write_sysfs_string(const char *filename, const char *basedir,
739 printf("Memory allocation failed\n"); 753 printf("Memory allocation failed\n");
740 return -ENOMEM; 754 return -ENOMEM;
741 } 755 }
756
742 ret = sprintf(temp, "%s/%s", basedir, filename); 757 ret = sprintf(temp, "%s/%s", basedir, filename);
743 if (ret < 0) 758 if (ret < 0)
744 goto error_free; 759 goto error_free;
@@ -749,6 +764,7 @@ static int _write_sysfs_string(const char *filename, const char *basedir,
749 printf("Could not open %s\n", temp); 764 printf("Could not open %s\n", temp);
750 goto error_free; 765 goto error_free;
751 } 766 }
767
752 ret = fprintf(sysfsfp, "%s", val); 768 ret = fprintf(sysfsfp, "%s", val);
753 if (ret < 0) { 769 if (ret < 0) {
754 if (fclose(sysfsfp)) 770 if (fclose(sysfsfp))
@@ -766,9 +782,10 @@ static int _write_sysfs_string(const char *filename, const char *basedir,
766 sysfsfp = fopen(temp, "r"); 782 sysfsfp = fopen(temp, "r");
767 if (sysfsfp == NULL) { 783 if (sysfsfp == NULL) {
768 ret = -errno; 784 ret = -errno;
769 printf("could not open file to verify\n"); 785 printf("Could not open file to verify\n");
770 goto error_free; 786 goto error_free;
771 } 787 }
788
772 if (fscanf(sysfsfp, "%s", temp) != 1) { 789 if (fscanf(sysfsfp, "%s", temp) != 1) {
773 ret = errno ? -errno : -ENODATA; 790 ret = errno ? -errno : -ENODATA;
774 if (fclose(sysfsfp)) 791 if (fclose(sysfsfp))
@@ -784,15 +801,12 @@ static int _write_sysfs_string(const char *filename, const char *basedir,
784 801
785 if (strcmp(temp, val) != 0) { 802 if (strcmp(temp, val) != 0) {
786 printf("Possible failure in string write of %s " 803 printf("Possible failure in string write of %s "
787 "Should be %s " 804 "Should be %s written to %s/%s\n", temp, val,
788 "written to %s\%s\n", 805 basedir, filename);
789 temp,
790 val,
791 basedir,
792 filename);
793 ret = -1; 806 ret = -1;
794 } 807 }
795 } 808 }
809
796error_free: 810error_free:
797 free(temp); 811 free(temp);
798 812
@@ -845,6 +859,7 @@ int read_sysfs_posint(const char *filename, const char *basedir)
845 printf("Memory allocation failed"); 859 printf("Memory allocation failed");
846 return -ENOMEM; 860 return -ENOMEM;
847 } 861 }
862
848 ret = sprintf(temp, "%s/%s", basedir, filename); 863 ret = sprintf(temp, "%s/%s", basedir, filename);
849 if (ret < 0) 864 if (ret < 0)
850 goto error_free; 865 goto error_free;
@@ -854,6 +869,7 @@ int read_sysfs_posint(const char *filename, const char *basedir)
854 ret = -errno; 869 ret = -errno;
855 goto error_free; 870 goto error_free;
856 } 871 }
872
857 errno = 0; 873 errno = 0;
858 if (fscanf(sysfsfp, "%d\n", &ret) != 1) { 874 if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
859 ret = errno ? -errno : -ENODATA; 875 ret = errno ? -errno : -ENODATA;
@@ -868,6 +884,7 @@ int read_sysfs_posint(const char *filename, const char *basedir)
868 884
869error_free: 885error_free:
870 free(temp); 886 free(temp);
887
871 return ret; 888 return ret;
872} 889}
873 890
@@ -889,6 +906,7 @@ int read_sysfs_float(const char *filename, const char *basedir, float *val)
889 printf("Memory allocation failed"); 906 printf("Memory allocation failed");
890 return -ENOMEM; 907 return -ENOMEM;
891 } 908 }
909
892 ret = sprintf(temp, "%s/%s", basedir, filename); 910 ret = sprintf(temp, "%s/%s", basedir, filename);
893 if (ret < 0) 911 if (ret < 0)
894 goto error_free; 912 goto error_free;
@@ -898,6 +916,7 @@ int read_sysfs_float(const char *filename, const char *basedir, float *val)
898 ret = -errno; 916 ret = -errno;
899 goto error_free; 917 goto error_free;
900 } 918 }
919
901 errno = 0; 920 errno = 0;
902 if (fscanf(sysfsfp, "%f\n", val) != 1) { 921 if (fscanf(sysfsfp, "%f\n", val) != 1) {
903 ret = errno ? -errno : -ENODATA; 922 ret = errno ? -errno : -ENODATA;
@@ -912,6 +931,7 @@ int read_sysfs_float(const char *filename, const char *basedir, float *val)
912 931
913error_free: 932error_free:
914 free(temp); 933 free(temp);
934
915 return ret; 935 return ret;
916} 936}
917 937
@@ -933,6 +953,7 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str)
933 printf("Memory allocation failed"); 953 printf("Memory allocation failed");
934 return -ENOMEM; 954 return -ENOMEM;
935 } 955 }
956
936 ret = sprintf(temp, "%s/%s", basedir, filename); 957 ret = sprintf(temp, "%s/%s", basedir, filename);
937 if (ret < 0) 958 if (ret < 0)
938 goto error_free; 959 goto error_free;
@@ -942,6 +963,7 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str)
942 ret = -errno; 963 ret = -errno;
943 goto error_free; 964 goto error_free;
944 } 965 }
966
945 errno = 0; 967 errno = 0;
946 if (fscanf(sysfsfp, "%s\n", str) != 1) { 968 if (fscanf(sysfsfp, "%s\n", str) != 1) {
947 ret = errno ? -errno : -ENODATA; 969 ret = errno ? -errno : -ENODATA;
@@ -956,6 +978,7 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str)
956 978
957error_free: 979error_free:
958 free(temp); 980 free(temp);
981
959 return ret; 982 return ret;
960} 983}
961 984
diff --git a/tools/iio/iio_utils.h b/tools/iio/iio_utils.h
index 379eed9deaea..086610139ade 100644
--- a/tools/iio/iio_utils.h
+++ b/tools/iio/iio_utils.h
@@ -51,17 +51,16 @@ struct iio_channel_info {
51}; 51};
52 52
53int iioutils_break_up_name(const char *full_name, char **generic_name); 53int iioutils_break_up_name(const char *full_name, char **generic_name);
54int iioutils_get_type(unsigned *is_signed, unsigned *bytes, 54int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
55 unsigned *bits_used, unsigned *shift, 55 unsigned *shift, uint64_t *mask, unsigned *be,
56 uint64_t *mask, unsigned *be, 56 const char *device_dir, const char *name,
57 const char *device_dir, const char *name, 57 const char *generic_name);
58 const char *generic_name);
59int iioutils_get_param_float(float *output, const char *param_name, 58int iioutils_get_param_float(float *output, const char *param_name,
60 const char *device_dir, const char *name, 59 const char *device_dir, const char *name,
61 const char *generic_name); 60 const char *generic_name);
62void bsort_channel_array_by_index(struct iio_channel_info **ci_array, int cnt); 61void bsort_channel_array_by_index(struct iio_channel_info **ci_array, int cnt);
63int build_channel_array(const char *device_dir, 62int build_channel_array(const char *device_dir,
64 struct iio_channel_info **ci_array, int *counter); 63 struct iio_channel_info **ci_array, int *counter);
65int find_type_by_name(const char *name, const char *type); 64int find_type_by_name(const char *name, const char *type);
66int write_sysfs_int(const char *filename, const char *basedir, int val); 65int write_sysfs_int(const char *filename, const char *basedir, int val);
67int write_sysfs_int_and_verify(const char *filename, const char *basedir, 66int write_sysfs_int_and_verify(const char *filename, const char *basedir,
diff --git a/tools/iio/lsiio.c b/tools/iio/lsiio.c
index b59ee1733924..f5b3bd0267d5 100644
--- a/tools/iio/lsiio.c
+++ b/tools/iio/lsiio.c
@@ -20,7 +20,6 @@
20#include <sys/dir.h> 20#include <sys/dir.h>
21#include "iio_utils.h" 21#include "iio_utils.h"
22 22
23
24static enum verbosity { 23static enum verbosity {
25 VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */ 24 VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */
26 VERBLEVEL_SENSORS, /* 1 lists sensors */ 25 VERBLEVEL_SENSORS, /* 1 lists sensors */
@@ -29,17 +28,16 @@ static enum verbosity {
29const char *type_device = "iio:device"; 28const char *type_device = "iio:device";
30const char *type_trigger = "trigger"; 29const char *type_trigger = "trigger";
31 30
32
33static inline int check_prefix(const char *str, const char *prefix) 31static inline int check_prefix(const char *str, const char *prefix)
34{ 32{
35 return strlen(str) > strlen(prefix) && 33 return strlen(str) > strlen(prefix) &&
36 strncmp(str, prefix, strlen(prefix)) == 0; 34 strncmp(str, prefix, strlen(prefix)) == 0;
37} 35}
38 36
39static inline int check_postfix(const char *str, const char *postfix) 37static inline int check_postfix(const char *str, const char *postfix)
40{ 38{
41 return strlen(str) > strlen(postfix) && 39 return strlen(str) > strlen(postfix) &&
42 strcmp(str + strlen(str) - strlen(postfix), postfix) == 0; 40 strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
43} 41}
44 42
45static int dump_channels(const char *dev_dir_name) 43static int dump_channels(const char *dev_dir_name)
@@ -50,11 +48,11 @@ static int dump_channels(const char *dev_dir_name)
50 dp = opendir(dev_dir_name); 48 dp = opendir(dev_dir_name);
51 if (dp == NULL) 49 if (dp == NULL)
52 return -errno; 50 return -errno;
51
53 while (ent = readdir(dp), ent != NULL) 52 while (ent = readdir(dp), ent != NULL)
54 if (check_prefix(ent->d_name, "in_") && 53 if (check_prefix(ent->d_name, "in_") &&
55 check_postfix(ent->d_name, "_raw")) { 54 check_postfix(ent->d_name, "_raw"))
56 printf(" %-10s\n", ent->d_name); 55 printf(" %-10s\n", ent->d_name);
57 }
58 56
59 return (closedir(dp) == -1) ? -errno : 0; 57 return (closedir(dp) == -1) ? -errno : 0;
60} 58}
@@ -69,6 +67,7 @@ static int dump_one_device(const char *dev_dir_name)
69 "%i", &dev_idx); 67 "%i", &dev_idx);
70 if (retval != 1) 68 if (retval != 1)
71 return -EINVAL; 69 return -EINVAL;
70
72 retval = read_sysfs_string("name", dev_dir_name, name); 71 retval = read_sysfs_string("name", dev_dir_name, name);
73 if (retval) 72 if (retval)
74 return retval; 73 return retval;
@@ -77,6 +76,7 @@ static int dump_one_device(const char *dev_dir_name)
77 76
78 if (verblevel >= VERBLEVEL_SENSORS) 77 if (verblevel >= VERBLEVEL_SENSORS)
79 return dump_channels(dev_dir_name); 78 return dump_channels(dev_dir_name);
79
80 return 0; 80 return 0;
81} 81}
82 82
@@ -90,11 +90,13 @@ static int dump_one_trigger(const char *dev_dir_name)
90 "%i", &dev_idx); 90 "%i", &dev_idx);
91 if (retval != 1) 91 if (retval != 1)
92 return -EINVAL; 92 return -EINVAL;
93
93 retval = read_sysfs_string("name", dev_dir_name, name); 94 retval = read_sysfs_string("name", dev_dir_name, name);
94 if (retval) 95 if (retval)
95 return retval; 96 return retval;
96 97
97 printf("Trigger %03d: %s\n", dev_idx, name); 98 printf("Trigger %03d: %s\n", dev_idx, name);
99
98 return 0; 100 return 0;
99} 101}
100 102
@@ -151,6 +153,7 @@ static int dump_devices(void)
151 free(dev_dir_name); 153 free(dev_dir_name);
152 } 154 }
153 } 155 }
156
154 return (closedir(dp) == -1) ? -errno : 0; 157 return (closedir(dp) == -1) ? -errno : 0;
155 158
156error_close_dir: 159error_close_dir: