/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* this implements a sensors hardware library for the Android emulator. * the following code should be built as a shared library that will be * placed into /system/lib/hw/sensors.goldfish.so * * it will be loaded by the code in hardware/libhardware/hardware.c * which is itself called from com_android_server_SensorService.cpp */ /* we connect with the emulator through the "sensors" qemud service */ #define SENSORS_SERVICE_NAME "sensors" #define LOG_TAG "QemuSensors" #include #include #include #include #include #include #include #include #if 0 #define D(...) LOGD(__VA_ARGS__) #else #define D(...) ((void)0) #endif #define E(...) LOGE(__VA_ARGS__) #include /** SENSOR IDS AND NAMES **/ #define MAX_NUM_SENSORS 4 #define SUPPORTED_SENSORS ((1<"; } static int _sensorIdFromName( const char* name ) { int nn; if (name == NULL) return -1; for (nn = 0; nn < MAX_NUM_SENSORS; nn++) if (!strcmp(name, _sensorIds[nn].name)) return _sensorIds[nn].id; return -1; } /** SENSORS CONTROL DEVICE ** ** This one is used to send commands to the sensors drivers. ** We implement this by sending directly commands to the emulator ** through the QEMUD channel. **/ typedef struct SensorControl { struct sensors_control_device_t device; int fd; uint32_t active_sensors; } SensorControl; /* this must return a file descriptor that will be used to read * the sensors data (it is passed to data__data_open() below */ static native_handle_t* control__open_data_source(struct sensors_control_device_t *dev) { SensorControl* ctl = (void*)dev; native_handle_t* handle; if (ctl->fd < 0) { ctl->fd = qemud_channel_open(SENSORS_SERVICE_NAME); } D("%s: fd=%d", __FUNCTION__, ctl->fd); handle = native_handle_create(1, 0); handle->data[0] = dup(ctl->fd); return handle; } static int control__activate(struct sensors_control_device_t *dev, int handle, int enabled) { SensorControl* ctl = (void*)dev; uint32_t mask, sensors, active, new_sensors, changed; char command[128]; int ret; D("%s: handle=%s (%d) fd=%d enabled=%d", __FUNCTION__, _sensorIdToName(handle), handle, ctl->fd, enabled); if (!ID_CHECK(handle)) { E("%s: bad handle ID", __FUNCTION__); return -1; } mask = (1<active_sensors; new_sensors = (active & ~mask) | (sensors & mask); changed = active ^ new_sensors; if (!changed) return 0; snprintf(command, sizeof command, "set:%s:%d", _sensorIdToName(handle), enabled != 0); if (ctl->fd < 0) { ctl->fd = qemud_channel_open(SENSORS_SERVICE_NAME); } ret = qemud_channel_send(ctl->fd, command, -1); if (ret < 0) { E("%s: when sending command errno=%d: %s", __FUNCTION__, errno, strerror(errno)); return -1; } ctl->active_sensors = new_sensors; return 0; } static int control__set_delay(struct sensors_control_device_t *dev, int32_t ms) { SensorControl* ctl = (void*)dev; char command[128]; D("%s: dev=%p delay-ms=%d", __FUNCTION__, dev, ms); snprintf(command, sizeof command, "set-delay:%d", ms); return qemud_channel_send(ctl->fd, command, -1); } /* this function is used to force-stop the blocking read() in * data__poll. In order to keep the implementation as simple * as possible here, we send a command to the emulator which * shall send back an appropriate data block to the system. */ static int control__wake(struct sensors_control_device_t *dev) { SensorControl* ctl = (void*)dev; D("%s: dev=%p", __FUNCTION__, dev); return qemud_channel_send(ctl->fd, "wake", -1); } static int control__close(struct hw_device_t *dev) { SensorControl* ctl = (void*)dev; close(ctl->fd); free(ctl); return 0; } /** SENSORS DATA DEVICE ** ** This one is used to read sensor data from the hardware. ** We implement this by simply reading the data from the ** emulator through the QEMUD channel. **/ typedef struct SensorData { struct sensors_data_device_t device; sensors_data_t sensors[MAX_NUM_SENSORS]; int events_fd; uint32_t pendingSensors; int64_t timeStart; int64_t timeOffset; } SensorData; /* return the current time in nanoseconds */ static int64_t data__now_ns(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec; } static int data__data_open(struct sensors_data_device_t *dev, native_handle_t* handle) { SensorData* data = (void*)dev; int i; D("%s: dev=%p fd=%d", __FUNCTION__, dev, handle->data[0]); memset(&data->sensors, 0, sizeof(data->sensors)); for (i=0 ; isensors[i].vector.status = SENSOR_STATUS_ACCURACY_HIGH; } data->pendingSensors = 0; data->timeStart = 0; data->timeOffset = 0; data->events_fd = dup(handle->data[0]); D("%s: dev=%p fd=%d (was %d)", __FUNCTION__, dev, data->events_fd, handle->data[0]); native_handle_close(handle); native_handle_delete(handle); return 0; } static int data__data_close(struct sensors_data_device_t *dev) { SensorData* data = (void*)dev; D("%s: dev=%p", __FUNCTION__, dev); if (data->events_fd > 0) { close(data->events_fd); data->events_fd = -1; } return 0; } static int pick_sensor(SensorData* data, sensors_data_t* values) { uint32_t mask = SUPPORTED_SENSORS; while (mask) { uint32_t i = 31 - __builtin_clz(mask); mask &= ~(1<pendingSensors & (1<pendingSensors &= ~(1<sensors[i]; values->sensor = (1<vector.x, values->vector.y, values->vector.z); return i; } } LOGE("No sensor to return!!! pendingSensors=%08x", data->pendingSensors); // we may end-up in a busy loop, slow things down, just in case. usleep(100000); return -EINVAL; } static int data__poll(struct sensors_data_device_t *dev, sensors_data_t* values) { SensorData* data = (void*)dev; int fd = data->events_fd; D("%s: data=%p", __FUNCTION__, dev); // there are pending sensors, returns them now... if (data->pendingSensors) { return pick_sensor(data, values); } // wait until we get a complete event for an enabled sensor uint32_t new_sensors = 0; while (1) { /* read the next event */ char buff[256]; int len = qemud_channel_recv(data->events_fd, buff, sizeof buff-1); float params[3]; int64_t event_time; if (len < 0) { E("%s: len=%d, errno=%d: %s", __FUNCTION__, len, errno, strerror(errno)); return -errno; } buff[len] = 0; /* "wake" is sent from the emulator to exit this loop. This shall * really be because another thread called "control__wake" in this * process. */ if (!strcmp((const char*)data, "wake")) { return 0x7FFFFFFF; } /* "acceleration:::" corresponds to an acceleration event */ if (sscanf(buff, "acceleration:%g:%g:%g", params+0, params+1, params+2) == 3) { new_sensors |= SENSORS_ACCELERATION; data->sensors[ID_ACCELERATION].acceleration.x = params[0]; data->sensors[ID_ACCELERATION].acceleration.y = params[1]; data->sensors[ID_ACCELERATION].acceleration.z = params[2]; continue; } /* "orientation:::" is sent when orientation changes */ if (sscanf(buff, "orientation:%g:%g:%g", params+0, params+1, params+2) == 3) { new_sensors |= SENSORS_ORIENTATION; data->sensors[ID_ORIENTATION].orientation.azimuth = params[0]; data->sensors[ID_ORIENTATION].orientation.pitch = params[1]; data->sensors[ID_ORIENTATION].orientation.roll = params[2]; continue; } /* "magnetic:::" is sent for the params of the magnetic field */ if (sscanf(buff, "magnetic:%g:%g:%g", params+0, params+1, params+2) == 3) { new_sensors |= SENSORS_MAGNETIC_FIELD; data->sensors[ID_MAGNETIC_FIELD].magnetic.x = params[0]; data->sensors[ID_MAGNETIC_FIELD].magnetic.y = params[1]; data->sensors[ID_MAGNETIC_FIELD].magnetic.z = params[2]; continue; } /* "temperature:" */ if (sscanf(buff, "temperature:%g", params+0) == 2) { new_sensors |= SENSORS_TEMPERATURE; data->sensors[ID_TEMPERATURE].temperature = params[0]; continue; } /* "sync: