| 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 |  | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 20 | #include "SensorList.h" | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 21 | #include "RecentEventLogger.h" | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 22 |  | 
| Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 23 | #include <android-base/macros.h> | 
| Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 24 | #include <binder/AppOpsManager.h> | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 25 | #include <binder/BinderService.h> | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 26 | #include <binder/IUidObserver.h> | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 27 | #include <cutils/compiler.h> | 
| Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 28 | #include <cutils/multiuser.h> | 
| Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 29 | #include <sensor/ISensorServer.h> | 
 | 30 | #include <sensor/ISensorEventConnection.h> | 
 | 31 | #include <sensor/Sensor.h> | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 32 | #include "android/hardware/BnSensorPrivacyListener.h" | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 33 |  | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 34 | #include <utils/AndroidThreads.h> | 
 | 35 | #include <utils/KeyedVector.h> | 
 | 36 | #include <utils/Looper.h> | 
 | 37 | #include <utils/SortedVector.h> | 
 | 38 | #include <utils/String8.h> | 
 | 39 | #include <utils/Vector.h> | 
 | 40 | #include <utils/threads.h> | 
 | 41 |  | 
 | 42 | #include <stdint.h> | 
 | 43 | #include <sys/types.h> | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 44 | #include <unordered_map> | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 45 | #include <unordered_set> | 
| Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 46 | #include <vector> | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 47 |  | 
| Bernhard Rosenkränzer | 72952ef | 2014-11-17 21:03:39 +0100 | [diff] [blame] | 48 | #if __clang__ | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 49 | // Clang warns about SensorEventConnection::dump hiding BBinder::dump. The cause isn't fixable | 
 | 50 | // without changing the API, so let's tell clang this is indeed intentional. | 
| Bernhard Rosenkränzer | 72952ef | 2014-11-17 21:03:39 +0100 | [diff] [blame] | 51 | #pragma clang diagnostic ignored "-Woverloaded-virtual" | 
 | 52 | #endif | 
 | 53 |  | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 54 | // --------------------------------------------------------------------------- | 
| Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 55 | #define IGNORE_HARDWARE_FUSION  false | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 56 | #define DEBUG_CONNECTIONS   false | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 57 | // Max size is 100 KB which is enough to accept a batch of about 1000 events. | 
| Chih-Hung Hsieh | a389c7a | 2016-05-20 11:40:04 -0700 | [diff] [blame] | 58 | #define MAX_SOCKET_BUFFER_SIZE_BATCHED (100 * 1024) | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 59 | // For older HALs which don't support batching, use a smaller socket buffer size. | 
| Chih-Hung Hsieh | a389c7a | 2016-05-20 11:40:04 -0700 | [diff] [blame] | 60 | #define SOCKET_BUFFER_SIZE_NON_BATCHED (4 * 1024) | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 61 |  | 
| Peng Xu | 363b3fd | 2016-07-07 15:40:08 -0700 | [diff] [blame] | 62 | #define SENSOR_REGISTRATIONS_BUF_SIZE 200 | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 63 |  | 
| Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 64 | // Apps that targets S+ and do not have HIGH_SAMPLING_RATE_SENSORS permission will be capped | 
| Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 65 | // at 200 Hz. The cap also applies to all requests when the mic toggle is flipped to on, regardless | 
 | 66 | // of their target SDKs and permission. | 
| Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 67 | // Capped sampling periods for apps that have non-direct sensor connections. | 
 | 68 | #define SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS (5 * 1000 * 1000) | 
 | 69 | // Capped sampling rate level for apps that have direct sensor connections. | 
 | 70 | // The enum SENSOR_DIRECT_RATE_NORMAL corresponds to a rate value of at most 110 Hz. | 
 | 71 | #define SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL SENSOR_DIRECT_RATE_NORMAL | 
 | 72 |  | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 73 | namespace android { | 
 | 74 | // --------------------------------------------------------------------------- | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 75 | class SensorInterface; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 76 |  | 
 | 77 | class SensorService : | 
 | 78 |         public BinderService<SensorService>, | 
 | 79 |         public BnSensorServer, | 
 | 80 |         protected Thread | 
 | 81 | { | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 82 |     // nested class/struct for internal use | 
 | 83 |     class SensorEventConnection; | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 84 |     class SensorDirectConnection; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 85 |  | 
 | 86 | public: | 
| Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 87 |     enum UidState { | 
 | 88 |       UID_STATE_ACTIVE = 0, | 
 | 89 |       UID_STATE_IDLE, | 
 | 90 |     }; | 
 | 91 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 92 |     void cleanupConnection(SensorEventConnection* connection); | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 93 |     void cleanupConnection(SensorDirectConnection* c); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 94 |  | 
 | 95 |     status_t enable(const sp<SensorEventConnection>& connection, int handle, | 
 | 96 |                     nsecs_t samplingPeriodNs,  nsecs_t maxBatchReportLatencyNs, int reservedFlags, | 
 | 97 |                     const String16& opPackageName); | 
 | 98 |  | 
 | 99 |     status_t disable(const sp<SensorEventConnection>& connection, int handle); | 
 | 100 |  | 
 | 101 |     status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns, | 
 | 102 |                           const String16& opPackageName); | 
 | 103 |  | 
 | 104 |     status_t flushSensor(const sp<SensorEventConnection>& connection, | 
 | 105 |                          const String16& opPackageName); | 
 | 106 |  | 
| Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 107 |     // Returns true if a sensor should be throttled according to our rate-throttling rules. | 
 | 108 |     static bool isSensorInCappedSet(int sensorType); | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 109 |  | 
 | 110 |     virtual status_t shellCommand(int in, int out, int err, Vector<String16>& args); | 
 | 111 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 112 | private: | 
| Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 113 |     friend class BinderService<SensorService>; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 114 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 115 |     // nested class/struct for internal use | 
| Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 116 |     class ConnectionSafeAutolock; | 
 | 117 |     class SensorConnectionHolder; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 118 |     class SensorEventAckReceiver; | 
| Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 119 |     class SensorRecord; | 
| Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 120 |     class SensorRegistrationInfo; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 121 |  | 
| Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 122 |     // Promoting a SensorEventConnection or SensorDirectConnection from wp to sp must be done with | 
 | 123 |     // mLock held, but destroying that sp must be done unlocked to avoid a race condition that | 
 | 124 |     // causes a deadlock (remote dies while we hold a local sp, then our decStrong() call invokes | 
 | 125 |     // the dtor -> cleanupConnection() tries to re-lock the mutex). This class ensures safe usage | 
 | 126 |     // by wrapping a Mutex::Autolock on SensorService's mLock, plus vectors that hold promoted sp<> | 
 | 127 |     // references until the lock is released, when they are safely destroyed. | 
 | 128 |     // All read accesses to the connection lists in mConnectionHolder must be done via this class. | 
 | 129 |     class ConnectionSafeAutolock final { | 
 | 130 |     public: | 
 | 131 |         // Returns a list of non-null promoted connection references | 
 | 132 |         const std::vector<sp<SensorEventConnection>>& getActiveConnections(); | 
 | 133 |         const std::vector<sp<SensorDirectConnection>>& getDirectConnections(); | 
 | 134 |  | 
 | 135 |     private: | 
 | 136 |         // Constructed via SensorConnectionHolder::lock() | 
 | 137 |         friend class SensorConnectionHolder; | 
 | 138 |         explicit ConnectionSafeAutolock(SensorConnectionHolder& holder, Mutex& mutex); | 
 | 139 |         DISALLOW_IMPLICIT_CONSTRUCTORS(ConnectionSafeAutolock); | 
 | 140 |  | 
 | 141 |         // NOTE: Order of these members is important, as the destructor for non-static members | 
 | 142 |         // get invoked in the reverse order of their declaration. Here we are relying on the | 
 | 143 |         // Autolock to be destroyed *before* the vectors, so the sp<> objects are destroyed without | 
 | 144 |         // the lock held, which avoids the deadlock. | 
 | 145 |         SensorConnectionHolder& mConnectionHolder; | 
 | 146 |         std::vector<std::vector<sp<SensorEventConnection>>> mReferencedActiveConnections; | 
 | 147 |         std::vector<std::vector<sp<SensorDirectConnection>>> mReferencedDirectConnections; | 
 | 148 |         Mutex::Autolock mAutolock; | 
 | 149 |  | 
 | 150 |         template<typename ConnectionType> | 
 | 151 |         const std::vector<sp<ConnectionType>>& getConnectionsHelper( | 
 | 152 |                 const SortedVector<wp<ConnectionType>>& connectionList, | 
 | 153 |                 std::vector<std::vector<sp<ConnectionType>>>* referenceHolder); | 
 | 154 |     }; | 
 | 155 |  | 
 | 156 |     // Encapsulates the collection of active SensorEventConection and SensorDirectConnection | 
 | 157 |     // references. Write access is done through this class with mLock held, but all read access | 
 | 158 |     // must be routed through ConnectionSafeAutolock. | 
 | 159 |     class SensorConnectionHolder { | 
 | 160 |     public: | 
 | 161 |         void addEventConnectionIfNotPresent(const sp<SensorEventConnection>& connection); | 
 | 162 |         void removeEventConnection(const wp<SensorEventConnection>& connection); | 
 | 163 |  | 
 | 164 |         void addDirectConnection(const sp<SensorDirectConnection>& connection); | 
 | 165 |         void removeDirectConnection(const wp<SensorDirectConnection>& connection); | 
 | 166 |  | 
 | 167 |         // Pass in the mutex that protects this connection holder; acquires the lock and returns an | 
 | 168 |         // object that can be used to safely read the lists of connections | 
 | 169 |         ConnectionSafeAutolock lock(Mutex& mutex); | 
 | 170 |  | 
 | 171 |     private: | 
 | 172 |         friend class ConnectionSafeAutolock; | 
 | 173 |         SortedVector< wp<SensorEventConnection> > mActiveConnections; | 
 | 174 |         SortedVector< wp<SensorDirectConnection> > mDirectConnections; | 
 | 175 |     }; | 
 | 176 |  | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 177 |     // If accessing a sensor we need to make sure the UID has access to it. If | 
 | 178 |     // the app UID is idle then it cannot access sensors and gets no trigger | 
 | 179 |     // events, no on-change events, flush event behavior does not change, and | 
 | 180 |     // recurring events are the same as the first one delivered in idle state | 
 | 181 |     // emulating no sensor change. As soon as the app UID transitions to an | 
 | 182 |     // active state we will start reporting events as usual and vise versa. This | 
 | 183 |     // approach transparently handles observing sensors while the app UID transitions | 
 | 184 |     // between idle/active state avoiding to get stuck in a state receiving sensor | 
 | 185 |     // data while idle or not receiving sensor data while active. | 
 | 186 |     class UidPolicy : public BnUidObserver { | 
 | 187 |         public: | 
 | 188 |             explicit UidPolicy(wp<SensorService> service) | 
 | 189 |                     : mService(service) {} | 
 | 190 |             void registerSelf(); | 
 | 191 |             void unregisterSelf(); | 
 | 192 |  | 
 | 193 |             bool isUidActive(uid_t uid); | 
 | 194 |  | 
 | 195 |             void onUidGone(uid_t uid, bool disabled); | 
 | 196 |             void onUidActive(uid_t uid); | 
 | 197 |             void onUidIdle(uid_t uid, bool disabled); | 
| Eric Laurent | 0559589 | 2018-10-18 14:56:24 -0700 | [diff] [blame] | 198 |             void onUidStateChanged(uid_t uid __unused, int32_t procState __unused, | 
| Hui Yu | ee03b78 | 2019-08-22 14:48:40 -0700 | [diff] [blame] | 199 |                                    int64_t procStateSeq __unused, int32_t capability __unused) {} | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 200 |  | 
 | 201 |             void addOverrideUid(uid_t uid, bool active); | 
 | 202 |             void removeOverrideUid(uid_t uid); | 
 | 203 |         private: | 
 | 204 |             bool isUidActiveLocked(uid_t uid); | 
 | 205 |             void updateOverrideUid(uid_t uid, bool active, bool insert); | 
 | 206 |  | 
 | 207 |             Mutex mUidLock; | 
 | 208 |             wp<SensorService> mService; | 
 | 209 |             std::unordered_set<uid_t> mActiveUids; | 
 | 210 |             std::unordered_map<uid_t, bool> mOverrideUids; | 
 | 211 |     }; | 
 | 212 |  | 
| Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 213 |     bool isUidActive(uid_t uid); | 
 | 214 |  | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 215 |     // Sensor privacy allows a user to disable access to all sensors on the device. When | 
 | 216 |     // enabled sensor privacy will prevent all apps, including active apps, from accessing | 
 | 217 |     // sensors, they will not receive trigger nor on-change events, flush event behavior | 
 | 218 |     // does not change, and recurring events are the same as the first one delivered when | 
 | 219 |     // sensor privacy was enabled. All sensor direct connections will be stopped as well | 
 | 220 |     // and new direct connections will not be allowed while sensor privacy is enabled. | 
 | 221 |     // Once sensor privacy is disabled access to sensors will be restored for active | 
 | 222 |     // apps, previously stopped direct connections will be restarted, and new direct | 
 | 223 |     // connections will be allowed again. | 
 | 224 |     class SensorPrivacyPolicy : public hardware::BnSensorPrivacyListener { | 
 | 225 |         public: | 
| Greg Kaiser | 1706759 | 2021-02-17 22:05:25 -0800 | [diff] [blame] | 226 |             explicit SensorPrivacyPolicy(wp<SensorService> service) | 
 | 227 |                     : mService(service), mIsIndividualMic(false), mUserId(0) {} | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 228 |             void registerSelf(); | 
 | 229 |             void unregisterSelf(); | 
 | 230 |  | 
| Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 231 |             status_t registerSelfForIndividual(int userId); | 
 | 232 |  | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 233 |             bool isSensorPrivacyEnabled(); | 
 | 234 |  | 
 | 235 |             binder::Status onSensorPrivacyChanged(bool enabled); | 
 | 236 |  | 
 | 237 |         private: | 
 | 238 |             wp<SensorService> mService; | 
| Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 239 |             Mutex mSensorPrivacyLock; | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 240 |             std::atomic_bool mSensorPrivacyEnabled; | 
| Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 241 |             bool mIsIndividualMic; | 
 | 242 |             userid_t mUserId; | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 243 |     }; | 
 | 244 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 245 |     enum Mode { | 
 | 246 |        // The regular operating mode where any application can register/unregister/call flush on | 
 | 247 |        // sensors. | 
 | 248 |        NORMAL = 0, | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 249 |        // This mode is only used for testing purposes. Not all HALs support this mode. In this mode, | 
 | 250 |        // the HAL ignores the sensor data provided by physical sensors and accepts the data that is | 
 | 251 |        // injected from the SensorService as if it were the real sensor data. This mode is primarily | 
 | 252 |        // used for testing various algorithms like vendor provided SensorFusion, Step Counter and | 
 | 253 |        // Step Detector etc. Typically in this mode, there will be a client (a | 
 | 254 |        // SensorEventConnection) which will be injecting sensor data into the HAL. Normal apps can | 
 | 255 |        // unregister and register for any sensor that supports injection. Registering to sensors | 
 | 256 |        // that do not support injection will give an error.  TODO(aakella) : Allow exactly one | 
 | 257 |        // client to inject sensor data at a time. | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 258 |        DATA_INJECTION = 1, | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 259 |        // This mode is used only for testing sensors. Each sensor can be tested in isolation with | 
 | 260 |        // the required sampling_rate and maxReportLatency parameters without having to think about | 
 | 261 |        // the data rates requested by other applications. End user devices are always expected to be | 
 | 262 |        // in NORMAL mode. When this mode is first activated, all active sensors from all connections | 
 | 263 |        // are disabled. Calling flush() will return an error. In this mode, only the requests from | 
 | 264 |        // selected apps whose package names are whitelisted are allowed (typically CTS apps).  Only | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 265 |        // these apps can register/unregister/call flush() on sensors. If SensorService switches to | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 266 |        // NORMAL mode again, all sensors that were previously registered to are activated with the | 
 | 267 |        // corresponding paramaters if the application hasn't unregistered for sensors in the mean | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 268 |        // time.  NOTE: Non whitelisted app whose sensors were previously deactivated may still | 
 | 269 |        // receive events if a whitelisted app requests data from the same sensor. | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 270 |        RESTRICTED = 2 | 
 | 271 |  | 
 | 272 |       // State Transitions supported. | 
 | 273 |       //     RESTRICTED   <---  NORMAL   ---> DATA_INJECTION | 
 | 274 |       //                  --->           <--- | 
| Aravind Akella | 5c53805 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 275 |  | 
 | 276 |       // Shell commands to switch modes in SensorService. | 
 | 277 |       // 1) Put SensorService in RESTRICTED mode with packageName .cts. If it is already in | 
 | 278 |       // restricted mode it is treated as a NO_OP (and packageName is NOT changed). | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 279 |       // | 
 | 280 |       //     $ adb shell dumpsys sensorservice restrict .cts. | 
| Aravind Akella | 5c53805 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 281 |       // | 
 | 282 |       // 2) Put SensorService in DATA_INJECTION mode with packageName .xts. If it is already in | 
 | 283 |       // data_injection mode it is treated as a NO_OP (and packageName is NOT changed). | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 284 |       // | 
 | 285 |       //     $ adb shell dumpsys sensorservice data_injection .xts. | 
| Aravind Akella | 5c53805 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 286 |       // | 
 | 287 |       // 3) Reset sensorservice back to NORMAL mode. | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 288 |       //     $ adb shell dumpsys sensorservice enable | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 289 |     }; | 
 | 290 |  | 
| Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 291 |     static const char* WAKE_LOCK_NAME; | 
| Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 292 |     static char const* getServiceName() ANDROID_API { return "sensorservice"; } | 
 | 293 |     SensorService() ANDROID_API; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 294 |     virtual ~SensorService(); | 
 | 295 |  | 
 | 296 |     virtual void onFirstRef(); | 
 | 297 |  | 
 | 298 |     // Thread interface | 
 | 299 |     virtual bool threadLoop(); | 
 | 300 |  | 
 | 301 |     // ISensorServer interface | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 302 |     virtual Vector<Sensor> getSensorList(const String16& opPackageName); | 
| Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 303 |     virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 304 |     virtual sp<ISensorEventConnection> createSensorEventConnection( | 
 | 305 |             const String8& packageName, | 
| Arthur Ishiguro | 340882c | 2021-02-18 15:17:44 -0800 | [diff] [blame] | 306 |             int requestedMode, const String16& opPackageName, const String16& attributionTag); | 
| Aravind Akella | 5c53805 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 307 |     virtual int isDataInjectionEnabled(); | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 308 |     virtual sp<ISensorEventConnection> createSensorDirectConnection(const String16& opPackageName, | 
 | 309 |             uint32_t size, int32_t type, int32_t format, const native_handle *resource); | 
| Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 310 |     virtual int setOperationParameter( | 
| Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 311 |             int32_t handle, int32_t type, const Vector<float> &floats, const Vector<int32_t> &ints); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 312 |     virtual status_t dump(int fd, const Vector<String16>& args); | 
| Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 313 |     status_t dumpProtoLocked(int fd, ConnectionSafeAutolock* connLock) const; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 314 |     String8 getSensorName(int handle) const; | 
| Arthur Ishiguro | 883748c | 2020-10-28 13:18:02 -0700 | [diff] [blame] | 315 |     String8 getSensorStringType(int handle) const; | 
| Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 316 |     bool isVirtualSensor(int handle) const; | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 317 |     sp<SensorInterface> getSensorInterfaceFromHandle(int handle) const; | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 318 |     bool isWakeUpSensor(int type) const; | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 319 |     void recordLastValueLocked(sensors_event_t const* buffer, size_t count); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 320 |     static void sortEventBuffer(sensors_event_t* buffer, size_t count); | 
| Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 321 |     const Sensor& registerSensor(SensorInterface* sensor, | 
 | 322 |                                  bool isDebug = false, bool isVirtual = false); | 
 | 323 |     const Sensor& registerVirtualSensor(SensorInterface* sensor, bool isDebug = false); | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 324 |     const Sensor& registerDynamicSensorLocked(SensorInterface* sensor, bool isDebug = false); | 
 | 325 |     bool unregisterDynamicSensorLocked(int handle); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 326 |     status_t cleanupWithoutDisable(const sp<SensorEventConnection>& connection, int handle); | 
 | 327 |     status_t cleanupWithoutDisableLocked(const sp<SensorEventConnection>& connection, int handle); | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 328 |     void cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection, | 
| Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 329 |             sensors_event_t const* buffer, const int count); | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 330 |     static bool canAccessSensor(const Sensor& sensor, const char* operation, | 
 | 331 |             const String16& opPackageName); | 
| Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 332 |     static bool hasPermissionForSensor(const Sensor& sensor); | 
 | 333 |     static int getTargetSdkVersion(const String16& opPackageName); | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 334 |     // SensorService acquires a partial wakelock for delivering events from wake up sensors. This | 
 | 335 |     // method checks whether all the events from these wake up sensors have been delivered to the | 
 | 336 |     // corresponding applications, if yes the wakelock is released. | 
 | 337 |     void checkWakeLockState(); | 
| Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 338 |     void checkWakeLockStateLocked(ConnectionSafeAutolock* connLock); | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 339 |     bool isWakeLockAcquired(); | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 340 |     bool isWakeUpSensorEvent(const sensors_event_t& event) const; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 341 |  | 
 | 342 |     sp<Looper> getLooper() const; | 
 | 343 |  | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 344 |     // Reset mWakeLockRefCounts for all SensorEventConnections to zero. This may happen if | 
 | 345 |     // SensorService did not receive any acknowledgements from apps which have registered for | 
 | 346 |     // wake_up sensors. | 
 | 347 |     void resetAllWakeLockRefCounts(); | 
 | 348 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 349 |     // Acquire or release wake_lock. If wake_lock is acquired, set the timeout in the looper to 5 | 
 | 350 |     // seconds and wake the looper. | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 351 |     void setWakeLockAcquiredLocked(bool acquire); | 
 | 352 |  | 
 | 353 |     // Send events from the event cache for this particular connection. | 
 | 354 |     void sendEventsFromCache(const sp<SensorEventConnection>& connection); | 
 | 355 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 356 |     // If SensorService is operating in RESTRICTED mode, only select whitelisted packages are | 
 | 357 |     // allowed to register for or call flush on sensors. Typically only cts test packages are | 
 | 358 |     // allowed. | 
 | 359 |     bool isWhiteListedPackage(const String8& packageName); | 
| Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 360 |  | 
 | 361 |     // Returns true if a connection with the specified opPackageName has no access to sensors | 
 | 362 |     // in the RESTRICTED mode (i.e. the service is in RESTRICTED mode, and the package is not | 
 | 363 |     // whitelisted). mLock must be held to invoke this method. | 
 | 364 |     bool isOperationRestrictedLocked(const String16& opPackageName); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 365 |  | 
| Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 366 |     status_t adjustSamplingPeriodBasedOnMicAndPermission(nsecs_t* requestedPeriodNs, | 
 | 367 |                                                     const String16& opPackageName); | 
 | 368 |     status_t adjustRateLevelBasedOnMicAndPermission(int* requestedRateLevel, | 
 | 369 |                                               const String16& opPackageName); | 
 | 370 |     bool isRateCappedBasedOnPermission(const String16& opPackageName); | 
 | 371 |     bool isPackageDebuggable(const String16& opPackageName); | 
 | 372 |  | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 373 |     // Reset the state of SensorService to NORMAL mode. | 
 | 374 |     status_t resetToNormalMode(); | 
 | 375 |     status_t resetToNormalModeLocked(); | 
 | 376 |  | 
| Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 377 |     // Transforms the UUIDs for all the sensors into proper IDs. | 
 | 378 |     void makeUuidsIntoIdsForSensorList(Vector<Sensor> &sensorList) const; | 
 | 379 |     // Gets the appropriate ID from the given UUID. | 
 | 380 |     int32_t getIdFromUuid(const Sensor::uuid_t &uuid) const; | 
 | 381 |     // Either read from storage or create a new one. | 
 | 382 |     static bool initializeHmacKey(); | 
 | 383 |  | 
| Peng Xu | 98d30f6 | 2016-08-01 18:12:11 -0700 | [diff] [blame] | 384 |     // Enable SCHED_FIFO priority for thread | 
 | 385 |     void enableSchedFifoMode(); | 
| Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 386 |  | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 387 |     // Sets whether the given UID can get sensor data | 
| Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 388 |     void onUidStateChanged(uid_t uid, UidState state); | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 389 |  | 
| Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 390 |     // Returns true if a connection with the given uid and opPackageName | 
 | 391 |     // currently has access to sensors. | 
 | 392 |     bool hasSensorAccess(uid_t uid, const String16& opPackageName); | 
 | 393 |     // Same as hasSensorAccess but with mLock held. | 
 | 394 |     bool hasSensorAccessLocked(uid_t uid, const String16& opPackageName); | 
 | 395 |  | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 396 |     // Overrides the UID state as if it is idle | 
 | 397 |     status_t handleSetUidState(Vector<String16>& args, int err); | 
 | 398 |     // Clears the override for the UID state | 
 | 399 |     status_t handleResetUidState(Vector<String16>& args, int err); | 
 | 400 |     // Gets the UID state | 
 | 401 |     status_t handleGetUidState(Vector<String16>& args, int out, int err); | 
 | 402 |     // Prints the shell command help | 
 | 403 |     status_t printHelp(int out); | 
 | 404 |  | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 405 |     // temporarily stops all active direct connections and disables all sensors | 
 | 406 |     void disableAllSensors(); | 
| Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 407 |     void disableAllSensorsLocked(ConnectionSafeAutolock* connLock); | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 408 |     // restarts the previously stopped direct connections and enables all sensors | 
 | 409 |     void enableAllSensors(); | 
| Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 410 |     void enableAllSensorsLocked(ConnectionSafeAutolock* connLock); | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 411 |  | 
| Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 412 |     // Caps active direct connections (when the mic toggle is flipped to on) | 
 | 413 |     void capRates(userid_t userId); | 
 | 414 |     // Removes the capped rate on active direct connections (when the mic toggle is flipped to off) | 
 | 415 |     void uncapRates(userid_t userId); | 
 | 416 |  | 
| Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 417 |     static uint8_t sHmacGlobalKey[128]; | 
 | 418 |     static bool sHmacGlobalKeyIsValid; | 
 | 419 |  | 
| Brian Stack | 4baa5be | 2018-09-18 14:03:13 -0700 | [diff] [blame] | 420 |     SensorServiceUtil::SensorList mSensors; | 
| Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 421 |     status_t mInitCheck; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 422 |  | 
| Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 423 |     // Socket buffersize used to initialize BitTube. This size depends on whether batching is | 
 | 424 |     // supported or not. | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 425 |     uint32_t mSocketBufferSize; | 
 | 426 |     sp<Looper> mLooper; | 
 | 427 |     sp<SensorEventAckReceiver> mAckReceiver; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 428 |  | 
 | 429 |     // protected by mLock | 
 | 430 |     mutable Mutex mLock; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 431 |     DefaultKeyedVector<int, SensorRecord*> mActiveSensors; | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 432 |     std::unordered_set<int> mActiveVirtualSensors; | 
| Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 433 |     SensorConnectionHolder mConnectionHolder; | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 434 |     bool mWakeLockAcquired; | 
| Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 435 |     sensors_event_t *mSensorEventBuffer, *mSensorEventScratch; | 
| Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 436 |     // WARNING: these SensorEventConnection instances must not be promoted to sp, except via | 
 | 437 |     // modification to add support for them in ConnectionSafeAutolock | 
| Peng Xu | ded526e | 2016-08-12 16:39:44 -0700 | [diff] [blame] | 438 |     wp<const SensorEventConnection> * mMapFlushEventsToConnections; | 
| Brian Stack | 4baa5be | 2018-09-18 14:03:13 -0700 | [diff] [blame] | 439 |     std::unordered_map<int, SensorServiceUtil::RecentEventLogger*> mRecentEvent; | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 440 |     Mode mCurrentOperatingMode; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 441 |  | 
| Aravind Akella | 5c53805 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 442 |     // This packagaName is set when SensorService is in RESTRICTED or DATA_INJECTION mode. Only | 
 | 443 |     // applications with this packageName are allowed to activate/deactivate or call flush on | 
 | 444 |     // sensors. To run CTS this is can be set to ".cts." and only CTS tests will get access to | 
 | 445 |     // sensors. | 
 | 446 |     String8 mWhiteListedPackage; | 
| Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 447 |  | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 448 |     int mNextSensorRegIndex; | 
 | 449 |     Vector<SensorRegistrationInfo> mLastNSensorRegistrations; | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 450 |  | 
 | 451 |     sp<UidPolicy> mUidPolicy; | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 452 |     sp<SensorPrivacyPolicy> mSensorPrivacyPolicy; | 
| Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 453 |  | 
 | 454 |     static AppOpsManager sAppOpsManager; | 
 | 455 |     static std::map<String16, int> sPackageTargetVersion; | 
 | 456 |     static Mutex sPackageTargetVersionLock; | 
| Anthony Stange | 07eb421 | 2020-08-28 14:50:28 -0400 | [diff] [blame] | 457 |     static String16 sSensorInterfaceDescriptorPrefix; | 
| Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 458 |  | 
 | 459 |     // Map from user to SensorPrivacyPolicy | 
 | 460 |     std::map<userid_t, sp<SensorPrivacyPolicy>> mMicSensorPrivacyPolicies; | 
 | 461 |     // Checks if the mic sensor privacy is enabled for the uid | 
 | 462 |     bool isMicSensorPrivacyEnabledForUid(uid_t uid); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 463 | }; | 
 | 464 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 465 | } // namespace android | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 466 | #endif // ANDROID_SENSOR_SERVICE_H |