From 93e42e580d09229d32e9254bcfe6424e38b8fcaa Mon Sep 17 00:00:00 2001 From: Patrick Porlan Date: Mon, 7 Apr 2014 14:31:41 +0200 Subject: [PATCH] STPK-1429 Make the sensor enable/disable code path more robust Tests on the number of enabled sensors can be replaced by a check on the iio device fd status (having any sensor active on a iio device implies that the associated device was open). This simplifies the enable path somewhat. On the disable path we still need to check whether a regular or poll-mode sensor associated to the device remains in operation before actually closing the fd and setting it back to -1. Issue: STPK-1429 Change-Id: Ib3084d0075630d48ffb97bcdd01b880271b3192c Signed-off-by: Patrick Porlan --- control.c | 60 ++++++++++++++++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/control.c b/control.c index 7edd8db..4155432 100644 --- a/control.c +++ b/control.c @@ -325,10 +325,9 @@ int sensor_activate(int s, int enabled) */ dev_fd = device_fd[dev_num]; - switch (poll_sensors_per_dev[dev_num] + trig_sensors_per_dev[dev_num]) { - - case 0: - if (dev_fd != -1) { + if (!enabled) { + if (dev_fd != -1 && !poll_sensors_per_dev[dev_num] && + !trig_sensors_per_dev[dev_num]) { /* * Stop watching this fd. This should be a no-op * in case this fd was not in the poll set. @@ -338,44 +337,41 @@ int sensor_activate(int s, int enabled) close(dev_fd); device_fd[dev_num] = -1; } - return 0; + return 0; + } - case 1: - /* First enabled sensor on this iio device */ - sprintf(device_name, DEV_FILE_PATH, dev_num); - dev_fd = open(device_name, O_RDONLY | O_NONBLOCK); + if (dev_fd == -1) { + /* First enabled sensor on this iio device */ + sprintf(device_name, DEV_FILE_PATH, dev_num); + dev_fd = open(device_name, O_RDONLY | O_NONBLOCK); - device_fd[dev_num] = dev_fd; + device_fd[dev_num] = dev_fd; - if (dev_fd == -1) { - ALOGE("Could not open fd on %s (%s)\n", - device_name, strerror(errno)); - adjust_counters(s, 0); - return -1; - } + if (dev_fd == -1) { + ALOGE("Could not open fd on %s (%s)\n", + device_name, strerror(errno)); + adjust_counters(s, 0); + return -1; + } - ALOGV("Opened %s: fd=%d\n", device_name, dev_fd); - break; + ALOGV("Opened %s: fd=%d\n", device_name, dev_fd); - default: - break; - } + if (!is_poll_sensor) { - if (!is_poll_sensor && trig_sensors_per_dev[dev_num] == 1) { + /* Add this iio device fd to the set of watched fds */ + ev.events = EPOLLIN; + ev.data.u32 = dev_num; - /* Add this iio device fd to the set of watched fds */ - ev.events = EPOLLIN; - ev.data.u32 = dev_num; + ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, dev_fd, &ev); - ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, dev_fd, &ev); + if (ret == -1) { + ALOGE( "Failed adding %d to poll set (%s)\n", + dev_fd, strerror(errno)); + return -1; + } - if (ret == -1) { - ALOGE("Failed adding %d to poll set (%s)\n", dev_fd, - strerror(errno)); - return -1; + /* Note: poll-mode fds are not readable */ } - - /* Note: poll-mode fds are not readable */ } return 0; -- 2.11.0