3 /// In the same vein as pppoe, used with pppd to create a
4 /// pty tunnel and GPRS modem link.
8 Copyright (C) 2007-2013, 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
);
57 cerr
<< string_vprintf(
58 _("pppob - PPP over Barry\n"
59 " Copyright 2007-2013, Net Direct Inc. (http://www.netdirect.ca/)\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"),
73 void signal_handler(int signum
)
78 void SerialDataCallback(void *context
, const unsigned char *data
, int len
)
80 if( len
&& data_dump
)
81 barryverbose("ReadThread:\n" << Data(data
, len
));
84 int written
= write(write_fd
, data
, len
);
90 barryverbose(_("Error in write()"));
95 void ProcessStdin(Modem
&modem
)
97 // Read from stdin and write to USB, until
105 // Handle interrupt signals from pppd
107 signal(SIGINT
, &signal_handler
);
108 signal(SIGHUP
, &signal_handler
);
109 signal(SIGTERM
, &signal_handler
);
112 while( signal_end
== false ) {
113 // Need to use select() here, so that pppd doesn't
114 // hang when it tries to set the line discipline
117 FD_SET(read_fd
, &rfds
);
121 ret
= select(read_fd
+1, &rfds
, NULL
, NULL
, &tv
);
125 else if( ret
&& FD_ISSET(read_fd
, &rfds
) ) {
126 bytes_read
= read(read_fd
, data
.GetBuffer(), data
.GetBufSize());
127 if( bytes_read
== 0 )
128 break; // end of file
129 else if( bytes_read
> 0 ) {
130 data
.ReleaseBuffer(bytes_read
);
135 barryverbose(_("Read error in ProcessStdin: ") << strerror(errno
));
142 int main(int argc
, char *argv
[])
146 cout
.sync_with_stdio(true); // leave this on, since libusb uses
147 // stdio for debug messages
152 bool force_serial
= false,
155 std::string password
;
157 // check for options via the fifo first, so the command
158 // line args can override them
160 if( fifo
.Fetch(4) ) {
161 const FifoArgs
&args
= fifo
.GetArgs();
162 pin
= args
.m_pin
.Value();
163 force_serial
= args
.m_use_serial_mode
;
164 logfile
= args
.m_log_filename
;
165 password
= args
.m_password
;
166 data_dump
= args
.m_verbose
;
169 // process command line options
171 int cmd
= getopt(argc
, argv
, "l:p:P:stv");
177 case 'l': // Verbose log file
181 case 'p': // Blackberry PIN
182 pin
= strtoul(optarg
, NULL
, 16);
185 case 'P': // Device password
189 case 's': // Use Serial mode
193 case 't': // Use pseudo-tty
197 case 'v': // data dump on
209 // open pty/tty master to get slave
210 int master
= open("/dev/ptmx", O_RDWR
);
212 cerr
<< _("Cannot open /dev/ptmx: ") << strerror(errno
) << endl
;
216 // grant and unlock, as per pts(4) man page
217 if( grantpt(master
) == -1 ) {
218 cerr
<< _("Warning: grantpt() failure: ")
219 << strerror(errno
) << endl
;
221 if( unlockpt(master
) == -1 ) {
222 cerr
<< _("Warning: unlockpt() failure: ")
223 << strerror(errno
) << endl
;
228 tcgetattr(master
, &tp
);
230 tcsetattr(master
, TCSANOW
, &tp
);
232 // send name of slave to stdout
233 cout
<< ptsname(master
) << endl
;
235 // set the global fd's
240 // just default to stdin/stdout
245 // Initialize the barry library. Must be called before
247 // Log to stderr, since stdout is for data in this program.
248 std::auto_ptr
<std::ofstream
> log
;
249 if( logfile
.size() ) {
250 log
.reset( new std::ofstream(logfile
.c_str(), ios::app
) );
251 Barry::Init(data_dump
, log
.get());
254 Barry::Init(data_dump
, &std::cerr
);
257 // Display version if in data_dump mode
259 int logical
, major
, minor
;
260 const char *Version
= Barry::Version(logical
, major
, minor
);
261 barryverbose(Version
);
264 // Probe the USB bus for Blackberry devices and display.
265 // If user has specified a PIN, search for it in the
266 // available device list here as well
268 int activeDevice
= probe
.FindActive(pin
);
269 if( activeDevice
== -1 ) {
271 cerr
<< _("PIN not found: ")
272 << setbase(16) << pin
<< endl
;
273 cerr
<< _("No device selected") << endl
;
277 const ProbeResult
&device
= probe
.Get(activeDevice
);
279 if( !force_serial
&& device
.HasIpModem() ) {
280 barryverbose(_("Using IpModem mode..."));
282 // Create our controller object using our threaded router.
283 Controller
con(probe
.Get(activeDevice
));
285 // Open serial mode... the callback handles reading from
286 // USB and writing to stdout
287 Mode::IpModem
modem(con
, SerialDataCallback
, 0);
288 modem
.Open(password
.c_str());
291 modem
.Close(); // graceful close so we can restart without unplugging
295 barryverbose(_("Using Serial mode per command line..."));
298 barryverbose(_("No IpModem mode available, using Serial mode..."));
301 // Create our socket router and start thread to handle
302 // the USB reading, instead of creating our own thread.
303 SocketRoutingQueue router
;
304 router
.SpinoffSimpleReadThread();
306 // Create our controller object using our threaded router.
307 Controller
con(probe
.Get(activeDevice
), router
);
309 // Open desktop mode... this handles the password side
311 Mode::Desktop
desktop(con
);
312 desktop
.Open(password
.c_str());
314 // Open serial connection
315 Mode::Serial
modem(con
, SerialDataCallback
, 0);
316 modem
.Open(password
.c_str());
321 barryverbose(_("Exiting"));
324 catch( std::exception
&e
) {
325 cerr
<< _("exception caught in main(): ") << e
.what() << endl
;