| 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 |  | 
 | 20 | #include <stdint.h> | 
 | 21 | #include <sys/types.h> | 
 | 22 |  | 
 | 23 | #include <utils/Vector.h> | 
 | 24 | #include <utils/SortedVector.h> | 
 | 25 | #include <utils/KeyedVector.h> | 
 | 26 | #include <utils/threads.h> | 
 | 27 | #include <utils/AndroidThreads.h> | 
 | 28 | #include <utils/RefBase.h> | 
 | 29 | #include <utils/Looper.h> | 
 | 30 | #include <utils/String8.h> | 
 | 31 |  | 
 | 32 | #include <binder/BinderService.h> | 
 | 33 |  | 
| Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 34 | #include <sensor/Sensor.h> | 
 | 35 | #include <sensor/BitTube.h> | 
 | 36 | #include <sensor/ISensorServer.h> | 
 | 37 | #include <sensor/ISensorEventConnection.h> | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 38 |  | 
 | 39 | #include "SensorService.h" | 
 | 40 |  | 
 | 41 | namespace android { | 
 | 42 |  | 
 | 43 | class SensorService; | 
 | 44 |  | 
 | 45 | class SensorService::SensorEventConnection: | 
 | 46 |     public BnSensorEventConnection, public LooperCallback { | 
 | 47 |  | 
 | 48 |     friend class SensorService; | 
 | 49 |  | 
 | 50 | public: | 
 | 51 |     SensorEventConnection(const sp<SensorService>& service, uid_t uid, String8 packageName, | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 52 |                           bool isDataInjectionMode, const String16& opPackageName, | 
 | 53 |                           bool hasSensorAccess); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 54 |  | 
 | 55 |     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] | 56 |                         wp<const SensorEventConnection> const * mapFlushEventsToConnections = nullptr); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 57 |     bool hasSensor(int32_t handle) const; | 
 | 58 |     bool hasAnySensor() const; | 
 | 59 |     bool hasOneShotSensors() const; | 
 | 60 |     bool addSensor(int32_t handle); | 
 | 61 |     bool removeSensor(int32_t handle); | 
 | 62 |     void setFirstFlushPending(int32_t handle, bool value); | 
 | 63 |     void dump(String8& result); | 
 | 64 |     bool needsWakeLock(); | 
 | 65 |     void resetWakeLockRefCount(); | 
 | 66 |     String8 getPackageName() const; | 
 | 67 |  | 
 | 68 |     uid_t getUid() const { return mUid; } | 
 | 69 |  | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 70 |     void setSensorAccess(const bool hasAccess); | 
 | 71 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 72 | private: | 
 | 73 |     virtual ~SensorEventConnection(); | 
 | 74 |     virtual void onFirstRef(); | 
 | 75 |     virtual sp<BitTube> getSensorChannel() const; | 
 | 76 |     virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs, | 
 | 77 |                                    nsecs_t maxBatchReportLatencyNs, int reservedFlags); | 
 | 78 |     virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs); | 
 | 79 |     virtual status_t flush(); | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 80 |     virtual int32_t configureChannel(int handle, int rateLevel); | 
| Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 81 |     virtual void destroy(); | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 82 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 83 |     // Count the number of flush complete events which are about to be dropped in the buffer. | 
 | 84 |     // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be sent | 
 | 85 |     // separately before the next batch of events. | 
 | 86 |     void countFlushCompleteEventsLocked(sensors_event_t const* scratch, int numEventsDropped); | 
 | 87 |  | 
 | 88 |     // Check if there are any wake up events in the buffer. If yes, return the index of the first | 
 | 89 |     // wake_up sensor event in the buffer else return -1.  This wake_up sensor event will have the | 
 | 90 |     // flag WAKE_UP_SENSOR_EVENT_NEEDS_ACK set. Exactly one event per packet will have the wake_up | 
 | 91 |     // flag set. SOCK_SEQPACKET ensures that either the entire packet is read or dropped. | 
 | 92 |     int findWakeUpSensorEventLocked(sensors_event_t const* scratch, int count); | 
 | 93 |  | 
 | 94 |     // Send pending flush_complete events. There may have been flush_complete_events that are | 
 | 95 |     // dropped which need to be sent separately before other events. On older HALs (1_0) this method | 
 | 96 |     // emulates the behavior of flush(). | 
 | 97 |     void sendPendingFlushEventsLocked(); | 
 | 98 |  | 
 | 99 |     // Writes events from mEventCache to the socket. | 
 | 100 |     void writeToSocketFromCache(); | 
 | 101 |  | 
 | 102 |     // Compute the approximate cache size from the FIFO sizes of various sensors registered for this | 
 | 103 |     // connection. Wake up and non-wake up sensors have separate FIFOs but FIFO may be shared | 
 | 104 |     // amongst wake-up sensors and non-wake up sensors. | 
 | 105 |     int computeMaxCacheSizeLocked() const; | 
 | 106 |  | 
 | 107 |     // When more sensors register, the maximum cache size desired may change.  Compute max cache | 
 | 108 |     // size, reallocate memory and copy over events from the older cache. | 
 | 109 |     void reAllocateCacheLocked(sensors_event_t const* scratch, int count); | 
 | 110 |  | 
