Barry debian version 0.18.5-1
[barry.git] / tools / bjdwp.cc
bloba00599c4e3c2eaea37a3ddc609093dd0cd363409
1 ///
2 /// \file bjdwp.cc
3 /// bjdwp command line tool
4 ///
6 /*
7 Copyright (C) 2008-2009, Nicolas VIVIEN
8 Copyright (C) 2005-2013, 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 <unistd.h>
29 #include "i18n.h"
31 using namespace std;
32 using namespace Barry;
35 void printMessage(const std::string &message);
38 void Usage()
40 int logical, major, minor;
41 const char *Version = Barry::Version(logical, major, minor);
43 cerr << string_vprintf(
44 _("bjdwp - Command line USB Blackberry JDWP\n"
45 " Copyright 2008-2009, Nicolas VIVIEN.\n"
46 " Using: %s\n"
47 "\n"
48 " -h This help\n"
49 " -p pin PIN of device to talk with\n"
50 " If only one device is plugged in, this flag is optional\n"
51 " -P pass Simplistic method to specify device password\n"
52 " -v Dump protocol data during operation\n"
53 "\n"
54 "arguments\n"
55 "\n"
56 " <address> Interface\n"
57 " <port> Listen port\n"), Version)
58 << endl;
62 int main(int argc, char *argv[], char *envp[])
64 INIT_I18N(PACKAGE);
66 try {
67 uint32_t pin = 0;
68 bool data_dump = false;
69 string password;
70 vector<string> params;
71 string iconvCharset;
73 // process command line options
74 for(;;) {
75 int cmd = getopt(argc, argv, "hp:P:v");
76 if( cmd == -1 )
77 break;
79 switch( cmd )
81 case 'p': // Blackberry PIN
82 pin = strtoul(optarg, NULL, 16);
83 break;
85 case 'P': // Device password
86 password = optarg;
87 break;
89 case 'v': // data dump on
90 data_dump = true;
91 break;
93 case 'h': // help
94 default:
95 Usage();
96 return 0;
100 argc -= optind;
101 argv += optind;
103 if( argc != 2 ) {
104 cerr << _("missing command") << endl;
105 Usage();
106 return 1;
109 // Fetch address & port arguments
110 char *address = argv[0];
111 int port = atoi(argv[1]);
114 // Initialize the barry library. Must be called before
115 // anything else.
116 Barry::Init(data_dump);
118 // Probe the USB bus for Blackberry devices and display.
119 // If user has specified a PIN, search for it in the
120 // available device list here as well
121 Barry::Probe probe;
122 int activeDevice = probe.FindActive(pin);
123 if( activeDevice == -1 ) {
124 cerr << _("No device selected, or PIN not found")
125 << endl;
126 return 1;
129 Barry::Controller con(probe.Get(activeDevice));
130 Barry::Mode::JVMDebug jvmdebug(con);
132 // Start JDW daemon...
133 //---------------------
135 // Create JDWP server and configure
136 JDWP::JDWServer server(jvmdebug, address, port);
138 // Link device
139 server.SetPasswordDevice(password);
141 // Redirect console message
142 server.SetConsoleCallback(&printMessage);
144 server.Start();
146 // FIXME - is this needed... couldn't we do a join here?
147 while (true)
148 sleep(1);
150 server.Stop();
152 catch( Usb::Error &ue) {
153 std::cout << endl; // flush any normal output first
154 std::cerr << _("Usb::Error caught: ") << ue.what() << endl;
155 return 1;
157 catch( Barry::Error &se ) {
158 std::cout << endl;
159 std::cerr << _("Barry::Error caught: ") << se.what() << endl;
160 return 1;
162 catch( std::exception &e ) {
163 std::cout << endl;
164 std::cerr << _("std::exception caught: ") << e.what() << endl;
165 return 1;
168 return 0;
172 void printMessage(const std::string &message)
174 const char esc = 27;
175 const int green = 32;
176 const int blue = 34;
178 std::cout << esc << '[' << green << "mJVM>" << esc << '[' << blue << "m " << message << esc << "[0m";