blob: 6267dd1a637f0705a891e83adac24a4de91fdd14 [file] [log] [blame]
Mathias Agopianfc328812010-07-14 23:41:37 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_SENSOR_SERVICE_H
18#define ANDROID_SENSOR_SERVICE_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/Vector.h>
24#include <utils/SortedVector.h>
25#include <utils/KeyedVector.h>
26#include <utils/threads.h>
27#include <utils/RefBase.h>
28
29#include <binder/BinderService.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070030
31#include <gui/Sensor.h>
Mathias Agopianb3989272011-10-20 18:42:02 -070032#include <gui/BitTube.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070033#include <gui/ISensorServer.h>
34#include <gui/ISensorEventConnection.h>
35
Mathias Agopianf001c922010-11-11 17:58:51 -080036#include "SensorInterface.h"
37
Mathias Agopianfc328812010-07-14 23:41:37 -070038// ---------------------------------------------------------------------------
39
Mathias Agopiana1b7db92011-05-27 16:23:58 -070040#define DEBUG_CONNECTIONS false
41
Mathias Agopianfc328812010-07-14 23:41:37 -070042struct sensors_poll_device_t;
43struct sensors_module_t;
44
45namespace android {
46// ---------------------------------------------------------------------------
47
48class SensorService :
49 public BinderService<SensorService>,
50 public BnSensorServer,
51 protected Thread
52{
Mathias Agopianb6df7d02013-05-09 14:53:35 -070053 friend class BinderService<SensorService>;
Mathias Agopianfc328812010-07-14 23:41:37 -070054
Mathias Agopianb6df7d02013-05-09 14:53:35 -070055 static const char* WAKE_LOCK_NAME;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070056
Mathias Agopianb6df7d02013-05-09 14:53:35 -070057 static char const* getServiceName() ANDROID_API { return "sensorservice"; }
58 SensorService() ANDROID_API;
Mathias Agopianfc328812010-07-14 23:41:37 -070059 virtual ~SensorService();
60
61 virtual void onFirstRef();
62
63 // Thread interface
64 virtual bool threadLoop();
65
66 // ISensorServer interface
67 virtual Vector<Sensor> getSensorList();
68 virtual sp<ISensorEventConnection> createSensorEventConnection();
69 virtual status_t dump(int fd, const Vector<String16>& args);
70
71
72 class SensorEventConnection : public BnSensorEventConnection {
Mathias Agopian7c1c5312010-07-21 15:59:50 -070073 virtual ~SensorEventConnection();
74 virtual void onFirstRef();
Mathias Agopianb3989272011-10-20 18:42:02 -070075 virtual sp<BitTube> getSensorChannel() const;
Aravind Akella724d91d2013-06-27 12:04:23 -070076 virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
77 nsecs_t maxBatchReportLatencyNs, int reservedFlags);
78 virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
79 virtual status_t flushSensor(int handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -070080
Mathias Agopianfc328812010-07-14 23:41:37 -070081 sp<SensorService> const mService;
Mathias Agopianb3989272011-10-20 18:42:02 -070082 sp<BitTube> const mChannel;
Mathias Agopian5307d172012-09-18 17:02:43 -070083 uid_t mUid;
Mathias Agopian71d7a5c2010-11-14 20:55:25 -080084 mutable Mutex mConnectionLock;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070085
Mathias Agopianf001c922010-11-11 17:58:51 -080086 // protected by SensorService::mLock
87 SortedVector<int> mSensorInfo;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070088
Mathias Agopianfc328812010-07-14 23:41:37 -070089 public:
Mathias Agopian5307d172012-09-18 17:02:43 -070090 SensorEventConnection(const sp<SensorService>& service, uid_t uid);
Mathias Agopian7c1c5312010-07-21 15:59:50 -070091
Mathias Agopiancf510012010-07-22 16:18:10 -070092 status_t sendEvents(sensors_event_t const* buffer, size_t count,
Mathias Agopian3560fb22010-07-22 21:24:39 -070093 sensors_event_t* scratch = NULL);
Mathias Agopianfc328812010-07-14 23:41:37 -070094 bool hasSensor(int32_t handle) const;
95 bool hasAnySensor() const;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070096 bool addSensor(int32_t handle);
97 bool removeSensor(int32_t handle);
Mathias Agopian5307d172012-09-18 17:02:43 -070098
99 uid_t getUid() const { return mUid; }
Mathias Agopianfc328812010-07-14 23:41:37 -0700100 };
101
102 class SensorRecord {
103 SortedVector< wp<SensorEventConnection> > mConnections;
104 public:
105 SensorRecord(const sp<SensorEventConnection>& connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700106 bool addConnection(const sp<SensorEventConnection>& connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700107 bool removeConnection(const wp<SensorEventConnection>& connection);
108 size_t getNumConnections() const { return mConnections.size(); }
109 };
110
111 SortedVector< wp<SensorEventConnection> > getActiveConnections() const;
Mathias Agopianf001c922010-11-11 17:58:51 -0800112 DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const;
Mathias Agopianfc328812010-07-14 23:41:37 -0700113
Mathias Agopianf001c922010-11-11 17:58:51 -0800114 String8 getSensorName(int handle) const;
Mathias Agopian94e8f682010-11-10 17:50:28 -0800115 void recordLastValue(sensors_event_t const * buffer, size_t count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800116 static void sortEventBuffer(sensors_event_t* buffer, size_t count);
Mathias Agopian03193062013-05-10 19:32:39 -0700117 Sensor registerSensor(SensorInterface* sensor);
118 Sensor registerVirtualSensor(SensorInterface* sensor);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700119 status_t cleanupWithoutDisable(
120 const sp<SensorEventConnection>& connection, int handle);
121 status_t cleanupWithoutDisableLocked(
122 const sp<SensorEventConnection>& connection, int handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700123 void cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
Mathias Agopianb6df7d02013-05-09 14:53:35 -0700124 sensors_event_t const* buffer, const int count);
Mathias Agopian94e8f682010-11-10 17:50:28 -0800125
Mathias Agopianfc328812010-07-14 23:41:37 -0700126 // constants
127 Vector<Sensor> mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700128 Vector<Sensor> mUserSensorListDebug;
Mathias Agopian010e4222011-06-08 20:06:50 -0700129 Vector<Sensor> mUserSensorList;
Mathias Agopianf001c922010-11-11 17:58:51 -0800130 DefaultKeyedVector<int, SensorInterface*> mSensorMap;
131 Vector<SensorInterface *> mVirtualSensorList;
Mathias Agopian50df2952010-07-19 19:09:10 -0700132 status_t mInitCheck;
Mathias Agopianfc328812010-07-14 23:41:37 -0700133
134 // protected by mLock
135 mutable Mutex mLock;
Mathias Agopianfc328812010-07-14 23:41:37 -0700136 DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
Mathias Agopianf001c922010-11-11 17:58:51 -0800137 DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
Mathias Agopianfc328812010-07-14 23:41:37 -0700138 SortedVector< wp<SensorEventConnection> > mActiveConnections;
139
Mathias Agopian3560fb22010-07-22 21:24:39 -0700140 // The size of this vector is constant, only the items are mutable
141 KeyedVector<int32_t, sensors_event_t> mLastEventSeen;
142
Mathias Agopianfc328812010-07-14 23:41:37 -0700143public:
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800144 void cleanupConnection(SensorEventConnection* connection);
Aravind Akella724d91d2013-06-27 12:04:23 -0700145 status_t enable(const sp<SensorEventConnection>& connection, int handle,
146 nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags);
Mathias Agopianfc328812010-07-14 23:41:37 -0700147 status_t disable(const sp<SensorEventConnection>& connection, int handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700148 status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns);
Aravind Akella724d91d2013-06-27 12:04:23 -0700149 status_t flushSensor(const sp<SensorEventConnection>& connection, int handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700150};
151
152// ---------------------------------------------------------------------------
153}; // namespace android
154
155#endif // ANDROID_SENSOR_SERVICE_H