3 /// In the same vein as pppoe, used with pppd to create a
4 /// pty tunnel and GPRS modem link.
8 Copyright (C) 2007-2012, 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>
42 #include "barrygetopt.h"
45 using namespace Barry
;
47 bool data_dump
= false;
48 volatile bool signal_end
= false;
54 int logical
, major
, minor
;
55 const char *Version
= Barry::Version(logical
, major
, minor
);
58 << "pppob - PPP over Barry\n"
59 << " Copyright 2007-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
60 << " Using: " << Version
<< "\n"
62 << " -l file Direct pppob log output to file (useful with -v)\n"
63 << " -p pin PIN of device to talk with\n"
64 << " If only one device plugged in, this flag is optional\n"
65 << " -P pass Simplistic method to specify device password\n"
66 << " -s Use Serial mode instead of IpModem\n"
67 << " -t Use a pseudo-tty instead of stdin/stdout\n"
68 << " -v Dump protocol data during operation (debugging only!)\n"
72 void signal_handler(int signum
)
77 void SerialDataCallback(void *context
, const unsigned char *data
, int len
)
79 if( len
&& data_dump
)
80 barryverbose("ReadThread:\n" << Data(data
, len
));
83 int written
= write(write_fd
, data
, len
);
89 barryverbose("Error in write()");
94 void ProcessStdin(Modem
&modem
)
96 // Read from stdin and write to USB, until
104 // Handle interrupt signals from pppd
106 signal(SIGINT
, &signal_handler
);
107 signal(SIGHUP
, &signal_handler
);
108 signal(SIGTERM
, &signal_handler
);
111 while( signal_end
== false ) {
112 // Need to use select() here, so that pppd doesn't
113 // hang when it tries to set the line discipline
116 FD_SET(read_fd
, &rfds
);
120 ret
= select(read_fd
+1, &rfds
, NULL
, NULL
, &tv
);
124 else if( ret
&& FD_ISSET(read_fd
, &rfds
) ) {
125 bytes_read
= read(read_fd
, data
.GetBuffer(), data
.GetBufSize());
126 if( bytes_read
== 0 )
127 break; // end of file
128 else if( bytes_read
> 0 ) {
129 data
.ReleaseBuffer(bytes_read
);
134 barryverbose("Read error in ProcessStdin: " << strerror(errno
));
141 int main(int argc
, char *argv
[])
145 cout
.sync_with_stdio(true); // leave this on, since libusb uses
146 // stdio for debug messages
151 bool force_serial
= false,
154 std::string password
;
156 // check for options via the fifo first, so the command
157 // line args can override them
159 if( fifo
.Fetch(4) ) {
160 const FifoArgs
&args
= fifo
.GetArgs();
161 pin
= args
.m_pin
.Value();
162 force_serial
= args
.m_use_serial_mode
;
163 logfile
= args
.m_log_filename
;
164 password
= args
.m_password
;
165 data_dump
= args
.m_verbose
;
168 // process command line options
170 int cmd
= getopt(argc
, argv
, "l:p:P:stv");
176 case 'l': // Verbose log file
180 case 'p': // Blackberry PIN
181 pin
= strtoul(optarg
, NULL
, 16);
184 case 'P': // Device password
188 case 's': // Use Serial mode
192 case 't': // Use pseudo-tty
196 case 'v': // data dump on
208 // open pty/tty master to get slave
209 int master
= open("/dev/ptmx", O_RDWR
);
211 cerr
<< "Cannot open /dev/ptmx: " << strerror(errno
) << endl
;
215 // grant and unlock, as per pts(4) man page
216 if( grantpt(master
) == -1 ) {
217 cerr
<< "Warning: grantpt() failure: "
218 << strerror(errno
) << endl
;
220 if( unlockpt(master
) == -1 ) {
221 cerr
<< "Warning: unlockpt() failure: "
222 << strerror(errno
) << endl
;
227 tcgetattr(master
, &tp
);
229 tcsetattr(master
, TCSANOW
, &tp
);
231 // send name of slave to stdout
232 cout
<< ptsname(master
) << endl
;
234 // set the global fd's
239 // just default to stdin/stdout
244 // Initialize the barry library. Must be called before
246 // Log to stderr, since stdout is for data in this program.
247 std::auto_ptr
<std::ofstream
> log
;
248 if( logfile
.size() ) {
249 log
.reset( new std::ofstream(logfile
.c_str(), ios::app
) );
250 Barry::Init(data_dump
, log
.get());
253 Barry::Init(data_dump
, &std::cerr
);
256 // Display version if in data_dump mode
258 int logical
, major
, minor
;
259 const char *Version
= Barry::Version(logical
, major
, minor
);
260 barryverbose(Version
);
263 // Probe the USB bus for Blackberry devices and display.
264 // If user has specified a PIN, search for it in the
265 // available device list here as well
267 int activeDevice
= probe
.FindActive(pin
);
268 if( activeDevice
== -1 ) {
270 cerr
<< "PIN " << setbase(16) << pin
271 << " not found" << endl
;
272 cerr
<< "No device selected" << endl
;
276 const ProbeResult
&device
= probe
.Get(activeDevice
);
278 if( !force_serial
&& device
.HasIpModem() ) {
279 barryverbose("Using IpModem mode...");
281 // Create our controller object using our threaded router.
282 Controller
con(probe
.Get(activeDevice
));
284 // Open serial mode... the callback handles reading from
285 // USB and writing to stdout
286 Mode::IpModem
modem(con
, SerialDataCallback
, 0);
287 modem
.Open(password
.c_str());
290 modem
.Close(); // graceful close so we can restart without unplugging
294 barryverbose("Using Serial mode per command line...");
297 barryverbose("No IpModem mode available, using Serial mode...");
300 // Create our socket router and start thread to handle
301 // the USB reading, instead of creating our own thread.
302 SocketRoutingQueue router
;
303 router
.SpinoffSimpleReadThread();
305 // Create our controller object using our threaded router.
306 Controller
con(probe
.Get(activeDevice
), router
);
308 // Open desktop mode... this handles the password side
310 Mode::Desktop
desktop(con
);
311 desktop
.Open(password
.c_str());
313 // Open serial connection
314 Mode::Serial
modem(con
, SerialDataCallback
, 0);
315 modem
.Open(password
.c_str());
320 barryverbose("Exiting");
323 catch( std::exception
&e
) {
324 cerr
<< "exception caught in main(): " << e
.what() << endl
;