Mention CVS and git on main documentation "how to" list
[barry/pauldeden.git] / tools / pppob.cc
bloba2d13accac9a72e322ac5f20ca3113172b748598
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 <getopt.h>
30 #include <sys/select.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #include <unistd.h>
36 using namespace std;
37 using namespace Barry;
39 bool data_dump = false;
41 void Usage()
43 int major, minor;
44 const char *Version = Barry::Version(major, minor);
46 cerr
47 << "pppob - PPP over Barry\n"
48 << " Copyright 2007-2008, Net Direct Inc. (http://www.netdirect.ca/)\n"
49 << " Using: " << Version << "\n"
50 << "\n"
51 << " -p pin PIN of device to talk with\n"
52 << " If only one device plugged in, this flag is optional\n"
53 << " -v Dump protocol data during operation (debugging only!)\n"
54 << endl;
57 void SerialDataCallback(void *context, const unsigned char *data, int len)
59 if( len && data_dump )
60 barryverbose("ReadThread:\n" << Data(data, len));
62 while( len ) {
63 int written = write(1, data, len);
64 if( written > 0 ) {
65 len -= written;
66 data += written;
68 else {
69 barryverbose("Error in write()");
74 int main(int argc, char *argv[])
76 cout.sync_with_stdio(true); // leave this on, since libusb uses
77 // stdio for debug messages
79 try {
81 uint32_t pin = 0;
83 // process command line options
84 for(;;) {
85 int cmd = getopt(argc, argv, "p:v");
86 if( cmd == -1 )
87 break;
89 switch( cmd )
91 case 'p': // Blackberry PIN
92 pin = strtoul(optarg, NULL, 16);
93 break;
95 case 'v': // data dump on
96 data_dump = true;
97 break;
99 case 'h': // help
100 default:
101 Usage();
102 return 0;
106 // Initialize the barry library. Must be called before
107 // anything else.
108 // Log to stderr, since stdout is for data in this program.
109 // std::ofstream log("/tmp/log", ios::app);
110 // Barry::Init(data_dump, &log);
111 Barry::Init(data_dump, &std::cerr);
113 // Probe the USB bus for Blackberry devices and display.
114 // If user has specified a PIN, search for it in the
115 // available device list here as well
116 Barry::Probe probe;
117 int activeDevice = probe.FindActive(pin);
118 if( activeDevice == -1 ) {
119 if( pin )
120 cerr << "PIN " << setbase(16) << pin
121 << " not found" << endl;
122 cerr << "No device selected" << endl;
123 return 1;
127 // for serial mode, not yet supported
128 // Create our socket router and start thread to handle
129 // the USB reading, instead of creating our own thread.
130 SocketRoutingQueue router;
131 router.SpinoffSimpleReadThread();
133 // Create our controller object using our threaded router.
134 Controller con(probe.Get(activeDevice), router);
136 // Open desktop mode... this handles the password side
137 // of things
138 // Mode::Desktop desktop(con);
139 // desktop.Open(); // FIXME - support password here?
143 // Create our controller object using our threaded router.
144 Controller con(probe.Get(activeDevice));
146 // Open serial mode... the callback handles reading from
147 // USB and writing to stdout
148 Mode::IpModem modem(con, SerialDataCallback, 0);
149 modem.Open();
151 // Read from stdin and write to USB, until
152 // stdin is closed
153 Data data;
154 int bytes_read;
155 fd_set rfds;
156 struct timeval tv;
157 int ret;
159 FD_ZERO(&rfds);
161 for(;;) {
162 // Need to use select() here, so that pppd doesn't
163 // hang when it tries to set the line discipline
164 // on our stdin.
166 FD_SET(0, &rfds);
167 tv.tv_sec = 30;
168 tv.tv_usec = 0;
170 ret = select(1, &rfds, NULL, NULL, &tv);
171 if( ret == -1 ) {
172 perror("select()");
174 else if( ret && FD_ISSET(0, &rfds) ) {
175 bytes_read = read(0, data.GetBuffer(), data.GetBufSize());
176 if( bytes_read == 0 )
177 break;
179 if( bytes_read > 0 ) {
180 data.ReleaseBuffer(bytes_read);
181 modem.Write(data);
186 barryverbose("Exiting");
189 catch( std::exception &e ) {
190 cerr << "exception caught in main(): " << e.what() << endl;
191 return 1;
194 return 0;