blob: 46eaf286ce2cae2229bbd18d442c54134a99958b [file] [log] [blame]
Mathias Agopian589ce852010-07-13 22:21:56 -07001/*
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 Agopiana7352c92010-07-14 23:41:37 -070017#define LOG_TAG "Sensors"
18
Mathias Agopian589ce852010-07-13 22:21:56 -070019#include <stdint.h>
20#include <sys/types.h>
21
Peng Xue36e3472016-11-03 11:57:10 -070022#include <cutils/native_handle.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070023#include <utils/Errors.h>
24#include <utils/RefBase.h>
25#include <utils/Singleton.h>
26
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070027#include <binder/IBinder.h>
Mathias Agopiana7352c92010-07-14 23:41:37 -070028#include <binder/IServiceManager.h>
29
Mathias Agopian589ce852010-07-13 22:21:56 -070030#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
Aravind Akellae2806cb2015-07-29 18:03:48 -070040android::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
Svetoslavb412f6e2015-04-29 16:50:41 -070092SensorManager::SensorManager(const String16& opPackageName)
Peng Xue36e3472016-11-03 11:57:10 -070093 : mSensorList(0), mOpPackageName(opPackageName), mDirectConnectionHandle(1) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070094 // okay we're not locked here, but it's not needed during construction
95 assertStateLocked();
Mathias Agopian589ce852010-07-13 22:21:56 -070096}
97
Peng Xu2576cb62016-01-20 00:22:09 -080098SensorManager::~SensorManager() {
Mathias Agopiana7352c92010-07-14 23:41:37 -070099 free(mSensorList);
Mathias Agopian589ce852010-07-13 22:21:56 -0700100}
101
Peng Xu2576cb62016-01-20 00:22:09 -0800102void SensorManager::sensorManagerDied() {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700103 Mutex::Autolock _l(mLock);
104 mSensorServer.clear();
105 free(mSensorList);
106 mSensorList = NULL;
107 mSensors.clear();
108}
109
Peng Xu2576cb62016-01-20 00:22:09 -0800110status_t SensorManager::assertStateLocked() {
Aravind Akella8f35ca92015-08-17 15:22:12 -0700111 bool initSensorManager = false;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700112 if (mSensorServer == NULL) {
Aravind Akella8f35ca92015-08-17 15:22:12 -0700113 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 ...
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700123 const String16 name("sensorservice");
Aravind Akella8f35ca92015-08-17 15:22:12 -0700124 for (int i = 0; i < 60; i++) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700125 status_t err = getService(name, &mSensorServer);
126 if (err == NAME_NOT_FOUND) {
Aravind Akellae2806cb2015-07-29 18:03:48 -0700127 sleep(1);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700128 continue;
129 }
130 if (err != NO_ERROR) {
131 return err;
132 }
133 break;
134 }
135
136 class DeathObserver : public IBinder::DeathRecipient {
Peng Xu2576cb62016-01-20 00:22:09 -0800137 SensorManager& mSensorManager;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700138 virtual void binderDied(const wp<IBinder>& who) {
Steve Block32397c12012-01-05 23:22:43 +0000139 ALOGW("sensorservice died [%p]", who.unsafe_get());
Peng Xu2576cb62016-01-20 00:22:09 -0800140 mSensorManager.sensorManagerDied();
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700141 }
142 public:
Chih-Hung Hsieh68a593e2016-04-28 09:14:32 -0700143 explicit DeathObserver(SensorManager& mgr) : mSensorManager(mgr) { }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700144 };
145
Aravind Akella8f35ca92015-08-17 15:22:12 -0700146 LOG_ALWAYS_FATAL_IF(mSensorServer.get() == NULL, "getService(SensorService) NULL");
Aravind Akellae2806cb2015-07-29 18:03:48 -0700147
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700148 mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800149 IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700150
Svetoslavb412f6e2015-04-29 16:50:41 -0700151 mSensors = mSensorServer->getSensorList(mOpPackageName);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700152 size_t count = mSensors.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800153 mSensorList =
154 static_cast<Sensor const**>(malloc(count * sizeof(Sensor*)));
Aravind Akella8f35ca92015-08-17 15:22:12 -0700155 LOG_ALWAYS_FATAL_IF(mSensorList == NULL, "mSensorList NULL");
156
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700157 for (size_t i=0 ; i<count ; i++) {
158 mSensorList[i] = mSensors.array() + i;
159 }
160 }
161
162 return NO_ERROR;
163}
164
Peng Xu2576cb62016-01-20 00:22:09 -0800165ssize_t SensorManager::getSensorList(Sensor const* const** list) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700166 Mutex::Autolock _l(mLock);
167 status_t err = assertStateLocked();
168 if (err < 0) {
Dan Stozad723bd72014-11-18 10:24:03 -0800169 return static_cast<ssize_t>(err);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700170 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700171 *list = mSensorList;
Dan Stozad723bd72014-11-18 10:24:03 -0800172 return static_cast<ssize_t>(mSensors.size());
Mathias Agopian589ce852010-07-13 22:21:56 -0700173}
174
Peng Xu2576cb62016-01-20 00:22:09 -0800175ssize_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
Mathias Agopiana7352c92010-07-14 23:41:37 -0700188Sensor const* SensorManager::getDefaultSensor(int type)
Mathias Agopian589ce852010-07-13 22:21:56 -0700189{
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700190 Mutex::Autolock _l(mLock);
191 if (assertStateLocked() == NO_ERROR) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700192 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 ||
Nick Vaccaro5e7f79b2016-10-17 15:40:51 -0700198 type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE ||
199 type == SENSOR_TYPE_WRIST_TILT_GESTURE) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700200 wakeUpSensor = true;
201 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700202 // 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++) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700206 if (mSensorList[i]->getType() == type &&
207 mSensorList[i]->isWakeUpSensor() == wakeUpSensor) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700208 return mSensorList[i];
Aravind Akellab37ba392014-08-05 14:53:07 -0700209 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700210 }
Mathias Agopiana7352c92010-07-14 23:41:37 -0700211 }
212 return NULL;
Mathias Agopian589ce852010-07-13 22:21:56 -0700213}
214
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700215sp<SensorEventQueue> SensorManager::createEventQueue(String8 packageName, int mode) {
Mathias Agopianbe58de02011-10-16 00:38:30 -0700216 sp<SensorEventQueue> queue;
217
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700218 Mutex::Autolock _l(mLock);
219 while (assertStateLocked() == NO_ERROR) {
220 sp<ISensorEventConnection> connection =
Svetoslavb412f6e2015-04-29 16:50:41 -0700221 mSensorServer->createSensorEventConnection(packageName, mode, mOpPackageName);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700222 if (connection == NULL) {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700223 // SensorService just died or the app doesn't have required permissions.
224 ALOGE("createEventQueue: connection is NULL.");
225 return NULL;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700226 }
227 queue = new SensorEventQueue(connection);
228 break;
Mathias Agopianbe58de02011-10-16 00:38:30 -0700229 }
Mathias Agopianbe58de02011-10-16 00:38:30 -0700230 return queue;
Mathias Agopian589ce852010-07-13 22:21:56 -0700231}
232
Aravind Akella841a5922015-06-29 12:37:48 -0700233bool SensorManager::isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700234 Mutex::Autolock _l(mLock);
235 if (assertStateLocked() == NO_ERROR) {
Aravind Akella841a5922015-06-29 12:37:48 -0700236 return mSensorServer->isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700237 }
Aravind Akella841a5922015-06-29 12:37:48 -0700238 return false;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700239}
240
Peng Xue36e3472016-11-03 11:57:10 -0700241int 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
Mathias Agopian589ce852010-07-13 22:21:56 -0700298// ----------------------------------------------------------------------------
299}; // namespace android