dbgagents/trkagent/toolsstarter/toolsstarterserver/src/toolssrvsession.cpp
changeset 0 c6b0df440bee
equal deleted inserted replaced
-1:000000000000 0:c6b0df440bee
       
     1 /*
       
     2 * Copyright (c) 2009 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "toolssrvsession.h"
       
    19 #include "toolssrvserver.h"
       
    20 // System includes
       
    21 
       
    22 //User Includes
       
    23 #include "ToolsCmdCodes.h"
       
    24 
       
    25 // Type definitions
       
    26 
       
    27 // Constants
       
    28 const TInt KToolsServerTransferBufferExpandSize = 100;
       
    29 const TInt KSlot0 = 0;
       
    30 
       
    31 // Enumerations
       
    32 
       
    33 // Classes referenced
       
    34 
       
    35 
       
    36 // Static functions
       
    37 //
       
    38 // Checks for a null'd handle before attempting complete. 
       
    39 // If handle is null'd then don't complete as this will panic server.
       
    40 //
       
    41 void SafeComplete(const RMessagePtr2& aMessage, TInt aCompletionCode)
       
    42 {
       
    43     if(!aMessage.IsNull())
       
    44     {
       
    45         aMessage.Complete(aCompletionCode);
       
    46     }
       
    47 }
       
    48 
       
    49 
       
    50 
       
    51 //
       
    52 // CToolsSrvSession (source)
       
    53 //
       
    54 
       
    55 //
       
    56 // CToolsSrvServer::CToolsSrvSession()
       
    57 //
       
    58 // Constructor
       
    59 //
       
    60 CToolsSrvSession::CToolsSrvSession()
       
    61 :   iPendingRead(EFalse),
       
    62     iLaunchManager(NULL)
       
    63 {
       
    64 }
       
    65 
       
    66 //
       
    67 // CToolsSrvServer::~CToolsSrvSession()
       
    68 //
       
    69 // Destructor
       
    70 //
       
    71 CToolsSrvSession::~CToolsSrvSession()
       
    72 {
       
    73 	HandleServerDestruction();
       
    74 	delete iTransferBuffer;
       
    75 	//get a reference to the server
       
    76 	CToolsSrvServer* toolsserver = (CToolsSrvServer*)Server();
       
    77 	
       
    78 	//notify the server that the session has been opened
       
    79 	if (toolsserver != NULL)
       
    80 	    toolsserver->SessionClosed();
       
    81 	if(iLaunchManager)
       
    82 	    iLaunchManager->UnregisterListener(this);
       
    83 }
       
    84 
       
    85 //
       
    86 // CToolsSrvServer::ConstructL()
       
    87 //
       
    88 // Creates an instance of CToolsSrvSession.
       
    89 //
       
    90 void CToolsSrvSession::ConstructL()
       
    91 {
       
    92     iTransferBuffer = CBufFlat::NewL(KToolsServerTransferBufferExpandSize);
       
    93 }
       
    94 
       
    95 //
       
    96 //  CToolsSrvSession::CreateL()
       
    97 //
       
    98 void CToolsSrvSession::CreateL()
       
    99 {
       
   100     //get a reference to the server
       
   101     CToolsSrvServer* toolsserver = (CToolsSrvServer*)Server();
       
   102     
       
   103     //notify the server that the session has been opened
       
   104     if (toolsserver != NULL)
       
   105     {
       
   106         toolsserver->SessionOpened();
       
   107         iLaunchManager = toolsserver->GetLaunchInterface();
       
   108     }
       
   109   
       
   110 }
       
   111 //
       
   112 // CToolsSrvServer::NewL()
       
   113 //
       
   114 // Static self construction
       
   115 //
       
   116 CToolsSrvSession* CToolsSrvSession::NewL()
       
   117 {
       
   118 	CToolsSrvSession* self = new(ELeave) CToolsSrvSession();
       
   119 	CleanupStack::PushL(self);
       
   120 	self->ConstructL();
       
   121 	CleanupStack::Pop(self);
       
   122 	return self;
       
   123 }
       
   124 
       
   125 //
       
   126 // CToolsSrvServer::HandleServerDestruction()
       
   127 //
       
   128 // Called by the server's destructor. We need to be told that the server is
       
   129 // being destroyed.
       
   130 //
       
   131 void CToolsSrvSession::HandleServerDestruction()
       
   132 {
       
   133 }
       
   134 
       
   135 //
       
   136 // CToolsSrvServer::ServiceL()
       
   137 //
       
   138 // Services requests from a client.
       
   139 // Called by the IPC framework whenever a client sends a request to the server.
       
   140 //
       
   141 void CToolsSrvSession::ServiceL(const RMessage2& aMessage)
       
   142 {
       
   143 
       
   144     const TInt cmd = aMessage.Function();
       
   145 
       
   146 	switch(cmd)
       
   147 	{
       
   148 		case EToolsCmdCodeGetUsbConnStatus:
       
   149 			 CmdGetUsbConnStatus(aMessage);
       
   150 			break;
       
   151 		case EToolsCmdCodeConnNotify:
       
   152 			CmdConnNofify(aMessage);
       
   153 			break;
       
   154 		case EToolCmdCodeConnNotifyCancel:
       
   155 			CmdConnNotifyCancel(aMessage);
       
   156 			break;		
       
   157 		case EToolsCmdCodeShutDownServer:
       
   158 			CmdShutDownServer();			
       
   159 			break;
       
   160 	
       
   161 		default:
       
   162 			aMessage.Panic(KServerIntiatedSessionPanic, EToolsServerInitiatedClientPanicInvalidOperation);
       
   163 			break;
       
   164 	}
       
   165 		
       
   166 }
       
   167 
       
   168 //
       
   169 //  CToolsSrvSession::CmdGetUsbConnStatus      
       
   170 //
       
   171 //  Command implementation to find out the usb connection status
       
   172 //
       
   173 void CToolsSrvSession::CmdGetUsbConnStatus(const RMessage2& aMessage)
       
   174 {
       
   175     TConnectionStatus status = iLaunchManager->GetUsbConnStatus();
       
   176     TPckg<TConnectionStatus> package(status);
       
   177     TRAPD(err,aMessage.Write(KSlot0,package)); 
       
   178     if (err != KErrNone)
       
   179     {
       
   180         aMessage.Panic(_L("ToolsServer"),2);
       
   181     }
       
   182     SafeComplete(aMessage,KErrNone);
       
   183 }
       
   184 //
       
   185 //  CToolsSrvSession::CmdConnNofify      
       
   186 //
       
   187 //  To register for usb cable connection notifications
       
   188 //
       
   189 void CToolsSrvSession::CmdConnNofify(const RMessage2& aMessage)
       
   190 {   
       
   191     if (iPendingRead)
       
   192     {   
       
   193         SafeComplete(aMessage, KErrAlreadyExists);
       
   194     }
       
   195     else
       
   196     {
       
   197         iLaunchManager->RegisterListener(this);
       
   198         iBlockedRead = aMessage;
       
   199         iPendingRead = ETrue;     
       
   200         
       
   201     }   
       
   202 }
       
   203 //
       
   204 //  CToolsSrvSession::CmdConnNotifyCancel      
       
   205 //
       
   206 //  To unregister for usb cable connection notifications
       
   207 //
       
   208 void CToolsSrvSession::CmdConnNotifyCancel(const RMessage2& aMessage)
       
   209 {
       
   210     if ( iPendingRead && iBlockedRead.Handle())
       
   211     {
       
   212         SafeComplete(iBlockedRead, KErrCancel);
       
   213         iPendingRead = EFalse;
       
   214         iLaunchManager->UnregisterListener(this);
       
   215     }
       
   216     SafeComplete(aMessage, KErrNone);
       
   217 }
       
   218 
       
   219 //
       
   220 //  CToolsSrvSession::ConnStatusChanged
       
   221 //
       
   222 //  Called when the usb cable connection changes
       
   223 //
       
   224 void CToolsSrvSession::ConnStatusChanged(TConnectionStatus aStatus)
       
   225 {
       
   226     if(iPendingRead && iBlockedRead.Handle())
       
   227     {
       
   228         iPendingRead = EFalse;
       
   229         TConnectionStatus status = aStatus;
       
   230         TPckg<TConnectionStatus> package(status);
       
   231         TRAPD(err, iBlockedRead.WriteL(KSlot0, package));  
       
   232         if ( err != KErrNone )
       
   233         {
       
   234             iBlockedRead.Panic(_L("ToolsServer"),2);
       
   235         }
       
   236         SafeComplete(iBlockedRead,KErrNone);
       
   237     }
       
   238        
       
   239 }
       
   240 //
       
   241 // CToolsSrvServer::CmdShutDownServer()
       
   242 //
       
   243 // Stops the active scheduler. This way, server process will run to completion.
       
   244 //
       
   245 void CToolsSrvSession::CmdShutDownServer()
       
   246 {
       
   247 	CActiveScheduler::Stop();
       
   248 }