1 | /*
|
---|
2 | * Copyright (c) 2011 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 usbvirthub
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief Virtual USB hub.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <str_error.h>
|
---|
41 | #include <bool.h>
|
---|
42 |
|
---|
43 | #include <usb/usb.h>
|
---|
44 | #include <usb/descriptor.h>
|
---|
45 | #include <usb/classes/hub.h>
|
---|
46 | #include <usbvirt/device.h>
|
---|
47 | #include <usbvirt/hub.h>
|
---|
48 |
|
---|
49 | #include "vhc_hub/virthub.h"
|
---|
50 |
|
---|
51 | #define NAME "vuh"
|
---|
52 |
|
---|
53 | static usbvirt_device_t hub_device;
|
---|
54 |
|
---|
55 | #define VERBOSE_SLEEP(sec, msg, ...) \
|
---|
56 | do { \
|
---|
57 | char _status[HUB_PORT_COUNT + 2]; \
|
---|
58 | printf(NAME ": doing nothing for %zu seconds...\n", \
|
---|
59 | (size_t) (sec)); \
|
---|
60 | fibril_sleep((sec)); \
|
---|
61 | virthub_get_status(&hub_device, _status, HUB_PORT_COUNT + 1); \
|
---|
62 | printf(NAME ": " msg " [%s]\n" #__VA_ARGS__, _status); \
|
---|
63 | } while (0)
|
---|
64 |
|
---|
65 | static void fibril_sleep(size_t sec)
|
---|
66 | {
|
---|
67 | while (sec-- > 0) {
|
---|
68 | async_usleep(1000*1000);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | static int dev1 = 1;
|
---|
73 |
|
---|
74 | int main(int argc, char * argv[])
|
---|
75 | {
|
---|
76 | int rc;
|
---|
77 |
|
---|
78 | printf(NAME ": virtual USB hub.\n");
|
---|
79 |
|
---|
80 | rc = virthub_init(&hub_device);
|
---|
81 | if (rc != EOK) {
|
---|
82 | printf(NAME ": Unable to start communication with VHCD (%s).\n",
|
---|
83 | str_error(rc));
|
---|
84 | return rc;
|
---|
85 | }
|
---|
86 |
|
---|
87 | while (true) {
|
---|
88 | VERBOSE_SLEEP(8, "will pretend device plug-in...");
|
---|
89 | virthub_connect_device(&hub_device, &dev1);
|
---|
90 |
|
---|
91 | VERBOSE_SLEEP(8, "will pretend device un-plug...");
|
---|
92 | virthub_disconnect_device(&hub_device, &dev1);
|
---|
93 | }
|
---|
94 |
|
---|
95 | usbvirt_disconnect(&hub_device);
|
---|
96 |
|
---|
97 | return 0;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | /** @}
|
---|
102 | */
|
---|