Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_SENSOR_EVENT_CONNECTION_H |
| 18 | #define ANDROID_SENSOR_EVENT_CONNECTION_H |
| 19 | |
Arthur Ishiguro | 3e9afc1 | 2020-09-22 13:05:15 -0700 | [diff] [blame] | 20 | #include <atomic> |
Arthur Ishiguro | 8a62852 | 2021-09-22 14:10:16 -0700 | [diff] [blame] | 21 | #include <optional> |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <sys/types.h> |
Brian Stack | c225aa1 | 2019-04-19 09:30:25 -0700 | [diff] [blame] | 24 | #include <unordered_map> |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 25 | |
| 26 | #include <utils/Vector.h> |
| 27 | #include <utils/SortedVector.h> |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 28 | #include <utils/threads.h> |
| 29 | #include <utils/AndroidThreads.h> |
| 30 | #include <utils/RefBase.h> |
| 31 | #include <utils/Looper.h> |
| 32 | #include <utils/String8.h> |
| 33 | |
| 34 | #include <binder/BinderService.h> |
| 35 | |
Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 36 | #include <sensor/Sensor.h> |
| 37 | #include <sensor/BitTube.h> |
| 38 | #include <sensor/ISensorServer.h> |
| 39 | #include <sensor/ISensorEventConnection.h> |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 40 | |
| 41 | #include "SensorService.h" |
| 42 | |
| 43 | namespace android { |
| 44 | |
| 45 | class SensorService; |
| 46 | |
| 47 | class SensorService::SensorEventConnection: |
| 48 | public BnSensorEventConnection, public LooperCallback { |
| 49 | |
| 50 | friend class SensorService; |
| 51 | |
| 52 | public: |
| 53 | SensorEventConnection(const sp<SensorService>& service, uid_t uid, String8 packageName, |
Arthur Ishiguro | 340882c | 2021-02-18 15:17:44 -0800 | [diff] [blame] | 54 | bool isDataInjectionMode, const String16& opPackageName, |
| 55 | const String16& attributionTag); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 56 | |
| 57 | status_t sendEvents(sensors_event_t const* buffer, size_t count, sensors_event_t* scratch, |
Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 58 | wp<const SensorEventConnection> const * mapFlushEventsToConnections = nullptr); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 59 | bool hasSensor(int32_t handle) const; |
| 60 | bool hasAnySensor() const; |
| 61 | bool hasOneShotSensors() const; |
Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 62 | bool addSensor(int32_t handle); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 63 | bool removeSensor(int32_t handle); |
Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 64 | std::vector<int32_t> getActiveSensorHandles() const; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 65 | void setFirstFlushPending(int32_t handle, bool value); |
| 66 | void dump(String8& result); |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 67 | void dump(util::ProtoOutputStream* proto) const; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 68 | bool needsWakeLock(); |
| 69 | void resetWakeLockRefCount(); |
| 70 | String8 getPackageName() const; |
| 71 | |
| 72 | uid_t getUid() const { return mUid; } |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 73 | // cap/uncap existing connection depending on the state of the mic toggle. |
| 74 | void onMicSensorAccessChanged(bool isMicToggleOn); |
| 75 | userid_t getUserId() const { return mUserId; } |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 76 | |
| 77 | private: |
| 78 | virtual ~SensorEventConnection(); |
| 79 | virtual void onFirstRef(); |
| 80 | virtual sp<BitTube> getSensorChannel() const; |
| 81 | virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs, |
| 82 | nsecs_t maxBatchReportLatencyNs, int reservedFlags); |
| 83 | virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs); |
| 84 | virtual status_t flush(); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 85 | virtual int32_t configureChannel(int handle, int rateLevel); |
Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 86 | virtual void destroy(); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 87 | |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 88 | // Count the number of flush complete events which are about to be dropped in the buffer. |
| 89 | // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be sent |
| 90 | // separately before the next batch of events. |
| 91 | void countFlushCompleteEventsLocked(sensors_event_t const* scratch, int numEventsDropped); |
| 92 | |
| 93 | // Check if there are any wake up events in the buffer. If yes, return the index of the first |
| 94 | // wake_up sensor event in the buffer else return -1. This wake_up sensor event will have the |
| 95 | // flag WAKE_UP_SENSOR_EVENT_NEEDS_ACK set. Exactly one event per packet will have the wake_up |
| 96 | // flag set. SOCK_SEQPACKET ensures that either the entire packet is read or dropped. |
| 97 | int findWakeUpSensorEventLocked(sensors_event_t const* scratch, int count); |
| 98 | |
| 99 | // Send pending flush_complete events. There may have been flush_complete_events that are |
| 100 | // dropped which need to be sent separately before other events. On older HALs (1_0) this method |
| 101 | // emulates the behavior of flush(). |
| 102 | void sendPendingFlushEventsLocked(); |
| 103 | |
| 104 | // Writes events from mEventCache to the socket. |
| 105 | void writeToSocketFromCache(); |
| 106 | |
| 107 | // Compute the approximate cache size from the FIFO sizes of various sensors registered for this |
| 108 | // connection. Wake up and non-wake up sensors have separate FIFOs but FIFO may be shared |
| 109 | // amongst wake-up sensors and non-wake up sensors. |
| 110 | int computeMaxCacheSizeLocked() const; |
| 111 | |
| 112 | // When more sensors register, the maximum cache size desired may change. Compute max cache |
| 113 | // size, reallocate memory and copy over events from the older cache. |
| 114 | void reAllocateCacheLocked(sensors_event_t const* scratch, int count); |
| 115 | |
Brian Stack | 93432ad | 2018-11-27 18:28:48 -0800 | [diff] [blame] | 116 | // Add the events to the cache. If the cache would be exceeded, drop events at the beginning of |
| 117 | // the cache. |
| 118 | void appendEventsToCacheLocked(sensors_event_t const* events, int count); |
| 119 | |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 120 | // LooperCallback method. If there is data to read on this fd, it is an ack from the app that it |
| 121 | // has read events from a wake up sensor, decrement mWakeLockRefCount. If this fd is available |
| 122 | // for writing send the data from the cache. |
| 123 | virtual int handleEvent(int fd, int events, void* data); |
| 124 | |
Stan Rokita | 29adc8c | 2020-07-06 17:38:03 -0700 | [diff] [blame] | 125 | // Increment mPendingFlushEventsToSend for the given handle if the connection has sensor access. |
| 126 | // Returns true if this connection does have sensor access. |
| 127 | bool incrementPendingFlushCountIfHasAccess(int32_t handle); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 128 | |
| 129 | // Add or remove the file descriptor associated with the BitTube to the looper. If mDead is set |
| 130 | // to true or there are no more sensors for this connection, the file descriptor is removed if |
| 131 | // it has been previously added to the Looper. Depending on the state of the connection FD may |
| 132 | // be added to the Looper. The flags to set are determined by the internal state of the |
| 133 | // connection. FDs are added to the looper when wake-up sensors are registered (to poll for |
| 134 | // acknowledgements) and when write fails on the socket when there are too many error and the |
| 135 | // other end hangs up or when this client unregisters for this connection. |
| 136 | void updateLooperRegistration(const sp<Looper>& looper); void |
| 137 | updateLooperRegistrationLocked(const sp<Looper>& looper); |
| 138 | |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 139 | // Returns whether sensor access is available based on both the uid being active and sensor |
| 140 | // privacy not being enabled. |
| 141 | bool hasSensorAccess(); |
| 142 | |
Brian Stack | c225aa1 | 2019-04-19 09:30:25 -0700 | [diff] [blame] | 143 | // Call noteOp for the sensor if the sensor requires a permission |
| 144 | bool noteOpIfRequired(const sensors_event_t& event); |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 145 | // Limits all active connections when the mic toggle is flipped to on. |
| 146 | void capRates(); |
| 147 | // Recover sensor connection previously capped by capRates(). |
| 148 | void uncapRates(); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 149 | sp<SensorService> const mService; |
| 150 | sp<BitTube> mChannel; |
| 151 | uid_t mUid; |
Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 152 | mutable Mutex mConnectionLock; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 153 | // Number of events from wake up sensors which are still pending and haven't been delivered to |
| 154 | // the corresponding application. It is incremented by one unit for each write to the socket. |
| 155 | uint32_t mWakeLockRefCount; |
| 156 | |
| 157 | // If this flag is set to true, it means that the file descriptor associated with the BitTube |
| 158 | // has been added to the Looper in SensorService. This flag is typically set when this |
| 159 | // connection has wake-up sensors associated with it or when write has failed on this connection |
| 160 | // and we're storing some events in the cache. |
| 161 | bool mHasLooperCallbacks; |
| 162 | // If there are any errors associated with the Looper this flag is set to true and |
| 163 | // mWakeLockRefCount is reset to zero. needsWakeLock method will always return false, if this |
| 164 | // flag is set. |
| 165 | bool mDead; |
| 166 | |
| 167 | bool mDataInjectionMode; |
| 168 | struct FlushInfo { |
| 169 | // The number of flush complete events dropped for this sensor is stored here. They are |
| 170 | // sent separately before the next batch of events. |
| 171 | int mPendingFlushEventsToSend; |
| 172 | |
| 173 | // Every activate is preceded by a flush. Only after the first flush complete is received, |
| 174 | // the events for the sensor are sent on that *connection*. |
| 175 | bool mFirstFlushPending; |
| 176 | |
| 177 | FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {} |
| 178 | }; |
Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 179 | // protected by SensorService::mLock. Key for this map is the sensor handle. |
Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 180 | std::unordered_map<int32_t, FlushInfo> mSensorInfo; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 181 | |
| 182 | sensors_event_t *mEventCache; |
| 183 | int mCacheSize, mMaxCacheSize; |
Brian Stack | ae4053f | 2018-12-10 14:54:18 -0800 | [diff] [blame] | 184 | int64_t mTimeOfLastEventDrop; |
| 185 | int mEventsDropped; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 186 | String8 mPackageName; |
| 187 | const String16 mOpPackageName; |
Arthur Ishiguro | 340882c | 2021-02-18 15:17:44 -0800 | [diff] [blame] | 188 | const String16 mAttributionTag; |
Brian Duddie | 457e639 | 2020-06-19 11:38:29 -0700 | [diff] [blame] | 189 | int mTargetSdk; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 190 | #if DEBUG_CONNECTIONS |
| 191 | int mEventsReceived, mEventsSent, mEventsSentFromCache; |
| 192 | int mTotalAcksNeeded, mTotalAcksReceived; |
| 193 | #endif |
| 194 | |
Arthur Ishiguro | 3e9afc1 | 2020-09-22 13:05:15 -0700 | [diff] [blame] | 195 | // Used to track if this object was inappropriately used after destroy(). |
| 196 | std::atomic_bool mDestroyed; |
Brian Stack | c225aa1 | 2019-04-19 09:30:25 -0700 | [diff] [blame] | 197 | |
| 198 | // Store a mapping of sensor handles to required AppOp for a sensor. This map only contains a |
| 199 | // valid mapping for sensors that require a permission in order to reduce the lookup time. |
| 200 | std::unordered_map<int32_t, int32_t> mHandleToAppOp; |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 201 | // Mapping of sensor handles to its rate before being capped by the mic toggle. |
| 202 | std::unordered_map<int, nsecs_t> mMicSamplingPeriodBackup; |
| 203 | userid_t mUserId; |
Arthur Ishiguro | 8a62852 | 2021-09-22 14:10:16 -0700 | [diff] [blame] | 204 | |
| 205 | std::optional<bool> mIsRateCappedBasedOnPermission; |
| 206 | |
| 207 | bool isRateCappedBasedOnPermission() { |
| 208 | if (!mIsRateCappedBasedOnPermission.has_value()) { |
| 209 | mIsRateCappedBasedOnPermission |
| 210 | = mService->isRateCappedBasedOnPermission(mOpPackageName); |
| 211 | } |
| 212 | return mIsRateCappedBasedOnPermission.value(); |
| 213 | } |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | } // namepsace android |
| 217 | |
| 218 | #endif // ANDROID_SENSOR_EVENT_CONNECTION_H |
| 219 | |