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>
34 using namespace Barry
;
39 const char *Version
= Barry::Version(major
, minor
);
42 << "btool - Command line USB Blackberry Test Tool\n"
43 << " Copyright 2005-2008, 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 << " -l List devices\n"
72 << " -L List Contact field names\n"
73 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
74 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
75 << " Unmap: ldif name alone\n"
76 << " -M List current LDIF mapping\n"
77 << " -p pin PIN of device to talk with\n"
78 << " If only one device is plugged in, this flag is optional\n"
79 << " -P pass Simplistic method to specify device password\n"
80 << " -s db Save database 'db' TO device from data loaded from -f file\n"
81 << " -S Show list of supported database parsers\n"
82 << " -t Show database database table\n"
83 << " -T db Show record state table for given database\n"
84 << " -v Dump protocol data during operation\n"
85 << " -X Reset device\n"
86 << " -z Use non-threaded sockets\n"
87 << " -Z Use threaded socket router (default)\n"
89 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
91 << " -r # Record index number as seen in the -T state table.\n"
92 << " This overrides the default -d behaviour, and only\n"
93 << " downloads the one specified record, sending to stdout.\n"
94 << " -R # Same as -r, but also clears the record's dirty flags.\n"
95 << " -D # Record index number as seen in the -T state table,\n"
96 << " which indicates the record to delete. Used with the -d\n"
97 << " command to specify the database.\n"
104 Barry::ContactLdif
&ldif
;
106 Contact2Ldif(Barry::ContactLdif
&ldif
) : ldif(ldif
) {}
108 void operator()(const Contact
&rec
)
110 ldif
.DumpLdif(cout
, rec
);
114 template <class Record
>
117 std::vector
<Record
> records
;
118 mutable typename
std::vector
<Record
>::const_iterator rec_it
;
119 std::string filename
;
123 Store(const string
&filename
, bool load
)
124 : rec_it(records
.end()),
129 #ifdef __BARRY_BOOST_MODE__
132 if( load
&& filename
.size() ) {
133 // filename is available, attempt to load
134 cout
<< "Loading: " << filename
<< endl
;
135 ifstream
ifs(filename
.c_str());
137 getline(ifs
, dbName
);
138 boost::archive::text_iarchive
ia(ifs
);
140 cout
<< records
.size()
141 << " records loaded from '"
142 << filename
<< "'" << endl
;
143 sort(records
.begin(), records
.end());
144 rec_it
= records
.begin();
147 typename
std::vector
<Record
>::const_iterator beg
= records
.begin(), end
= records
.end();
148 for( ; beg
!= end
; beg
++ ) {
149 cout
<< (*beg
) << endl
;
153 } catch( boost::archive::archive_exception
&ae
) {
154 cerr
<< "Archive exception in ~Store(): "
155 << ae
.what() << endl
;
161 cout
<< "Store counted " << dec
<< count
<< " records." << endl
;
162 #ifdef __BARRY_BOOST_MODE__
165 if( !load
&& filename
.size() ) {
166 // filename is available, attempt to save
167 cout
<< "Saving: " << filename
<< endl
;
168 const std::vector
<Record
> &r
= records
;
169 ofstream
ofs(filename
.c_str());
170 ofs
<< Record::GetDBName() << endl
;
171 boost::archive::text_oarchive
oa(ofs
);
173 cout
<< dec
<< r
.size() << " records saved to '"
174 << filename
<< "'" << endl
;
177 } catch( boost::archive::archive_exception
&ae
) {
178 cerr
<< "Archive exception in ~Store(): "
179 << ae
.what() << endl
;
185 void operator()(const Record
&rec
)
188 std::cout
<< rec
<< std::endl
;
189 records
.push_back(rec
);
192 // retrieval operator
193 bool operator()(Record
&rec
, unsigned int databaseId
) const
195 if( rec_it
== records
.end() )
203 class DataDumpParser
: public Barry::Parser
208 virtual void SetUniqueId(uint32_t id
)
213 virtual void ParseFields(const Barry::Data
&data
, size_t &offset
)
215 std::cout
<< "Raw record dump for record: "
216 << std::hex
<< m_id
<< std::endl
;
217 std::cout
<< data
<< std::endl
;
221 auto_ptr
<Parser
> GetParser(const string
&name
, const string
&filename
)
223 // check for recognized database names
224 if( name
== Contact::GetDBName() ) {
225 return auto_ptr
<Parser
>(
226 new RecordParser
<Contact
, Store
<Contact
> > (
227 new Store
<Contact
>(filename
, false)));
229 else if( name
== Message::GetDBName() ) {
230 return auto_ptr
<Parser
>(
231 new RecordParser
<Message
, Store
<Message
> > (
232 new Store
<Message
>(filename
, false)));
234 else if( name
== Calendar::GetDBName() ) {
235 return auto_ptr
<Parser
>(
236 new RecordParser
<Calendar
, Store
<Calendar
> > (
237 new Store
<Calendar
>(filename
, false)));
239 else if( name
== ServiceBook::GetDBName() ) {
240 return auto_ptr
<Parser
>(
241 new RecordParser
<ServiceBook
, Store
<ServiceBook
> > (
242 new Store
<ServiceBook
>(filename
, false)));
245 else if( name
== Memo::GetDBName() ) {
246 return auto_ptr
<Parser
>(
247 new RecordParser
<Memo
, Store
<Memo
> > (
248 new Store
<Memo
>(filename
, false)));
250 else if( name
== Task::GetDBName() ) {
251 return auto_ptr
<Parser
>(
252 new RecordParser
<Task
, Store
<Task
> > (
253 new Store
<Task
>(filename
, false)));
255 else if( name
== PINMessage::GetDBName() ) {
256 return auto_ptr
<Parser
>(
257 new RecordParser
<PINMessage
, Store
<PINMessage
> > (
258 new Store
<PINMessage
>(filename
, false)));
260 else if( name
== SavedMessage::GetDBName() ) {
261 return auto_ptr
<Parser
>(
262 new RecordParser
<SavedMessage
, Store
<SavedMessage
> > (
263 new Store
<SavedMessage
>(filename
, false)));
265 else if( name
== Folder::GetDBName() ) {
266 return auto_ptr
<Parser
>(
267 new RecordParser
<Folder
, Store
<Folder
> > (
268 new Store
<Folder
>(filename
, false)));
270 else if( name
== Timezone::GetDBName() ) {
271 return auto_ptr
<Parser
>(
272 new RecordParser
<Timezone
, Store
<Timezone
> > (
273 new Store
<Timezone
>(filename
, false)));
276 // unknown database, use null parser
277 return auto_ptr
<Parser
>( new DataDumpParser
);
281 auto_ptr
<Builder
> GetBuilder(const string
&name
, const string
&filename
)
283 // check for recognized database names
284 if( name
== Contact::GetDBName() ) {
285 return auto_ptr
<Builder
>(
286 new RecordBuilder
<Contact
, Store
<Contact
> > (
287 new Store
<Contact
>(filename
, true)));
289 else if( name
== Calendar::GetDBName() ) {
290 return auto_ptr
<Builder
>(
291 new RecordBuilder
<Calendar
, Store
<Calendar
> > (
292 new Store
<Calendar
>(filename
, true)));
295 else if( name == "Messages" ) {
296 return auto_ptr<Parser>(
297 new RecordParser<Message, Store<Message> > (
298 new Store<Message>(filename, true)));
300 else if( name == "Service Book" ) {
301 return auto_ptr<Parser>(
302 new RecordParser<ServiceBook, Store<ServiceBook> > (
303 new Store<ServiceBook>(filename, true)));
305 else if( name == "Memos" ) {
306 return auto_ptr<Parser>(
307 new RecordParser<Memo, Store<Memo> > (
308 new Store<Memo>(filename, true)));
310 else if( name == "Tasks" ) {
311 return auto_ptr<Parser>(
312 new RecordParser<Task, Store<Task> > (
313 new Store<Task>(filename, true)));
317 throw std::runtime_error("No Builder available for database");
323 cout
<< "Supported Database parsers:\n"
331 << " Saved Email Messages\n"
333 << " Time Zones (read only)\n"
335 << "Supported Database builders:\n"
341 struct StateTableCommand
347 StateTableCommand(char f
, bool c
, unsigned int i
)
348 : flag(f
), clear(c
), index(i
) {}
351 bool SplitMap(const string
&map
, string
&ldif
, string
&read
, string
&write
)
353 string::size_type a
= map
.find(',');
354 if( a
== string::npos
)
357 string::size_type b
= map
.find(',', a
+1);
358 if( b
== string::npos
)
361 ldif
.assign(map
, 0, a
);
362 read
.assign(map
, a
+ 1, b
- a
- 1);
363 write
.assign(map
, b
+ 1, map
.size() - b
- 1);
365 return ldif
.size() && read
.size() && write
.size();
368 void DoMapping(ContactLdif
&ldif
, const vector
<string
> &mapCommands
)
370 for( vector
<string
>::const_iterator i
= mapCommands
.begin();
371 i
!= mapCommands
.end();
374 // single names mean unmapping
375 if( i
->find(',') == string::npos
) {
377 cerr
<< "Unmapping: " << *i
<< endl
;
381 cerr
<< "Mapping: " << *i
<< endl
;
383 // map... extract ldif/read/write names
384 string ldifname
, read
, write
;
385 if( SplitMap(*i
, ldifname
, read
, write
) ) {
386 if( !ldif
.Map(ldifname
, read
, write
) ) {
387 cerr
<< "Read/Write name unknown: " << *i
<< endl
;
391 cerr
<< "Invalid map format: " << *i
<< endl
;
397 bool ParseEpOverride(const char *arg
, Usb::EndpointPair
*epp
)
401 istringstream
iss(arg
);
402 iss
>> hex
>> read
>> comma
>> write
;
410 int main(int argc
, char *argv
[])
412 cout
.sync_with_stdio(true); // leave this on, since libusb uses
413 // stdio for debug messages
418 bool list_only
= false,
420 ldif_contacts
= false,
422 reset_device
= false,
423 list_contact_fields
= false,
424 list_ldif_map
= false,
425 epp_override
= false,
426 threaded_sockets
= true,
427 record_state
= false;
428 string ldifBaseDN
, ldifDnAttr
;
433 vector
<string
> dbNames
, saveDbNames
, mapCommands
;
434 vector
<StateTableCommand
> stCommands
;
435 Usb::EndpointPair epOverride
;
437 // process command line options
439 int cmd
= getopt(argc
, argv
, "B:c:C:d:D:e:f:hlLm:MN:p:P:r:R:Ss:tT:vXzZ");
449 case 'c': // contacts to ldap ldif
450 ldif_contacts
= true;
454 case 'C': // DN Attribute for FQDN
458 case 'd': // show dbname
459 dbNames
.push_back(string(optarg
));
462 case 'D': // delete record
463 stCommands
.push_back(
464 StateTableCommand('D', false, atoi(optarg
)));
467 case 'e': // endpoint override
468 if( !ParseEpOverride(optarg
, &epOverride
) ) {
475 case 'f': // filename
476 #ifdef __BARRY_BOOST_MODE__
479 cerr
<< "-f option not supported - no Boost "
480 "serialization support available\n";
484 case 'l': // list only
488 case 'L': // List Contact field names
489 list_contact_fields
= true;
492 case 'm': // Map / Unmap
493 mapCommands
.push_back(string(optarg
));
496 case 'M': // List LDIF map
497 list_ldif_map
= true;
504 case 'p': // Blackberry PIN
505 pin
= strtoul(optarg
, NULL
, 16);
508 case 'P': // Device password
512 case 'r': // get specific record index
513 stCommands
.push_back(
514 StateTableCommand('r', false, atoi(optarg
)));
517 case 'R': // same as 'r', and clears dirty
518 stCommands
.push_back(
519 StateTableCommand('r', true, atoi(optarg
)));
522 case 's': // save dbname
523 saveDbNames
.push_back(string(optarg
));
526 case 'S': // show supported databases
530 case 't': // display database database
534 case 'T': // show RecordStateTable
536 dbNames
.push_back(string(optarg
));
539 case 'v': // data dump on
543 case 'X': // reset device
547 case 'z': // non-threaded sockets
548 threaded_sockets
= false;
551 case 'Z': // threaded socket router
552 threaded_sockets
= true;
562 // Initialize the barry library. Must be called before
564 Barry::Init(data_dump
);
566 // LDIF class... only needed if ldif output turned on
567 ContactLdif
ldif(ldifBaseDN
);
568 DoMapping(ldif
, mapCommands
);
569 if( ldifDnAttr
.size() ) {
570 if( !ldif
.SetDNAttr(ldifDnAttr
) ) {
571 cerr
<< "Unable to set DN Attr: " << ldifDnAttr
<< endl
;
575 // Probe the USB bus for Blackberry devices and display.
576 // If user has specified a PIN, search for it in the
577 // available device list here as well
578 Barry::Probe
probe(busname
.c_str(), devname
.c_str());
579 int activeDevice
= -1;
581 // show any errors during probe first
582 if( probe
.GetFailCount() ) {
585 cout
<< "Blackberry device errors with errors during probe:" << endl
;
586 for( int i
= 0; i
< probe
.GetFailCount(); i
++ ) {
589 cout
<< probe
.GetFailMsg(i
) << endl
;
593 // show all successfully found devices
596 cout
<< "Blackberry devices found:" << endl
;
597 for( int i
= 0; i
< probe
.GetCount(); i
++ ) {
601 probe
.Get(i
).DumpAll(cout
);
603 cout
<< probe
.Get(i
);
605 if( probe
.Get(i
).m_pin
== pin
)
612 if( activeDevice
== -1 ) {
614 // can we default to single device?
615 if( probe
.GetCount() == 1 )
618 cerr
<< "No device selected" << endl
;
623 cerr
<< "PIN " << setbase(16) << pin
624 << " not found" << endl
;
631 cout
<< "Using device (PIN): " << setbase(16)
632 << probe
.Get(activeDevice
).m_pin
<< endl
;
635 Usb::Device
dev(probe
.Get(activeDevice
).m_dev
);
640 // Override device endpoints if user asks
641 Barry::ProbeResult device
= probe
.Get(activeDevice
);
643 device
.m_ep
.read
= epOverride
.read
;
644 device
.m_ep
.write
= epOverride
.write
;
645 device
.m_ep
.type
= 2; // FIXME - override this too?
646 cout
<< "Endpoint pair (read,write) overridden with: "
648 << (unsigned int) device
.m_ep
.read
<< ","
649 << (unsigned int) device
.m_ep
.write
<< endl
;
653 // Create our controller object
655 // Order is important in the following auto_ptr<> objects,
656 // since Controller must get destroyed before router.
657 // Normally you'd pick one method, and not bother
658 // with auto_ptr<> and so the normal C++ constructor
659 // rules would guarantee this safety for you, but
660 // here we want the user to pick.
662 auto_ptr
<SocketRoutingQueue
> router
;
663 auto_ptr
<Barry::Controller
> pcon
;
664 if( threaded_sockets
) {
665 router
.reset( new SocketRoutingQueue
);
666 router
->SpinoffSimpleReadThread();
667 pcon
.reset( new Barry::Controller(device
, *router
) );
670 pcon
.reset( new Barry::Controller(device
) );
673 Barry::Controller
&con
= *pcon
;
674 Barry::Mode::Desktop
desktop(con
);
677 // execute each mode that was turned on
681 // Dump list of all databases to stdout
683 // open desktop mode socket
684 desktop
.Open(password
.c_str());
685 cout
<< desktop
.GetDBDB() << endl
;
688 // Dump list of Contact field names
689 if( list_contact_fields
) {
690 for( const ContactLdif::NameToFunc
*n
= ldif
.GetFieldNames(); n
->name
; n
++ ) {
692 cout
<< " " << left
<< setw(20) << n
->name
<< ": "
693 << n
->description
<< endl
;
697 // Dump current LDIF mapping
698 if( list_ldif_map
) {
699 cout
<< ldif
<< endl
;
702 // Dump list of contacts to an LDAP LDIF file
703 // This uses the Controller convenience templates
704 if( ldif_contacts
) {
705 // make sure we're in desktop mode
706 desktop
.Open(password
.c_str());
708 // create a storage functor object that accepts
709 // Barry::Contact objects as input
710 Contact2Ldif
storage(ldif
);
712 // load all the Contact records into storage
713 desktop
.LoadDatabaseByType
<Barry::Contact
>(storage
);
716 // Dump record state table to stdout
718 if( dbNames
.size() == 0 ) {
719 cout
<< "No db names to process" << endl
;
723 desktop
.Open(password
.c_str());
725 vector
<string
>::iterator b
= dbNames
.begin();
726 for( ; b
!= dbNames
.end(); b
++ ) {
727 unsigned int id
= desktop
.GetDBID(*b
);
728 RecordStateTable state
;
729 desktop
.GetRecordStateTable(id
, state
);
730 cout
<< "Record state table for: " << *b
<< endl
;
736 // Get Record mode overrides the default name mode
737 if( stCommands
.size() ) {
738 if( dbNames
.size() != 1 ) {
739 cout
<< "Must have 1 db name to process" << endl
;
743 desktop
.Open(password
.c_str());
744 unsigned int id
= desktop
.GetDBID(dbNames
[0]);
745 auto_ptr
<Parser
> parse
= GetParser(dbNames
[0],filename
);
747 for( unsigned int i
= 0; i
< stCommands
.size(); i
++ ) {
748 desktop
.GetRecord(id
, stCommands
[i
].index
, *parse
.get());
750 if( stCommands
[i
].flag
== 'r' && stCommands
[i
].clear
) {
751 cout
<< "Clearing record's dirty flags..." << endl
;
752 desktop
.ClearDirty(id
, stCommands
[i
].index
);
755 if( stCommands
[i
].flag
== 'D' ) {
756 desktop
.DeleteRecord(id
, stCommands
[i
].index
);
763 // Dump contents of selected databases to stdout, or
764 // to file if specified.
765 // This is retrieving data from the Blackberry.
766 if( dbNames
.size() ) {
767 vector
<string
>::iterator b
= dbNames
.begin();
769 desktop
.Open(password
.c_str());
770 for( ; b
!= dbNames
.end(); b
++ ) {
771 auto_ptr
<Parser
> parse
= GetParser(*b
,filename
);
772 unsigned int id
= desktop
.GetDBID(*b
);
773 desktop
.LoadDatabase(id
, *parse
.get());
777 // Save contents of file to specified databases
778 // This is writing data to the Blackberry.
779 if( saveDbNames
.size() ) {
780 vector
<string
>::iterator b
= saveDbNames
.begin();
782 desktop
.Open(password
.c_str());
783 for( ; b
!= saveDbNames
.end(); b
++ ) {
784 auto_ptr
<Builder
> build
=
785 GetBuilder(*b
, filename
);
786 unsigned int id
= desktop
.GetDBID(*b
);
787 desktop
.SaveDatabase(id
, *build
);
792 catch( Usb::Error
&ue
) {
793 std::cerr
<< "Usb::Error caught: " << ue
.what() << endl
;
796 catch( Barry::Error
&se
) {
797 std::cerr
<< "Barry::Error caught: " << se
.what() << endl
;
800 catch( std::exception
&e
) {
801 std::cerr
<< "std::exception caught: " << e
.what() << endl
;