From 3d946592e6336aa8b9ed2da0156011a9c964637f Mon Sep 17 00:00:00 2001 From: Cosmin Tanislav Date: Tue, 21 Sep 2021 02:43:52 +0300 Subject: [PATCH] hidl: sensors: 1.0: Standardize xiaomi pickup sensor * use standard sensor type * ignore non-wakeup sensor variant * ignore events that do not properly match a pickup Change-Id: I32bb097afb33603190dfd00a21202301a56bda08 --- hidl/sensors/1.0/Sensors.cpp | 63 ++++++++++++++++------ hidl/sensors/1.0/Sensors.h | 5 +- hidl/sensors/1.0/convert.cpp | 19 +++++++ hidl/sensors/1.0/include/sensors/convert.h | 2 + 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/hidl/sensors/1.0/Sensors.cpp b/hidl/sensors/1.0/Sensors.cpp index ad90e21..c6ab844 100644 --- a/hidl/sensors/1.0/Sensors.cpp +++ b/hidl/sensors/1.0/Sensors.cpp @@ -103,18 +103,7 @@ status_t Sensors::initCheck() const { } Return Sensors::getSensorsList(getSensorsList_cb _hidl_cb) { - sensor_t const* list; - size_t count = mSensorModule->get_sensors_list(mSensorModule, &list); - - hidl_vec out; - out.resize(count); - - for (size_t i = 0; i < count; ++i) { - const sensor_t* src = &list[i]; - SensorInfo* dst = &out[i]; - - convertFromSensor(*src, dst); - } + hidl_vec out = getFixedUpSensorList(); _hidl_cb(out); @@ -206,8 +195,9 @@ Return Sensors::poll(int32_t maxCount, poll_cb _hidl_cb) { dynamicSensorsAdded[numDynamicSensors] = info; } - out.resize(count); - convertFromSensorEvents(err, data.get(), &out); + std::vector events; + convertFromSensorEvents(err, data.get(), events, getFixedUpSensorList()); + out = events; _hidl_cb(Result::OK, out, dynamicSensorsAdded); @@ -299,14 +289,53 @@ Return Sensors::configDirectReport(int32_t sensorHandle, int32_t channelHa return Void(); } +std::vector Sensors::getFixedUpSensorList() { + std::vector sensors; + + sensor_t const* list; + size_t count = mSensorModule->get_sensors_list(mSensorModule, &list); + + for (size_t i = 0; i < count; ++i) { + const sensor_t* src = &list[i]; + SensorInfo sensor; + + convertFromSensor(*src, &sensor); + + bool keep = patchXiaomiPickupSensor(sensor); + if (keep) { + sensors.push_back(sensor); + } + } + + return sensors; +}; + // static void Sensors::convertFromSensorEvents(size_t count, const sensors_event_t* srcArray, - hidl_vec* dstVec) { + std::vector& dstVec, + std::vector sensorsList) { for (size_t i = 0; i < count; ++i) { const sensors_event_t& src = srcArray[i]; - Event* dst = &(*dstVec)[i]; + Event event; - convertFromSensorEvent(src, dst); + convertFromSensorEvent(src, &event); + + SensorInfo* sensor = nullptr; + for (auto& s : sensorsList) { + if (s.sensorHandle == event.sensorHandle) { + sensor = &s; + break; + } + } + + if (sensor && sensor->type == SensorType::PICK_UP_GESTURE) { + if (event.u.scalar != 1) { + continue; + } + event.sensorType = SensorType::PICK_UP_GESTURE; + } + + dstVec.push_back(event); } } diff --git a/hidl/sensors/1.0/Sensors.h b/hidl/sensors/1.0/Sensors.h index e2ad6bd..aa13d80 100644 --- a/hidl/sensors/1.0/Sensors.h +++ b/hidl/sensors/1.0/Sensors.h @@ -20,6 +20,7 @@ #include #include #include +#include namespace android { namespace hardware { @@ -63,9 +64,11 @@ struct Sensors : public ::android::hardware::sensors::V1_0::ISensors { std::mutex mPollLock; int getHalDeviceVersion() const; + std::vector getFixedUpSensorList(); static void convertFromSensorEvents(size_t count, const sensors_event_t* src, - hidl_vec* dst); + std::vector& dst, + std::vector sensorsList); DISALLOW_COPY_AND_ASSIGN(Sensors); }; diff --git a/hidl/sensors/1.0/convert.cpp b/hidl/sensors/1.0/convert.cpp index 6b1be27..95ab124 100644 --- a/hidl/sensors/1.0/convert.cpp +++ b/hidl/sensors/1.0/convert.cpp @@ -374,6 +374,25 @@ int convertFromRateLevel(RateLevel rate) { } } +bool patchXiaomiPickupSensor(SensorInfo& sensor) { + if (sensor.typeAsString != "xiaomi.sensor.pickup") { + return true; + } + + /* + * Implement only the wake-up version of this sensor. + */ + if (!(sensor.flags & SensorFlagBits::WAKE_UP)) { + return false; + } + + sensor.type = SensorType::PICK_UP_GESTURE; + sensor.typeAsString = SENSOR_STRING_TYPE_PICK_UP_GESTURE; + sensor.maxRange = 1; + + return true; +} + } // namespace implementation } // namespace V1_0 } // namespace sensors diff --git a/hidl/sensors/1.0/include/sensors/convert.h b/hidl/sensors/1.0/include/sensors/convert.h index 82829d8..53cf922 100644 --- a/hidl/sensors/1.0/include/sensors/convert.h +++ b/hidl/sensors/1.0/include/sensors/convert.h @@ -34,6 +34,8 @@ void convertToSensorEvent(const Event& src, sensors_event_t* dst); bool convertFromSharedMemInfo(const SharedMemInfo& memIn, sensors_direct_mem_t* memOut); int convertFromRateLevel(RateLevel rate); +bool patchXiaomiPickupSensor(SensorInfo& sensor); + } // namespace implementation } // namespace V1_0 } // namespace sensors