/* * 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 "SensorInterface.h" // --------------------------------------------------------------------------- #define DEBUG_CONNECTIONS false // Max size is 1 MB which is enough to accept a batch of about 10k events. #define MAX_SOCKET_BUFFER_SIZE_BATCHED 1024 * 1024 #define SOCKET_BUFFER_SIZE_NON_BATCHED 4 * 1024 #define WAKE_UP_SENSOR_EVENT_NEEDS_ACK (1 << 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 { 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(); void decreaseWakeLockRefCount(); // 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, const int count); 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; FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {} }; // protected by SensorService::mLock. Key for this vector is the sensor handle. KeyedVector mSensorInfo; 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(); } }; 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; // constants Vector mSensorList; Vector mUserSensorListDebug; Vector mUserSensorList; DefaultKeyedVector mSensorMap; Vector mVirtualSensorList; status_t mInitCheck; size_t mSocketBufferSize; // 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