OSDN Git Service

libsensors: correct read() usage
authorAndy Ross <andy.ross@windriver.com>
Thu, 26 Sep 2013 19:31:26 +0000 (12:31 -0700)
committerMattias Pettersson <mattias.pettersson@intel.com>
Wed, 4 Dec 2013 14:13:12 +0000 (15:13 +0100)
The readEvents() implementation would attempt to read buffer_len (two
by default) IIO events from the device, but would only ever process
one, leading to dropped events in the case of overflow.  It was also
not checking the return value from read and would push garbage data on
I/O errors (probably would not happen in regular usage, but IIO
devices are known to return -EBUSY or -EINVAL if there are other users
or if the sysfs parameters are set incorrectly).

Change-Id: I7f66f530c9fce0540f1596456b3ca191406bbd27
For: AXIA-3287
Signed-off-by: Andy Ross <andy.ross@windriver.com>
Reviewed-on: https://otc-android.intel.com/gerrit/22150
Reviewed-by: Daniel Leung <daniel.leung@intel.com>
Reviewed-by: Russell Webb <russell.webb@intel.com>
Tested-by: jenkins autobuilder
common/libsensors/SensorIIODev.cpp

index 9259b14..faf7fdd 100644 (file)
@@ -623,13 +623,16 @@ int SensorIIODev::readEvents(sensors_event_t *data, int count){
         *data = mPendingEvent;
         return 1;
     }
-    read_size = read(mFd, raw_buffer, datum_size * buffer_len);
+    read_size = read(mFd, raw_buffer, datum_size);
     numEventReceived = 0;
-    if (processEvent(raw_buffer, datum_size) >= 0){
-        mPendingEvent.timestamp = getTimestamp();
-        mPendingEvent.timestamp = getTimestamp();
-        *data = mPendingEvent;
-        numEventReceived++;
+    if(read_size != datum_size) {
+        ALOGE("read() error (or short count) from IIO device: %d\n", errno);
+    } else {
+        if (processEvent(raw_buffer, datum_size) >= 0) {
+            mPendingEvent.timestamp = getTimestamp();
+            *data = mPendingEvent;
+            numEventReceived++;
+        }
     }
     return numEventReceived;
 }