tools: added -s switch (usb serial number) to bidentify
[barry.git] / tools / bidentify.cc
blob633f0080c64bdd13905fa268cd75df499f4f31c4
1 ///
2 /// \file bidentify.cc
3 /// Tool for probing identifying Blackberry devices
4 ///
6 /*
7 Copyright (C) 2005-2012, 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 "barrygetopt.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 << string_vprintf(
37 _("bidentify - USB Blackberry Identifier Tool\n"
38 " Copyright 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
39 " Using: %s\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 " -s Also show the device's USB serial number (same as shown by lsusb)\n"
47 " -h This help\n"
48 " -v Dump protocol data during operation\n"), Version)
49 << endl;
52 std::string FetchMEID(Barry::Mode::Desktop &desktop)
54 using namespace Barry;
56 unsigned int dbId = desktop.GetDBID(HandheldAgent::GetDBName());
57 RecordStateTable state;
58 desktop.GetRecordStateTable(dbId, state);
60 unsigned int recId = HandheldAgent::GetMEIDRecordId();
62 RecordStateTable::IndexType meid_index;
63 if( state.GetIndex(recId, &meid_index) ) {
64 NullStore<HandheldAgent> store;
65 RecordParser<HandheldAgent, NullStore<HandheldAgent> > parser(store);
66 desktop.GetRecord(dbId, meid_index, parser);
67 return parser.GetRecord().MEID;
69 else {
70 return string();
74 int main(int argc, char *argv[])
76 INIT_I18N(PACKAGE);
78 cout.sync_with_stdio(true); // leave this on, since libusb uses
79 // stdio for debug messages
81 try {
83 bool data_dump = false,
84 get_meid = false,
85 get_usb_serial = false,
86 show_both = false;
87 string busname;
88 string devname;
90 // process command line options
91 for(;;) {
92 int cmd = getopt(argc, argv, "B:hN:vmsc");
93 if( cmd == -1 )
94 break;
96 switch( cmd )
98 case 'B': // busname
99 busname = optarg;
100 break;
102 case 'N': // Devname
103 devname = optarg;
104 break;
106 case 'c': // show both hex and dec
107 show_both = true;
108 break;
110 case 'm': // get meid / esn
111 get_meid = true;
112 break;
114 case 's': // get usb serial number
115 get_usb_serial = true;
116 break;
118 case 'v': // data dump on
119 data_dump = true;
120 break;
122 case 'h': // help
123 default:
124 Usage();
125 return 0;
129 Barry::Init(data_dump, &std::cerr);
130 Barry::Probe probe(busname.c_str(), devname.c_str());
132 // show all successfully found devices
133 for( int i = 0; i < probe.GetCount(); i++ ) {
134 const ProbeResult &pr = probe.Get(i);
135 cout << pr.m_pin.Str() << ", "
136 << pr.m_description;
138 if( get_meid ) try {
139 Barry::Controller con(pr);
140 Barry::Mode::Desktop desktop(con);
141 desktop.Open();
143 // store as temporary in case of exceptions
144 string meid = FetchMEID(desktop);
145 cout << ", " << meid;
147 if( show_both ) {
148 if( HandheldAgent::IsESNHex(meid) ) {
149 cout << " (" << HandheldAgent::ESNHex2Dec(meid) << ")";
151 else if( HandheldAgent::IsESNDec(meid) ) {
152 cout << " (" << HandheldAgent::ESNDec2Hex(meid) << ")";
156 catch( Barry::BadPassword & ) {
157 // ignore password protected devices
159 catch( Barry::Error &e ) {
160 // output on cerr so as not to mess up
161 // the identify output
162 cerr << _("Error on PIN: ") << pr.m_pin.Str() << ": " << e.what() << endl;
165 if( get_usb_serial ) {
166 Usb::Device dev(pr.m_dev);
167 cout << ", " << dev.GetSimpleSerialNumber();
170 // finish the identity line
171 cout << endl;
174 return probe.GetFailCount();
177 catch( std::exception &e ) {
178 cerr << _("exception caught: ") << e.what() << endl;
179 return 1;