Mathias Agopian | 1bf7978 | 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 | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 30 | |
| 31 | #include <gui/Sensor.h> |
| 32 | #include <gui/SensorChannel.h> |
| 33 | #include <gui/ISensorServer.h> |
| 34 | #include <gui/ISensorEventConnection.h> |
| 35 | |
Mathias Agopian | 671a6ff | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 36 | #include "SensorInterface.h" |
| 37 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 38 | // --------------------------------------------------------------------------- |
| 39 | |
Mathias Agopian | 9ff73de | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 40 | #define DEBUG_CONNECTIONS false |
| 41 | |
Mathias Agopian | 1bf7978 | 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 | 94c4f5c | 2010-11-05 19:12:58 -0700 | [diff] [blame] | 55 | static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz |
Mathias Agopian | d865351 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 56 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 57 | SensorService(); |
| 58 | virtual ~SensorService(); |
| 59 | |
| 60 | virtual void onFirstRef(); |
| 61 | |
| 62 | // Thread interface |
| 63 | virtual bool threadLoop(); |
| 64 | |
| 65 | // ISensorServer interface |
| 66 | virtual Vector<Sensor> getSensorList(); |
| 67 | virtual sp<ISensorEventConnection> createSensorEventConnection(); |
| 68 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 69 | |
| 70 | |
| 71 | class SensorEventConnection : public BnSensorEventConnection { |
Mathias Agopian | d865351 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 72 | virtual ~SensorEventConnection(); |
| 73 | virtual void onFirstRef(); |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 74 | virtual sp<SensorChannel> getSensorChannel() const; |
| 75 | virtual status_t enableDisable(int handle, bool enabled); |
| 76 | virtual status_t setEventRate(int handle, nsecs_t ns); |
Mathias Agopian | d865351 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 77 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 78 | sp<SensorService> const mService; |
| 79 | sp<SensorChannel> const mChannel; |
Mathias Agopian | f33a6e9 | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 80 | mutable Mutex mConnectionLock; |
Mathias Agopian | d865351 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 81 | |
Mathias Agopian | 671a6ff | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 82 | // protected by SensorService::mLock |
| 83 | SortedVector<int> mSensorInfo; |
Mathias Agopian | d865351 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 84 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 85 | public: |
| 86 | SensorEventConnection(const sp<SensorService>& service); |
Mathias Agopian | d865351 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 87 | |
Mathias Agopian | efba8bf | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 88 | status_t sendEvents(sensors_event_t const* buffer, size_t count, |
Mathias Agopian | aea786f | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 89 | sensors_event_t* scratch = NULL); |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 90 | bool hasSensor(int32_t handle) const; |
| 91 | bool hasAnySensor() const; |
Mathias Agopian | d865351 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 92 | bool addSensor(int32_t handle); |
| 93 | bool removeSensor(int32_t handle); |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | class SensorRecord { |
| 97 | SortedVector< wp<SensorEventConnection> > mConnections; |
| 98 | public: |
| 99 | SensorRecord(const sp<SensorEventConnection>& connection); |
Mathias Agopian | d865351 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 100 | bool addConnection(const sp<SensorEventConnection>& connection); |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 101 | bool removeConnection(const wp<SensorEventConnection>& connection); |
| 102 | size_t getNumConnections() const { return mConnections.size(); } |
| 103 | }; |
| 104 | |
| 105 | SortedVector< wp<SensorEventConnection> > getActiveConnections() const; |
Mathias Agopian | 671a6ff | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 106 | DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const; |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 107 | |
Mathias Agopian | 671a6ff | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 108 | String8 getSensorName(int handle) const; |
Mathias Agopian | 3a9223e | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 109 | void recordLastValue(sensors_event_t const * buffer, size_t count); |
Mathias Agopian | 671a6ff | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 110 | static void sortEventBuffer(sensors_event_t* buffer, size_t count); |
| 111 | void registerSensor(SensorInterface* sensor); |
| 112 | void registerVirtualSensor(SensorInterface* sensor); |
Mathias Agopian | 3a9223e | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 113 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 114 | // constants |
| 115 | Vector<Sensor> mSensorList; |
Mathias Agopian | f8a6737 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 116 | Vector<Sensor> mUserSensorList; |
Mathias Agopian | 671a6ff | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 117 | DefaultKeyedVector<int, SensorInterface*> mSensorMap; |
| 118 | Vector<SensorInterface *> mVirtualSensorList; |
Mathias Agopian | 5df13ef | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 119 | status_t mInitCheck; |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 120 | |
| 121 | // protected by mLock |
| 122 | mutable Mutex mLock; |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 123 | DefaultKeyedVector<int, SensorRecord*> mActiveSensors; |
Mathias Agopian | 671a6ff | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 124 | DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors; |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 125 | SortedVector< wp<SensorEventConnection> > mActiveConnections; |
| 126 | |
Mathias Agopian | aea786f | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 127 | // The size of this vector is constant, only the items are mutable |
| 128 | KeyedVector<int32_t, sensors_event_t> mLastEventSeen; |
| 129 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 130 | public: |
| 131 | static char const* getServiceName() { return "sensorservice"; } |
| 132 | |
Mathias Agopian | f0aec21 | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 133 | void cleanupConnection(SensorEventConnection* connection); |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 134 | status_t enable(const sp<SensorEventConnection>& connection, int handle); |
| 135 | status_t disable(const sp<SensorEventConnection>& connection, int handle); |
Mathias Agopian | d865351 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 136 | status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns); |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | // --------------------------------------------------------------------------- |
| 140 | }; // namespace android |
| 141 | |
| 142 | #endif // ANDROID_SENSOR_SERVICE_H |