Fix various issues with the sensor HAL

This commit fixes the following reported issues:
1) there are a lot of LOG spew
Silent Poll__setDelay and Poll__activiate LOG spew.
Changed "setDelay handle" to LOGD instead of of LOGE.

2) Gyro HAL reports a resolution of 0.28 rad/s, that is 16 deg/s. That seems wrong.
It appears the previous values we used was under the most sensitive settings, the new value is using an average setting instead.

3) The orientation sensor reports a name of "MPL Orientation (android deprecated format)". Please remove the "android deprecated format"
from the name.
Removed "android deprecated format"

4) MPL linear accel reports a max range of 10240.0 m/s^2. This seems wrong (that 1000+ Gs!!) Please rename to "MPL Linear Acceleration" (accel is not a word).
It appears that the dynamic sensor list does not recognize the accel id (or compass id) that were passed, causing the system to always read the initialized value. This is now fixed.
Renamed to MPL Linear Acceleration and MPL labels to be more humanly readable.

5) Fix for more accurate time stamps. This is a small subset of what we committed to Rocky's team and Kevin felt it was an easy change.
Instead of using system time stamp, the fix is to retrive timestamp directly from the driver when an IRQ is fired.

Change-Id: I8cfbd7cdecbd2e25613204ec1013e6079de97f37
diff --git a/libsensors/MPLSensor.cpp b/libsensors/MPLSensor.cpp
index 9663bfc..484396b 100644
--- a/libsensors/MPLSensor.cpp
+++ b/libsensors/MPLSensor.cpp
@@ -45,6 +45,8 @@
 #include "mldl.h"
 
 #include "mpu.h"
+#include "accel.h"
+#include "compass.h"
 #include "kernel/timerirq.h"
 #include "kernel/mpuirq.h"
 #include "kernel/slaveirq.h"
@@ -68,29 +70,30 @@
 
 /* Base values for the sensor list, these need to be in the order defined in MPLSensor.h */
 static struct sensor_t sSensorList[] =
