lib: added C-style BarryLogf() function for internal use
[barry.git] / tools / bjdwp.cc
blobba71abdad7ce18497179ba03767e06a40a9d1bb2
1 ///
2 /// \file bjdwp.cc
3 /// bjdwp command line tool
4 ///
6 /*
7 Copyright (C) 2008-2009, Nicolas VIVIEN
8 Copyright (C) 2005-2010, 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 "i18n.h"
29 using namespace std;
30 using namespace Barry;
33 void printMessage(const std::string &message);
36 void Usage()
38 int major, minor;
39 const char *Version = Barry::Version(major, minor);
41 cerr
42 << "bjdwp - Command line USB Blackberry JDWP\n"
43 << " Copyright 2008-2009, Nicolas VIVIEN.\n"
44 << " Using: " << Version << "\n"
45 << "\n"
46 << " -h This help\n"
47 << " -p pin PIN of device to talk with\n"
48 << " If only one device is plugged in, this flag is optional\n"
49 << " -P pass Simplistic method to specify device password\n"
50 << " -v Dump protocol data during operation\n"
51 << "\n"
52 << "arguments\n"
53 << "\n"
54 << " <address> Interface\n"
55 << " <port> Listen port\n"
56 << endl;
60 int main(int argc, char *argv[], char *envp[])
62 INIT_I18N(PACKAGE);
64 try {
65 uint32_t pin = 0;
66 bool data_dump = false;
67 string password;
68 vector<string> params;
69 string iconvCharset;
71 // process command line options
72 for(;;) {
73 int cmd = getopt(argc, argv, "hp:P:v");
74 if( cmd == -1 )
75 break;
77 switch( cmd )
79 case 'p': // Blackberry PIN
80 pin = strtoul(optarg, NULL, 16);
81 break;
83 case 'P': // Device password
84 password = optarg;
85 break;
87 case 'v': // data dump on
88 data_dump = true;
89 break;
91 case 'h': // help
92 default:
93 Usage();
94 return 0;
98 argc -= optind;
99 argv += optind;
101 if( argc != 2 ) {
102 cerr << "missing command" << endl;
103 Usage();
104 return 1;
107 // Fetch address & port arguments
108 char *address = argv[0];
109 int port = atoi(argv[1]);
112 // Initialize the barry library. Must be called before
113 // anything else.
114 Barry::Init(data_dump);
116 // Probe the USB bus for Blackberry devices and display.
117 // If user has specified a PIN, search for it in the
118 // available device list here as well
119 Barry::Probe probe;
120 int activeDevice = probe.FindActive(pin);
121 if( activeDevice == -1 ) {
122 cerr << "No device selected, or PIN not found" << endl;
123 return 1;
126 Barry::Controller con(probe.Get(activeDevice));
127 Barry::Mode::JVMDebug jvmdebug(con);
129 // Start JDW daemon...
130 //---------------------
132 // Create JDWP server and configure
133 JDWP::JDWServer server(jvmdebug, address, port);
135 // Link device
136 server.SetPasswordDevice(password);
138 // Redirect console message
139 server.SetConsoleCallback(&printMessage);
141 server.Start();
143 // FIXME - is this needed... couldn't we do a join here?
144 while (true)
145 sleep(1);
147 server.Stop();
149 catch( Usb::Error &ue) {
150 std::cout << endl; // flush any normal output first
151 std::cerr << "Usb::Error caught: " << ue.what() << endl;
152 return 1;
154 catch( Barry::Error &se ) {
155 std::cout << endl;
156 std::cerr << "Barry::Error caught: " << se.what() << endl;
157 return 1;
159 catch( std::exception &e ) {
160 std::cout << endl;
161 std::cerr << "std::exception caught: " << e.what() << endl;
162 return 1;
165 return 0;
169 void printMessage(const std::string &message)
171 const char esc = 27;
172 const int green = 32;
173 const int blue = 34;
175 std::cout << esc << '[' << green << "mJVM>" << esc << '[' << blue << "m " << message << esc << "[0m";