sl@0: /* sl@0: * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Defines a tool to export/import UPS Decision Database sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: @test sl@0: */ sl@0: sl@0: #ifndef DUMPUPSDB_H sl@0: #define DUMPUPSDB_H sl@0: sl@0: #include "upsdbw.h" sl@0: #include "activewaiter.h" sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: using namespace UserPromptService; sl@0: sl@0: sl@0: _LIT(KAppName, "UPS Decision Database Dump Tool"); sl@0: sl@0: //Set flag values sl@0: static const TUint16 KLocClientSid = 0x0001; sl@0: static const TUint16 KLocEvaluatorId = 0x0002; sl@0: static const TUint16 KLocServiceId = 0x0004; sl@0: static const TUint16 KLocServerSid = 0x0008; sl@0: static const TUint16 KLocFingerprint = 0x0010; sl@0: static const TUint16 KLocClientEntity = 0x0020; sl@0: static const TUint16 KLocDescription = 0x0040; sl@0: static const TUint16 KLocResult = 0x0080; sl@0: static const TUint16 KLocEvaluatorInfo = 0x0100; sl@0: static const TUint16 KLocMajorPolicyVersion = 0x0200; sl@0: static const TUint16 KLocRecordId = 0x0400; sl@0: sl@0: // sl@0: //CPrinter sl@0: // sl@0: sl@0: class CPrinter : public CBase sl@0: /** sl@0: Class that prints text to the console and optionally writes to a log file. sl@0: */ sl@0: { sl@0: public: sl@0: static CPrinter* NewLC(CConsoleBase* aConsole); sl@0: static CPrinter* NewLC(CConsoleBase* aConsole, RFile& aFile); sl@0: static void Usage(CConsoleBase* aConsole); sl@0: sl@0: ~CPrinter(); sl@0: sl@0: void PrintfL(TRefByValue aFormat, ...); sl@0: void Printf8L(TRefByValue aFormat, ...); sl@0: void PrintOnlyConsoleL(const TDesC16& aFormat, ...); sl@0: void Wait(); sl@0: void ReadNextLineL(TDes8& aLine); sl@0: TInt FileSizeL(); sl@0: sl@0: private : sl@0: CPrinter(CConsoleBase* aConsole); sl@0: void ConstructL(RFile& aFile); sl@0: sl@0: private: sl@0: /** Console object to print text to the console*/ sl@0: CConsoleBase* iConsole; sl@0: sl@0: /** Optional file handle to write text to the file*/ sl@0: RFile iFile; sl@0: sl@0: /** Whether to log to the output to the file */ sl@0: TBool iLogToFile; sl@0: sl@0: /** Temporary 16-bit buffer*/ sl@0: TBuf<256> iBuffer; sl@0: sl@0: /** Temporary 8-bit buffer*/ sl@0: TBuf8<256> iBuffer8; sl@0: sl@0: /** File stream handle*/ sl@0: RFileReadStream iReader; sl@0: }; sl@0: sl@0: // sl@0: //CDatabase sl@0: // sl@0: sl@0: class CDatabase : public CBase sl@0: /** sl@0: This class connects to the database and import/exports it to the console/file sl@0: */ sl@0: { sl@0: public: sl@0: static CDatabase* NewLC(CConsoleBase* aConsole, RFs& aFs, TBool aImport, const TDesC& aDb, const TDesC& aFile); sl@0: sl@0: ~CDatabase(); sl@0: sl@0: void DumpL(); sl@0: sl@0: private: sl@0: CDatabase(TBool aImport); sl@0: void ConstructL(CConsoleBase* aConsole, RFs& aFs, const TDesC& aDb, const TDesC& aFile); sl@0: sl@0: void PrintDecisionL(CDecisionRecord& aRecord); sl@0: void PrintHeaderL(); sl@0: CDecisionRecord* ParseAndCreateRecordLC(TDesC8& aLine); sl@0: TUint8 HexToIntL(TUint8* aPtr); sl@0: void HexToStrL(TDes8& aSource); sl@0: void PrintTestResultsL(); sl@0: void PrintTestHeaderL(); sl@0: sl@0: private: sl@0: /** Indicates the operation type: Import Db is ETrue and Export Db is EFalse*/ sl@0: TBool iImport; sl@0: sl@0: /** Handle to the decision database*/ sl@0: CDecisionDbW* iUpsDb; sl@0: sl@0: /** Handle to the printer object*/ sl@0: CPrinter* iPrinter; sl@0: sl@0: /** Optional file handle to write text to the file*/ sl@0: RFile iFile; sl@0: sl@0: public: sl@0: /** Enumerated values to arrange run options. */ sl@0: enum TDbOptions sl@0: { sl@0: /** Indicates whether the dummy test result lines are written into the output file. */ sl@0: EAppendTestResults = 0x01, sl@0: /** Indicates whether the command line waits for the user input at the end of the operation.*/ sl@0: EDoNotStop = 0x02, sl@0: /** Indicates that fingerprint and client entity fields are printed as binary instead hexadecimal. */ sl@0: EPrintBinary = 0x04 sl@0: }; sl@0: sl@0: TUint8 iFlag; ///< A flag for running options. sl@0: }; sl@0: sl@0: sl@0: static void MainL(void) sl@0: /** sl@0: Takes a User Prompt Service decision database file and dumps it as human readable text to the sl@0: console. The user may also specify the name of an output file on the command line. If so, text sl@0: is also written to this file. Also recreates a UPS decision database from a dumped file. Where sl@0: the database is created is specified by the user. sl@0: */ sl@0: { sl@0: sl@0: RFs fs; sl@0: sl@0: User::LeaveIfError(fs.Connect()); sl@0: CleanupClosePushL(fs); sl@0: sl@0: CConsoleBase *console = Console::NewL(KAppName,TSize(KDefaultConsWidth, KDefaultConsHeight)); sl@0: CleanupStack::PushL(console); sl@0: sl@0: CCommandLineArguments *cmdLineArgs = CCommandLineArguments::NewLC(); sl@0: sl@0: TInt argc; sl@0: argc = cmdLineArgs->Count(); sl@0: sl@0: //At least operation type and database file path must be given sl@0: if(argc < 4) sl@0: { sl@0: CPrinter::Usage(console); sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: TInt loopCount(1); sl@0: TBool import(EFalse); sl@0: TUint16 *dbPath(0); sl@0: TInt dbPathLen(0); sl@0: TUint16 *filePath(0); sl@0: TInt filePathLen(0); sl@0: TUint8 flag(0); sl@0: sl@0: while(loopCount+1 <= argc) sl@0: { sl@0: TPtrC argv = cmdLineArgs->Arg(loopCount); sl@0: sl@0: if(argv.Locate('-')) sl@0: { sl@0: break; sl@0: } sl@0: sl@0: switch(argv.Ptr()[1]) sl@0: { sl@0: case 'i': sl@0: case 'I': sl@0: {//the operation type is import sl@0: import = ETrue; sl@0: break; sl@0: } sl@0: case 'e': sl@0: case 'E': sl@0: {//the operation type is export sl@0: import = EFalse; sl@0: break; sl@0: } sl@0: case 'd': sl@0: case 'D': sl@0: {//the decision database file path sl@0: TPtrC path = cmdLineArgs->Arg(++loopCount); sl@0: dbPath = (TUint16 *)path.Ptr(); sl@0: dbPathLen = path.Length(); sl@0: break; sl@0: } sl@0: case 'f': sl@0: case 'F': sl@0: {//the file which the database will be dumped to or populated from sl@0: TPtrC path = cmdLineArgs->Arg(++loopCount); sl@0: filePath = (TUint16 *)path.Ptr(); sl@0: filePathLen = path.Length(); sl@0: break; sl@0: } sl@0: case 's': sl@0: case 'S': sl@0: {//This parameter is not published in the help page sl@0: //It's been added to get rid of waiting at the end of the program. sl@0: flag |= CDatabase::EDoNotStop; sl@0: break; sl@0: } sl@0: case 't': sl@0: case 'T': sl@0: {//This parameter is not published in the help page sl@0: //It's been added to get rid of parsing error in the test environment. sl@0: flag |= CDatabase::EAppendTestResults; sl@0: break; sl@0: } sl@0: case 'b': sl@0: case 'B': sl@0: {//This parameter is not published in the help page sl@0: //It's been added to print fields as binary instead hexadecimal. sl@0: flag |= CDatabase::EPrintBinary; sl@0: break; sl@0: } sl@0: default: sl@0: break; sl@0: }//switch sl@0: sl@0: ++loopCount; sl@0: sl@0: }//while loop sl@0: sl@0: if(loopCount != argc || !dbPath || (import && !filePath)) sl@0: { sl@0: CPrinter::Usage(console); sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: TPtr db(dbPath,dbPathLen); sl@0: db.SetLength(dbPathLen); sl@0: sl@0: CDatabase* database = NULL; sl@0: sl@0: if(filePath) sl@0: { sl@0: TPtr file(filePath,filePathLen); sl@0: file.SetLength(filePathLen); sl@0: database = CDatabase::NewLC(console, fs, import, db, file); sl@0: } sl@0: else sl@0: { sl@0: database = CDatabase::NewLC(console, fs, import, db, KNullDesC); sl@0: } sl@0: sl@0: database->iFlag = flag; sl@0: sl@0: //Do actual import or export database operation sl@0: database->DumpL(); sl@0: sl@0: CleanupStack::PopAndDestroy(4, &fs); sl@0: } sl@0: sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: CTrapCleanup *dumpupsdbCleanup = CTrapCleanup::New(); sl@0: if(dumpupsdbCleanup == NULL) sl@0: { sl@0: return KErrNoMemory; sl@0: } sl@0: sl@0: CActiveScheduler* dumpupsdbScheduler = new(ELeave) CActiveScheduler; sl@0: CActiveScheduler::Install(dumpupsdbScheduler); sl@0: sl@0: __UHEAP_MARK; sl@0: sl@0: TRAPD(retval,MainL()); sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: delete dumpupsdbCleanup; sl@0: delete dumpupsdbScheduler; sl@0: sl@0: if(retval == KErrArgument) sl@0: { sl@0: return KErrNone; sl@0: } sl@0: return retval; sl@0: } sl@0: sl@0: #endif