lsiio.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.
  9. */
  10. #include <string.h>
  11. #include <dirent.h>
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include <stdint.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <sys/dir.h>
  20. #include "iio_utils.h"
  21. static enum verbosity {
  22. VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */
  23. VERBLEVEL_SENSORS, /* 1 lists sensors */
  24. } verblevel = VERBLEVEL_DEFAULT;
  25. const char *type_device = "iio:device";
  26. const char *type_trigger = "trigger";
  27. static inline int check_prefix(const char *str, const char *prefix)
  28. {
  29. return strlen(str) > strlen(prefix) &&
  30. strncmp(str, prefix, strlen(prefix)) == 0;
  31. }
  32. static inline int check_postfix(const char *str, const char *postfix)
  33. {
  34. return strlen(str) > strlen(postfix) &&
  35. strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
  36. }
  37. static int dump_channels(const char *dev_dir_name)
  38. {
  39. DIR *dp;
  40. const struct dirent *ent;
  41. dp = opendir(dev_dir_name);
  42. if (dp == NULL)
  43. return -errno;
  44. while (ent = readdir(dp), ent != NULL)
  45. if (check_prefix(ent->d_name, "in_") &&
  46. check_postfix(ent->d_name, "_raw")) {
  47. printf(" %-10s\n", ent->d_name);
  48. }
  49. return 0;
  50. }
  51. static int dump_one_device(const char *dev_dir_name)
  52. {
  53. char name[IIO_MAX_NAME_LENGTH];
  54. int dev_idx;
  55. int retval;
  56. retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device),
  57. "%i", &dev_idx);
  58. if (retval != 1)
  59. return -EINVAL;
  60. read_sysfs_string("name", dev_dir_name, name);
  61. printf("Device %03d: %s\n", dev_idx, name);
  62. if (verblevel >= VERBLEVEL_SENSORS)
  63. return dump_channels(dev_dir_name);
  64. return 0;
  65. }
  66. static int dump_one_trigger(const char *dev_dir_name)
  67. {
  68. char name[IIO_MAX_NAME_LENGTH];
  69. int dev_idx;
  70. int retval;
  71. retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger),
  72. "%i", &dev_idx);
  73. if (retval != 1)
  74. return -EINVAL;
  75. read_sysfs_string("name", dev_dir_name, name);
  76. printf("Trigger %03d: %s\n", dev_idx, name);
  77. return 0;
  78. }
  79. static void dump_devices(void)
  80. {
  81. const struct dirent *ent;
  82. DIR *dp;
  83. dp = opendir(iio_dir);
  84. if (dp == NULL) {
  85. printf("No industrial I/O devices available\n");
  86. return;
  87. }
  88. while (ent = readdir(dp), ent != NULL) {
  89. if (check_prefix(ent->d_name, type_device)) {
  90. char *dev_dir_name;
  91. asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name);
  92. dump_one_device(dev_dir_name);
  93. free(dev_dir_name);
  94. if (verblevel >= VERBLEVEL_SENSORS)
  95. printf("\n");
  96. }
  97. }
  98. rewinddir(dp);
  99. while (ent = readdir(dp), ent != NULL) {
  100. if (check_prefix(ent->d_name, type_trigger)) {
  101. char *dev_dir_name;
  102. asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name);
  103. dump_one_trigger(dev_dir_name);
  104. free(dev_dir_name);
  105. }
  106. }
  107. closedir(dp);
  108. }
  109. int main(int argc, char **argv)
  110. {
  111. int c, err = 0;
  112. while ((c = getopt(argc, argv, "d:D:v")) != EOF) {
  113. switch (c) {
  114. case 'v':
  115. verblevel++;
  116. break;
  117. case '?':
  118. default:
  119. err++;
  120. break;
  121. }
  122. }
  123. if (err || argc > optind) {
  124. fprintf(stderr, "Usage: lsiio [options]...\n"
  125. "List industrial I/O devices\n"
  126. " -v, --verbose\n"
  127. " Increase verbosity (may be given multiple times)\n"
  128. );
  129. exit(1);
  130. }
  131. dump_devices();
  132. return 0;
  133. }