blob: 96d5eb9d1f555a9b8b96b0e4c27d1748741f09b3 [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>
Jiyong Park47f876b2018-04-17 13:56:46 +090030#include <binder/IPermissionController.h>
Mathias Agopiana7352c92010-07-14 23:41:37 -070031#include <binder/IServiceManager.h>
32
Mathias Agopian801ea092017-03-06 15:05:04 -080033#include <sensor/ISensorServer.h>
34#include <sensor/ISensorEventConnection.h>
35#include <sensor/Sensor.h>
36#include <sensor/SensorEventQueue.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070037
38// ----------------------------------------------------------------------------
39namespace android {
40// ----------------------------------------------------------------------------
41
Peng Xufe5476a2017-03-17 17:27:42 -070042Mutex SensorManager::sLock;
43std::map<String16, SensorManager*> SensorManager::sPackageInstances;
Aravind Akellae2806cb2015-07-29 18:03:48 -070044
45SensorManager& SensorManager::getInstanceForPackage(const String16& packageName) {
Peng Xufe5476a2017-03-17 17:27:42 -070046 waitForSensorService(nullptr);
47
Aravind Akellae2806cb2015-07-29 18:03:48 -070048 Mutex::Autolock _l(sLock);
49 SensorManager* sensorManager;
Peng Xufe5476a2017-03-17 17:27:42 -070050 auto iterator = sPackageInstances.find(packageName);
Aravind Akellae2806cb2015-07-29 18:03:48 -070051
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 Kongd5e079f2018-07-17 16:08:27 -070065 if (binder != nullptr) {
Aravind Akellae2806cb2015-07-29 18:03:48 -070066 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
Svetoslavb412f6e2015-04-29 16:50:41 -070095SensorManager::SensorManager(const String16& opPackageName)
Yi Kongd5e079f2018-07-17 16:08:27 -070096 : mSensorList(nullptr), mOpPackageName(opPackageName), mDirectConnectionHandle(1) {
Brian Duddie8bbd6f42019-06-06 16:43:41 -070097 Mutex::Autolock _l(mLock);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070098 assertStateLocked();
Mathias Agopian589ce852010-07-13 22:21:56 -070099}
100
Peng Xu2576cb62016-01-20 00:22:09 -0800101SensorManager::~SensorManager() {
Mathias Agopiana7352c92010-07-14 23:41:37 -0700102 free(mSensorList);
Mathias Agopian589ce852010-07-13 22:21:56 -0700103}
104
Peng Xufe5476a2017-03-17 17:27:42 -0700105status_t SensorManager::waitForSensorService(sp<ISensorServer> *server) {
106 // try for 300 seconds (60*5(getService() tries for 5 seconds)) before giving up ...
107 sp<ISensorServer> s;
108 const String16 name("sensorservice");
109 for (int i = 0; i < 60; i++) {
110 status_t err = getService(name, &s);
111 switch (err) {
112 case NAME_NOT_FOUND:
113 sleep(1);
114 continue;
115 case NO_ERROR:
116 if (server != nullptr) {
117 *server = s;
118 }
119 return NO_ERROR;
120 default:
121 return err;
122 }
123 }
124 return TIMED_OUT;
125}
126
Peng Xu2576cb62016-01-20 00:22:09 -0800127void SensorManager::sensorManagerDied() {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700128 Mutex::Autolock _l(mLock);
129 mSensorServer.clear();
130 free(mSensorList);
Yi Kongd5e079f2018-07-17 16:08:27 -0700131 mSensorList = nullptr;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700132 mSensors.clear();
133}
134
Peng Xu2576cb62016-01-20 00:22:09 -0800135status_t SensorManager::assertStateLocked() {
Aravind Akella8f35ca92015-08-17 15:22:12 -0700136 bool initSensorManager = false;
Yi Kongd5e079f2018-07-17 16:08:27 -0700137 if (mSensorServer == nullptr) {
Aravind Akella8f35ca92015-08-17 15:22:12 -0700138 initSensorManager = true;
139 } else {
140 // Ping binder to check if sensorservice is alive.
141 status_t err = IInterface::asBinder(mSensorServer)->pingBinder();
142 if (err != NO_ERROR) {
143 initSensorManager = true;
144 }
145 }
146 if (initSensorManager) {
Peng Xufe5476a2017-03-17 17:27:42 -0700147 waitForSensorService(&mSensorServer);
148 LOG_ALWAYS_FATAL_IF(mSensorServer == nullptr, "getService(SensorService) NULL");
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700149
150 class DeathObserver : public IBinder::DeathRecipient {
Peng Xu2576cb62016-01-20 00:22:09 -0800151 SensorManager& mSensorManager;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700152 virtual void binderDied(const wp<IBinder>& who) {
Steve Block32397c12012-01-05 23:22:43 +0000153 ALOGW("sensorservice died [%p]", who.unsafe_get());
Peng Xu2576cb62016-01-20 00:22:09 -0800154 mSensorManager.sensorManagerDied();
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700155 }
156 public:
Chih-Hung Hsieh68a593e2016-04-28 09:14:32 -0700157 explicit DeathObserver(SensorManager& mgr) : mSensorManager(mgr) { }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700158 };
159
160 mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800161 IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700162
Svetoslavb412f6e2015-04-29 16:50:41 -0700163 mSensors = mSensorServer->getSensorList(mOpPackageName);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700164 size_t count = mSensors.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800165 mSensorList =
166 static_cast<Sensor const**>(malloc(count * sizeof(Sensor*)));
Yi Kongd5e079f2018-07-17 16:08:27 -0700167 LOG_ALWAYS_FATAL_IF(mSensorList == nullptr, "mSensorList NULL");
Aravind Akella8f35ca92015-08-17 15:22:12 -0700168
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700169 for (size_t i=0 ; i<count ; i++) {
170 mSensorList[i] = mSensors.array() + i;
171 }
172 }
173
174 return NO_ERROR;
175}
176
Peng Xu2576cb62016-01-20 00:22:09 -0800177ssize_t SensorManager::getSensorList(Sensor const* const** list) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700178 Mutex::Autolock _l(mLock);
179 status_t err = assertStateLocked();
180 if (err < 0) {
Dan Stozad723bd72014-11-18 10:24:03 -0800181 return static_cast<ssize_t>(err);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700182 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700183 *list = mSensorList;
Dan Stozad723bd72014-11-18 10:24:03 -0800184 return static_cast<ssize_t>(mSensors.size());
Mathias Agopian589ce852010-07-13 22:21:56 -0700185}
186
Peng Xu2576cb62016-01-20 00:22:09 -0800187ssize_t SensorManager::getDynamicSensorList(Vector<Sensor> & dynamicSensors) {
188 Mutex::Autolock _l(mLock);
189 status_t err = assertStateLocked();
190 if (err < 0) {
191 return static_cast<ssize_t>(err);
192 }
193
194 dynamicSensors = mSensorServer->getDynamicSensorList(mOpPackageName);
195 size_t count = dynamicSensors.size();
196
197 return static_cast<ssize_t>(count);
198}
199
Mathias Agopiana7352c92010-07-14 23:41:37 -0700200Sensor const* SensorManager::getDefaultSensor(int type)
Mathias Agopian589ce852010-07-13 22:21:56 -0700201{
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700202 Mutex::Autolock _l(mLock);
203 if (assertStateLocked() == NO_ERROR) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700204 bool wakeUpSensor = false;
205 // For the following sensor types, return a wake-up sensor. These types are by default
206 // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return
207 // a non_wake-up version.
208 if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION ||
209 type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE ||
Nick Vaccaro5e7f79b2016-10-17 15:40:51 -0700210 type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE ||
Nick Vaccaro2e990eb2017-01-12 21:13:58 -0800211 type == SENSOR_TYPE_WRIST_TILT_GESTURE ||
212 type == SENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700213 wakeUpSensor = true;
214 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700215 // For now we just return the first sensor of that type we find.
216 // in the future it will make sense to let the SensorService make
217 // that decision.
218 for (size_t i=0 ; i<mSensors.size() ; i++) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700219 if (mSensorList[i]->getType() == type &&
220 mSensorList[i]->isWakeUpSensor() == wakeUpSensor) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700221 return mSensorList[i];
Aravind Akellab37ba392014-08-05 14:53:07 -0700222 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700223 }
Mathias Agopiana7352c92010-07-14 23:41:37 -0700224 }
Yi Kongd5e079f2018-07-17 16:08:27 -0700225 return nullptr;
Mathias Agopian589ce852010-07-13 22:21:56 -0700226}
227
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700228sp<SensorEventQueue> SensorManager::createEventQueue(String8 packageName, int mode) {
Mathias Agopianbe58de02011-10-16 00:38:30 -0700229 sp<SensorEventQueue> queue;
230
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700231 Mutex::Autolock _l(mLock);
232 while (assertStateLocked() == NO_ERROR) {
233 sp<ISensorEventConnection> connection =
Svetoslavb412f6e2015-04-29 16:50:41 -0700234 mSensorServer->createSensorEventConnection(packageName, mode, mOpPackageName);
Yi Kongd5e079f2018-07-17 16:08:27 -0700235 if (connection == nullptr) {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700236 // SensorService just died or the app doesn't have required permissions.
237 ALOGE("createEventQueue: connection is NULL.");
Yi Kongd5e079f2018-07-17 16:08:27 -0700238 return nullptr;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700239 }
240 queue = new SensorEventQueue(connection);
241 break;
Mathias Agopianbe58de02011-10-16 00:38:30 -0700242 }
Mathias Agopianbe58de02011-10-16 00:38:30 -0700243 return queue;
Mathias Agopian589ce852010-07-13 22:21:56 -0700244}
245
Aravind Akella841a5922015-06-29 12:37:48 -0700246bool SensorManager::isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700247 Mutex::Autolock _l(mLock);
248 if (assertStateLocked() == NO_ERROR) {
Aravind Akella841a5922015-06-29 12:37:48 -0700249 return mSensorServer->isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700250 }
Aravind Akella841a5922015-06-29 12:37:48 -0700251 return false;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700252}
253
Peng Xue36e3472016-11-03 11:57:10 -0700254int SensorManager::createDirectChannel(
255 size_t size, int channelType, const native_handle_t *resourceHandle) {
256 Mutex::Autolock _l(mLock);
257 if (assertStateLocked() != NO_ERROR) {
258 return NO_INIT;
259 }
260
Peng Xud9c8a862017-03-05 01:48:11 -0800261 if (channelType != SENSOR_DIRECT_MEM_TYPE_ASHMEM
262 && channelType != SENSOR_DIRECT_MEM_TYPE_GRALLOC) {
263 ALOGE("Bad channel shared memory type %d", channelType);
264 return BAD_VALUE;
Peng Xue36e3472016-11-03 11:57:10 -0700265 }
Peng Xud9c8a862017-03-05 01:48:11 -0800266
267 sp<ISensorEventConnection> conn =
268 mSensorServer->createSensorDirectConnection(mOpPackageName,
269 static_cast<uint32_t>(size),
270 static_cast<int32_t>(channelType),
271 SENSOR_DIRECT_FMT_SENSORS_EVENT, resourceHandle);
272 if (conn == nullptr) {
273 return NO_MEMORY;
274 }
275
276 int nativeHandle = mDirectConnectionHandle++;
277 mDirectConnection.emplace(nativeHandle, conn);
278 return nativeHandle;
Peng Xue36e3472016-11-03 11:57:10 -0700279}
280
281void SensorManager::destroyDirectChannel(int channelNativeHandle) {
282 Mutex::Autolock _l(mLock);
283 if (assertStateLocked() == NO_ERROR) {
284 mDirectConnection.erase(channelNativeHandle);
285 }
286}
287
288int SensorManager::configureDirectChannel(int channelNativeHandle, int sensorHandle, int rateLevel) {
289 Mutex::Autolock _l(mLock);
290 if (assertStateLocked() != NO_ERROR) {
291 return NO_INIT;
292 }
293
294 auto i = mDirectConnection.find(channelNativeHandle);
295 if (i == mDirectConnection.end()) {
296 ALOGE("Cannot find the handle in client direct connection table");
297 return BAD_VALUE;
298 }
299
300 int ret;
301 ret = i->second->configureChannel(sensorHandle, rateLevel);
302 ALOGE_IF(ret < 0, "SensorManager::configureChannel (%d, %d) returns %d",
303 static_cast<int>(sensorHandle), static_cast<int>(rateLevel),
304 static_cast<int>(ret));
305 return ret;
306}
307
Peng Xudd5c5cb2017-03-16 17:39:43 -0700308int SensorManager::setOperationParameter(
Alexey Polyudov88711e82017-05-23 19:54:04 -0700309 int handle, int type,
310 const Vector<float> &floats, const Vector<int32_t> &ints) {
Peng Xudd5c5cb2017-03-16 17:39:43 -0700311 Mutex::Autolock _l(mLock);
312 if (assertStateLocked() != NO_ERROR) {
313 return NO_INIT;
314 }
Alexey Polyudov88711e82017-05-23 19:54:04 -0700315 return mSensorServer->setOperationParameter(handle, type, floats, ints);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700316}
317
Mathias Agopian589ce852010-07-13 22:21:56 -0700318// ----------------------------------------------------------------------------
319}; // namespace android