PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/iio/iio_event_monitor.c

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