libsensors_iio: Don't segfault when constructor fails when device absent

Currently too much work is been done in the constructor.
If a device is not there in the kernel, the constructor will fail
to correctly initialize, and will leave a bunch of uninitialized strings
that when used will cause segfaults.

So now the classes have an isValid() method to be used after construction.
If false, the object should be destroyed.

Added extra checks to locate failed initialization.

Change-Id: I3f767f9dc0b0f19b66d034a683444baed771a5db
diff --git a/libsensors_iio/CompassSensor.IIO.9150.cpp b/libsensors_iio/CompassSensor.IIO.9150.cpp
index ce0df34..d9f2e0c 100644
--- a/libsensors_iio/CompassSensor.IIO.9150.cpp
+++ b/libsensors_iio/CompassSensor.IIO.9150.cpp
@@ -375,7 +375,7 @@
     dptr = (char**)&compassSysFs;
     if (sptr == NULL)
         return -1;
-    
+
     do {
         *dptr++ = sptr;
         sptr += sizeof(char[MAX_SYSFS_NAME_LEN]);
@@ -383,7 +383,11 @@
 
     // get proper (in absolute/relative) IIO path & build MPU's sysfs paths
     // inv_get_sysfs_abs_path(sysfs_path);
-    inv_get_sysfs_path(sysfs_path);
+    if(INV_SUCCESS != inv_get_sysfs_path(sysfs_path)) {
+        ALOGE("CompassSensor failed get sysfs path");
+        return -1;
+    }
+
     inv_get_iio_trigger_path(iio_trigger_path);
 
 #if defined COMPASS_YAS530 || defined COMPASS_AK8975
diff --git a/libsensors_iio/MPLSensor.cpp b/libsensors_iio/MPLSensor.cpp
index ae82459..e23ecc9 100644
--- a/libsensors_iio/MPLSensor.cpp
+++ b/libsensors_iio/MPLSensor.cpp
@@ -136,6 +136,7 @@
 
 MPLSensor::MPLSensor(CompassSensor *compass)
                        : SensorBase(NULL, NULL),
+                         mMplSensorInitialized(false),
                          mNewData(0),
                          mMasterSensorMask(INV_ALL_SENSORS),
                          mLocalSensorMask(ALL_MPL_SENSORS_NP),
@@ -183,7 +184,10 @@
 #endif
 
     /* setup sysfs paths */
-    inv_init_sysfs_attributes();
+    if(inv_init_sysfs_attributes()) {
+        ALOGE("MPLSensor failed to init sysfs attributes");
+        return;
+    }
 
     /* get chip name */
     if (inv_get_chip_name(chip_ID) != INV_SUCCESS) {
@@ -203,7 +207,7 @@
     /* Load DMP image if capable, ie. MPU6xxx/9xxx */
     // TODO: disabled for GED tablet
 #ifdef ENABLE_LP_QUAT_FEAT
-    loadDMP();	
+    loadDMP();
 #endif
 
     /* open temperature fd for temp comp */
@@ -220,7 +224,7 @@
     if (USE_THIRD_PARTY_ACCEL == 0) {
         char buf[3];
         int count = 0;
-        LOGV_IF(SYSFS_VERBOSE, 
+        LOGV_IF(SYSFS_VERBOSE,
                 "HAL:sysfs:cat %s (%lld)", mpu.accel_fsr, getTimestamp());
 
         fd = open(mpu.accel_fsr, O_RDONLY);
@@ -323,6 +327,8 @@
     if (logfile)
         inv_turn_on_data_logging(logfile);
 #endif
+
+    mMplSensorInitialized = true;
 }
 
 
@@ -2472,6 +2478,12 @@
     char **dptr;
     int num;
 
+    // get proper (in absolute/relative) IIO path & build MPU's sysfs paths
+    // inv_get_sysfs_abs_path(sysfs_path);
+    if(INV_SUCCESS != inv_get_sysfs_path(sysfs_path)) {
+        ALOGE("MPLSensor failed get sysfs path");
+        return -1;
+    }
     sysfs_names_ptr = 
             (char*)malloc(sizeof(char[MAX_SYSFS_ATTRB][MAX_SYSFS_NAME_LEN]));
     sptr = sysfs_names_ptr;
@@ -2486,9 +2498,6 @@
         return -1;
     }
 
-    // get proper (in absolute/relative) IIO path & build MPU's sysfs paths
-    // inv_get_sysfs_abs_path(sysfs_path);
-    inv_get_sysfs_path(sysfs_path);
     inv_get_iio_trigger_path(iio_trigger_path);
 
     sprintf(mpu.key, "%s%s", sysfs_path, "/key");
diff --git a/libsensors_iio/MPLSensor.h b/libsensors_iio/MPLSensor.h
index 4c38c57..3ef1ba3 100644
--- a/libsensors_iio/MPLSensor.h
+++ b/libsensors_iio/MPLSensor.h
@@ -112,6 +112,9 @@
     int populateSensorList(struct sensor_t *list, int len);

     void cbProcData();

 

+    // Do not work with this object unless it is initialized

+    bool isValid() { return mMplSensorInitialized; };

+

     //static pointer to the object that will handle callbacks

     static MPLSensor* gMPLSensor;

 

@@ -122,6 +125,9 @@
     int readCompassEvents(sensors_event_t* data, int count);

 

 protected:

+    // Lets us know if the constructor was actually able to finish its job.

+    // E.g. false if init sysfs failed.

+    bool mMplSensorInitialized;

     CompassSensor *mCompassSensor;

 

     int gyroHandler(sensors_event_t *data);