| [bc9a629] | 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>
|
|---|
| [b8100da] | 48 | #include <usbvirt/device.h>
|
|---|
| 49 | #include <usbvirt/hub.h>
|
|---|
| 50 | #include <usbvirt/ids.h>
|
|---|
| [bc9a629] | 51 |
|
|---|
| 52 | #define LOOPS 5
|
|---|
| 53 | #define NAME "virt-usb-kbd"
|
|---|
| 54 |
|
|---|
| 55 | #define DEV_HCD_NAME "hcd-virt"
|
|---|
| 56 |
|
|---|
| 57 | #define __QUOTEME(x) #x
|
|---|
| 58 | #define _QUOTEME(x) __QUOTEME(x)
|
|---|
| 59 |
|
|---|
| 60 | #define VERBOSE_EXEC(cmd, fmt, ...) \
|
|---|
| 61 | (printf("%s: %s" fmt "\n", NAME, _QUOTEME(cmd), __VA_ARGS__), cmd(__VA_ARGS__))
|
|---|
| 62 |
|
|---|
| [b8100da] | 63 | static int on_incoming_data(struct usbvirt_device *dev,
|
|---|
| 64 | usb_endpoint_t endpoint, void *buffer, size_t size)
|
|---|
| 65 | {
|
|---|
| 66 | printf("%s: ignoring incomming data to endpoint %d\n", NAME, endpoint);
|
|---|
| 67 |
|
|---|
| 68 | return EOK;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | /** Keyboard callbacks.
|
|---|
| 72 | * We abuse the fact that static variables are zero-filled.
|
|---|
| 73 | */
|
|---|
| 74 | static usbvirt_device_ops_t keyboard_ops = {
|
|---|
| 75 | .on_data = on_incoming_data
|
|---|
| 76 | };
|
|---|
| 77 |
|
|---|
| 78 | /** Keyboard device.
|
|---|
| 79 | * Rest of the items will be initialized later.
|
|---|
| 80 | */
|
|---|
| 81 | static usbvirt_device_t keyboard_dev = {
|
|---|
| 82 | .ops = &keyboard_ops,
|
|---|
| 83 | .device_id_ = USBVIRT_DEV_KEYBOARD_ID
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| [bc9a629] | 87 | static void fibril_sleep(size_t sec)
|
|---|
| 88 | {
|
|---|
| 89 | while (sec-- > 0) {
|
|---|
| 90 | async_usleep(1000*1000);
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | int main(int argc, char * argv[])
|
|---|
| 96 | {
|
|---|
| [b8100da] | 97 | int rc = usbvirt_connect(&keyboard_dev, DEV_HCD_NAME);
|
|---|
| 98 | if (rc != EOK) {
|
|---|
| [bc9a629] | 99 | printf("%s: Unable to start comunication with VHCD at usb://%s (%s).\n",
|
|---|
| [b8100da] | 100 | NAME, DEV_HCD_NAME, str_error(rc));
|
|---|
| 101 | return rc;
|
|---|
| [bc9a629] | 102 | }
|
|---|
| 103 |
|
|---|
| 104 | size_t i;
|
|---|
| 105 | for (i = 0; i < LOOPS; i++) {
|
|---|
| 106 | size_t size = 5;
|
|---|
| 107 | char *data = (char *) "Hullo, World!";
|
|---|
| 108 |
|
|---|
| 109 | if (i > 0) {
|
|---|
| 110 | fibril_sleep(2);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | printf("%s: Will send data to VHCD...\n", NAME);
|
|---|
| [b8100da] | 114 | int rc = keyboard_dev.send_data(&keyboard_dev, 0, data, size);
|
|---|
| [bc9a629] | 115 | printf("%s: ...data sent (%s).\n", NAME, str_error(rc));
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | fibril_sleep(1);
|
|---|
| 119 | printf("%s: Terminating...\n", NAME);
|
|---|
| 120 |
|
|---|
| [b8100da] | 121 | usbvirt_disconnect();
|
|---|
| [bc9a629] | 122 |
|
|---|
| 123 | return 0;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 | /** @}
|
|---|
| 128 | */
|
|---|