Home | History | Annotate | Download | only in descriptors
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.server.usb.descriptors;
     17 
     18 import android.hardware.usb.UsbDeviceConnection;
     19 import android.util.Log;
     20 
     21 import com.android.server.usb.descriptors.report.UsbStrings;
     22 
     23 /**
     24  * @hide
     25  * A class that just walks the descriptors and does a hex dump of the contained values.
     26  * Usefull as a debugging tool.
     27  */
     28 public final class UsbBinaryParser {
     29     private static final String TAG = "UsbBinaryParser";
     30     private static final boolean LOGGING = false;
     31 
     32     private void dumpDescriptor(ByteStream stream, int length, byte type, StringBuilder builder) {
     33 
     34         // Log
     35         if (LOGGING) {
     36             Log.i(TAG, "l: " + length + " t: " + Integer.toHexString(type) + " "
     37                     + UsbStrings.getDescriptorName(type));
     38             StringBuilder sb = new StringBuilder();
     39             for (int index = 2; index < length; index++) {
     40                 sb.append("0x" + Integer.toHexString(stream.getByte() & 0xFF) + " ");
     41             }
     42             Log.i(TAG, sb.toString());
     43         } else {
     44             // Screen Dump
     45             builder.append("<p>");
     46             builder.append("<b> l: " + length
     47                     + " t:0x" + Integer.toHexString(type) + " "
     48                     + UsbStrings.getDescriptorName(type) + "</b><br>");
     49             for (int index = 2; index < length; index++) {
     50                 builder.append("0x" + Integer.toHexString(stream.getByte() & 0xFF) + " ");
     51             }
     52             builder.append("</p>");
     53         }
     54     }
     55 
     56     /**
     57      * Walk through descriptor stream and generate an HTML text report of the contents.
     58      * TODO: This should be done in the model of UsbDescriptorsParser/Reporter model.
     59      */
     60     public void parseDescriptors(UsbDeviceConnection connection, byte[] descriptors,
     61                                  StringBuilder builder) {
     62 
     63         builder.append("<tt>");
     64         ByteStream stream = new ByteStream(descriptors);
     65         while (stream.available() > 0) {
     66             int length = (int) stream.getByte() & 0x000000FF;
     67             byte type = stream.getByte();
     68             dumpDescriptor(stream, length, type, builder);
     69         }
     70         builder.append("</tt>");
     71     }
     72 }
     73