diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2017-09-17 07:32:55 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2017-09-17 07:32:55 +0000 |
commit | b1a8e55ffcff1b5cc8f3aba33d24fa05f45dab5b (patch) | |
tree | 73ce78563918aa5aee8b10a01d98e198e5b554a3 | |
parent | 6b59d39098b625f75600f221f376ed7e1b87e5b3 (diff) | |
parent | 8006ae6dd68df939763be951e9b954dab7b153e5 (diff) | |
download | contexthub-security-oc-mr1-release.tar.gz |
release-request-1f2fcfef-9736-44dc-8628-3ba96dac60db-for-git_oc-mr1-release-4343541 snap-temp-L73700000103533431android-wear-8.1.0_r1android-vts-8.1_r9android-vts-8.1_r8android-vts-8.1_r7android-vts-8.1_r6android-vts-8.1_r5android-vts-8.1_r4android-vts-8.1_r3android-vts-8.1_r14android-vts-8.1_r13android-vts-8.1_r12android-vts-8.1_r11android-vts-8.1_r10android-security-8.1.0_r93android-security-8.1.0_r92android-security-8.1.0_r91android-security-8.1.0_r90android-security-8.1.0_r89android-security-8.1.0_r88android-security-8.1.0_r87android-security-8.1.0_r86android-security-8.1.0_r85android-security-8.1.0_r84android-security-8.1.0_r83android-security-8.1.0_r82android-cts-8.1_r9android-cts-8.1_r8android-cts-8.1_r7android-cts-8.1_r6android-cts-8.1_r5android-cts-8.1_r4android-cts-8.1_r3android-cts-8.1_r25android-cts-8.1_r24android-cts-8.1_r23android-cts-8.1_r22android-cts-8.1_r21android-cts-8.1_r20android-cts-8.1_r2android-cts-8.1_r19android-cts-8.1_r18android-cts-8.1_r17android-cts-8.1_r16android-cts-8.1_r15android-cts-8.1_r14android-cts-8.1_r13android-cts-8.1_r12android-cts-8.1_r11android-cts-8.1_r10android-cts-8.1_r1android-8.1.0_r81android-8.1.0_r80android-8.1.0_r79android-8.1.0_r78android-8.1.0_r77android-8.1.0_r76android-8.1.0_r75android-8.1.0_r74android-8.1.0_r73android-8.1.0_r72android-8.1.0_r71android-8.1.0_r70android-8.1.0_r69android-8.1.0_r68android-8.1.0_r66android-8.1.0_r6android-8.1.0_r5android-8.1.0_r4android-8.1.0_r3android-8.1.0_r23android-8.1.0_r19android-8.1.0_r16android-8.1.0_r15android-8.1.0_r12android-8.1.0_r11android-8.1.0_r10android-8.1.0_r1security-oc-mr1-releaseoreo-mr1-wear-releaseoreo-mr1-vts-releaseoreo-mr1-security-releaseoreo-mr1-s1-releaseoreo-mr1-releaseoreo-mr1-cuttlefish-testingoreo-mr1-cts-releaseoreo-m4-s1-release
Change-Id: Ib56c71c353d99c0651e4863c3ff0824e44d3a8ab
-rw-r--r-- | sensorhal/hubconnection.cpp | 106 | ||||
-rw-r--r-- | sensorhal/hubconnection.h | 7 | ||||
-rw-r--r-- | sensorhal/sensors.cpp | 23 |
3 files changed, 72 insertions, 64 deletions
diff --git a/sensorhal/hubconnection.cpp b/sensorhal/hubconnection.cpp index 251dcb9e..ba87c152 100644 --- a/sensorhal/hubconnection.cpp +++ b/sensorhal/hubconnection.cpp @@ -114,6 +114,21 @@ static bool isActivitySensor(int sensorIndex) { && sensorIndex <= COMMS_SENSOR_ACTIVITY_LAST; } +static bool isWakeEvent(int32_t sensor) +{ + switch (sensor) { + case COMMS_SENSOR_DOUBLE_TOUCH: + case COMMS_SENSOR_DOUBLE_TWIST: + case COMMS_SENSOR_GESTURE: + case COMMS_SENSOR_PROXIMITY: + case COMMS_SENSOR_SIGNIFICANT_MOTION: + case COMMS_SENSOR_TILT: + return true; + default: + return false; + } +} + HubConnection::HubConnection() : Thread(false /* canCallJava */), mRing(10 *1024), @@ -143,6 +158,7 @@ HubConnection::HubConnection() mWakelockHeld = false; mWakeEventCount = 0; + mWriteFailures = 0; initNanohubLock(); @@ -616,31 +632,20 @@ sensors_event_t *HubConnection::initEv(sensors_event_t *ev, uint64_t timestamp, return ev; } -ssize_t HubConnection::getWakeEventCount() +ssize_t HubConnection::decrementIfWakeEventLocked(int32_t sensor) { - return mWakeEventCount; -} - -ssize_t HubConnection::decrementWakeEventCount() -{ - return --mWakeEventCount; -} - -bool HubConnection::isWakeEvent(int32_t sensor) -{ - switch (sensor) { - case COMMS_SENSOR_PROXIMITY: - case COMMS_SENSOR_SIGNIFICANT_MOTION: - case COMMS_SENSOR_TILT: - case COMMS_SENSOR_DOUBLE_TWIST: - case COMMS_SENSOR_GESTURE: - return true; - default: - return false; + if (isWakeEvent(sensor)) { + if (mWakeEventCount > 0) + mWakeEventCount--; + else + ALOGW("%s: sensor=%d, unexpected count=%d, no-op", + __FUNCTION__, sensor, mWakeEventCount); } + + return mWakeEventCount; } -void HubConnection::protectIfWakeEvent(int32_t sensor) +void HubConnection::protectIfWakeEventLocked(int32_t sensor) { if (isWakeEvent(sensor)) { if (mWakelockHeld == false) { @@ -653,6 +658,8 @@ void HubConnection::protectIfWakeEvent(int32_t sensor) void HubConnection::releaseWakeLockIfAppropriate() { + Mutex::Autolock autoLock(mLock); + if (mWakelockHeld && (mWakeEventCount == 0)) { mWakelockHeld = false; release_wake_lock(WAKELOCK_NAME); @@ -728,11 +735,8 @@ void HubConnection::processSample(uint64_t timestamp, uint32_t type, uint32_t se break; } - if (cnt > 0) { - // If event is a wake event, protect it with a wakelock - protectIfWakeEvent(sensor); + if (cnt > 0) write(nev, cnt); - } } uint8_t HubConnection::magAccuracyUpdate(sensors_vec_t *sv) @@ -830,11 +834,8 @@ void HubConnection::processSample(uint64_t timestamp, uint32_t type, uint32_t se break; } - if (cnt > 0) { - // If event is a wake event, protect it with a wakelock - protectIfWakeEvent(sensor); + if (cnt > 0) write(nev, cnt); - } } void HubConnection::processSample(uint64_t timestamp, uint32_t type, uint32_t sensor, struct ThreeAxisSample *sample, bool highAccuracy) @@ -1021,11 +1022,8 @@ void HubConnection::processSample(uint64_t timestamp, uint32_t type, uint32_t se break; } - if (cnt > 0) { - // If event is a wake event, protect it with a wakelock - protectIfWakeEvent(sensor); + if (cnt > 0) write(nev, cnt); - } } void HubConnection::discardInotifyEvent() { @@ -1659,10 +1657,6 @@ bool HubConnection::threadLoop() { return false; } -ssize_t HubConnection::read(sensors_event_t *ev, size_t size) { - return mRing.read(ev, size); -} - void HubConnection::setActivityCallback(ActivityEventHandler *eventHandler) { Mutex::Autolock autoLock(mLock); @@ -1912,8 +1906,44 @@ void HubConnection::initNanohubLock() { } } +ssize_t HubConnection::read(sensors_event_t *ev, size_t size) { + ssize_t n = mRing.read(ev, size); + + Mutex::Autolock autoLock(mLock); + + // We log the first failure in write, so only log 2+ errors + if (mWriteFailures > 1) { + ALOGW("%s: mRing.write failed %d times", + __FUNCTION__, mWriteFailures); + mWriteFailures = 0; + } + + for (ssize_t i = 0; i < n; i++) + decrementIfWakeEventLocked(ev[i].sensor); + + return n; +} + + ssize_t HubConnection::write(const sensors_event_t *ev, size_t n) { - return mRing.write(ev, n); + ssize_t ret = 0; + + Mutex::Autolock autoLock(mLock); + + for (size_t i=0; i<n; i++) { + if (mRing.write(&ev[i], 1) == 1) { + ret++; + // If event is a wake event, protect it with a wakelock + protectIfWakeEventLocked(ev[i].sensor); + } else { + if (mWriteFailures++ == 0) + ALOGW("%s: mRing.write failed @ %zu/%zu", + __FUNCTION__, i, n); + break; + } + } + + return ret; } #ifdef USB_MAG_BIAS_REPORTING_ENABLED diff --git a/sensorhal/hubconnection.h b/sensorhal/hubconnection.h index 4dbd64e0..388660cf 100644 --- a/sensorhal/hubconnection.h +++ b/sensorhal/hubconnection.h @@ -79,10 +79,7 @@ struct HubConnection : public Thread { void setOperationParameter(const additional_info_event_t &info); - bool isWakeEvent(int32_t sensor); void releaseWakeLockIfAppropriate(); - ssize_t getWakeEventCount(); - ssize_t decrementWakeEventCount(); //TODO: factor out event ring buffer functionality into a separate class ssize_t read(sensors_event_t *ev, size_t size); @@ -111,7 +108,8 @@ private: bool mWakelockHeld; int32_t mWakeEventCount; - void protectIfWakeEvent(int32_t sensor); + void protectIfWakeEventLocked(int32_t sensor); + ssize_t decrementIfWakeEventLocked(int32_t sensor); static inline uint64_t period_ns_to_frequency_q10(nsecs_t period_ns) { return 1024000000000ULL / period_ns; @@ -244,6 +242,7 @@ private: Mutex mLock; RingBuffer mRing; + int32_t mWriteFailures; ActivityEventHandler *mActivityEventHandler; diff --git a/sensorhal/sensors.cpp b/sensorhal/sensors.cpp index 0ddb263b..234f7da3 100644 --- a/sensorhal/sensors.cpp +++ b/sensorhal/sensors.cpp @@ -105,28 +105,7 @@ int SensorContext::poll(sensors_event_t *data, int count) { // Release wakelock if held and no more events in ring buffer mHubConnection->releaseWakeLockIfAppropriate(); - ssize_t n = mHubConnection->read(data, count); - - if (n < 0) { - return -1; - } - - // If we have wake events in the queue, determine how many we're sending - // up this round and decrement that count now so that when we get called back, - // we'll have an accurate count of how many wake events are STILL in the HAL queue - // to be able to determine whether we can release our wakelock if held. - if (mHubConnection->getWakeEventCount() != 0) { - for (ssize_t i = 0; i < n; i++) { - if (mHubConnection->isWakeEvent(data[i].sensor)) { - ssize_t count = mHubConnection->decrementWakeEventCount(); - if (count == 0) { - break; - } - } - } - } - - return n; + return mHubConnection->read(data, count); } int SensorContext::batch( |