3 /// Barry library tester
7 Copyright (C) 2005-2008, 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>
33 using namespace Barry
;
38 const char *Version
= Barry::Version(major
, minor
);
41 << "btool - Command line USB Blackberry Test Tool\n"
42 << " Copyright 2005-2008, Net Direct Inc. (http://www.netdirect.ca/)\n"
43 << " Using: " << Version
<< "\n"
45 #ifdef __BARRY_BOOST_MODE__
52 << " -B bus Specify which USB bus to search on\n"
53 << " -N dev Specify which system device, using system specific string\n"
55 << " -c dn Convert address book database to LDIF format, using the\n"
56 << " specified baseDN\n"
57 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
58 << " Defaults to 'cn'\n"
59 << " -d db Load database 'db' FROM device and dump to screen\n"
60 << " Can be used multiple times to fetch more than one DB\n"
61 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
62 << " string separated by a comma, holding the read,write\n"
63 << " endpoint pair. Example: -e 83,5\n"
64 << " Note: Endpoints are specified in hex.\n"
65 << " You should never need to use this option.\n"
66 #ifdef __BARRY_BOOST_MODE__
67 << " -f file Filename to save or load handheld data to/from\n"
70 << " -l List devices\n"
71 << " -L List Contact field names\n"
72 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
73 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
74 << " Unmap: ldif name alone\n"
75 << " -M List current LDIF mapping\n"
76 << " -p pin PIN of device to talk with\n"
77 << " If only one device is plugged in, this flag is optional\n"
78 << " -P pass Simplistic method to specify device password\n"
79 << " -s db Save database 'db' TO device from data loaded from -f file\n"
80 << " -S Show list of supported database parsers\n"
81 << " -t Show database database table\n"
82 << " -T db Show record state table for given database\n"
83 << " -v Dump protocol data during operation\n"
84 << " -X Reset device\n"
86 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
88 << " -r # Record index number as seen in the -T state table.\n"
89 << " This overrides the default -d behaviour, and only\n"
90 << " downloads the one specified record, sending to stdout.\n"
91 << " -R # Same as -r, but also clears the record's dirty flags.\n"
92 << " -D # Record index number as seen in the -T state table,\n"
93 << " which indicates the record to delete. Used with the -d\n"
94 << " command to specify the database.\n"
101 Barry::ContactLdif
&ldif
;
103 Contact2Ldif(Barry::ContactLdif
&ldif
) : ldif(ldif
) {}
105 void operator()(const Contact
&rec
)
107 ldif
.DumpLdif(cout
, rec
);
111 template <class Record
>
114 std::vector
<Record
> records
;
115 mutable typename
std::vector
<Record
>::const_iterator rec_it
;
116 std::string filename
;
120 Store(const string
&filename
, bool load
)
121 : rec_it(records
.end()),
126 #ifdef __BARRY_BOOST_MODE__
129 if( load
&& filename
.size() ) {
130 // filename is available, attempt to load
131 cout
<< "Loading: " << filename
<< endl
;
132 ifstream
ifs(filename
.c_str());
134 getline(ifs
, dbName
);
135 boost::archive::text_iarchive
ia(ifs
);
137 cout
<< records
.size()
138 << " records loaded from '"
139 << filename
<< "'" << endl
;
140 sort(records
.begin(), records
.end());
141 rec_it
= records
.begin();
144 typename
std::vector
<Record
>::const_iterator beg
= records
.begin(), end
= records
.end();
145 for( ; beg
!= end
; beg
++ ) {
146 cout
<< (*beg
) << endl
;
150 } catch( boost::archive::archive_exception
&ae
) {
151 cerr
<< "Archive exception in ~Store(): "
152 << ae
.what() << endl
;
158 cout
<< "Store counted " << dec
<< count
<< " records." << endl
;
159 #ifdef __BARRY_BOOST_MODE__
162 if( !load
&& filename
.size() ) {
163 // filename is available, attempt to save
164 cout
<< "Saving: " << filename
<< endl
;
165 const std::vector
<Record
> &r
= records
;
166 ofstream
ofs(filename
.c_str());
167 ofs
<< Record::GetDBName() << endl
;
168 boost::archive::text_oarchive
oa(ofs
);
170 cout
<< dec
<< r
.size() << " records saved to '"
171 << filename
<< "'" << endl
;
174 } catch( boost::archive::archive_exception
&ae
) {
175 cerr
<< "Archive exception in ~Store(): "
176 << ae
.what() << endl
;
182 void operator()(const Record
&rec
)
185 std::cout
<< rec
<< std::endl
;
186 records
.push_back(rec
);
189 // retrieval operator
190 bool operator()(Record
&rec
, unsigned int databaseId
) const
192 if( rec_it
== records
.end() )
200 class DataDumpParser
: public Barry::Parser
205 virtual void SetUniqueId(uint32_t id
)
210 virtual void ParseFields(const Barry::Data
&data
, size_t &offset
)
212 std::cout
<< "Raw record dump for record: "
213 << std::hex
<< m_id
<< std::endl
;
214 std::cout
<< data
<< std::endl
;
218 auto_ptr
<Parser
> GetParser(const string
&name
, const string
&filename
)
220 // check for recognized database names
221 if( name
== Contact::GetDBName() ) {
222 return auto_ptr
<Parser
>(
223 new RecordParser
<Contact
, Store
<Contact
> > (
224 new Store
<Contact
>(filename
, false)));
226 else if( name
== Message::GetDBName() ) {
227 return auto_ptr
<Parser
>(
228 new RecordParser
<Message
, Store
<Message
> > (
229 new Store
<Message
>(filename
, false)));
231 else if( name
== Calendar::GetDBName() ) {
232 return auto_ptr
<Parser
>(
233 new RecordParser
<Calendar
, Store
<Calendar
> > (
234 new Store
<Calendar
>(filename
, false)));
236 else if( name
== ServiceBook::GetDBName() ) {
237 return auto_ptr
<Parser
>(
238 new RecordParser
<ServiceBook
, Store
<ServiceBook
> > (
239 new Store
<ServiceBook
>(filename
, false)));
242 else if( name
== Memo::GetDBName() ) {
243 return auto_ptr
<Parser
>(
244 new RecordParser
<Memo
, Store
<Memo
> > (
245 new Store
<Memo
>(filename
, false)));
247 else if( name
== Task::GetDBName() ) {
248 return auto_ptr
<Parser
>(
249 new RecordParser
<Task
, Store
<Task
> > (
250 new Store
<Task
>(filename
, false)));
252 else if( name
== PINMessage::GetDBName() ) {
253 return auto_ptr
<Parser
>(
254 new RecordParser
<PINMessage
, Store
<PINMessage
> > (
255 new Store
<PINMessage
>(filename
, false)));
257 else if( name
== SavedMessage::GetDBName() ) {
258 return auto_ptr
<Parser
>(
259 new RecordParser
<SavedMessage
, Store
<SavedMessage
> > (
260 new Store
<SavedMessage
>(filename
, false)));
262 else if( name
== Folder::GetDBName() ) {
263 return auto_ptr
<Parser
>(
264 new RecordParser
<Folder
, Store
<Folder
> > (
265 new Store
<Folder
>(filename
, false)));
267 else if( name
== Timezone::GetDBName() ) {
268 return auto_ptr
<Parser
>(
269 new RecordParser
<Timezone
, Store
<Timezone
> > (
270 new Store
<Timezone
>(filename
, false)));
273 // unknown database, use null parser
274 return auto_ptr
<Parser
>( new DataDumpParser
);
278 auto_ptr
<Builder
> GetBuilder(const string
&name
, const string
&filename
)
280 // check for recognized database names
281 if( name
== Contact::GetDBName() ) {
282 return auto_ptr
<Builder
>(
283 new RecordBuilder
<Contact
, Store
<Contact
> > (
284 new Store
<Contact
>(filename
, true)));
287 else if( name == "Messages" ) {
288 return auto_ptr<Parser>(
289 new RecordParser<Message, Store<Message> > (
290 new Store<Message>(filename, true)));
292 else if( name == "Calendar" ) {
293 return auto_ptr<Parser>(
294 new RecordParser<Calendar, Store<Calendar> > (
295 new Store<Calendar>(filename, true)));
297 else if( name == "Service Book" ) {
298 return auto_ptr<Parser>(
299 new RecordParser<ServiceBook, Store<ServiceBook> > (
300 new Store<ServiceBook>(filename, true)));
302 else if( name == "Memos" ) {
303 return auto_ptr<Parser>(
304 new RecordParser<Memo, Store<Memo> > (
305 new Store<Memo>(filename, true)));
307 else if( name == "Tasks" ) {
308 return auto_ptr<Parser>(
309 new RecordParser<Task, Store<Task> > (
310 new Store<Task>(filename, true)));
314 throw std::runtime_error("No Builder available for database");
320 cout
<< "Supported Database parsers:\n"
328 << " Saved Email Messages\n"
330 << " Time Zones (read only)\n"
332 << "Supported Database builders:\n"
337 struct StateTableCommand
343 StateTableCommand(char f
, bool c
, unsigned int i
)
344 : flag(f
), clear(c
), index(i
) {}
347 bool SplitMap(const string
&map
, string
&ldif
, string
&read
, string
&write
)
349 string::size_type a
= map
.find(',');
350 if( a
== string::npos
)
353 string::size_type b
= map
.find(',', a
+1);
354 if( b
== string::npos
)
357 ldif
.assign(map
, 0, a
);
358 read
.assign(map
, a
+ 1, b
- a
- 1);
359 write
.assign(map
, b
+ 1, map
.size() - b
- 1);
361 return ldif
.size() && read
.size() && write
.size();
364 void DoMapping(ContactLdif
&ldif
, const vector
<string
> &mapCommands
)
366 for( vector
<string
>::const_iterator i
= mapCommands
.begin();
367 i
!= mapCommands
.end();
370 // single names mean unmapping
371 if( i
->find(',') == string::npos
) {
373 cerr
<< "Unmapping: " << *i
<< endl
;
377 cerr
<< "Mapping: " << *i
<< endl
;
379 // map... extract ldif/read/write names
380 string ldifname
, read
, write
;
381 if( SplitMap(*i
, ldifname
, read
, write
) ) {
382 if( !ldif
.Map(ldifname
, read
, write
) ) {
383 cerr
<< "Read/Write name unknown: " << *i
<< endl
;
387 cerr
<< "Invalid map format: " << *i
<< endl
;
393 bool ParseEpOverride(const char *arg
, Usb::EndpointPair
*epp
)
397 istringstream
iss(arg
);
398 iss
>> hex
>> read
>> comma
>> write
;
406 int main(int argc
, char *argv
[])
408 cout
.sync_with_stdio(true); // leave this on, since libusb uses
409 // stdio for debug messages
414 bool list_only
= false,
416 ldif_contacts
= false,
418 reset_device
= false,
419 list_contact_fields
= false,
420 list_ldif_map
= false,
421 epp_override
= false,
422 record_state
= false;
423 string ldifBaseDN
, ldifDnAttr
;
428 vector
<string
> dbNames
, saveDbNames
, mapCommands
;
429 vector
<StateTableCommand
> stCommands
;
430 Usb::EndpointPair epOverride
;
432 // process command line options
434 int cmd
= getopt(argc
, argv
, "B:c:C:d:D:e:f:hlLm:MN:p:P:r:R:Ss:tT:vX");
444 case 'c': // contacts to ldap ldif
445 ldif_contacts
= true;
449 case 'C': // DN Attribute for FQDN
453 case 'd': // show dbname
454 dbNames
.push_back(string(optarg
));
457 case 'D': // delete record
458 stCommands
.push_back(
459 StateTableCommand('D', false, atoi(optarg
)));
462 case 'e': // endpoint override
463 if( !ParseEpOverride(optarg
, &epOverride
) ) {
470 case 'f': // filename
471 #ifdef __BARRY_BOOST_MODE__
474 cerr
<< "-f option not supported - no Boost "
475 "serialization support available\n";
479 case 'l': // list only
483 case 'L': // List Contact field names
484 list_contact_fields
= true;
487 case 'm': // Map / Unmap
488 mapCommands
.push_back(string(optarg
));
491 case 'M': // List LDIF map
492 list_ldif_map
= true;
499 case 'p': // Blackberry PIN
500 pin
= strtoul(optarg
, NULL
, 16);
503 case 'P': // Device password
507 case 'r': // get specific record index
508 stCommands
.push_back(
509 StateTableCommand('r', false, atoi(optarg
)));
512 case 'R': // same as 'r', and clears dirty
513 stCommands
.push_back(
514 StateTableCommand('r', true, atoi(optarg
)));
517 case 's': // save dbname
518 saveDbNames
.push_back(string(optarg
));
521 case 'S': // show supported databases
525 case 't': // display database database
529 case 'T': // show RecordStateTable
531 dbNames
.push_back(string(optarg
));
534 case 'v': // data dump on
538 case 'X': // reset device
549 // Initialize the barry library. Must be called before
551 Barry::Init(data_dump
);
553 // LDIF class... only needed if ldif output turned on
554 ContactLdif
ldif(ldifBaseDN
);
555 DoMapping(ldif
, mapCommands
);
556 if( ldifDnAttr
.size() ) {
557 if( !ldif
.SetDNAttr(ldifDnAttr
) ) {
558 cerr
<< "Unable to set DN Attr: " << ldifDnAttr
<< endl
;
562 // Probe the USB bus for Blackberry devices and display.
563 // If user has specified a PIN, search for it in the
564 // available device list here as well
565 Barry::Probe
probe(busname
.c_str(), devname
.c_str());
566 int activeDevice
= -1;
568 // show any errors during probe first
569 if( probe
.GetFailCount() ) {
572 cout
<< "Blackberry device errors with errors during probe:" << endl
;
573 for( int i
= 0; i
< probe
.GetFailCount(); i
++ ) {
576 cout
<< probe
.GetFailMsg(i
) << endl
;
580 // show all successfully found devices
583 cout
<< "Blackberry devices found:" << endl
;
584 for( int i
= 0; i
< probe
.GetCount(); i
++ ) {
588 probe
.Get(i
).DumpAll(cout
);
590 cout
<< probe
.Get(i
);
592 if( probe
.Get(i
).m_pin
== pin
)
599 if( activeDevice
== -1 ) {
601 // can we default to single device?
602 if( probe
.GetCount() == 1 )
605 cerr
<< "No device selected" << endl
;
610 cerr
<< "PIN " << setbase(16) << pin
611 << " not found" << endl
;
618 cout
<< "Using device (PIN): " << setbase(16)
619 << probe
.Get(activeDevice
).m_pin
<< endl
;
622 Usb::Device
dev(probe
.Get(activeDevice
).m_dev
);
627 // Create our socket router and thread
628 SocketRoutingQueue router
;
629 router
.SpinoffSimpleReadThread();
631 // Create our controller object
632 Barry::ProbeResult device
= probe
.Get(activeDevice
);
634 device
.m_ep
.read
= epOverride
.read
;
635 device
.m_ep
.write
= epOverride
.write
;
636 device
.m_ep
.type
= 2; // FIXME - override this too?
637 cout
<< "Endpoint pair (read,write) overridden with: "
639 << (unsigned int) device
.m_ep
.read
<< ","
640 << (unsigned int) device
.m_ep
.write
<< endl
;
642 Barry::Controller
con(device
, router
);
643 Barry::Mode::Desktop
desktop(con
);
646 // execute each mode that was turned on
650 // Dump list of all databases to stdout
652 // open desktop mode socket
653 desktop
.Open(password
.c_str());
654 cout
<< desktop
.GetDBDB() << endl
;
657 // Dump list of Contact field names
658 if( list_contact_fields
) {
659 for( const ContactLdif::NameToFunc
*n
= ldif
.GetFieldNames(); n
->name
; n
++ ) {
661 cout
<< " " << left
<< setw(20) << n
->name
<< ": "
662 << n
->description
<< endl
;
666 // Dump current LDIF mapping
667 if( list_ldif_map
) {
668 cout
<< ldif
<< endl
;
671 // Dump list of contacts to an LDAP LDIF file
672 // This uses the Controller convenience templates
673 if( ldif_contacts
) {
674 // make sure we're in desktop mode
675 desktop
.Open(password
.c_str());
677 // create a storage functor object that accepts
678 // Barry::Contact objects as input
679 Contact2Ldif
storage(ldif
);
681 // load all the Contact records into storage
682 desktop
.LoadDatabaseByType
<Barry::Contact
>(storage
);
685 // Dump record state table to stdout
687 if( dbNames
.size() == 0 ) {
688 cout
<< "No db names to process" << endl
;
692 desktop
.Open(password
.c_str());
694 vector
<string
>::iterator b
= dbNames
.begin();
695 for( ; b
!= dbNames
.end(); b
++ ) {
696 unsigned int id
= desktop
.GetDBID(*b
);
697 RecordStateTable state
;
698 desktop
.GetRecordStateTable(id
, state
);
699 cout
<< "Record state table for: " << *b
<< endl
;
705 // Get Record mode overrides the default name mode
706 if( stCommands
.size() ) {
707 if( dbNames
.size() != 1 ) {
708 cout
<< "Must have 1 db name to process" << endl
;
712 desktop
.Open(password
.c_str());
713 unsigned int id
= desktop
.GetDBID(dbNames
[0]);
714 auto_ptr
<Parser
> parse
= GetParser(dbNames
[0],filename
);
716 for( unsigned int i
= 0; i
< stCommands
.size(); i
++ ) {
717 desktop
.GetRecord(id
, stCommands
[i
].index
, *parse
.get());
719 if( stCommands
[i
].flag
== 'r' && stCommands
[i
].clear
) {
720 cout
<< "Clearing record's dirty flags..." << endl
;
721 desktop
.ClearDirty(id
, stCommands
[i
].index
);
724 if( stCommands
[i
].flag
== 'D' ) {
725 desktop
.DeleteRecord(id
, stCommands
[i
].index
);
732 // Dump contents of selected databases to stdout, or
733 // to file if specified.
734 // This is retrieving data from the Blackberry.
735 if( dbNames
.size() ) {
736 vector
<string
>::iterator b
= dbNames
.begin();
738 desktop
.Open(password
.c_str());
739 for( ; b
!= dbNames
.end(); b
++ ) {
740 auto_ptr
<Parser
> parse
= GetParser(*b
,filename
);
741 unsigned int id
= desktop
.GetDBID(*b
);
742 desktop
.LoadDatabase(id
, *parse
.get());
746 // Save contents of file to specified databases
747 // This is writing data to the Blackberry.
748 if( saveDbNames
.size() ) {
749 vector
<string
>::iterator b
= saveDbNames
.begin();
751 desktop
.Open(password
.c_str());
752 for( ; b
!= saveDbNames
.end(); b
++ ) {
753 auto_ptr
<Builder
> build
=
754 GetBuilder(*b
, filename
);
755 unsigned int id
= desktop
.GetDBID(*b
);
756 desktop
.SaveDatabase(id
, *build
);
761 catch( Usb::Error
&ue
) {
762 std::cerr
<< "Usb::Error caught: " << ue
.what() << endl
;
765 catch( Barry::Error
&se
) {
766 std::cerr
<< "Barry::Error caught: " << se
.what() << endl
;
769 catch( std::exception
&e
) {
770 std::cerr
<< "std::exception caught: " << e
.what() << endl
;