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

Commit df549ea5 authored by Anh Pham's avatar Anh Pham
Browse files

Revert "Resample sensor events for non-direct connections."

This reverts commit 532e655e.
See discussion at b/179649922.

Test: atest CtsSensorTestCases CtsSensorRatePermissionTestCases
Bug: 136069189
Bug: 179649922
Change-Id: I4f6957428b4ac438a1cc9d7ba3f333f7f43d2f16
parent 0fd5362a
Loading
Loading
Loading
Loading
+5 −31
Original line number Original line Diff line number Diff line
@@ -287,29 +287,6 @@ bool SensorService::SensorEventConnection::incrementPendingFlushCountIfHasAccess
    }
    }
}
}


// TODO(b/179649922): A better algorithm to guarantee that capped connections will get a sampling
// rate close to 200 Hz. With the current algorithm, apps might be punished unfairly: E.g.,two apps
// make requests to the sensor service at the same time, one is not capped and uses 250 Hz, and one
//is capped, the capped connection will only get 125 Hz.
void SensorService::SensorEventConnection::addSensorEventsToBuffer(bool shouldResample,
    const sensors_event_t& sensorEvent, sensors_event_t* buffer, int* index) {
    if (!shouldResample || !mService->isSensorInCappedSet(sensorEvent.type)) {
        buffer[(*index)++] = sensorEvent;
    } else {
        int64_t lastTimestamp = -1;
        auto entry = mSensorLastTimestamp.find(sensorEvent.sensor);
        if (entry != mSensorLastTimestamp.end()) {
            lastTimestamp = entry->second;
        }
        // Allow 10% headroom here because the clocks are not perfect.
        if (lastTimestamp == -1  || sensorEvent.timestamp - lastTimestamp
                                        >= 0.9 * SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS) {
            mSensorLastTimestamp[sensorEvent.sensor] = sensorEvent.timestamp;
            buffer[(*index)++] = sensorEvent;
        }
    }
}

status_t SensorService::SensorEventConnection::sendEvents(
status_t SensorService::SensorEventConnection::sendEvents(
        sensors_event_t const* buffer, size_t numEvents,
        sensors_event_t const* buffer, size_t numEvents,
        sensors_event_t* scratch,
        sensors_event_t* scratch,
@@ -318,8 +295,6 @@ status_t SensorService::SensorEventConnection::sendEvents(


    std::unique_ptr<sensors_event_t[]> sanitizedBuffer;
    std::unique_ptr<sensors_event_t[]> sanitizedBuffer;


    bool shouldResample = mService->isMicSensorPrivacyEnabledForUid(mUid) ||
                            mIsRateCappedBasedOnPermission;
    int count = 0;
    int count = 0;
    Mutex::Autolock _l(mConnectionLock);
    Mutex::Autolock _l(mConnectionLock);
    if (scratch) {
    if (scratch) {
@@ -373,7 +348,7 @@ status_t SensorService::SensorEventConnection::sendEvents(
                    // Regular sensor event, just copy it to the scratch buffer after checking
                    // Regular sensor event, just copy it to the scratch buffer after checking
                    // the AppOp.
                    // the AppOp.
                    if (hasSensorAccess() && noteOpIfRequired(buffer[i])) {
                    if (hasSensorAccess() && noteOpIfRequired(buffer[i])) {
                        addSensorEventsToBuffer(shouldResample, buffer[i], scratch, &count);
                        scratch[count++] = buffer[i];
                    }
                    }
                }
                }
                i++;
                i++;
@@ -383,13 +358,12 @@ status_t SensorService::SensorEventConnection::sendEvents(
                                        buffer[i].meta_data.sensor == sensor_handle)));
                                        buffer[i].meta_data.sensor == sensor_handle)));
        }
        }
    } else {
    } else {
        sanitizedBuffer.reset(new sensors_event_t[numEvents]);
        scratch = sanitizedBuffer.get();
        if (hasSensorAccess()) {
        if (hasSensorAccess()) {
            for (size_t i = 0; i < numEvents; i++) {
            scratch = const_cast<sensors_event_t *>(buffer);
                addSensorEventsToBuffer(shouldResample, buffer[i], scratch, &count);
            count = numEvents;
            }
        } else {
        } else {
            sanitizedBuffer.reset(new sensors_event_t[numEvents]);
            scratch = sanitizedBuffer.get();
            for (size_t i = 0; i < numEvents; i++) {
            for (size_t i = 0; i < numEvents; i++) {
                if (buffer[i].type == SENSOR_TYPE_META_DATA) {
                if (buffer[i].type == SENSOR_TYPE_META_DATA) {
                    scratch[count++] = buffer[i++];
                    scratch[count++] = buffer[i++];
+0 −6
Original line number Original line Diff line number Diff line
@@ -145,16 +145,10 @@ private:
    void capRates();
    void capRates();
    // Recover sensor connection previously capped by capRates().
    // Recover sensor connection previously capped by capRates().
    void uncapRates();
    void uncapRates();

    // Add sensorEvent to buffer at position index if the sensorEvent satisfies throttling rules.
    void addSensorEventsToBuffer(bool shouldResample, const sensors_event_t& sensorEvent,
                        sensors_event_t* buffer, int* index);
    sp<SensorService> const mService;
    sp<SensorService> const mService;
    sp<BitTube> mChannel;
    sp<BitTube> mChannel;
    uid_t mUid;
    uid_t mUid;
    std::atomic_bool mIsRateCappedBasedOnPermission;
    std::atomic_bool mIsRateCappedBasedOnPermission;
    // Store a mapping of sensor to the timestamp of their last sensor event.
    std::unordered_map<int, int64_t> mSensorLastTimestamp;
    mutable Mutex mConnectionLock;
    mutable 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.