-    { { "MPL Gyro", "Invensense", 1,
+    { { "MPL Gyroscope", "Invensense", 1,
          SENSORS_GYROSCOPE_HANDLE,
          SENSOR_TYPE_GYROSCOPE, 2000.0f, 1.0f, 0.5f, 10000, { } },
-      { "MPL accel", "Invensense", 1,
+      { "MPL Accelerometer", "Invensense", 1,
          SENSORS_ACCELERATION_HANDLE,
          SENSOR_TYPE_ACCELEROMETER, 10240.0f, 1.0f, 0.5f, 10000, { } },
-      { "MPL magnetic field", "Invensense", 1,
+      { "MPL Magnetic Field", "Invensense", 1,
          SENSORS_MAGNETIC_FIELD_HANDLE,
          SENSOR_TYPE_MAGNETIC_FIELD, 10240.0f, 1.0f, 0.5f, 10000, { } },
-      { "MPL Orientation (android deprecated format)", "Invensense", 1,
+      { "MPL Orientation", "Invensense", 1,
          SENSORS_ORIENTATION_HANDLE,
          SENSOR_TYPE_ORIENTATION, 360.0f, 1.0f, 9.7f, 10000, { } },
-      { "MPL rotation vector", "Invensense", 1,
+      { "MPL Rotation Vector", "Invensense", 1,
          SENSORS_ROTATION_VECTOR_HANDLE,
          SENSOR_TYPE_ROTATION_VECTOR, 10240.0f, 1.0f, 0.5f, 10000, { } },
-      { "MPL linear accel", "Invensense", 1,
+      { "MPL Linear Acceleration", "Invensense", 1,
          SENSORS_LINEAR_ACCEL_HANDLE,
          SENSOR_TYPE_LINEAR_ACCELERATION, 10240.0f, 1.0f, 0.5f, 10000, { } },
-      { "MPL gravity", "Invensense", 1,
+      { "MPL Gravity", "Invensense", 1,
          SENSORS_GRAVITY_HANDLE,
          SENSOR_TYPE_GRAVITY, 10240.0f, 1.0f, 0.5f, 10000, { } },
 };
 
+static unsigned long long irq_timestamp = 0;
 /* ***************************************************************************
  * MPL interface misc.
  */
@@ -137,14 +140,15 @@
 #define RV_ENABLED ((1<<ID_RV) & enabled_sensors)
 
 MPLSensor::MPLSensor() :
-    SensorBase(NULL, NULL), mEnabled(0), mPendingMask(0), mMpuAccuracy(0),
-            mNewData(0), mDmpStarted(false),
-            mMplMutex(PTHREAD_MUTEX_INITIALIZER),
+    SensorBase(NULL, NULL),
+            mMpuAccuracy(0), mNewData(0),
+            mDmpStarted(false),
             mMasterSensorMask(INV_ALL_SENSORS),
             mLocalSensorMask(ALL_MPL_SENSORS_NP), mPollTime(-1),
             mCurFifoRate(-1), mHaveGoodMpuCal(false),
             mUseTimerIrqAccel(false), mUsetimerIrqCompass(true),
-            mUseTimerirq(false), mSampleCount(0)
+            mUseTimerirq(false), mSampleCount(0),
+            mEnabled(0), mPendingMask(0)
 {
     FUNC_LOG;
     inv_error_t rv;
@@ -153,6 +157,8 @@
 
     LOGV_IF(EXTRA_VERBOSE, "MPLSensor constructor: numSensors = %d", numSensors);
 
+    pthread_mutex_init(&mMplMutex, NULL);
+
     mForceSleep = false;
 
     /* used for identifying whether 9axis is enabled or not             */
@@ -205,7 +211,7 @@
     if ((accel_fd == -1) && (timer_fd != -1)) {
         //no accel irq and timer available
         mUseTimerIrqAccel = true;
-        LOGD("MPLSensor falling back to timerirq for accel data");
+        //LOGD("MPLSensor falling back to timerirq for accel data");
     }
 
     memset(mPendingEvents, 0, sizeof(mPendingEvents));
@@ -295,6 +301,7 @@
         LOGD("Error : could not close the serial port");
     }
     pthread_mutex_unlock(&mMplMutex);
+    pthread_mutex_destroy(&mMplMutex);
 }
 
 /* clear any data from our various filehandles */
@@ -313,6 +320,7 @@
             nread = read(cur_fd, &irqdata, sizeof(irqdata));
             if (nread > 0) {
                 irq_set[i] = true;
+                irq_timestamp = irqdata.irqtime;
                 //LOGV_IF(EXTRA_VERBOSE, "irq: %d %d (%d)", i, irqdata.interruptcount, j++);
             }
         }
@@ -858,7 +866,7 @@
 {
     FUNC_LOG;
     /* LOGV_IF(EXTRA_VERBOSE, */
-    LOGE(" setDelay handle: %d rate %d", handle, (int) (ns / 1000000LL));
+    LOGD(" setDelay handle: %d rate %d", handle, (int) (ns / 1000000LL));
     int what = -1;
     switch (handle) {
     case ID_A:
@@ -927,7 +935,7 @@
             rate = 1;
 
         if (rate != mCurFifoRate) {
-            LOGV("set fifo rate: %d %llu", rate, wanted);
+            LOGD("set fifo rate: %d %llu", rate, wanted);
             inv_error_t res; // = inv_dmp_stop();
             res = inv_set_fifo_rate(rate);
             LOGE_IF(res != INV_SUCCESS, "error setting FIFO rate");
@@ -1006,13 +1014,14 @@
         return 0;
     }
     mNewData = 0;
-    int64_t tt = now_ns();
+    
+    /* google timestamp */
     pthread_mutex_lock(&mMplMutex);
     for (int i = 0; i < numSensors; i++) {
         if (mEnabled & (1 << i)) {
             CALL_MEMBER_FN(this,mHandlers[i])(mPendingEvents + i,
                                               &mPendingMask, i);
-            mPendingEvents[i].timestamp = tt;
+	    mPendingEvents[i].timestamp = irq_timestamp;
         }
     }
 
@@ -1128,21 +1137,23 @@
     /* fill in the base values */
     memcpy(list, sSensorList, sizeof (struct sensor_t) * 7);
 
-    /* get platform config  */
-    struct mldl_cfg *mldl_cfg = inv_get_dl_config();
-    if( mldl_cfg == NULL )
-    {
-        LOGE("Can not get mldl_cfg handle\n");
-        return 0;
-    }
-
     /* first add gyro, accel and compass to the list */
 
     /* fill in accel values                          */
-    fillAccel(mldl_cfg->accel->id, list);
+    unsigned short accelId = inv_get_accel_id();
+    if(accelId == 0)
+    {
+	LOGE("Can not get accel id");
+    }   
+    fillAccel(accelId, list);
 
     /* fill in compass values                        */
-    fillCompass(mldl_cfg->compass->id, list);
+    unsigned short compassId = inv_get_compass_id();
+    if(compassId == 0)
+    {
+	LOGE("Can not get compass id");
+    }  
+    fillCompass(compassId, list);
 
     /* fill in gyro values                           */
     fillGyro(MPU_NAME, list);
diff --git a/libsensors/MPLSensor.h b/libsensors/MPLSensor.h
index d3b85d7..7ed69ed 100644
--- a/libsensors/MPLSensor.h
+++ b/libsensors/MPLSensor.h
@@ -102,6 +102,7 @@
     int mSampleCount;
     pthread_mutex_t mMplMutex;
     int64_t now_ns();
+    int64_t select_ns(unsigned long long time_set[]);    
 
     enum FILEHANDLES
     {
diff --git a/libsensors/sensor_params.h b/libsensors/sensor_params.h
index 35b64be..4925ac4 100644
--- a/libsensors/sensor_params.h
+++ b/libsensors/sensor_params.h
@@ -123,7 +123,7 @@
 //GYRO MPU3050
 #define RAD_P_DEG (3.14159f/180.0f)
 #define GYRO_MPU3050_RANGE      (2000.0f*RAD_P_DEG)
-#define GYRO_MPU3050_RESOLUTION     (16.4f*RAD_P_DEG)
+#define GYRO_MPU3050_RESOLUTION     (32.8f*RAD_P_DEG)
 #define GYRO_MPU3050_POWER      (6.1f)
 //GYRO MPU6050
 #define GYRO_MPU6050_RANGE      (2000.0f*RAD_P_DEG)
diff --git a/libsensors/sensors_mpl.cpp b/libsensors/sensors_mpl.cpp
index 847ad91..6b877c0 100644
--- a/libsensors/sensors_mpl.cpp
+++ b/libsensors/sensors_mpl.cpp
@@ -16,7 +16,7 @@
  */
 /**********Removed the gesture related code from this file for Google code check modified by Meenakshi Ramamoorthi on May 31st  **********/
 
-#define LOG_NDEBUG 0
+#define LOG_NDEBUG 1
 #define LOG_TAG "Sensors"
 #define FUNC_LOG LOGV("%s", __PRETTY_FUNCTION__)
 
@@ -197,7 +197,7 @@
 
 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
 {
-    //FUNC_LOG;
+    FUNC_LOG;
     int nbEvents = 0;
     int n = 0;
     int polltime = -1;
@@ -289,7 +289,7 @@
 static int poll__poll(struct sensors_poll_device_t *dev,
                       sensors_event_t* data, int count)
 {
-    //FUNC_LOG;
+    FUNC_LOG;
     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
     return ctx->pollEvents(data, count);
 }
diff --git a/mlsdk/mllite/mldl_cfg_mpu.c b/mlsdk/mllite/mldl_cfg_mpu.c
index b03ce20..f89f94c 100644
--- a/mlsdk/mllite/mldl_cfg_mpu.c
+++ b/mlsdk/mllite/mldl_cfg_mpu.c
@@ -222,8 +222,6 @@
         LOG_RESULT_LOCATION(result);
         return result;
     }
-    mpu_print_cfg(mldl_cfg);
-
     return result;
 }
 
@@ -260,7 +258,6 @@
 {
     int result;
     
-    mpu_print_cfg(mldl_cfg);
     mldl_cfg->requested_sensors = sensors;
     result = ioctl((int)mlsl_handle, MPU_SET_MPU_CONFIG, mldl_cfg);
     if (result) {
@@ -277,7 +274,7 @@
         LOG_RESULT_LOCATION(result);
         return result;
     }
-    MPL_LOGI("%s: Resuming to %04lx\n", __func__, mldl_cfg->requested_sensors);
+    //MPL_LOGI("%s: Resuming to %04lx\n", __func__, mldl_cfg->requested_sensors);
 
     return result;
 }
@@ -294,8 +291,8 @@
     unsigned long requested = mldl_cfg->requested_sensors;
 
     mldl_cfg->requested_sensors = (~sensors) & INV_ALL_SENSORS;
-    MPL_LOGI("%s: suspending sensors to %04lx\n", __func__,
-             mldl_cfg->requested_sensors);
+    //MPL_LOGI("%s: suspending sensors to %04lx\n", __func__,
+    //         mldl_cfg->requested_sensors);
 
     result = ioctl((int)mlsl_handle, MPU_SET_MPU_CONFIG, mldl_cfg);
     if (result) {
@@ -314,7 +311,7 @@
     }
 
     mldl_cfg->requested_sensors = requested;
-    MPL_LOGI("%s: Will resume next to %04lx\n", __func__, requested);
+    //MPL_LOGI("%s: Will resume next to %04lx\n", __func__, requested);
 
     return result;
 }