SensorDevice.cpp revision db45861ff4b636b9dd833d622dd64e2ad24b0645
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 <inttypes.h> 18#include <math.h> 19#include <stdint.h> 20#include <sys/types.h> 21 22#include <utils/Atomic.h> 23#include <utils/Errors.h> 24#include <utils/Singleton.h> 25 26#include <binder/BinderService.h> 27#include <binder/Parcel.h> 28#include <binder/IServiceManager.h> 29 30#include <hardware/sensors.h> 31 32#include "SensorDevice.h" 33#include "SensorService.h" 34 35namespace android { 36// --------------------------------------------------------------------------- 37 38ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice) 39 40SensorDevice::SensorDevice() 41 : mSensorDevice(0), 42 mSensorModule(0) 43{ 44 status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, 45 (hw_module_t const**)&mSensorModule); 46 47 ALOGE_IF(err, "couldn't load %s module (%s)", 48 SENSORS_HARDWARE_MODULE_ID, strerror(-err)); 49 50 if (mSensorModule) { 51 err = sensors_open_1(&mSensorModule->common, &mSensorDevice); 52 53 ALOGE_IF(err, "couldn't open device for module %s (%s)", 54 SENSORS_HARDWARE_MODULE_ID, strerror(-err)); 55 56 if (mSensorDevice) { 57 sensor_t const* list; 58 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list); 59 mActivationCount.setCapacity(count); 60 Info model; 61 for (size_t i=0 ; i<size_t(count) ; i++) { 62 mActivationCount.add(list[i].handle, model); 63 mSensorDevice->activate( 64 reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), 65 list[i].handle, 0); 66 } 67 } 68 } 69} 70 71void SensorDevice::dump(String8& result) 72{ 73 if (!mSensorModule) return; 74 sensor_t const* list; 75 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list); 76 77 result.appendFormat("%d h/w sensors:\n", int(count)); 78 79 Mutex::Autolock _l(mLock); 80 for (size_t i=0 ; i<size_t(count) ; i++) { 81 const Info& info = mActivationCount.valueFor(list[i].handle); 82 result.appendFormat("handle=0x%08x, active-count=%zu, batch_period(ms)={ ", list[i].handle, 83 info.batchParams.size()); 84 for (size_t j = 0; j < info.batchParams.size(); j++) { 85 BatchParams params = info.batchParams.valueAt(j); 86 result.appendFormat("%4.1f%s", params.batchDelay / 1e6f, 87 j < info.batchParams.size() - 1 ? ", " : ""); 88 } 89 result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchDelay / 1e6f); 90 91 result.appendFormat("handle=0x%08x, active-count=%zu, batch_timeout(ms)={ ", list[i].handle, 92 info.batchParams.size()); 93 for (size_t j = 0; j < info.batchParams.size(); j++) { 94 BatchParams params = info.batchParams.valueAt(j); 95 result.appendFormat("%4.1f%s", params.batchTimeout / 1e6f, 96 j < info.batchParams.size() - 1 ? ", " : ""); 97 } 98 result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchTimeout / 1e6f); 99 } 100} 101 102ssize_t SensorDevice::getSensorList(sensor_t const** list) { 103 if (!mSensorModule) return NO_INIT; 104 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list); 105 return count; 106} 107 108status_t SensorDevice::initCheck() const { 109 return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT; 110} 111 112ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) { 113 if (!mSensorDevice) return NO_INIT; 114 ssize_t c; 115 do { 116 c = mSensorDevice->poll(reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), 117 buffer, count); 118 } while (c == -EINTR); 119 return c; 120} 121 122void SensorDevice::autoDisable(void *ident, int handle) { 123 Info& info( mActivationCount.editValueFor(handle) ); 124 Mutex::Autolock _l(mLock); 125 info.removeBatchParamsForIdent(ident); 126} 127 128status_t SensorDevice::activate(void* ident, int handle, int enabled) 129{ 130 if (!mSensorDevice) return NO_INIT; 131 status_t err(NO_ERROR); 132 bool actuateHardware = false; 133 134 Mutex::Autolock _l(mLock); 135 Info& info( mActivationCount.editValueFor(handle) ); 136 137 ALOGD_IF(DEBUG_CONNECTIONS, 138 "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu", 139 ident, handle, enabled, info.batchParams.size()); 140 141 if (enabled) { 142 ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident)); 143 144 if (info.batchParams.indexOfKey(ident) >= 0) { 145 if (info.batchParams.size() == 1) { 146 // This is the first connection, we need to activate the underlying h/w sensor. 147 actuateHardware = true; 148 } 149 } else { 150 // Log error. Every activate call should be preceded by a batch() call. 151 ALOGE("\t >>>ERROR: activate called without batch"); 152 } 153 } else { 154 ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident)); 155 156 if (info.removeBatchParamsForIdent(ident) >= 0) { 157 if (info.batchParams.size() == 0) { 158 // This is the last connection, we need to de-activate the underlying h/w sensor. 159 actuateHardware = true; 160 } else { 161 const int halVersion = getHalDeviceVersion(); 162 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) { 163 // Call batch for this sensor with the previously calculated best effort 164 // batch_rate and timeout. One of the apps has unregistered for sensor 165 // events, and the best effort batch parameters might have changed. 166 ALOGD_IF(DEBUG_CONNECTIONS, 167 "\t>>> actuating h/w batch %d %d %" PRId64 " %" PRId64, handle, 168 info.bestBatchParams.flags, info.bestBatchParams.batchDelay, 169 info.bestBatchParams.batchTimeout); 170 mSensorDevice->batch(mSensorDevice, handle,info.bestBatchParams.flags, 171 info.bestBatchParams.batchDelay, 172 info.bestBatchParams.batchTimeout); 173 } 174 } 175 } else { 176 // sensor wasn't enabled for this ident 177 } 178 } 179 180 if (actuateHardware) { 181 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle, enabled); 182 err = mSensorDevice->activate( 183 reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), handle, enabled); 184 ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle, 185 strerror(-err)); 186 187 if (err != NO_ERROR && enabled) { 188 // Failure when enabling the sensor. Clean up on failure. 189 info.removeBatchParamsForIdent(ident); 190 } 191 } 192 193 // On older devices which do not support batch, call setDelay(). 194 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1 && info.batchParams.size() > 0) { 195 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w setDelay %d %" PRId64, handle, 196 info.bestBatchParams.batchDelay); 197 mSensorDevice->setDelay( 198 reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), 199 handle, info.bestBatchParams.batchDelay); 200 } 201 return err; 202} 203 204status_t SensorDevice::batch(void* ident, int handle, int flags, int64_t samplingPeriodNs, 205 int64_t maxBatchReportLatencyNs) { 206 if (!mSensorDevice) return NO_INIT; 207 208 if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { 209 samplingPeriodNs = MINIMUM_EVENTS_PERIOD; 210 } 211 212 const int halVersion = getHalDeviceVersion(); 213 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) { 214 if (flags & SENSORS_BATCH_DRY_RUN) { 215 return mSensorDevice->batch(mSensorDevice, handle, flags, samplingPeriodNs, 216 maxBatchReportLatencyNs); 217 } else { 218 // Call h/w with dry run to see if the given parameters are feasible or not. Return if 219 // there is an error. 220 status_t errDryRun(NO_ERROR); 221 errDryRun = mSensorDevice->batch(mSensorDevice, handle, flags | SENSORS_BATCH_DRY_RUN, 222 samplingPeriodNs, maxBatchReportLatencyNs); 223 if (errDryRun != NO_ERROR) { 224 ALOGD_IF(DEBUG_CONNECTIONS, "SensorDevice::batch dry run error %s", 225 strerror(-errDryRun)); 226 return errDryRun; 227 } 228 } 229 } else if (maxBatchReportLatencyNs != 0) { 230 // Batch is not supported on older devices. 231 return INVALID_OPERATION; 232 } 233 234 ALOGD_IF(DEBUG_CONNECTIONS, 235 "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64, 236 ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); 237 238 Mutex::Autolock _l(mLock); 239 Info& info(mActivationCount.editValueFor(handle)); 240 241 if (info.batchParams.indexOfKey(ident) < 0) { 242 BatchParams params(flags, samplingPeriodNs, maxBatchReportLatencyNs); 243 info.batchParams.add(ident, params); 244 } else { 245 // A batch has already been called with this ident. Update the batch parameters. 246 info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs); 247 } 248 249 BatchParams prevBestBatchParams = info.bestBatchParams; 250 // Find the minimum of all timeouts and batch_rates for this sensor. 251 info.selectBatchParams(); 252 253 ALOGD_IF(DEBUG_CONNECTIONS, 254 "\t>>> curr_period=%" PRId64 " min_period=%" PRId64 255 " curr_timeout=%" PRId64 " min_timeout=%" PRId64, 256 prevBestBatchParams.batchDelay, info.bestBatchParams.batchDelay, 257 prevBestBatchParams.batchTimeout, info.bestBatchParams.batchTimeout); 258 259 status_t err(NO_ERROR); 260 // If the min period or min timeout has changed since the last batch call, call batch. 261 if (prevBestBatchParams != info.bestBatchParams) { 262 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) { 263 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH %d %d %" PRId64 " %" PRId64, handle, 264 info.bestBatchParams.flags, info.bestBatchParams.batchDelay, 265 info.bestBatchParams.batchTimeout); 266 err = mSensorDevice->batch(mSensorDevice, handle, info.bestBatchParams.flags, 267 info.bestBatchParams.batchDelay, 268 info.bestBatchParams.batchTimeout); 269 } else { 270 // For older devices which do not support batch, call setDelay() after activate() is 271 // called. Some older devices may not support calling setDelay before activate(), so 272 // call setDelay in SensorDevice::activate() method. 273 } 274 if (err != NO_ERROR) { 275 ALOGE("sensor batch failed %p %d %d %" PRId64 " %" PRId64 " err=%s", 276 mSensorDevice, handle, 277 info.bestBatchParams.flags, info.bestBatchParams.batchDelay, 278 info.bestBatchParams.batchTimeout, strerror(-err)); 279 info.removeBatchParamsForIdent(ident); 280 } 281 } 282 return err; 283} 284 285status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) 286{ 287 if (!mSensorDevice) return NO_INIT; 288 if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { 289 samplingPeriodNs = MINIMUM_EVENTS_PERIOD; 290 } 291 Mutex::Autolock _l(mLock); 292 Info& info( mActivationCount.editValueFor(handle) ); 293 // If the underlying sensor is NOT in continuous mode, setDelay() should return an error. 294 // Calling setDelay() in batch mode is an invalid operation. 295 if (info.bestBatchParams.batchTimeout != 0) { 296 return INVALID_OPERATION; 297 } 298 ssize_t index = info.batchParams.indexOfKey(ident); 299 if (index < 0) { 300 return BAD_INDEX; 301 } 302 BatchParams& params = info.batchParams.editValueAt(index); 303 params.batchDelay = samplingPeriodNs; 304 info.selectBatchParams(); 305 return mSensorDevice->setDelay(reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), 306 handle, info.bestBatchParams.batchDelay); 307} 308 309int SensorDevice::getHalDeviceVersion() const { 310 if (!mSensorDevice) return -1; 311 312 return mSensorDevice->common.version; 313} 314 315status_t SensorDevice::flush(void* /*ident*/, int handle) { 316 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1) { 317 return INVALID_OPERATION; 318 } 319 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle); 320 return mSensorDevice->flush(mSensorDevice, handle); 321} 322 323// --------------------------------------------------------------------------- 324 325status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int flags, 326 int64_t samplingPeriodNs, 327 int64_t maxBatchReportLatencyNs) { 328 ssize_t index = batchParams.indexOfKey(ident); 329 if (index < 0) { 330 ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 " timeout=%" PRId64 ") failed (%s)", 331 ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index)); 332 return BAD_INDEX; 333 } 334 BatchParams& params = batchParams.editValueAt(index); 335 params.flags = flags; 336 params.batchDelay = samplingPeriodNs; 337 params.batchTimeout = maxBatchReportLatencyNs; 338 return NO_ERROR; 339} 340 341void SensorDevice::Info::selectBatchParams() { 342 BatchParams bestParams(-1, -1, -1); 343 344 if (batchParams.size() > 0) { 345 BatchParams params = batchParams.valueAt(0); 346 bestParams = params; 347 } 348 349 for (size_t i = 1; i < batchParams.size(); ++i) { 350 BatchParams params = batchParams.valueAt(i); 351 if (params.batchDelay < bestParams.batchDelay) { 352 bestParams.batchDelay = params.batchDelay; 353 } 354 if (params.batchTimeout < bestParams.batchTimeout) { 355 bestParams.batchTimeout = params.batchTimeout; 356 } 357 } 358 bestBatchParams = bestParams; 359} 360 361ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) { 362 ssize_t idx = batchParams.removeItem(ident); 363 if (idx >= 0) { 364 selectBatchParams(); 365 } 366 return idx; 367} 368 369// --------------------------------------------------------------------------- 370}; // namespace android 371 372