Add back the clarified logging from commit c7685942140b123bf11
[barry.git] / tools / btool.cc
blobf84686b86b839d9fcd596c9a904319dc955eee0b
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 class DataDumpParser : public Barry::Parser
328 uint32_t m_id;
330 public:
331 virtual void Clear() {}
333 virtual void SetIds(const std::string &DbName,
334 uint8_t RecType, uint32_t UniqueId)
336 m_id = UniqueId;
339 virtual void ParseHeader(const Data &, size_t &) {}
341 virtual void ParseFields(const Barry::Data &data, size_t &offset,
342 const IConverter *ic)
344 std::cout << "Raw record dump for record: "
345 << std::hex << m_id << std::endl;
346 std::cout << data << std::endl;
349 virtual void Store() {}
352 shared_ptr<Parser> GetParser(const string &name,
353 const string &filename,
354 bool null_parser,
355 bool immediate_display,
356 bool vformat_mode,
357 bool bbackup_mode)
359 bool dnow = immediate_display;
360 bool vmode = vformat_mode;
362 if( null_parser ) {
363 // use null parser
364 return shared_ptr<Parser>( new DataDumpParser );
366 else if( bbackup_mode ) {
367 #ifdef __BARRY_BACKUP_MODE__
368 // Only one backup file per run
369 static shared_ptr<Parser> backup;
370 if( !backup.get() ) {
371 backup.reset( new Backup(filename) );
373 return backup;
374 #else
375 return shared_ptr<Parser>( new DataDumpParser );
376 #endif
378 // check for recognized database names
379 else if( name == Contact::GetDBName() ) {
380 return shared_ptr<Parser>(
381 new RecordParser<Contact, Store<Contact> > (
382 new Store<Contact>(filename, false, dnow, vmode)));
384 else if( name == Message::GetDBName() ) {
385 return shared_ptr<Parser>(
386 new RecordParser<Message, Store<Message> > (
387 new Store<Message>(filename, false, dnow, vmode)));
389 else if( name == Calendar::GetDBName() ) {
390 return shared_ptr<Parser>(
391 new RecordParser<Calendar, Store<Calendar> > (
392 new Store<Calendar>(filename, false, dnow, vmode)));
394 else if( name == CalendarAll::GetDBName() ) {
395 return shared_ptr<Parser>(
396 new RecordParser<CalendarAll, Store<CalendarAll> > (
397 new Store<CalendarAll>(filename, false, dnow, vmode)));
399 else if( name == CallLog::GetDBName() ) {
400 return shared_ptr<Parser>(
401 new RecordParser<CallLog, Store<CallLog> > (
402 new Store<CallLog>(filename, false, dnow, vmode)));
404 else if( name == ServiceBook::GetDBName() ) {
405 return shared_ptr<Parser>(
406 new RecordParser<ServiceBook, Store<ServiceBook> > (
407 new Store<ServiceBook>(filename, false, dnow, vmode)));
410 else if( name == Memo::GetDBName() ) {
411 return shared_ptr<Parser>(
412 new RecordParser<Memo, Store<Memo> > (
413 new Store<Memo>(filename, false, dnow, vmode)));
415 else if( name == Task::GetDBName() ) {
416 return shared_ptr<Parser>(
417 new RecordParser<Task, Store<Task> > (
418 new Store<Task>(filename, false, dnow, vmode)));
420 else if( name == PINMessage::GetDBName() ) {
421 return shared_ptr<Parser>(
422 new RecordParser<PINMessage, Store<PINMessage> > (
423 new Store<PINMessage>(filename, false, dnow, vmode)));
425 else if( name == SavedMessage::GetDBName() ) {
426 return shared_ptr<Parser>(
427 new RecordParser<SavedMessage, Store<SavedMessage> > (
428 new Store<SavedMessage>(filename, false, dnow, vmode)));
430 else if( name == Sms::GetDBName() ) {
431 return shared_ptr<Parser>(
432 new RecordParser<Sms, Store<Sms> > (
433 new Store<Sms>(filename, false, dnow, vmode)));
435 else if( name == Folder::GetDBName() ) {
436 return shared_ptr<Parser>(
437 new RecordParser<Folder, Store<Folder> > (
438 new Store<Folder>(filename, false, dnow, vmode)));
440 else if( name == Timezone::GetDBName() ) {
441 return shared_ptr<Parser>(
442 new RecordParser<Timezone, Store<Timezone> > (
443 new Store<Timezone>(filename, false, dnow, vmode)));
445 else {
446 // unknown database, use null parser
447 return shared_ptr<Parser>( new DataDumpParser );
451 shared_ptr<Builder> GetBuilder(const string &name, const string &filename)
453 // check for recognized database names
454 if( name == Contact::GetDBName() ) {
455 return shared_ptr<Builder>(
456 new RecordBuilder<Contact, Store<Contact> > (
457 new Store<Contact>(filename, true, true, false)));
459 else if( name == Calendar::GetDBName() ) {
460 return shared_ptr<Builder>(
461 new RecordBuilder<Calendar, Store<Calendar> > (
462 new Store<Calendar>(filename, true, true, false)));
464 else if( name == CalendarAll::GetDBName() ) {
465 return shared_ptr<Builder>(
466 new RecordBuilder<CalendarAll, Store<CalendarAll> > (
467 new Store<CalendarAll>(filename, true, true, false)));
469 else if( name == Memo::GetDBName() ) {
470 return shared_ptr<Builder>(
471 new RecordBuilder<Memo, Store<Memo> > (
472 new Store<Memo>(filename, true, true, false)));
474 else if( name == Task::GetDBName() ) {
475 return shared_ptr<Builder>(
476 new RecordBuilder<Task, Store<Task> > (
477 new Store<Task>(filename, true, true, false)));
480 else if( name == "Messages" ) {
481 return shared_ptr<Parser>(
482 new RecordParser<Message, Store<Message> > (
483 new Store<Message>(filename, true, true, false)));
485 else if( name == "Service Book" ) {
486 return shared_ptr<Parser>(
487 new RecordParser<ServiceBook, Store<ServiceBook> > (
488 new Store<ServiceBook>(filename, true, true, false)));
491 else {
492 throw std::runtime_error("No Builder available for database");
496 void ShowParsers()
498 cout << "Supported Database parsers:\n"
499 << " (* = can display in vformat MIME mode)\n"
500 << " Address Book *\n"
501 << " Messages\n"
502 << " Calendar *\n"
503 << " Calendar - All *\n"
504 << " Phone Call Logs\n"
505 << " Service Book\n"
506 << " Memos *\n"
507 << " Tasks *\n"
508 << " PIN Messages\n"
509 << " Saved Email Messages\n"
510 << " SMS Messages\n"
511 << " Folders\n"
512 << " Time Zones (read only)\n"
513 << "\n"
514 << "Supported Database builders:\n"
515 << " Address Book\n"
516 << " Calendar\n"
517 << " Calendar - All\n"
518 << " Memo\n"
519 << " Task\n"
520 << endl;
523 struct StateTableCommand
525 char flag;
526 bool clear;
527 unsigned int index;
529 StateTableCommand(char f, bool c, unsigned int i)
530 : flag(f), clear(c), index(i) {}
533 bool SplitMap(const string &map, string &ldif, string &read, string &write)
535 string::size_type a = map.find(',');
536 if( a == string::npos )
537 return false;
539 string::size_type b = map.find(',', a+1);
540 if( b == string::npos )
541 return false;
543 ldif.assign(map, 0, a);
544 read.assign(map, a + 1, b - a - 1);
545 write.assign(map, b + 1, map.size() - b - 1);
547 return ldif.size() && read.size() && write.size();
550 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
552 for( vector<string>::const_iterator i = mapCommands.begin();
553 i != mapCommands.end();
554 ++i )
556 // single names mean unmapping
557 if( i->find(',') == string::npos ) {
558 // unmap
559 cerr << "Unmapping: " << *i << endl;
560 ldif.Unmap(*i);
562 else {
563 cerr << "Mapping: " << *i << endl;
565 // map... extract ldif/read/write names
566 string ldifname, read, write;
567 if( SplitMap(*i, ldifname, read, write) ) {
568 if( !ldif.Map(ldifname, read, write) ) {
569 cerr << "Read/Write name unknown: " << *i << endl;
572 else {
573 cerr << "Invalid map format: " << *i << endl;
579 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
581 int read, write;
582 char comma;
583 istringstream iss(arg);
584 iss >> hex >> read >> comma >> write;
585 if( !iss )
586 return false;
587 epp->read = read;
588 epp->write = write;
589 return true;
592 int main(int argc, char *argv[])
594 INIT_I18N(PACKAGE);
596 cout.sync_with_stdio(true); // leave this on, since libusb uses
597 // stdio for debug messages
599 try {
601 uint32_t pin = 0;
602 bool list_only = false,
603 show_dbdb = false,
604 ldif_contacts = false,
605 data_dump = false,
606 vformat_mode = false,
607 reset_device = false,
608 list_contact_fields = false,
609 list_ldif_map = false,
610 epp_override = false,
611 threaded_sockets = true,
612 record_state = false,
613 clear_database = false,
614 null_parser = false,
615 bbackup_mode = false,
616 sort_records = false;
617 string ldifBaseDN, ldifDnAttr;
618 string filename;
619 string password;
620 string busname;
621 string devname;
622 string iconvCharset;
623 vector<string> dbNames, saveDbNames, mapCommands, clearDbNames;
624 vector<StateTableCommand> stCommands;
625 Usb::EndpointPair epOverride;
627 // process command line options
628 for(;;) {
629 int cmd = getopt(argc, argv, "a:b:B:c:C:d:D:e:f:hi:IlLm:MnN:p:P:r:R:Ss:tT:vVXzZ");
630 if( cmd == -1 )
631 break;
633 switch( cmd )
635 case 'a': // Clear Database
636 clear_database = true;
637 clearDbNames.push_back(string(optarg));
638 break;
640 case 'b': // Barry backup filename (tar.gz)
641 #ifdef __BARRY_BACKUP_MODE__
642 if( filename.size() == 0 ) {
643 filename = optarg;
644 bbackup_mode = true;
646 else {
647 cerr << "Do not use -f with -b\n";
648 return 1;
650 #else
651 cerr << "-b option not supported - no Barry "
652 "Backup library support available\n";
653 return 1;
654 #endif
655 break;
657 case 'B': // busname
658 busname = optarg;
659 break;
661 case 'c': // contacts to ldap ldif
662 ldif_contacts = true;
663 ldifBaseDN = optarg;
664 break;
666 case 'C': // DN Attribute for FQDN
667 ldifDnAttr = optarg;
668 break;
670 case 'd': // show dbname
671 dbNames.push_back(string(optarg));
672 break;
674 case 'D': // delete record
675 stCommands.push_back(
676 StateTableCommand('D', false, atoi(optarg)));
677 break;
679 case 'e': // endpoint override
680 if( !ParseEpOverride(optarg, &epOverride) ) {
681 Usage();
682 return 1;
684 epp_override = true;
685 break;
687 case 'f': // filename
688 #ifdef __BARRY_BOOST_MODE__
689 if( !bbackup_mode && filename.size() == 0 ) {
690 filename = optarg;
692 else {
693 cerr << "Do not use -f with -b\n";
694 return 1;
696 #else
697 cerr << "-f option not supported - no Boost "
698 "serialization support available\n";
699 return 1;
700 #endif
701 break;
703 case 'i': // international charset (iconv)
704 iconvCharset = optarg;
705 break;
707 case 'I': // sort before dump
708 sort_records = true;
709 break;
711 case 'l': // list only
712 list_only = true;
713 break;
715 case 'L': // List Contact field names
716 list_contact_fields = true;
717 break;
719 case 'm': // Map / Unmap
720 mapCommands.push_back(string(optarg));
721 break;
723 case 'M': // List LDIF map
724 list_ldif_map = true;
725 break;
727 case 'n': // use null parser
728 null_parser = true;
729 break;
731 case 'N': // Devname
732 devname = optarg;
733 break;
735 case 'p': // Blackberry PIN
736 pin = strtoul(optarg, NULL, 16);
737 break;
739 case 'P': // Device password
740 password = optarg;
741 break;
743 case 'r': // get specific record index
744 stCommands.push_back(
745 StateTableCommand('r', false, atoi(optarg)));
746 break;
748 case 'R': // same as 'r', and clears dirty
749 stCommands.push_back(
750 StateTableCommand('r', true, atoi(optarg)));
751 break;
753 case 's': // save dbname
754 saveDbNames.push_back(string(optarg));
755 break;
757 case 'S': // show supported databases
758 ShowParsers();
759 return 0;
761 case 't': // display database database
762 show_dbdb = true;
763 break;
765 case 'T': // show RecordStateTable
766 record_state = true;
767 dbNames.push_back(string(optarg));
768 break;
770 case 'v': // data dump on
771 data_dump = true;
772 break;
774 case 'V': // vformat MIME mode
775 #ifdef __BARRY_SYNC_MODE__
776 vformat_mode = true;
777 #else
778 cerr << "-V option not supported - no Sync "
779 "library support available\n";
780 return 1;
781 #endif
782 break;
784 case 'X': // reset device
785 reset_device = true;
786 break;
788 case 'z': // non-threaded sockets
789 threaded_sockets = false;
790 break;
792 case 'Z': // threaded socket router
793 threaded_sockets = true;
794 break;
796 case 'h': // help
797 default:
798 Usage();
799 return 0;
803 // Initialize the barry library. Must be called before
804 // anything else.
805 Barry::Init(data_dump);
806 if( data_dump ) {
807 int major, minor;
808 const char *Version = Barry::Version(major, minor);
809 cout << Version << endl;
812 // Create an IConverter object if needed
813 auto_ptr<IConverter> ic;
814 if( iconvCharset.size() ) {
815 ic.reset( new IConverter(iconvCharset.c_str(), true) );
818 // LDIF class... only needed if ldif output turned on
819 ContactLdif ldif(ldifBaseDN);
820 DoMapping(ldif, mapCommands);
821 if( ldifDnAttr.size() ) {
822 if( !ldif.SetDNAttr(ldifDnAttr) ) {
823 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
827 // Probe the USB bus for Blackberry devices and display.
828 // If user has specified a PIN, search for it in the
829 // available device list here as well
830 Barry::Probe probe(busname.c_str(), devname.c_str(),
831 epp_override ? &epOverride : 0);
832 int activeDevice = -1;
834 // show any errors during probe first
835 if( probe.GetFailCount() ) {
836 if( ldif_contacts )
837 cout << "# ";
838 cout << "Blackberry device errors with errors during probe:" << endl;
839 for( int i = 0; i < probe.GetFailCount(); i++ ) {
840 if( ldif_contacts )
841 cout << "# ";
842 cout << probe.GetFailMsg(i) << endl;
846 // show all successfully found devices
847 if( ldif_contacts )
848 cout << "# ";
849 cout << "Blackberry devices found:" << endl;
850 for( int i = 0; i < probe.GetCount(); i++ ) {
851 if( ldif_contacts )
852 cout << "# ";
853 if( data_dump )
854 probe.Get(i).DumpAll(cout);
855 else
856 cout << probe.Get(i);
857 cout << endl;
858 if( probe.Get(i).m_pin == pin )
859 activeDevice = i;
862 if( list_only )
863 return 0; // done
865 if( activeDevice == -1 ) {
866 if( pin == 0 ) {
867 // can we default to single device?
868 if( probe.GetCount() == 1 )
869 activeDevice = 0;
870 else {
871 cerr << "No device selected" << endl;
872 return 1;
875 else {
876 cerr << "PIN " << setbase(16) << pin
877 << " not found" << endl;
878 return 1;
882 if( ldif_contacts )
883 cout << "# ";
884 cout << "Using device (PIN): "
885 << probe.Get(activeDevice).m_pin.str() << endl;
887 if( reset_device ) {
888 Usb::Device dev(probe.Get(activeDevice).m_dev);
889 dev.Reset();
890 return 0;
893 // Override device endpoints if user asks
894 Barry::ProbeResult device = probe.Get(activeDevice);
895 if( epp_override ) {
896 device.m_ep.read = epOverride.read;
897 device.m_ep.write = epOverride.write;
898 device.m_ep.type = 2; // FIXME - override this too?
899 cout << "Endpoint pair (read,write) overridden with: "
900 << hex
901 << (unsigned int) device.m_ep.read << ","
902 << (unsigned int) device.m_ep.write << endl;
906 // Create our controller object
908 // Order is important in the following auto_ptr<> objects,
909 // since Controller must get destroyed before router.
910 // Normally you'd pick one method, and not bother
911 // with auto_ptr<> and so the normal C++ constructor
912 // rules would guarantee this safety for you, but
913 // here we want the user to pick.
915 auto_ptr<SocketRoutingQueue> router;
916 auto_ptr<Barry::Controller> pcon;
917 if( threaded_sockets ) {
918 router.reset( new SocketRoutingQueue );
919 router->SpinoffSimpleReadThread();
920 pcon.reset( new Barry::Controller(device, *router) );
922 else {
923 pcon.reset( new Barry::Controller(device) );
926 Barry::Controller &con = *pcon;
927 Barry::Mode::Desktop desktop(con, *ic);
930 // execute each mode that was turned on
934 // Dump list of all databases to stdout
935 if( show_dbdb ) {
936 // open desktop mode socket
937 desktop.Open(password.c_str());
938 cout << desktop.GetDBDB() << endl;
941 // Dump list of Contact field names
942 if( list_contact_fields ) {
943 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
944 cout.fill(' ');
945 cout << " " << left << setw(20) << n->name << ": "
946 << n->description << endl;
950 // Dump current LDIF mapping
951 if( list_ldif_map ) {
952 cout << ldif << endl;
955 // Dump list of contacts to an LDAP LDIF file
956 // This uses the Controller convenience templates
957 if( ldif_contacts ) {
958 // make sure we're in desktop mode
959 desktop.Open(password.c_str());
961 // create a storage functor object that accepts
962 // Barry::Contact objects as input
963 Contact2Ldif storage(ldif);
965 // load all the Contact records into storage
966 desktop.LoadDatabaseByType<Barry::Contact>(storage);
969 // Dump record state table to stdout
970 if( record_state ) {
971 if( dbNames.size() == 0 ) {
972 cout << "No db names to process" << endl;
973 return 1;
976 desktop.Open(password.c_str());
978 vector<string>::iterator b = dbNames.begin();
979 for( ; b != dbNames.end(); b++ ) {
980 unsigned int id = desktop.GetDBID(*b);
981 RecordStateTable state;
982 desktop.GetRecordStateTable(id, state);
983 cout << "Record state table for: " << *b << endl;
984 cout << state;
986 return 0;
989 // Get Record mode overrides the default name mode
990 if( stCommands.size() ) {
991 if( dbNames.size() != 1 ) {
992 cout << "Must have 1 db name to process" << endl;
993 return 1;
996 desktop.Open(password.c_str());
997 unsigned int id = desktop.GetDBID(dbNames[0]);
998 shared_ptr<Parser> parse = GetParser(dbNames[0],filename,
999 null_parser, true, vformat_mode, bbackup_mode);
1001 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
1002 desktop.GetRecord(id, stCommands[i].index, *parse.get());
1004 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
1005 cout << "Clearing record's dirty flags..." << endl;
1006 desktop.ClearDirty(id, stCommands[i].index);
1009 if( stCommands[i].flag == 'D' ) {
1010 desktop.DeleteRecord(id, stCommands[i].index);
1014 return 0;
1017 // Clear databases
1018 if (clear_database) {
1019 if( clearDbNames.size() == 0 ) {
1020 cout << "No db names to erase" << endl;
1021 return 1;
1024 vector<string>::iterator b = clearDbNames.begin();
1026 desktop.Open(password.c_str());
1028 for( ; b != clearDbNames.end(); b++ ) {
1029 unsigned int id = desktop.GetDBID(*b);
1030 cout << "Deleting all records from " << (*b) << "..." << endl;
1031 desktop.ClearDatabase(id);
1034 return 0;
1037 // Dump contents of selected databases to stdout, or
1038 // to file if specified.
1039 // This is retrieving data from the Blackberry.
1040 if( dbNames.size() ) {
1041 vector<string>::iterator b = dbNames.begin();
1043 desktop.Open(password.c_str());
1044 for( ; b != dbNames.end(); b++ ) {
1045 shared_ptr<Parser> parse = GetParser(*b,filename,
1046 null_parser, !sort_records,
1047 vformat_mode, bbackup_mode);
1048 unsigned int id = desktop.GetDBID(*b);
1049 desktop.LoadDatabase(id, *parse.get());
1053 // Save contents of file to specified databases
1054 // This is writing data to the Blackberry.
1055 if( saveDbNames.size() ) {
1056 vector<string>::iterator b = saveDbNames.begin();
1058 desktop.Open(password.c_str());
1059 for( ; b != saveDbNames.end(); b++ ) {
1060 shared_ptr<Builder> build =
1061 GetBuilder(*b, filename);
1062 unsigned int id = desktop.GetDBID(*b);
1063 desktop.SaveDatabase(id, *build);
1068 catch( Usb::Error &ue) {
1069 std::cerr << "Usb::Error caught: " << ue.what() << endl;
1070 return 1;
1072 catch( Barry::Error &se ) {
1073 std::cerr << "Barry::Error caught: " << se.what() << endl;
1074 return 1;
1076 catch( std::exception &e ) {
1077 std::cerr << "std::exception caught: " << e.what() << endl;
1078 return 1;
1081 return 0;