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> |
| 30 | #include <binder/Permission.h> |
| 31 | |
| 32 | #include <gui/Sensor.h> |
| 33 | #include <gui/SensorChannel.h> |
| 34 | #include <gui/ISensorServer.h> |
| 35 | #include <gui/ISensorEventConnection.h> |
| 36 | |
| 37 | // --------------------------------------------------------------------------- |
| 38 | |
| 39 | struct sensors_poll_device_t; |
| 40 | struct sensors_module_t; |
| 41 | |
| 42 | namespace android { |
| 43 | // --------------------------------------------------------------------------- |
| 44 | |
| 45 | class SensorService : |
| 46 | public BinderService<SensorService>, |
| 47 | public BnSensorServer, |
| 48 | protected Thread |
| 49 | { |
| 50 | friend class BinderService<SensorService>; |
| 51 | |
Mathias Agopian | 24d7235 | 2010-11-05 19:12:58 -0700 | [diff] [blame] | 52 | static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz |
| 53 | static const nsecs_t DEFAULT_EVENTS_PERIOD = 200000000; // 5 Hz |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 54 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 55 | SensorService(); |
| 56 | virtual ~SensorService(); |
| 57 | |
| 58 | virtual void onFirstRef(); |
| 59 | |
| 60 | // Thread interface |
| 61 | virtual bool threadLoop(); |
| 62 | |
| 63 | // ISensorServer interface |
| 64 | virtual Vector<Sensor> getSensorList(); |
| 65 | virtual sp<ISensorEventConnection> createSensorEventConnection(); |
| 66 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 67 | |
| 68 | |
| 69 | class SensorEventConnection : public BnSensorEventConnection { |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 70 | virtual ~SensorEventConnection(); |
| 71 | virtual void onFirstRef(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 72 | virtual sp<SensorChannel> getSensorChannel() const; |
| 73 | virtual status_t enableDisable(int handle, bool enabled); |
| 74 | virtual status_t setEventRate(int handle, nsecs_t ns); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 75 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 76 | sp<SensorService> const mService; |
| 77 | sp<SensorChannel> const mChannel; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 78 | |
| 79 | // protected by SensorService::mLock |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 80 | struct SensorInfo { |
| 81 | SensorInfo() : ns(DEFAULT_EVENTS_PERIOD) { } |
| 82 | nsecs_t ns; |
| 83 | }; |
| 84 | DefaultKeyedVector<int32_t, SensorInfo> mSensorInfo; |
| 85 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 86 | public: |
| 87 | SensorEventConnection(const sp<SensorService>& service); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 88 | |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 89 | status_t sendEvents(sensors_event_t const* buffer, size_t count, |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 90 | sensors_event_t* scratch = NULL); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 91 | bool hasSensor(int32_t handle) const; |
| 92 | bool hasAnySensor() const; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 93 | bool addSensor(int32_t handle); |
| 94 | bool removeSensor(int32_t handle); |
| 95 | status_t setEventRateLocked(int handle, nsecs_t ns); |
| 96 | nsecs_t getEventRateForSensor(int32_t handle) const { |
| 97 | return mSensorInfo.valueFor(handle).ns; |
| 98 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | class SensorRecord { |
| 102 | SortedVector< wp<SensorEventConnection> > mConnections; |
| 103 | public: |
| 104 | SensorRecord(const sp<SensorEventConnection>& connection); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 105 | bool addConnection(const sp<SensorEventConnection>& connection); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 106 | bool removeConnection(const wp<SensorEventConnection>& connection); |
| 107 | size_t getNumConnections() const { return mConnections.size(); } |
| 108 | }; |
| 109 | |
| 110 | SortedVector< wp<SensorEventConnection> > getActiveConnections() const; |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 111 | String8 getSensorName(int handle) const; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 112 | status_t recomputeEventsPeriodLocked(int32_t handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 113 | |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame^] | 114 | void recordLastValue(sensors_event_t const * buffer, size_t count); |
| 115 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 116 | // constants |
| 117 | Vector<Sensor> mSensorList; |
| 118 | struct sensors_poll_device_t* mSensorDevice; |
| 119 | struct sensors_module_t* mSensorModule; |
| 120 | Permission mDump; |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 121 | status_t mInitCheck; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 122 | |
| 123 | // protected by mLock |
| 124 | mutable Mutex mLock; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 125 | DefaultKeyedVector<int, SensorRecord*> mActiveSensors; |
| 126 | SortedVector< wp<SensorEventConnection> > mActiveConnections; |
| 127 | |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 128 | // The size of this vector is constant, only the items are mutable |
| 129 | KeyedVector<int32_t, sensors_event_t> mLastEventSeen; |
| 130 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 131 | public: |
| 132 | static char const* getServiceName() { return "sensorservice"; } |
| 133 | |
| 134 | void cleanupConnection(const wp<SensorEventConnection>& connection); |
| 135 | status_t enable(const sp<SensorEventConnection>& connection, int handle); |
| 136 | status_t disable(const sp<SensorEventConnection>& connection, int handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 137 | status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | // --------------------------------------------------------------------------- |
| 141 | }; // namespace android |
| 142 | |
| 143 | #endif // ANDROID_SENSOR_SERVICE_H |