• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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 <hardware/sensors.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <dirent.h>
21 #include <math.h>
22 #include <poll.h>
23 #include <pthread.h>
24 #include <stdlib.h>
25 
26 #include <linux/input.h>
27 
28 #include <utils/Atomic.h>
29 #include <utils/Log.h>
30 
31 #include "sensors.h"
32 #include "MPLSensor.h"
33 #include "LightSensor.h"
34 #include "ProximitySensor.h"
35 
36 /*****************************************************************************/
37 /* The SENSORS Module */
38 
39 #ifdef ENABLE_DMP_SCREEN_AUTO_ROTATION
40 #define GLOBAL_SENSORS (MPLSensor::NumSensors + 1)
41 #else
42 #define GLOBAL_SENSORS MPLSensor::NumSensors
43 #endif
44 
45 #define LOCAL_SENSORS (2)
46 
47 #define SENSORS_LIGHT_HANDLE        (ID_L)
48 #define SENSORS_PROXIMITY_HANDLE    (ID_PX)
49 
50 static struct sensor_t sSensorList[GLOBAL_SENSORS + LOCAL_SENSORS] = {
51     {
52         .name       = "Light Sensor",
53         .vendor     = "Avago Technologies",
54         .version    = 1,
55         .handle     = SENSORS_LIGHT_HANDLE,
56         .type       = SENSOR_TYPE_LIGHT,
57         .maxRange   = 30000.0f,
58         .resolution = 1.0f,
59         .power      = 0.5f,
60         .minDelay   = 100000,
61         .reserved   = {}
62     },
63     {
64         .name       = "Proximity Sensor",
65         .vendor     = "Avago Technologies",
66         .version    = 1,
67         .handle     = SENSORS_PROXIMITY_HANDLE,
68         .type       = SENSOR_TYPE_PROXIMITY,
69         .maxRange   = 5.0f,
70         .resolution = 1.0f,
71         .power      = 0.5f,
72         .minDelay   = 100000,
73         .reserved   = {}
74     },
75 };
76 static int sensors = (sizeof(sSensorList) / sizeof(sensor_t));
77 
78 static int open_sensors(const struct hw_module_t* module, const char* id,
79                         struct hw_device_t** device);
80 
sensors__get_sensors_list(struct sensors_module_t * module,struct sensor_t const ** list)81 static int sensors__get_sensors_list(struct sensors_module_t* module,
82                                      struct sensor_t const** list)
83 {
84     *list = sSensorList;
85     return sensors;
86 }
87 
88 static struct hw_module_methods_t sensors_module_methods = {
89         open: open_sensors
90 };
91 
92 struct sensors_module_t HAL_MODULE_INFO_SYM = {
93     common: {
94         tag: HARDWARE_MODULE_TAG,
95         version_major: 1,
96         version_minor: 0,
97         id: SENSORS_HARDWARE_MODULE_ID,
98         name: "LGE Sensor module",
99         author: "LG Electronics Inc.",
100         methods: &sensors_module_methods,
101         dso: NULL,
102         reserved: {0}
103     },
104     get_sensors_list: sensors__get_sensors_list,
105 };
106 
107 struct sensors_poll_context_t {
108     sensors_poll_device_1_t device; // must be first
109 
110     sensors_poll_context_t();
111     ~sensors_poll_context_t();
112     int activate(int handle, int enabled);
113     int setDelay(int handle, int64_t ns);
114     int pollEvents(sensors_event_t* data, int count);
115     int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
116 
117     // return true if the constructor is completed
isValidsensors_poll_context_t118     bool isValid() { return mInitialized; };
119     int flush(int handle);
120 
121 private:
122     enum {
123         mpl = 0,
124         compass,
125         dmpOrient,
126         dmpSign,
127         dmpPed,
128         light,
129         proximity,
130         numSensorDrivers,   // wake pipe goes here
131         numFds,
132     };
133 
134     static const size_t wake = numFds - 1;
135     static const char WAKE_MESSAGE = 'W';
136     struct pollfd mPollFds[numFds];
137     int mWritePipeFd;
138     SensorBase *mSensor[numSensorDrivers];
139     CompassSensor *mCompassSensor;
140     // return true if the constructor is completed
141     bool mInitialized;
142 
handleToDriversensors_poll_context_t143     int handleToDriver(int handle) const {
144         switch (handle) {
145             case ID_GY:
146             case ID_RG:
147             case ID_A:
148             case ID_M:
149             case ID_RM:
150             case ID_PS:
151             case ID_O:
152             case ID_RV:
153             case ID_GRV:
154             case ID_LA:
155             case ID_GR:
156             case ID_SM:
157             case ID_P:
158             case ID_SC:
159             case ID_GMRV:
160             case ID_SO:
161                 return mpl;
162             case ID_L:
163                 return light;
164             case ID_PX:
165                 return proximity;
166         }
167         return -EINVAL;
168     }
169 };
170 
171 /******************************************************************************/
172 
sensors_poll_context_t()173 sensors_poll_context_t::sensors_poll_context_t() {
174     /* TODO: Handle external pressure sensor */
175     mCompassSensor = new CompassSensor();
176     MPLSensor *mplSensor = new MPLSensor(mCompassSensor);
177     mInitialized = false;
178     // Must clean this up early or else the destructor will make a mess.
179     memset(mSensor, 0, sizeof(mSensor));
180 
181     // setup the callback object for handing mpl callbacks
182     setCallbackObject(mplSensor);
183 
184     // populate the sensor list
185     sensors = LOCAL_SENSORS +
186        mplSensor->populateSensorList(sSensorList + LOCAL_SENSORS,
187                sizeof(sSensorList[0]) * (ARRAY_SIZE(sSensorList) - LOCAL_SENSORS));
188 
189     mSensor[mpl] = mplSensor;
190     mPollFds[mpl].fd = mSensor[mpl]->getFd();
191     mPollFds[mpl].events = POLLIN;
192     mPollFds[mpl].revents = 0;
193 
194     mSensor[compass] = mplSensor;
195     mPollFds[compass].fd = mCompassSensor->getFd();
196     mPollFds[compass].events = POLLIN;
197     mPollFds[compass].revents = 0;
198 
199     mSensor[dmpOrient] = mplSensor;
200     mPollFds[dmpOrient].fd = ((MPLSensor*) mSensor[dmpOrient])->getDmpOrientFd();
201     mPollFds[dmpOrient].events = POLLPRI;
202     mPollFds[dmpOrient].revents = 0;
203 
204     mSensor[dmpSign] = mplSensor;
205     mPollFds[dmpSign].fd = ((MPLSensor*) mSensor[dmpSign])->getDmpSignificantMotionFd();
206     mPollFds[dmpSign].events = POLLPRI;
207     mPollFds[dmpSign].revents = 0;
208 
209     mSensor[dmpPed] = mplSensor;
210     mPollFds[dmpPed].fd = ((MPLSensor*) mSensor[dmpPed])->getDmpPedometerFd();
211     mPollFds[dmpPed].events = POLLPRI;
212     mPollFds[dmpPed].revents = 0;
213 
214     mSensor[light] = new LightSensor();
215     mPollFds[light].fd = mSensor[light]->getFd();
216     mPollFds[light].events = POLLIN;
217     mPollFds[light].revents = 0;
218 
219     mSensor[proximity] = new ProximitySensor();
220     mPollFds[proximity].fd = mSensor[proximity]->getFd();
221     mPollFds[proximity].events = POLLIN;
222     mPollFds[proximity].revents = 0;
223 
224     if (mPollFds[light].fd < 0 || mPollFds[proximity].fd < 0) {
225         delete mCompassSensor;
226         return;
227     }
228 
229     /* Timer based sensor initialization */
230     int wakeFds[2];
231     int result = pipe(wakeFds);
232     ALOGE_IF(result < 0, "error creating wake pipe (%s)", strerror(errno));
233     fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);
234     fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);
235     mWritePipeFd = wakeFds[1];
236 
237     mPollFds[wake].fd = wakeFds[0];
238     mPollFds[wake].events = POLLIN;
239     mPollFds[wake].revents = 0;
240     mInitialized = true;
241 }
242 
~sensors_poll_context_t()243 sensors_poll_context_t::~sensors_poll_context_t() {
244     for (int i=0 ; i<numSensorDrivers ; i++) {
245         delete mSensor[i];
246     }
247     delete mCompassSensor;
248     close(mPollFds[wake].fd);
249     close(mWritePipeFd);
250     mInitialized = false;
251 }
252 
activate(int handle,int enabled)253 int sensors_poll_context_t::activate(int handle, int enabled) {
254     if (!mInitialized) return -EINVAL;
255     int index = handleToDriver(handle);
256     if (index < 0) return index;
257     int err =  mSensor[index]->enable(handle, enabled);
258     if (!err) {
259         const char wakeMessage(WAKE_MESSAGE);
260         int result = write(mWritePipeFd, &wakeMessage, 1);
261         ALOGE_IF(result < 0, "error sending wake message (%s)", strerror(errno));
262     }
263     return err;
264 }
265 
setDelay(int handle,int64_t ns)266 int sensors_poll_context_t::setDelay(int handle, int64_t ns)
267 {
268     int index = handleToDriver(handle);
269     if (index < 0) return index;
270     return mSensor[index]->setDelay(handle, ns);
271 }
272 
pollEvents(sensors_event_t * data,int count)273 int sensors_poll_context_t::pollEvents(sensors_event_t *data, int count)
274 {
275     int nbEvents = 0;
276     int n = 0;
277     int nb, polltime = -1;
278 
279     do {
280         for (int i = 0; count && i < numSensorDrivers; i++) {
281             SensorBase* const sensor(mSensor[i]);
282             if (mPollFds[i].revents & (POLLIN | POLLPRI)) {
283                 nb = 0;
284                 if (i == mpl) {
285                     ((MPLSensor*) sensor)->buildMpuEvent();
286                     mPollFds[i].revents = 0;
287                     nb = ((MPLSensor*) sensor)->readEvents(data, count);
288                     if (nb > 0) {
289                         count -= nb;
290                         nbEvents += nb;
291                         data += nb;
292                     }
293                 } else if (i == compass) {
294                     ((MPLSensor*) sensor)->buildCompassEvent();
295                     mPollFds[i].revents = 0;
296                     nb = ((MPLSensor*) sensor)->readEvents(data, count);
297                     if (nb > 0) {
298                         count -= nb;
299                         nbEvents += nb;
300                         data += nb;
301                     }
302                 } else if (i == dmpOrient) {
303                     nb = ((MPLSensor*) sensor)->readDmpOrientEvents(data, count);
304                     mPollFds[dmpOrient].revents= 0;
305                     if (isDmpScreenAutoRotationEnabled() && nb > 0) {
306                         count -= nb;
307                         nbEvents += nb;
308                         data += nb;
309                     }
310                 } else if (i == dmpSign) {
311                     ALOGI("HAL: dmpSign interrupt");
312                     nb = ((MPLSensor*) sensor)->readDmpSignificantMotionEvents(data, count);
313                     mPollFds[i].revents = 0;
314                     count -= nb;
315                     nbEvents += nb;
316                     data += nb;
317                 } else if (i == dmpPed) {
318                     ALOGI("HAL: dmpPed interrupt");
319                     nb = ((MPLSensor*) sensor)->readDmpPedometerEvents(data, count, ID_P, SENSOR_TYPE_STEP_DETECTOR, 0);
320                     mPollFds[i].revents = 0;
321                     count -= nb;
322                     nbEvents += nb;
323                     data += nb;
324                 } else {
325                     // LightSensor and ProximitySensor
326                     nb = sensor->readEvents(data, count);
327                     if (nb < count) {
328                         // no more data for this sensor
329                         mPollFds[i].revents = 0;
330                     }
331                     count -= nb;
332                     nbEvents += nb;
333                     data += nb;
334                 }
335                 ALOGI_IF(0, "sensors_mpl:readEvents() - nb=%d, count=%d, nbEvents=%d, data->timestamp=%lld, data->data[0]=%f,",
336                         nb, count, nbEvents, data->timestamp, data->data[0]);
337             }
338         }
339 
340         /* to see if any step counter events */
341         if (((MPLSensor*) mSensor[mpl])->hasStepCountPendingEvents() == true) {
342             nb = 0;
343             nb = ((MPLSensor*) mSensor[mpl])->readDmpPedometerEvents(data, count, ID_SC, SENSOR_TYPE_STEP_COUNTER, 0);
344             ALOGI_IF(0, "sensors_mpl:readStepCount() - nb=%d, count=%d, nbEvents=%d, data->timestamp=%lld, data->data[0]=%f,",
345                     nb, count, nbEvents, data->timestamp, data->data[0]);
346             if (nb > 0) {
347                 count -= nb;
348                 nbEvents += nb;
349                 data += nb;
350             }
351         }
352         if (count) {
353             do {
354                 n = poll(mPollFds, numFds, nbEvents ? 0 : polltime);
355             } while (n < 0 && errno == EINTR);
356             if (n < 0) {
357                 ALOGE("poll() failed (%s)", strerror(errno));
358                 return -errno;
359             }
360             if (mPollFds[wake].revents & (POLLIN | POLLPRI)) {
361                 char msg;
362                 int result = read(mPollFds[wake].fd, &msg, 1);
363                 ALOGE_IF(result < 0, "error reading from wake pipe (%s)", strerror(errno));
364                 ALOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
365                 mPollFds[wake].revents = 0;
366             }
367         }
368     } while (n && count);
369 
370     return nbEvents;
371 }
372 
batch(int handle,int flags,int64_t period_ns,int64_t timeout)373 int sensors_poll_context_t::batch(int handle, int flags, int64_t period_ns, int64_t timeout)
374 {
375     int index = handleToDriver(handle);
376     if (index < 0) return index;
377     return mSensor[index]->batch(handle, flags, period_ns, timeout);
378 }
379 
flush(int handle)380 int sensors_poll_context_t::flush(int handle)
381 {
382     int index = handleToDriver(handle);
383     if (index < 0) return index;
384     return mSensor[index]->flush(handle);
385 }
386 /******************************************************************************/
387 
poll__close(struct hw_device_t * dev)388 static int poll__close(struct hw_device_t *dev)
389 {
390     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
391     if (ctx) {
392         delete ctx;
393     }
394     return 0;
395 }
396 
poll__activate(struct sensors_poll_device_t * dev,int handle,int enabled)397 static int poll__activate(struct sensors_poll_device_t *dev,
398                           int handle, int enabled)
399 {
400     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
401     return ctx->activate(handle, enabled);
402 }
403 
poll__setDelay(struct sensors_poll_device_t * dev,int handle,int64_t ns)404 static int poll__setDelay(struct sensors_poll_device_t *dev,
405                           int handle, int64_t ns)
406 {
407     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
408     int s= ctx->setDelay(handle, ns);
409     return s;
410 }
411 
poll__poll(struct sensors_poll_device_t * dev,sensors_event_t * data,int count)412 static int poll__poll(struct sensors_poll_device_t *dev,
413                       sensors_event_t* data, int count)
414 {
415     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
416     return ctx->pollEvents(data, count);
417 }
418 
poll__batch(struct sensors_poll_device_1 * dev,int handle,int flags,int64_t period_ns,int64_t timeout)419 static int poll__batch(struct sensors_poll_device_1 *dev,
420                       int handle, int flags, int64_t period_ns, int64_t timeout)
421 {
422     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
423     return ctx->batch(handle, flags, period_ns, timeout);
424 }
425 
poll__flush(struct sensors_poll_device_1 * dev,int handle)426 static int poll__flush(struct sensors_poll_device_1 *dev,
427                       int handle)
428 {
429     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
430     return ctx->flush(handle);
431 }
432 /******************************************************************************/
433 
434 /** 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)435 static int open_sensors(const struct hw_module_t* module, const char* id,
436                         struct hw_device_t** device)
437 {
438     int status = -EINVAL;
439     sensors_poll_context_t *dev = new sensors_poll_context_t();
440 
441     if (!dev->isValid()) {
442         ALOGE("Failed to open the sensors");
443         return status;
444     }
445 
446     memset(&dev->device, 0, sizeof(sensors_poll_device_1));
447 
448     dev->device.common.tag = HARDWARE_DEVICE_TAG;
449     dev->device.common.version  = SENSORS_DEVICE_API_VERSION_1_0;
450     dev->device.common.module   = const_cast<hw_module_t*>(module);
451     dev->device.common.close    = poll__close;
452     dev->device.activate        = poll__activate;
453     dev->device.setDelay        = poll__setDelay;
454     dev->device.poll            = poll__poll;
455 
456     /* Batch processing */
457     dev->device.batch           = poll__batch;
458     dev->device.flush           = poll__flush;
459 
460     *device = &dev->device.common;
461     status = 0;
462 
463     return status;
464 }
465