- added command line parameters to bbtool.cc
[barry.git] / src / bbtool.cc
blob3b10c6a8abebc65deff3077ab02d4240d7fe94bc
1 ///
2 /// \file bbtool.cc
3 /// Syncberry library tester
4 ///
5 // FIXME - add copyright notices
8 #include "sbcommon.h"
9 #include "probe.h"
10 #include "usbwrap.h"
11 #include "error.h"
12 #include "blackberry.h"
13 #include <iostream>
14 #include <iomanip>
15 #include <getopt.h>
17 using namespace std;
18 using namespace Syncberry;
20 void Usage()
22 cerr
23 << "bbtool - Command line USB Blackberry Test Tool\n\n"
24 << " -h This help\n"
25 << " -l List Blackberry devices\n"
26 << " -p pin Blackberry PIN of device to talk with\n"
27 << " If only one Blackberry plugged in, this flag is optional\n"
28 << endl;
31 int main(int argc, char *argv[])
33 cout.sync_with_stdio(true); // leave this on, since libusb uses
34 // stdio for debug messages
36 try {
38 uint32_t pin = 0;
39 bool list_only = false;
41 // process command line options
42 for(;;) {
43 int cmd = getopt(argc, argv, "hlp:");
44 if( cmd == -1 )
45 break;
47 switch( cmd )
49 case 'l': // list only
50 list_only = true;
51 break;
53 case 'p': // Blackberry PIN
54 pin = strtoul(optarg, NULL, 16);
55 break;
57 case 'h': // help
58 default:
59 Usage();
60 return 0;
64 Init();
66 Probe probe;
67 int activeDevice = -1;
68 cout << "Blackberry devices found:" << endl;
69 for( int i = 0; i < probe.GetCount(); i++ ) {
70 cout << probe.Get(i) << endl;
71 if( probe.Get(i).m_pin == pin )
72 activeDevice = i;
75 if( list_only )
76 return 0; // done
78 if( activeDevice == -1 ) {
79 if( pin == 0 ) {
80 // can we default to single device?
81 if( probe.GetCount() == 1 )
82 activeDevice = 0;
83 else {
84 cerr << "No device selected" << endl;
85 return 1;
88 else {
89 cerr << "PIN " << setbase(16) << pin
90 << " not found" << endl;
91 return 1;
95 Blackberry bb(probe.Get(activeDevice));
96 bb.Test();
99 catch( Syncberry::SBError &se ) {
100 std::cerr << "SBError caught: " << se.what() << endl;
102 catch( Usb::UsbError &ue) {
103 std::cerr << "UsbError caught: " << ue.what() << endl;
105 catch( std::runtime_error &re ) {
106 std::cerr << "std::runtime_error caught: " << re.what() << endl;
107 return 1;
110 return 0;