1eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu/*
2eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu * Copyright (C) 2010 The Android Open Source Project
3eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu *
4eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu * Licensed under the Apache License, Version 2.0 (the "License");
5eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu * you may not use this file except in compliance with the License.
6eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu * You may obtain a copy of the License at
7eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu *
8eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu *      http://www.apache.org/licenses/LICENSE-2.0
9eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu *
10eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu * Unless required by applicable law or agreed to in writing, software
11eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu * distributed under the License is distributed on an "AS IS" BASIS,
12eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu * See the License for the specific language governing permissions and
14eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu * limitations under the License.
15eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu */
16eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
17eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#ifndef ANDROID_SENSOR_EVENT_CONNECTION_H
18eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#define ANDROID_SENSOR_EVENT_CONNECTION_H
19eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
20eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <stdint.h>
21eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <sys/types.h>
22eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
23eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <utils/Vector.h>
24eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <utils/SortedVector.h>
25eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <utils/KeyedVector.h>
26eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <utils/threads.h>
27eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <utils/AndroidThreads.h>
28eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <utils/RefBase.h>
29eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <utils/Looper.h>
30eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <utils/String8.h>
31eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
32eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <binder/BinderService.h>
33eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
34eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <gui/Sensor.h>
35eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <gui/BitTube.h>
36eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <gui/ISensorServer.h>
37eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include <gui/ISensorEventConnection.h>
38eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
39eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#include "SensorService.h"
40eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
41eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xunamespace android {
42eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
43eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xuclass SensorService;
44eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
45eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xuclass SensorService::SensorEventConnection:
46eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    public BnSensorEventConnection, public LooperCallback {
47eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
48eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    friend class SensorService;
49eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
50eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xupublic:
51eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    SensorEventConnection(const sp<SensorService>& service, uid_t uid, String8 packageName,
52eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu                          bool isDataInjectionMode, const String16& opPackageName);
53eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
54eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    status_t sendEvents(sensors_event_t const* buffer, size_t count, sensors_event_t* scratch,
55eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu                        SensorEventConnection const * const * mapFlushEventsToConnections = NULL);
56eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    bool hasSensor(int32_t handle) const;
57eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    bool hasAnySensor() const;
58eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    bool hasOneShotSensors() const;
59eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    bool addSensor(int32_t handle);
60eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    bool removeSensor(int32_t handle);
61eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    void setFirstFlushPending(int32_t handle, bool value);
62eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    void dump(String8& result);
63eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    bool needsWakeLock();
64eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    void resetWakeLockRefCount();
65eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    String8 getPackageName() const;
66eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
67eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    uid_t getUid() const { return mUid; }
68eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
69eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xuprivate:
70eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    virtual ~SensorEventConnection();
71eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    virtual void onFirstRef();
72eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    virtual sp<BitTube> getSensorChannel() const;
73eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
74eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu                                   nsecs_t maxBatchReportLatencyNs, int reservedFlags);
75eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
76eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    virtual status_t flush();
77eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // Count the number of flush complete events which are about to be dropped in the buffer.
78eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be sent
79eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // separately before the next batch of events.
80eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    void countFlushCompleteEventsLocked(sensors_event_t const* scratch, int numEventsDropped);
81eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
82eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // Check if there are any wake up events in the buffer. If yes, return the index of the first
83eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // wake_up sensor event in the buffer else return -1.  This wake_up sensor event will have the
84eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // flag WAKE_UP_SENSOR_EVENT_NEEDS_ACK set. Exactly one event per packet will have the wake_up
85eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // flag set. SOCK_SEQPACKET ensures that either the entire packet is read or dropped.
86eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    int findWakeUpSensorEventLocked(sensors_event_t const* scratch, int count);
87eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
88eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // Send pending flush_complete events. There may have been flush_complete_events that are
89eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // dropped which need to be sent separately before other events. On older HALs (1_0) this method
90eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // emulates the behavior of flush().
91eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    void sendPendingFlushEventsLocked();
92eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
93eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // Writes events from mEventCache to the socket.
94eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    void writeToSocketFromCache();
95eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
96eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // Compute the approximate cache size from the FIFO sizes of various sensors registered for this
97eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // connection. Wake up and non-wake up sensors have separate FIFOs but FIFO may be shared
98eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // amongst wake-up sensors and non-wake up sensors.
99eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    int computeMaxCacheSizeLocked() const;
100eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
101eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // When more sensors register, the maximum cache size desired may change.  Compute max cache
102eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // size, reallocate memory and copy over events from the older cache.
103eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    void reAllocateCacheLocked(sensors_event_t const* scratch, int count);
104eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
105eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // LooperCallback method. If there is data to read on this fd, it is an ack from the app that it
106eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // has read events from a wake up sensor, decrement mWakeLockRefCount.  If this fd is available
107eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // for writing send the data from the cache.
108eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    virtual int handleEvent(int fd, int events, void* data);
109eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
110eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // Increment mPendingFlushEventsToSend for the given sensor handle.
111eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    void incrementPendingFlushCount(int32_t handle);
112eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
113eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // Add or remove the file descriptor associated with the BitTube to the looper. If mDead is set
114eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // to true or there are no more sensors for this connection, the file descriptor is removed if
115eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // it has been previously added to the Looper. Depending on the state of the connection FD may
116eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // be added to the Looper. The flags to set are determined by the internal state of the
117eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // connection. FDs are added to the looper when wake-up sensors are registered (to poll for
118eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // acknowledgements) and when write fails on the socket when there are too many error and the
119eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // other end hangs up or when this client unregisters for this connection.
120eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    void updateLooperRegistration(const sp<Looper>& looper); void
121eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu            updateLooperRegistrationLocked(const sp<Looper>& looper);
122eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
123eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    sp<SensorService> const mService;
124eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    sp<BitTube> mChannel;
125eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    uid_t mUid;
126eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    mutable Mutex mConnectionLock;
127eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // Number of events from wake up sensors which are still pending and haven't been delivered to
128eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // the corresponding application. It is incremented by one unit for each write to the socket.
129eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    uint32_t mWakeLockRefCount;
130eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
131eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // If this flag is set to true, it means that the file descriptor associated with the BitTube
132eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // has been added to the Looper in SensorService. This flag is typically set when this
133eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // connection has wake-up sensors associated with it or when write has failed on this connection
134eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // and we're storing some events in the cache.
135eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    bool mHasLooperCallbacks;
136eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // If there are any errors associated with the Looper this flag is set to true and
137eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // mWakeLockRefCount is reset to zero. needsWakeLock method will always return false, if this
138eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // flag is set.
139eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    bool mDead;
140eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
141eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    bool mDataInjectionMode;
142eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    struct FlushInfo {
143eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu        // The number of flush complete events dropped for this sensor is stored here.  They are
144eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu        // sent separately before the next batch of events.
145eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu        int mPendingFlushEventsToSend;
146eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
147eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu        // Every activate is preceded by a flush. Only after the first flush complete is received,
148eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu        // the events for the sensor are sent on that *connection*.
149eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu        bool mFirstFlushPending;
150eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
151eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu        FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
152eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    };
153eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // protected by SensorService::mLock. Key for this vector is the sensor handle.
154eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    KeyedVector<int, FlushInfo> mSensorInfo;
155eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
156eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    sensors_event_t *mEventCache;
157eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    int mCacheSize, mMaxCacheSize;
158eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    String8 mPackageName;
159eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    const String16 mOpPackageName;
160eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#if DEBUG_CONNECTIONS
161eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    int mEventsReceived, mEventsSent, mEventsSentFromCache;
162eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    int mTotalAcksNeeded, mTotalAcksReceived;
163eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#endif
164eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
165eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu};
166eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
167eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu} // namepsace android
168eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
169eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu#endif // ANDROID_SENSOR_EVENT_CONNECTION_H
170eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
171