/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ANDROID_SENSOR_SERVICE_H #define ANDROID_SENSOR_SERVICE_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "SensorInterface.h" // --------------------------------------------------------------------------- #define DEBUG_CONNECTIONS false // Max size is 100 KB which is enough to accept a batch of about 1000 events. #define MAX_SOCKET_BUFFER_SIZE_BATCHED 100 * 1024 // For older HALs which don't support batching, use a smaller socket buffer size. #define SOCKET_BUFFER_SIZE_NON_BATCHED 4 * 1024 #define WAKE_UP_SENSOR_EVENT_NEEDS_ACK (1U << 31) struct sensors_poll_device_t; struct sensors_module_t; namespace android { // --------------------------------------------------------------------------- class SensorService : public BinderService, public BnSensorServer, protected Thread { friend class BinderService; static const char* WAKE_LOCK_NAME; static char const* getServiceName() ANDROID_API { return "sensorservice"; } SensorService() ANDROID_API; virtual ~SensorService(); virtual void onFirstRef(); // Thread interface virtual bool threadLoop(); // ISensorServer interface virtual Vector getSensorList(); virtual sp createSensorEventConnection(); virtual status_t dump(int fd, const Vector& args); class SensorEventConnection : public BnSensorEventConnection, public LooperCallback { friend class SensorService; virtual ~SensorEventConnection(); virtual void onFirstRef(); virtual sp getSensorChannel() const; virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags); virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs); virtual status_t flush(); // Count the number of flush complete events which are about to be dropped in the buffer. // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be // sent separately before the next batch of events. void countFlushCompleteEventsLocked(sensors_event_t* scratch, int numEventsDropped); // Check if there are any wake up events in the buffer. If yes, increment the ref count. // Increment it by exactly one unit for each packet sent on the socket. SOCK_SEQPACKET for // the socket ensures that either the entire packet is read or dropped. // Return 1 if mWakeLockRefCount has been incremented, zero if not. int countWakeUpSensorEventsLocked(sensors_event_t* scratch, int count); // Writes events from mEventCache to the socket. void writeToSocketFromCacheLocked(); // Compute the approximate cache size from the FIFO sizes of various sensors registered for // this connection. Wake up and non-wake up sensors have separate FIFOs but FIFO may be // shared amongst wake-up sensors and non-wake up sensors. int computeMaxCacheSizeLocked() const; // LooperCallback method. If there is data to read on this fd, it is an ack from the // app that it has read events from a wake up sensor, decrement mWakeLockRefCount. // If this fd is available for writing send the data from the cache. virtual int handleEvent(int fd, int events, void* data); sp const mService; sp mChannel; uid_t mUid; mutable Mutex mConnectionLock; // Number of events from wake up sensors which are still pending and haven't been delivered // to the corresponding application. It is incremented by one unit for each write to the // socket. int mWakeLockRefCount; struct FlushInfo { // The number of flush complete events dropped for this sensor is stored here. // They are sent separately before the next batch of events. int mPendingFlushEventsToSend; // Every activate is preceded by a flush. Only after the first flush complete is // received, the events for the sensor are sent on that *connection*. bool mFirstFlushPending; // Number of time flush() was called on this connection. This is incremented every time // flush() is called and decremented when flush_complete_event is received. int mNumFlushCalls; FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false), mNumFlushCalls(0) {} }; // protected by SensorService::mLock. Key for this vector is the sensor handle. KeyedVector mSensorInfo; sensors_event_t *mEventCache; int mCacheSize, mMaxCacheSize; #if DEBUG_CONNECTIONS int mEventsReceived, mEventsSent, mEventsSentFromCache; #endif public: SensorEventConnection(const sp& service, uid_t uid); status_t sendEvents(sensors_event_t const* buffer, size_t count, sensors_event_t* scratch); bool hasSensor(int32_t handle) const; bool hasAnySensor() const; bool addSensor(int32_t handle); bool removeSensor(int32_t handle); void setFirstFlushPending(int32_t handle, bool value); void dump(String8& result); bool needsWakeLock(); uid_t getUid() const { return mUid; } }; class SensorRecord { SortedVector< wp > mConnections; public: SensorRecord(const sp& connection); bool addConnection(const sp& connection); bool removeConnection(const wp& connection); size_t getNumConnections() const { return mConnections.size(); } }; class SensorEventAckReceiver : public Thread { sp const mService; public: virtual bool threadLoop(); SensorEventAckReceiver(const sp& service): mService(service) {} }; String8 getSensorName(int handle) const; bool isVirtualSensor(int handle) const; Sensor getSensorFromHandle(int handle) const; bool isWakeUpSensor(int type) const; void recordLastValueLocked(const sensors_event_t* buffer, size_t count); static void sortEventBuffer(sensors_event_t* buffer, size_t count); Sensor registerSensor(SensorInterface* sensor); Sensor registerVirtualSensor(SensorInterface* sensor); status_t cleanupWithoutDisable( const sp& connection, int handle); status_t cleanupWithoutDisableLocked( const sp& connection, int handle); void cleanupAutoDisabledSensorLocked(const sp& connection, sensors_event_t const* buffer, const int count); static bool canAccessSensor(const Sensor& sensor); static bool verifyCanAccessSensor(const Sensor& sensor, const char* operation); // SensorService acquires a partial wakelock for delivering events from wake up sensors. This // method checks whether all the events from these wake up sensors have been delivered to the // corresponding applications, if yes the wakelock is released. void checkWakeLockState(); void checkWakeLockStateLocked(); bool isWakeUpSensorEvent(const sensors_event_t& event) const; sp getLooper() const; // constants Vector mSensorList; Vector mUserSensorListDebug; Vector mUserSensorList; DefaultKeyedVector mSensorMap; Vector mVirtualSensorList; status_t mInitCheck; size_t mSocketBufferSize; sp mLooper; // protected by mLock mutable Mutex mLock; DefaultKeyedVector mActiveSensors; DefaultKeyedVector mActiveVirtualSensors; SortedVector< wp > mActiveConnections; bool mWakeLockAcquired; // The size of this vector is constant, only the items are mutable KeyedVector mLastEventSeen; public: void cleanupConnection(SensorEventConnection* connection); status_t enable(const sp& connection, int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags); status_t disable(const sp& connection, int handle); status_t setEventRate(const sp& connection, int handle, nsecs_t ns); status_t flushSensor(const sp& connection, int handle); }; // --------------------------------------------------------------------------- }; // namespace android #endif // ANDROID_SENSOR_SERVICE_H