1 /* 2 * Copyright (C) 2009 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 /* this implements a sensors hardware library for the Android emulator. 18 * the following code should be built as a shared library that will be 19 * placed into /system/lib/hw/sensors.goldfish.so 20 * 21 * it will be loaded by the code in hardware/libhardware/hardware.c 22 * which is itself called from com_android_server_SensorService.cpp 23 */ 24 25 26 /* we connect with the emulator through the "sensors" qemud service 27 */ 28 #define SENSORS_SERVICE_NAME "sensors" 29 30 #define LOG_TAG "QemuSensors" 31 32 #include <unistd.h> 33 #include <fcntl.h> 34 #include <errno.h> 35 #include <string.h> 36 #include <cutils/log.h> 37 #include <cutils/native_handle.h> 38 #include <cutils/sockets.h> 39 #include <hardware/sensors.h> 40 41 #if 0 42 #define D(...) ALOGD(__VA_ARGS__) 43 #else 44 #define D(...) ((void)0) 45 #endif 46 47 #define E(...) ALOGE(__VA_ARGS__) 48 49 #include <hardware/qemud.h> 50 51 /** SENSOR IDS AND NAMES 52 **/ 53 54 #define MAX_NUM_SENSORS 5 55 56 #define SUPPORTED_SENSORS ((1<<MAX_NUM_SENSORS)-1) 57 58 #define ID_BASE SENSORS_HANDLE_BASE 59 #define ID_ACCELERATION (ID_BASE+0) 60 #define ID_MAGNETIC_FIELD (ID_BASE+1) 61 #define ID_ORIENTATION (ID_BASE+2) 62 #define ID_TEMPERATURE (ID_BASE+3) 63 #define ID_PROXIMITY (ID_BASE+4) 64 65 #define SENSORS_ACCELERATION (1 << ID_ACCELERATION) 66 #define SENSORS_MAGNETIC_FIELD (1 << ID_MAGNETIC_FIELD) 67 #define SENSORS_ORIENTATION (1 << ID_ORIENTATION) 68 #define SENSORS_TEMPERATURE (1 << ID_TEMPERATURE) 69 #define SENSORS_PROXIMITY (1 << ID_PROXIMITY) 70 71 #define ID_CHECK(x) ((unsigned)((x)-ID_BASE) < MAX_NUM_SENSORS) 72 73 #define SENSORS_LIST \ 74 SENSOR_(ACCELERATION,"acceleration") \ 75 SENSOR_(MAGNETIC_FIELD,"magnetic-field") \ 76 SENSOR_(ORIENTATION,"orientation") \ 77 SENSOR_(TEMPERATURE,"temperature") \ 78 SENSOR_(PROXIMITY,"proximity") \ 79 80 static const struct { 81 const char* name; 82 int id; } _sensorIds[MAX_NUM_SENSORS] = 83 { 84 #define SENSOR_(x,y) { y, ID_##x }, 85 SENSORS_LIST 86 #undef SENSOR_ 87 }; 88 89 static const char* 90 _sensorIdToName( int id ) 91 { 92 int nn; 93 for (nn = 0; nn < MAX_NUM_SENSORS; nn++) 94 if (id == _sensorIds[nn].id) 95 return _sensorIds[nn].name; 96 return "<UNKNOWN>"; 97 } 98 99 static int 100 _sensorIdFromName( const char* name ) 101 { 102 int nn; 103 104 if (name == NULL) 105 return -1; 106 107 for (nn = 0; nn < MAX_NUM_SENSORS; nn++) 108 if (!strcmp(name, _sensorIds[nn].name)) 109 return _sensorIds[nn].id; 110 111 return -1; 112 } 113 114 /** SENSORS POLL DEVICE 115 ** 116 ** This one is used to read sensor data from the hardware. 117 ** We implement this by simply reading the data from the 118 ** emulator through the QEMUD channel. 119 **/ 120 121 typedef struct SensorPoll { 122 struct sensors_poll_device_t device; 123 sensors_event_t sensors[MAX_NUM_SENSORS]; 124 int events_fd; 125 uint32_t pendingSensors; 126 int64_t timeStart; 127 int64_t timeOffset; 128 int fd; 129 uint32_t active_sensors; 130 } SensorPoll; 131 132 /* this must return a file descriptor that will be used to read 133 * the sensors data (it is passed to data__data_open() below 134 */ 135 static native_handle_t* 136 control__open_data_source(struct sensors_poll_device_t *dev) 137 { 138 SensorPoll* ctl = (void*)dev; 139 native_handle_t* handle; 140 141 if (ctl->fd < 0) { 142 ctl->fd = qemud_channel_open(SENSORS_SERVICE_NAME); 143 } 144 D("%s: fd=%d", __FUNCTION__, ctl->fd); 145 handle = native_handle_create(1, 0); 146 handle->data[0] = dup(ctl->fd); 147 return handle; 148 } 149 150 static int 151 control__activate(struct sensors_poll_device_t *dev, 152 int handle, 153 int enabled) 154 { 155 SensorPoll* ctl = (void*)dev; 156 uint32_t mask, sensors, active, new_sensors, changed; 157 char command[128]; 158 int ret; 159 160 D("%s: handle=%s (%d) fd=%d enabled=%d", __FUNCTION__, 161 _sensorIdToName(handle), handle, ctl->fd, enabled); 162 163 if (!ID_CHECK(handle)) { 164 E("%s: bad handle ID", __FUNCTION__); 165 return -1; 166 } 167 168 mask = (1<<handle); 169 sensors = enabled ? mask : 0; 170 171 active = ctl->active_sensors; 172 new_sensors = (active & ~mask) | (sensors & mask); 173 changed = active ^ new_sensors; 174 175 if (!changed) 176 return 0; 177 178 snprintf(command, sizeof command, "set:%s:%d", 179 _sensorIdToName(handle), enabled != 0); 180 181 if (ctl->fd < 0) { 182 ctl->fd = qemud_channel_open(SENSORS_SERVICE_NAME); 183 } 184 185 ret = qemud_channel_send(ctl->fd, command, -1); 186 if (ret < 0) { 187 E("%s: when sending command errno=%d: %s", __FUNCTION__, errno, strerror(errno)); 188 return -1; 189 } 190 ctl->active_sensors = new_sensors; 191 192 return 0; 193 } 194 195 static int 196 control__set_delay(struct sensors_poll_device_t *dev, int32_t ms) 197 { 198 SensorPoll* ctl = (void*)dev; 199 char command[128]; 200 201 D("%s: dev=%p delay-ms=%d", __FUNCTION__, dev, ms); 202 203 snprintf(command, sizeof command, "set-delay:%d", ms); 204 205 return qemud_channel_send(ctl->fd, command, -1); 206 } 207 208 static int 209 control__close(struct hw_device_t *dev) 210 { 211 SensorPoll* ctl = (void*)dev; 212 close(ctl->fd); 213 free(ctl); 214 return 0; 215 } 216 217 /* return the current time in nanoseconds */ 218 static int64_t 219 data__now_ns(void) 220 { 221 struct timespec ts; 222 223 clock_gettime(CLOCK_MONOTONIC, &ts); 224 225 return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec; 226 } 227 228 static int 229 data__data_open(struct sensors_poll_device_t *dev, native_handle_t* handle) 230 { 231 SensorPoll* data = (void*)dev; 232 int i; 233 D("%s: dev=%p fd=%d", __FUNCTION__, dev, handle->data[0]); 234 memset(&data->sensors, 0, sizeof(data->sensors)); 235 236 data->pendingSensors = 0; 237 data->timeStart = 0; 238 data->timeOffset = 0; 239 240 data->events_fd = dup(handle->data[0]); 241 D("%s: dev=%p fd=%d (was %d)", __FUNCTION__, dev, data->events_fd, handle->data[0]); 242 native_handle_close(handle); 243 native_handle_delete(handle); 244 return 0; 245 } 246 247 static int 248 data__data_close(struct sensors_poll_device_t *dev) 249 { 250 SensorPoll* data = (void*)dev; 251 D("%s: dev=%p", __FUNCTION__, dev); 252 if (data->events_fd >= 0) { 253 close(data->events_fd); 254 data->events_fd = -1; 255 } 256 return 0; 257 } 258 259 static int 260 pick_sensor(SensorPoll* data, 261 sensors_event_t* values) 262 { 263 uint32_t mask = SUPPORTED_SENSORS; 264 while (mask) { 265 uint32_t i = 31 - __builtin_clz(mask); 266 mask &= ~(1<<i); 267 if (data->pendingSensors & (1<<i)) { 268 data->pendingSensors &= ~(1<<i); 269 *values = data->sensors[i]; 270 values->sensor = i; 271 values->version = sizeof(*values); 272 273 D("%s: %d [%f, %f, %f]", __FUNCTION__, 274 i, 275 values->data[0], 276 values->data[1], 277 values->data[2]); 278 return i; 279 } 280 } 281 ALOGE("No sensor to return!!! pendingSensors=%08x", data->pendingSensors); 282 // we may end-up in a busy loop, slow things down, just in case. 283 usleep(100000); 284 return -EINVAL; 285 } 286 287 static int 288 data__poll(struct sensors_poll_device_t *dev, sensors_event_t* values) 289 { 290 SensorPoll* data = (void*)dev; 291 int fd = data->events_fd; 292 293 D("%s: data=%p", __FUNCTION__, dev); 294 295 // there are pending sensors, returns them now... 296 if (data->pendingSensors) { 297 return pick_sensor(data, values); 298 } 299 300 // wait until we get a complete event for an enabled sensor 301 uint32_t new_sensors = 0; 302 303 while (1) { 304 /* read the next event */ 305 char buff[256]; 306 int len = qemud_channel_recv(data->events_fd, buff, sizeof buff-1); 307 float params[3]; 308 int64_t event_time; 309 310 if (len < 0) { 311 E("%s: len=%d, errno=%d: %s", __FUNCTION__, len, errno, strerror(errno)); 312 return -errno; 313 } 314 315 buff[len] = 0; 316 317 /* "wake" is sent from the emulator to exit this loop. */ 318 if (!strcmp((const char*)data, "wake")) { 319 return 0x7FFFFFFF; 320 } 321 322 /* "acceleration:<x>:<y>:<z>" corresponds to an acceleration event */ 323 if (sscanf(buff, "acceleration:%g:%g:%g", params+0, params+1, params+2) == 3) { 324 new_sensors |= SENSORS_ACCELERATION; 325 data->sensors[ID_ACCELERATION].acceleration.x = params[0]; 326 data->sensors[ID_ACCELERATION].acceleration.y = params[1]; 327 data->sensors[ID_ACCELERATION].acceleration.z = params[2]; 328 continue; 329 } 330 331 /* "orientation:<azimuth>:<pitch>:<roll>" is sent when orientation changes */ 332 if (sscanf(buff, "orientation:%g:%g:%g", params+0, params+1, params+2) == 3) { 333 new_sensors |= SENSORS_ORIENTATION; 334 data->sensors[ID_ORIENTATION].orientation.azimuth = params[0]; 335 data->sensors[ID_ORIENTATION].orientation.pitch = params[1]; 336 data->sensors[ID_ORIENTATION].orientation.roll = params[2]; 337 data->sensors[ID_ORIENTATION].orientation.status = SENSOR_STATUS_ACCURACY_HIGH; 338 continue; 339 } 340 341 /* "magnetic:<x>:<y>:<z>" is sent for the params of the magnetic field */ 342 if (sscanf(buff, "magnetic:%g:%g:%g", params+0, params+1, params+2) == 3) { 343 new_sensors |= SENSORS_MAGNETIC_FIELD; 344 data->sensors[ID_MAGNETIC_FIELD].magnetic.x = params[0]; 345 data->sensors[ID_MAGNETIC_FIELD].magnetic.y = params[1]; 346 data->sensors[ID_MAGNETIC_FIELD].magnetic.z = params[2]; 347 data->sensors[ID_MAGNETIC_FIELD].magnetic.status = SENSOR_STATUS_ACCURACY_HIGH; 348 continue; 349 } 350 351 /* "temperature:<celsius>" */ 352 if (sscanf(buff, "temperature:%g", params+0) == 1) { 353 new_sensors |= SENSORS_TEMPERATURE; 354 data->sensors[ID_TEMPERATURE].temperature = params[0]; 355 continue; 356 } 357 358 /* "proximity:<value>" */ 359 if (sscanf(buff, "proximity:%g", params+0) == 1) { 360 new_sensors |= SENSORS_PROXIMITY; 361 data->sensors[ID_PROXIMITY].distance = params[0]; 362 continue; 363 } 364 365 /* "sync:<time>" is sent after a series of sensor events. 366 * where 'time' is expressed in micro-seconds and corresponds 367 * to the VM time when the real poll occured. 368 */ 369 if (sscanf(buff, "sync:%lld", &event_time) == 1) { 370 if (new_sensors) { 371 data->pendingSensors = new_sensors; 372 int64_t t = event_time * 1000LL; /* convert to nano-seconds */ 373 374 /* use the time at the first sync: as the base for later 375 * time values */ 376 if (data->timeStart == 0) { 377 data->timeStart = data__now_ns(); 378 data->timeOffset = data->timeStart - t; 379 } 380 t += data->timeOffset; 381 382 while (new_sensors) { 383 uint32_t i = 31 - __builtin_clz(new_sensors); 384 new_sensors &= ~(1<<i); 385 data->sensors[i].timestamp = t; 386 } 387 return pick_sensor(data, values); 388 } else { 389 D("huh ? sync without any sensor data ?"); 390 } 391 continue; 392 } 393 D("huh ? unsupported command"); 394 } 395 return -1; 396 } 397 398 static int 399 data__close(struct hw_device_t *dev) 400 { 401 SensorPoll* data = (SensorPoll*)dev; 402 if (data) { 403 if (data->events_fd >= 0) { 404 //ALOGD("(device close) about to close fd=%d", data->events_fd); 405 close(data->events_fd); 406 } 407 free(data); 408 } 409 return 0; 410 } 411 412 /** SENSORS POLL DEVICE FUNCTIONS **/ 413 414 static int poll__close(struct hw_device_t* dev) 415 { 416 SensorPoll* ctl = (void*)dev; 417 close(ctl->fd); 418 if (ctl->fd >= 0) { 419 close(ctl->fd); 420 } 421 if (ctl->events_fd >= 0) { 422 close(ctl->events_fd); 423 } 424 free(ctl); 425 return 0; 426 } 427 428 static int poll__poll(struct sensors_poll_device_t *dev, 429 sensors_event_t* data, int count) 430 { 431 SensorPoll* datadev = (void*)dev; 432 int ret; 433 int i; 434 D("%s: dev=%p data=%p count=%d ", __FUNCTION__, dev, data, count); 435 436 for (i = 0; i < count; i++) { 437 ret = data__poll(dev, data); 438 data++; 439 if (ret > MAX_NUM_SENSORS || ret < 0) { 440 return i; 441 } 442 if (!datadev->pendingSensors) { 443 return i + 1; 444 } 445 } 446 return count; 447 } 448 449 static int poll__activate(struct sensors_poll_device_t *dev, 450 int handle, int enabled) 451 { 452 int ret; 453 native_handle_t* hdl; 454 SensorPoll* ctl = (void*)dev; 455 D("%s: dev=%p handle=%x enable=%d ", __FUNCTION__, dev, handle, enabled); 456 if (ctl->fd < 0) { 457 D("%s: OPEN CTRL and DATA ", __FUNCTION__); 458 hdl = control__open_data_source(dev); 459 ret = data__data_open(dev,hdl); 460 } 461 ret = control__activate(dev, handle, enabled); 462 return ret; 463 } 464 465 static int poll__setDelay(struct sensors_poll_device_t *dev, 466 int handle, int64_t ns) 467 { 468 // TODO 469 return 0; 470 } 471 472 /** MODULE REGISTRATION SUPPORT 473 ** 474 ** This is required so that hardware/libhardware/hardware.c 475 ** will dlopen() this library appropriately. 476 **/ 477 478 /* 479 * the following is the list of all supported sensors. 480 * this table is used to build sSensorList declared below 481 * according to which hardware sensors are reported as 482 * available from the emulator (see get_sensors_list below) 483 * 484 * note: numerical values for maxRange/resolution/power were 485 * taken from the reference AK8976A implementation 486 */ 487 static const struct sensor_t sSensorListInit[] = { 488 { .name = "Goldfish 3-axis Accelerometer", 489 .vendor = "The Android Open Source Project", 490 .version = 1, 491 .handle = ID_ACCELERATION, 492 .type = SENSOR_TYPE_ACCELEROMETER, 493 .maxRange = 2.8f, 494 .resolution = 1.0f/4032.0f, 495 .power = 3.0f, 496 .reserved = {} 497 }, 498 499 { .name = "Goldfish 3-axis Magnetic field sensor", 500 .vendor = "The Android Open Source Project", 501 .version = 1, 502 .handle = ID_MAGNETIC_FIELD, 503 .type = SENSOR_TYPE_MAGNETIC_FIELD, 504 .maxRange = 2000.0f, 505 .resolution = 1.0f, 506 .power = 6.7f, 507 .reserved = {} 508 }, 509 510 { .name = "Goldfish Orientation sensor", 511 .vendor = "The Android Open Source Project", 512 .version = 1, 513 .handle = ID_ORIENTATION, 514 .type = SENSOR_TYPE_ORIENTATION, 515 .maxRange = 360.0f, 516 .resolution = 1.0f, 517 .power = 9.7f, 518 .reserved = {} 519 }, 520 521 { .name = "Goldfish Temperature sensor", 522 .vendor = "The Android Open Source Project", 523 .version = 1, 524 .handle = ID_TEMPERATURE, 525 .type = SENSOR_TYPE_TEMPERATURE, 526 .maxRange = 80.0f, 527 .resolution = 1.0f, 528 .power = 0.0f, 529 .reserved = {} 530 }, 531 532 { .name = "Goldfish Proximity sensor", 533 .vendor = "The Android Open Source Project", 534 .version = 1, 535 .handle = ID_PROXIMITY, 536 .type = SENSOR_TYPE_PROXIMITY, 537 .maxRange = 1.0f, 538 .resolution = 1.0f, 539 .power = 20.0f, 540 .reserved = {} 541 }, 542 }; 543 544 static struct sensor_t sSensorList[MAX_NUM_SENSORS]; 545 546 static int sensors__get_sensors_list(struct sensors_module_t* module, 547 struct sensor_t const** list) 548 { 549 int fd = qemud_channel_open(SENSORS_SERVICE_NAME); 550 char buffer[12]; 551 int mask, nn, count; 552 553 int ret; 554 if (fd < 0) { 555 E("%s: no qemud connection", __FUNCTION__); 556 return 0; 557 } 558 ret = qemud_channel_send(fd, "list-sensors", -1); 559 if (ret < 0) { 560 E("%s: could not query sensor list: %s", __FUNCTION__, 561 strerror(errno)); 562 close(fd); 563 return 0; 564 } 565 ret = qemud_channel_recv(fd, buffer, sizeof buffer-1); 566 if (ret < 0) { 567 E("%s: could not receive sensor list: %s", __FUNCTION__, 568 strerror(errno)); 569 close(fd); 570 return 0; 571 } 572 buffer[ret] = 0; 573 close(fd); 574 575 /* the result is a integer used as a mask for available sensors */ 576 mask = atoi(buffer); 577 count = 0; 578 for (nn = 0; nn < MAX_NUM_SENSORS; nn++) { 579 if (((1 << nn) & mask) == 0) 580 continue; 581 582 sSensorList[count++] = sSensorListInit[nn]; 583 } 584 D("%s: returned %d sensors (mask=%d)", __FUNCTION__, count, mask); 585 *list = sSensorList; 586 return count; 587 } 588 589 590 static int 591 open_sensors(const struct hw_module_t* module, 592 const char* name, 593 struct hw_device_t* *device) 594 { 595 int status = -EINVAL; 596 597 D("%s: name=%s", __FUNCTION__, name); 598 599 if (!strcmp(name, SENSORS_HARDWARE_POLL)) { 600 SensorPoll *dev = malloc(sizeof(*dev)); 601 602 memset(dev, 0, sizeof(*dev)); 603 604 dev->device.common.tag = HARDWARE_DEVICE_TAG; 605 dev->device.common.version = 0; 606 dev->device.common.module = (struct hw_module_t*) module; 607 dev->device.common.close = poll__close; 608 dev->device.poll = poll__poll; 609 dev->device.activate = poll__activate; 610 dev->device.setDelay = poll__setDelay; 611 dev->events_fd = -1; 612 dev->fd = -1; 613 614 *device = &dev->device.common; 615 status = 0; 616 } 617 return status; 618 } 619 620 621 static struct hw_module_methods_t sensors_module_methods = { 622 .open = open_sensors 623 }; 624 625 struct sensors_module_t HAL_MODULE_INFO_SYM = { 626 .common = { 627 .tag = HARDWARE_MODULE_TAG, 628 .version_major = 1, 629 .version_minor = 0, 630 .id = SENSORS_HARDWARE_MODULE_ID, 631 .name = "Goldfish SENSORS Module", 632 .author = "The Android Open Source Project", 633 .methods = &sensors_module_methods, 634 }, 635 .get_sensors_list = sensors__get_sensors_list 636 }; 637