iio_event_monitor.c 11 KB

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