iio_event_monitor.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Industrialio event test code.
  3. *
  4. * Copyright (c) 2011-2012 Lars-Peter Clausen <lars@metafoo.de>
  5. *
  6. * This program is primarily intended as an example application.
  7. * Reads the current buffer setup from sysfs and starts a short capture
  8. * from the specified device, pretty printing the result after appropriate
  9. * conversion.
  10. *
  11. * Usage:
  12. * iio_event_monitor <device_name>
  13. */
  14. #include <unistd.h>
  15. #include <stdlib.h>
  16. #include <stdbool.h>
  17. #include <stdio.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <poll.h>
  21. #include <fcntl.h>
  22. #include <sys/ioctl.h>
  23. #include "iio_utils.h"
  24. #include <linux/iio/events.h>
  25. #include <linux/iio/types.h>
  26. static const char * const iio_chan_type_name_spec[] = {
  27. [IIO_VOLTAGE] = "voltage",
  28. [IIO_CURRENT] = "current",
  29. [IIO_POWER] = "power",
  30. [IIO_ACCEL] = "accel",
  31. [IIO_ANGL_VEL] = "anglvel",
  32. [IIO_MAGN] = "magn",
  33. [IIO_LIGHT] = "illuminance",
  34. [IIO_INTENSITY] = "intensity",
  35. [IIO_PROXIMITY] = "proximity",
  36. [IIO_TEMP] = "temp",
  37. [IIO_INCLI] = "incli",
  38. [IIO_ROT] = "rot",
  39. [IIO_ANGL] = "angl",
  40. [IIO_TIMESTAMP] = "timestamp",
  41. [IIO_CAPACITANCE] = "capacitance",
  42. [IIO_ALTVOLTAGE] = "altvoltage",
  43. [IIO_CCT] = "cct",
  44. [IIO_PRESSURE] = "pressure",
  45. [IIO_HUMIDITYRELATIVE] = "humidityrelative",
  46. [IIO_ACTIVITY] = "activity",
  47. [IIO_STEPS] = "steps",
  48. [IIO_ENERGY] = "energy",
  49. [IIO_DISTANCE] = "distance",
  50. [IIO_VELOCITY] = "velocity",
  51. [IIO_CONCENTRATION] = "concentration",
  52. [IIO_RESISTANCE] = "resistance",
  53. [IIO_PH] = "ph",
  54. [IIO_UVINDEX] = "uvindex",
  55. [IIO_GRAVITY] = "gravity",
  56. [IIO_POSITIONRELATIVE] = "positionrelative",
  57. [IIO_PHASE] = "phase",
  58. [IIO_MASSCONCENTRATION] = "massconcentration",
  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_LIGHT_DUV] = "duv",
  95. [IIO_MOD_QUATERNION] = "quaternion",
  96. [IIO_MOD_TEMP_AMBIENT] = "ambient",
  97. [IIO_MOD_TEMP_OBJECT] = "object",
  98. [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
  99. [IIO_MOD_NORTH_TRUE] = "from_north_true",
  100. [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
  101. [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
  102. [IIO_MOD_RUNNING] = "running",
  103. [IIO_MOD_JOGGING] = "jogging",
  104. [IIO_MOD_WALKING] = "walking",
  105. [IIO_MOD_STILL] = "still",
  106. [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
  107. [IIO_MOD_I] = "i",
  108. [IIO_MOD_Q] = "q",
  109. [IIO_MOD_CO2] = "co2",
  110. [IIO_MOD_ETHANOL] = "ethanol",
  111. [IIO_MOD_H2] = "h2",
  112. [IIO_MOD_VOC] = "voc",
  113. [IIO_MOD_PM1] = "pm1",
  114. [IIO_MOD_PM2P5] = "pm2p5",
  115. [IIO_MOD_PM4] = "pm4",
  116. [IIO_MOD_PM10] = "pm10",
  117. [IIO_MOD_O2] = "o2",
  118. };
  119. static bool event_is_known(struct iio_event_data *event)
  120. {
  121. enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
  122. enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
  123. enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
  124. enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
  125. switch (type) {
  126. case IIO_VOLTAGE:
  127. case IIO_CURRENT:
  128. case IIO_POWER:
  129. case IIO_ACCEL:
  130. case IIO_ANGL_VEL:
  131. case IIO_MAGN:
  132. case IIO_LIGHT:
  133. case IIO_INTENSITY:
  134. case IIO_PROXIMITY:
  135. case IIO_TEMP:
  136. case IIO_INCLI:
  137. case IIO_ROT:
  138. case IIO_ANGL:
  139. case IIO_TIMESTAMP:
  140. case IIO_CAPACITANCE:
  141. case IIO_ALTVOLTAGE:
  142. case IIO_CCT:
  143. case IIO_PRESSURE:
  144. case IIO_HUMIDITYRELATIVE:
  145. case IIO_ACTIVITY:
  146. case IIO_STEPS:
  147. case IIO_ENERGY:
  148. case IIO_DISTANCE:
  149. case IIO_VELOCITY:
  150. case IIO_CONCENTRATION:
  151. case IIO_RESISTANCE:
  152. case IIO_PH:
  153. case IIO_UVINDEX:
  154. case IIO_GRAVITY:
  155. case IIO_POSITIONRELATIVE:
  156. case IIO_PHASE:
  157. case IIO_MASSCONCENTRATION:
  158. break;
  159. default:
  160. return false;
  161. }
  162. switch (mod) {
  163. case IIO_NO_MOD:
  164. case IIO_MOD_X:
  165. case IIO_MOD_Y:
  166. case IIO_MOD_Z:
  167. case IIO_MOD_X_AND_Y:
  168. case IIO_MOD_X_AND_Z:
  169. case IIO_MOD_Y_AND_Z:
  170. case IIO_MOD_X_AND_Y_AND_Z:
  171. case IIO_MOD_X_OR_Y:
  172. case IIO_MOD_X_OR_Z:
  173. case IIO_MOD_Y_OR_Z:
  174. case IIO_MOD_X_OR_Y_OR_Z:
  175. case IIO_MOD_LIGHT_BOTH:
  176. case IIO_MOD_LIGHT_IR:
  177. case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
  178. case IIO_MOD_SUM_SQUARED_X_Y_Z:
  179. case IIO_MOD_LIGHT_CLEAR:
  180. case IIO_MOD_LIGHT_RED:
  181. case IIO_MOD_LIGHT_GREEN:
  182. case IIO_MOD_LIGHT_BLUE:
  183. case IIO_MOD_LIGHT_UV:
  184. case IIO_MOD_LIGHT_DUV:
  185. case IIO_MOD_QUATERNION:
  186. case IIO_MOD_TEMP_AMBIENT:
  187. case IIO_MOD_TEMP_OBJECT:
  188. case IIO_MOD_NORTH_MAGN:
  189. case IIO_MOD_NORTH_TRUE:
  190. case IIO_MOD_NORTH_MAGN_TILT_COMP:
  191. case IIO_MOD_NORTH_TRUE_TILT_COMP:
  192. case IIO_MOD_RUNNING:
  193. case IIO_MOD_JOGGING:
  194. case IIO_MOD_WALKING:
  195. case IIO_MOD_STILL:
  196. case IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z:
  197. case IIO_MOD_I:
  198. case IIO_MOD_Q:
  199. case IIO_MOD_CO2:
  200. case IIO_MOD_ETHANOL:
  201. case IIO_MOD_H2:
  202. case IIO_MOD_VOC:
  203. case IIO_MOD_PM1:
  204. case IIO_MOD_PM2P5:
  205. case IIO_MOD_PM4:
  206. case IIO_MOD_PM10:
  207. case IIO_MOD_O2:
  208. break;
  209. default:
  210. return false;
  211. }
  212. switch (ev_type) {
  213. case IIO_EV_TYPE_THRESH:
  214. case IIO_EV_TYPE_MAG:
  215. case IIO_EV_TYPE_ROC:
  216. case IIO_EV_TYPE_THRESH_ADAPTIVE:
  217. case IIO_EV_TYPE_MAG_ADAPTIVE:
  218. case IIO_EV_TYPE_CHANGE:
  219. break;
  220. default:
  221. return false;
  222. }
  223. switch (dir) {
  224. case IIO_EV_DIR_EITHER:
  225. case IIO_EV_DIR_RISING:
  226. case IIO_EV_DIR_FALLING:
  227. case IIO_EV_DIR_NONE:
  228. break;
  229. default:
  230. return false;
  231. }
  232. return true;
  233. }
  234. static void print_event(struct iio_event_data *event)
  235. {
  236. enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
  237. enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
  238. enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
  239. enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
  240. int chan = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
  241. int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
  242. bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
  243. if (!event_is_known(event)) {
  244. fprintf(stderr, "Unknown event: time: %lld, id: %llx\n",
  245. event->timestamp, event->id);
  246. return;
  247. }
  248. printf("Event: time: %lld, type: %s", event->timestamp,
  249. iio_chan_type_name_spec[type]);
  250. if (mod != IIO_NO_MOD)
  251. printf("(%s)", iio_modifier_names[mod]);
  252. if (chan >= 0) {
  253. printf(", channel: %d", chan);
  254. if (diff && chan2 >= 0)
  255. printf("-%d", chan2);
  256. }
  257. printf(", evtype: %s", iio_ev_type_text[ev_type]);
  258. if (dir != IIO_EV_DIR_NONE)
  259. printf(", direction: %s", iio_ev_dir_text[dir]);
  260. printf("\n");
  261. }
  262. int main(int argc, char **argv)
  263. {
  264. struct iio_event_data event;
  265. const char *device_name;
  266. char *chrdev_name;
  267. int ret;
  268. int dev_num;
  269. int fd, event_fd;
  270. if (argc <= 1) {
  271. fprintf(stderr, "Usage: %s <device_name>\n", argv[0]);
  272. return -1;
  273. }
  274. device_name = argv[1];
  275. dev_num = find_type_by_name(device_name, "iio:device");
  276. if (dev_num >= 0) {
  277. printf("Found IIO device with name %s with device number %d\n",
  278. device_name, dev_num);
  279. ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
  280. if (ret < 0)
  281. return -ENOMEM;
  282. } else {
  283. /*
  284. * If we can't find an IIO device by name assume device_name is
  285. * an IIO chrdev
  286. */
  287. chrdev_name = strdup(device_name);
  288. if (!chrdev_name)
  289. return -ENOMEM;
  290. }
  291. fd = open(chrdev_name, 0);
  292. if (fd == -1) {
  293. ret = -errno;
  294. fprintf(stderr, "Failed to open %s\n", chrdev_name);
  295. goto error_free_chrdev_name;
  296. }
  297. ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
  298. if (ret == -1 || event_fd == -1) {
  299. ret = -errno;
  300. if (ret == -ENODEV)
  301. fprintf(stderr,
  302. "This device does not support events\n");
  303. else
  304. fprintf(stderr, "Failed to retrieve event fd\n");
  305. if (close(fd) == -1)
  306. perror("Failed to close character device file");
  307. goto error_free_chrdev_name;
  308. }
  309. if (close(fd) == -1) {
  310. ret = -errno;
  311. goto error_free_chrdev_name;
  312. }
  313. while (true) {
  314. ret = read(event_fd, &event, sizeof(event));
  315. if (ret == -1) {
  316. if (errno == EAGAIN) {
  317. fprintf(stderr, "nothing available\n");
  318. continue;
  319. } else {
  320. ret = -errno;
  321. perror("Failed to read event from device");
  322. break;
  323. }
  324. }
  325. if (ret != sizeof(event)) {
  326. fprintf(stderr, "Reading event failed!\n");
  327. ret = -EIO;
  328. break;
  329. }
  330. print_event(&event);
  331. }
  332. if (close(event_fd) == -1)
  333. perror("Failed to close event file");
  334. error_free_chrdev_name:
  335. free(chrdev_name);
  336. return ret;
  337. }