3 /// In the same vein as pppoe, used with pppd to create a
4 /// pty tunnel and GPRS modem link.
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>
34 using namespace Barry
;
36 volatile bool read_finished
= false;
41 const char *Version
= Barry::Version(major
, minor
);
44 << "pppob - PPP over Barry\n"
45 << " Copyright 2007, Net Direct Inc. (http://www.netdirect.ca/)\n"
46 << " Using: " << Version
<< "\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"
54 void *UsbReadThread(void *userptr
)
59 Controller
*con
= (Controller
*)userptr
;
61 // read from USB and write to stdout until finished
62 while( !read_finished
) {
65 con
->SerialRead(data
, 60000);
67 int todo
= data
.GetSize();
68 const unsigned char *buf
= data
.GetData();
71 int written
= write(1, buf
, todo
);
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
;
95 int main(int argc
, char *argv
[])
97 cout
.sync_with_stdio(true); // leave this on, since libusb uses
98 // stdio for debug messages
103 bool data_dump
= false;
105 // process command line options
107 int cmd
= getopt(argc
, argv
, "p:v");
113 case 'p': // Blackberry PIN
114 pin
= strtoul(optarg
, NULL
, 16);
117 case 'v': // data dump on
128 // Initialize the barry library. Must be called before
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
136 int activeDevice
= probe
.FindActive(pin
);
137 if( activeDevice
== -1 ) {
139 cerr
<< "PIN " << setbase(16) << pin
140 << " not found" << endl
;
141 cerr
<< "No device selected" << endl
;
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
;
158 // read from stdin and write to USB, until
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
;