1 | /*
|
---|
2 | * Copyright (c) 2011 Lubos Slovak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup mkbd
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * Sample application using the data from multimedia keys on keyboard
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <inttypes.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <str_error.h>
|
---|
42 | #include <bool.h>
|
---|
43 | #include <getopt.h>
|
---|
44 | #include <devman.h>
|
---|
45 | #include <devmap.h>
|
---|
46 | #include <usb/dev/hub.h>
|
---|
47 | //#include <usb/host.h>
|
---|
48 | //#include <usb/driver.h>
|
---|
49 | #include <usb/hid/iface.h>
|
---|
50 | #include <usb/dev/pipes.h>
|
---|
51 | #include <async.h>
|
---|
52 | #include <usb/hid/usages/core.h>
|
---|
53 | #include <usb/hid/hidparser.h>
|
---|
54 | #include <usb/hid/hiddescriptor.h>
|
---|
55 | #include <usb/hid/usages/consumer.h>
|
---|
56 | #include <assert.h>
|
---|
57 |
|
---|
58 | #define NAME "mkbd"
|
---|
59 |
|
---|
60 | static int dev_phone = -1;
|
---|
61 |
|
---|
62 | static int initialize_report_parser(int dev_phone, usb_hid_report_t **report)
|
---|
63 | {
|
---|
64 | *report = (usb_hid_report_t *)malloc(sizeof(usb_hid_report_t));
|
---|
65 | if (*report == NULL) {
|
---|
66 | return ENOMEM;
|
---|
67 | }
|
---|
68 |
|
---|
69 | int rc = usb_hid_report_init(*report);
|
---|
70 | if (rc != EOK) {
|
---|
71 | usb_hid_free_report(*report);
|
---|
72 | *report = NULL;
|
---|
73 | printf("usb_hid_report_init() failed.\n");
|
---|
74 | return rc;
|
---|
75 | }
|
---|
76 |
|
---|
77 | // get the report descriptor length from the device
|
---|
78 | size_t report_desc_size;
|
---|
79 | rc = usbhid_dev_get_report_descriptor_length(
|
---|
80 | dev_phone, &report_desc_size);
|
---|
81 | if (rc != EOK) {
|
---|
82 | usb_hid_free_report(*report);
|
---|
83 | *report = NULL;
|
---|
84 | printf("usbhid_dev_get_report_descriptor_length() failed.\n");
|
---|
85 | return rc;
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (report_desc_size == 0) {
|
---|
89 | usb_hid_free_report(*report);
|
---|
90 | *report = NULL;
|
---|
91 | printf("usbhid_dev_get_report_descriptor_length() returned 0.\n");
|
---|
92 | return EINVAL; // TODO: other error code?
|
---|
93 | }
|
---|
94 |
|
---|
95 | uint8_t *desc = (uint8_t *)malloc(report_desc_size);
|
---|
96 | if (desc == NULL) {
|
---|
97 | usb_hid_free_report(*report);
|
---|
98 | *report = NULL;
|
---|
99 | return ENOMEM;
|
---|
100 | }
|
---|
101 |
|
---|
102 | // get the report descriptor from the device
|
---|
103 | size_t actual_size;
|
---|
104 | rc = usbhid_dev_get_report_descriptor(dev_phone, desc, report_desc_size,
|
---|
105 | &actual_size);
|
---|
106 | if (rc != EOK) {
|
---|
107 | usb_hid_free_report(*report);
|
---|
108 | *report = NULL;
|
---|
109 | free(desc);
|
---|
110 | printf("usbhid_dev_get_report_descriptor() failed.\n");
|
---|
111 | return rc;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (actual_size != report_desc_size) {
|
---|
115 | usb_hid_free_report(*report);
|
---|
116 | *report = NULL;
|
---|
117 | free(desc);
|
---|
118 | printf("usbhid_dev_get_report_descriptor() returned wrong size:"
|
---|
119 | " %zu, expected: %zu.\n", actual_size, report_desc_size);
|
---|
120 | return EINVAL; // TODO: other error code?
|
---|
121 | }
|
---|
122 |
|
---|
123 | // initialize the report parser
|
---|
124 |
|
---|
125 | rc = usb_hid_parse_report_descriptor(*report, desc, report_desc_size);
|
---|
126 | free(desc);
|
---|
127 |
|
---|
128 | if (rc != EOK) {
|
---|
129 | free(desc);
|
---|
130 | printf("usb_hid_parse_report_descriptor() failed.\n");
|
---|
131 | return rc;
|
---|
132 | }
|
---|
133 |
|
---|
134 | return EOK;
|
---|
135 | }
|
---|
136 |
|
---|
137 | static void print_key(uint8_t *buffer, size_t size, usb_hid_report_t *report)
|
---|
138 | {
|
---|
139 | assert(buffer != NULL);
|
---|
140 | assert(report != NULL);
|
---|
141 |
|
---|
142 | // printf("Calling usb_hid_parse_report() with size %zu and "
|
---|
143 | // "buffer: \n", size);
|
---|
144 | // for (size_t i = 0; i < size; ++i) {
|
---|
145 | // printf(" %X ", buffer[i]);
|
---|
146 | // }
|
---|
147 | // printf("\n");
|
---|
148 |
|
---|
149 | uint8_t report_id;
|
---|
150 | int rc = usb_hid_parse_report(report, buffer, size, &report_id);
|
---|
151 | if (rc != EOK) {
|
---|
152 | // printf("Error parsing report: %s\n", str_error(rc));
|
---|
153 | return;
|
---|
154 | }
|
---|
155 |
|
---|
156 | usb_hid_report_path_t *path = usb_hid_report_path();
|
---|
157 | if (path == NULL) {
|
---|
158 | return;
|
---|
159 | }
|
---|
160 |
|
---|
161 | usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_CONSUMER, 0);
|
---|
162 |
|
---|
163 | usb_hid_report_path_set_report_id(path, report_id);
|
---|
164 |
|
---|
165 | usb_hid_report_field_t *field = usb_hid_report_get_sibling(
|
---|
166 | report, NULL, path, USB_HID_PATH_COMPARE_END
|
---|
167 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
---|
168 | USB_HID_REPORT_TYPE_INPUT);
|
---|
169 |
|
---|
170 | // printf("Field: %p\n", field);
|
---|
171 |
|
---|
172 | while (field != NULL) {
|
---|
173 | // printf("Field usage: %u, field value: %d\n", field->usage,
|
---|
174 | // field->value);
|
---|
175 | if (field->value != 0) {
|
---|
176 | const char *key_str =
|
---|
177 | usbhid_multimedia_usage_to_str(field->usage);
|
---|
178 | printf("Pressed key: %s\n", key_str);
|
---|
179 | }
|
---|
180 |
|
---|
181 | field = usb_hid_report_get_sibling(
|
---|
182 | report, field, path, USB_HID_PATH_COMPARE_END
|
---|
183 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
---|
184 | USB_HID_REPORT_TYPE_INPUT);
|
---|
185 | // printf("Next field: %p\n", field);
|
---|
186 | }
|
---|
187 |
|
---|
188 | usb_hid_report_path_free(path);
|
---|
189 | }
|
---|
190 |
|
---|
191 | #define MAX_PATH_LENGTH 1024
|
---|
192 |
|
---|
193 | static void print_usage(char *app_name)
|
---|
194 | {
|
---|
195 | #define _INDENT " "
|
---|
196 |
|
---|
197 | printf(NAME ": Print out what multimedia keys were pressed.\n\n");
|
---|
198 | printf("Usage: %s device\n", app_name);
|
---|
199 | printf(_INDENT "The device is a devman path to the device.\n");
|
---|
200 |
|
---|
201 | #undef _OPTION
|
---|
202 | #undef _INDENT
|
---|
203 | }
|
---|
204 |
|
---|
205 | int main(int argc, char *argv[])
|
---|
206 | {
|
---|
207 | int act_event = -1;
|
---|
208 |
|
---|
209 | if (argc <= 1) {
|
---|
210 | print_usage(argv[0]);
|
---|
211 | return -1;
|
---|
212 | }
|
---|
213 |
|
---|
214 | //char *devpath = argv[1];
|
---|
215 | const char *devpath = "/hw/pci0/00:06.0/ohci-rh/usb00_a2/HID1/hid";
|
---|
216 |
|
---|
217 | int rc;
|
---|
218 |
|
---|
219 | devman_handle_t dev_handle = 0;
|
---|
220 | rc = devman_device_get_handle(devpath, &dev_handle, 0);
|
---|
221 | if (rc != EOK) {
|
---|
222 | printf("Failed to get handle from devman: %s.\n",
|
---|
223 | str_error(rc));
|
---|
224 | return rc;
|
---|
225 | }
|
---|
226 |
|
---|
227 | rc = devman_device_connect(dev_handle, 0);
|
---|
228 | if (rc < 0) {
|
---|
229 | printf(NAME ": failed to connect to the device (handle %"
|
---|
230 | PRIun "): %s.\n", dev_handle, str_error(rc));
|
---|
231 | return rc;
|
---|
232 | }
|
---|
233 |
|
---|
234 | dev_phone = rc;
|
---|
235 | // printf("Got phone to the device: %d\n", dev_phone);
|
---|
236 |
|
---|
237 | char path[MAX_PATH_LENGTH];
|
---|
238 | rc = devman_get_device_path(dev_handle, path, MAX_PATH_LENGTH);
|
---|
239 | if (rc != EOK) {
|
---|
240 | return ENOMEM;
|
---|
241 | }
|
---|
242 |
|
---|
243 | printf("Device path: %s\n", path);
|
---|
244 |
|
---|
245 |
|
---|
246 | usb_hid_report_t *report = NULL;
|
---|
247 | rc = initialize_report_parser(dev_phone, &report);
|
---|
248 | if (rc != EOK) {
|
---|
249 | printf("Failed to initialize report parser: %s\n",
|
---|
250 | str_error(rc));
|
---|
251 | return rc;
|
---|
252 | }
|
---|
253 |
|
---|
254 | assert(report != NULL);
|
---|
255 |
|
---|
256 | size_t size;
|
---|
257 | rc = usbhid_dev_get_event_length(dev_phone, &size);
|
---|
258 | if (rc != EOK) {
|
---|
259 | printf("Failed to get event length: %s.\n", str_error(rc));
|
---|
260 | return rc;
|
---|
261 | }
|
---|
262 |
|
---|
263 | // printf("Event length: %zu\n", size);
|
---|
264 | uint8_t *event = (uint8_t *)malloc(size);
|
---|
265 | if (event == NULL) {
|
---|
266 | // hangup phone?
|
---|
267 | return ENOMEM;
|
---|
268 | }
|
---|
269 |
|
---|
270 | // printf("Event length: %zu\n", size);
|
---|
271 |
|
---|
272 | size_t actual_size;
|
---|
273 | int event_nr;
|
---|
274 |
|
---|
275 | while (1) {
|
---|
276 | // get event from the driver
|
---|
277 | // printf("Getting event from the driver.\n");
|
---|
278 |
|
---|
279 | /** @todo Try blocking call. */
|
---|
280 | rc = usbhid_dev_get_event(dev_phone, event, size, &actual_size,
|
---|
281 | &event_nr, 0);
|
---|
282 | if (rc != EOK) {
|
---|
283 | // hangup phone?
|
---|
284 | printf("Error in getting event from the HID driver:"
|
---|
285 | "%s.\n", str_error(rc));
|
---|
286 | break;
|
---|
287 | }
|
---|
288 |
|
---|
289 | // printf("Got buffer: %p, size: %zu, max size: %zu\n", event,
|
---|
290 | // actual_size, size);
|
---|
291 |
|
---|
292 | // printf("Event number: %d, my actual event: %d\n", event_nr,
|
---|
293 | // act_event);
|
---|
294 | if (event_nr > act_event) {
|
---|
295 | print_key(event, size, report);
|
---|
296 | act_event = event_nr;
|
---|
297 | }
|
---|
298 |
|
---|
299 | async_usleep(10000);
|
---|
300 | }
|
---|
301 |
|
---|
302 | return 0;
|
---|
303 | }
|
---|
304 |
|
---|
305 |
|
---|
306 | /** @}
|
---|
307 | */
|
---|