connectivitymodules/SeCon/plugins/hapticsconnplugin/src/hapticsbridgeclient.cpp
branchRCL_3
changeset 20 4a793f564d72
parent 0 d0791faffa3f
equal deleted inserted replaced
19:0aa8cc770c8a 20:4a793f564d72
       
     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:  Client interface to haptics server for haptics bridge 
       
    15 *                commands.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <e32svr.h>
       
    21 #include <f32file.h>
       
    22 #include <data_caging_path_literals.hrh> // KDC_PROGRAMS_DIR
       
    23 #include <hwrmhaptics.h>
       
    24 #include <hwrmlogicalactuators.h>
       
    25 #include <hwrmhapticspacketizer.h>
       
    26 
       
    27 #include "hapticsbridgeclient.h"
       
    28 #include "hapticsconntrace.h"
       
    29 
       
    30 const TInt KServerVersionMajor       = 1;
       
    31 const TInt KServerVersionMinor       = 1;
       
    32 const TInt KServerVersionBuild       = 1;
       
    33 _LIT( KServerProcessName, "!hwrmhapticsserver" );
       
    34 _LIT( KServerExeName, "hwrmhapticsserver.exe" );
       
    35 const TInt KAsyncMsgSlots = 10;
       
    36 
       
    37 const TInt KHWRMHapticsServiceCommand      = 2001;
       
    38 const TInt KHWRMHapticsOpenActuatorCommand = 2004;
       
    39 const TInt KHWRMHapticsCleanupCommand      = 2008;
       
    40 const TInt KHWRMHapticsBridgeCommand       = 2011;
       
    41 
       
    42 // --------------------------------------------------------------------------
       
    43 // Simple constructor
       
    44 // 
       
    45 // --------------------------------------------------------------------------
       
    46 // 
       
    47 RHapticsBridgeClient::RHapticsBridgeClient()
       
    48     {
       
    49     TRAP_IGNORE( iPacketizer = 
       
    50                  CHWRMHapticsPacketizer::NewL( EHWRMLogicalActuatorAny ) );
       
    51     }
       
    52 
       
    53 // --------------------------------------------------------------------------
       
    54 // Simple destructor
       
    55 // 
       
    56 // --------------------------------------------------------------------------
       
    57 // 
       
    58 RHapticsBridgeClient::~RHapticsBridgeClient()
       
    59     {
       
    60     delete iPacketizer;
       
    61     }
       
    62 
       
    63 // --------------------------------------------------------------------------
       
    64 // Method for connecting (creating a new session) with Haptics Server
       
    65 // 
       
    66 // --------------------------------------------------------------------------
       
    67 // 
       
    68 TInt RHapticsBridgeClient::Connect()
       
    69     {
       
    70     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::Connect()" ) ) );
       
    71     TInt ret = CreateSession( KServerProcessName, 
       
    72                               ServerVersion(), 
       
    73                               KAsyncMsgSlots );
       
    74     if ( ret != KErrNone )
       
    75         {
       
    76         ret = StartServer();
       
    77         if ( ret == KErrNone )
       
    78             {
       
    79             ret = CreateSession( KServerProcessName, 
       
    80                                  ServerVersion(),
       
    81                                  KAsyncMsgSlots );    
       
    82             COMPONENT_TRACE(_L("RHapticsBridgeClient::Connect(), Session created"));
       
    83             }       
       
    84         }
       
    85 
       
    86     // Initialize server components.
       
    87     if ( ret == KErrNone )
       
    88         {
       
    89         ret = SendReceive( KHWRMHapticsServiceCommand );
       
    90         }
       
    91 
       
    92     // Open the haptic effect ("vibra") device.
       
    93     if ( ret == KErrNone )
       
    94         {
       
    95         ret = OpenHapticsDevice();
       
    96         }
       
    97     
       
    98         
       
    99     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::Connect - return(%d)" ), ret ) );        
       
   100     return ret;
       
   101     }
       
   102 
       
   103 // --------------------------------------------------------------------------
       
   104 // Method for starting Haptics Server (if not already running)
       
   105 // 
       
   106 // --------------------------------------------------------------------------
       
   107 //
       
   108 TInt RHapticsBridgeClient::StartServer() const
       
   109     {
       
   110     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::StartServer()" ) ) );
       
   111 
       
   112     TParse parser;
       
   113     parser.Set( KServerExeName, &KDC_PROGRAMS_DIR, NULL );
       
   114         
       
   115     RProcess server;
       
   116     TInt ret = server.Create( parser.FullName(), KNullDesC );
       
   117 
       
   118     if ( ret != KErrNone ) // Loading failed.
       
   119         {
       
   120         return ret;
       
   121         }
       
   122 
       
   123     TRequestStatus status;
       
   124     server.Rendezvous( status );
       
   125 
       
   126     if ( status != KRequestPending )
       
   127         {
       
   128         server.Kill( 0 ); // Abort startup.
       
   129         server.Close();
       
   130         return KErrGeneral;
       
   131         }
       
   132     else
       
   133         {
       
   134         server.Resume(); // Logon OK - start the server.
       
   135         }
       
   136         
       
   137     User::WaitForRequest( status );
       
   138     server.Close();
       
   139 
       
   140     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::StartServer() - return %d" ), status.Int() ) );
       
   141     return status.Int();    
       
   142     }
       
   143 
       
   144 // --------------------------------------------------------------------------
       
   145 // Method for closing session to haptics server.
       
   146 // 
       
   147 // --------------------------------------------------------------------------
       
   148 //    
       
   149 void RHapticsBridgeClient::Close()    
       
   150     {
       
   151     RSessionBase::Close();
       
   152     }
       
   153 
       
   154 // --------------------------------------------------------------------------
       
   155 // Getter method for server version.
       
   156 // 
       
   157 // --------------------------------------------------------------------------
       
   158 //
       
   159 TVersion RHapticsBridgeClient::ServerVersion() const 
       
   160     {
       
   161     return TVersion( KServerVersionMajor, 
       
   162                      KServerVersionMinor,
       
   163                      KServerVersionBuild );
       
   164     }
       
   165 
       
   166 // --------------------------------------------------------------------------
       
   167 // Method for sending commands to Haptics Server.
       
   168 // 
       
   169 // --------------------------------------------------------------------------
       
   170 //    
       
   171 TInt RHapticsBridgeClient::SendBridgeBuffer( const TDesC8& aReqData, TDes8& aRetDataPckg )
       
   172     {
       
   173     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::SendBridgeBufferL() - Begin" ) ) );
       
   174 
       
   175     TInt retVal( KErrDisconnected );
       
   176     if ( iHandle )
       
   177         {
       
   178         TInt vibeStatus(0);
       
   179         TPckg<TInt> vibeStatusPckg( vibeStatus );
       
   180         retVal = SendReceive( KHWRMHapticsBridgeCommand, 
       
   181                               TIpcArgs( &aReqData, 
       
   182                                         &vibeStatusPckg, 
       
   183                                         &aRetDataPckg ) );
       
   184 
       
   185         if ( retVal == KErrNone ) 
       
   186             {
       
   187             TRAP( retVal, HandleOpenDeviceResponseL( aRetDataPckg ) );
       
   188             }
       
   189         }
       
   190     
       
   191     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::SendBridgeBufferL() - End (%d)" ), retVal ) );    
       
   192     return retVal;
       
   193     }
       
   194 
       
   195 // --------------------------------------------------------------------------
       
   196 // Method for cleaning up session to Haptics Server.
       
   197 // 
       
   198 // --------------------------------------------------------------------------
       
   199 //
       
   200 void RHapticsBridgeClient::CleanUp()
       
   201     {
       
   202     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::CleanUp() - Begin" ) ) );
       
   203     if ( iHandle )
       
   204         {
       
   205         SendReceive ( KHWRMHapticsCleanupCommand );
       
   206         }
       
   207     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::CleanUp() - End" ) ) );
       
   208     }
       
   209 
       
   210 // --------------------------------------------------------------------------
       
   211 // Method for handling Open Device responses (basically for requesting the 
       
   212 // setting of the license key).
       
   213 // 
       
   214 // --------------------------------------------------------------------------
       
   215 //
       
   216 void RHapticsBridgeClient::HandleOpenDeviceResponseL( const TDesC8& aResponse )
       
   217     {
       
   218     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::HandleOpenDeviceResponse() - Begin" ) ) );
       
   219     TInt err( KErrNone );
       
   220     if ( !iPacketizer )
       
   221         {
       
   222         COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::HandleOpenDeviceResponse() - No Packetizer!" ) ) );
       
   223         User::Leave( KErrGeneral );
       
   224         }
       
   225     else    
       
   226         {
       
   227         const TUint8* pRsp ( aResponse.Ptr() );
       
   228         if ( 0x12 == pRsp[0] && 0x00 == pRsp[1] && 0x00 == pRsp[2] )
       
   229             {
       
   230             COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::HandleOpenDeviceResponse() - OPEN DEVICE RESPONSE handling" ) ) );
       
   231             iPacketizer->DecodeMessageL( aResponse, err );
       
   232             if ( !err )
       
   233                 {
       
   234                 RBuf8 reqBuf;
       
   235                 err = iPacketizer->EncSetPlatformLicenseKeyReq( 
       
   236                     iPacketizer->DeviceHandle(), reqBuf );
       
   237                 if ( !err )
       
   238                     {
       
   239                     TBuf8<50> dummyRetBuf;
       
   240                     TInt dummyStatus;
       
   241                     TPckg<TInt> dummyStatusPckg( dummyStatus );
       
   242                     err = SendReceive( KHWRMHapticsBridgeCommand, 
       
   243                                        TIpcArgs ( &reqBuf,
       
   244                                                   &dummyStatusPckg, 
       
   245                                                   &dummyRetBuf ) );
       
   246                     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::HandleOpenDeviceResponse() - Sent set license command (%d)"), err ) );
       
   247                     }
       
   248                 reqBuf.Close();
       
   249                 }
       
   250             User::LeaveIfError( err );
       
   251             }
       
   252         }
       
   253     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::HandleOpenDeviceResponse() - End" ) ) );
       
   254     }
       
   255 
       
   256 
       
   257 // --------------------------------------------------------------------------
       
   258 // Method for opening haptics device. This is needed since there's possibility
       
   259 // that no other haptics client has opened the device prior to this bridge
       
   260 // connectivity. This method also call set license key, i.e., completes the
       
   261 // device opening. 
       
   262 //
       
   263 // --------------------------------------------------------------------------
       
   264 //
       
   265 TInt RHapticsBridgeClient::OpenHapticsDevice()
       
   266     {
       
   267     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::OpenHapticsDevice - Begin" ) ) );
       
   268     TInt err( KErrGeneral );
       
   269     if ( iPacketizer )
       
   270         {
       
   271         RBuf8 openDevReqBuf;
       
   272         RBuf8 setLicenseReqBuf;
       
   273         TBuf8<50> setLicenseRspBuf;
       
   274         TInt dummyStatus;
       
   275         TPckg<TInt> dummyStatusPckg( dummyStatus );
       
   276         
       
   277         err = iPacketizer->EncOpenDeviceReq( EHWRMLogicalActuatorAny,
       
   278                                              openDevReqBuf );
       
   279 
       
   280         COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::OpenHapticsDevice - EncOpenDevice (%d)" ), err ) );
       
   281         if ( !err )
       
   282             {
       
   283             TInt deviceHandle;
       
   284             TPckg<TInt> deviceHandlePckg( deviceHandle );
       
   285             err = SendReceive( KHWRMHapticsOpenActuatorCommand,
       
   286                                TIpcArgs ( &openDevReqBuf, 
       
   287                                           &deviceHandlePckg,
       
   288                                           EHWRMLogicalActuatorAny ) );
       
   289             COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::OpenHapticsDevice - Send OpenDevice (%d)" ), err ) );
       
   290             COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::OpenHapticsDevice - Device handle = %d" ), deviceHandle ) );
       
   291             if ( !err )
       
   292                 {
       
   293                 err = iPacketizer->EncSetPlatformLicenseKeyReq( 
       
   294                     deviceHandle, setLicenseReqBuf );
       
   295                 COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::OpenHapticsDevice - EncSetEmptyLicenseKey (%d)" ), err ) );    
       
   296                 if ( !err )
       
   297                     {
       
   298                     TBuf8<50> dummyRspBuf;
       
   299                     TInt dummyStatus;
       
   300                     TPckg<TInt> dummyStatusPckg( dummyStatus );
       
   301                     err = SendReceive( KHWRMHapticsBridgeCommand, 
       
   302                                        TIpcArgs ( &setLicenseReqBuf,
       
   303                                                   &dummyStatusPckg, 
       
   304                                                   &dummyRspBuf ) );
       
   305                     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::OpenHapticsDevice - Send SetLicense (%d)" ), err ) );
       
   306                     }
       
   307                 }
       
   308             }
       
   309 
       
   310         openDevReqBuf.Close();
       
   311         setLicenseReqBuf.Close();
       
   312         }
       
   313     COMPONENT_TRACE( ( _L( "RHapticsBridgeClient::OpenHapticsDevice - End (err=%d)" ),err ) );    
       
   314     return err;
       
   315     }
       
   316 
       
   317 // eof