2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
21 * it will be loaded by the code in hardware/libhardware/hardware.c
22 * which is itself called from com_android_server_SensorService.cpp
26 /* we connect with the emulator through the "sensors" qemud service
28 #define SENSORS_SERVICE_NAME "sensors"
30 #define LOG_TAG "QemuSensors"
36 #include <cutils/log.h>
37 #include <cutils/native_handle.h>
38 #include <cutils/sockets.h>
39 #include <hardware/sensors.h>
42 #define D(...) LOGD(__VA_ARGS__)
44 #define D(...) ((void)0)
47 #define E(...) LOGE(__VA_ARGS__)
49 #include <hardware/qemud.h>
51 /** SENSOR IDS AND NAMES
54 #define MAX_NUM_SENSORS 4
56 #define SUPPORTED_SENSORS ((1<<MAX_NUM_SENSORS)-1)
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)
64 #define SENSORS_ACCELERATION (1 << ID_ACCELERATION)
65 #define SENSORS_MAGNETIC_FIELD (1 << ID_MAGNETIC_FIELD)
66 #define SENSORS_ORIENTATION (1 << ID_ORIENTATION)
67 #define SENSORS_TEMPERATURE (1 << ID_TEMPERATURE)
69 #define ID_CHECK(x) ((unsigned)((x)-ID_BASE) < 4)
71 #define SENSORS_LIST \
72 SENSOR_(ACCELERATION,"acceleration") \
73 SENSOR_(MAGNETIC_FIELD,"magnetic-field") \
74 SENSOR_(ORIENTATION,"orientation") \
75 SENSOR_(TEMPERATURE,"temperature") \
79 int id; } _sensorIds[MAX_NUM_SENSORS] =
81 #define SENSOR_(x,y) { y, ID_##x },
87 _sensorIdToName( int id )
90 for (nn = 0; nn < MAX_NUM_SENSORS; nn++)
91 if (id == _sensorIds[nn].id)
92 return _sensorIds[nn].name;
97 _sensorIdFromName( const char* name )
104 for (nn = 0; nn < MAX_NUM_SENSORS; nn++)
105 if (!strcmp(name, _sensorIds[nn].name))
106 return _sensorIds[nn].id;
111 /** SENSORS CONTROL DEVICE
113 ** This one is used to send commands to the sensors drivers.
114 ** We implement this by sending directly commands to the emulator
115 ** through the QEMUD channel.
118 typedef struct SensorControl {
119 struct sensors_control_device_t device;
121 uint32_t active_sensors;
124 /* this must return a file descriptor that will be used to read
125 * the sensors data (it is passed to data__data_open() below
127 static native_handle_t*
128 control__open_data_source(struct sensors_control_device_t *dev)
130 SensorControl* ctl = (void*)dev;
131 native_handle_t* handle;
134 ctl->fd = qemud_channel_open(SENSORS_SERVICE_NAME);
136 D("%s: fd=%d", __FUNCTION__, ctl->fd);
137 handle = native_handle_create(1, 0);
138 handle->data[0] = dup(ctl->fd);
143 control__activate(struct sensors_control_device_t *dev,
147 SensorControl* ctl = (void*)dev;
148 uint32_t mask, sensors, active, new_sensors, changed;
152 D("%s: handle=%s (%d) fd=%d enabled=%d", __FUNCTION__,
153 _sensorIdToName(handle), handle, ctl->fd, enabled);
155 if (!ID_CHECK(handle)) {
156 E("%s: bad handle ID", __FUNCTION__);
161 sensors = enabled ? mask : 0;
163 active = ctl->active_sensors;
164 new_sensors = (active & ~mask) | (sensors & mask);
165 changed = active ^ new_sensors;
170 snprintf(command, sizeof command, "set:%s:%d",
171 _sensorIdToName(handle), enabled != 0);
174 ctl->fd = qemud_channel_open(SENSORS_SERVICE_NAME);
177 ret = qemud_channel_send(ctl->fd, command, -1);
179 E("%s: when sending command errno=%d: %s", __FUNCTION__, errno, strerror(errno));
182 ctl->active_sensors = new_sensors;
188 control__set_delay(struct sensors_control_device_t *dev, int32_t ms)
190 SensorControl* ctl = (void*)dev;
193 D("%s: dev=%p delay-ms=%d", __FUNCTION__, dev, ms);
195 snprintf(command, sizeof command, "set-delay:%d", ms);
197 return qemud_channel_send(ctl->fd, command, -1);
200 /* this function is used to force-stop the blocking read() in
201 * data__poll. In order to keep the implementation as simple
202 * as possible here, we send a command to the emulator which
203 * shall send back an appropriate data block to the system.
206 control__wake(struct sensors_control_device_t *dev)
208 SensorControl* ctl = (void*)dev;
209 D("%s: dev=%p", __FUNCTION__, dev);
210 return qemud_channel_send(ctl->fd, "wake", -1);
215 control__close(struct hw_device_t *dev)
217 SensorControl* ctl = (void*)dev;
223 /** SENSORS DATA DEVICE
225 ** This one is used to read sensor data from the hardware.
226 ** We implement this by simply reading the data from the
227 ** emulator through the QEMUD channel.
231 typedef struct SensorData {
232 struct sensors_data_device_t device;
233 sensors_data_t sensors[MAX_NUM_SENSORS];
235 uint32_t pendingSensors;
240 /* return the current time in nanoseconds */
246 clock_gettime(CLOCK_MONOTONIC, &ts);
248 return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
252 data__data_open(struct sensors_data_device_t *dev, native_handle_t* handle)
254 SensorData* data = (void*)dev;
256 D("%s: dev=%p fd=%d", __FUNCTION__, dev, handle->data[0]);
257 memset(&data->sensors, 0, sizeof(data->sensors));
259 for (i=0 ; i<MAX_NUM_SENSORS ; i++) {
260 data->sensors[i].vector.status = SENSOR_STATUS_ACCURACY_HIGH;
262 data->pendingSensors = 0;
264 data->timeOffset = 0;
266 data->events_fd = dup(handle->data[0]);
267 D("%s: dev=%p fd=%d (was %d)", __FUNCTION__, dev, data->events_fd, handle->data[0]);
268 native_handle_close(handle);
269 native_handle_delete(handle);
274 data__data_close(struct sensors_data_device_t *dev)
276 SensorData* data = (void*)dev;
277 D("%s: dev=%p", __FUNCTION__, dev);
278 if (data->events_fd > 0) {
279 close(data->events_fd);
280 data->events_fd = -1;
286 pick_sensor(SensorData* data,
287 sensors_data_t* values)
289 uint32_t mask = SUPPORTED_SENSORS;
291 uint32_t i = 31 - __builtin_clz(mask);
293 if (data->pendingSensors & (1<<i)) {
294 data->pendingSensors &= ~(1<<i);
295 *values = data->sensors[i];
296 values->sensor = (1<<i);
297 D("%s: %d [%f, %f, %f]", __FUNCTION__,
305 LOGE("No sensor to return!!! pendingSensors=%08x", data->pendingSensors);
306 // we may end-up in a busy loop, slow things down, just in case.
312 data__poll(struct sensors_data_device_t *dev, sensors_data_t* values)
314 SensorData* data = (void*)dev;
315 int fd = data->events_fd;
317 D("%s: data=%p", __FUNCTION__, dev);
319 // there are pending sensors, returns them now...
320 if (data->pendingSensors) {
321 return pick_sensor(data, values);
324 // wait until we get a complete event for an enabled sensor
325 uint32_t new_sensors = 0;
328 /* read the next event */
330 int len = qemud_channel_recv(data->events_fd, buff, sizeof buff-1);
335 E("%s: len=%d, errno=%d: %s", __FUNCTION__, len, errno, strerror(errno));
341 /* "wake" is sent from the emulator to exit this loop. This shall
342 * really be because another thread called "control__wake" in this
345 if (!strcmp((const char*)data, "wake")) {
349 /* "acceleration:<x>:<y>:<z>" corresponds to an acceleration event */
350 if (sscanf(buff, "acceleration:%g:%g:%g", params+0, params+1, params+2) == 3) {
351 new_sensors |= SENSORS_ACCELERATION;
352 data->sensors[ID_ACCELERATION].acceleration.x = params[0];
353 data->sensors[ID_ACCELERATION].acceleration.y = params[1];
354 data->sensors[ID_ACCELERATION].acceleration.z = params[2];
358 /* "orientation:<azimuth>:<pitch>:<roll>" is sent when orientation changes */
359 if (sscanf(buff, "orientation:%g:%g:%g", params+0, params+1, params+2) == 3) {
360 new_sensors |= SENSORS_ORIENTATION;
361 data->sensors[ID_ORIENTATION].orientation.azimuth = params[0];
362 data->sensors[ID_ORIENTATION].orientation.pitch = params[1];
363 data->sensors[ID_ORIENTATION].orientation.roll = params[2];
367 /* "magnetic:<x>:<y>:<z>" is sent for the params of the magnetic field */
368 if (sscanf(buff, "magnetic:%g:%g:%g", params+0, params+1, params+2) == 3) {
369 new_sensors |= SENSORS_MAGNETIC_FIELD;
370 data->sensors[ID_MAGNETIC_FIELD].magnetic.x = params[0];
371 data->sensors[ID_MAGNETIC_FIELD].magnetic.y = params[1];
372 data->sensors[ID_MAGNETIC_FIELD].magnetic.z = params[2];
376 /* "temperature:<celsius>" */
377 if (sscanf(buff, "temperature:%g", params+0) == 2) {
378 new_sensors |= SENSORS_TEMPERATURE;
379 data->sensors[ID_TEMPERATURE].temperature = params[0];
383 /* "sync:<time>" is sent after a series of sensor events.
384 * where 'time' is expressed in micro-seconds and corresponds
385 * to the VM time when the real poll occured.
387 if (sscanf(buff, "sync:%lld", &event_time) == 1) {
389 data->pendingSensors = new_sensors;
390 int64_t t = event_time * 1000LL; /* convert to nano-seconds */
392 /* use the time at the first sync: as the base for later
394 if (data->timeStart == 0) {
395 data->timeStart = data__now_ns();
396 data->timeOffset = data->timeStart - t;
398 t += data->timeOffset;
400 while (new_sensors) {
401 uint32_t i = 31 - __builtin_clz(new_sensors);
402 new_sensors &= ~(1<<i);
403 data->sensors[i].time = t;
405 return pick_sensor(data, values);
407 D("huh ? sync without any sensor data ?");
411 D("huh ? unsupported command");
416 data__close(struct hw_device_t *dev)
418 SensorData* data = (SensorData*)dev;
420 if (data->events_fd > 0) {
421 //LOGD("(device close) about to close fd=%d", data->events_fd);
422 close(data->events_fd);
430 /** MODULE REGISTRATION SUPPORT
432 ** This is required so that hardware/libhardware/hardware.c
433 ** will dlopen() this library appropriately.
437 * the following is the list of all supported sensors.
438 * this table is used to build sSensorList declared below
439 * according to which hardware sensors are reported as
440 * available from the emulator (see get_sensors_list below)
442 * note: numerical values for maxRange/resolution/power were
443 * taken from the reference AK8976A implementation
445 static const struct sensor_t sSensorListInit[] = {
446 { .name = "Goldfish 3-axis Accelerometer",
447 .vendor = "The Android Open Source Project",
449 .handle = ID_ACCELERATION,
450 .type = SENSOR_TYPE_ACCELEROMETER,
452 .resolution = 1.0f/4032.0f,
457 { .name = "Goldfish 3-axis Magnetic field sensor",
458 .vendor = "The Android Open Source Project",
460 .handle = ID_MAGNETIC_FIELD,
461 .type = SENSOR_TYPE_MAGNETIC_FIELD,
468 { .name = "Goldfish Orientation sensor",
469 .vendor = "The Android Open Source Project",
471 .handle = ID_ORIENTATION,
472 .type = SENSOR_TYPE_ORIENTATION,
479 { .name = "Goldfish Temperature sensor",
480 .vendor = "The Android Open Source Project",
482 .handle = ID_TEMPERATURE,
483 .type = SENSOR_TYPE_TEMPERATURE,
491 static struct sensor_t sSensorList[MAX_NUM_SENSORS];
493 static uint32_t sensors__get_sensors_list(struct sensors_module_t* module,
494 struct sensor_t const** list)
496 int fd = qemud_channel_open(SENSORS_SERVICE_NAME);
502 E("%s: no qemud connection", __FUNCTION__);
505 ret = qemud_channel_send(fd, "list-sensors", -1);
507 E("%s: could not query sensor list: %s", __FUNCTION__,
512 ret = qemud_channel_recv(fd, buffer, sizeof buffer-1);
514 E("%s: could not receive sensor list: %s", __FUNCTION__,
522 /* the result is a integer used as a mask for available sensors */
525 for (nn = 0; nn < MAX_NUM_SENSORS; nn++) {
526 if (((1 << nn) & mask) == 0)
529 sSensorList[count++] = sSensorListInit[nn];
531 D("%s: returned %d sensors (mask=%d)", __FUNCTION__, count, mask);
538 open_sensors(const struct hw_module_t* module,
540 struct hw_device_t* *device)
542 int status = -EINVAL;
544 D("%s: name=%s", __FUNCTION__, name);
546 if (!strcmp(name, SENSORS_HARDWARE_CONTROL))
548 SensorControl *dev = malloc(sizeof(*dev));
550 memset(dev, 0, sizeof(*dev));
552 dev->device.common.tag = HARDWARE_DEVICE_TAG;
553 dev->device.common.version = 0;
554 dev->device.common.module = (struct hw_module_t*) module;
555 dev->device.common.close = control__close;
556 dev->device.open_data_source = control__open_data_source;
557 dev->device.activate = control__activate;
558 dev->device.set_delay = control__set_delay;
559 dev->device.wake = control__wake;
562 *device = &dev->device.common;
565 else if (!strcmp(name, SENSORS_HARDWARE_DATA)) {
566 SensorData *dev = malloc(sizeof(*dev));
568 memset(dev, 0, sizeof(*dev));
570 dev->device.common.tag = HARDWARE_DEVICE_TAG;
571 dev->device.common.version = 0;
572 dev->device.common.module = (struct hw_module_t*) module;
573 dev->device.common.close = data__close;
574 dev->device.data_open = data__data_open;
575 dev->device.data_close = data__data_close;
576 dev->device.poll = data__poll;
579 *device = &dev->device.common;
586 static struct hw_module_methods_t sensors_module_methods = {
590 const struct sensors_module_t HAL_MODULE_INFO_SYM = {
592 .tag = HARDWARE_MODULE_TAG,
595 .id = SENSORS_HARDWARE_MODULE_ID,
596 .name = "Goldfish SENSORS Module",
597 .author = "The Android Open Source Project",
598 .methods = &sensors_module_methods,
600 .get_sensors_list = sensors__get_sensors_list