blob: 18a1523ab4ab55e671fca03b9fe5d4208a4f8215 [file] [log] [blame]
Mathias Agopianf001c922010-11-11 17:58:51 -08001/*
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 <stdint.h>
18#include <math.h>
19#include <sys/types.h>
20
21#include <utils/Atomic.h>
22#include <utils/Errors.h>
23#include <utils/Singleton.h>
24
25#include <binder/BinderService.h>
26#include <binder/Parcel.h>
27#include <binder/IServiceManager.h>
28
29#include <hardware/sensors.h>
30
31#include "SensorDevice.h"
Mathias Agopiana1b7db92011-05-27 16:23:58 -070032#include "SensorService.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080033
34namespace android {
35// ---------------------------------------------------------------------------
Mathias Agopianf001c922010-11-11 17:58:51 -080036
37ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)
38
39SensorDevice::SensorDevice()
40 : mSensorDevice(0),
41 mSensorModule(0)
42{
43 status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID,
44 (hw_module_t const**)&mSensorModule);
45
Steve Blockf5a12302012-01-06 19:20:56 +000046 ALOGE_IF(err, "couldn't load %s module (%s)",
Mathias Agopianf001c922010-11-11 17:58:51 -080047 SENSORS_HARDWARE_MODULE_ID, strerror(-err));
48
49 if (mSensorModule) {
50 err = sensors_open(&mSensorModule->common, &mSensorDevice);
51
Steve Blockf5a12302012-01-06 19:20:56 +000052 ALOGE_IF(err, "couldn't open device for module %s (%s)",
Mathias Agopianf001c922010-11-11 17:58:51 -080053 SENSORS_HARDWARE_MODULE_ID, strerror(-err));
54
55 if (mSensorDevice) {
56 sensor_t const* list;
57 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
58 mActivationCount.setCapacity(count);
59 Info model;
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);
63 }
64 }
65 }
66}
67
Mathias Agopianba02cd22013-07-03 16:20:57 -070068void SensorDevice::dump(String8& result)
Mathias Agopianf001c922010-11-11 17:58:51 -080069{
70 if (!mSensorModule) return;
71 sensor_t const* list;
72 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
73
Mathias Agopianba02cd22013-07-03 16:20:57 -070074 result.appendFormat("%d h/w sensors:\n", int(count));
Mathias Agopianf001c922010-11-11 17:58:51 -080075
76 Mutex::Autolock _l(mLock);
77 for (size_t i=0 ; i<size_t(count) ; i++) {
Mathias Agopian667102f2011-09-14 16:43:34 -070078 const Info& info = mActivationCount.valueFor(list[i].handle);
Mathias Agopianba02cd22013-07-03 16:20:57 -070079 result.appendFormat("handle=0x%08x, active-count=%d, rates(ms)={ ",
Mathias Agopianf001c922010-11-11 17:58:51 -080080 list[i].handle,
Mathias Agopian667102f2011-09-14 16:43:34 -070081 info.rates.size());
Mathias Agopian667102f2011-09-14 16:43:34 -070082 for (size_t j=0 ; j<info.rates.size() ; j++) {
Mathias Agopianba02cd22013-07-03 16:20:57 -070083 result.appendFormat("%4.1f%s",
Mathias Agopian667102f2011-09-14 16:43:34 -070084 info.rates.valueAt(j) / 1e6f,
85 j<info.rates.size()-1 ? ", " : "");
Mathias Agopian667102f2011-09-14 16:43:34 -070086 }
Mathias Agopianba02cd22013-07-03 16:20:57 -070087 result.appendFormat(" }, selected=%4.1f ms\n", info.delay / 1e6f);
Mathias Agopianf001c922010-11-11 17:58:51 -080088 }
89}
90
91ssize_t SensorDevice::getSensorList(sensor_t const** list) {
92 if (!mSensorModule) return NO_INIT;
93 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list);
94 return count;
95}
96
97status_t SensorDevice::initCheck() const {
98 return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT;
99}
100
101ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
102 if (!mSensorDevice) return NO_INIT;
Mathias Agopian1a623012011-11-09 17:50:15 -0800103 ssize_t c;
104 do {
105 c = mSensorDevice->poll(mSensorDevice, buffer, count);
106 } while (c == -EINTR);
107 return c;
Mathias Agopianf001c922010-11-11 17:58:51 -0800108}
109
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700110status_t SensorDevice::resetStateWithoutActuatingHardware(void *ident, int handle)
111{
112 if (!mSensorDevice) return NO_INIT;
113 Info& info( mActivationCount.editValueFor(handle));
114 Mutex::Autolock _l(mLock);
115 info.rates.removeItem(ident);
116 return NO_ERROR;
117}
118
Mathias Agopianf001c922010-11-11 17:58:51 -0800119status_t SensorDevice::activate(void* ident, int handle, int enabled)
120{
121 if (!mSensorDevice) return NO_INIT;
122 status_t err(NO_ERROR);
123 bool actuateHardware = false;
124
125 Info& info( mActivationCount.editValueFor(handle) );
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700126
127
Steve Blocka5512372011-12-20 16:23:08 +0000128 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700129 "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%d",
130 ident, handle, enabled, info.rates.size());
131
Mathias Agopianf001c922010-11-11 17:58:51 -0800132 if (enabled) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800133 Mutex::Autolock _l(mLock);
Steve Blocka5512372011-12-20 16:23:08 +0000134 ALOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700135 info.rates.indexOfKey(ident));
136
Mathias Agopianf001c922010-11-11 17:58:51 -0800137 if (info.rates.indexOfKey(ident) < 0) {
138 info.rates.add(ident, DEFAULT_EVENTS_PERIOD);
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700139 if (info.rates.size() == 1) {
140 actuateHardware = true;
141 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800142 } else {
143 // sensor was already activated for this ident
Mathias Agopianf001c922010-11-11 17:58:51 -0800144 }
145 } else {
Mathias Agopianf001c922010-11-11 17:58:51 -0800146 Mutex::Autolock _l(mLock);
Steve Blocka5512372011-12-20 16:23:08 +0000147 ALOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700148 info.rates.indexOfKey(ident));
149
150 ssize_t idx = info.rates.removeItem(ident);
151 if (idx >= 0) {
Mathias Agopian50b66762010-11-29 17:26:51 -0800152 if (info.rates.size() == 0) {
153 actuateHardware = true;
154 }
155 } else {
156 // sensor wasn't enabled for this ident
157 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800158 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800159
Mathias Agopianf001c922010-11-11 17:58:51 -0800160 if (actuateHardware) {
Steve Blocka5512372011-12-20 16:23:08 +0000161 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w");
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700162
Mathias Agopianf001c922010-11-11 17:58:51 -0800163 err = mSensorDevice->activate(mSensorDevice, handle, enabled);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700164 ALOGE_IF(err, "Error %s sensor %d (%s)",
165 enabled ? "activating" : "disabling",
166 handle, strerror(-err));
Mathias Agopianf001c922010-11-11 17:58:51 -0800167 }
168
Mathias Agopian667102f2011-09-14 16:43:34 -0700169 { // scope for the lock
Mathias Agopianf001c922010-11-11 17:58:51 -0800170 Mutex::Autolock _l(mLock);
Mathias Agopian667102f2011-09-14 16:43:34 -0700171 nsecs_t ns = info.selectDelay();
Mathias Agopianf001c922010-11-11 17:58:51 -0800172 mSensorDevice->setDelay(mSensorDevice, handle, ns);
173 }
174
175 return err;
176}
177
178status_t SensorDevice::setDelay(void* ident, int handle, int64_t ns)
179{
180 if (!mSensorDevice) return NO_INIT;
Mathias Agopian667102f2011-09-14 16:43:34 -0700181 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800182 Info& info( mActivationCount.editValueFor(handle) );
Mathias Agopian667102f2011-09-14 16:43:34 -0700183 status_t err = info.setDelayForIdent(ident, ns);
184 if (err < 0) return err;
185 ns = info.selectDelay();
186 return mSensorDevice->setDelay(mSensorDevice, handle, ns);
187}
188
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700189int SensorDevice::getHalDeviceVersion() const {
190 if (!mSensorDevice) return -1;
191
192 return mSensorDevice->common.version;
193}
194
Mathias Agopian667102f2011-09-14 16:43:34 -0700195// ---------------------------------------------------------------------------
196
197status_t SensorDevice::Info::setDelayForIdent(void* ident, int64_t ns)
198{
199 ssize_t index = rates.indexOfKey(ident);
200 if (index < 0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000201 ALOGE("Info::setDelayForIdent(ident=%p, ns=%lld) failed (%s)",
Mathias Agopian667102f2011-09-14 16:43:34 -0700202 ident, ns, strerror(-index));
203 return BAD_INDEX;
204 }
205 rates.editValueAt(index) = ns;
206 return NO_ERROR;
207}
208
209nsecs_t SensorDevice::Info::selectDelay()
210{
211 nsecs_t ns = rates.valueAt(0);
212 for (size_t i=1 ; i<rates.size() ; i++) {
213 nsecs_t cur = rates.valueAt(i);
214 if (cur < ns) {
215 ns = cur;
Mathias Agopianf001c922010-11-11 17:58:51 -0800216 }
217 }
Mathias Agopian667102f2011-09-14 16:43:34 -0700218 delay = ns;
219 return ns;
Mathias Agopianf001c922010-11-11 17:58:51 -0800220}
221
222// ---------------------------------------------------------------------------
223}; // namespace android
224