iio_event_monitor.c 11 KB

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