From f673cfa526289fc42bcbf13289b351b5d430043c Mon Sep 17 00:00:00 2001 From: Toby Gray Date: Tue, 17 Aug 2010 15:24:08 +0100 Subject: [PATCH] Renaming raw sockets to raw channels to follow device side API naming. --- man/Makefile.am | 1 + man/brawchannel.1 | 64 ++++++++ src/Makefile.am | 4 +- src/barry.h | 2 +- src/controller.cc | 6 +- src/controller.h | 2 +- src/{m_raw_socket.cc => m_raw_channel.cc} | 67 ++++---- src/m_raw_channel.h | 98 ++++++++++++ src/m_raw_socket.h | 89 ----------- tools/Makefile.am | 6 +- tools/brawchannel.cc | 253 +++++++++++++++++++++++++++++ tools/brawsocket.cc | 255 ------------------------------ 12 files changed, 461 insertions(+), 386 deletions(-) create mode 100644 man/brawchannel.1 rename src/{m_raw_socket.cc => m_raw_channel.cc} (51%) create mode 100644 src/m_raw_channel.h delete mode 100644 src/m_raw_socket.h create mode 100644 tools/brawchannel.cc delete mode 100644 tools/brawsocket.cc diff --git a/man/Makefile.am b/man/Makefile.am index a0a4af58..2504d7d3 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -7,6 +7,7 @@ dist_man_MANS = \ bs11nread.1 \ bidentify.1 \ breset.1 \ + brawchannel.1 \ pppob.1 \ brecsum.1 \ upldif.1 diff --git a/man/brawchannel.1 b/man/brawchannel.1 new file mode 100644 index 00000000..fb92cc38 --- /dev/null +++ b/man/brawchannel.1 @@ -0,0 +1,64 @@ +.\" Hey, EMACS: -*- nroff -*- +.\" First parameter, NAME, should be all caps +.\" Second parameter, SECTION, should be 1-8, maybe w/ subsection +.\" other parameters are allowed: see man(7), man(1) +.TH BRAWCHANNEL 1 "August 17, 2010" +.\" Please adjust this date whenever revising the manpage. +.\" +.\" Some roff macros, for reference: +.\" .nh disable hyphenation +.\" .hy enable hyphenation +.\" .ad l left justify +.\" .ad b justify to both left and right margins +.\" .nf disable filling +.\" .fi enable filling +.\" .br insert line break +.\" .sp insert n+1 empty lines +.\" for manpage-specific macros, see man(7) +.SH NAME +.B brawchannel +\- Barry Project's program to create channel to BlackBerry applications +.SH SYNOPSIS +.B brawchannel [-h][-p pin][-P pass][-v] +.SH DESCRIPTION +.PP +.B brawchannel +is a tool for connecting to an application channel on a BlackBerry device, +as provided to on device programs by the +.B net.rim.device.api.system.USBPort +API. Data received on the channel will be output to STDOUT and any data +received on STDIN in will be sent over the channel. Any errors or verbose +output will be written to STDERR. +.BR +The +.B channel name +provided to this program should be the same as the channel name passed +to the call to +.B net.rim.device.api.system.USBPort.registerChannel +.SH OPTIONS +.TP +.B \-h +Display a command help summary. +.TP +.B \-p pin +Specifies the PIN number of the device with which you wish to communicate, +when multiple devices are connected. +.TP +.B \-P password +A simplistic method to specify the device password. +.TP +.B \-v +Verbose debug output. This enables dumping of USB bus scanning, as +well as the protocol packets used during communication. +Unlike other Barry Project tools, this -v mode won't enable logging from +libusb, due to libusb's use of STDOUT. + +.SH AUTHOR +.nh +.B brawchannel +was initially written for the Barry project by RealVNC Ltd., +based on bjavaloader by Nicolas Vivien. +.SH SEE ALSO +.PP +http://www.netdirect.ca/software/packages/barry + diff --git a/src/Makefile.am b/src/Makefile.am index d6be97c0..0e87dd69 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -82,7 +82,7 @@ include_barry_HEADERS = barry.h barrysync.h \ controller.h \ m_mode_base.h \ m_desktop.h \ - m_raw_socket.h \ + m_raw_channel.h \ m_desktoptmpl.h \ m_ipmodem.h \ m_serial.h \ @@ -195,7 +195,7 @@ libbarry_la_SOURCES = dll.h \ controller.h controller.cc \ m_mode_base.h m_mode_base.cc \ m_desktop.h m_desktop.cc \ - m_raw_socket.h m_raw_socket.cc \ + m_raw_channel.h m_raw_channel.cc \ m_ipmodem.h m_ipmodem.cc \ m_serial.h m_serial.cc \ m_javaloader.h m_javaloader.cc \ diff --git a/src/barry.h b/src/barry.h index 0eb906c4..8284d716 100644 --- a/src/barry.h +++ b/src/barry.h @@ -65,7 +65,7 @@ #include "m_ipmodem.h" #include "m_serial.h" #include "m_javaloader.h" -#include "m_raw_socket.h" +#include "m_raw_channel.h" #include "m_jvmdebug.h" #include "version.h" #include "log.h" diff --git a/src/controller.cc b/src/controller.cc index 8e4c00b9..f2cb57bf 100644 --- a/src/controller.cc +++ b/src/controller.cc @@ -166,7 +166,7 @@ Controller::~Controller() // Otherwise the default mode name for the given mode will be used. // It should be a nul terminated string if it is provided. // -// The RawSocket mode requires an explicitModeName to be specified. +// The RawChannel mode requires an explicitModeName to be specified. // uint16_t Controller::SelectMode(ModeType mode, const char *explicitModeName) { @@ -213,6 +213,10 @@ Controller::~Controller() strcpy(modeName, "RIM_UsbSerCtrl"); break; + case RawChannel: + throw std::logic_error("Controller: No channel name given with RawChannel mode"); + break; + default: throw std::logic_error("Controller: Invalid mode in SelectMode"); break; diff --git a/src/controller.h b/src/controller.h index ea296a49..35ca2f93 100644 --- a/src/controller.h +++ b/src/controller.h @@ -86,7 +86,7 @@ public: JVMDebug, //< experimental UsbSerData, //< GPRS modem support over USB UsbSerCtrl, //< internally used behind the scenes - RawSocket //< raw socket + RawChannel //< raw channel }; private: diff --git a/src/m_raw_socket.cc b/src/m_raw_channel.cc similarity index 51% rename from src/m_raw_socket.cc rename to src/m_raw_channel.cc index 50e5e069..73884c2f 100644 --- a/src/m_raw_socket.cc +++ b/src/m_raw_channel.cc @@ -1,6 +1,6 @@ /// -/// \file m_raw_socket.cc -/// Mode class for the raw socket +/// \file m_raw_channel.cc +/// Mode class for a raw channel /// /* @@ -20,7 +20,7 @@ root directory of this project for more details. */ -#include "m_raw_socket.h" +#include "m_raw_channel.h" #include "data.h" #include "protocol.h" #include "protostructs.h" @@ -37,58 +37,57 @@ namespace Barry { namespace Mode { -static void HandleReceivedDataCallback(void* ctx, Data* data) -{ - ((RawSocket*)ctx)->HandleReceivedData(*data); +static void HandleReceivedDataCallback(void* ctx, Data* data) { + ((RawChannel*)ctx)->HandleReceivedData(*data); } /////////////////////////////////////////////////////////////////////////////// -// RawSocket Mode class +// RawChannel Mode class -RawSocket::RawSocket(Controller &con, RawSocketDataCallback& callback) - : Mode(con, Controller::RawSocket), +RawChannel::RawChannel(Controller &con, RawChannelDataCallback& callback) + : Mode(con, Controller::RawChannel), Callback(callback) { } -RawSocket::~RawSocket() +RawChannel::~RawChannel() { } -void RawSocket::OnOpen() +void RawChannel::OnOpen() { - m_socket->RegisterInterest(HandleReceivedDataCallback, this); + m_socket->RegisterInterest(HandleReceivedDataCallback, this); } /////////////////////////////////////////////////////////////////////////////// // public API -void RawSocket::Send(Data& data) +void RawChannel::Send(Data& data) { - Data toReceive; - try - { - size_t packetSize = 4 + data.GetSize(); - Barry::Protocol::Packet* packet = (Barry::Protocol::Packet*)m_sendBuffer; - packet->socket = htobs(m_socket->GetSocket()); - packet->size = htobs(packetSize); - std::memcpy(&(m_sendBuffer[4]), data.GetData(), data.GetSize()); - - Data toSend(m_sendBuffer, packetSize); - m_socket->PacketData(toSend, toReceive, 0); // timeout immediately - if (toReceive.GetSize() != 0) - HandleReceivedData(toReceive); - } - catch (Usb::Error& err) - { - } + Data toReceive; + try + { + size_t packetSize = HeaderSize + data.GetSize(); + Barry::Protocol::Packet* packet = (Barry::Protocol::Packet*)m_sendBuffer; + packet->socket = htobs(m_socket->GetSocket()); + packet->size = htobs(packetSize); + std::memcpy(&(m_sendBuffer[HeaderSize]), data.GetData(), data.GetSize()); + + Data toSend(m_sendBuffer, packetSize); + m_socket->PacketData(toSend, toReceive, 0); // timeout immediately + if (toReceive.GetSize() != 0) + HandleReceivedData(toReceive); + } + catch (Usb::Error& err) + { + } } -void RawSocket::HandleReceivedData(Data& data) +void RawChannel::HandleReceivedData(Data& data) { - // Remove packet headers - Data partial(data.GetData() + 4, data.GetSize() - 4); - Callback.DataReceived(partial); + // Remove packet headers + Data partial(data.GetData() + HeaderSize, data.GetSize() - HeaderSize); + Callback.DataReceived(partial); } }} // namespace Barry::Mode diff --git a/src/m_raw_channel.h b/src/m_raw_channel.h new file mode 100644 index 00000000..ec519f44 --- /dev/null +++ b/src/m_raw_channel.h @@ -0,0 +1,98 @@ +/// +/// \file m_raw_channel.h +/// Mode class for a raw channel +/// + +/* + Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/) + Portions Copyright (C) 2010 RealVNC Ltd. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU General Public License in the COPYING file at the + root directory of this project for more details. +*/ + +#ifndef __BARRY_M_RAW_CHANNEL_H__ +#define __BARRY_M_RAW_CHANNEL_H__ + +#include "dll.h" +#include "m_mode_base.h" +#include "socket.h" + +namespace Barry { + +namespace Mode { + +// Callback from the raw channel. + +class BXEXPORT RawChannelDataCallback +{ +public: + virtual void DataReceived(Data& data) = 0; + virtual ~RawChannelDataCallback() {}; +}; + +// +// Raw channel class +// +/// The main class for creating a raw channel session. +/// +/// To use this class, use the following steps: +/// +/// - Implement RawChannelDataCallback +/// - Create a Controller object (see Controller class for more details) +/// - Create this Mode::RawChannel object, passing in the Controller +/// object during construction +/// - Call Open() to open the channel and finish constructing. +/// - Call GetData() to fetch data +/// - Call SendData() to send data +/// +class BXEXPORT RawChannel : public Mode +{ +public: + RawChannel(Controller &con, RawChannelDataCallback& callback); + ~RawChannel(); + + ////////////////////////////////// + // Raw channel mode specific methods + + // Send some data on the raw channel + // Will throw an error if data is longer than + // RAW_CHANNEL_MAXIMUM_PACKET_CONTENTS_SIZE + void Send(Data& data); + + void HandleReceivedData(Data& data); + + void OnOpen(); + +private: + // Private constants + enum { + HeaderSize = 4, + MaximumPacketSize = 16384 + }; + +public: + // Public constants + enum { + MaximumPacketContentsSize = MaximumPacketSize - HeaderSize + }; + +private: + RawChannelDataCallback& Callback; + unsigned char m_sendBuffer[MaximumPacketSize]; + +}; + +}} // namespace Barry::Mode + +#endif + diff --git a/src/m_raw_socket.h b/src/m_raw_socket.h deleted file mode 100644 index 266bbe0f..00000000 --- a/src/m_raw_socket.h +++ /dev/null @@ -1,89 +0,0 @@ -/// -/// \file m_raw_socket.h -/// Mode class for a raw socket -/// - -/* - Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/) - Portions Copyright (C) 2010 RealVNC Ltd. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - - See the GNU General Public License in the COPYING file at the - root directory of this project for more details. -*/ - -#ifndef __BARRY_M_RAW_SOCKET_H__ -#define __BARRY_M_RAW_SOCKET_H__ - -#include "dll.h" -#include "m_mode_base.h" -#include "socket.h" - -namespace Barry { - -namespace Mode { - -// Callback from the raw socket. - -class BXEXPORT RawSocketDataCallback -{ -public: - virtual void DataReceived(Data& data) = 0; - virtual ~RawSocketDataCallback() {}; -}; - -#define RAW_SOCKET_HEADER_SIZE 4 -#define RAW_SOCKET_MAXIMUM_PACKET_SIZE 16384 -#define RAW_SOCKET_MAXIMUM_PACKET_CONTENTS_SIZE (RAW_SOCKET_MAXIMUM_PACKET_SIZE - RAW_SOCKET_HEADER_SIZE) - -// -// Raw socket class -// -/// The main class for creating a raw socket session. -/// -/// To use this class, use the following steps: -/// -/// - Implement RawSocketDataCallback -/// - Create a Controller object (see Controller class for more details) -/// - Create this Mode::RawSocket object, passing in the Controller -/// object during construction -/// - Call Open() to open database socket and finish constructing. -/// - Call GetData() to fetch data -/// - Call SendData() to send data -/// -class BXEXPORT RawSocket : public Mode -{ -public: - RawSocket(Controller &con, RawSocketDataCallback& callback); - ~RawSocket(); - - ////////////////////////////////// - // Raw Socket mode specific methods - - // Send some data on the raw socket - // Will throw an error if data is longer than - // RAW_SOCKET_MAXIMUM_PACKET_CONTENTS_SIZE - void Send(Data& data); - - void HandleReceivedData(Data& data); - - void OnOpen(); - -private: - RawSocketDataCallback& Callback; - unsigned char m_sendBuffer[RAW_SOCKET_MAXIMUM_PACKET_SIZE]; - -}; - -}} // namespace Barry::Mode - -#endif - diff --git a/tools/Makefile.am b/tools/Makefile.am index 54b98cff..7783c6ef 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -17,7 +17,7 @@ bin_PROGRAMS = \ btool \ bidentify \ bjavaloader \ - brawsocket \ + brawchannel \ bjvmdebug \ bdptest \ bjdwp \ @@ -62,8 +62,8 @@ bidentify_LDADD = ../src/libbarry.la $(LIBUSB_LIBS) -lpthread $(LTLIBINTL) bjavaloader_SOURCES = bjavaloader.cc bjavaloader_LDADD = ../src/libbarry.la $(LIBUSB_LIBS) -lpthread $(LTLIBINTL) -brawsocket_SOURCES = brawsocket.cc -brawsocket_LDADD = ../src/libbarry.la $(LIBUSB_LIBS) -lpthread +brawchannel_SOURCES = brawchannel.cc +brawchannel_LDADD = ../src/libbarry.la $(LIBUSB_LIBS) -lpthread bjvmdebug_SOURCES = bjvmdebug.cc bjvmdebug_LDADD = ../src/libbarry.la $(LIBUSB_LIBS) -lpthread $(LTLIBINTL) diff --git a/tools/brawchannel.cc b/tools/brawchannel.cc new file mode 100644 index 00000000..94739bc8 --- /dev/null +++ b/tools/brawchannel.cc @@ -0,0 +1,253 @@ +/// +/// \file brawchannel.cc +/// +/// + +/* + Copyright (C) 2010, RealVNC Ltd. + + Some parts are inspired from bjavaloader.cc + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU General Public License in the COPYING file at the + root directory of this project for more details. +*/ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "i18n.h" + +using namespace std; +using namespace Barry; + +class StdoutWriter : public Barry::Mode::RawChannelDataCallback +{ +public: + StdoutWriter(bool& keepGoing, bool verbose) + : m_continuePtr(&keepGoing) + , m_verbose(verbose) + { + } + + + void DataReceived(Data& data) + { + if (m_verbose) + { + std::cerr << "From BB: "; + data.DumpHex(std::cerr); + std::cerr << "\n"; + } + + size_t toWrite = data.GetSize(); + size_t written = 0; + + while (written < toWrite && *m_continuePtr) + { + ssize_t writtenThisTime = write(STDOUT_FILENO, &(data.GetData()[written]), toWrite - written); + if (m_verbose) + cerr << "Written " << writtenThisTime << " bytes over stdout\n"; + std::fflush(stdout); + if (writtenThisTime < 0) + { + *m_continuePtr = false; + } + else + { + written += writtenThisTime; + } + } + } + +private: + bool* m_continuePtr; + bool m_verbose; +}; + +void Usage() +{ + int major, minor; + const char *Version = Barry::Version(major, minor); + + cerr + << "brawchannel - Command line USB Blackberry raw channel interface\n" + << " Copyright 2010, RealVNC Ltd.\n" + << " Using: " << Version << "\n" + << "\n" + << "Usage:\n" + << "brawchannel [options] \n" + << "\n" + << " -h This help\n" + << " -p pin PIN of device to talk with\n" + << " If only one device is plugged in, this flag is optional\n" + << " -P pass Simplistic method to specify device password\n" + << " -v Dump protocol data during operation\n" + << endl; +} + +int main(int argc, char *argv[]) +{ + INIT_I18N(PACKAGE); + + cout.sync_with_stdio(true); // leave this on, since libusb uses + // stdio for debug messages + + try { + uint32_t pin = 0; + bool data_dump = false; + string password; + vector params; + string busname; + string devname; + string iconvCharset; + Usb::EndpointPair epOverride; + + // process command line options + for(;;) { + int cmd = getopt(argc, argv, "hp:P:v"); + if( cmd == -1 ) + break; + + switch( cmd ) + { + case 'p': // Blackberry PIN + pin = strtoul(optarg, NULL, 16); + break; + + case 'P': // Device password + password = optarg; + break; + + case 'v': // data dump on + data_dump = true; + break; + + case 'h': // help + default: + Usage(); + return 0; + } + } + + argc -= optind; + argv += optind; + + if( argc < 1 ) { + cerr << "Error: Missing raw channel name." << endl; + Usage(); + return 1; + } + + // Fetch command from remaining arguments + string channelName = argv[0]; + argc --; + argv ++; + + // Put the remaining arguments into an array + for (; argc > 0; argc --, argv ++) { + params.push_back(string(argv[0])); + } + + // Initialize the barry library. Must be called before + // anything else. + Barry::Init(data_dump); + + // Probe the USB bus for Blackberry devices and display. + // If user has specified a PIN, search for it in the + // available device list here as well + Barry::Probe probe; + int activeDevice = probe.FindActive(pin); + if( activeDevice == -1 ) { + cerr << "No device selected, or PIN not found" << endl; + return 1; + } + + // Now start to read from cin and get ready to write + // to the BlackBerry. + if (data_dump) + std::cerr << "Connected to device, starting read/write\n"; + + bool running = true; + + // Create the thing which will write onto stdout + StdoutWriter stdoutWriter(running, data_dump); + + // Set up the BlackBerry gubbins + // Start a thread to handle any data arriving from + // the BlackBerry. + auto_ptr router; + router.reset(new SocketRoutingQueue); + router->SpinoffSimpleReadThread(); + + // Create our controller object + Barry::Controller con(probe.Get(activeDevice), *router); + Barry::Mode::RawChannel rawChannel(con, stdoutWriter); + + // + // execute each mode that was turned on + // + rawChannel.Open(password.c_str(), channelName.c_str()); + + // We now have a thread running to read from the + // BB and write over stdout; in this thread we'll + // read from stdin and write to the BB. + unsigned char buf[Barry::Mode::RawChannel::MaximumPacketContentsSize]; + while (running) + { + size_t haveRead = read(STDIN_FILENO, buf, sizeof(buf)); + if (haveRead > 0) + { + Data toWrite(buf, haveRead); + if(data_dump) + { + std::cerr << "Sending " << haveRead << " bytes stdin->USB\n"; + std::cerr << "To BB: "; + toWrite.DumpHex(std::cerr); + std::cerr << "\n"; + } + rawChannel.Send(toWrite); + if(data_dump) + std::cerr << "Sent " << haveRead << " bytes stdin->USB\n"; + } + else if (haveRead < 0 || !std::cin.good()) + { + running = false; + } + } + } + catch( Usb::Error &ue) { + std::cerr << "Usb::Error caught: " << ue.what() << endl; + return 1; + } + catch( Barry::Error &se ) { + std::cerr << "Barry::Error caught: " << se.what() << endl; + return 1; + } + catch( std::exception &e ) { + std::cerr << "std::exception caught: " << e.what() << endl; + return 1; + } + + return 0; +} + diff --git a/tools/brawsocket.cc b/tools/brawsocket.cc deleted file mode 100644 index c52ec81b..00000000 --- a/tools/brawsocket.cc +++ /dev/null @@ -1,255 +0,0 @@ -/// -/// \file brawsocket.cc -/// -/// - -/* - Copyright (C) 2010, RealVNC Ltd. - - Some parts are inspired from bjavaloader.cc - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - - See the GNU General Public License in the COPYING file at the - root directory of this project for more details. -*/ - - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "i18n.h" - -using namespace std; -using namespace Barry; - -#define READ_BUF_SIZE 16384 - -class StdoutWriter : public Barry::Mode::RawSocketDataCallback -{ -public: - StdoutWriter(bool& keepGoing, bool verbose) - : m_continuePtr(&keepGoing) - , m_verbose(verbose) - { - } - - - void DataReceived(Data& data) - { - if (m_verbose) - { - std::cerr << "From BB: "; - data.DumpHex(std::cerr); - std::cerr << "\n"; - } - - size_t toWrite = data.GetSize(); - size_t written = 0; - - while (written < toWrite && *m_continuePtr) - { - ssize_t writtenThisTime = write(STDOUT_FILENO, &(data.GetData()[written]), toWrite - written); - if (m_verbose) - cerr << "Written " << writtenThisTime << " bytes over stdout\n"; - std::fflush(stdout); - if (writtenThisTime < 0) - { - *m_continuePtr = false; - } - else - { - written += writtenThisTime; - } - } - } - -private: - bool* m_continuePtr; - bool m_verbose; -}; - -void Usage() -{ - int major, minor; - const char *Version = Barry::Version(major, minor); - - cerr - << "brawsocket - Command line USB Blackberry raw socket interface\n" - << " Copyright 2010, RealVNC Ltd.\n" - << " Using: " << Version << "\n" - << "\n" - << " -h This help\n" - << " -p pin PIN of device to talk with\n" - << " If only one device is plugged in, this flag is optional\n" - << " -P pass Simplistic method to specify device password\n" - << " -v Dump protocol data during operation\n" - << "\n" - << "Usage:\n" - << "brawsocket [options] \n" - << endl; -} - -int main(int argc, char *argv[]) -{ - INIT_I18N(PACKAGE); - - cout.sync_with_stdio(true); // leave this on, since libusb uses - // stdio for debug messages - - try { - uint32_t pin = 0; - bool data_dump = false; - string password; - vector params; - string busname; - string devname; - string iconvCharset; - Usb::EndpointPair epOverride; - - // process command line options - for(;;) { - int cmd = getopt(argc, argv, "hp:P:v"); - if( cmd == -1 ) - break; - - switch( cmd ) - { - case 'p': // Blackberry PIN - pin = strtoul(optarg, NULL, 16); - break; - - case 'P': // Device password - password = optarg; - break; - - case 'v': // data dump on - data_dump = true; - break; - - case 'h': // help - default: - Usage(); - return 0; - } - } - - argc -= optind; - argv += optind; - - if( argc < 1 ) { - cerr << "Error: Missing raw socket name." << endl; - Usage(); - return 1; - } - - // Fetch command from remaining arguments - string socketName = argv[0]; - argc --; - argv ++; - - // Put the remaining arguments into an array - for (; argc > 0; argc --, argv ++) { - params.push_back(string(argv[0])); - } - - // Initialize the barry library. Must be called before - // anything else. - Barry::Init(data_dump); - - // Probe the USB bus for Blackberry devices and display. - // If user has specified a PIN, search for it in the - // available device list here as well - Barry::Probe probe; - int activeDevice = probe.FindActive(pin); - if( activeDevice == -1 ) { - cerr << "No device selected, or PIN not found" << endl; - return 1; - } - - // Now start to read from cin and get ready to write - // to the BlackBerry. - if (data_dump) - std::cerr << "Connected to device, starting read/write\n"; - - bool running = true; - - // Create the thing which will write onto stdout - StdoutWriter stdoutWriter(running, data_dump); - - // Set up the BlackBerry gubbins - // Start a thread to handle any data arriving from - // the BlackBerry. - auto_ptr router; - router.reset(new SocketRoutingQueue); - router->SpinoffSimpleReadThread(); - - // Create our controller object - Barry::Controller con(probe.Get(activeDevice), *router); - Barry::Mode::RawSocket rawSocket(con, stdoutWriter); - - // - // execute each mode that was turned on - // - rawSocket.Open(password.c_str(), socketName.c_str()); - - // We now have a thread running to read from the - // BB and write over stdout; in this thread we'll - // read from stdin and write to the BB. - unsigned char buf[READ_BUF_SIZE]; - while (running) - { - size_t haveRead = read(STDIN_FILENO, buf, READ_BUF_SIZE); - if (haveRead > 0) - { - Data toWrite(buf, haveRead); - if(data_dump) - { - std::cerr << "Sending " << haveRead << " bytes stdin->USB\n"; - std::cerr << "To BB: "; - toWrite.DumpHex(std::cerr); - std::cerr << "\n"; - } - rawSocket.Send(toWrite); - if(data_dump) - std::cerr << "Sent " << haveRead << " bytes stdin->USB\n"; - } - else if (haveRead < 0 || !std::cin.good()) - { - running = false; - } - } - } - catch( Usb::Error &ue) { - std::cerr << "Usb::Error caught: " << ue.what() << endl; - return 1; - } - catch( Barry::Error &se ) { - std::cerr << "Barry::Error caught: " << se.what() << endl; - return 1; - } - catch( std::exception &e ) { - std::cerr << "std::exception caught: " << e.what() << endl; - return 1; - } - - return 0; -} - -- 2.11.4.GIT