source: mainline/uspace/app/tmon/tf.c@ 3038d51

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3038d51 was 3b5a5e3, checked in by Petr Manek <petr.manek@…>, 8 years ago

tmon: catch up with libdrv changes, simplify code

  • Property mode set to 100644
File size: 5.2 KB
Line 
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 * Testing framework.
35 */
36
37#include <stdio.h>
38#include <macros.h>
39#include <devman.h>
40#include <str_error.h>
41#include <usbdiag_iface.h>
42#include "resolve.h"
43#include "tf.h"
44
45#define NAME "tmon"
46#define MAX_PATH_LENGTH 1024
47
48/** Common command handler for all test commands.
49 * @param[in] argc Number of arguments.
50 * @param[in] argv Argument values. Must point to exactly `argc` strings.
51 *
52 * @return Exit code
53 */
54int tmon_test_main(int argc, char *argv[], const tmon_test_ops_t *ops)
55{
56 // Resolve device function.
57 devman_handle_t fun = -1;
58
59 if (argc >= 2 && *argv[1] != '-') {
60 // Assume that the first argument is device path.
61 if (tmon_resolve_named(argv[1], &fun))
62 return 1;
63 } else {
64 // The first argument is either an option or not present.
65 if (tmon_resolve_default(&fun))
66 return 1;
67 }
68
69 int rc, ec;
70 char path[MAX_PATH_LENGTH];
71 if ((rc = devman_fun_get_path(fun, path, sizeof(path)))) {
72 printf(NAME ": Error resolving path of device with handle "
73 "%" PRIun ". %s\n", fun, str_error(rc));
74 return 1;
75 }
76
77 printf("Device: %s\n", path);
78
79 // Read test parameters from options.
80 void *params = NULL;
81 if ((rc = ops->read_params(argc, argv, &params))) {
82 printf(NAME ": Reading test parameters failed. %s\n", str_error(rc));
83 return 1;
84 }
85
86 if ((rc = ops->pre_run(params))) {
87 printf(NAME ": Pre-run hook failed. %s\n", str_error(rc));
88 return 1;
89 }
90
91 // Run the test body.
92 async_sess_t *sess = usbdiag_connect(fun);
93 if (!sess) {
94 printf(NAME ": Could not connect to the device.\n");
95 return 1;
96 }
97
98 async_exch_t *exch = async_exchange_begin(sess);
99 if (!exch) {
100 printf(NAME ": Could not start exchange with the device.\n");
101 ec = 1;
102 goto err_sess;
103 }
104
105 ec = ops->run(exch, params);
106 async_exchange_end(exch);
107
108err_sess:
109 usbdiag_disconnect(sess);
110 free(params);
111 return ec;
112}
113
114/** Unit of quantity used for pretty formatting. */
115typedef struct tmon_unit {
116 /** Prefix letter, which is printed before the actual unit. */
117 const char *unit;
118 /** Factor of the unit. */
119 double factor;
120} tmon_unit_t;
121
122/** Format a value for human reading.
123 * @param[in] val The value to format.
124 * @param[in] fmt Format string. Must include one double and char.
125 *
126 * @return Heap-allocated string if successful (caller becomes its owner), NULL otherwise.
127 */
128static char *format_unit(double val, const char *fmt, const tmon_unit_t *units, size_t len)
129{
130 // Figure out the "tightest" unit.
131 unsigned i;
132 for (i = 0; i < len; ++i) {
133 if (units[i].factor <= val)
134 break;
135 }
136
137 if (i == len) --i;
138 const char *unit = units[i].unit;
139 double factor = units[i].factor;
140
141 // Format the size.
142 const double div_size = val / factor;
143
144 char *out = NULL;
145 asprintf(&out, fmt, div_size, unit);
146
147 return out;
148}
149
150/** Static array of size units with decreasing factors. */
151static const tmon_unit_t size_units[] = {
152 { .unit = "EB", .factor = 1ULL << 60 },
153 { .unit = "PB", .factor = 1ULL << 50 },
154 { .unit = "TB", .factor = 1ULL << 40 },
155 { .unit = "GB", .factor = 1ULL << 30 },
156 { .unit = "MB", .factor = 1ULL << 20 },
157 { .unit = "kB", .factor = 1ULL << 10 },
158 { .unit = "B", .factor = 1ULL },
159};
160
161char *tmon_format_size(double val, const char *fmt)
162{
163 return format_unit(val, fmt, size_units, ARRAY_SIZE(size_units));
164}
165
166/** Static array of duration units with decreasing factors. */
167static const tmon_unit_t dur_units[] = {
168 { .unit = "d", .factor = 60 * 60 * 24 },
169 { .unit = "h", .factor = 60 * 60 },
170 { .unit = "min", .factor = 60 },
171 { .unit = "s", .factor = 1 },
172 { .unit = "ms", .factor = 1e-3 },
173 { .unit = "us", .factor = 1e-6 },
174 { .unit = "ns", .factor = 1e-9 },
175 { .unit = "ps", .factor = 1e-12 },
176};
177
178char *tmon_format_duration(usbdiag_dur_t val, const char *fmt)
179{
180 return format_unit(val / 1000.0, fmt, dur_units, ARRAY_SIZE(dur_units));
181}
182
183/** @}
184 */
Note: See TracBrowser for help on using the repository browser.