| Brian Stack | 93432ad | 2018-11-27 18:28:48 -0800 | [diff] [blame] | 111 |     // Add the events to the cache. If the cache would be exceeded, drop events at the beginning of | 
 | 112 |     // the cache. | 
 | 113 |     void appendEventsToCacheLocked(sensors_event_t const* events, int count); | 
 | 114 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 115 |     // LooperCallback method. If there is data to read on this fd, it is an ack from the app that it | 
 | 116 |     // has read events from a wake up sensor, decrement mWakeLockRefCount.  If this fd is available | 
 | 117 |     // for writing send the data from the cache. | 
 | 118 |     virtual int handleEvent(int fd, int events, void* data); | 
 | 119 |  | 
 | 120 |     // Increment mPendingFlushEventsToSend for the given sensor handle. | 
 | 121 |     void incrementPendingFlushCount(int32_t handle); | 
 | 122 |  | 
 | 123 |     // Add or remove the file descriptor associated with the BitTube to the looper. If mDead is set | 
 | 124 |     // to true or there are no more sensors for this connection, the file descriptor is removed if | 
 | 125 |     // it has been previously added to the Looper. Depending on the state of the connection FD may | 
 | 126 |     // be added to the Looper. The flags to set are determined by the internal state of the | 
 | 127 |     // connection. FDs are added to the looper when wake-up sensors are registered (to poll for | 
 | 128 |     // acknowledgements) and when write fails on the socket when there are too many error and the | 
 | 129 |     // other end hangs up or when this client unregisters for this connection. | 
 | 130 |     void updateLooperRegistration(const sp<Looper>& looper); void | 
 | 131 |             updateLooperRegistrationLocked(const sp<Looper>& looper); | 
 | 132 |  | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 133 |     // Returns whether sensor access is available based on both the uid being active and sensor | 
 | 134 |     // privacy not being enabled. | 
 | 135 |     bool hasSensorAccess(); | 
 | 136 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 137 |     sp<SensorService> const mService; | 
 | 138 |     sp<BitTube> mChannel; | 
 | 139 |     uid_t mUid; | 
 | 140 |     mutable Mutex mConnectionLock; | 
 | 141 |     // Number of events from wake up sensors which are still pending and haven't been delivered to | 
 | 142 |     // the corresponding application. It is incremented by one unit for each write to the socket. | 
 | 143 |     uint32_t mWakeLockRefCount; | 
 | 144 |  | 
 | 145 |     // If this flag is set to true, it means that the file descriptor associated with the BitTube | 
 | 146 |     // has been added to the Looper in SensorService. This flag is typically set when this | 
 | 147 |     // connection has wake-up sensors associated with it or when write has failed on this connection | 
 | 148 |     // and we're storing some events in the cache. | 
 | 149 |     bool mHasLooperCallbacks; | 
 | 150 |     // If there are any errors associated with the Looper this flag is set to true and | 
 | 151 |     // mWakeLockRefCount is reset to zero. needsWakeLock method will always return false, if this | 
 | 152 |     // flag is set. | 
 | 153 |     bool mDead; | 
 | 154 |  | 
 | 155 |     bool mDataInjectionMode; | 
 | 156 |     struct FlushInfo { | 
 | 157 |         // The number of flush complete events dropped for this sensor is stored here.  They are | 
 | 158 |         // sent separately before the next batch of events. | 
 | 159 |         int mPendingFlushEventsToSend; | 
 | 160 |  | 
 | 161 |         // Every activate is preceded by a flush. Only after the first flush complete is received, | 
 | 162 |         // the events for the sensor are sent on that *connection*. | 
 | 163 |         bool mFirstFlushPending; | 
 | 164 |  | 
 | 165 |         FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {} | 
 | 166 |     }; | 
 | 167 |     // protected by SensorService::mLock. Key for this vector is the sensor handle. | 
 | 168 |     KeyedVector<int, FlushInfo> mSensorInfo; | 
 | 169 |  | 
 | 170 |     sensors_event_t *mEventCache; | 
 | 171 |     int mCacheSize, mMaxCacheSize; | 
| Brian Stack | ae4053f | 2018-12-10 14:54:18 -0800 | [diff] [blame] | 172 |     int64_t mTimeOfLastEventDrop; | 
 | 173 |     int mEventsDropped; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 174 |     String8 mPackageName; | 
 | 175 |     const String16 mOpPackageName; | 
 | 176 | #if DEBUG_CONNECTIONS | 
 | 177 |     int mEventsReceived, mEventsSent, mEventsSentFromCache; | 
 | 178 |     int mTotalAcksNeeded, mTotalAcksReceived; | 
 | 179 | #endif | 
 | 180 |  | 
| Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 181 |     mutable Mutex mDestroyLock; | 
 | 182 |     bool mDestroyed; | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 183 |     bool mHasSensorAccess; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 184 | }; | 
 | 185 |  | 
 | 186 | } // namepsace android | 
 | 187 |  | 
 | 188 | #endif // ANDROID_SENSOR_EVENT_CONNECTION_H | 
 | 189 |  |