-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnusensors.cpp
298 lines (248 loc) · 8.61 KB
/
nusensors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright (C) 2008 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.
*/
#define LOG_TAG "Sensors"
#include <hardware/sensors.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <math.h>
#include <poll.h>
#include <pthread.h>
#include <linux/input.h>
#include <cutils/atomic.h>
#include <cutils/log.h>
#include "nusensors.h"
#include "Hwmsen.h"
#include "Acceleration.h"
#include "Magnetic.h"
#include "Gyroscope.h"
#undef NDEBUG
/*****************************************************************************/
struct sensors_poll_context_t {
struct sensors_poll_device_t device; // must be first
sensors_poll_context_t();
~sensors_poll_context_t();
int activate(int handle, int enabled);
int setDelay(int handle, int64_t ns);
int pollEvents(sensors_event_t* data, int count);
private:
enum {
hwmsen = 0,
accel,
magnetic,
gyro,
//light,
//proximity,
//pressure,
numSensorDrivers,
numFds,
};
int handleToDriver(int handle) const {
switch (handle) {
case ID_ACCELEROMETER:
return accel;
case ID_MAGNETIC:
case ID_ORIENTATION:
return magnetic;
case ID_PROXIMITY:
//return proximity;
case ID_LIGHT:
//return light;
case ID_GYROSCOPE:
return gyro;
case ID_PRESSURE:
case ID_STEP_COUNTER:
case ID_TEMPRERATURE:
case ID_RELATIVE_HUMIDITY:
break;
//return pressure;
}
return -EINVAL;
}
static const size_t wake = numFds - 1;
static const char WAKE_MESSAGE = 'W';
struct pollfd mPollFds[numFds];
int mWritePipeFd;
SensorBase* mSensors[numSensorDrivers];
};
/*****************************************************************************/
sensors_poll_context_t::sensors_poll_context_t()
{
mSensors[hwmsen] = new Hwmsen();
mPollFds[hwmsen].fd = ((Hwmsen *)mSensors[hwmsen])->mdata_fd;
mPollFds[hwmsen].events = POLLIN;
mPollFds[hwmsen].revents = 0;
mSensors[accel] = new AccelerationSensor();
mPollFds[accel].fd = ((AccelerationSensor*)mSensors[accel])->mdata_fd;
mPollFds[accel].events = POLLIN;
mPollFds[accel].revents = 0;
mSensors[magnetic] = new MagneticSensor();
mPollFds[magnetic].fd = ((MagneticSensor*)mSensors[magnetic])->mdata_fd;
mPollFds[magnetic].events = POLLIN;
mPollFds[magnetic].revents = 0;
mSensors[gyro] = new GyroscopeSensor();
mPollFds[gyro].fd = ((GyroscopeSensor*)mSensors[gyro])->mdata_fd;
mPollFds[gyro].events = POLLIN;
mPollFds[gyro].revents = 0;
int wakeFds[2];
int result = pipe(wakeFds);
ALOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno));
fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);
fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);
mWritePipeFd = wakeFds[1];
mPollFds[wake].fd = wakeFds[0];
mPollFds[wake].events = POLLIN;
mPollFds[wake].revents = 0;
}
sensors_poll_context_t::~sensors_poll_context_t() {
for (int i=0 ; i<numSensorDrivers ; i++) {
delete mSensors[i];
}
close(mPollFds[wake].fd);
close(mWritePipeFd);
}
int sensors_poll_context_t::activate(int handle, int enabled)
{
ALOGD( "activate handle =%d, enable = %d",handle, enabled );
int err=0;
int index = handleToDriver(handle);
//
if(ID_ORIENTATION == handle)
{
//ALOGD( "fwq1111" );
((AccelerationSensor*)(mSensors[accel]))->enableNoHALDataAcc(enabled);
((Hwmsen*)(mSensors[hwmsen]))->enableNoHALDataAcc(enabled);
}
if(NULL != mSensors[index] && index >0 )
{
ALOGD( "use new sensor index=%d, mSensors[index](%x)", index, mSensors[index]);
err = mSensors[index]->enable(handle, enabled);
}
if(err || index<0 )
{
ALOGD("use old sensor err(%d),index(%d) go to old hwmsen\n",err,index);
// notify to hwmsen sensor to support old architecture
err = mSensors[hwmsen]->enable(handle, enabled);
}
if (enabled && !err) {
const char wakeMessage(WAKE_MESSAGE);
int result = write(mWritePipeFd, &wakeMessage, 1);
ALOGE_IF(result<0, "error sending wake message (%s)", strerror(errno));
}
return err;
}
int sensors_poll_context_t::setDelay(int handle, int64_t ns)
{
int err =0;
int index = handleToDriver(handle);
if(NULL != mSensors[index] && index >0)
{
err = mSensors[index]->setDelay(handle, ns);
}
if(err || index<0)
{
ALOGE("new acc setDelay handle(%d),ns(%lld) err! go to hwmsen\n",handle,ns);
// notify to hwmsen sensor to support old architecture
err = mSensors[hwmsen]->setDelay(handle, ns);
}
return err;
}
int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
{
int nbEvents = 0;
int n = 0;
ALOGE("pollEvents count =%d",count );
do {
// see if we have some leftover from the last poll()
for (int i=0 ; count && i<numSensorDrivers ; i++) {
SensorBase* const sensor(mSensors[i]);
if ((mPollFds[i].revents & POLLIN) || (sensor->hasPendingEvents())) {
int nb = sensor->readEvents(data, count);
if (nb < count) {
// no more data for this sensor
mPollFds[i].revents = 0;
}
//if(nb < 0||nb > count)
// ALOGE("pollEvents count error nb:%d, count:%d, nbEvents:%d", nb, count, nbEvents);//for sensor NE debug
count -= nb;
nbEvents += nb;
data += nb;
//if(nb < 0||nb > count)
// ALOGE("pollEvents count error nb:%d, count:%d, nbEvents:%d", nb, count, nbEvents);//for sensor NE debug
}
}
if (count) {
// we still have some room, so try to see if we can get
// some events immediately or just wait if we don't have
// anything to return
n = poll(mPollFds, numFds, nbEvents ? 0 : -1);
if (n<0) {
ALOGE("poll() failed (%s)", strerror(errno));
return -errno;
}
if (mPollFds[wake].revents & POLLIN) {
char msg;
int result = read(mPollFds[wake].fd, &msg, 1);
ALOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno));
ALOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
mPollFds[wake].revents = 0;
}
}
// if we have events and space, go read them
} while (n && count);
return nbEvents;
}
/*****************************************************************************/
static int poll__close(struct hw_device_t *dev)
{
sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
if (ctx) {
delete ctx;
}
return 0;
}
static int poll__activate(struct sensors_poll_device_t *dev,
int handle, int enabled) {
sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
return ctx->activate(handle, enabled);
}
static int poll__setDelay(struct sensors_poll_device_t *dev,
int handle, int64_t ns) {
sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
return ctx->setDelay(handle, ns);
}
static int poll__poll(struct sensors_poll_device_t *dev,
sensors_event_t* data, int count) {
sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
return ctx->pollEvents(data, count);
}
/*****************************************************************************/
int init_nusensors(hw_module_t const* module, hw_device_t** device)
{
int status = -EINVAL;
sensors_poll_context_t *dev = new sensors_poll_context_t();
memset(&dev->device, 0, sizeof(sensors_poll_device_t));
dev->device.common.tag = HARDWARE_DEVICE_TAG;
dev->device.common.version = 0;
dev->device.common.module = const_cast<hw_module_t*>(module);
dev->device.common.close = poll__close;
dev->device.activate = poll__activate;
dev->device.setDelay = poll__setDelay;
dev->device.poll = poll__poll;
*device = &dev->device.common;
status = 0;
return status;
}