2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/types.h>
21 #include <utils/Atomic.h>
22 #include <utils/Errors.h>
23 #include <utils/Singleton.h>
25 #include <binder/BinderService.h>
26 #include <binder/Parcel.h>
27 #include <binder/IServiceManager.h>
29 #include <hardware/sensors.h>
31 #include "SensorDevice.h"
32 #include "SensorService.h"
35 // ---------------------------------------------------------------------------
37 ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)
39 SensorDevice::SensorDevice()
43 status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID,
44 (hw_module_t const**)&mSensorModule);
46 ALOGE_IF(err, "couldn't load %s module (%s)",
47 SENSORS_HARDWARE_MODULE_ID, strerror(-err));
50 err = sensors_open(&mSensorModule->common, &mSensorDevice);
52 ALOGE_IF(err, "couldn't open device for module %s (%s)",
53 SENSORS_HARDWARE_MODULE_ID, strerror(-err));
57 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
58 mActivationCount.setCapacity(count);
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);
68 void SensorDevice::dump(String8& result)
70 if (!mSensorModule) return;
72 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
74 result.appendFormat("%d h/w sensors:\n", int(count));
76 Mutex::Autolock _l(mLock);
77 for (size_t i=0 ; i<size_t(count) ; i++) {
78 const Info& info = mActivationCount.valueFor(list[i].handle);
79 result.appendFormat("handle=0x%08x, active-count=%d, rates(ms)={ ",
82 for (size_t j=0 ; j<info.rates.size() ; j++) {
83 result.appendFormat("%4.1f%s",
84 info.rates.valueAt(j) / 1e6f,
85 j<info.rates.size()-1 ? ", " : "");
87 result.appendFormat(" }, selected=%4.1f ms\n", info.delay / 1e6f);
91 ssize_t SensorDevice::getSensorList(sensor_t const** list) {
92 if (!mSensorModule) return NO_INIT;
93 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list);
97 status_t SensorDevice::initCheck() const {
98 return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT;
101 ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
102 if (!mSensorDevice) return NO_INIT;
105 c = mSensorDevice->poll(mSensorDevice, buffer, count);
106 } while (c == -EINTR);
110 void SensorDevice::autoDisable(void *ident, int handle) {
111 Info& info( mActivationCount.editValueFor(handle) );
112 Mutex::Autolock _l(mLock);
113 info.rates.removeItem(ident);
116 status_t SensorDevice::activate(void* ident, int handle, int enabled)
118 if (!mSensorDevice) return NO_INIT;
119 status_t err(NO_ERROR);
120 bool actuateHardware = false;
122 Info& info( mActivationCount.editValueFor(handle) );
125 ALOGD_IF(DEBUG_CONNECTIONS,
126 "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%d",
127 ident, handle, enabled, info.rates.size());
130 Mutex::Autolock _l(mLock);
131 ALOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
132 info.rates.indexOfKey(ident));
134 if (info.rates.indexOfKey(ident) < 0) {
135 info.rates.add(ident, DEFAULT_EVENTS_PERIOD);
136 if (info.rates.size() == 1) {
137 actuateHardware = true;
140 // sensor was already activated for this ident
143 Mutex::Autolock _l(mLock);
144 ALOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
145 info.rates.indexOfKey(ident));
147 ssize_t idx = info.rates.removeItem(ident);
149 if (info.rates.size() == 0) {
150 actuateHardware = true;
153 // sensor wasn't enabled for this ident
157 if (actuateHardware) {
158 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w");
160 err = mSensorDevice->activate(mSensorDevice, handle, enabled);
161 ALOGE_IF(err, "Error %s sensor %d (%s)",
162 enabled ? "activating" : "disabling",
163 handle, strerror(-err));
165 if (err != NO_ERROR) {
166 // clean-up on failure
168 // failure when enabling the sensor
169 Mutex::Autolock _l(mLock);
170 info.rates.removeItem(ident);
175 { // scope for the lock
176 Mutex::Autolock _l(mLock);
177 nsecs_t ns = info.selectDelay();
178 mSensorDevice->setDelay(mSensorDevice, handle, ns);
184 status_t SensorDevice::setDelay(void* ident, int handle, int64_t ns)
186 if (!mSensorDevice) return NO_INIT;
187 Mutex::Autolock _l(mLock);
188 Info& info( mActivationCount.editValueFor(handle) );
189 status_t err = info.setDelayForIdent(ident, ns);
190 if (err < 0) return err;
191 ns = info.selectDelay();
192 return mSensorDevice->setDelay(mSensorDevice, handle, ns);
195 int SensorDevice::getHalDeviceVersion() const {
196 if (!mSensorDevice) return -1;
198 return mSensorDevice->common.version;
201 // ---------------------------------------------------------------------------
203 status_t SensorDevice::Info::setDelayForIdent(void* ident, int64_t ns)
205 ssize_t index = rates.indexOfKey(ident);
207 ALOGE("Info::setDelayForIdent(ident=%p, ns=%lld) failed (%s)",
208 ident, ns, strerror(-index));
211 rates.editValueAt(index) = ns;
215 nsecs_t SensorDevice::Info::selectDelay()
217 nsecs_t ns = rates.valueAt(0);
218 for (size_t i=1 ; i<rates.size() ; i++) {
219 nsecs_t cur = rates.valueAt(i);
228 // ---------------------------------------------------------------------------
229 }; // namespace android