Fixing readings over stdin not working. Connection still isn't 100% reliable yet...
[barry.git] / tools / brawsocket.cc
blobc52ec81b467797ff166c89cc41aa301ee8b40f26
1 ///
2 /// \file brawsocket.cc
3 ///
4 ///
6 /*
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>
26 #include <iostream>
27 #include <vector>
28 #include <string>
29 #include <cstring>
30 #include <cstdio>
31 #include <algorithm>
32 #include <getopt.h>
33 #include <fstream>
34 #include <string.h>
35 #include <unistd.h>
36 #include <sys/types.h>
38 #include "i18n.h"
40 using namespace std;
41 using namespace Barry;
43 #define READ_BUF_SIZE 16384
45 class StdoutWriter : public Barry::Mode::RawSocketDataCallback
47 public:
48 StdoutWriter(bool& keepGoing, bool verbose)
49 : m_continuePtr(&keepGoing)
50 , m_verbose(verbose)
55 void DataReceived(Data& data)
57 if (m_verbose)
59 std::cerr << "From BB: ";
60 data.DumpHex(std::cerr);
61 std::cerr << "\n";
64 size_t toWrite = data.GetSize();
65 size_t written = 0;
67 while (written < toWrite && *m_continuePtr)
69 ssize_t writtenThisTime = write(STDOUT_FILENO, &(data.GetData()[written]), toWrite - written);
70 if (m_verbose)
71 cerr << "Written " << writtenThisTime << " bytes over stdout\n";
72 std::fflush(stdout);
73 if (writtenThisTime < 0)
75 *m_continuePtr = false;
77 else
79 written += writtenThisTime;
84 private:
85 bool* m_continuePtr;
86 bool m_verbose;
89 void Usage()
91 int major, minor;
92 const char *Version = Barry::Version(major, minor);
94 cerr
95 << "brawsocket - Command line USB Blackberry raw socket interface\n"
96 << " Copyright 2010, RealVNC Ltd.\n"
97 << " Using: " << Version << "\n"
98 << "\n"
99 << " -h This help\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"
104 << "\n"
105 << "Usage:\n"
106 << "brawsocket [options] <socket name>\n"
107 << endl;
110 int main(int argc, char *argv[])
112 INIT_I18N(PACKAGE);
114 cout.sync_with_stdio(true); // leave this on, since libusb uses
115 // stdio for debug messages
117 try {
118 uint32_t pin = 0;
119 bool data_dump = false;
120 string password;
121 vector<string> params;
122 string busname;
123 string devname;
124 string iconvCharset;
125 Usb::EndpointPair epOverride;
127 // process command line options
128 for(;;) {
129 int cmd = getopt(argc, argv, "hp:P:v");
130 if( cmd == -1 )
131 break;
133 switch( cmd )
135 case 'p': // Blackberry PIN
136 pin = strtoul(optarg, NULL, 16);
137 break;
139 case 'P': // Device password
140 password = optarg;
141 break;
143 case 'v': // data dump on
144 data_dump = true;
145 break;
147 case 'h': // help
148 default:
149 Usage();
150 return 0;
154 argc -= optind;
155 argv += optind;
157 if( argc < 1 ) {
158 cerr << "Error: Missing raw socket name." << endl;
159 Usage();
160 return 1;
163 // Fetch command from remaining arguments
164 string socketName = argv[0];
165 argc --;
166 argv ++;
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
174 // anything else.
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
180 Barry::Probe probe;
181 int activeDevice = probe.FindActive(pin);
182 if( activeDevice == -1 ) {
183 cerr << "No device selected, or PIN not found" << endl;
184 return 1;
187 // Now start to read from cin and get ready to write
188 // to the BlackBerry.
189 if (data_dump)
190 std::cerr << "Connected to device, starting read/write\n";
192 bool running = true;
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
199 // the BlackBerry.
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];
217 while (running)
219 size_t haveRead = read(STDIN_FILENO, buf, READ_BUF_SIZE);
220 if (haveRead > 0)
222 Data toWrite(buf, haveRead);
223 if(data_dump)
225 std::cerr << "Sending " << haveRead << " bytes stdin->USB\n";
226 std::cerr << "To BB: ";
227 toWrite.DumpHex(std::cerr);
228 std::cerr << "\n";
230 rawSocket.Send(toWrite);
231 if(data_dump)
232 std::cerr << "Sent " << haveRead << " bytes stdin->USB\n";
234 else if (haveRead < 0 || !std::cin.good())
236 running = false;
240 catch( Usb::Error &ue) {
241 std::cerr << "Usb::Error caught: " << ue.what() << endl;
242 return 1;
244 catch( Barry::Error &se ) {
245 std::cerr << "Barry::Error caught: " << se.what() << endl;
246 return 1;
248 catch( std::exception &e ) {
249 std::cerr << "std::exception caught: " << e.what() << endl;
250 return 1;
253 return 0;