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