blob: 8e0ea6ce8cedbd90a6102594999210b1109133ac [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);
Erik Staatsd35a5742022-02-04 06:37:58 -0800103 free(mDynamicSensorList);
Mathias Agopian589ce852010-07-13 22:21:56 -0700104}
105
Peng Xufe5476a2017-03-17 17:27:42 -0700106status_t SensorManager::waitForSensorService(sp<ISensorServer> *server) {
107 // try for 300 seconds (60*5(getService() tries for 5 seconds)) before giving up ...
108 sp<ISensorServer> s;
109 const String16 name("sensorservice");
110 for (int i = 0; i < 60; i++) {
111 status_t err = getService(name, &s);
112 switch (err) {
113 case NAME_NOT_FOUND:
114 sleep(1);
115 continue;
116 case NO_ERROR:
117 if (server != nullptr) {
118 *server = s;
119 }
120 return NO_ERROR;
121 default:
122 return err;
123 }
124 }
125 return TIMED_OUT;
126}
127
Peng Xu2576cb62016-01-20 00:22:09 -0800128void SensorManager::sensorManagerDied() {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700129 Mutex::Autolock _l(mLock);
130 mSensorServer.clear();
131 free(mSensorList);
Yi Kongd5e079f2018-07-17 16:08:27 -0700132 mSensorList = nullptr;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700133 mSensors.clear();
Erik Staatsd35a5742022-02-04 06:37:58 -0800134 free(mDynamicSensorList);
135 mDynamicSensorList = nullptr;
136 mDynamicSensors.clear();
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700137}
138
Peng Xu2576cb62016-01-20 00:22:09 -0800139status_t SensorManager::assertStateLocked() {
Aravind Akella8f35ca92015-08-17 15:22:12 -0700140 bool initSensorManager = false;
Yi Kongd5e079f2018-07-17 16:08:27 -0700141 if (mSensorServer == nullptr) {
Aravind Akella8f35ca92015-08-17 15:22:12 -0700142 initSensorManager = true;
143 } else {
144 // Ping binder to check if sensorservice is alive.
145 status_t err = IInterface::asBinder(mSensorServer)->pingBinder();
146 if (err != NO_ERROR) {
147 initSensorManager = true;
148 }
149 }
150 if (initSensorManager) {
Peng Xufe5476a2017-03-17 17:27:42 -0700151 waitForSensorService(&mSensorServer);
152 LOG_ALWAYS_FATAL_IF(mSensorServer == nullptr, "getService(SensorService) NULL");
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700153
154 class DeathObserver : public IBinder::DeathRecipient {
Peng Xu2576cb62016-01-20 00:22:09 -0800155 SensorManager& mSensorManager;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700156 virtual void binderDied(const wp<IBinder>& who) {
Yi Kong82d7c632019-09-20 12:10:47 -0700157 ALOGW("sensorservice died [%p]", static_cast<void*>(who.unsafe_get()));
Peng Xu2576cb62016-01-20 00:22:09 -0800158 mSensorManager.sensorManagerDied();
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700159 }
160 public:
Chih-Hung Hsieh68a593e2016-04-28 09:14:32 -0700161 explicit DeathObserver(SensorManager& mgr) : mSensorManager(mgr) { }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700162 };
163
164 mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800165 IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700166
Svetoslavb412f6e2015-04-29 16:50:41 -0700167 mSensors = mSensorServer->getSensorList(mOpPackageName);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700168 size_t count = mSensors.size();
Devin Moored3909792023-02-17 19:35:25 +0000169 if (count == 0) {
170 ALOGE("Failed to get Sensor list");
171 mSensorServer.clear();
172 return UNKNOWN_ERROR;
173 }
Dan Stozad723bd72014-11-18 10:24:03 -0800174 mSensorList =
175 static_cast<Sensor const**>(malloc(count * sizeof(Sensor*)));
Yi Kongd5e079f2018-07-17 16:08:27 -0700176 LOG_ALWAYS_FATAL_IF(mSensorList == nullptr, "mSensorList NULL");
Aravind Akella8f35ca92015-08-17 15:22:12 -0700177
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700178 for (size_t i=0 ; i<count ; i++) {
179 mSensorList[i] = mSensors.array() + i;
180 }
181 }
182
183 return NO_ERROR;
184}
185
Peng Xu2576cb62016-01-20 00:22:09 -0800186ssize_t SensorManager::getSensorList(Sensor const* const** list) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700187 Mutex::Autolock _l(mLock);
188 status_t err = assertStateLocked();
189 if (err < 0) {
Dan Stozad723bd72014-11-18 10:24:03 -0800190 return static_cast<ssize_t>(err);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700191 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700192 *list = mSensorList;
Dan Stozad723bd72014-11-18 10:24:03 -0800193 return static_cast<ssize_t>(mSensors.size());
Mathias Agopian589ce852010-07-13 22:21:56 -0700194}
195
Peng Xu2576cb62016-01-20 00:22:09 -0800196ssize_t SensorManager::getDynamicSensorList(Vector<Sensor> & dynamicSensors) {
197 Mutex::Autolock _l(mLock);
198 status_t err = assertStateLocked();
199 if (err < 0) {
200 return static_cast<ssize_t>(err);
201 }
202
203 dynamicSensors = mSensorServer->getDynamicSensorList(mOpPackageName);
204 size_t count = dynamicSensors.size();
205
206 return static_cast<ssize_t>(count);
207}
208
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200209ssize_t SensorManager::getRuntimeSensorList(int deviceId, Vector<Sensor>& runtimeSensors) {
210 Mutex::Autolock _l(mLock);
211 status_t err = assertStateLocked();
212 if (err < 0) {
213 return static_cast<ssize_t>(err);
214 }
215
216 runtimeSensors = mSensorServer->getRuntimeSensorList(mOpPackageName, deviceId);
217 size_t count = runtimeSensors.size();
218
219 return static_cast<ssize_t>(count);
220}
221
Erik Staatsd35a5742022-02-04 06:37:58 -0800222ssize_t SensorManager::getDynamicSensorList(Sensor const* const** list) {
223 Mutex::Autolock _l(mLock);
224 status_t err = assertStateLocked();
225 if (err < 0) {
226 return static_cast<ssize_t>(err);
227 }
228
229 free(mDynamicSensorList);
230 mDynamicSensorList = nullptr;
231 mDynamicSensors = mSensorServer->getDynamicSensorList(mOpPackageName);
232 size_t dynamicCount = mDynamicSensors.size();
233 if (dynamicCount > 0) {
234 mDynamicSensorList = static_cast<Sensor const**>(
235 malloc(dynamicCount * sizeof(Sensor*)));
236 if (mDynamicSensorList == nullptr) {
237 ALOGE("Failed to allocate dynamic sensor list for %zu sensors.",
238 dynamicCount);
239 return static_cast<ssize_t>(NO_MEMORY);
240 }
241
242 for (size_t i = 0; i < dynamicCount; i++) {
243 mDynamicSensorList[i] = mDynamicSensors.array() + i;
244 }
245 }
246
247 *list = mDynamicSensorList;
248 return static_cast<ssize_t>(mDynamicSensors.size());
249}
250
Mathias Agopiana7352c92010-07-14 23:41:37 -0700251Sensor const* SensorManager::getDefaultSensor(int type)
Mathias Agopian589ce852010-07-13 22:21:56 -0700252{
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700253 Mutex::Autolock _l(mLock);
254 if (assertStateLocked() == NO_ERROR) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700255 bool wakeUpSensor = false;
256 // For the following sensor types, return a wake-up sensor. These types are by default
257 // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return
258 // a non_wake-up version.
259 if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION ||
260 type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE ||
Nick Vaccaro5e7f79b2016-10-17 15:40:51 -0700261 type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE ||
Nick Vaccaro2e990eb2017-01-12 21:13:58 -0800262 type == SENSOR_TYPE_WRIST_TILT_GESTURE ||
Anthony Stangefdb1fc82020-01-16 15:02:48 -0500263 type == SENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT || type == SENSOR_TYPE_HINGE_ANGLE) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700264 wakeUpSensor = true;
265 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700266 // 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++) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700270 if (mSensorList[i]->getType() == type &&
271 mSensorList[i]->isWakeUpSensor() == wakeUpSensor) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700272 return mSensorList[i];
Aravind Akellab37ba392014-08-05 14:53:07 -0700273 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700274 }
Mathias Agopiana7352c92010-07-14 23:41:37 -0700275 }
Yi Kongd5e079f2018-07-17 16:08:27 -0700276 return nullptr;
Mathias Agopian589ce852010-07-13 22:21:56 -0700277}
278
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800279sp<SensorEventQueue> SensorManager::createEventQueue(
280 String8 packageName, int mode, String16 attributionTag) {
Mathias Agopianbe58de02011-10-16 00:38:30 -0700281 sp<SensorEventQueue> queue;
282
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700283 Mutex::Autolock _l(mLock);
284 while (assertStateLocked() == NO_ERROR) {
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800285 sp<ISensorEventConnection> connection = mSensorServer->createSensorEventConnection(
286 packageName, mode, mOpPackageName, attributionTag);
Yi Kongd5e079f2018-07-17 16:08:27 -0700287 if (connection == nullptr) {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700288 // SensorService just died or the app doesn't have required permissions.
289 ALOGE("createEventQueue: connection is NULL.");
Yi Kongd5e079f2018-07-17 16:08:27 -0700290 return nullptr;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700291 }
292 queue = new SensorEventQueue(connection);
293 break;
Mathias Agopianbe58de02011-10-16 00:38:30 -0700294 }
Mathias Agopianbe58de02011-10-16 00:38:30 -0700295 return queue;
Mathias Agopian589ce852010-07-13 22:21:56 -0700296}
297
Aravind Akella841a5922015-06-29 12:37:48 -0700298bool SensorManager::isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700299 Mutex::Autolock _l(mLock);
300 if (assertStateLocked() == NO_ERROR) {
Aravind Akella841a5922015-06-29 12:37:48 -0700301 return mSensorServer->isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700302 }
Aravind Akella841a5922015-06-29 12:37:48 -0700303 return false;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700304}
305
Peng Xue36e3472016-11-03 11:57:10 -0700306int SensorManager::createDirectChannel(
307 size_t size, int channelType, const native_handle_t *resourceHandle) {
308 Mutex::Autolock _l(mLock);
309 if (assertStateLocked() != NO_ERROR) {
310 return NO_INIT;
311 }
312
Peng Xud9c8a862017-03-05 01:48:11 -0800313 if (channelType != SENSOR_DIRECT_MEM_TYPE_ASHMEM
314 && channelType != SENSOR_DIRECT_MEM_TYPE_GRALLOC) {
315 ALOGE("Bad channel shared memory type %d", channelType);
316 return BAD_VALUE;
Peng Xue36e3472016-11-03 11:57:10 -0700317 }
Peng Xud9c8a862017-03-05 01:48:11 -0800318
319 sp<ISensorEventConnection> conn =
320 mSensorServer->createSensorDirectConnection(mOpPackageName,
321 static_cast<uint32_t>(size),
322 static_cast<int32_t>(channelType),
323 SENSOR_DIRECT_FMT_SENSORS_EVENT, resourceHandle);
324 if (conn == nullptr) {
325 return NO_MEMORY;
326 }
327
328 int nativeHandle = mDirectConnectionHandle++;
329 mDirectConnection.emplace(nativeHandle, conn);
330 return nativeHandle;
Peng Xue36e3472016-11-03 11:57:10 -0700331}
332
333void SensorManager::destroyDirectChannel(int channelNativeHandle) {
334 Mutex::Autolock _l(mLock);
335 if (assertStateLocked() == NO_ERROR) {
336 mDirectConnection.erase(channelNativeHandle);
337 }
338}
339
340int SensorManager::configureDirectChannel(int channelNativeHandle, int sensorHandle, int rateLevel) {
341 Mutex::Autolock _l(mLock);
342 if (assertStateLocked() != NO_ERROR) {
343 return NO_INIT;
344 }
345
346 auto i = mDirectConnection.find(channelNativeHandle);
347 if (i == mDirectConnection.end()) {
348 ALOGE("Cannot find the handle in client direct connection table");
349 return BAD_VALUE;
350 }
351
352 int ret;
353 ret = i->second->configureChannel(sensorHandle, rateLevel);
354 ALOGE_IF(ret < 0, "SensorManager::configureChannel (%d, %d) returns %d",
355 static_cast<int>(sensorHandle), static_cast<int>(rateLevel),
356 static_cast<int>(ret));
357 return ret;
358}
359
Peng Xudd5c5cb2017-03-16 17:39:43 -0700360int SensorManager::setOperationParameter(
Alexey Polyudov88711e82017-05-23 19:54:04 -0700361 int handle, int type,
362 const Vector<float> &floats, const Vector<int32_t> &ints) {
Peng Xudd5c5cb2017-03-16 17:39:43 -0700363 Mutex::Autolock _l(mLock);
364 if (assertStateLocked() != NO_ERROR) {
365 return NO_INIT;
366 }
Alexey Polyudov88711e82017-05-23 19:54:04 -0700367 return mSensorServer->setOperationParameter(handle, type, floats, ints);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700368}
369
Mathias Agopian589ce852010-07-13 22:21:56 -0700370// ----------------------------------------------------------------------------
371}; // namespace android