SensorManager.cpp revision e36e34731cbe77a49aa5e7d687dde041d83d0370
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 17#define LOG_TAG "Sensors" 18 19#include <stdint.h> 20#include <sys/types.h> 21 22#include <cutils/native_handle.h> 23#include <utils/Errors.h> 24#include <utils/RefBase.h> 25#include <utils/Singleton.h> 26 27#include <binder/IBinder.h> 28#include <binder/IServiceManager.h> 29 30#include <gui/ISensorServer.h> 31#include <gui/ISensorEventConnection.h> 32#include <gui/Sensor.h> 33#include <gui/SensorManager.h> 34#include <gui/SensorEventQueue.h> 35 36// ---------------------------------------------------------------------------- 37namespace android { 38// ---------------------------------------------------------------------------- 39 40android::Mutex android::SensorManager::sLock; 41std::map<String16, SensorManager*> android::SensorManager::sPackageInstances; 42 43SensorManager& SensorManager::getInstanceForPackage(const String16& packageName) { 44 Mutex::Autolock _l(sLock); 45 SensorManager* sensorManager; 46 std::map<String16, SensorManager*>::iterator iterator = 47 sPackageInstances.find(packageName); 48 49 if (iterator != sPackageInstances.end()) { 50 sensorManager = iterator->second; 51 } else { 52 String16 opPackageName = packageName; 53 54 // It is possible that the calling code has no access to the package name. 55 // In this case we will get the packages for the calling UID and pick the 56 // first one for attributing the app op. This will work correctly for 57 // runtime permissions as for legacy apps we will toggle the app op for 58 // all packages in the UID. The caveat is that the operation may be attributed 59 // to the wrong package and stats based on app ops may be slightly off. 60 if (opPackageName.size() <= 0) { 61 sp<IBinder> binder = defaultServiceManager()->getService(String16("permission")); 62 if (binder != 0) { 63 const uid_t uid = IPCThreadState::self()->getCallingUid(); 64 Vector<String16> packages; 65 interface_cast<IPermissionController>(binder)->getPackagesForUid(uid, packages); 66 if (!packages.isEmpty()) { 67 opPackageName = packages[0]; 68 } else { 69 ALOGE("No packages for calling UID"); 70 } 71 } else { 72 ALOGE("Cannot get permission service"); 73 } 74 } 75 76 sensorManager = new SensorManager(opPackageName); 77 78 // If we had no package name, we looked it up from the UID and the sensor 79 // manager instance we created should also be mapped to the empty package 80 // name, to avoid looking up the packages for a UID and get the same result. 81 if (packageName.size() <= 0) { 82 sPackageInstances.insert(std::make_pair(String16(), sensorManager)); 83 } 84 85 // Stash the per package sensor manager. 86 sPackageInstances.insert(std::make_pair(opPackageName, sensorManager)); 87 } 88 89 return *sensorManager; 90} 91 92SensorManager::SensorManager(const String16& opPackageName) 93 : mSensorList(0), mOpPackageName(opPackageName), mDirectConnectionHandle(1) { 94 // okay we're not locked here, but it's not needed during construction 95 assertStateLocked(); 96} 97 98SensorManager::~SensorManager() { 99 free(mSensorList); 100} 101 102void SensorManager::sensorManagerDied() { 103 Mutex::Autolock _l(mLock); 104 mSensorServer.clear(); 105 free(mSensorList); 106 mSensorList = NULL; 107 mSensors.clear(); 108} 109 110status_t SensorManager::assertStateLocked() { 111 bool initSensorManager = false; 112 if (mSensorServer == NULL) { 113 initSensorManager = true; 114 } else { 115 // Ping binder to check if sensorservice is alive. 116 status_t err = IInterface::asBinder(mSensorServer)->pingBinder(); 117 if (err != NO_ERROR) { 118 initSensorManager = true; 119 } 120 } 121 if (initSensorManager) { 122 // try for 300 seconds (60*5(getService() tries for 5 seconds)) before giving up ... 123 const String16 name("sensorservice"); 124 for (int i = 0; i < 60; i++) { 125 status_t err = getService(name, &mSensorServer); 126 if (err == NAME_NOT_FOUND) { 127 sleep(1); 128 continue; 129 } 130 if (err != NO_ERROR) { 131 return err; 132 } 133 break; 134 } 135 136 class DeathObserver : public IBinder::DeathRecipient { 137 SensorManager& mSensorManager; 138 virtual void binderDied(const wp<IBinder>& who) { 139 ALOGW("sensorservice died [%p]", who.unsafe_get()); 140 mSensorManager.sensorManagerDied(); 141 } 142 public: 143 explicit DeathObserver(SensorManager& mgr) : mSensorManager(mgr) { } 144 }; 145 146 LOG_ALWAYS_FATAL_IF(mSensorServer.get() == NULL, "getService(SensorService) NULL"); 147 148 mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this)); 149 IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver); 150 151 mSensors = mSensorServer->getSensorList(mOpPackageName); 152 size_t count = mSensors.size(); 153 mSensorList = 154 static_cast<Sensor const**>(malloc(count * sizeof(Sensor*))); 155 LOG_ALWAYS_FATAL_IF(mSensorList == NULL, "mSensorList NULL"); 156 157 for (size_t i=0 ; i<count ; i++) { 158 mSensorList[i] = mSensors.array() + i; 159 } 160 } 161 162 return NO_ERROR; 163} 164 165ssize_t SensorManager::getSensorList(Sensor const* const** list) { 166 Mutex::Autolock _l(mLock); 167 status_t err = assertStateLocked(); 168 if (err < 0) { 169 return static_cast<ssize_t>(err); 170 } 171 *list = mSensorList; 172 return static_cast<ssize_t>(mSensors.size()); 173} 174 175ssize_t SensorManager::getDynamicSensorList(Vector<Sensor> & dynamicSensors) { 176 Mutex::Autolock _l(mLock); 177 status_t err = assertStateLocked(); 178 if (err < 0) { 179 return static_cast<ssize_t>(err); 180 } 181 182 dynamicSensors = mSensorServer->getDynamicSensorList(mOpPackageName); 183 size_t count = dynamicSensors.size(); 184 185 return static_cast<ssize_t>(count); 186} 187 188Sensor const* SensorManager::getDefaultSensor(int type) 189{ 190 Mutex::Autolock _l(mLock); 191 if (assertStateLocked() == NO_ERROR) { 192 bool wakeUpSensor = false; 193 // For the following sensor types, return a wake-up sensor. These types are by default 194 // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return 195 // a non_wake-up version. 196 if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION || 197 type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE || 198 type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE || 199 type == SENSOR_TYPE_WRIST_TILT_GESTURE) { 200 wakeUpSensor = true; 201 } 202 // For now we just return the first sensor of that type we find. 203 // in the future it will make sense to let the SensorService make 204 // that decision. 205 for (size_t i=0 ; i<mSensors.size() ; i++) { 206 if (mSensorList[i]->getType() == type && 207 mSensorList[i]->isWakeUpSensor() == wakeUpSensor) { 208 return mSensorList[i]; 209 } 210 } 211 } 212 return NULL; 213} 214 215sp<SensorEventQueue> SensorManager::createEventQueue(String8 packageName, int mode) { 216 sp<SensorEventQueue> queue; 217 218 Mutex::Autolock _l(mLock); 219 while (assertStateLocked() == NO_ERROR) { 220 sp<ISensorEventConnection> connection = 221 mSensorServer->createSensorEventConnection(packageName, mode, mOpPackageName); 222 if (connection == NULL) { 223 // SensorService just died or the app doesn't have required permissions. 224 ALOGE("createEventQueue: connection is NULL."); 225 return NULL; 226 } 227 queue = new SensorEventQueue(connection); 228 break; 229 } 230 return queue; 231} 232 233bool SensorManager::isDataInjectionEnabled() { 234 Mutex::Autolock _l(mLock); 235 if (assertStateLocked() == NO_ERROR) { 236 return mSensorServer->isDataInjectionEnabled(); 237 } 238 return false; 239} 240 241int SensorManager::createDirectChannel( 242 size_t size, int channelType, const native_handle_t *resourceHandle) { 243 Mutex::Autolock _l(mLock); 244 if (assertStateLocked() != NO_ERROR) { 245 return NO_INIT; 246 } 247 248 switch (channelType) { 249 case SENSOR_DIRECT_MEM_TYPE_ASHMEM: { 250 sp<ISensorEventConnection> conn = 251 mSensorServer->createSensorDirectConnection(mOpPackageName, 252 static_cast<uint32_t>(size), 253 static_cast<int32_t>(channelType), 254 SENSOR_DIRECT_FMT_SENSORS_EVENT, resourceHandle); 255 if (conn == nullptr) { 256 return NO_MEMORY; 257 } 258 int nativeHandle = mDirectConnectionHandle++; 259 mDirectConnection.emplace(nativeHandle, conn); 260 return nativeHandle; 261 } 262 case SENSOR_DIRECT_MEM_TYPE_GRALLOC: 263 LOG_FATAL("%s: Finish implementation of ION and GRALLOC or remove", __FUNCTION__); 264 return BAD_VALUE; 265 default: 266 ALOGE("Bad channel shared memory type %d", channelType); 267 return BAD_VALUE; 268 } 269} 270 271void SensorManager::destroyDirectChannel(int channelNativeHandle) { 272 Mutex::Autolock _l(mLock); 273 if (assertStateLocked() == NO_ERROR) { 274 mDirectConnection.erase(channelNativeHandle); 275 } 276} 277 278int SensorManager::configureDirectChannel(int channelNativeHandle, int sensorHandle, int rateLevel) { 279 Mutex::Autolock _l(mLock); 280 if (assertStateLocked() != NO_ERROR) { 281 return NO_INIT; 282 } 283 284 auto i = mDirectConnection.find(channelNativeHandle); 285 if (i == mDirectConnection.end()) { 286 ALOGE("Cannot find the handle in client direct connection table"); 287 return BAD_VALUE; 288 } 289 290 int ret; 291 ret = i->second->configureChannel(sensorHandle, rateLevel); 292 ALOGE_IF(ret < 0, "SensorManager::configureChannel (%d, %d) returns %d", 293 static_cast<int>(sensorHandle), static_cast<int>(rateLevel), 294 static_cast<int>(ret)); 295 return ret; 296} 297 298// ---------------------------------------------------------------------------- 299}; // namespace android 300