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] = 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) enabled=%d", __FUNCTION__,
153 _sensorIdToName(handle), handle, 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);
181 ctl->active_sensors = new_sensors;
187 control__set_delay(struct sensors_control_device_t *dev, int32_t ms)
189 SensorControl* ctl = (void*)dev;
192 D("%s: dev=%p delay-ms=%d", __FUNCTION__, dev, ms);
194 snprintf(command, sizeof command, "set-delay:%d", ms);
196 return qemud_channel_send(ctl->fd, command, -1);
199 /* this function is used to force-stop the blocking read() in
200 * data__poll. In order to keep the implementation as simple
201 * as possible here, we send a command to the emulator which
202 * shall send back an appropriate data block to the system.
205 control__wake(struct sensors_control_device_t *dev)
207 SensorControl* ctl = (void*)dev;
208 D("%s: dev=%p", __FUNCTION__, dev);
209 return qemud_channel_send(ctl->fd, "wake", -1);
214 control__close(struct hw_device_t *dev)
216 SensorControl* ctl = (void*)dev;
222 /** SENSORS DATA DEVICE
224 ** This one is used to read sensor data from the hardware.
225 ** We implement this by simply reading the data from the
226 ** emulator through the QEMUD channel.
230 typedef struct SensorData {
231 struct sensors_data_device_t device;
232 sensors_data_t sensors[MAX_NUM_SENSORS];
234 uint32_t pendingSensors;
239 /* return the current time in nanoseconds */
245 clock_gettime(CLOCK_MONOTONIC, &ts);
247 return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
251 data__data_open(struct sensors_data_device_t *dev, native_handle_t* handle)
253 SensorData* data = (void*)dev;
255 D("%s: dev=%p fd=%d", __FUNCTION__, dev, fd);
256 memset(&data->sensors, 0, sizeof(data->sensors));
258 for (i=0 ; i<MAX_NUM_SENSORS ; i++) {
259 data->sensors[i].vector.status = SENSOR_STATUS_ACCURACY_HIGH;
261 data->pendingSensors = 0;
263 data->timeOffset = 0;
265 data->events_fd = dup(handle->data[0]);
266 native_handle_close(handle);
267 native_handle_delete(handle);
272 data__data_close(struct sensors_data_device_t *dev)
274 SensorData* data = (void*)dev;
275 D("%s: dev=%p", __FUNCTION__, dev);
276 if (data->events_fd > 0) {
277 close(data->events_fd);
278 data->events_fd = -1;
284 pick_sensor(SensorData* data,
285 sensors_data_t* values)
287 uint32_t mask = SUPPORTED_SENSORS;
289 uint32_t i = 31 - __builtin_clz(mask);
291 if (data->pendingSensors & (1<<i)) {
292 data->pendingSensors &= ~(1<<i);
293 *values = data->sensors[i];
294 values->sensor = (1<<i);
295 LOGD_IF(0, "%s: %d [%f, %f, %f]", __FUNCTION__,
303 LOGE("No sensor to return!!! pendingSensors=%08x", data->pendingSensors);
304 // we may end-up in a busy loop, slow things down, just in case.
310 data__poll(struct sensors_data_device_t *dev, sensors_data_t* values)
312 SensorData* data = (void*)dev;
313 int fd = data->events_fd;
315 D("%s: data=%p", __FUNCTION__, dev);
317 // there are pending sensors, returns them now...
318 if (data->pendingSensors) {
319 return pick_sensor(data, values);
322 // wait until we get a complete event for an enabled sensor
323 uint32_t new_sensors = 0;
326 /* read the next event */
328 int len = qemud_channel_recv(data->events_fd, buff, sizeof buff-1);
337 /* "wake" is sent from the emulator to exit this loop. This shall
338 * really be because another thread called "control__wake" in this
341 if (!strcmp((const char*)data, "wake")) {
345 /* "acceleration:<x>:<y>:<z>" corresponds to an acceleration event */
346 if (sscanf(buff, "acceleration:%g:%g:%g", params+0, params+1, params+2) == 3) {
347 new_sensors |= SENSORS_ACCELERATION;
348 data->sensors[ID_ACCELERATION].acceleration.x = params[0];
349 data->sensors[ID_ACCELERATION].acceleration.y = params[1];
350 data->sensors[ID_ACCELERATION].acceleration.z = params[2];
354 /* "orientation:<azimuth>:<pitch>:<roll>" is sent when orientation changes */
355 if (sscanf(buff, "orientation:%g:%g:%g", params+0, params+1, params+2) == 3) {
356 new_sensors |= SENSORS_ORIENTATION;
357 data->sensors[ID_ORIENTATION].orientation.azimuth = params[0];
358 data->sensors[ID_ORIENTATION].orientation.pitch = params[1];
359 data->sensors[ID_ORIENTATION].orientation.roll = params[2];
363 /* "magnetic:<x>:<y>:<z>" is sent for the params of the magnetic field */
364 if (sscanf(buff, "magnetic:%g:%g:%g", params+0, params+1, params+2) == 3) {
365 new_sensors |= SENSORS_MAGNETIC_FIELD;
366 data->sensors[ID_MAGNETIC_FIELD].magnetic.x = params[0];
367 data->sensors[ID_MAGNETIC_FIELD].magnetic.y = params[1];
368 data->sensors[ID_MAGNETIC_FIELD].magnetic.z = params[2];
372 /* "temperature:<celsius>" */
373 if (sscanf(buff, "temperature:%g", params+0) == 2) {
374 new_sensors |= SENSORS_TEMPERATURE;
375 data->sensors[ID_TEMPERATURE].temperature = params[0];
379 /* "sync:<time>" is sent after a series of sensor events.
380 * where 'time' is expressed in micro-seconds and corresponds
381 * to the VM time when the real poll occured.
383 if (sscanf(buff, "sync:%lld", &event_time) == 1) {
385 data->pendingSensors = new_sensors;
386 int64_t t = event_time * 1000LL; /* convert to nano-seconds */
388 /* use the time at the first sync: as the base for later
390 if (data->timeStart == 0) {
391 data->timeStart = data__now_ns();
392 data->timeOffset = data->timeStart - t;
394 t += data->timeOffset;
396 while (new_sensors) {
397 uint32_t i = 31 - __builtin_clz(new_sensors);
398 new_sensors &= ~(1<<i);
399 data->sensors[i].time = t;
401 return pick_sensor(data, values);
403 D("huh ? sync without any sensor data ?");
407 D("huh ? unsupported command");
412 data__close(struct hw_device_t *dev)
414 SensorData* data = (SensorData*)dev;
416 if (data->events_fd > 0) {
417 //LOGD("(device close) about to close fd=%d", data->events_fd);
418 close(data->events_fd);
426 /** MODULE REGISTRATION SUPPORT
428 ** This is required so that hardware/libhardware/hardware.c
429 ** will dlopen() this library appropriately.
433 * the following is the list of all supported sensors.
434 * this table is used to build sSensorList declared below
435 * according to which hardware sensors are reported as
436 * available from the emulator (see get_sensors_list below)
438 * note: numerical values for maxRange/resolution/power were
439 * taken from the reference AK8976A implementation
441 static const struct sensor_t sSensorListInit[] = {
442 { .name = "Goldfish 3-axis Accelerometer",
443 .vendor = "The Android Open Source Project",
445 .handle = ID_ACCELERATION,
446 .type = SENSOR_TYPE_ACCELEROMETER,
448 .resolution = 1.0f/4032.0f,
453 { .name = "Goldfish 3-axis Magnetic field sensor",
454 .vendor = "The Android Open Source Project",
456 .handle = ID_MAGNETIC_FIELD,
457 .type = SENSOR_TYPE_MAGNETIC_FIELD,
464 { .name = "Goldfish Orientation sensor",
465 .vendor = "The Android Open Source Project",
467 .handle = ID_ORIENTATION,
468 .type = SENSOR_TYPE_ORIENTATION,
475 { .name = "Goldfish Temperature sensor",
476 .vendor = "The Android Open Source Project",
478 .handle = ID_TEMPERATURE,
479 .type = SENSOR_TYPE_TEMPERATURE,
487 static struct sensor_t sSensorList[MAX_NUM_SENSORS];
489 static uint32_t sensors__get_sensors_list(struct sensors_module_t* module,
490 struct sensor_t const** list)
492 int fd = qemud_channel_open(SENSORS_SERVICE_NAME);
498 E("%s: no qemud connection", __FUNCTION__);
501 ret = qemud_channel_send(fd, "list-sensors", -1);
503 E("%s: could not query sensor list: %s", __FUNCTION__,
508 ret = qemud_channel_recv(fd, buffer, sizeof buffer-1);
510 E("%s: could not receive sensor list: %s", __FUNCTION__,
518 /* the result is a integer used as a mask for available sensors */
521 for (nn = 0; nn < MAX_NUM_SENSORS; nn++) {
522 if (((1 << nn) & mask) == 0)
525 sSensorList[count++] = sSensorListInit[nn];
527 D("%s: returned %d sensors (mask=%d)", __FUNCTION__, count, mask);
534 open_sensors(const struct hw_module_t* module,
536 struct hw_device_t* *device)
538 int status = -EINVAL;
540 D("%s: name=%s", __FUNCTION__, name);
542 if (!strcmp(name, SENSORS_HARDWARE_CONTROL))
544 SensorControl *dev = malloc(sizeof(*dev));
546 memset(dev, 0, sizeof(*dev));
548 dev->device.common.tag = HARDWARE_DEVICE_TAG;
549 dev->device.common.version = 0;
550 dev->device.common.module = (struct hw_module_t*) module;
551 dev->device.common.close = control__close;
552 dev->device.open_data_source = control__open_data_source;
553 dev->device.activate = control__activate;
554 dev->device.set_delay = control__set_delay;
555 dev->device.wake = control__wake;
558 *device = &dev->device.common;
561 else if (!strcmp(name, SENSORS_HARDWARE_DATA)) {
562 SensorData *dev = malloc(sizeof(*dev));
564 memset(dev, 0, sizeof(*dev));
566 dev->device.common.tag = HARDWARE_DEVICE_TAG;
567 dev->device.common.version = 0;
568 dev->device.common.module = (struct hw_module_t*) module;
569 dev->device.common.close = data__close;
570 dev->device.data_open = data__data_open;
571 dev->device.data_close = data__data_close;
572 dev->device.poll = data__poll;
575 *device = &dev->device.common;
582 static struct hw_module_methods_t sensors_module_methods = {
586 const struct sensors_module_t HAL_MODULE_INFO_SYM = {
588 .tag = HARDWARE_MODULE_TAG,
591 .id = SENSORS_HARDWARE_MODULE_ID,
592 .name = "Goldfish SENSORS Module",
593 .author = "The Android Open Source Project",
594 .methods = &sensors_module_methods,
596 .get_sensors_list = sensors__get_sensors_list