TODO update
[barry.git] / tools / btool.cc
blobde9e9ba9b456b423b74f917302a4e7511a6715e4
1 ///
2 /// \file btool.cc
3 /// Barry library tester
4 ///
6 /*
7 Copyright (C) 2005-2009, 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-2009, 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 << " -i cs International charset for string conversions\n"
72 << " Valid values here are available with 'iconv --list'\n"
73 << " -l List devices\n"
74 << " -L List Contact field names\n"
75 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
76 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
77 << " Unmap: ldif name alone\n"
78 << " -M List current LDIF mapping\n"
79 << " -n Use null parser on all databases.\n"
80 << " -p pin PIN of device to talk with\n"
81 << " If only one device is plugged in, this flag is optional\n"
82 << " -P pass Simplistic method to specify device password\n"
83 << " -s db Save database 'db' TO device from data loaded from -f file\n"
84 << " -S Show list of supported database parsers\n"
85 << " -t Show database database table\n"
86 << " -T db Show record state table for given database\n"
87 << " -v Dump protocol data during operation\n"
88 << " -X Reset device\n"
89 << " -z Use non-threaded sockets\n"
90 << " -Z Use threaded socket router (default)\n"
91 << "\n"
92 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
93 << "\n"
94 << " -r # Record index number as seen in the -T state table.\n"
95 << " This overrides the default -d behaviour, and only\n"
96 << " downloads the one specified record, sending to stdout.\n"
97 << " -R # Same as -r, but also clears the record's dirty flags.\n"
98 << " -D # Record index number as seen in the -T state table,\n"
99 << " which indicates the record to delete. Used with the -d\n"
100 << " command to specify the database.\n"
101 << endl;
104 class Contact2Ldif
106 public:
107 Barry::ContactLdif &ldif;
109 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
111 void operator()(const Contact &rec)
113 ldif.DumpLdif(cout, rec);
117 template <class Record>
118 struct Store
120 std::vector<Record> records;
121 mutable typename std::vector<Record>::const_iterator rec_it;
122 std::string filename;
123 bool load;
124 int count;
126 Store(const string &filename, bool load)
127 : rec_it(records.end()),
128 filename(filename),
129 load(load),
130 count(0)
132 #ifdef __BARRY_BOOST_MODE__
133 try {
135 if( load && filename.size() ) {
136 // filename is available, attempt to load
137 cout << "Loading: " << filename << endl;
138 ifstream ifs(filename.c_str());
139 std::string dbName;
140 getline(ifs, dbName);
141 boost::archive::text_iarchive ia(ifs);
142 ia >> records;
143 cout << records.size()
144 << " records loaded from '"
145 << filename << "'" << endl;
146 sort(records.begin(), records.end());
147 rec_it = records.begin();
149 // debugging aid
150 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
151 for( ; beg != end; beg++ ) {
152 cout << (*beg) << endl;
156 } catch( boost::archive::archive_exception &ae ) {
157 cerr << "Archive exception in ~Store(): "
158 << ae.what() << endl;
160 #endif
162 ~Store()
164 cout << "Store counted " << dec << count << " records." << endl;
165 #ifdef __BARRY_BOOST_MODE__
166 try {
168 if( !load && filename.size() ) {
169 // filename is available, attempt to save
170 cout << "Saving: " << filename << endl;
171 const std::vector<Record> &r = records;
172 ofstream ofs(filename.c_str());
173 ofs << Record::GetDBName() << endl;
174 boost::archive::text_oarchive oa(ofs);
175 oa << r;
176 cout << dec << r.size() << " records saved to '"
177 << filename << "'" << endl;
180 } catch( boost::archive::archive_exception &ae ) {
181 cerr << "Archive exception in ~Store(): "
182 << ae.what() << endl;
184 #endif
187 // storage operator
188 void operator()(const Record &rec)
190 count++;
191 std::cout << rec << std::endl;
192 records.push_back(rec);
195 // retrieval operator
196 bool operator()(Record &rec, unsigned int databaseId) const
198 if( rec_it == records.end() )
199 return false;
200 rec = *rec_it;
201 rec_it++;
202 return true;
206 class DataDumpParser : public Barry::Parser
208 uint32_t m_id;
210 public:
211 virtual void Clear() {}
213 virtual void SetIds(uint8_t RecType, uint32_t UniqueId)
215 m_id = UniqueId;
218 virtual void ParseHeader(const Data &, size_t &) {}
220 virtual void ParseFields(const Barry::Data &data, size_t &offset,
221 const IConverter *ic)
223 std::cout << "Raw record dump for record: "
224 << std::hex << m_id << std::endl;
225 std::cout << data << std::endl;
228 virtual void Store() {}
231 auto_ptr<Parser> GetParser(const string &name, const string &filename, bool null_parser)
233 if( null_parser ) {
234 // use null parser
235 return auto_ptr<Parser>( new DataDumpParser );
237 // check for recognized database names
238 else if( name == Contact::GetDBName() ) {
239 return auto_ptr<Parser>(
240 new RecordParser<Contact, Store<Contact> > (
241 new Store<Contact>(filename, false)));
243 else if( name == Message::GetDBName() ) {
244 return auto_ptr<Parser>(
245 new RecordParser<Message, Store<Message> > (
246 new Store<Message>(filename, false)));
248 else if( name == Calendar::GetDBName() ) {
249 return auto_ptr<Parser>(
250 new RecordParser<Calendar, Store<Calendar> > (
251 new Store<Calendar>(filename, false)));
253 else if( name == ServiceBook::GetDBName() ) {
254 return auto_ptr<Parser>(
255 new RecordParser<ServiceBook, Store<ServiceBook> > (
256 new Store<ServiceBook>(filename, false)));
259 else if( name == Memo::GetDBName() ) {
260 return auto_ptr<Parser>(
261 new RecordParser<Memo, Store<Memo> > (
262 new Store<Memo>(filename, false)));
264 else if( name == Task::GetDBName() ) {
265 return auto_ptr<Parser>(
266 new RecordParser<Task, Store<Task> > (
267 new Store<Task>(filename, false)));
269 else if( name == PINMessage::GetDBName() ) {
270 return auto_ptr<Parser>(
271 new RecordParser<PINMessage, Store<PINMessage> > (
272 new Store<PINMessage>(filename, false)));
274 else if( name == SavedMessage::GetDBName() ) {
275 return auto_ptr<Parser>(
276 new RecordParser<SavedMessage, Store<SavedMessage> > (
277 new Store<SavedMessage>(filename, false)));
279 else if( name == Folder::GetDBName() ) {
280 return auto_ptr<Parser>(
281 new RecordParser<Folder, Store<Folder> > (
282 new Store<Folder>(filename, false)));
284 else if( name == Timezone::GetDBName() ) {
285 return auto_ptr<Parser>(
286 new RecordParser<Timezone, Store<Timezone> > (
287 new Store<Timezone>(filename, false)));
289 else {
290 // unknown database, use null parser
291 return auto_ptr<Parser>( new DataDumpParser );
295 auto_ptr<Builder> GetBuilder(const string &name, const string &filename)
297 // check for recognized database names
298 if( name == Contact::GetDBName() ) {
299 return auto_ptr<Builder>(
300 new RecordBuilder<Contact, Store<Contact> > (
301 new Store<Contact>(filename, true)));
303 else if( name == Calendar::GetDBName() ) {
304 return auto_ptr<Builder>(
305 new RecordBuilder<Calendar, Store<Calendar> > (
306 new Store<Calendar>(filename, true)));
309 else if( name == "Messages" ) {
310 return auto_ptr<Parser>(
311 new RecordParser<Message, Store<Message> > (
312 new Store<Message>(filename, true)));
314 else if( name == "Service Book" ) {
315 return auto_ptr<Parser>(
316 new RecordParser<ServiceBook, Store<ServiceBook> > (
317 new Store<ServiceBook>(filename, true)));
319 else if( name == "Memos" ) {
320 return auto_ptr<Parser>(
321 new RecordParser<Memo, Store<Memo> > (
322 new Store<Memo>(filename, true)));
324 else if( name == "Tasks" ) {
325 return auto_ptr<Parser>(
326 new RecordParser<Task, Store<Task> > (
327 new Store<Task>(filename, true)));
330 else {
331 throw std::runtime_error("No Builder available for database");
335 void ShowParsers()
337 cout << "Supported Database parsers:\n"
338 << " Address Book\n"
339 << " Messages\n"
340 << " Calendar\n"
341 << " Service Book\n"
342 << " Memos\n"
343 << " Tasks\n"
344 << " PIN Messages\n"
345 << " Saved Email Messages\n"
346 << " Folders\n"
347 << " Time Zones (read only)\n"
348 << "\n"
349 << "Supported Database builders:\n"
350 << " Address Book\n"
351 << " Calendar\n"
352 << endl;
355 struct StateTableCommand
357 char flag;
358 bool clear;
359 unsigned int index;
361 StateTableCommand(char f, bool c, unsigned int i)
362 : flag(f), clear(c), index(i) {}
365 bool SplitMap(const string &map, string &ldif, string &read, string &write)
367 string::size_type a = map.find(',');
368 if( a == string::npos )
369 return false;
371 string::size_type b = map.find(',', a+1);
372 if( b == string::npos )
373 return false;
375 ldif.assign(map, 0, a);
376 read.assign(map, a + 1, b - a - 1);
377 write.assign(map, b + 1, map.size() - b - 1);
379 return ldif.size() && read.size() && write.size();
382 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
384 for( vector<string>::const_iterator i = mapCommands.begin();
385 i != mapCommands.end();
386 ++i )
388 // single names mean unmapping
389 if( i->find(',') == string::npos ) {
390 // unmap
391 cerr << "Unmapping: " << *i << endl;
392 ldif.Unmap(*i);
394 else {
395 cerr << "Mapping: " << *i << endl;
397 // map... extract ldif/read/write names
398 string ldifname, read, write;
399 if( SplitMap(*i, ldifname, read, write) ) {
400 if( !ldif.Map(ldifname, read, write) ) {
401 cerr << "Read/Write name unknown: " << *i << endl;
404 else {
405 cerr << "Invalid map format: " << *i << endl;
411 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
413 int read, write;
414 char comma;
415 istringstream iss(arg);
416 iss >> hex >> read >> comma >> write;
417 if( !iss )
418 return false;
419 epp->read = read;
420 epp->write = write;
421 return true;
424 int main(int argc, char *argv[])
426 cout.sync_with_stdio(true); // leave this on, since libusb uses
427 // stdio for debug messages
429 try {
431 uint32_t pin = 0;
432 bool list_only = false,
433 show_dbdb = false,
434 ldif_contacts = false,
435 data_dump = false,
436 reset_device = false,
437 list_contact_fields = false,
438 list_ldif_map = false,
439 epp_override = false,
440 threaded_sockets = true,
441 record_state = false,
442 null_parser = false;
443 string ldifBaseDN, ldifDnAttr;
444 string filename;
445 string password;
446 string busname;
447 string devname;
448 string iconvCharset;
449 vector<string> dbNames, saveDbNames, mapCommands;
450 vector<StateTableCommand> stCommands;
451 Usb::EndpointPair epOverride;
453 // process command line options
454 for(;;) {
455 int cmd = getopt(argc, argv, "B:c:C:d:D:e:f:hi:lLm:MnN:p:P:r:R:Ss:tT:vXzZ");
456 if( cmd == -1 )
457 break;
459 switch( cmd )
461 case 'B': // busname
462 busname = optarg;
463 break;
465 case 'c': // contacts to ldap ldif
466 ldif_contacts = true;
467 ldifBaseDN = optarg;
468 break;
470 case 'C': // DN Attribute for FQDN
471 ldifDnAttr = optarg;
472 break;
474 case 'd': // show dbname
475 dbNames.push_back(string(optarg));
476 break;
478 case 'D': // delete record
479 stCommands.push_back(
480 StateTableCommand('D', false, atoi(optarg)));
481 break;
483 case 'e': // endpoint override
484 if( !ParseEpOverride(optarg, &epOverride) ) {
485 Usage();
486 return 1;
488 epp_override = true;
489 break;
491 case 'f': // filename
492 #ifdef __BARRY_BOOST_MODE__
493 filename = optarg;
494 #else
495 cerr << "-f option not supported - no Boost "
496 "serialization support available\n";
497 return 1;
498 #endif
499 break;
501 case 'i': // international charset (iconv)
502 iconvCharset = optarg;
503 break;
505 case 'l': // list only
506 list_only = true;
507 break;
509 case 'L': // List Contact field names
510 list_contact_fields = true;
511 break;
513 case 'm': // Map / Unmap
514 mapCommands.push_back(string(optarg));
515 break;
517 case 'M': // List LDIF map
518 list_ldif_map = true;
519 break;
521 case 'n': // use null parser
522 null_parser = true;
523 break;
525 case 'N': // Devname
526 devname = optarg;
527 break;
529 case 'p': // Blackberry PIN
530 pin = strtoul(optarg, NULL, 16);
531 break;
533 case 'P': // Device password
534 password = optarg;
535 break;
537 case 'r': // get specific record index
538 stCommands.push_back(
539 StateTableCommand('r', false, atoi(optarg)));
540 break;
542 case 'R': // same as 'r', and clears dirty
543 stCommands.push_back(
544 StateTableCommand('r', true, atoi(optarg)));
545 break;
547 case 's': // save dbname
548 saveDbNames.push_back(string(optarg));
549 break;
551 case 'S': // show supported databases
552 ShowParsers();
553 return 0;
555 case 't': // display database database
556 show_dbdb = true;
557 break;
559 case 'T': // show RecordStateTable
560 record_state = true;
561 dbNames.push_back(string(optarg));
562 break;
564 case 'v': // data dump on
565 data_dump = true;
566 break;
568 case 'X': // reset device
569 reset_device = true;
570 break;
572 case 'z': // non-threaded sockets
573 threaded_sockets = false;
574 break;
576 case 'Z': // threaded socket router
577 threaded_sockets = true;
578 break;
580 case 'h': // help
581 default:
582 Usage();
583 return 0;
587 // Initialize the barry library. Must be called before
588 // anything else.
589 Barry::Init(data_dump);
591 // Create an IConverter object if needed
592 auto_ptr<IConverter> ic;
593 if( iconvCharset.size() ) {
594 ic.reset( new IConverter(iconvCharset.c_str(), true) );
597 // LDIF class... only needed if ldif output turned on
598 ContactLdif ldif(ldifBaseDN);
599 DoMapping(ldif, mapCommands);
600 if( ldifDnAttr.size() ) {
601 if( !ldif.SetDNAttr(ldifDnAttr) ) {
602 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
606 // Probe the USB bus for Blackberry devices and display.
607 // If user has specified a PIN, search for it in the
608 // available device list here as well
609 Barry::Probe probe(busname.c_str(), devname.c_str());
610 int activeDevice = -1;
612 // show any errors during probe first
613 if( probe.GetFailCount() ) {
614 if( ldif_contacts )
615 cout << "# ";
616 cout << "Blackberry device errors with errors during probe:" << endl;
617 for( int i = 0; i < probe.GetFailCount(); i++ ) {
618 if( ldif_contacts )
619 cout << "# ";
620 cout << probe.GetFailMsg(i) << endl;
624 // show all successfully found devices
625 if( ldif_contacts )
626 cout << "# ";
627 cout << "Blackberry devices found:" << endl;
628 for( int i = 0; i < probe.GetCount(); i++ ) {
629 if( ldif_contacts )
630 cout << "# ";
631 if( data_dump )
632 probe.Get(i).DumpAll(cout);
633 else
634 cout << probe.Get(i);
635 cout << endl;
636 if( probe.Get(i).m_pin == pin )
637 activeDevice = i;
640 if( list_only )
641 return 0; // done
643 if( activeDevice == -1 ) {
644 if( pin == 0 ) {
645 // can we default to single device?
646 if( probe.GetCount() == 1 )
647 activeDevice = 0;
648 else {
649 cerr << "No device selected" << endl;
650 return 1;
653 else {
654 cerr << "PIN " << setbase(16) << pin
655 << " not found" << endl;
656 return 1;
660 if( ldif_contacts )
661 cout << "# ";
662 cout << "Using device (PIN): " << setbase(16)
663 << probe.Get(activeDevice).m_pin << endl;
665 if( reset_device ) {
666 Usb::Device dev(probe.Get(activeDevice).m_dev);
667 dev.Reset();
668 return 0;
671 // Override device endpoints if user asks
672 Barry::ProbeResult device = probe.Get(activeDevice);
673 if( epp_override ) {
674 device.m_ep.read = epOverride.read;
675 device.m_ep.write = epOverride.write;
676 device.m_ep.type = 2; // FIXME - override this too?
677 cout << "Endpoint pair (read,write) overridden with: "
678 << hex
679 << (unsigned int) device.m_ep.read << ","
680 << (unsigned int) device.m_ep.write << endl;
684 // Create our controller object
686 // Order is important in the following auto_ptr<> objects,
687 // since Controller must get destroyed before router.
688 // Normally you'd pick one method, and not bother
689 // with auto_ptr<> and so the normal C++ constructor
690 // rules would guarantee this safety for you, but
691 // here we want the user to pick.
693 auto_ptr<SocketRoutingQueue> router;
694 auto_ptr<Barry::Controller> pcon;
695 if( threaded_sockets ) {
696 router.reset( new SocketRoutingQueue );
697 router->SpinoffSimpleReadThread();
698 pcon.reset( new Barry::Controller(device, *router) );
700 else {
701 pcon.reset( new Barry::Controller(device) );
704 Barry::Controller &con = *pcon;
705 Barry::Mode::Desktop desktop(con, *ic);
708 // execute each mode that was turned on
712 // Dump list of all databases to stdout
713 if( show_dbdb ) {
714 // open desktop mode socket
715 desktop.Open(password.c_str());
716 cout << desktop.GetDBDB() << endl;
719 // Dump list of Contact field names
720 if( list_contact_fields ) {
721 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
722 cout.fill(' ');
723 cout << " " << left << setw(20) << n->name << ": "
724 << n->description << endl;
728 // Dump current LDIF mapping
729 if( list_ldif_map ) {
730 cout << ldif << endl;
733 // Dump list of contacts to an LDAP LDIF file
734 // This uses the Controller convenience templates
735 if( ldif_contacts ) {
736 // make sure we're in desktop mode
737 desktop.Open(password.c_str());
739 // create a storage functor object that accepts
740 // Barry::Contact objects as input
741 Contact2Ldif storage(ldif);
743 // load all the Contact records into storage
744 desktop.LoadDatabaseByType<Barry::Contact>(storage);
747 // Dump record state table to stdout
748 if( record_state ) {
749 if( dbNames.size() == 0 ) {
750 cout << "No db names to process" << endl;
751 return 1;
754 desktop.Open(password.c_str());
756 vector<string>::iterator b = dbNames.begin();
757 for( ; b != dbNames.end(); b++ ) {
758 unsigned int id = desktop.GetDBID(*b);
759 RecordStateTable state;
760 desktop.GetRecordStateTable(id, state);
761 cout << "Record state table for: " << *b << endl;
762 cout << state;
764 return 0;
767 // Get Record mode overrides the default name mode
768 if( stCommands.size() ) {
769 if( dbNames.size() != 1 ) {
770 cout << "Must have 1 db name to process" << endl;
771 return 1;
774 desktop.Open(password.c_str());
775 unsigned int id = desktop.GetDBID(dbNames[0]);
776 auto_ptr<Parser> parse = GetParser(dbNames[0],filename,null_parser);
778 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
779 desktop.GetRecord(id, stCommands[i].index, *parse.get());
781 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
782 cout << "Clearing record's dirty flags..." << endl;
783 desktop.ClearDirty(id, stCommands[i].index);
786 if( stCommands[i].flag == 'D' ) {
787 desktop.DeleteRecord(id, stCommands[i].index);
791 return 0;
794 // Dump contents of selected databases to stdout, or
795 // to file if specified.
796 // This is retrieving data from the Blackberry.
797 if( dbNames.size() ) {
798 vector<string>::iterator b = dbNames.begin();
800 desktop.Open(password.c_str());
801 for( ; b != dbNames.end(); b++ ) {
802 auto_ptr<Parser> parse = GetParser(*b,filename,null_parser);
803 unsigned int id = desktop.GetDBID(*b);
804 desktop.LoadDatabase(id, *parse.get());
808 // Save contents of file to specified databases
809 // This is writing data to the Blackberry.
810 if( saveDbNames.size() ) {
811 vector<string>::iterator b = saveDbNames.begin();
813 desktop.Open(password.c_str());
814 for( ; b != saveDbNames.end(); b++ ) {
815 auto_ptr<Builder> build =
816 GetBuilder(*b, filename);
817 unsigned int id = desktop.GetDBID(*b);
818 desktop.SaveDatabase(id, *build);
823 catch( Usb::Error &ue) {
824 std::cerr << "Usb::Error caught: " << ue.what() << endl;
825 return 1;
827 catch( Barry::Error &se ) {
828 std::cerr << "Barry::Error caught: " << se.what() << endl;
829 return 1;
831 catch( std::exception &e ) {
832 std::cerr << "std::exception caught: " << e.what() << endl;
833 return 1;
836 return 0;