Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -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 | |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 17 | #define LOG_TAG "Sensors" |
| 18 | |
Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 19 | #include <sensor/SensorManager.h> |
| 20 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
| 23 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 24 | #include <cutils/native_handle.h> |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 25 | #include <utils/Errors.h> |
| 26 | #include <utils/RefBase.h> |
| 27 | #include <utils/Singleton.h> |
| 28 | |
Vladimir Komsiyski | f1d48af | 2023-10-06 10:11:44 +0200 | [diff] [blame] | 29 | #include <android/companion/virtualnative/IVirtualDeviceManagerNative.h> |
| 30 | |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 31 | #include <binder/IBinder.h> |
Jiyong Park | 47f876b | 2018-04-17 13:56:46 +0900 | [diff] [blame] | 32 | #include <binder/IPermissionController.h> |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 33 | #include <binder/IServiceManager.h> |
| 34 | |
Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 35 | #include <sensor/ISensorServer.h> |
| 36 | #include <sensor/ISensorEventConnection.h> |
| 37 | #include <sensor/Sensor.h> |
| 38 | #include <sensor/SensorEventQueue.h> |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 39 | |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | namespace android { |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | |
Vladimir Komsiyski | f1d48af | 2023-10-06 10:11:44 +0200 | [diff] [blame] | 44 | namespace { |
| 45 | |
| 46 | using ::android::companion::virtualnative::IVirtualDeviceManagerNative; |
| 47 | |
| 48 | static constexpr int DEVICE_ID_DEFAULT = 0; |
| 49 | |
| 50 | // Returns the deviceId of the device where this uid is observed. If the uid is present on more than |
| 51 | // one devices, return the default deviceId. |
| 52 | int getDeviceIdForUid(uid_t uid) { |
| 53 | sp<IBinder> binder = |
| 54 | defaultServiceManager()->checkService(String16("virtualdevice_native")); |
| 55 | if (binder != nullptr) { |
| 56 | auto vdm = interface_cast<IVirtualDeviceManagerNative>(binder); |
| 57 | std::vector<int> deviceIds; |
| 58 | vdm->getDeviceIdsForUid(uid, &deviceIds); |
| 59 | // If the UID is associated with multiple virtual devices, use the default device's |
| 60 | // sensors as we cannot disambiguate here. This effectively means that the app has |
| 61 | // activities on different devices at the same time, so it must handle the device |
| 62 | // awareness by itself. |
| 63 | if (deviceIds.size() == 1) { |
| 64 | const int deviceId = deviceIds.at(0); |
| 65 | int devicePolicy = IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT; |
| 66 | vdm->getDevicePolicy(deviceId, |
| 67 | IVirtualDeviceManagerNative::POLICY_TYPE_SENSORS, |
| 68 | &devicePolicy); |
| 69 | if (devicePolicy == IVirtualDeviceManagerNative::DEVICE_POLICY_CUSTOM) { |
| 70 | return deviceId; |
| 71 | } |
| 72 | } |
| 73 | } else { |
| 74 | ALOGW("Cannot get virtualdevice_native service"); |
| 75 | } |
| 76 | return DEVICE_ID_DEFAULT; |
| 77 | } |
| 78 | |
| 79 | } // namespace |
| 80 | |
Peng Xu | fe5476a | 2017-03-17 17:27:42 -0700 | [diff] [blame] | 81 | Mutex SensorManager::sLock; |
| 82 | std::map<String16, SensorManager*> SensorManager::sPackageInstances; |
Aravind Akella | e2806cb | 2015-07-29 18:03:48 -0700 | [diff] [blame] | 83 | |
| 84 | SensorManager& SensorManager::getInstanceForPackage(const String16& packageName) { |
Peng Xu | fe5476a | 2017-03-17 17:27:42 -0700 | [diff] [blame] | 85 | waitForSensorService(nullptr); |
| 86 | |
Aravind Akella | e2806cb | 2015-07-29 18:03:48 -0700 | [diff] [blame] | 87 | Mutex::Autolock _l(sLock); |
| 88 | SensorManager* sensorManager; |
Peng Xu | fe5476a | 2017-03-17 17:27:42 -0700 | [diff] [blame] | 89 | auto iterator = sPackageInstances.find(packageName); |
Aravind Akella | e2806cb | 2015-07-29 18:03:48 -0700 | [diff] [blame] | 90 | |
| 91 | if (iterator != sPackageInstances.end()) { |
| 92 | sensorManager = iterator->second; |
| 93 | } else { |
| 94 | String16 opPackageName = packageName; |
Vladimir Komsiyski | f1d48af | 2023-10-06 10:11:44 +0200 | [diff] [blame] | 95 | const uid_t uid = IPCThreadState::self()->getCallingUid(); |
Aravind Akella | e2806cb | 2015-07-29 18:03:48 -0700 | [diff] [blame] | 96 | |
| 97 | // It is possible that the calling code has no access to the package name. |
| 98 | // In this case we will get the packages for the calling UID and pick the |
| 99 | // first one for attributing the app op. This will work correctly for |
| 100 | // runtime permissions as for legacy apps we will toggle the app op for |
| 101 | // all packages in the UID. The caveat is that the operation may be attributed |
| 102 | // to the wrong package and stats based on app ops may be slightly off. |
| 103 | if (opPackageName.size() <= 0) { |
| 104 | sp<IBinder> binder = defaultServiceManager()->getService(String16("permission")); |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 105 | if (binder != nullptr) { |
Aravind Akella | e2806cb | 2015-07-29 18:03:48 -0700 | [diff] [blame] | 106 | Vector<String16> packages; |
| 107 | interface_cast<IPermissionController>(binder)->getPackagesForUid(uid, packages); |
| 108 | if (!packages.isEmpty()) { |
| 109 | opPackageName = packages[0]; |
| 110 | } else { |
| 111 | ALOGE("No packages for calling UID"); |
| 112 | } |
| 113 | } else { |
| 114 | ALOGE("Cannot get permission service"); |
| 115 | } |
| 116 | } |
| 117 | |
Vladimir Komsiyski | f1d48af | 2023-10-06 10:11:44 +0200 | [diff] [blame] | 118 | // Check if the calling UID is observed on a virtual device. If so, provide that device's |
| 119 | // sensors by default instead of the default device's sensors. |
| 120 | const int deviceId = getDeviceIdForUid(uid); |
| 121 | sensorManager = new SensorManager(opPackageName, deviceId); |
Aravind Akella | e2806cb | 2015-07-29 18:03:48 -0700 | [diff] [blame] | 122 | |
| 123 | // If we had no package name, we looked it up from the UID and the sensor |
| 124 | // manager instance we created should also be mapped to the empty package |
| 125 | // name, to avoid looking up the packages for a UID and get the same result. |
| 126 | if (packageName.size() <= 0) { |
| 127 | sPackageInstances.insert(std::make_pair(String16(), sensorManager)); |
| 128 | } |
| 129 | |
| 130 | // Stash the per package sensor manager. |
| 131 | sPackageInstances.insert(std::make_pair(opPackageName, sensorManager)); |
| 132 | } |
| 133 | |
| 134 | return *sensorManager; |
| 135 | } |
| 136 | |
Anthony Stange | 9352ab1 | 2023-02-21 17:57:38 +0000 | [diff] [blame] | 137 | void SensorManager::removeInstanceForPackage(const String16& packageName) { |
| 138 | Mutex::Autolock _l(sLock); |
| 139 | auto iterator = sPackageInstances.find(packageName); |
| 140 | if (iterator != sPackageInstances.end()) { |
| 141 | SensorManager* sensorManager = iterator->second; |
| 142 | delete sensorManager; |
| 143 | sPackageInstances.erase(iterator); |
| 144 | } |
| 145 | } |
| 146 | |
Vladimir Komsiyski | f1d48af | 2023-10-06 10:11:44 +0200 | [diff] [blame] | 147 | SensorManager::SensorManager(const String16& opPackageName, int deviceId) |
| 148 | : mSensorList(nullptr), mOpPackageName(opPackageName), mDeviceId(deviceId), |
| 149 | mDirectConnectionHandle(1) { |
Brian Duddie | 8bbd6f4 | 2019-06-06 16:43:41 -0700 | [diff] [blame] | 150 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 151 | assertStateLocked(); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 154 | SensorManager::~SensorManager() { |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 155 | free(mSensorList); |
Erik Staats | d35a574 | 2022-02-04 06:37:58 -0800 | [diff] [blame] | 156 | free(mDynamicSensorList); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Peng Xu | fe5476a | 2017-03-17 17:27:42 -0700 | [diff] [blame] | 159 | status_t SensorManager::waitForSensorService(sp<ISensorServer> *server) { |
| 160 | // try for 300 seconds (60*5(getService() tries for 5 seconds)) before giving up ... |
| 161 | sp<ISensorServer> s; |
| 162 | const String16 name("sensorservice"); |
| 163 | for (int i = 0; i < 60; i++) { |
| 164 | status_t err = getService(name, &s); |
| 165 | switch (err) { |
| 166 | case NAME_NOT_FOUND: |
| 167 | sleep(1); |
| 168 | continue; |
| 169 | case NO_ERROR: |
| 170 | if (server != nullptr) { |
| 171 | *server = s; |
| 172 | } |
| 173 | return NO_ERROR; |
| 174 | default: |
| 175 | return err; |
| 176 | } |
| 177 | } |
| 178 | return TIMED_OUT; |
| 179 | } |
| 180 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 181 | void SensorManager::sensorManagerDied() { |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 182 | Mutex::Autolock _l(mLock); |
| 183 | mSensorServer.clear(); |
| 184 | free(mSensorList); |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 185 | mSensorList = nullptr; |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 186 | mSensors.clear(); |
Erik Staats | d35a574 | 2022-02-04 06:37:58 -0800 | [diff] [blame] | 187 | free(mDynamicSensorList); |
| 188 | mDynamicSensorList = nullptr; |
| 189 | mDynamicSensors.clear(); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 192 | status_t SensorManager::assertStateLocked() { |
Aravind Akella | 8f35ca9 | 2015-08-17 15:22:12 -0700 | [diff] [blame] | 193 | bool initSensorManager = false; |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 194 | if (mSensorServer == nullptr) { |
Aravind Akella | 8f35ca9 | 2015-08-17 15:22:12 -0700 | [diff] [blame] | 195 | initSensorManager = true; |
| 196 | } else { |
| 197 | // Ping binder to check if sensorservice is alive. |
| 198 | status_t err = IInterface::asBinder(mSensorServer)->pingBinder(); |
| 199 | if (err != NO_ERROR) { |
| 200 | initSensorManager = true; |
| 201 | } |
| 202 | } |
| 203 | if (initSensorManager) { |
Peng Xu | fe5476a | 2017-03-17 17:27:42 -0700 | [diff] [blame] | 204 | waitForSensorService(&mSensorServer); |
| 205 | LOG_ALWAYS_FATAL_IF(mSensorServer == nullptr, "getService(SensorService) NULL"); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 206 | |
| 207 | class DeathObserver : public IBinder::DeathRecipient { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 208 | SensorManager& mSensorManager; |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 209 | virtual void binderDied(const wp<IBinder>& who) { |
Yi Kong | 82d7c63 | 2019-09-20 12:10:47 -0700 | [diff] [blame] | 210 | ALOGW("sensorservice died [%p]", static_cast<void*>(who.unsafe_get())); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 211 | mSensorManager.sensorManagerDied(); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 212 | } |
| 213 | public: |
Chih-Hung Hsieh | 68a593e | 2016-04-28 09:14:32 -0700 | [diff] [blame] | 214 | explicit DeathObserver(SensorManager& mgr) : mSensorManager(mgr) { } |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this)); |
Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 218 | IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 219 | |
Vladimir Komsiyski | f1d48af | 2023-10-06 10:11:44 +0200 | [diff] [blame] | 220 | if (mDeviceId == DEVICE_ID_DEFAULT) { |
| 221 | mSensors = mSensorServer->getSensorList(mOpPackageName); |
| 222 | } else { |
| 223 | mSensors = mSensorServer->getRuntimeSensorList(mOpPackageName, mDeviceId); |
| 224 | } |
| 225 | |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 226 | size_t count = mSensors.size(); |
Devin Moore | 49600b1 | 2023-04-25 00:17:13 +0000 | [diff] [blame] | 227 | // If count is 0, mSensorList will be non-null. This is old |
| 228 | // existing behavior and callers expect this. |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 229 | mSensorList = |
| 230 | static_cast<Sensor const**>(malloc(count * sizeof(Sensor*))); |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 231 | LOG_ALWAYS_FATAL_IF(mSensorList == nullptr, "mSensorList NULL"); |
Aravind Akella | 8f35ca9 | 2015-08-17 15:22:12 -0700 | [diff] [blame] | 232 | |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 233 | for (size_t i=0 ; i<count ; i++) { |
| 234 | mSensorList[i] = mSensors.array() + i; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return NO_ERROR; |
| 239 | } |
| 240 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 241 | ssize_t SensorManager::getSensorList(Sensor const* const** list) { |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 242 | Mutex::Autolock _l(mLock); |
| 243 | status_t err = assertStateLocked(); |
| 244 | if (err < 0) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 245 | return static_cast<ssize_t>(err); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 246 | } |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 247 | *list = mSensorList; |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 248 | return static_cast<ssize_t>(mSensors.size()); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Vladimir Komsiyski | 157e4e0 | 2023-12-07 18:52:18 +0100 | [diff] [blame^] | 251 | ssize_t SensorManager::getDefaultDeviceSensorList(Vector<Sensor> & list) { |
| 252 | Mutex::Autolock _l(mLock); |
| 253 | status_t err = assertStateLocked(); |
| 254 | if (err < 0) { |
| 255 | return static_cast<ssize_t>(err); |
| 256 | } |
| 257 | |
| 258 | if (mDeviceId == DEVICE_ID_DEFAULT) { |
| 259 | list = mSensors; |
| 260 | } else { |
| 261 | list = mSensorServer->getSensorList(mOpPackageName); |
| 262 | } |
| 263 | |
| 264 | return static_cast<ssize_t>(list.size()); |
| 265 | } |
| 266 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 267 | ssize_t SensorManager::getDynamicSensorList(Vector<Sensor> & dynamicSensors) { |
| 268 | Mutex::Autolock _l(mLock); |
| 269 | status_t err = assertStateLocked(); |
| 270 | if (err < 0) { |
| 271 | return static_cast<ssize_t>(err); |
| 272 | } |
| 273 | |
| 274 | dynamicSensors = mSensorServer->getDynamicSensorList(mOpPackageName); |
| 275 | size_t count = dynamicSensors.size(); |
| 276 | |
| 277 | return static_cast<ssize_t>(count); |
| 278 | } |
| 279 | |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame] | 280 | ssize_t SensorManager::getRuntimeSensorList(int deviceId, Vector<Sensor>& runtimeSensors) { |
| 281 | Mutex::Autolock _l(mLock); |
| 282 | status_t err = assertStateLocked(); |
| 283 | if (err < 0) { |
| 284 | return static_cast<ssize_t>(err); |
| 285 | } |
| 286 | |
| 287 | runtimeSensors = mSensorServer->getRuntimeSensorList(mOpPackageName, deviceId); |
| 288 | size_t count = runtimeSensors.size(); |
| 289 | |
| 290 | return static_cast<ssize_t>(count); |
| 291 | } |
| 292 | |
Erik Staats | d35a574 | 2022-02-04 06:37:58 -0800 | [diff] [blame] | 293 | ssize_t SensorManager::getDynamicSensorList(Sensor const* const** list) { |
| 294 | Mutex::Autolock _l(mLock); |
| 295 | status_t err = assertStateLocked(); |
| 296 | if (err < 0) { |
| 297 | return static_cast<ssize_t>(err); |
| 298 | } |
| 299 | |
| 300 | free(mDynamicSensorList); |
| 301 | mDynamicSensorList = nullptr; |
| 302 | mDynamicSensors = mSensorServer->getDynamicSensorList(mOpPackageName); |
| 303 | size_t dynamicCount = mDynamicSensors.size(); |
| 304 | if (dynamicCount > 0) { |
| 305 | mDynamicSensorList = static_cast<Sensor const**>( |
| 306 | malloc(dynamicCount * sizeof(Sensor*))); |
| 307 | if (mDynamicSensorList == nullptr) { |
| 308 | ALOGE("Failed to allocate dynamic sensor list for %zu sensors.", |
| 309 | dynamicCount); |
| 310 | return static_cast<ssize_t>(NO_MEMORY); |
| 311 | } |
| 312 | |
| 313 | for (size_t i = 0; i < dynamicCount; i++) { |
| 314 | mDynamicSensorList[i] = mDynamicSensors.array() + i; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | *list = mDynamicSensorList; |
| 319 | return static_cast<ssize_t>(mDynamicSensors.size()); |
| 320 | } |
| 321 | |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 322 | Sensor const* SensorManager::getDefaultSensor(int type) |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 323 | { |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 324 | Mutex::Autolock _l(mLock); |
| 325 | if (assertStateLocked() == NO_ERROR) { |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 326 | bool wakeUpSensor = false; |
| 327 | // For the following sensor types, return a wake-up sensor. These types are by default |
| 328 | // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return |
| 329 | // a non_wake-up version. |
| 330 | if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION || |
| 331 | type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE || |
Nick Vaccaro | 5e7f79b | 2016-10-17 15:40:51 -0700 | [diff] [blame] | 332 | type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE || |
Nick Vaccaro | 2e990eb | 2017-01-12 21:13:58 -0800 | [diff] [blame] | 333 | type == SENSOR_TYPE_WRIST_TILT_GESTURE || |
Anthony Stange | fdb1fc8 | 2020-01-16 15:02:48 -0500 | [diff] [blame] | 334 | type == SENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT || type == SENSOR_TYPE_HINGE_ANGLE) { |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 335 | wakeUpSensor = true; |
| 336 | } |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 337 | // For now we just return the first sensor of that type we find. |
| 338 | // in the future it will make sense to let the SensorService make |
| 339 | // that decision. |
| 340 | for (size_t i=0 ; i<mSensors.size() ; i++) { |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 341 | if (mSensorList[i]->getType() == type && |
| 342 | mSensorList[i]->isWakeUpSensor() == wakeUpSensor) { |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 343 | return mSensorList[i]; |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 344 | } |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 345 | } |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 346 | } |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 347 | return nullptr; |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Arthur Ishiguro | 340882c | 2021-02-18 15:17:44 -0800 | [diff] [blame] | 350 | sp<SensorEventQueue> SensorManager::createEventQueue( |
| 351 | String8 packageName, int mode, String16 attributionTag) { |
Mathias Agopian | be58de0 | 2011-10-16 00:38:30 -0700 | [diff] [blame] | 352 | sp<SensorEventQueue> queue; |
| 353 | |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 354 | Mutex::Autolock _l(mLock); |
| 355 | while (assertStateLocked() == NO_ERROR) { |
Arthur Ishiguro | 340882c | 2021-02-18 15:17:44 -0800 | [diff] [blame] | 356 | sp<ISensorEventConnection> connection = mSensorServer->createSensorEventConnection( |
| 357 | packageName, mode, mOpPackageName, attributionTag); |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 358 | if (connection == nullptr) { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 359 | // SensorService just died or the app doesn't have required permissions. |
| 360 | ALOGE("createEventQueue: connection is NULL."); |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 361 | return nullptr; |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 362 | } |
| 363 | queue = new SensorEventQueue(connection); |
| 364 | break; |
Mathias Agopian | be58de0 | 2011-10-16 00:38:30 -0700 | [diff] [blame] | 365 | } |
Mathias Agopian | be58de0 | 2011-10-16 00:38:30 -0700 | [diff] [blame] | 366 | return queue; |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 369 | bool SensorManager::isDataInjectionEnabled() { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 370 | Mutex::Autolock _l(mLock); |
| 371 | if (assertStateLocked() == NO_ERROR) { |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 372 | return mSensorServer->isDataInjectionEnabled(); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 373 | } |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 374 | return false; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Mark Wheatley | 8f285d9 | 2023-07-07 20:07:18 +0000 | [diff] [blame] | 377 | bool SensorManager::isReplayDataInjectionEnabled() { |
| 378 | Mutex::Autolock _l(mLock); |
| 379 | if (assertStateLocked() == NO_ERROR) { |
| 380 | return mSensorServer->isReplayDataInjectionEnabled(); |
| 381 | } |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | bool SensorManager::isHalBypassReplayDataInjectionEnabled() { |
| 386 | Mutex::Autolock _l(mLock); |
| 387 | if (assertStateLocked() == NO_ERROR) { |
| 388 | return mSensorServer->isHalBypassReplayDataInjectionEnabled(); |
| 389 | } |
| 390 | return false; |
| 391 | } |
| 392 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 393 | int SensorManager::createDirectChannel( |
| 394 | size_t size, int channelType, const native_handle_t *resourceHandle) { |
Vladimir Komsiyski | 4871f09 | 2023-01-19 18:25:43 +0100 | [diff] [blame] | 395 | static constexpr int DEFAULT_DEVICE_ID = 0; |
| 396 | return createDirectChannel(DEFAULT_DEVICE_ID, size, channelType, resourceHandle); |
| 397 | } |
| 398 | |
| 399 | int SensorManager::createDirectChannel( |
| 400 | int deviceId, size_t size, int channelType, const native_handle_t *resourceHandle) { |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 401 | Mutex::Autolock _l(mLock); |
| 402 | if (assertStateLocked() != NO_ERROR) { |
| 403 | return NO_INIT; |
| 404 | } |
| 405 | |
Peng Xu | d9c8a86 | 2017-03-05 01:48:11 -0800 | [diff] [blame] | 406 | if (channelType != SENSOR_DIRECT_MEM_TYPE_ASHMEM |
| 407 | && channelType != SENSOR_DIRECT_MEM_TYPE_GRALLOC) { |
| 408 | ALOGE("Bad channel shared memory type %d", channelType); |
| 409 | return BAD_VALUE; |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 410 | } |
Peng Xu | d9c8a86 | 2017-03-05 01:48:11 -0800 | [diff] [blame] | 411 | |
| 412 | sp<ISensorEventConnection> conn = |
Vladimir Komsiyski | 4871f09 | 2023-01-19 18:25:43 +0100 | [diff] [blame] | 413 | mSensorServer->createSensorDirectConnection(mOpPackageName, deviceId, |
Peng Xu | d9c8a86 | 2017-03-05 01:48:11 -0800 | [diff] [blame] | 414 | static_cast<uint32_t>(size), |
| 415 | static_cast<int32_t>(channelType), |
| 416 | SENSOR_DIRECT_FMT_SENSORS_EVENT, resourceHandle); |
| 417 | if (conn == nullptr) { |
| 418 | return NO_MEMORY; |
| 419 | } |
| 420 | |
| 421 | int nativeHandle = mDirectConnectionHandle++; |
| 422 | mDirectConnection.emplace(nativeHandle, conn); |
| 423 | return nativeHandle; |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void SensorManager::destroyDirectChannel(int channelNativeHandle) { |
| 427 | Mutex::Autolock _l(mLock); |
| 428 | if (assertStateLocked() == NO_ERROR) { |
| 429 | mDirectConnection.erase(channelNativeHandle); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | int SensorManager::configureDirectChannel(int channelNativeHandle, int sensorHandle, int rateLevel) { |
| 434 | Mutex::Autolock _l(mLock); |
| 435 | if (assertStateLocked() != NO_ERROR) { |
| 436 | return NO_INIT; |
| 437 | } |
| 438 | |
| 439 | auto i = mDirectConnection.find(channelNativeHandle); |
| 440 | if (i == mDirectConnection.end()) { |
| 441 | ALOGE("Cannot find the handle in client direct connection table"); |
| 442 | return BAD_VALUE; |
| 443 | } |
| 444 | |
| 445 | int ret; |
| 446 | ret = i->second->configureChannel(sensorHandle, rateLevel); |
| 447 | ALOGE_IF(ret < 0, "SensorManager::configureChannel (%d, %d) returns %d", |
| 448 | static_cast<int>(sensorHandle), static_cast<int>(rateLevel), |
| 449 | static_cast<int>(ret)); |
| 450 | return ret; |
| 451 | } |
| 452 | |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 453 | int SensorManager::setOperationParameter( |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 454 | int handle, int type, |
| 455 | const Vector<float> &floats, const Vector<int32_t> &ints) { |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 456 | Mutex::Autolock _l(mLock); |
| 457 | if (assertStateLocked() != NO_ERROR) { |
| 458 | return NO_INIT; |
| 459 | } |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 460 | return mSensorServer->setOperationParameter(handle, type, floats, ints); |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 463 | // ---------------------------------------------------------------------------- |
| 464 | }; // namespace android |