Fixed virtual function mis-name bug in btool's null parser
[barry/pauldeden.git] / tools / pppob.cc
blob037e6a94ce5e8f7ad13cdcecfbffcc5a071af9bb
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 << " -P pass Simplistic method to specify device password\n"
58 << " -s Use Serial mode instead of IpModem\n"
59 << " -v Dump protocol data during operation (debugging only!)\n"
60 << endl;
63 void signal_handler(int signum)
65 signal_end = true;
68 void SerialDataCallback(void *context, const unsigned char *data, int len)
70 if( len && data_dump )
71 barryverbose("ReadThread:\n" << Data(data, len));
73 while( len ) {
74 int written = write(1, data, len);
75 if( written > 0 ) {
76 len -= written;
77 data += written;
79 else {
80 barryverbose("Error in write()");
85 void ProcessStdin(Modem &modem)
87 // Read from stdin and write to USB, until
88 // stdin is closed
89 Data data;
90 int bytes_read;
91 fd_set rfds;
92 struct timeval tv;
93 int ret;
95 // Handle interrupt signals from pppd
96 signal_end = false;
97 signal(SIGINT, &signal_handler);
98 signal(SIGHUP, &signal_handler);
99 signal(SIGTERM, &signal_handler);
101 FD_ZERO(&rfds);
102 while( signal_end == false ) {
103 // Need to use select() here, so that pppd doesn't
104 // hang when it tries to set the line discipline
105 // on our stdin.
107 FD_SET(0, &rfds);
108 tv.tv_sec = 30;
109 tv.tv_usec = 0;
111 ret = select(1, &rfds, NULL, NULL, &tv);
112 if( ret == -1 ) {
113 perror("select()");
115 else if( ret && FD_ISSET(0, &rfds) ) {
116 bytes_read = read(0, data.GetBuffer(), data.GetBufSize());
117 if( bytes_read == 0 )
118 break;
120 if( bytes_read > 0 ) {
121 data.ReleaseBuffer(bytes_read);
122 modem.Write(data);
128 int main(int argc, char *argv[])
130 cout.sync_with_stdio(true); // leave this on, since libusb uses
131 // stdio for debug messages
133 try {
135 uint32_t pin = 0;
136 bool force_serial = false;
137 std::string logfile;
138 std::string password;
140 // process command line options
141 for(;;) {
142 int cmd = getopt(argc, argv, "l:p:P:sv");
143 if( cmd == -1 )
144 break;
146 switch( cmd )
148 case 'l': // Verbose log file
149 logfile = optarg;
150 break;
152 case 'p': // Blackberry PIN
153 pin = strtoul(optarg, NULL, 16);
154 break;
156 case 'P': // Device password
157 password = optarg;
158 break;
160 case 's': // Use Serial mode
161 force_serial = true;
162 break;
164 case 'v': // data dump on
165 data_dump = true;
166 break;
168 case 'h': // help
169 default:
170 Usage();
171 return 0;
175 // Initialize the barry library. Must be called before
176 // anything else.
177 // Log to stderr, since stdout is for data in this program.
178 std::auto_ptr<std::ofstream> log;
179 if( logfile.size() ) {
180 log.reset( new std::ofstream(logfile.c_str(), ios::app) );
181 Barry::Init(data_dump, log.get());
183 else {
184 Barry::Init(data_dump, &std::cerr);
187 // Probe the USB bus for Blackberry devices and display.
188 // If user has specified a PIN, search for it in the
189 // available device list here as well
190 Barry::Probe probe;
191 int activeDevice = probe.FindActive(pin);
192 if( activeDevice == -1 ) {
193 if( pin )
194 cerr << "PIN " << setbase(16) << pin
195 << " not found" << endl;
196 cerr << "No device selected" << endl;
197 return 1;
200 const ProbeResult &device = probe.Get(activeDevice);
202 if( !force_serial && device.HasIpModem() ) {
203 barryverbose("Using IpModem mode...");
205 // Create our controller object using our threaded router.
206 Controller con(probe.Get(activeDevice));
208 // Open serial mode... the callback handles reading from
209 // USB and writing to stdout
210 Mode::IpModem modem(con, SerialDataCallback, 0);
211 modem.Open(password.c_str());
213 ProcessStdin(modem);
214 modem.Close(); // graceful close so we can restart without unplugging
216 else {
217 barryverbose("Using Serial mode...");
219 // Create our socket router and start thread to handle
220 // the USB reading, instead of creating our own thread.
221 SocketRoutingQueue router;
222 router.SpinoffSimpleReadThread();
224 // Create our controller object using our threaded router.
225 Controller con(probe.Get(activeDevice), router);
227 // Open desktop mode... this handles the password side
228 // of things
229 Mode::Desktop desktop(con);
230 desktop.Open(password.c_str());
232 // Open serial connection
233 Mode::Serial modem(con, SerialDataCallback, 0);
234 modem.Open();
236 ProcessStdin(modem);
239 barryverbose("Exiting");
242 catch( std::exception &e ) {
243 cerr << "exception caught in main(): " << e.what() << endl;
244 return 1;
247 return 0;