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