diff options
Diffstat (limited to 'modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp')
-rw-r--r-- | modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp | 80 |
1 files changed, 78 insertions, 2 deletions
diff --git a/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp b/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp index 9f9a9fef..4f0cc481 100644 --- a/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp +++ b/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp @@ -14,12 +14,19 @@ * limitations under the License. */ +#include "BaseSensorObject.h" #include "DynamicSensorsSubHal.h" +#include <convertV2_1.h> #include <hardware/sensors-base.h> #include <log/log.h> +#include <chrono> +#include <thread> + using ::android::hardware::sensors::V1_0::Result; +using ::android::hardware::sensors::V2_0::implementation::ScopedWakelock; +using ::android::hardware::sensors::V2_1::implementation::convertFromSensorEvent; using ::android::hardware::sensors::V2_1::SensorInfo; using ::android::hardware::sensors::V2_1::SensorType; template<class T> using Return = ::android::hardware::Return<T>; @@ -33,7 +40,7 @@ DynamicSensorsSubHal::DynamicSensorsSubHal() { mDynamicSensorManager.reset( DynamicSensorManager::createInstance(kDynamicHandleBase, kMaxDynamicHandleCount, - nullptr /* callback */)); + this /* callback */)); } // ISensors. @@ -131,12 +138,81 @@ Return<void> DynamicSensorsSubHal::debug( // ISensorsSubHal. Return<Result> DynamicSensorsSubHal::initialize( - const sp<IHalProxyCallback>& hal_proxy_callback __unused) { + const sp<IHalProxyCallback>& hal_proxy_callback) { ALOGD("DynamicSensorsSubHal::initialize invoked."); + mHalProxyCallback = hal_proxy_callback; + return Result::OK; } +// SensorEventCallback. +int DynamicSensorsSubHal::submitEvent(SP(BaseSensorObject) sensor, + const sensors_event_t& e) { + std::vector<Event> events; + Event hal_event; + bool wakeup; + + if (e.type == SENSOR_TYPE_DYNAMIC_SENSOR_META) { + const dynamic_sensor_meta_event_t* sensor_meta; + + sensor_meta = static_cast<const dynamic_sensor_meta_event_t*>( + &(e.dynamic_sensor_meta)); + if (sensor_meta->connected != 0) { + // The sensor framework must be notified of the connected sensor + // through the callback before handling the sensor added event. If + // it isn't, it will assert when looking up the sensor handle when + // processing the sensor added event. + // + // TODO (b/201529167): Fix dynamic sensors addition / removal when + // converting to AIDL. + // The sensor framework runs in a separate process from the sensor + // HAL, and it processes events in a dedicated thread, so it's + // possible the event handling can be done before the callback is + // run. Thus, a delay is added after sending notification of the + // connected sensor. + onSensorConnected(sensor_meta->handle, sensor_meta->sensor); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + } + } + + convertFromSensorEvent(e, &hal_event); + events.push_back(hal_event); + if (sensor && sensor->getSensor()) { + wakeup = sensor->getSensor()->flags & SENSOR_FLAG_WAKE_UP; + } else { + wakeup = false; + } + ScopedWakelock wakelock = mHalProxyCallback->createScopedWakelock(wakeup); + mHalProxyCallback->postEvents(events, std::move(wakelock)); + + return 0; +} + +void DynamicSensorsSubHal::onSensorConnected( + int handle, const sensor_t* sensor_info) { + hidl_vec<SensorInfo> sensor_list; + + sensor_list.resize(1); + sensor_list[0].sensorHandle = handle; + sensor_list[0].name = sensor_info->name; + sensor_list[0].vendor = sensor_info->vendor; + sensor_list[0].version = 1; + sensor_list[0].type = static_cast<SensorType>(sensor_info->type); + sensor_list[0].typeAsString = sensor_info->stringType; + sensor_list[0].maxRange = sensor_info->maxRange; + sensor_list[0].resolution = sensor_info->resolution; + sensor_list[0].power = sensor_info->power; + sensor_list[0].minDelay = sensor_info->minDelay; + sensor_list[0].fifoReservedEventCount = sensor_info->fifoReservedEventCount; + sensor_list[0].fifoMaxEventCount = sensor_info->fifoMaxEventCount; + sensor_list[0].requiredPermission = sensor_info->requiredPermission; + sensor_list[0].maxDelay = sensor_info->maxDelay; + sensor_list[0].flags = sensor_info->flags; + + mHalProxyCallback->onDynamicSensorsConnected_2_1(sensor_list); +} + } // namespace SensorHalExt } // namespace android |