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>
|
|
|
|
#include <utils/RefBase.h>
|
|
|
|
|
|
|
|
#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
|
2013-09-06 00:03:38 +00:00
|
|
|
// 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
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
class SensorEventConnection : public BnSensorEventConnection {
|
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
|
|
|
|
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;
|
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;
|
|
|
|
FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
|
|
|
|
};
|
|
|
|
// protected by SensorService::mLock. Key for this vector is the sensor handle.
|
|
|
|
KeyedVector<int, FlushInfo> mSensorInfo;
|
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,
|
2010-07-23 04:24:39 +00:00
|
|
|
sensors_event_t* scratch = NULL);
|
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);
|
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;
|
|
|
|
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(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
SortedVector< wp<SensorEventConnection> > getActiveConnections() const;
|
2010-11-12 01:58:51 +00:00
|
|
|
DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const;
|
2010-07-15 06:41:37 +00:00
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
String8 getSensorName(int handle) const;
|
2010-11-11 01:50:28 +00:00
|
|
|
void recordLastValue(sensors_event_t const * 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);
|
2013-04-08 23:43:12 +00:00
|
|
|
void cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
|
2013-05-09 21:53:35 +00:00
|
|
|
sensors_event_t const* buffer, const int count);
|
2010-11-11 01:50:28 +00:00
|
|
|
|
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;
|
2013-09-06 00:03:38 +00:00
|
|
|
size_t mSocketBufferSize;
|
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;
|
|
|
|
|
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
|