Cleaned up Orange UK file formatting to match the others
[barry/progweb.git] / tools / bidentify.cc
blob10118aedbc07300b505118695bab20b2f2449b96
1 ///
2 /// \file bidentify.cc
3 /// Tool for probing identifying Blackberry devices
4 ///
6 /*
7 Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include <barry/barry.h>
23 #include <iostream>
24 #include <iomanip>
25 #include <getopt.h>
26 #include "i18n.h"
28 using namespace std;
29 using namespace Barry;
31 void Usage()
33 int logical, major, minor;
34 const char *Version = Barry::Version(logical, major, minor);
36 cerr
37 << "bidentify - USB Blackberry Identifier Tool\n"
38 << " Copyright 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)\n"
39 << " Using: " << Version << "\n"
40 << "\n"
41 << " -B bus Specify which USB bus to search on\n"
42 << " -N dev Specify which system device, using system specific string\n"
43 << "\n"
44 << " -c If used with -m, show both hex and dec where possible\n"
45 << " -m Also show the device's ESN / MEID / IMEI\n"
46 << " -h This help\n"
47 << " -v Dump protocol data during operation\n"
48 << endl;
51 std::string FetchMEID(Barry::Mode::Desktop &desktop)
53 using namespace Barry;
55 unsigned int dbId = desktop.GetDBID(HandheldAgent::GetDBName());
56 RecordStateTable state;
57 desktop.GetRecordStateTable(dbId, state);
59 unsigned int recId = HandheldAgent::GetMEIDRecordId();
61 RecordStateTable::IndexType meid_index;
62 if( state.GetIndex(recId, &meid_index) ) {
63 NullStore<HandheldAgent> store;
64 RecordParser<HandheldAgent, NullStore<HandheldAgent> > parser(store);
65 desktop.GetRecord(dbId, meid_index, parser);
66 return parser.GetRecord().MEID;
68 else {
69 return string();
73 int main(int argc, char *argv[])
75 INIT_I18N(PACKAGE);
77 cout.sync_with_stdio(true); // leave this on, since libusb uses
78 // stdio for debug messages
80 try {
82 bool data_dump = false,
83 get_meid = false,
84 show_both = false;
85 string busname;
86 string devname;
88 // process command line options
89 for(;;) {
90 int cmd = getopt(argc, argv, "B:hN:vmc");
91 if( cmd == -1 )
92 break;
94 switch( cmd )
96 case 'B': // busname
97 busname = optarg;
98 break;
100 case 'N': // Devname
101 devname = optarg;
102 break;
104 case 'c': // show both hex and dec
105 show_both = true;
106 break;
108 case 'm': // get meid / esn
109 get_meid = true;
110 break;
112 case 'v': // data dump on
113 data_dump = true;
114 break;
116 case 'h': // help
117 default:
118 Usage();
119 return 0;
123 Barry::Init(data_dump);
124 Barry::Probe probe(busname.c_str(), devname.c_str());
126 // show any errors during probe first
127 if( probe.GetFailCount() ) {
128 cerr << "Blackberry device errors with errors during probe:" << endl;
129 for( int i = 0; i < probe.GetFailCount(); i++ ) {
130 cerr << probe.GetFailMsg(i) << endl;
134 // show all successfully found devices
135 for( int i = 0; i < probe.GetCount(); i++ ) {
136 const ProbeResult &pr = probe.Get(i);
137 cout << pr.m_pin.Str() << ", "
138 << pr.m_description;
140 if( get_meid ) try {
141 Barry::Controller con(pr);
142 Barry::Mode::Desktop desktop(con);
143 desktop.Open();
145 // store as temporary in case of exceptions
146 string meid = FetchMEID(desktop);
147 cout << ", " << meid;
149 if( show_both ) {
150 if( HandheldAgent::IsESNHex(meid) ) {
151 cout << " (" << HandheldAgent::ESNHex2Dec(meid) << ")";
153 else if( HandheldAgent::IsESNDec(meid) ) {
154 cout << " (" << HandheldAgent::ESNDec2Hex(meid) << ")";
158 catch( Barry::BadPassword & ) {
159 // ignore password protected devices
161 catch( Barry::Error &e ) {
162 // output on cerr so as not to mess up
163 // the identify output
164 cerr << "Error on PIN: " << pr.m_pin.Str() << ": " << e.what() << endl;
167 // finish the identity line
168 cout << endl;
171 return probe.GetFailCount();
174 catch( std::exception &e ) {
175 cerr << "exception caught: " << e.what() << endl;
176 return 1;