PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/iio/iio_event_monitor.c

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