Sensors: Invensense: 6515: increase event throughput
am: d47d7879a0

* commit 'd47d7879a03b983dc3546b717c729f7294e16c7f':
  Sensors: Invensense: 6515: increase event throughput
diff --git a/6515/libsensors_iio/MPLSensor.cpp b/6515/libsensors_iio/MPLSensor.cpp
index 5a57d0b..67cc010 100644
--- a/6515/libsensors_iio/MPLSensor.cpp
+++ b/6515/libsensors_iio/MPLSensor.cpp
@@ -244,7 +244,7 @@
     if(fd < 0) {
         LOGE("HAL:Error opening gyro self test scale");
     } else {
-        memset(gyroBuf, 0, sizeof(gyroBuf));
+        memset(gyroScale, 0, sizeof(gyroScale));
         count = read_attribute_sensor(fd, gyroScale, sizeof(gyroScale));
         if(count < 1) {
             LOGE("HAL:Error reading gyro self test scale");
@@ -323,7 +323,7 @@
         if(fd < 0) {
             LOGE("HAL:Error opening gyro self test scale");
         } else {
-            memset(buf, 0, sizeof(buf));
+            memset(accelScale, 0, sizeof(accelScale));
             count = read_attribute_sensor(fd, accelScale, sizeof(accelScale));
             if(count < 1) {
                 LOGE("HAL:Error reading accel self test scale");
@@ -377,13 +377,13 @@
         LOGV_IF(ENG_VERBOSE,
                 "HAL:dmp_sign_motion_fd opened : %d", dmp_sign_motion_fd);
     }
-#if 1
+
     /* the following threshold can be modified for SMD sensitivity */
     int motionThreshold = 3000;
     LOGV_IF(SYSFS_VERBOSE, "HAL:sysfs:echo %d > %s (%lld)",
                 motionThreshold, mpu.smd_threshold, getTimestamp());
         res = write_sysfs_int(mpu.smd_threshold, motionThreshold);
-#endif
+
 #if 0
     int StepCounterThreshold = 5;
     LOGV_IF(SYSFS_VERBOSE, "HAL:sysfs:echo %d > %s (%lld)",
@@ -419,7 +419,7 @@
 
     /* initialize sensor data */
     memset(mPendingEvents, 0, sizeof(mPendingEvents));
-    memset(mPendingFlushEvents, 0, sizeof(mPendingEvents));
+    memset(mPendingFlushEvents, 0, sizeof(mPendingFlushEvents));
 
     mPendingEvents[RotationVector].version = sizeof(sensors_event_t);
     mPendingEvents[RotationVector].sensor = ID_RV;
@@ -978,13 +978,13 @@
     }
 
     if (gyro_x_offset_fd > 0) {
-        close(gyro_x_dmp_bias_fd);
+        close(gyro_x_offset_fd);
     }
     if (gyro_y_offset_fd > 0) {
         close(gyro_y_offset_fd);
     }
     if (gyro_z_offset_fd > 0) {
-        close(accel_z_offset_fd);
+        close(gyro_z_offset_fd);
     }
 
     /* Turn off Gyro master enable          */
@@ -2391,7 +2391,7 @@
     }
 
     // if virtual sensors are on but not batched, turn off batch mode.
-    for(int i = Orientation; i <= GeomagneticRotationVector; i++) {
+    for(int i = Orientation; i < NumSensors; i++) {
         if ((enableSensors & (1 << i)) && !(tempBatchSensor & (1 << i))) {
              LOGV_IF(ENG_VERBOSE, "HAL:computeBatchSensorMask: "
                      "composite sensor on continuous mode:%d", i);
@@ -2400,7 +2400,7 @@
     }
 
     if ((mFeatureActiveMask & INV_DMP_PEDOMETER) && !(tempBatchSensor & (1 << StepDetector))) {
-        LOGV("HAL:computeBatchSensorMask: step detector on continuous mode.");
+        LOGV_IF(ENG_VERBOSE, "HAL:computeBatchSensorMask: step detector on continuous mode.");
         return 0;
     }
 
@@ -3847,8 +3847,11 @@
         inv_execute_on_data();
 
     int numEventReceived = 0;
-
     long msg;
+
+    if (count <= 0)
+        return 0;
+
     msg = inv_get_message_level_0(1);
     if (msg) {
         if (msg & INV_MSG_MOTION_EVENT) {
@@ -3930,46 +3933,45 @@
                         count--;
                         numEventReceived++;
                     } else {
-                        ALOGE("Event from type=%d with duplicate timestamp %lld discarded",
-                                    mPendingEvents[i].type, mStepSensorTimestamp);
+                        ALOGE("Event from type=%d with duplicate timestamp %lld (%+f, %+f, %+f) discarded",
+                                    mPendingEvents[i].type, mLastTimestamp[i], mPendingEvents[i].data[0], mPendingEvents[i].data[1], mPendingEvents[i].data[2]);
                     }
                 }
             }
-        }
-        mCompassOverFlow = 0;
-    }
+            mCompassOverFlow = 0;
 
-    // handle partial packet read and end marker
-    // skip readEvents from hal_outputs
-    if (mFlushBatchSet && count>0 && !mFlushSensorEnabledVector.isEmpty()) {
-        while (mFlushBatchSet && count>0 && !mFlushSensorEnabledVector.isEmpty()) {
-            int sendEvent = metaHandler(&mPendingFlushEvents[0], META_DATA_FLUSH_COMPLETE);
-            if (sendEvent) {
-                LOGV_IF(ENG_VERBOSE, "Queueing flush complete for handle=%d",
-                        mPendingFlushEvents[0].meta_data.sensor);
-                *data++ = mPendingFlushEvents[0];
-                count--;
-                numEventReceived++;
-            } else {
-                LOGV_IF(ENG_VERBOSE, "sendEvent false, NOT queueing flush complete for handle=%d",
-                        mPendingFlushEvents[0].meta_data.sensor);
+            // handle partial packet read and end marker
+            // skip readEvents from hal_outputs
+            if (mFlushBatchSet && count>0 && !mFlushSensorEnabledVector.isEmpty()) {
+                while (mFlushBatchSet && count>0 && !mFlushSensorEnabledVector.isEmpty()) {
+                    int sendEvent = metaHandler(&mPendingFlushEvents[0], META_DATA_FLUSH_COMPLETE);
+                    if (sendEvent) {
+                        LOGV_IF(ENG_VERBOSE, "Queueing flush complete for handle=%d",
+                                mPendingFlushEvents[0].meta_data.sensor);
+                        *data++ = mPendingFlushEvents[0];
+                        count--;
+                        numEventReceived++;
+                    } else {
+                        LOGV_IF(ENG_VERBOSE, "sendEvent false, NOT queueing flush complete for handle=%d",
+                                mPendingFlushEvents[0].meta_data.sensor);
+                    }
+                    mFlushBatchSet--;
+                }
+
+                // Double check flush status
+                if (mFlushSensorEnabledVector.isEmpty()) {
+                    mEmptyDataMarkerDetected = 0;
+                    mDataMarkerDetected = 0;
+                    mFlushBatchSet = 0;
+                    LOGV_IF(ENG_VERBOSE, "Flush completed");
+                } else {
+                    LOGV_IF(ENG_VERBOSE, "Flush is still active");
+                }
+            } else if (mFlushBatchSet && mFlushSensorEnabledVector.isEmpty()) {
+                mFlushBatchSet = 0;
             }
-            mFlushBatchSet--;
         }
-
-        // Double check flush status
-        if (mFlushSensorEnabledVector.isEmpty()) {
-            mEmptyDataMarkerDetected = 0;
-            mDataMarkerDetected = 0;
-            mFlushBatchSet = 0;
-            LOGV_IF(ENG_VERBOSE, "Flush completed");
-        } else {
-            LOGV_IF(ENG_VERBOSE, "Flush is still active");
-        }
-    } else if (mFlushBatchSet && mFlushSensorEnabledVector.isEmpty()) {
-        mFlushBatchSet = 0;
     }
-
     return numEventReceived;
 }
 
@@ -6065,7 +6067,7 @@
     } else {
         if (calctDataRates(&tmp_reset_rate, &tmp_gyro_rate, &tmp_accel_rate, &tmp_compass_rate, &tmp_pressure_rate) < 0) {
             skip_reset_data_rate = true;
-            LOGW("HAL:ERR can't get output rate back to original setting");
+            LOGV_IF(ENG_VERBOSE, "HAL:ERR can't get output rate back to original setting");
         }
         if (tmp_reset_rate != mResetRate)
             master_enable_call++;
@@ -6275,7 +6277,7 @@
 
     /* driver returns 0 if FIFO is empty */
     if (res == 0) {
-        LOGI("HAL: flush - no data in FIFO");
+        LOGV_IF(ENG_VERBOSE, "HAL: flush - no data in FIFO");
     }
 
     LOGV_IF(ENG_VERBOSE, "HAl:flush - mFlushSensorEnabledVector=%d res=%d status=%d", handle, res, status);
@@ -6447,7 +6449,7 @@
                 LOGE("HAL:cannot open pedometer_steps");
             } else {
                 if (fscanf(fp, "%lld\n", &stepCount) < 0) {
-                    LOGW("HAL:cannot read pedometer_steps");
+                    LOGV_IF(PROCESS_VERBOSE, "HAL:cannot read pedometer_steps");
                     if (fclose(fp) < 0) {
                        LOGW("HAL:cannot close pedometer_steps");
                     }
diff --git a/6515/libsensors_iio/sensors_mpl.cpp b/6515/libsensors_iio/sensors_mpl.cpp
index b277e3b..3f1b67a 100755
--- a/6515/libsensors_iio/sensors_mpl.cpp
+++ b/6515/libsensors_iio/sensors_mpl.cpp
@@ -250,7 +250,21 @@
     // look for new events
     nb = poll(mPollFds, numSensorDrivers, polltime);
     LOGI_IF(0, "poll nb=%d, count=%d, pt=%d ts=%lld", nb, count, polltime, getTimestamp());
-    if (nb > 0) {
+    if (nb == 0 && count > 0) {
+        /* to see if any step counter events */
+        if(((MPLSensor*) mSensor)->hasStepCountPendingEvents() == true) {
+            nb = ((MPLSensor*) mSensor)->readDmpPedometerEvents(
+                            data, count, ID_SC, 0);
+            LOGI_IF(SensorBase::HANDLER_DATA, "sensors_mpl:readStepCount() - "
+                    "nb=%d, count=%d, nbEvents=%d, data->timestamp=%lld, ",
+                    nb, count, nbEvents, data->timestamp);
+            if (nb > 0) {
+                count -= nb;
+                nbEvents += nb;
+                data += nb;
+            }
+        }
+    } else while (nb > 0) {
         for (int i = 0; count && i < numSensorDrivers; i++) {
             if (mPollFds[i].revents & (POLLIN | POLLPRI)) {
                 nb = 0;
@@ -293,6 +307,7 @@
                     nbEvents += nb;
                     data += nb;
                 }
+
                 if(nb == 0) {
                     nb = ((MPLSensor*) mSensor)->readEvents(data, count);
                     LOGI_IF(0, "sensors_mpl:readEvents() - "
@@ -323,20 +338,11 @@
                 data += nb;
             }
         }
-    } else if(nb == 0) {
-        /* to see if any step counter events */
-        if(((MPLSensor*) mSensor)->hasStepCountPendingEvents() == true) {
+        if (count > 0) {
+            // We still have room for more events, try an immediate poll for more data
+            nb = poll(mPollFds, numSensorDrivers, 0);
+        } else {
             nb = 0;
-            nb = ((MPLSensor*) mSensor)->readDmpPedometerEvents(
-                            data, count, ID_SC, 0);
-            LOGI_IF(SensorBase::HANDLER_DATA, "sensors_mpl:readStepCount() - "
-                    "nb=%d, count=%d, nbEvents=%d, data->timestamp=%lld, ",
-                    nb, count, nbEvents, data->timestamp);
-            if (nb > 0) {
-                count -= nb;
-                nbEvents += nb;
-                data += nb;
-            }
         }
     }
     return nbEvents;