2019-06-04 11:11:33 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2012-02-13 13:25:34 +04:00
/* Industrialio event test code.
*
* Copyright ( c ) 2011 - 2012 Lars - Peter Clausen < lars @ metafoo . de >
*
* This program is primarily intended as an example application .
* Reads the current buffer setup from sysfs and starts a short capture
* from the specified device , pretty printing the result after appropriate
* conversion .
*
* Usage :
* iio_event_monitor < device_name >
*/
# include <unistd.h>
2015-02-26 11:49:24 +03:00
# include <stdlib.h>
2021-03-19 16:53:01 +03:00
# include <dirent.h>
2012-02-13 13:25:34 +04:00
# include <stdbool.h>
# include <stdio.h>
# include <errno.h>
# include <string.h>
# include <poll.h>
# include <fcntl.h>
# include <sys/ioctl.h>
# include "iio_utils.h"
2012-04-25 18:54:58 +04:00
# include <linux/iio/events.h>
2015-02-26 11:49:23 +03:00
# include <linux/iio/types.h>
2012-02-13 13:25:34 +04:00
static const char * const iio_chan_type_name_spec [ ] = {
[ IIO_VOLTAGE ] = " voltage " ,
[ IIO_CURRENT ] = " current " ,
[ IIO_POWER ] = " power " ,
[ IIO_ACCEL ] = " accel " ,
[ IIO_ANGL_VEL ] = " anglvel " ,
[ IIO_MAGN ] = " magn " ,
[ IIO_LIGHT ] = " illuminance " ,
[ IIO_INTENSITY ] = " intensity " ,
[ IIO_PROXIMITY ] = " proximity " ,
[ IIO_TEMP ] = " temp " ,
[ IIO_INCLI ] = " incli " ,
[ IIO_ROT ] = " rot " ,
[ IIO_ANGL ] = " angl " ,
[ IIO_TIMESTAMP ] = " timestamp " ,
[ IIO_CAPACITANCE ] = " capacitance " ,
2012-06-15 21:25:26 +04:00
[ IIO_ALTVOLTAGE ] = " altvoltage " ,
2014-12-06 09:00:00 +03:00
[ IIO_CCT ] = " cct " ,
[ IIO_PRESSURE ] = " pressure " ,
[ IIO_HUMIDITYRELATIVE ] = " humidityrelative " ,
iio: event_monitor: Add support for new channel types
We have the following testing scenario:
$ insmod iio_dummy_evgen.ko
$ insmod iio_dummy.ko
./iio_event_monitor /dev/iio:device0
Event: time: 1412786467971335337, type: activity(running), channel: 0,
evtype: thresh, direction: rising
Event: time: 1412786530792974091, type: activity(walking), channel: 0,
evtype: thresh, direction: falling
Event: time: 1412764319184761765, type: steps, channel: 0, evtype: instance
$ echo 1 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 2 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 3 > /sys/bus/iio/devices/iio_evgen/poke_ev0
Signed-off-by: Irina Tirdea <irina.tirdea@intel.com>
Signed-off-by: Daniel Baluta <daniel.baluta@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2014-11-10 15:45:35 +03:00
[ IIO_ACTIVITY ] = " activity " ,
[ IIO_STEPS ] = " steps " ,
2015-06-21 00:52:31 +03:00
[ IIO_ENERGY ] = " energy " ,
[ IIO_DISTANCE ] = " distance " ,
[ IIO_VELOCITY ] = " velocity " ,
2016-03-16 00:54:51 +03:00
[ IIO_CONCENTRATION ] = " concentration " ,
[ IIO_RESISTANCE ] = " resistance " ,
[ IIO_PH ] = " ph " ,
2016-03-20 18:20:23 +03:00
[ IIO_UVINDEX ] = " uvindex " ,
2017-01-05 13:24:03 +03:00
[ IIO_GRAVITY ] = " gravity " ,
2018-05-22 10:52:32 +03:00
[ IIO_POSITIONRELATIVE ] = " positionrelative " ,
2018-07-20 20:34:25 +03:00
[ IIO_PHASE ] = " phase " ,
2018-12-14 21:28:01 +03:00
[ IIO_MASSCONCENTRATION ] = " massconcentration " ,
2012-02-13 13:25:34 +04:00
} ;
static const char * const iio_ev_type_text [ ] = {
[ IIO_EV_TYPE_THRESH ] = " thresh " ,
[ IIO_EV_TYPE_MAG ] = " mag " ,
[ IIO_EV_TYPE_ROC ] = " roc " ,
[ IIO_EV_TYPE_THRESH_ADAPTIVE ] = " thresh_adaptive " ,
[ IIO_EV_TYPE_MAG_ADAPTIVE ] = " mag_adaptive " ,
2015-01-11 22:10:11 +03:00
[ IIO_EV_TYPE_CHANGE ] = " change " ,
2022-02-14 10:38:06 +03:00
[ IIO_EV_TYPE_MAG_REFERENCED ] = " mag_referenced " ,
2022-08-31 09:31:16 +03:00
[ IIO_EV_TYPE_GESTURE ] = " gesture " ,
2012-02-13 13:25:34 +04:00
} ;
static const char * const iio_ev_dir_text [ ] = {
[ IIO_EV_DIR_EITHER ] = " either " ,
[ IIO_EV_DIR_RISING ] = " rising " ,
2022-08-31 09:31:16 +03:00
[ IIO_EV_DIR_FALLING ] = " falling " ,
[ IIO_EV_DIR_SINGLETAP ] = " singletap " ,
[ IIO_EV_DIR_DOUBLETAP ] = " doubletap " ,
2012-02-13 13:25:34 +04:00
} ;
static const char * const iio_modifier_names [ ] = {
[ IIO_MOD_X ] = " x " ,
[ IIO_MOD_Y ] = " y " ,
[ IIO_MOD_Z ] = " z " ,
2014-10-22 17:50:17 +04:00
[ IIO_MOD_X_AND_Y ] = " x&y " ,
[ IIO_MOD_X_AND_Z ] = " x&z " ,
[ IIO_MOD_Y_AND_Z ] = " y&z " ,
[ IIO_MOD_X_AND_Y_AND_Z ] = " x&y&z " ,
[ IIO_MOD_X_OR_Y ] = " x|y " ,
[ IIO_MOD_X_OR_Z ] = " x|z " ,
[ IIO_MOD_Y_OR_Z ] = " y|z " ,
[ IIO_MOD_X_OR_Y_OR_Z ] = " x|y|z " ,
2012-02-13 13:25:34 +04:00
[ IIO_MOD_LIGHT_BOTH ] = " both " ,
[ IIO_MOD_LIGHT_IR ] = " ir " ,
2012-07-01 14:20:13 +04:00
[ IIO_MOD_ROOT_SUM_SQUARED_X_Y ] = " sqrt(x^2+y^2) " ,
[ IIO_MOD_SUM_SQUARED_X_Y_Z ] = " x^2+y^2+z^2 " ,
[ IIO_MOD_LIGHT_CLEAR ] = " clear " ,
[ IIO_MOD_LIGHT_RED ] = " red " ,
[ IIO_MOD_LIGHT_GREEN ] = " green " ,
[ IIO_MOD_LIGHT_BLUE ] = " blue " ,
2016-03-20 18:20:22 +03:00
[ IIO_MOD_LIGHT_UV ] = " uv " ,
2018-07-19 23:26:24 +03:00
[ IIO_MOD_LIGHT_DUV ] = " duv " ,
2014-10-22 17:50:17 +04:00
[ IIO_MOD_QUATERNION ] = " quaternion " ,
[ IIO_MOD_TEMP_AMBIENT ] = " ambient " ,
[ IIO_MOD_TEMP_OBJECT ] = " object " ,
[ IIO_MOD_NORTH_MAGN ] = " from_north_magnetic " ,
[ IIO_MOD_NORTH_TRUE ] = " from_north_true " ,
[ IIO_MOD_NORTH_MAGN_TILT_COMP ] = " from_north_magnetic_tilt_comp " ,
[ IIO_MOD_NORTH_TRUE_TILT_COMP ] = " from_north_true_tilt_comp " ,
iio: event_monitor: Add support for new channel types
We have the following testing scenario:
$ insmod iio_dummy_evgen.ko
$ insmod iio_dummy.ko
./iio_event_monitor /dev/iio:device0
Event: time: 1412786467971335337, type: activity(running), channel: 0,
evtype: thresh, direction: rising
Event: time: 1412786530792974091, type: activity(walking), channel: 0,
evtype: thresh, direction: falling
Event: time: 1412764319184761765, type: steps, channel: 0, evtype: instance
$ echo 1 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 2 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 3 > /sys/bus/iio/devices/iio_evgen/poke_ev0
Signed-off-by: Irina Tirdea <irina.tirdea@intel.com>
Signed-off-by: Daniel Baluta <daniel.baluta@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2014-11-10 15:45:35 +03:00
[ IIO_MOD_RUNNING ] = " running " ,
[ IIO_MOD_JOGGING ] = " jogging " ,
[ IIO_MOD_WALKING ] = " walking " ,
[ IIO_MOD_STILL ] = " still " ,
2015-06-21 00:52:31 +03:00
[ IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z ] = " sqrt(x^2+y^2+z^2) " ,
2016-03-16 00:54:51 +03:00
[ IIO_MOD_I ] = " i " ,
[ IIO_MOD_Q ] = " q " ,
[ IIO_MOD_CO2 ] = " co2 " ,
2018-12-13 17:43:22 +03:00
[ IIO_MOD_ETHANOL ] = " ethanol " ,
[ IIO_MOD_H2 ] = " h2 " ,
2016-03-16 00:54:51 +03:00
[ IIO_MOD_VOC ] = " voc " ,
2018-12-14 21:28:01 +03:00
[ IIO_MOD_PM1 ] = " pm1 " ,
[ IIO_MOD_PM2P5 ] = " pm2p5 " ,
[ IIO_MOD_PM4 ] = " pm4 " ,
[ IIO_MOD_PM10 ] = " pm10 " ,
2020-07-23 09:29:43 +03:00
[ IIO_MOD_O2 ] = " o2 " ,
2022-09-07 16:21:54 +03:00
[ IIO_MOD_LINEAR_X ] = " linear_x " ,
[ IIO_MOD_LINEAR_Y ] = " linear_y " ,
[ IIO_MOD_LINEAR_Z ] = " linear_z " ,
2022-09-07 16:21:57 +03:00
[ IIO_MOD_PITCH ] = " pitch " ,
[ IIO_MOD_YAW ] = " yaw " ,
[ IIO_MOD_ROLL ] = " roll " ,
2012-02-13 13:25:34 +04:00
} ;
static bool event_is_known ( struct iio_event_data * event )
{
enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE ( event - > id ) ;
enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER ( event - > id ) ;
enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE ( event - > id ) ;
enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR ( event - > id ) ;
switch ( type ) {
case IIO_VOLTAGE :
case IIO_CURRENT :
case IIO_POWER :
case IIO_ACCEL :
case IIO_ANGL_VEL :
case IIO_MAGN :
case IIO_LIGHT :
case IIO_INTENSITY :
case IIO_PROXIMITY :
case IIO_TEMP :
case IIO_INCLI :
case IIO_ROT :
case IIO_ANGL :
case IIO_TIMESTAMP :
case IIO_CAPACITANCE :
2012-06-15 21:25:26 +04:00
case IIO_ALTVOLTAGE :
2014-12-06 09:00:00 +03:00
case IIO_CCT :
case IIO_PRESSURE :
case IIO_HUMIDITYRELATIVE :
iio: event_monitor: Add support for new channel types
We have the following testing scenario:
$ insmod iio_dummy_evgen.ko
$ insmod iio_dummy.ko
./iio_event_monitor /dev/iio:device0
Event: time: 1412786467971335337, type: activity(running), channel: 0,
evtype: thresh, direction: rising
Event: time: 1412786530792974091, type: activity(walking), channel: 0,
evtype: thresh, direction: falling
Event: time: 1412764319184761765, type: steps, channel: 0, evtype: instance
$ echo 1 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 2 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 3 > /sys/bus/iio/devices/iio_evgen/poke_ev0
Signed-off-by: Irina Tirdea <irina.tirdea@intel.com>
Signed-off-by: Daniel Baluta <daniel.baluta@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2014-11-10 15:45:35 +03:00
case IIO_ACTIVITY :
case IIO_STEPS :
2015-06-21 00:52:31 +03:00
case IIO_ENERGY :
case IIO_DISTANCE :
case IIO_VELOCITY :
2016-03-16 00:54:51 +03:00
case IIO_CONCENTRATION :
case IIO_RESISTANCE :
case IIO_PH :
2016-03-20 18:20:23 +03:00
case IIO_UVINDEX :
2017-01-05 13:24:03 +03:00
case IIO_GRAVITY :
2018-05-22 10:52:32 +03:00
case IIO_POSITIONRELATIVE :
2018-07-20 20:34:25 +03:00
case IIO_PHASE :
2018-12-14 21:28:01 +03:00
case IIO_MASSCONCENTRATION :
2012-02-13 13:25:34 +04:00
break ;
default :
return false ;
}
switch ( mod ) {
case IIO_NO_MOD :
case IIO_MOD_X :
case IIO_MOD_Y :
case IIO_MOD_Z :
2014-10-22 17:50:17 +04:00
case IIO_MOD_X_AND_Y :
case IIO_MOD_X_AND_Z :
case IIO_MOD_Y_AND_Z :
case IIO_MOD_X_AND_Y_AND_Z :
case IIO_MOD_X_OR_Y :
case IIO_MOD_X_OR_Z :
case IIO_MOD_Y_OR_Z :
case IIO_MOD_X_OR_Y_OR_Z :
2012-02-13 13:25:34 +04:00
case IIO_MOD_LIGHT_BOTH :
case IIO_MOD_LIGHT_IR :
2012-07-01 14:20:13 +04:00
case IIO_MOD_ROOT_SUM_SQUARED_X_Y :
case IIO_MOD_SUM_SQUARED_X_Y_Z :
case IIO_MOD_LIGHT_CLEAR :
case IIO_MOD_LIGHT_RED :
case IIO_MOD_LIGHT_GREEN :
case IIO_MOD_LIGHT_BLUE :
2016-03-20 18:20:22 +03:00
case IIO_MOD_LIGHT_UV :
2018-07-19 23:26:24 +03:00
case IIO_MOD_LIGHT_DUV :
2014-10-22 17:50:17 +04:00
case IIO_MOD_QUATERNION :
case IIO_MOD_TEMP_AMBIENT :
case IIO_MOD_TEMP_OBJECT :
case IIO_MOD_NORTH_MAGN :
case IIO_MOD_NORTH_TRUE :
case IIO_MOD_NORTH_MAGN_TILT_COMP :
case IIO_MOD_NORTH_TRUE_TILT_COMP :
iio: event_monitor: Add support for new channel types
We have the following testing scenario:
$ insmod iio_dummy_evgen.ko
$ insmod iio_dummy.ko
./iio_event_monitor /dev/iio:device0
Event: time: 1412786467971335337, type: activity(running), channel: 0,
evtype: thresh, direction: rising
Event: time: 1412786530792974091, type: activity(walking), channel: 0,
evtype: thresh, direction: falling
Event: time: 1412764319184761765, type: steps, channel: 0, evtype: instance
$ echo 1 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 2 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 3 > /sys/bus/iio/devices/iio_evgen/poke_ev0
Signed-off-by: Irina Tirdea <irina.tirdea@intel.com>
Signed-off-by: Daniel Baluta <daniel.baluta@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2014-11-10 15:45:35 +03:00
case IIO_MOD_RUNNING :
case IIO_MOD_JOGGING :
case IIO_MOD_WALKING :
case IIO_MOD_STILL :
2015-06-21 00:52:31 +03:00
case IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z :
2016-03-16 00:54:51 +03:00
case IIO_MOD_I :
case IIO_MOD_Q :
case IIO_MOD_CO2 :
2018-12-13 17:43:22 +03:00
case IIO_MOD_ETHANOL :
case IIO_MOD_H2 :
2016-03-16 00:54:51 +03:00
case IIO_MOD_VOC :
2018-12-14 21:28:01 +03:00
case IIO_MOD_PM1 :
case IIO_MOD_PM2P5 :
case IIO_MOD_PM4 :
case IIO_MOD_PM10 :
2020-07-23 09:29:43 +03:00
case IIO_MOD_O2 :
2012-02-13 13:25:34 +04:00
break ;
default :
return false ;
}
switch ( ev_type ) {
case IIO_EV_TYPE_THRESH :
case IIO_EV_TYPE_MAG :
case IIO_EV_TYPE_ROC :
case IIO_EV_TYPE_THRESH_ADAPTIVE :
case IIO_EV_TYPE_MAG_ADAPTIVE :
2015-01-11 22:10:11 +03:00
case IIO_EV_TYPE_CHANGE :
2022-08-31 09:31:16 +03:00
case IIO_EV_TYPE_GESTURE :
2012-02-13 13:25:34 +04:00
break ;
default :
return false ;
}
switch ( dir ) {
case IIO_EV_DIR_EITHER :
case IIO_EV_DIR_RISING :
case IIO_EV_DIR_FALLING :
2022-08-31 09:31:16 +03:00
case IIO_EV_DIR_SINGLETAP :
case IIO_EV_DIR_DOUBLETAP :
iio: event_monitor: Add support for new channel types
We have the following testing scenario:
$ insmod iio_dummy_evgen.ko
$ insmod iio_dummy.ko
./iio_event_monitor /dev/iio:device0
Event: time: 1412786467971335337, type: activity(running), channel: 0,
evtype: thresh, direction: rising
Event: time: 1412786530792974091, type: activity(walking), channel: 0,
evtype: thresh, direction: falling
Event: time: 1412764319184761765, type: steps, channel: 0, evtype: instance
$ echo 1 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 2 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 3 > /sys/bus/iio/devices/iio_evgen/poke_ev0
Signed-off-by: Irina Tirdea <irina.tirdea@intel.com>
Signed-off-by: Daniel Baluta <daniel.baluta@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2014-11-10 15:45:35 +03:00
case IIO_EV_DIR_NONE :
2012-02-13 13:25:34 +04:00
break ;
default :
return false ;
}
return true ;
}
static void print_event ( struct iio_event_data * event )
{
enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE ( event - > id ) ;
enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER ( event - > id ) ;
enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE ( event - > id ) ;
enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR ( event - > id ) ;
int chan = IIO_EVENT_CODE_EXTRACT_CHAN ( event - > id ) ;
int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2 ( event - > id ) ;
bool diff = IIO_EVENT_CODE_EXTRACT_DIFF ( event - > id ) ;
if ( ! event_is_known ( event ) ) {
2015-07-17 18:43:42 +03:00
fprintf ( stderr , " Unknown event: time: %lld, id: %llx \n " ,
event - > timestamp , event - > id ) ;
2015-06-10 22:51:20 +03:00
2012-02-13 13:25:34 +04:00
return ;
}
2015-05-31 15:40:18 +03:00
printf ( " Event: time: %lld, type: %s " , event - > timestamp ,
iio_chan_type_name_spec [ type ] ) ;
2012-02-13 13:25:34 +04:00
2015-05-31 15:40:18 +03:00
if ( mod ! = IIO_NO_MOD )
printf ( " (%s) " , iio_modifier_names [ mod ] ) ;
2012-02-13 13:25:34 +04:00
2015-05-31 15:40:18 +03:00
if ( chan > = 0 ) {
printf ( " , channel: %d " , chan ) ;
if ( diff & & chan2 > = 0 )
printf ( " -%d " , chan2 ) ;
}
2012-02-13 13:25:34 +04:00
2015-05-31 15:40:18 +03:00
printf ( " , evtype: %s " , iio_ev_type_text [ ev_type ] ) ;
iio: event_monitor: Add support for new channel types
We have the following testing scenario:
$ insmod iio_dummy_evgen.ko
$ insmod iio_dummy.ko
./iio_event_monitor /dev/iio:device0
Event: time: 1412786467971335337, type: activity(running), channel: 0,
evtype: thresh, direction: rising
Event: time: 1412786530792974091, type: activity(walking), channel: 0,
evtype: thresh, direction: falling
Event: time: 1412764319184761765, type: steps, channel: 0, evtype: instance
$ echo 1 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 2 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 3 > /sys/bus/iio/devices/iio_evgen/poke_ev0
Signed-off-by: Irina Tirdea <irina.tirdea@intel.com>
Signed-off-by: Daniel Baluta <daniel.baluta@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2014-11-10 15:45:35 +03:00
if ( dir ! = IIO_EV_DIR_NONE )
printf ( " , direction: %s " , iio_ev_dir_text [ dir ] ) ;
2015-06-10 22:51:20 +03:00
iio: event_monitor: Add support for new channel types
We have the following testing scenario:
$ insmod iio_dummy_evgen.ko
$ insmod iio_dummy.ko
./iio_event_monitor /dev/iio:device0
Event: time: 1412786467971335337, type: activity(running), channel: 0,
evtype: thresh, direction: rising
Event: time: 1412786530792974091, type: activity(walking), channel: 0,
evtype: thresh, direction: falling
Event: time: 1412764319184761765, type: steps, channel: 0, evtype: instance
$ echo 1 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 2 > /sys/bus/iio/devices/iio_evgen/poke_ev0
$ echo 3 > /sys/bus/iio/devices/iio_evgen/poke_ev0
Signed-off-by: Irina Tirdea <irina.tirdea@intel.com>
Signed-off-by: Daniel Baluta <daniel.baluta@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2014-11-10 15:45:35 +03:00
printf ( " \n " ) ;
2021-12-09 00:48:08 +03:00
fflush ( stdout ) ;
2012-02-13 13:25:34 +04:00
}
2021-03-19 16:53:01 +03:00
/* Enable or disable events in sysfs if the knob is available */
static void enable_events ( char * dev_dir , int enable )
{
const struct dirent * ent ;
char evdir [ 256 ] ;
int ret ;
DIR * dp ;
snprintf ( evdir , sizeof ( evdir ) , FORMAT_EVENTS_DIR , dev_dir ) ;
evdir [ sizeof ( evdir ) - 1 ] = ' \0 ' ;
dp = opendir ( evdir ) ;
if ( ! dp ) {
fprintf ( stderr , " Enabling/disabling events: can't open %s \n " ,
evdir ) ;
return ;
}
while ( ent = readdir ( dp ) , ent ) {
if ( iioutils_check_suffix ( ent - > d_name , " _en " ) ) {
printf ( " %sabling: %s \n " ,
enable ? " En " : " Dis " ,
ent - > d_name ) ;
ret = write_sysfs_int ( ent - > d_name , evdir ,
enable ) ;
if ( ret < 0 )
fprintf ( stderr , " Failed to enable/disable %s \n " ,
ent - > d_name ) ;
}
}
if ( closedir ( dp ) = = - 1 ) {
perror ( " Enabling/disabling channels: "
" Failed to close directory " ) ;
return ;
}
}
2012-02-13 13:25:34 +04:00
int main ( int argc , char * * argv )
{
struct iio_event_data event ;
const char * device_name ;
2021-03-19 16:53:01 +03:00
char * dev_dir_name = NULL ;
2012-02-13 13:25:34 +04:00
char * chrdev_name ;
int ret ;
int dev_num ;
int fd , event_fd ;
2021-03-19 16:53:01 +03:00
bool all_events = false ;
if ( argc = = 2 ) {
device_name = argv [ 1 ] ;
} else if ( argc = = 3 ) {
device_name = argv [ 2 ] ;
if ( ! strcmp ( argv [ 1 ] , " -a " ) )
all_events = true ;
} else {
fprintf ( stderr ,
" Usage: iio_event_monitor [options] <device_name> \n "
" Listen and display events from IIO devices \n "
" -a Auto-activate all available events \n " ) ;
2012-02-13 13:25:34 +04:00
return - 1 ;
}
dev_num = find_type_by_name ( device_name , " iio:device " ) ;
if ( dev_num > = 0 ) {
printf ( " Found IIO device with name %s with device number %d \n " ,
2015-06-10 22:51:20 +03:00
device_name , dev_num ) ;
2012-02-13 13:25:34 +04:00
ret = asprintf ( & chrdev_name , " /dev/iio:device%d " , dev_num ) ;
2015-07-13 16:17:47 +03:00
if ( ret < 0 )
2015-05-31 15:40:17 +03:00
return - ENOMEM ;
2021-03-19 16:53:01 +03:00
/* Look up sysfs dir as well if we can */
ret = asprintf ( & dev_dir_name , " %siio:device%d " , iio_dir , dev_num ) ;
if ( ret < 0 )
return - ENOMEM ;
2012-02-13 13:25:34 +04:00
} else {
2015-06-10 22:51:20 +03:00
/*
* If we can ' t find an IIO device by name assume device_name is
* an IIO chrdev
*/
2012-02-13 13:25:34 +04:00
chrdev_name = strdup ( device_name ) ;
2015-05-31 15:40:02 +03:00
if ( ! chrdev_name )
return - ENOMEM ;
2012-02-13 13:25:34 +04:00
}
2021-03-19 16:53:01 +03:00
if ( all_events & & dev_dir_name )
enable_events ( dev_dir_name , 1 ) ;
2012-02-13 13:25:34 +04:00
fd = open ( chrdev_name , 0 ) ;
if ( fd = = - 1 ) {
ret = - errno ;
2015-07-17 18:43:42 +03:00
fprintf ( stderr , " Failed to open %s \n " , chrdev_name ) ;
2012-02-13 13:25:34 +04:00
goto error_free_chrdev_name ;
}
ret = ioctl ( fd , IIO_GET_EVENT_FD_IOCTL , & event_fd ) ;
if ( ret = = - 1 | | event_fd = = - 1 ) {
ret = - errno ;
2015-08-11 15:34:38 +03:00
if ( ret = = - ENODEV )
fprintf ( stderr ,
" This device does not support events \n " ) ;
else
fprintf ( stderr , " Failed to retrieve event fd \n " ) ;
2015-05-31 15:39:49 +03:00
if ( close ( fd ) = = - 1 )
perror ( " Failed to close character device file " ) ;
2012-02-13 13:25:34 +04:00
goto error_free_chrdev_name ;
}
2015-05-31 15:40:05 +03:00
if ( close ( fd ) = = - 1 ) {
ret = - errno ;
goto error_free_chrdev_name ;
}
2015-05-31 15:39:49 +03:00
2012-02-13 13:25:34 +04:00
while ( true ) {
ret = read ( event_fd , & event , sizeof ( event ) ) ;
if ( ret = = - 1 ) {
if ( errno = = EAGAIN ) {
2015-07-17 18:43:42 +03:00
fprintf ( stderr , " nothing available \n " ) ;
2012-02-13 13:25:34 +04:00
continue ;
} else {
ret = - errno ;
2015-05-31 15:39:48 +03:00
perror ( " Failed to read event from device " ) ;
2012-02-13 13:25:34 +04:00
break ;
}
}
2015-06-10 22:51:19 +03:00
if ( ret ! = sizeof ( event ) ) {
2015-07-17 18:43:42 +03:00
fprintf ( stderr , " Reading event failed! \n " ) ;
2015-06-10 22:51:19 +03:00
ret = - EIO ;
break ;
}
2012-02-13 13:25:34 +04:00
print_event ( & event ) ;
}
2015-05-31 15:40:05 +03:00
if ( close ( event_fd ) = = - 1 )
perror ( " Failed to close event file " ) ;
2012-02-13 13:25:34 +04:00
error_free_chrdev_name :
2021-03-19 16:53:01 +03:00
/* Disable events after use */
if ( all_events & & dev_dir_name )
enable_events ( dev_dir_name , 0 ) ;
2012-02-13 13:25:34 +04:00
free ( chrdev_name ) ;
2015-05-31 15:40:17 +03:00
2012-02-13 13:25:34 +04:00
return ret ;
}