| 1 | /*
|
|---|
| 2 | * Copyright (c) 2017 Petr Manek
|
|---|
| 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 tmon
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * USB burst tests.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <str_error.h>
|
|---|
| 40 | #include <getopt.h>
|
|---|
| 41 | #include <usbdiag_iface.h>
|
|---|
| 42 | #include "commands.h"
|
|---|
| 43 | #include "tf.h"
|
|---|
| 44 |
|
|---|
| 45 | #define NAME "tmon"
|
|---|
| 46 | #define INDENT " "
|
|---|
| 47 |
|
|---|
| 48 | typedef struct tmon_burst_test_params {
|
|---|
| 49 | tmon_test_params_t base; /* inheritance */
|
|---|
| 50 | uint32_t cycles;
|
|---|
| 51 | size_t size;
|
|---|
| 52 | } tmon_burst_test_params_t;
|
|---|
| 53 |
|
|---|
| 54 | static struct option long_options[] = {
|
|---|
| 55 | {"cycles", required_argument, NULL, 'n'},
|
|---|
| 56 | {"size", required_argument, NULL, 's'},
|
|---|
| 57 | {0, 0, NULL, 0}
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | static const char *short_options = "n:s:";
|
|---|
| 61 |
|
|---|
| 62 | static int read_params(int argc, char *argv[], tmon_test_params_t **params)
|
|---|
| 63 | {
|
|---|
| 64 | int rc;
|
|---|
| 65 | tmon_burst_test_params_t *p = (tmon_burst_test_params_t *) malloc(sizeof(tmon_burst_test_params_t));
|
|---|
| 66 | if (!p)
|
|---|
| 67 | return ENOMEM;
|
|---|
| 68 |
|
|---|
| 69 | // Default values.
|
|---|
| 70 | p->cycles = 256;
|
|---|
| 71 | p->size = 1024;
|
|---|
| 72 |
|
|---|
| 73 | // Parse other than default values.
|
|---|
| 74 | int c;
|
|---|
| 75 | for (c = 0, optreset = 1, optind = 0; c != -1;) {
|
|---|
| 76 | c = getopt_long(argc, argv, short_options, long_options, NULL);
|
|---|
| 77 | switch (c) {
|
|---|
| 78 | case -1:
|
|---|
| 79 | break;
|
|---|
| 80 | case 'n':
|
|---|
| 81 | if (!optarg || str_uint32_t(optarg, NULL, 10, false, &p->cycles) != EOK) {
|
|---|
| 82 | puts(NAME ": Invalid number of cycles.\n");
|
|---|
| 83 | rc = EINVAL;
|
|---|
| 84 | goto err_malloc;
|
|---|
| 85 | }
|
|---|
| 86 | break;
|
|---|
| 87 | case 's':
|
|---|
| 88 | if (!optarg || str_size_t(optarg, NULL, 10, false, &p->size) != EOK) {
|
|---|
| 89 | puts(NAME ": Invalid data size.\n");
|
|---|
| 90 | rc = EINVAL;
|
|---|
| 91 | goto err_malloc;
|
|---|
| 92 | }
|
|---|
| 93 | break;
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | *params = (tmon_test_params_t *) p;
|
|---|
| 98 | return EOK;
|
|---|
| 99 |
|
|---|
| 100 | err_malloc:
|
|---|
| 101 | free(p);
|
|---|
| 102 | *params = NULL;
|
|---|
| 103 | return rc;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | static void print_params(const tmon_burst_test_params_t *params)
|
|---|
| 107 | {
|
|---|
| 108 | printf(INDENT "Number of cycles: %d\n", params->cycles);
|
|---|
| 109 | printf(INDENT "Data size: %ld B\n", params->size);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | static void print_results(const tmon_burst_test_params_t *params, usbdiag_dur_t duration)
|
|---|
| 113 | {
|
|---|
| 114 | printf(INDENT "Total duration: %ld ms\n", duration);
|
|---|
| 115 |
|
|---|
| 116 | const double dur_per_cycle = (double) duration / (double) params->cycles;
|
|---|
| 117 | printf(INDENT "Duration per cycle: %0.3f ms\n", dur_per_cycle);
|
|---|
| 118 |
|
|---|
| 119 | const double speed = (double) params->size / (double) duration;
|
|---|
| 120 | printf(INDENT "Transfer speed: %0.3f B/s\n", speed);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | static int run_intr_in(async_exch_t *exch, const tmon_test_params_t *generic_params)
|
|---|
| 124 | {
|
|---|
| 125 | const tmon_burst_test_params_t *params = (tmon_burst_test_params_t *) generic_params;
|
|---|
| 126 | puts("Reading data from interrupt endpoint.\n");
|
|---|
| 127 | print_params(params);
|
|---|
| 128 |
|
|---|
| 129 | usbdiag_dur_t duration;
|
|---|
| 130 | int rc = usbdiag_burst_intr_in(exch, params->cycles, params->size, &duration);
|
|---|
| 131 | if (rc) {
|
|---|
| 132 | printf(NAME ": Test failed with error: %s\n", str_error(rc));
|
|---|
| 133 | return 1;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | puts("Test succeeded.\n");
|
|---|
| 137 | print_results(params, duration);
|
|---|
| 138 | return 0;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | static int run_intr_out(async_exch_t *exch, const tmon_test_params_t *generic_params)
|
|---|
| 142 | {
|
|---|
| 143 | const tmon_burst_test_params_t *params = (tmon_burst_test_params_t *) generic_params;
|
|---|
| 144 | puts("Writing data to interrupt endpoint.\n");
|
|---|
| 145 | print_params(params);
|
|---|
| 146 |
|
|---|
| 147 | usbdiag_dur_t duration;
|
|---|
| 148 | int rc = usbdiag_burst_intr_out(exch, params->cycles, params->size, &duration);
|
|---|
| 149 | if (rc) {
|
|---|
| 150 | printf(NAME ": Test failed with error: %s\n", str_error(rc));
|
|---|
| 151 | return 1;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | puts("Test succeeded.\n");
|
|---|
| 155 | print_results(params, duration);
|
|---|
| 156 | return 0;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | static int run_bulk_in(async_exch_t *exch, const tmon_test_params_t *generic_params)
|
|---|
| 160 | {
|
|---|
| 161 | const tmon_burst_test_params_t *params = (tmon_burst_test_params_t *) generic_params;
|
|---|
| 162 | puts("Reading data from bulk endpoint.\n");
|
|---|
| 163 | print_params(params);
|
|---|
| 164 |
|
|---|
| 165 | usbdiag_dur_t duration;
|
|---|
| 166 | int rc = usbdiag_burst_bulk_in(exch, params->cycles, params->size, &duration);
|
|---|
| 167 | if (rc) {
|
|---|
| 168 | printf(NAME ": Test failed with error: %s\n", str_error(rc));
|
|---|
| 169 | return 1;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | puts("Test succeeded.\n");
|
|---|
| 173 | print_results(params, duration);
|
|---|
| 174 | return 0;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | static int run_bulk_out(async_exch_t *exch, const tmon_test_params_t *generic_params)
|
|---|
| 178 | {
|
|---|
| 179 | const tmon_burst_test_params_t *params = (tmon_burst_test_params_t *) generic_params;
|
|---|
| 180 | puts("Writing data to bulk endpoint.\n");
|
|---|
| 181 | print_params(params);
|
|---|
| 182 |
|
|---|
| 183 | usbdiag_dur_t duration;
|
|---|
| 184 | int rc = usbdiag_burst_bulk_out(exch, params->cycles, params->size, &duration);
|
|---|
| 185 | if (rc) {
|
|---|
| 186 | printf(NAME ": Test failed with error: %s\n", str_error(rc));
|
|---|
| 187 | return 1;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | puts("Test succeeded.\n");
|
|---|
| 191 | print_results(params, duration);
|
|---|
| 192 | return 0;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | static int run_isoch_in(async_exch_t *exch, const tmon_test_params_t *generic_params)
|
|---|
| 196 | {
|
|---|
| 197 | const tmon_burst_test_params_t *params = (tmon_burst_test_params_t *) generic_params;
|
|---|
| 198 | puts("Reading data from isochronous endpoint.\n");
|
|---|
| 199 | print_params(params);
|
|---|
| 200 |
|
|---|
| 201 | usbdiag_dur_t duration;
|
|---|
| 202 | int rc = usbdiag_burst_isoch_in(exch, params->cycles, params->size, &duration);
|
|---|
| 203 | if (rc) {
|
|---|
| 204 | printf(NAME ": Test failed with error: %s\n", str_error(rc));
|
|---|
| 205 | return 1;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | puts("Test succeeded.\n");
|
|---|
| 209 | print_results(params, duration);
|
|---|
| 210 | return 0;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | static int run_isoch_out(async_exch_t *exch, const tmon_test_params_t *generic_params)
|
|---|
| 214 | {
|
|---|
| 215 | const tmon_burst_test_params_t *params = (tmon_burst_test_params_t *) generic_params;
|
|---|
| 216 | puts("Writing data to isochronous endpoint.\n");
|
|---|
| 217 | print_params(params);
|
|---|
| 218 |
|
|---|
| 219 | usbdiag_dur_t duration;
|
|---|
| 220 | int rc = usbdiag_burst_isoch_out(exch, params->cycles, params->size, &duration);
|
|---|
| 221 | if (rc) {
|
|---|
| 222 | printf(NAME ": Test failed with error: %s\n", str_error(rc));
|
|---|
| 223 | return 1;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | puts("Test succeeded.\n");
|
|---|
| 227 | print_results(params, duration);
|
|---|
| 228 | return 0;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | int tmon_burst_intr_in(int argc, char *argv[])
|
|---|
| 232 | {
|
|---|
| 233 | static const tmon_test_ops_t ops = {
|
|---|
| 234 | .run = run_intr_in,
|
|---|
| 235 | .read_params = read_params
|
|---|
| 236 | };
|
|---|
| 237 |
|
|---|
| 238 | return tmon_test_main(argc, argv, &ops);
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | int tmon_burst_intr_out(int argc, char *argv[])
|
|---|
| 242 | {
|
|---|
| 243 | static const tmon_test_ops_t ops = {
|
|---|
| 244 | .run = run_intr_out,
|
|---|
| 245 | .read_params = read_params
|
|---|
| 246 | };
|
|---|
| 247 |
|
|---|
| 248 | return tmon_test_main(argc, argv, &ops);
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | int tmon_burst_bulk_in(int argc, char *argv[])
|
|---|
| 252 | {
|
|---|
| 253 | static const tmon_test_ops_t ops = {
|
|---|
| 254 | .run = run_bulk_in,
|
|---|
| 255 | .read_params = read_params
|
|---|
| 256 | };
|
|---|
| 257 |
|
|---|
| 258 | return tmon_test_main(argc, argv, &ops);
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | int tmon_burst_bulk_out(int argc, char *argv[])
|
|---|
| 262 | {
|
|---|
| 263 | static const tmon_test_ops_t ops = {
|
|---|
| 264 | .run = run_bulk_out,
|
|---|
| 265 | .read_params = read_params
|
|---|
| 266 | };
|
|---|
| 267 |
|
|---|
| 268 | return tmon_test_main(argc, argv, &ops);
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | int tmon_burst_isoch_in(int argc, char *argv[])
|
|---|
| 272 | {
|
|---|
| 273 | static const tmon_test_ops_t ops = {
|
|---|
| 274 | .run = run_isoch_in,
|
|---|
| 275 | .read_params = read_params
|
|---|
| 276 | };
|
|---|
| 277 |
|
|---|
| 278 | return tmon_test_main(argc, argv, &ops);
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | int tmon_burst_isoch_out(int argc, char *argv[])
|
|---|
| 282 | {
|
|---|
| 283 | static const tmon_test_ops_t ops = {
|
|---|
| 284 | .run = run_isoch_out,
|
|---|
| 285 | .read_params = read_params
|
|---|
| 286 | };
|
|---|
| 287 |
|
|---|
| 288 | return tmon_test_main(argc, argv, &ops);
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | /** @}
|
|---|
| 292 | */
|
|---|