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 | |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 29 | #include <binder/IBinder.h> |
Jiyong Park | 47f876b | 2018-04-17 13:56:46 +0900 | [diff] [blame] | 30 | #include <binder/IPermissionController.h> |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 31 | #include <binder/IServiceManager.h> |
| 32 | |
Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 33 | #include <sensor/ISensorServer.h> |
| 34 | #include <sensor/ISensorEventConnection.h> |
| 35 | #include <sensor/Sensor.h> |
| 36 | #include <sensor/SensorEventQueue.h> |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 37 | |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | namespace android { |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | |
Peng Xu | fe5476a | 2017-03-17 17:27:42 -0700 | [diff] [blame] | 42 | Mutex SensorManager::sLock; |
| 43 | std::map<String16, SensorManager*> SensorManager::sPackageInstances; |
Aravind Akella | e2806cb | 2015-07-29 18:03:48 -0700 | [diff] [blame] | 44 | |
| 45 | SensorManager& SensorManager::getInstanceForPackage(const String16& packageName) { |
Peng Xu | fe5476a | 2017-03-17 17:27:42 -0700 | [diff] [blame] | 46 | waitForSensorService(nullptr); |
| 47 | |
Aravind Akella | e2806cb | 2015-07-29 18:03:48 -0700 | [diff] [blame] | 48 | Mutex::Autolock _l(sLock); |
| 49 | SensorManager* sensorManager; |
Peng Xu | fe5476a | 2017-03-17 17:27:42 -0700 | [diff] [blame] | 50 | auto iterator = sPackageInstances.find(packageName); |
Aravind Akella | e2806cb | 2015-07-29 18:03:48 -0700 | [diff] [blame] | 51 | |
| 52 | if (iterator != sPackageInstances.end()) { |
| 53 | sensorManager = iterator->second; |
| 54 | } else { |
| 55 | String16 opPackageName = packageName; |
| 56 | |
| 57 | // It is possible that the calling code has no access to the package name. |
| 58 | // In this case we will get the packages for the calling UID and pick the |
| 59 | // first one for attributing the app op. This will work correctly for |
| 60 | // runtime permissions as for legacy apps we will toggle the app op for |
| 61 | // all packages in the UID. The caveat is that the operation may be attributed |
| 62 | // to the wrong package and stats based on app ops may be slightly off. |
| 63 | if (opPackageName.size() <= 0) { |
| 64 | sp<IBinder> binder = defaultServiceManager()->getService(String16("permission")); |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 65 | if (binder != nullptr) { |
Aravind Akella | e2806cb | 2015-07-29 18:03:48 -0700 | [diff] [blame] | 66 | const uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 67 | Vector<String16> packages; |
| 68 | interface_cast<IPermissionController>(binder)->getPackagesForUid(uid, packages); |
| 69 | if (!packages.isEmpty()) { |
| 70 | opPackageName = packages[0]; |
| 71 | } else { |
| 72 | ALOGE("No packages for calling UID"); |
| 73 | } |
| 74 | } else { |
| 75 | ALOGE("Cannot get permission service"); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | sensorManager = new SensorManager(opPackageName); |
| 80 | |
| 81 | // If we had no package name, we looked it up from the UID and the sensor |
| 82 | // manager instance we created should also be mapped to the empty package |
| 83 | // name, to avoid looking up the packages for a UID and get the same result. |
| 84 | if (packageName.size() <= 0) { |
| 85 | sPackageInstances.insert(std::make_pair(String16(), sensorManager)); |
| 86 | } |
| 87 | |
| 88 | // Stash the per package sensor manager. |
| 89 | sPackageInstances.insert(std::make_pair(opPackageName, sensorManager)); |
| 90 | } |
| 91 | |
| 92 | return *sensorManager; |
| 93 | } |
| 94 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 95 | SensorManager::SensorManager(const String16& opPackageName) |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 96 | : mSensorList(nullptr), mOpPackageName(opPackageName), mDirectConnectionHandle(1) { |
Brian Duddie | 8bbd6f4 | 2019-06-06 16:43:41 -0700 | [diff] [blame] | 97 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 98 | assertStateLocked(); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 101 | SensorManager::~SensorManager() { |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 102 | free(mSensorList); |
Erik Staats | d35a574 | 2022-02-04 06:37:58 -0800 | [diff] [blame^] | 103 | free(mDynamicSensorList); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Peng Xu | fe5476a | 2017-03-17 17:27:42 -0700 | [diff] [blame] | 106 | status_t SensorManager::waitForSensorService(sp<ISensorServer> *server) { |
| 107 | // try for 300 seconds (60*5(getService() tries for 5 seconds)) before giving up ... |
| 108 | sp<ISensorServer> s; |
| 109 | const String16 name("sensorservice"); |
| 110 | for (int i = 0; i < 60; i++) { |
| 111 | status_t err = getService(name, &s); |
| 112 | switch (err) { |
| 113 | case NAME_NOT_FOUND: |
| 114 | sleep(1); |
| 115 | continue; |
| 116 | case NO_ERROR: |
| 117 | if (server != nullptr) { |
| 118 | *server = s; |
| 119 | } |
| 120 | return NO_ERROR; |
| 121 | default: |
| 122 | return err; |
| 123 | } |
| 124 | } |
| 125 | return TIMED_OUT; |
| 126 | } |
| 127 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 128 | void SensorManager::sensorManagerDied() { |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 129 | Mutex::Autolock _l(mLock); |
| 130 | mSensorServer.clear(); |
| 131 | free(mSensorList); |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 132 | mSensorList = nullptr; |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 133 | mSensors.clear(); |
Erik Staats | d35a574 | 2022-02-04 06:37:58 -0800 | [diff] [blame^] | 134 | free(mDynamicSensorList); |
| 135 | mDynamicSensorList = nullptr; |
| 136 | mDynamicSensors.clear(); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 139 | status_t SensorManager::assertStateLocked() { |
Aravind Akella | 8f35ca9 | 2015-08-17 15:22:12 -0700 | [diff] [blame] | 140 | bool initSensorManager = false; |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 141 | if (mSensorServer == nullptr) { |
Aravind Akella | 8f35ca9 | 2015-08-17 15:22:12 -0700 | [diff] [blame] | 142 | initSensorManager = true; |
| 143 | } else { |
| 144 | // Ping binder to check if sensorservice is alive. |
| 145 | status_t err = IInterface::asBinder(mSensorServer)->pingBinder(); |
| 146 | if (err != NO_ERROR) { |
| 147 | initSensorManager = true; |
| 148 | } |
| 149 | } |
| 150 | if (initSensorManager) { |
Peng Xu | fe5476a | 2017-03-17 17:27:42 -0700 | [diff] [blame] | 151 | waitForSensorService(&mSensorServer); |
| 152 | LOG_ALWAYS_FATAL_IF(mSensorServer == nullptr, "getService(SensorService) NULL"); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 153 | |
| 154 | class DeathObserver : public IBinder::DeathRecipient { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 155 | SensorManager& mSensorManager; |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 156 | virtual void binderDied(const wp<IBinder>& who) { |
Yi Kong | 82d7c63 | 2019-09-20 12:10:47 -0700 | [diff] [blame] | 157 | ALOGW("sensorservice died [%p]", static_cast<void*>(who.unsafe_get())); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 158 | mSensorManager.sensorManagerDied(); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 159 | } |
| 160 | public: |
Chih-Hung Hsieh | 68a593e | 2016-04-28 09:14:32 -0700 | [diff] [blame] | 161 | explicit DeathObserver(SensorManager& mgr) : mSensorManager(mgr) { } |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this)); |
Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 165 | IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 166 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 167 | mSensors = mSensorServer->getSensorList(mOpPackageName); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 168 | size_t count = mSensors.size(); |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 169 | mSensorList = |
| 170 | static_cast<Sensor const**>(malloc(count * sizeof(Sensor*))); |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 171 | LOG_ALWAYS_FATAL_IF(mSensorList == nullptr, "mSensorList NULL"); |
Aravind Akella | 8f35ca9 | 2015-08-17 15:22:12 -0700 | [diff] [blame] | 172 | |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 173 | for (size_t i=0 ; i<count ; i++) { |
| 174 | mSensorList[i] = mSensors.array() + i; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return NO_ERROR; |
| 179 | } |
| 180 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 181 | ssize_t SensorManager::getSensorList(Sensor const* const** list) { |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 182 | Mutex::Autolock _l(mLock); |
| 183 | status_t err = assertStateLocked(); |
| 184 | if (err < 0) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 185 | return static_cast<ssize_t>(err); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 186 | } |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 187 | *list = mSensorList; |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 188 | return static_cast<ssize_t>(mSensors.size()); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 191 | ssize_t SensorManager::getDynamicSensorList(Vector<Sensor> & dynamicSensors) { |
| 192 | Mutex::Autolock _l(mLock); |
| 193 | status_t err = assertStateLocked(); |
| 194 | if (err < 0) { |
| 195 | return static_cast<ssize_t>(err); |
| 196 | } |
| 197 | |
| 198 | dynamicSensors = mSensorServer->getDynamicSensorList(mOpPackageName); |
| 199 | size_t count = dynamicSensors.size(); |
| 200 | |
| 201 | return static_cast<ssize_t>(count); |
| 202 | } |
| 203 | |
Erik Staats | d35a574 | 2022-02-04 06:37:58 -0800 | [diff] [blame^] | 204 | ssize_t SensorManager::getDynamicSensorList(Sensor const* const** list) { |
| 205 | Mutex::Autolock _l(mLock); |
| 206 | status_t err = assertStateLocked(); |
| 207 | if (err < 0) { |
| 208 | return static_cast<ssize_t>(err); |
| 209 | } |
| 210 | |
| 211 | free(mDynamicSensorList); |
| 212 | mDynamicSensorList = nullptr; |
| 213 | mDynamicSensors = mSensorServer->getDynamicSensorList(mOpPackageName); |
| 214 | size_t dynamicCount = mDynamicSensors.size(); |
| 215 | if (dynamicCount > 0) { |
| 216 | mDynamicSensorList = static_cast<Sensor const**>( |
| 217 | malloc(dynamicCount * sizeof(Sensor*))); |
| 218 | if (mDynamicSensorList == nullptr) { |
| 219 | ALOGE("Failed to allocate dynamic sensor list for %zu sensors.", |
| 220 | dynamicCount); |
| 221 | return static_cast<ssize_t>(NO_MEMORY); |
| 222 | } |
| 223 | |
| 224 | for (size_t i = 0; i < dynamicCount; i++) { |
| 225 | mDynamicSensorList[i] = mDynamicSensors.array() + i; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | *list = mDynamicSensorList; |
| 230 | return static_cast<ssize_t>(mDynamicSensors.size()); |
| 231 | } |
| 232 | |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 233 | Sensor const* SensorManager::getDefaultSensor(int type) |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 234 | { |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 235 | Mutex::Autolock _l(mLock); |
| 236 | if (assertStateLocked() == NO_ERROR) { |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 237 | bool wakeUpSensor = false; |
| 238 | // For the following sensor types, return a wake-up sensor. These types are by default |
| 239 | // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return |
| 240 | // a non_wake-up version. |
| 241 | if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION || |
| 242 | type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE || |
Nick Vaccaro | 5e7f79b | 2016-10-17 15:40:51 -0700 | [diff] [blame] | 243 | type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE || |
Nick Vaccaro | 2e990eb | 2017-01-12 21:13:58 -0800 | [diff] [blame] | 244 | type == SENSOR_TYPE_WRIST_TILT_GESTURE || |
Anthony Stange | fdb1fc8 | 2020-01-16 15:02:48 -0500 | [diff] [blame] | 245 | type == SENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT || type == SENSOR_TYPE_HINGE_ANGLE) { |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 246 | wakeUpSensor = true; |
| 247 | } |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 248 | // For now we just return the first sensor of that type we find. |
| 249 | // in the future it will make sense to let the SensorService make |
| 250 | // that decision. |
| 251 | for (size_t i=0 ; i<mSensors.size() ; i++) { |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 252 | if (mSensorList[i]->getType() == type && |
| 253 | mSensorList[i]->isWakeUpSensor() == wakeUpSensor) { |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 254 | return mSensorList[i]; |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 255 | } |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 256 | } |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 257 | } |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 258 | return nullptr; |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Arthur Ishiguro | 340882c | 2021-02-18 15:17:44 -0800 | [diff] [blame] | 261 | sp<SensorEventQueue> SensorManager::createEventQueue( |
| 262 | String8 packageName, int mode, String16 attributionTag) { |
Mathias Agopian | be58de0 | 2011-10-16 00:38:30 -0700 | [diff] [blame] | 263 | sp<SensorEventQueue> queue; |
| 264 | |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 265 | Mutex::Autolock _l(mLock); |
| 266 | while (assertStateLocked() == NO_ERROR) { |
Arthur Ishiguro | 340882c | 2021-02-18 15:17:44 -0800 | [diff] [blame] | 267 | sp<ISensorEventConnection> connection = mSensorServer->createSensorEventConnection( |
| 268 | packageName, mode, mOpPackageName, attributionTag); |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 269 | if (connection == nullptr) { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 270 | // SensorService just died or the app doesn't have required permissions. |
| 271 | ALOGE("createEventQueue: connection is NULL."); |
Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 272 | return nullptr; |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 273 | } |
| 274 | queue = new SensorEventQueue(connection); |
| 275 | break; |
Mathias Agopian | be58de0 | 2011-10-16 00:38:30 -0700 | [diff] [blame] | 276 | } |
Mathias Agopian | be58de0 | 2011-10-16 00:38:30 -0700 | [diff] [blame] | 277 | return queue; |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 280 | bool SensorManager::isDataInjectionEnabled() { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 281 | Mutex::Autolock _l(mLock); |
| 282 | if (assertStateLocked() == NO_ERROR) { |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 283 | return mSensorServer->isDataInjectionEnabled(); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 284 | } |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 285 | return false; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 288 | int SensorManager::createDirectChannel( |
| 289 | size_t size, int channelType, const native_handle_t *resourceHandle) { |
| 290 | Mutex::Autolock _l(mLock); |
| 291 | if (assertStateLocked() != NO_ERROR) { |
| 292 | return NO_INIT; |
| 293 | } |
| 294 | |
Peng Xu | d9c8a86 | 2017-03-05 01:48:11 -0800 | [diff] [blame] | 295 | if (channelType != SENSOR_DIRECT_MEM_TYPE_ASHMEM |
| 296 | && channelType != SENSOR_DIRECT_MEM_TYPE_GRALLOC) { |
| 297 | ALOGE("Bad channel shared memory type %d", channelType); |
| 298 | return BAD_VALUE; |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 299 | } |
Peng Xu | d9c8a86 | 2017-03-05 01:48:11 -0800 | [diff] [blame] | 300 | |
| 301 | sp<ISensorEventConnection> conn = |
| 302 | mSensorServer->createSensorDirectConnection(mOpPackageName, |
| 303 | static_cast<uint32_t>(size), |
| 304 | static_cast<int32_t>(channelType), |
| 305 | SENSOR_DIRECT_FMT_SENSORS_EVENT, resourceHandle); |
| 306 | if (conn == nullptr) { |
| 307 | return NO_MEMORY; |
| 308 | } |
| 309 | |
| 310 | int nativeHandle = mDirectConnectionHandle++; |
| 311 | mDirectConnection.emplace(nativeHandle, conn); |
| 312 | return nativeHandle; |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | void SensorManager::destroyDirectChannel(int channelNativeHandle) { |
| 316 | Mutex::Autolock _l(mLock); |
| 317 | if (assertStateLocked() == NO_ERROR) { |
| 318 | mDirectConnection.erase(channelNativeHandle); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | int SensorManager::configureDirectChannel(int channelNativeHandle, int sensorHandle, int rateLevel) { |
| 323 | Mutex::Autolock _l(mLock); |
| 324 | if (assertStateLocked() != NO_ERROR) { |
| 325 | return NO_INIT; |
| 326 | } |
| 327 | |
| 328 | auto i = mDirectConnection.find(channelNativeHandle); |
| 329 | if (i == mDirectConnection.end()) { |
| 330 | ALOGE("Cannot find the handle in client direct connection table"); |
| 331 | return BAD_VALUE; |
| 332 | } |
| 333 | |
| 334 | int ret; |
| 335 | ret = i->second->configureChannel(sensorHandle, rateLevel); |
| 336 | ALOGE_IF(ret < 0, "SensorManager::configureChannel (%d, %d) returns %d", |
| 337 | static_cast<int>(sensorHandle), static_cast<int>(rateLevel), |
| 338 | static_cast<int>(ret)); |
| 339 | return ret; |
| 340 | } |
| 341 | |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 342 | int SensorManager::setOperationParameter( |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 343 | int handle, int type, |
| 344 | const Vector<float> &floats, const Vector<int32_t> &ints) { |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 345 | Mutex::Autolock _l(mLock); |
| 346 | if (assertStateLocked() != NO_ERROR) { |
| 347 | return NO_INIT; |
| 348 | } |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 349 | return mSensorServer->setOperationParameter(handle, type, floats, ints); |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 352 | // ---------------------------------------------------------------------------- |
| 353 | }; // namespace android |