3 /// Barry library tester
7 Copyright (C) 2005-2007, 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-2007, Net Direct Inc. (http://www.netdirect.ca/)\n"
43 << " Using: " << Version
<< "\n"
45 #ifdef __BARRY_BOOST_MODE__
52 << " -c dn Convert address book database to LDIF format, using the\n"
53 << " specified baseDN\n"
54 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
55 << " Defaults to 'cn'\n"
56 << " -d db Load database 'db' FROM device and dump to screen\n"
57 << " Can be used multiple times to fetch more than one DB\n"
58 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
59 << " string separated by a comma, holding the read,write\n"
60 << " endpoint pair. Example: -e 83,5\n"
61 << " Note: Endpoints are specified in hex.\n"
62 << " You should never need to use this option.\n"
63 #ifdef __BARRY_BOOST_MODE__
64 << " -f file Filename to save or load handheld data to/from\n"
67 << " -l List devices\n"
68 << " -L List Contact field names\n"
69 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
70 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
71 << " Unmap: ldif name alone\n"
72 << " -M List current LDIF mapping\n"
73 << " -p pin PIN of device to talk with\n"
74 << " If only one device plugged in, this flag is optional\n"
75 << " -P pass Simplistic method to specify device password\n"
76 << " -s db Save database 'db' TO device from data loaded from -f file\n"
77 << " -S Show list of supported database parsers\n"
78 << " -t Show database database table\n"
79 << " -T db Show record state table for given database\n"
80 << " -v Dump protocol data during operation\n"
81 << " -X Reset device\n"
83 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
85 << " -r # Record index number as seen in the -T state table.\n"
86 << " This overrides the default -d behaviour, and only\n"
87 << " downloads the one specified record, sending to stdout.\n"
88 << " -R # Same as -r, but also clears the record's dirty flags.\n"
89 << " -D # Record index number as seen in the -T state table,\n"
90 << " which indicates the record to delete. Used with the -d\n"
91 << " command to specify the database.\n"
98 Barry::ContactLdif
&ldif
;
100 Contact2Ldif(Barry::ContactLdif
&ldif
) : ldif(ldif
) {}
102 void operator()(const Contact
&rec
)
104 ldif
.DumpLdif(cout
, rec
);
108 template <class Record
>
111 std::vector
<Record
> records
;
112 mutable typename
std::vector
<Record
>::const_iterator rec_it
;
113 std::string filename
;
117 Store(const string
&filename
, bool load
)
118 : rec_it(records
.end()),
123 #ifdef __BARRY_BOOST_MODE__
126 if( load
&& filename
.size() ) {
127 // filename is available, attempt to load
128 cout
<< "Loading: " << filename
<< endl
;
129 ifstream
ifs(filename
.c_str());
130 boost::archive::text_iarchive
ia(ifs
);
132 cout
<< records
.size()
133 << " records loaded from '"
134 << filename
<< "'" << endl
;
135 sort(records
.begin(), records
.end());
136 rec_it
= records
.begin();
139 typename
std::vector
<Record
>::const_iterator beg
= records
.begin(), end
= records
.end();
140 for( ; beg
!= end
; beg
++ ) {
141 cout
<< (*beg
) << endl
;
145 } catch( boost::archive::archive_exception
&ae
) {
146 cerr
<< "Archive exception in ~Store(): "
147 << ae
.what() << endl
;
153 cout
<< "Store counted " << dec
<< count
<< " records." << endl
;
154 #ifdef __BARRY_BOOST_MODE__
157 if( !load
&& filename
.size() ) {
158 // filename is available, attempt to save
159 cout
<< "Saving: " << filename
<< endl
;
160 const std::vector
<Record
> &r
= records
;
161 ofstream
ofs(filename
.c_str());
162 boost::archive::text_oarchive
oa(ofs
);
164 cout
<< dec
<< r
.size() << " records saved to '"
165 << filename
<< "'" << endl
;
168 } catch( boost::archive::archive_exception
&ae
) {
169 cerr
<< "Archive exception in ~Store(): "
170 << ae
.what() << endl
;
176 void operator()(const Record
&rec
)
179 std::cout
<< rec
<< std::endl
;
180 records
.push_back(rec
);
183 // retrieval operator
184 bool operator()(Record
&rec
, unsigned int databaseId
) const
186 if( rec_it
== records
.end() )
194 class DataDumpParser
: public Barry::Parser
199 virtual void SetUniqueId(uint32_t id
)
204 virtual void ParseFields(const Barry::Data
&data
, size_t &offset
)
206 std::cout
<< "Raw record dump for record: "
207 << std::hex
<< m_id
<< std::endl
;
208 std::cout
<< data
<< std::endl
;
212 auto_ptr
<Parser
> GetParser(const string
&name
, const string
&filename
)
214 // check for recognized database names
215 if( name
== "Address Book" ) {
216 return auto_ptr
<Parser
>(
217 new RecordParser
<Contact
, Store
<Contact
> > (
218 new Store
<Contact
>(filename
, false)));
220 else if( name
== "Messages" ) {
221 return auto_ptr
<Parser
>(
222 new RecordParser
<Message
, Store
<Message
> > (
223 new Store
<Message
>(filename
, false)));
225 else if( name
== "Calendar" ) {
226 return auto_ptr
<Parser
>(
227 new RecordParser
<Calendar
, Store
<Calendar
> > (
228 new Store
<Calendar
>(filename
, false)));
230 else if( name
== "Service Book" ) {
231 return auto_ptr
<Parser
>(
232 new RecordParser
<ServiceBook
, Store
<ServiceBook
> > (
233 new Store
<ServiceBook
>(filename
, false)));
236 else if( name
== "Memos" ) {
237 return auto_ptr
<Parser
>(
238 new RecordParser
<Memo
, Store
<Memo
> > (
239 new Store
<Memo
>(filename
, false)));
241 else if( name
== "Tasks" ) {
242 return auto_ptr
<Parser
>(
243 new RecordParser
<Task
, Store
<Task
> > (
244 new Store
<Task
>(filename
, false)));
246 else if( name
== "PIN Messages" ) {
247 return auto_ptr
<Parser
>(
248 new RecordParser
<PINMessage
, Store
<PINMessage
> > (
249 new Store
<PINMessage
>(filename
, false)));
251 else if( name
== "Saved Email Messages" ) {
252 return auto_ptr
<Parser
>(
253 new RecordParser
<SavedMessage
, Store
<SavedMessage
> > (
254 new Store
<SavedMessage
>(filename
, false)));
256 else if( name
== "Folders" ) {
257 return auto_ptr
<Parser
>(
258 new RecordParser
<Folder
, Store
<Folder
> > (
259 new Store
<Folder
>(filename
, false)));
262 // unknown database, use null parser
263 return auto_ptr
<Parser
>( new DataDumpParser
);
267 auto_ptr
<Builder
> GetBuilder(const string
&name
, const string
&filename
)
269 // check for recognized database names
270 if( name
== "Address Book" ) {
271 return auto_ptr
<Builder
>(
272 new RecordBuilder
<Contact
, Store
<Contact
> > (
273 new Store
<Contact
>(filename
, true)));
276 else if( name == "Messages" ) {
277 return auto_ptr<Parser>(
278 new RecordParser<Message, Store<Message> > (
279 new Store<Message>(filename, true)));
281 else if( name == "Calendar" ) {
282 return auto_ptr<Parser>(
283 new RecordParser<Calendar, Store<Calendar> > (
284 new Store<Calendar>(filename, true)));
286 else if( name == "Service Book" ) {
287 return auto_ptr<Parser>(
288 new RecordParser<ServiceBook, Store<ServiceBook> > (
289 new Store<ServiceBook>(filename, true)));
291 else if( name == "Memos" ) {
292 return auto_ptr<Parser>(
293 new RecordParser<Memo, Store<Memo> > (
294 new Store<Memo>(filename, true)));
296 else if( name == "Tasks" ) {
297 return auto_ptr<Parser>(
298 new RecordParser<Task, Store<Task> > (
299 new Store<Task>(filename, true)));
303 throw std::runtime_error("No Builder available for database");
309 cout
<< "Supported Database parsers:\n"
317 << " Saved Email Messages\n"
320 << "Supported Database builders:\n"
325 struct StateTableCommand
331 StateTableCommand(char f
, bool c
, unsigned int i
)
332 : flag(f
), clear(c
), index(i
) {}
335 bool SplitMap(const string
&map
, string
&ldif
, string
&read
, string
&write
)
337 string::size_type a
= map
.find(',');
338 if( a
== string::npos
)
341 string::size_type b
= map
.find(',', a
+1);
342 if( b
== string::npos
)
345 ldif
.assign(map
, 0, a
);
346 read
.assign(map
, a
+ 1, b
- a
- 1);
347 write
.assign(map
, b
+ 1, map
.size() - b
- 1);
349 return ldif
.size() && read
.size() && write
.size();
352 void DoMapping(ContactLdif
&ldif
, const vector
<string
> &mapCommands
)
354 for( vector
<string
>::const_iterator i
= mapCommands
.begin();
355 i
!= mapCommands
.end();
358 // single names mean unmapping
359 if( i
->find(',') == string::npos
) {
361 cerr
<< "Unmapping: " << *i
<< endl
;
365 cerr
<< "Mapping: " << *i
<< endl
;
367 // map... extract ldif/read/write names
368 string ldifname
, read
, write
;
369 if( SplitMap(*i
, ldifname
, read
, write
) ) {
370 if( !ldif
.Map(ldifname
, read
, write
) ) {
371 cerr
<< "Read/Write name unknown: " << *i
<< endl
;
375 cerr
<< "Invalid map format: " << *i
<< endl
;
381 bool ParseEpOverride(const char *arg
, Usb::EndpointPair
*epp
)
385 istringstream
iss(arg
);
386 iss
>> hex
>> read
>> comma
>> write
;
394 int main(int argc
, char *argv
[])
396 cout
.sync_with_stdio(true); // leave this on, since libusb uses
397 // stdio for debug messages
402 bool list_only
= false,
404 ldif_contacts
= false,
406 reset_device
= false,
407 list_contact_fields
= false,
408 list_ldif_map
= false,
409 epp_override
= false,
410 record_state
= false;
411 string ldifBaseDN
, ldifDnAttr
;
414 vector
<string
> dbNames
, saveDbNames
, mapCommands
;
415 vector
<StateTableCommand
> stCommands
;
416 Usb::EndpointPair epOverride
;
418 // process command line options
420 int cmd
= getopt(argc
, argv
, "c:C:d:D:e:f:hlLm:Mp:P:r:R:Ss:tT:vX");
426 case 'c': // contacts to ldap ldif
427 ldif_contacts
= true;
431 case 'C': // DN Attribute for FQDN
435 case 'd': // show dbname
436 dbNames
.push_back(string(optarg
));
439 case 'D': // delete record
440 stCommands
.push_back(
441 StateTableCommand('D', false, atoi(optarg
)));
444 case 'e': // endpoint override
445 if( !ParseEpOverride(optarg
, &epOverride
) ) {
452 case 'f': // filename
453 #ifdef __BARRY_BOOST_MODE__
456 cerr
<< "-f option not supported - no Boost "
457 "serialization support available\n";
461 case 'l': // list only
465 case 'L': // List Contact field names
466 list_contact_fields
= true;
469 case 'm': // Map / Unmap
470 mapCommands
.push_back(string(optarg
));
473 case 'M': // List LDIF map
474 list_ldif_map
= true;
477 case 'p': // Blackberry PIN
478 pin
= strtoul(optarg
, NULL
, 16);
481 case 'P': // Device password
485 case 'r': // get specific record index
486 stCommands
.push_back(
487 StateTableCommand('r', false, atoi(optarg
)));
490 case 'R': // same as 'r', and clears dirty
491 stCommands
.push_back(
492 StateTableCommand('r', true, atoi(optarg
)));
495 case 's': // save dbname
496 saveDbNames
.push_back(string(optarg
));
499 case 'S': // show supported databases
503 case 't': // display database database
507 case 'T': // show RecordStateTable
509 dbNames
.push_back(string(optarg
));
512 case 'v': // data dump on
516 case 'X': // reset device
527 // Initialize the barry library. Must be called before
529 Barry::Init(data_dump
);
531 // LDIF class... only needed if ldif output turned on
532 ContactLdif
ldif(ldifBaseDN
);
533 DoMapping(ldif
, mapCommands
);
534 if( ldifDnAttr
.size() ) {
535 if( !ldif
.SetDNAttr(ldifDnAttr
) ) {
536 cerr
<< "Unable to set DN Attr: " << ldifDnAttr
<< endl
;
540 // Probe the USB bus for Blackberry devices and display.
541 // If user has specified a PIN, search for it in the
542 // available device list here as well
544 int activeDevice
= -1;
547 cout
<< "Blackberry devices found:" << endl
;
548 for( int i
= 0; i
< probe
.GetCount(); i
++ ) {
551 cout
<< probe
.Get(i
) << endl
;
552 if( probe
.Get(i
).m_pin
== pin
)
559 if( activeDevice
== -1 ) {
561 // can we default to single device?
562 if( probe
.GetCount() == 1 )
565 cerr
<< "No device selected" << endl
;
570 cerr
<< "PIN " << setbase(16) << pin
571 << " not found" << endl
;
577 Usb::Device
dev(probe
.Get(activeDevice
).m_dev
);
582 // Create our controller object
583 Barry::ProbeResult device
= probe
.Get(activeDevice
);
585 device
.m_ep
.read
= epOverride
.read
;
586 device
.m_ep
.write
= epOverride
.write
;
587 device
.m_ep
.type
= 2; // FIXME - override this too?
588 cout
<< "Endpoint pair (read,write) overridden with: "
590 << (unsigned int) device
.m_ep
.read
<< ","
591 << (unsigned int) device
.m_ep
.write
<< endl
;
593 Barry::Controller
con(device
);
596 // execute each mode that was turned on
600 // Dump list of all databases to stdout
602 // open desktop mode socket
603 con
.OpenMode(Controller::Desktop
, password
.c_str());
604 cout
<< con
.GetDBDB() << endl
;
607 // Dump list of Contact field names
608 if( list_contact_fields
) {
609 for( const ContactLdif::NameToFunc
*n
= ldif
.GetFieldNames(); n
->name
; n
++ ) {
611 cout
<< " " << left
<< setw(20) << n
->name
<< ": "
612 << n
->description
<< endl
;
616 // Dump current LDIF mapping
617 if( list_ldif_map
) {
618 cout
<< ldif
<< endl
;
621 // Dump list of contacts to an LDAP LDIF file
622 // This uses the Controller convenience templates
623 if( ldif_contacts
) {
624 // make sure we're in desktop mode
625 con
.OpenMode(Controller::Desktop
, password
.c_str());
627 // create a storage functor object that accepts
628 // Barry::Contact objects as input
629 Contact2Ldif
storage(ldif
);
631 // load all the Contact records into storage
632 con
.LoadDatabaseByType
<Barry::Contact
>(storage
);
635 // Dump record state table to stdout
637 if( dbNames
.size() == 0 ) {
638 cout
<< "No db names to process" << endl
;
642 vector
<string
>::iterator b
= dbNames
.begin();
643 for( ; b
!= dbNames
.end(); b
++ ) {
644 con
.OpenMode(Controller::Desktop
, password
.c_str());
645 unsigned int id
= con
.GetDBID(*b
);
646 RecordStateTable state
;
647 con
.GetRecordStateTable(id
, state
);
648 cout
<< "Record state table for: " << *b
<< endl
;
654 // Get Record mode overrides the default name mode
655 if( stCommands
.size() ) {
656 if( dbNames
.size() != 1 ) {
657 cout
<< "Must have 1 db name to process" << endl
;
661 con
.OpenMode(Controller::Desktop
, password
.c_str());
662 unsigned int id
= con
.GetDBID(dbNames
[0]);
663 auto_ptr
<Parser
> parse
= GetParser(dbNames
[0],filename
);
665 for( unsigned int i
= 0; i
< stCommands
.size(); i
++ ) {
666 con
.GetRecord(id
, stCommands
[i
].index
, *parse
.get());
668 if( stCommands
[i
].flag
== 'r' && stCommands
[i
].clear
) {
669 cout
<< "Clearing record's dirty flags..." << endl
;
670 con
.ClearDirty(id
, stCommands
[i
].index
);
673 if( stCommands
[i
].flag
== 'D' ) {
674 con
.DeleteRecord(id
, stCommands
[i
].index
);
681 // Dump contents of selected databases to stdout, or
682 // to file if specified.
683 // This is retrieving data from the Blackberry.
684 if( dbNames
.size() ) {
685 vector
<string
>::iterator b
= dbNames
.begin();
687 for( ; b
!= dbNames
.end(); b
++ ) {
688 con
.OpenMode(Controller::Desktop
, password
.c_str());
689 auto_ptr
<Parser
> parse
= GetParser(*b
,filename
);
690 unsigned int id
= con
.GetDBID(*b
);
691 con
.LoadDatabase(id
, *parse
.get());
695 // Save contents of file to specified databases
696 // This is writing data to the Blackberry.
697 if( saveDbNames
.size() ) {
698 vector
<string
>::iterator b
= saveDbNames
.begin();
700 for( ; b
!= saveDbNames
.end(); b
++ ) {
701 con
.OpenMode(Controller::Desktop
, password
.c_str());
702 auto_ptr
<Builder
> build
=
703 GetBuilder(*b
, filename
);
704 unsigned int id
= con
.GetDBID(*b
);
705 con
.SaveDatabase(id
, *build
);
710 catch( Usb::Error
&ue
) {
711 std::cerr
<< "Usb::Error caught: " << ue
.what() << endl
;
714 catch( Barry::Error
&se
) {
715 std::cerr
<< "Barry::Error caught: " << se
.what() << endl
;
718 catch( std::exception
&e
) {
719 std::cerr
<< "std::exception caught: " << e
.what() << endl
;