blob: b6e9fa1c720f9382a3db322f624bae11080d5571 [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 Agopian801ea092017-03-06 15:05:04 -080019#include <sensor/SensorManager.h>
20
Mathias Agopian589ce852010-07-13 22:21:56 -070021#include <stdint.h>
22#include <sys/types.h>
23
Peng Xue36e3472016-11-03 11:57:10 -070024#include <cutils/native_handle.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070025#include <utils/Errors.h>
26#include <utils/RefBase.h>
27#include <utils/Singleton.h>
28
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070029#include <binder/IBinder.h>
Mathias Agopiana7352c92010-07-14 23:41:37 -070030#include <binder/IServiceManager.h>
31
Mathias Agopian801ea092017-03-06 15:05:04 -080032#include <sensor/ISensorServer.h>
33#include <sensor/ISensorEventConnection.h>
34#include <sensor/Sensor.h>
35#include <sensor/SensorEventQueue.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070036
37// ----------------------------------------------------------------------------
38namespace android {
39// ----------------------------------------------------------------------------
40
Aravind Akellae2806cb2015-07-29 18:03:48 -070041android::Mutex android::SensorManager::sLock;
42std::map<String16, SensorManager*> android::SensorManager::sPackageInstances;
43
44SensorManager& SensorManager::getInstanceForPackage(const String16& packageName) {
45 Mutex::Autolock _l(sLock);
46 SensorManager* sensorManager;
47 std::map<String16, SensorManager*>::iterator iterator =
48 sPackageInstances.find(packageName);
49
50 if (iterator != sPackageInstances.end()) {
51 sensorManager = iterator->second;
52 } else {
53 String16 opPackageName = packageName;
54
55 // It is possible that the calling code has no access to the package name.
56 // In this case we will get the packages for the calling UID and pick the
57 // first one for attributing the app op. This will work correctly for
58 // runtime permissions as for legacy apps we will toggle the app op for
59 // all packages in the UID. The caveat is that the operation may be attributed
60 // to the wrong package and stats based on app ops may be slightly off.
61 if (opPackageName.size() <= 0) {
62 sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
63 if (binder != 0) {
64 const uid_t uid = IPCThreadState::self()->getCallingUid();
65 Vector<String16> packages;
66 interface_cast<IPermissionController>(binder)->getPackagesForUid(uid, packages);
67 if (!packages.isEmpty()) {
68 opPackageName = packages[0];
69 } else {
70 ALOGE("No packages for calling UID");
71 }
72 } else {
73 ALOGE("Cannot get permission service");
74 }
75 }
76
77 sensorManager = new SensorManager(opPackageName);
78
79 // If we had no package name, we looked it up from the UID and the sensor
80 // manager instance we created should also be mapped to the empty package
81 // name, to avoid looking up the packages for a UID and get the same result.
82 if (packageName.size() <= 0) {
83 sPackageInstances.insert(std::make_pair(String16(), sensorManager));
84 }
85
86 // Stash the per package sensor manager.
87 sPackageInstances.insert(std::make_pair(opPackageName, sensorManager));
88 }
89
90 return *sensorManager;
91}
92
Svetoslavb412f6e2015-04-29 16:50:41 -070093SensorManager::SensorManager(const String16& opPackageName)
Peng Xue36e3472016-11-03 11:57:10 -070094 : mSensorList(0), mOpPackageName(opPackageName), mDirectConnectionHandle(1) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070095 // okay we're not locked here, but it's not needed during construction
96 assertStateLocked();
Mathias Agopian589ce852010-07-13 22:21:56 -070097}
98
Peng Xu2576cb62016-01-20 00:22:09 -080099SensorManager::~SensorManager() {
Mathias Agopiana7352c92010-07-14 23:41:37 -0700100 free(mSensorList);
Mathias Agopian589ce852010-07-13 22:21:56 -0700101}
102
Peng Xu2576cb62016-01-20 00:22:09 -0800103void SensorManager::sensorManagerDied() {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700104 Mutex::Autolock _l(mLock);
105 mSensorServer.clear();
106 free(mSensorList);
107 mSensorList = NULL;
108 mSensors.clear();
109}
110
Peng Xu2576cb62016-01-20 00:22:09 -0800111status_t SensorManager::assertStateLocked() {
Aravind Akella8f35ca92015-08-17 15:22:12 -0700112 bool initSensorManager = false;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700113 if (mSensorServer == NULL) {
Aravind Akella8f35ca92015-08-17 15:22:12 -0700114 initSensorManager = true;
115 } else {
116 // Ping binder to check if sensorservice is alive.
117 status_t err = IInterface::asBinder(mSensorServer)->pingBinder();
118 if (err != NO_ERROR) {
119 initSensorManager = true;
120 }
121 }
122 if (initSensorManager) {
123 // try for 300 seconds (60*5(getService() tries for 5 seconds)) before giving up ...
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700124 const String16 name("sensorservice");
Aravind Akella8f35ca92015-08-17 15:22:12 -0700125 for (int i = 0; i < 60; i++) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700126 status_t err = getService(name, &mSensorServer);
127 if (err == NAME_NOT_FOUND) {
Aravind Akellae2806cb2015-07-29 18:03:48 -0700128 sleep(1);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700129 continue;
130 }
131 if (err != NO_ERROR) {
132 return err;
133 }
134 break;
135 }
136
137 class DeathObserver : public IBinder::DeathRecipient {
Peng Xu2576cb62016-01-20 00:22:09 -0800138 SensorManager& mSensorManager;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700139 virtual void binderDied(const wp<IBinder>& who) {
Steve Block32397c12012-01-05 23:22:43 +0000140 ALOGW("sensorservice died [%p]", who.unsafe_get());
Peng Xu2576cb62016-01-20 00:22:09 -0800141 mSensorManager.sensorManagerDied();
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700142 }
143 public:
Chih-Hung Hsieh68a593e2016-04-28 09:14:32 -0700144 explicit DeathObserver(SensorManager& mgr) : mSensorManager(mgr) { }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700145 };
146
Aravind Akella8f35ca92015-08-17 15:22:12 -0700147 LOG_ALWAYS_FATAL_IF(mSensorServer.get() == NULL, "getService(SensorService) NULL");
Aravind Akellae2806cb2015-07-29 18:03:48 -0700148
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700149 mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800150 IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700151
Svetoslavb412f6e2015-04-29 16:50:41 -0700152 mSensors = mSensorServer->getSensorList(mOpPackageName);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700153 size_t count = mSensors.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800154 mSensorList =
155 static_cast<Sensor const**>(malloc(count * sizeof(Sensor*)));
Aravind Akella8f35ca92015-08-17 15:22:12 -0700156 LOG_ALWAYS_FATAL_IF(mSensorList == NULL, "mSensorList NULL");
157
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700158 for (size_t i=0 ; i<count ; i++) {
159 mSensorList[i] = mSensors.array() + i;
160 }
161 }
162
163 return NO_ERROR;
164}
165
Peng Xu2576cb62016-01-20 00:22:09 -0800166ssize_t SensorManager::getSensorList(Sensor const* const** list) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700167 Mutex::Autolock _l(mLock);
168 status_t err = assertStateLocked();
169 if (err < 0) {
Dan Stozad723bd72014-11-18 10:24:03 -0800170 return static_cast<ssize_t>(err);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700171 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700172 *list = mSensorList;
Dan Stozad723bd72014-11-18 10:24:03 -0800173 return static_cast<ssize_t>(mSensors.size());
Mathias Agopian589ce852010-07-13 22:21:56 -0700174}
175
Peng Xu2576cb62016-01-20 00:22:09 -0800176ssize_t SensorManager::getDynamicSensorList(Vector<Sensor> & dynamicSensors) {
177 Mutex::Autolock _l(mLock);
178 status_t err = assertStateLocked();
179 if (err < 0) {
180 return static_cast<ssize_t>(err);
181 }
182
183 dynamicSensors = mSensorServer->getDynamicSensorList(mOpPackageName);
184 size_t count = dynamicSensors.size();
185
186 return static_cast<ssize_t>(count);
187}
188
Mathias Agopiana7352c92010-07-14 23:41:37 -0700189Sensor const* SensorManager::getDefaultSensor(int type)
Mathias Agopian589ce852010-07-13 22:21:56 -0700190{
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700191 Mutex::Autolock _l(mLock);
192 if (assertStateLocked() == NO_ERROR) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700193 bool wakeUpSensor = false;
194 // For the following sensor types, return a wake-up sensor. These types are by default
195 // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return
196 // a non_wake-up version.
197 if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION ||
198 type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE ||
Nick Vaccaro5e7f79b2016-10-17 15:40:51 -0700199 type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE ||
Nick Vaccaro2e990eb2017-01-12 21:13:58 -0800200 type == SENSOR_TYPE_WRIST_TILT_GESTURE ||
201 type == SENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700202 wakeUpSensor = true;
203 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700204 // For now we just return the first sensor of that type we find.
205 // in the future it will make sense to let the SensorService make
206 // that decision.
207 for (size_t i=0 ; i<mSensors.size() ; i++) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700208 if (mSensorList[i]->getType() == type &&
209 mSensorList[i]->isWakeUpSensor() == wakeUpSensor) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700210 return mSensorList[i];
Aravind Akellab37ba392014-08-05 14:53:07 -0700211 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700212 }
Mathias Agopiana7352c92010-07-14 23:41:37 -0700213 }
214 return NULL;
Mathias Agopian589ce852010-07-13 22:21:56 -0700215}
216
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700217sp<SensorEventQueue> SensorManager::createEventQueue(String8 packageName, int mode) {
Mathias Agopianbe58de02011-10-16 00:38:30 -0700218 sp<SensorEventQueue> queue;
219
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700220 Mutex::Autolock _l(mLock);
221 while (assertStateLocked() == NO_ERROR) {
222 sp<ISensorEventConnection> connection =
Svetoslavb412f6e2015-04-29 16:50:41 -0700223 mSensorServer->createSensorEventConnection(packageName, mode, mOpPackageName);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700224 if (connection == NULL) {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700225 // SensorService just died or the app doesn't have required permissions.
226 ALOGE("createEventQueue: connection is NULL.");
227 return NULL;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700228 }
229 queue = new SensorEventQueue(connection);
230 break;
Mathias Agopianbe58de02011-10-16 00:38:30 -0700231 }
Mathias Agopianbe58de02011-10-16 00:38:30 -0700232 return queue;
Mathias Agopian589ce852010-07-13 22:21:56 -0700233}
234
Aravind Akella841a5922015-06-29 12:37:48 -0700235bool SensorManager::isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700236 Mutex::Autolock _l(mLock);
237 if (assertStateLocked() == NO_ERROR) {
Aravind Akella841a5922015-06-29 12:37:48 -0700238 return mSensorServer->isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700239 }
Aravind Akella841a5922015-06-29 12:37:48 -0700240 return false;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700241}
242
Peng Xue36e3472016-11-03 11:57:10 -0700243int SensorManager::createDirectChannel(
244 size_t size, int channelType, const native_handle_t *resourceHandle) {
245 Mutex::Autolock _l(mLock);
246 if (assertStateLocked() != NO_ERROR) {
247 return NO_INIT;
248 }
249
250 switch (channelType) {
251 case SENSOR_DIRECT_MEM_TYPE_ASHMEM: {
252 sp<ISensorEventConnection> conn =
253 mSensorServer->createSensorDirectConnection(mOpPackageName,
254 static_cast<uint32_t>(size),
255 static_cast<int32_t>(channelType),
256 SENSOR_DIRECT_FMT_SENSORS_EVENT, resourceHandle);
257 if (conn == nullptr) {
258 return NO_MEMORY;
259 }
260 int nativeHandle = mDirectConnectionHandle++;
261 mDirectConnection.emplace(nativeHandle, conn);
262 return nativeHandle;
263 }
264 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
265 LOG_FATAL("%s: Finish implementation of ION and GRALLOC or remove", __FUNCTION__);
266 return BAD_VALUE;
267 default:
268 ALOGE("Bad channel shared memory type %d", channelType);
269 return BAD_VALUE;
270 }
271}
272
273void SensorManager::destroyDirectChannel(int channelNativeHandle) {
274 Mutex::Autolock _l(mLock);
275 if (assertStateLocked() == NO_ERROR) {
276 mDirectConnection.erase(channelNativeHandle);
277 }
278}
279
280int SensorManager::configureDirectChannel(int channelNativeHandle, int sensorHandle, int rateLevel) {
281 Mutex::Autolock _l(mLock);
282 if (assertStateLocked() != NO_ERROR) {
283 return NO_INIT;
284 }
285
286 auto i = mDirectConnection.find(channelNativeHandle);
287 if (i == mDirectConnection.end()) {
288 ALOGE("Cannot find the handle in client direct connection table");
289 return BAD_VALUE;
290 }
291
292 int ret;
293 ret = i->second->configureChannel(sensorHandle, rateLevel);
294 ALOGE_IF(ret < 0, "SensorManager::configureChannel (%d, %d) returns %d",
295 static_cast<int>(sensorHandle), static_cast<int>(rateLevel),
296 static_cast<int>(ret));
297 return ret;
298}
299
Mathias Agopian589ce852010-07-13 22:21:56 -0700300// ----------------------------------------------------------------------------
301}; // namespace android