Android-x86
Fork
Faire un don

  • R/O
  • HTTP
  • SSH
  • HTTPS

hardware-libsensors: Commit

hardware/libsensors


Commit MetaInfo

Révision528fb5aeaf291255d7ade364d3832d82c9d6f6bf (tree)
l'heure2011-04-20 17:37:39
AuteurChih-Wei Huang <cwhuang@linu...>
CommiterChih-Wei Huang

Message de Log

update to use new sensor interface of gingerbread

Change Summary

Modification

--- a/kbdsensor.cpp
+++ b/kbdsensor.cpp
@@ -31,8 +31,6 @@ template <typename T> struct SensorFd : T {
3131
3232 SensorFd(const struct hw_module_t *module, struct hw_device_t **device);
3333 ~SensorFd();
34-
35- static int common_close(struct hw_device_t *dev);
3634 };
3735
3836 template <typename T> SensorFd<T>::SensorFd(const struct hw_module_t *module, struct hw_device_t **device) : ufd(-1)
@@ -40,7 +38,6 @@ template <typename T> SensorFd<T>::SensorFd(const struct hw_module_t *module, st
4038 this->common.tag = HARDWARE_DEVICE_TAG;
4139 this->common.version = 0;
4240 this->common.module = const_cast<struct hw_module_t *>(module);
43- this->common.close = common_close;
4441 *device = &this->common;
4542 LOGD("%s: module=%p dev=%p", __FUNCTION__, module, *device);
4643 }
@@ -50,42 +47,39 @@ template <typename T> SensorFd<T>::~SensorFd()
5047 close(ufd);
5148 }
5249
53-template <typename T> int SensorFd<T>::common_close(struct hw_device_t *dev)
54-{
55- LOGD("%s: dev=%p", __FUNCTION__, dev);
56- delete reinterpret_cast<SensorFd<T> *>(dev);
57- return 0;
58-}
59-
60-/**
61- ** SENSORS CONTROL DEVICE -- used to send commands to the sensors drivers
62- **/
63-struct SensorControl : SensorFd<sensors_control_device_t> {
50+struct SensorPollContext : SensorFd<sensors_poll_device_t> {
6451 public:
65- SensorControl(const struct hw_module_t *module, struct hw_device_t **device);
52+ SensorPollContext(const struct hw_module_t *module, struct hw_device_t **device);
53+ ~SensorPollContext();
6654
6755 private:
68- static native_handle_t *control_open_data_source(struct sensors_control_device_t *dev);
69- static int control_activate(struct sensors_control_device_t *dev, int handle, int enabled);
70- static int control_set_delay(struct sensors_control_device_t *dev, int32_t ms);
71- static int control_wake(struct sensors_control_device_t *dev);
56+ static int poll_close(struct hw_device_t *dev);
57+ static int poll_activate(struct sensors_poll_device_t *dev, int handle, int enabled);
58+ static int poll_setDelay(struct sensors_poll_device_t *dev, int handle, int64_t ns);
59+ static int poll_poll(struct sensors_poll_device_t *dev, sensors_event_t *data, int count);
60+
61+ enum {
62+ ROT_0,
63+ ROT_90,
64+ ROT_180,
65+ ROT_270
66+ };
67+
68+ bool enabled;
69+ int rotation;
70+ struct pollfd pfd;
71+ sensors_event_t orients[4];
7272 };
7373
74-SensorControl::SensorControl(const struct hw_module_t *module, struct hw_device_t **device)
75- : SensorFd<sensors_control_device_t>(module, device)
74+SensorPollContext::SensorPollContext(const struct hw_module_t *module, struct hw_device_t **device)
75+ : SensorFd<sensors_poll_device_t>(module, device), enabled(false), rotation(ROT_0)
7676 {
77- open_data_source = control_open_data_source;
78- activate = control_activate;
79- set_delay = control_set_delay;
80- wake = control_wake;
81-}
77+ common.close = poll_close;
78+ activate = poll_activate;
79+ setDelay = poll_setDelay;
80+ poll = poll_poll;
8281
83-native_handle_t* SensorControl::control_open_data_source(struct sensors_control_device_t *dev)
84-{
85- SensorControl *ctl = reinterpret_cast<SensorControl *>(dev);
86- native_handle_t *handle;
87- handle = native_handle_create(1, 1);
88- int fd = -1;
82+ int &fd = pfd.fd;
8983 const char *dirname = "/dev/input";
9084 if (DIR *dir = opendir(dirname)) {
9185 while (struct dirent *de = readdir(dir)) {
@@ -113,83 +107,27 @@ native_handle_t* SensorControl::control_open_data_source(struct sensors_control_
113107 }
114108 closedir(dir);
115109 }
116- handle->data[0] = fd;
117110
118- handle->data[1] = -1;
119- if (ctl->ufd < 0) {
120- fd = open("/dev/uinput", O_WRONLY | O_NDELAY);
121- if (fd >= 0) {
122- struct uinput_user_dev ud;
123- memset(&ud, 0, sizeof(ud));
124- strcpy(ud.name, "Tega V2 Buttons");
125- write(fd, &ud, sizeof(ud));
126- ioctl(fd, UI_SET_EVBIT, EV_KEY);
127- ioctl(fd, UI_SET_EVBIT, EV_REP);
128- ioctl(fd, UI_SET_KEYBIT, KEY_ESC);
129- ioctl(fd, UI_SET_KEYBIT, KEY_COMPOSE);
130- ioctl(fd, UI_SET_KEYBIT, KEY_LEFTMETA);
131- ioctl(fd, UI_DEV_CREATE, 0);
132- } else {
133- LOGE("could not open uinput device: %s", strerror(errno));
134- }
135- handle->data[1] = ctl->ufd = fd;
111+ ufd = open("/dev/uinput", O_WRONLY | O_NDELAY);
112+ if (ufd >= 0) {
113+ struct uinput_user_dev ud;
114+ memset(&ud, 0, sizeof(ud));
115+ strcpy(ud.name, "Tega V2 Buttons");
116+ write(ufd, &ud, sizeof(ud));
117+ ioctl(ufd, UI_SET_EVBIT, EV_KEY);
118+ ioctl(ufd, UI_SET_EVBIT, EV_REP);
119+ ioctl(ufd, UI_SET_KEYBIT, KEY_ESC);
120+ ioctl(ufd, UI_SET_KEYBIT, KEY_COMPOSE);
121+ ioctl(ufd, UI_SET_KEYBIT, KEY_LEFTMETA);
122+ ioctl(ufd, UI_DEV_CREATE, 0);
123+ } else {
124+ LOGE("could not open uinput device: %s", strerror(errno));
136125 }
137126
138- LOGD("%s: dev=%p handle=%p data[0]=%d data[1]=%d", __FUNCTION__, dev, handle, handle->data[0], handle->data[1]);
139- return handle;
140-}
141-
142-int SensorControl::control_activate(struct sensors_control_device_t *dev, int handle, int enabled)
143-{
144- LOGD("%s: dev=%p handle=%d enabled=%d", __FUNCTION__, dev, handle, enabled);
145- return 0;
146-}
147-
148-int SensorControl::control_set_delay(struct sensors_control_device_t *dev, int32_t ms)
149-{
150- LOGD("%s: dev=%p delay-ms=%d", __FUNCTION__, dev, ms);
151- return 0;
152-}
153-
154-int SensorControl::control_wake(struct sensors_control_device_t *dev)
155-{
156- LOGD("%s: dev=%p", __FUNCTION__, dev);
157- return 0;
158-}
159-
160-/**
161- ** SENSORS DATA DEVICE -- used to read sensor data from the hardware.
162- **/
163-class SensorData : SensorFd<sensors_data_device_t> {
164- public:
165- SensorData(const struct hw_module_t *module, struct hw_device_t **device);
166-
167- private:
168- static int data_data_open(struct sensors_data_device_t *dev, native_handle_t *handle);
169- static int data_data_close(struct sensors_data_device_t *dev);
170- static int data_poll(struct sensors_data_device_t *dev, sensors_data_t *values);
171-
172- enum {
173- ROT_0,
174- ROT_90,
175- ROT_180,
176- ROT_270
177- };
178-
179- int rotation;
180- struct pollfd pfd;
181- sensors_data_t orients[4];
182-};
183-
184-SensorData::SensorData(const struct hw_module_t *module, struct hw_device_t **device)
185- : SensorFd<sensors_data_device_t>(module, device), rotation(ROT_0)
186-{
187- data_open = data_data_open;
188- data_close = data_data_close;
189- poll = data_poll;
190-
191127 pfd.events = POLLIN;
128+ orients[ROT_0].version = sizeof(sensors_event_t);
192129 orients[ROT_0].sensor = ID_ACCELERATION;
130+ orients[ROT_0].type = SENSOR_TYPE_ACCELEROMETER;
193131 orients[ROT_0].acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
194132 orients[ROT_270] = orients[ROT_180] = orients[ROT_90] = orients[ROT_0];
195133 const double angle = 20.0;
@@ -207,39 +145,39 @@ SensorData::SensorData(const struct hw_module_t *module, struct hw_device_t **de
207145 orients[ROT_270].acceleration.x = -cos_angle;
208146 orients[ROT_270].acceleration.y = 0.0;
209147 orients[ROT_270].acceleration.z = -sin_angle;
148+
149+ LOGD("%s: dev=%p ufd=%d fd=%d", __FUNCTION__, this, ufd, fd);
210150 }
211151
212-int SensorData::data_data_open(struct sensors_data_device_t *dev, native_handle_t *handle)
152+SensorPollContext::~SensorPollContext()
213153 {
214- SensorData *data = reinterpret_cast<SensorData *>(dev);
215-
216- data->ufd = handle->data[1];
217- data->pfd.fd = dup(handle->data[0]);
218- LOGD("%s: dev=%p ufd=%d fd=%d(%d) handle=%p)", __FUNCTION__, dev, data->ufd, data->pfd.fd, handle->data[0], handle);
219- native_handle_close(handle);
220- native_handle_delete(handle);
221- return 0;
154+ close(pfd.fd);
222155 }
223156
224-int SensorData::data_data_close(struct sensors_data_device_t *dev)
157+int SensorPollContext::poll_close(struct hw_device_t *dev)
225158 {
226159 LOGD("%s: dev=%p", __FUNCTION__, dev);
227- SensorData *data = reinterpret_cast<SensorData *>(dev);
228- if (data) {
229- close(data->ufd);
230- data->ufd = -1;
231- close(data->pfd.fd);
232- data->pfd.fd = -1;
233- }
160+ delete reinterpret_cast<SensorPollContext *>(dev);
234161 return 0;
235162 }
236-
237-int SensorData::data_poll(struct sensors_data_device_t *dev, sensors_data_t *values)
163+int SensorPollContext::poll_activate(struct sensors_poll_device_t *dev, int handle, int enabled)
238164 {
239- SensorData *data = reinterpret_cast<SensorData *>(dev);
240- LOGV("%s: dev=%p fd=%d,%d", __FUNCTION__, dev, data->fd[0], data->fd[1]);
165+ LOGD("%s: dev=%p handle=%d enabled=%d", __FUNCTION__, dev, handle, enabled);
166+ SensorPollContext *ctx = reinterpret_cast<SensorPollContext *>(dev);
167+ ctx->enabled = enabled;
168+ return 0;
169+}
170+int SensorPollContext::poll_setDelay(struct sensors_poll_device_t *dev, int handle, int64_t ns)
171+{
172+ LOGD("%s: dev=%p delay-ns=%lld", __FUNCTION__, dev, ns);
173+ return 0;
174+}
175+int SensorPollContext::poll_poll(struct sensors_poll_device_t *dev, sensors_event_t *data, int count)
176+{
177+ LOGD("%s: dev=%p data=%p count=%d", __FUNCTION__, dev, data, count);
178+ SensorPollContext *ctx = reinterpret_cast<SensorPollContext *>(dev);
241179
242- struct pollfd &pfd = data->pfd;
180+ struct pollfd &pfd = ctx->pfd;
243181 while (int pollres = ::poll(&pfd, 1, -1)) {
244182 if (pollres < 0) {
245183 LOGE("%s: poll %d error: %s", __FUNCTION__, pfd.fd, strerror(errno));
@@ -265,7 +203,7 @@ int SensorData::data_poll(struct sensors_data_device_t *dev, sensors_data_t *val
265203 case KEY_LEFTALT:
266204 if (iev.value)
267205 continue;
268- rot = data->rotation;
206+ rot = ctx->rotation;
269207 break;
270208 case KEY_UP:
271209 rot = ROT_0;
@@ -289,32 +227,28 @@ int SensorData::data_poll(struct sensors_data_device_t *dev, sensors_data_t *val
289227 #endif
290228 }
291229 if (rot >= 0) {
292- if (rot != data->rotation) {
293- LOGI("orientation changed from %d to %d", data->rotation * 90, rot * 90);
294- data->rotation = rot;
230+ if (rot != ctx->rotation) {
231+ LOGI("orientation changed from %d to %d", ctx->rotation * 90, rot * 90);
232+ ctx->rotation = rot;
295233 }
296- break;
234+ if (ctx->enabled && count > 0)
235+ break;
297236 }
298237 }
299238
300- if (data->ufd >= 0)
301- write(data->ufd, &iev, sizeof(iev));
239+ if (ctx->ufd >= 0)
240+ write(ctx->ufd, &iev, sizeof(iev));
302241 }
303242
304- *values = data->orients[data->rotation];
305- LOGD("%s: dev=%p ufd=%d fd=%d rotation=%d", __FUNCTION__, dev, data->ufd, pfd.fd, data->rotation * 90);
306- return 0;
243+ LOGD("%s: dev=%p ufd=%d fd=%d rotation=%d", __FUNCTION__, dev, ctx->ufd, pfd.fd, ctx->rotation * 90);
244+ data[0] = ctx->orients[ctx->rotation];
245+ return 1;
307246 }
308247
309248 static int open_kbd_sensor(const struct hw_module_t *module, const char *id, struct hw_device_t **device)
310249 {
311250 LOGD("%s: id=%s", __FUNCTION__, id);
312- void *dev = 0;
313- if (!strcmp(id, SENSORS_HARDWARE_CONTROL))
314- dev = new SensorControl(module, device);
315- else if (!strcmp(id, SENSORS_HARDWARE_DATA))
316- dev = new SensorData(module, device);
317- return dev ? 0 : -1;
251+ return new SensorPollContext(module, device) ? 0 : -EINVAL;
318252 }
319253
320254 static struct sensor_t sSensorListInit[] = {
@@ -327,6 +261,7 @@ static struct sensor_t sSensorListInit[] = {
327261 maxRange: 2.8f,
328262 resolution: 1.0f/4032.0f,
329263 power: 3.0f,
264+ minDelay: 0,
330265 reserved: { }
331266 }
332267 };
@@ -345,7 +280,7 @@ struct sensors_module_t HAL_MODULE_INFO_SYM = {
345280 common: {
346281 tag: HARDWARE_MODULE_TAG,
347282 version_major: 2,
348- version_minor: 2,
283+ version_minor: 3,
349284 id: SENSORS_HARDWARE_MODULE_ID,
350285 name: "Kbd Orientation Sensor",
351286 author: "Chih-Wei Huang",
Afficher sur ancien navigateur de dépôt.