AUTHORS update
[barry.git] / tools / btool.cc
blobc1c96af0952a7a18e2dd4d742f1b99c1374b48e3
1 ///
2 /// \file btool.cc
3 /// Barry library tester
4 ///
6 /*
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>
23 #include <iomanip>
24 #include <iostream>
25 #include <fstream>
26 #include <vector>
27 #include <string>
28 #include <getopt.h>
31 using namespace std;
32 using namespace Barry;
34 void Usage()
36 int major, minor;
37 const char *Version = Barry::Version(major, minor);
39 cerr
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"
43 << "\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"
52 #endif
53 << " -h This help\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"
68 << "\n"
69 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
70 << "\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"
78 << endl;
81 class Contact2Ldif
83 public:
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>
95 struct Store
97 std::vector<Record> records;
98 mutable typename std::vector<Record>::const_iterator rec_it;
99 std::string filename;
100 bool load;
101 int count;
103 Store(const string &filename, bool load)
104 : rec_it(records.end()),
105 filename(filename),
106 load(load),
107 count(0)
109 #ifdef __BARRY_BOOST_MODE__
110 try {
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);
117 ia >> records;
118 cout << records.size()
119 << " records loaded from '"
120 << filename << "'" << endl;
121 sort(records.begin(), records.end());
122 rec_it = records.begin();
124 // debugging aid
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;
135 #endif
137 ~Store()
139 cout << "Store counted " << dec << count << " records." << endl;
140 #ifdef __BARRY_BOOST_MODE__
141 try {
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);
149 oa << r;
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;
158 #endif
161 // storage operator
162 void operator()(const Record &rec)
164 count++;
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() )
173 return false;
174 rec = *rec_it;
175 rec_it++;
176 return true;
180 class DataDumpParser : public Barry::Parser
182 uint32_t m_id;
184 public:
185 virtual void SetUniqueId(uint32_t id)
187 m_id = 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 else if( name == "Memos" ) {
223 return auto_ptr<Parser>(
224 new RecordParser<Memo, Store<Memo> > (
225 new Store<Memo>(filename, false)));
227 else if( name == "Tasks" ) {
228 return auto_ptr<Parser>(
229 new RecordParser<Task, Store<Task> > (
230 new Store<Task>(filename, false)));
232 else if( name == "PIN Messages" ) {
233 return auto_ptr<Parser>(
234 new RecordParser<PINMessage, Store<PINMessage> > (
235 new Store<PINMessage>(filename, false)));
237 else if( name == "Saved Email Messages" ) {
238 return auto_ptr<Parser>(
239 new RecordParser<SavedMessage, Store<SavedMessage> > (
240 new Store<SavedMessage>(filename, false)));
242 else {
243 // unknown database, use null parser
244 return auto_ptr<Parser>( new DataDumpParser );
248 auto_ptr<Builder> GetBuilder(const string &name, const string &filename)
250 // check for recognized database names
251 if( name == "Address Book" ) {
252 return auto_ptr<Builder>(
253 new RecordBuilder<Contact, Store<Contact> > (
254 new Store<Contact>(filename, true)));
257 else if( name == "Messages" ) {
258 return auto_ptr<Parser>(
259 new RecordParser<Message, Store<Message> > (
260 new Store<Message>(filename, true)));
262 else if( name == "Calendar" ) {
263 return auto_ptr<Parser>(
264 new RecordParser<Calendar, Store<Calendar> > (
265 new Store<Calendar>(filename, true)));
267 else if( name == "Service Book" ) {
268 return auto_ptr<Parser>(
269 new RecordParser<ServiceBook, Store<ServiceBook> > (
270 new Store<ServiceBook>(filename, true)));
272 else if( name == "Memos" ) {
273 return auto_ptr<Parser>(
274 new RecordParser<Memo, Store<Memo> > (
275 new Store<Memo>(filename, true)));
277 else if( name == "Tasks" ) {
278 return auto_ptr<Parser>(
279 new RecordParser<Task, Store<Task> > (
280 new Store<Task>(filename, true)));
283 else {
284 throw std::runtime_error("No Builder available for database");
288 struct StateTableCommand
290 char flag;
291 bool clear;
292 unsigned int index;
294 StateTableCommand(char f, bool c, unsigned int i)
295 : flag(f), clear(c), index(i) {}
298 bool SplitMap(const string &map, string &ldif, string &read, string &write)
300 string::size_type a = map.find(',');
301 if( a == string::npos )
302 return false;
304 string::size_type b = map.find(',', a+1);
305 if( b == string::npos )
306 return false;
308 ldif.assign(map, 0, a);
309 read.assign(map, a + 1, b - a - 1);
310 write.assign(map, b + 1, map.size() - b - 1);
312 return ldif.size() && read.size() && write.size();
315 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
317 for( vector<string>::const_iterator i = mapCommands.begin();
318 i != mapCommands.end();
319 ++i )
321 // single names mean unmapping
322 if( i->find(',') == string::npos ) {
323 // unmap
324 cerr << "Unmapping: " << *i << endl;
325 ldif.Unmap(*i);
327 else {
328 cerr << "Mapping: " << *i << endl;
330 // map... extract ldif/read/write names
331 string ldifname, read, write;
332 if( SplitMap(*i, ldifname, read, write) ) {
333 if( !ldif.Map(ldifname, read, write) ) {
334 cerr << "Read/Write name unknown: " << *i << endl;
337 else {
338 cerr << "Invalid map format: " << *i << endl;
344 int main(int argc, char *argv[])
346 cout.sync_with_stdio(true); // leave this on, since libusb uses
347 // stdio for debug messages
349 try {
351 uint32_t pin = 0;
352 bool list_only = false,
353 show_dbdb = false,
354 ldif_contacts = false,
355 data_dump = false,
356 reset_device = false,
357 list_contact_fields = false,
358 list_ldif_map = false,
359 record_state = false;
360 string ldifBaseDN, ldifDnAttr;
361 string filename;
362 string password;
363 vector<string> dbNames, saveDbNames, mapCommands;
364 vector<StateTableCommand> stCommands;
366 // process command line options
367 for(;;) {
368 int cmd = getopt(argc, argv, "c:C:d:D:f:hlLm:Mp:P:r:R:s:tT:vX");
369 if( cmd == -1 )
370 break;
372 switch( cmd )
374 case 'c': // contacts to ldap ldif
375 ldif_contacts = true;
376 ldifBaseDN = optarg;
377 break;
379 case 'C': // DN Attribute for FQDN
380 ldifDnAttr = optarg;
381 break;
383 case 'd': // show dbname
384 dbNames.push_back(string(optarg));
385 break;
387 case 'D': // delete record
388 stCommands.push_back(
389 StateTableCommand('D', false, atoi(optarg)));
390 break;
392 case 'f': // filename
393 #ifdef __BARRY_BOOST_MODE__
394 filename = optarg;
395 #else
396 cerr << "-f option not supported - no Boost "
397 "serialization support available\n";
398 return 1;
399 #endif
400 break;
401 case 'l': // list only
402 list_only = true;
403 break;
405 case 'L': // List Contact field names
406 list_contact_fields = true;
407 break;
409 case 'm': // Map / Unmap
410 mapCommands.push_back(string(optarg));
411 break;
413 case 'M': // List LDIF map
414 list_ldif_map = true;
415 break;
417 case 'p': // Blackberry PIN
418 pin = strtoul(optarg, NULL, 16);
419 break;
421 case 'P': // Device password
422 password = optarg;
423 break;
425 case 'r': // get specific record index
426 stCommands.push_back(
427 StateTableCommand('r', false, atoi(optarg)));
428 break;
430 case 'R': // same as 'r', and clears dirty
431 stCommands.push_back(
432 StateTableCommand('r', true, atoi(optarg)));
433 break;
435 case 's': // save dbname
436 saveDbNames.push_back(string(optarg));
437 break;
439 case 't': // display database database
440 show_dbdb = true;
441 break;
443 case 'T': // show RecordStateTable
444 record_state = true;
445 dbNames.push_back(string(optarg));
446 break;
448 case 'v': // data dump on
449 data_dump = true;
450 break;
452 case 'X': // reset device
453 reset_device = true;
454 break;
456 case 'h': // help
457 default:
458 Usage();
459 return 0;
463 // Initialize the barry library. Must be called before
464 // anything else.
465 Barry::Init(data_dump);
467 // LDIF class... only needed if ldif output turned on
468 ContactLdif ldif(ldifBaseDN);
469 DoMapping(ldif, mapCommands);
470 if( ldifDnAttr.size() ) {
471 if( !ldif.SetDNAttr(ldifDnAttr) ) {
472 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
476 // Probe the USB bus for Blackberry devices and display.
477 // If user has specified a PIN, search for it in the
478 // available device list here as well
479 Barry::Probe probe;
480 int activeDevice = -1;
481 if( ldif_contacts )
482 cout << "# ";
483 cout << "Blackberry devices found:" << endl;
484 for( int i = 0; i < probe.GetCount(); i++ ) {
485 if( ldif_contacts )
486 cout << "# ";
487 cout << probe.Get(i) << endl;
488 if( probe.Get(i).m_pin == pin )
489 activeDevice = i;
492 if( list_only )
493 return 0; // done
495 if( activeDevice == -1 ) {
496 if( pin == 0 ) {
497 // can we default to single device?
498 if( probe.GetCount() == 1 )
499 activeDevice = 0;
500 else {
501 cerr << "No device selected" << endl;
502 return 1;
505 else {
506 cerr << "PIN " << setbase(16) << pin
507 << " not found" << endl;
508 return 1;
512 if( reset_device ) {
513 Usb::Device dev(probe.Get(activeDevice).m_dev);
514 dev.Reset();
515 return 0;
518 // Create our controller object
519 Barry::Controller con(probe.Get(activeDevice));
522 // execute each mode that was turned on
526 // Dump list of all databases to stdout
527 if( show_dbdb ) {
528 // open desktop mode socket
529 con.OpenMode(Controller::Desktop, password.c_str());
530 cout << con.GetDBDB() << endl;
533 // Dump list of Contact field names
534 if( list_contact_fields ) {
535 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
536 cout.fill(' ');
537 cout << " " << left << setw(20) << n->name << ": "
538 << n->description << endl;
542 // Dump current LDIF mapping
543 if( list_ldif_map ) {
544 cout << ldif << endl;
547 // Dump list of contacts to an LDAP LDIF file
548 // This uses the Controller convenience templates
549 if( ldif_contacts ) {
550 // make sure we're in desktop mode
551 con.OpenMode(Controller::Desktop, password.c_str());
553 // create a storage functor object that accepts
554 // Barry::Contact objects as input
555 Contact2Ldif storage(ldif);
557 // load all the Contact records into storage
558 con.LoadDatabaseByType<Barry::Contact>(storage);
561 // Dump record state table to stdout
562 if( record_state ) {
563 if( dbNames.size() == 0 ) {
564 cout << "No db names to process" << endl;
565 return 1;
568 vector<string>::iterator b = dbNames.begin();
569 for( ; b != dbNames.end(); b++ ) {
570 con.OpenMode(Controller::Desktop, password.c_str());
571 unsigned int id = con.GetDBID(*b);
572 RecordStateTable state;
573 con.GetRecordStateTable(id, state);
574 cout << "Record state table for: " << *b << endl;
575 cout << state;
577 return 0;
580 // Get Record mode overrides the default name mode
581 if( stCommands.size() ) {
582 if( dbNames.size() != 1 ) {
583 cout << "Must have 1 db name to process" << endl;
584 return 1;
587 con.OpenMode(Controller::Desktop, password.c_str());
588 unsigned int id = con.GetDBID(dbNames[0]);
589 auto_ptr<Parser> parse = GetParser(dbNames[0],filename);
591 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
592 con.GetRecord(id, stCommands[i].index, *parse.get());
594 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
595 cout << "Clearing record's dirty flags..." << endl;
596 con.ClearDirty(id, stCommands[i].index);
599 if( stCommands[i].flag == 'D' ) {
600 con.DeleteRecord(id, stCommands[i].index);
604 return 0;
607 // Dump contents of selected databases to stdout, or
608 // to file if specified.
609 // This is retrieving data from the Blackberry.
610 if( dbNames.size() ) {
611 vector<string>::iterator b = dbNames.begin();
613 for( ; b != dbNames.end(); b++ ) {
614 con.OpenMode(Controller::Desktop, password.c_str());
615 auto_ptr<Parser> parse = GetParser(*b,filename);
616 unsigned int id = con.GetDBID(*b);
617 con.LoadDatabase(id, *parse.get());
621 // Save contents of file to specified databases
622 // This is writing data to the Blackberry.
623 if( saveDbNames.size() ) {
624 vector<string>::iterator b = saveDbNames.begin();
626 for( ; b != saveDbNames.end(); b++ ) {
627 con.OpenMode(Controller::Desktop, password.c_str());
628 auto_ptr<Builder> build =
629 GetBuilder(*b, filename);
630 unsigned int id = con.GetDBID(*b);
631 con.SaveDatabase(id, *build);
636 catch( Usb::Error &ue) {
637 std::cerr << "Usb::Error caught: " << ue.what() << endl;
638 return 1;
640 catch( Barry::Error &se ) {
641 std::cerr << "Barry::Error caught: " << se.what() << endl;
642 return 1;
644 catch( std::exception &e ) {
645 std::cerr << "std::exception caught: " << e.what() << endl;
646 return 1;
649 return 0;