2010-11-12 01:58:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <utils/Atomic.h>
|
|
|
|
#include <utils/Errors.h>
|
|
|
|
#include <utils/Singleton.h>
|
|
|
|
|
|
|
|
#include <binder/BinderService.h>
|
|
|
|
#include <binder/Parcel.h>
|
|
|
|
#include <binder/IServiceManager.h>
|
|
|
|
|
|
|
|
#include <hardware/sensors.h>
|
|
|
|
|
|
|
|
#include "SensorDevice.h"
|
2011-05-27 23:23:58 +00:00
|
|
|
#include "SensorService.h"
|
2010-11-12 01:58:51 +00:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)
|
|
|
|
|
|
|
|
SensorDevice::SensorDevice()
|
|
|
|
: mSensorDevice(0),
|
|
|
|
mSensorModule(0)
|
|
|
|
{
|
|
|
|
status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID,
|
|
|
|
(hw_module_t const**)&mSensorModule);
|
|
|
|
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE_IF(err, "couldn't load %s module (%s)",
|
2010-11-12 01:58:51 +00:00
|
|
|
SENSORS_HARDWARE_MODULE_ID, strerror(-err));
|
|
|
|
|
|
|
|
if (mSensorModule) {
|
|
|
|
err = sensors_open(&mSensorModule->common, &mSensorDevice);
|
|
|
|
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE_IF(err, "couldn't open device for module %s (%s)",
|
2010-11-12 01:58:51 +00:00
|
|
|
SENSORS_HARDWARE_MODULE_ID, strerror(-err));
|
|
|
|
|
|
|
|
if (mSensorDevice) {
|
|
|
|
sensor_t const* list;
|
|
|
|
ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
|
|
|
|
mActivationCount.setCapacity(count);
|
|
|
|
Info model;
|
|
|
|
for (size_t i=0 ; i<size_t(count) ; i++) {
|
|
|
|
mActivationCount.add(list[i].handle, model);
|
|
|
|
mSensorDevice->activate(mSensorDevice, list[i].handle, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-03 23:20:57 +00:00
|
|
|
void SensorDevice::dump(String8& result)
|
2010-11-12 01:58:51 +00:00
|
|
|
{
|
|
|
|
if (!mSensorModule) return;
|
|
|
|
sensor_t const* list;
|
|
|
|
ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
|
|
|
|
|
2013-07-03 23:20:57 +00:00
|
|
|
result.appendFormat("%d h/w sensors:\n", int(count));
|
2010-11-12 01:58:51 +00:00
|
|
|
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
for (size_t i=0 ; i<size_t(count) ; i++) {
|
2011-09-14 23:43:34 +00:00
|
|
|
const Info& info = mActivationCount.valueFor(list[i].handle);
|
2013-07-03 23:20:57 +00:00
|
|
|
result.appendFormat("handle=0x%08x, active-count=%d, rates(ms)={ ",
|
2010-11-12 01:58:51 +00:00
|
|
|
list[i].handle,
|
2011-09-14 23:43:34 +00:00
|
|
|
info.rates.size());
|
|
|
|
for (size_t j=0 ; j<info.rates.size() ; j++) {
|
2013-07-03 23:20:57 +00:00
|
|
|
result.appendFormat("%4.1f%s",
|
2011-09-14 23:43:34 +00:00
|
|
|
info.rates.valueAt(j) / 1e6f,
|
|
|
|
j<info.rates.size()-1 ? ", " : "");
|
|
|
|
}
|
2013-07-03 23:20:57 +00:00
|
|
|
result.appendFormat(" }, selected=%4.1f ms\n", info.delay / 1e6f);
|
2010-11-12 01:58:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t SensorDevice::getSensorList(sensor_t const** list) {
|
|
|
|
if (!mSensorModule) return NO_INIT;
|
|
|
|
ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorDevice::initCheck() const {
|
|
|
|
return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
|
|
|
|
if (!mSensorDevice) return NO_INIT;
|
2011-11-10 01:50:15 +00:00
|
|
|
ssize_t c;
|
|
|
|
do {
|
|
|
|
c = mSensorDevice->poll(mSensorDevice, buffer, count);
|
|
|
|
} while (c == -EINTR);
|
|
|
|
return c;
|
2010-11-12 01:58:51 +00:00
|
|
|
}
|
|
|
|
|
2013-04-16 22:52:23 +00:00
|
|
|
status_t SensorDevice::resetStateWithoutActuatingHardware(void *ident, int handle)
|
|
|
|
{
|
|
|
|
if (!mSensorDevice) return NO_INIT;
|
|
|
|
Info& info( mActivationCount.editValueFor(handle));
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
info.rates.removeItem(ident);
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
status_t SensorDevice::activate(void* ident, int handle, int enabled)
|
|
|
|
{
|
|
|
|
if (!mSensorDevice) return NO_INIT;
|
|
|
|
status_t err(NO_ERROR);
|
|
|
|
bool actuateHardware = false;
|
|
|
|
|
|
|
|
Info& info( mActivationCount.editValueFor(handle) );
|
2011-05-27 23:23:58 +00:00
|
|
|
|
|
|
|
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS,
|
2011-05-27 23:23:58 +00:00
|
|
|
"SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%d",
|
|
|
|
ident, handle, enabled, info.rates.size());
|
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
if (enabled) {
|
|
|
|
Mutex::Autolock _l(mLock);
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
|
2011-05-27 23:23:58 +00:00
|
|
|
info.rates.indexOfKey(ident));
|
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
if (info.rates.indexOfKey(ident) < 0) {
|
|
|
|
info.rates.add(ident, DEFAULT_EVENTS_PERIOD);
|
2011-05-27 23:23:58 +00:00
|
|
|
if (info.rates.size() == 1) {
|
|
|
|
actuateHardware = true;
|
|
|
|
}
|
2010-11-30 01:26:51 +00:00
|
|
|
} else {
|
|
|
|
// sensor was already activated for this ident
|
2010-11-12 01:58:51 +00:00
|
|
|
}
|
2010-11-30 01:26:51 +00:00
|
|
|
} else {
|
2010-11-12 01:58:51 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
|
2011-05-27 23:23:58 +00:00
|
|
|
info.rates.indexOfKey(ident));
|
|
|
|
|
|
|
|
ssize_t idx = info.rates.removeItem(ident);
|
|
|
|
if (idx >= 0) {
|
2010-11-30 01:26:51 +00:00
|
|
|
if (info.rates.size() == 0) {
|
|
|
|
actuateHardware = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// sensor wasn't enabled for this ident
|
|
|
|
}
|
2010-11-12 01:58:51 +00:00
|
|
|
}
|
2010-11-30 01:26:51 +00:00
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
if (actuateHardware) {
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w");
|
2011-05-27 23:23:58 +00:00
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
err = mSensorDevice->activate(mSensorDevice, handle, enabled);
|
2012-09-19 01:49:18 +00:00
|
|
|
ALOGE_IF(err, "Error %s sensor %d (%s)",
|
|
|
|
enabled ? "activating" : "disabling",
|
|
|
|
handle, strerror(-err));
|
2010-11-12 01:58:51 +00:00
|
|
|
}
|
|
|
|
|
2011-09-14 23:43:34 +00:00
|
|
|
{ // scope for the lock
|
2010-11-12 01:58:51 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2011-09-14 23:43:34 +00:00
|
|
|
nsecs_t ns = info.selectDelay();
|
2010-11-12 01:58:51 +00:00
|
|
|
mSensorDevice->setDelay(mSensorDevice, handle, ns);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorDevice::setDelay(void* ident, int handle, int64_t ns)
|
|
|
|
{
|
|
|
|
if (!mSensorDevice) return NO_INIT;
|
2011-09-14 23:43:34 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2010-11-12 01:58:51 +00:00
|
|
|
Info& info( mActivationCount.editValueFor(handle) );
|
2011-09-14 23:43:34 +00:00
|
|
|
status_t err = info.setDelayForIdent(ident, ns);
|
|
|
|
if (err < 0) return err;
|
|
|
|
ns = info.selectDelay();
|
|
|
|
return mSensorDevice->setDelay(mSensorDevice, handle, ns);
|
|
|
|
}
|
2011-05-18 05:54:42 +00:00
|
|
|
|
2013-04-08 23:43:12 +00:00
|
|
|
int SensorDevice::getHalDeviceVersion() const {
|
|
|
|
if (!mSensorDevice) return -1;
|
|
|
|
|
|
|
|
return mSensorDevice->common.version;
|
|
|
|
}
|
|
|
|
|
2011-09-14 23:43:34 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
2011-05-18 05:54:42 +00:00
|
|
|
|
2011-09-14 23:43:34 +00:00
|
|
|
status_t SensorDevice::Info::setDelayForIdent(void* ident, int64_t ns)
|
|
|
|
{
|
|
|
|
ssize_t index = rates.indexOfKey(ident);
|
|
|
|
if (index < 0) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Info::setDelayForIdent(ident=%p, ns=%lld) failed (%s)",
|
2011-09-14 23:43:34 +00:00
|
|
|
ident, ns, strerror(-index));
|
|
|
|
return BAD_INDEX;
|
|
|
|
}
|
|
|
|
rates.editValueAt(index) = ns;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsecs_t SensorDevice::Info::selectDelay()
|
|
|
|
{
|
|
|
|
nsecs_t ns = rates.valueAt(0);
|
|
|
|
for (size_t i=1 ; i<rates.size() ; i++) {
|
|
|
|
nsecs_t cur = rates.valueAt(i);
|
|
|
|
if (cur < ns) {
|
|
|
|
ns = cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delay = ns;
|
|
|
|
return ns;
|
2010-11-12 01:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|
|
|
|
|