| 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 transfer debugging.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <devman.h>
|
|---|
| 39 | #include <loc.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <str_error.h>
|
|---|
| 42 | #include <usbdiag_iface.h>
|
|---|
| 43 | #include "commands.h"
|
|---|
| 44 |
|
|---|
| 45 | #define NAME "tmon"
|
|---|
| 46 |
|
|---|
| 47 | static int resolve_default_fun(devman_handle_t *fun)
|
|---|
| 48 | {
|
|---|
| 49 | category_id_t diag_cat;
|
|---|
| 50 | service_id_t *svcs;
|
|---|
| 51 | size_t count;
|
|---|
| 52 | int rc;
|
|---|
| 53 |
|
|---|
| 54 | if ((rc = loc_category_get_id(USBDIAG_CATEGORY, &diag_cat, 0))) {
|
|---|
| 55 | printf(NAME ": Error resolving category '%s'", USBDIAG_CATEGORY);
|
|---|
| 56 | return rc;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | if ((rc = loc_category_get_svcs(diag_cat, &svcs, &count))) {
|
|---|
| 60 | printf(NAME ": Error getting list of diagnostic devices.\n");
|
|---|
| 61 | return rc;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | // There must be exactly one diagnostic device for this to work.
|
|---|
| 65 | if (count != 1) {
|
|---|
| 66 | if (count) {
|
|---|
| 67 | printf(NAME ": Found %ld devices. Please specify which to use.\n", count);
|
|---|
| 68 | } else {
|
|---|
| 69 | printf(NAME ": No diagnostic devices found.\n");
|
|---|
| 70 | }
|
|---|
| 71 | return ENOENT;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | if ((rc = devman_fun_sid_to_handle(svcs[0], fun))) {
|
|---|
| 75 | printf(NAME ": Error resolving handle of device with SID %ld.\n", svcs[0]);
|
|---|
| 76 | return rc;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | return EOK;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | static int resolve_named_fun(const char *dev_path, devman_handle_t *fun)
|
|---|
| 83 | {
|
|---|
| 84 | int rc;
|
|---|
| 85 | service_id_t svc;
|
|---|
| 86 | if ((rc = loc_service_get_id(dev_path, &svc, IPC_FLAG_BLOCKING))) {
|
|---|
| 87 | printf(NAME ": Error resolving device. %s\n", str_error(rc));
|
|---|
| 88 | return rc;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | if ((rc = devman_fun_sid_to_handle(svc, fun))) {
|
|---|
| 92 | printf(NAME ": Error resolving handle of device with SID %ld.\n", svc);
|
|---|
| 93 | return rc;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | return EOK;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | static int resolve_and_test(int argc, char *argv[], int (*test)(devman_handle_t)) {
|
|---|
| 100 | devman_handle_t fun = -1;
|
|---|
| 101 |
|
|---|
| 102 | if (argc == 0) {
|
|---|
| 103 | if (resolve_default_fun(&fun))
|
|---|
| 104 | return 1;
|
|---|
| 105 | } else if (argc == 1) {
|
|---|
| 106 | if (resolve_named_fun(argv[0], &fun))
|
|---|
| 107 | return 1;
|
|---|
| 108 | } else {
|
|---|
| 109 | printf(NAME ": Too many arguments provided.\n");
|
|---|
| 110 | return 1;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | return test(fun);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | static int bulk_worker(devman_handle_t fun) {
|
|---|
| 117 | async_sess_t *sess = usbdiag_connect(fun);
|
|---|
| 118 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 119 |
|
|---|
| 120 | // TODO: do some testing
|
|---|
| 121 | int y;
|
|---|
| 122 | int rc = usbdiag_test(exch, 4200, &y);
|
|---|
| 123 |
|
|---|
| 124 | if (rc) {
|
|---|
| 125 | printf(NAME ": %s\n", str_error(rc));
|
|---|
| 126 | } else {
|
|---|
| 127 | printf("The number is %d.\n", y);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | async_exchange_end(exch);
|
|---|
| 131 | usbdiag_disconnect(sess);
|
|---|
| 132 | return 0;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | int tmon_test_bulk(int argc, char *argv[])
|
|---|
| 136 | {
|
|---|
| 137 | return resolve_and_test(argc, argv, bulk_worker);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /** @}
|
|---|
| 141 | */
|
|---|