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>
32 using namespace Barry
;
37 const char *Version
= Barry::Version(major
, minor
);
40 << "btool - Command line USB Blackberry Test Tool\n"
41 << " Copyright 2005-2007, Net Direct Inc. (http://www.netdirect.ca/)\n"
42 << " Using: " << Version
<< "\n"
44 #ifdef __BARRY_BOOST_MODE__
51 << " -c dn Convert address book database to LDIF format, using the\n"
52 << " specified baseDN\n"
53 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
54 << " Defaults to 'cn'\n"
55 << " -d db Load database 'db' FROM device and dump to screen\n"
56 << " Can be used multiple times to fetch more than one DB\n"
57 #ifdef __BARRY_BOOST_MODE__
58 << " -f file Filename to save or load handheld data to/from\n"
61 << " -l List devices\n"
62 << " -L List Contact field names\n"
63 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
64 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
65 << " Unmap: ldif name alone\n"
66 << " -M List current LDIF mapping\n"
67 << " -p pin PIN of device to talk with\n"
68 << " If only one device plugged in, this flag is optional\n"
69 << " -P pass Simplistic method to specify device password\n"
70 << " -s db Save database 'db' TO device from data loaded from -f file\n"
71 << " -t Show database database table\n"
72 << " -T db Show record state table for given database\n"
73 << " -v Dump protocol data during operation\n"
74 << " -X Reset device\n"
76 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
78 << " -r # Record index number as seen in the -T state table.\n"
79 << " This overrides the default -d behaviour, and only\n"
80 << " downloads the one specified record, sending to stdout.\n"
81 << " -R # Same as -r, but also clears the record's dirty flags.\n"
82 << " -D # Record index number as seen in the -T state table,\n"
83 << " which indicates the record to delete. Used with the -d\n"
84 << " command to specify the database.\n"
91 Barry::ContactLdif
&ldif
;
93 Contact2Ldif(Barry::ContactLdif
&ldif
) : ldif(ldif
) {}
95 void operator()(const Contact
&rec
)
97 ldif
.DumpLdif(cout
, rec
);
101 template <class Record
>
104 std::vector
<Record
> records
;
105 mutable typename
std::vector
<Record
>::const_iterator rec_it
;
106 std::string filename
;
110 Store(const string
&filename
, bool load
)
111 : rec_it(records
.end()),
116 #ifdef __BARRY_BOOST_MODE__
119 if( load
&& filename
.size() ) {
120 // filename is available, attempt to load
121 cout
<< "Loading: " << filename
<< endl
;
122 ifstream
ifs(filename
.c_str());
123 boost::archive::text_iarchive
ia(ifs
);
125 cout
<< records
.size()
126 << " records loaded from '"
127 << filename
<< "'" << endl
;
128 sort(records
.begin(), records
.end());
129 rec_it
= records
.begin();
132 typename
std::vector
<Record
>::const_iterator beg
= records
.begin(), end
= records
.end();
133 for( ; beg
!= end
; beg
++ ) {
134 cout
<< (*beg
) << endl
;
138 } catch( boost::archive::archive_exception
&ae
) {
139 cerr
<< "Archive exception in ~Store(): "
140 << ae
.what() << endl
;
146 cout
<< "Store counted " << dec
<< count
<< " records." << endl
;
147 #ifdef __BARRY_BOOST_MODE__
150 if( !load
&& filename
.size() ) {
151 // filename is available, attempt to save
152 cout
<< "Saving: " << filename
<< endl
;
153 const std::vector
<Record
> &r
= records
;
154 ofstream
ofs(filename
.c_str());
155 boost::archive::text_oarchive
oa(ofs
);
157 cout
<< dec
<< r
.size() << " records saved to '"
158 << filename
<< "'" << endl
;
161 } catch( boost::archive::archive_exception
&ae
) {
162 cerr
<< "Archive exception in ~Store(): "
163 << ae
.what() << endl
;
169 void operator()(const Record
&rec
)
172 std::cout
<< rec
<< std::endl
;
173 records
.push_back(rec
);
176 // retrieval operator
177 bool operator()(Record
&rec
, unsigned int databaseId
) const
179 if( rec_it
== records
.end() )
187 class DataDumpParser
: public Barry::Parser
192 virtual void SetUniqueId(uint32_t id
)
197 virtual void ParseFields(const Barry::Data
&data
, size_t &offset
)
199 std::cout
<< "Raw record dump for record: "
200 << std::hex
<< m_id
<< std::endl
;
201 std::cout
<< data
<< std::endl
;
205 auto_ptr
<Parser
> GetParser(const string
&name
, const string
&filename
)
207 // check for recognized database names
208 if( name
== "Address Book" ) {
209 return auto_ptr
<Parser
>(
210 new RecordParser
<Contact
, Store
<Contact
> > (
211 new Store
<Contact
>(filename
, false)));
213 else if( name
== "Messages" ) {
214 return auto_ptr
<Parser
>(
215 new RecordParser
<Message
, Store
<Message
> > (
216 new Store
<Message
>(filename
, false)));
218 else if( name
== "Calendar" ) {
219 return auto_ptr
<Parser
>(
220 new RecordParser
<Calendar
, Store
<Calendar
> > (
221 new Store
<Calendar
>(filename
, false)));
223 else if( name
== "Service Book" ) {
224 return auto_ptr
<Parser
>(
225 new RecordParser
<ServiceBook
, Store
<ServiceBook
> > (
226 new Store
<ServiceBook
>(filename
, false)));
229 else if( name
== "Memos" ) {
230 return auto_ptr
<Parser
>(
231 new RecordParser
<Memo
, Store
<Memo
> > (
232 new Store
<Memo
>(filename
, false)));
234 else if( name
== "Tasks" ) {
235 return auto_ptr
<Parser
>(
236 new RecordParser
<Task
, Store
<Task
> > (
237 new Store
<Task
>(filename
, false)));
239 else if( name
== "PIN Messages" ) {
240 return auto_ptr
<Parser
>(
241 new RecordParser
<PINMessage
, Store
<PINMessage
> > (
242 new Store
<PINMessage
>(filename
, false)));
244 else if( name
== "Saved Email Messages" ) {
245 return auto_ptr
<Parser
>(
246 new RecordParser
<SavedMessage
, Store
<SavedMessage
> > (
247 new Store
<SavedMessage
>(filename
, false)));
249 else if( name
== "Folders" ) {
250 return auto_ptr
<Parser
>(
251 new RecordParser
<Folder
, Store
<Folder
> > (
252 new Store
<Folder
>(filename
, false)));
255 // unknown database, use null parser
256 return auto_ptr
<Parser
>( new DataDumpParser
);
260 auto_ptr
<Builder
> GetBuilder(const string
&name
, const string
&filename
)
262 // check for recognized database names
263 if( name
== "Address Book" ) {
264 return auto_ptr
<Builder
>(
265 new RecordBuilder
<Contact
, Store
<Contact
> > (
266 new Store
<Contact
>(filename
, true)));
269 else if( name == "Messages" ) {
270 return auto_ptr<Parser>(
271 new RecordParser<Message, Store<Message> > (
272 new Store<Message>(filename, true)));
274 else if( name == "Calendar" ) {
275 return auto_ptr<Parser>(
276 new RecordParser<Calendar, Store<Calendar> > (
277 new Store<Calendar>(filename, true)));
279 else if( name == "Service Book" ) {
280 return auto_ptr<Parser>(
281 new RecordParser<ServiceBook, Store<ServiceBook> > (
282 new Store<ServiceBook>(filename, true)));
284 else if( name == "Memos" ) {
285 return auto_ptr<Parser>(
286 new RecordParser<Memo, Store<Memo> > (
287 new Store<Memo>(filename, true)));
289 else if( name == "Tasks" ) {
290 return auto_ptr<Parser>(
291 new RecordParser<Task, Store<Task> > (
292 new Store<Task>(filename, true)));
296 throw std::runtime_error("No Builder available for database");
300 struct StateTableCommand
306 StateTableCommand(char f
, bool c
, unsigned int i
)
307 : flag(f
), clear(c
), index(i
) {}
310 bool SplitMap(const string
&map
, string
&ldif
, string
&read
, string
&write
)
312 string::size_type a
= map
.find(',');
313 if( a
== string::npos
)
316 string::size_type b
= map
.find(',', a
+1);
317 if( b
== string::npos
)
320 ldif
.assign(map
, 0, a
);
321 read
.assign(map
, a
+ 1, b
- a
- 1);
322 write
.assign(map
, b
+ 1, map
.size() - b
- 1);
324 return ldif
.size() && read
.size() && write
.size();
327 void DoMapping(ContactLdif
&ldif
, const vector
<string
> &mapCommands
)
329 for( vector
<string
>::const_iterator i
= mapCommands
.begin();
330 i
!= mapCommands
.end();
333 // single names mean unmapping
334 if( i
->find(',') == string::npos
) {
336 cerr
<< "Unmapping: " << *i
<< endl
;
340 cerr
<< "Mapping: " << *i
<< endl
;
342 // map... extract ldif/read/write names
343 string ldifname
, read
, write
;
344 if( SplitMap(*i
, ldifname
, read
, write
) ) {
345 if( !ldif
.Map(ldifname
, read
, write
) ) {
346 cerr
<< "Read/Write name unknown: " << *i
<< endl
;
350 cerr
<< "Invalid map format: " << *i
<< endl
;
356 int main(int argc
, char *argv
[])
358 cout
.sync_with_stdio(true); // leave this on, since libusb uses
359 // stdio for debug messages
364 bool list_only
= false,
366 ldif_contacts
= false,
368 reset_device
= false,
369 list_contact_fields
= false,
370 list_ldif_map
= false,
371 record_state
= false;
372 string ldifBaseDN
, ldifDnAttr
;
375 vector
<string
> dbNames
, saveDbNames
, mapCommands
;
376 vector
<StateTableCommand
> stCommands
;
378 // process command line options
380 int cmd
= getopt(argc
, argv
, "c:C:d:D:f:hlLm:Mp:P:r:R:s:tT:vX");
386 case 'c': // contacts to ldap ldif
387 ldif_contacts
= true;
391 case 'C': // DN Attribute for FQDN
395 case 'd': // show dbname
396 dbNames
.push_back(string(optarg
));
399 case 'D': // delete record
400 stCommands
.push_back(
401 StateTableCommand('D', false, atoi(optarg
)));
404 case 'f': // filename
405 #ifdef __BARRY_BOOST_MODE__
408 cerr
<< "-f option not supported - no Boost "
409 "serialization support available\n";
413 case 'l': // list only
417 case 'L': // List Contact field names
418 list_contact_fields
= true;
421 case 'm': // Map / Unmap
422 mapCommands
.push_back(string(optarg
));
425 case 'M': // List LDIF map
426 list_ldif_map
= true;
429 case 'p': // Blackberry PIN
430 pin
= strtoul(optarg
, NULL
, 16);
433 case 'P': // Device password
437 case 'r': // get specific record index
438 stCommands
.push_back(
439 StateTableCommand('r', false, atoi(optarg
)));
442 case 'R': // same as 'r', and clears dirty
443 stCommands
.push_back(
444 StateTableCommand('r', true, atoi(optarg
)));
447 case 's': // save dbname
448 saveDbNames
.push_back(string(optarg
));
451 case 't': // display database database
455 case 'T': // show RecordStateTable
457 dbNames
.push_back(string(optarg
));
460 case 'v': // data dump on
464 case 'X': // reset device
475 // Initialize the barry library. Must be called before
477 Barry::Init(data_dump
);
479 // LDIF class... only needed if ldif output turned on
480 ContactLdif
ldif(ldifBaseDN
);
481 DoMapping(ldif
, mapCommands
);
482 if( ldifDnAttr
.size() ) {
483 if( !ldif
.SetDNAttr(ldifDnAttr
) ) {
484 cerr
<< "Unable to set DN Attr: " << ldifDnAttr
<< endl
;
488 // Probe the USB bus for Blackberry devices and display.
489 // If user has specified a PIN, search for it in the
490 // available device list here as well
492 int activeDevice
= -1;
495 cout
<< "Blackberry devices found:" << endl
;
496 for( int i
= 0; i
< probe
.GetCount(); i
++ ) {
499 cout
<< probe
.Get(i
) << endl
;
500 if( probe
.Get(i
).m_pin
== pin
)
507 if( activeDevice
== -1 ) {
509 // can we default to single device?
510 if( probe
.GetCount() == 1 )
513 cerr
<< "No device selected" << endl
;
518 cerr
<< "PIN " << setbase(16) << pin
519 << " not found" << endl
;
525 Usb::Device
dev(probe
.Get(activeDevice
).m_dev
);
530 // Create our controller object
531 Barry::Controller
con(probe
.Get(activeDevice
));
534 // execute each mode that was turned on
538 // Dump list of all databases to stdout
540 // open desktop mode socket
541 con
.OpenMode(Controller::Desktop
, password
.c_str());
542 cout
<< con
.GetDBDB() << endl
;
545 // Dump list of Contact field names
546 if( list_contact_fields
) {
547 for( const ContactLdif::NameToFunc
*n
= ldif
.GetFieldNames(); n
->name
; n
++ ) {
549 cout
<< " " << left
<< setw(20) << n
->name
<< ": "
550 << n
->description
<< endl
;
554 // Dump current LDIF mapping
555 if( list_ldif_map
) {
556 cout
<< ldif
<< endl
;
559 // Dump list of contacts to an LDAP LDIF file
560 // This uses the Controller convenience templates
561 if( ldif_contacts
) {
562 // make sure we're in desktop mode
563 con
.OpenMode(Controller::Desktop
, password
.c_str());
565 // create a storage functor object that accepts
566 // Barry::Contact objects as input
567 Contact2Ldif
storage(ldif
);
569 // load all the Contact records into storage
570 con
.LoadDatabaseByType
<Barry::Contact
>(storage
);
573 // Dump record state table to stdout
575 if( dbNames
.size() == 0 ) {
576 cout
<< "No db names to process" << endl
;
580 vector
<string
>::iterator b
= dbNames
.begin();
581 for( ; b
!= dbNames
.end(); b
++ ) {
582 con
.OpenMode(Controller::Desktop
, password
.c_str());
583 unsigned int id
= con
.GetDBID(*b
);
584 RecordStateTable state
;
585 con
.GetRecordStateTable(id
, state
);
586 cout
<< "Record state table for: " << *b
<< endl
;
592 // Get Record mode overrides the default name mode
593 if( stCommands
.size() ) {
594 if( dbNames
.size() != 1 ) {
595 cout
<< "Must have 1 db name to process" << endl
;
599 con
.OpenMode(Controller::Desktop
, password
.c_str());
600 unsigned int id
= con
.GetDBID(dbNames
[0]);
601 auto_ptr
<Parser
> parse
= GetParser(dbNames
[0],filename
);
603 for( unsigned int i
= 0; i
< stCommands
.size(); i
++ ) {
604 con
.GetRecord(id
, stCommands
[i
].index
, *parse
.get());
606 if( stCommands
[i
].flag
== 'r' && stCommands
[i
].clear
) {
607 cout
<< "Clearing record's dirty flags..." << endl
;
608 con
.ClearDirty(id
, stCommands
[i
].index
);
611 if( stCommands
[i
].flag
== 'D' ) {
612 con
.DeleteRecord(id
, stCommands
[i
].index
);
619 // Dump contents of selected databases to stdout, or
620 // to file if specified.
621 // This is retrieving data from the Blackberry.
622 if( dbNames
.size() ) {
623 vector
<string
>::iterator b
= dbNames
.begin();
625 for( ; b
!= dbNames
.end(); b
++ ) {
626 con
.OpenMode(Controller::Desktop
, password
.c_str());
627 auto_ptr
<Parser
> parse
= GetParser(*b
,filename
);
628 unsigned int id
= con
.GetDBID(*b
);
629 con
.LoadDatabase(id
, *parse
.get());
633 // Save contents of file to specified databases
634 // This is writing data to the Blackberry.
635 if( saveDbNames
.size() ) {
636 vector
<string
>::iterator b
= saveDbNames
.begin();
638 for( ; b
!= saveDbNames
.end(); b
++ ) {
639 con
.OpenMode(Controller::Desktop
, password
.c_str());
640 auto_ptr
<Builder
> build
=
641 GetBuilder(*b
, filename
);
642 unsigned int id
= con
.GetDBID(*b
);
643 con
.SaveDatabase(id
, *build
);
648 catch( Usb::Error
&ue
) {
649 std::cerr
<< "Usb::Error caught: " << ue
.what() << endl
;
652 catch( Barry::Error
&se
) {
653 std::cerr
<< "Barry::Error caught: " << se
.what() << endl
;
656 catch( std::exception
&e
) {
657 std::cerr
<< "std::exception caught: " << e
.what() << endl
;