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