source: mainline/uspace/app/virtusbkbd/virtusbkbd.c@ 0a9ea4a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0a9ea4a was 1f383dde, checked in by Lubos Slovak <lubos.slovak@…>, 15 years ago

Debugging

  • disabled some output from other parts (virtual hcd)
  • added some debug output to descparser
  • Property mode set to 100644
File size: 7.1 KB
Line 
1/*
2 * Copyright (c) 2010 Vojtech Horky
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 usb
30 * @{
31 */
32/**
33 * @file
34 * @brief Virtual USB keyboard.
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <vfs/vfs.h>
41#include <fcntl.h>
42#include <errno.h>
43#include <str_error.h>
44#include <bool.h>
45#include <async.h>
46
47#include <usb/usb.h>
48#include <usb/descriptor.h>
49#include <usb/classes/hid.h>
50#include <usbvirt/device.h>
51#include <usbvirt/hub.h>
52
53#include "kbdconfig.h"
54#include "keys.h"
55#include "stdreq.h"
56
57/** Pause between individual key-presses in seconds. */
58#define KEY_PRESS_DELAY 2
59#define NAME "virt-usb-kbd"
60
61
62#define __QUOTEME(x) #x
63#define _QUOTEME(x) __QUOTEME(x)
64
65#define VERBOSE_EXEC(cmd, fmt, ...) \
66 (printf("%s: %s" fmt "\n", NAME, _QUOTEME(cmd), __VA_ARGS__), cmd(__VA_ARGS__))
67
68kb_status_t status;
69
70static int on_incoming_data(struct usbvirt_device *dev,
71 usb_endpoint_t endpoint, void *buffer, size_t size)
72{
73 printf("%s: ignoring incomming data to endpoint %d\n", NAME, endpoint);
74
75 return EOK;
76}
77
78
79/** Compares current and last status of pressed keys.
80 *
81 * @warning Has side-efect - changes status_last field.
82 *
83 * @param status_now Status now.
84 * @param status_last Last status.
85 * @param len Size of status.
86 * @return Whether they are the same.
87 */
88static bool keypress_check_with_last_request(uint8_t *status_now,
89 uint8_t *status_last, size_t len)
90{
91 bool same = true;
92 size_t i;
93 for (i = 0; i < len; i++) {
94 if (status_now[i] != status_last[i]) {
95 status_last[i] = status_now[i];
96 same = false;
97 }
98 }
99 return same;
100}
101
102static int on_request_for_data(struct usbvirt_device *dev,
103 usb_endpoint_t endpoint, void *buffer, size_t size, size_t *actual_size)
104{
105 static uint8_t last_data[2 + KB_MAX_KEYS_AT_ONCE];
106
107 if (size < 2 + KB_MAX_KEYS_AT_ONCE) {
108 return EINVAL;
109 }
110
111 *actual_size = 2 + KB_MAX_KEYS_AT_ONCE;
112
113 uint8_t data[2 + KB_MAX_KEYS_AT_ONCE];
114 data[0] = status.modifiers;
115 data[1] = 0;
116
117 size_t i;
118 for (i = 0; i < KB_MAX_KEYS_AT_ONCE; i++) {
119 data[2 + i] = status.pressed_keys[i];
120 }
121
122 if (keypress_check_with_last_request(data, last_data,
123 2 + KB_MAX_KEYS_AT_ONCE)) {
124 *actual_size = 0;
125 return EOK;
126 }
127
128 memcpy(buffer, &data, *actual_size);
129
130 return EOK;
131}
132
133static usbvirt_control_transfer_handler_t endpoint_zero_handlers[] = {
134 {
135 .request_type = USBVIRT_MAKE_CONTROL_REQUEST_TYPE(
136 USB_DIRECTION_IN,
137 USBVIRT_REQUEST_TYPE_STANDARD,
138 USBVIRT_REQUEST_RECIPIENT_DEVICE),
139 .request = USB_DEVREQ_GET_DESCRIPTOR,
140 .name = "GetDescriptor",
141 .callback = stdreq_on_get_descriptor
142 },
143 {
144 .request_type = USBVIRT_MAKE_CONTROL_REQUEST_TYPE(
145 USB_DIRECTION_IN,
146 USBVIRT_REQUEST_TYPE_CLASS,
147 USBVIRT_REQUEST_RECIPIENT_DEVICE),
148 .request = USB_DEVREQ_GET_DESCRIPTOR,
149 .name = "GetDescriptor",
150 .callback = stdreq_on_get_descriptor
151 },
152 USBVIRT_CONTROL_TRANSFER_HANDLER_LAST
153};
154
155/** Keyboard callbacks.
156 * We abuse the fact that static variables are zero-filled.
157 */
158static usbvirt_device_ops_t keyboard_ops = {
159 .control_transfer_handlers = endpoint_zero_handlers,
160 .on_data = on_incoming_data,
161 .on_data_request = on_request_for_data
162};
163
164usbvirt_device_configuration_extras_t extra_descriptors[] = {
165 {
166 .data = (uint8_t *) &std_interface_descriptor,
167 .length = sizeof(std_interface_descriptor)
168 },
169 {
170 .data = (uint8_t *) &hid_descriptor,
171 .length = sizeof(hid_descriptor)
172 },
173 {
174 .data = (uint8_t *) &endpoint_descriptor,
175 .length = sizeof(endpoint_descriptor)
176 }
177};
178
179/** Keyboard configuration. */
180usbvirt_device_configuration_t configuration = {
181 .descriptor = &std_configuration_descriptor,
182 .extra = extra_descriptors,
183 .extra_count = sizeof(extra_descriptors)/sizeof(extra_descriptors[0])
184};
185
186/** Keyboard standard descriptors. */
187usbvirt_descriptors_t descriptors = {
188 .device = &std_device_descriptor,
189 .configuration = &configuration,
190 .configuration_count = 1,
191};
192
193/** Keyboard device.
194 * Rest of the items will be initialized later.
195 */
196static usbvirt_device_t keyboard_dev = {
197 .ops = &keyboard_ops,
198 .descriptors = &descriptors,
199 .lib_debug_level = 3,
200 .lib_debug_enabled_tags = USBVIRT_DEBUGTAG_ALL,
201 .name = "keyboard"
202};
203
204
205static void fibril_sleep(size_t sec)
206{
207 while (sec-- > 0) {
208 async_usleep(1000*1000);
209 }
210}
211
212
213/** Callback when keyboard status changed.
214 *
215 * @param status Current keyboard status.
216 */
217static void on_keyboard_change(kb_status_t *status)
218{
219 printf("%s: Current keyboard status: %02hhx", NAME, status->modifiers);
220 size_t i;
221 for (i = 0; i < KB_MAX_KEYS_AT_ONCE; i++) {
222 printf(" 0x%02X", (int)status->pressed_keys[i]);
223 }
224 printf("\n");
225
226 fibril_sleep(KEY_PRESS_DELAY);
227}
228
229/** Simulated keyboard events. */
230static kb_event_t keyboard_events[] = {
231 /* Switch to VT6 (Alt+F6) */
232 M_DOWN(KB_MOD_LEFT_ALT),
233 K_PRESS(KB_KEY_F6),
234 M_UP(KB_MOD_LEFT_ALT),
235 /* Type the word 'Hello' */
236 M_DOWN(KB_MOD_LEFT_SHIFT),
237 K_PRESS(KB_KEY_H),
238 M_UP(KB_MOD_LEFT_SHIFT),
239 K_PRESS(KB_KEY_E),
240 K_PRESS(KB_KEY_L),
241 K_PRESS(KB_KEY_L),
242 K_PRESS(KB_KEY_O)
243};
244static size_t keyboard_events_count =
245 sizeof(keyboard_events)/sizeof(keyboard_events[0]);
246
247
248
249int main(int argc, char * argv[])
250{
251 printf("Dump of report descriptor (%zu bytes):\n", report_descriptor_size);
252 size_t i;
253 for (i = 0; i < report_descriptor_size; i++) {
254 printf(" 0x%02X", report_descriptor[i]);
255 if (((i > 0) && (((i+1) % 10) == 0))
256 || (i + 1 == report_descriptor_size)) {
257 printf("\n");
258 }
259 }
260
261 kb_init(&status);
262
263
264 int rc = usbvirt_connect(&keyboard_dev);
265 if (rc != EOK) {
266 printf("%s: Unable to start communication with VHCD (%s).\n",
267 NAME, str_error(rc));
268 return rc;
269 }
270
271 printf("%s: Simulating keyboard events...\n", NAME);
272 fibril_sleep(10);
273 //while (1) {
274 kb_process_events(&status, keyboard_events, keyboard_events_count,
275 on_keyboard_change);
276 //}
277
278 printf("%s: Terminating...\n", NAME);
279
280 usbvirt_disconnect(&keyboard_dev);
281
282 return 0;
283}
284
285
286/** @}
287 */
Note: See TracBrowser for help on using the repository browser.