- fixed the configure scripts and makefiles so that when building
[barry.git] / tools / btool.cc
blobbbd8f3ef97de7aa29d23d09849856b2f9727e5da
1 ///
2 /// \file btool.cc
3 /// Barry library tester
4 ///
6 /*
7 Copyright (C) 2005-2007, 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-2007, 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 << " -c dn Convert address book database to LDIF format, using the\n"
53 << " specified baseDN\n"
54 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
55 << " Defaults to 'cn'\n"
56 << " -d db Load database 'db' FROM device and dump to screen\n"
57 << " Can be used multiple times to fetch more than one DB\n"
58 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
59 << " string separated by a comma, holding the read,write\n"
60 << " endpoint pair. Example: -e 83,5\n"
61 << " Note: Endpoints are specified in hex.\n"
62 << " You should never need to use this option.\n"
63 #ifdef __BARRY_BOOST_MODE__
64 << " -f file Filename to save or load handheld data to/from\n"
65 #endif
66 << " -h This help\n"
67 << " -l List devices\n"
68 << " -L List Contact field names\n"
69 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
70 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
71 << " Unmap: ldif name alone\n"
72 << " -M List current LDIF mapping\n"
73 << " -p pin PIN of device to talk with\n"
74 << " If only one device plugged in, this flag is optional\n"
75 << " -P pass Simplistic method to specify device password\n"
76 << " -s db Save database 'db' TO device from data loaded from -f file\n"
77 << " -S Show list of supported database parsers\n"
78 << " -t Show database database table\n"
79 << " -T db Show record state table for given database\n"
80 << " -v Dump protocol data during operation\n"
81 << " -X Reset device\n"
82 << "\n"
83 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
84 << "\n"
85 << " -r # Record index number as seen in the -T state table.\n"
86 << " This overrides the default -d behaviour, and only\n"
87 << " downloads the one specified record, sending to stdout.\n"
88 << " -R # Same as -r, but also clears the record's dirty flags.\n"
89 << " -D # Record index number as seen in the -T state table,\n"
90 << " which indicates the record to delete. Used with the -d\n"
91 << " command to specify the database.\n"
92 << endl;
95 class Contact2Ldif
97 public:
98 Barry::ContactLdif &ldif;
100 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
102 void operator()(const Contact &rec)
104 ldif.DumpLdif(cout, rec);
108 template <class Record>
109 struct Store
111 std::vector<Record> records;
112 mutable typename std::vector<Record>::const_iterator rec_it;
113 std::string filename;
114 bool load;
115 int count;
117 Store(const string &filename, bool load)
118 : rec_it(records.end()),
119 filename(filename),
120 load(load),
121 count(0)
123 #ifdef __BARRY_BOOST_MODE__
124 try {
126 if( load && filename.size() ) {
127 // filename is available, attempt to load
128 cout << "Loading: " << filename << endl;
129 ifstream ifs(filename.c_str());
130 boost::archive::text_iarchive ia(ifs);
131 ia >> records;
132 cout << records.size()
133 << " records loaded from '"
134 << filename << "'" << endl;
135 sort(records.begin(), records.end());
136 rec_it = records.begin();
138 // debugging aid
139 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
140 for( ; beg != end; beg++ ) {
141 cout << (*beg) << endl;
145 } catch( boost::archive::archive_exception &ae ) {
146 cerr << "Archive exception in ~Store(): "
147 << ae.what() << endl;
149 #endif
151 ~Store()
153 cout << "Store counted " << dec << count << " records." << endl;
154 #ifdef __BARRY_BOOST_MODE__
155 try {
157 if( !load && filename.size() ) {
158 // filename is available, attempt to save
159 cout << "Saving: " << filename << endl;
160 const std::vector<Record> &r = records;
161 ofstream ofs(filename.c_str());
162 boost::archive::text_oarchive oa(ofs);
163 oa << r;
164 cout << dec << r.size() << " records saved to '"
165 << filename << "'" << endl;
168 } catch( boost::archive::archive_exception &ae ) {
169 cerr << "Archive exception in ~Store(): "
170 << ae.what() << endl;
172 #endif
175 // storage operator
176 void operator()(const Record &rec)
178 count++;
179 std::cout << rec << std::endl;
180 records.push_back(rec);
183 // retrieval operator
184 bool operator()(Record &rec, unsigned int databaseId) const
186 if( rec_it == records.end() )
187 return false;
188 rec = *rec_it;
189 rec_it++;
190 return true;
194 class DataDumpParser : public Barry::Parser
196 uint32_t m_id;
198 public:
199 virtual void SetUniqueId(uint32_t id)
201 m_id = id;
204 virtual void ParseFields(const Barry::Data &data, size_t &offset)
206 std::cout << "Raw record dump for record: "
207 << std::hex << m_id << std::endl;
208 std::cout << data << std::endl;
212 auto_ptr<Parser> GetParser(const string &name, const string &filename)
214 // check for recognized database names
215 if( name == "Address Book" ) {
216 return auto_ptr<Parser>(
217 new RecordParser<Contact, Store<Contact> > (
218 new Store<Contact>(filename, false)));
220 else if( name == "Messages" ) {
221 return auto_ptr<Parser>(
222 new RecordParser<Message, Store<Message> > (
223 new Store<Message>(filename, false)));
225 else if( name == "Calendar" ) {
226 return auto_ptr<Parser>(
227 new RecordParser<Calendar, Store<Calendar> > (
228 new Store<Calendar>(filename, false)));
230 else if( name == "Service Book" ) {
231 return auto_ptr<Parser>(
232 new RecordParser<ServiceBook, Store<ServiceBook> > (
233 new Store<ServiceBook>(filename, false)));
236 else if( name == "Memos" ) {
237 return auto_ptr<Parser>(
238 new RecordParser<Memo, Store<Memo> > (
239 new Store<Memo>(filename, false)));
241 else if( name == "Tasks" ) {
242 return auto_ptr<Parser>(
243 new RecordParser<Task, Store<Task> > (
244 new Store<Task>(filename, false)));
246 else if( name == "PIN Messages" ) {
247 return auto_ptr<Parser>(
248 new RecordParser<PINMessage, Store<PINMessage> > (
249 new Store<PINMessage>(filename, false)));
251 else if( name == "Saved Email Messages" ) {
252 return auto_ptr<Parser>(
253 new RecordParser<SavedMessage, Store<SavedMessage> > (
254 new Store<SavedMessage>(filename, false)));
256 else if( name == "Folders" ) {
257 return auto_ptr<Parser>(
258 new RecordParser<Folder, Store<Folder> > (
259 new Store<Folder>(filename, false)));
261 else {
262 // unknown database, use null parser
263 return auto_ptr<Parser>( new DataDumpParser );
267 auto_ptr<Builder> GetBuilder(const string &name, const string &filename)
269 // check for recognized database names
270 if( name == "Address Book" ) {
271 return auto_ptr<Builder>(
272 new RecordBuilder<Contact, Store<Contact> > (
273 new Store<Contact>(filename, true)));
276 else if( name == "Messages" ) {
277 return auto_ptr<Parser>(
278 new RecordParser<Message, Store<Message> > (
279 new Store<Message>(filename, true)));
281 else if( name == "Calendar" ) {
282 return auto_ptr<Parser>(
283 new RecordParser<Calendar, Store<Calendar> > (
284 new Store<Calendar>(filename, true)));
286 else if( name == "Service Book" ) {
287 return auto_ptr<Parser>(
288 new RecordParser<ServiceBook, Store<ServiceBook> > (
289 new Store<ServiceBook>(filename, true)));
291 else if( name == "Memos" ) {
292 return auto_ptr<Parser>(
293 new RecordParser<Memo, Store<Memo> > (
294 new Store<Memo>(filename, true)));
296 else if( name == "Tasks" ) {
297 return auto_ptr<Parser>(
298 new RecordParser<Task, Store<Task> > (
299 new Store<Task>(filename, true)));
302 else {
303 throw std::runtime_error("No Builder available for database");
307 void ShowParsers()
309 cout << "Supported Database parsers:\n"
310 << " Address Book\n"
311 << " Messages\n"
312 << " Calendar\n"
313 << " Service Book\n"
314 << " Memos\n"
315 << " Tasks\n"
316 << " PIN Messages\n"
317 << " Saved Email Messages\n"
318 << " Folders\n"
319 << "\n"
320 << "Supported Database builders:\n"
321 << " Address Book\n"
322 << endl;
325 struct StateTableCommand
327 char flag;
328 bool clear;
329 unsigned int index;
331 StateTableCommand(char f, bool c, unsigned int i)
332 : flag(f), clear(c), index(i) {}
335 bool SplitMap(const string &map, string &ldif, string &read, string &write)
337 string::size_type a = map.find(',');
338 if( a == string::npos )
339 return false;
341 string::size_type b = map.find(',', a+1);
342 if( b == string::npos )
343 return false;
345 ldif.assign(map, 0, a);
346 read.assign(map, a + 1, b - a - 1);
347 write.assign(map, b + 1, map.size() - b - 1);
349 return ldif.size() && read.size() && write.size();
352 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
354 for( vector<string>::const_iterator i = mapCommands.begin();
355 i != mapCommands.end();
356 ++i )
358 // single names mean unmapping
359 if( i->find(',') == string::npos ) {
360 // unmap
361 cerr << "Unmapping: " << *i << endl;
362 ldif.Unmap(*i);
364 else {
365 cerr << "Mapping: " << *i << endl;
367 // map... extract ldif/read/write names
368 string ldifname, read, write;
369 if( SplitMap(*i, ldifname, read, write) ) {
370 if( !ldif.Map(ldifname, read, write) ) {
371 cerr << "Read/Write name unknown: " << *i << endl;
374 else {
375 cerr << "Invalid map format: " << *i << endl;
381 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
383 int read, write;
384 char comma;
385 istringstream iss(arg);
386 iss >> hex >> read >> comma >> write;
387 if( !iss )
388 return false;
389 epp->read = read;
390 epp->write = write;
391 return true;
394 int main(int argc, char *argv[])
396 cout.sync_with_stdio(true); // leave this on, since libusb uses
397 // stdio for debug messages
399 try {
401 uint32_t pin = 0;
402 bool list_only = false,
403 show_dbdb = false,
404 ldif_contacts = false,
405 data_dump = false,
406 reset_device = false,
407 list_contact_fields = false,
408 list_ldif_map = false,
409 epp_override = false,
410 record_state = false;
411 string ldifBaseDN, ldifDnAttr;
412 string filename;
413 string password;
414 vector<string> dbNames, saveDbNames, mapCommands;
415 vector<StateTableCommand> stCommands;
416 Usb::EndpointPair epOverride;
418 // process command line options
419 for(;;) {
420 int cmd = getopt(argc, argv, "c:C:d:D:e:f:hlLm:Mp:P:r:R:Ss:tT:vX");
421 if( cmd == -1 )
422 break;
424 switch( cmd )
426 case 'c': // contacts to ldap ldif
427 ldif_contacts = true;
428 ldifBaseDN = optarg;
429 break;
431 case 'C': // DN Attribute for FQDN
432 ldifDnAttr = optarg;
433 break;
435 case 'd': // show dbname
436 dbNames.push_back(string(optarg));
437 break;
439 case 'D': // delete record
440 stCommands.push_back(
441 StateTableCommand('D', false, atoi(optarg)));
442 break;
444 case 'e': // endpoint override
445 if( !ParseEpOverride(optarg, &epOverride) ) {
446 Usage();
447 return 1;
449 epp_override = true;
450 break;
452 case 'f': // filename
453 #ifdef __BARRY_BOOST_MODE__
454 filename = optarg;
455 #else
456 cerr << "-f option not supported - no Boost "
457 "serialization support available\n";
458 return 1;
459 #endif
460 break;
461 case 'l': // list only
462 list_only = true;
463 break;
465 case 'L': // List Contact field names
466 list_contact_fields = true;
467 break;
469 case 'm': // Map / Unmap
470 mapCommands.push_back(string(optarg));
471 break;
473 case 'M': // List LDIF map
474 list_ldif_map = true;
475 break;
477 case 'p': // Blackberry PIN
478 pin = strtoul(optarg, NULL, 16);
479 break;
481 case 'P': // Device password
482 password = optarg;
483 break;
485 case 'r': // get specific record index
486 stCommands.push_back(
487 StateTableCommand('r', false, atoi(optarg)));
488 break;
490 case 'R': // same as 'r', and clears dirty
491 stCommands.push_back(
492 StateTableCommand('r', true, atoi(optarg)));
493 break;
495 case 's': // save dbname
496 saveDbNames.push_back(string(optarg));
497 break;
499 case 'S': // show supported databases
500 ShowParsers();
501 return 0;
503 case 't': // display database database
504 show_dbdb = true;
505 break;
507 case 'T': // show RecordStateTable
508 record_state = true;
509 dbNames.push_back(string(optarg));
510 break;
512 case 'v': // data dump on
513 data_dump = true;
514 break;
516 case 'X': // reset device
517 reset_device = true;
518 break;
520 case 'h': // help
521 default:
522 Usage();
523 return 0;
527 // Initialize the barry library. Must be called before
528 // anything else.
529 Barry::Init(data_dump);
531 // LDIF class... only needed if ldif output turned on
532 ContactLdif ldif(ldifBaseDN);
533 DoMapping(ldif, mapCommands);
534 if( ldifDnAttr.size() ) {
535 if( !ldif.SetDNAttr(ldifDnAttr) ) {
536 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
540 // Probe the USB bus for Blackberry devices and display.
541 // If user has specified a PIN, search for it in the
542 // available device list here as well
543 Barry::Probe probe;
544 int activeDevice = -1;
545 if( ldif_contacts )
546 cout << "# ";
547 cout << "Blackberry devices found:" << endl;
548 for( int i = 0; i < probe.GetCount(); i++ ) {
549 if( ldif_contacts )
550 cout << "# ";
551 cout << probe.Get(i) << endl;
552 if( probe.Get(i).m_pin == pin )
553 activeDevice = i;
556 if( list_only )
557 return 0; // done
559 if( activeDevice == -1 ) {
560 if( pin == 0 ) {
561 // can we default to single device?
562 if( probe.GetCount() == 1 )
563 activeDevice = 0;
564 else {
565 cerr << "No device selected" << endl;
566 return 1;
569 else {
570 cerr << "PIN " << setbase(16) << pin
571 << " not found" << endl;
572 return 1;
576 if( reset_device ) {
577 Usb::Device dev(probe.Get(activeDevice).m_dev);
578 dev.Reset();
579 return 0;
582 // Create our controller object
583 Barry::ProbeResult device = probe.Get(activeDevice);
584 if( epp_override ) {
585 device.m_ep.read = epOverride.read;
586 device.m_ep.write = epOverride.write;
587 device.m_ep.type = 2; // FIXME - override this too?
588 cout << "Endpoint pair (read,write) overridden with: "
589 << hex
590 << (unsigned int) device.m_ep.read << ","
591 << (unsigned int) device.m_ep.write << endl;
593 Barry::Controller con(device);
596 // execute each mode that was turned on
600 // Dump list of all databases to stdout
601 if( show_dbdb ) {
602 // open desktop mode socket
603 con.OpenMode(Controller::Desktop, password.c_str());
604 cout << con.GetDBDB() << endl;
607 // Dump list of Contact field names
608 if( list_contact_fields ) {
609 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
610 cout.fill(' ');
611 cout << " " << left << setw(20) << n->name << ": "
612 << n->description << endl;
616 // Dump current LDIF mapping
617 if( list_ldif_map ) {
618 cout << ldif << endl;
621 // Dump list of contacts to an LDAP LDIF file
622 // This uses the Controller convenience templates
623 if( ldif_contacts ) {
624 // make sure we're in desktop mode
625 con.OpenMode(Controller::Desktop, password.c_str());
627 // create a storage functor object that accepts
628 // Barry::Contact objects as input
629 Contact2Ldif storage(ldif);
631 // load all the Contact records into storage
632 con.LoadDatabaseByType<Barry::Contact>(storage);
635 // Dump record state table to stdout
636 if( record_state ) {
637 if( dbNames.size() == 0 ) {
638 cout << "No db names to process" << endl;
639 return 1;
642 vector<string>::iterator b = dbNames.begin();
643 for( ; b != dbNames.end(); b++ ) {
644 con.OpenMode(Controller::Desktop, password.c_str());
645 unsigned int id = con.GetDBID(*b);
646 RecordStateTable state;
647 con.GetRecordStateTable(id, state);
648 cout << "Record state table for: " << *b << endl;
649 cout << state;
651 return 0;
654 // Get Record mode overrides the default name mode
655 if( stCommands.size() ) {
656 if( dbNames.size() != 1 ) {
657 cout << "Must have 1 db name to process" << endl;
658 return 1;
661 con.OpenMode(Controller::Desktop, password.c_str());
662 unsigned int id = con.GetDBID(dbNames[0]);
663 auto_ptr<Parser> parse = GetParser(dbNames[0],filename);
665 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
666 con.GetRecord(id, stCommands[i].index, *parse.get());
668 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
669 cout << "Clearing record's dirty flags..." << endl;
670 con.ClearDirty(id, stCommands[i].index);
673 if( stCommands[i].flag == 'D' ) {
674 con.DeleteRecord(id, stCommands[i].index);
678 return 0;
681 // Dump contents of selected databases to stdout, or
682 // to file if specified.
683 // This is retrieving data from the Blackberry.
684 if( dbNames.size() ) {
685 vector<string>::iterator b = dbNames.begin();
687 for( ; b != dbNames.end(); b++ ) {
688 con.OpenMode(Controller::Desktop, password.c_str());
689 auto_ptr<Parser> parse = GetParser(*b,filename);
690 unsigned int id = con.GetDBID(*b);
691 con.LoadDatabase(id, *parse.get());
695 // Save contents of file to specified databases
696 // This is writing data to the Blackberry.
697 if( saveDbNames.size() ) {
698 vector<string>::iterator b = saveDbNames.begin();
700 for( ; b != saveDbNames.end(); b++ ) {
701 con.OpenMode(Controller::Desktop, password.c_str());
702 auto_ptr<Builder> build =
703 GetBuilder(*b, filename);
704 unsigned int id = con.GetDBID(*b);
705 con.SaveDatabase(id, *build);
710 catch( Usb::Error &ue) {
711 std::cerr << "Usb::Error caught: " << ue.what() << endl;
712 return 1;
714 catch( Barry::Error &se ) {
715 std::cerr << "Barry::Error caught: " << se.what() << endl;
716 return 1;
718 catch( std::exception &e ) {
719 std::cerr << "std::exception caught: " << e.what() << endl;
720 return 1;
723 return 0;