2 /// \file brawchannel.cc
3 /// Directs a named raw channel over STDIN/STDOUT
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>
37 #include <sys/types.h>
46 using namespace Barry
;
48 // How long to wait between reads before checking if should shutdown
49 #define READ_TIMEOUT_SECONDS 1
51 static volatile bool signalReceived
= false;
53 static void signalHandler(int signum
)
55 signalReceived
= true;
58 class CallbackHandler
: public Barry::Mode::RawChannelDataCallback
61 volatile bool *m_continuePtr
;
65 CallbackHandler(volatile bool &keepGoing
, bool verbose
)
66 : m_continuePtr(&keepGoing
)
72 public: // From RawChannelDataCallback
73 virtual void DataReceived(Data
&data
);
74 virtual void ChannelError(string msg
);
75 virtual void ChannelClose();
79 void CallbackHandler::DataReceived(Data
&data
)
87 size_t toWrite
= data
.GetSize();
90 while( written
< toWrite
&& *m_continuePtr
) {
91 ssize_t writtenThisTime
= write(STDOUT_FILENO
, &(data
.GetData()[written
]), toWrite
- written
);
93 cerr
.setf(ios::dec
, ios::basefield
);
94 cerr
<< "Written " << writtenThisTime
<< " bytes over stdout" << endl
;
97 if( writtenThisTime
< 0 ) {
101 written
+= writtenThisTime
;
106 void CallbackHandler::ChannelError(string msg
)
108 cerr
<< "CallbackHandler: Received error: " << msg
<< endl
;
112 void CallbackHandler::ChannelClose()
114 *m_continuePtr
= false;
120 const char *Version
= Barry::Version(major
, minor
);
123 << "brawchannel - Command line USB Blackberry raw channel interface\n"
124 << " Copyright 2010, RealVNC Ltd.\n"
125 << " Using: " << Version
<< "\n"
128 << "brawchannel [options] <channel name>\n"
131 << " -p pin PIN of device to talk with\n"
132 << " If only one device is plugged in, this flag is optional\n"
133 << " -P pass Simplistic method to specify device password\n"
134 << " -v Dump protocol data during operation\n"
135 << " This will cause libusb output to appear on STDOUT unless\n"
136 << " the environment variable USB_DEBUG is set to 0,1 or 2.\n"
140 // Helper class to restore signal handlers when shutdown is occuring
141 // This class isn't responsible for setting up the signal handlers
142 // as they need to be restored before the Barry::Socket starts closing.
147 sighandler_t m_handler
;
149 SignalRestorer(int signum
, sighandler_t handler
)
150 : m_signum(signum
), m_handler(handler
) {}
151 ~SignalRestorer() { signal(m_signum
, m_handler
); }
154 int main(int argc
, char *argv
[])
158 // Setup signal handling
159 sighandler_t oldSigHup
= signal(SIGHUP
, &signalHandler
);
160 sighandler_t oldSigTerm
= signal(SIGTERM
, &signalHandler
);
161 sighandler_t oldSigInt
= signal(SIGINT
, &signalHandler
);
162 sighandler_t oldSigQuit
= signal(SIGQUIT
, &signalHandler
);
164 cerr
.sync_with_stdio(true); // since libusb uses
165 // stdio for debug messages
167 // Buffer to hold data read in from STDIN before sending it
168 // to the BlackBerry.
169 unsigned char *buf
= NULL
;
172 bool data_dump
= false;
175 // process command line options
177 int cmd
= getopt(argc
, argv
, "hp:P:v");
184 case 'p': // Blackberry PIN
185 pin
= strtoul(optarg
, NULL
, 16);
188 case 'P': // Device password
192 case 'v': // data dump on
207 cerr
<< "Error: Missing raw channel name." << endl
;
213 cerr
<< "Error: Too many arguments." << endl
;
218 // Fetch command from remaining arguments
219 string channelName
= argv
[0];
225 // Warn if USB_DEBUG isn't set to 0, 1 or 2
226 // as that usually means libusb will write to STDOUT
227 char *val
= getenv("USB_DEBUG");
228 int parsedValue
= -1;
230 parsedValue
= atoi(val
);
232 if( parsedValue
!= 0 && parsedValue
!= 1 && parsedValue
!= 2 ) {
233 cerr
<< "Warning: Protocol dump enabled without setting USB_DEBUG to 0, 1 or 2.\n"
234 << " libusb might log to STDOUT and ruin data stream." << endl
;
238 // Initialize the barry library. Must be called before
240 Barry::Init(data_dump
, &cerr
);
242 // Probe the USB bus for Blackberry devices.
243 // If user has specified a PIN, search for it in the
244 // available device list here as well
246 int activeDevice
= probe
.FindActive(pin
);
247 if( activeDevice
== -1 ) {
248 cerr
<< "No device selected, or PIN not found" << endl
;
252 // Now get setup to open the channel.
254 cerr
<< "Connected to device, starting read/write\n";
257 volatile bool running
= true;
259 // Create the thing which will write onto stdout
260 // and perform other callback duties.
261 CallbackHandler
callbackHandler(running
, data_dump
);
263 // Start a thread to handle any data arriving from
265 auto_ptr
<SocketRoutingQueue
> router
;
266 router
.reset(new SocketRoutingQueue());
267 router
->SpinoffSimpleReadThread();
269 // Create our controller object
270 Barry::Controller
con(probe
.Get(activeDevice
), *router
);
272 Barry::Mode::RawChannel
rawChannel(con
, callbackHandler
);
274 // Try to open the requested channel now everything is setup
275 rawChannel
.Open(password
.c_str(), channelName
.c_str());
277 // We now have a thread running to read from the
278 // BB and write over stdout; in this thread we'll
279 // read from stdin and write to the BB.
280 const size_t bufSize
= rawChannel
.MaximumSendSize();
281 buf
= new unsigned char[bufSize
];
286 // Set up the signal restorers to restore signal
287 // handling (in their destructors) before the socket
288 // starts to be closed. This allows, for example,
289 // double control-c presses to stop graceful close
291 SignalRestorer
srh(SIGHUP
, oldSigHup
);
292 SignalRestorer
srt(SIGTERM
, oldSigTerm
);
293 SignalRestorer
sri(SIGINT
, oldSigInt
);
294 SignalRestorer
srq(SIGQUIT
, oldSigQuit
);
296 while( running
&& !signalReceived
) {
297 FD_SET(STDIN_FILENO
, &rfds
);
298 tv
.tv_sec
= READ_TIMEOUT_SECONDS
;
301 int ret
= select(STDIN_FILENO
+ 1, &rfds
, NULL
, NULL
, &tv
);
303 cerr
<< "Select failed with errno: " << errno
<< endl
;
306 else if ( ret
&& FD_ISSET(STDIN_FILENO
, &rfds
) ) {
307 ssize_t haveRead
= read(STDIN_FILENO
, buf
, bufSize
);
309 Data
toWrite(buf
, haveRead
);
311 cerr
.setf(ios::dec
, ios::basefield
);
312 cerr
<< "Sending " << haveRead
<< " bytes stdin->USB\n";
314 toWrite
.DumpHex(cerr
);
317 rawChannel
.Send(toWrite
);
319 cerr
.setf(ios::dec
, ios::basefield
);
320 cerr
<< "Sent " << haveRead
<< " bytes stdin->USB\n";
323 else if( haveRead
< 0 ) {
329 catch( Usb::Error
&ue
) {
330 cerr
<< "Usb::Error caught: " << ue
.what() << endl
;
333 catch( Barry::Error
&se
) {
334 cerr
<< "Barry::Error caught: " << se
.what() << endl
;
337 catch( exception
&e
) {
338 cerr
<< "exception caught: " << e
.what() << endl
;