bjdwp.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       bjdwp.cc
00003 ///             bjdwp command line tool
00004 ///
00005 
00006 /*
00007     Copyright (C) 2008-2009, Nicolas VIVIEN
00008     Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
00009 
00010     This program is free software; you can redistribute it and/or modify
00011     it under the terms of the GNU General Public License as published by
00012     the Free Software Foundation; either version 2 of the License, or
00013     (at your option) any later version.
00014 
00015     This program is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 
00019     See the GNU General Public License in the COPYING file at the
00020     root directory of this project for more details.
00021 */
00022 
00023 
00024 #include <barry/barry.h>
00025 #include <iostream>
00026 #include <string>
00027 #include <stdlib.h>
00028 #include <unistd.h>
00029 #include "i18n.h"
00030 
00031 using namespace std;
00032 using namespace Barry;
00033 
00034 
00035 void printMessage(const std::string &message);
00036 
00037 
00038 void Usage()
00039 {
00040    int logical, major, minor;
00041    const char *Version = Barry::Version(logical, major, minor);
00042 
00043    cerr
00044    << "bjdwp - Command line USB Blackberry JDWP\n"
00045    << "        Copyright 2008-2009, Nicolas VIVIEN.\n"
00046    << "        Using: " << Version << "\n"
00047    << "\n"
00048    << "   -h        This help\n"
00049    << "   -p pin    PIN of device to talk with\n"
00050    << "             If only one device is plugged in, this flag is optional\n"
00051    << "   -P pass   Simplistic method to specify device password\n"
00052    << "   -v        Dump protocol data during operation\n"
00053    << "\n"
00054    << "arguments\n"
00055    << "\n"
00056    << "  <address>  Interface\n"
00057    << "  <port>     Listen port\n"
00058    << endl;
00059 }
00060 
00061 
00062 int main(int argc, char *argv[], char *envp[])
00063 {
00064         INIT_I18N(PACKAGE);
00065 
00066         try {
00067                 uint32_t pin = 0;
00068                 bool data_dump = false;
00069                 string password;
00070                 vector<string> params;
00071                 string iconvCharset;
00072 
00073                 // process command line options
00074                 for(;;) {
00075                         int cmd = getopt(argc, argv, "hp:P:v");
00076                         if( cmd == -1 )
00077                                 break;
00078 
00079                         switch( cmd )
00080                         {
00081                         case 'p':       // Blackberry PIN
00082                                 pin = strtoul(optarg, NULL, 16);
00083                                 break;
00084 
00085                         case 'P':       // Device password
00086                                 password = optarg;
00087                                 break;
00088 
00089                         case 'v':       // data dump on
00090                                 data_dump = true;
00091                                 break;
00092 
00093                         case 'h':       // help
00094                         default:
00095                                 Usage();
00096                                 return 0;
00097                         }
00098                 }
00099 
00100                 argc -= optind;
00101                 argv += optind;
00102 
00103                 if( argc != 2 ) {
00104                         cerr << "missing command" << endl;
00105                         Usage();
00106                         return 1;
00107                 }
00108 
00109                 // Fetch address & port arguments
00110                 char *address = argv[0];
00111                 int port = atoi(argv[1]);
00112 
00113 
00114                 // Initialize the barry library.  Must be called before
00115                 // anything else.
00116                 Barry::Init(data_dump);
00117 
00118                 // Probe the USB bus for Blackberry devices and display.
00119                 // If user has specified a PIN, search for it in the
00120                 // available device list here as well
00121                 Barry::Probe probe;
00122                 int activeDevice = probe.FindActive(pin);
00123                 if( activeDevice == -1 ) {
00124                         cerr << "No device selected, or PIN not found" << endl;
00125                         return 1;
00126                 }
00127 
00128                 Barry::Controller con(probe.Get(activeDevice));
00129                 Barry::Mode::JVMDebug jvmdebug(con);
00130 
00131                 // Start JDW daemon...
00132                 //---------------------
00133 
00134                 // Create JDWP server and configure
00135                 JDWP::JDWServer server(jvmdebug, address, port);
00136 
00137                 // Link device
00138                 server.SetPasswordDevice(password);
00139 
00140                 // Redirect console message
00141                 server.SetConsoleCallback(&printMessage);
00142 
00143                 server.Start();
00144 
00145                 // FIXME - is this needed... couldn't we do a join here?
00146                 while (true)
00147                         sleep(1);
00148 
00149                 server.Stop();
00150         }
00151         catch( Usb::Error &ue) {
00152                 std::cout << endl;      // flush any normal output first
00153                 std::cerr << "Usb::Error caught: " << ue.what() << endl;
00154                 return 1;
00155         }
00156         catch( Barry::Error &se ) {
00157                 std::cout << endl;
00158                 std::cerr << "Barry::Error caught: " << se.what() << endl;
00159                 return 1;
00160         }
00161         catch( std::exception &e ) {
00162                 std::cout << endl;
00163                 std::cerr << "std::exception caught: " << e.what() << endl;
00164                 return 1;
00165         }
00166 
00167         return 0;
00168 }
00169 
00170 
00171 void printMessage(const std::string &message)
00172 {
00173         const char esc = 27;
00174         const int green = 32;
00175         const int blue = 34;
00176 
00177         std::cout << esc << '[' << green << "mJVM>" << esc << '[' << blue << "m " << message << esc << "[0m";
00178 }
00179