- added raw data version of BuildField() to record-internal.h
[barry.git] / tools / btool.cc
blob6c91c8279fd76d3d5509e4b3ccfb64d015f189e2
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 <vector>
27 #include <string>
28 #include <getopt.h>
31 using namespace std;
32 using namespace Barry;
34 void Usage()
36 int major, minor;
37 const char *Version = Barry::Version(major, minor);
39 cerr
40 << "btool - Command line USB Blackberry Test Tool\n"
41 << " Copyright 2005-2007, Net Direct Inc. (http://www.netdirect.ca/)\n"
42 << " Using: " << Version << "\n"
43 << " Compiled "
44 #ifdef __BARRY_BOOST_MODE__
45 << "with"
46 #else
47 << "without"
48 #endif
49 << " Boost support\n"
50 << "\n"
51 << " -c dn Convert address book database to LDIF format, using the\n"
52 << " specified baseDN\n"
53 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
54 << " Defaults to 'cn'\n"
55 << " -d db Load database 'db' FROM device and dump to screen\n"
56 << " Can be used multiple times to fetch more than one DB\n"
57 #ifdef __BARRY_BOOST_MODE__
58 << " -f file Filename to save or load handheld data to/from\n"
59 #endif
60 << " -h This help\n"
61 << " -l List devices\n"
62 << " -L List Contact field names\n"
63 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
64 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
65 << " Unmap: ldif name alone\n"
66 << " -M List current LDIF mapping\n"
67 << " -p pin PIN of device to talk with\n"
68 << " If only one device plugged in, this flag is optional\n"
69 << " -P pass Simplistic method to specify device password\n"
70 << " -s db Save database 'db' TO device from data loaded from -f file\n"
71 << " -t Show database database table\n"
72 << " -T db Show record state table for given database\n"
73 << " -v Dump protocol data during operation\n"
74 << " -X Reset device\n"
75 << "\n"
76 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
77 << "\n"
78 << " -r # Record index number as seen in the -T state table.\n"
79 << " This overrides the default -d behaviour, and only\n"
80 << " downloads the one specified record, sending to stdout.\n"
81 << " -R # Same as -r, but also clears the record's dirty flags.\n"
82 << " -D # Record index number as seen in the -T state table,\n"
83 << " which indicates the record to delete. Used with the -d\n"
84 << " command to specify the database.\n"
85 << endl;
88 class Contact2Ldif
90 public:
91 Barry::ContactLdif &ldif;
93 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
95 void operator()(const Contact &rec)
97 ldif.DumpLdif(cout, rec);
101 template <class Record>
102 struct Store
104 std::vector<Record> records;
105 mutable typename std::vector<Record>::const_iterator rec_it;
106 std::string filename;
107 bool load;
108 int count;
110 Store(const string &filename, bool load)
111 : rec_it(records.end()),
112 filename(filename),
113 load(load),
114 count(0)
116 #ifdef __BARRY_BOOST_MODE__
117 try {
119 if( load && filename.size() ) {
120 // filename is available, attempt to load
121 cout << "Loading: " << filename << endl;
122 ifstream ifs(filename.c_str());
123 boost::archive::text_iarchive ia(ifs);
124 ia >> records;
125 cout << records.size()
126 << " records loaded from '"
127 << filename << "'" << endl;
128 sort(records.begin(), records.end());
129 rec_it = records.begin();
131 // debugging aid
132 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
133 for( ; beg != end; beg++ ) {
134 cout << (*beg) << endl;
138 } catch( boost::archive::archive_exception &ae ) {
139 cerr << "Archive exception in ~Store(): "
140 << ae.what() << endl;
142 #endif
144 ~Store()
146 cout << "Store counted " << dec << count << " records." << endl;
147 #ifdef __BARRY_BOOST_MODE__
148 try {
150 if( !load && filename.size() ) {
151 // filename is available, attempt to save
152 cout << "Saving: " << filename << endl;
153 const std::vector<Record> &r = records;
154 ofstream ofs(filename.c_str());
155 boost::archive::text_oarchive oa(ofs);
156 oa << r;
157 cout << dec << r.size() << " records saved to '"
158 << filename << "'" << endl;
161 } catch( boost::archive::archive_exception &ae ) {
162 cerr << "Archive exception in ~Store(): "
163 << ae.what() << endl;
165 #endif
168 // storage operator
169 void operator()(const Record &rec)
171 count++;
172 std::cout << rec << std::endl;
173 records.push_back(rec);
176 // retrieval operator
177 bool operator()(Record &rec, unsigned int databaseId) const
179 if( rec_it == records.end() )
180 return false;
181 rec = *rec_it;
182 rec_it++;
183 return true;
187 class DataDumpParser : public Barry::Parser
189 uint32_t m_id;
191 public:
192 virtual void SetUniqueId(uint32_t id)
194 m_id = id;
197 virtual void ParseFields(const Barry::Data &data, size_t &offset)
199 std::cout << "Raw record dump for record: "
200 << std::hex << m_id << std::endl;
201 std::cout << data << std::endl;
205 auto_ptr<Parser> GetParser(const string &name, const string &filename)
207 // check for recognized database names
208 if( name == "Address Book" ) {
209 return auto_ptr<Parser>(
210 new RecordParser<Contact, Store<Contact> > (
211 new Store<Contact>(filename, false)));
213 else if( name == "Messages" ) {
214 return auto_ptr<Parser>(
215 new RecordParser<Message, Store<Message> > (
216 new Store<Message>(filename, false)));
218 else if( name == "Calendar" ) {
219 return auto_ptr<Parser>(
220 new RecordParser<Calendar, Store<Calendar> > (
221 new Store<Calendar>(filename, false)));
223 else if( name == "Service Book" ) {
224 return auto_ptr<Parser>(
225 new RecordParser<ServiceBook, Store<ServiceBook> > (
226 new Store<ServiceBook>(filename, false)));
229 else if( name == "Memos" ) {
230 return auto_ptr<Parser>(
231 new RecordParser<Memo, Store<Memo> > (
232 new Store<Memo>(filename, false)));
234 else if( name == "Tasks" ) {
235 return auto_ptr<Parser>(
236 new RecordParser<Task, Store<Task> > (
237 new Store<Task>(filename, false)));
239 else if( name == "PIN Messages" ) {
240 return auto_ptr<Parser>(
241 new RecordParser<PINMessage, Store<PINMessage> > (
242 new Store<PINMessage>(filename, false)));
244 else if( name == "Saved Email Messages" ) {
245 return auto_ptr<Parser>(
246 new RecordParser<SavedMessage, Store<SavedMessage> > (
247 new Store<SavedMessage>(filename, false)));
249 else if( name == "Folders" ) {
250 return auto_ptr<Parser>(
251 new RecordParser<Folder, Store<Folder> > (
252 new Store<Folder>(filename, false)));
254 else {
255 // unknown database, use null parser
256 return auto_ptr<Parser>( new DataDumpParser );
260 auto_ptr<Builder> GetBuilder(const string &name, const string &filename)
262 // check for recognized database names
263 if( name == "Address Book" ) {
264 return auto_ptr<Builder>(
265 new RecordBuilder<Contact, Store<Contact> > (
266 new Store<Contact>(filename, true)));
269 else if( name == "Messages" ) {
270 return auto_ptr<Parser>(
271 new RecordParser<Message, Store<Message> > (
272 new Store<Message>(filename, true)));
274 else if( name == "Calendar" ) {
275 return auto_ptr<Parser>(
276 new RecordParser<Calendar, Store<Calendar> > (
277 new Store<Calendar>(filename, true)));
279 else if( name == "Service Book" ) {
280 return auto_ptr<Parser>(
281 new RecordParser<ServiceBook, Store<ServiceBook> > (
282 new Store<ServiceBook>(filename, true)));
284 else if( name == "Memos" ) {
285 return auto_ptr<Parser>(
286 new RecordParser<Memo, Store<Memo> > (
287 new Store<Memo>(filename, true)));
289 else if( name == "Tasks" ) {
290 return auto_ptr<Parser>(
291 new RecordParser<Task, Store<Task> > (
292 new Store<Task>(filename, true)));
295 else {
296 throw std::runtime_error("No Builder available for database");
300 struct StateTableCommand
302 char flag;
303 bool clear;
304 unsigned int index;
306 StateTableCommand(char f, bool c, unsigned int i)
307 : flag(f), clear(c), index(i) {}
310 bool SplitMap(const string &map, string &ldif, string &read, string &write)
312 string::size_type a = map.find(',');
313 if( a == string::npos )
314 return false;
316 string::size_type b = map.find(',', a+1);
317 if( b == string::npos )
318 return false;
320 ldif.assign(map, 0, a);
321 read.assign(map, a + 1, b - a - 1);
322 write.assign(map, b + 1, map.size() - b - 1);
324 return ldif.size() && read.size() && write.size();
327 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
329 for( vector<string>::const_iterator i = mapCommands.begin();
330 i != mapCommands.end();
331 ++i )
333 // single names mean unmapping
334 if( i->find(',') == string::npos ) {
335 // unmap
336 cerr << "Unmapping: " << *i << endl;
337 ldif.Unmap(*i);
339 else {
340 cerr << "Mapping: " << *i << endl;
342 // map... extract ldif/read/write names
343 string ldifname, read, write;
344 if( SplitMap(*i, ldifname, read, write) ) {
345 if( !ldif.Map(ldifname, read, write) ) {
346 cerr << "Read/Write name unknown: " << *i << endl;
349 else {
350 cerr << "Invalid map format: " << *i << endl;
356 int main(int argc, char *argv[])
358 cout.sync_with_stdio(true); // leave this on, since libusb uses
359 // stdio for debug messages
361 try {
363 uint32_t pin = 0;
364 bool list_only = false,
365 show_dbdb = false,
366 ldif_contacts = false,
367 data_dump = false,
368 reset_device = false,
369 list_contact_fields = false,
370 list_ldif_map = false,
371 record_state = false;
372 string ldifBaseDN, ldifDnAttr;
373 string filename;
374 string password;
375 vector<string> dbNames, saveDbNames, mapCommands;
376 vector<StateTableCommand> stCommands;
378 // process command line options
379 for(;;) {
380 int cmd = getopt(argc, argv, "c:C:d:D:f:hlLm:Mp:P:r:R:s:tT:vX");
381 if( cmd == -1 )
382 break;
384 switch( cmd )
386 case 'c': // contacts to ldap ldif
387 ldif_contacts = true;
388 ldifBaseDN = optarg;
389 break;
391 case 'C': // DN Attribute for FQDN
392 ldifDnAttr = optarg;
393 break;
395 case 'd': // show dbname
396 dbNames.push_back(string(optarg));
397 break;
399 case 'D': // delete record
400 stCommands.push_back(
401 StateTableCommand('D', false, atoi(optarg)));
402 break;
404 case 'f': // filename
405 #ifdef __BARRY_BOOST_MODE__
406 filename = optarg;
407 #else
408 cerr << "-f option not supported - no Boost "
409 "serialization support available\n";
410 return 1;
411 #endif
412 break;
413 case 'l': // list only
414 list_only = true;
415 break;
417 case 'L': // List Contact field names
418 list_contact_fields = true;
419 break;
421 case 'm': // Map / Unmap
422 mapCommands.push_back(string(optarg));
423 break;
425 case 'M': // List LDIF map
426 list_ldif_map = true;
427 break;
429 case 'p': // Blackberry PIN
430 pin = strtoul(optarg, NULL, 16);
431 break;
433 case 'P': // Device password
434 password = optarg;
435 break;
437 case 'r': // get specific record index
438 stCommands.push_back(
439 StateTableCommand('r', false, atoi(optarg)));
440 break;
442 case 'R': // same as 'r', and clears dirty
443 stCommands.push_back(
444 StateTableCommand('r', true, atoi(optarg)));
445 break;
447 case 's': // save dbname
448 saveDbNames.push_back(string(optarg));
449 break;
451 case 't': // display database database
452 show_dbdb = true;
453 break;
455 case 'T': // show RecordStateTable
456 record_state = true;
457 dbNames.push_back(string(optarg));
458 break;
460 case 'v': // data dump on
461 data_dump = true;
462 break;
464 case 'X': // reset device
465 reset_device = true;
466 break;
468 case 'h': // help
469 default:
470 Usage();
471 return 0;
475 // Initialize the barry library. Must be called before
476 // anything else.
477 Barry::Init(data_dump);
479 // LDIF class... only needed if ldif output turned on
480 ContactLdif ldif(ldifBaseDN);
481 DoMapping(ldif, mapCommands);
482 if( ldifDnAttr.size() ) {
483 if( !ldif.SetDNAttr(ldifDnAttr) ) {
484 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
488 // Probe the USB bus for Blackberry devices and display.
489 // If user has specified a PIN, search for it in the
490 // available device list here as well
491 Barry::Probe probe;
492 int activeDevice = -1;
493 if( ldif_contacts )
494 cout << "# ";
495 cout << "Blackberry devices found:" << endl;
496 for( int i = 0; i < probe.GetCount(); i++ ) {
497 if( ldif_contacts )
498 cout << "# ";
499 cout << probe.Get(i) << endl;
500 if( probe.Get(i).m_pin == pin )
501 activeDevice = i;
504 if( list_only )
505 return 0; // done
507 if( activeDevice == -1 ) {
508 if( pin == 0 ) {
509 // can we default to single device?
510 if( probe.GetCount() == 1 )
511 activeDevice = 0;
512 else {
513 cerr << "No device selected" << endl;
514 return 1;
517 else {
518 cerr << "PIN " << setbase(16) << pin
519 << " not found" << endl;
520 return 1;
524 if( reset_device ) {
525 Usb::Device dev(probe.Get(activeDevice).m_dev);
526 dev.Reset();
527 return 0;
530 // Create our controller object
531 Barry::Controller con(probe.Get(activeDevice));
534 // execute each mode that was turned on
538 // Dump list of all databases to stdout
539 if( show_dbdb ) {
540 // open desktop mode socket
541 con.OpenMode(Controller::Desktop, password.c_str());
542 cout << con.GetDBDB() << endl;
545 // Dump list of Contact field names
546 if( list_contact_fields ) {
547 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
548 cout.fill(' ');
549 cout << " " << left << setw(20) << n->name << ": "
550 << n->description << endl;
554 // Dump current LDIF mapping
555 if( list_ldif_map ) {
556 cout << ldif << endl;
559 // Dump list of contacts to an LDAP LDIF file
560 // This uses the Controller convenience templates
561 if( ldif_contacts ) {
562 // make sure we're in desktop mode
563 con.OpenMode(Controller::Desktop, password.c_str());
565 // create a storage functor object that accepts
566 // Barry::Contact objects as input
567 Contact2Ldif storage(ldif);
569 // load all the Contact records into storage
570 con.LoadDatabaseByType<Barry::Contact>(storage);
573 // Dump record state table to stdout
574 if( record_state ) {
575 if( dbNames.size() == 0 ) {
576 cout << "No db names to process" << endl;
577 return 1;
580 vector<string>::iterator b = dbNames.begin();
581 for( ; b != dbNames.end(); b++ ) {
582 con.OpenMode(Controller::Desktop, password.c_str());
583 unsigned int id = con.GetDBID(*b);
584 RecordStateTable state;
585 con.GetRecordStateTable(id, state);
586 cout << "Record state table for: " << *b << endl;
587 cout << state;
589 return 0;
592 // Get Record mode overrides the default name mode
593 if( stCommands.size() ) {
594 if( dbNames.size() != 1 ) {
595 cout << "Must have 1 db name to process" << endl;
596 return 1;
599 con.OpenMode(Controller::Desktop, password.c_str());
600 unsigned int id = con.GetDBID(dbNames[0]);
601 auto_ptr<Parser> parse = GetParser(dbNames[0],filename);
603 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
604 con.GetRecord(id, stCommands[i].index, *parse.get());
606 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
607 cout << "Clearing record's dirty flags..." << endl;
608 con.ClearDirty(id, stCommands[i].index);
611 if( stCommands[i].flag == 'D' ) {
612 con.DeleteRecord(id, stCommands[i].index);
616 return 0;
619 // Dump contents of selected databases to stdout, or
620 // to file if specified.
621 // This is retrieving data from the Blackberry.
622 if( dbNames.size() ) {
623 vector<string>::iterator b = dbNames.begin();
625 for( ; b != dbNames.end(); b++ ) {
626 con.OpenMode(Controller::Desktop, password.c_str());
627 auto_ptr<Parser> parse = GetParser(*b,filename);
628 unsigned int id = con.GetDBID(*b);
629 con.LoadDatabase(id, *parse.get());
633 // Save contents of file to specified databases
634 // This is writing data to the Blackberry.
635 if( saveDbNames.size() ) {
636 vector<string>::iterator b = saveDbNames.begin();
638 for( ; b != saveDbNames.end(); b++ ) {
639 con.OpenMode(Controller::Desktop, password.c_str());
640 auto_ptr<Builder> build =
641 GetBuilder(*b, filename);
642 unsigned int id = con.GetDBID(*b);
643 con.SaveDatabase(id, *build);
648 catch( Usb::Error &ue) {
649 std::cerr << "Usb::Error caught: " << ue.what() << endl;
650 return 1;
652 catch( Barry::Error &se ) {
653 std::cerr << "Barry::Error caught: " << se.what() << endl;
654 return 1;
656 catch( std::exception &e ) {
657 std::cerr << "std::exception caught: " << e.what() << endl;
658 return 1;
661 return 0;