lsiio.c (a9d7acc8b0d52ae2f15f6280f63ac9d1e73cfb78) | lsiio.c (23908db413eccd77084b09c9b0a4451dfb0524c0) |
---|---|
1/* 2 * Industrial I/O utilities - lsiio.c 3 * 4 * Copyright (c) 2010 Manuel Stahl <manuel.stahl@iis.fraunhofer.de> 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 as published by 8 * the Free Software Foundation. --- 6 unchanged lines hidden (view full) --- 15#include <stdint.h> 16#include <stdlib.h> 17#include <unistd.h> 18#include <sys/types.h> 19#include <sys/stat.h> 20#include <sys/dir.h> 21#include "iio_utils.h" 22 | 1/* 2 * Industrial I/O utilities - lsiio.c 3 * 4 * Copyright (c) 2010 Manuel Stahl <manuel.stahl@iis.fraunhofer.de> 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 as published by 8 * the Free Software Foundation. --- 6 unchanged lines hidden (view full) --- 15#include <stdint.h> 16#include <stdlib.h> 17#include <unistd.h> 18#include <sys/types.h> 19#include <sys/stat.h> 20#include <sys/dir.h> 21#include "iio_utils.h" 22 |
23 |
|
23static enum verbosity { 24 VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */ 25 VERBLEVEL_SENSORS, /* 1 lists sensors */ 26} verblevel = VERBLEVEL_DEFAULT; 27 28const char *type_device = "iio:device"; 29const char *type_trigger = "trigger"; 30 | 24static enum verbosity { 25 VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */ 26 VERBLEVEL_SENSORS, /* 1 lists sensors */ 27} verblevel = VERBLEVEL_DEFAULT; 28 29const char *type_device = "iio:device"; 30const char *type_trigger = "trigger"; 31 |
32 |
|
31static inline int check_prefix(const char *str, const char *prefix) 32{ 33 return strlen(str) > strlen(prefix) && | 33static inline int check_prefix(const char *str, const char *prefix) 34{ 35 return strlen(str) > strlen(prefix) && |
34 strncmp(str, prefix, strlen(prefix)) == 0; | 36 strncmp(str, prefix, strlen(prefix)) == 0; |
35} 36 37static inline int check_postfix(const char *str, const char *postfix) 38{ 39 return strlen(str) > strlen(postfix) && | 37} 38 39static inline int check_postfix(const char *str, const char *postfix) 40{ 41 return strlen(str) > strlen(postfix) && |
40 strcmp(str + strlen(str) - strlen(postfix), postfix) == 0; | 42 strcmp(str + strlen(str) - strlen(postfix), postfix) == 0; |
41} 42 43static int dump_channels(const char *dev_dir_name) 44{ 45 DIR *dp; 46 const struct dirent *ent; 47 48 dp = opendir(dev_dir_name); 49 if (dp == NULL) 50 return -errno; | 43} 44 45static int dump_channels(const char *dev_dir_name) 46{ 47 DIR *dp; 48 const struct dirent *ent; 49 50 dp = opendir(dev_dir_name); 51 if (dp == NULL) 52 return -errno; |
51 | |
52 while (ent = readdir(dp), ent != NULL) 53 if (check_prefix(ent->d_name, "in_") && | 53 while (ent = readdir(dp), ent != NULL) 54 if (check_prefix(ent->d_name, "in_") && |
54 check_postfix(ent->d_name, "_raw")) | 55 check_postfix(ent->d_name, "_raw")) { |
55 printf(" %-10s\n", ent->d_name); | 56 printf(" %-10s\n", ent->d_name); |
57 } |
|
56 57 return (closedir(dp) == -1) ? -errno : 0; 58} 59 60static int dump_one_device(const char *dev_dir_name) 61{ 62 char name[IIO_MAX_NAME_LENGTH]; 63 int dev_idx; | 58 59 return (closedir(dp) == -1) ? -errno : 0; 60} 61 62static int dump_one_device(const char *dev_dir_name) 63{ 64 char name[IIO_MAX_NAME_LENGTH]; 65 int dev_idx; |
64 int ret; | 66 int retval; |
65 | 67 |
66 ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device), "%i", 67 &dev_idx); 68 if (ret != 1) | 68 retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device), 69 "%i", &dev_idx); 70 if (retval != 1) |
69 return -EINVAL; | 71 return -EINVAL; |
72 retval = read_sysfs_string("name", dev_dir_name, name); 73 if (retval) 74 return retval; |
|
70 | 75 |
71 ret = read_sysfs_string("name", dev_dir_name, name); 72 if (ret) 73 return ret; 74 | |
75 printf("Device %03d: %s\n", dev_idx, name); 76 77 if (verblevel >= VERBLEVEL_SENSORS) 78 return dump_channels(dev_dir_name); | 76 printf("Device %03d: %s\n", dev_idx, name); 77 78 if (verblevel >= VERBLEVEL_SENSORS) 79 return dump_channels(dev_dir_name); |
79 | |
80 return 0; 81} 82 83static int dump_one_trigger(const char *dev_dir_name) 84{ 85 char name[IIO_MAX_NAME_LENGTH]; 86 int dev_idx; | 80 return 0; 81} 82 83static int dump_one_trigger(const char *dev_dir_name) 84{ 85 char name[IIO_MAX_NAME_LENGTH]; 86 int dev_idx; |
87 int ret; | 87 int retval; |
88 | 88 |
89 ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger), 90 "%i", &dev_idx); 91 if (ret != 1) | 89 retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger), 90 "%i", &dev_idx); 91 if (retval != 1) |
92 return -EINVAL; | 92 return -EINVAL; |
93 retval = read_sysfs_string("name", dev_dir_name, name); 94 if (retval) 95 return retval; |
|
93 | 96 |
94 ret = read_sysfs_string("name", dev_dir_name, name); 95 if (ret) 96 return ret; 97 | |
98 printf("Trigger %03d: %s\n", dev_idx, name); | 97 printf("Trigger %03d: %s\n", dev_idx, name); |
99 | |
100 return 0; 101} 102 103static int dump_devices(void) 104{ 105 const struct dirent *ent; 106 int ret; 107 DIR *dp; --- 40 unchanged lines hidden (view full) --- 148 if (ret) { 149 free(dev_dir_name); 150 goto error_close_dir; 151 } 152 153 free(dev_dir_name); 154 } 155 } | 98 return 0; 99} 100 101static int dump_devices(void) 102{ 103 const struct dirent *ent; 104 int ret; 105 DIR *dp; --- 40 unchanged lines hidden (view full) --- 146 if (ret) { 147 free(dev_dir_name); 148 goto error_close_dir; 149 } 150 151 free(dev_dir_name); 152 } 153 } |
156 | |
157 return (closedir(dp) == -1) ? -errno : 0; 158 159error_close_dir: 160 if (closedir(dp) == -1) 161 perror("dump_devices(): Failed to close directory"); 162 163 return ret; 164} --- 26 unchanged lines hidden --- | 154 return (closedir(dp) == -1) ? -errno : 0; 155 156error_close_dir: 157 if (closedir(dp) == -1) 158 perror("dump_devices(): Failed to close directory"); 159 160 return ret; 161} --- 26 unchanged lines hidden --- |