2 * Copyright (c) 2017 Petr Manek
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
34 * USB transfer debugging.
46 /** Command which is executed by tmon. */
47 typedef struct tmon_cmd
{
48 /** Unique name, by which the command is executed. */
50 /** Description of the command, which is displayed in the usage string. */
51 const char *description
;
52 /** Function, which executes the command. Same as int main(int, char**). */
53 int (*action
)(int, char **);
56 /** Static array of commands supported by tmon. */
57 static tmon_cmd_t commands
[] = {
60 .description
= "Print a list of connected diagnostic devices.",
64 .name
= "test-intr-in",
65 .description
= "Read from interrupt endpoint as fast as possible.",
66 .action
= tmon_test_intr_in
,
69 .name
= "test-intr-out",
70 .description
= "Write to interrupt endpoint as fast as possible.",
71 .action
= tmon_test_intr_out
,
74 .name
= "test-bulk-in",
75 .description
= "Read from bulk endpoint as fast as possible.",
76 .action
= tmon_test_bulk_in
,
79 .name
= "test-bulk-out",
80 .description
= "Write to bulk endpoint as fast as possible.",
81 .action
= tmon_test_bulk_out
,
84 .name
= "test-isoch-in",
85 .description
= "Read from isochronous endpoint as fast as possible.",
86 .action
= tmon_test_isoch_in
,
89 .name
= "test-isoch-out",
90 .description
= "Write to isochronous endpoint as fast as possible.",
91 .action
= tmon_test_isoch_out
,
95 /** Option shown in the usage string. */
96 typedef struct tmon_opt
{
97 /** Long name of the option without "--" prefix. */
98 const char *long_name
;
99 /** Short name of the option without "-" prefix. */
101 /** Description of the option displayed in the usage string. */
102 const char *description
;
105 /** Static array of options displayed in the tmon usage string. */
106 static tmon_opt_t options
[] = {
108 .long_name
= "duration",
110 .description
= "Set the minimum test duration (in seconds)."
115 .description
= "Set the data size (in bytes) transferred in a single cycle."
118 .long_name
= "validate",
120 .description
= "Validate the correctness of transferred data (impacts performance)."
124 /** Print usage string.
125 * @param[in] app_name Name to print in the invocation example.
127 static void print_usage(char *app_name
)
129 puts(NAME
": benchmark USB diagnostic device\n");
130 printf("Usage: %s command [device] [options]\n\n", app_name
);
132 for (unsigned i
= 0; i
< ARRAY_SIZE(commands
); ++i
) {
133 printf(INDENT
"%s - %s\n", commands
[i
].name
, commands
[i
].description
);
137 for (unsigned i
= 0; i
< ARRAY_SIZE(options
); ++i
) {
138 printf(INDENT
"-%c --%s\n" INDENT INDENT
"%s\n", options
[i
].short_name
, options
[i
].long_name
, options
[i
].description
);
141 puts("\nIf no device is specified, the first device is used provided that it is the only one connected. Otherwise, the command fails.\n");
144 /** Main tmon entry point.
145 * @param[in] argc Number of arguments.
146 * @param[in] argv Argument values. Must point to exactly `argc` strings.
150 int main(int argc
, char *argv
[])
152 // Find a command to execute.
153 tmon_cmd_t
*cmd
= NULL
;
154 for (unsigned i
= 0; argc
> 1 && i
< ARRAY_SIZE(commands
); ++i
) {
155 if (str_cmp(argv
[1], commands
[i
].name
) == 0) {
162 print_usage(argv
[0]);
166 return cmd
->action(argc
- 1, argv
+ 1);