btool.cc

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

Generated on Tue Jun 30 16:08:13 2009 for Barry by  doxygen 1.5.8