1 /* 2 * Copyright (C) 2018 The Android Open Source Project 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 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 #pragma once 29 30 #include <sys/types.h> 31 #include <unistd.h> 32 #include <chrono> 33 #include <cstdlib> 34 #include <fstream> 35 #include <string> 36 #include <vector> 37 38 #include "usb.h" 39 40 namespace fastboot { 41 42 /* A special class for sniffing reads and writes 43 * 44 * A useful debugging tool is to see the raw fastboot transactions going between 45 * the host and device. This class wraps the UsbTransport class, and snoops and saves 46 * all the transactions going on. Additionally, if there is a console serial port 47 * from the device, this class can monitor it as well and capture the interleaving of 48 * transport transactions and UART log messages. 49 */ 50 class UsbTransportSniffer : public UsbTransport { 51 public: 52 enum EventType { 53 READ, 54 WRITE, 55 RESET, 56 SERIAL, // Serial log message from device 57 READ_ERROR, 58 WRITE_ERROR, 59 }; 60 61 struct Event { EventEvent62 Event(EventType t, const std::vector<char> cbuf) : type(t), buf(cbuf) { 63 start = std::chrono::high_resolution_clock::now(); 64 }; 65 EventType type; 66 std::chrono::high_resolution_clock::time_point start; 67 const std::vector<char> buf; 68 }; 69 70 UsbTransportSniffer(std::unique_ptr<UsbTransport> transport, const int serial_fd = 0); 71 ~UsbTransportSniffer() override; 72 73 virtual ssize_t Read(void* data, size_t len) override; 74 virtual ssize_t Write(const void* data, size_t len) override; 75 virtual int Close() override final; // note usage in destructor 76 virtual int Reset() override; 77 78 const std::vector<Event> Transfers(); 79 std::string CreateTrace(); 80 void ProcessSerial(); 81 82 private: 83 std::vector<Event> transfers_; 84 std::unique_ptr<UsbTransport> transport_; 85 const int serial_fd_; 86 }; 87 88 } // End namespace fastboot 89