void SensorDevice::enableAllSensors() { Mutex::Autolock _l(mLock); mDisabledClients.clear(); const int halVersion = getHalDeviceVersion(); for (size_t i = 0; i< mActivationCount.size(); ++i) { Info& info = mActivationCount.editValueAt(i); if (info.batchParams.isEmpty()) continue; info.selectBatchParams(); const int sensor_handle = mActivationCount.keyAt(i); ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ", sensor_handle); status_t err(NO_ERROR); if (halVersion > SENSORS_DEVICE_API_VERSION_1_0) { err = mSensorDevice->batch(mSensorDevice, sensor_handle, info.bestBatchParams.flags, info.bestBatchParams.batchDelay, info.bestBatchParams.batchTimeout); ALOGE_IF(err, "Error calling batch on sensor %d (%s)", sensor_handle, strerror(-err)); } if (err == NO_ERROR) { err = mSensorDevice->activate( reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), sensor_handle, 1); ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err)); } if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0) { err = mSensorDevice->setDelay( reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), sensor_handle, info.bestBatchParams.batchDelay); ALOGE_IF(err, "Error calling setDelay sensor %d (%s)", sensor_handle, strerror(-err)); } } }
void SensorDevice::dump(String8& result) { if (!mSensorModule) return; sensor_t const* list; ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list); result.appendFormat("halVersion 0x%08x\n", getHalDeviceVersion()); result.appendFormat("%d h/w sensors:\n", int(count)); Mutex::Autolock _l(mLock); for (size_t i=0 ; i<size_t(count) ; i++) { const Info& info = mActivationCount.valueFor(list[i].handle); if (info.batchParams.isEmpty()) continue; result.appendFormat("handle=0x%08x, active-count=%zu, batch_period(ms)={ ", list[i].handle, info.batchParams.size()); for (size_t j = 0; j < info.batchParams.size(); j++) { const BatchParams& params = info.batchParams.valueAt(j); result.appendFormat("%4.1f%s", params.batchDelay / 1e6f, j < info.batchParams.size() - 1 ? ", " : ""); } result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchDelay / 1e6f); result.appendFormat("handle=0x%08x, active-count=%zu, batch_timeout(ms)={ ", list[i].handle, info.batchParams.size()); for (size_t j = 0; j < info.batchParams.size(); j++) { BatchParams params = info.batchParams.valueAt(j); result.appendFormat("%4.1f%s", params.batchTimeout / 1e6f, j < info.batchParams.size() - 1 ? ", " : ""); } result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchTimeout / 1e6f); } }
status_t SensorDevice::flush(void* ident, int handle) { if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1) { return INVALID_OPERATION; } ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle); return mSensorDevice->flush(mSensorDevice, handle); }
status_t SensorDevice::injectSensorData(const sensors_event_t *injected_sensor_event) { ALOGD_IF(DEBUG_CONNECTIONS, "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f", injected_sensor_event->sensor, injected_sensor_event->timestamp, injected_sensor_event->data[0], injected_sensor_event->data[1], injected_sensor_event->data[2], injected_sensor_event->data[3], injected_sensor_event->data[4], injected_sensor_event->data[5]); if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) { return INVALID_OPERATION; } return mSensorDevice->inject_sensor_data(mSensorDevice, injected_sensor_event); }
std::string SensorDevice::dump() const { if (!mSensorModule) return "HAL not initialized\n"; String8 result; sensor_t const* list; int count = mSensorModule->get_sensors_list(mSensorModule, &list); result.appendFormat("HAL: %s (%s), version %#010x\n", mSensorModule->common.name, mSensorModule->common.author, getHalDeviceVersion()); result.appendFormat("Total %d h/w sensors, %zu running:\n", count, mActivationCount.size()); Mutex::Autolock _l(mLock); for (int i = 0 ; i < count ; i++) { const Info& info = mActivationCount.valueFor(list[i].handle); if (info.batchParams.isEmpty()) continue; result.appendFormat("0x%08x) active-count = %zu; ", list[i].handle, info.batchParams.size()); result.append("sampling_period(ms) = {"); for (size_t j = 0; j < info.batchParams.size(); j++) { const BatchParams& params = info.batchParams.valueAt(j); result.appendFormat("%.1f%s", params.batchDelay / 1e6f, j < info.batchParams.size() - 1 ? ", " : ""); } result.appendFormat("}, selected = %.1f ms; ", info.bestBatchParams.batchDelay / 1e6f); result.append("batching_period(ms) = {"); for (size_t j = 0; j < info.batchParams.size(); j++) { BatchParams params = info.batchParams.valueAt(j); result.appendFormat("%.1f%s", params.batchTimeout / 1e6f, j < info.batchParams.size() - 1 ? ", " : ""); } result.appendFormat("}, selected = %.1f ms\n", info.bestBatchParams.batchTimeout / 1e6f); } return result.string(); }
status_t SensorDevice::setMode(uint32_t mode) { if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) { return INVALID_OPERATION; } return mSensorModule->set_operation_mode(mode); }
status_t SensorDevice::batch(void* ident, int handle, int flags, int64_t samplingPeriodNs, int64_t maxBatchReportLatencyNs) { if (!mSensorDevice) return NO_INIT; if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { samplingPeriodNs = MINIMUM_EVENTS_PERIOD; } const int halVersion = getHalDeviceVersion(); if (halVersion < SENSORS_DEVICE_API_VERSION_1_1 && maxBatchReportLatencyNs != 0) { // Batch is not supported on older devices return invalid operation. return INVALID_OPERATION; } ALOGD_IF(DEBUG_CONNECTIONS, "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64, ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); Mutex::Autolock _l(mLock); Info& info(mActivationCount.editValueFor(handle)); if (info.batchParams.indexOfKey(ident) < 0) { BatchParams params(flags, samplingPeriodNs, maxBatchReportLatencyNs); info.batchParams.add(ident, params); } else { // A batch has already been called with this ident. Update the batch parameters. info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs); } BatchParams prevBestBatchParams = info.bestBatchParams; // Find the minimum of all timeouts and batch_rates for this sensor. info.selectBatchParams(); ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> curr_period=%" PRId64 " min_period=%" PRId64 " curr_timeout=%" PRId64 " min_timeout=%" PRId64, prevBestBatchParams.batchDelay, info.bestBatchParams.batchDelay, prevBestBatchParams.batchTimeout, info.bestBatchParams.batchTimeout); status_t err(NO_ERROR); // If the min period or min timeout has changed since the last batch call, call batch. if (prevBestBatchParams != info.bestBatchParams) { if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) { ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH %d %d %" PRId64 " %" PRId64, handle, info.bestBatchParams.flags, info.bestBatchParams.batchDelay, info.bestBatchParams.batchTimeout); err = mSensorDevice->batch(mSensorDevice, handle, info.bestBatchParams.flags, info.bestBatchParams.batchDelay, info.bestBatchParams.batchTimeout); } else { // For older devices which do not support batch, call setDelay() after activate() is // called. Some older devices may not support calling setDelay before activate(), so // call setDelay in SensorDevice::activate() method. } if (err != NO_ERROR) { ALOGE("sensor batch failed %p %d %d %" PRId64 " %" PRId64 " err=%s", mSensorDevice, handle, info.bestBatchParams.flags, info.bestBatchParams.batchDelay, info.bestBatchParams.batchTimeout, strerror(-err)); info.removeBatchParamsForIdent(ident); } } return err; }
status_t SensorDevice::activate(void* ident, int handle, int enabled) { if (!mSensorDevice) return NO_INIT; status_t err(NO_ERROR); bool actuateHardware = false; Mutex::Autolock _l(mLock); Info& info( mActivationCount.editValueFor(handle) ); ALOGD_IF(DEBUG_CONNECTIONS, "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu", ident, handle, enabled, info.batchParams.size()); if (enabled) { ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident)); if (isClientDisabledLocked(ident)) { return INVALID_OPERATION; } if (info.batchParams.indexOfKey(ident) >= 0) { if (info.numActiveClients() == 1) { // This is the first connection, we need to activate the underlying h/w sensor. actuateHardware = true; } } else { // Log error. Every activate call should be preceded by a batch() call. ALOGE("\t >>>ERROR: activate called without batch"); } } else { ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident)); if (info.removeBatchParamsForIdent(ident) >= 0) { if (info.numActiveClients() == 0) { // This is the last connection, we need to de-activate the underlying h/w sensor. actuateHardware = true; } else { const int halVersion = getHalDeviceVersion(); if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) { // Call batch for this sensor with the previously calculated best effort // batch_rate and timeout. One of the apps has unregistered for sensor // events, and the best effort batch parameters might have changed. ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w batch %d %d %" PRId64 " %" PRId64, handle, info.bestBatchParams.flags, info.bestBatchParams.batchDelay, info.bestBatchParams.batchTimeout); mSensorDevice->batch(mSensorDevice, handle,info.bestBatchParams.flags, info.bestBatchParams.batchDelay, info.bestBatchParams.batchTimeout); } } } else { // sensor wasn't enabled for this ident } if (isClientDisabledLocked(ident)) { return NO_ERROR; } } if (actuateHardware) { ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle, enabled); err = mSensorDevice->activate( reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), handle, enabled); ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle, strerror(-err)); if (err != NO_ERROR && enabled) { // Failure when enabling the sensor. Clean up on failure. info.removeBatchParamsForIdent(ident); } } // On older devices which do not support batch, call setDelay(). if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1 && info.numActiveClients() > 0) { ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w setDelay %d %" PRId64, handle, info.bestBatchParams.batchDelay); mSensorDevice->setDelay( reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), handle, info.bestBatchParams.batchDelay); } return err; }
status_t SensorDevice::activate(void* ident, int handle, int enabled) { if (!mSensorDevice) return NO_INIT; status_t err(NO_ERROR); bool actuateHardware = false; #ifdef SYSFS_LIGHT_SENSOR if (handle == DUMMY_ALS_HANDLE) { int nwr, ret, fd; char value[2]; fd = open(SYSFS_LIGHT_SENSOR, O_RDWR); if(fd < 0) return -ENODEV; nwr = snprintf(value, 2, "%d\n", enabled ? 1 : 0); write(fd, value, nwr); close(fd); return 0; } #endif Mutex::Autolock _l(mLock); Info& info( mActivationCount.editValueFor(handle) ); ALOGD_IF(DEBUG_CONNECTIONS, "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%d", ident, handle, enabled, info.batchParams.size()); if (enabled) { ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%d", info.batchParams.indexOfKey(ident)); if (info.batchParams.indexOfKey(ident) >= 0) { if (info.batchParams.size() == 1) { // This is the first connection, we need to activate the underlying h/w sensor. actuateHardware = true; } } else { // Log error. Every activate call should be preceded by a batch() call. ALOGE("\t >>>ERROR: activate called without batch"); } } else { ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%d", info.batchParams.indexOfKey(ident)); if (info.removeBatchParamsForIdent(ident) >= 0) { if (info.batchParams.size() == 0) { // This is the last connection, we need to de-activate the underlying h/w sensor. actuateHardware = true; } else { const int halVersion = getHalDeviceVersion(); if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) { // Call batch for this sensor with the previously calculated best effort // batch_rate and timeout. One of the apps has unregistered for sensor // events, and the best effort batch parameters might have changed. ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w batch %d %d %lld %lld ", handle, info.bestBatchParams.flags, info.bestBatchParams.batchDelay, info.bestBatchParams.batchTimeout); mSensorDevice->batch(mSensorDevice, handle,info.bestBatchParams.flags, info.bestBatchParams.batchDelay, info.bestBatchParams.batchTimeout); } } } else { // sensor wasn't enabled for this ident } } if (actuateHardware) { ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle, enabled); err = mSensorDevice->activate( reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), handle, enabled); ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle, strerror(-err)); if (err != NO_ERROR && enabled) { // Failure when enabling the sensor. Clean up on failure. info.removeBatchParamsForIdent(ident); } } // On older devices which do not support batch, call setDelay(). if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1 && info.batchParams.size() > 0) { ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w setDelay %d %lld ", handle, info.bestBatchParams.batchDelay); mSensorDevice->setDelay( reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), handle, info.bestBatchParams.batchDelay); } return err; }