Do not use SensorDevice as ISensorsCallback
If the SensorDevice implements the ISensorsCallback, then when the
last client disconnects (e.g. the Sensors HAL crashes), the Binder
framework will release the server implementation, causing the
SensorDevice to be destructed. A separate object must be used so that
the SensorDevice is not destroyed whenever the Sensors HAL
disconnects.
Bug: 111070257
Test: Builds, verifies SensorDevice is not destroyed when Sensors HAL
crashes
Change-Id: I11b7946a24e8ef8dff30b35ca44ccb5be56035ba
diff --git a/services/sensorservice/SensorDevice.cpp b/services/sensorservice/SensorDevice.cpp
index ebc7eb1..68e5172 100644
--- a/services/sensorservice/SensorDevice.cpp
+++ b/services/sensorservice/SensorDevice.cpp
@@ -16,6 +16,7 @@
#include "SensorDevice.h"
+#include "android/hardware/sensors/2.0/ISensorsCallback.h"
#include "android/hardware/sensors/2.0/types.h"
#include "SensorService.h"
@@ -32,6 +33,7 @@
using namespace android::hardware::sensors;
using namespace android::hardware::sensors::V1_0;
using namespace android::hardware::sensors::V1_0::implementation;
+using android::hardware::sensors::V2_0::ISensorsCallback;
using android::hardware::sensors::V2_0::EventQueueFlagBits;
using android::hardware::hidl_vec;
using android::hardware::Return;
@@ -57,6 +59,20 @@
}
}
+struct SensorsCallback : public ISensorsCallback {
+ using Result = ::android::hardware::sensors::V1_0::Result;
+ Return<void> onDynamicSensorsConnected(
+ const hidl_vec<SensorInfo> &dynamicSensorsAdded) override {
+ return SensorDevice::getInstance().onDynamicSensorsConnected(dynamicSensorsAdded);
+ }
+
+ Return<void> onDynamicSensorsDisconnected(
+ const hidl_vec<int32_t> &dynamicSensorHandlesRemoved) override {
+ return SensorDevice::getInstance().onDynamicSensorsDisconnected(
+ dynamicSensorHandlesRemoved);
+ }
+};
+
SensorDevice::SensorDevice()
: mHidlTransportErrors(20), mRestartWaiter(new HidlServiceRegistrationWaiter()) {
if (!connectHidlService()) {
@@ -166,7 +182,7 @@
status_t status = StatusFromResult(checkReturn(mSensors->initialize(
*mEventQueue->getDesc(),
*mWakeLockQueue->getDesc(),
- this)));
+ new SensorsCallback())));
if (status != NO_ERROR) {
connectionStatus = HalConnectionStatus::FAILED_TO_CONNECT;
diff --git a/services/sensorservice/SensorDevice.h b/services/sensorservice/SensorDevice.h
index 282550f..85ea4b9 100644
--- a/services/sensorservice/SensorDevice.h
+++ b/services/sensorservice/SensorDevice.h
@@ -44,7 +44,6 @@
// ---------------------------------------------------------------------------
class SensorDevice : public Singleton<SensorDevice>,
- public android::hardware::sensors::V2_0::ISensorsCallback,
public SensorServiceUtil::Dumpable {
public:
class HidlTransportErrorLog {
@@ -107,9 +106,9 @@
using Result = ::android::hardware::sensors::V1_0::Result;
hardware::Return<void> onDynamicSensorsConnected(
- const hardware::hidl_vec<hardware::sensors::V1_0::SensorInfo> &dynamicSensorsAdded) override;
+ const hardware::hidl_vec<hardware::sensors::V1_0::SensorInfo> &dynamicSensorsAdded);
hardware::Return<void> onDynamicSensorsDisconnected(
- const hardware::hidl_vec<int32_t> &dynamicSensorHandlesRemoved) override;
+ const hardware::hidl_vec<int32_t> &dynamicSensorHandlesRemoved);
// Dumpable
virtual std::string dump() const;