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 |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame^] | 44 | #define WAKE_UP_SENSOR_EVENT_NEEDS_ACK (1 << 31) |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 45 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 46 | struct sensors_poll_device_t; |
| 47 | struct sensors_module_t; |
| 48 | |
| 49 | namespace android { |
| 50 | // --------------------------------------------------------------------------- |
| 51 | |
| 52 | class SensorService : |
| 53 | public BinderService<SensorService>, |
| 54 | public BnSensorServer, |
| 55 | protected Thread |
| 56 | { |
Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 57 | friend class BinderService<SensorService>; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 58 | |
Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 59 | static const char* WAKE_LOCK_NAME; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 60 | |
Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 61 | static char const* getServiceName() ANDROID_API { return "sensorservice"; } |
| 62 | SensorService() ANDROID_API; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 63 | virtual ~SensorService(); |
| 64 | |
| 65 | virtual void onFirstRef(); |
| 66 | |
| 67 | // Thread interface |
| 68 | virtual bool threadLoop(); |
| 69 | |
| 70 | // ISensorServer interface |
| 71 | virtual Vector<Sensor> getSensorList(); |
| 72 | virtual sp<ISensorEventConnection> createSensorEventConnection(); |
| 73 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 74 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 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 | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame^] | 83 | void decreaseWakeLockRefCount(); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 84 | // Count the number of flush complete events which are about to be dropped in the buffer. |
| 85 | // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be |
| 86 | // sent separately before the next batch of events. |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 87 | void countFlushCompleteEventsLocked(sensors_event_t* scratch, int numEventsDropped); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 88 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame^] | 89 | // Check if there are any wake up events in the buffer. If yes, increment the ref count. |
| 90 | // Increment it by exactly one unit for each packet sent on the socket. SOCK_SEQPACKET for |
| 91 | // the socket ensures that either the entire packet is read or dropped. |
| 92 | // Return 1 if mWakeLockRefCount has been incremented, zero if not. |
| 93 | int countWakeUpSensorEventsLocked(sensors_event_t* scratch, const int count); |
| 94 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 95 | sp<SensorService> const mService; |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 96 | sp<BitTube> mChannel; |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 97 | uid_t mUid; |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 98 | mutable Mutex mConnectionLock; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame^] | 99 | // Number of events from wake up sensors which are still pending and haven't been delivered |
| 100 | // to the corresponding application. It is incremented by one unit for each write to the |
| 101 | // socket. |
| 102 | int mWakeLockRefCount; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 103 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 104 | struct FlushInfo { |
| 105 | // The number of flush complete events dropped for this sensor is stored here. |
| 106 | // They are sent separately before the next batch of events. |
| 107 | int mPendingFlushEventsToSend; |
| 108 | // Every activate is preceded by a flush. Only after the first flush complete is |
| 109 | // received, the events for the sensor are sent on that *connection*. |
| 110 | bool mFirstFlushPending; |
| 111 | FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {} |
| 112 | }; |
| 113 | // protected by SensorService::mLock. Key for this vector is the sensor handle. |
| 114 | KeyedVector<int, FlushInfo> mSensorInfo; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 115 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 116 | public: |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 117 | SensorEventConnection(const sp<SensorService>& service, uid_t uid); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 118 | |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 119 | status_t sendEvents(sensors_event_t const* buffer, size_t count, |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame^] | 120 | sensors_event_t* scratch); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 121 | bool hasSensor(int32_t handle) const; |
| 122 | bool hasAnySensor() const; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 123 | bool addSensor(int32_t handle); |
| 124 | bool removeSensor(int32_t handle); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 125 | void setFirstFlushPending(int32_t handle, bool value); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 126 | void dump(String8& result); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame^] | 127 | bool needsWakeLock(); |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 128 | |
| 129 | uid_t getUid() const { return mUid; } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | class SensorRecord { |
| 133 | SortedVector< wp<SensorEventConnection> > mConnections; |
| 134 | public: |
| 135 | SensorRecord(const sp<SensorEventConnection>& connection); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 136 | bool addConnection(const sp<SensorEventConnection>& connection); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 137 | bool removeConnection(const wp<SensorEventConnection>& connection); |
| 138 | size_t getNumConnections() const { return mConnections.size(); } |
| 139 | }; |
| 140 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 141 | String8 getSensorName(int handle) const; |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 142 | bool isVirtualSensor(int handle) const; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 143 | Sensor getSensorFromHandle(int handle) const; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame^] | 144 | bool isWakeUpSensor(int type) const; |
| 145 | void recordLastValueLocked(const sensors_event_t* buffer, size_t count); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 146 | static void sortEventBuffer(sensors_event_t* buffer, size_t count); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 147 | Sensor registerSensor(SensorInterface* sensor); |
| 148 | Sensor registerVirtualSensor(SensorInterface* sensor); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 149 | status_t cleanupWithoutDisable( |
| 150 | const sp<SensorEventConnection>& connection, int handle); |
| 151 | status_t cleanupWithoutDisableLocked( |
| 152 | const sp<SensorEventConnection>& connection, int handle); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame^] | 153 | void cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection, |
Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 154 | sensors_event_t const* buffer, const int count); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 155 | static bool canAccessSensor(const Sensor& sensor); |
| 156 | static bool verifyCanAccessSensor(const Sensor& sensor, const char* operation); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame^] | 157 | // SensorService acquires a partial wakelock for delivering events from wake up sensors. This |
| 158 | // method checks whether all the events from these wake up sensors have been delivered to the |
| 159 | // corresponding applications, if yes the wakelock is released. |
| 160 | void checkWakeLockState(); |
| 161 | void checkWakeLockStateLocked(); |
| 162 | bool isWakeUpSensorEvent(const sensors_event_t& event) const; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 163 | // constants |
| 164 | Vector<Sensor> mSensorList; |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 165 | Vector<Sensor> mUserSensorListDebug; |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 166 | Vector<Sensor> mUserSensorList; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 167 | DefaultKeyedVector<int, SensorInterface*> mSensorMap; |
| 168 | Vector<SensorInterface *> mVirtualSensorList; |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 169 | status_t mInitCheck; |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 170 | size_t mSocketBufferSize; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 171 | |
| 172 | // protected by mLock |
| 173 | mutable Mutex mLock; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 174 | DefaultKeyedVector<int, SensorRecord*> mActiveSensors; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 175 | DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 176 | SortedVector< wp<SensorEventConnection> > mActiveConnections; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame^] | 177 | bool mWakeLockAcquired; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 178 | |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 179 | // The size of this vector is constant, only the items are mutable |
| 180 | KeyedVector<int32_t, sensors_event_t> mLastEventSeen; |
| 181 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 182 | public: |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 183 | void cleanupConnection(SensorEventConnection* connection); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 184 | status_t enable(const sp<SensorEventConnection>& connection, int handle, |
| 185 | nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 186 | status_t disable(const sp<SensorEventConnection>& connection, int handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 187 | status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 188 | status_t flushSensor(const sp<SensorEventConnection>& connection, int handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | // --------------------------------------------------------------------------- |
| 192 | }; // namespace android |
| 193 | |
| 194 | #endif // ANDROID_SENSOR_SERVICE_H |