# 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-2011, 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 "i18n.h"
    00028
    00029 using namespace std;
    00030 using namespace Barry;
    00031
    00032
    00033 void printMessage(const std::string &message);
    00034
    00035
    00036 void Usage()
    00037 {
    00038    int major, minor;
    00039    const char *Version = Barry::Version(major, minor);
    00040
    00041    cerr
    00042    << "bjdwp - Command line USB Blackberry JDWP\n"
    00043    << "        Copyright 2008-2009, Nicolas VIVIEN.\n"
    00044    << "        Using: " << Version << "\n"
    00045    << "\n"
    00046    << "   -h        This help\n"
    00047    << "   -p pin    PIN of device to talk with\n"
    00048    << "             If only one device is plugged in, this flag is optional\n"
    00049    << "   -P pass   Simplistic method to specify device password\n"
    00050    << "   -v        Dump protocol data during operation\n"
    00051    << "\n"
    00052    << "arguments\n"
    00053    << "\n"
    00054    << "  <address>  Interface\n"
    00055    << "  <port>     Listen port\n"
    00056    << endl;
    00057 }
    00058
    00059
    00060 int main(int argc, char *argv[], char *envp[])
    00061 {
    00062         INIT_I18N(PACKAGE);
    00063
    00064         try {
    00065                 uint32_t pin = 0;
    00066                 bool data_dump = false;
    00067                 string password;
    00068                 vector<string> params;
    00069                 string iconvCharset;
    00070
    00071                 // process command line options
    00072                 for(;;) {
    00073                         int cmd = getopt(argc, argv, "hp:P:v");
    00074                         if( cmd == -1 )
    00075                                 break;
    00076
    00077                         switch( cmd )
    00078                         {
    00079                         case 'p':       // Blackberry PIN
    00080                                 pin = strtoul(optarg, NULL, 16);
    00081                                 break;
    00082
    00083                         case 'P':       // Device password
    00084                                 password = optarg;
    00085                                 break;
    00086
    00087                         case 'v':       // data dump on
    00088                                 data_dump = true;
    00089                                 break;
    00090
    00091                         case 'h':       // help
    00092                         default:
    00093                                 Usage();
    00094                                 return 0;
    00095                         }
    00096                 }
    00097
    00098                 argc -= optind;
    00099                 argv += optind;
    00100
    00101                 if( argc != 2 ) {
    00102                         cerr << "missing command" << endl;
    00103                         Usage();
    00104                         return 1;
    00105                 }
    00106
    00107                 // Fetch address & port arguments
    00108                 char *address = argv[0];
    00109                 int port = atoi(argv[1]);
    00110
    00111
    00112                 // Initialize the barry library.  Must be called before
    00113                 // anything else.
    00114                 Barry::Init(data_dump);
    00115
    00116                 // Probe the USB bus for Blackberry devices and display.
    00117                 // If user has specified a PIN, search for it in the
    00118                 // available device list here as well
    00119                 Barry::Probe probe;
    00120                 int activeDevice = probe.FindActive(pin);
    00121                 if( activeDevice == -1 ) {
    00122                         cerr << "No device selected, or PIN not found" << endl;
    00123                         return 1;
    00124                 }
    00125
    00126                 Barry::Controller con(probe.Get(activeDevice));
    00127                 Barry::Mode::JVMDebug jvmdebug(con);
    00128
    00129                 // Start JDW daemon...
    00130                 //---------------------
    00131
    00132                 // Create JDWP server and configure
    00133                 JDWP::JDWServer server(jvmdebug, address, port);
    00134
    00135                 // Link device
    00136                 server.SetPasswordDevice(password);
    00137
    00138                 // Redirect console message
    00139                 server.SetConsoleCallback(&printMessage);
    00140
    00141                 server.Start();
    00142
    00143                 // FIXME - is this needed... couldn't we do a join here?
    00144                 while (true)
    00145                         sleep(1);
    00146
    00147                 server.Stop();
    00148         }
    00149         catch( Usb::Error &ue) {
    00150                 std::cout << endl;      // flush any normal output first
    00151                 std::cerr << "Usb::Error caught: " << ue.what() << endl;
    00152                 return 1;
    00153         }
    00154         catch( Barry::Error &se ) {
    00155                 std::cout << endl;
    00156                 std::cerr << "Barry::Error caught: " << se.what() << endl;
    00157                 return 1;
    00158         }
    00159         catch( std::exception &e ) {
    00160                 std::cout << endl;
    00161                 std::cerr << "std::exception caught: " << e.what() << endl;
    00162                 return 1;
    00163         }
    00164
    00165         return 0;
    00166 }
    00167
    00168
    00169 void printMessage(const std::string &message)
    00170 {
    00171         const char esc = 27;
    00172         const int green = 32;
    00173         const int blue = 34;
    00174
    00175         std::cout << esc << '[' << green << "mJVM>" << esc << '[' << blue << "m " << message << esc << "[0m";
    00176 }
    00177