wim/Scard/src/ScardServer.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2003 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:  Smart card server
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "ScardBase.h"
       
    22 #include    "ScardClsv.h"
       
    23 #include    "ScardServerBase.h"
       
    24 #include    "ScardServer.h"
       
    25 #include    "WimTrace.h"
       
    26 
       
    27 // MODULE DATA STRUCTURES
       
    28 struct TThreadParameter
       
    29     {
       
    30     RSemaphore  iSemaphore;
       
    31     TInt        iPanicCode;
       
    32     };
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CScardServer::CScardServer
       
    38 // C++ default constructor can NOT contain any code, that
       
    39 // might leave.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CScardServer::CScardServer()
       
    43     : CServer2( EPriorityStandard )
       
    44     {
       
    45     _WIMTRACE(_L("WIM|Scard|CScardServer::CScardServer|Begin"));
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CScardServer::ConstructL
       
    50 // Symbian 2nd phase constructor can leave.
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 void CScardServer::ConstructL()
       
    54     {
       
    55     _WIMTRACE(_L("WIM|Scard|CScardServer::ConstructL|Begin"));
       
    56     iNotifyRegistry = CScardNotifyRegistry::NewL( this );
       
    57     iControlRegistry = CScardAccessControlRegistry::NewL( this );
       
    58     iReaderFactoryRegistry = CScardReaderRegistry::NewL( this );
       
    59     iResourceRegistry = CScardResourceRegistry::NewL( this );
       
    60     iConnectionRegistry = CScardConnectionRegistry::NewL( this );
       
    61     }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CScardServer::NewL
       
    65 // Two-phased constructor.
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 CScardServer* CScardServer::NewL()
       
    69     {
       
    70     _WIMTRACE(_L("WIM|Scard|CScardServer::NewL|Begin"));
       
    71     CScardServer* self = new( ELeave ) CScardServer;
       
    72     
       
    73     CleanupStack::PushL( self );
       
    74     self->ConstructL();
       
    75     CleanupStack::Pop( self );
       
    76 
       
    77     TPtrC tempName;
       
    78     tempName.Set( KScardServerName );
       
    79     HBufC* pName = tempName.Alloc();
       
    80     //Again these panics EPR
       
    81     __ASSERT_ALWAYS( pName,
       
    82         PanicServer( KScServerPanicSvrCreateServer ) );
       
    83     
       
    84     self->iName = pName; // Ownership moves
       
    85 
       
    86     // Start the server
       
    87     TPtr name = self->iName->Des();     
       
    88     TInt r = self->Start( name );
       
    89     __ASSERT_ALWAYS( r == KErrNone,
       
    90         PanicServer( KScServerPanicSvrCreateServer ) );
       
    91 
       
    92     return self;
       
    93     }
       
    94 
       
    95     
       
    96 // Destructor
       
    97 CScardServer::~CScardServer()
       
    98     {
       
    99     _WIMTRACE(_L("WIM|Scard|CScardServer::~CScardServer|Begin"));
       
   100     delete iNotifyRegistry;
       
   101     delete iControlRegistry;
       
   102     delete iReaderFactoryRegistry;
       
   103     delete iResourceRegistry;
       
   104     delete iConnectionRegistry;
       
   105     }
       
   106 
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CScardServer::PanicServer
       
   110 // Panics the server
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 void CScardServer::PanicServer( const TInt aPanic )
       
   114     {
       
   115     _WIMTRACE(_L("WIM|Scard|CScardServer::PanicServer|Begin"));
       
   116     User::Panic( _L( "ScardServer" ), aPanic );
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // CScardServer::NewSessionL
       
   121 // Create a new client session for this server.
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 CSession2* CScardServer::NewSessionL(
       
   125     const TVersion& aVersion, const RMessage2& /*aMessage*/ ) const
       
   126     {
       
   127     _WIMTRACE(_L("WIM|Scard|CScardServer::NewSessionL|Begin"));
       
   128     // check version is ok
       
   129     TVersion v( KScardServMajorVersionNumber, KScardServMinorVersionNumber, 
       
   130         KScardServBuildVersionNumber );
       
   131     if ( !User::QueryVersionSupported( v, aVersion ) )
       
   132         {
       
   133         User::Leave( KErrNotSupported );
       
   134         }
       
   135 
       
   136     // make new session
       
   137     return CScardSession::NewL( const_cast< CScardServer* >( this ) );
       
   138     }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // CScardServer::ThreadFunction
       
   142 // Start active scheduler and do other new thread things
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 GLDEF_C TInt CScardServer::ThreadFunction( TAny* aStarted )
       
   146     {
       
   147     _WIMTRACE(_L("WIM|Scard|CScardServer::ThreadFunction|Begin"));
       
   148     //  convert argument to initial data
       
   149     TThreadParameter& data = *(static_cast< TThreadParameter* >( aStarted ) );
       
   150     RSemaphore& started = data.iSemaphore;
       
   151 
       
   152     data.iPanicCode = KErrNone;
       
   153     
       
   154     // create cleanup stack
       
   155     if ( !CTrapCleanup::New() )
       
   156         {
       
   157         data.iPanicCode = KScServerPanicCreateTrapCleanup;
       
   158         started.Signal();
       
   159         PanicServer( KScServerPanicCreateTrapCleanup );
       
   160         }
       
   161 
       
   162     // construct and install active scheduler
       
   163     if ( !CActiveScheduler::Current() ) //Check if scheduler already started
       
   164         {
       
   165         CActiveScheduler* scheduler = new CActiveScheduler;
       
   166         if ( !scheduler )
       
   167             {
       
   168             data.iPanicCode = KScServerPanicMainSchedulerError;
       
   169             started.Signal();
       
   170             PanicServer( KScServerPanicMainSchedulerError );
       
   171             }
       
   172         CActiveScheduler::Install( scheduler );
       
   173         }
       
   174     // construct server, an active object
       
   175     TRAPD( err, CScardServer::NewL() );
       
   176 
       
   177     if ( err )
       
   178         {
       
   179         data.iPanicCode = KScServerPanicSvrCreateServer;
       
   180         started.Signal();
       
   181         PanicServer( KScServerPanicSvrCreateServer );
       
   182         }
       
   183 
       
   184     // signal everything has started
       
   185     started.Signal(); // now started ok
       
   186 
       
   187     // start handling requests
       
   188     CActiveScheduler::Start();
       
   189     return KErrNone;
       
   190     }
       
   191 
       
   192 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // StartThread Starts the server thread.
       
   196 // Returns: TInt:   error code
       
   197 // -----------------------------------------------------------------------------
       
   198 //
       
   199 EXPORT_C TInt StartThread()
       
   200     {
       
   201     _WIMTRACE(_L("WIM|Scard|CScardServer::StartThread|Begin"));
       
   202     TInt res = KErrNone;
       
   203 
       
   204     // check server not already started
       
   205     TFindServer findSCardServer( KScardServerName );
       
   206     TFullName name;
       
   207     if ( findSCardServer.Next( name ) != KErrNone )
       
   208         {
       
   209         // create server thread
       
   210         RThread thread;
       
   211         TThreadParameter threadData;
       
   212         threadData.iSemaphore.CreateLocal( 0 );
       
   213 
       
   214         res = thread.Create(
       
   215             KScardServerName, // name of thread
       
   216             CScardServer::ThreadFunction, // thread function
       
   217             KDefaultStackSize,
       
   218             KDefaultMinHeapSize,
       
   219             KDefaultMaxHeapSize,
       
   220             &threadData // parameter to thread function
       
   221             );
       
   222 
       
   223         //  If creation was successful, try starting the server
       
   224         if ( res == KErrNone )
       
   225             {
       
   226             // now start thread
       
   227             thread.SetPriority( EPriorityNormal ); // set its priority
       
   228             thread.Resume(); // kick it into life
       
   229             threadData.iSemaphore.Wait(); // wait until it's got going
       
   230 
       
   231             if ( threadData.iPanicCode )   
       
   232                 {
       
   233                 //  The server did not start properly
       
   234                 thread.Kill( threadData.iPanicCode );
       
   235                 res = threadData.iPanicCode; // set the response
       
   236                 return res;
       
   237                 }
       
   238 
       
   239             // tidy up
       
   240             thread.Close(); // no longer interested in that thread
       
   241             }
       
   242         else
       
   243             {
       
   244             thread.Close();         
       
   245             }
       
   246 
       
   247         threadData.iSemaphore.Close(); // or semaphore
       
   248         }
       
   249 
       
   250     // all well
       
   251     return res;
       
   252     }
       
   253 
       
   254 //  End of File