omadrm/drmengine/dcfrepository/client/src/DcfRepCli.cpp
changeset 0 95b198f216e5
child 84 b09186059647
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2002-2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Client implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "DcfRepCli.h"
       
    22 #include    "DcfRepCommon.h"
       
    23 #include    <e32std.h>
       
    24 #include    <e32base.h>
       
    25 
       
    26 // EXTERNAL DATA STRUCTURES
       
    27 // EXTERNAL FUNCTION PROTOTYPES  
       
    28 // CONSTANTS
       
    29 LOCAL_C const TUint KDefaultMessageSlots = 4;
       
    30 LOCAL_C const TInt32 KServerUid3 = 0x10205CA8;
       
    31 // MACROS
       
    32 // LOCAL CONSTANTS AND MACROS
       
    33 // MODULE DATA STRUCTURES
       
    34 // LOCAL FUNCTION PROTOTYPES
       
    35 // FORWARD DECLARATIONS
       
    36 
       
    37 
       
    38 //#define _DRM_TESTING
       
    39 
       
    40 
       
    41 
       
    42 // ============================= LOCAL FUNCTIONS ===============================
       
    43 // ============================ LOCAL FUNCTIONS ===============================
       
    44 #ifdef _DRM_TESTING
       
    45 LOCAL_C void WriteLogL( const TDesC8& text , RFs &aFs );
       
    46 LOCAL_C void WriteFileL( const TDesC8& text , RFs &aFs , const TDesC& aName );
       
    47 LOCAL_C void CreateLogL();
       
    48 LOCAL_C void WriteL( const TDesC8& aText );
       
    49 LOCAL_C void WriteL( const TDesC8& aText , TInt aErr );
       
    50 LOCAL_C void WriteCurrentTimeL();
       
    51 #endif
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // Testing stuff
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 #ifdef _DRM_TESTING
       
    58 LOCAL_C void WriteLogL( const TDesC8& text , RFs &aFs )
       
    59     {
       
    60     _LIT( KLogFile , "c:\\RDcfRepCli.txt" );
       
    61     WriteFileL( text , aFs , KLogFile );
       
    62     }
       
    63 
       
    64 LOCAL_C void WriteFileL( const TDesC8& text , RFs &aFs , const TDesC& aName )
       
    65     {
       
    66     RFile file;
       
    67     TInt size;
       
    68     User::LeaveIfError( file.Open( aFs, aName , EFileWrite ) );
       
    69     CleanupClosePushL( file );
       
    70     User::LeaveIfError( file.Size( size ) );
       
    71     User::LeaveIfError( file.Write( size, text ) );
       
    72     CleanupStack::PopAndDestroy(&file); //file
       
    73     }
       
    74 
       
    75 LOCAL_C void CreateLogL()
       
    76     {
       
    77     RFs fs;
       
    78     User::LeaveIfError(fs.Connect());
       
    79     CleanupClosePushL(fs);
       
    80     RFile file;
       
    81     User::LeaveIfError( file.Replace( fs , _L("c:\\RDcfRepCli.txt") , EFileWrite ) );
       
    82     file.Close();
       
    83     CleanupStack::PopAndDestroy(&fs); //fs
       
    84     }
       
    85 
       
    86 LOCAL_C void WriteL( const TDesC8& aText )
       
    87     {
       
    88     RFs fs;
       
    89     User::LeaveIfError( fs.Connect() );
       
    90     CleanupClosePushL(fs);
       
    91     HBufC8* text = HBufC8::NewLC(1000);
       
    92     TPtr8 textptr(text->Des() );
       
    93     textptr.Append( aText );
       
    94     textptr.Append( _L8("\r\n") );
       
    95     WriteLogL(textptr , fs);
       
    96     CleanupStack::PopAndDestroy(text);
       
    97     CleanupStack::PopAndDestroy(&fs); //fs
       
    98     WriteCurrentTimeL();
       
    99     }
       
   100     
       
   101 LOCAL_C void WriteL( const TDesC8& aText , TInt aErr )
       
   102     {
       
   103     _LIT8(KErr,": %d");
       
   104     HBufC8* text = HBufC8::NewLC(1000+20);
       
   105     TBuf8<20> num;
       
   106     TPtr8 textptr(text->Des());
       
   107     textptr.Append(aText);
       
   108     num.Format(KErr(),aErr);
       
   109     textptr.Append(num);
       
   110     WriteL(textptr);
       
   111     CleanupStack::PopAndDestroy(text);
       
   112     }
       
   113 
       
   114 LOCAL_C void WriteCurrentTimeL()
       
   115     {
       
   116     RFs fs;
       
   117     User::LeaveIfError( fs.Connect() );
       
   118     CleanupClosePushL(fs);
       
   119     HBufC8* text = HBufC8::NewLC(100);
       
   120     TPtr8 textptr(text->Des() );
       
   121 // Date and Time display
       
   122     TTime time;
       
   123     time.HomeTime();
       
   124     TBuf<256> dateString;
       
   125     _LIT(KDate,"%*E%*D%X%*N%*Y %1 %2 '%3");
       
   126     time.FormatL(dateString,KDate);
       
   127     textptr.Append(_L( "\r\n\t\tData:\t" ) );
       
   128     textptr.Append( dateString );
       
   129     _LIT(KTime,"%-B%:0%J%:1%T%:2%S%:3%+B");
       
   130     time.FormatL(dateString,KTime);
       
   131     textptr.Append(_L( "\r\n\t\tTime:\t" ) );
       
   132     textptr.Append( dateString );
       
   133     textptr.Append(_L( "\r\n" ) );
       
   134     textptr.Append(_L( "\r\n" ) );
       
   135     WriteLogL(textptr , fs);
       
   136     CleanupStack::PopAndDestroy(text);
       
   137     CleanupStack::PopAndDestroy(&fs); //fs
       
   138     }
       
   139 #endif
       
   140 
       
   141 
       
   142 
       
   143 
       
   144 
       
   145 
       
   146 LOCAL_C TInt StartServer();
       
   147 LOCAL_C TInt CreateServerProcess();
       
   148 // ============================ MEMBER FUNCTIONS ===============================
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // RDcfRepCli::RDcfRepCli
       
   152 // C++ default constructor can NOT contain any code, that
       
   153 // might leave.
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 EXPORT_C RDcfRepCli::RDcfRepCli()
       
   157     {
       
   158 #ifdef _DRM_TESTING
       
   159     TRAPD(r,CreateLogL());
       
   160 #endif
       
   161     }
       
   162 
       
   163     
       
   164 // Destructor
       
   165 EXPORT_C RDcfRepCli::~RDcfRepCli()
       
   166     {
       
   167 #ifdef _DRM_TESTING
       
   168     TRAPD(r,WriteL(_L8("~RDcfRepCli")));
       
   169 #endif
       
   170     }
       
   171 
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // RDcfRepCli::AddFile
       
   175 // add an entry into database
       
   176 // (other items were commented in a header).
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 EXPORT_C TInt RDcfRepCli::AddFile(const TDesC& aFile)
       
   180     {
       
   181 #ifdef _DRM_TESTING
       
   182     TRAPD(r,WriteL(_L8("AddFile")));
       
   183 #endif
       
   184 
       
   185     TInt err = 0;
       
   186     err = SendReceive( EAddFile,TIpcArgs( &aFile ) );
       
   187     return err;    
       
   188     }
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // RDcfRepCli::Connect
       
   192 // Connect to DCF Repository Server
       
   193 // (other items were commented in a header).
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 EXPORT_C TInt RDcfRepCli::Connect()
       
   197     {
       
   198 #ifdef _DRM_TESTING
       
   199     TRAPD(r,WriteL(_L8("Connect")));
       
   200 #endif
       
   201 
       
   202     TInt error = StartServer();
       
   203 
       
   204     if (KErrNone == error)
       
   205         {
       
   206 
       
   207         error = CreateSession(KDcfRepSrvName,
       
   208                               Version(),
       
   209                               KDefaultMessageSlots);
       
   210         }
       
   211     return error;
       
   212     }
       
   213     
       
   214 // -----------------------------------------------------------------------------
       
   215 // RDcfRepCli::Close
       
   216 // Close session to server
       
   217 // (other items were commented in a header).
       
   218 // -----------------------------------------------------------------------------
       
   219 //
       
   220 EXPORT_C void RDcfRepCli::Close()
       
   221     {
       
   222 #ifdef _DRM_TESTING
       
   223     TRAPD(r,WriteL(_L8("Close")));
       
   224 #endif
       
   225 
       
   226     RHandleBase::Close();    
       
   227     }
       
   228 
       
   229 // -----------------------------------------------------------------------------
       
   230 // RDcfRepCli::Version
       
   231 // 
       
   232 // return server version
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 EXPORT_C TVersion RDcfRepCli::Version() const
       
   236     {
       
   237 #ifdef _DRM_TESTING
       
   238     TRAPD(r,WriteL(_L8("Version")));
       
   239 #endif
       
   240 
       
   241     return(TVersion(KDcfRepMajorVersionNumber,
       
   242                     KDcfRepMinorVersionNumber,
       
   243                     KDcfRepBuildVersionNumber));
       
   244     }
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // RDcfRepCli::ScanDcf
       
   248 // triger scaning for file system
       
   249 // (other items were commented in a header).
       
   250 // -----------------------------------------------------------------------------
       
   251 //
       
   252 EXPORT_C void RDcfRepCli::ScanDcf(TRequestStatus& aStatus)
       
   253     {
       
   254 #ifdef _DRM_TESTING
       
   255     TRAPD(r,WriteL(_L8("ScanDcf(1)")));
       
   256 #endif
       
   257 
       
   258     SendReceive( EFullScan , aStatus );    
       
   259     }
       
   260     
       
   261 // -----------------------------------------------------------------------------
       
   262 // RDcfRepCli::ScanDcf
       
   263 // triger scaning for file system
       
   264 // (other items were commented in a header).
       
   265 // -----------------------------------------------------------------------------
       
   266 //
       
   267 EXPORT_C void RDcfRepCli::ScanDcf(RPointerArray<HBufC8>& aList , TRequestStatus& aStatus)
       
   268     {
       
   269 #ifdef _DRM_TESTING
       
   270     TRAPD(r,WriteL(_L8("ScanDcf(2)")));
       
   271 #endif
       
   272 
       
   273     TInt err = 0;
       
   274     TInt i = 0;
       
   275     TRequestStatus* status = &aStatus;
       
   276     err = SendReceive( EScanStart );
       
   277     if (err)
       
   278         {
       
   279         User::RequestComplete( status , err );
       
   280         return;
       
   281         }
       
   282     for (;i<aList.Count();i++)
       
   283         {
       
   284         err = SendReceive( EScanAdd,TIpcArgs(aList[i]) );
       
   285         if (err)
       
   286             {
       
   287             User::RequestComplete( status , err );
       
   288             return;
       
   289             }    
       
   290         }
       
   291     SendReceive(EScanEnd , aStatus);
       
   292     }    
       
   293 // -----------------------------------------------------------------------------
       
   294 // RDcfRepCli::SetTtid
       
   295 // Set transaction ID to all Dcfs associated with a specific CID
       
   296 // (other items were commented in a header).
       
   297 // -----------------------------------------------------------------------------
       
   298 //
       
   299 EXPORT_C void RDcfRepCli::SetTtid( 
       
   300             RArray<TPair>& aList ,
       
   301             TRequestStatus& aStatus
       
   302             )
       
   303     {
       
   304 #ifdef _DRM_TESTING
       
   305     TRAPD(r,WriteL(_L8("SetTtid")));
       
   306 #endif
       
   307 
       
   308     TInt err = 0;
       
   309     TInt i = 0;
       
   310     TRequestStatus* status = &aStatus;
       
   311     err = SendReceive( ESetTtidStart );
       
   312     if (err)
       
   313         {
       
   314         User::RequestComplete( status , err );
       
   315         return;
       
   316         }
       
   317     for (;i<aList.Count();i++)
       
   318         {
       
   319         if ( aList[i].iCid && aList[i].iTtid != NULL )
       
   320             {
       
   321             err = SendReceive( ESetTtidAdd,TIpcArgs(aList[i].iCid,aList[i].iTtid) );    
       
   322             }
       
   323         if (err)
       
   324             {
       
   325             User::RequestComplete( status , err );
       
   326             return;
       
   327             }
       
   328         }
       
   329     SendReceive(ESetTtidEnd , aStatus);
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // RDcfRepCli::NotifyServerIdle
       
   334 // notify if server is in idle.
       
   335 // (other items were commented in a header).
       
   336 // -----------------------------------------------------------------------------
       
   337 //
       
   338 EXPORT_C void RDcfRepCli::NotifyServerIdle( TRequestStatus& aStatus )
       
   339     {
       
   340 #ifdef _DRM_TESTING
       
   341     TRAPD(r,WriteL(_L8("NotifyServerIdle")));
       
   342 #endif
       
   343 
       
   344     SendReceive( ENotifyIdle , aStatus );        
       
   345     }
       
   346 
       
   347 // -----------------------------------------------------------------------------
       
   348 // RDcfRepCli::NotifyServerIdle
       
   349 // notify if server is in idle.
       
   350 // (other items were commented in a header).
       
   351 // -----------------------------------------------------------------------------
       
   352 //
       
   353 EXPORT_C void RDcfRepCli::StopWatching()
       
   354     {
       
   355 #ifdef _DRM_TESTING
       
   356     TRAPD(r,WriteL(_L8("StopWatchingL")));
       
   357 #endif
       
   358 
       
   359     SendReceive( EStopWatching );
       
   360     }
       
   361 
       
   362 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   363 
       
   364 // -----------------------------------------------------------------------------
       
   365 // StartServer
       
   366 // Start the server 
       
   367 // (other items were commented in a header).
       
   368 // -----------------------------------------------------------------------------
       
   369 //
       
   370 LOCAL_C TInt StartServer()
       
   371     {
       
   372 #ifdef _DRM_TESTING
       
   373     TRAPD(r,WriteL(_L8("StartServer")));
       
   374 #endif    
       
   375     
       
   376     TInt result = 0;
       
   377 
       
   378     TFindServer findDcfRepSrv(KDcfRepSrvName);
       
   379     TFullName name;
       
   380 
       
   381     result = findDcfRepSrv.Next(name);
       
   382     if (result == KErrNone)
       
   383         {
       
   384         
       
   385 #ifdef _DRM_TESTING
       
   386     TRAPD(r,WriteL(_L8("StartServer->Server already running")));
       
   387 #endif  
       
   388         // Server already running
       
   389         return KErrNone;
       
   390         }
       
   391 
       
   392     RSemaphore semaphore;       
       
   393     result = semaphore.CreateGlobal(KDcfRepSemaphoreName, 0);
       
   394     if (result != KErrNone)
       
   395         {
       
   396         
       
   397 #ifdef _DRM_TESTING
       
   398     TRAPD(r,WriteL(_L8("StartServer->CreateSemaphore"),result));
       
   399 #endif
       
   400 
       
   401         return  result;
       
   402         }
       
   403 
       
   404     result = CreateServerProcess();
       
   405     if (result != KErrNone)
       
   406         {
       
   407         
       
   408 #ifdef _DRM_TESTING
       
   409     TRAPD(r,WriteL(_L8("StartServer->CreateServerProcess"),result));
       
   410 #endif
       
   411 
       
   412         semaphore.Close(); 
       
   413         return  result;
       
   414         }
       
   415 
       
   416     semaphore.Wait();
       
   417     semaphore.Close();       
       
   418 
       
   419     return  KErrNone;
       
   420     }
       
   421 
       
   422 
       
   423 // -----------------------------------------------------------------------------
       
   424 // CreateServerProcess
       
   425 // Create server process 
       
   426 // (other items were commented in a header).
       
   427 // -----------------------------------------------------------------------------
       
   428 //
       
   429 LOCAL_C TInt CreateServerProcess()
       
   430     {
       
   431 #ifdef _DRM_TESTING
       
   432     TRAPD(r,WriteL(_L8("CreateServerProcess")));
       
   433 #endif    
       
   434     TInt result;
       
   435     TUid id;
       
   436     id.iUid = KServerUid3;
       
   437     const TUidType serverUid(KNullUid, KNullUid, id );
       
   438 
       
   439 
       
   440     RProcess server;
       
   441     result = server.Create( KDcfRepSrvFileName, KNullDesC, serverUid);
       
   442     if (result != KErrNone)
       
   443         {
       
   444 #ifdef _DRM_TESTING
       
   445     TRAPD(r,WriteL(_L8("CreateServerProcess->server.Create:"),result));
       
   446 #endif 
       
   447         return  result;
       
   448         }
       
   449     server.Resume();
       
   450     server.Close();
       
   451 
       
   452     return  KErrNone;
       
   453     }
       
   454 
       
   455 
       
   456 
       
   457 
       
   458 //  End of File