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 |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 41 | // Max size is 1 MB which is enough to accept a batch of about 10k events. |
| 42 | #define MAX_SOCKET_BUFFER_SIZE_BATCHED 1024 * 1024 |
| 43 | #define SOCKET_BUFFER_SIZE_NON_BATCHED 4 * 1024 |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 44 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 45 | struct sensors_poll_device_t; |
| 46 | struct sensors_module_t; |
| 47 | |
| 48 | namespace android { |
| 49 | // --------------------------------------------------------------------------- |
| 50 | |
| 51 | class SensorService : |
| 52 | public BinderService<SensorService>, |
| 53 | public BnSensorServer, |
| 54 | protected Thread |
| 55 | { |
Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 56 | friend class BinderService<SensorService>; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 57 | |
Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 58 | static const char* WAKE_LOCK_NAME; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 59 | |
Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 60 | static char const* getServiceName() ANDROID_API { return "sensorservice"; } |
| 61 | SensorService() ANDROID_API; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 62 | virtual ~SensorService(); |
| 63 | |
| 64 | virtual void onFirstRef(); |
| 65 | |
| 66 | // Thread interface |
| 67 | virtual bool threadLoop(); |
| 68 | |
| 69 | // ISensorServer interface |
| 70 | virtual Vector<Sensor> getSensorList(); |
| 71 | virtual sp<ISensorEventConnection> createSensorEventConnection(); |
| 72 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 73 | |
| 74 | |
| 75 | class SensorEventConnection : public BnSensorEventConnection { |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 76 | virtual ~SensorEventConnection(); |
| 77 | virtual void onFirstRef(); |
Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 78 | virtual sp<BitTube> getSensorChannel() const; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 79 | virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs, |
| 80 | nsecs_t maxBatchReportLatencyNs, int reservedFlags); |
| 81 | virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs); |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame^] | 82 | virtual status_t flush(); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 83 | // Count the number of flush complete events which are about to be dropped in the buffer. |
| 84 | // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be |
| 85 | // sent separately before the next batch of events. |
| 86 | void countFlushCompleteEvents(sensors_event_t* scratch, int numEventsDropped); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 87 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 88 | sp<SensorService> const mService; |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 89 | sp<BitTube> mChannel; |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 90 | uid_t mUid; |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 91 | mutable Mutex mConnectionLock; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 92 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 93 | struct FlushInfo { |
| 94 | // The number of flush complete events dropped for this sensor is stored here. |
| 95 | // They are sent separately before the next batch of events. |
| 96 | int mPendingFlushEventsToSend; |
| 97 | // Every activate is preceded by a flush. Only after the first flush complete is |
| 98 | // received, the events for the sensor are sent on that *connection*. |
| 99 | bool mFirstFlushPending; |
| 100 | FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {} |
| 101 | }; |
| 102 | // protected by SensorService::mLock. Key for this vector is the sensor handle. |
| 103 | KeyedVector<int, FlushInfo> mSensorInfo; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 104 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 105 | public: |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 106 | SensorEventConnection(const sp<SensorService>& service, uid_t uid); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 107 | |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 108 | status_t sendEvents(sensors_event_t const* buffer, size_t count, |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 109 | sensors_event_t* scratch = NULL); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 110 | bool hasSensor(int32_t handle) const; |
| 111 | bool hasAnySensor() const; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 112 | bool addSensor(int32_t handle); |
| 113 | bool removeSensor(int32_t handle); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 114 | void setFirstFlushPending(int32_t handle, bool value); |
| 115 | void incrementPendingFlushCount(int32_t handle); |
| 116 | void dump(String8& result); |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 117 | |
| 118 | uid_t getUid() const { return mUid; } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | class SensorRecord { |
| 122 | SortedVector< wp<SensorEventConnection> > mConnections; |
| 123 | public: |
| 124 | SensorRecord(const sp<SensorEventConnection>& connection); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 125 | bool addConnection(const sp<SensorEventConnection>& connection); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 126 | bool removeConnection(const wp<SensorEventConnection>& connection); |
| 127 | size_t getNumConnections() const { return mConnections.size(); } |
| 128 | }; |
| 129 | |
| 130 | SortedVector< wp<SensorEventConnection> > getActiveConnections() const; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 131 | DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 132 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 133 | String8 getSensorName(int handle) const; |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 134 | void recordLastValue(sensors_event_t const * buffer, size_t count); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 135 | static void sortEventBuffer(sensors_event_t* buffer, size_t count); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 136 | Sensor registerSensor(SensorInterface* sensor); |
| 137 | Sensor registerVirtualSensor(SensorInterface* sensor); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 138 | status_t cleanupWithoutDisable( |
| 139 | const sp<SensorEventConnection>& connection, int handle); |
| 140 | status_t cleanupWithoutDisableLocked( |
| 141 | const sp<SensorEventConnection>& connection, int handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 142 | void cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection, |
Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 143 | sensors_event_t const* buffer, const int count); |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 144 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 145 | // constants |
| 146 | Vector<Sensor> mSensorList; |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 147 | Vector<Sensor> mUserSensorListDebug; |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 148 | Vector<Sensor> mUserSensorList; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 149 | DefaultKeyedVector<int, SensorInterface*> mSensorMap; |
| 150 | Vector<SensorInterface *> mVirtualSensorList; |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 151 | status_t mInitCheck; |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 152 | size_t mSocketBufferSize; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 153 | |
| 154 | // protected by mLock |
| 155 | mutable Mutex mLock; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 156 | DefaultKeyedVector<int, SensorRecord*> mActiveSensors; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 157 | DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 158 | SortedVector< wp<SensorEventConnection> > mActiveConnections; |
| 159 | |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 160 | // The size of this vector is constant, only the items are mutable |
| 161 | KeyedVector<int32_t, sensors_event_t> mLastEventSeen; |
| 162 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 163 | public: |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 164 | void cleanupConnection(SensorEventConnection* connection); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 165 | status_t enable(const sp<SensorEventConnection>& connection, int handle, |
| 166 | nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 167 | status_t disable(const sp<SensorEventConnection>& connection, int handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 168 | status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 169 | status_t flushSensor(const sp<SensorEventConnection>& connection, int handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | // --------------------------------------------------------------------------- |
| 173 | }; // namespace android |
| 174 | |
| 175 | #endif // ANDROID_SENSOR_SERVICE_H |