source: mainline/uspace/app/usbinfo/dump.c

Last change on this file was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[07b9203e]1/*
2 * Copyright (c) 2010 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
[632ed68]29/** @addtogroup usbinfo
[07b9203e]30 * @{
31 */
32/**
33 * @file
[632ed68]34 * USB querying.
[07b9203e]35 */
36
37#include <stdio.h>
38#include <stdlib.h>
[582a0b8]39#include <stdbool.h>
40#include <stddef.h>
[07b9203e]41#include <errno.h>
42#include <str_error.h>
43
44#include <usb/usb.h>
45#include <usb/descriptor.h>
[466b120]46#include <usb/debug.h>
[c9d5577]47#include <usb/classes/classes.h>
[07b9203e]48
49#include "usbinfo.h"
[7d521e24]50#include <usb/dev/dp.h>
[07b9203e]51
52#define INDENT " "
[2c5cefa]53#define BYTES_PER_LINE 12
[07b9203e]54
[b1c6e58]55const char *get_indent(size_t level)
[6fe1ab4]56{
57 static const char *indents[] = {
58 INDENT,
59 INDENT INDENT,
60 INDENT INDENT INDENT,
61 INDENT INDENT INDENT INDENT,
[7f70d1c]62 INDENT INDENT INDENT INDENT INDENT,
63 INDENT INDENT INDENT INDENT INDENT INDENT,
[6fe1ab4]64 };
[3bacee1]65 static size_t indents_count = sizeof(indents) / sizeof(indents[0]);
[6fe1ab4]66 if (level >= indents_count) {
67 return indents[indents_count - 1];
68 }
69 return indents[level];
70}
71
72void dump_buffer(const char *msg, size_t indent,
73 const uint8_t *buffer, size_t length)
[2c5cefa]74{
[5f635ca]75 if (msg != NULL) {
76 printf("%s\n", msg);
77 }
[2c5cefa]78
79 size_t i;
[6fe1ab4]80 if (length > 0) {
81 printf("%s", get_indent(indent));
82 }
[2c5cefa]83 for (i = 0; i < length; i++) {
[6fe1ab4]84 printf("0x%02X", buffer[i]);
[3bacee1]85 if (((i > 0) && (((i + 1) % BYTES_PER_LINE) == 0)) ||
86 (i + 1 == length)) {
[2c5cefa]87 printf("\n");
[6fe1ab4]88 if (i + 1 < length) {
89 printf("%s", get_indent(indent));
90 }
91 } else {
92 printf(" ");
[2c5cefa]93 }
94 }
95}
96
[5f635ca]97void dump_usb_descriptor(uint8_t *descriptor, size_t size)
[07b9203e]98{
[a12917e]99 printf("Device descriptor:\n");
[466b120]100 usb_dump_standard_descriptor(stdout, get_indent(0), "\n",
101 descriptor, size);
[5f635ca]102}
103
[3100ebe]104void dump_match_ids(match_id_list_t *matches, const char *line_prefix)
[5f635ca]105{
[feeac0d]106 list_foreach(matches->ids, link, match_id_t, match) {
[7b13d8e]107 printf("%s%3d %s\n", line_prefix, match->score, match->id);
[5f635ca]108 }
[2c5cefa]109}
110
[8a121b1]111static void dump_tree_descriptor(const uint8_t *descriptor, size_t depth)
[5ccb15c]112{
113 if (descriptor == NULL) {
114 return;
115 }
[8a121b1]116 int type = descriptor[1];
[5ccb15c]117 const char *name = "unknown";
118 switch (type) {
119#define _TYPE(descriptor_type) \
120 case USB_DESCTYPE_##descriptor_type: name = #descriptor_type; break
121 _TYPE(DEVICE);
122 _TYPE(CONFIGURATION);
123 _TYPE(STRING);
124 _TYPE(INTERFACE);
125 _TYPE(ENDPOINT);
126 _TYPE(HID);
127 _TYPE(HID_REPORT);
128 _TYPE(HID_PHYSICAL);
129 _TYPE(HUB);
130#undef _TYPE
131 }
[6fe1ab4]132 printf("%s%s (0x%02X):\n", get_indent(depth), name, type);
[466b120]133 usb_dump_standard_descriptor(stdout, get_indent(depth), "\n",
134 descriptor, descriptor[0]);
[5ccb15c]135}
136
[8a121b1]137static void dump_tree_internal(
138 usb_dp_parser_t *parser, usb_dp_parser_data_t *data,
139 const uint8_t *root, size_t depth)
[5ccb15c]140{
141 if (root == NULL) {
142 return;
143 }
144 dump_tree_descriptor(root, depth);
[8a121b1]145 const uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
[5ccb15c]146 do {
147 dump_tree_internal(parser, data, child, depth + 1);
148 child = usb_dp_get_sibling_descriptor(parser, data, root, child);
149 } while (child != NULL);
150}
151
152static void dump_tree(usb_dp_parser_t *parser, usb_dp_parser_data_t *data)
153{
[7c95d6f5]154 const uint8_t *ptr = data->data;
[5ccb15c]155 printf("Descriptor tree:\n");
[6fe1ab4]156 dump_tree_internal(parser, data, ptr, 0);
[5ccb15c]157}
158
159#define NESTING(parentname, childname) \
160 { \
161 .child = USB_DESCTYPE_##childname, \
162 .parent = USB_DESCTYPE_##parentname, \
163 }
164#define LAST_NESTING { -1, -1 }
165
166static usb_dp_descriptor_nesting_t descriptor_nesting[] = {
167 NESTING(CONFIGURATION, INTERFACE),
168 NESTING(INTERFACE, ENDPOINT),
169 NESTING(INTERFACE, HUB),
170 NESTING(INTERFACE, HID),
171 NESTING(HID, HID_REPORT),
172 LAST_NESTING
173};
174
175static usb_dp_parser_t parser = {
176 .nesting = descriptor_nesting
177};
178
179void dump_descriptor_tree(uint8_t *descriptors, size_t length)
180{
181 usb_dp_parser_data_t data = {
182 .data = descriptors,
183 .size = length,
184 .arg = NULL
185 };
186
187 dump_tree(&parser, &data);
188}
[2c5cefa]189
[07b9203e]190/** @}
191 */
Note: See TracBrowser for help on using the repository browser.