lib: added support for a global config file
[barry.git] / tools / pppob.cc
bloba4c14a35235c34a09a28b9281c3ccfa9868b2b55
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-2009, 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>
36 #include "i18n.h"
39 using namespace std;
40 using namespace Barry;
42 bool data_dump = false;
43 volatile bool signal_end = false;
45 void Usage()
47 int major, minor;
48 const char *Version = Barry::Version(major, minor);
50 cerr
51 << "pppob - PPP over Barry\n"
52 << " Copyright 2007-2009, Net Direct Inc. (http://www.netdirect.ca/)\n"
53 << " Using: " << Version << "\n"
54 << "\n"
55 << " -l file Direct pppob log output to file (useful with -v)\n"
56 << " -p pin PIN of device to talk with\n"
57 << " If only one device plugged in, this flag is optional\n"
58 << " -P pass Simplistic method to specify device password\n"
59 << " -s Use Serial mode instead of IpModem\n"
60 << " -v Dump protocol data during operation (debugging only!)\n"
61 << endl;
64 void signal_handler(int signum)
66 signal_end = true;
69 void SerialDataCallback(void *context, const unsigned char *data, int len)
71 if( len && data_dump )
72 barryverbose("ReadThread:\n" << Data(data, len));
74 while( len ) {
75 int written = write(1, data, len);
76 if( written > 0 ) {
77 len -= written;
78 data += written;
80 else {
81 barryverbose("Error in write()");
86 void ProcessStdin(Modem &modem)
88 // Read from stdin and write to USB, until
89 // stdin is closed
90 Data data;
91 int bytes_read;
92 fd_set rfds;
93 struct timeval tv;
94 int ret;
96 // Handle interrupt signals from pppd
97 signal_end = false;
98 signal(SIGINT, &signal_handler);
99 signal(SIGHUP, &signal_handler);
100 signal(SIGTERM, &signal_handler);
102 FD_ZERO(&rfds);
103 while( signal_end == false ) {
104 // Need to use select() here, so that pppd doesn't
105 // hang when it tries to set the line discipline
106 // on our stdin.
108 FD_SET(0, &rfds);
109 tv.tv_sec = 30;
110 tv.tv_usec = 0;
112 ret = select(1, &rfds, NULL, NULL, &tv);
113 if( ret == -1 ) {
114 perror("select()");
116 else if( ret && FD_ISSET(0, &rfds) ) {
117 bytes_read = read(0, data.GetBuffer(), data.GetBufSize());
118 if( bytes_read == 0 )
119 break;
121 if( bytes_read > 0 ) {
122 data.ReleaseBuffer(bytes_read);
123 modem.Write(data);
129 int main(int argc, char *argv[])
131 INIT_I18N(PACKAGE);
133 cout.sync_with_stdio(true); // leave this on, since libusb uses
134 // stdio for debug messages
136 try {
138 uint32_t pin = 0;
139 bool force_serial = false;
140 std::string logfile;
141 std::string password;
143 // process command line options
144 for(;;) {
145 int cmd = getopt(argc, argv, "l:p:P:sv");
146 if( cmd == -1 )
147 break;
149 switch( cmd )
151 case 'l': // Verbose log file
152 logfile = optarg;
153 break;
155 case 'p': // Blackberry PIN
156 pin = strtoul(optarg, NULL, 16);
157 break;
159 case 'P': // Device password
160 password = optarg;
161 break;
163 case 's': // Use Serial mode
164 force_serial = true;
165 break;
167 case 'v': // data dump on
168 data_dump = true;
169 break;
171 case 'h': // help
172 default:
173 Usage();
174 return 0;
178 // Initialize the barry library. Must be called before
179 // anything else.
180 // Log to stderr, since stdout is for data in this program.
181 std::auto_ptr<std::ofstream> log;
182 if( logfile.size() ) {
183 log.reset( new std::ofstream(logfile.c_str(), ios::app) );
184 Barry::Init(data_dump, log.get());
186 else {
187 Barry::Init(data_dump, &std::cerr);
190 // Display version if in data_dump mode
191 if( data_dump ) {
192 int major, minor;
193 const char *Version = Barry::Version(major, minor);
194 barryverbose(Version);
197 // Probe the USB bus for Blackberry devices and display.
198 // If user has specified a PIN, search for it in the
199 // available device list here as well
200 Barry::Probe probe;
201 int activeDevice = probe.FindActive(pin);
202 if( activeDevice == -1 ) {
203 if( pin )
204 cerr << "PIN " << setbase(16) << pin
205 << " not found" << endl;
206 cerr << "No device selected" << endl;
207 return 1;
210 const ProbeResult &device = probe.Get(activeDevice);
212 if( !force_serial && device.HasIpModem() ) {
213 barryverbose("Using IpModem mode...");
215 // Create our controller object using our threaded router.
216 Controller con(probe.Get(activeDevice));
218 // Open serial mode... the callback handles reading from
219 // USB and writing to stdout
220 Mode::IpModem modem(con, SerialDataCallback, 0);
221 modem.Open(password.c_str());
223 ProcessStdin(modem);
224 modem.Close(); // graceful close so we can restart without unplugging
226 else {
227 if( force_serial ) {
228 barryverbose("Using Serial mode per command line...");
230 else {
231 barryverbose("No IpModem mode available, using Serial mode...");
234 // Create our socket router and start thread to handle
235 // the USB reading, instead of creating our own thread.
236 SocketRoutingQueue router;
237 router.SpinoffSimpleReadThread();
239 // Create our controller object using our threaded router.
240 Controller con(probe.Get(activeDevice), router);
242 // Open desktop mode... this handles the password side
243 // of things
244 Mode::Desktop desktop(con);
245 desktop.Open(password.c_str());
247 // Open serial connection
248 Mode::Serial modem(con, SerialDataCallback, 0);
249 modem.Open(password.c_str());
251 ProcessStdin(modem);
254 barryverbose("Exiting");
257 catch( std::exception &e ) {
258 cerr << "exception caught in main(): " << e.what() << endl;
259 return 1;
262 return 0;