hwrmhaptics/hapticsclient/src/hwrmhapticssession.cpp
changeset 0 4e1aa6a622a0
child 21 ccb4f6b3db21
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Implementation of client side session.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32svr.h>
       
    20 #include <data_caging_path_literals.hrh>
       
    21 
       
    22 #include "hwrmhapticssession.h"
       
    23 #include "hwrmhapticsconfiguration.h"
       
    24 #include "hwrmhapticsclientserver.h"
       
    25 #include "hwrmhapticstrace.h"
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 // Connects to server
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 TInt RHWRMHapticsSession::Connect()
       
    32     {
       
    33    COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::Connect()" ) ) );
       
    34 
       
    35     TInt err = MakeConnection();
       
    36 
       
    37     // Create proper service on server side. i.e. initialize
       
    38     // server components
       
    39     if ( err == KErrNone )
       
    40         {
       
    41         err = SendReceive( EHWRMHapticsService );
       
    42         }
       
    43         
       
    44     COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::Connect - return(%d)" ), err ) );        
       
    45     
       
    46     return err;
       
    47     }
       
    48 
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // Connects to server (ASYNC)
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 void RHWRMHapticsSession::Connect( TRequestStatus& aStatus )
       
    55     {
       
    56    COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::Connect()" ) ) );
       
    57 
       
    58     TInt err = MakeConnection();
       
    59 
       
    60     // Create proper service on server side. i.e. initialize
       
    61     // server components
       
    62     if ( err == KErrNone )
       
    63         {
       
    64         SendReceive( EHWRMHapticsService, aStatus );
       
    65         }
       
    66     else
       
    67         {
       
    68         TRequestStatus* status = &aStatus;
       
    69         User::RequestComplete( status, err );
       
    70         }
       
    71 
       
    72     COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::Connect - return(%d)" ), err ) );
       
    73     }
       
    74 
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // Calls SendReceive with given parameters.
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 TInt RHWRMHapticsSession::ExecuteOperation( 
       
    81                                             TInt aCommand, 
       
    82                                             const TIpcArgs& aArgs ) const
       
    83     {
       
    84     COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::ExecuteOperation(0x%x)" ), aCommand ) );
       
    85     
       
    86     TInt retval = SendReceive( aCommand, aArgs );
       
    87     
       
    88     COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::ExecuteOperation - return %d" ), retval ) );
       
    89     
       
    90     return retval;
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // Calls SendReceive with given parameter.
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 TInt RHWRMHapticsSession::ExecuteOperation( TInt aCommand ) const
       
    98     {
       
    99     COMPONENT_TRACE( _L( "RHWRMHapticsSession::ExecuteOperation" ) );
       
   100     
       
   101     TInt retval = SendReceive( aCommand );
       
   102     
       
   103     COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::ExecuteOperation - return %d" ), retval ) );
       
   104     
       
   105     return retval;
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // Calls SendReceive with given parameters.
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 void RHWRMHapticsSession::ExecuteAsyncOperation(
       
   113                                             TInt aCommand, 
       
   114                                             const TIpcArgs& aArgs, 
       
   115                                             TRequestStatus& aStatus ) const
       
   116     {
       
   117     COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::ExecuteAsyncOperation - commandId(%d), aStatus(%d)" ), aCommand, aStatus.Int() ) );
       
   118     
       
   119     SendReceive( aCommand, aArgs, aStatus );
       
   120     
       
   121     COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::ExecuteAsyncOperation - return" ) ) );
       
   122     }
       
   123     
       
   124 // ---------------------------------------------------------------------------
       
   125 // Starts server.
       
   126 // ---------------------------------------------------------------------------
       
   127 //
       
   128 TInt RHWRMHapticsSession::StartServer() const
       
   129     {
       
   130     COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::StartServer()" ) ) );
       
   131     
       
   132     RProcess server;
       
   133     TInt ret = server.Create( ServerLocation(), KNullDesC );
       
   134 
       
   135     if ( ret != KErrNone ) // Loading failed.
       
   136         {
       
   137         return ret;
       
   138         }
       
   139     
       
   140     TRequestStatus status;
       
   141     server.Rendezvous( status );
       
   142 
       
   143     if ( status != KRequestPending )
       
   144         {
       
   145         server.Kill( 0 ); // Abort startup.
       
   146         server.Close();
       
   147         return KErrGeneral;
       
   148         }
       
   149     else
       
   150         {
       
   151         server.Resume(); // Logon OK - start the server.
       
   152         }
       
   153         
       
   154     User::WaitForRequest( status );
       
   155     server.Close();
       
   156 
       
   157     COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::StartServer - return %d" ), status.Int() ) );
       
   158 
       
   159     return status.Int();    
       
   160     }
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 // Returns the server location.
       
   164 // ---------------------------------------------------------------------------
       
   165 //
       
   166 TFullName RHWRMHapticsSession::ServerLocation() const
       
   167     {
       
   168     TFullName fullPathAndName( KServerExeDrive );
       
   169     fullPathAndName.Append( KDC_PROGRAMS_DIR );
       
   170     fullPathAndName.Append( KServerExeName );
       
   171     
       
   172     COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::ServerLocation - return %S" ), &fullPathAndName ) );
       
   173 
       
   174     return fullPathAndName;
       
   175     }
       
   176 
       
   177 // ---------------------------------------------------------------------------
       
   178 // Returns the version of the server.
       
   179 // ---------------------------------------------------------------------------
       
   180 //
       
   181 TVersion RHWRMHapticsSession::ServerVersion() const 
       
   182     {
       
   183     return TVersion( KServerVersionMajor, 
       
   184                      KServerVersionMinor,
       
   185                      KServerVersionBuild );
       
   186     }
       
   187 
       
   188 // ---------------------------------------------------------------------------
       
   189 // Makes connection to server.
       
   190 // ---------------------------------------------------------------------------
       
   191 //
       
   192 TInt RHWRMHapticsSession::MakeConnection()
       
   193     {
       
   194     // Try connection twice, if not successful on the first attempt
       
   195     TInt retry( 2 );
       
   196     TInt err( KErrNone );
       
   197 
       
   198     while ( retry > 0 )
       
   199         {
       
   200         // try to create a session to the server
       
   201         err = CreateSession( KServerProcessName, 
       
   202                              ServerVersion(), 
       
   203                              KDefaultAsyncSlots );
       
   204 
       
   205         COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::MakeConnection - CreateSession returned: err(%d)" ), err ) );        
       
   206         
       
   207         if ( err != KErrNotFound && err != KErrServerTerminated )
       
   208             {
       
   209             retry = 0; // KErrNone or unrecoverable error
       
   210             }
       
   211         else
       
   212             {
       
   213             // Return code was KErrNotFound or KErrServerTerminated.
       
   214             // Try to start the Haptics server
       
   215             err = StartServer();
       
   216 
       
   217             COMPONENT_TRACE( ( _L( "RHWRMHapticsSession::MakeConnection - StartServer returned: err(%d)" ), err ) );        
       
   218 
       
   219             if ( err != KErrNone && err != KErrAlreadyExists )
       
   220                 {
       
   221                 retry = 0; // Unrecoverable error
       
   222                 }
       
   223             }
       
   224         
       
   225         retry--;
       
   226         }
       
   227 
       
   228     return err;    
       
   229     }
       
   230 //  End of File