Barry debian version 0.18.5-1
[barry.git] / tools / pppob.cc
blobec4788b3cdabd14c2d9e9fe053fc0e270bd63073
1 ///
2 /// \file pppob.cc
3 /// In the same vein as pppoe, used with pppd to create a
4 /// pty tunnel and GPRS modem link.
5 ///
7 /*
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>
24 #include <iomanip>
25 #include <iostream>
26 #include <fstream>
27 #include <vector>
28 #include <string>
29 #include <memory>
30 #include <stdlib.h>
31 #include <sys/select.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <termios.h>
37 #include <unistd.h>
38 #include <signal.h>
39 #include <errno.h>
40 #include "i18n.h"
42 #include "barrygetopt.h"
44 using namespace std;
45 using namespace Barry;
47 bool data_dump = false;
48 volatile bool signal_end = false;
49 int read_fd = -1;
50 int write_fd = -1;
52 void Usage()
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"
60 " Using: %s\n"
61 "\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"),
69 Version)
70 << endl;
73 void signal_handler(int signum)
75 signal_end = true;
78 void SerialDataCallback(void *context, const unsigned char *data, int len)
80 if( len && data_dump )
81 barryverbose("ReadThread:\n" << Data(data, len));
83 while( len ) {
84 int written = write(write_fd, data, len);
85 if( written > 0 ) {
86 len -= written;
87 data += written;
89 else {
90 barryverbose(_("Error in write()"));
95 void ProcessStdin(Modem &modem)
97 // Read from stdin and write to USB, until
98 // stdin is closed
99 Data data;
100 int bytes_read;
101 fd_set rfds;
102 struct timeval tv;
103 int ret;
105 // Handle interrupt signals from pppd
106 signal_end = false;
107 signal(SIGINT, &signal_handler);
108 signal(SIGHUP, &signal_handler);
109 signal(SIGTERM, &signal_handler);
111 FD_ZERO(&rfds);
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
115 // on our stdin.
117 FD_SET(read_fd, &rfds);
118 tv.tv_sec = 30;
119 tv.tv_usec = 0;
121 ret = select(read_fd+1, &rfds, NULL, NULL, &tv);
122 if( ret == -1 ) {
123 perror("select()");
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);
131 modem.Write(data);
133 else {
134 // read error
135 barryverbose(_("Read error in ProcessStdin: ") << strerror(errno));
136 break;
142 int main(int argc, char *argv[])
144 INIT_I18N(PACKAGE);
146 cout.sync_with_stdio(true); // leave this on, since libusb uses
147 // stdio for debug messages
149 try {
151 uint32_t pin = 0;
152 bool force_serial = false,
153 pseudo_tty = false;
154 std::string logfile;
155 std::string password;
157 // check for options via the fifo first, so the command
158 // line args can override them
159 FifoClient fifo;
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
170 for(;;) {
171 int cmd = getopt(argc, argv, "l:p:P:stv");
172 if( cmd == -1 )
173 break;
175 switch( cmd )
177 case 'l': // Verbose log file
178 logfile = optarg;
179 break;
181 case 'p': // Blackberry PIN
182 pin = strtoul(optarg, NULL, 16);
183 break;
185 case 'P': // Device password
186 password = optarg;
187 break;
189 case 's': // Use Serial mode
190 force_serial = true;
191 break;
193 case 't': // Use pseudo-tty
194 pseudo_tty = true;
195 break;
197 case 'v': // data dump on
198 data_dump = true;
199 break;
201 case 'h': // help
202 default:
203 Usage();
204 return 0;
208 if( pseudo_tty ) {
209 // open pty/tty master to get slave
210 int master = open("/dev/ptmx", O_RDWR);
211 if( master == -1 ) {
212 cerr << _("Cannot open /dev/ptmx: ") << strerror(errno) << endl;
213 return 1;
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;
226 // set raw mode
227 struct termios tp;
228 tcgetattr(master, &tp);
229 cfmakeraw(&tp);
230 tcsetattr(master, TCSANOW, &tp);
232 // send name of slave to stdout
233 cout << ptsname(master) << endl;
235 // set the global fd's
236 read_fd = master;
237 write_fd = master;
239 else {
240 // just default to stdin/stdout
241 read_fd = 0;
242 write_fd = 1;
245 // Initialize the barry library. Must be called before
246 // anything else.
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());
253 else {
254 Barry::Init(data_dump, &std::cerr);
257 // Display version if in data_dump mode
258 if( data_dump ) {
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
267 Barry::Probe probe;
268 int activeDevice = probe.FindActive(pin);
269 if( activeDevice == -1 ) {
270 if( pin )
271 cerr << _("PIN not found: ")
272 << setbase(16) << pin << endl;
273 cerr << _("No device selected") << endl;
274 return 1;
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());
290 ProcessStdin(modem);
291 modem.Close(); // graceful close so we can restart without unplugging
293 else {
294 if( force_serial ) {
295 barryverbose(_("Using Serial mode per command line..."));
297 else {
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
310 // of things
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());
318 ProcessStdin(modem);
321 barryverbose(_("Exiting"));
324 catch( std::exception &e ) {
325 cerr << _("exception caught in main(): ") << e.what() << endl;
326 return 1;
329 return 0;