Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 1 | /* |
| 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 Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 17 | #define LOG_TAG "Sensors" |
| 18 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 19 | #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 Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 26 | #include <binder/IBinder.h> |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 27 | #include <binder/IServiceManager.h> |
| 28 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 29 | #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 | // ---------------------------------------------------------------------------- |
| 36 | namespace android { |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 39 | SensorManager::SensorManager(const String16& opPackageName) |
| 40 | : mSensorList(0), mOpPackageName(opPackageName) |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 41 | { |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 42 | // okay we're not locked here, but it's not needed during construction |
| 43 | assertStateLocked(); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | SensorManager::~SensorManager() |
| 47 | { |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 48 | free(mSensorList); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 51 | void SensorManager::sensorManagerDied() |
| 52 | { |
| 53 | Mutex::Autolock _l(mLock); |
| 54 | mSensorServer.clear(); |
| 55 | free(mSensorList); |
| 56 | mSensorList = NULL; |
| 57 | mSensors.clear(); |
| 58 | } |
| 59 | |
| 60 | status_t SensorManager::assertStateLocked() const { |
| 61 | if (mSensorServer == NULL) { |
Narayan Kamath | 8034fc6 | 2015-07-29 09:36:05 +0000 | [diff] [blame] | 62 | // try for one second |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 63 | const String16 name("sensorservice"); |
Narayan Kamath | 8034fc6 | 2015-07-29 09:36:05 +0000 | [diff] [blame] | 64 | for (int i=0 ; i<4 ; i++) { |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 65 | status_t err = getService(name, &mSensorServer); |
| 66 | if (err == NAME_NOT_FOUND) { |
Narayan Kamath | 8034fc6 | 2015-07-29 09:36:05 +0000 | [diff] [blame] | 67 | usleep(250000); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 68 | continue; |
| 69 | } |
| 70 | if (err != NO_ERROR) { |
| 71 | return err; |
| 72 | } |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | class DeathObserver : public IBinder::DeathRecipient { |
| 77 | SensorManager& mSensorManger; |
| 78 | virtual void binderDied(const wp<IBinder>& who) { |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 79 | ALOGW("sensorservice died [%p]", who.unsafe_get()); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 80 | mSensorManger.sensorManagerDied(); |
| 81 | } |
| 82 | public: |
| 83 | DeathObserver(SensorManager& mgr) : mSensorManger(mgr) { } |
| 84 | }; |
| 85 | |
| 86 | mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this)); |
Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 87 | IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 88 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 89 | mSensors = mSensorServer->getSensorList(mOpPackageName); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 90 | size_t count = mSensors.size(); |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 91 | mSensorList = |
| 92 | static_cast<Sensor const**>(malloc(count * sizeof(Sensor*))); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 93 | for (size_t i=0 ; i<count ; i++) { |
| 94 | mSensorList[i] = mSensors.array() + i; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return NO_ERROR; |
| 99 | } |
| 100 | |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 101 | ssize_t SensorManager::getSensorList(Sensor const* const** list) const |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 102 | { |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 103 | Mutex::Autolock _l(mLock); |
| 104 | status_t err = assertStateLocked(); |
| 105 | if (err < 0) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 106 | return static_cast<ssize_t>(err); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 107 | } |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 108 | *list = mSensorList; |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 109 | return static_cast<ssize_t>(mSensors.size()); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 112 | Sensor const* SensorManager::getDefaultSensor(int type) |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 113 | { |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 114 | Mutex::Autolock _l(mLock); |
| 115 | if (assertStateLocked() == NO_ERROR) { |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 116 | bool wakeUpSensor = false; |
| 117 | // For the following sensor types, return a wake-up sensor. These types are by default |
| 118 | // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return |
| 119 | // a non_wake-up version. |
| 120 | if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION || |
| 121 | type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE || |
| 122 | type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE) { |
| 123 | wakeUpSensor = true; |
| 124 | } |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 125 | // For now we just return the first sensor of that type we find. |
| 126 | // in the future it will make sense to let the SensorService make |
| 127 | // that decision. |
| 128 | for (size_t i=0 ; i<mSensors.size() ; i++) { |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 129 | if (mSensorList[i]->getType() == type && |
| 130 | mSensorList[i]->isWakeUpSensor() == wakeUpSensor) { |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 131 | return mSensorList[i]; |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 132 | } |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 133 | } |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 134 | } |
| 135 | return NULL; |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 138 | sp<SensorEventQueue> SensorManager::createEventQueue(String8 packageName, int mode) { |
Mathias Agopian | be58de0 | 2011-10-16 00:38:30 -0700 | [diff] [blame] | 139 | sp<SensorEventQueue> queue; |
| 140 | |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 141 | Mutex::Autolock _l(mLock); |
| 142 | while (assertStateLocked() == NO_ERROR) { |
| 143 | sp<ISensorEventConnection> connection = |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 144 | mSensorServer->createSensorEventConnection(packageName, mode, mOpPackageName); |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 145 | if (connection == NULL) { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 146 | // SensorService just died or the app doesn't have required permissions. |
| 147 | ALOGE("createEventQueue: connection is NULL."); |
| 148 | return NULL; |
Mathias Agopian | 1a2b83a | 2011-10-16 22:15:23 -0700 | [diff] [blame] | 149 | } |
| 150 | queue = new SensorEventQueue(connection); |
| 151 | break; |
Mathias Agopian | be58de0 | 2011-10-16 00:38:30 -0700 | [diff] [blame] | 152 | } |
Mathias Agopian | be58de0 | 2011-10-16 00:38:30 -0700 | [diff] [blame] | 153 | return queue; |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 156 | bool SensorManager::isDataInjectionEnabled() { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 157 | Mutex::Autolock _l(mLock); |
| 158 | if (assertStateLocked() == NO_ERROR) { |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 159 | return mSensorServer->isDataInjectionEnabled(); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 160 | } |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 161 | return false; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 164 | // ---------------------------------------------------------------------------- |
| 165 | }; // namespace android |