Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [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 | |
| 17 | #include <stdint.h> |
| 18 | #include <math.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <utils/Atomic.h> |
| 22 | #include <utils/Errors.h> |
| 23 | #include <utils/Singleton.h> |
| 24 | |
| 25 | #include <binder/BinderService.h> |
| 26 | #include <binder/Parcel.h> |
| 27 | #include <binder/IServiceManager.h> |
| 28 | |
| 29 | #include <hardware/sensors.h> |
| 30 | |
| 31 | #include "SensorDevice.h" |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 32 | #include "SensorService.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | // --------------------------------------------------------------------------- |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 36 | |
| 37 | ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice) |
| 38 | |
| 39 | SensorDevice::SensorDevice() |
| 40 | : mSensorDevice(0), |
| 41 | mSensorModule(0) |
| 42 | { |
| 43 | status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, |
| 44 | (hw_module_t const**)&mSensorModule); |
| 45 | |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 46 | ALOGE_IF(err, "couldn't load %s module (%s)", |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 47 | SENSORS_HARDWARE_MODULE_ID, strerror(-err)); |
| 48 | |
| 49 | if (mSensorModule) { |
| 50 | err = sensors_open(&mSensorModule->common, &mSensorDevice); |
| 51 | |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 52 | ALOGE_IF(err, "couldn't open device for module %s (%s)", |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 53 | SENSORS_HARDWARE_MODULE_ID, strerror(-err)); |
| 54 | |
| 55 | if (mSensorDevice) { |
| 56 | sensor_t const* list; |
| 57 | ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list); |
| 58 | mActivationCount.setCapacity(count); |
| 59 | Info model; |
| 60 | for (size_t i=0 ; i<size_t(count) ; i++) { |
| 61 | mActivationCount.add(list[i].handle, model); |
| 62 | mSensorDevice->activate(mSensorDevice, list[i].handle, 0); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void SensorDevice::dump(String8& result, char* buffer, size_t SIZE) |
| 69 | { |
| 70 | if (!mSensorModule) return; |
| 71 | sensor_t const* list; |
| 72 | ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list); |
| 73 | |
| 74 | snprintf(buffer, SIZE, "%d h/w sensors:\n", int(count)); |
| 75 | result.append(buffer); |
| 76 | |
| 77 | Mutex::Autolock _l(mLock); |
| 78 | for (size_t i=0 ; i<size_t(count) ; i++) { |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 79 | const Info& info = mActivationCount.valueFor(list[i].handle); |
| 80 | snprintf(buffer, SIZE, "handle=0x%08x, active-count=%d, rates(ms)={ ", |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 81 | list[i].handle, |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 82 | info.rates.size()); |
| 83 | result.append(buffer); |
| 84 | for (size_t j=0 ; j<info.rates.size() ; j++) { |
| 85 | snprintf(buffer, SIZE, "%4.1f%s", |
| 86 | info.rates.valueAt(j) / 1e6f, |
| 87 | j<info.rates.size()-1 ? ", " : ""); |
| 88 | result.append(buffer); |
| 89 | } |
| 90 | snprintf(buffer, SIZE, " }, selected=%4.1f ms\n", info.delay / 1e6f); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 91 | result.append(buffer); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | ssize_t SensorDevice::getSensorList(sensor_t const** list) { |
| 96 | if (!mSensorModule) return NO_INIT; |
| 97 | ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list); |
| 98 | return count; |
| 99 | } |
| 100 | |
| 101 | status_t SensorDevice::initCheck() const { |
| 102 | return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT; |
| 103 | } |
| 104 | |
| 105 | ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) { |
| 106 | if (!mSensorDevice) return NO_INIT; |
Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 107 | ssize_t c; |
| 108 | do { |
| 109 | c = mSensorDevice->poll(mSensorDevice, buffer, count); |
| 110 | } while (c == -EINTR); |
| 111 | return c; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame^] | 114 | void SensorDevice::autoDisable(void *ident, int handle) { |
| 115 | Info& info( mActivationCount.editValueFor(handle) ); |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 116 | Mutex::Autolock _l(mLock); |
| 117 | info.rates.removeItem(ident); |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 120 | status_t SensorDevice::activate(void* ident, int handle, int enabled) |
| 121 | { |
| 122 | if (!mSensorDevice) return NO_INIT; |
| 123 | status_t err(NO_ERROR); |
| 124 | bool actuateHardware = false; |
| 125 | |
| 126 | Info& info( mActivationCount.editValueFor(handle) ); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 127 | |
| 128 | |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 129 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 130 | "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%d", |
| 131 | ident, handle, enabled, info.rates.size()); |
| 132 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 133 | if (enabled) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 134 | Mutex::Autolock _l(mLock); |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 135 | ALOGD_IF(DEBUG_CONNECTIONS, "... index=%ld", |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 136 | info.rates.indexOfKey(ident)); |
| 137 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 138 | if (info.rates.indexOfKey(ident) < 0) { |
| 139 | info.rates.add(ident, DEFAULT_EVENTS_PERIOD); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 140 | if (info.rates.size() == 1) { |
| 141 | actuateHardware = true; |
| 142 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 143 | } else { |
| 144 | // sensor was already activated for this ident |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 145 | } |
| 146 | } else { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 147 | Mutex::Autolock _l(mLock); |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 148 | ALOGD_IF(DEBUG_CONNECTIONS, "... index=%ld", |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 149 | info.rates.indexOfKey(ident)); |
| 150 | |
| 151 | ssize_t idx = info.rates.removeItem(ident); |
| 152 | if (idx >= 0) { |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 153 | if (info.rates.size() == 0) { |
| 154 | actuateHardware = true; |
| 155 | } |
| 156 | } else { |
| 157 | // sensor wasn't enabled for this ident |
| 158 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 159 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 160 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 161 | if (actuateHardware) { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 162 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w"); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 163 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 164 | err = mSensorDevice->activate(mSensorDevice, handle, enabled); |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 165 | ALOGE_IF(err, "Error %s sensor %d (%s)", |
| 166 | enabled ? "activating" : "disabling", |
| 167 | handle, strerror(-err)); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame^] | 168 | |
| 169 | if (err != NO_ERROR) { |
| 170 | // clean-up on failure |
| 171 | if (enabled) { |
| 172 | // failure when enabling the sensor |
| 173 | Mutex::Autolock _l(mLock); |
| 174 | info.rates.removeItem(ident); |
| 175 | } |
| 176 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 179 | { // scope for the lock |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 180 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 181 | nsecs_t ns = info.selectDelay(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 182 | mSensorDevice->setDelay(mSensorDevice, handle, ns); |
| 183 | } |
| 184 | |
| 185 | return err; |
| 186 | } |
| 187 | |
| 188 | status_t SensorDevice::setDelay(void* ident, int handle, int64_t ns) |
| 189 | { |
| 190 | if (!mSensorDevice) return NO_INIT; |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 191 | Mutex::Autolock _l(mLock); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 192 | Info& info( mActivationCount.editValueFor(handle) ); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 193 | status_t err = info.setDelayForIdent(ident, ns); |
| 194 | if (err < 0) return err; |
| 195 | ns = info.selectDelay(); |
| 196 | return mSensorDevice->setDelay(mSensorDevice, handle, ns); |
| 197 | } |
| 198 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 199 | int SensorDevice::getHalDeviceVersion() const { |
| 200 | if (!mSensorDevice) return -1; |
| 201 | |
| 202 | return mSensorDevice->common.version; |
| 203 | } |
| 204 | |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 205 | // --------------------------------------------------------------------------- |
| 206 | |
| 207 | status_t SensorDevice::Info::setDelayForIdent(void* ident, int64_t ns) |
| 208 | { |
| 209 | ssize_t index = rates.indexOfKey(ident); |
| 210 | if (index < 0) { |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 211 | ALOGE("Info::setDelayForIdent(ident=%p, ns=%lld) failed (%s)", |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 212 | ident, ns, strerror(-index)); |
| 213 | return BAD_INDEX; |
| 214 | } |
| 215 | rates.editValueAt(index) = ns; |
| 216 | return NO_ERROR; |
| 217 | } |
| 218 | |
| 219 | nsecs_t SensorDevice::Info::selectDelay() |
| 220 | { |
| 221 | nsecs_t ns = rates.valueAt(0); |
| 222 | for (size_t i=1 ; i<rates.size() ; i++) { |
| 223 | nsecs_t cur = rates.valueAt(i); |
| 224 | if (cur < ns) { |
| 225 | ns = cur; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 226 | } |
| 227 | } |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 228 | delay = ns; |
| 229 | return ns; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // --------------------------------------------------------------------------- |
| 233 | }; // namespace android |
| 234 | |