blob: 16dabe8fe5686401adde1988e413523b40fd8659 [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
68void SensorDevice::dump(String8& result, char* buffer, size_t SIZE)
69{
70 if (!mSensorModule) return;
71 sensor_t const* list;
72 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
73
74 snprintf(buffer, SIZE, "%d h/w sensors:\n", int(count));
75 result.append(buffer);
76
77 Mutex::Autolock _l(mLock);
78 for (size_t i=0 ; i<size_t(count) ; i++) {
Mathias Agopian667102f2011-09-14 16:43:34 -070079 const Info& info = mActivationCount.valueFor(list[i].handle);
80 snprintf(buffer, SIZE, "handle=0x%08x, active-count=%d, rates(ms)={ ",
Mathias Agopianf001c922010-11-11 17:58:51 -080081 list[i].handle,
Mathias Agopian667102f2011-09-14 16:43:34 -070082 info.rates.size());
83 result.append(buffer);
84 for (size_t j=0 ; j<info.rates.size() ; j++) {
85 snprintf(buffer, SIZE, "%4.1f%s",
86 info.rates.valueAt(j) / 1e6f,
87 j<info.rates.size()-1 ? ", " : "");
88 result.append(buffer);
89 }
90 snprintf(buffer, SIZE, " }, selected=%4.1f ms\n", info.delay / 1e6f);
Mathias Agopianf001c922010-11-11 17:58:51 -080091 result.append(buffer);
92 }
93}
94
95ssize_t SensorDevice::getSensorList(sensor_t const** list) {
96 if (!mSensorModule) return NO_INIT;
97 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list);
98 return count;
99}
100
101status_t SensorDevice::initCheck() const {
102 return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT;
103}
104
105ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
106 if (!mSensorDevice) return NO_INIT;
Mathias Agopian1a623012011-11-09 17:50:15 -0800107 ssize_t c;
108 do {
109 c = mSensorDevice->poll(mSensorDevice, buffer, count);
110 } while (c == -EINTR);
111 return c;
Mathias Agopianf001c922010-11-11 17:58:51 -0800112}
113
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700114status_t SensorDevice::resetStateWithoutActuatingHardware(void *ident, int handle)
115{
116 if (!mSensorDevice) return NO_INIT;
117 Info& info( mActivationCount.editValueFor(handle));
118 Mutex::Autolock _l(mLock);
119 info.rates.removeItem(ident);
120 return NO_ERROR;
121}
122
Mathias Agopianf001c922010-11-11 17:58:51 -0800123status_t SensorDevice::activate(void* ident, int handle, int enabled)
124{
125 if (!mSensorDevice) return NO_INIT;
126 status_t err(NO_ERROR);
127 bool actuateHardware = false;
128
129 Info& info( mActivationCount.editValueFor(handle) );
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700130
131
Steve Blocka5512372011-12-20 16:23:08 +0000132 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700133 "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%d",
134 ident, handle, enabled, info.rates.size());
135
Mathias Agopianf001c922010-11-11 17:58:51 -0800136 if (enabled) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800137 Mutex::Autolock _l(mLock);
Steve Blocka5512372011-12-20 16:23:08 +0000138 ALOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700139 info.rates.indexOfKey(ident));
140
Mathias Agopianf001c922010-11-11 17:58:51 -0800141 if (info.rates.indexOfKey(ident) < 0) {
142 info.rates.add(ident, DEFAULT_EVENTS_PERIOD);
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700143 if (info.rates.size() == 1) {
144 actuateHardware = true;
145 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800146 } else {
147 // sensor was already activated for this ident
Mathias Agopianf001c922010-11-11 17:58:51 -0800148 }
149 } else {
Mathias Agopianf001c922010-11-11 17:58:51 -0800150 Mutex::Autolock _l(mLock);
Steve Blocka5512372011-12-20 16:23:08 +0000151 ALOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700152 info.rates.indexOfKey(ident));
153
154 ssize_t idx = info.rates.removeItem(ident);
155 if (idx >= 0) {
Mathias Agopian50b66762010-11-29 17:26:51 -0800156 if (info.rates.size() == 0) {
157 actuateHardware = true;
158 }
159 } else {
160 // sensor wasn't enabled for this ident
161 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800162 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800163
Mathias Agopianf001c922010-11-11 17:58:51 -0800164 if (actuateHardware) {
Steve Blocka5512372011-12-20 16:23:08 +0000165 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w");
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700166
Mathias Agopianf001c922010-11-11 17:58:51 -0800167 err = mSensorDevice->activate(mSensorDevice, handle, enabled);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700168 ALOGE_IF(err, "Error %s sensor %d (%s)",
169 enabled ? "activating" : "disabling",
170 handle, strerror(-err));
Mathias Agopianf001c922010-11-11 17:58:51 -0800171 }
172
Mathias Agopian667102f2011-09-14 16:43:34 -0700173 { // scope for the lock
Mathias Agopianf001c922010-11-11 17:58:51 -0800174 Mutex::Autolock _l(mLock);
Mathias Agopian667102f2011-09-14 16:43:34 -0700175 nsecs_t ns = info.selectDelay();
Mathias Agopianf001c922010-11-11 17:58:51 -0800176 mSensorDevice->setDelay(mSensorDevice, handle, ns);
177 }
178
179 return err;
180}
181
182status_t SensorDevice::setDelay(void* ident, int handle, int64_t ns)
183{
184 if (!mSensorDevice) return NO_INIT;
Mathias Agopian667102f2011-09-14 16:43:34 -0700185 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800186 Info& info( mActivationCount.editValueFor(handle) );
Mathias Agopian667102f2011-09-14 16:43:34 -0700187 status_t err = info.setDelayForIdent(ident, ns);
188 if (err < 0) return err;
189 ns = info.selectDelay();
190 return mSensorDevice->setDelay(mSensorDevice, handle, ns);
191}
192
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700193int SensorDevice::getHalDeviceVersion() const {
194 if (!mSensorDevice) return -1;
195
196 return mSensorDevice->common.version;
197}
198
Mathias Agopian667102f2011-09-14 16:43:34 -0700199// ---------------------------------------------------------------------------
200
201status_t SensorDevice::Info::setDelayForIdent(void* ident, int64_t ns)
202{
203 ssize_t index = rates.indexOfKey(ident);
204 if (index < 0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000205 ALOGE("Info::setDelayForIdent(ident=%p, ns=%lld) failed (%s)",
Mathias Agopian667102f2011-09-14 16:43:34 -0700206 ident, ns, strerror(-index));
207 return BAD_INDEX;
208 }
209 rates.editValueAt(index) = ns;
210 return NO_ERROR;
211}
212
213nsecs_t SensorDevice::Info::selectDelay()
214{
215 nsecs_t ns = rates.valueAt(0);
216 for (size_t i=1 ; i<rates.size() ; i++) {
217 nsecs_t cur = rates.valueAt(i);
218 if (cur < ns) {
219 ns = cur;
Mathias Agopianf001c922010-11-11 17:58:51 -0800220 }
221 }
Mathias Agopian667102f2011-09-14 16:43:34 -0700222 delay = ns;
223 return ns;
Mathias Agopianf001c922010-11-11 17:58:51 -0800224}
225
226// ---------------------------------------------------------------------------
227}; // namespace android
228