Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e8a88834 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topic "sensor_background_apps" into rvc-dev

* changes:
  Fail on flush if no sensors are found
  Disable sensors when an app goes to background
  Convert KeyedVector to std::ordered_map
parents a02bd194 6eb75be7
Loading
Loading
Loading
Loading
+121 −56
Original line number Original line Diff line number Diff line
@@ -14,6 +14,7 @@
 * limitations under the License.
 * limitations under the License.
 */
 */


#include <cinttypes>
#include <sys/socket.h>
#include <sys/socket.h>
#include <utils/threads.h>
#include <utils/threads.h>


@@ -69,17 +70,17 @@ void SensorService::SensorEventConnection::onFirstRef() {
}
}


bool SensorService::SensorEventConnection::needsWakeLock() {
bool SensorService::SensorEventConnection::needsWakeLock() {
    Mutex::Autolock _l(mConnectionLock);
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    return !mDead && mWakeLockRefCount > 0;
    return !mDead && mWakeLockRefCount > 0;
}
}


void SensorService::SensorEventConnection::resetWakeLockRefCount() {
void SensorService::SensorEventConnection::resetWakeLockRefCount() {
    Mutex::Autolock _l(mConnectionLock);
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    mWakeLockRefCount = 0;
    mWakeLockRefCount = 0;
}
}


