removed unneeded sstream header
[barry.git] / tools / pppob.cc
blobbd876bb7f1fb319a29919d21455361afc369c93f
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, 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 <pthread.h>
33 using namespace std;
34 using namespace Barry;
36 volatile bool read_finished = false;
38 void Usage()
40 int major, minor;
41 const char *Version = Barry::Version(major, minor);
43 cerr
44 << "pppob - PPP over Barry\n"
45 << " Copyright 2007, Net Direct Inc. (http://www.netdirect.ca/)\n"
46 << " Using: " << Version << "\n"
47 << "\n"
48 << " -p pin PIN of device to talk with\n"
49 << " If only one device plugged in, this flag is optional\n"
50 << " -v Dump protocol data during operation (debugging only!)\n"
51 << endl;
54 void *UsbReadThread(void *userptr)
56 try {
58 Data data;
59 Controller *con = (Controller *)userptr;
61 // read from USB and write to stdout until finished
62 while( !read_finished ) {
64 try {
65 con->SerialRead(data, 60000);
67 int todo = data.GetSize();
68 const unsigned char *buf = data.GetData();
70 while( todo ) {
71 int written = write(1, buf, todo);
72 if( written > 0 ) {
73 todo -= written;
74 buf += written;
76 else {
77 cerr << "Error in write()" << endl;
81 catch( Usb::Timeout & ) {
82 // timeouts are allowed
88 catch( std::exception &e ) {
89 cerr << "Exception caught in UsbReadThread(): " << e.what() << endl;
92 return 0;
95 int main(int argc, char *argv[])
97 cout.sync_with_stdio(true); // leave this on, since libusb uses
98 // stdio for debug messages
100 try {
102 uint32_t pin = 0;
103 bool data_dump = false;
105 // process command line options
106 for(;;) {
107 int cmd = getopt(argc, argv, "p:v");
108 if( cmd == -1 )
109 break;
111 switch( cmd )
113 case 'p': // Blackberry PIN
114 pin = strtoul(optarg, NULL, 16);
115 break;
117 case 'v': // data dump on
118 data_dump = true;
119 break;
121 case 'h': // help
122 default:
123 Usage();
124 return 0;
128 // Initialize the barry library. Must be called before
129 // anything else.
130 Barry::Init(data_dump);
132 // Probe the USB bus for Blackberry devices and display.
133 // If user has specified a PIN, search for it in the
134 // available device list here as well
135 Barry::Probe probe;
136 int activeDevice = probe.FindActive(pin);
137 if( activeDevice == -1 ) {
138 if( pin )
139 cerr << "PIN " << setbase(16) << pin
140 << " not found" << endl;
141 cerr << "No device selected" << endl;
142 return 1;
145 // Create our controller object
146 Barry::Controller con(probe.Get(activeDevice));
148 // open serial mode socket
149 con.OpenMode(Controller::UsbSerData);
151 // start USB read thread
152 pthread_t usb_read_thread;
153 if( pthread_create(&usb_read_thread, NULL, UsbReadThread, &con) ) {
154 cerr << "Error creating USB read thread." << endl;
155 return 1;
158 // read from stdin and write to USB, until
159 // stdin is closed
160 Data data;
161 int bytes_read;
162 while( (bytes_read = read(0, data.GetBuffer(), data.GetBufSize())) != 0 ) {
163 if( bytes_read > 0 ) {
164 data.ReleaseBuffer(bytes_read);
165 con.SerialWrite(data);
170 // flag end / kill read thread
171 read_finished = true;
172 pthread_join(usb_read_thread, NULL);
175 catch( std::exception &e ) {
176 cerr << "exception caught in main(): " << e.what() << endl;
177 return 1;
180 return 0;