1 /*
2 * Copyright (C) 2012 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 #define LOG_TAG "Sensors"
18 //#define FUNC_LOG ALOGV("%s", __PRETTY_FUNCTION__)
19 #define FUNC_LOG
20
21 #include <hardware/sensors.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <dirent.h>
25 #include <math.h>
26 #include <poll.h>
27 #include <pthread.h>
28 #include <stdlib.h>
29
30 #include <utils/Atomic.h>
31 #include <utils/Log.h>
32
33 #include "sensors.h"
34
35 #include "MPLSensor.h"
36 #include "LightSensor.h"
37 #include "PressureSensor.h"
38
39
40 /*****************************************************************************/
41
42 #define DELAY_OUT_TIME 0x7FFFFFFF
43
44 #define SENSORS_ROTATION_VECTOR (1<<ID_RV)
45 #define SENSORS_LINEAR_ACCEL (1<<ID_LA)
46 #define SENSORS_GRAVITY (1<<ID_GR)
47 #define SENSORS_GYROSCOPE (1<<ID_GY)
48 #define SENSORS_ACCELERATION (1<<ID_A)
49 #define SENSORS_MAGNETIC_FIELD (1<<ID_M)
50 #define SENSORS_ORIENTATION (1<<ID_O)
51 #define SENSORS_LIGHT (1<<ID_L)
52 #define SENSORS_PROXIMITY (1<<ID_P)
53 #define SENSORS_PRESSURE (1<<ID_PR)
54
55 #define SENSORS_ROTATION_VECTOR_HANDLE (ID_RV)
56 #define SENSORS_LINEAR_ACCEL_HANDLE (ID_LA)
57 #define SENSORS_GRAVITY_HANDLE (ID_GR)
58 #define SENSORS_GYROSCOPE_HANDLE (ID_GY)
59 #define SENSORS_RAW_GYROSCOPE_HANDLE (ID_RG)
60 #define SENSORS_ACCELERATION_HANDLE (ID_A)
61 #define SENSORS_MAGNETIC_FIELD_HANDLE (ID_M)
62 #define SENSORS_ORIENTATION_HANDLE (ID_O)
63 #define SENSORS_LIGHT_HANDLE (ID_L)
64 #define SENSORS_PROXIMITY_HANDLE (ID_P)
65 #define SENSORS_PRESSURE_HANDLE (ID_PR)
66 #define AKM_FTRACE 0
67 #define AKM_DEBUG 0
68 #define AKM_DATA 0
69
70 /*****************************************************************************/
71
72 /* The SENSORS Module */
73 #define LOCAL_SENSORS (2)
74 static struct sensor_t sSensorList[LOCAL_SENSORS + MPLSensor::numSensors] = {
75 { "BH1721fvc Light sensor",
76 "Rohm",
77 1, SENSORS_LIGHT_HANDLE,
78 SENSOR_TYPE_LIGHT, 65528.0f, 1.0f, 0.20f, 16000, { } },
79 { "BMP182 Pressure sensor",
80 "Bosch",
81 1, SENSORS_PRESSURE_HANDLE,
82 SENSOR_TYPE_PRESSURE, 1100.0f, 0.01f, 0.06f, 50000, { } },
83 };
84 static int numSensors = LOCAL_SENSORS;
85
86 static int open_sensors(const struct hw_module_t* module, const char* id,
87 struct hw_device_t** device);
88
89
sensors__get_sensors_list(struct sensors_module_t * module,struct sensor_t const ** list)90 static int sensors__get_sensors_list(struct sensors_module_t* module,
91 struct sensor_t const** list)
92 {
93 *list = sSensorList;
94 return numSensors;
95 }
96
97 static struct hw_module_methods_t sensors_module_methods = {
98 open: open_sensors
99 };
100
101 struct sensors_module_t HAL_MODULE_INFO_SYM = {
102 common: {
103 tag: HARDWARE_MODULE_TAG,
104 version_major: 1,
105 version_minor: 0,
106 id: SENSORS_HARDWARE_MODULE_ID,
107 name: "Samsung Sensor module",
108 author: "Samsung Electronic Company",
109 methods: &sensors_module_methods,
110 dso: 0,
111 reserved: {},
112 },
113 get_sensors_list: sensors__get_sensors_list,
114 };
115
116 struct sensors_poll_context_t {
117 struct sensors_poll_device_t device; // must be first
118
119 sensors_poll_context_t();
120 ~sensors_poll_context_t();
121 int activate(int handle, int enabled);
122 int setDelay(int handle, int64_t ns);
123 int pollEvents(sensors_event_t* data, int count);
124
125 // Will return true if the constructor completed
isValidsensors_poll_context_t126 bool isValid() { return mInitialized; };
127
128 private:
129 // Will be true if the constructor completed
130 bool mInitialized;
131
132 enum {
133 mpl = 0,
134 compass,
135 light,
136 pressure,
137 numSensorDrivers, // wake pipe goes here
138 numFds,
139 };
140
141 static const size_t wake = numFds - 1;
142 static const char WAKE_MESSAGE = 'W';
143 struct pollfd mPollFds[numFds];
144 int mWritePipeFd;
145 SensorBase* mSensors[numSensorDrivers];
146
handleToDriversensors_poll_context_t147 int handleToDriver(int handle) const {
148 switch (handle) {
149 case ID_RV:
150 case ID_LA:
151 case ID_GR:
152 case ID_GY:
153 case ID_RG:
154 case ID_A:
155 case ID_M:
156 case ID_O:
157 return mpl;
158 case ID_L:
159 return light;
160 case ID_PR:
161 return pressure;
162 }
163 return -EINVAL;
164 }
165 };
166
167 /*****************************************************************************/
168
sensors_poll_context_t()169 sensors_poll_context_t::sensors_poll_context_t()
170 {
171 FUNC_LOG;
172 CompassSensor *p_compasssensor = new CompassSensor();
173 MPLSensor *p_mplsen = new MPLSensor(p_compasssensor);
174 mInitialized = false;
175 // Must clean this up early or else the destructor will make a mess.
176 memset(mSensors, 0, sizeof(mSensors));
177
178 if (!p_mplsen->isValid()) {
179 delete p_compasssensor;
180 return;
181 }
182
183 setCallbackObject(p_mplsen); //setup the callback object for handing mpl callbacks
184 numSensors =
185 LOCAL_SENSORS +
186 p_mplsen->populateSensorList(sSensorList + LOCAL_SENSORS,
187 sizeof(sSensorList[0]) * (ARRAY_SIZE(sSensorList) - LOCAL_SENSORS));
188
189 mSensors[mpl] = p_mplsen;
190 mPollFds[mpl].fd = mSensors[mpl]->getFd();
191 mPollFds[mpl].events = POLLIN;
192 mPollFds[mpl].revents = 0;
193
194 mSensors[compass] = p_mplsen;
195 mPollFds[compass].fd = ((MPLSensor*)mSensors[mpl])->getCompassFd();
196 mPollFds[compass].events = POLLIN;
197 mPollFds[compass].revents = 0;
198
199 mSensors[light] = new LightSensor();
200 mPollFds[light].fd = mSensors[light]->getFd();
201 mPollFds[light].events = POLLIN;
202 mPollFds[light].revents = 0;
203
204 mSensors[pressure] = new PressureSensor();
205 mPollFds[pressure].fd = mSensors[pressure]->getFd();
206 mPollFds[pressure].events = POLLIN;
207 mPollFds[pressure].revents = 0;
208
209 int wakeFds[2];
210 int result = pipe(wakeFds);
211 ALOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno));
212 fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);
213 fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);
214 mWritePipeFd = wakeFds[1];
215
216 mPollFds[wake].fd = wakeFds[0];
217 mPollFds[wake].events = POLLIN;
218 mPollFds[wake].revents = 0;
219 mInitialized = true;
220 }
221
~sensors_poll_context_t()222 sensors_poll_context_t::~sensors_poll_context_t()
223 {
224 FUNC_LOG;
225 for (int i=0 ; i<numSensorDrivers ; i++) {
226 delete mSensors[i];
227 }
228 close(mPollFds[wake].fd);
229 close(mWritePipeFd);
230 mInitialized = false;
231 }
232
activate(int handle,int enabled)233 int sensors_poll_context_t::activate(int handle, int enabled)
234 {
235 FUNC_LOG;
236 if (!mInitialized) return -EINVAL;
237 int index = handleToDriver(handle);
238 if (index < 0) return index;
239 int err = mSensors[index]->enable(handle, enabled);
240 if (!err) {
241 const char wakeMessage(WAKE_MESSAGE);
242 int result = write(mWritePipeFd, &wakeMessage, 1);
243 ALOGE_IF(result<0, "error sending wake message (%s)", strerror(errno));
244 }
245 return err;
246 }
247
setDelay(int handle,int64_t ns)248 int sensors_poll_context_t::setDelay(int handle, int64_t ns)
249 {
250 FUNC_LOG;
251 int index = handleToDriver(handle);
252 if (index < 0) return index;
253 return mSensors[index]->setDelay(handle, ns);
254 }
255
pollEvents(sensors_event_t * data,int count)256 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
257 {
258 //FUNC_LOG;
259 int nbEvents = 0;
260 int n = 0;
261 int polltime = -1;
262 do {
263 for (int i=0 ; count && i<numSensorDrivers ; i++) {
264 SensorBase* const sensor(mSensors[i]);
265 // See if we have some pending events from the last poll()
266 if ((mPollFds[i].revents & (POLLIN | POLLPRI)) || (sensor->hasPendingEvents())) {
267 int nb;
268 if (i == compass) {
269 /* result is hardcoded to 0 */
270 ((MPLSensor*) sensor)->readCompassEvents(NULL, count);
271 nb = ((MPLSensor*) mSensors[mpl])->executeOnData(data, count);
272 }
273 else if (i == mpl) {
274 /* result is hardcoded to 0 */
275 sensor->readEvents(NULL, count);
276 nb = ((MPLSensor*) mSensors[mpl])->executeOnData(data, count);
277 mPollFds[i].revents = 0;
278 }
279 else {
280 nb = sensor->readEvents(data, count);
281 }
282 if (nb < count) {
283 // no more data for this sensor
284 mPollFds[i].revents = 0;
285 }
286 count -= nb;
287 nbEvents += nb;
288 data += nb;
289 }
290 }
291 if (count) {
292 do {
293 n = poll(mPollFds, numFds, nbEvents ? 0 : polltime);
294 } while (n < 0 && errno == EINTR);
295 if (n < 0) {
296 ALOGE("poll() failed (%s)", strerror(errno));
297 return -errno;
298 }
299 if (mPollFds[wake].revents & (POLLIN | POLLPRI)) {
300 char msg;
301 int result = read(mPollFds[wake].fd, &msg, 1);
302 ALOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno));
303 ALOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
304 mPollFds[wake].revents = 0;
305 }
306 }
307 // if we have events and space, go read them
308 } while (n && count);
309
310 return nbEvents;
311 }
312
313 /*****************************************************************************/
314
poll__close(struct hw_device_t * dev)315 static int poll__close(struct hw_device_t *dev)
316 {
317 FUNC_LOG;
318 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
319 if (ctx) {
320 delete ctx;
321 }
322 return 0;
323 }
324
poll__activate(struct sensors_poll_device_t * dev,int handle,int enabled)325 static int poll__activate(struct sensors_poll_device_t *dev,
326 int handle, int enabled)
327 {
328 FUNC_LOG;
329 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
330 return ctx->activate(handle, enabled);
331 }
332
poll__setDelay(struct sensors_poll_device_t * dev,int handle,int64_t ns)333 static int poll__setDelay(struct sensors_poll_device_t *dev,
334 int handle, int64_t ns)
335 {
336 FUNC_LOG;
337 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
338 return ctx->setDelay(handle, ns);
339 }
340
poll__poll(struct sensors_poll_device_t * dev,sensors_event_t * data,int count)341 static int poll__poll(struct sensors_poll_device_t *dev,
342 sensors_event_t* data, int count)
343 {
344 FUNC_LOG;
345 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
346 return ctx->pollEvents(data, count);
347 }
348
349 /*****************************************************************************/
350
351 /** Open a new instance of a sensor device using name */
open_sensors(const struct hw_module_t * module,const char * id,struct hw_device_t ** device)352 static int open_sensors(const struct hw_module_t* module, const char* id,
353 struct hw_device_t** device)
354 {
355 FUNC_LOG;
356 int status = -EINVAL;
357 sensors_poll_context_t *dev = new sensors_poll_context_t();
358
359 if (!dev->isValid()) {
360 ALOGE("Failed to open the sensors");
361 return status;
362 }
363
364 memset(&dev->device, 0, sizeof(sensors_poll_device_t));
365
366 dev->device.common.tag = HARDWARE_DEVICE_TAG;
367 dev->device.common.version = 0;
368 dev->device.common.module = const_cast<hw_module_t*>(module);
369 dev->device.common.close = poll__close;
370 dev->device.activate = poll__activate;
371 dev->device.setDelay = poll__setDelay;
372 dev->device.poll = poll__poll;
373
374 *device = &dev->device.common;
375 status = 0;
376
377 return status;
378 }
379