iio_event_monitor.c 8.6 KB

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