source: mainline/uspace/app/virtusbkbd/virtusbkbd.c@ bc9a629

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bc9a629 was bc9a629, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

Start work on virtual USB keyboard

The virtual HC(D) is now able to serve as HCD towards central USB driver
as well as virtual HC towards virtual devices.

Each device is a separate task that communicates with VHCD through simple
protocol.

Will clean and comment the code in next revisions. Promise.

  • Property mode set to 100644
File size: 3.0 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/hcd.h>
48#include <usb/virtdev.h>
49
50#define LOOPS 5
51#define NAME "virt-usb-kbd"
52
53#define DEV_HCD_NAME "hcd-virt"
54
55#define __QUOTEME(x) #x
56#define _QUOTEME(x) __QUOTEME(x)
57
58#define VERBOSE_EXEC(cmd, fmt, ...) \
59 (printf("%s: %s" fmt "\n", NAME, _QUOTEME(cmd), __VA_ARGS__), cmd(__VA_ARGS__))
60
61static void fibril_sleep(size_t sec)
62{
63 while (sec-- > 0) {
64 async_usleep(1000*1000);
65 }
66}
67
68static void on_data_from_host(usb_endpoint_t endpoint, void *buffer, size_t len)
69{
70 printf("%s: ignoring incomming data to endpoint %d\n", NAME, endpoint);
71}
72
73int main(int argc, char * argv[])
74{
75 int vhcd_phone = usb_virtdev_connect(DEV_HCD_NAME,
76 USB_VIRTDEV_KEYBOARD_ID, on_data_from_host);
77
78 if (vhcd_phone < 0) {
79 printf("%s: Unable to start comunication with VHCD at usb://%s (%s).\n",
80 NAME, DEV_HCD_NAME, str_error(vhcd_phone));
81 return vhcd_phone;
82 }
83
84
85
86 size_t i;
87 for (i = 0; i < LOOPS; i++) {
88 size_t size = 5;
89 char *data = (char *) "Hullo, World!";
90
91 if (i > 0) {
92 fibril_sleep(2);
93 }
94
95 printf("%s: Will send data to VHCD...\n", NAME);
96 int rc = usb_virtdev_data_to_host(vhcd_phone, 0, data, size);
97 printf("%s: ...data sent (%s).\n", NAME, str_error(rc));
98 }
99
100 fibril_sleep(1);
101 printf("%s: Terminating...\n", NAME);
102
103 ipc_hangup(vhcd_phone);
104
105 return 0;
106}
107
108
109/** @}
110 */
Note: See TracBrowser for help on using the repository browser.