iio_event_monitor.c 8.4 KB

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