blob: 883c16e23b7c294c4d90684b6f9ea6675f408063 [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>
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
34#include <gui/Sensor.h>
35#include <gui/BitTube.h>
36#include <gui/ISensorServer.h>
37#include <gui/ISensorEventConnection.h>
38
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,
52 bool isDataInjectionMode, const String16& opPackageName);
53
54 status_t sendEvents(sensors_event_t const* buffer, size_t count, sensors_event_t* scratch,
Peng Xueb059472016-08-12 16:39:44 -070055 wp<const SensorEventConnection> const * mapFlushEventsToConnections = NULL);
Peng Xueb4d6282015-12-10 18:02:41 -080056 bool hasSensor(int32_t handle) const;
57 bool hasAnySensor() const;
58 bool hasOneShotSensors() const;
59 bool addSensor(int32_t handle);
60 bool removeSensor(int32_t handle);
61 void setFirstFlushPending(int32_t handle, bool value);
62 void dump(String8& result);
63 bool needsWakeLock();
64 void resetWakeLockRefCount();
65 String8 getPackageName() const;
66
67 uid_t getUid() const { return mUid; }
68
69private:
70 virtual ~SensorEventConnection();
71 virtual void onFirstRef();
72 virtual sp<BitTube> getSensorChannel() const;
73 virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
74 nsecs_t maxBatchReportLatencyNs, int reservedFlags);
75 virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
76 virtual status_t flush();
77 // Count the number of flush complete events which are about to be dropped in the buffer.
78 // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be sent
79 // separately before the next batch of events.
80 void countFlushCompleteEventsLocked(sensors_event_t const* scratch, int numEventsDropped);
81
82 // Check if there are any wake up events in the buffer. If yes, return the index of the first
83 // wake_up sensor event in the buffer else return -1. This wake_up sensor event will have the
84 // flag WAKE_UP_SENSOR_EVENT_NEEDS_ACK set. Exactly one event per packet will have the wake_up
85 // flag set. SOCK_SEQPACKET ensures that either the entire packet is read or dropped.
86 int findWakeUpSensorEventLocked(sensors_event_t const* scratch, int count);
87
88 // Send pending flush_complete events. There may have been flush_complete_events that are
89 // dropped which need to be sent separately before other events. On older HALs (1_0) this method
90 // emulates the behavior of flush().
91 void sendPendingFlushEventsLocked();
92
93 // Writes events from mEventCache to the socket.
94 void writeToSocketFromCache();
95
96 // Compute the approximate cache size from the FIFO sizes of various sensors registered for this
97 // connection. Wake up and non-wake up sensors have separate FIFOs but FIFO may be shared
98 // amongst wake-up sensors and non-wake up sensors.
99 int computeMaxCacheSizeLocked() const;
100
101 // When more sensors register, the maximum cache size desired may change. Compute max cache
102 // size, reallocate memory and copy over events from the older cache.
103 void reAllocateCacheLocked(sensors_event_t const* scratch, int count);
104
105 // LooperCallback method. If there is data to read on this fd, it is an ack from the app that it
106 // has read events from a wake up sensor, decrement mWakeLockRefCount. If this fd is available
107 // for writing send the data from the cache.
108 virtual int handleEvent(int fd, int events, void* data);
109
110 // Increment mPendingFlushEventsToSend for the given sensor handle.
111 void incrementPendingFlushCount(int32_t handle);
112
113 // Add or remove the file descriptor associated with the BitTube to the looper. If mDead is set
114 // to true or there are no more sensors for this connection, the file descriptor is removed if
115 // it has been previously added to the Looper. Depending on the state of the connection FD may
116 // be added to the Looper. The flags to set are determined by the internal state of the
117 // connection. FDs are added to the looper when wake-up sensors are registered (to poll for
118 // acknowledgements) and when write fails on the socket when there are too many error and the
119 // other end hangs up or when this client unregisters for this connection.
120 void updateLooperRegistration(const sp<Looper>& looper); void
121 updateLooperRegistrationLocked(const sp<Looper>& looper);
122
123 sp<SensorService> const mService;
124 sp<BitTube> mChannel;
125 uid_t mUid;
126 mutable Mutex mConnectionLock;
127 // Number of events from wake up sensors which are still pending and haven't been delivered to
128 // the corresponding application. It is incremented by one unit for each write to the socket.
129 uint32_t mWakeLockRefCount;
130
131 // If this flag is set to true, it means that the file descriptor associated with the BitTube
132 // has been added to the Looper in SensorService. This flag is typically set when this
133 // connection has wake-up sensors associated with it or when write has failed on this connection
134 // and we're storing some events in the cache.
135 bool mHasLooperCallbacks;
136 // If there are any errors associated with the Looper this flag is set to true and
137 // mWakeLockRefCount is reset to zero. needsWakeLock method will always return false, if this
138 // flag is set.
139 bool mDead;
140
141 bool mDataInjectionMode;
142 struct FlushInfo {
143 // The number of flush complete events dropped for this sensor is stored here. They are
144 // sent separately before the next batch of events.
145 int mPendingFlushEventsToSend;
146
147 // Every activate is preceded by a flush. Only after the first flush complete is received,
148 // the events for the sensor are sent on that *connection*.
149 bool mFirstFlushPending;
150
151 FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
152 };
153 // protected by SensorService::mLock. Key for this vector is the sensor handle.
154 KeyedVector<int, FlushInfo> mSensorInfo;
155
156 sensors_event_t *mEventCache;
157 int mCacheSize, mMaxCacheSize;
158 String8 mPackageName;
159 const String16 mOpPackageName;
160#if DEBUG_CONNECTIONS
161 int mEventsReceived, mEventsSent, mEventsSentFromCache;
162 int mTotalAcksNeeded, mTotalAcksReceived;
163#endif
164
165};
166
167} // namepsace android
168
169#endif // ANDROID_SENSOR_EVENT_CONNECTION_H
170