Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/usbinfo/dump.c

    r5ccb15c rb1c6e58  
    2727 */
    2828
    29 /** @addtogroup usb
     29/** @addtogroup usbinfo
    3030 * @{
    3131 */
    3232/**
    3333 * @file
    34  * @brief USB querying.
     34 * USB querying.
    3535 */
    3636
     
    4343#include <usb/usb.h>
    4444#include <usb/descriptor.h>
     45#include <usb/debug.h>
     46#include <usb/classes/classes.h>
    4547
    4648#include "usbinfo.h"
     
    5052#define BYTES_PER_LINE 12
    5153
    52 #define BCD_INT(a) (((unsigned int)(a)) / 256)
    53 #define BCD_FRAC(a) (((unsigned int)(a)) % 256)
    5454
    55 #define BCD_FMT "%x.%x"
    56 #define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
     55const char *get_indent(size_t level)
     56{
     57        static const char *indents[] = {
     58                INDENT,
     59                INDENT INDENT,
     60                INDENT INDENT INDENT,
     61                INDENT INDENT INDENT INDENT,
     62                INDENT INDENT INDENT INDENT INDENT
     63        };
     64        static size_t indents_count = sizeof(indents)/sizeof(indents[0]);
     65        if (level >= indents_count) {
     66                return indents[indents_count - 1];
     67        }
     68        return indents[level];
     69}
    5770
    58 void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
     71void dump_buffer(const char *msg, size_t indent,
     72    const uint8_t *buffer, size_t length)
    5973{
    60         printf("%s\n", msg);
     74        if (msg != NULL) {
     75                printf("%s\n", msg);
     76        }
    6177
    6278        size_t i;
     79        if (length > 0) {
     80                printf("%s", get_indent(indent));
     81        }
    6382        for (i = 0; i < length; i++) {
    64                 printf("  0x%02X", buffer[i]);
     83                printf("0x%02X", buffer[i]);
    6584                if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
    6685                    || (i + 1 == length)) {
    6786                        printf("\n");
     87                        if (i + 1 < length) {
     88                                printf("%s", get_indent(indent));
     89                        }
     90                } else {
     91                        printf("  ");
    6892                }
    6993        }
     94}
     95
     96void dump_usb_descriptor(uint8_t *descriptor, size_t size)
     97{
     98        printf("Device descriptor:\n");
     99        usb_dump_standard_descriptor(stdout, get_indent(0), "\n",
     100            descriptor, size);
    70101}
    71102
     
    83114}
    84115
    85 void dump_standard_device_descriptor(usb_standard_device_descriptor_t *d)
    86 {
    87         printf("Standard device descriptor:\n");
    88 
    89         printf(INDENT "bLength = %d\n", d->length);
    90         printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
    91         printf(INDENT "bcdUSB = %d (" BCD_FMT ")\n", d->usb_spec_version,
    92             BCD_ARGS(d->usb_spec_version));
    93         printf(INDENT "bDeviceClass = 0x%02x\n", d->device_class);
    94         printf(INDENT "bDeviceSubClass = 0x%02x\n", d->device_subclass);
    95         printf(INDENT "bDeviceProtocol = 0x%02x\n", d->device_protocol);
    96         printf(INDENT "bMaxPacketSize0 = %d\n", d->max_packet_size);
    97         printf(INDENT "idVendor = %d\n", d->vendor_id);
    98         printf(INDENT "idProduct = %d\n", d->product_id);
    99         printf(INDENT "bcdDevice = %d\n", d->device_version);
    100         printf(INDENT "iManufacturer = %d\n", d->str_manufacturer);
    101         printf(INDENT "iProduct = %d\n", d->str_product);
    102         printf(INDENT "iSerialNumber = %d\n", d->str_serial_number);
    103         printf(INDENT "bNumConfigurations = %d\n", d->configuration_count);
    104 }
    105 
    106 void dump_standard_configuration_descriptor(
    107     int index, usb_standard_configuration_descriptor_t *d)
    108 {
    109         bool self_powered = d->attributes & 64;
    110         bool remote_wakeup = d->attributes & 32;
    111        
    112         printf("Standard configuration descriptor #%d\n", index);
    113         printf(INDENT "bLength = %d\n", d->length);
    114         printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
    115         printf(INDENT "wTotalLength = %d\n", d->total_length);
    116         printf(INDENT "bNumInterfaces = %d\n", d->interface_count);
    117         printf(INDENT "bConfigurationValue = %d\n", d->configuration_number);
    118         printf(INDENT "iConfiguration = %d\n", d->str_configuration);
    119         printf(INDENT "bmAttributes = %d [%s%s%s]\n", d->attributes,
    120             self_powered ? "self-powered" : "",
    121             (self_powered & remote_wakeup) ? ", " : "",
    122             remote_wakeup ? "remote-wakeup" : "");
    123         printf(INDENT "MaxPower = %d (%dmA)\n", d->max_power,
    124             2 * d->max_power);
    125         // printf(INDENT " = %d\n", d->);
    126 }
    127 
    128116static void dump_tree_descriptor(uint8_t *descriptor, size_t depth)
    129117{
    130118        if (descriptor == NULL) {
    131119                return;
    132         }
    133         while (depth > 0) {
    134                 printf("  ");
    135                 depth--;
    136120        }
    137121        int type = (int) *(descriptor + 1);
     
    151135#undef _TYPE
    152136        }
    153         printf("0x%02x (%s)\n", type, name);
     137        printf("%s%s (0x%02X):\n", get_indent(depth), name, type);
     138        usb_dump_standard_descriptor(stdout, get_indent(depth), "\n",
     139            descriptor, descriptor[0]);
    154140}
    155141
     
    172158        uint8_t *ptr = data->data;
    173159        printf("Descriptor tree:\n");
    174         dump_tree_internal(parser, data, ptr, 1);
     160        dump_tree_internal(parser, data, ptr, 0);
    175161}
    176162
Note: See TracChangeset for help on using the changeset viewer.