void SensorService::SensorEventConnection::dump(String8& result) {
void SensorService::SensorEventConnection::dump(String8& result) {
    Mutex::Autolock _l(mConnectionLock);
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    result.appendFormat("\tOperating Mode: ");
    result.appendFormat("\tOperating Mode: ");
    if (!mService->isWhiteListedPackage(getPackageName())) {
    if (!mService->isWhiteListedPackage(getPackageName())) {
        result.append("RESTRICTED\n");
        result.append("RESTRICTED\n");
@@ -91,11 +92,11 @@ void SensorService::SensorEventConnection::dump(String8& result) {
    result.appendFormat("\t %s | WakeLockRefCount %d | uid %d | cache size %d | "
    result.appendFormat("\t %s | WakeLockRefCount %d | uid %d | cache size %d | "
            "max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize,
            "max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize,
            mMaxCacheSize);
            mMaxCacheSize);
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (auto& it : mSensorInfo) {
        const FlushInfo& flushInfo = mSensorInfo.valueAt(i);
        const FlushInfo& flushInfo = it.second.flushInfo;
        result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n",
        result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n",
                            mService->getSensorName(mSensorInfo.keyAt(i)).string(),
                            mService->getSensorName(it.first).string(),
                            mSensorInfo.keyAt(i),
                            it.first,
                            flushInfo.mFirstFlushPending ? "First flush pending" :
                            flushInfo.mFirstFlushPending ? "First flush pending" :
                                                           "active",
                                                           "active",
                            flushInfo.mPendingFlushEventsToSend);
                            flushInfo.mPendingFlushEventsToSend);
@@ -121,7 +122,7 @@ void SensorService::SensorEventConnection::dump(String8& result) {
 */
 */
void SensorService::SensorEventConnection::dump(util::ProtoOutputStream* proto) const {
void SensorService::SensorEventConnection::dump(util::ProtoOutputStream* proto) const {
    using namespace service::SensorEventConnectionProto;
    using namespace service::SensorEventConnectionProto;
    Mutex::Autolock _l(mConnectionLock);
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);


    if (!mService->isWhiteListedPackage(getPackageName())) {
    if (!mService->isWhiteListedPackage(getPackageName())) {
        proto->write(OPERATING_MODE, OP_MODE_RESTRICTED);
        proto->write(OPERATING_MODE, OP_MODE_RESTRICTED);
@@ -135,12 +136,12 @@ void SensorService::SensorEventConnection::dump(util::ProtoOutputStream* proto)
    proto->write(UID, int32_t(mUid));
    proto->write(UID, int32_t(mUid));
    proto->write(CACHE_SIZE, int32_t(mCacheSize));
    proto->write(CACHE_SIZE, int32_t(mCacheSize));
    proto->write(MAX_CACHE_SIZE, int32_t(mMaxCacheSize));
    proto->write(MAX_CACHE_SIZE, int32_t(mMaxCacheSize));
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (auto& it : mSensorInfo) {
        const FlushInfo& flushInfo = mSensorInfo.valueAt(i);
        const FlushInfo& flushInfo = it.second.flushInfo;
        const uint64_t token = proto->start(FLUSH_INFOS);
        const uint64_t token = proto->start(FLUSH_INFOS);
        proto->write(FlushInfoProto::SENSOR_NAME,
        proto->write(FlushInfoProto::SENSOR_NAME,
                std::string(mService->getSensorName(mSensorInfo.keyAt(i))));
                std::string(mService->getSensorName(it.first)));
        proto->write(FlushInfoProto::SENSOR_HANDLE, mSensorInfo.keyAt(i));
        proto->write(FlushInfoProto::SENSOR_HANDLE, it.first);
        proto->write(FlushInfoProto::FIRST_FLUSH_PENDING, flushInfo.mFirstFlushPending);
        proto->write(FlushInfoProto::FIRST_FLUSH_PENDING, flushInfo.mFirstFlushPending);
        proto->write(FlushInfoProto::PENDING_FLUSH_EVENTS_TO_SEND,
        proto->write(FlushInfoProto::PENDING_FLUSH_EVENTS_TO_SEND,
                flushInfo.mPendingFlushEventsToSend);
                flushInfo.mPendingFlushEventsToSend);
@@ -157,40 +158,54 @@ void SensorService::SensorEventConnection::dump(util::ProtoOutputStream* proto)
#endif
#endif
}
}


bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
bool SensorService::SensorEventConnection::addSensor(
    Mutex::Autolock _l(mConnectionLock);
    int32_t handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags) {
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
    sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
    if (si == nullptr ||
    if (si == nullptr ||
        !canAccessSensor(si->getSensor(), "Tried adding", mOpPackageName) ||
        !canAccessSensor(si->getSensor(), "Tried adding", mOpPackageName) ||
        mSensorInfo.indexOfKey(handle) >= 0) {
        mSensorInfo.count(handle) > 0) {
        return false;
        return false;
    }
    }
    mSensorInfo.add(handle, FlushInfo());

    SensorRequest request = {
      .samplingPeriodNs = samplingPeriodNs,
      .maxBatchReportLatencyNs = maxBatchReportLatencyNs,
      .reservedFlags = reservedFlags
    };

    mSensorInfo[handle] = request;
    return true;
    return true;
}
}


bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
    Mutex::Autolock _l(mConnectionLock);
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    if (mSensorInfo.removeItem(handle) >= 0) {
    return mSensorInfo.erase(handle) > 0;
        return true;
}
}
    return false;

std::vector<int32_t> SensorService::SensorEventConnection::getActiveSensorHandles() const {
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    std::vector<int32_t> list;
    for (auto& it : mSensorInfo) {
        list.push_back(it.first);
    }
    return list;
}
}


bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
    Mutex::Autolock _l(mConnectionLock);
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    return mSensorInfo.indexOfKey(handle) >= 0;
    return mSensorInfo.count(handle) + mSensorInfoBackup.count(handle) > 0;
}
}


bool SensorService::SensorEventConnection::hasAnySensor() const {
bool SensorService::SensorEventConnection::hasAnySensor() const {
    Mutex::Autolock _l(mConnectionLock);
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    return mSensorInfo.size() ? true : false;
    return mSensorInfo.size() + mSensorInfoBackup.size() ? true : false;
}
}


bool SensorService::SensorEventConnection::hasOneShotSensors() const {
bool SensorService::SensorEventConnection::hasOneShotSensors() const {
    Mutex::Autolock _l(mConnectionLock);
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (auto &it : mSensorInfo) {
        const int handle = mSensorInfo.keyAt(i);
        const int handle = it.first;
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
        if (si != nullptr && si->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
        if (si != nullptr && si->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
            return true;
            return true;
@@ -205,16 +220,15 @@ String8 SensorService::SensorEventConnection::getPackageName() const {


void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
                                bool value) {
                                bool value) {
    Mutex::Autolock _l(mConnectionLock);
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    ssize_t index = mSensorInfo.indexOfKey(handle);
    if (mSensorInfo.count(handle) > 0) {
    if (index >= 0) {
        FlushInfo& flushInfo = mSensorInfo[handle].flushInfo;
        FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
        flushInfo.mFirstFlushPending = value;
        flushInfo.mFirstFlushPending = value;
    }
    }
}
}


void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) {
void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) {
    Mutex::Autolock _l(mConnectionLock);
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    updateLooperRegistrationLocked(looper);
    updateLooperRegistrationLocked(looper);
}
}


@@ -233,8 +247,8 @@ void SensorService::SensorEventConnection::updateLooperRegistrationLocked(
    int looper_flags = 0;
    int looper_flags = 0;
    if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT;
    if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT;
    if (mDataInjectionMode) looper_flags |= ALOOPER_EVENT_INPUT;
    if (mDataInjectionMode) looper_flags |= ALOOPER_EVENT_INPUT;
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (auto& it : mSensorInfo) {
        const int handle = mSensorInfo.keyAt(i);
        const int handle = it.first;
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
        if (si != nullptr && si->getSensor().isWakeUpSensor()) {
        if (si != nullptr && si->getSensor().isWakeUpSensor()) {
            looper_flags |= ALOOPER_EVENT_INPUT;
            looper_flags |= ALOOPER_EVENT_INPUT;
@@ -265,10 +279,9 @@ void SensorService::SensorEventConnection::updateLooperRegistrationLocked(
}
}


void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) {
void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) {
    Mutex::Autolock _l(mConnectionLock);
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    ssize_t index = mSensorInfo.indexOfKey(handle);
    if (mSensorInfo.count(handle) > 0) {
    if (index >= 0) {
        FlushInfo& flushInfo = mSensorInfo[handle].flushInfo;
        FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
        flushInfo.mPendingFlushEventsToSend++;
        flushInfo.mPendingFlushEventsToSend++;
    }
    }
}
}
@@ -282,7 +295,7 @@ status_t SensorService::SensorEventConnection::sendEvents(
    std::unique_ptr<sensors_event_t[]> sanitizedBuffer;
    std::unique_ptr<sensors_event_t[]> sanitizedBuffer;


    int count = 0;
    int count = 0;
    Mutex::Autolock _l(mConnectionLock);
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    if (scratch) {
    if (scratch) {
        size_t i=0;
        size_t i=0;
        while (i<numEvents) {
        while (i<numEvents) {
@@ -296,15 +309,14 @@ status_t SensorService::SensorEventConnection::sendEvents(
                sensor_handle = buffer[i].meta_data.sensor;
                sensor_handle = buffer[i].meta_data.sensor;
            }
            }


            ssize_t index = mSensorInfo.indexOfKey(sensor_handle);
            // Check if this connection has registered for this sensor. If not continue to the
            // Check if this connection has registered for this sensor. If not continue to the
            // next sensor_event.
            // next sensor_event.
            if (index < 0) {
            if (mSensorInfo.count(sensor_handle) == 0) {
                ++i;
                ++i;
                continue;
                continue;
            }
            }


            FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
            FlushInfo& flushInfo = mSensorInfo[sensor_handle].flushInfo;
            // Check if there is a pending flush_complete event for this sensor on this connection.
            // Check if there is a pending flush_complete event for this sensor on this connection.
            if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true &&
            if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true &&
                    mapFlushEventsToConnections[i] == this) {
                    mapFlushEventsToConnections[i] == this) {
@@ -425,9 +437,55 @@ status_t SensorService::SensorEventConnection::sendEvents(
    return size < 0 ? status_t(size) : status_t(NO_ERROR);
    return size < 0 ? status_t(size) : status_t(NO_ERROR);
}
}


void SensorService::SensorEventConnection::updateSensorSubscriptions() {
    if (!hasSensorAccess()) {
        stopAll();
    } else {
        recoverAll();
    }
}

void SensorService::SensorEventConnection::setSensorAccess(const bool hasAccess) {
void SensorService::SensorEventConnection::setSensorAccess(const bool hasAccess) {
    Mutex::Autolock _l(mConnectionLock);
    if (mHasSensorAccess != hasAccess) {
        mHasSensorAccess = hasAccess;
        mHasSensorAccess = hasAccess;
        updateSensorSubscriptions();
    }
}

void SensorService::SensorEventConnection::stopAll() {
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    if (!mSensorInfo.empty()) {
        mSensorInfoBackup = mSensorInfo;
        mSensorInfo.clear();

        for (auto& it : mSensorInfoBackup) {
            int32_t handle = it.first;

            status_t err =  mService->disable(this, handle);

            if (err != NO_ERROR) {
                ALOGE("Error disabling sensor %d", handle);
            }
        }
    }
}

void SensorService::SensorEventConnection::recoverAll() {
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    for (auto& it : mSensorInfoBackup) {
        int32_t handle = it.first;
        SensorRequest &request = it.second;

        status_t err =  mService->enable(
            this, handle, request.samplingPeriodNs, request.maxBatchReportLatencyNs,
            request.reservedFlags, mOpPackageName);

        if (err != NO_ERROR) {
            ALOGE("Error recovering sensor %d", handle);
        }
    }

    mSensorInfoBackup.clear();
}
}


bool SensorService::SensorEventConnection::hasSensorAccess() {
bool SensorService::SensorEventConnection::hasSensorAccess() {
@@ -522,14 +580,14 @@ void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() {
    flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
    flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
    // Loop through all the sensors for this connection and check if there are any pending
    // Loop through all the sensors for this connection and check if there are any pending
    // flush complete events to be sent.
    // flush complete events to be sent.
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (auto& it : mSensorInfo) {
        const int handle = mSensorInfo.keyAt(i);
        const int handle = it.first;
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
        if (si == nullptr) {
        if (si == nullptr) {
            continue;
            continue;
        }
        }


        FlushInfo& flushInfo = mSensorInfo.editValueAt(i);
        FlushInfo& flushInfo = it.second.flushInfo;
        while (flushInfo.mPendingFlushEventsToSend > 0) {
        while (flushInfo.mPendingFlushEventsToSend > 0) {
            flushCompleteEvent.meta_data.sensor = handle;
            flushCompleteEvent.meta_data.sensor = handle;
            bool wakeUpSensor = si->getSensor().isWakeUpSensor();
            bool wakeUpSensor = si->getSensor().isWakeUpSensor();
@@ -554,7 +612,7 @@ void SensorService::SensorEventConnection::writeToSocketFromCache() {
    // half the size of the socket buffer allocated in BitTube whichever is smaller.
    // half the size of the socket buffer allocated in BitTube whichever is smaller.
    const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2,
    const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2,
            int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2)));
            int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2)));
    Mutex::Autolock _l(mConnectionLock);
    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    // Send pending flush complete events (if any)
    // Send pending flush complete events (if any)
    sendPendingFlushEventsLocked();
    sendPendingFlushEventsLocked();
    for (int numEventsSent = 0; numEventsSent < mCacheSize;) {
    for (int numEventsSent = 0; numEventsSent < mCacheSize;) {
@@ -615,14 +673,13 @@ void SensorService::SensorEventConnection::countFlushCompleteEventsLocked(
    // separately before the next batch of events.
    // separately before the next batch of events.
    for (int j = 0; j < numEventsDropped; ++j) {
    for (int j = 0; j < numEventsDropped; ++j) {
        if (scratch[j].type == SENSOR_TYPE_META_DATA) {
        if (scratch[j].type == SENSOR_TYPE_META_DATA) {
            ssize_t index = mSensorInfo.indexOfKey(scratch[j].meta_data.sensor);
            if (mSensorInfo.count(scratch[j].meta_data.sensor) == 0) {
            if (index < 0) {
                ALOGW("%s: sensor 0x%x is not found in connection",
                ALOGW("%s: sensor 0x%x is not found in connection",
                      __func__, scratch[j].meta_data.sensor);
                      __func__, scratch[j].meta_data.sensor);
                continue;
                continue;
            }
            }


            FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
            FlushInfo& flushInfo = mSensorInfo[scratch[j].meta_data.sensor].flushInfo;
            flushInfo.mPendingFlushEventsToSend++;
            flushInfo.mPendingFlushEventsToSend++;
            ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
            ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
                     flushInfo.mPendingFlushEventsToSend);
                     flushInfo.mPendingFlushEventsToSend);
@@ -658,13 +715,21 @@ status_t SensorService::SensorEventConnection::enableDisable(
    } else {
    } else {
        err = mService->disable(this, handle);
        err = mService->disable(this, handle);
    }
    }

    return err;
    return err;
}
}


status_t SensorService::SensorEventConnection::setEventRate(
status_t SensorService::SensorEventConnection::setEventRate(
        int handle, nsecs_t samplingPeriodNs)
        int handle, nsecs_t samplingPeriodNs)
{
{
    return mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName);
    status_t err = mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName);

    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
    if (err == NO_ERROR && mSensorInfo.count(handle) > 0) {
        mSensorInfo[handle].samplingPeriodNs = samplingPeriodNs;
    }

    return err;
}
}


status_t  SensorService::SensorEventConnection::flush() {
status_t  SensorService::SensorEventConnection::flush() {
@@ -685,7 +750,7 @@ int SensorService::SensorEventConnection::handleEvent(int fd, int events, void*
            // and remove the fd from Looper. Call checkWakeLockState to know if SensorService
            // and remove the fd from Looper. Call checkWakeLockState to know if SensorService
            // can release the wake-lock.
            // can release the wake-lock.
            ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd);
            ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd);
            Mutex::Autolock _l(mConnectionLock);
            std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
            mDead = true;
            mDead = true;
            mWakeLockRefCount = 0;
            mWakeLockRefCount = 0;
            updateLooperRegistrationLocked(mService->getLooper());
            updateLooperRegistrationLocked(mService->getLooper());
@@ -704,7 +769,7 @@ int SensorService::SensorEventConnection::handleEvent(int fd, int events, void*
        unsigned char buf[sizeof(sensors_event_t)];
        unsigned char buf[sizeof(sensors_event_t)];
        ssize_t numBytesRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
        ssize_t numBytesRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
        {
        {
            Mutex::Autolock _l(mConnectionLock);
            std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
            if (numBytesRead == sizeof(sensors_event_t)) {
            if (numBytesRead == sizeof(sensors_event_t)) {
                if (!mDataInjectionMode) {
                if (!mDataInjectionMode) {
                    ALOGE("Data injected in normal mode, dropping event"
                    ALOGE("Data injected in normal mode, dropping event"
@@ -764,8 +829,8 @@ int SensorService::SensorEventConnection::handleEvent(int fd, int events, void*
int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const {
int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const {
    size_t fifoWakeUpSensors = 0;
    size_t fifoWakeUpSensors = 0;
    size_t fifoNonWakeUpSensors = 0;
    size_t fifoNonWakeUpSensors = 0;
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (auto& it : mSensorInfo) {
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(mSensorInfo.keyAt(i));
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(it.first);
        if (si == nullptr) {
        if (si == nullptr) {
            continue;
            continue;
        }
        }
+20 −7
Original line number Original line Diff line number Diff line
@@ -23,7 +23,6 @@


#include <utils/Vector.h>
#include <utils/Vector.h>
#include <utils/SortedVector.h>
#include <utils/SortedVector.h>
#include <utils/KeyedVector.h>
#include <utils/threads.h>
#include <utils/threads.h>
#include <utils/AndroidThreads.h>
#include <utils/AndroidThreads.h>
#include <utils/RefBase.h>
#include <utils/RefBase.h>
@@ -58,8 +57,10 @@ public:
    bool hasSensor(int32_t handle) const;
    bool hasSensor(int32_t handle) const;
    bool hasAnySensor() const;
    bool hasAnySensor() const;
    bool hasOneShotSensors() const;
    bool hasOneShotSensors() const;
    bool addSensor(int32_t handle);
    bool addSensor(
        int32_t handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags);
    bool removeSensor(int32_t handle);
    bool removeSensor(int32_t handle);
    std::vector<int32_t> getActiveSensorHandles() const;
    void setFirstFlushPending(int32_t handle, bool value);
    void setFirstFlushPending(int32_t handle, bool value);
    void dump(String8& result);
    void dump(String8& result);
    void dump(util::ProtoOutputStream* proto) const;
    void dump(util::ProtoOutputStream* proto) const;
@@ -70,7 +71,7 @@ public:
    uid_t getUid() const { return mUid; }
    uid_t getUid() const { return mUid; }


    void setSensorAccess(const bool hasAccess);
    void setSensorAccess(const bool hasAccess);

    void updateSensorSubscriptions();
private:
private:
    virtual ~SensorEventConnection();
    virtual ~SensorEventConnection();
    virtual void onFirstRef();
    virtual void onFirstRef();
@@ -136,13 +137,16 @@ private:
    // privacy not being enabled.
    // privacy not being enabled.
    bool hasSensorAccess();
    bool hasSensorAccess();


    void stopAll();
    void recoverAll();

    // Call noteOp for the sensor if the sensor requires a permission
    // Call noteOp for the sensor if the sensor requires a permission
    bool noteOpIfRequired(const sensors_event_t& event);
    bool noteOpIfRequired(const sensors_event_t& event);


    sp<SensorService> const mService;
    sp<SensorService> const mService;
    sp<BitTube> mChannel;
    sp<BitTube> mChannel;
    uid_t mUid;
    uid_t mUid;
    mutable Mutex mConnectionLock;
    mutable std::recursive_mutex mConnectionLock;
    // Number of events from wake up sensors which are still pending and haven't been delivered to
    // Number of events from wake up sensors which are still pending and haven't been delivered to
    // the corresponding application. It is incremented by one unit for each write to the socket.
    // the corresponding application. It is incremented by one unit for each write to the socket.
    uint32_t mWakeLockRefCount;
    uint32_t mWakeLockRefCount;
@@ -169,8 +173,17 @@ private:


        FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
        FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
    };
    };
    // protected by SensorService::mLock. Key for this vector is the sensor handle.

    KeyedVector<int, FlushInfo> mSensorInfo;
    struct SensorRequest {
      nsecs_t samplingPeriodNs;
      nsecs_t maxBatchReportLatencyNs;
      int reservedFlags;
      FlushInfo flushInfo;
    };

    // protected by SensorService::mLock. Key for this map is the sensor handle.
    std::unordered_map<int32_t, SensorRequest> mSensorInfo;
    std::unordered_map<int32_t, SensorRequest> mSensorInfoBackup;


    sensors_event_t *mEventCache;
    sensors_event_t *mEventCache;
    int mCacheSize, mMaxCacheSize;
    int mCacheSize, mMaxCacheSize;
@@ -185,7 +198,7 @@ private:


    mutable Mutex mDestroyLock;
    mutable Mutex mDestroyLock;
    bool mDestroyed;
    bool mDestroyed;
    bool mHasSensorAccess;
    std::atomic_bool mHasSensorAccess;


    // Store a mapping of sensor handles to required AppOp for a sensor. This map only contains a
    // Store a mapping of sensor handles to required AppOp for a sensor. This map only contains a
    // valid mapping for sensors that require a permission in order to reduce the lookup time.
    // valid mapping for sensors that require a permission in order to reduce the lookup time.
+19 −5
Original line number Original line Diff line number Diff line
@@ -301,7 +301,10 @@ void SensorService::onFirstRef() {


void SensorService::setSensorAccess(uid_t uid, bool hasAccess) {
void SensorService::setSensorAccess(uid_t uid, bool hasAccess) {
    ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock);
    ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock);
    for (const sp<SensorEventConnection>& conn : connLock.getActiveConnections()) {
    const auto& connections = connLock.getActiveConnections();

    mLock.unlock();
    for (const sp<SensorEventConnection>& conn : connections) {
        if (conn->getUid() == uid) {
        if (conn->getUid() == uid) {
            conn->setSensorAccess(hasAccess);
            conn->setSensorAccess(hasAccess);
        }
        }
@@ -638,6 +641,9 @@ void SensorService::disableAllSensors() {


void SensorService::disableAllSensorsLocked(ConnectionSafeAutolock* connLock) {
void SensorService::disableAllSensorsLocked(ConnectionSafeAutolock* connLock) {
    SensorDevice& dev(SensorDevice::getInstance());
    SensorDevice& dev(SensorDevice::getInstance());
    for (const sp<SensorEventConnection>& connection : connLock->getActiveConnections()) {
        connection->updateSensorSubscriptions();
    }
    for (const sp<SensorDirectConnection>& connection : connLock->getDirectConnections()) {
    for (const sp<SensorDirectConnection>& connection : connLock->getDirectConnections()) {
        connection->stopAll(true /* backupRecord */);
        connection->stopAll(true /* backupRecord */);
    }
    }
@@ -666,6 +672,9 @@ void SensorService::enableAllSensorsLocked(ConnectionSafeAutolock* connLock) {
    }
    }
    SensorDevice& dev(SensorDevice::getInstance());
    SensorDevice& dev(SensorDevice::getInstance());
    dev.enableAllSensors();
    dev.enableAllSensors();
    for (const sp<SensorEventConnection>& connection : connLock->getActiveConnections()) {
        connection->updateSensorSubscriptions();
    }
    for (const sp<SensorDirectConnection>& connection : connLock->getDirectConnections()) {
    for (const sp<SensorDirectConnection>& connection : connLock->getDirectConnections()) {
        connection->recoverAll();
        connection->recoverAll();
    }
    }
@@ -1589,7 +1598,7 @@ status_t SensorService::enable(const sp<SensorEventConnection>& connection,
        }
        }
    }
    }


    if (connection->addSensor(handle)) {
    if (connection->addSensor(handle, samplingPeriodNs, maxBatchReportLatencyNs, reservedFlags)) {
        BatteryService::enableSensor(connection->getUid(), handle);
        BatteryService::enableSensor(connection->getUid(), handle);
        // the sensor was added (which means it wasn't already there)
        // the sensor was added (which means it wasn't already there)
        // so, see if this connection becomes active
        // so, see if this connection becomes active
@@ -1739,18 +1748,22 @@ status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection,
    const int halVersion = dev.getHalDeviceVersion();
    const int halVersion = dev.getHalDeviceVersion();
    status_t err(NO_ERROR);
    status_t err(NO_ERROR);
    Mutex::Autolock _l(mLock);
    Mutex::Autolock _l(mLock);

    size_t numSensors = 0;
    // Loop through all sensors for this connection and call flush on each of them.
    // Loop through all sensors for this connection and call flush on each of them.
    for (size_t i = 0; i < connection->mSensorInfo.size(); ++i) {
    for (int handle : connection->getActiveSensorHandles()) {
        const int handle = connection->mSensorInfo.keyAt(i);
        sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle);
        sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle);
        if (sensor == nullptr) {
        if (sensor == nullptr) {
            continue;
            continue;
        }
        }
        numSensors++;

        if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
        if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
            ALOGE("flush called on a one-shot sensor");
            ALOGE("flush called on a one-shot sensor");
            err = INVALID_OPERATION;
            err = INVALID_OPERATION;
            continue;
            continue;
        }
        }

        if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0 || isVirtualSensor(handle)) {
        if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0 || isVirtualSensor(handle)) {
            // For older devices just increment pending flush count which will send a trivial
            // For older devices just increment pending flush count which will send a trivial
            // flush complete event.
            // flush complete event.
@@ -1768,7 +1781,8 @@ status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection,
            err = (err_flush != NO_ERROR) ? err_flush : err;
            err = (err_flush != NO_ERROR) ? err_flush : err;
        }
        }
    }
    }
    return err;

    return (numSensors == 0) ? INVALID_OPERATION : err;
}
}


bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation,
bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation,