2 /// \file brawsocket.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 #define READ_BUF_SIZE 16384
45 class StdoutWriter
: public Barry::Mode::RawSocketDataCallback
48 StdoutWriter(bool& keepGoing
, bool verbose
)
49 : m_continuePtr(&keepGoing
)
55 void DataReceived(Data
& data
)
59 std::cerr
<< "From BB: ";
60 data
.DumpHex(std::cerr
);
64 size_t toWrite
= data
.GetSize();
67 while (written
< toWrite
&& *m_continuePtr
)
69 ssize_t writtenThisTime
= write(STDOUT_FILENO
, &(data
.GetData()[written
]), toWrite
- written
);
71 cerr
<< "Written " << writtenThisTime
<< " bytes over stdout\n";
73 if (writtenThisTime
< 0)
75 *m_continuePtr
= false;
79 written
+= writtenThisTime
;
92 const char *Version
= Barry::Version(major
, minor
);
95 << "brawsocket - Command line USB Blackberry raw socket interface\n"
96 << " Copyright 2010, RealVNC Ltd.\n"
97 << " Using: " << Version
<< "\n"
100 << " -p pin PIN of device to talk with\n"
101 << " If only one device is plugged in, this flag is optional\n"
102 << " -P pass Simplistic method to specify device password\n"
103 << " -v Dump protocol data during operation\n"
106 << "brawsocket [options] <socket name>\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 socket name." << endl
;
163 // Fetch command from remaining arguments
164 string socketName
= 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::RawSocket
rawSocket(con
, stdoutWriter
);
209 // execute each mode that was turned on
211 rawSocket
.Open(password
.c_str(), socketName
.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
[READ_BUF_SIZE
];
219 size_t haveRead
= read(STDIN_FILENO
, buf
, READ_BUF_SIZE
);
222 Data
toWrite(buf
, haveRead
);
225 std::cerr
<< "Sending " << haveRead
<< " bytes stdin->USB\n";
226 std::cerr
<< "To BB: ";
227 toWrite
.DumpHex(std::cerr
);
230 rawSocket
.Send(toWrite
);
232 std::cerr
<< "Sent " << haveRead
<< " bytes stdin->USB\n";
234 else if (haveRead
< 0 || !std::cin
.good())
240 catch( Usb::Error
&ue
) {
241 std::cerr
<< "Usb::Error caught: " << ue
.what() << endl
;
244 catch( Barry::Error
&se
) {
245 std::cerr
<< "Barry::Error caught: " << se
.what() << endl
;
248 catch( std::exception
&e
) {
249 std::cerr
<< "std::exception caught: " << e
.what() << endl
;