Documentation updates: git, tarball, bugs, contact
[barry/pauldeden.git] / tools / btool.cc
blob84381450c4ddd6e0f0a987b223356ebd748b394c
1 ///
2 /// \file btool.cc
3 /// Barry library tester
4 ///
6 /*
7 Copyright (C) 2005-2008, 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 <sstream>
27 #include <vector>
28 #include <string>
29 #include <algorithm>
30 #include <getopt.h>
33 using namespace std;
34 using namespace Barry;
36 void Usage()
38 int major, minor;
39 const char *Version = Barry::Version(major, minor);
41 cerr
42 << "btool - Command line USB Blackberry Test Tool\n"
43 << " Copyright 2005-2008, Net Direct Inc. (http://www.netdirect.ca/)\n"
44 << " Using: " << Version << "\n"
45 << " Compiled "
46 #ifdef __BARRY_BOOST_MODE__
47 << "with"
48 #else
49 << "without"
50 #endif
51 << " Boost support\n"
52 << "\n"
53 << " -B bus Specify which USB bus to search on\n"
54 << " -N dev Specify which system device, using system specific string\n"
55 << "\n"
56 << " -c dn Convert address book database to LDIF format, using the\n"
57 << " specified baseDN\n"
58 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
59 << " Defaults to 'cn'\n"
60 << " -d db Load database 'db' FROM device and dump to screen\n"
61 << " Can be used multiple times to fetch more than one DB\n"
62 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
63 << " string separated by a comma, holding the read,write\n"
64 << " endpoint pair. Example: -e 83,5\n"
65 << " Note: Endpoints are specified in hex.\n"
66 << " You should never need to use this option.\n"
67 #ifdef __BARRY_BOOST_MODE__
68 << " -f file Filename to save or load handheld data to/from\n"
69 #endif
70 << " -h This help\n"
71 << " -l List devices\n"
72 << " -L List Contact field names\n"
73 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
74 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
75 << " Unmap: ldif name alone\n"
76 << " -M List current LDIF mapping\n"
77 << " -p pin PIN of device to talk with\n"
78 << " If only one device is plugged in, this flag is optional\n"
79 << " -P pass Simplistic method to specify device password\n"
80 << " -s db Save database 'db' TO device from data loaded from -f file\n"
81 << " -S Show list of supported database parsers\n"
82 << " -t Show database database table\n"
83 << " -T db Show record state table for given database\n"
84 << " -v Dump protocol data during operation\n"
85 << " -X Reset device\n"
86 << " -z Use non-threaded sockets\n"
87 << " -Z Use threaded socket router (default)\n"
88 << "\n"
89 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
90 << "\n"
91 << " -r # Record index number as seen in the -T state table.\n"
92 << " This overrides the default -d behaviour, and only\n"
93 << " downloads the one specified record, sending to stdout.\n"
94 << " -R # Same as -r, but also clears the record's dirty flags.\n"
95 << " -D # Record index number as seen in the -T state table,\n"
96 << " which indicates the record to delete. Used with the -d\n"
97 << " command to specify the database.\n"
98 << endl;
101 class Contact2Ldif
103 public:
104 Barry::ContactLdif &ldif;
106 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
108 void operator()(const Contact &rec)
110 ldif.DumpLdif(cout, rec);
114 template <class Record>
115 struct Store
117 std::vector<Record> records;
118 mutable typename std::vector<Record>::const_iterator rec_it;
119 std::string filename;
120 bool load;
121 int count;
123 Store(const string &filename, bool load)
124 : rec_it(records.end()),
125 filename(filename),
126 load(load),
127 count(0)
129 #ifdef __BARRY_BOOST_MODE__
130 try {
132 if( load && filename.size() ) {
133 // filename is available, attempt to load
134 cout << "Loading: " << filename << endl;
135 ifstream ifs(filename.c_str());
136 std::string dbName;
137 getline(ifs, dbName);
138 boost::archive::text_iarchive ia(ifs);
139 ia >> records;
140 cout << records.size()
141 << " records loaded from '"
142 << filename << "'" << endl;
143 sort(records.begin(), records.end());
144 rec_it = records.begin();
146 // debugging aid
147 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
148 for( ; beg != end; beg++ ) {
149 cout << (*beg) << endl;
153 } catch( boost::archive::archive_exception &ae ) {
154 cerr << "Archive exception in ~Store(): "
155 << ae.what() << endl;
157 #endif
159 ~Store()
161 cout << "Store counted " << dec << count << " records." << endl;
162 #ifdef __BARRY_BOOST_MODE__
163 try {
165 if( !load && filename.size() ) {
166 // filename is available, attempt to save
167 cout << "Saving: " << filename << endl;
168 const std::vector<Record> &r = records;
169 ofstream ofs(filename.c_str());
170 ofs << Record::GetDBName() << endl;
171 boost::archive::text_oarchive oa(ofs);
172 oa << r;
173 cout << dec << r.size() << " records saved to '"
174 << filename << "'" << endl;
177 } catch( boost::archive::archive_exception &ae ) {
178 cerr << "Archive exception in ~Store(): "
179 << ae.what() << endl;
181 #endif
184 // storage operator
185 void operator()(const Record &rec)
187 count++;
188 std::cout << rec << std::endl;
189 records.push_back(rec);
192 // retrieval operator
193 bool operator()(Record &rec, unsigned int databaseId) const
195 if( rec_it == records.end() )
196 return false;
197 rec = *rec_it;
198 rec_it++;
199 return true;
203 class DataDumpParser : public Barry::Parser
205 uint32_t m_id;
207 public:
208 virtual void SetUniqueId(uint32_t id)
210 m_id = id;
213 virtual void ParseFields(const Barry::Data &data, size_t &offset)
215 std::cout << "Raw record dump for record: "
216 << std::hex << m_id << std::endl;
217 std::cout << data << std::endl;
221 auto_ptr<Parser> GetParser(const string &name, const string &filename)
223 // check for recognized database names
224 if( name == Contact::GetDBName() ) {
225 return auto_ptr<Parser>(
226 new RecordParser<Contact, Store<Contact> > (
227 new Store<Contact>(filename, false)));
229 else if( name == Message::GetDBName() ) {
230 return auto_ptr<Parser>(
231 new RecordParser<Message, Store<Message> > (
232 new Store<Message>(filename, false)));
234 else if( name == Calendar::GetDBName() ) {
235 return auto_ptr<Parser>(
236 new RecordParser<Calendar, Store<Calendar> > (
237 new Store<Calendar>(filename, false)));
239 else if( name == ServiceBook::GetDBName() ) {
240 return auto_ptr<Parser>(
241 new RecordParser<ServiceBook, Store<ServiceBook> > (
242 new Store<ServiceBook>(filename, false)));
245 else if( name == Memo::GetDBName() ) {
246 return auto_ptr<Parser>(
247 new RecordParser<Memo, Store<Memo> > (
248 new Store<Memo>(filename, false)));
250 else if( name == Task::GetDBName() ) {
251 return auto_ptr<Parser>(
252 new RecordParser<Task, Store<Task> > (
253 new Store<Task>(filename, false)));
255 else if( name == PINMessage::GetDBName() ) {
256 return auto_ptr<Parser>(
257 new RecordParser<PINMessage, Store<PINMessage> > (
258 new Store<PINMessage>(filename, false)));
260 else if( name == SavedMessage::GetDBName() ) {
261 return auto_ptr<Parser>(
262 new RecordParser<SavedMessage, Store<SavedMessage> > (
263 new Store<SavedMessage>(filename, false)));
265 else if( name == Folder::GetDBName() ) {
266 return auto_ptr<Parser>(
267 new RecordParser<Folder, Store<Folder> > (
268 new Store<Folder>(filename, false)));
270 else if( name == Timezone::GetDBName() ) {
271 return auto_ptr<Parser>(
272 new RecordParser<Timezone, Store<Timezone> > (
273 new Store<Timezone>(filename, false)));
275 else {
276 // unknown database, use null parser
277 return auto_ptr<Parser>( new DataDumpParser );
281 auto_ptr<Builder> GetBuilder(const string &name, const string &filename)
283 // check for recognized database names
284 if( name == Contact::GetDBName() ) {
285 return auto_ptr<Builder>(
286 new RecordBuilder<Contact, Store<Contact> > (
287 new Store<Contact>(filename, true)));
289 else if( name == Calendar::GetDBName() ) {
290 return auto_ptr<Builder>(
291 new RecordBuilder<Calendar, Store<Calendar> > (
292 new Store<Calendar>(filename, true)));
295 else if( name == "Messages" ) {
296 return auto_ptr<Parser>(
297 new RecordParser<Message, Store<Message> > (
298 new Store<Message>(filename, true)));
300 else if( name == "Service Book" ) {
301 return auto_ptr<Parser>(
302 new RecordParser<ServiceBook, Store<ServiceBook> > (
303 new Store<ServiceBook>(filename, true)));
305 else if( name == "Memos" ) {
306 return auto_ptr<Parser>(
307 new RecordParser<Memo, Store<Memo> > (
308 new Store<Memo>(filename, true)));
310 else if( name == "Tasks" ) {
311 return auto_ptr<Parser>(
312 new RecordParser<Task, Store<Task> > (
313 new Store<Task>(filename, true)));
316 else {
317 throw std::runtime_error("No Builder available for database");
321 void ShowParsers()
323 cout << "Supported Database parsers:\n"
324 << " Address Book\n"
325 << " Messages\n"
326 << " Calendar\n"
327 << " Service Book\n"
328 << " Memos\n"
329 << " Tasks\n"
330 << " PIN Messages\n"
331 << " Saved Email Messages\n"
332 << " Folders\n"
333 << " Time Zones (read only)\n"
334 << "\n"
335 << "Supported Database builders:\n"
336 << " Address Book\n"
337 << " Calendar\n"
338 << endl;
341 struct StateTableCommand
343 char flag;
344 bool clear;
345 unsigned int index;
347 StateTableCommand(char f, bool c, unsigned int i)
348 : flag(f), clear(c), index(i) {}
351 bool SplitMap(const string &map, string &ldif, string &read, string &write)
353 string::size_type a = map.find(',');
354 if( a == string::npos )
355 return false;
357 string::size_type b = map.find(',', a+1);
358 if( b == string::npos )
359 return false;
361 ldif.assign(map, 0, a);
362 read.assign(map, a + 1, b - a - 1);
363 write.assign(map, b + 1, map.size() - b - 1);
365 return ldif.size() && read.size() && write.size();
368 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
370 for( vector<string>::const_iterator i = mapCommands.begin();
371 i != mapCommands.end();
372 ++i )
374 // single names mean unmapping
375 if( i->find(',') == string::npos ) {
376 // unmap
377 cerr << "Unmapping: " << *i << endl;
378 ldif.Unmap(*i);
380 else {
381 cerr << "Mapping: " << *i << endl;
383 // map... extract ldif/read/write names
384 string ldifname, read, write;
385 if( SplitMap(*i, ldifname, read, write) ) {
386 if( !ldif.Map(ldifname, read, write) ) {
387 cerr << "Read/Write name unknown: " << *i << endl;
390 else {
391 cerr << "Invalid map format: " << *i << endl;
397 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
399 int read, write;
400 char comma;
401 istringstream iss(arg);
402 iss >> hex >> read >> comma >> write;
403 if( !iss )
404 return false;
405 epp->read = read;
406 epp->write = write;
407 return true;
410 int main(int argc, char *argv[])
412 cout.sync_with_stdio(true); // leave this on, since libusb uses
413 // stdio for debug messages
415 try {
417 uint32_t pin = 0;
418 bool list_only = false,
419 show_dbdb = false,
420 ldif_contacts = false,
421 data_dump = false,
422 reset_device = false,
423 list_contact_fields = false,
424 list_ldif_map = false,
425 epp_override = false,
426 threaded_sockets = true,
427 record_state = false;
428 string ldifBaseDN, ldifDnAttr;
429 string filename;
430 string password;
431 string busname;
432 string devname;
433 vector<string> dbNames, saveDbNames, mapCommands;
434 vector<StateTableCommand> stCommands;
435 Usb::EndpointPair epOverride;
437 // process command line options
438 for(;;) {
439 int cmd = getopt(argc, argv, "B:c:C:d:D:e:f:hlLm:MN:p:P:r:R:Ss:tT:vXzZ");
440 if( cmd == -1 )
441 break;
443 switch( cmd )
445 case 'B': // busname
446 busname = optarg;
447 break;
449 case 'c': // contacts to ldap ldif
450 ldif_contacts = true;
451 ldifBaseDN = optarg;
452 break;
454 case 'C': // DN Attribute for FQDN
455 ldifDnAttr = optarg;
456 break;
458 case 'd': // show dbname
459 dbNames.push_back(string(optarg));
460 break;
462 case 'D': // delete record
463 stCommands.push_back(
464 StateTableCommand('D', false, atoi(optarg)));
465 break;
467 case 'e': // endpoint override
468 if( !ParseEpOverride(optarg, &epOverride) ) {
469 Usage();
470 return 1;
472 epp_override = true;
473 break;
475 case 'f': // filename
476 #ifdef __BARRY_BOOST_MODE__
477 filename = optarg;
478 #else
479 cerr << "-f option not supported - no Boost "
480 "serialization support available\n";
481 return 1;
482 #endif
483 break;
484 case 'l': // list only
485 list_only = true;
486 break;
488 case 'L': // List Contact field names
489 list_contact_fields = true;
490 break;
492 case 'm': // Map / Unmap
493 mapCommands.push_back(string(optarg));
494 break;
496 case 'M': // List LDIF map
497 list_ldif_map = true;
498 break;
500 case 'N': // Devname
501 devname = optarg;
502 break;
504 case 'p': // Blackberry PIN
505 pin = strtoul(optarg, NULL, 16);
506 break;
508 case 'P': // Device password
509 password = optarg;
510 break;
512 case 'r': // get specific record index
513 stCommands.push_back(
514 StateTableCommand('r', false, atoi(optarg)));
515 break;
517 case 'R': // same as 'r', and clears dirty
518 stCommands.push_back(
519 StateTableCommand('r', true, atoi(optarg)));
520 break;
522 case 's': // save dbname
523 saveDbNames.push_back(string(optarg));
524 break;
526 case 'S': // show supported databases
527 ShowParsers();
528 return 0;
530 case 't': // display database database
531 show_dbdb = true;
532 break;
534 case 'T': // show RecordStateTable
535 record_state = true;
536 dbNames.push_back(string(optarg));
537 break;
539 case 'v': // data dump on
540 data_dump = true;
541 break;
543 case 'X': // reset device
544 reset_device = true;
545 break;
547 case 'z': // non-threaded sockets
548 threaded_sockets = false;
549 break;
551 case 'Z': // threaded socket router
552 threaded_sockets = true;
553 break;
555 case 'h': // help
556 default:
557 Usage();
558 return 0;
562 // Initialize the barry library. Must be called before
563 // anything else.
564 Barry::Init(data_dump);
566 // LDIF class... only needed if ldif output turned on
567 ContactLdif ldif(ldifBaseDN);
568 DoMapping(ldif, mapCommands);
569 if( ldifDnAttr.size() ) {
570 if( !ldif.SetDNAttr(ldifDnAttr) ) {
571 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
575 // Probe the USB bus for Blackberry devices and display.
576 // If user has specified a PIN, search for it in the
577 // available device list here as well
578 Barry::Probe probe(busname.c_str(), devname.c_str());
579 int activeDevice = -1;
581 // show any errors during probe first
582 if( probe.GetFailCount() ) {
583 if( ldif_contacts )
584 cout << "# ";
585 cout << "Blackberry device errors with errors during probe:" << endl;
586 for( int i = 0; i < probe.GetFailCount(); i++ ) {
587 if( ldif_contacts )
588 cout << "# ";
589 cout << probe.GetFailMsg(i) << endl;
593 // show all successfully found devices
594 if( ldif_contacts )
595 cout << "# ";
596 cout << "Blackberry devices found:" << endl;
597 for( int i = 0; i < probe.GetCount(); i++ ) {
598 if( ldif_contacts )
599 cout << "# ";
600 if( data_dump )
601 probe.Get(i).DumpAll(cout);
602 else
603 cout << probe.Get(i);
604 cout << endl;
605 if( probe.Get(i).m_pin == pin )
606 activeDevice = i;
609 if( list_only )
610 return 0; // done
612 if( activeDevice == -1 ) {
613 if( pin == 0 ) {
614 // can we default to single device?
615 if( probe.GetCount() == 1 )
616 activeDevice = 0;
617 else {
618 cerr << "No device selected" << endl;
619 return 1;
622 else {
623 cerr << "PIN " << setbase(16) << pin
624 << " not found" << endl;
625 return 1;
629 if( ldif_contacts )
630 cout << "# ";
631 cout << "Using device (PIN): " << setbase(16)
632 << probe.Get(activeDevice).m_pin << endl;
634 if( reset_device ) {
635 Usb::Device dev(probe.Get(activeDevice).m_dev);
636 dev.Reset();
637 return 0;
640 // Override device endpoints if user asks
641 Barry::ProbeResult device = probe.Get(activeDevice);
642 if( epp_override ) {
643 device.m_ep.read = epOverride.read;
644 device.m_ep.write = epOverride.write;
645 device.m_ep.type = 2; // FIXME - override this too?
646 cout << "Endpoint pair (read,write) overridden with: "
647 << hex
648 << (unsigned int) device.m_ep.read << ","
649 << (unsigned int) device.m_ep.write << endl;
653 // Create our controller object
655 // Order is important in the following auto_ptr<> objects,
656 // since Controller must get destroyed before router.
657 // Normally you'd pick one method, and not bother
658 // with auto_ptr<> and so the normal C++ constructor
659 // rules would guarantee this safety for you, but
660 // here we want the user to pick.
662 auto_ptr<SocketRoutingQueue> router;
663 auto_ptr<Barry::Controller> pcon;
664 if( threaded_sockets ) {
665 router.reset( new SocketRoutingQueue );
666 router->SpinoffSimpleReadThread();
667 pcon.reset( new Barry::Controller(device, *router) );
669 else {
670 pcon.reset( new Barry::Controller(device) );
673 Barry::Controller &con = *pcon;
674 Barry::Mode::Desktop desktop(con);
677 // execute each mode that was turned on
681 // Dump list of all databases to stdout
682 if( show_dbdb ) {
683 // open desktop mode socket
684 desktop.Open(password.c_str());
685 cout << desktop.GetDBDB() << endl;
688 // Dump list of Contact field names
689 if( list_contact_fields ) {
690 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
691 cout.fill(' ');
692 cout << " " << left << setw(20) << n->name << ": "
693 << n->description << endl;
697 // Dump current LDIF mapping
698 if( list_ldif_map ) {
699 cout << ldif << endl;
702 // Dump list of contacts to an LDAP LDIF file
703 // This uses the Controller convenience templates
704 if( ldif_contacts ) {
705 // make sure we're in desktop mode
706 desktop.Open(password.c_str());
708 // create a storage functor object that accepts
709 // Barry::Contact objects as input
710 Contact2Ldif storage(ldif);
712 // load all the Contact records into storage
713 desktop.LoadDatabaseByType<Barry::Contact>(storage);
716 // Dump record state table to stdout
717 if( record_state ) {
718 if( dbNames.size() == 0 ) {
719 cout << "No db names to process" << endl;
720 return 1;
723 desktop.Open(password.c_str());
725 vector<string>::iterator b = dbNames.begin();
726 for( ; b != dbNames.end(); b++ ) {
727 unsigned int id = desktop.GetDBID(*b);
728 RecordStateTable state;
729 desktop.GetRecordStateTable(id, state);
730 cout << "Record state table for: " << *b << endl;
731 cout << state;
733 return 0;
736 // Get Record mode overrides the default name mode
737 if( stCommands.size() ) {
738 if( dbNames.size() != 1 ) {
739 cout << "Must have 1 db name to process" << endl;
740 return 1;
743 desktop.Open(password.c_str());
744 unsigned int id = desktop.GetDBID(dbNames[0]);
745 auto_ptr<Parser> parse = GetParser(dbNames[0],filename);
747 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
748 desktop.GetRecord(id, stCommands[i].index, *parse.get());
750 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
751 cout << "Clearing record's dirty flags..." << endl;
752 desktop.ClearDirty(id, stCommands[i].index);
755 if( stCommands[i].flag == 'D' ) {
756 desktop.DeleteRecord(id, stCommands[i].index);
760 return 0;
763 // Dump contents of selected databases to stdout, or
764 // to file if specified.
765 // This is retrieving data from the Blackberry.
766 if( dbNames.size() ) {
767 vector<string>::iterator b = dbNames.begin();
769 desktop.Open(password.c_str());
770 for( ; b != dbNames.end(); b++ ) {
771 auto_ptr<Parser> parse = GetParser(*b,filename);
772 unsigned int id = desktop.GetDBID(*b);
773 desktop.LoadDatabase(id, *parse.get());
777 // Save contents of file to specified databases
778 // This is writing data to the Blackberry.
779 if( saveDbNames.size() ) {
780 vector<string>::iterator b = saveDbNames.begin();
782 desktop.Open(password.c_str());
783 for( ; b != saveDbNames.end(); b++ ) {
784 auto_ptr<Builder> build =
785 GetBuilder(*b, filename);
786 unsigned int id = desktop.GetDBID(*b);
787 desktop.SaveDatabase(id, *build);
792 catch( Usb::Error &ue) {
793 std::cerr << "Usb::Error caught: " << ue.what() << endl;
794 return 1;
796 catch( Barry::Error &se ) {
797 std::cerr << "Barry::Error caught: " << se.what() << endl;
798 return 1;
800 catch( std::exception &e ) {
801 std::cerr << "std::exception caught: " << e.what() << endl;
802 return 1;
805 return 0;