mtpfws/mtpfw/daemon/server/src/cmtpserver.cpp
changeset 0 d0791faffa3f
child 15 f85613f12947
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include <ecom/ecom.h>
       
    22 
       
    23 #include "cmtpconnectionmgr.h"
       
    24 #include "cmtpdataprovidercontroller.h"
       
    25 #include "cmtpserver.h"
       
    26 #include "cmtpserversession.h"
       
    27 #include "cmtpshutdown.h"
       
    28 #include "mtpclientserver.h"
       
    29 #include "mtpdebug.h"
       
    30 #include "rmtpframework.h"
       
    31 
       
    32 #define UNUSED_VAR(a) (a) = (a)
       
    33 
       
    34 __FLOG_STMT(_LIT8(KComponent,"Server");)
       
    35 
       
    36 /**
       
    37 PlatSec policy.
       
    38 */ 
       
    39 const TUint8 KMTPPolicyElementNetworkAndLocal = 0;
       
    40 
       
    41 const TInt KMTPFunctionCodeRanges[] = 
       
    42     {    
       
    43     EMTPClientStartTransport, 
       
    44     EMTPClientNotSupported,
       
    45     };
       
    46 
       
    47 const TUint KMTPFunctionCodeRangeCount(sizeof(KMTPFunctionCodeRanges) / sizeof(KMTPFunctionCodeRanges[0]));
       
    48 
       
    49 const TUint8 KMtpElementsIndex[KMTPFunctionCodeRangeCount] =
       
    50     {
       
    51     KMTPPolicyElementNetworkAndLocal,
       
    52     CPolicyServer::ENotSupported,
       
    53     };
       
    54 
       
    55 const CPolicyServer::TPolicyElement KMtpPolicyElements[] = 
       
    56     { 
       
    57     {_INIT_SECURITY_POLICY_C2(ECapabilityNetworkServices, ECapabilityLocalServices), CPolicyServer::EFailClient},
       
    58     };
       
    59 
       
    60 const static CPolicyServer::TPolicy KMTPServerPolicy =
       
    61     {
       
    62     CPolicyServer::EAlwaysPass, //specifies all connect attempts should pass
       
    63     KMTPFunctionCodeRangeCount,
       
    64     KMTPFunctionCodeRanges,
       
    65     KMtpElementsIndex,     // what each range is compared to 
       
    66     KMtpPolicyElements     // what policies range is compared to
       
    67     };
       
    68     
       
    69 /**
       
    70 Destructor.
       
    71 */
       
    72 CMTPServer::~CMTPServer()
       
    73     {
       
    74     __FLOG(_L8("~CMTPServer - Entry"));
       
    75     delete iShutdown;
       
    76     iFrameworkSingletons.ConnectionMgr().StopTransports();
       
    77     iFrameworkSingletons.DpController().UnloadDataProviders();
       
    78     iFrameworkSingletons.Close();
       
    79     REComSession::FinalClose();
       
    80     __FLOG(_L8("~CMTPServer - Exit"));
       
    81     __FLOG_CLOSE;
       
    82     }
       
    83 
       
    84 /**
       
    85 Creates and executes a new CMTPServer instance.
       
    86 @leave One of the system wide error codes, if a processing failure occurs.
       
    87 */
       
    88 void CMTPServer::RunServerL()
       
    89     {
       
    90     __FLOG_STATIC(KMTPSubsystem, KComponent, _L8("RunServerL - Entry"));
       
    91     
       
    92     // Naming the server thread after the server helps to debug panics
       
    93     User::LeaveIfError(User::RenameProcess(KMTPServerName));
       
    94     
       
    95     // Create and install the active scheduler.
       
    96     CActiveScheduler* scheduler(new(ELeave) CActiveScheduler);
       
    97     CleanupStack::PushL(scheduler);
       
    98     CActiveScheduler::Install(scheduler);
       
    99     
       
   100     // Create the server and leave it on the cleanup stack.
       
   101     CMTPServer* server(CMTPServer::NewLC());
       
   102     
       
   103     // Initialisation complete, signal the client
       
   104     RProcess::Rendezvous(KErrNone);
       
   105     
       
   106     // Execute the server.
       
   107     CActiveScheduler::Start();
       
   108 
       
   109 	// Server shutting down. 
       
   110 	CleanupStack::PopAndDestroy(server);
       
   111         
       
   112     CleanupStack::PopAndDestroy(1); // scheduler
       
   113     __FLOG_STATIC(KMTPSubsystem, KComponent, _L8("RunServerL - Exit"));
       
   114     } 
       
   115 
       
   116 /**
       
   117 Adds a new CMTPServer session.
       
   118 */
       
   119 void CMTPServer::AddSession()
       
   120     {  
       
   121     __FLOG(_L8("AddSession - Entry"));
       
   122     if(iShutdown && iShutdown->IsActive())
       
   123         {  
       
   124         iShutdown->Cancel();
       
   125         }
       
   126     ++iSessionCount;
       
   127     __FLOG(_L8("AddSession - Exit"));
       
   128     }
       
   129 
       
   130 /**
       
   131 Removes a CMTPServer session. If there are no active MTP client API sessions 
       
   132 remaining and no active MTP connections, then a shutdown timer is started to 
       
   133 terminate the server thread.
       
   134 */
       
   135 void CMTPServer::DropSession()
       
   136     {
       
   137     __FLOG(_L8("DropSession - Entry"));
       
   138          
       
   139     if (--iSessionCount==0 && iFrameworkSingletons.ConnectionMgr().TransportCount() == 0)
       
   140         {
       
   141         // No active MTP client API sessions remain, start the shutdown timer.
       
   142         if (!iShutdown)
       
   143             {
       
   144             TRAPD(error, iShutdown = CMTPShutdown::NewL());
       
   145             __FLOG(_L8("CMTPShutdown Loaded- Entry"));
       
   146             UNUSED_VAR(error);    
       
   147             }
       
   148         if (iShutdown)
       
   149             {
       
   150             __FLOG(_L8("Shutdown Started - Entry"));
       
   151             iShutdown->Start();
       
   152             }
       
   153         }
       
   154     __FLOG(_L8("DropSession - Exit"));
       
   155     }
       
   156     
       
   157 CSession2* CMTPServer::NewSessionL(const TVersion&,const RMessage2&) const
       
   158     {
       
   159     __FLOG(_L8("NewSessionL - Entry"));
       
   160     __FLOG(_L8("NewSessionL - Exit"));
       
   161     return new(ELeave) CMTPServerSession();
       
   162     }
       
   163        
       
   164 /**
       
   165 CMTPServer factory method. A pointer to the constructed CMTPServer instance is
       
   166 placed on the cleanup stack.
       
   167 @return A pointer to a new CMTPServer instance. Ownership IS transfered.
       
   168 @leave One of the system wide error codes if a processing failure occurs.
       
   169 */
       
   170 CMTPServer* CMTPServer::NewLC()
       
   171     {
       
   172     CMTPServer* self=new(ELeave) CMTPServer;
       
   173     CleanupStack::PushL(self);
       
   174     self->ConstructL();
       
   175     return self;
       
   176     }
       
   177 
       
   178 /**
       
   179 Constructor.
       
   180 */    
       
   181 CMTPServer::CMTPServer() : 
       
   182     CPolicyServer(CActive::EPriorityStandard, KMTPServerPolicy)
       
   183     {
       
   184 
       
   185     }
       
   186     
       
   187 /**
       
   188 second-phase constructor.
       
   189 */
       
   190 void CMTPServer::ConstructL()
       
   191     {
       
   192     __FLOG_OPEN(KMTPSubsystem, KComponent);
       
   193     __FLOG(_L8("ConstructL - Entry"));
       
   194     StartL(KMTPServerName);
       
   195     iFrameworkSingletons.OpenL();
       
   196     __FLOG(_L8("ConstructL - Exit"));
       
   197     }
       
   198         
       
   199 /*
       
   200 RMessage::Panic() also completes the message. This is:
       
   201 (a) important for efficient cleanup within the kernel
       
   202 (b) a problem if the message is completed a second time
       
   203 @param aMessage Message to be paniced.
       
   204 @param aPanic Panic code.
       
   205 */
       
   206 void PanicClient(const RMessagePtr2& aMessage,TMTPPanic aPanic)
       
   207     {
       
   208     __FLOG_STATIC(KMTPSubsystem, KComponent, _L8("PanicClient - Entry"));
       
   209     __FLOG_STATIC_VA((KMTPSubsystem, KComponent, _L8("Panic = %d"), aPanic));
       
   210     _LIT(KPanic,"MTPServer");
       
   211     aMessage.Panic(KPanic, aPanic);
       
   212     __FLOG_STATIC(KMTPSubsystem, KComponent, _L8("PanicClient - Exit"));
       
   213     }
       
   214 
       
   215 /**
       
   216 Process entry point
       
   217 */
       
   218 TInt E32Main()
       
   219     {
       
   220     __UHEAP_MARK;
       
   221     __FLOG_STATIC(KMTPSubsystem, KComponent, _L8("E32Main - Entry"));
       
   222     __MTP_HEAP_FLOG
       
   223     
       
   224     CTrapCleanup* cleanup=CTrapCleanup::New();
       
   225     TInt ret = KErrNoMemory;
       
   226     if (cleanup)
       
   227         {
       
   228 #ifdef __OOM_TESTING__
       
   229         TInt i = 0;
       
   230         while (ret == KErrNoMemory || ret == KErrNone)
       
   231              {
       
   232              __UHEAP_SETFAIL(RHeap::EDeterministic,i++);
       
   233              __UHEAP_MARK;
       
   234 
       
   235              TRAP(nRet, RunServerL());
       
   236 
       
   237              __UHEAP_MARKEND;
       
   238              __UHEAP_RESET;
       
   239              }
       
   240 #else
       
   241         TRAP(ret, CMTPServer::RunServerL());
       
   242       
       
   243 #endif        
       
   244         delete cleanup;
       
   245         }
       
   246     __FLOG_STATIC(KMTPSubsystem, KComponent, _L8("E32Main - Exit"));
       
   247     __UHEAP_MARKEND;
       
   248     return ret;
       
   249     }