blob: 8d5fcf7aa058f245a9d7f24d93e2fcf36dcd0ec0 [file] [log] [blame]
Peng Xueb4d6282015-12-10 18:02:41 -08001/*
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>
Brian Stackc225aa12019-04-19 09:30:25 -070022#include <unordered_map>
Peng Xueb4d6282015-12-10 18:02:41 -080023
24#include <utils/Vector.h>
25#include <utils/SortedVector.h>
Peng Xueb4d6282015-12-10 18:02:41 -080026#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 Agopian801ea092017-03-06 15:05:04 -080034#include <sensor/Sensor.h>
35#include <sensor/BitTube.h>
36#include <sensor/ISensorServer.h>
37#include <sensor/ISensorEventConnection.h>
Peng Xueb4d6282015-12-10 18:02:41 -080038
39#include "SensorService.h"
40
41namespace android {
42
43class SensorService;
44
45class SensorService::SensorEventConnection:
46 public BnSensorEventConnection, public LooperCallback {
47
48 friend class SensorService;
49
50public:
51 SensorEventConnection(const sp<SensorService>& service, uid_t uid, String8 packageName,
Arthur Ishiguro539c27c2020-04-13 09:47:59 -070052 bool isDataInjectionMode, const String16& opPackageName);
Peng Xueb4d6282015-12-10 18:02:41 -080053
54 status_t sendEvents(sensors_event_t const* buffer, size_t count, sensors_event_t* scratch,
Yi Kong8f313e32018-07-17 14:13:29 -070055 wp<const SensorEventConnection> const * mapFlushEventsToConnections = nullptr);
Peng Xueb4d6282015-12-10 18:02:41 -080056 bool hasSensor(int32_t handle) const;
57 bool hasAnySensor() const;
58 bool hasOneShotSensors() const;
Arthur Ishiguro062b27b2020-04-13 08:04:49 -070059 bool addSensor(int32_t handle);
Peng Xueb4d6282015-12-10 18:02:41 -080060 bool removeSensor(int32_t handle);
Arthur Ishiguroad46c782020-03-26 11:24:43 -070061 std::vector<int32_t> getActiveSensorHandles() const;
Peng Xueb4d6282015-12-10 18:02:41 -080062 void setFirstFlushPending(int32_t handle, bool value);
63 void dump(String8& result);
Mike Ma24743862020-01-29 00:36:55 -080064 void dump(util::ProtoOutputStream* proto) const;
Peng Xueb4d6282015-12-10 18:02:41 -080065 bool needsWakeLock();
66 void resetWakeLockRefCount();
67 String8 getPackageName() const;
68
69 uid_t getUid() const { return mUid; }
70
71private:
72 virtual ~SensorEventConnection();
73 virtual void onFirstRef();
74 virtual sp<BitTube> getSensorChannel() const;
75 virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
76 nsecs_t maxBatchReportLatencyNs, int reservedFlags);
77 virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
78 virtual status_t flush();
Peng Xue36e3472016-11-03 11:57:10 -070079 virtual int32_t configureChannel(int handle, int rateLevel);
Peng Xu8cbefd72017-07-10 16:41:08 -070080 virtual void destroy();
Peng Xue36e3472016-11-03 11:57:10 -070081
Peng Xueb4d6282015-12-10 18:02:41 -080082 // Count the number of flush complete events which are about to be dropped in the buffer.
83 // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be sent
84 // separately before the next batch of events.
85 void countFlushCompleteEventsLocked(sensors_event_t const* scratch, int numEventsDropped);
86
87 // Check if there are any wake up events in the buffer. If yes, return the index of the first
88 // wake_up sensor event in the buffer else return -1. This wake_up sensor event will have the
89 // flag WAKE_UP_SENSOR_EVENT_NEEDS_ACK set. Exactly one event per packet will have the wake_up
90 // flag set. SOCK_SEQPACKET ensures that either the entire packet is read or dropped.
91 int findWakeUpSensorEventLocked(sensors_event_t const* scratch, int count);
92
93 // Send pending flush_complete events. There may have been flush_complete_events that are
94 // dropped which need to be sent separately before other events. On older HALs (1_0) this method
95 // emulates the behavior of flush().
96 void sendPendingFlushEventsLocked();
97
98 // Writes events from mEventCache to the socket.
99 void writeToSocketFromCache();
100
101 // Compute the approximate cache size from the FIFO sizes of various sensors registered for this
102 // connection. Wake up and non-wake up sensors have separate FIFOs but FIFO may be shared
103 // amongst wake-up sensors and non-wake up sensors.
104 int computeMaxCacheSizeLocked() const;
105
106 // When more sensors register, the maximum cache size desired may change. Compute max cache
107 // size, reallocate memory and copy over events from the older cache.
108 void reAllocateCacheLocked(sensors_event_t const* scratch, int count);
109
Brian Stack93432ad2018-11-27 18:28:48 -0800110 // Add the events to the cache. If the cache would be exceeded, drop events at the beginning of
111 // the cache.
112 void appendEventsToCacheLocked(sensors_event_t const* events, int count);
113
Peng Xueb4d6282015-12-10 18:02:41 -0800114 // LooperCallback method. If there is data to read on this fd, it is an ack from the app that it
115 // has read events from a wake up sensor, decrement mWakeLockRefCount. If this fd is available
116 // for writing send the data from the cache.
117 virtual int handleEvent(int fd, int events, void* data);
118
119 // Increment mPendingFlushEventsToSend for the given sensor handle.
120 void incrementPendingFlushCount(int32_t handle);
121
122 // Add or remove the file descriptor associated with the BitTube to the looper. If mDead is set
123 // to true or there are no more sensors for this connection, the file descriptor is removed if
124 // it has been previously added to the Looper. Depending on the state of the connection FD may
125 // be added to the Looper. The flags to set are determined by the internal state of the
126 // connection. FDs are added to the looper when wake-up sensors are registered (to poll for
127 // acknowledgements) and when write fails on the socket when there are too many error and the
128 // other end hangs up or when this client unregisters for this connection.
129 void updateLooperRegistration(const sp<Looper>& looper); void
130 updateLooperRegistrationLocked(const sp<Looper>& looper);
131
Michael Groover5e1f60b2018-12-04 22:34:29 -0800132 // Returns whether sensor access is available based on both the uid being active and sensor
133 // privacy not being enabled.
134 bool hasSensorAccess();
135
Brian Stackc225aa12019-04-19 09:30:25 -0700136 // Call noteOp for the sensor if the sensor requires a permission
137 bool noteOpIfRequired(const sensors_event_t& event);
138
Peng Xueb4d6282015-12-10 18:02:41 -0800139 sp<SensorService> const mService;
140 sp<BitTube> mChannel;
141 uid_t mUid;
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700142 mutable Mutex mConnectionLock;
Peng Xueb4d6282015-12-10 18:02:41 -0800143 // Number of events from wake up sensors which are still pending and haven't been delivered to
144 // the corresponding application. It is incremented by one unit for each write to the socket.
145 uint32_t mWakeLockRefCount;
146
147 // If this flag is set to true, it means that the file descriptor associated with the BitTube
148 // has been added to the Looper in SensorService. This flag is typically set when this
149 // connection has wake-up sensors associated with it or when write has failed on this connection
150 // and we're storing some events in the cache.
151 bool mHasLooperCallbacks;
152 // If there are any errors associated with the Looper this flag is set to true and
153 // mWakeLockRefCount is reset to zero. needsWakeLock method will always return false, if this
154 // flag is set.
155 bool mDead;
156
157 bool mDataInjectionMode;
158 struct FlushInfo {
159 // The number of flush complete events dropped for this sensor is stored here. They are
160 // sent separately before the next batch of events.
161 int mPendingFlushEventsToSend;
162
163 // Every activate is preceded by a flush. Only after the first flush complete is received,
164 // the events for the sensor are sent on that *connection*.
165 bool mFirstFlushPending;
166
167 FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
168 };
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700169 // protected by SensorService::mLock. Key for this map is the sensor handle.
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700170 std::unordered_map<int32_t, FlushInfo> mSensorInfo;
Peng Xueb4d6282015-12-10 18:02:41 -0800171
172 sensors_event_t *mEventCache;
173 int mCacheSize, mMaxCacheSize;
Brian Stackae4053f2018-12-10 14:54:18 -0800174 int64_t mTimeOfLastEventDrop;
175 int mEventsDropped;
Peng Xueb4d6282015-12-10 18:02:41 -0800176 String8 mPackageName;
177 const String16 mOpPackageName;
Brian Duddie457e6392020-06-19 11:38:29 -0700178 int mTargetSdk;
Peng Xueb4d6282015-12-10 18:02:41 -0800179#if DEBUG_CONNECTIONS
180 int mEventsReceived, mEventsSent, mEventsSentFromCache;
181 int mTotalAcksNeeded, mTotalAcksReceived;
182#endif
183
Peng Xu8cbefd72017-07-10 16:41:08 -0700184 mutable Mutex mDestroyLock;
185 bool mDestroyed;
Brian Stackc225aa12019-04-19 09:30:25 -0700186
187 // Store a mapping of sensor handles to required AppOp for a sensor. This map only contains a
188 // valid mapping for sensors that require a permission in order to reduce the lookup time.
189 std::unordered_map<int32_t, int32_t> mHandleToAppOp;
Peng Xueb4d6282015-12-10 18:02:41 -0800190};
191
192} // namepsace android
193
194#endif // ANDROID_SENSOR_EVENT_CONNECTION_H
195