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