blob: ead535e637d6909ababe7eafb31f3b3565ef881a [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
22#include <utils/Errors.h>
23#include <utils/RefBase.h>
24#include <utils/Singleton.h>
25
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070026#include <binder/IBinder.h>
Mathias Agopiana7352c92010-07-14 23:41:37 -070027#include <binder/IServiceManager.h>
28
Mathias Agopian589ce852010-07-13 22:21:56 -070029#include <gui/ISensorServer.h>
30#include <gui/ISensorEventConnection.h>
31#include <gui/Sensor.h>
32#include <gui/SensorManager.h>
33#include <gui/SensorEventQueue.h>
34
35// ----------------------------------------------------------------------------
36namespace android {
37// ----------------------------------------------------------------------------
38
Svetoslavb412f6e2015-04-29 16:50:41 -070039SensorManager::SensorManager(const String16& opPackageName)
40 : mSensorList(0), mOpPackageName(opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070041{
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070042 // okay we're not locked here, but it's not needed during construction
43 assertStateLocked();
Mathias Agopian589ce852010-07-13 22:21:56 -070044}
45
46SensorManager::~SensorManager()
47{
Mathias Agopiana7352c92010-07-14 23:41:37 -070048 free(mSensorList);
Mathias Agopian589ce852010-07-13 22:21:56 -070049}
50
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070051void SensorManager::sensorManagerDied()
52{
53 Mutex::Autolock _l(mLock);
54 mSensorServer.clear();
55 free(mSensorList);
56 mSensorList = NULL;
57 mSensors.clear();
58}
59
60status_t SensorManager::assertStateLocked() const {
61 if (mSensorServer == NULL) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070062 const String16 name("sensorservice");
Aravind Akella869eb202015-07-24 11:08:41 -070063 // try 10 times before giving up ...
64 for (int i = 0; i < 10; ++i) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070065 status_t err = getService(name, &mSensorServer);
66 if (err == NAME_NOT_FOUND) {
Aravind Akella869eb202015-07-24 11:08:41 -070067 // Sleep for 1 second before retrying.
68 sleep(1);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070069 continue;
70 }
71 if (err != NO_ERROR) {
72 return err;
73 }
74 break;
75 }
76
Aravind Akella869eb202015-07-24 11:08:41 -070077 if (mSensorServer == NULL) {
78 ALOGE("FATAL getsensorservice returned` NULL");
79 }
80
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070081 class DeathObserver : public IBinder::DeathRecipient {
82 SensorManager& mSensorManger;
83 virtual void binderDied(const wp<IBinder>& who) {
Steve Block32397c12012-01-05 23:22:43 +000084 ALOGW("sensorservice died [%p]", who.unsafe_get());
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070085 mSensorManger.sensorManagerDied();
86 }
87 public:
88 DeathObserver(SensorManager& mgr) : mSensorManger(mgr) { }
89 };
90
91 mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -080092 IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070093
Svetoslavb412f6e2015-04-29 16:50:41 -070094 mSensors = mSensorServer->getSensorList(mOpPackageName);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070095 size_t count = mSensors.size();
Dan Stozad723bd72014-11-18 10:24:03 -080096 mSensorList =
97 static_cast<Sensor const**>(malloc(count * sizeof(Sensor*)));
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070098 for (size_t i=0 ; i<count ; i++) {
99 mSensorList[i] = mSensors.array() + i;
100 }
101 }
102
103 return NO_ERROR;
104}
105
Mathias Agopiana7352c92010-07-14 23:41:37 -0700106ssize_t SensorManager::getSensorList(Sensor const* const** list) const
Mathias Agopian589ce852010-07-13 22:21:56 -0700107{
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700108 Mutex::Autolock _l(mLock);
109 status_t err = assertStateLocked();
110 if (err < 0) {
Dan Stozad723bd72014-11-18 10:24:03 -0800111 return static_cast<ssize_t>(err);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700112 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700113 *list = mSensorList;
Dan Stozad723bd72014-11-18 10:24:03 -0800114 return static_cast<ssize_t>(mSensors.size());
Mathias Agopian589ce852010-07-13 22:21:56 -0700115}
116
Mathias Agopiana7352c92010-07-14 23:41:37 -0700117Sensor const* SensorManager::getDefaultSensor(int type)
Mathias Agopian589ce852010-07-13 22:21:56 -0700118{
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700119 Mutex::Autolock _l(mLock);
120 if (assertStateLocked() == NO_ERROR) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700121 bool wakeUpSensor = false;
122 // For the following sensor types, return a wake-up sensor. These types are by default
123 // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return
124 // a non_wake-up version.
125 if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION ||
126 type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE ||
127 type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE) {
128 wakeUpSensor = true;
129 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700130 // For now we just return the first sensor of that type we find.
131 // in the future it will make sense to let the SensorService make
132 // that decision.
133 for (size_t i=0 ; i<mSensors.size() ; i++) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700134 if (mSensorList[i]->getType() == type &&
135 mSensorList[i]->isWakeUpSensor() == wakeUpSensor) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700136 return mSensorList[i];
Aravind Akellab37ba392014-08-05 14:53:07 -0700137 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700138 }
Mathias Agopiana7352c92010-07-14 23:41:37 -0700139 }
140 return NULL;
Mathias Agopian589ce852010-07-13 22:21:56 -0700141}
142
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700143sp<SensorEventQueue> SensorManager::createEventQueue(String8 packageName, int mode) {
Mathias Agopianbe58de02011-10-16 00:38:30 -0700144 sp<SensorEventQueue> queue;
145
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700146 Mutex::Autolock _l(mLock);
147 while (assertStateLocked() == NO_ERROR) {
148 sp<ISensorEventConnection> connection =
Svetoslavb412f6e2015-04-29 16:50:41 -0700149 mSensorServer->createSensorEventConnection(packageName, mode, mOpPackageName);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700150 if (connection == NULL) {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700151 // SensorService just died or the app doesn't have required permissions.
152 ALOGE("createEventQueue: connection is NULL.");
153 return NULL;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700154 }
155 queue = new SensorEventQueue(connection);
156 break;
Mathias Agopianbe58de02011-10-16 00:38:30 -0700157 }
Mathias Agopianbe58de02011-10-16 00:38:30 -0700158 return queue;
Mathias Agopian589ce852010-07-13 22:21:56 -0700159}
160
Aravind Akella841a5922015-06-29 12:37:48 -0700161bool SensorManager::isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700162 Mutex::Autolock _l(mLock);
163 if (assertStateLocked() == NO_ERROR) {
Aravind Akella841a5922015-06-29 12:37:48 -0700164 return mSensorServer->isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700165 }
Aravind Akella841a5922015-06-29 12:37:48 -0700166 return false;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700167}
168
Mathias Agopian589ce852010-07-13 22:21:56 -0700169// ----------------------------------------------------------------------------
170}; // namespace android