hardware/libsensors
修订版 | 528fb5aeaf291255d7ade364d3832d82c9d6f6bf (tree) |
---|---|
时间 | 2011-04-20 17:37:39 |
作者 | Chih-Wei Huang <cwhuang@linu...> |
Commiter | Chih-Wei Huang |
update to use new sensor interface of gingerbread
@@ -31,8 +31,6 @@ template <typename T> struct SensorFd : T { | ||
31 | 31 | |
32 | 32 | SensorFd(const struct hw_module_t *module, struct hw_device_t **device); |
33 | 33 | ~SensorFd(); |
34 | - | |
35 | - static int common_close(struct hw_device_t *dev); | |
36 | 34 | }; |
37 | 35 | |
38 | 36 | 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 | ||
40 | 38 | this->common.tag = HARDWARE_DEVICE_TAG; |
41 | 39 | this->common.version = 0; |
42 | 40 | this->common.module = const_cast<struct hw_module_t *>(module); |
43 | - this->common.close = common_close; | |
44 | 41 | *device = &this->common; |
45 | 42 | LOGD("%s: module=%p dev=%p", __FUNCTION__, module, *device); |
46 | 43 | } |
@@ -50,42 +47,39 @@ template <typename T> SensorFd<T>::~SensorFd() | ||
50 | 47 | close(ufd); |
51 | 48 | } |
52 | 49 | |
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> { | |
64 | 51 | 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(); | |
66 | 54 | |
67 | 55 | 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]; | |
72 | 72 | }; |
73 | 73 | |
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) | |
76 | 76 | { |
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; | |
82 | 81 | |
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; | |
89 | 83 | const char *dirname = "/dev/input"; |
90 | 84 | if (DIR *dir = opendir(dirname)) { |
91 | 85 | while (struct dirent *de = readdir(dir)) { |
@@ -113,83 +107,27 @@ native_handle_t* SensorControl::control_open_data_source(struct sensors_control_ | ||
113 | 107 | } |
114 | 108 | closedir(dir); |
115 | 109 | } |
116 | - handle->data[0] = fd; | |
117 | 110 | |
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)); | |
136 | 125 | } |
137 | 126 | |
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 | - | |
191 | 127 | pfd.events = POLLIN; |
128 | + orients[ROT_0].version = sizeof(sensors_event_t); | |
192 | 129 | orients[ROT_0].sensor = ID_ACCELERATION; |
130 | + orients[ROT_0].type = SENSOR_TYPE_ACCELEROMETER; | |
193 | 131 | orients[ROT_0].acceleration.status = SENSOR_STATUS_ACCURACY_HIGH; |
194 | 132 | orients[ROT_270] = orients[ROT_180] = orients[ROT_90] = orients[ROT_0]; |
195 | 133 | const double angle = 20.0; |
@@ -207,39 +145,39 @@ SensorData::SensorData(const struct hw_module_t *module, struct hw_device_t **de | ||
207 | 145 | orients[ROT_270].acceleration.x = -cos_angle; |
208 | 146 | orients[ROT_270].acceleration.y = 0.0; |
209 | 147 | orients[ROT_270].acceleration.z = -sin_angle; |
148 | + | |
149 | + LOGD("%s: dev=%p ufd=%d fd=%d", __FUNCTION__, this, ufd, fd); | |
210 | 150 | } |
211 | 151 | |
212 | -int SensorData::data_data_open(struct sensors_data_device_t *dev, native_handle_t *handle) | |
152 | +SensorPollContext::~SensorPollContext() | |
213 | 153 | { |
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); | |
222 | 155 | } |
223 | 156 | |
224 | -int SensorData::data_data_close(struct sensors_data_device_t *dev) | |
157 | +int SensorPollContext::poll_close(struct hw_device_t *dev) | |
225 | 158 | { |
226 | 159 | 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); | |
234 | 161 | return 0; |
235 | 162 | } |
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) | |
238 | 164 | { |
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); | |
241 | 179 | |
242 | - struct pollfd &pfd = data->pfd; | |
180 | + struct pollfd &pfd = ctx->pfd; | |
243 | 181 | while (int pollres = ::poll(&pfd, 1, -1)) { |
244 | 182 | if (pollres < 0) { |
245 | 183 | 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 | ||
265 | 203 | case KEY_LEFTALT: |
266 | 204 | if (iev.value) |
267 | 205 | continue; |
268 | - rot = data->rotation; | |
206 | + rot = ctx->rotation; | |
269 | 207 | break; |
270 | 208 | case KEY_UP: |
271 | 209 | rot = ROT_0; |
@@ -289,32 +227,28 @@ int SensorData::data_poll(struct sensors_data_device_t *dev, sensors_data_t *val | ||
289 | 227 | #endif |
290 | 228 | } |
291 | 229 | 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; | |
295 | 233 | } |
296 | - break; | |
234 | + if (ctx->enabled && count > 0) | |
235 | + break; | |
297 | 236 | } |
298 | 237 | } |
299 | 238 | |
300 | - if (data->ufd >= 0) | |
301 | - write(data->ufd, &iev, sizeof(iev)); | |
239 | + if (ctx->ufd >= 0) | |
240 | + write(ctx->ufd, &iev, sizeof(iev)); | |
302 | 241 | } |
303 | 242 | |
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; | |
307 | 246 | } |
308 | 247 | |
309 | 248 | static int open_kbd_sensor(const struct hw_module_t *module, const char *id, struct hw_device_t **device) |
310 | 249 | { |
311 | 250 | 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; | |
318 | 252 | } |
319 | 253 | |
320 | 254 | static struct sensor_t sSensorListInit[] = { |
@@ -327,6 +261,7 @@ static struct sensor_t sSensorListInit[] = { | ||
327 | 261 | maxRange: 2.8f, |
328 | 262 | resolution: 1.0f/4032.0f, |
329 | 263 | power: 3.0f, |
264 | + minDelay: 0, | |
330 | 265 | reserved: { } |
331 | 266 | } |
332 | 267 | }; |
@@ -345,7 +280,7 @@ struct sensors_module_t HAL_MODULE_INFO_SYM = { | ||
345 | 280 | common: { |
346 | 281 | tag: HARDWARE_MODULE_TAG, |
347 | 282 | version_major: 2, |
348 | - version_minor: 2, | |
283 | + version_minor: 3, | |
349 | 284 | id: SENSORS_HARDWARE_MODULE_ID, |
350 | 285 | name: "Kbd Orientation Sensor", |
351 | 286 | author: "Chih-Wei Huang", |