[3ae93a8] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 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 |
|
---|
| 29 | /** @addtogroup drvusbmid
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * Dumping and debugging functions.
|
---|
| 35 | */
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <str_error.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <usb/pipes.h>
|
---|
| 40 | #include <usb/dp.h>
|
---|
| 41 | #include <usb/classes/classes.h>
|
---|
| 42 | #include "usbmid.h"
|
---|
| 43 |
|
---|
| 44 | /** Dump found descriptor.
|
---|
| 45 | *
|
---|
| 46 | * @param data Descriptor data.
|
---|
| 47 | * @param depth Nesting depth.
|
---|
| 48 | */
|
---|
| 49 | static void dump_tree_descriptor(uint8_t *data, size_t depth)
|
---|
| 50 | {
|
---|
| 51 | if (data == NULL) {
|
---|
| 52 | return;
|
---|
| 53 | }
|
---|
| 54 | int type = (int) *(data + 1);
|
---|
| 55 | if (type == USB_DESCTYPE_INTERFACE) {
|
---|
| 56 | usb_standard_interface_descriptor_t *descriptor
|
---|
| 57 | = (usb_standard_interface_descriptor_t *) data;
|
---|
| 58 | usb_log_info("Found interface: %s (0x%02x/0x%02x/0x%02x).\n",
|
---|
| 59 | usb_str_class(descriptor->interface_class),
|
---|
| 60 | (int) descriptor->interface_class,
|
---|
| 61 | (int) descriptor->interface_subclass,
|
---|
| 62 | (int) descriptor->interface_protocol);
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /** Dump tree of descriptors.
|
---|
| 67 | *
|
---|
| 68 | * @param parser Descriptor parser.
|
---|
| 69 | * @param data Descriptor parser data.
|
---|
| 70 | * @param root Pointer to current root.
|
---|
| 71 | * @param depth Nesting depth.
|
---|
| 72 | */
|
---|
| 73 | static void dump_tree_internal(usb_dp_parser_t *parser, usb_dp_parser_data_t *data,
|
---|
| 74 | uint8_t *root, size_t depth)
|
---|
| 75 | {
|
---|
| 76 | if (root == NULL) {
|
---|
| 77 | return;
|
---|
| 78 | }
|
---|
| 79 | dump_tree_descriptor(root, depth);
|
---|
| 80 | uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
|
---|
| 81 | do {
|
---|
| 82 | dump_tree_internal(parser, data, child, depth + 1);
|
---|
| 83 | child = usb_dp_get_sibling_descriptor(parser, data, root, child);
|
---|
| 84 | } while (child != NULL);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /** Dump descriptor tree.
|
---|
| 88 | *
|
---|
| 89 | * @param parser Descriptor parser.
|
---|
| 90 | * @param data Descriptor parser data.
|
---|
| 91 | */
|
---|
| 92 | static void dump_tree(usb_dp_parser_t *parser, usb_dp_parser_data_t *data)
|
---|
| 93 | {
|
---|
| 94 | uint8_t *ptr = data->data;
|
---|
| 95 | dump_tree_internal(parser, data, ptr, 0);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /** Dump given descriptors.
|
---|
| 99 | *
|
---|
| 100 | * @param descriptors Descriptors buffer (typically full config descriptor).
|
---|
| 101 | * @param length Size of @p descriptors buffer in bytes.
|
---|
| 102 | */
|
---|
| 103 | void usbmid_dump_descriptors(uint8_t *descriptors, size_t length)
|
---|
| 104 | {
|
---|
| 105 | usb_dp_parser_data_t data = {
|
---|
| 106 | .data = descriptors,
|
---|
| 107 | .size = length,
|
---|
| 108 | .arg = NULL
|
---|
| 109 | };
|
---|
| 110 |
|
---|
| 111 | usb_dp_parser_t parser = {
|
---|
| 112 | .nesting = usb_dp_standard_descriptor_nesting
|
---|
| 113 | };
|
---|
| 114 |
|
---|
| 115 | dump_tree(&parser, &data);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /**
|
---|
| 119 | * @}
|
---|
| 120 | */
|
---|