Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1 | /* |
| 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 Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 30 | |
| 31 | #include <gui/Sensor.h> |
Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 32 | #include <gui/BitTube.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 33 | #include <gui/ISensorServer.h> |
| 34 | #include <gui/ISensorEventConnection.h> |
| 35 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 36 | #include "SensorInterface.h" |
| 37 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 38 | // --------------------------------------------------------------------------- |
| 39 | |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 40 | #define DEBUG_CONNECTIONS false |
| 41 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 42 | struct sensors_poll_device_t; |
| 43 | struct sensors_module_t; |
| 44 | |
| 45 | namespace android { |
| 46 | // --------------------------------------------------------------------------- |
| 47 | |
| 48 | class SensorService : |
| 49 | public BinderService<SensorService>, |
| 50 | public BnSensorServer, |
| 51 | protected Thread |
| 52 | { |
| 53 | friend class BinderService<SensorService>; |
| 54 | |
Mathias Agopian | 24d7235 | 2010-11-05 19:12:58 -0700 | [diff] [blame] | 55 | static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 56 | static const char* WAKE_LOCK_NAME; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 57 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 58 | SensorService(); |
| 59 | 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 Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 73 | virtual ~SensorEventConnection(); |
| 74 | virtual void onFirstRef(); |
Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 75 | virtual sp<BitTube> getSensorChannel() const; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 76 | virtual status_t enableDisable(int handle, bool enabled); |
| 77 | virtual status_t setEventRate(int handle, nsecs_t ns); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 78 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 79 | sp<SensorService> const mService; |
Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 80 | sp<BitTube> const mChannel; |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 81 | uid_t mUid; |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 82 | mutable Mutex mConnectionLock; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 83 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 84 | // protected by SensorService::mLock |
| 85 | SortedVector<int> mSensorInfo; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 86 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 87 | public: |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 88 | SensorEventConnection(const sp<SensorService>& service, uid_t uid); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 89 | |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 90 | status_t sendEvents(sensors_event_t const* buffer, size_t count, |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 91 | sensors_event_t* scratch = NULL); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 92 | bool hasSensor(int32_t handle) const; |
| 93 | bool hasAnySensor() const; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 94 | bool addSensor(int32_t handle); |
| 95 | bool removeSensor(int32_t handle); |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 96 | |
| 97 | uid_t getUid() const { return mUid; } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | class SensorRecord { |
| 101 | SortedVector< wp<SensorEventConnection> > mConnections; |
| 102 | public: |
| 103 | SensorRecord(const sp<SensorEventConnection>& connection); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 104 | bool addConnection(const sp<SensorEventConnection>& connection); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 105 | bool removeConnection(const wp<SensorEventConnection>& connection); |
| 106 | size_t getNumConnections() const { return mConnections.size(); } |
| 107 | }; |
| 108 | |
| 109 | SortedVector< wp<SensorEventConnection> > getActiveConnections() const; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 110 | DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 111 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 112 | String8 getSensorName(int handle) const; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 113 | int getSensorType(int handle) const; |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 114 | void recordLastValue(sensors_event_t const * buffer, size_t count); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 115 | static void sortEventBuffer(sensors_event_t* buffer, size_t count); |
| 116 | void registerSensor(SensorInterface* sensor); |
| 117 | void registerVirtualSensor(SensorInterface* sensor); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 118 | status_t cleanupWithoutDisable(const sp<SensorEventConnection>& connection, |
| 119 | int handle); |
| 120 | void cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection, |
| 121 | sensors_event_t const* buffer, const int count); |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 122 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 123 | // constants |
| 124 | Vector<Sensor> mSensorList; |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 125 | Vector<Sensor> mUserSensorListDebug; |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 126 | Vector<Sensor> mUserSensorList; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 127 | DefaultKeyedVector<int, SensorInterface*> mSensorMap; |
| 128 | Vector<SensorInterface *> mVirtualSensorList; |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 129 | status_t mInitCheck; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 130 | |
| 131 | // protected by mLock |
| 132 | mutable Mutex mLock; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 133 | DefaultKeyedVector<int, SensorRecord*> mActiveSensors; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 134 | DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 135 | SortedVector< wp<SensorEventConnection> > mActiveConnections; |
| 136 | |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 137 | // The size of this vector is constant, only the items are mutable |
| 138 | KeyedVector<int32_t, sensors_event_t> mLastEventSeen; |
| 139 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 140 | public: |
| 141 | static char const* getServiceName() { return "sensorservice"; } |
| 142 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 143 | void cleanupConnection(SensorEventConnection* connection); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 144 | status_t enable(const sp<SensorEventConnection>& connection, int handle); |
| 145 | status_t disable(const sp<SensorEventConnection>& connection, int handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 146 | status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | // --------------------------------------------------------------------------- |
| 150 | }; // namespace android |
| 151 | |
| 152 | #endif // ANDROID_SENSOR_SERVICE_H |