2010-07-15 06:41:37 +00:00
|
|
|
/*
|
|
|
|
* 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 <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <utils/Vector.h>
|
|
|
|
#include <utils/SortedVector.h>
|
|
|
|
#include <utils/KeyedVector.h>
|
|
|
|
#include <utils/threads.h>
|
2014-07-10 23:01:10 +00:00
|
|
|
#include <utils/AndroidThreads.h>
|
2010-07-15 06:41:37 +00:00
|
|
|
#include <utils/RefBase.h>
|
2014-07-10 23:01:10 +00:00
|
|
|
#include <utils/Looper.h>
|
2010-07-15 06:41:37 +00:00
|
|
|
|
|
|
|
#include <binder/BinderService.h>
|
|
|
|
|
|
|
|
#include <gui/Sensor.h>
|
2011-10-21 01:42:02 +00:00
|
|
|
#include <gui/BitTube.h>
|
2010-07-15 06:41:37 +00:00
|
|
|
#include <gui/ISensorServer.h>
|
|
|
|
#include <gui/ISensorEventConnection.h>
|
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
#include "SensorInterface.h"
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2011-05-27 23:23:58 +00:00
|
|
|
#define DEBUG_CONNECTIONS false
|
2014-07-10 23:01:10 +00:00
|
|
|
// 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.
|
2013-09-06 00:03:38 +00:00
|
|
|
#define SOCKET_BUFFER_SIZE_NON_BATCHED 4 * 1024
|
2011-05-27 23:23:58 +00:00
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
struct sensors_poll_device_t;
|
|
|
|
struct sensors_module_t;
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class SensorService :
|
|
|
|
public BinderService<SensorService>,
|
|
|
|
public BnSensorServer,
|
|
|
|
protected Thread
|
|
|
|
{
|
2013-05-09 21:53:35 +00:00
|
|
|
friend class BinderService<SensorService>;
|
2010-07-15 06:41:37 +00:00
|
|
|
|
2013-05-09 21:53:35 +00:00
|
|
|
static const char* WAKE_LOCK_NAME;
|
2010-07-21 22:59:50 +00:00
|
|
|
|
2013-05-09 21:53:35 +00:00
|
|
|
static char const* getServiceName() ANDROID_API { return "sensorservice"; }
|
|
|
|
SensorService() ANDROID_API;
|
2010-07-15 06:41:37 +00:00
|
|
|
virtual ~SensorService();
|
|
|
|
|
|
|
|
virtual void onFirstRef();
|
|
|
|
|
|
|
|
// Thread interface
|
|
|
|
virtual bool threadLoop();
|
|
|
|
|
|
|
|
// ISensorServer interface
|
|
|
|
virtual Vector<Sensor> getSensorList();
|
|
|
|
virtual sp<ISensorEventConnection> createSensorEventConnection();
|
|
|
|
virtual status_t dump(int fd, const Vector<String16>& args);
|
|
|
|
|
2014-07-10 23:01:10 +00:00
|
|
|
class SensorEventConnection : public BnSensorEventConnection, public LooperCallback {
|
|
|
|
friend class SensorService;
|
2010-07-21 22:59:50 +00:00
|
|
|
virtual ~SensorEventConnection();
|
|
|
|
virtual void onFirstRef();
|
2011-10-21 01:42:02 +00:00
|
|
|
virtual sp<BitTube> getSensorChannel() const;
|
2013-06-27 19:04:23 +00:00
|
|
|
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);
|
2013-10-08 21:59:26 +00:00
|
|
|
virtual status_t flush();
|
2013-09-06 00:03:38 +00:00
|
|
|
// 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.
|
2013-10-15 00:04:42 +00:00
|
|
|
void countFlushCompleteEventsLocked(sensors_event_t* scratch, int numEventsDropped);
|
2010-07-21 22:59:50 +00:00
|
|
|
|
2014-08-13 19:24:50 +00:00
|
|
|
// Check if there are any wake up events in the buffer. If yes, return the index of the
|
|
|
|
// first wake_up sensor event in the buffer else return -1. This wake_up sensor event will
|
|
|
|
// have the flag WAKE_UP_SENSOR_EVENT_NEEDS_ACK set. Exactly one event per packet will have
|
|
|
|
// the wake_up flag set. SOCK_SEQPACKET ensures that either the entire packet is read or
|
|
|
|
// dropped.
|
|
|
|
int findWakeUpSensorEventLocked(sensors_event_t const* scratch, int count);
|
|
|
|
|
|
|
|
// Send pending flush_complete events. There may have been flush_complete_events that are
|
|
|
|
// dropped which need to be sent separately before other events. On older HALs (1_0) this
|
|
|
|
// method emulates the behavior of flush().
|
|
|
|
void sendPendingFlushEventsLocked();
|
2014-07-10 23:01:10 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2014-08-13 19:24:50 +00:00
|
|
|
// When more sensors register, the maximum cache size desired may change. Compute max cache
|
|
|
|
// size, reallocate memory and copy over events from the older cache.
|
|
|
|
void reAllocateCacheLocked(sensors_event_t const* scratch, int count);
|
|
|
|
|
2014-07-10 23:01:10 +00:00
|
|
|
// 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);
|
2014-02-12 02:58:52 +00:00
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
sp<SensorService> const mService;
|
2013-09-06 00:03:38 +00:00
|
|
|
sp<BitTube> mChannel;
|
2012-09-19 00:02:43 +00:00
|
|
|
uid_t mUid;
|
2010-11-15 04:55:25 +00:00
|
|
|
mutable Mutex mConnectionLock;
|
2014-02-12 02:58:52 +00:00
|
|
|
// 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;
|
2010-07-21 22:59:50 +00:00
|
|
|
|
2013-09-06 00:03:38 +00:00
|
|
|
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;
|
2014-08-13 19:24:50 +00:00
|
|
|
FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
|
2013-09-06 00:03:38 +00:00
|
|
|
};
|
|
|
|
// protected by SensorService::mLock. Key for this vector is the sensor handle.
|
|
|
|
KeyedVector<int, FlushInfo> mSensorInfo;
|
2014-07-10 23:01:10 +00:00
|
|
|
sensors_event_t *mEventCache;
|
|
|
|
int mCacheSize, mMaxCacheSize;
|
|
|
|
|
|
|
|
#if DEBUG_CONNECTIONS
|
|
|
|
int mEventsReceived, mEventsSent, mEventsSentFromCache;
|
2014-08-21 19:28:35 +00:00
|
|
|
int mTotalAcksNeeded, mTotalAcksReceived;
|
2014-07-10 23:01:10 +00:00
|
|
|
#endif
|
2010-07-21 22:59:50 +00:00
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
public:
|
2012-09-19 00:02:43 +00:00
|
|
|
SensorEventConnection(const sp<SensorService>& service, uid_t uid);
|
2010-07-21 22:59:50 +00:00
|
|
|
|
2010-07-22 23:18:10 +00:00
|
|
|
status_t sendEvents(sensors_event_t const* buffer, size_t count,
|
2014-02-12 02:58:52 +00:00
|
|
|
sensors_event_t* scratch);
|
2010-07-15 06:41:37 +00:00
|
|
|
bool hasSensor(int32_t handle) const;
|
|
|
|
bool hasAnySensor() const;
|
2010-07-21 22:59:50 +00:00
|
|
|
bool addSensor(int32_t handle);
|
|
|
|
bool removeSensor(int32_t handle);
|
2013-09-06 00:03:38 +00:00
|
|
|
void setFirstFlushPending(int32_t handle, bool value);
|
|
|
|
void dump(String8& result);
|
2014-02-12 02:58:52 +00:00
|
|
|
bool needsWakeLock();
|
2012-09-19 00:02:43 +00:00
|
|
|
|
|
|
|
uid_t getUid() const { return mUid; }
|
2010-07-15 06:41:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SensorRecord {
|
|
|
|
SortedVector< wp<SensorEventConnection> > mConnections;
|
2014-08-13 19:24:50 +00:00
|
|
|
// A queue of all flush() calls made on this sensor. Flush complete events will be
|
|
|
|
// sent in this order.
|
|
|
|
Vector< wp<SensorEventConnection> > mPendingFlushConnections;
|
2010-07-15 06:41:37 +00:00
|
|
|
public:
|
|
|
|
SensorRecord(const sp<SensorEventConnection>& connection);
|
2010-07-21 22:59:50 +00:00
|
|
|
bool addConnection(const sp<SensorEventConnection>& connection);
|
2010-07-15 06:41:37 +00:00
|
|
|
bool removeConnection(const wp<SensorEventConnection>& connection);
|
|
|
|
size_t getNumConnections() const { return mConnections.size(); }
|
2014-08-13 19:24:50 +00:00
|
|
|
|
|
|
|
void addPendingFlushConnection(const sp<SensorEventConnection>& connection);
|
|
|
|
void removeFirstPendingFlushConnection();
|
|
|
|
SensorEventConnection * getFirstPendingFlushConnection();
|
2010-07-15 06:41:37 +00:00
|
|
|
};
|
|
|
|
|
2014-07-10 23:01:10 +00:00
|
|
|
class SensorEventAckReceiver : public Thread {
|
|
|
|
sp<SensorService> const mService;
|
|
|
|
public:
|
|
|
|
virtual bool threadLoop();
|
|
|
|
SensorEventAckReceiver(const sp<SensorService>& service): mService(service) {}
|
|
|
|
};
|
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
String8 getSensorName(int handle) const;
|
2013-10-15 22:43:10 +00:00
|
|
|
bool isVirtualSensor(int handle) const;
|
2014-04-07 22:52:37 +00:00
|
|
|
Sensor getSensorFromHandle(int handle) const;
|
2014-02-12 02:58:52 +00:00
|
|
|
bool isWakeUpSensor(int type) const;
|
|
|
|
void recordLastValueLocked(const sensors_event_t* buffer, size_t count);
|
2010-11-12 01:58:51 +00:00
|
|
|
static void sortEventBuffer(sensors_event_t* buffer, size_t count);
|
2013-05-11 02:32:39 +00:00
|
|
|
Sensor registerSensor(SensorInterface* sensor);
|
|
|
|
Sensor registerVirtualSensor(SensorInterface* sensor);
|
2013-07-12 09:01:16 +00:00
|
|
|
status_t cleanupWithoutDisable(
|
|
|
|
const sp<SensorEventConnection>& connection, int handle);
|
|
|
|
status_t cleanupWithoutDisableLocked(
|
|
|
|
const sp<SensorEventConnection>& connection, int handle);
|
2014-02-12 02:58:52 +00:00
|
|
|
void cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection,
|
2013-05-09 21:53:35 +00:00
|
|
|
sensors_event_t const* buffer, const int count);
|
2014-04-07 22:52:37 +00:00
|
|
|
static bool canAccessSensor(const Sensor& sensor);
|
|
|
|
static bool verifyCanAccessSensor(const Sensor& sensor, const char* operation);
|
2014-02-12 02:58:52 +00:00
|
|
|
// 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;
|
2014-07-10 23:01:10 +00:00
|
|
|
|
2014-08-13 19:24:50 +00:00
|
|
|
SensorRecord * getSensorRecord(int handle);
|
|
|
|
|
2014-07-10 23:01:10 +00:00
|
|
|
sp<Looper> getLooper() const;
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
// constants
|
|
|
|
Vector<Sensor> mSensorList;
|
2012-06-29 02:46:54 +00:00
|
|
|
Vector<Sensor> mUserSensorListDebug;
|
2011-06-09 03:06:50 +00:00
|
|
|
Vector<Sensor> mUserSensorList;
|
2010-11-12 01:58:51 +00:00
|
|
|
DefaultKeyedVector<int, SensorInterface*> mSensorMap;
|
|
|
|
Vector<SensorInterface *> mVirtualSensorList;
|
2010-07-20 02:09:10 +00:00
|
|
|
status_t mInitCheck;
|
2014-08-22 23:11:10 +00:00
|
|
|
// Socket buffersize used to initialize BitTube. This size depends on whether batching is
|
|
|
|
// supported or not.
|
2013-09-06 00:03:38 +00:00
|
|
|
size_t mSocketBufferSize;
|
2014-07-10 23:01:10 +00:00
|
|
|
sp<Looper> mLooper;
|
2010-07-15 06:41:37 +00:00
|
|
|
|
|
|
|
// protected by mLock
|
|
|
|
mutable Mutex mLock;
|
|
|
|
DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
|
2010-11-12 01:58:51 +00:00
|
|
|
DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
|
2010-07-15 06:41:37 +00:00
|
|
|
SortedVector< wp<SensorEventConnection> > mActiveConnections;
|
2014-02-12 02:58:52 +00:00
|
|
|
bool mWakeLockAcquired;
|
2010-07-15 06:41:37 +00:00
|
|
|
|
2010-07-23 04:24:39 +00:00
|
|
|
// The size of this vector is constant, only the items are mutable
|
|
|
|
KeyedVector<int32_t, sensors_event_t> mLastEventSeen;
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
public:
|
2011-02-03 22:52:47 +00:00
|
|
|
void cleanupConnection(SensorEventConnection* connection);
|
2013-06-27 19:04:23 +00:00
|
|
|
status_t enable(const sp<SensorEventConnection>& connection, int handle,
|
|
|
|
nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags);
|
2010-07-15 06:41:37 +00:00
|
|
|
status_t disable(const sp<SensorEventConnection>& connection, int handle);
|
2010-07-21 22:59:50 +00:00
|
|
|
status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns);
|
2013-06-27 19:04:23 +00:00
|
|
|
status_t flushSensor(const sp<SensorEventConnection>& connection, int handle);
|
2010-07-15 06:41:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|
|
|
|
|
|
|
|
#endif // ANDROID_SENSOR_SERVICE_H
|