diff options
author | Constantin Musca <constantin.musca@intel.com> | 2016-02-03 00:09:09 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2016-02-03 00:09:09 +0000 |
commit | c7b07f8429f174944ea76cf4c1ed6152b1e08a29 (patch) | |
tree | a70e3511117d862b54abb12f7a0bdce9ba5d9af5 | |
parent | c1a7a7abe7c1959bea4230b54b789de2283ca999 (diff) | |
parent | 9178ce05cb05831817580fc11fb7b20bc2031319 (diff) | |
download | intel-c7b07f8429f174944ea76cf4c1ed6152b1e08a29.tar.gz |
sensors: add missing checks and fix the pollEvents comments
am: 9178ce05cb
* commit '9178ce05cb05831817580fc11fb7b20bc2031319':
sensors: add missing checks and fix the pollEvents comments
11 files changed, 41 insertions, 19 deletions
diff --git a/peripheral/sensors/mraa/AcquisitionThread.cpp b/peripheral/sensors/mraa/AcquisitionThread.cpp index 8adc9da..c1c9206 100644 --- a/peripheral/sensors/mraa/AcquisitionThread.cpp +++ b/peripheral/sensors/mraa/AcquisitionThread.cpp @@ -54,23 +54,27 @@ void* AcquisitionThread::acquisitionRoutine(void *param) { /* loop until the thread is canceled */ while(acquisitionThread->getWritePipeFd() != -1) { - /* get data from the sensor */ - sensor->pollEvents(&data, 1); + /* get one event from the sensor */ + if (sensor->pollEvents(&data, 1) != 1) { + ALOGE("%s: Sensor %d: Cannot read data", __func__, data.sensor); + goto exit; + } /* send the data over the pipe to the main thread */ rc = write(acquisitionThread->getWritePipeFd(), &data, sizeof(sensors_event_t)); if (rc != sizeof(sensors_event_t)) { - ALOGE("%s: not all data has been sent over the pipe", __func__); + ALOGE("%s: Sensor %d: Cannot write data to pipe", __func__, data.sensor); + goto exit; } if (acquisitionThread->getWritePipeFd() == -1) { + ALOGE("%s: Sensor %d: The write pipe file descriptor is invalid", __func__, data.sensor); goto exit; } timestamp += sensor->getDelay(); set_timestamp(&target_time, timestamp); pthread_cond_timedwait(&acquisitionThread->pthreadCond, &acquisitionThread->pthreadMutex, &target_time); - } exit: diff --git a/peripheral/sensors/mraa/Sensor.hpp b/peripheral/sensors/mraa/Sensor.hpp index f3a9d95..7fc98a2 100644 --- a/peripheral/sensors/mraa/Sensor.hpp +++ b/peripheral/sensors/mraa/Sensor.hpp @@ -67,7 +67,7 @@ class Sensor { * Poll for events * @param data where to store the events * @param count the number of events returned must be <= to the count - * @return 0 on success and a negative error number otherwise + * @return number of events returned in data on success and a negative error number otherwise */ virtual int pollEvents(sensors_event_t* data, int count) = 0; diff --git a/peripheral/sensors/mraa/SensorsHAL.cpp b/peripheral/sensors/mraa/SensorsHAL.cpp index 10348a9..94f768d 100644 --- a/peripheral/sensors/mraa/SensorsHAL.cpp +++ b/peripheral/sensors/mraa/SensorsHAL.cpp @@ -178,21 +178,39 @@ int SensorContext::setDelay(int handle, int64_t ns) { int SensorContext::pollEvents(sensors_event_t *data, int count) { int nfds, i; struct epoll_event ev[MAX_DEVICES]; - int returned_events = 0; + int returnedEvents = 0, sensorIndex = -1; /* return only when at least one event is available */ while(true) { nfds = epoll_wait(pollFd, ev, MAX_DEVICES, -1); - for(i = 0; i < nfds && returned_events < count; i++) { + if (nfds < 0) { + ALOGE("%s: epoll_wait returned an error: %d", __func__, errno); + return nfds; + } + + for(i = 0; i < nfds && returnedEvents < count; i++) { if (ev[i].events == EPOLLIN) { + sensorIndex = ev[i].data.u32; + if ((sensorIndex < 0) || (sensorIndex > sensorsNum)) { + ALOGE("%s: Invalid sensor index", __func__); + return -1; + } - if(sensors[ev[i].data.u32]->readOneEvent(data + returned_events)) { - returned_events++; + if (sensors[sensorIndex] == nullptr) { + ALOGE("%s: Sensor %d is not activated", __func__, sensorIndex); + return -1; + } + + if (sensors[sensorIndex]->readOneEvent(data + returnedEvents)) { + returnedEvents++; + } else { + ALOGE("%s: Cannot read event from sensor %d", __func__, sensorIndex); + return -1; } } } - if (returned_events > 0) { - return returned_events; + if (returnedEvents > 0) { + return returnedEvents; } } } diff --git a/peripheral/sensors/mraa/sensors/GroveLight.hpp b/peripheral/sensors/mraa/sensors/GroveLight.hpp index 8843058..401e834 100644 --- a/peripheral/sensors/mraa/sensors/GroveLight.hpp +++ b/peripheral/sensors/mraa/sensors/GroveLight.hpp @@ -46,7 +46,7 @@ class GroveLight : public Sensor, public upm::GroveLight { * Poll for events * @param data where to store the events * @param count the number of events returned must be <= to the count - * @return 0 on success and a negative error number otherwise + * @return number of events returned in data on success and a negative error number otherwise */ int pollEvents(sensors_event_t* data, int count) override; diff --git a/peripheral/sensors/mraa/sensors/GroveTemperature.hpp b/peripheral/sensors/mraa/sensors/GroveTemperature.hpp index 50f296b..a3df4db 100644 --- a/peripheral/sensors/mraa/sensors/GroveTemperature.hpp +++ b/peripheral/sensors/mraa/sensors/GroveTemperature.hpp @@ -46,7 +46,7 @@ class GroveTemperature : public Sensor, public upm::GroveTemp { * Poll for events * @param data where to store the events * @param count the number of events returned must be <= to the count - * @return 0 on success and a negative error number otherwise + * @return number of events returned in data on success and a negative error number otherwise */ int pollEvents(sensors_event_t* data, int count) override; diff --git a/peripheral/sensors/mraa/sensors/LSM303dAccelerometer.hpp b/peripheral/sensors/mraa/sensors/LSM303dAccelerometer.hpp index 9171799..42d7372 100644 --- a/peripheral/sensors/mraa/sensors/LSM303dAccelerometer.hpp +++ b/peripheral/sensors/mraa/sensors/LSM303dAccelerometer.hpp @@ -52,7 +52,7 @@ class LSM303dAccelerometer : public Sensor, public upm::LSM303d { * Poll for events * @param data where to store the events * @param count the number of events returned must be <= to the count - * @return 0 on success and a negative error number otherwise + * @return number of events returned in data on success and a negative error number otherwise */ int pollEvents(sensors_event_t* data, int count) override; diff --git a/peripheral/sensors/mraa/sensors/LSM303dOrientation.hpp b/peripheral/sensors/mraa/sensors/LSM303dOrientation.hpp index 99ba077..afc16cc 100644 --- a/peripheral/sensors/mraa/sensors/LSM303dOrientation.hpp +++ b/peripheral/sensors/mraa/sensors/LSM303dOrientation.hpp @@ -49,7 +49,7 @@ class LSM303dOrientation : public Sensor, public upm::LSM303d { * Poll for events * @param data where to store the events * @param count the number of events returned must be <= to the count - * @return 0 on success and a negative error number otherwise + * @return number of events returned in data on success and a negative error number otherwise */ int pollEvents(sensors_event_t* data, int count) override; diff --git a/peripheral/sensors/mraa/sensors/LSM9DS0Accelerometer.hpp b/peripheral/sensors/mraa/sensors/LSM9DS0Accelerometer.hpp index 31aaf10..4be9acd 100644 --- a/peripheral/sensors/mraa/sensors/LSM9DS0Accelerometer.hpp +++ b/peripheral/sensors/mraa/sensors/LSM9DS0Accelerometer.hpp @@ -48,7 +48,7 @@ class LSM9DS0Accelerometer : public Sensor, public upm::LSM9DS0 { * Poll for events * @param data where to store the events * @param count the number of events returned must be <= to the count - * @return 0 on success and a negative error number otherwise + * @return number of events returned in data on success and a negative error number otherwise */ int pollEvents(sensors_event_t* data, int count) override; diff --git a/peripheral/sensors/mraa/sensors/MMA7660Accelerometer.hpp b/peripheral/sensors/mraa/sensors/MMA7660Accelerometer.hpp index bfa3c27..5233868 100644 --- a/peripheral/sensors/mraa/sensors/MMA7660Accelerometer.hpp +++ b/peripheral/sensors/mraa/sensors/MMA7660Accelerometer.hpp @@ -54,7 +54,7 @@ class MMA7660Accelerometer : public Sensor, public upm::MMA7660 { * Poll for events * @param data where to store the events * @param count the number of events returned must be <= to the count - * @return 0 on success and a negative error number otherwise + * @return number of events returned in data on success and a negative error number otherwise */ int pollEvents(sensors_event_t* data, int count) override; diff --git a/peripheral/sensors/mraa/sensors/MPU9150Accelerometer.hpp b/peripheral/sensors/mraa/sensors/MPU9150Accelerometer.hpp index c1cc432..100eb6b 100644 --- a/peripheral/sensors/mraa/sensors/MPU9150Accelerometer.hpp +++ b/peripheral/sensors/mraa/sensors/MPU9150Accelerometer.hpp @@ -51,7 +51,7 @@ class MPU9150Accelerometer : public Sensor, public upm::MPU9150 { * Poll for events * @param data where to store the events * @param count the number of events returned must be <= to the count - * @return 0 on success and a negative error number otherwise + * @return number of events returned in data on success and a negative error number otherwise */ int pollEvents(sensors_event_t* data, int count) override; diff --git a/peripheral/sensors/mraa/sensors/ProximityGPIO.hpp b/peripheral/sensors/mraa/sensors/ProximityGPIO.hpp index 1ee0547..24935f7 100644 --- a/peripheral/sensors/mraa/sensors/ProximityGPIO.hpp +++ b/peripheral/sensors/mraa/sensors/ProximityGPIO.hpp @@ -46,7 +46,7 @@ class ProximityGPIO : public Sensor, public upm::GroveButton { * Poll for events * @param data where to store the events * @param count the number of events returned must be <= to the count - * @return 0 on success and a negative error number otherwise + * @return number of events returned in data on success and a negative error number otherwise */ int pollEvents(sensors_event_t* data, int count) override; |