Home | History | Annotate | Download | only in libsensors
      1 /*
      2  * Copyright (C) 2008 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 <fcntl.h>
     18 #include <errno.h>
     19 #include <math.h>
     20 #include <poll.h>
     21 #include <unistd.h>
     22 #include <dirent.h>
     23 #include <sys/select.h>
     24 
     25 #include <cutils/log.h>
     26 
     27 #include "ProximitySensor.h"
     28 
     29 /*****************************************************************************/
     30 
     31 ProximitySensor::ProximitySensor()
     32     : SensorBase(NULL, "proximity"),
     33       mEnabled(0),
     34       mInputReader(4),
     35       mHasPendingEvent(false)
     36 {
     37     mPendingEvent.version = sizeof(sensors_event_t);
     38     mPendingEvent.sensor = ID_P;
     39     mPendingEvent.type = SENSOR_TYPE_PROXIMITY;
     40     memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
     41 
     42     if (data_fd) {
     43         strcpy(input_sysfs_path, "/sys/class/input/");
     44         strcat(input_sysfs_path, input_name);
     45         strcat(input_sysfs_path, "/device/");
     46         input_sysfs_path_len = strlen(input_sysfs_path);
     47         enable(0, 1);
     48     }
     49 }
     50 
     51 ProximitySensor::~ProximitySensor() {
     52     if (mEnabled) {
     53         enable(0, 0);
     54     }
     55 }
     56 
     57 int ProximitySensor::setInitialState() {
     58     struct input_absinfo absinfo;
     59     if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_PROXIMITY), &absinfo)) {
     60         // make sure to report an event immediately
     61         mHasPendingEvent = true;
     62         mPendingEvent.distance = indexToValue(absinfo.value);
     63     }
     64     return 0;
     65 }
     66 
     67 int ProximitySensor::enable(int32_t, int en) {
     68     int flags = en ? 1 : 0;
     69     if (flags != mEnabled) {
     70         int fd;
     71         strcpy(&input_sysfs_path[input_sysfs_path_len], "enable");
     72         fd = open(input_sysfs_path, O_RDWR);
     73         if (fd >= 0) {
     74             char buf[2];
     75             buf[1] = 0;
     76             if (flags) {
     77                 buf[0] = '1';
     78             } else {
     79                 buf[0] = '0';
     80             }
     81             write(fd, buf, sizeof(buf));
     82             close(fd);
     83             mEnabled = flags;
     84             setInitialState();
     85             return 0;
     86         }
     87         return -1;
     88     }
     89     return 0;
     90 }
     91 
     92 bool ProximitySensor::hasPendingEvents() const {
     93     return mHasPendingEvent;
     94 }
     95 
     96 int ProximitySensor::readEvents(sensors_event_t* data, int count)
     97 {
     98     if (count < 1)
     99         return -EINVAL;
    100 
    101     if (mHasPendingEvent) {
    102         mHasPendingEvent = false;
    103         mPendingEvent.timestamp = getTimestamp();
    104         *data = mPendingEvent;
    105         return mEnabled ? 1 : 0;
    106     }
    107 
    108     ssize_t n = mInputReader.fill(data_fd);
    109     if (n < 0)
    110         return n;
    111 
    112     int numEventReceived = 0;
    113     input_event const* event;
    114 
    115     while (count && mInputReader.readEvent(&event)) {
    116         int type = event->type;
    117         if (type == EV_ABS) {
    118             if (event->code == EVENT_TYPE_PROXIMITY) {
    119                 if (event->value != -1) {
    120                     // FIXME: not sure why we're getting -1 sometimes
    121                     mPendingEvent.distance = indexToValue(event->value);
    122                 }
    123             }
    124         } else if (type == EV_SYN) {
    125             mPendingEvent.timestamp = timevalToNano(event->time);
    126             if (mEnabled) {
    127                 *data++ = mPendingEvent;
    128                 count--;
    129                 numEventReceived++;
    130             }
    131         } else {
    132             ALOGE("ProximitySensor: unknown event (type=%d, code=%d)",
    133                     type, event->code);
    134         }
    135         mInputReader.next();
    136     }
    137 
    138     return numEventReceived;
    139 }
    140 
    141 float ProximitySensor::indexToValue(size_t index) const
    142 {
    143     return index * PROXIMITY_THRESHOLD_GP2A;
    144 }
    145