brimtrans.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       brimtrans.cc
00003 ///             Translates log files generated by RIM's USB driver
00004 ///             into something more or less readable.
00005 ///
00006 
00007 /*
00008     Copyright (C) 2005-2009, Net Direct Inc. (http://www.netdirect.ca/)
00009     Copyright (C) 2009, Josh Kropf
00010 
00011     This program is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     This program is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00019 
00020     See the GNU General Public License in the COPYING file at the
00021     root directory of this project for more details.
00022 */
00023 
00024 #include <iostream>
00025 #include <iomanip>
00026 #include <sstream>
00027 #include <string>
00028 #include <vector>
00029 #include <list>
00030 #include <cctype>
00031 #include <stdint.h>
00032 
00033 using namespace std;
00034 
00035 string trim_left(const string& source, const string& t = " ");
00036 string trim_right(const string& source , const string& t = " ");
00037 string trim_data_line(const string& line);
00038 void dump(const list<string>& event);
00039 void parse_hex(const string& source, vector<uint16_t>& data);
00040 void print_hex(const vector<uint16_t>& data);
00041 
00042 int main(int argc, char* argv[])
00043 {
00044         list<string>* event = NULL;
00045         string line;
00046         while( !getline(cin, line).eof() ) {
00047                 size_t pos = line.find_first_of(':');
00048 
00049                 // skip lines missing colon
00050                 if( pos == string::npos )
00051                         continue;
00052 
00053                 line = trim_right(line.substr(pos + 2), "\n\r");
00054 
00055                 // each sequence of write/read packets begins with this line
00056                 if( line == "BbUsbDevice: WriteToDevice" ) {
00057                         if( event != NULL ) {
00058                                 dump(*event);
00059                                 delete event;
00060                         }
00061 
00062                         event = new list<string>;
00063                 }
00064                 else if( line.substr(0, 3) == "<-:" ) { // send to device
00065                         event->push_front(trim_data_line(line.erase(0, 3)));
00066                 }
00067                 else if( line.substr(0, 3) == "->:" ) { // receive from device
00068                         event->push_back(trim_data_line(line.erase(0, 3)));
00069                 }
00070         }
00071 
00072         if( event != NULL ) {
00073                 dump(*event);
00074                 delete event;
00075         }
00076 
00077         return 0;
00078 }
00079 
00080 string trim_left(const string& source, const string& t)
00081 {
00082         string str = source;
00083         return str.erase(0 , source.find_first_not_of(t));
00084 }
00085 
00086 string trim_right(const string& source , const string& t)
00087 {
00088         string str = source;
00089         return str.erase(str.find_last_not_of(t) + 1);
00090 }
00091 
00092 string trim_data_line(const string& line)
00093 {
00094         size_t pos = line.find_first_of(':');
00095         return line.substr(pos + 2);
00096 }
00097 
00098 void dump(const list<string>& event)
00099 {
00100         vector<uint16_t> data;
00101         list<string>::const_iterator i, begin = event.begin(), end = event.end();
00102         for( i=begin; i != end; ++i ) {
00103                 data.clear();
00104                 parse_hex(*i, data);
00105                 cout << (i == begin? "Send" : "Receive") << endl;
00106                 print_hex(data);
00107         }
00108         cout << endl;
00109 }
00110 
00111 void parse_hex(const string& source, vector<uint16_t>& data)
00112 {
00113         istringstream ss(source);
00114         uint16_t byte;
00115 
00116         while( !ss.eof() ) {
00117                 ss >> hex >> byte;
00118                 data.push_back(byte);
00119         }
00120 }
00121 
00122 void print_hex(const vector<uint16_t>& data)
00123 {
00124         int remaining = data.size(), offset = 0;
00125         do {
00126                 cout << "    " << hex << setfill('0') << setw(8) << offset;
00127                 int margin = 13;
00128 
00129                 for( int i=0, stop=min(16, remaining); i<stop; i++ ) {
00130                         cout << ' ' << hex << setfill('0') << setw(2) << data[offset + i];
00131                         margin += 3;
00132                 }
00133 
00134                 cout << string(62-margin, ' ');
00135 
00136                 for( int i=0, stop=min(16, remaining); i<stop; i++) {
00137                         char c = data[offset + i];
00138                         cout << (isprint(c)? c : '.');
00139                 }
00140 
00141                 offset += 16;
00142                 remaining -= 16;
00143 
00144                 cout << endl;
00145         } while( remaining > 0 );
00146 }

Generated on Tue Jun 30 16:08:13 2009 for Barry by  doxygen 1.5.8