00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <barry/barry.h>
00023 #include <iomanip>
00024 #include <iostream>
00025 #include <fstream>
00026 #include <sstream>
00027 #include <vector>
00028 #include <string>
00029 #include <algorithm>
00030 #include <getopt.h>
00031
00032
00033 using namespace std;
00034 using namespace Barry;
00035
00036 void Usage()
00037 {
00038 int major, minor;
00039 const char *Version = Barry::Version(major, minor);
00040
00041 cerr
00042 << "btool - Command line USB Blackberry Test Tool\n"
00043 << " Copyright 2005-2008, Net Direct Inc. (http://www.netdirect.ca/)\n"
00044 << " Using: " << Version << "\n"
00045 << " Compiled "
00046 #ifdef __BARRY_BOOST_MODE__
00047 << "with"
00048 #else
00049 << "without"
00050 #endif
00051 << " Boost support\n"
00052 << "\n"
00053 << " -B bus Specify which USB bus to search on\n"
00054 << " -N dev Specify which system device, using system specific string\n"
00055 << "\n"
00056 << " -c dn Convert address book database to LDIF format, using the\n"
00057 << " specified baseDN\n"
00058 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
00059 << " Defaults to 'cn'\n"
00060 << " -d db Load database 'db' FROM device and dump to screen\n"
00061 << " Can be used multiple times to fetch more than one DB\n"
00062 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
00063 << " string separated by a comma, holding the read,write\n"
00064 << " endpoint pair. Example: -e 83,5\n"
00065 << " Note: Endpoints are specified in hex.\n"
00066 << " You should never need to use this option.\n"
00067 #ifdef __BARRY_BOOST_MODE__
00068 << " -f file Filename to save or load handheld data to/from\n"
00069 #endif
00070 << " -h This help\n"
00071 << " -l List devices\n"
00072 << " -L List Contact field names\n"
00073 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
00074 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
00075 << " Unmap: ldif name alone\n"
00076 << " -M List current LDIF mapping\n"
00077 << " -n Use null parser on all databases.\n"
00078 << " -p pin PIN of device to talk with\n"
00079 << " If only one device is plugged in, this flag is optional\n"
00080 << " -P pass Simplistic method to specify device password\n"
00081 << " -s db Save database 'db' TO device from data loaded from -f file\n"
00082 << " -S Show list of supported database parsers\n"
00083 << " -t Show database database table\n"
00084 << " -T db Show record state table for given database\n"
00085 << " -v Dump protocol data during operation\n"
00086 << " -X Reset device\n"
00087 << " -z Use non-threaded sockets\n"
00088 << " -Z Use threaded socket router (default)\n"
00089 << "\n"
00090 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
00091 << "\n"
00092 << " -r # Record index number as seen in the -T state table.\n"
00093 << " This overrides the default -d behaviour, and only\n"
00094 << " downloads the one specified record, sending to stdout.\n"
00095 << " -R # Same as -r, but also clears the record's dirty flags.\n"
00096 << " -D # Record index number as seen in the -T state table,\n"
00097 << " which indicates the record to delete. Used with the -d\n"
00098 << " command to specify the database.\n"
00099 << endl;
00100 }
00101
00102 class Contact2Ldif
00103 {
00104 public:
00105 Barry::ContactLdif &ldif;
00106
00107 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
00108
00109 void operator()(const Contact &rec)
00110 {
00111 ldif.DumpLdif(cout, rec);
00112 }
00113 };
00114
00115 template <class Record>
00116 struct Store
00117 {
00118 std::vector<Record> records;
00119 mutable typename std::vector<Record>::const_iterator rec_it;
00120 std::string filename;
00121 bool load;
00122 int count;
00123
00124 Store(const string &filename, bool load)
00125 : rec_it(records.end()),
00126 filename(filename),
00127 load(load),
00128 count(0)
00129 {
00130 #ifdef __BARRY_BOOST_MODE__
00131 try {
00132
00133 if( load && filename.size() ) {
00134
00135 cout << "Loading: " << filename << endl;
00136 ifstream ifs(filename.c_str());
00137 std::string dbName;
00138 getline(ifs, dbName);
00139 boost::archive::text_iarchive ia(ifs);
00140 ia >> records;
00141 cout << records.size()
00142 << " records loaded from '"
00143 << filename << "'" << endl;
00144 sort(records.begin(), records.end());
00145 rec_it = records.begin();
00146
00147
00148 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
00149 for( ; beg != end; beg++ ) {
00150 cout << (*beg) << endl;
00151 }
00152 }
00153
00154 } catch( boost::archive::archive_exception &ae ) {
00155 cerr << "Archive exception in ~Store(): "
00156 << ae.what() << endl;
00157 }
00158 #endif
00159 }
00160 ~Store()
00161 {
00162 cout << "Store counted " << dec << count << " records." << endl;
00163 #ifdef __BARRY_BOOST_MODE__
00164 try {
00165
00166 if( !load && filename.size() ) {
00167
00168 cout << "Saving: " << filename << endl;
00169 const std::vector<Record> &r = records;
00170 ofstream ofs(filename.c_str());
00171 ofs << Record::GetDBName() << endl;
00172 boost::archive::text_oarchive oa(ofs);
00173 oa << r;
00174 cout << dec << r.size() << " records saved to '"
00175 << filename << "'" << endl;
00176 }
00177
00178 } catch( boost::archive::archive_exception &ae ) {
00179 cerr << "Archive exception in ~Store(): "
00180 << ae.what() << endl;
00181 }
00182 #endif
00183 }
00184
00185
00186 void operator()(const Record &rec)
00187 {
00188 count++;
00189 std::cout << rec << std::endl;
00190 records.push_back(rec);
00191 }
00192
00193
00194 bool operator()(Record &rec, unsigned int databaseId) const
00195 {
00196 if( rec_it == records.end() )
00197 return false;
00198 rec = *rec_it;
00199 rec_it++;
00200 return true;
00201 }
00202 };
00203
00204 class DataDumpParser : public Barry::Parser
00205 {
00206 uint32_t m_id;
00207
00208 public:
00209 virtual void SetIds(uint8_t RecType, uint32_t UniqueId)
00210 {
00211 m_id = UniqueId;
00212 }
00213
00214 virtual void ParseFields(const Barry::Data &data, size_t &offset)
00215 {
00216 std::cout << "Raw record dump for record: "
00217 << std::hex << m_id << std::endl;
00218 std::cout << data << std::endl;
00219 }
00220 };
00221
00222 auto_ptr<Parser> GetParser(const string &name, const string &filename, bool null_parser)
00223 {
00224 if( null_parser ) {
00225
00226 return auto_ptr<Parser>( new DataDumpParser );
00227 }
00228
00229 else if( name == Contact::GetDBName() ) {
00230 return auto_ptr<Parser>(
00231 new RecordParser<Contact, Store<Contact> > (
00232 new Store<Contact>(filename, false)));
00233 }
00234 else if( name == Message::GetDBName() ) {
00235 return auto_ptr<Parser>(
00236 new RecordParser<Message, Store<Message> > (
00237 new Store<Message>(filename, false)));
00238 }
00239 else if( name == Calendar::GetDBName() ) {
00240 return auto_ptr<Parser>(
00241 new RecordParser<Calendar, Store<Calendar> > (
00242 new Store<Calendar>(filename, false)));
00243 }
00244 else if( name == ServiceBook::GetDBName() ) {
00245 return auto_ptr<Parser>(
00246 new RecordParser<ServiceBook, Store<ServiceBook> > (
00247 new Store<ServiceBook>(filename, false)));
00248 }
00249
00250 else if( name == Memo::GetDBName() ) {
00251 return auto_ptr<Parser>(
00252 new RecordParser<Memo, Store<Memo> > (
00253 new Store<Memo>(filename, false)));
00254 }
00255 else if( name == Task::GetDBName() ) {
00256 return auto_ptr<Parser>(
00257 new RecordParser<Task, Store<Task> > (
00258 new Store<Task>(filename, false)));
00259 }
00260 else if( name == PINMessage::GetDBName() ) {
00261 return auto_ptr<Parser>(
00262 new RecordParser<PINMessage, Store<PINMessage> > (
00263 new Store<PINMessage>(filename, false)));
00264 }
00265 else if( name == SavedMessage::GetDBName() ) {
00266 return auto_ptr<Parser>(
00267 new RecordParser<SavedMessage, Store<SavedMessage> > (
00268 new Store<SavedMessage>(filename, false)));
00269 }
00270 else if( name == Folder::GetDBName() ) {
00271 return auto_ptr<Parser>(
00272 new RecordParser<Folder, Store<Folder> > (
00273 new Store<Folder>(filename, false)));
00274 }
00275 else if( name == Timezone::GetDBName() ) {
00276 return auto_ptr<Parser>(
00277 new RecordParser<Timezone, Store<Timezone> > (
00278 new Store<Timezone>(filename, false)));
00279 }
00280 else {
00281
00282 return auto_ptr<Parser>( new DataDumpParser );
00283 }
00284 }
00285
00286 auto_ptr<Builder> GetBuilder(const string &name, const string &filename)
00287 {
00288
00289 if( name == Contact::GetDBName() ) {
00290 return auto_ptr<Builder>(
00291 new RecordBuilder<Contact, Store<Contact> > (
00292 new Store<Contact>(filename, true)));
00293 }
00294 else if( name == Calendar::GetDBName() ) {
00295 return auto_ptr<Builder>(
00296 new RecordBuilder<Calendar, Store<Calendar> > (
00297 new Store<Calendar>(filename, true)));
00298 }
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 else {
00322 throw std::runtime_error("No Builder available for database");
00323 }
00324 }
00325
00326 void ShowParsers()
00327 {
00328 cout << "Supported Database parsers:\n"
00329 << " Address Book\n"
00330 << " Messages\n"
00331 << " Calendar\n"
00332 << " Service Book\n"
00333 << " Memos\n"
00334 << " Tasks\n"
00335 << " PIN Messages\n"
00336 << " Saved Email Messages\n"
00337 << " Folders\n"
00338 << " Time Zones (read only)\n"
00339 << "\n"
00340 << "Supported Database builders:\n"
00341 << " Address Book\n"
00342 << " Calendar\n"
00343 << endl;
00344 }
00345
00346 struct StateTableCommand
00347 {
00348 char flag;
00349 bool clear;
00350 unsigned int index;
00351
00352 StateTableCommand(char f, bool c, unsigned int i)
00353 : flag(f), clear(c), index(i) {}
00354 };
00355
00356 bool SplitMap(const string &map, string &ldif, string &read, string &write)
00357 {
00358 string::size_type a = map.find(',');
00359 if( a == string::npos )
00360 return false;
00361
00362 string::size_type b = map.find(',', a+1);
00363 if( b == string::npos )
00364 return false;
00365
00366 ldif.assign(map, 0, a);
00367 read.assign(map, a + 1, b - a - 1);
00368 write.assign(map, b + 1, map.size() - b - 1);
00369
00370 return ldif.size() && read.size() && write.size();
00371 }
00372
00373 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
00374 {
00375 for( vector<string>::const_iterator i = mapCommands.begin();
00376 i != mapCommands.end();
00377 ++i )
00378 {
00379
00380 if( i->find(',') == string::npos ) {
00381
00382 cerr << "Unmapping: " << *i << endl;
00383 ldif.Unmap(*i);
00384 }
00385 else {
00386 cerr << "Mapping: " << *i << endl;
00387
00388
00389 string ldifname, read, write;
00390 if( SplitMap(*i, ldifname, read, write) ) {
00391 if( !ldif.Map(ldifname, read, write) ) {
00392 cerr << "Read/Write name unknown: " << *i << endl;
00393 }
00394 }
00395 else {
00396 cerr << "Invalid map format: " << *i << endl;
00397 }
00398 }
00399 }
00400 }
00401
00402 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
00403 {
00404 int read, write;
00405 char comma;
00406 istringstream iss(arg);
00407 iss >> hex >> read >> comma >> write;
00408 if( !iss )
00409 return false;
00410 epp->read = read;
00411 epp->write = write;
00412 return true;
00413 }
00414
00415 int main(int argc, char *argv[])
00416 {
00417 cout.sync_with_stdio(true);
00418
00419
00420 try {
00421
00422 uint32_t pin = 0;
00423 bool list_only = false,
00424 show_dbdb = false,
00425 ldif_contacts = false,
00426 data_dump = false,
00427 reset_device = false,
00428 list_contact_fields = false,
00429 list_ldif_map = false,
00430 epp_override = false,
00431 threaded_sockets = true,
00432 record_state = false,
00433 null_parser = false;
00434 string ldifBaseDN, ldifDnAttr;
00435 string filename;
00436 string password;
00437 string busname;
00438 string devname;
00439 vector<string> dbNames, saveDbNames, mapCommands;
00440 vector<StateTableCommand> stCommands;
00441 Usb::EndpointPair epOverride;
00442
00443
00444 for(;;) {
00445 int cmd = getopt(argc, argv, "B:c:C:d:D:e:f:hlLm:MnN:p:P:r:R:Ss:tT:vXzZ");
00446 if( cmd == -1 )
00447 break;
00448
00449 switch( cmd )
00450 {
00451 case 'B':
00452 busname = optarg;
00453 break;
00454
00455 case 'c':
00456 ldif_contacts = true;
00457 ldifBaseDN = optarg;
00458 break;
00459
00460 case 'C':
00461 ldifDnAttr = optarg;
00462 break;
00463
00464 case 'd':
00465 dbNames.push_back(string(optarg));
00466 break;
00467
00468 case 'D':
00469 stCommands.push_back(
00470 StateTableCommand('D', false, atoi(optarg)));
00471 break;
00472
00473 case 'e':
00474 if( !ParseEpOverride(optarg, &epOverride) ) {
00475 Usage();
00476 return 1;
00477 }
00478 epp_override = true;
00479 break;
00480
00481 case 'f':
00482 #ifdef __BARRY_BOOST_MODE__
00483 filename = optarg;
00484 #else
00485 cerr << "-f option not supported - no Boost "
00486 "serialization support available\n";
00487 return 1;
00488 #endif
00489 break;
00490 case 'l':
00491 list_only = true;
00492 break;
00493
00494 case 'L':
00495 list_contact_fields = true;
00496 break;
00497
00498 case 'm':
00499 mapCommands.push_back(string(optarg));
00500 break;
00501
00502 case 'M':
00503 list_ldif_map = true;
00504 break;
00505
00506 case 'n':
00507 null_parser = true;
00508 break;
00509
00510 case 'N':
00511 devname = optarg;
00512 break;
00513
00514 case 'p':
00515 pin = strtoul(optarg, NULL, 16);
00516 break;
00517
00518 case 'P':
00519 password = optarg;
00520 break;
00521
00522 case 'r':
00523 stCommands.push_back(
00524 StateTableCommand('r', false, atoi(optarg)));
00525 break;
00526
00527 case 'R':
00528 stCommands.push_back(
00529 StateTableCommand('r', true, atoi(optarg)));
00530 break;
00531
00532 case 's':
00533 saveDbNames.push_back(string(optarg));
00534 break;
00535
00536 case 'S':
00537 ShowParsers();
00538 return 0;
00539
00540 case 't':
00541 show_dbdb = true;
00542 break;
00543
00544 case 'T':
00545 record_state = true;
00546 dbNames.push_back(string(optarg));
00547 break;
00548
00549 case 'v':
00550 data_dump = true;
00551 break;
00552
00553 case 'X':
00554 reset_device = true;
00555 break;
00556
00557 case 'z':
00558 threaded_sockets = false;
00559 break;
00560
00561 case 'Z':
00562 threaded_sockets = true;
00563 break;
00564
00565 case 'h':
00566 default:
00567 Usage();
00568 return 0;
00569 }
00570 }
00571
00572
00573
00574 Barry::Init(data_dump);
00575
00576
00577 ContactLdif ldif(ldifBaseDN);
00578 DoMapping(ldif, mapCommands);
00579 if( ldifDnAttr.size() ) {
00580 if( !ldif.SetDNAttr(ldifDnAttr) ) {
00581 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
00582 }
00583 }
00584
00585
00586
00587
00588 Barry::Probe probe(busname.c_str(), devname.c_str());
00589 int activeDevice = -1;
00590
00591
00592 if( probe.GetFailCount() ) {
00593 if( ldif_contacts )
00594 cout << "# ";
00595 cout << "Blackberry device errors with errors during probe:" << endl;
00596 for( int i = 0; i < probe.GetFailCount(); i++ ) {
00597 if( ldif_contacts )
00598 cout << "# ";
00599 cout << probe.GetFailMsg(i) << endl;
00600 }
00601 }
00602
00603
00604 if( ldif_contacts )
00605 cout << "# ";
00606 cout << "Blackberry devices found:" << endl;
00607 for( int i = 0; i < probe.GetCount(); i++ ) {
00608 if( ldif_contacts )
00609 cout << "# ";
00610 if( data_dump )
00611 probe.Get(i).DumpAll(cout);
00612 else
00613 cout << probe.Get(i);
00614 cout << endl;
00615 if( probe.Get(i).m_pin == pin )
00616 activeDevice = i;
00617 }
00618
00619 if( list_only )
00620 return 0;
00621
00622 if( activeDevice == -1 ) {
00623 if( pin == 0 ) {
00624
00625 if( probe.GetCount() == 1 )
00626 activeDevice = 0;
00627 else {
00628 cerr << "No device selected" << endl;
00629 return 1;
00630 }
00631 }
00632 else {
00633 cerr << "PIN " << setbase(16) << pin
00634 << " not found" << endl;
00635 return 1;
00636 }
00637 }
00638
00639 if( ldif_contacts )
00640 cout << "# ";
00641 cout << "Using device (PIN): " << setbase(16)
00642 << probe.Get(activeDevice).m_pin << endl;
00643
00644 if( reset_device ) {
00645 Usb::Device dev(probe.Get(activeDevice).m_dev);
00646 dev.Reset();
00647 return 0;
00648 }
00649
00650
00651 Barry::ProbeResult device = probe.Get(activeDevice);
00652 if( epp_override ) {
00653 device.m_ep.read = epOverride.read;
00654 device.m_ep.write = epOverride.write;
00655 device.m_ep.type = 2;
00656 cout << "Endpoint pair (read,write) overridden with: "
00657 << hex
00658 << (unsigned int) device.m_ep.read << ","
00659 << (unsigned int) device.m_ep.write << endl;
00660 }
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672 auto_ptr<SocketRoutingQueue> router;
00673 auto_ptr<Barry::Controller> pcon;
00674 if( threaded_sockets ) {
00675 router.reset( new SocketRoutingQueue );
00676 router->SpinoffSimpleReadThread();
00677 pcon.reset( new Barry::Controller(device, *router) );
00678 }
00679 else {
00680 pcon.reset( new Barry::Controller(device) );
00681 }
00682
00683 Barry::Controller &con = *pcon;
00684 Barry::Mode::Desktop desktop(con);
00685
00686
00687
00688
00689
00690
00691
00692 if( show_dbdb ) {
00693
00694 desktop.Open(password.c_str());
00695 cout << desktop.GetDBDB() << endl;
00696 }
00697
00698
00699 if( list_contact_fields ) {
00700 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
00701 cout.fill(' ');
00702 cout << " " << left << setw(20) << n->name << ": "
00703 << n->description << endl;
00704 }
00705 }
00706
00707
00708 if( list_ldif_map ) {
00709 cout << ldif << endl;
00710 }
00711
00712
00713
00714 if( ldif_contacts ) {
00715
00716 desktop.Open(password.c_str());
00717
00718
00719
00720 Contact2Ldif storage(ldif);
00721
00722
00723 desktop.LoadDatabaseByType<Barry::Contact>(storage);
00724 }
00725
00726
00727 if( record_state ) {
00728 if( dbNames.size() == 0 ) {
00729 cout << "No db names to process" << endl;
00730 return 1;
00731 }
00732
00733 desktop.Open(password.c_str());
00734
00735 vector<string>::iterator b = dbNames.begin();
00736 for( ; b != dbNames.end(); b++ ) {
00737 unsigned int id = desktop.GetDBID(*b);
00738 RecordStateTable state;
00739 desktop.GetRecordStateTable(id, state);
00740 cout << "Record state table for: " << *b << endl;
00741 cout << state;
00742 }
00743 return 0;
00744 }
00745
00746
00747 if( stCommands.size() ) {
00748 if( dbNames.size() != 1 ) {
00749 cout << "Must have 1 db name to process" << endl;
00750 return 1;
00751 }
00752
00753 desktop.Open(password.c_str());
00754 unsigned int id = desktop.GetDBID(dbNames[0]);
00755 auto_ptr<Parser> parse = GetParser(dbNames[0],filename,null_parser);
00756
00757 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
00758 desktop.GetRecord(id, stCommands[i].index, *parse.get());
00759
00760 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
00761 cout << "Clearing record's dirty flags..." << endl;
00762 desktop.ClearDirty(id, stCommands[i].index);
00763 }
00764
00765 if( stCommands[i].flag == 'D' ) {
00766 desktop.DeleteRecord(id, stCommands[i].index);
00767 }
00768 }
00769
00770 return 0;
00771 }
00772
00773
00774
00775
00776 if( dbNames.size() ) {
00777 vector<string>::iterator b = dbNames.begin();
00778
00779 desktop.Open(password.c_str());
00780 for( ; b != dbNames.end(); b++ ) {
00781 auto_ptr<Parser> parse = GetParser(*b,filename,null_parser);
00782 unsigned int id = desktop.GetDBID(*b);
00783 desktop.LoadDatabase(id, *parse.get());
00784 }
00785 }
00786
00787
00788
00789 if( saveDbNames.size() ) {
00790 vector<string>::iterator b = saveDbNames.begin();
00791
00792 desktop.Open(password.c_str());
00793 for( ; b != saveDbNames.end(); b++ ) {
00794 auto_ptr<Builder> build =
00795 GetBuilder(*b, filename);
00796 unsigned int id = desktop.GetDBID(*b);
00797 desktop.SaveDatabase(id, *build);
00798 }
00799 }
00800
00801 }
00802 catch( Usb::Error &ue) {
00803 std::cerr << "Usb::Error caught: " << ue.what() << endl;
00804 return 1;
00805 }
00806 catch( Barry::Error &se ) {
00807 std::cerr << "Barry::Error caught: " << se.what() << endl;
00808 return 1;
00809 }
00810 catch( std::exception &e ) {
00811 std::cerr << "std::exception caught: " << e.what() << endl;
00812 return 1;
00813 }
00814
00815 return 0;
00816 }
00817