Added libbarryalx.* to DEB binary package (RPM uses wildcards, so already done)
[barry.git] / tools / btool.cc
blob6a68a70e9f5a173c825b4de4120a602bddf2ea52
1 ///
2 /// \file btool.cc
3 /// Barry library tester
4 ///
6 /*
7 Copyright (C) 2005-2010, 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 #ifdef __BARRY_SYNC_MODE__
24 #include <barry/barrysync.h>
25 #endif
26 #ifdef __BARRY_BACKUP_MODE__
27 #include <barry/barrybackup.h>
28 #endif
30 #include <iomanip>
31 #include <iostream>
32 #include <fstream>
33 #include <sstream>
34 #include <vector>
35 #include <string>
36 #include <algorithm>
37 #include <getopt.h>
38 #include <tr1/memory>
39 #include "i18n.h"
42 using namespace std;
43 using namespace std::tr1;
44 using namespace Barry;
46 void Usage()
48 int major, minor;
49 const char *Version = Barry::Version(major, minor);
51 cerr
52 << "btool - Command line USB Blackberry Test Tool\n"
53 << " Copyright 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)\n"
54 << " Using: " << Version << "\n"
55 << " Compiled "
56 #ifdef __BARRY_BOOST_MODE__
57 << "with"
58 #else
59 << "without"
60 #endif
61 << " Boost support\n"
62 << "\n"
63 << " -b file Filename to save or load a Barry Backup to (tar.gz)\n"
64 << " -B bus Specify which USB bus to search on\n"
65 << " -N dev Specify which system device, using system specific string\n"
66 << "\n"
67 << " -a db Erase / clear database 'db' FROM device, deleting all\n"
68 << " its records. Can be used multiple times to clear more\n"
69 << " than one DB.\n"
70 << " -c dn Convert address book database to LDIF format, using the\n"
71 << " specified baseDN\n"
72 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
73 << " Defaults to 'cn'\n"
74 << " -d db Load database 'db' FROM device and dump to screen\n"
75 << " Can be used multiple times to fetch more than one DB\n"
76 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
77 << " string separated by a comma, holding the read,write\n"
78 << " endpoint pair. Example: -e 83,5\n"
79 << " Note: Endpoints are specified in hex.\n"
80 << " You should never need to use this option.\n"
81 #ifdef __BARRY_BOOST_MODE__
82 << " -f file Filename to save or load handheld data to/from\n"
83 #endif
84 << " -h This help\n"
85 << " -i cs International charset for string conversions\n"
86 << " Valid values here are available with 'iconv --list'\n"
87 << " -I Sort records before output\n"
88 << " -l List devices\n"
89 << " -L List Contact field names\n"
90 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
91 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
92 << " Unmap: ldif name alone\n"
93 << " -M List current LDIF mapping\n"
94 << " -n Use null parser on all databases.\n"
95 << " -p pin PIN of device to talk with\n"
96 << " If only one device is plugged in, this flag is optional\n"
97 << " -P pass Simplistic method to specify device password\n"
98 << " -s db Save database 'db' TO device from data loaded from -f file\n"
99 << " -S Show list of supported database parsers\n"
100 << " -t Show database database table\n"
101 << " -T db Show record state table for given database\n"
102 << " -v Dump protocol data during operation\n"
103 #ifdef __BARRY_SYNC_MODE__
104 << " -V Dump records using MIME vformats where possible\n"
105 #endif
106 << " -X Reset device\n"
107 << " -z Use non-threaded sockets\n"
108 << " -Z Use threaded socket router (default)\n"
109 << "\n"
110 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
111 << "\n"
112 << " -r # Record index number as seen in the -T state table.\n"
113 << " This overrides the default -d behaviour, and only\n"
114 << " downloads the one specified record, sending to stdout.\n"
115 << " -R # Same as -r, but also clears the record's dirty flags.\n"
116 << " -D # Record index number as seen in the -T state table,\n"
117 << " which indicates the record to delete. Used with the -d\n"
118 << " command to specify the database.\n"
119 << endl;
122 class Contact2Ldif
124 public:
125 Barry::ContactLdif &ldif;
127 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
129 void operator()(const Contact &rec)
131 ldif.DumpLdif(cout, rec);
135 #ifdef __BARRY_SYNC_MODE__
136 template <class Record>
137 class MimeDump
139 public:
140 void Dump(std::ostream &os, const Record &rec)
142 os << rec << endl;
145 static bool Supported() { return false; }
148 template <>
149 class MimeDump<Contact>
151 public:
152 void Dump(std::ostream &os, const Contact &rec)
154 Sync::vCard vcard;
155 os << vcard.ToVCard(rec) << endl;
158 static bool Supported() { return true; }
161 template <>
162 class MimeDump<Calendar>
164 public:
165 void Dump(std::ostream &os, const Calendar &rec)
167 Sync::vTimeConverter vtc;
168 Sync::vCalendar vcal(vtc);
169 os << vcal.ToVCal(rec) << endl;
172 static bool Supported() { return true; }
175 template <>
176 class MimeDump<Memo>
178 public:
179 void Dump(std::ostream &os, const Memo &rec)
181 Sync::vJournal vjournal;
182 os << vjournal.ToMemo(rec) << endl;
185 static bool Supported() { return true; }
188 template <>
189 class MimeDump<Task>
191 public:
192 void Dump(std::ostream &os, const Task &rec)
194 Sync::vTimeConverter vtc;
195 Sync::vTodo vtodo(vtc);
196 os << vtodo.ToTask(rec) << endl;
199 static bool Supported() { return true; }
201 #endif
203 template <class Record>
204 struct Store
206 std::vector<Record> records;
207 mutable typename std::vector<Record>::const_iterator rec_it;
208 std::string filename;
209 bool load;
210 bool immediate_display;
211 bool vformat_mode;
212 int count;
214 Store(const string &filename, bool load, bool immediate_display,
215 bool vformat_mode)
216 : rec_it(records.end()),
217 filename(filename),
218 load(load),
219 immediate_display(immediate_display),
220 vformat_mode(vformat_mode),
221 count(0)
223 #ifdef __BARRY_BOOST_MODE__
224 try {
226 if( load && filename.size() ) {
227 // filename is available, attempt to load
228 cout << "Loading: " << filename << endl;
229 ifstream ifs(filename.c_str());
230 std::string dbName;
231 getline(ifs, dbName);
232 boost::archive::text_iarchive ia(ifs);
233 ia >> records;
234 cout << records.size()
235 << " records loaded from '"
236 << filename << "'" << endl;
237 sort(records.begin(), records.end());
238 rec_it = records.begin();
240 // debugging aid
241 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
242 for( ; beg != end; beg++ ) {
243 cout << (*beg) << endl;
247 } catch( boost::archive::archive_exception &ae ) {
248 cerr << "Archive exception in ~Store(): "
249 << ae.what() << endl;
251 #endif
254 ~Store()
256 if( !immediate_display ) {
257 // not dumped yet, sort then dump
258 sort(records.begin(), records.end());
259 DumpAll();
262 cout << "Store counted " << dec << count << " records." << endl;
263 #ifdef __BARRY_BOOST_MODE__
264 try {
266 if( !load && filename.size() ) {
267 // filename is available, attempt to save
268 cout << "Saving: " << filename << endl;
269 const std::vector<Record> &r = records;
270 ofstream ofs(filename.c_str());
271 ofs << Record::GetDBName() << endl;
272 boost::archive::text_oarchive oa(ofs);
273 oa << r;
274 cout << dec << r.size() << " records saved to '"
275 << filename << "'" << endl;
278 } catch( boost::archive::archive_exception &ae ) {
279 cerr << "Archive exception in ~Store(): "
280 << ae.what() << endl;
282 #endif
285 void DumpAll()
287 typename vector<Record>::const_iterator i = records.begin();
288 for( ; i != records.end(); ++i ) {
289 Dump(*i);
293 void Dump(const Record &rec)
295 if( vformat_mode ) {
296 #ifdef __BARRY_SYNC_MODE__
297 MimeDump<Record> md;
298 md.Dump(cout, rec);
299 #endif
301 else {
302 cout << rec << endl;
306 // storage operator
307 void operator()(const Record &rec)
309 count++;
310 if( immediate_display )
311 Dump(rec);
312 records.push_back(rec);
315 // retrieval operator
316 bool operator()(Record &rec, Builder &builder) const
318 if( rec_it == records.end() )
319 return false;
320 rec = *rec_it;
321 rec_it++;
322 return true;
326 shared_ptr<Parser> GetParser(const string &name,
327 const string &filename,
328 bool null_parser,
329 bool immediate_display,
330 bool vformat_mode,
331 bool bbackup_mode)
333 bool dnow = immediate_display;
334 bool vmode = vformat_mode;
336 if( null_parser ) {
337 // use null parser
338 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
340 else if( bbackup_mode ) {
341 #ifdef __BARRY_BACKUP_MODE__
342 // Only one backup file per run
343 static shared_ptr<Parser> backup;
344 if( !backup.get() ) {
345 backup.reset( new Backup(filename) );
347 return backup;
348 #else
349 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
350 #endif
352 // check for recognized database names
353 else if( name == Contact::GetDBName() ) {
354 return shared_ptr<Parser>(
355 new RecordParser<Contact, Store<Contact> > (
356 new Store<Contact>(filename, false, dnow, vmode)));
358 else if( name == Message::GetDBName() ) {
359 return shared_ptr<Parser>(
360 new RecordParser<Message, Store<Message> > (
361 new Store<Message>(filename, false, dnow, vmode)));
363 else if( name == Calendar::GetDBName() ) {
364 return shared_ptr<Parser>(
365 new RecordParser<Calendar, Store<Calendar> > (
366 new Store<Calendar>(filename, false, dnow, vmode)));
368 else if( name == CalendarAll::GetDBName() ) {
369 return shared_ptr<Parser>(
370 new RecordParser<CalendarAll, Store<CalendarAll> > (
371 new Store<CalendarAll>(filename, false, dnow, vmode)));
373 else if( name == CallLog::GetDBName() ) {
374 return shared_ptr<Parser>(
375 new RecordParser<CallLog, Store<CallLog> > (
376 new Store<CallLog>(filename, false, dnow, vmode)));
378 else if( name == Bookmark::GetDBName() ) {
379 return shared_ptr<Parser>(
380 new RecordParser<Bookmark, Store<Bookmark> > (
381 new Store<Bookmark>(filename, false, dnow, vmode)));
383 else if( name == ServiceBook::GetDBName() ) {
384 return shared_ptr<Parser>(
385 new RecordParser<ServiceBook, Store<ServiceBook> > (
386 new Store<ServiceBook>(filename, false, dnow, vmode)));
389 else if( name == Memo::GetDBName() ) {
390 return shared_ptr<Parser>(
391 new RecordParser<Memo, Store<Memo> > (
392 new Store<Memo>(filename, false, dnow, vmode)));
394 else if( name == Task::GetDBName() ) {
395 return shared_ptr<Parser>(
396 new RecordParser<Task, Store<Task> > (
397 new Store<Task>(filename, false, dnow, vmode)));
399 else if( name == PINMessage::GetDBName() ) {
400 return shared_ptr<Parser>(
401 new RecordParser<PINMessage, Store<PINMessage> > (
402 new Store<PINMessage>(filename, false, dnow, vmode)));
404 else if( name == SavedMessage::GetDBName() ) {
405 return shared_ptr<Parser>(
406 new RecordParser<SavedMessage, Store<SavedMessage> > (
407 new Store<SavedMessage>(filename, false, dnow, vmode)));
409 else if( name == Sms::GetDBName() ) {
410 return shared_ptr<Parser>(
411 new RecordParser<Sms, Store<Sms> > (
412 new Store<Sms>(filename, false, dnow, vmode)));
414 else if( name == Folder::GetDBName() ) {
415 return shared_ptr<Parser>(
416 new RecordParser<Folder, Store<Folder> > (
417 new Store<Folder>(filename, false, dnow, vmode)));
419 else if( name == Timezone::GetDBName() ) {
420 return shared_ptr<Parser>(
421 new RecordParser<Timezone, Store<Timezone> > (
422 new Store<Timezone>(filename, false, dnow, vmode)));
424 else {
425 // unknown database, use null parser
426 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
430 shared_ptr<Builder> GetBuilder(const string &name, const string &filename)
432 // check for recognized database names
433 if( name == Contact::GetDBName() ) {
434 return shared_ptr<Builder>(
435 new RecordBuilder<Contact, Store<Contact> > (
436 new Store<Contact>(filename, true, true, false)));
438 else if( name == Calendar::GetDBName() ) {
439 return shared_ptr<Builder>(
440 new RecordBuilder<Calendar, Store<Calendar> > (
441 new Store<Calendar>(filename, true, true, false)));
443 else if( name == CalendarAll::GetDBName() ) {
444 return shared_ptr<Builder>(
445 new RecordBuilder<CalendarAll, Store<CalendarAll> > (
446 new Store<CalendarAll>(filename, true, true, false)));
448 else if( name == Memo::GetDBName() ) {
449 return shared_ptr<Builder>(
450 new RecordBuilder<Memo, Store<Memo> > (
451 new Store<Memo>(filename, true, true, false)));
453 else if( name == Task::GetDBName() ) {
454 return shared_ptr<Builder>(
455 new RecordBuilder<Task, Store<Task> > (
456 new Store<Task>(filename, true, true, false)));
459 else if( name == "Messages" ) {
460 return shared_ptr<Parser>(
461 new RecordParser<Message, Store<Message> > (
462 new Store<Message>(filename, true, true, false)));
464 else if( name == "Service Book" ) {
465 return shared_ptr<Parser>(
466 new RecordParser<ServiceBook, Store<ServiceBook> > (
467 new Store<ServiceBook>(filename, true, true, false)));
470 else {
471 throw std::runtime_error("No Builder available for database");
475 void ShowParsers()
477 cout << "Supported Database parsers:\n"
478 << " (* = can display in vformat MIME mode)\n"
479 << " Address Book *\n"
480 << " Messages\n"
481 << " Calendar *\n"
482 << " Calendar - All *\n"
483 << " Phone Call Logs\n"
484 << " Browser Bookmarks\n"
485 << " Service Book\n"
486 << " Memos *\n"
487 << " Tasks *\n"
488 << " PIN Messages\n"
489 << " Saved Email Messages\n"
490 << " SMS Messages\n"
491 << " Folders\n"
492 << " Time Zones (read only)\n"
493 << "\n"
494 << "Supported Database builders:\n"
495 << " Address Book\n"
496 << " Calendar\n"
497 << " Calendar - All\n"
498 << " Memo\n"
499 << " Task\n"
500 << endl;
503 struct StateTableCommand
505 char flag;
506 bool clear;
507 unsigned int index;
509 StateTableCommand(char f, bool c, unsigned int i)
510 : flag(f), clear(c), index(i) {}
513 bool SplitMap(const string &map, string &ldif, string &read, string &write)
515 string::size_type a = map.find(',');
516 if( a == string::npos )
517 return false;
519 string::size_type b = map.find(',', a+1);
520 if( b == string::npos )
521 return false;
523 ldif.assign(map, 0, a);
524 read.assign(map, a + 1, b - a - 1);
525 write.assign(map, b + 1, map.size() - b - 1);
527 return ldif.size() && read.size() && write.size();
530 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
532 for( vector<string>::const_iterator i = mapCommands.begin();
533 i != mapCommands.end();
534 ++i )
536 // single names mean unmapping
537 if( i->find(',') == string::npos ) {
538 // unmap
539 cerr << "Unmapping: " << *i << endl;
540 ldif.Unmap(*i);
542 else {
543 cerr << "Mapping: " << *i << endl;
545 // map... extract ldif/read/write names
546 string ldifname, read, write;
547 if( SplitMap(*i, ldifname, read, write) ) {
548 if( !ldif.Map(ldifname, read, write) ) {
549 cerr << "Read/Write name unknown: " << *i << endl;
552 else {
553 cerr << "Invalid map format: " << *i << endl;
559 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
561 int read, write;
562 char comma;
563 istringstream iss(arg);
564 iss >> hex >> read >> comma >> write;
565 if( !iss )
566 return false;
567 epp->read = read;
568 epp->write = write;
569 return true;
572 int main(int argc, char *argv[])
574 INIT_I18N(PACKAGE);
576 cout.sync_with_stdio(true); // leave this on, since libusb uses
577 // stdio for debug messages
579 try {
581 uint32_t pin = 0;
582 bool list_only = false,
583 show_dbdb = false,
584 ldif_contacts = false,
585 data_dump = false,
586 vformat_mode = false,
587 reset_device = false,
588 list_contact_fields = false,
589 list_ldif_map = false,
590 epp_override = false,
591 threaded_sockets = true,
592 record_state_table = false,
593 clear_database = false,
594 null_parser = false,
595 bbackup_mode = false,
596 sort_records = false;
597 string ldifBaseDN, ldifDnAttr;
598 string filename;
599 string password;
600 string busname;
601 string devname;
602 string iconvCharset;
603 vector<string> dbNames, saveDbNames, mapCommands, clearDbNames;
604 vector<StateTableCommand> stCommands;
605 Usb::EndpointPair epOverride;
607 // process command line options
608 for(;;) {
609 int cmd = getopt(argc, argv, "a:b:B:c:C:d:D:e:f:hi:IlLm:MnN:p:P:r:R:Ss:tT:vVXzZ");
610 if( cmd == -1 )
611 break;
613 switch( cmd )
615 case 'a': // Clear Database
616 clear_database = true;
617 clearDbNames.push_back(string(optarg));
618 break;
620 case 'b': // Barry backup filename (tar.gz)
621 #ifdef __BARRY_BACKUP_MODE__
622 if( filename.size() == 0 ) {
623 filename = optarg;
624 bbackup_mode = true;
626 else {
627 cerr << "Do not use -f with -b\n";
628 return 1;
630 #else
631 cerr << "-b option not supported - no Barry "
632 "Backup library support available\n";
633 return 1;
634 #endif
635 break;
637 case 'B': // busname
638 busname = optarg;
639 break;
641 case 'c': // contacts to ldap ldif
642 ldif_contacts = true;
643 ldifBaseDN = optarg;
644 break;
646 case 'C': // DN Attribute for FQDN
647 ldifDnAttr = optarg;
648 break;
650 case 'd': // show dbname
651 dbNames.push_back(string(optarg));
652 break;
654 case 'D': // delete record
655 stCommands.push_back(
656 StateTableCommand('D', false, atoi(optarg)));
657 break;
659 case 'e': // endpoint override
660 if( !ParseEpOverride(optarg, &epOverride) ) {
661 Usage();
662 return 1;
664 epp_override = true;
665 break;
667 case 'f': // filename
668 #ifdef __BARRY_BOOST_MODE__
669 if( !bbackup_mode && filename.size() == 0 ) {
670 filename = optarg;
672 else {
673 cerr << "Do not use -f with -b\n";
674 return 1;
676 #else
677 cerr << "-f option not supported - no Boost "
678 "serialization support available\n";
679 return 1;
680 #endif
681 break;
683 case 'i': // international charset (iconv)
684 iconvCharset = optarg;
685 break;
687 case 'I': // sort before dump
688 sort_records = true;
689 break;
691 case 'l': // list only
692 list_only = true;
693 break;
695 case 'L': // List Contact field names
696 list_contact_fields = true;
697 break;
699 case 'm': // Map / Unmap
700 mapCommands.push_back(string(optarg));
701 break;
703 case 'M': // List LDIF map
704 list_ldif_map = true;
705 break;
707 case 'n': // use null parser
708 null_parser = true;
709 break;
711 case 'N': // Devname
712 devname = optarg;
713 break;
715 case 'p': // Blackberry PIN
716 pin = strtoul(optarg, NULL, 16);
717 break;
719 case 'P': // Device password
720 password = optarg;
721 break;
723 case 'r': // get specific record index
724 stCommands.push_back(
725 StateTableCommand('r', false, atoi(optarg)));
726 break;
728 case 'R': // same as 'r', and clears dirty
729 stCommands.push_back(
730 StateTableCommand('r', true, atoi(optarg)));
731 break;
733 case 's': // save dbname
734 saveDbNames.push_back(string(optarg));
735 break;
737 case 'S': // show supported databases
738 ShowParsers();
739 return 0;
741 case 't': // display database database
742 show_dbdb = true;
743 break;
745 case 'T': // show RecordStateTable
746 record_state_table = true;
747 dbNames.push_back(string(optarg));
748 break;
750 case 'v': // data dump on
751 data_dump = true;
752 break;
754 case 'V': // vformat MIME mode
755 #ifdef __BARRY_SYNC_MODE__
756 vformat_mode = true;
757 #else
758 cerr << "-V option not supported - no Sync "
759 "library support available\n";
760 return 1;
761 #endif
762 break;
764 case 'X': // reset device
765 reset_device = true;
766 break;
768 case 'z': // non-threaded sockets
769 threaded_sockets = false;
770 break;
772 case 'Z': // threaded socket router
773 threaded_sockets = true;
774 break;
776 case 'h': // help
777 default:
778 Usage();
779 return 0;
783 // Initialize the barry library. Must be called before
784 // anything else.
785 Barry::Init(data_dump);
786 if( data_dump ) {
787 int major, minor;
788 const char *Version = Barry::Version(major, minor);
789 cout << Version << endl;
792 // Create an IConverter object if needed
793 auto_ptr<IConverter> ic;
794 if( iconvCharset.size() ) {
795 ic.reset( new IConverter(iconvCharset.c_str(), true) );
798 // LDIF class... only needed if ldif output turned on
799 ContactLdif ldif(ldifBaseDN);
800 DoMapping(ldif, mapCommands);
801 if( ldifDnAttr.size() ) {
802 if( !ldif.SetDNAttr(ldifDnAttr) ) {
803 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
807 // Probe the USB bus for Blackberry devices and display.
808 // If user has specified a PIN, search for it in the
809 // available device list here as well
810 Barry::Probe probe(busname.c_str(), devname.c_str(),
811 epp_override ? &epOverride : 0);
812 int activeDevice = -1;
814 // show any errors during probe first
815 if( probe.GetFailCount() ) {
816 if( ldif_contacts )
817 cout << "# ";
818 cout << "Blackberry device errors with errors during probe:" << endl;
819 for( int i = 0; i < probe.GetFailCount(); i++ ) {
820 if( ldif_contacts )
821 cout << "# ";
822 cout << probe.GetFailMsg(i) << endl;
826 // show all successfully found devices
827 if( ldif_contacts )
828 cout << "# ";
829 cout << "Blackberry devices found:" << endl;
830 for( int i = 0; i < probe.GetCount(); i++ ) {
831 if( ldif_contacts )
832 cout << "# ";
833 if( data_dump )
834 probe.Get(i).DumpAll(cout);
835 else
836 cout << probe.Get(i);
837 cout << endl;
838 if( probe.Get(i).m_pin == pin )
839 activeDevice = i;
842 if( list_only )
843 return 0; // done
845 if( activeDevice == -1 ) {
846 if( pin == 0 ) {
847 // can we default to single device?
848 if( probe.GetCount() == 1 )
849 activeDevice = 0;
850 else {
851 cerr << "No device selected" << endl;
852 return 1;
855 else {
856 cerr << "PIN " << setbase(16) << pin
857 << " not found" << endl;
858 return 1;
862 if( ldif_contacts )
863 cout << "# ";
864 cout << "Using device (PIN): "
865 << probe.Get(activeDevice).m_pin.str() << endl;
867 if( reset_device ) {
868 Usb::Device dev(probe.Get(activeDevice).m_dev);
869 dev.Reset();
870 return 0;
873 // Override device endpoints if user asks
874 Barry::ProbeResult device = probe.Get(activeDevice);
875 if( epp_override ) {
876 device.m_ep.read = epOverride.read;
877 device.m_ep.write = epOverride.write;
878 device.m_ep.type = 2; // FIXME - override this too?
879 cout << "Endpoint pair (read,write) overridden with: "
880 << hex
881 << (unsigned int) device.m_ep.read << ","
882 << (unsigned int) device.m_ep.write << endl;
886 // execute each mode that was turned on
890 // Dump current LDIF mapping
891 if( list_ldif_map ) {
892 cout << ldif << endl;
895 // Dump list of Contact field names
896 if( list_contact_fields ) {
897 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
898 cout.fill(' ');
899 cout << " " << left << setw(20) << n->name << ": "
900 << n->description << endl;
904 // Check if Desktop access is needed
905 if( !( show_dbdb ||
906 ldif_contacts ||
907 record_state_table ||
908 clear_database ||
909 stCommands.size() ||
910 dbNames.size() ||
911 saveDbNames.size() ) )
912 return 0; // done
915 // Create our controller object
917 // Order is important in the following auto_ptr<> objects,
918 // since Controller must get destroyed before router.
919 // Normally you'd pick one method, and not bother
920 // with auto_ptr<> and so the normal C++ constructor
921 // rules would guarantee this safety for you, but
922 // here we want the user to pick.
924 auto_ptr<SocketRoutingQueue> router;
925 auto_ptr<Barry::Controller> pcon;
926 if( threaded_sockets ) {
927 router.reset( new SocketRoutingQueue );
928 router->SpinoffSimpleReadThread();
929 pcon.reset( new Barry::Controller(device, *router) );
931 else {
932 pcon.reset( new Barry::Controller(device) );
935 Barry::Controller &con = *pcon;
936 Barry::Mode::Desktop desktop(con, *ic);
937 desktop.Open(password.c_str());
939 // Dump list of all databases to stdout
940 if( show_dbdb ) {
941 // open desktop mode socket
942 cout << desktop.GetDBDB() << endl;
945 // Dump list of contacts to an LDAP LDIF file
946 // This uses the Controller convenience templates
947 if( ldif_contacts ) {
948 // create a storage functor object that accepts
949 // Barry::Contact objects as input
950 Contact2Ldif storage(ldif);
952 // load all the Contact records into storage
953 desktop.LoadDatabaseByType<Barry::Contact>(storage);
956 // Dump record state table to stdout
957 if( record_state_table ) {
958 if( dbNames.size() == 0 ) {
959 cout << "No db names to process" << endl;
960 return 1;
963 vector<string>::iterator b = dbNames.begin();
964 for( ; b != dbNames.end(); b++ ) {
965 unsigned int id = desktop.GetDBID(*b);
966 RecordStateTable state;
967 desktop.GetRecordStateTable(id, state);
968 cout << "Record state table for: " << *b << endl;
969 cout << state;
971 return 0;
974 // Get Record mode overrides the default name mode
975 if( stCommands.size() ) {
976 if( dbNames.size() != 1 ) {
977 cout << "Must have 1 db name to process" << endl;
978 return 1;
981 unsigned int id = desktop.GetDBID(dbNames[0]);
982 shared_ptr<Parser> parse = GetParser(dbNames[0],filename,
983 null_parser, true, vformat_mode, bbackup_mode);
985 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
986 desktop.GetRecord(id, stCommands[i].index, *parse.get());
988 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
989 cout << "Clearing record's dirty flags..." << endl;
990 desktop.ClearDirty(id, stCommands[i].index);
993 if( stCommands[i].flag == 'D' ) {
994 desktop.DeleteRecord(id, stCommands[i].index);
998 return 0;
1001 // Dump contents of selected databases to stdout, or
1002 // to file if specified.
1003 // This is retrieving data from the Blackberry.
1004 if( dbNames.size() ) {
1005 vector<string>::iterator b = dbNames.begin();
1007 for( ; b != dbNames.end(); b++ ) {
1008 shared_ptr<Parser> parse = GetParser(*b,
1009 filename, null_parser, !sort_records,
1010 vformat_mode, bbackup_mode);
1011 unsigned int id = desktop.GetDBID(*b);
1012 desktop.LoadDatabase(id, *parse.get());
1016 // Clear databases
1017 if( clear_database ) {
1018 if( clearDbNames.size() == 0 ) {
1019 cout << "No db names to erase" << endl;
1020 return 1;
1023 vector<string>::iterator b = clearDbNames.begin();
1025 for( ; b != clearDbNames.end(); b++ ) {
1026 unsigned int id = desktop.GetDBID(*b);
1027 cout << "Deleting all records from " << (*b) << "..." << endl;
1028 desktop.ClearDatabase(id);
1031 return 0;
1034 // Save contents of file to specified databases
1035 // This is writing data to the Blackberry.
1036 if( saveDbNames.size() ) {
1037 vector<string>::iterator b = saveDbNames.begin();
1039 for( ; b != saveDbNames.end(); b++ ) {
1040 shared_ptr<Builder> build = GetBuilder(*b,
1041 filename);
1042 unsigned int id = desktop.GetDBID(*b);
1043 desktop.SaveDatabase(id, *build);
1048 catch( Usb::Error &ue ) {
1049 std::cerr << "Usb::Error caught: " << ue.what() << endl;
1050 return 1;
1052 catch( Barry::Error &se ) {
1053 std::cerr << "Barry::Error caught: " << se.what() << endl;
1054 return 1;
1056 catch( std::exception &e ) {
1057 std::cerr << "std::exception caught: " << e.what() << endl;
1058 return 1;
1061 return 0;