desktop: made it possible to run Detect() multiple times in EvoSources
[barry/progweb.git] / tools / bjdwp.cc
blobaf57ca8b9a4971e25e6b08970530e0e5b497f856
1 ///
2 /// \file bjdwp.cc
3 /// bjdwp command line tool
4 ///
6 /*
7 Copyright (C) 2008-2009, Nicolas VIVIEN
8 Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
24 #include <barry/barry.h>
25 #include <iostream>
26 #include <string>
27 #include <stdlib.h>
28 #include "i18n.h"
30 using namespace std;
31 using namespace Barry;
34 void printMessage(const std::string &message);
37 void Usage()
39 int logical, major, minor;
40 const char *Version = Barry::Version(logical, major, minor);
42 cerr
43 << "bjdwp - Command line USB Blackberry JDWP\n"
44 << " Copyright 2008-2009, Nicolas VIVIEN.\n"
45 << " Using: " << Version << "\n"
46 << "\n"
47 << " -h This help\n"
48 << " -p pin PIN of device to talk with\n"
49 << " If only one device is plugged in, this flag is optional\n"
50 << " -P pass Simplistic method to specify device password\n"
51 << " -v Dump protocol data during operation\n"
52 << "\n"
53 << "arguments\n"
54 << "\n"
55 << " <address> Interface\n"
56 << " <port> Listen port\n"
57 << endl;
61 int main(int argc, char *argv[], char *envp[])
63 INIT_I18N(PACKAGE);
65 try {
66 uint32_t pin = 0;
67 bool data_dump = false;
68 string password;
69 vector<string> params;
70 string iconvCharset;
72 // process command line options
73 for(;;) {
74 int cmd = getopt(argc, argv, "hp:P:v");
75 if( cmd == -1 )
76 break;
78 switch( cmd )
80 case 'p': // Blackberry PIN
81 pin = strtoul(optarg, NULL, 16);
82 break;
84 case 'P': // Device password
85 password = optarg;
86 break;
88 case 'v': // data dump on
89 data_dump = true;
90 break;
92 case 'h': // help
93 default:
94 Usage();
95 return 0;
99 argc -= optind;
100 argv += optind;
102 if( argc != 2 ) {
103 cerr << "missing command" << endl;
104 Usage();
105 return 1;
108 // Fetch address & port arguments
109 char *address = argv[0];
110 int port = atoi(argv[1]);
113 // Initialize the barry library. Must be called before
114 // anything else.
115 Barry::Init(data_dump);
117 // Probe the USB bus for Blackberry devices and display.
118 // If user has specified a PIN, search for it in the
119 // available device list here as well
120 Barry::Probe probe;
121 int activeDevice = probe.FindActive(pin);
122 if( activeDevice == -1 ) {
123 cerr << "No device selected, or PIN not found" << endl;
124 return 1;
127 Barry::Controller con(probe.Get(activeDevice));
128 Barry::Mode::JVMDebug jvmdebug(con);
130 // Start JDW daemon...
131 //---------------------
133 // Create JDWP server and configure
134 JDWP::JDWServer server(jvmdebug, address, port);
136 // Link device
137 server.SetPasswordDevice(password);
139 // Redirect console message
140 server.SetConsoleCallback(&printMessage);
142 server.Start();
144 // FIXME - is this needed... couldn't we do a join here?
145 while (true)
146 sleep(1);
148 server.Stop();
150 catch( Usb::Error &ue) {
151 std::cout << endl; // flush any normal output first
152 std::cerr << "Usb::Error caught: " << ue.what() << endl;
153 return 1;
155 catch( Barry::Error &se ) {
156 std::cout << endl;
157 std::cerr << "Barry::Error caught: " << se.what() << endl;
158 return 1;
160 catch( std::exception &e ) {
161 std::cout << endl;
162 std::cerr << "std::exception caught: " << e.what() << endl;
163 return 1;
166 return 0;
170 void printMessage(const std::string &message)
172 const char esc = 27;
173 const int green = 32;
174 const int blue = 34;
176 std::cout << esc << '[' << green << "mJVM>" << esc << '[' << blue << "m " << message << esc << "[0m";