3 /// Barry library tester
7 Copyright (C) 2005-2009, 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>
34 using namespace Barry
;
39 const char *Version
= Barry::Version(major
, minor
);
42 << "btool - Command line USB Blackberry Test Tool\n"
43 << " Copyright 2005-2009, Net Direct Inc. (http://www.netdirect.ca/)\n"
44 << " Using: " << Version
<< "\n"
46 #ifdef __BARRY_BOOST_MODE__
53 << " -B bus Specify which USB bus to search on\n"
54 << " -N dev Specify which system device, using system specific string\n"
56 << " -c dn Convert address book database to LDIF format, using the\n"
57 << " specified baseDN\n"
58 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
59 << " Defaults to 'cn'\n"
60 << " -d db Load database 'db' FROM device and dump to screen\n"
61 << " Can be used multiple times to fetch more than one DB\n"
62 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
63 << " string separated by a comma, holding the read,write\n"
64 << " endpoint pair. Example: -e 83,5\n"
65 << " Note: Endpoints are specified in hex.\n"
66 << " You should never need to use this option.\n"
67 #ifdef __BARRY_BOOST_MODE__
68 << " -f file Filename to save or load handheld data to/from\n"
71 << " -i cs International charset for string conversions\n"
72 << " Valid values here are available with 'iconv --list'\n"
73 << " -l List devices\n"
74 << " -L List Contact field names\n"
75 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
76 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
77 << " Unmap: ldif name alone\n"
78 << " -M List current LDIF mapping\n"
79 << " -n Use null parser on all databases.\n"
80 << " -p pin PIN of device to talk with\n"
81 << " If only one device is plugged in, this flag is optional\n"
82 << " -P pass Simplistic method to specify device password\n"
83 << " -s db Save database 'db' TO device from data loaded from -f file\n"
84 << " -S Show list of supported database parsers\n"
85 << " -t Show database database table\n"
86 << " -T db Show record state table for given database\n"
87 << " -v Dump protocol data during operation\n"
88 << " -X Reset device\n"
89 << " -z Use non-threaded sockets\n"
90 << " -Z Use threaded socket router (default)\n"
92 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
94 << " -r # Record index number as seen in the -T state table.\n"
95 << " This overrides the default -d behaviour, and only\n"
96 << " downloads the one specified record, sending to stdout.\n"
97 << " -R # Same as -r, but also clears the record's dirty flags.\n"
98 << " -D # Record index number as seen in the -T state table,\n"
99 << " which indicates the record to delete. Used with the -d\n"
100 << " command to specify the database.\n"
107 Barry::ContactLdif
&ldif
;
109 Contact2Ldif(Barry::ContactLdif
&ldif
) : ldif(ldif
) {}
111 void operator()(const Contact
&rec
)
113 ldif
.DumpLdif(cout
, rec
);
117 template <class Record
>
120 std::vector
<Record
> records
;
121 mutable typename
std::vector
<Record
>::const_iterator rec_it
;
122 std::string filename
;
126 Store(const string
&filename
, bool load
)
127 : rec_it(records
.end()),
132 #ifdef __BARRY_BOOST_MODE__
135 if( load
&& filename
.size() ) {
136 // filename is available, attempt to load
137 cout
<< "Loading: " << filename
<< endl
;
138 ifstream
ifs(filename
.c_str());
140 getline(ifs
, dbName
);
141 boost::archive::text_iarchive
ia(ifs
);
143 cout
<< records
.size()
144 << " records loaded from '"
145 << filename
<< "'" << endl
;
146 sort(records
.begin(), records
.end());
147 rec_it
= records
.begin();
150 typename
std::vector
<Record
>::const_iterator beg
= records
.begin(), end
= records
.end();
151 for( ; beg
!= end
; beg
++ ) {
152 cout
<< (*beg
) << endl
;
156 } catch( boost::archive::archive_exception
&ae
) {
157 cerr
<< "Archive exception in ~Store(): "
158 << ae
.what() << endl
;
164 cout
<< "Store counted " << dec
<< count
<< " records." << endl
;
165 #ifdef __BARRY_BOOST_MODE__
168 if( !load
&& filename
.size() ) {
169 // filename is available, attempt to save
170 cout
<< "Saving: " << filename
<< endl
;
171 const std::vector
<Record
> &r
= records
;
172 ofstream
ofs(filename
.c_str());
173 ofs
<< Record::GetDBName() << endl
;
174 boost::archive::text_oarchive
oa(ofs
);
176 cout
<< dec
<< r
.size() << " records saved to '"
177 << filename
<< "'" << endl
;
180 } catch( boost::archive::archive_exception
&ae
) {
181 cerr
<< "Archive exception in ~Store(): "
182 << ae
.what() << endl
;
188 void operator()(const Record
&rec
)
191 std::cout
<< rec
<< std::endl
;
192 records
.push_back(rec
);
195 // retrieval operator
196 bool operator()(Record
&rec
, unsigned int databaseId
) const
198 if( rec_it
== records
.end() )
206 class DataDumpParser
: public Barry::Parser
211 virtual void Clear() {}
213 virtual void SetIds(uint8_t RecType
, uint32_t UniqueId
)
218 virtual void ParseHeader(const Data
&, size_t &) {}
220 virtual void ParseFields(const Barry::Data
&data
, size_t &offset
,
221 const IConverter
*ic
)
223 std::cout
<< "Raw record dump for record: "
224 << std::hex
<< m_id
<< std::endl
;
225 std::cout
<< data
<< std::endl
;
228 virtual void Store() {}
231 auto_ptr
<Parser
> GetParser(const string
&name
, const string
&filename
, bool null_parser
)
235 return auto_ptr
<Parser
>( new DataDumpParser
);
237 // check for recognized database names
238 else if( name
== Contact::GetDBName() ) {
239 return auto_ptr
<Parser
>(
240 new RecordParser
<Contact
, Store
<Contact
> > (
241 new Store
<Contact
>(filename
, false)));
243 else if( name
== Message::GetDBName() ) {
244 return auto_ptr
<Parser
>(
245 new RecordParser
<Message
, Store
<Message
> > (
246 new Store
<Message
>(filename
, false)));
248 else if( name
== Calendar::GetDBName() ) {
249 return auto_ptr
<Parser
>(
250 new RecordParser
<Calendar
, Store
<Calendar
> > (
251 new Store
<Calendar
>(filename
, false)));
253 else if( name
== ServiceBook::GetDBName() ) {
254 return auto_ptr
<Parser
>(
255 new RecordParser
<ServiceBook
, Store
<ServiceBook
> > (
256 new Store
<ServiceBook
>(filename
, false)));
259 else if( name
== Memo::GetDBName() ) {
260 return auto_ptr
<Parser
>(
261 new RecordParser
<Memo
, Store
<Memo
> > (
262 new Store
<Memo
>(filename
, false)));
264 else if( name
== Task::GetDBName() ) {
265 return auto_ptr
<Parser
>(
266 new RecordParser
<Task
, Store
<Task
> > (
267 new Store
<Task
>(filename
, false)));
269 else if( name
== PINMessage::GetDBName() ) {
270 return auto_ptr
<Parser
>(
271 new RecordParser
<PINMessage
, Store
<PINMessage
> > (
272 new Store
<PINMessage
>(filename
, false)));
274 else if( name
== SavedMessage::GetDBName() ) {
275 return auto_ptr
<Parser
>(
276 new RecordParser
<SavedMessage
, Store
<SavedMessage
> > (
277 new Store
<SavedMessage
>(filename
, false)));
279 else if( name
== Folder::GetDBName() ) {
280 return auto_ptr
<Parser
>(
281 new RecordParser
<Folder
, Store
<Folder
> > (
282 new Store
<Folder
>(filename
, false)));
284 else if( name
== Timezone::GetDBName() ) {
285 return auto_ptr
<Parser
>(
286 new RecordParser
<Timezone
, Store
<Timezone
> > (
287 new Store
<Timezone
>(filename
, false)));
290 // unknown database, use null parser
291 return auto_ptr
<Parser
>( new DataDumpParser
);
295 auto_ptr
<Builder
> GetBuilder(const string
&name
, const string
&filename
)
297 // check for recognized database names
298 if( name
== Contact::GetDBName() ) {
299 return auto_ptr
<Builder
>(
300 new RecordBuilder
<Contact
, Store
<Contact
> > (
301 new Store
<Contact
>(filename
, true)));
303 else if( name
== Calendar::GetDBName() ) {
304 return auto_ptr
<Builder
>(
305 new RecordBuilder
<Calendar
, Store
<Calendar
> > (
306 new Store
<Calendar
>(filename
, true)));
309 else if( name == "Messages" ) {
310 return auto_ptr<Parser>(
311 new RecordParser<Message, Store<Message> > (
312 new Store<Message>(filename, true)));
314 else if( name == "Service Book" ) {
315 return auto_ptr<Parser>(
316 new RecordParser<ServiceBook, Store<ServiceBook> > (
317 new Store<ServiceBook>(filename, true)));
319 else if( name == "Memos" ) {
320 return auto_ptr<Parser>(
321 new RecordParser<Memo, Store<Memo> > (
322 new Store<Memo>(filename, true)));
324 else if( name == "Tasks" ) {
325 return auto_ptr<Parser>(
326 new RecordParser<Task, Store<Task> > (
327 new Store<Task>(filename, true)));
331 throw std::runtime_error("No Builder available for database");
337 cout
<< "Supported Database parsers:\n"
345 << " Saved Email Messages\n"
347 << " Time Zones (read only)\n"
349 << "Supported Database builders:\n"
355 struct StateTableCommand
361 StateTableCommand(char f
, bool c
, unsigned int i
)
362 : flag(f
), clear(c
), index(i
) {}
365 bool SplitMap(const string
&map
, string
&ldif
, string
&read
, string
&write
)
367 string::size_type a
= map
.find(',');
368 if( a
== string::npos
)
371 string::size_type b
= map
.find(',', a
+1);
372 if( b
== string::npos
)
375 ldif
.assign(map
, 0, a
);
376 read
.assign(map
, a
+ 1, b
- a
- 1);
377 write
.assign(map
, b
+ 1, map
.size() - b
- 1);
379 return ldif
.size() && read
.size() && write
.size();
382 void DoMapping(ContactLdif
&ldif
, const vector
<string
> &mapCommands
)
384 for( vector
<string
>::const_iterator i
= mapCommands
.begin();
385 i
!= mapCommands
.end();
388 // single names mean unmapping
389 if( i
->find(',') == string::npos
) {
391 cerr
<< "Unmapping: " << *i
<< endl
;
395 cerr
<< "Mapping: " << *i
<< endl
;
397 // map... extract ldif/read/write names
398 string ldifname
, read
, write
;
399 if( SplitMap(*i
, ldifname
, read
, write
) ) {
400 if( !ldif
.Map(ldifname
, read
, write
) ) {
401 cerr
<< "Read/Write name unknown: " << *i
<< endl
;
405 cerr
<< "Invalid map format: " << *i
<< endl
;
411 bool ParseEpOverride(const char *arg
, Usb::EndpointPair
*epp
)
415 istringstream
iss(arg
);
416 iss
>> hex
>> read
>> comma
>> write
;
424 int main(int argc
, char *argv
[])
426 cout
.sync_with_stdio(true); // leave this on, since libusb uses
427 // stdio for debug messages
432 bool list_only
= false,
434 ldif_contacts
= false,
436 reset_device
= false,
437 list_contact_fields
= false,
438 list_ldif_map
= false,
439 epp_override
= false,
440 threaded_sockets
= true,
441 record_state
= false,
443 string ldifBaseDN
, ldifDnAttr
;
449 vector
<string
> dbNames
, saveDbNames
, mapCommands
;
450 vector
<StateTableCommand
> stCommands
;
451 Usb::EndpointPair epOverride
;
453 // process command line options
455 int cmd
= getopt(argc
, argv
, "B:c:C:d:D:e:f:hi:lLm:MnN:p:P:r:R:Ss:tT:vXzZ");
465 case 'c': // contacts to ldap ldif
466 ldif_contacts
= true;
470 case 'C': // DN Attribute for FQDN
474 case 'd': // show dbname
475 dbNames
.push_back(string(optarg
));
478 case 'D': // delete record
479 stCommands
.push_back(
480 StateTableCommand('D', false, atoi(optarg
)));
483 case 'e': // endpoint override
484 if( !ParseEpOverride(optarg
, &epOverride
) ) {
491 case 'f': // filename
492 #ifdef __BARRY_BOOST_MODE__
495 cerr
<< "-f option not supported - no Boost "
496 "serialization support available\n";
501 case 'i': // international charset (iconv)
502 iconvCharset
= optarg
;
505 case 'l': // list only
509 case 'L': // List Contact field names
510 list_contact_fields
= true;
513 case 'm': // Map / Unmap
514 mapCommands
.push_back(string(optarg
));
517 case 'M': // List LDIF map
518 list_ldif_map
= true;
521 case 'n': // use null parser
529 case 'p': // Blackberry PIN
530 pin
= strtoul(optarg
, NULL
, 16);
533 case 'P': // Device password
537 case 'r': // get specific record index
538 stCommands
.push_back(
539 StateTableCommand('r', false, atoi(optarg
)));
542 case 'R': // same as 'r', and clears dirty
543 stCommands
.push_back(
544 StateTableCommand('r', true, atoi(optarg
)));
547 case 's': // save dbname
548 saveDbNames
.push_back(string(optarg
));
551 case 'S': // show supported databases
555 case 't': // display database database
559 case 'T': // show RecordStateTable
561 dbNames
.push_back(string(optarg
));
564 case 'v': // data dump on
568 case 'X': // reset device
572 case 'z': // non-threaded sockets
573 threaded_sockets
= false;
576 case 'Z': // threaded socket router
577 threaded_sockets
= true;
587 // Initialize the barry library. Must be called before
589 Barry::Init(data_dump
);
591 // Create an IConverter object if needed
592 auto_ptr
<IConverter
> ic
;
593 if( iconvCharset
.size() ) {
594 ic
.reset( new IConverter(iconvCharset
.c_str(), true) );
597 // LDIF class... only needed if ldif output turned on
598 ContactLdif
ldif(ldifBaseDN
);
599 DoMapping(ldif
, mapCommands
);
600 if( ldifDnAttr
.size() ) {
601 if( !ldif
.SetDNAttr(ldifDnAttr
) ) {
602 cerr
<< "Unable to set DN Attr: " << ldifDnAttr
<< endl
;
606 // Probe the USB bus for Blackberry devices and display.
607 // If user has specified a PIN, search for it in the
608 // available device list here as well
609 Barry::Probe
probe(busname
.c_str(), devname
.c_str());
610 int activeDevice
= -1;
612 // show any errors during probe first
613 if( probe
.GetFailCount() ) {
616 cout
<< "Blackberry device errors with errors during probe:" << endl
;
617 for( int i
= 0; i
< probe
.GetFailCount(); i
++ ) {
620 cout
<< probe
.GetFailMsg(i
) << endl
;
624 // show all successfully found devices
627 cout
<< "Blackberry devices found:" << endl
;
628 for( int i
= 0; i
< probe
.GetCount(); i
++ ) {
632 probe
.Get(i
).DumpAll(cout
);
634 cout
<< probe
.Get(i
);
636 if( probe
.Get(i
).m_pin
== pin
)
643 if( activeDevice
== -1 ) {
645 // can we default to single device?
646 if( probe
.GetCount() == 1 )
649 cerr
<< "No device selected" << endl
;
654 cerr
<< "PIN " << setbase(16) << pin
655 << " not found" << endl
;
662 cout
<< "Using device (PIN): " << setbase(16)
663 << probe
.Get(activeDevice
).m_pin
<< endl
;
666 Usb::Device
dev(probe
.Get(activeDevice
).m_dev
);
671 // Override device endpoints if user asks
672 Barry::ProbeResult device
= probe
.Get(activeDevice
);
674 device
.m_ep
.read
= epOverride
.read
;
675 device
.m_ep
.write
= epOverride
.write
;
676 device
.m_ep
.type
= 2; // FIXME - override this too?
677 cout
<< "Endpoint pair (read,write) overridden with: "
679 << (unsigned int) device
.m_ep
.read
<< ","
680 << (unsigned int) device
.m_ep
.write
<< endl
;
684 // Create our controller object
686 // Order is important in the following auto_ptr<> objects,
687 // since Controller must get destroyed before router.
688 // Normally you'd pick one method, and not bother
689 // with auto_ptr<> and so the normal C++ constructor
690 // rules would guarantee this safety for you, but
691 // here we want the user to pick.
693 auto_ptr
<SocketRoutingQueue
> router
;
694 auto_ptr
<Barry::Controller
> pcon
;
695 if( threaded_sockets
) {
696 router
.reset( new SocketRoutingQueue
);
697 router
->SpinoffSimpleReadThread();
698 pcon
.reset( new Barry::Controller(device
, *router
) );
701 pcon
.reset( new Barry::Controller(device
) );
704 Barry::Controller
&con
= *pcon
;
705 Barry::Mode::Desktop
desktop(con
, *ic
);
708 // execute each mode that was turned on
712 // Dump list of all databases to stdout
714 // open desktop mode socket
715 desktop
.Open(password
.c_str());
716 cout
<< desktop
.GetDBDB() << endl
;
719 // Dump list of Contact field names
720 if( list_contact_fields
) {
721 for( const ContactLdif::NameToFunc
*n
= ldif
.GetFieldNames(); n
->name
; n
++ ) {
723 cout
<< " " << left
<< setw(20) << n
->name
<< ": "
724 << n
->description
<< endl
;
728 // Dump current LDIF mapping
729 if( list_ldif_map
) {
730 cout
<< ldif
<< endl
;
733 // Dump list of contacts to an LDAP LDIF file
734 // This uses the Controller convenience templates
735 if( ldif_contacts
) {
736 // make sure we're in desktop mode
737 desktop
.Open(password
.c_str());
739 // create a storage functor object that accepts
740 // Barry::Contact objects as input
741 Contact2Ldif
storage(ldif
);
743 // load all the Contact records into storage
744 desktop
.LoadDatabaseByType
<Barry::Contact
>(storage
);
747 // Dump record state table to stdout
749 if( dbNames
.size() == 0 ) {
750 cout
<< "No db names to process" << endl
;
754 desktop
.Open(password
.c_str());
756 vector
<string
>::iterator b
= dbNames
.begin();
757 for( ; b
!= dbNames
.end(); b
++ ) {
758 unsigned int id
= desktop
.GetDBID(*b
);
759 RecordStateTable state
;
760 desktop
.GetRecordStateTable(id
, state
);
761 cout
<< "Record state table for: " << *b
<< endl
;
767 // Get Record mode overrides the default name mode
768 if( stCommands
.size() ) {
769 if( dbNames
.size() != 1 ) {
770 cout
<< "Must have 1 db name to process" << endl
;
774 desktop
.Open(password
.c_str());
775 unsigned int id
= desktop
.GetDBID(dbNames
[0]);
776 auto_ptr
<Parser
> parse
= GetParser(dbNames
[0],filename
,null_parser
);
778 for( unsigned int i
= 0; i
< stCommands
.size(); i
++ ) {
779 desktop
.GetRecord(id
, stCommands
[i
].index
, *parse
.get());
781 if( stCommands
[i
].flag
== 'r' && stCommands
[i
].clear
) {
782 cout
<< "Clearing record's dirty flags..." << endl
;
783 desktop
.ClearDirty(id
, stCommands
[i
].index
);
786 if( stCommands
[i
].flag
== 'D' ) {
787 desktop
.DeleteRecord(id
, stCommands
[i
].index
);
794 // Dump contents of selected databases to stdout, or
795 // to file if specified.
796 // This is retrieving data from the Blackberry.
797 if( dbNames
.size() ) {
798 vector
<string
>::iterator b
= dbNames
.begin();
800 desktop
.Open(password
.c_str());
801 for( ; b
!= dbNames
.end(); b
++ ) {
802 auto_ptr
<Parser
> parse
= GetParser(*b
,filename
,null_parser
);
803 unsigned int id
= desktop
.GetDBID(*b
);
804 desktop
.LoadDatabase(id
, *parse
.get());
808 // Save contents of file to specified databases
809 // This is writing data to the Blackberry.
810 if( saveDbNames
.size() ) {
811 vector
<string
>::iterator b
= saveDbNames
.begin();
813 desktop
.Open(password
.c_str());
814 for( ; b
!= saveDbNames
.end(); b
++ ) {
815 auto_ptr
<Builder
> build
=
816 GetBuilder(*b
, filename
);
817 unsigned int id
= desktop
.GetDBID(*b
);
818 desktop
.SaveDatabase(id
, *build
);
823 catch( Usb::Error
&ue
) {
824 std::cerr
<< "Usb::Error caught: " << ue
.what() << endl
;
827 catch( Barry::Error
&se
) {
828 std::cerr
<< "Barry::Error caught: " << se
.what() << endl
;
831 catch( std::exception
&e
) {
832 std::cerr
<< "std::exception caught: " << e
.what() << endl
;