test: make it obvious which branch we're testing
[barry.git] / tools / pppob.cc
blobbf97d7d78ecf905a422132eb76d381c70d038b23
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-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>
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
58 << "pppob - PPP over Barry\n"
59 << " Copyright 2007-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
60 << " Using: " << Version << "\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 << endl;
72 void signal_handler(int signum)
74 signal_end = true;
77 void SerialDataCallback(void *context, const unsigned char *data, int len)
79 if( len && data_dump )
80 barryverbose("ReadThread:\n" << Data(data, len));
82 while( len ) {
83 int written = write(write_fd, data, len);
84 if( written > 0 ) {
85 len -= written;
86 data += written;
88 else {
89 barryverbose("Error in write()");
94 void ProcessStdin(Modem &modem)
96 // Read from stdin and write to USB, until
97 // stdin is closed
98 Data data;
99 int bytes_read;
100 fd_set rfds;
101 struct timeval tv;
102 int ret;
104 // Handle interrupt signals from pppd
105 signal_end = false;
106 signal(SIGINT, &signal_handler);
107 signal(SIGHUP, &signal_handler);
108 signal(SIGTERM, &signal_handler);
110 FD_ZERO(&rfds);
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
114 // on our stdin.
116 FD_SET(read_fd, &rfds);
117 tv.tv_sec = 30;
118 tv.tv_usec = 0;
120 ret = select(read_fd+1, &rfds, NULL, NULL, &tv);
121 if( ret == -1 ) {
122 perror("select()");
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);
130 modem.Write(data);
132 else {
133 // read error
134 barryverbose("Read error in ProcessStdin: " << strerror(errno));
135 break;
141 int main(int argc, char *argv[])
143 INIT_I18N(PACKAGE);
145 cout.sync_with_stdio(true); // leave this on, since libusb uses
146 // stdio for debug messages
148 try {
150 uint32_t pin = 0;
151 bool force_serial = false,
152 pseudo_tty = false;
153 std::string logfile;
154 std::string password;
156 // check for options via the fifo first, so the command
157 // line args can override them
158 FifoClient fifo;
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
169 for(;;) {
170 int cmd = getopt(argc, argv, "l:p:P:stv");
171 if( cmd == -1 )
172 break;
174 switch( cmd )
176 case 'l': // Verbose log file
177 logfile = optarg;
178 break;
180 case 'p': // Blackberry PIN
181 pin = strtoul(optarg, NULL, 16);
182 break;
184 case 'P': // Device password
185 password = optarg;
186 break;
188 case 's': // Use Serial mode
189 force_serial = true;
190 break;
192 case 't': // Use pseudo-tty
193 pseudo_tty = true;
194 break;
196 case 'v': // data dump on
197 data_dump = true;
198 break;
200 case 'h': // help
201 default:
202 Usage();
203 return 0;
207 if( pseudo_tty ) {
208 // open pty/tty master to get slave
209 int master = open("/dev/ptmx", O_RDWR);
210 if( master == -1 ) {
211 cerr << "Cannot open /dev/ptmx: " << strerror(errno) << endl;
212 return 1;
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;
225 // set raw mode
226 struct termios tp;
227 tcgetattr(master, &tp);
228 cfmakeraw(&tp);
229 tcsetattr(master, TCSANOW, &tp);
231 // send name of slave to stdout
232 cout << ptsname(master) << endl;
234 // set the global fd's
235 read_fd = master;
236 write_fd = master;
238 else {
239 // just default to stdin/stdout
240 read_fd = 0;
241 write_fd = 1;
244 // Initialize the barry library. Must be called before
245 // anything else.
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());
252 else {
253 Barry::Init(data_dump, &std::cerr);
256 // Display version if in data_dump mode
257 if( data_dump ) {
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
266 Barry::Probe probe;
267 int activeDevice = probe.FindActive(pin);
268 if( activeDevice == -1 ) {
269 if( pin )
270 cerr << "PIN " << setbase(16) << pin
271 << " not found" << endl;
272 cerr << "No device selected" << endl;
273 return 1;
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());
289 ProcessStdin(modem);
290 modem.Close(); // graceful close so we can restart without unplugging
292 else {
293 if( force_serial ) {
294 barryverbose("Using Serial mode per command line...");
296 else {
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
309 // of things
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());
317 ProcessStdin(modem);
320 barryverbose("Exiting");
323 catch( std::exception &e ) {
324 cerr << "exception caught in main(): " << e.what() << endl;
325 return 1;
328 return 0;