3 /// In the same vein as pppoe, used with pppd to create a
4 /// pty tunnel and GPRS modem link.
8 Copyright (C) 2007-2008, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include <barry/barry.h>
31 #include <sys/select.h>
33 #include <sys/types.h>
39 using namespace Barry
;
41 bool data_dump
= false;
42 volatile bool signal_end
= false;
47 const char *Version
= Barry::Version(major
, minor
);
50 << "pppob - PPP over Barry\n"
51 << " Copyright 2007-2008, Net Direct Inc. (http://www.netdirect.ca/)\n"
52 << " Using: " << Version
<< "\n"
54 << " -l file Direct pppob log output to file (useful with -v)\n"
55 << " -p pin PIN of device to talk with\n"
56 << " If only one device plugged in, this flag is optional\n"
57 << " -s Use Serial mode instead of IpModem\n"
58 << " -v Dump protocol data during operation (debugging only!)\n"
62 void signal_handler(int signum
)
67 void SerialDataCallback(void *context
, const unsigned char *data
, int len
)
69 if( len
&& data_dump
)
70 barryverbose("ReadThread:\n" << Data(data
, len
));
73 int written
= write(1, data
, len
);
79 barryverbose("Error in write()");
84 void ProcessStdin(Modem
&modem
)
86 // Read from stdin and write to USB, until
94 // Handle interrupt signals from pppd
96 signal(SIGINT
, &signal_handler
);
99 while( signal_end
== false ) {
100 // Need to use select() here, so that pppd doesn't
101 // hang when it tries to set the line discipline
108 ret
= select(1, &rfds
, NULL
, NULL
, &tv
);
112 else if( ret
&& FD_ISSET(0, &rfds
) ) {
113 bytes_read
= read(0, data
.GetBuffer(), data
.GetBufSize());
114 if( bytes_read
== 0 )
117 if( bytes_read
> 0 ) {
118 data
.ReleaseBuffer(bytes_read
);
125 int main(int argc
, char *argv
[])
127 cout
.sync_with_stdio(true); // leave this on, since libusb uses
128 // stdio for debug messages
133 bool force_serial
= false;
136 // process command line options
138 int cmd
= getopt(argc
, argv
, "l:p:sv");
144 case 'l': // Verbose log file
148 case 'p': // Blackberry PIN
149 pin
= strtoul(optarg
, NULL
, 16);
152 case 's': // Use Serial mode
156 case 'v': // data dump on
167 // Initialize the barry library. Must be called before
169 // Log to stderr, since stdout is for data in this program.
170 std::auto_ptr
<std::ofstream
> log
;
171 if( logfile
.size() ) {
172 log
.reset( new std::ofstream(logfile
.c_str(), ios::app
) );
173 Barry::Init(data_dump
, log
.get());
176 Barry::Init(data_dump
, &std::cerr
);
179 // Probe the USB bus for Blackberry devices and display.
180 // If user has specified a PIN, search for it in the
181 // available device list here as well
183 int activeDevice
= probe
.FindActive(pin
);
184 if( activeDevice
== -1 ) {
186 cerr
<< "PIN " << setbase(16) << pin
187 << " not found" << endl
;
188 cerr
<< "No device selected" << endl
;
192 const ProbeResult
&device
= probe
.Get(activeDevice
);
194 if( !force_serial
&& device
.HasIpModem() ) {
195 barryverbose("Using IpModem mode...");
197 // Create our controller object using our threaded router.
198 Controller
con(probe
.Get(activeDevice
));
200 // Open serial mode... the callback handles reading from
201 // USB and writing to stdout
202 Mode::IpModem
modem(con
, SerialDataCallback
, 0);
208 barryverbose("Using Serial mode...");
210 // Create our socket router and start thread to handle
211 // the USB reading, instead of creating our own thread.
212 SocketRoutingQueue router
;
213 router
.SpinoffSimpleReadThread();
215 // Create our controller object using our threaded router.
216 Controller
con(probe
.Get(activeDevice
), router
);
218 // Open desktop mode... this handles the password side
220 Mode::Desktop
desktop(con
);
221 desktop
.Open(); // FIXME - support password here?
223 // Open serial connection
224 Mode::Serial
modem(con
, SerialDataCallback
, 0);
231 barryverbose("Exiting");
234 catch( std::exception
&e
) {
235 cerr
<< "exception caught in main(): " << e
.what() << endl
;