iio_event_monitor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 <dirent.h>
  17. #include <stdbool.h>
  18. #include <stdio.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <poll.h>
  22. #include <fcntl.h>
  23. #include <sys/ioctl.h>
  24. #include "iio_utils.h"
  25. #include <linux/iio/events.h>
  26. #include <linux/iio/types.h>
  27. static const char * const iio_chan_type_name_spec[] = {
  28. [IIO_VOLTAGE] = "voltage",
  29. [IIO_CURRENT] = "current",
  30. [IIO_POWER] = "power",
  31. [IIO_ACCEL] = "accel",
  32. [IIO_ANGL_VEL] = "anglvel",
  33. [IIO_MAGN] = "magn",
  34. [IIO_LIGHT] = "illuminance",
  35. [IIO_INTENSITY] = "intensity",
  36. [IIO_PROXIMITY] = "proximity",
  37. [IIO_TEMP] = "temp",
  38. [IIO_INCLI] = "incli",
  39. [IIO_ROT] = "rot",
  40. [IIO_ANGL] = "angl",
  41. [IIO_TIMESTAMP] = "timestamp",
  42. [IIO_CAPACITANCE] = "capacitance",
  43. [IIO_ALTVOLTAGE] = "altvoltage",
  44. [IIO_CCT] = "cct",
  45. [IIO_PRESSURE] = "pressure",
  46. [IIO_HUMIDITYRELATIVE] = "humidityrelative",
  47. [IIO_ACTIVITY] = "activity",
  48. [IIO_STEPS] = "steps",
  49. [IIO_ENERGY] = "energy",
  50. [IIO_DISTANCE] = "distance",
  51. [IIO_VELOCITY] = "velocity",
  52. [IIO_CONCENTRATION] = "concentration",
  53. [IIO_RESISTANCE] = "resistance",
  54. [IIO_PH] = "ph",
  55. [IIO_UVINDEX] = "uvindex",
  56. [IIO_GRAVITY] = "gravity",
  57. [IIO_POSITIONRELATIVE] = "positionrelative",
  58. [IIO_PHASE] = "phase",
  59. [IIO_MASSCONCENTRATION] = "massconcentration",
  60. [IIO_DELTA_ANGL] = "deltaangl",
  61. [IIO_DELTA_VELOCITY] = "deltavelocity",
  62. [IIO_COLORTEMP] = "colortemp",
  63. [IIO_CHROMATICITY] = "chromaticity",
  64. };
  65. static const char * const iio_ev_type_text[] = {
  66. [IIO_EV_TYPE_THRESH] = "thresh",
  67. [IIO_EV_TYPE_MAG] = "mag",
  68. [IIO_EV_TYPE_ROC] = "roc",
  69. [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
  70. [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
  71. [IIO_EV_TYPE_CHANGE] = "change",
  72. [IIO_EV_TYPE_MAG_REFERENCED] = "mag_referenced",
  73. [IIO_EV_TYPE_GESTURE] = "gesture",
  74. };
  75. static const char * const iio_ev_dir_text[] = {
  76. [IIO_EV_DIR_EITHER] = "either",
  77. [IIO_EV_DIR_RISING] = "rising",
  78. [IIO_EV_DIR_FALLING] = "falling",
  79. [IIO_EV_DIR_SINGLETAP] = "singletap",
  80. [IIO_EV_DIR_DOUBLETAP] = "doubletap",
  81. };
  82. static const char * const iio_modifier_names[] = {
  83. [IIO_MOD_X] = "x",
  84. [IIO_MOD_Y] = "y",
  85. [IIO_MOD_Z] = "z",
  86. [IIO_MOD_X_AND_Y] = "x&y",
  87. [IIO_MOD_X_AND_Z] = "x&z",
  88. [IIO_MOD_Y_AND_Z] = "y&z",
  89. [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
  90. [IIO_MOD_X_OR_Y] = "x|y",
  91. [IIO_MOD_X_OR_Z] = "x|z",
  92. [IIO_MOD_Y_OR_Z] = "y|z",
  93. [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
  94. [IIO_MOD_LIGHT_BOTH] = "both",
  95. [IIO_MOD_LIGHT_IR] = "ir",
  96. [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
  97. [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
  98. [IIO_MOD_LIGHT_CLEAR] = "clear",
  99. [IIO_MOD_LIGHT_RED] = "red",
  100. [IIO_MOD_LIGHT_GREEN] = "green",
  101. [IIO_MOD_LIGHT_BLUE] = "blue",
  102. [IIO_MOD_LIGHT_UV] = "uv",
  103. [IIO_MOD_LIGHT_UVA] = "uva",
  104. [IIO_MOD_LIGHT_UVB] = "uvb",
  105. [IIO_MOD_LIGHT_DUV] = "duv",
  106. [IIO_MOD_QUATERNION] = "quaternion",
  107. [IIO_MOD_TEMP_AMBIENT] = "ambient",
  108. [IIO_MOD_TEMP_OBJECT] = "object",
  109. [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
  110. [IIO_MOD_NORTH_TRUE] = "from_north_true",
  111. [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
  112. [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
  113. [IIO_MOD_RUNNING] = "running",
  114. [IIO_MOD_JOGGING] = "jogging",
  115. [IIO_MOD_WALKING] = "walking",
  116. [IIO_MOD_STILL] = "still",
  117. [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
  118. [IIO_MOD_I] = "i",
  119. [IIO_MOD_Q] = "q",
  120. [IIO_MOD_CO2] = "co2",
  121. [IIO_MOD_ETHANOL] = "ethanol",
  122. [IIO_MOD_H2] = "h2",
  123. [IIO_MOD_VOC] = "voc",
  124. [IIO_MOD_PM1] = "pm1",
  125. [IIO_MOD_PM2P5] = "pm2p5",
  126. [IIO_MOD_PM4] = "pm4",
  127. [IIO_MOD_PM10] = "pm10",
  128. [IIO_MOD_O2] = "o2",
  129. [IIO_MOD_LINEAR_X] = "linear_x",
  130. [IIO_MOD_LINEAR_Y] = "linear_y",
  131. [IIO_MOD_LINEAR_Z] = "linear_z",
  132. [IIO_MOD_PITCH] = "pitch",
  133. [IIO_MOD_YAW] = "yaw",
  134. [IIO_MOD_ROLL] = "roll",
  135. };
  136. static bool event_is_known(struct iio_event_data *event)
  137. {
  138. enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
  139. enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
  140. enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
  141. enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
  142. switch (type) {
  143. case IIO_VOLTAGE:
  144. case IIO_CURRENT:
  145. case IIO_POWER:
  146. case IIO_ACCEL:
  147. case IIO_ANGL_VEL:
  148. case IIO_MAGN:
  149. case IIO_LIGHT:
  150. case IIO_INTENSITY:
  151. case IIO_PROXIMITY:
  152. case IIO_TEMP:
  153. case IIO_INCLI:
  154. case IIO_ROT:
  155. case IIO_ANGL:
  156. case IIO_TIMESTAMP:
  157. case IIO_CAPACITANCE:
  158. case IIO_ALTVOLTAGE:
  159. case IIO_CCT:
  160. case IIO_PRESSURE:
  161. case IIO_HUMIDITYRELATIVE:
  162. case IIO_ACTIVITY:
  163. case IIO_STEPS:
  164. case IIO_ENERGY:
  165. case IIO_DISTANCE:
  166. case IIO_VELOCITY:
  167. case IIO_CONCENTRATION:
  168. case IIO_RESISTANCE:
  169. case IIO_PH:
  170. case IIO_UVINDEX:
  171. case IIO_GRAVITY:
  172. case IIO_POSITIONRELATIVE:
  173. case IIO_PHASE:
  174. case IIO_MASSCONCENTRATION:
  175. case IIO_DELTA_ANGL:
  176. case IIO_DELTA_VELOCITY:
  177. case IIO_COLORTEMP:
  178. case IIO_CHROMATICITY:
  179. break;
  180. default:
  181. return false;
  182. }
  183. switch (mod) {
  184. case IIO_NO_MOD:
  185. case IIO_MOD_X:
  186. case IIO_MOD_Y:
  187. case IIO_MOD_Z:
  188. case IIO_MOD_X_AND_Y:
  189. case IIO_MOD_X_AND_Z:
  190. case IIO_MOD_Y_AND_Z:
  191. case IIO_MOD_X_AND_Y_AND_Z:
  192. case IIO_MOD_X_OR_Y:
  193. case IIO_MOD_X_OR_Z:
  194. case IIO_MOD_Y_OR_Z:
  195. case IIO_MOD_X_OR_Y_OR_Z:
  196. case IIO_MOD_LIGHT_BOTH:
  197. case IIO_MOD_LIGHT_IR:
  198. case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
  199. case IIO_MOD_SUM_SQUARED_X_Y_Z:
  200. case IIO_MOD_LIGHT_CLEAR:
  201. case IIO_MOD_LIGHT_RED:
  202. case IIO_MOD_LIGHT_GREEN:
  203. case IIO_MOD_LIGHT_BLUE:
  204. case IIO_MOD_LIGHT_UV:
  205. case IIO_MOD_LIGHT_DUV:
  206. case IIO_MOD_QUATERNION:
  207. case IIO_MOD_TEMP_AMBIENT:
  208. case IIO_MOD_TEMP_OBJECT:
  209. case IIO_MOD_NORTH_MAGN:
  210. case IIO_MOD_NORTH_TRUE:
  211. case IIO_MOD_NORTH_MAGN_TILT_COMP:
  212. case IIO_MOD_NORTH_TRUE_TILT_COMP:
  213. case IIO_MOD_RUNNING:
  214. case IIO_MOD_JOGGING:
  215. case IIO_MOD_WALKING:
  216. case IIO_MOD_STILL:
  217. case IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z:
  218. case IIO_MOD_I:
  219. case IIO_MOD_Q:
  220. case IIO_MOD_CO2:
  221. case IIO_MOD_ETHANOL:
  222. case IIO_MOD_H2:
  223. case IIO_MOD_VOC:
  224. case IIO_MOD_PM1:
  225. case IIO_MOD_PM2P5:
  226. case IIO_MOD_PM4:
  227. case IIO_MOD_PM10:
  228. case IIO_MOD_O2:
  229. break;
  230. default:
  231. return false;
  232. }
  233. switch (ev_type) {
  234. case IIO_EV_TYPE_THRESH:
  235. case IIO_EV_TYPE_MAG:
  236. case IIO_EV_TYPE_ROC:
  237. case IIO_EV_TYPE_THRESH_ADAPTIVE:
  238. case IIO_EV_TYPE_MAG_ADAPTIVE:
  239. case IIO_EV_TYPE_CHANGE:
  240. case IIO_EV_TYPE_GESTURE:
  241. break;
  242. default:
  243. return false;
  244. }
  245. switch (dir) {
  246. case IIO_EV_DIR_EITHER:
  247. case IIO_EV_DIR_RISING:
  248. case IIO_EV_DIR_FALLING:
  249. case IIO_EV_DIR_SINGLETAP:
  250. case IIO_EV_DIR_DOUBLETAP:
  251. case IIO_EV_DIR_NONE:
  252. break;
  253. default:
  254. return false;
  255. }
  256. return true;
  257. }
  258. static void print_event(struct iio_event_data *event)
  259. {
  260. enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
  261. enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
  262. enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
  263. enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
  264. int chan = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
  265. int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
  266. bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
  267. if (!event_is_known(event)) {
  268. fprintf(stderr, "Unknown event: time: %lld, id: %llx\n",
  269. event->timestamp, event->id);
  270. return;
  271. }
  272. printf("Event: time: %lld, type: %s", event->timestamp,
  273. iio_chan_type_name_spec[type]);
  274. if (mod != IIO_NO_MOD)
  275. printf("(%s)", iio_modifier_names[mod]);
  276. if (chan >= 0) {
  277. printf(", channel: %d", chan);
  278. if (diff && chan2 >= 0)
  279. printf("-%d", chan2);
  280. }
  281. printf(", evtype: %s", iio_ev_type_text[ev_type]);
  282. if (dir != IIO_EV_DIR_NONE)
  283. printf(", direction: %s", iio_ev_dir_text[dir]);
  284. printf("\n");
  285. fflush(stdout);
  286. }
  287. /* Enable or disable events in sysfs if the knob is available */
  288. static void enable_events(char *dev_dir, int enable)
  289. {
  290. const struct dirent *ent;
  291. char evdir[256];
  292. int ret;
  293. DIR *dp;
  294. snprintf(evdir, sizeof(evdir), FORMAT_EVENTS_DIR, dev_dir);
  295. evdir[sizeof(evdir)-1] = '\0';
  296. dp = opendir(evdir);
  297. if (!dp) {
  298. fprintf(stderr, "Enabling/disabling events: can't open %s\n",
  299. evdir);
  300. return;
  301. }
  302. while (ent = readdir(dp), ent) {
  303. if (iioutils_check_suffix(ent->d_name, "_en")) {
  304. printf("%sabling: %s\n",
  305. enable ? "En" : "Dis",
  306. ent->d_name);
  307. ret = write_sysfs_int(ent->d_name, evdir,
  308. enable);
  309. if (ret < 0)
  310. fprintf(stderr, "Failed to enable/disable %s\n",
  311. ent->d_name);
  312. }
  313. }
  314. if (closedir(dp) == -1) {
  315. perror("Enabling/disabling channels: "
  316. "Failed to close directory");
  317. return;
  318. }
  319. }
  320. int main(int argc, char **argv)
  321. {
  322. struct iio_event_data event;
  323. const char *device_name;
  324. char *dev_dir_name = NULL;
  325. char *chrdev_name;
  326. int ret;
  327. int dev_num;
  328. int fd, event_fd;
  329. bool all_events = false;
  330. if (argc == 2) {
  331. device_name = argv[1];
  332. } else if (argc == 3) {
  333. device_name = argv[2];
  334. if (!strcmp(argv[1], "-a"))
  335. all_events = true;
  336. } else {
  337. fprintf(stderr,
  338. "Usage: iio_event_monitor [options] <device_name>\n"
  339. "Listen and display events from IIO devices\n"
  340. " -a Auto-activate all available events\n");
  341. return -1;
  342. }
  343. dev_num = find_type_by_name(device_name, "iio:device");
  344. if (dev_num >= 0) {
  345. printf("Found IIO device with name %s with device number %d\n",
  346. device_name, dev_num);
  347. ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
  348. if (ret < 0)
  349. return -ENOMEM;
  350. /* Look up sysfs dir as well if we can */
  351. ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
  352. if (ret < 0)
  353. return -ENOMEM;
  354. } else {
  355. /*
  356. * If we can't find an IIO device by name assume device_name is
  357. * an IIO chrdev
  358. */
  359. chrdev_name = strdup(device_name);
  360. if (!chrdev_name)
  361. return -ENOMEM;
  362. }
  363. if (all_events && dev_dir_name)
  364. enable_events(dev_dir_name, 1);
  365. fd = open(chrdev_name, 0);
  366. if (fd == -1) {
  367. ret = -errno;
  368. fprintf(stderr, "Failed to open %s\n", chrdev_name);
  369. goto error_free_chrdev_name;
  370. }
  371. ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
  372. if (ret == -1 || event_fd == -1) {
  373. ret = -errno;
  374. if (ret == -ENODEV)
  375. fprintf(stderr,
  376. "This device does not support events\n");
  377. else
  378. fprintf(stderr, "Failed to retrieve event fd\n");
  379. if (close(fd) == -1)
  380. perror("Failed to close character device file");
  381. goto error_free_chrdev_name;
  382. }
  383. if (close(fd) == -1) {
  384. ret = -errno;
  385. goto error_free_chrdev_name;
  386. }
  387. while (true) {
  388. ret = read(event_fd, &event, sizeof(event));
  389. if (ret == -1) {
  390. if (errno == EAGAIN) {
  391. fprintf(stderr, "nothing available\n");
  392. continue;
  393. } else {
  394. ret = -errno;
  395. perror("Failed to read event from device");
  396. break;
  397. }
  398. }
  399. if (ret != sizeof(event)) {
  400. fprintf(stderr, "Reading event failed!\n");
  401. ret = -EIO;
  402. break;
  403. }
  404. print_event(&event);
  405. }
  406. if (close(event_fd) == -1)
  407. perror("Failed to close event file");
  408. error_free_chrdev_name:
  409. /* Disable events after use */
  410. if (all_events && dev_dir_name)
  411. enable_events(dev_dir_name, 0);
  412. free(chrdev_name);
  413. return ret;
  414. }