| 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 | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 19 | #include <stdint.h> | 
|  | 20 | #include <sys/types.h> | 
|  | 21 |  | 
|  | 22 | #include <utils/Errors.h> | 
|  | 23 | #include <utils/RefBase.h> | 
|  | 24 | #include <utils/Singleton.h> | 
|  | 25 |  | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 26 | #include <binder/IBinder.h> | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 27 | #include <binder/IServiceManager.h> | 
|  | 28 |  | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 29 | #include <gui/ISensorServer.h> | 
|  | 30 | #include <gui/ISensorEventConnection.h> | 
|  | 31 | #include <gui/Sensor.h> | 
|  | 32 | #include <gui/SensorManager.h> | 
|  | 33 | #include <gui/SensorEventQueue.h> | 
|  | 34 |  | 
|  | 35 | // ---------------------------------------------------------------------------- | 
|  | 36 | namespace android { | 
|  | 37 | // ---------------------------------------------------------------------------- | 
|  | 38 |  | 
| Aravind Akella | e2806cb | 2015-07-29 18:03:48 -0700 | [diff] [blame] | 39 | android::Mutex android::SensorManager::sLock; | 
|  | 40 | std::map<String16, SensorManager*> android::SensorManager::sPackageInstances; | 
|  | 41 |  | 
|  | 42 | SensorManager& SensorManager::getInstanceForPackage(const String16& packageName) { | 
|  | 43 | Mutex::Autolock _l(sLock); | 
|  | 44 | SensorManager* sensorManager; | 
|  | 45 | std::map<String16, SensorManager*>::iterator iterator = | 
|  | 46 | sPackageInstances.find(packageName); | 
|  | 47 |  | 
|  | 48 | if (iterator != sPackageInstances.end()) { | 
|  | 49 | sensorManager = iterator->second; | 
|  | 50 | } else { | 
|  | 51 | String16 opPackageName = packageName; | 
|  | 52 |  | 
|  | 53 | // It is possible that the calling code has no access to the package name. | 
|  | 54 | // In this case we will get the packages for the calling UID and pick the | 
|  | 55 | // first one for attributing the app op. This will work correctly for | 
|  | 56 | // runtime permissions as for legacy apps we will toggle the app op for | 
|  | 57 | // all packages in the UID. The caveat is that the operation may be attributed | 
|  | 58 | // to the wrong package and stats based on app ops may be slightly off. | 
|  | 59 | if (opPackageName.size() <= 0) { | 
|  | 60 | sp<IBinder> binder = defaultServiceManager()->getService(String16("permission")); | 
|  | 61 | if (binder != 0) { | 
|  | 62 | const uid_t uid = IPCThreadState::self()->getCallingUid(); | 
|  | 63 | Vector<String16> packages; | 
|  | 64 | interface_cast<IPermissionController>(binder)->getPackagesForUid(uid, packages); | 
|  | 65 | if (!packages.isEmpty()) { | 
|  | 66 | opPackageName = packages[0]; | 
|  | 67 | } else { | 
|  | 68 | ALOGE("No packages for calling UID"); | 
|  | 69 | } | 
|  | 70 | } else { | 
|  | 71 | ALOGE("Cannot get permission service"); | 
|  | 72 | } | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | sensorManager = new SensorManager(opPackageName); | 
|  | 76 |  | 
|  | 77 | // If we had no package name, we looked it up from the UID and the sensor | 
|  | 78 | // manager instance we created should also be mapped to the empty package | 
|  | 79 | // name, to avoid looking up the packages for a UID and get the same result. | 
|  | 80 | if (packageName.size() <= 0) { | 
|  | 81 | sPackageInstances.insert(std::make_pair(String16(), sensorManager)); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | // Stash the per package sensor manager. | 
|  | 85 | sPackageInstances.insert(std::make_pair(opPackageName, sensorManager)); | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | return *sensorManager; | 
|  | 89 | } | 
|  | 90 |  | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 91 | SensorManager::SensorManager(const String16& opPackageName) | 
| Peng Xu | b7beb52 | 2016-01-21 23:15:04 -0800 | [diff] [blame] | 92 | : mSensorList(NULL), mAvailableSensorList(NULL), mNumAvailableSensor(0), | 
|  | 93 | mOpPackageName(opPackageName), mBodyPermission(false) | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 94 | { | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 95 | // okay we're not locked here, but it's not needed during construction | 
|  | 96 | assertStateLocked(); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 97 | } | 
|  | 98 |  | 
|  | 99 | SensorManager::~SensorManager() | 
|  | 100 | { | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 101 | free(mSensorList); | 
| Peng Xu | b7beb52 | 2016-01-21 23:15:04 -0800 | [diff] [blame] | 102 | if (mAvailableSensorList) { | 
|  | 103 | free(mAvailableSensorList); | 
|  | 104 | } | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 107 | void SensorManager::sensorManagerDied() | 
|  | 108 | { | 
|  | 109 | Mutex::Autolock _l(mLock); | 
|  | 110 | mSensorServer.clear(); | 
|  | 111 | free(mSensorList); | 
|  | 112 | mSensorList = NULL; | 
| Peng Xu | b7beb52 | 2016-01-21 23:15:04 -0800 | [diff] [blame] | 113 | if (mAvailableSensorList) { | 
|  | 114 | free(mAvailableSensorList); | 
|  | 115 | mAvailableSensorList = NULL; | 
|  | 116 | } | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 117 | mSensors.clear(); | 
|  | 118 | } | 
|  | 119 |  | 
| Peng Xu | b7beb52 | 2016-01-21 23:15:04 -0800 | [diff] [blame] | 120 | status_t SensorManager::assertStateLocked() { | 
| Aravind Akella | 8f35ca9 | 2015-08-17 15:22:12 -0700 | [diff] [blame] | 121 | bool initSensorManager = false; | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 122 | if (mSensorServer == NULL) { | 
| Aravind Akella | 8f35ca9 | 2015-08-17 15:22:12 -0700 | [diff] [blame] | 123 | initSensorManager = true; | 
|  | 124 | } else { | 
|  | 125 | // Ping binder to check if sensorservice is alive. | 
|  | 126 | status_t err = IInterface::asBinder(mSensorServer)->pingBinder(); | 
|  | 127 | if (err != NO_ERROR) { | 
|  | 128 | initSensorManager = true; | 
|  | 129 | } | 
|  | 130 | } | 
|  | 131 | if (initSensorManager) { | 
|  | 132 | // try for 300 seconds (60*5(getService() tries for 5 seconds)) before giving up ... | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 133 | const String16 name("sensorservice"); | 
| Aravind Akella | 8f35ca9 | 2015-08-17 15:22:12 -0700 | [diff] [blame] | 134 | for (int i = 0; i < 60; i++) { | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 135 | status_t err = getService(name, &mSensorServer); | 
|  | 136 | if (err == NAME_NOT_FOUND) { | 
| Aravind Akella | e2806cb | 2015-07-29 18:03:48 -0700 | [diff] [blame] | 137 | sleep(1); | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 138 | continue; | 
|  | 139 | } | 
|  | 140 | if (err != NO_ERROR) { | 
|  | 141 | return err; | 
|  | 142 | } | 
|  | 143 | break; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | class DeathObserver : public IBinder::DeathRecipient { | 
|  | 147 | SensorManager& mSensorManger; | 
|  | 148 | virtual void binderDied(const wp<IBinder>& who) { | 
| Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 149 | ALOGW("sensorservice died [%p]", who.unsafe_get()); | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 150 | mSensorManger.sensorManagerDied(); | 
|  | 151 | } | 
|  | 152 | public: | 
|  | 153 | DeathObserver(SensorManager& mgr) : mSensorManger(mgr) { } | 
|  | 154 | }; | 
|  | 155 |  | 
| Aravind Akella | 8f35ca9 | 2015-08-17 15:22:12 -0700 | [diff] [blame] | 156 | LOG_ALWAYS_FATAL_IF(mSensorServer.get() == NULL, "getService(SensorService) NULL"); | 
| Aravind Akella | e2806cb | 2015-07-29 18:03:48 -0700 | [diff] [blame] | 157 |  | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 158 | mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this)); | 
| Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 159 | IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver); | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 160 |  | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 161 | mSensors = mSensorServer->getSensorList(mOpPackageName); | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 162 | size_t count = mSensors.size(); | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 163 | mSensorList = | 
|  | 164 | static_cast<Sensor const**>(malloc(count * sizeof(Sensor*))); | 
| Aravind Akella | 8f35ca9 | 2015-08-17 15:22:12 -0700 | [diff] [blame] | 165 | LOG_ALWAYS_FATAL_IF(mSensorList == NULL, "mSensorList NULL"); | 
|  | 166 |  | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 167 | for (size_t i=0 ; i<count ; i++) { | 
|  | 168 | mSensorList[i] = mSensors.array() + i; | 
|  | 169 | } | 
| Peng Xu | b7beb52 | 2016-01-21 23:15:04 -0800 | [diff] [blame] | 170 |  | 
|  | 171 | updateAvailableSensorList(); | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 172 | } | 
|  | 173 |  | 
|  | 174 | return NO_ERROR; | 
|  | 175 | } | 
|  | 176 |  | 
| Peng Xu | b7beb52 | 2016-01-21 23:15:04 -0800 | [diff] [blame] | 177 | ssize_t SensorManager::getSensorList(Sensor const* const** list) { | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 178 | Mutex::Autolock _l(mLock); | 
|  | 179 | status_t err = assertStateLocked(); | 
|  | 180 | if (err < 0) { | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 181 | return static_cast<ssize_t>(err); | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 182 | } | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 183 | *list = mSensorList; | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 184 | return static_cast<ssize_t>(mSensors.size()); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 185 | } | 
|  | 186 |  | 
| Peng Xu | b7beb52 | 2016-01-21 23:15:04 -0800 | [diff] [blame] | 187 | void SensorManager::updateAvailableSensorList() { | 
|  | 188 | const int uid = static_cast<int>(IPCThreadState::self()->getCallingUid()); | 
|  | 189 | const int pid = static_cast<int>(IPCThreadState::self()->getCallingPid()); | 
|  | 190 | const String16 BODY_SENSOR_PERMISSION("android.permission.BODY_SENSORS"); | 
|  | 191 | const String8 BODY_SENSOR_PERMISSION8("android.permission.BODY_SENSORS"); | 
|  | 192 |  | 
|  | 193 | bool bodySensorPermission = false; | 
|  | 194 |  | 
|  | 195 | sp<IBinder> binder = defaultServiceManager()->getService(String16("permission")); | 
|  | 196 | if (binder != NULL) { | 
|  | 197 | bodySensorPermission = interface_cast<IPermissionController>(binder)-> | 
|  | 198 | checkPermission(BODY_SENSOR_PERMISSION, pid, uid); | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | // only update if app got BODY_SENSORS permission after last call or the sensor list has not | 
|  | 202 | // been populated. | 
|  | 203 | // | 
|  | 204 | // it is not possible for the reverse transition, as the app will be killed when permission is | 
|  | 205 | // revoked. | 
|  | 206 | if ( (bodySensorPermission && !mBodyPermission) || mAvailableSensorList == NULL) { | 
|  | 207 |  | 
|  | 208 | // allocate only when necessary | 
|  | 209 | if (mAvailableSensorList == NULL) { | 
|  | 210 | // allocate a list big enough to fit all sensors (including those requires permission | 
|  | 211 | // that the app do not have; | 
|  | 212 | mAvailableSensorList = | 
|  | 213 | static_cast<Sensor const**>(malloc(mSensors.size() * sizeof(Sensor*))); | 
|  | 214 |  | 
|  | 215 | // first populate all sensors that do not need body sensor permission | 
|  | 216 | ssize_t& n = mNumAvailableSensor; | 
|  | 217 | for (size_t i = 0; i < mSensors.size() ; i++) { | 
|  | 218 | if (mSensors[i].getRequiredPermission() != BODY_SENSOR_PERMISSION8) { | 
|  | 219 | mAvailableSensorList[n++] = mSensors.array() + i; | 
|  | 220 | } | 
|  | 221 | } | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | if (bodySensorPermission) { | 
|  | 225 | // if the app just got the sensor permission back, fill the sensor at the end of list | 
|  | 226 | ssize_t& n = mNumAvailableSensor; | 
|  | 227 | for (size_t i = 0; i < mSensors.size() ; i++) { | 
|  | 228 | if (mSensors[i].getRequiredPermission() == BODY_SENSOR_PERMISSION8) { | 
|  | 229 | mAvailableSensorList[n++] = mSensors.array() + i; | 
|  | 230 | } | 
|  | 231 | } | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | mBodyPermission = bodySensorPermission; | 
|  | 235 | } | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 | ssize_t SensorManager::getAvailableSensorList(Sensor const* const** list) { | 
|  | 239 | Mutex::Autolock _l(mLock); | 
|  | 240 | status_t err = assertStateLocked(); | 
|  | 241 | if (err < 0) { | 
|  | 242 | return static_cast<ssize_t>(err); | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | updateAvailableSensorList(); | 
|  | 246 |  | 
|  | 247 | *list = mAvailableSensorList; | 
|  | 248 | return mNumAvailableSensor; | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | Sensor const* SensorManager::getDefaultSensor(int type) { | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 252 | Mutex::Autolock _l(mLock); | 
|  | 253 | if (assertStateLocked() == NO_ERROR) { | 
| Peng Xu | b7beb52 | 2016-01-21 23:15:04 -0800 | [diff] [blame] | 254 |  | 
|  | 255 | updateAvailableSensorList(); | 
|  | 256 |  | 
| Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 257 | bool wakeUpSensor = false; | 
|  | 258 | // For the following sensor types, return a wake-up sensor. These types are by default | 
|  | 259 | // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return | 
|  | 260 | // a non_wake-up version. | 
|  | 261 | if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION || | 
|  | 262 | type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE || | 
|  | 263 | type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE) { | 
|  | 264 | wakeUpSensor = true; | 
|  | 265 | } | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 266 | // For now we just return the first sensor of that type we find. | 
|  | 267 | // in the future it will make sense to let the SensorService make | 
|  | 268 | // that decision. | 
|  | 269 | for (size_t i=0 ; i<mSensors.size() ; i++) { | 
| Peng Xu | b7beb52 | 2016-01-21 23:15:04 -0800 | [diff] [blame] | 270 | if (mAvailableSensorList[i]->getType() == type && | 
|  | 271 | mAvailableSensorList[i]->isWakeUpSensor() == wakeUpSensor) { | 
|  | 272 | return mAvailableSensorList[i]; | 
| Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 273 | } | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 274 | } | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 275 | } | 
|  | 276 | return NULL; | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 277 | } | 
|  | 278 |  | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 279 | sp<SensorEventQueue> SensorManager::createEventQueue(String8 packageName, int mode) { | 
| Mathias Agopian | be58de0 | 2011-10-16 00:38:30 -0700 | [diff] [blame] | 280 | sp<SensorEventQueue> queue; | 
|  | 281 |  | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 282 | Mutex::Autolock _l(mLock); | 
|  | 283 | while (assertStateLocked() == NO_ERROR) { | 
|  | 284 | sp<ISensorEventConnection> connection = | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 285 | mSensorServer->createSensorEventConnection(packageName, mode, mOpPackageName); | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 286 | if (connection == NULL) { | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 287 | // SensorService just died or the app doesn't have required permissions. | 
|  | 288 | ALOGE("createEventQueue: connection is NULL."); | 
|  | 289 | return NULL; | 
| Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 290 | } | 
|  | 291 | queue = new SensorEventQueue(connection); | 
|  | 292 | break; | 
| Mathias Agopian | be58de0 | 2011-10-16 00:38:30 -0700 | [diff] [blame] | 293 | } | 
| Mathias Agopian | be58de0 | 2011-10-16 00:38:30 -0700 | [diff] [blame] | 294 | return queue; | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 295 | } | 
|  | 296 |  | 
| Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 297 | bool SensorManager::isDataInjectionEnabled() { | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 298 | Mutex::Autolock _l(mLock); | 
|  | 299 | if (assertStateLocked() == NO_ERROR) { | 
| Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 300 | return mSensorServer->isDataInjectionEnabled(); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 301 | } | 
| Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 302 | return false; | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 303 | } | 
|  | 304 |  | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 305 | // ---------------------------------------------------------------------------- | 
|  | 306 | }; // namespace android |