|
| 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 | + |
| 18 | +#define LOG_TAG "sensors" |
| 19 | +#define SENSORS_SERVICE_NAME "sensors" |
| 20 | + |
| 21 | +#include <cutils/log.h> |
| 22 | + |
| 23 | +#include <stdint.h> |
| 24 | +#include <string.h> |
| 25 | +#include <unistd.h> |
| 26 | +#include <errno.h> |
| 27 | +#include <fcntl.h> |
| 28 | +#include <stdlib.h> |
| 29 | +#include <pthread.h> |
| 30 | +#include <dirent.h> |
| 31 | +#include <sys/poll.h> |
| 32 | +#include <sys/ioctl.h> |
| 33 | +#include <sys/types.h> |
| 34 | +#include <linux/input.h> |
| 35 | +#include <sys/select.h> |
| 36 | + |
| 37 | +#include <hardware/sensors.h> |
| 38 | +#include <cutils/native_handle.h> |
| 39 | +#include <cutils/sockets.h> |
| 40 | +#include <cutils/properties.h> |
| 41 | + |
| 42 | +/******************************************************************************/ |
| 43 | +#define ID_BASE SENSORS_HANDLE_BASE |
| 44 | +#define ID_ACCELERATION (ID_BASE+0) |
| 45 | + |
| 46 | +#define CONVERT (GRAVITY_EARTH/64.0f) |
| 47 | +#define SENSORS_ACCELERATION (1 << ID_ACCELERATION) |
| 48 | +#define INPUT_DIR "/dev/input" |
| 49 | +#define ACCEL_SYS_DIR "/sys/devices/i2c-0/0-003a/input" |
| 50 | +#define SUPPORTED_SENSORS (SENSORS_ACCELERATION) |
| 51 | +#define EVENT_MASK_ACCEL_ALL ( (1 << ABS_X) | (1 << ABS_Y) | (1 << ABS_Z)) |
| 52 | +#define DEFAULT_THRESHOLD 100 |
| 53 | + |
| 54 | +#define ACCELERATION_X (1 << ABS_X) |
| 55 | +#define ACCELERATION_Y (1 << ABS_Y) |
| 56 | +#define ACCELERATION_Z (1 << ABS_Z) |
| 57 | +#define SENSORS_ACCELERATION_ALL (ACCELERATION_X | ACCELERATION_Y | \ |
| 58 | + ACCELERATION_Z) |
| 59 | +#define SEC_TO_NSEC 1000000000LL |
| 60 | +#define USEC_TO_NSEC 1000 |
| 61 | +#define CONTROL_READ 0 |
| 62 | +#define CONTROL_WRITE 1 |
| 63 | +#define WAKE_SOURCE 0x1a |
| 64 | + |
| 65 | +uint32_t active_sensors; |
| 66 | +int sensor_fd = -1; |
| 67 | +int event_fd = -1; |
| 68 | +int control_fd[2] = { -1, -1 }; |
| 69 | + |
| 70 | + |
| 71 | +/* |
| 72 | + * the following is the list of all supported sensors |
| 73 | + */ |
| 74 | +static const struct sensor_t apple_sensor_list[] = |
| 75 | +{ |
| 76 | + { |
| 77 | + .name = "LIS331DL 3-axis Accelerometer", |
| 78 | + .vendor = "STMicroelectronics", |
| 79 | + .version = 1, |
| 80 | + .handle =SENSOR_TYPE_ACCELEROMETER, |
| 81 | + .type = SENSOR_TYPE_ACCELEROMETER, |
| 82 | + .maxRange = (GRAVITY_EARTH * 2.3f), |
| 83 | + .resolution = (GRAVITY_EARTH * 2.3f) / 128.0f, |
| 84 | + .power = 0.4f, //power consumption max is rated max 0.3mA... |
| 85 | + .reserved = {}, |
| 86 | + }, |
| 87 | +}; |
| 88 | + |
| 89 | +static uint32_t sensors_get_list(struct sensors_module_t *module, |
| 90 | + struct sensor_t const** list) |
| 91 | +{ |
| 92 | + *list = apple_sensor_list; |
| 93 | + return 1; |
| 94 | +} |
| 95 | + |
| 96 | +/** Close the sensors device */ |
| 97 | +static int |
| 98 | +close_sensors(struct hw_device_t *dev) |
| 99 | +{ |
| 100 | + struct sensors_data_device_t *device_data = |
| 101 | + (struct sensors_data_device_t *)dev; |
| 102 | + if (device_data) { |
| 103 | + if (event_fd > 0) |
| 104 | + close(event_fd); |
| 105 | + free(device_data); |
| 106 | + } |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +int open_sensors_phy(struct sensors_control_device_t *dev) |
| 111 | +{ |
| 112 | + char devname[PATH_MAX]; |
| 113 | + char *filename; |
| 114 | + int fd; |
| 115 | + int res; |
| 116 | + uint8_t bits[4]; |
| 117 | + DIR *dir; |
| 118 | + struct dirent *de; |
| 119 | + |
| 120 | + dir = opendir(ACCEL_SYS_DIR); |
| 121 | + if (dir == NULL) |
| 122 | + return -1; |
| 123 | + |
| 124 | + |
| 125 | + strcpy(devname, INPUT_DIR); |
| 126 | + filename = devname + strlen(devname); |
| 127 | + strcpy(filename, "/event"); |
| 128 | + |
| 129 | + while ((de = readdir(dir))) |
| 130 | + { |
| 131 | + if (de->d_name[0] == '.' && |
| 132 | + (de->d_name[1] == '\0' || |
| 133 | + (de->d_name[1] == '.' && de->d_name[2] == '\0'))) |
| 134 | + continue; |
| 135 | + filename = devname + strlen(devname); |
| 136 | + *filename++ = de->d_name[5]; |
| 137 | + *filename++ = '\0'; |
| 138 | + fd = open(devname, O_RDONLY); |
| 139 | + if (fd < 0) |
| 140 | + { |
| 141 | + LOGE("Accelerometer: Couldn't open %s, error = %d", devname, fd); |
| 142 | + continue; |
| 143 | + } |
| 144 | + res = ioctl(fd, EVIOCGBIT(EV_ABS, 4), bits); |
| 145 | + if (res <= 0 || bits[0] != EVENT_MASK_ACCEL_ALL) |
| 146 | + { |
| 147 | + close(fd); |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + closedir(dir); |
| 152 | + return fd; |
| 153 | + } |
| 154 | + closedir(dir); |
| 155 | + |
| 156 | + return -1; |
| 157 | +} |
| 158 | + |
| 159 | +static native_handle_t *control_open_data_source(struct sensors_control_device_t *dev) |
| 160 | +{ |
| 161 | + native_handle_t *hd; |
| 162 | + if (control_fd[0] == -1 && control_fd[1] == -1) { |
| 163 | + if (socketpair(AF_LOCAL, SOCK_STREAM, 0, control_fd) < 0 ) |
| 164 | + { |
| 165 | + LOGE("Accelerometer: could not create thread control socket pair: %s", |
| 166 | + strerror(errno)); |
| 167 | + return NULL; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + sensor_fd = open_sensors_phy(dev); |
| 172 | + |
| 173 | + hd = native_handle_create(1, 0); |
| 174 | + hd->data[0] = sensor_fd; |
| 175 | + |
| 176 | + return hd; |
| 177 | +} |
| 178 | + |
| 179 | +static int control_activate(struct sensors_control_device_t *dev, |
| 180 | + int handle, int enabled) |
| 181 | +{ |
| 182 | + uint32_t mask = (1 << handle); |
| 183 | + uint32_t sensors; |
| 184 | + uint32_t new_sensors, active, changed; |
| 185 | + |
| 186 | + sensors = enabled ? mask : 0; |
| 187 | + active = active_sensors; |
| 188 | + new_sensors = (active & ~mask) | (sensors & mask); |
| 189 | + changed = active ^ new_sensors; |
| 190 | + if (!changed) |
| 191 | + return 0; |
| 192 | + |
| 193 | + active_sensors = new_sensors; |
| 194 | + |
| 195 | + if (!enabled) |
| 196 | + { |
| 197 | + LOGD("Accelerometer: Deactivating Accelerometer sensor\n"); |
| 198 | + } |
| 199 | + else |
| 200 | + { |
| 201 | + LOGD("Accelerometer: Activating Accelerometer sensor\n"); |
| 202 | + } |
| 203 | + |
| 204 | + return 0; |
| 205 | +} |
| 206 | + |
| 207 | +static int control_set_delay(struct sensors_control_device_t *dev, int32_t ms) |
| 208 | +{ |
| 209 | + return 0; |
| 210 | +} |
| 211 | + |
| 212 | +static int control_wake(struct sensors_control_device_t *dev) |
| 213 | +{ |
| 214 | + int err = 0; |
| 215 | + if (control_fd[CONTROL_WRITE] > 0) |
| 216 | + { |
| 217 | + struct input_event event[1]; |
| 218 | + |
| 219 | + event[0].type = EV_SYN; |
| 220 | + event[0].code = SYN_CONFIG; |
| 221 | + event[0].value = 0; |
| 222 | + err = write(control_fd[CONTROL_WRITE], event, sizeof(event)); |
| 223 | + LOGD_IF(err<0, "control__wake, err=%d (%s)", errno, strerror(errno)); |
| 224 | + } |
| 225 | + |
| 226 | + return err; |
| 227 | +} |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | +int sensors_open(struct sensors_data_device_t *dev, native_handle_t* hd) |
| 232 | +{ |
| 233 | + event_fd = dup(hd->data[0]); |
| 234 | + |
| 235 | + native_handle_close(hd); |
| 236 | + native_handle_delete(hd); |
| 237 | + |
| 238 | + return 0; |
| 239 | +} |
| 240 | +static int control_close(struct hw_device_t *dev) |
| 241 | +{ |
| 242 | + struct sensors_control_device_t *device_control = (void *) dev; |
| 243 | + close(control_fd[CONTROL_WRITE]); |
| 244 | + close(control_fd[CONTROL_READ]); |
| 245 | + close(sensor_fd); |
| 246 | + sensor_fd = -1; |
| 247 | + control_fd[0] = -1; |
| 248 | + control_fd[1] = -1; |
| 249 | + return 0; |
| 250 | +} |
| 251 | +int sensors_close(struct sensors_data_device_t *dev) |
| 252 | +{ |
| 253 | + if (event_fd > 0) { |
| 254 | + close(event_fd); |
| 255 | + event_fd = -1; |
| 256 | + } |
| 257 | + return 0; |
| 258 | +} |
| 259 | + |
| 260 | +int sensors_poll(struct sensors_data_device_t *dev, sensors_data_t* sensors) |
| 261 | +{ |
| 262 | + int fd = event_fd; |
| 263 | + fd_set rfds; |
| 264 | + struct input_event ev; |
| 265 | + int ret; |
| 266 | + uint32_t new_sensors = 0; |
| 267 | + int select_dim; |
| 268 | + |
| 269 | + sensors->vector.status = SENSOR_STATUS_ACCURACY_HIGH; |
| 270 | + select_dim = (fd > control_fd[CONTROL_READ]) ? |
| 271 | + fd + 1 : control_fd[CONTROL_READ] + 1; |
| 272 | + while (1) |
| 273 | + { |
| 274 | + FD_ZERO(&rfds); |
| 275 | + FD_SET(fd, &rfds); |
| 276 | + FD_SET(control_fd[CONTROL_READ], &rfds); |
| 277 | + |
| 278 | + do { |
| 279 | + ret = select(select_dim, &rfds, NULL, NULL, 0); |
| 280 | + } while (ret < 0 && errno == EINTR); |
| 281 | + |
| 282 | + if (FD_ISSET(control_fd[CONTROL_READ], &rfds)) |
| 283 | + { |
| 284 | + char ch; |
| 285 | + read(control_fd[CONTROL_READ], &ch, sizeof(ch)); |
| 286 | + if (ch==WAKE_SOURCE) |
| 287 | + { |
| 288 | + FD_ZERO(&rfds); |
| 289 | + return -EWOULDBLOCK; |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + ret = read(fd, &ev, sizeof(ev)); |
| 294 | + if (ret < (int)sizeof(ev)) |
| 295 | + break; |
| 296 | + |
| 297 | + FD_CLR(control_fd[CONTROL_READ], &rfds); |
| 298 | + |
| 299 | + if (ev.type == EV_ABS) |
| 300 | + { |
| 301 | + /* Orientation or acceleration event */ |
| 302 | + switch (ev.code) |
| 303 | + { |
| 304 | + case ABS_X: |
| 305 | + new_sensors |= ACCELERATION_X; |
| 306 | + sensors->acceleration.x = (ev.value * CONVERT ); |
| 307 | + break; |
| 308 | + case ABS_Y: |
| 309 | + new_sensors |= ACCELERATION_Y; |
| 310 | + sensors->acceleration.y = (ev.value * CONVERT); |
| 311 | + break; |
| 312 | + case ABS_Z: |
| 313 | + new_sensors |= ACCELERATION_Z; |
| 314 | + sensors->acceleration.z = (ev.value * CONVERT); |
| 315 | + break; |
| 316 | + } |
| 317 | + } |
| 318 | + else |
| 319 | + return 0; |
| 320 | + if (new_sensors == (ACCELERATION_X | ACCELERATION_Y | ACCELERATION_Z)) |
| 321 | + { |
| 322 | + sensors->sensor = SENSOR_TYPE_ACCELEROMETER; |
| 323 | + return SENSOR_TYPE_ACCELEROMETER; |
| 324 | + } |
| 325 | + } |
| 326 | + return 0; |
| 327 | +} |
| 328 | + |
| 329 | +/******************************************************************************/ |
| 330 | + |
| 331 | +/** |
| 332 | + * module methods |
| 333 | + */ |
| 334 | + |
| 335 | +/** Open a new instance of a sensors device using name */ |
| 336 | +static int open_sensors(const struct hw_module_t* module, char const* name, |
| 337 | + struct hw_device_t** device) |
| 338 | +{ |
| 339 | + int status = -EINVAL; |
| 340 | + |
| 341 | + if (!strcmp(name, SENSORS_HARDWARE_CONTROL)) |
| 342 | + { |
| 343 | + struct sensors_control_device_t *device_control = |
| 344 | + malloc(sizeof(struct sensors_control_device_t)); |
| 345 | + memset(device_control, 0, sizeof(*device_control)); |
| 346 | + |
| 347 | + device_control->common.tag = HARDWARE_DEVICE_TAG; |
| 348 | + device_control->common.version = 0; |
| 349 | + device_control->common.module = (struct hw_module_t*)module; |
| 350 | + device_control->common.close = control_close; |
| 351 | + device_control->open_data_source = control_open_data_source; |
| 352 | + device_control->activate = control_activate; |
| 353 | + device_control->set_delay = control_set_delay; |
| 354 | + device_control->wake = control_wake; |
| 355 | + sensor_fd = -1; |
| 356 | + *device = &device_control->common; |
| 357 | + status = 0; |
| 358 | + } else if (!strcmp(name, SENSORS_HARDWARE_DATA)) { |
| 359 | + struct sensors_data_device_t *device_data = |
| 360 | + malloc(sizeof(struct sensors_data_device_t)); |
| 361 | + memset(device_data, 0, sizeof(*device_data)); |
| 362 | + |
| 363 | + device_data->common.tag = HARDWARE_DEVICE_TAG; |
| 364 | + device_data->common.version = 0; |
| 365 | + device_data->common.module = (struct hw_module_t*)module; |
| 366 | + device_data->common.close = close_sensors; |
| 367 | + device_data->data_open = sensors_open; |
| 368 | + device_data->data_close = sensors_close; |
| 369 | + device_data->poll = sensors_poll; |
| 370 | + event_fd = -1; |
| 371 | + *device = &device_data->common; |
| 372 | + status = 0; |
| 373 | + } |
| 374 | + return status; |
| 375 | +} |
| 376 | + |
| 377 | +static struct hw_module_methods_t sensors_module_methods = |
| 378 | +{ |
| 379 | + .open = open_sensors, |
| 380 | +}; |
| 381 | + |
| 382 | +/* |
| 383 | + * The Sensors Hardware Module |
| 384 | + */ |
| 385 | +const struct sensors_module_t HAL_MODULE_INFO_SYM = |
| 386 | +{ |
| 387 | + .common = { |
| 388 | + .tag = HARDWARE_MODULE_TAG, |
| 389 | + .version_major = 1, |
| 390 | + .version_minor = 0, |
| 391 | + .id = SENSORS_HARDWARE_MODULE_ID, |
| 392 | + .name = "LIS331DL accelerometer sensors Module", |
| 393 | + .author = "Dario Russo (turbominchiameister@gmail.com)", |
| 394 | + .methods = &sensors_module_methods, |
| 395 | + }, |
| 396 | + .get_sensors_list = sensors_get_list |
| 397 | +}; |
0 commit comments