int get_events_val(ECOM_ValueTypeDef *data) { struct input_event acc_event; struct input_event mag_event; int ret; while(1) { ret = read(acc_event_fd, &acc_event, sizeof(struct input_event)); if (ret != sizeof(struct input_event)) continue; if(acc_event.type == EV_ABS) { if(acc_event.code == ABS_X) data->ax = acc_event.value; if(acc_event.code == ABS_Y) data->ay = acc_event.value; if(acc_event.code == ABS_Z) data->az = acc_event.value; } if(acc_event.type == 0) break; } while(1) { ret = read(mag_event_fd, &mag_event, sizeof(struct input_event)); if (ret != sizeof(struct input_event)) continue; if(mag_event.type == EV_ABS) { if(mag_event.code == ABS_X) data->mx = mag_event.value; if(mag_event.code == ABS_Y) data->my = mag_event.value; if(mag_event.code == ABS_Z) data->mz = mag_event.value; } if(mag_event.type == 0) break; } data->time = data__now_ns()/1000000; LOGD_IF(0, "%s %d %d %d %d %d %d %d\n", __FUNCTION__, data->ax, data->ay, data->az, data->mx, data->my, data->mz, data->time); return 0; }
static int data__poll(struct sensors_poll_device_t *dev, sensors_event_t* values) { SensorPoll* 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. */ if (!strcmp((const char*)data, "wake")) { return 0x7FFFFFFF; } /* "acceleration:<x>:<y>:<z>" 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:<azimuth>:<pitch>:<roll>" 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]; data->sensors[ID_ORIENTATION].orientation.status = SENSOR_STATUS_ACCURACY_HIGH; continue; } /* "magnetic:<x>:<y>:<z>" 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]; data->sensors[ID_MAGNETIC_FIELD].magnetic.status = SENSOR_STATUS_ACCURACY_HIGH; continue; } /* "temperature:<celsius>" */ if (sscanf(buff, "temperature:%g", params+0) == 1) { new_sensors |= SENSORS_TEMPERATURE; data->sensors[ID_TEMPERATURE].temperature = params[0]; continue; } /* "proximity:<value>" */ if (sscanf(buff, "proximity:%g", params+0) == 1) { new_sensors |= SENSORS_PROXIMITY; data->sensors[ID_PROXIMITY].distance = params[0]; continue; } /* "sync:<time>" is sent after a series of sensor events. * where 'time' is expressed in micro-seconds and corresponds * to the VM time when the real poll occured. */ if (sscanf(buff, "sync:%lld", &event_time) == 1) { if (new_sensors) { data->pendingSensors = new_sensors; int64_t t = event_time * 1000LL; /* convert to nano-seconds */ /* use the time at the first sync: as the base for later * time values */ if (data->timeStart == 0) { data->timeStart = data__now_ns(); data->timeOffset = data->timeStart - t; } t += data->timeOffset; while (new_sensors) { uint32_t i = 31 - __builtin_clz(new_sensors); new_sensors &= ~(1<<i); data->sensors[i].timestamp = t; } return pick_sensor(data, values); } else { D("huh ? sync without any sensor data ?"); } continue; } D("huh ? unsupported command"); } return -1; }