iio_event_monitor.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* Industrialio event test code.
  2. *
  3. * Copyright (c) 2011-2012 Lars-Peter Clausen <lars@metafoo.de>
  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. * This program is primarily intended as an example application.
  10. * Reads the current buffer setup from sysfs and starts a short capture
  11. * from the specified device, pretty printing the result after appropriate
  12. * conversion.
  13. *
  14. * Usage:
  15. * iio_event_monitor <device_name>
  16. *
  17. */
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <stdbool.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <poll.h>
  25. #include <fcntl.h>
  26. #include <sys/ioctl.h>
  27. #include "iio_utils.h"
  28. #include <linux/iio/events.h>
  29. #include <linux/iio/types.h>
  30. static const char * const iio_chan_type_name_spec[] = {
  31. [IIO_VOLTAGE] = "voltage",
  32. [IIO_CURRENT] = "current",
  33. [IIO_POWER] = "power",
  34. [IIO_ACCEL] = "accel",
  35. [IIO_ANGL_VEL] = "anglvel",
  36. [IIO_MAGN] = "magn",
  37. [IIO_LIGHT] = "illuminance",
  38. [IIO_INTENSITY] = "intensity",
  39. [IIO_PROXIMITY] = "proximity",
  40. [IIO_TEMP] = "temp",
  41. [IIO_INCLI] = "incli",
  42. [IIO_ROT] = "rot",
  43. [IIO_ANGL] = "angl",
  44. [IIO_TIMESTAMP] = "timestamp",
  45. [IIO_CAPACITANCE] = "capacitance",
  46. [IIO_ALTVOLTAGE] = "altvoltage",
  47. [IIO_CCT] = "cct",
  48. [IIO_PRESSURE] = "pressure",
  49. [IIO_HUMIDITYRELATIVE] = "humidityrelative",
  50. [IIO_ACTIVITY] = "activity",
  51. [IIO_STEPS] = "steps",
  52. };
  53. static const char * const iio_ev_type_text[] = {
  54. [IIO_EV_TYPE_THRESH] = "thresh",
  55. [IIO_EV_TYPE_MAG] = "mag",
  56. [IIO_EV_TYPE_ROC] = "roc",
  57. [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
  58. [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
  59. [IIO_EV_TYPE_CHANGE] = "change",
  60. };
  61. static const char * const iio_ev_dir_text[] = {
  62. [IIO_EV_DIR_EITHER] = "either",
  63. [IIO_EV_DIR_RISING] = "rising",
  64. [IIO_EV_DIR_FALLING] = "falling"
  65. };
  66. static const char * const iio_modifier_names[] = {
  67. [IIO_MOD_X] = "x",
  68. [IIO_MOD_Y] = "y",
  69. [IIO_MOD_Z] = "z",
  70. [IIO_MOD_X_AND_Y] = "x&y",
  71. [IIO_MOD_X_AND_Z] = "x&z",
  72. [IIO_MOD_Y_AND_Z] = "y&z",
  73. [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
  74. [IIO_MOD_X_OR_Y] = "x|y",
  75. [IIO_MOD_X_OR_Z] = "x|z",
  76. [IIO_MOD_Y_OR_Z] = "y|z",
  77. [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
  78. [IIO_MOD_LIGHT_BOTH] = "both",
  79. [IIO_MOD_LIGHT_IR] = "ir",
  80. [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
  81. [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
  82. [IIO_MOD_LIGHT_CLEAR] = "clear",
  83. [IIO_MOD_LIGHT_RED] = "red",
  84. [IIO_MOD_LIGHT_GREEN] = "green",
  85. [IIO_MOD_LIGHT_BLUE] = "blue",
  86. [IIO_MOD_QUATERNION] = "quaternion",
  87. [IIO_MOD_TEMP_AMBIENT] = "ambient",
  88. [IIO_MOD_TEMP_OBJECT] = "object",
  89. [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
  90. [IIO_MOD_NORTH_TRUE] = "from_north_true",
  91. [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
  92. [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
  93. [IIO_MOD_RUNNING] = "running",
  94. [IIO_MOD_JOGGING] = "jogging",
  95. [IIO_MOD_WALKING] = "walking",
  96. [IIO_MOD_STILL] = "still",
  97. };
  98. static bool event_is_known(struct iio_event_data *event)
  99. {
  100. enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
  101. enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
  102. enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
  103. enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
  104. switch (type) {
  105. case IIO_VOLTAGE:
  106. case IIO_CURRENT:
  107. case IIO_POWER:
  108. case IIO_ACCEL:
  109. case IIO_ANGL_VEL:
  110. case IIO_MAGN:
  111. case IIO_LIGHT:
  112. case IIO_INTENSITY:
  113. case IIO_PROXIMITY:
  114. case IIO_TEMP:
  115. case IIO_INCLI:
  116. case IIO_ROT:
  117. case IIO_ANGL:
  118. case IIO_TIMESTAMP:
  119. case IIO_CAPACITANCE:
  120. case IIO_ALTVOLTAGE:
  121. case IIO_CCT:
  122. case IIO_PRESSURE:
  123. case IIO_HUMIDITYRELATIVE:
  124. case IIO_ACTIVITY:
  125. case IIO_STEPS:
  126. break;
  127. default:
  128. return false;
  129. }
  130. switch (mod) {
  131. case IIO_NO_MOD:
  132. case IIO_MOD_X:
  133. case IIO_MOD_Y:
  134. case IIO_MOD_Z:
  135. case IIO_MOD_X_AND_Y:
  136. case IIO_MOD_X_AND_Z:
  137. case IIO_MOD_Y_AND_Z:
  138. case IIO_MOD_X_AND_Y_AND_Z:
  139. case IIO_MOD_X_OR_Y:
  140. case IIO_MOD_X_OR_Z:
  141. case IIO_MOD_Y_OR_Z:
  142. case IIO_MOD_X_OR_Y_OR_Z:
  143. case IIO_MOD_LIGHT_BOTH:
  144. case IIO_MOD_LIGHT_IR:
  145. case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
  146. case IIO_MOD_SUM_SQUARED_X_Y_Z:
  147. case IIO_MOD_LIGHT_CLEAR:
  148. case IIO_MOD_LIGHT_RED:
  149. case IIO_MOD_LIGHT_GREEN:
  150. case IIO_MOD_LIGHT_BLUE:
  151. case IIO_MOD_QUATERNION:
  152. case IIO_MOD_TEMP_AMBIENT:
  153. case IIO_MOD_TEMP_OBJECT:
  154. case IIO_MOD_NORTH_MAGN:
  155. case IIO_MOD_NORTH_TRUE:
  156. case IIO_MOD_NORTH_MAGN_TILT_COMP:
  157. case IIO_MOD_NORTH_TRUE_TILT_COMP:
  158. case IIO_MOD_RUNNING:
  159. case IIO_MOD_JOGGING:
  160. case IIO_MOD_WALKING:
  161. case IIO_MOD_STILL:
  162. break;
  163. default:
  164. return false;
  165. }
  166. switch (ev_type) {
  167. case IIO_EV_TYPE_THRESH:
  168. case IIO_EV_TYPE_MAG:
  169. case IIO_EV_TYPE_ROC:
  170. case IIO_EV_TYPE_THRESH_ADAPTIVE:
  171. case IIO_EV_TYPE_MAG_ADAPTIVE:
  172. case IIO_EV_TYPE_CHANGE:
  173. break;
  174. default:
  175. return false;
  176. }
  177. switch (dir) {
  178. case IIO_EV_DIR_EITHER:
  179. case IIO_EV_DIR_RISING:
  180. case IIO_EV_DIR_FALLING:
  181. case IIO_EV_DIR_NONE:
  182. break;
  183. default:
  184. return false;
  185. }
  186. return true;
  187. }
  188. static void print_event(struct iio_event_data *event)
  189. {
  190. enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
  191. enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
  192. enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
  193. enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
  194. int chan = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
  195. int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
  196. bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
  197. if (!event_is_known(event)) {
  198. printf("Unknown event: time: %lld, id: %llx\n",
  199. event->timestamp, event->id);
  200. return;
  201. }
  202. printf("Event: time: %lld, type: %s", event->timestamp,
  203. iio_chan_type_name_spec[type]);
  204. if (mod != IIO_NO_MOD)
  205. printf("(%s)", iio_modifier_names[mod]);
  206. if (chan >= 0) {
  207. printf(", channel: %d", chan);
  208. if (diff && chan2 >= 0)
  209. printf("-%d", chan2);
  210. }
  211. printf(", evtype: %s", iio_ev_type_text[ev_type]);
  212. if (dir != IIO_EV_DIR_NONE)
  213. printf(", direction: %s", iio_ev_dir_text[dir]);
  214. printf("\n");
  215. }
  216. int main(int argc, char **argv)
  217. {
  218. struct iio_event_data event;
  219. const char *device_name;
  220. char *chrdev_name;
  221. int ret;
  222. int dev_num;
  223. int fd, event_fd;
  224. if (argc <= 1) {
  225. printf("Usage: %s <device_name>\n", argv[0]);
  226. return -1;
  227. }
  228. device_name = argv[1];
  229. dev_num = find_type_by_name(device_name, "iio:device");
  230. if (dev_num >= 0) {
  231. printf("Found IIO device with name %s with device number %d\n",
  232. device_name, dev_num);
  233. ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
  234. if (ret < 0) {
  235. return -ENOMEM;
  236. }
  237. } else {
  238. /* If we can't find a IIO device by name assume device_name is a
  239. IIO chrdev */
  240. chrdev_name = strdup(device_name);
  241. if (!chrdev_name)
  242. return -ENOMEM;
  243. }
  244. fd = open(chrdev_name, 0);
  245. if (fd == -1) {
  246. ret = -errno;
  247. fprintf(stdout, "Failed to open %s\n", chrdev_name);
  248. goto error_free_chrdev_name;
  249. }
  250. ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
  251. if (ret == -1 || event_fd == -1) {
  252. ret = -errno;
  253. fprintf(stdout, "Failed to retrieve event fd\n");
  254. if (close(fd) == -1)
  255. perror("Failed to close character device file");
  256. goto error_free_chrdev_name;
  257. }
  258. if (close(fd) == -1) {
  259. ret = -errno;
  260. goto error_free_chrdev_name;
  261. }
  262. while (true) {
  263. ret = read(event_fd, &event, sizeof(event));
  264. if (ret == -1) {
  265. if (errno == EAGAIN) {
  266. printf("nothing available\n");
  267. continue;
  268. } else {
  269. ret = -errno;
  270. perror("Failed to read event from device");
  271. break;
  272. }
  273. }
  274. print_event(&event);
  275. }
  276. if (close(event_fd) == -1)
  277. perror("Failed to close event file");
  278. error_free_chrdev_name:
  279. free(chrdev_name);
  280. return ret;
  281. }