SensorFd(const struct hw_module_t *module, struct hw_device_t **device);
~SensorFd();
-
- static int common_close(struct hw_device_t *dev);
};
template <typename T> SensorFd<T>::SensorFd(const struct hw_module_t *module, struct hw_device_t **device) : ufd(-1)
this->common.tag = HARDWARE_DEVICE_TAG;
this->common.version = 0;
this->common.module = const_cast<struct hw_module_t *>(module);
- this->common.close = common_close;
*device = &this->common;
LOGD("%s: module=%p dev=%p", __FUNCTION__, module, *device);
}
close(ufd);
}
-template <typename T> int SensorFd<T>::common_close(struct hw_device_t *dev)
-{
- LOGD("%s: dev=%p", __FUNCTION__, dev);
- delete reinterpret_cast<SensorFd<T> *>(dev);
- return 0;
-}
-
-/**
- ** SENSORS CONTROL DEVICE -- used to send commands to the sensors drivers
- **/
-struct SensorControl : SensorFd<sensors_control_device_t> {
+struct SensorPollContext : SensorFd<sensors_poll_device_t> {
public:
- SensorControl(const struct hw_module_t *module, struct hw_device_t **device);
+ SensorPollContext(const struct hw_module_t *module, struct hw_device_t **device);
+ ~SensorPollContext();
private:
- static native_handle_t *control_open_data_source(struct sensors_control_device_t *dev);
- static int control_activate(struct sensors_control_device_t *dev, int handle, int enabled);
- static int control_set_delay(struct sensors_control_device_t *dev, int32_t ms);
- static int control_wake(struct sensors_control_device_t *dev);
+ static int poll_close(struct hw_device_t *dev);
+ static int poll_activate(struct sensors_poll_device_t *dev, int handle, int enabled);
+ static int poll_setDelay(struct sensors_poll_device_t *dev, int handle, int64_t ns);
+ static int poll_poll(struct sensors_poll_device_t *dev, sensors_event_t *data, int count);
+
+ enum {
+ ROT_0,
+ ROT_90,
+ ROT_180,
+ ROT_270
+ };
+
+ bool enabled;
+ int rotation;
+ struct pollfd pfd;
+ sensors_event_t orients[4];
};
-SensorControl::SensorControl(const struct hw_module_t *module, struct hw_device_t **device)
- : SensorFd<sensors_control_device_t>(module, device)
+SensorPollContext::SensorPollContext(const struct hw_module_t *module, struct hw_device_t **device)
+ : SensorFd<sensors_poll_device_t>(module, device), enabled(false), rotation(ROT_0)
{
- open_data_source = control_open_data_source;
- activate = control_activate;
- set_delay = control_set_delay;
- wake = control_wake;
-}
+ common.close = poll_close;
+ activate = poll_activate;
+ setDelay = poll_setDelay;
+ poll = poll_poll;
-native_handle_t* SensorControl::control_open_data_source(struct sensors_control_device_t *dev)
-{
- SensorControl *ctl = reinterpret_cast<SensorControl *>(dev);
- native_handle_t *handle;
- handle = native_handle_create(1, 1);
- int fd = -1;
+ int &fd = pfd.fd;
const char *dirname = "/dev/input";
if (DIR *dir = opendir(dirname)) {
while (struct dirent *de = readdir(dir)) {
}
closedir(dir);
}
- handle->data[0] = fd;
- handle->data[1] = -1;
- if (ctl->ufd < 0) {
- fd = open("/dev/uinput", O_WRONLY | O_NDELAY);
- if (fd >= 0) {
- struct uinput_user_dev ud;
- memset(&ud, 0, sizeof(ud));
- strcpy(ud.name, "Tega V2 Buttons");
- write(fd, &ud, sizeof(ud));
- ioctl(fd, UI_SET_EVBIT, EV_KEY);
- ioctl(fd, UI_SET_EVBIT, EV_REP);
- ioctl(fd, UI_SET_KEYBIT, KEY_ESC);
- ioctl(fd, UI_SET_KEYBIT, KEY_COMPOSE);
- ioctl(fd, UI_SET_KEYBIT, KEY_LEFTMETA);
- ioctl(fd, UI_DEV_CREATE, 0);
- } else {
- LOGE("could not open uinput device: %s", strerror(errno));
- }
- handle->data[1] = ctl->ufd = fd;
+ ufd = open("/dev/uinput", O_WRONLY | O_NDELAY);
+ if (ufd >= 0) {
+ struct uinput_user_dev ud;
+ memset(&ud, 0, sizeof(ud));
+ strcpy(ud.name, "Tega V2 Buttons");
+ write(ufd, &ud, sizeof(ud));
+ ioctl(ufd, UI_SET_EVBIT, EV_KEY);
+ ioctl(ufd, UI_SET_EVBIT, EV_REP);
+ ioctl(ufd, UI_SET_KEYBIT, KEY_ESC);
+ ioctl(ufd, UI_SET_KEYBIT, KEY_COMPOSE);
+ ioctl(ufd, UI_SET_KEYBIT, KEY_LEFTMETA);
+ ioctl(ufd, UI_DEV_CREATE, 0);
+ } else {
+ LOGE("could not open uinput device: %s", strerror(errno));
}
- LOGD("%s: dev=%p handle=%p data[0]=%d data[1]=%d", __FUNCTION__, dev, handle, handle->data[0], handle->data[1]);
- return handle;
-}
-
-int SensorControl::control_activate(struct sensors_control_device_t *dev, int handle, int enabled)
-{
- LOGD("%s: dev=%p handle=%d enabled=%d", __FUNCTION__, dev, handle, enabled);
- return 0;
-}
-
-int SensorControl::control_set_delay(struct sensors_control_device_t *dev, int32_t ms)
-{
- LOGD("%s: dev=%p delay-ms=%d", __FUNCTION__, dev, ms);
- return 0;
-}
-
-int SensorControl::control_wake(struct sensors_control_device_t *dev)
-{
- LOGD("%s: dev=%p", __FUNCTION__, dev);
- return 0;
-}
-
-/**
- ** SENSORS DATA DEVICE -- used to read sensor data from the hardware.
- **/
-class SensorData : SensorFd<sensors_data_device_t> {
- public:
- SensorData(const struct hw_module_t *module, struct hw_device_t **device);
-
- private:
- static int data_data_open(struct sensors_data_device_t *dev, native_handle_t *handle);
- static int data_data_close(struct sensors_data_device_t *dev);
- static int data_poll(struct sensors_data_device_t *dev, sensors_data_t *values);
-
- enum {
- ROT_0,
- ROT_90,
- ROT_180,
- ROT_270
- };
-
- int rotation;
- struct pollfd pfd;
- sensors_data_t orients[4];
-};
-
-SensorData::SensorData(const struct hw_module_t *module, struct hw_device_t **device)
- : SensorFd<sensors_data_device_t>(module, device), rotation(ROT_0)
-{
- data_open = data_data_open;
- data_close = data_data_close;
- poll = data_poll;
-
pfd.events = POLLIN;
+ orients[ROT_0].version = sizeof(sensors_event_t);
orients[ROT_0].sensor = ID_ACCELERATION;
+ orients[ROT_0].type = SENSOR_TYPE_ACCELEROMETER;
orients[ROT_0].acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
orients[ROT_270] = orients[ROT_180] = orients[ROT_90] = orients[ROT_0];
const double angle = 20.0;
orients[ROT_270].acceleration.x = -cos_angle;
orients[ROT_270].acceleration.y = 0.0;
orients[ROT_270].acceleration.z = -sin_angle;
+
+ LOGD("%s: dev=%p ufd=%d fd=%d", __FUNCTION__, this, ufd, fd);
}
-int SensorData::data_data_open(struct sensors_data_device_t *dev, native_handle_t *handle)
+SensorPollContext::~SensorPollContext()
{
- SensorData *data = reinterpret_cast<SensorData *>(dev);
-
- data->ufd = handle->data[1];
- data->pfd.fd = dup(handle->data[0]);
- LOGD("%s: dev=%p ufd=%d fd=%d(%d) handle=%p)", __FUNCTION__, dev, data->ufd, data->pfd.fd, handle->data[0], handle);
- native_handle_close(handle);
- native_handle_delete(handle);
- return 0;
+ close(pfd.fd);
}
-int SensorData::data_data_close(struct sensors_data_device_t *dev)
+int SensorPollContext::poll_close(struct hw_device_t *dev)
{
LOGD("%s: dev=%p", __FUNCTION__, dev);
- SensorData *data = reinterpret_cast<SensorData *>(dev);
- if (data) {
- close(data->ufd);
- data->ufd = -1;
- close(data->pfd.fd);
- data->pfd.fd = -1;
- }
+ delete reinterpret_cast<SensorPollContext *>(dev);
return 0;
}
-
-int SensorData::data_poll(struct sensors_data_device_t *dev, sensors_data_t *values)
+int SensorPollContext::poll_activate(struct sensors_poll_device_t *dev, int handle, int enabled)
{
- SensorData *data = reinterpret_cast<SensorData *>(dev);
- LOGV("%s: dev=%p fd=%d,%d", __FUNCTION__, dev, data->fd[0], data->fd[1]);
+ LOGD("%s: dev=%p handle=%d enabled=%d", __FUNCTION__, dev, handle, enabled);
+ SensorPollContext *ctx = reinterpret_cast<SensorPollContext *>(dev);
+ ctx->enabled = enabled;
+ return 0;
+}
+int SensorPollContext::poll_setDelay(struct sensors_poll_device_t *dev, int handle, int64_t ns)
+{
+ LOGD("%s: dev=%p delay-ns=%lld", __FUNCTION__, dev, ns);
+ return 0;
+}
+int SensorPollContext::poll_poll(struct sensors_poll_device_t *dev, sensors_event_t *data, int count)
+{
+ LOGD("%s: dev=%p data=%p count=%d", __FUNCTION__, dev, data, count);
+ SensorPollContext *ctx = reinterpret_cast<SensorPollContext *>(dev);
- struct pollfd &pfd = data->pfd;
+ struct pollfd &pfd = ctx->pfd;
while (int pollres = ::poll(&pfd, 1, -1)) {
if (pollres < 0) {
LOGE("%s: poll %d error: %s", __FUNCTION__, pfd.fd, strerror(errno));
case KEY_LEFTALT:
if (iev.value)
continue;
- rot = data->rotation;
+ rot = ctx->rotation;
break;
case KEY_UP:
rot = ROT_0;
#endif
}
if (rot >= 0) {
- if (rot != data->rotation) {
- LOGI("orientation changed from %d to %d", data->rotation * 90, rot * 90);
- data->rotation = rot;
+ if (rot != ctx->rotation) {
+ LOGI("orientation changed from %d to %d", ctx->rotation * 90, rot * 90);
+ ctx->rotation = rot;
}
- break;
+ if (ctx->enabled && count > 0)
+ break;
}
}
- if (data->ufd >= 0)
- write(data->ufd, &iev, sizeof(iev));
+ if (ctx->ufd >= 0)
+ write(ctx->ufd, &iev, sizeof(iev));
}
- *values = data->orients[data->rotation];
- LOGD("%s: dev=%p ufd=%d fd=%d rotation=%d", __FUNCTION__, dev, data->ufd, pfd.fd, data->rotation * 90);
- return 0;
+ LOGD("%s: dev=%p ufd=%d fd=%d rotation=%d", __FUNCTION__, dev, ctx->ufd, pfd.fd, ctx->rotation * 90);
+ data[0] = ctx->orients[ctx->rotation];
+ return 1;
}
static int open_kbd_sensor(const struct hw_module_t *module, const char *id, struct hw_device_t **device)
{
LOGD("%s: id=%s", __FUNCTION__, id);
- void *dev = 0;
- if (!strcmp(id, SENSORS_HARDWARE_CONTROL))
- dev = new SensorControl(module, device);
- else if (!strcmp(id, SENSORS_HARDWARE_DATA))
- dev = new SensorData(module, device);
- return dev ? 0 : -1;
+ return new SensorPollContext(module, device) ? 0 : -EINVAL;
}
static struct sensor_t sSensorListInit[] = {
maxRange: 2.8f,
resolution: 1.0f/4032.0f,
power: 3.0f,
+ minDelay: 0,
reserved: { }
}
};
common: {
tag: HARDWARE_MODULE_TAG,
version_major: 2,
- version_minor: 2,
+ version_minor: 3,
id: SENSORS_HARDWARE_MODULE_ID,
name: "Kbd Orientation Sensor",
author: "Chih-Wei Huang",