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 << " -c dn Convert address book database to LDIF format, using the\n"
45 << " specified baseDN\n"
46 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
47 << " Defaults to 'cn'\n"
48 << " -d db Load database 'db' FROM device and dump to screen\n"
49 << " Can be used multiple times to fetch more than one DB\n"
50 #ifdef __BARRY_BOOST_MODE__
51 << " -f file Filename to save or load handheld data to/from\n"
54 << " -l List devices\n"
55 << " -L List Contact field names\n"
56 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
57 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
58 << " Unmap: ldif name alone\n"
59 << " -M List current LDIF mapping\n"
60 << " -p pin PIN of device to talk with\n"
61 << " If only one device plugged in, this flag is optional\n"
62 << " -P pass Simplistic method to specify device password\n"
63 << " -s db Save database 'db' TO device from data loaded from -f file\n"
64 << " -t Show database database table\n"
65 << " -T db Show record state table for given database\n"
66 << " -v Dump protocol data during operation\n"
67 << " -X Reset device\n"
69 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
71 << " -r # Record index number as seen in the -T state table.\n"
72 << " This overrides the default -d behaviour, and only\n"
73 << " downloads the one specified record, sending to stdout.\n"
74 << " -R # Same as -r, but also clears the record's dirty flags.\n"
75 << " -D # Record index number as seen in the -T state table,\n"
76 << " which indicates the record to delete. Used with the -d\n"
77 << " command to specify the database.\n"
84 Barry::ContactLdif
&ldif
;
86 Contact2Ldif(Barry::ContactLdif
&ldif
) : ldif(ldif
) {}
88 void operator()(const Contact
&rec
)
90 ldif
.DumpLdif(cout
, rec
);
94 template <class Record
>
97 std::vector
<Record
> records
;
98 mutable typename
std::vector
<Record
>::const_iterator rec_it
;
103 Store(const string
&filename
, bool load
)
104 : rec_it(records
.end()),
109 #ifdef __BARRY_BOOST_MODE__
112 if( load
&& filename
.size() ) {
113 // filename is available, attempt to load
114 cout
<< "Loading: " << filename
<< endl
;
115 ifstream
ifs(filename
.c_str());
116 boost::archive::text_iarchive
ia(ifs
);
118 cout
<< records
.size()
119 << " records loaded from '"
120 << filename
<< "'" << endl
;
121 sort(records
.begin(), records
.end());
122 rec_it
= records
.begin();
125 typename
std::vector
<Record
>::const_iterator beg
= records
.begin(), end
= records
.end();
126 for( ; beg
!= end
; beg
++ ) {
127 cout
<< (*beg
) << endl
;
131 } catch( boost::archive::archive_exception
&ae
) {
132 cerr
<< "Archive exception in ~Store(): "
133 << ae
.what() << endl
;
139 cout
<< "Store counted " << dec
<< count
<< " records." << endl
;
140 #ifdef __BARRY_BOOST_MODE__
143 if( !load
&& filename
.size() ) {
144 // filename is available, attempt to save
145 cout
<< "Saving: " << filename
<< endl
;
146 const std::vector
<Record
> &r
= records
;
147 ofstream
ofs(filename
.c_str());
148 boost::archive::text_oarchive
oa(ofs
);
150 cout
<< dec
<< r
.size() << " records saved to '"
151 << filename
<< "'" << endl
;
154 } catch( boost::archive::archive_exception
&ae
) {
155 cerr
<< "Archive exception in ~Store(): "
156 << ae
.what() << endl
;
162 void operator()(const Record
&rec
)
165 std::cout
<< rec
<< std::endl
;
166 records
.push_back(rec
);
169 // retrieval operator
170 bool operator()(Record
&rec
, unsigned int databaseId
) const
172 if( rec_it
== records
.end() )
180 class DataDumpParser
: public Barry::Parser
185 virtual void SetUniqueId(uint32_t id
)
190 virtual void ParseFields(const Barry::Data
&data
, size_t &offset
)
192 std::cout
<< "Raw record dump for record: "
193 << std::hex
<< m_id
<< std::endl
;
194 std::cout
<< data
<< std::endl
;
198 auto_ptr
<Parser
> GetParser(const string
&name
, const string
&filename
)
200 // check for recognized database names
201 if( name
== "Address Book" ) {
202 return auto_ptr
<Parser
>(
203 new RecordParser
<Contact
, Store
<Contact
> > (
204 new Store
<Contact
>(filename
, false)));
206 else if( name
== "Messages" ) {
207 return auto_ptr
<Parser
>(
208 new RecordParser
<Message
, Store
<Message
> > (
209 new Store
<Message
>(filename
, false)));
211 else if( name
== "Calendar" ) {
212 return auto_ptr
<Parser
>(
213 new RecordParser
<Calendar
, Store
<Calendar
> > (
214 new Store
<Calendar
>(filename
, false)));
216 else if( name
== "Service Book" ) {
217 return auto_ptr
<Parser
>(
218 new RecordParser
<ServiceBook
, Store
<ServiceBook
> > (
219 new Store
<ServiceBook
>(filename
, false)));
222 // unknown database, use null parser
223 return auto_ptr
<Parser
>( new DataDumpParser
);
227 auto_ptr
<Builder
> GetBuilder(const string
&name
, const string
&filename
)
229 // check for recognized database names
230 if( name
== "Address Book" ) {
231 return auto_ptr
<Builder
>(
232 new RecordBuilder
<Contact
, Store
<Contact
> > (
233 new Store
<Contact
>(filename
, true)));
236 else if( name == "Messages" ) {
237 return auto_ptr<Parser>(
238 new RecordParser<Message, Store<Message> > (
239 new Store<Message>(filename, true)));
241 else if( name == "Calendar" ) {
242 return auto_ptr<Parser>(
243 new RecordParser<Calendar, Store<Calendar> > (
244 new Store<Calendar>(filename, true)));
246 else if( name == "Service Book" ) {
247 return auto_ptr<Parser>(
248 new RecordParser<ServiceBook, Store<ServiceBook> > (
249 new Store<ServiceBook>(filename, true)));
253 throw std::runtime_error("No Builder available for database");
257 struct StateTableCommand
263 StateTableCommand(char f
, bool c
, unsigned int i
)
264 : flag(f
), clear(c
), index(i
) {}
267 bool SplitMap(const string
&map
, string
&ldif
, string
&read
, string
&write
)
269 string::size_type a
= map
.find(',');
270 if( a
== string::npos
)
273 string::size_type b
= map
.find(',', a
+1);
274 if( b
== string::npos
)
277 ldif
.assign(map
, 0, a
);
278 read
.assign(map
, a
+ 1, b
- a
- 1);
279 write
.assign(map
, b
+ 1, map
.size() - b
- 1);
281 return ldif
.size() && read
.size() && write
.size();
284 void DoMapping(ContactLdif
&ldif
, const vector
<string
> &mapCommands
)
286 for( vector
<string
>::const_iterator i
= mapCommands
.begin();
287 i
!= mapCommands
.end();
290 // single names mean unmapping
291 if( i
->find(',') == string::npos
) {
293 cerr
<< "Unmapping: " << *i
<< endl
;
297 cerr
<< "Mapping: " << *i
<< endl
;
299 // map... extract ldif/read/write names
300 string ldifname
, read
, write
;
301 if( SplitMap(*i
, ldifname
, read
, write
) ) {
302 if( !ldif
.Map(ldifname
, read
, write
) ) {
303 cerr
<< "Read/Write name unknown: " << *i
<< endl
;
307 cerr
<< "Invalid map format: " << *i
<< endl
;
313 int main(int argc
, char *argv
[])
315 cout
.sync_with_stdio(true); // leave this on, since libusb uses
316 // stdio for debug messages
321 bool list_only
= false,
323 ldif_contacts
= false,
325 reset_device
= false,
326 list_contact_fields
= false,
327 list_ldif_map
= false,
328 record_state
= false;
329 string ldifBaseDN
, ldifDnAttr
;
332 vector
<string
> dbNames
, saveDbNames
, mapCommands
;
333 vector
<StateTableCommand
> stCommands
;
335 // process command line options
337 int cmd
= getopt(argc
, argv
, "c:C:d:D:f:hlLm:Mp:P:r:R:s:tT:vX");
343 case 'c': // contacts to ldap ldif
344 ldif_contacts
= true;
348 case 'C': // DN Attribute for FQDN
352 case 'd': // show dbname
353 dbNames
.push_back(string(optarg
));
356 case 'D': // delete record
357 stCommands
.push_back(
358 StateTableCommand('D', false, atoi(optarg
)));
361 case 'f': // filename
362 #ifdef __BARRY_BOOST_MODE__
365 cerr
<< "-f option not supported - no Boost "
366 "serialization support available\n";
370 case 'l': // list only
374 case 'L': // List Contact field names
375 list_contact_fields
= true;
378 case 'm': // Map / Unmap
379 mapCommands
.push_back(string(optarg
));
382 case 'M': // List LDIF map
383 list_ldif_map
= true;
386 case 'p': // Blackberry PIN
387 pin
= strtoul(optarg
, NULL
, 16);
390 case 'P': // Device password
394 case 'r': // get specific record index
395 stCommands
.push_back(
396 StateTableCommand('r', false, atoi(optarg
)));
399 case 'R': // same as 'r', and clears dirty
400 stCommands
.push_back(
401 StateTableCommand('r', true, atoi(optarg
)));
404 case 's': // save dbname
405 saveDbNames
.push_back(string(optarg
));
408 case 't': // display database database
412 case 'T': // show RecordStateTable
414 dbNames
.push_back(string(optarg
));
417 case 'v': // data dump on
421 case 'X': // reset device
432 // Initialize the barry library. Must be called before
434 Barry::Init(data_dump
);
436 // LDIF class... only needed if ldif output turned on
437 ContactLdif
ldif(ldifBaseDN
);
438 DoMapping(ldif
, mapCommands
);
439 if( ldifDnAttr
.size() ) {
440 if( !ldif
.SetDNAttr(ldifDnAttr
) ) {
441 cerr
<< "Unable to set DN Attr: " << ldifDnAttr
<< endl
;
445 // Probe the USB bus for Blackberry devices and display.
446 // If user has specified a PIN, search for it in the
447 // available device list here as well
449 int activeDevice
= -1;
452 cout
<< "Blackberry devices found:" << endl
;
453 for( int i
= 0; i
< probe
.GetCount(); i
++ ) {
456 cout
<< probe
.Get(i
) << endl
;
457 if( probe
.Get(i
).m_pin
== pin
)
464 if( activeDevice
== -1 ) {
466 // can we default to single device?
467 if( probe
.GetCount() == 1 )
470 cerr
<< "No device selected" << endl
;
475 cerr
<< "PIN " << setbase(16) << pin
476 << " not found" << endl
;
482 Usb::Device
dev(probe
.Get(activeDevice
).m_dev
);
487 // Create our controller object
488 Barry::Controller
con(probe
.Get(activeDevice
));
491 // execute each mode that was turned on
495 // Dump list of all databases to stdout
497 // open desktop mode socket
498 con
.OpenMode(Controller::Desktop
, password
.c_str());
499 cout
<< con
.GetDBDB() << endl
;
502 // Dump list of Contact field names
503 if( list_contact_fields
) {
504 for( const ContactLdif::NameToFunc
*n
= ldif
.GetFieldNames(); n
->name
; n
++ ) {
506 cout
<< " " << left
<< setw(20) << n
->name
<< ": "
507 << n
->description
<< endl
;
511 // Dump current LDIF mapping
512 if( list_ldif_map
) {
513 cout
<< ldif
<< endl
;
516 // Dump list of contacts to an LDAP LDIF file
517 // This uses the Controller convenience templates
518 if( ldif_contacts
) {
519 // make sure we're in desktop mode
520 con
.OpenMode(Controller::Desktop
, password
.c_str());
522 // create a storage functor object that accepts
523 // Barry::Contact objects as input
524 Contact2Ldif
storage(ldif
);
526 // load all the Contact records into storage
527 con
.LoadDatabaseByType
<Barry::Contact
>(storage
);
530 // Dump record state table to stdout
532 if( dbNames
.size() == 0 ) {
533 cout
<< "No db names to process" << endl
;
537 vector
<string
>::iterator b
= dbNames
.begin();
538 for( ; b
!= dbNames
.end(); b
++ ) {
539 con
.OpenMode(Controller::Desktop
, password
.c_str());
540 unsigned int id
= con
.GetDBID(*b
);
541 RecordStateTable state
;
542 con
.GetRecordStateTable(id
, state
);
543 cout
<< "Record state table for: " << *b
<< endl
;
549 // Get Record mode overrides the default name mode
550 if( stCommands
.size() ) {
551 if( dbNames
.size() != 1 ) {
552 cout
<< "Must have 1 db name to process" << endl
;
556 con
.OpenMode(Controller::Desktop
, password
.c_str());
557 unsigned int id
= con
.GetDBID(dbNames
[0]);
558 auto_ptr
<Parser
> parse
= GetParser(dbNames
[0],filename
);
560 for( unsigned int i
= 0; i
< stCommands
.size(); i
++ ) {
561 con
.GetRecord(id
, stCommands
[i
].index
, *parse
.get());
563 if( stCommands
[i
].flag
== 'r' && stCommands
[i
].clear
) {
564 cout
<< "Clearing record's dirty flags..." << endl
;
565 con
.ClearDirty(id
, stCommands
[i
].index
);
568 if( stCommands
[i
].flag
== 'D' ) {
569 con
.DeleteRecord(id
, stCommands
[i
].index
);
576 // Dump contents of selected databases to stdout, or
577 // to file if specified.
578 // This is retrieving data from the Blackberry.
579 if( dbNames
.size() ) {
580 vector
<string
>::iterator b
= dbNames
.begin();
582 for( ; b
!= dbNames
.end(); b
++ ) {
583 con
.OpenMode(Controller::Desktop
, password
.c_str());
584 auto_ptr
<Parser
> parse
= GetParser(*b
,filename
);
585 unsigned int id
= con
.GetDBID(*b
);
586 con
.LoadDatabase(id
, *parse
.get());
590 // Save contents of file to specified databases
591 // This is writing data to the Blackberry.
592 if( saveDbNames
.size() ) {
593 vector
<string
>::iterator b
= saveDbNames
.begin();
595 for( ; b
!= saveDbNames
.end(); b
++ ) {
596 con
.OpenMode(Controller::Desktop
, password
.c_str());
597 auto_ptr
<Builder
> build
=
598 GetBuilder(*b
, filename
);
599 unsigned int id
= con
.GetDBID(*b
);
600 con
.SaveDatabase(id
, *build
);
605 catch( Usb::Error
&ue
) {
606 std::cerr
<< "Usb::Error caught: " << ue
.what() << endl
;
609 catch( Barry::Error
&se
) {
610 std::cerr
<< "Barry::Error caught: " << se
.what() << endl
;
613 catch( std::exception
&e
) {
614 std::cerr
<< "std::exception caught: " << e
.what() << endl
;