doc: highlighted that syncing should now be done with the GUI
[barry.git] / tools / pppob.cc
blob2e390aba047b9e43ce1a06f527637b9f9e21f032
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 <errno.h>
37 #include "i18n.h"
39 #include "barrygetopt.h"
41 using namespace std;
42 using namespace Barry;
44 bool data_dump = false;
45 volatile bool signal_end = false;
47 void Usage()
49 int logical, major, minor;
50 const char *Version = Barry::Version(logical, major, minor);
52 cerr
53 << "pppob - PPP over Barry\n"
54 << " Copyright 2007-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
55 << " Using: " << Version << "\n"
56 << "\n"
57 << " -l file Direct pppob log output to file (useful with -v)\n"
58 << " -p pin PIN of device to talk with\n"
59 << " If only one device plugged in, this flag is optional\n"
60 << " -P pass Simplistic method to specify device password\n"
61 << " -s Use Serial mode instead of IpModem\n"
62 << " -v Dump protocol data during operation (debugging only!)\n"
63 << endl;
66 void signal_handler(int signum)
68 signal_end = true;
71 void SerialDataCallback(void *context, const unsigned char *data, int len)
73 if( len && data_dump )
74 barryverbose("ReadThread:\n" << Data(data, len));
76 while( len ) {
77 int written = write(1, data, len);
78 if( written > 0 ) {
79 len -= written;
80 data += written;
82 else {
83 barryverbose("Error in write()");
88 void ProcessStdin(Modem &modem)
90 // Read from stdin and write to USB, until
91 // stdin is closed
92 Data data;
93 int bytes_read;
94 fd_set rfds;
95 struct timeval tv;
96 int ret;
98 // Handle interrupt signals from pppd
99 signal_end = false;
100 signal(SIGINT, &signal_handler);
101 signal(SIGHUP, &signal_handler);
102 signal(SIGTERM, &signal_handler);
104 FD_ZERO(&rfds);
105 while( signal_end == false ) {
106 // Need to use select() here, so that pppd doesn't
107 // hang when it tries to set the line discipline
108 // on our stdin.
110 FD_SET(0, &rfds);
111 tv.tv_sec = 30;
112 tv.tv_usec = 0;
114 ret = select(1, &rfds, NULL, NULL, &tv);
115 if( ret == -1 ) {
116 perror("select()");
118 else if( ret && FD_ISSET(0, &rfds) ) {
119 bytes_read = read(0, data.GetBuffer(), data.GetBufSize());
120 if( bytes_read == 0 )
121 break; // end of file
122 else if( bytes_read > 0 ) {
123 data.ReleaseBuffer(bytes_read);
124 modem.Write(data);
126 else {
127 // read error
128 barryverbose("Read error in ProcessStdin: " << strerror(errno));
129 break;
135 int main(int argc, char *argv[])
137 INIT_I18N(PACKAGE);
139 cout.sync_with_stdio(true); // leave this on, since libusb uses
140 // stdio for debug messages
142 try {
144 uint32_t pin = 0;
145 bool force_serial = false;
146 std::string logfile;
147 std::string password;
149 // check for options via the fifo first, so the command
150 // line args can override them
151 FifoClient fifo;
152 if( fifo.Fetch(4) ) {
153 const FifoArgs &args = fifo.GetArgs();
154 pin = args.m_pin.Value();
155 force_serial = args.m_use_serial_mode;
156 logfile = args.m_log_filename;
157 password = args.m_password;
158 data_dump = args.m_verbose;
161 // process command line options
162 for(;;) {
163 int cmd = getopt(argc, argv, "l:p:P:sv");
164 if( cmd == -1 )
165 break;
167 switch( cmd )
169 case 'l': // Verbose log file
170 logfile = optarg;
171 break;
173 case 'p': // Blackberry PIN
174 pin = strtoul(optarg, NULL, 16);
175 break;
177 case 'P': // Device password
178 password = optarg;
179 break;
181 case 's': // Use Serial mode
182 force_serial = true;
183 break;
185 case 'v': // data dump on
186 data_dump = true;
187 break;
189 case 'h': // help
190 default:
191 Usage();
192 return 0;
196 // Initialize the barry library. Must be called before
197 // anything else.
198 // Log to stderr, since stdout is for data in this program.
199 std::auto_ptr<std::ofstream> log;
200 if( logfile.size() ) {
201 log.reset( new std::ofstream(logfile.c_str(), ios::app) );
202 Barry::Init(data_dump, log.get());
204 else {
205 Barry::Init(data_dump, &std::cerr);
208 // Display version if in data_dump mode
209 if( data_dump ) {
210 int logical, major, minor;
211 const char *Version = Barry::Version(logical, major, minor);
212 barryverbose(Version);
215 // Probe the USB bus for Blackberry devices and display.
216 // If user has specified a PIN, search for it in the
217 // available device list here as well
218 Barry::Probe probe;
219 int activeDevice = probe.FindActive(pin);
220 if( activeDevice == -1 ) {
221 if( pin )
222 cerr << "PIN " << setbase(16) << pin
223 << " not found" << endl;
224 cerr << "No device selected" << endl;
225 return 1;
228 const ProbeResult &device = probe.Get(activeDevice);
230 if( !force_serial && device.HasIpModem() ) {
231 barryverbose("Using IpModem mode...");
233 // Create our controller object using our threaded router.
234 Controller con(probe.Get(activeDevice));
236 // Open serial mode... the callback handles reading from
237 // USB and writing to stdout
238 Mode::IpModem modem(con, SerialDataCallback, 0);
239 modem.Open(password.c_str());
241 ProcessStdin(modem);
242 modem.Close(); // graceful close so we can restart without unplugging
244 else {
245 if( force_serial ) {
246 barryverbose("Using Serial mode per command line...");
248 else {
249 barryverbose("No IpModem mode available, using Serial mode...");
252 // Create our socket router and start thread to handle
253 // the USB reading, instead of creating our own thread.
254 SocketRoutingQueue router;
255 router.SpinoffSimpleReadThread();
257 // Create our controller object using our threaded router.
258 Controller con(probe.Get(activeDevice), router);
260 // Open desktop mode... this handles the password side
261 // of things
262 Mode::Desktop desktop(con);
263 desktop.Open(password.c_str());
265 // Open serial connection
266 Mode::Serial modem(con, SerialDataCallback, 0);
267 modem.Open(password.c_str());
269 ProcessStdin(modem);
272 barryverbose("Exiting");
275 catch( std::exception &e ) {
276 cerr << "exception caught in main(): " << e.what() << endl;
277 return 1;
280 return 0;