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 << " -P pass Simplistic method to specify device password\n"
58 << " -s Use Serial mode instead of IpModem\n"
59 << " -v Dump protocol data during operation (debugging only!)\n"
63 void signal_handler(int signum
)
68 void SerialDataCallback(void *context
, const unsigned char *data
, int len
)
70 if( len
&& data_dump
)
71 barryverbose("ReadThread:\n" << Data(data
, len
));
74 int written
= write(1, data
, len
);
80 barryverbose("Error in write()");
85 void ProcessStdin(Modem
&modem
)
87 // Read from stdin and write to USB, until
95 // Handle interrupt signals from pppd
97 signal(SIGINT
, &signal_handler
);
98 signal(SIGHUP
, &signal_handler
);
99 signal(SIGTERM
, &signal_handler
);
102 while( signal_end
== false ) {
103 // Need to use select() here, so that pppd doesn't
104 // hang when it tries to set the line discipline
111 ret
= select(1, &rfds
, NULL
, NULL
, &tv
);
115 else if( ret
&& FD_ISSET(0, &rfds
) ) {
116 bytes_read
= read(0, data
.GetBuffer(), data
.GetBufSize());
117 if( bytes_read
== 0 )
120 if( bytes_read
> 0 ) {
121 data
.ReleaseBuffer(bytes_read
);
128 int main(int argc
, char *argv
[])
130 cout
.sync_with_stdio(true); // leave this on, since libusb uses
131 // stdio for debug messages
136 bool force_serial
= false;
138 std::string password
;
140 // process command line options
142 int cmd
= getopt(argc
, argv
, "l:p:P:sv");
148 case 'l': // Verbose log file
152 case 'p': // Blackberry PIN
153 pin
= strtoul(optarg
, NULL
, 16);
156 case 'P': // Device password
160 case 's': // Use Serial mode
164 case 'v': // data dump on
175 // Initialize the barry library. Must be called before
177 // Log to stderr, since stdout is for data in this program.
178 std::auto_ptr
<std::ofstream
> log
;
179 if( logfile
.size() ) {
180 log
.reset( new std::ofstream(logfile
.c_str(), ios::app
) );
181 Barry::Init(data_dump
, log
.get());
184 Barry::Init(data_dump
, &std::cerr
);
187 // Probe the USB bus for Blackberry devices and display.
188 // If user has specified a PIN, search for it in the
189 // available device list here as well
191 int activeDevice
= probe
.FindActive(pin
);
192 if( activeDevice
== -1 ) {
194 cerr
<< "PIN " << setbase(16) << pin
195 << " not found" << endl
;
196 cerr
<< "No device selected" << endl
;
200 const ProbeResult
&device
= probe
.Get(activeDevice
);
202 if( !force_serial
&& device
.HasIpModem() ) {
203 barryverbose("Using IpModem mode...");
205 // Create our controller object using our threaded router.
206 Controller
con(probe
.Get(activeDevice
));
208 // Open serial mode... the callback handles reading from
209 // USB and writing to stdout
210 Mode::IpModem
modem(con
, SerialDataCallback
, 0);
211 modem
.Open(password
.c_str());
214 modem
.Close(); // graceful close so we can restart without unplugging
217 barryverbose("Using Serial mode...");
219 // Create our socket router and start thread to handle
220 // the USB reading, instead of creating our own thread.
221 SocketRoutingQueue router
;
222 router
.SpinoffSimpleReadThread();
224 // Create our controller object using our threaded router.
225 Controller
con(probe
.Get(activeDevice
), router
);
227 // Open desktop mode... this handles the password side
229 Mode::Desktop
desktop(con
);
230 desktop
.Open(password
.c_str());
232 // Open serial connection
233 Mode::Serial
modem(con
, SerialDataCallback
, 0);
239 barryverbose("Exiting");
242 catch( std::exception
&e
) {
243 cerr
<< "exception caught in main(): " << e
.what() << endl
;