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