iio_event_monitor.c 8.0 KB

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