3 /// In the same vein as pppoe, used with pppd to create a
4 /// pty tunnel and GPRS modem link.
8 Copyright (C) 2007-2009, 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>
40 using namespace Barry
;
42 bool data_dump
= false;
43 volatile bool signal_end
= false;
48 const char *Version
= Barry::Version(major
, minor
);
51 << "pppob - PPP over Barry\n"
52 << " Copyright 2007-2009, Net Direct Inc. (http://www.netdirect.ca/)\n"
53 << " Using: " << Version
<< "\n"
55 << " -l file Direct pppob log output to file (useful with -v)\n"
56 << " -p pin PIN of device to talk with\n"
57 << " If only one device plugged in, this flag is optional\n"
58 << " -P pass Simplistic method to specify device password\n"
59 << " -s Use Serial mode instead of IpModem\n"
60 << " -v Dump protocol data during operation (debugging only!)\n"
64 void signal_handler(int signum
)
69 void SerialDataCallback(void *context
, const unsigned char *data
, int len
)
71 if( len
&& data_dump
)
72 barryverbose("ReadThread:\n" << Data(data
, len
));
75 int written
= write(1, data
, len
);
81 barryverbose("Error in write()");
86 void ProcessStdin(Modem
&modem
)
88 // Read from stdin and write to USB, until
96 // Handle interrupt signals from pppd
98 signal(SIGINT
, &signal_handler
);
99 signal(SIGHUP
, &signal_handler
);
100 signal(SIGTERM
, &signal_handler
);
103 while( signal_end
== false ) {
104 // Need to use select() here, so that pppd doesn't
105 // hang when it tries to set the line discipline
112 ret
= select(1, &rfds
, NULL
, NULL
, &tv
);
116 else if( ret
&& FD_ISSET(0, &rfds
) ) {
117 bytes_read
= read(0, data
.GetBuffer(), data
.GetBufSize());
118 if( bytes_read
== 0 )
121 if( bytes_read
> 0 ) {
122 data
.ReleaseBuffer(bytes_read
);
129 int main(int argc
, char *argv
[])
133 cout
.sync_with_stdio(true); // leave this on, since libusb uses
134 // stdio for debug messages
139 bool force_serial
= false;
141 std::string password
;
143 // process command line options
145 int cmd
= getopt(argc
, argv
, "l:p:P:sv");
151 case 'l': // Verbose log file
155 case 'p': // Blackberry PIN
156 pin
= strtoul(optarg
, NULL
, 16);
159 case 'P': // Device password
163 case 's': // Use Serial mode
167 case 'v': // data dump on
178 // Initialize the barry library. Must be called before
180 // Log to stderr, since stdout is for data in this program.
181 std::auto_ptr
<std::ofstream
> log
;
182 if( logfile
.size() ) {
183 log
.reset( new std::ofstream(logfile
.c_str(), ios::app
) );
184 Barry::Init(data_dump
, log
.get());
187 Barry::Init(data_dump
, &std::cerr
);
190 // Display version if in data_dump mode
193 const char *Version
= Barry::Version(major
, minor
);
194 barryverbose(Version
);
197 // Probe the USB bus for Blackberry devices and display.
198 // If user has specified a PIN, search for it in the
199 // available device list here as well
201 int activeDevice
= probe
.FindActive(pin
);
202 if( activeDevice
== -1 ) {
204 cerr
<< "PIN " << setbase(16) << pin
205 << " not found" << endl
;
206 cerr
<< "No device selected" << endl
;
210 const ProbeResult
&device
= probe
.Get(activeDevice
);
212 if( !force_serial
&& device
.HasIpModem() ) {
213 barryverbose("Using IpModem mode...");
215 // Create our controller object using our threaded router.
216 Controller
con(probe
.Get(activeDevice
));
218 // Open serial mode... the callback handles reading from
219 // USB and writing to stdout
220 Mode::IpModem
modem(con
, SerialDataCallback
, 0);
221 modem
.Open(password
.c_str());
224 modem
.Close(); // graceful close so we can restart without unplugging
228 barryverbose("Using Serial mode per command line...");
231 barryverbose("No IpModem mode available, using Serial mode...");
234 // Create our socket router and start thread to handle
235 // the USB reading, instead of creating our own thread.
236 SocketRoutingQueue router
;
237 router
.SpinoffSimpleReadThread();
239 // Create our controller object using our threaded router.
240 Controller
con(probe
.Get(activeDevice
), router
);
242 // Open desktop mode... this handles the password side
244 Mode::Desktop
desktop(con
);
245 desktop
.Open(password
.c_str());
247 // Open serial connection
248 Mode::Serial
modem(con
, SerialDataCallback
, 0);
249 modem
.Open(password
.c_str());
254 barryverbose("Exiting");
257 catch( std::exception
&e
) {
258 cerr
<< "exception caught in main(): " << e
.what() << endl
;