tools: add playbook driver switch
[barry/progweb.git] / tools / pppob.cc
blobf3253d2ac9da8a03389c87f36059cde887849fd5
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 <unistd.h>
35 #include <signal.h>
36 #include "i18n.h"
38 #include "barrygetopt.h"
40 using namespace std;
41 using namespace Barry;
43 bool data_dump = false;
44 volatile bool signal_end = false;
46 void Usage()
48 int logical, major, minor;
49 const char *Version = Barry::Version(logical, major, minor);
51 cerr
52 << "pppob - PPP over Barry\n"
53 << " Copyright 2007-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
54 << " Using: " << Version << "\n"
55 << "\n"
56 << " -l file Direct pppob log output to file (useful with -v)\n"
57 << " -p pin PIN of device to talk with\n"
58 << " If only one device plugged in, this flag is optional\n"
59 << " -P pass Simplistic method to specify device password\n"
60 << " -s Use Serial mode instead of IpModem\n"
61 << " -v Dump protocol data during operation (debugging only!)\n"
62 << endl;
65 void signal_handler(int signum)
67 signal_end = true;
70 void SerialDataCallback(void *context, const unsigned char *data, int len)
72 if( len && data_dump )
73 barryverbose("ReadThread:\n" << Data(data, len));
75 while( len ) {
76 int written = write(1, data, len);
77 if( written > 0 ) {
78 len -= written;
79 data += written;
81 else {
82 barryverbose("Error in write()");
87 void ProcessStdin(Modem &modem)
89 // Read from stdin and write to USB, until
90 // stdin is closed
91 Data data;
92 int bytes_read;
93 fd_set rfds;
94 struct timeval tv;
95 int ret;
97 // Handle interrupt signals from pppd
98 signal_end = false;
99 signal(SIGINT, &signal_handler);
100 signal(SIGHUP, &signal_handler);
101 signal(SIGTERM, &signal_handler);
103 FD_ZERO(&rfds);
104 while( signal_end == false ) {
105 // Need to use select() here, so that pppd doesn't
106 // hang when it tries to set the line discipline
107 // on our stdin.
109 FD_SET(0, &rfds);
110 tv.tv_sec = 30;
111 tv.tv_usec = 0;
113 ret = select(1, &rfds, NULL, NULL, &tv);
114 if( ret == -1 ) {
115 perror("select()");
117 else if( ret && FD_ISSET(0, &rfds) ) {
118 bytes_read = read(0, data.GetBuffer(), data.GetBufSize());
119 if( bytes_read == 0 )
120 break;
122 if( bytes_read > 0 ) {
123 data.ReleaseBuffer(bytes_read);
124 modem.Write(data);
130 int main(int argc, char *argv[])
132 INIT_I18N(PACKAGE);
134 cout.sync_with_stdio(true); // leave this on, since libusb uses
135 // stdio for debug messages
137 try {
139 uint32_t pin = 0;
140 bool force_serial = false;
141 std::string logfile;
142 std::string password;
144 // check for options via the fifo first, so the command
145 // line args can override them
146 FifoClient fifo;
147 if( fifo.Fetch(4) ) {
148 const FifoArgs &args = fifo.GetArgs();
149 pin = args.m_pin.Value();
150 force_serial = args.m_use_serial_mode;
151 logfile = args.m_log_filename;
152 password = args.m_password;
153 data_dump = args.m_verbose;
156 // process command line options
157 for(;;) {
158 int cmd = getopt(argc, argv, "l:p:P:sv");
159 if( cmd == -1 )
160 break;
162 switch( cmd )
164 case 'l': // Verbose log file
165 logfile = optarg;
166 break;
168 case 'p': // Blackberry PIN
169 pin = strtoul(optarg, NULL, 16);
170 break;
172 case 'P': // Device password
173 password = optarg;
174 break;
176 case 's': // Use Serial mode
177 force_serial = true;
178 break;
180 case 'v': // data dump on
181 data_dump = true;
182 break;
184 case 'h': // help
185 default:
186 Usage();
187 return 0;
191 // Initialize the barry library. Must be called before
192 // anything else.
193 // Log to stderr, since stdout is for data in this program.
194 std::auto_ptr<std::ofstream> log;
195 if( logfile.size() ) {
196 log.reset( new std::ofstream(logfile.c_str(), ios::app) );
197 Barry::Init(data_dump, log.get());
199 else {
200 Barry::Init(data_dump, &std::cerr);
203 // Display version if in data_dump mode
204 if( data_dump ) {
205 int logical, major, minor;
206 const char *Version = Barry::Version(logical, major, minor);
207 barryverbose(Version);
210 // Probe the USB bus for Blackberry devices and display.
211 // If user has specified a PIN, search for it in the
212 // available device list here as well
213 Barry::Probe probe;
214 int activeDevice = probe.FindActive(pin);
215 if( activeDevice == -1 ) {
216 if( pin )
217 cerr << "PIN " << setbase(16) << pin
218 << " not found" << endl;
219 cerr << "No device selected" << endl;
220 return 1;
223 const ProbeResult &device = probe.Get(activeDevice);
225 if( !force_serial && device.HasIpModem() ) {
226 barryverbose("Using IpModem mode...");
228 // Create our controller object using our threaded router.
229 Controller con(probe.Get(activeDevice));
231 // Open serial mode... the callback handles reading from
232 // USB and writing to stdout
233 Mode::IpModem modem(con, SerialDataCallback, 0);
234 modem.Open(password.c_str());
236 ProcessStdin(modem);
237 modem.Close(); // graceful close so we can restart without unplugging
239 else {
240 if( force_serial ) {
241 barryverbose("Using Serial mode per command line...");
243 else {
244 barryverbose("No IpModem mode available, using Serial mode...");
247 // Create our socket router and start thread to handle
248 // the USB reading, instead of creating our own thread.
249 SocketRoutingQueue router;
250 router.SpinoffSimpleReadThread();
252 // Create our controller object using our threaded router.
253 Controller con(probe.Get(activeDevice), router);
255 // Open desktop mode... this handles the password side
256 // of things
257 Mode::Desktop desktop(con);
258 desktop.Open(password.c_str());
260 // Open serial connection
261 Mode::Serial modem(con, SerialDataCallback, 0);
262 modem.Open(password.c_str());
264 ProcessStdin(modem);
267 barryverbose("Exiting");
270 catch( std::exception &e ) {
271 cerr << "exception caught in main(): " << e.what() << endl;
272 return 1;
275 return 0;