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 #include <string.h>
     28 
     29 #include <linux/input.h>
     30 
     31 #include <utils/Atomic.h>
     32 #include <utils/Log.h>
     33 
     34 #include "sensors.h"
     35 
     36 #if defined SENSORHAL_ACC_ADXL346
     37 #include "AdxlSensor.h"
     38 #elif defined SENSORHAL_ACC_KXTF9
     39 #include "KionixSensor.h"
     40 #else
     41 #error "Sensor configuration ERROR: No sensor is defined."
     42 #endif
     43 
     44 #include "AkmSensor.h"
     45 
     46 /*****************************************************************************/
     47 
     48 #define DELAY_OUT_TIME 0x7FFFFFFF
     49 
     50 #define LIGHT_SENSOR_POLLTIME    2000000000
     51 
     52 
     53 #define SENSORS_ACCELERATION     (1<<ID_A)
     54 #define SENSORS_MAGNETIC_FIELD   (1<<ID_M)
     55 #define SENSORS_ORIENTATION      (1<<ID_O)
     56 
     57 #define SENSORS_ACCELERATION_HANDLE     0
     58 #define SENSORS_MAGNETIC_FIELD_HANDLE   1
     59 #define SENSORS_ORIENTATION_HANDLE      2
     60 
     61 /*****************************************************************************/
     62 
     63 /* The SENSORS Module */
     64 static const struct sensor_t sSensorList[] = {
     65         { "AK8975 3-axis Magnetic field sensor",
     66           "Asahi Kasei Microdevices",
     67           1,
     68 		  SENSORS_MAGNETIC_FIELD_HANDLE,
     69           SENSOR_TYPE_MAGNETIC_FIELD, 1228.8f,
     70 		  CONVERT_M, 0.35f, 10000, 0, 0, 0, 0, 0, 0, { } },
     71 #ifdef SENSORHAL_ACC_ADXL346
     72         { "Analog Devices ADXL345/6 3-axis Accelerometer",
     73           "ADI",
     74           1, SENSORS_ACCELERATION_HANDLE,
     75           SENSOR_TYPE_ACCELEROMETER, (GRAVITY_EARTH * 16.0f),
     76 		  (GRAVITY_EARTH * 16.0f) / 4096.0f, 0.145f, 10000, 0, 0, 0, 0, 0, 0, { } },
     77         { "AK8975 Orientation sensor",
     78           "Asahi Kasei Microdevices",
     79           1, SENSORS_ORIENTATION_HANDLE,
     80           SENSOR_TYPE_ORIENTATION, 360.0f,
     81 		  CONVERT_O, 0.495f, 10000, 0, 0, 0, 0, 0, 0, { } }
     82 #endif
     83 #ifdef SENSORHAL_ACC_KXTF9
     84         { "Kionix KXTF9 3-axis Accelerometer",
     85           "Kionix",
     86           1, SENSORS_ACCELERATION_HANDLE,
     87           SENSOR_TYPE_ACCELEROMETER, (GRAVITY_EARTH * 2.0f),
     88 		  (GRAVITY_EARTH) / 1024.0f, 0.7f, 10000, 0, 0, 0, 0, 0, 0, { } },
     89         { "AK8975 Orientation sensor",
     90           "Asahi Kasei Microdevices",
     91           1, SENSORS_ORIENTATION_HANDLE,
     92           SENSOR_TYPE_ORIENTATION, 360.0f,
     93 		  CONVERT_O, 1.05f, 10000, 0, 0, 0, 0, 0, 0, { } }
     94 #endif
     95 };
     96 
     97 
     98 static int open_sensors(const struct hw_module_t* module, const char* id,
     99                         struct hw_device_t** device);
    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 = "AKM Sensor module",
    119                 .author = "Asahi Kasei Microdevices",
    120                 .methods = &sensors_module_methods,
    121                 .dso  = NULL,
    122                 .reserved = {0},
    123         },
    124         .get_sensors_list = sensors__get_sensors_list,
    125 };
    126 
    127 struct sensors_poll_context_t {
    128     struct sensors_poll_device_t device; // must be first
    129 
    130         sensors_poll_context_t();
    131         ~sensors_poll_context_t();
    132     int activate(int handle, int enabled);
    133     int setDelay(int handle, int64_t ns);
    134     int setDelay_sub(int handle, int64_t ns);
    135     int pollEvents(sensors_event_t* data, int count);
    136 
    137 private:
    138     enum {
    139         acc          = 0,
    140         akm          = 1,
    141         numSensorDrivers,
    142         numFds,
    143     };
    144 
    145     static const size_t wake = numFds - 1;
    146     static const char WAKE_MESSAGE = 'W';
    147     struct pollfd mPollFds[numFds];
    148     int mWritePipeFd;
    149     SensorBase* mSensors[numSensorDrivers];
    150 
    151 	/* These function will be different depends on
    152 	 * which sensor is implemented in AKMD program.
    153 	 */
    154     int handleToDriver(int handle);
    155 	int proxy_enable(int handle, int enabled);
    156 	int proxy_setDelay(int handle, int64_t ns);
    157 };
    158 
    159 /*****************************************************************************/
    160 
    161 sensors_poll_context_t::sensors_poll_context_t()
    162 {
    163 #ifdef SENSORHAL_ACC_ADXL346
    164     mSensors[acc] = new AdxlSensor();
    165 #endif
    166 #ifdef SENSORHAL_ACC_KXTF9
    167     mSensors[acc] = new KionixSensor();
    168 #endif
    169     mPollFds[acc].fd = mSensors[acc]->getFd();
    170     mPollFds[acc].events = POLLIN;
    171     mPollFds[acc].revents = 0;
    172 
    173     mSensors[akm] = new AkmSensor();
    174     mPollFds[akm].fd = mSensors[akm]->getFd();
    175     mPollFds[akm].events = POLLIN;
    176     mPollFds[akm].revents = 0;
    177 
    178     int wakeFds[2];
    179     int result = pipe(wakeFds);
    180     ALOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno));
    181     fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);
    182     fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);
    183     mWritePipeFd = wakeFds[1];
    184 
    185     mPollFds[wake].fd = wakeFds[0];
    186     mPollFds[wake].events = POLLIN;
    187     mPollFds[wake].revents = 0;
    188 }
    189 
    190 sensors_poll_context_t::~sensors_poll_context_t() {
    191     for (int i=0 ; i<numSensorDrivers ; i++) {
    192         delete mSensors[i];
    193     }
    194     close(mPollFds[wake].fd);
    195     close(mWritePipeFd);
    196 }
    197 
    198 int sensors_poll_context_t::handleToDriver(int handle) {
    199 	switch (handle) {
    200 		case ID_A:
    201 			return acc;
    202 		case ID_M:
    203 		case ID_O:
    204 			return akm;
    205 	}
    206 	return -EINVAL;
    207 }
    208 
    209 int sensors_poll_context_t::activate(int handle, int enabled) {
    210 	int drv = handleToDriver(handle);
    211 	int err;
    212 
    213 	switch (handle) {
    214 		case ID_A:
    215 		case ID_M:
    216 			/* No dependencies */
    217 			break;
    218 
    219 		case ID_O:
    220 			/* These sensors depend on ID_A and ID_M */
    221 			mSensors[handleToDriver(ID_A)]->setEnable(ID_A, enabled);
    222 			mSensors[handleToDriver(ID_M)]->setEnable(ID_M, enabled);
    223 			break;
    224 
    225 		default:
    226 			return -EINVAL;
    227 	}
    228 	err = mSensors[drv]->setEnable(handle, enabled);
    229 
    230     if (enabled && !err) {
    231         const char wakeMessage(WAKE_MESSAGE);
    232         int result = write(mWritePipeFd, &wakeMessage, 1);
    233         ALOGE_IF(result<0, "error sending wake message (%s)", strerror(errno));
    234     }
    235     return err;
    236 }
    237 
    238 int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
    239 	switch (handle) {
    240 		case ID_A:
    241 		case ID_M:
    242 			/* No dependencies */
    243 			break;
    244 
    245 		case ID_O:
    246 			/* These sensors depend on ID_A and ID_M */
    247 			setDelay_sub(ID_A, ns);
    248 			setDelay_sub(ID_M, ns);
    249 			break;
    250 
    251 		default:
    252 			return -EINVAL;
    253 	}
    254 	return setDelay_sub(handle, ns);
    255 }
    256 
    257 int sensors_poll_context_t::setDelay_sub(int handle, int64_t ns) {
    258 	int drv = handleToDriver(handle);
    259 	int en = mSensors[drv]->getEnable(handle);
    260 	int64_t cur = mSensors[drv]->getDelay(handle);
    261 	int err = 0;
    262 
    263 	if (en <= 1) {
    264 		/* no dependencies */
    265 		if (cur != ns) {
    266 			err = mSensors[drv]->setDelay(handle, ns);
    267 		}
    268 	} else {
    269 		/* has dependencies, choose shorter interval */
    270 		if (cur > ns) {
    271 			err = mSensors[drv]->setDelay(handle, ns);
    272 		}
    273 	}
    274 	return err;
    275 }
    276 
    277 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
    278 {
    279     int nbEvents = 0;
    280     int n = 0;
    281 
    282     do {
    283         // see if we have some leftover from the last poll()
    284         for (int i=0 ; count && i<numSensorDrivers ; i++) {
    285             SensorBase* const sensor(mSensors[i]);
    286             if ((mPollFds[i].revents & POLLIN) || (sensor->hasPendingEvents())) {
    287                 int nb = sensor->readEvents(data, count);
    288                 if (nb < count) {
    289                     // no more data for this sensor
    290                     mPollFds[i].revents = 0;
    291                 }
    292 				if ((0 != nb) && (acc == i)) {
    293 					((AkmSensor*)(mSensors[akm]))->setAccel(&data[nb-1]);
    294 				}
    295                 count -= nb;
    296                 nbEvents += nb;
    297                 data += nb;
    298             }
    299         }
    300 
    301         if (count) {
    302             // we still have some room, so try to see if we can get
    303             // some events immediately or just wait if we don't have
    304             // anything to return
    305             n = poll(mPollFds, numFds, nbEvents ? 0 : -1);
    306             if (n<0) {
    307                 ALOGE("poll() failed (%s)", strerror(errno));
    308                 return -errno;
    309             }
    310             if (mPollFds[wake].revents & POLLIN) {
    311                 char msg;
    312                 int result = read(mPollFds[wake].fd, &msg, 1);
    313                 ALOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno));
    314                 ALOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
    315                 mPollFds[wake].revents = 0;
    316             }
    317         }
    318         // if we have events and space, go read them
    319     } while (n && count);
    320 
    321     return nbEvents;
    322 }
    323 
    324 /*****************************************************************************/
    325 
    326 static int poll__close(struct hw_device_t *dev)
    327 {
    328     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
    329     if (ctx) {
    330         delete ctx;
    331     }
    332     return 0;
    333 }
    334 
    335 static int poll__activate(struct sensors_poll_device_t *dev,
    336         int handle, int enabled) {
    337     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
    338     return ctx->activate(handle, enabled);
    339 }
    340 
    341 static int poll__setDelay(struct sensors_poll_device_t *dev,
    342         int handle, int64_t ns) {
    343     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
    344     return ctx->setDelay(handle, ns);
    345 }
    346 
    347 static int poll__poll(struct sensors_poll_device_t *dev,
    348         sensors_event_t* data, int count) {
    349     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
    350     return ctx->pollEvents(data, count);
    351 }
    352 
    353 /*****************************************************************************/
    354 
    355 /** Open a new instance of a sensor device using name */
    356 static int open_sensors(const struct hw_module_t* module, const char* id,
    357                         struct hw_device_t** device)
    358 {
    359         int status = -EINVAL;
    360         sensors_poll_context_t *dev = new sensors_poll_context_t();
    361 
    362         memset(&dev->device, 0, sizeof(sensors_poll_device_t));
    363 
    364         dev->device.common.tag = HARDWARE_DEVICE_TAG;
    365         dev->device.common.version  = 0;
    366         dev->device.common.module   = const_cast<hw_module_t*>(module);
    367         dev->device.common.close    = poll__close;
    368         dev->device.activate        = poll__activate;
    369         dev->device.setDelay        = poll__setDelay;
    370         dev->device.poll            = poll__poll;
    371 
    372         *device = &dev->device.common;
    373         status = 0;
    374 
    375         return status;
    376 }
    377 
    378