From 20483c49377fdb0330d9dfbbb2168b470c0b29d3 Mon Sep 17 00:00:00 2001 From: Peng Xu Date: Mon, 26 Oct 2015 15:14:43 -0700 Subject: [PATCH] Avoiding flush on-change sensors at subscription Initial sensor flush at subscription is a mechanism to avoid sensors to get stale samples before subscription happens. However, there is a slight chance that a most recent sample will be lost during the flush process. This is OK for continuous sensors but problematic in on-change sensor as on-change event does not come continuously and a lost event can cause inconsistent state in client. Flush at subscription of on-change sensor is disabled in this CL to avoid new important on-change event to be discarded during the initial flush process. Bugs: b/24647069 b/25241873 b/24804819 Change-Id: Ibda099c6b9f5fb6e200f13cf13a850b0026e9e7c --- services/sensorservice/SensorService.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/services/sensorservice/SensorService.cpp b/services/sensorservice/SensorService.cpp index 40b21a96a..fd72b2312 100644 --- a/services/sensorservice/SensorService.cpp +++ b/services/sensorservice/SensorService.cpp @@ -912,10 +912,15 @@ status_t SensorService::enable(const sp& connection, status_t err = sensor->batch(connection.get(), handle, 0, samplingPeriodNs, maxBatchReportLatencyNs); - // Call flush() before calling activate() on the sensor. Wait for a first flush complete - // event before sending events on this connection. Ignore one-shot sensors which don't - // support flush(). Also if this sensor isn't already active, don't call flush(). - if (err == NO_ERROR && sensor->getSensor().getReportingMode() != AREPORTING_MODE_ONE_SHOT && + // Call flush() before calling activate() on the sensor. Wait for a first + // flush complete event before sending events on this connection. Ignore + // one-shot sensors which don't support flush(). Ignore on-change sensors + // to maintain the on-change logic (any on-change events except the initial + // one should be trigger by a change in value). Also if this sensor isn't + // already active, don't call flush(). + if (err == NO_ERROR && + sensor->getSensor().getReportingMode() != AREPORTING_MODE_ONE_SHOT && + sensor->getSensor().getReportingMode() != AREPORTING_MODE_ON_CHANGE && rec->getNumConnections() > 1) { connection->setFirstFlushPending(handle, true); status_t err_flush = sensor->flush(connection.get(), handle);