Reorganized sample ppp options files
[barry/pauldeden.git] / tools / pppob.cc
blobbb476d3dd0bd91d929ee1639b8148faab27980e6
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-2008, 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 <getopt.h>
31 #include <sys/select.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <signal.h>
38 using namespace std;
39 using namespace Barry;
41 bool data_dump = false;
42 volatile bool signal_end = false;
44 void Usage()
46 int major, minor;
47 const char *Version = Barry::Version(major, minor);
49 cerr
50 << "pppob - PPP over Barry\n"
51 << " Copyright 2007-2008, Net Direct Inc. (http://www.netdirect.ca/)\n"
52 << " Using: " << Version << "\n"
53 << "\n"
54 << " -l file Direct pppob log output to file (useful with -v)\n"
55 << " -p pin PIN of device to talk with\n"
56 << " If only one device plugged in, this flag is optional\n"
57 << " -s Use Serial mode instead of IpModem\n"
58 << " -v Dump protocol data during operation (debugging only!)\n"
59 << endl;
62 void signal_handler(int signum)
64 signal_end = true;
67 void SerialDataCallback(void *context, const unsigned char *data, int len)
69 if( len && data_dump )
70 barryverbose("ReadThread:\n" << Data(data, len));
72 while( len ) {
73 int written = write(1, data, len);
74 if( written > 0 ) {
75 len -= written;
76 data += written;
78 else {
79 barryverbose("Error in write()");
84 void ProcessStdin(Modem &modem)
86 // Read from stdin and write to USB, until
87 // stdin is closed
88 Data data;
89 int bytes_read;
90 fd_set rfds;
91 struct timeval tv;
92 int ret;
94 // Handle interrupt signals from pppd
95 signal_end = false;
96 signal(SIGINT, &signal_handler);
98 FD_ZERO(&rfds);
99 while( signal_end == false ) {
100 // Need to use select() here, so that pppd doesn't
101 // hang when it tries to set the line discipline
102 // on our stdin.
104 FD_SET(0, &rfds);
105 tv.tv_sec = 30;
106 tv.tv_usec = 0;
108 ret = select(1, &rfds, NULL, NULL, &tv);
109 if( ret == -1 ) {
110 perror("select()");
112 else if( ret && FD_ISSET(0, &rfds) ) {
113 bytes_read = read(0, data.GetBuffer(), data.GetBufSize());
114 if( bytes_read == 0 )
115 break;
117 if( bytes_read > 0 ) {
118 data.ReleaseBuffer(bytes_read);
119 modem.Write(data);
125 int main(int argc, char *argv[])
127 cout.sync_with_stdio(true); // leave this on, since libusb uses
128 // stdio for debug messages
130 try {
132 uint32_t pin = 0;
133 bool force_serial = false;
134 std::string logfile;
136 // process command line options
137 for(;;) {
138 int cmd = getopt(argc, argv, "l:p:sv");
139 if( cmd == -1 )
140 break;
142 switch( cmd )
144 case 'l': // Verbose log file
145 logfile = optarg;
146 break;
148 case 'p': // Blackberry PIN
149 pin = strtoul(optarg, NULL, 16);
150 break;
152 case 's': // Use Serial mode
153 force_serial = true;
154 break;
156 case 'v': // data dump on
157 data_dump = true;
158 break;
160 case 'h': // help
161 default:
162 Usage();
163 return 0;
167 // Initialize the barry library. Must be called before
168 // anything else.
169 // Log to stderr, since stdout is for data in this program.
170 std::auto_ptr<std::ofstream> log;
171 if( logfile.size() ) {
172 log.reset( new std::ofstream(logfile.c_str(), ios::app) );
173 Barry::Init(data_dump, log.get());
175 else {
176 Barry::Init(data_dump, &std::cerr);
179 // Probe the USB bus for Blackberry devices and display.
180 // If user has specified a PIN, search for it in the
181 // available device list here as well
182 Barry::Probe probe;
183 int activeDevice = probe.FindActive(pin);
184 if( activeDevice == -1 ) {
185 if( pin )
186 cerr << "PIN " << setbase(16) << pin
187 << " not found" << endl;
188 cerr << "No device selected" << endl;
189 return 1;
192 const ProbeResult &device = probe.Get(activeDevice);
194 if( !force_serial && device.HasIpModem() ) {
195 barryverbose("Using IpModem mode...");
197 // Create our controller object using our threaded router.
198 Controller con(probe.Get(activeDevice));
200 // Open serial mode... the callback handles reading from
201 // USB and writing to stdout
202 Mode::IpModem modem(con, SerialDataCallback, 0);
203 modem.Open();
205 ProcessStdin(modem);
207 else {
208 barryverbose("Using Serial mode...");
210 // Create our socket router and start thread to handle
211 // the USB reading, instead of creating our own thread.
212 SocketRoutingQueue router;
213 router.SpinoffSimpleReadThread();
215 // Create our controller object using our threaded router.
216 Controller con(probe.Get(activeDevice), router);
218 // Open desktop mode... this handles the password side
219 // of things
220 Mode::Desktop desktop(con);
221 desktop.Open(); // FIXME - support password here?
223 // Open serial connection
224 Mode::Serial modem(con, SerialDataCallback, 0);
225 modem.Open();
227 ProcessStdin(modem);
231 barryverbose("Exiting");
234 catch( std::exception &e ) {
235 cerr << "exception caught in main(): " << e.what() << endl;
236 return 1;
239 return 0;