summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Abgrall <jpa@google.com>2012-06-21 20:02:12 -0700
committerJP Abgrall <jpa@google.com>2012-06-21 20:02:12 -0700
commitac5129a60eb420290835e7cb39f7f577a0b83f70 (patch)
treedf17201276fe098c050392d500933656732a1e9c
parent7494581689b0fc1d8addd016b1c92d74d01f5ad4 (diff)
downloadandroid_hardware_invensense-ac5129a60eb420290835e7cb39f7f577a0b83f70.tar.gz
android_hardware_invensense-ac5129a60eb420290835e7cb39f7f577a0b83f70.tar.bz2
android_hardware_invensense-ac5129a60eb420290835e7cb39f7f577a0b83f70.zip
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
-rw-r--r--libsensors_iio/CompassSensor.IIO.9150.cpp8
-rw-r--r--libsensors_iio/MPLSensor.cpp21
-rw-r--r--libsensors_iio/MPLSensor.h6
3 files changed, 27 insertions, 8 deletions
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 @@ int CompassSensor::inv_init_sysfs_attributes(void)
dptr = (char**)&compassSysFs;
if (sptr == NULL)
return -1;
-
+
do {
*dptr++ = sptr;
sptr += sizeof(char[MAX_SYSFS_NAME_LEN]);
@@ -383,7 +383,11 @@ int CompassSensor::inv_init_sysfs_attributes(void)
// 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 @@ static FILE *logfile = NULL;
MPLSensor::MPLSensor(CompassSensor *compass)
: SensorBase(NULL, NULL),
+ mMplSensorInitialized(false),
mNewData(0),
mMasterSensorMask(INV_ALL_SENSORS),
mLocalSensorMask(ALL_MPL_SENSORS_NP),
@@ -183,7 +184,10 @@ MPLSensor::MPLSensor(CompassSensor *compass)
#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 @@ MPLSensor::MPLSensor(CompassSensor *compass)
/* 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 @@ MPLSensor::MPLSensor(CompassSensor *compass)
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 @@ MPLSensor::MPLSensor(CompassSensor *compass)
if (logfile)
inv_turn_on_data_logging(logfile);
#endif
+
+ mMplSensorInitialized = true;
}
@@ -2472,6 +2478,12 @@ int MPLSensor::inv_init_sysfs_attributes(void)
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 @@ int MPLSensor::inv_init_sysfs_attributes(void)
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 @@ public:
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 @@ public:
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);