2 /// \file brawchannel.cc
7 Copyright (C) 2010, RealVNC Ltd.
9 Some parts are inspired from bjavaloader.cc
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 See the GNU General Public License in the COPYING file at the
21 root directory of this project for more details.
25 #include <barry/barry.h>
36 #include <sys/types.h>
41 using namespace Barry
;
43 class StdoutWriter
: public Barry::Mode::RawChannelDataCallback
46 StdoutWriter(bool& keepGoing
, bool verbose
)
47 : m_continuePtr(&keepGoing
)
53 void DataReceived(Data
& data
)
57 std::cerr
<< "From BB: ";
58 data
.DumpHex(std::cerr
);
62 size_t toWrite
= data
.GetSize();
65 while (written
< toWrite
&& *m_continuePtr
)
67 ssize_t writtenThisTime
= write(STDOUT_FILENO
, &(data
.GetData()[written
]), toWrite
- written
);
69 cerr
<< "Written " << writtenThisTime
<< " bytes over stdout\n";
71 if (writtenThisTime
< 0)
73 *m_continuePtr
= false;
77 written
+= writtenThisTime
;
90 const char *Version
= Barry::Version(major
, minor
);
93 << "brawchannel - Command line USB Blackberry raw channel interface\n"
94 << " Copyright 2010, RealVNC Ltd.\n"
95 << " Using: " << Version
<< "\n"
98 << "brawchannel [options] <channel name>\n"
101 << " -p pin PIN of device to talk with\n"
102 << " If only one device is plugged in, this flag is optional\n"
103 << " -P pass Simplistic method to specify device password\n"
104 << " -v Dump protocol data during operation\n"
105 << " This will cause libusb output to appear on STDOUT unless\n"
106 << " the environment variable LIBUSB_DEBUG is set to 0,1 or 2.\n"
110 int main(int argc
, char *argv
[])
114 cout
.sync_with_stdio(true); // leave this on, since libusb uses
115 // stdio for debug messages
119 bool data_dump
= false;
121 vector
<string
> params
;
125 Usb::EndpointPair epOverride
;
127 // process command line options
129 int cmd
= getopt(argc
, argv
, "hp:P:v");
135 case 'p': // Blackberry PIN
136 pin
= strtoul(optarg
, NULL
, 16);
139 case 'P': // Device password
143 case 'v': // data dump on
158 cerr
<< "Error: Missing raw channel name." << endl
;
163 // Fetch command from remaining arguments
164 string channelName
= argv
[0];
168 // Put the remaining arguments into an array
169 for (; argc
> 0; argc
--, argv
++) {
170 params
.push_back(string(argv
[0]));
173 // Initialize the barry library. Must be called before
175 Barry::Init(data_dump
);
177 // Probe the USB bus for Blackberry devices and display.
178 // If user has specified a PIN, search for it in the
179 // available device list here as well
181 int activeDevice
= probe
.FindActive(pin
);
182 if( activeDevice
== -1 ) {
183 cerr
<< "No device selected, or PIN not found" << endl
;
187 // Now start to read from cin and get ready to write
188 // to the BlackBerry.
190 std::cerr
<< "Connected to device, starting read/write\n";
194 // Create the thing which will write onto stdout
195 StdoutWriter
stdoutWriter(running
, data_dump
);
197 // Set up the BlackBerry gubbins
198 // Start a thread to handle any data arriving from
200 auto_ptr
<SocketRoutingQueue
> router
;
201 router
.reset(new SocketRoutingQueue
);
202 router
->SpinoffSimpleReadThread();
204 // Create our controller object
205 Barry::Controller
con(probe
.Get(activeDevice
), *router
);
206 Barry::Mode::RawChannel
rawChannel(con
, stdoutWriter
);
209 // execute each mode that was turned on
211 rawChannel
.Open(password
.c_str(), channelName
.c_str());
213 // We now have a thread running to read from the
214 // BB and write over stdout; in this thread we'll
215 // read from stdin and write to the BB.
216 unsigned char buf
[Barry::Mode::RawChannel::MaximumPacketContentsSize
];
218 size_t haveRead
= read(STDIN_FILENO
, buf
, sizeof(buf
));
220 Data
toWrite(buf
, haveRead
);
222 std::cerr
<< "Sending " << haveRead
<< " bytes stdin->USB\n";
223 std::cerr
<< "To BB: ";
224 toWrite
.DumpHex(std::cerr
);
227 rawChannel
.Send(toWrite
);
229 std::cerr
<< "Sent " << haveRead
<< " bytes stdin->USB\n";
231 else if (haveRead
< 0 || !std::cin
.good()) {
236 catch( Usb::Error
&ue
) {
237 std::cerr
<< "Usb::Error caught: " << ue
.what() << endl
;
240 catch( Barry::Error
&se
) {
241 std::cerr
<< "Barry::Error caught: " << se
.what() << endl
;
244 catch( std::exception
&e
) {
245 std::cerr
<< "std::exception caught: " << e
.what() << endl
;