blob: f15454cb90ba42db33eac7c5084eaf66671d5c1a [file] [log] [blame]
Nick Vaccaroc3d4ca92013-12-05 17:24:12 -08001/*
Nick Vaccaroa73d5742014-03-27 12:31:34 -07002* Copyright (C) 2014 Invensense, Inc.
Nick Vaccaroc3d4ca92013-12-05 17:24:12 -08003*
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#define FUNC_LOG LOGV("%s", __PRETTY_FUNCTION__)
18
19#include <hardware/sensors.h>
20#include <fcntl.h>
21#include <errno.h>
22#include <dirent.h>
23#include <math.h>
24#include <poll.h>
25#include <pthread.h>
26#include <stdlib.h>
27
28#include <linux/input.h>
29
30#include <utils/Atomic.h>
31#include <utils/Log.h>
32
33#include "sensors.h"
34#include "MPLSensor.h"
35
36/*
37 * Vendor-defined Accel Load Calibration File Method
38 * @param[out] Accel bias, length 3. In HW units scaled by 2^16 in body frame
39 * @return '0' for a successful load, '1' otherwise
40 * example: int AccelLoadConfig(long* offset);
41 * End of Vendor-defined Accel Load Cal Method
42 */
43
44/*****************************************************************************/
45/* The SENSORS Module */
46
47#ifdef ENABLE_DMP_SCREEN_AUTO_ROTATION
48#define LOCAL_SENSORS (NumSensors + 1)
49#else
50#define LOCAL_SENSORS (NumSensors)
51#endif
52
53static struct sensor_t sSensorList[LOCAL_SENSORS];
54static int sensors = (sizeof(sSensorList) / sizeof(sensor_t));
55
56static int open_sensors(const struct hw_module_t* module, const char* id,
57 struct hw_device_t** device);
58
59static int sensors__get_sensors_list(struct sensors_module_t* module,
60 struct sensor_t const** list)
61{
62 *list = sSensorList;
63 return sensors;
64}
65
66static struct hw_module_methods_t sensors_module_methods = {
67 open: open_sensors
68};
69
70struct sensors_module_t HAL_MODULE_INFO_SYM = {
71 common: {
72 tag: HARDWARE_MODULE_TAG,
73 version_major: 1,
74 version_minor: 0,
75 id: SENSORS_HARDWARE_MODULE_ID,
76 name: "Invensense module",
77 author: "Invensense Inc.",
78 methods: &sensors_module_methods,
79 dso: NULL,
80 reserved: {0}
81 },
82 get_sensors_list: sensors__get_sensors_list,
83};
84
85struct sensors_poll_context_t {
86 sensors_poll_device_1_t device; // must be first
87
88 sensors_poll_context_t();
89 ~sensors_poll_context_t();
90 int activate(int handle, int enabled);
91 int setDelay(int handle, int64_t ns);
92 int pollEvents(sensors_event_t* data, int count);
93 int query(int what, int *value);
94 int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
95#if defined ANDROID_KITKAT
96 int flush(int handle);
97#endif
98
99private:
100 enum {
101 mpl = 0,
102 compass,
103 dmpOrient,
104 dmpSign,
105 dmpPed,
Nick Vaccaroa73d5742014-03-27 12:31:34 -0700106 numSensorDrivers,
Nick Vaccaroc3d4ca92013-12-05 17:24:12 -0800107 numFds,
108 };
109
110 struct pollfd mPollFds[numFds];
111 SensorBase *mSensor;
112 CompassSensor *mCompassSensor;
Nick Vaccaroc3d4ca92013-12-05 17:24:12 -0800113};
114
115/******************************************************************************/
116
117sensors_poll_context_t::sensors_poll_context_t() {
118 VFUNC_LOG;
119
120 /* TODO: Handle external pressure sensor */
121 mCompassSensor = new CompassSensor();
122 MPLSensor *mplSensor = new MPLSensor(mCompassSensor);
123
124 /* For Vendor-defined Accel Calibration File Load
125 * Use the Following Constructor and Pass Your Load Cal File Function
126 *
127 * MPLSensor *mplSensor = new MPLSensor(mCompassSensor, AccelLoadConfig);
128 */
129
130 // populate the sensor list
131 sensors =
132 mplSensor->populateSensorList(sSensorList, sizeof(sSensorList));
133
134 mSensor = mplSensor;
135 mPollFds[mpl].fd = mSensor->getFd();
136 mPollFds[mpl].events = POLLIN;
137 mPollFds[mpl].revents = 0;
138
139 mPollFds[compass].fd = mCompassSensor->getFd();
140 mPollFds[compass].events = POLLIN;
141 mPollFds[compass].revents = 0;
142
143 mPollFds[dmpOrient].fd = ((MPLSensor*) mSensor)->getDmpOrientFd();
144 mPollFds[dmpOrient].events = POLLPRI;
145 mPollFds[dmpOrient].revents = 0;
146
147 mPollFds[dmpSign].fd = ((MPLSensor*) mSensor)->getDmpSignificantMotionFd();
148 mPollFds[dmpSign].events = POLLPRI;
149 mPollFds[dmpSign].revents = 0;
150
151 mPollFds[dmpPed].fd = ((MPLSensor*) mSensor)->getDmpPedometerFd();
152 mPollFds[dmpPed].events = POLLPRI;
Nick Vaccaroa73d5742014-03-27 12:31:34 -0700153 mPollFds[dmpPed].revents = 0;
Nick Vaccaroc3d4ca92013-12-05 17:24:12 -0800154}
155
156sensors_poll_context_t::~sensors_poll_context_t() {
157 FUNC_LOG;
158 delete mSensor;
159 delete mCompassSensor;
160 for (int i = 0; i < numSensorDrivers; i++) {
161 close(mPollFds[i].fd);
162 }
Nick Vaccaroc3d4ca92013-12-05 17:24:12 -0800163}
164
165int sensors_poll_context_t::activate(int handle, int enabled) {
166 FUNC_LOG;
167
168 int err;
Nick Vaccaroa73d5742014-03-27 12:31:34 -0700169 err = mSensor->enable(handle, enabled);
Nick Vaccaroc3d4ca92013-12-05 17:24:12 -0800170 return err;
171}
172
173int sensors_poll_context_t::setDelay(int handle, int64_t ns)
174{
175 FUNC_LOG;
176 return mSensor->setDelay(handle, ns);
177}
178
179int sensors_poll_context_t::pollEvents(sensors_event_t *data, int count)
180{
181 VHANDLER_LOG;
182
183 int nbEvents = 0;
184 int nb, polltime = -1;
185
186 polltime = ((MPLSensor*) mSensor)->getStepCountPollTime();
187
188 // look for new events
189 nb = poll(mPollFds, numSensorDrivers, polltime);
190 LOGI_IF(0, "poll nb=%d, count=%d, pt=%d", nb, count, polltime);
191 if (nb > 0) {
192 for (int i = 0; count && i < numSensorDrivers; i++) {
193 if (mPollFds[i].revents & (POLLIN | POLLPRI)) {
194 nb = 0;
195 if (i == mpl) {
196 ((MPLSensor*) mSensor)->buildMpuEvent();
197 mPollFds[i].revents = 0;
198 } else if (i == compass) {
199 ((MPLSensor*) mSensor)->buildCompassEvent();
200 mPollFds[i].revents = 0;
201 } else if (i == dmpOrient) {
202 nb = ((MPLSensor*)mSensor)->
203 readDmpOrientEvents(data, count);
204 mPollFds[dmpOrient].revents= 0;
205 if (isDmpScreenAutoRotationEnabled() && nb > 0) {
206 count -= nb;
207 nbEvents += nb;
208 data += nb;
209 }
210 } else if (i == dmpSign) {
211 nb = ((MPLSensor*) mSensor)->
212 readDmpSignificantMotionEvents(data, count);
213 mPollFds[i].revents = 0;
214 count -= nb;
215 nbEvents += nb;
216 data += nb;
217 } else if (i == dmpPed) {
218 nb = ((MPLSensor*) mSensor)->readDmpPedometerEvents(
219 data, count, ID_P, 0);
220 mPollFds[i].revents = 0;
221 count -= nb;
222 nbEvents += nb;
223 data += nb;
224 }
225 if(nb == 0) {
226 nb = ((MPLSensor*) mSensor)->readEvents(data, count);
227 LOGI_IF(0, "sensors_mpl:readEvents() - "
228 "i=%d, nb=%d, count=%d, nbEvents=%d, "
229 "data->timestamp=%lld, data->data[0]=%f,",
230 i, nb, count, nbEvents, data->timestamp,
231 data->data[0]);
232 if (nb > 0) {
233 count -= nb;
234 nbEvents += nb;
235 data += nb;
236 }
237 }
238 }
239 }
240
241 /* to see if any step counter events */
242 if(((MPLSensor*) mSensor)->hasStepCountPendingEvents() == true) {
243 nb = 0;
244 nb = ((MPLSensor*) mSensor)->readDmpPedometerEvents(
245 data, count, ID_SC, 0);
246 LOGI_IF(SensorBase::HANDLER_DATA, "sensors_mpl:readStepCount() - "
247 "nb=%d, count=%d, nbEvents=%d, data->timestamp=%lld, ",
248 nb, count, nbEvents, data->timestamp);
249 if (nb > 0) {
250 count -= nb;
251 nbEvents += nb;
252 data += nb;
253 }
254 }
255 } else if(nb == 0) {
256 /* to see if any step counter events */
257 if(((MPLSensor*) mSensor)->hasStepCountPendingEvents() == true) {
258 nb = 0;
259 nb = ((MPLSensor*) mSensor)->readDmpPedometerEvents(
260 data, count, ID_SC, 0);
261 LOGI_IF(SensorBase::HANDLER_DATA, "sensors_mpl:readStepCount() - "
262 "nb=%d, count=%d, nbEvents=%d, data->timestamp=%lld, ",
263 nb, count, nbEvents, data->timestamp);
264 if (nb > 0) {
265 count -= nb;
266 nbEvents += nb;
267 data += nb;
268 }
269 }
270
271 if (mPollFds[numSensorDrivers].revents & POLLIN) {
272 char msg;
273 int result = read(mPollFds[numSensorDrivers].fd, &msg, 1);
274 LOGE_IF(result < 0,
275 "error reading from wake pipe (%s)", strerror(errno));
276 mPollFds[numSensorDrivers].revents = 0;
277 }
278 }
279 return nbEvents;
280}
281
282int sensors_poll_context_t::query(int what, int* value)
283{
284 FUNC_LOG;
285 return mSensor->query(what, value);
286}
287
288int sensors_poll_context_t::batch(int handle, int flags, int64_t period_ns,
289 int64_t timeout)
290{
291 FUNC_LOG;
292 return mSensor->batch(handle, flags, period_ns, timeout);
293}
294
295#if defined ANDROID_KITKAT
296int sensors_poll_context_t::flush(int handle)
297{
298 FUNC_LOG;
299 return mSensor->flush(handle);
300}
301#endif
302
303/******************************************************************************/
304
305static int poll__close(struct hw_device_t *dev)
306{
307 FUNC_LOG;
308 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
309 if (ctx) {
310 delete ctx;
311 }
312 return 0;
313}
314
315static int poll__activate(struct sensors_poll_device_t *dev,
316 int handle, int enabled)
317{
318 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
319 return ctx->activate(handle, enabled);
320}
321
322static int poll__setDelay(struct sensors_poll_device_t *dev,
323 int handle, int64_t ns)
324{
325 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
326 int s= ctx->setDelay(handle, ns);
327 return s;
328}
329
330static int poll__poll(struct sensors_poll_device_t *dev,
331 sensors_event_t* data, int count)
332{
333 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
334 return ctx->pollEvents(data, count);
335}
336
337static int poll__query(struct sensors_poll_device_1 *dev,
338 int what, int *value)
339{
340 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
341 return ctx->query(what, value);
342}
343
344static int poll__batch(struct sensors_poll_device_1 *dev,
345 int handle, int flags, int64_t period_ns, int64_t timeout)
346{
347 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
348 return ctx->batch(handle, flags, period_ns, timeout);
349}
350
351#if defined ANDROID_KITKAT
352static int poll__flush(struct sensors_poll_device_1 *dev,
353 int handle)
354{
355 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
356 return ctx->flush(handle);
357}
358#endif
359/******************************************************************************/
360
361/** Open a new instance of a sensor device using name */
362static int open_sensors(const struct hw_module_t* module, const char* id,
363 struct hw_device_t** device)
364{
365 FUNC_LOG;
366 int status = -EINVAL;
367 sensors_poll_context_t *dev = new sensors_poll_context_t();
368
369 memset(&dev->device, 0, sizeof(sensors_poll_device_1));
370
371 dev->device.common.tag = HARDWARE_DEVICE_TAG;
372#if defined ANDROID_KITKAT
373 dev->device.common.version = SENSORS_DEVICE_API_VERSION_1_1;
374 dev->device.flush = poll__flush;
375#else
376 dev->device.common.version = SENSORS_DEVICE_API_VERSION_1_0;
377#endif
378 dev->device.common.module = const_cast<hw_module_t*>(module);
379 dev->device.common.close = poll__close;
380 dev->device.activate = poll__activate;
381 dev->device.setDelay = poll__setDelay;
382 dev->device.poll = poll__poll;
383 dev->device.batch = poll__batch;
384
385 *device = &dev->device.common;
386 status = 0;
387
388 return status;
389}