Typo fix
[barry/pauldeden.git] / tools / btool.cc
blob053564bf344e3a879c6fc371cc8b9708d3593b43
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 <getopt.h>
32 using namespace std;
33 using namespace Barry;
35 void Usage()
37 int major, minor;
38 const char *Version = Barry::Version(major, minor);
40 cerr
41 << "btool - Command line USB Blackberry Test Tool\n"
42 << " Copyright 2005-2008, Net Direct Inc. (http://www.netdirect.ca/)\n"
43 << " Using: " << Version << "\n"
44 << " Compiled "
45 #ifdef __BARRY_BOOST_MODE__
46 << "with"
47 #else
48 << "without"
49 #endif
50 << " Boost support\n"
51 << "\n"
52 << " -B bus Specify which USB bus to search on\n"
53 << " -N dev Specify which system device, using system specific string\n"
54 << "\n"
55 << " -c dn Convert address book database to LDIF format, using the\n"
56 << " specified baseDN\n"
57 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
58 << " Defaults to 'cn'\n"
59 << " -d db Load database 'db' FROM device and dump to screen\n"
60 << " Can be used multiple times to fetch more than one DB\n"
61 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
62 << " string separated by a comma, holding the read,write\n"
63 << " endpoint pair. Example: -e 83,5\n"
64 << " Note: Endpoints are specified in hex.\n"
65 << " You should never need to use this option.\n"
66 #ifdef __BARRY_BOOST_MODE__
67 << " -f file Filename to save or load handheld data to/from\n"
68 #endif
69 << " -h This help\n"
70 << " -l List devices\n"
71 << " -L List Contact field names\n"
72 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
73 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
74 << " Unmap: ldif name alone\n"
75 << " -M List current LDIF mapping\n"
76 << " -p pin PIN of device to talk with\n"
77 << " If only one device is plugged in, this flag is optional\n"
78 << " -P pass Simplistic method to specify device password\n"
79 << " -s db Save database 'db' TO device from data loaded from -f file\n"
80 << " -S Show list of supported database parsers\n"
81 << " -t Show database database table\n"
82 << " -T db Show record state table for given database\n"
83 << " -v Dump protocol data during operation\n"
84 << " -X Reset device\n"
85 << "\n"
86 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
87 << "\n"
88 << " -r # Record index number as seen in the -T state table.\n"
89 << " This overrides the default -d behaviour, and only\n"
90 << " downloads the one specified record, sending to stdout.\n"
91 << " -R # Same as -r, but also clears the record's dirty flags.\n"
92 << " -D # Record index number as seen in the -T state table,\n"
93 << " which indicates the record to delete. Used with the -d\n"
94 << " command to specify the database.\n"
95 << endl;
98 class Contact2Ldif
100 public:
101 Barry::ContactLdif &ldif;
103 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
105 void operator()(const Contact &rec)
107 ldif.DumpLdif(cout, rec);
111 template <class Record>
112 struct Store
114 std::vector<Record> records;
115 mutable typename std::vector<Record>::const_iterator rec_it;
116 std::string filename;
117 bool load;
118 int count;
120 Store(const string &filename, bool load)
121 : rec_it(records.end()),
122 filename(filename),
123 load(load),
124 count(0)
126 #ifdef __BARRY_BOOST_MODE__
127 try {
129 if( load && filename.size() ) {
130 // filename is available, attempt to load
131 cout << "Loading: " << filename << endl;
132 ifstream ifs(filename.c_str());
133 std::string dbName;
134 getline(ifs, dbName);
135 boost::archive::text_iarchive ia(ifs);
136 ia >> records;
137 cout << records.size()
138 << " records loaded from '"
139 << filename << "'" << endl;
140 sort(records.begin(), records.end());
141 rec_it = records.begin();
143 // debugging aid
144 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
145 for( ; beg != end; beg++ ) {
146 cout << (*beg) << endl;
150 } catch( boost::archive::archive_exception &ae ) {
151 cerr << "Archive exception in ~Store(): "
152 << ae.what() << endl;
154 #endif
156 ~Store()
158 cout << "Store counted " << dec << count << " records." << endl;
159 #ifdef __BARRY_BOOST_MODE__
160 try {
162 if( !load && filename.size() ) {
163 // filename is available, attempt to save
164 cout << "Saving: " << filename << endl;
165 const std::vector<Record> &r = records;
166 ofstream ofs(filename.c_str());
167 ofs << Record::GetDBName() << endl;
168 boost::archive::text_oarchive oa(ofs);
169 oa << r;
170 cout << dec << r.size() << " records saved to '"
171 << filename << "'" << endl;
174 } catch( boost::archive::archive_exception &ae ) {
175 cerr << "Archive exception in ~Store(): "
176 << ae.what() << endl;
178 #endif
181 // storage operator
182 void operator()(const Record &rec)
184 count++;
185 std::cout << rec << std::endl;
186 records.push_back(rec);
189 // retrieval operator
190 bool operator()(Record &rec, unsigned int databaseId) const
192 if( rec_it == records.end() )
193 return false;
194 rec = *rec_it;
195 rec_it++;
196 return true;
200 class DataDumpParser : public Barry::Parser
202 uint32_t m_id;
204 public:
205 virtual void SetUniqueId(uint32_t id)
207 m_id = id;
210 virtual void ParseFields(const Barry::Data &data, size_t &offset)
212 std::cout << "Raw record dump for record: "
213 << std::hex << m_id << std::endl;
214 std::cout << data << std::endl;
218 auto_ptr<Parser> GetParser(const string &name, const string &filename)
220 // check for recognized database names
221 if( name == Contact::GetDBName() ) {
222 return auto_ptr<Parser>(
223 new RecordParser<Contact, Store<Contact> > (
224 new Store<Contact>(filename, false)));
226 else if( name == Message::GetDBName() ) {
227 return auto_ptr<Parser>(
228 new RecordParser<Message, Store<Message> > (
229 new Store<Message>(filename, false)));
231 else if( name == Calendar::GetDBName() ) {
232 return auto_ptr<Parser>(
233 new RecordParser<Calendar, Store<Calendar> > (
234 new Store<Calendar>(filename, false)));
236 else if( name == ServiceBook::GetDBName() ) {
237 return auto_ptr<Parser>(
238 new RecordParser<ServiceBook, Store<ServiceBook> > (
239 new Store<ServiceBook>(filename, false)));
242 else if( name == Memo::GetDBName() ) {
243 return auto_ptr<Parser>(
244 new RecordParser<Memo, Store<Memo> > (
245 new Store<Memo>(filename, false)));
247 else if( name == Task::GetDBName() ) {
248 return auto_ptr<Parser>(
249 new RecordParser<Task, Store<Task> > (
250 new Store<Task>(filename, false)));
252 else if( name == PINMessage::GetDBName() ) {
253 return auto_ptr<Parser>(
254 new RecordParser<PINMessage, Store<PINMessage> > (
255 new Store<PINMessage>(filename, false)));
257 else if( name == SavedMessage::GetDBName() ) {
258 return auto_ptr<Parser>(
259 new RecordParser<SavedMessage, Store<SavedMessage> > (
260 new Store<SavedMessage>(filename, false)));
262 else if( name == Folder::GetDBName() ) {
263 return auto_ptr<Parser>(
264 new RecordParser<Folder, Store<Folder> > (
265 new Store<Folder>(filename, false)));
267 else if( name == Timezone::GetDBName() ) {
268 return auto_ptr<Parser>(
269 new RecordParser<Timezone, Store<Timezone> > (
270 new Store<Timezone>(filename, false)));
272 else {
273 // unknown database, use null parser
274 return auto_ptr<Parser>( new DataDumpParser );
278 auto_ptr<Builder> GetBuilder(const string &name, const string &filename)
280 // check for recognized database names
281 if( name == Contact::GetDBName() ) {
282 return auto_ptr<Builder>(
283 new RecordBuilder<Contact, Store<Contact> > (
284 new Store<Contact>(filename, true)));
287 else if( name == "Messages" ) {
288 return auto_ptr<Parser>(
289 new RecordParser<Message, Store<Message> > (
290 new Store<Message>(filename, true)));
292 else if( name == "Calendar" ) {
293 return auto_ptr<Parser>(
294 new RecordParser<Calendar, Store<Calendar> > (
295 new Store<Calendar>(filename, true)));
297 else if( name == "Service Book" ) {
298 return auto_ptr<Parser>(
299 new RecordParser<ServiceBook, Store<ServiceBook> > (
300 new Store<ServiceBook>(filename, true)));
302 else if( name == "Memos" ) {
303 return auto_ptr<Parser>(
304 new RecordParser<Memo, Store<Memo> > (
305 new Store<Memo>(filename, true)));
307 else if( name == "Tasks" ) {
308 return auto_ptr<Parser>(
309 new RecordParser<Task, Store<Task> > (
310 new Store<Task>(filename, true)));
313 else {
314 throw std::runtime_error("No Builder available for database");
318 void ShowParsers()
320 cout << "Supported Database parsers:\n"
321 << " Address Book\n"
322 << " Messages\n"
323 << " Calendar\n"
324 << " Service Book\n"
325 << " Memos\n"
326 << " Tasks\n"
327 << " PIN Messages\n"
328 << " Saved Email Messages\n"
329 << " Folders\n"
330 << " Time Zones (read only)\n"
331 << "\n"
332 << "Supported Database builders:\n"
333 << " Address Book\n"
334 << endl;
337 struct StateTableCommand
339 char flag;
340 bool clear;
341 unsigned int index;
343 StateTableCommand(char f, bool c, unsigned int i)
344 : flag(f), clear(c), index(i) {}
347 bool SplitMap(const string &map, string &ldif, string &read, string &write)
349 string::size_type a = map.find(',');
350 if( a == string::npos )
351 return false;
353 string::size_type b = map.find(',', a+1);
354 if( b == string::npos )
355 return false;
357 ldif.assign(map, 0, a);
358 read.assign(map, a + 1, b - a - 1);
359 write.assign(map, b + 1, map.size() - b - 1);
361 return ldif.size() && read.size() && write.size();
364 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
366 for( vector<string>::const_iterator i = mapCommands.begin();
367 i != mapCommands.end();
368 ++i )
370 // single names mean unmapping
371 if( i->find(',') == string::npos ) {
372 // unmap
373 cerr << "Unmapping: " << *i << endl;
374 ldif.Unmap(*i);
376 else {
377 cerr << "Mapping: " << *i << endl;
379 // map... extract ldif/read/write names
380 string ldifname, read, write;
381 if( SplitMap(*i, ldifname, read, write) ) {
382 if( !ldif.Map(ldifname, read, write) ) {
383 cerr << "Read/Write name unknown: " << *i << endl;
386 else {
387 cerr << "Invalid map format: " << *i << endl;
393 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
395 int read, write;
396 char comma;
397 istringstream iss(arg);
398 iss >> hex >> read >> comma >> write;
399 if( !iss )
400 return false;
401 epp->read = read;
402 epp->write = write;
403 return true;
406 int main(int argc, char *argv[])
408 cout.sync_with_stdio(true); // leave this on, since libusb uses
409 // stdio for debug messages
411 try {
413 uint32_t pin = 0;
414 bool list_only = false,
415 show_dbdb = false,
416 ldif_contacts = false,
417 data_dump = false,
418 reset_device = false,
419 list_contact_fields = false,
420 list_ldif_map = false,
421 epp_override = false,
422 record_state = false;
423 string ldifBaseDN, ldifDnAttr;
424 string filename;
425 string password;
426 string busname;
427 string devname;
428 vector<string> dbNames, saveDbNames, mapCommands;
429 vector<StateTableCommand> stCommands;
430 Usb::EndpointPair epOverride;
432 // process command line options
433 for(;;) {
434 int cmd = getopt(argc, argv, "B:c:C:d:D:e:f:hlLm:MN:p:P:r:R:Ss:tT:vX");
435 if( cmd == -1 )
436 break;
438 switch( cmd )
440 case 'B': // busname
441 busname = optarg;
442 break;
444 case 'c': // contacts to ldap ldif
445 ldif_contacts = true;
446 ldifBaseDN = optarg;
447 break;
449 case 'C': // DN Attribute for FQDN
450 ldifDnAttr = optarg;
451 break;
453 case 'd': // show dbname
454 dbNames.push_back(string(optarg));
455 break;
457 case 'D': // delete record
458 stCommands.push_back(
459 StateTableCommand('D', false, atoi(optarg)));
460 break;
462 case 'e': // endpoint override
463 if( !ParseEpOverride(optarg, &epOverride) ) {
464 Usage();
465 return 1;
467 epp_override = true;
468 break;
470 case 'f': // filename
471 #ifdef __BARRY_BOOST_MODE__
472 filename = optarg;
473 #else
474 cerr << "-f option not supported - no Boost "
475 "serialization support available\n";
476 return 1;
477 #endif
478 break;
479 case 'l': // list only
480 list_only = true;
481 break;
483 case 'L': // List Contact field names
484 list_contact_fields = true;
485 break;
487 case 'm': // Map / Unmap
488 mapCommands.push_back(string(optarg));
489 break;
491 case 'M': // List LDIF map
492 list_ldif_map = true;
493 break;
495 case 'N': // Devname
496 devname = optarg;
497 break;
499 case 'p': // Blackberry PIN
500 pin = strtoul(optarg, NULL, 16);
501 break;
503 case 'P': // Device password
504 password = optarg;
505 break;
507 case 'r': // get specific record index
508 stCommands.push_back(
509 StateTableCommand('r', false, atoi(optarg)));
510 break;
512 case 'R': // same as 'r', and clears dirty
513 stCommands.push_back(
514 StateTableCommand('r', true, atoi(optarg)));
515 break;
517 case 's': // save dbname
518 saveDbNames.push_back(string(optarg));
519 break;
521 case 'S': // show supported databases
522 ShowParsers();
523 return 0;
525 case 't': // display database database
526 show_dbdb = true;
527 break;
529 case 'T': // show RecordStateTable
530 record_state = true;
531 dbNames.push_back(string(optarg));
532 break;
534 case 'v': // data dump on
535 data_dump = true;
536 break;
538 case 'X': // reset device
539 reset_device = true;
540 break;
542 case 'h': // help
543 default:
544 Usage();
545 return 0;
549 // Initialize the barry library. Must be called before
550 // anything else.
551 Barry::Init(data_dump);
553 // LDIF class... only needed if ldif output turned on
554 ContactLdif ldif(ldifBaseDN);
555 DoMapping(ldif, mapCommands);
556 if( ldifDnAttr.size() ) {
557 if( !ldif.SetDNAttr(ldifDnAttr) ) {
558 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
562 // Probe the USB bus for Blackberry devices and display.
563 // If user has specified a PIN, search for it in the
564 // available device list here as well
565 Barry::Probe probe(busname.c_str(), devname.c_str());
566 int activeDevice = -1;
568 // show any errors during probe first
569 if( probe.GetFailCount() ) {
570 if( ldif_contacts )
571 cout << "# ";
572 cout << "Blackberry device errors with errors during probe:" << endl;
573 for( int i = 0; i < probe.GetFailCount(); i++ ) {
574 if( ldif_contacts )
575 cout << "# ";
576 cout << probe.GetFailMsg(i) << endl;
580 // show all successfully found devices
581 if( ldif_contacts )
582 cout << "# ";
583 cout << "Blackberry devices found:" << endl;
584 for( int i = 0; i < probe.GetCount(); i++ ) {
585 if( ldif_contacts )
586 cout << "# ";
587 if( data_dump )
588 probe.Get(i).DumpAll(cout);
589 else
590 cout << probe.Get(i);
591 cout << endl;
592 if( probe.Get(i).m_pin == pin )
593 activeDevice = i;
596 if( list_only )
597 return 0; // done
599 if( activeDevice == -1 ) {
600 if( pin == 0 ) {
601 // can we default to single device?
602 if( probe.GetCount() == 1 )
603 activeDevice = 0;
604 else {
605 cerr << "No device selected" << endl;
606 return 1;
609 else {
610 cerr << "PIN " << setbase(16) << pin
611 << " not found" << endl;
612 return 1;
616 if( ldif_contacts )
617 cout << "# ";
618 cout << "Using device (PIN): " << setbase(16)
619 << probe.Get(activeDevice).m_pin << endl;
621 if( reset_device ) {
622 Usb::Device dev(probe.Get(activeDevice).m_dev);
623 dev.Reset();
624 return 0;
627 // Create our socket router and thread
628 SocketRoutingQueue router;
629 router.SpinoffSimpleReadThread();
631 // Create our controller object
632 Barry::ProbeResult device = probe.Get(activeDevice);
633 if( epp_override ) {
634 device.m_ep.read = epOverride.read;
635 device.m_ep.write = epOverride.write;
636 device.m_ep.type = 2; // FIXME - override this too?
637 cout << "Endpoint pair (read,write) overridden with: "
638 << hex
639 << (unsigned int) device.m_ep.read << ","
640 << (unsigned int) device.m_ep.write << endl;
642 Barry::Controller con(device, router);
643 Barry::Mode::Desktop desktop(con);
646 // execute each mode that was turned on
650 // Dump list of all databases to stdout
651 if( show_dbdb ) {
652 // open desktop mode socket
653 desktop.Open(password.c_str());
654 cout << desktop.GetDBDB() << endl;
657 // Dump list of Contact field names
658 if( list_contact_fields ) {
659 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
660 cout.fill(' ');
661 cout << " " << left << setw(20) << n->name << ": "
662 << n->description << endl;
666 // Dump current LDIF mapping
667 if( list_ldif_map ) {
668 cout << ldif << endl;
671 // Dump list of contacts to an LDAP LDIF file
672 // This uses the Controller convenience templates
673 if( ldif_contacts ) {
674 // make sure we're in desktop mode
675 desktop.Open(password.c_str());
677 // create a storage functor object that accepts
678 // Barry::Contact objects as input
679 Contact2Ldif storage(ldif);
681 // load all the Contact records into storage
682 desktop.LoadDatabaseByType<Barry::Contact>(storage);
685 // Dump record state table to stdout
686 if( record_state ) {
687 if( dbNames.size() == 0 ) {
688 cout << "No db names to process" << endl;
689 return 1;
692 desktop.Open(password.c_str());
694 vector<string>::iterator b = dbNames.begin();
695 for( ; b != dbNames.end(); b++ ) {
696 unsigned int id = desktop.GetDBID(*b);
697 RecordStateTable state;
698 desktop.GetRecordStateTable(id, state);
699 cout << "Record state table for: " << *b << endl;
700 cout << state;
702 return 0;
705 // Get Record mode overrides the default name mode
706 if( stCommands.size() ) {
707 if( dbNames.size() != 1 ) {
708 cout << "Must have 1 db name to process" << endl;
709 return 1;
712 desktop.Open(password.c_str());
713 unsigned int id = desktop.GetDBID(dbNames[0]);
714 auto_ptr<Parser> parse = GetParser(dbNames[0],filename);
716 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
717 desktop.GetRecord(id, stCommands[i].index, *parse.get());
719 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
720 cout << "Clearing record's dirty flags..." << endl;
721 desktop.ClearDirty(id, stCommands[i].index);
724 if( stCommands[i].flag == 'D' ) {
725 desktop.DeleteRecord(id, stCommands[i].index);
729 return 0;
732 // Dump contents of selected databases to stdout, or
733 // to file if specified.
734 // This is retrieving data from the Blackberry.
735 if( dbNames.size() ) {
736 vector<string>::iterator b = dbNames.begin();
738 desktop.Open(password.c_str());
739 for( ; b != dbNames.end(); b++ ) {
740 auto_ptr<Parser> parse = GetParser(*b,filename);
741 unsigned int id = desktop.GetDBID(*b);
742 desktop.LoadDatabase(id, *parse.get());
746 // Save contents of file to specified databases
747 // This is writing data to the Blackberry.
748 if( saveDbNames.size() ) {
749 vector<string>::iterator b = saveDbNames.begin();
751 desktop.Open(password.c_str());
752 for( ; b != saveDbNames.end(); b++ ) {
753 auto_ptr<Builder> build =
754 GetBuilder(*b, filename);
755 unsigned int id = desktop.GetDBID(*b);
756 desktop.SaveDatabase(id, *build);
761 catch( Usb::Error &ue) {
762 std::cerr << "Usb::Error caught: " << ue.what() << endl;
763 return 1;
765 catch( Barry::Error &se ) {
766 std::cerr << "Barry::Error caught: " << se.what() << endl;
767 return 1;
769 catch( std::exception &e ) {
770 std::cerr << "std::exception caught: " << e.what() << endl;
771 return 1;
774 return 0;