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 #define LOG_TAG "Sensors"
     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 #include <linux/akm8973.h>
     30 
     31 #include <utils/Atomic.h>
     32 #include <utils/Log.h>
     33 
     34 #include "sensors.h"
     35 
     36 #include "LightSensor.h"
     37 #include "ProximitySensor.h"
     38 #include "AkmSensor.h"
     39 #include "GyroSensor.h"
     40 
     41 /*****************************************************************************/
     42 
     43 #define DELAY_OUT_TIME 0x7FFFFFFF
     44 
     45 #define LIGHT_SENSOR_POLLTIME    2000000000
     46 
     47 
     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_GYROSCOPE        (1<<ID_GY)
     54 
     55 #define SENSORS_ACCELERATION_HANDLE     0
     56 #define SENSORS_MAGNETIC_FIELD_HANDLE   1
     57 #define SENSORS_ORIENTATION_HANDLE      2
     58 #define SENSORS_LIGHT_HANDLE            3
     59 #define SENSORS_PROXIMITY_HANDLE        4
     60 #define SENSORS_GYROSCOPE_HANDLE        5
     61 
     62 #define AKM_FTRACE 0
     63 #define AKM_DEBUG 0
     64 #define AKM_DATA 0
     65 
     66 /*****************************************************************************/
     67 
     68 /* The SENSORS Module */
     69 static const struct sensor_t sSensorList[] = {
     70         { "KR3DM 3-axis Accelerometer",
     71           "STMicroelectronics",
     72           1, SENSORS_ACCELERATION_HANDLE,
     73           SENSOR_TYPE_ACCELEROMETER, RANGE_A, CONVERT_A, 0.23f, 20000, { } },
     74         { "AK8973 3-axis Magnetic field sensor",
     75           "Asahi Kasei Microdevices",
     76           1, SENSORS_MAGNETIC_FIELD_HANDLE,
     77           SENSOR_TYPE_MAGNETIC_FIELD, 2000.0f, CONVERT_M, 6.8f, 16667, { } },
     78         { "AK8973 Orientation sensor",
     79           "Asahi Kasei Microdevices",
     80           1, SENSORS_ORIENTATION_HANDLE,
     81           SENSOR_TYPE_ORIENTATION, 360.0f, CONVERT_O, 7.8f, 16667, { } },
     82         { "GP2A Light sensor",
     83           "Sharp",
     84           1, SENSORS_LIGHT_HANDLE,
     85           SENSOR_TYPE_LIGHT, 3000.0f, 1.0f, 0.75f, 0, { } },
     86         { "GP2A Proximity sensor",
     87           "Sharp",
     88           1, SENSORS_PROXIMITY_HANDLE,
     89           SENSOR_TYPE_PROXIMITY, 5.0f, 5.0f, 0.75f, 0, { } },
     90         { "K3G Gyroscope sensor",
     91           "STMicroelectronics",
     92           1, SENSORS_GYROSCOPE_HANDLE,
     93           SENSOR_TYPE_GYROSCOPE, RANGE_GYRO, CONVERT_GYRO, 6.1f, 1190, { } },
     94 };
     95 
     96 
     97 static int open_sensors(const struct hw_module_t* module, const char* id,
     98                         struct hw_device_t** device);
     99 
    100 
    101 static int sensors__get_sensors_list(struct sensors_module_t* module,
    102                                      struct sensor_t const** list)
    103 {
    104         *list = sSensorList;
    105         return ARRAY_SIZE(sSensorList);
    106 }
    107 
    108 static struct hw_module_methods_t sensors_module_methods = {
    109         open: open_sensors
    110 };
    111 
    112 struct sensors_module_t HAL_MODULE_INFO_SYM = {
    113         common: {
    114                 tag: HARDWARE_MODULE_TAG,
    115                 version_major: 1,
    116                 version_minor: 0,
    117                 id: SENSORS_HARDWARE_MODULE_ID,
    118                 name: "Samsung Sensor module",
    119                 author: "Samsung Electronic Company",
    120                 methods: &sensors_module_methods,
    121         },
    122         get_sensors_list: sensors__get_sensors_list,
    123 };
    124 
    125 struct sensors_poll_context_t {
    126     struct sensors_poll_device_t device; // must be first
    127 
    128         sensors_poll_context_t();
    129         ~sensors_poll_context_t();
    130     int activate(int handle, int enabled);
    131     int setDelay(int handle, int64_t ns);
    132     int pollEvents(sensors_event_t* data, int count);
    133 
    134 private:
    135     enum {
    136         light           = 0,
    137         proximity       = 1,
    138         akm             = 2,
    139         gyro            = 3,
    140         numSensorDrivers,
    141         numFds,
    142     };
    143 
    144     static const size_t wake = numFds - 1;
    145     static const char WAKE_MESSAGE = 'W';
    146     struct pollfd mPollFds[numFds];
    147     int mWritePipeFd;
    148     SensorBase* mSensors[numSensorDrivers];
    149 
    150     int handleToDriver(int handle) const {
    151         switch (handle) {
    152             case ID_A:
    153             case ID_M:
    154             case ID_O:
    155                 return akm;
    156             case ID_P:
    157                 return proximity;
    158             case ID_L:
    159                 return light;
    160             case ID_GY:
    161                 return gyro;
    162         }
    163         return -EINVAL;
    164     }
    165 };
    166 
    167 /*****************************************************************************/
    168 
    169 sensors_poll_context_t::sensors_poll_context_t()
    170 {
    171     mSensors[light] = new LightSensor();
    172     mPollFds[light].fd = mSensors[light]->getFd();
    173     mPollFds[light].events = POLLIN;
    174     mPollFds[light].revents = 0;
    175 
    176     mSensors[proximity] = new ProximitySensor();
    177     mPollFds[proximity].fd = mSensors[proximity]->getFd();
    178     mPollFds[proximity].events = POLLIN;
    179     mPollFds[proximity].revents = 0;
    180 
    181     mSensors[akm] = new AkmSensor();
    182     mPollFds[akm].fd = mSensors[akm]->getFd();
    183     mPollFds[akm].events = POLLIN;
    184     mPollFds[akm].revents = 0;
    185 
    186     mSensors[gyro] = new GyroSensor();
    187     mPollFds[gyro].fd = mSensors[gyro]->getFd();
    188     mPollFds[gyro].events = POLLIN;
    189     mPollFds[gyro].revents = 0;
    190 
    191     int wakeFds[2];
    192     int result = pipe(wakeFds);
    193     LOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno));
    194     fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);
    195     fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);
    196     mWritePipeFd = wakeFds[1];
    197 
    198     mPollFds[wake].fd = wakeFds[0];
    199     mPollFds[wake].events = POLLIN;
    200     mPollFds[wake].revents = 0;
    201 }
    202 
    203 sensors_poll_context_t::~sensors_poll_context_t() {
    204     for (int i=0 ; i<numSensorDrivers ; i++) {
    205         delete mSensors[i];
    206     }
    207     close(mPollFds[wake].fd);
    208     close(mWritePipeFd);
    209 }
    210 
    211 int sensors_poll_context_t::activate(int handle, int enabled) {
    212     int index = handleToDriver(handle);
    213     if (index < 0) return index;
    214     int err =  mSensors[index]->enable(handle, enabled);
    215     if (enabled && !err) {
    216         const char wakeMessage(WAKE_MESSAGE);
    217         int result = write(mWritePipeFd, &wakeMessage, 1);
    218         LOGE_IF(result<0, "error sending wake message (%s)", strerror(errno));
    219     }
    220     return err;
    221 }
    222 
    223 int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
    224 
    225     int index = handleToDriver(handle);
    226     if (index < 0) return index;
    227     return mSensors[index]->setDelay(handle, ns);
    228 }
    229 
    230 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
    231 {
    232     int nbEvents = 0;
    233     int n = 0;
    234 
    235     do {
    236         // see if we have some leftover from the last poll()
    237         for (int i=0 ; count && i<numSensorDrivers ; i++) {
    238             SensorBase* const sensor(mSensors[i]);
    239             if ((mPollFds[i].revents & POLLIN) || (sensor->hasPendingEvents())) {
    240                 int nb = sensor->readEvents(data, count);
    241                 if (nb < count) {
    242                     // no more data for this sensor
    243                     mPollFds[i].revents = 0;
    244                 }
    245                 count -= nb;
    246                 nbEvents += nb;
    247                 data += nb;
    248             }
    249         }
    250 
    251         if (count) {
    252             // we still have some room, so try to see if we can get
    253             // some events immediately or just wait if we don't have
    254             // anything to return
    255             n = poll(mPollFds, numFds, nbEvents ? 0 : -1);
    256             if (n<0) {
    257                 LOGE("poll() failed (%s)", strerror(errno));
    258                 return -errno;
    259             }
    260             if (mPollFds[wake].revents & POLLIN) {
    261                 char msg;
    262                 int result = read(mPollFds[wake].fd, &msg, 1);
    263                 LOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno));
    264                 LOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
    265                 mPollFds[wake].revents = 0;
    266             }
    267         }
    268         // if we have events and space, go read them
    269     } while (n && count);
    270 
    271     return nbEvents;
    272 }
    273 
    274 /*****************************************************************************/
    275 
    276 static int poll__close(struct hw_device_t *dev)
    277 {
    278     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
    279     if (ctx) {
    280         delete ctx;
    281     }
    282     return 0;
    283 }
    284 
    285 static int poll__activate(struct sensors_poll_device_t *dev,
    286         int handle, int enabled) {
    287     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
    288     return ctx->activate(handle, enabled);
    289 }
    290 
    291 static int poll__setDelay(struct sensors_poll_device_t *dev,
    292         int handle, int64_t ns) {
    293     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
    294     return ctx->setDelay(handle, ns);
    295 }
    296 
    297 static int poll__poll(struct sensors_poll_device_t *dev,
    298         sensors_event_t* data, int count) {
    299     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
    300     return ctx->pollEvents(data, count);
    301 }
    302 
    303 /*****************************************************************************/
    304 
    305 /** Open a new instance of a sensor device using name */
    306 static int open_sensors(const struct hw_module_t* module, const char* id,
    307                         struct hw_device_t** device)
    308 {
    309         int status = -EINVAL;
    310         sensors_poll_context_t *dev = new sensors_poll_context_t();
    311 
    312         memset(&dev->device, 0, sizeof(sensors_poll_device_t));
    313 
    314         dev->device.common.tag = HARDWARE_DEVICE_TAG;
    315         dev->device.common.version  = 0;
    316         dev->device.common.module   = const_cast<hw_module_t*>(module);
    317         dev->device.common.close    = poll__close;
    318         dev->device.activate        = poll__activate;
    319         dev->device.setDelay        = poll__setDelay;
    320         dev->device.poll            = poll__poll;
    321 
    322         *device = &dev->device.common;
    323         status = 0;
    324 
    325         return status;
    326 }
    327 
    328