SensorDevice.cpp revision 2576cb63b3fe1592f54816625036566b9eb0793a
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 if (mSensorDevice->common.version == SENSORS_DEVICE_API_VERSION_1_1 || 58 mSensorDevice->common.version == SENSORS_DEVICE_API_VERSION_1_2) { 59 ALOGE(">>>> WARNING <<< Upgrade sensor HAL to version 1_3"); 60 } 61 62 sensor_t const* list; 63 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list); 64 mActivationCount.setCapacity(count); 65 Info model; 66 for (size_t i=0 ; i<size_t(count) ; i++) { 67 mActivationCount.add(list[i].handle, model); 68 mSensorDevice->activate( 69 reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), 70 list[i].handle, 0); 71 } 72 } 73 } 74} 75 76void SensorDevice::handleDynamicSensorConnection(int handle, bool connected) { 77 if (connected) { 78 Info model; 79 mActivationCount.add(handle, model); 80 mSensorDevice->activate( 81 reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), handle, 0); 82 } else { 83 mActivationCount.removeItem(handle); 84 } 85} 86 87void SensorDevice::dump(String8& result) 88{ 89 if (!mSensorModule) return; 90 sensor_t const* list; 91 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list); 92 93 result.appendFormat("halVersion 0x%08x\n", getHalDeviceVersion()); 94 result.appendFormat("%d h/w sensors:\n", int(count)); 95 96 Mutex::Autolock _l(mLock); 97 for (size_t i=0 ; i<size_t(count) ; i++) { 98 const Info& info = mActivationCount.valueFor(list[i].handle); 99 if (info.batchParams.isEmpty()) continue; 100 result.appendFormat("handle=0x%08x, active-count=%zu, batch_period(ms)={ ", list[i].handle, 101 info.batchParams.size()); 102 for (size_t j = 0; j < info.batchParams.size(); j++) { 103 const BatchParams& params = info.batchParams.valueAt(j); 104 result.appendFormat("%4.1f%s", params.batchDelay / 1e6f, 105 j < info.batchParams.size() - 1 ? ", " : ""); 106 } 107 result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchDelay / 1e6f); 108 109 result.appendFormat("handle=0x%08x, active-count=%zu, batch_timeout(ms)={ ", list[i].handle, 110 info.batchParams.size()); 111 for (size_t j = 0; j < info.batchParams.size(); j++) { 112 BatchParams params = info.batchParams.valueAt(j); 113 result.appendFormat("%4.1f%s", params.batchTimeout / 1e6f, 114 j < info.batchParams.size() - 1 ? ", " : ""); 115 } 116 result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchTimeout / 1e6f); 117 } 118} 119 120ssize_t SensorDevice::getSensorList(sensor_t const** list) { 121 if (!mSensorModule) return NO_INIT; 122 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list); 123 return count; 124} 125 126status_t SensorDevice::initCheck() const { 127 return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT; 128} 129 130ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) { 131 if (!mSensorDevice) return NO_INIT; 132 ssize_t c; 133 do { 134 c = mSensorDevice->poll(reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), 135 buffer, count); 136 } while (c == -EINTR); 137 return c; 138} 139 140void SensorDevice::autoDisable(void *ident, int handle) { 141 Info& info( mActivationCount.editValueFor(handle) ); 142 Mutex::Autolock _l(mLock); 143 info.removeBatchParamsForIdent(ident); 144} 145 146status_t SensorDevice::activate(void* ident, int handle, int enabled) 147{ 148 if (!mSensorDevice) return NO_INIT; 149 status_t err(NO_ERROR); 150 bool actuateHardware = false; 151 152 Mutex::Autolock _l(mLock); 153 Info& info( mActivationCount.editValueFor(handle) ); 154 155 ALOGD_IF(DEBUG_CONNECTIONS, 156 "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu", 157 ident, handle, enabled, info.batchParams.size()); 158 159 if (enabled) { 160 ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident)); 161 162 if (isClientDisabledLocked(ident)) { 163 return INVALID_OPERATION; 164 } 165 166 if (info.batchParams.indexOfKey(ident) >= 0) { 167 if (info.numActiveClients() == 1) { 168 // This is the first connection, we need to activate the underlying h/w sensor. 169 actuateHardware = true; 170 } 171 } else { 172 // Log error. Every activate call should be preceded by a batch() call. 173 ALOGE("\t >>>ERROR: activate called without batch"); 174 } 175 } else { 176 ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident)); 177 178 if (info.removeBatchParamsForIdent(ident) >= 0) { 179 if (info.numActiveClients() == 0) { 180 // This is the last connection, we need to de-activate the underlying h/w sensor. 181 actuateHardware = true; 182 } else { 183 const int halVersion = getHalDeviceVersion(); 184 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) { 185 // Call batch for this sensor with the previously calculated best effort 186 // batch_rate and timeout. One of the apps has unregistered for sensor 187 // events, and the best effort batch parameters might have changed. 188 ALOGD_IF(DEBUG_CONNECTIONS, 189 "\t>>> actuating h/w batch %d %d %" PRId64 " %" PRId64, handle, 190 info.bestBatchParams.flags, info.bestBatchParams.batchDelay, 191 info.bestBatchParams.batchTimeout); 192 mSensorDevice->batch(mSensorDevice, handle,info.bestBatchParams.flags, 193 info.bestBatchParams.batchDelay, 194 info.bestBatchParams.batchTimeout); 195 } 196 } 197 } else { 198 // sensor wasn't enabled for this ident 199 } 200 201 if (isClientDisabledLocked(ident)) { 202 return NO_ERROR; 203 } 204 } 205 206 if (actuateHardware) { 207 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle, 208 enabled); 209 err = mSensorDevice->activate( 210 reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), handle, enabled); 211 ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle, 212 strerror(-err)); 213 214 if (err != NO_ERROR && enabled) { 215 // Failure when enabling the sensor. Clean up on failure. 216 info.removeBatchParamsForIdent(ident); 217 } 218 } 219 220 // On older devices which do not support batch, call setDelay(). 221 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1 && info.numActiveClients() > 0) { 222 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w setDelay %d %" PRId64, handle, 223 info.bestBatchParams.batchDelay); 224 mSensorDevice->setDelay( 225 reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), 226 handle, info.bestBatchParams.batchDelay); 227 } 228 return err; 229} 230 231status_t SensorDevice::batch(void* ident, int handle, int flags, int64_t samplingPeriodNs, 232 int64_t maxBatchReportLatencyNs) { 233 if (!mSensorDevice) return NO_INIT; 234 235 if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { 236 samplingPeriodNs = MINIMUM_EVENTS_PERIOD; 237 } 238 239 const int halVersion = getHalDeviceVersion(); 240 if (halVersion < SENSORS_DEVICE_API_VERSION_1_1 && maxBatchReportLatencyNs != 0) { 241 // Batch is not supported on older devices return invalid operation. 242 return INVALID_OPERATION; 243 } 244 245 ALOGD_IF(DEBUG_CONNECTIONS, 246 "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64, 247 ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); 248 249 Mutex::Autolock _l(mLock); 250 Info& info(mActivationCount.editValueFor(handle)); 251 252 if (info.batchParams.indexOfKey(ident) < 0) { 253 BatchParams params(flags, samplingPeriodNs, maxBatchReportLatencyNs); 254 info.batchParams.add(ident, params); 255 } else { 256 // A batch has already been called with this ident. Update the batch parameters. 257 info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs); 258 } 259 260 BatchParams prevBestBatchParams = info.bestBatchParams; 261 // Find the minimum of all timeouts and batch_rates for this sensor. 262 info.selectBatchParams(); 263 264 ALOGD_IF(DEBUG_CONNECTIONS, 265 "\t>>> curr_period=%" PRId64 " min_period=%" PRId64 266 " curr_timeout=%" PRId64 " min_timeout=%" PRId64, 267 prevBestBatchParams.batchDelay, info.bestBatchParams.batchDelay, 268 prevBestBatchParams.batchTimeout, info.bestBatchParams.batchTimeout); 269 270 status_t err(NO_ERROR); 271 // If the min period or min timeout has changed since the last batch call, call batch. 272 if (prevBestBatchParams != info.bestBatchParams) { 273 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) { 274 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH %d %d %" PRId64 " %" PRId64, handle, 275 info.bestBatchParams.flags, info.bestBatchParams.batchDelay, 276 info.bestBatchParams.batchTimeout); 277 err = mSensorDevice->batch(mSensorDevice, handle, info.bestBatchParams.flags, 278 info.bestBatchParams.batchDelay, 279 info.bestBatchParams.batchTimeout); 280 } else { 281 // For older devices which do not support batch, call setDelay() after activate() is 282 // called. Some older devices may not support calling setDelay before activate(), so 283 // call setDelay in SensorDevice::activate() method. 284 } 285 if (err != NO_ERROR) { 286 ALOGE("sensor batch failed %p %d %d %" PRId64 " %" PRId64 " err=%s", 287 mSensorDevice, handle, 288 info.bestBatchParams.flags, info.bestBatchParams.batchDelay, 289 info.bestBatchParams.batchTimeout, strerror(-err)); 290 info.removeBatchParamsForIdent(ident); 291 } 292 } 293 return err; 294} 295 296status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) 297{ 298 if (!mSensorDevice) return NO_INIT; 299 if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { 300 samplingPeriodNs = MINIMUM_EVENTS_PERIOD; 301 } 302 Mutex::Autolock _l(mLock); 303 if (isClientDisabledLocked(ident)) return INVALID_OPERATION; 304 Info& info( mActivationCount.editValueFor(handle) ); 305 // If the underlying sensor is NOT in continuous mode, setDelay() should return an error. 306 // Calling setDelay() in batch mode is an invalid operation. 307 if (info.bestBatchParams.batchTimeout != 0) { 308 return INVALID_OPERATION; 309 } 310 ssize_t index = info.batchParams.indexOfKey(ident); 311 if (index < 0) { 312 return BAD_INDEX; 313 } 314 BatchParams& params = info.batchParams.editValueAt(index); 315 params.batchDelay = samplingPeriodNs; 316 info.selectBatchParams(); 317 return mSensorDevice->setDelay(reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), 318 handle, info.bestBatchParams.batchDelay); 319} 320 321int SensorDevice::getHalDeviceVersion() const { 322 if (!mSensorDevice) return -1; 323 return mSensorDevice->common.version; 324} 325 326status_t SensorDevice::flush(void* ident, int handle) { 327 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1) { 328 return INVALID_OPERATION; 329 } 330 if (isClientDisabled(ident)) return INVALID_OPERATION; 331 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle); 332 return mSensorDevice->flush(mSensorDevice, handle); 333} 334 335bool SensorDevice::isClientDisabled(void* ident) { 336 Mutex::Autolock _l(mLock); 337 return isClientDisabledLocked(ident); 338} 339 340bool SensorDevice::isClientDisabledLocked(void* ident) { 341 return mDisabledClients.indexOf(ident) >= 0; 342} 343 344void SensorDevice::enableAllSensors() { 345 Mutex::Autolock _l(mLock); 346 mDisabledClients.clear(); 347 const int halVersion = getHalDeviceVersion(); 348 for (size_t i = 0; i< mActivationCount.size(); ++i) { 349 Info& info = mActivationCount.editValueAt(i); 350 if (info.batchParams.isEmpty()) continue; 351 info.selectBatchParams(); 352 const int sensor_handle = mActivationCount.keyAt(i); 353 ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ", 354 sensor_handle); 355 status_t err(NO_ERROR); 356 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0) { 357 err = mSensorDevice->batch(mSensorDevice, sensor_handle, 358 info.bestBatchParams.flags, info.bestBatchParams.batchDelay, 359 info.bestBatchParams.batchTimeout); 360 ALOGE_IF(err, "Error calling batch on sensor %d (%s)", sensor_handle, strerror(-err)); 361 } 362 363 if (err == NO_ERROR) { 364 err = mSensorDevice->activate( 365 reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), 366 sensor_handle, 1); 367 ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err)); 368 } 369 370 if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0) { 371 err = mSensorDevice->setDelay( 372 reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), 373 sensor_handle, info.bestBatchParams.batchDelay); 374 ALOGE_IF(err, "Error calling setDelay sensor %d (%s)", sensor_handle, strerror(-err)); 375 } 376 } 377} 378 379void SensorDevice::disableAllSensors() { 380 Mutex::Autolock _l(mLock); 381 for (size_t i = 0; i< mActivationCount.size(); ++i) { 382 const Info& info = mActivationCount.valueAt(i); 383 // Check if this sensor has been activated previously and disable it. 384 if (info.batchParams.size() > 0) { 385 const int sensor_handle = mActivationCount.keyAt(i); 386 ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ", 387 sensor_handle); 388 mSensorDevice->activate( 389 reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), 390 sensor_handle, 0); 391 // Add all the connections that were registered for this sensor to the disabled 392 // clients list. 393 for (size_t j = 0; j < info.batchParams.size(); ++j) { 394 mDisabledClients.add(info.batchParams.keyAt(j)); 395 } 396 } 397 } 398} 399 400status_t SensorDevice::injectSensorData(const sensors_event_t *injected_sensor_event) { 401 ALOGD_IF(DEBUG_CONNECTIONS, 402 "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f", 403 injected_sensor_event->sensor, 404 injected_sensor_event->timestamp, injected_sensor_event->data[0], 405 injected_sensor_event->data[1], injected_sensor_event->data[2], 406 injected_sensor_event->data[3], injected_sensor_event->data[4], 407 injected_sensor_event->data[5]); 408 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) { 409 return INVALID_OPERATION; 410 } 411 return mSensorDevice->inject_sensor_data(mSensorDevice, injected_sensor_event); 412} 413 414status_t SensorDevice::setMode(uint32_t mode) { 415 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) { 416 return INVALID_OPERATION; 417 } 418 return mSensorModule->set_operation_mode(mode); 419} 420 421// --------------------------------------------------------------------------- 422 423int SensorDevice::Info::numActiveClients() { 424 SensorDevice& device(SensorDevice::getInstance()); 425 int num = 0; 426 for (size_t i = 0; i < batchParams.size(); ++i) { 427 if (!device.isClientDisabledLocked(batchParams.keyAt(i))) { 428 ++num; 429 } 430 } 431 return num; 432} 433 434status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int flags, 435 int64_t samplingPeriodNs, 436 int64_t maxBatchReportLatencyNs) { 437 ssize_t index = batchParams.indexOfKey(ident); 438 if (index < 0) { 439 ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 " timeout=%" PRId64 ") failed (%s)", 440 ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index)); 441 return BAD_INDEX; 442 } 443 BatchParams& params = batchParams.editValueAt(index); 444 params.flags = flags; 445 params.batchDelay = samplingPeriodNs; 446 params.batchTimeout = maxBatchReportLatencyNs; 447 return NO_ERROR; 448} 449 450void SensorDevice::Info::selectBatchParams() { 451 BatchParams bestParams(0, -1, -1); 452 SensorDevice& device(SensorDevice::getInstance()); 453 454 for (size_t i = 0; i < batchParams.size(); ++i) { 455 if (device.isClientDisabledLocked(batchParams.keyAt(i))) continue; 456 BatchParams params = batchParams.valueAt(i); 457 if (bestParams.batchDelay == -1 || params.batchDelay < bestParams.batchDelay) { 458 bestParams.batchDelay = params.batchDelay; 459 } 460 if (bestParams.batchTimeout == -1 || params.batchTimeout < bestParams.batchTimeout) { 461 bestParams.batchTimeout = params.batchTimeout; 462 } 463 } 464 bestBatchParams = bestParams; 465} 466 467ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) { 468 ssize_t idx = batchParams.removeItem(ident); 469 if (idx >= 0) { 470 selectBatchParams(); 471 } 472 return idx; 473} 474 475// --------------------------------------------------------------------------- 476}; // namespace android 477 478