connectivitymodules/SeCon/plugins/hapticsconnplugin/src/hapticsmsghandler.cpp
branchRCL_3
changeset 42 0aa8cc770c8a
parent 40 453dfc402455
child 43 4a793f564d72
equal deleted inserted replaced
40:453dfc402455 42:0aa8cc770c8a
     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:  Handles messaging between file system and haptics 
       
    15 *                client interface.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <f32file.h> 
       
    21 #include <bautils.h> 
       
    22 #include <pathinfo.h>
       
    23 #include <hwrmhaptics.h>
       
    24 
       
    25 #include "hapticsmsghandler.h"
       
    26 #include "hapticsconntimer.h"
       
    27 #include "hapticsconntrace.h"
       
    28 
       
    29 _LIT( KVibeTonzFolder,"VibeTonz\\" );
       
    30 _LIT( KSourceFileName,"VibeTonzDataReq" );
       
    31 _LIT( KTargetFileName,"VibeTonzDataRsp" );
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // CHapticsMsgHandler* CHapticsMsgHandler::NewL()
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 CHapticsMsgHandler* CHapticsMsgHandler::NewL()
       
    38     {
       
    39     CHapticsMsgHandler* self = new (ELeave) CHapticsMsgHandler();
       
    40     CleanupStack::PushL( self );
       
    41     self->ConstructL();
       
    42     CleanupStack::Pop( self );
       
    43     return self;
       
    44     }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // Default C++ constructor
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 CHapticsMsgHandler::CHapticsMsgHandler () : CActive( EPriorityStandard )
       
    51     {
       
    52     }    
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // void CHapticsMsgHandler::ConstructL()
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 void CHapticsMsgHandler::ConstructL()
       
    59     {
       
    60     COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::ConstructL - Begin") ) );    
       
    61     CActiveScheduler::Add( this );
       
    62     ConnectToFilesystemL();
       
    63     iConnectionTimeout = CHapticsConnTimer::NewL(this);
       
    64     COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::ConstructL - End") ) );
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CHapticsMsgHandler::~CHapticsMsgHandler()
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 CHapticsMsgHandler::~CHapticsMsgHandler()
       
    72     {    
       
    73     Cancel();
       
    74 
       
    75     if ( iConnectionTimeout )
       
    76         {
       
    77         iConnectionTimeout->Cancel();
       
    78         delete iConnectionTimeout;
       
    79         }
       
    80 
       
    81     delete iReqBuf;
       
    82         
       
    83     iClient.Close();
       
    84     iFs.Close();
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // void ClearRequestFile()
       
    89 // ---------------------------------------------------------------------------
       
    90 //   
       
    91 void CHapticsMsgHandler::ClearRequestFile()
       
    92 	{
       
    93 	COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::ClearRequestFile - Begin") ) );
       
    94 	
       
    95     TFindFile findDataFile( iFs );
       
    96     
       
    97     TFileName sourcefile = PathInfo::PhoneMemoryRootPath();
       
    98     sourcefile.Append( PathInfo::OthersPath() );
       
    99     sourcefile.Append( KVibeTonzFolder );
       
   100     sourcefile.Append( KSourceFileName );
       
   101     
       
   102     if( findDataFile.FindByDir( sourcefile, KNullDesC ) == KErrNone )
       
   103     	{
       
   104     	COMPONENT_TRACE( _L("CHapticsMsgHandler::ClearRequestFile - Deleting REQ file") );
       
   105     	iFs.Delete( KSourceFileName ); 
       
   106     	}
       
   107     COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::ClearRequestFile - End") ) );    	
       
   108 	}
       
   109 // ---------------------------------------------------------------------------
       
   110 // void ConnectToFilesystem()
       
   111 // ---------------------------------------------------------------------------
       
   112 //    
       
   113 void CHapticsMsgHandler::ConnectToFilesystemL()
       
   114     {
       
   115     COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::ConnectToFilesystemL - Begin") ) );
       
   116     User::LeaveIfError( iFs.Connect() );
       
   117     
       
   118     TFileName sessionPath = PathInfo::PhoneMemoryRootPath();
       
   119     sessionPath.Append( PathInfo::OthersPath() );
       
   120     sessionPath.Append( KVibeTonzFolder );
       
   121     TBool folderExists = BaflUtils::FolderExists( iFs, sessionPath );
       
   122     
       
   123     if( folderExists )
       
   124         {
       
   125         User::LeaveIfError( iFs.SetSessionPath( sessionPath ) );
       
   126         ClearRequestFile();
       
   127         }
       
   128     else
       
   129         {
       
   130         User::LeaveIfError( iFs.MkDir( sessionPath ) );
       
   131         User::LeaveIfError( iFs.SetSessionPath( sessionPath ) );
       
   132         }
       
   133     COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::ConnectToFilesystemL - End") ) );
       
   134     }    
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // CHapticsMsgHandler::StartNotifier()
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140 void CHapticsMsgHandler::StartNotifier()
       
   141 	{
       
   142 	COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::StartNotifier - Begin" ) ) );
       
   143     if ( !IsActive() )
       
   144         {
       
   145         TFileName sessionPath = PathInfo::PhoneMemoryRootPath();
       
   146         sessionPath.Append( PathInfo::OthersPath() );
       
   147         sessionPath.Append( KVibeTonzFolder );        
       
   148         iFs.NotifyChange( ENotifyAll, iStatus, sessionPath );
       
   149         SetActive();
       
   150 	    COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::StartNotifier - Started" ) ) );        
       
   151         }
       
   152     COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::StartNotifier - End" ) ) );
       
   153     }
       
   154 
       
   155 // ---------------------------------------------------------------------------
       
   156 // CHapticsMsgHandler::RunL()
       
   157 // ---------------------------------------------------------------------------
       
   158 //
       
   159 void CHapticsMsgHandler::RunL()
       
   160     {
       
   161     COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::RunL - Begin" ) ) );
       
   162 
       
   163     StartNotifier(); // re-subscribe for change notifications.
       
   164 
       
   165     TFindFile findDataFile(iFs); 
       
   166     TFileName sourcefileName = PathInfo::PhoneMemoryRootPath();
       
   167     sourcefileName.Append( PathInfo::OthersPath() );
       
   168     sourcefileName.Append( KVibeTonzFolder );
       
   169     sourcefileName.Append( KSourceFileName ); 
       
   170           
       
   171     if( findDataFile.FindByDir( sourcefileName, KNullDesC ) == KErrNone )
       
   172         {
       
   173         COMPONENT_TRACE( ( _L("CHapticsMsgHandler::RunL - found request file") ) );
       
   174         
       
   175         RFile sourceFile;
       
   176 		TInt sourceFileSize;
       
   177 
       
   178 		User::LeaveIfError( sourceFile.Open( iFs, sourcefileName, EFileShareAny )  );
       
   179 		CleanupClosePushL( sourceFile );
       
   180 		sourceFile.Size( sourceFileSize );
       
   181 		
       
   182 		iReqBuf = HBufC8::NewL( sourceFileSize );
       
   183 		TPtr8 reqBufPtr = iReqBuf->Des();		
       
   184 		sourceFile.Read( reqBufPtr );
       
   185 		CleanupStack::PopAndDestroy( &sourceFile );
       
   186 			
       
   187 		ClearRequestFile();
       
   188 
       
   189         DATADUMP_TRACE( _L("CHapticsMsgHandler::RunL - request data dump:"), reqBufPtr );
       
   190 
       
   191         if ( !iClient.Handle() )
       
   192             {
       
   193             User::LeaveIfError( iClient.Connect() );
       
   194             }
       
   195 
       
   196 		TBuf8<256> retData;	
       
   197 		TInt bridgeErr = iClient.SendBridgeBuffer( reqBufPtr, retData );
       
   198 					
       
   199 	    delete iReqBuf;
       
   200 	    iReqBuf = NULL;
       
   201 	    
       
   202 		if ( bridgeErr == KErrNone )
       
   203 			{		
       
   204             iConnectionTimeout->Cancel();
       
   205             iConnectionTimeout->Start();
       
   206             			
       
   207 			DATADUMP_TRACE( _L("CHapticsMsgHandler::RunL - response data dump:"), retData );
       
   208             RFile targetFile;
       
   209 			targetFile.Replace( iFs, KTargetFileName, EFileWrite );
       
   210 			targetFile.Write( retData );
       
   211         	targetFile.Close();
       
   212         	}
       
   213         }      
       
   214     COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::RunL - End" ) ) );
       
   215     }
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 //
       
   219 //
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 TInt CHapticsMsgHandler::RunError( TInt aError )
       
   223     {
       
   224     COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::RunError: %d" ), aError ) );
       
   225     return aError;
       
   226     }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 //
       
   230 //
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 void CHapticsMsgHandler::DoCancel()
       
   234     {
       
   235     COMPONENT_TRACE( ( _L( "Inside CHapticsMsgHandler::DoCancel" ) ) );
       
   236     if ( IsActive() )
       
   237         {
       
   238         iFs.NotifyChangeCancel( iStatus );
       
   239         }
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CHapticsConnPlugin::CHapticsConnPlugin()
       
   244 // Constructor
       
   245 // -----------------------------------------------------------------------------
       
   246 //
       
   247 void CHapticsMsgHandler::NotifyShutdown()
       
   248     {
       
   249     COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::NotifyShutdown - Begin" ) ) );
       
   250     iClient.CleanUp();
       
   251     iClient.Close(); 
       
   252     COMPONENT_TRACE( ( _L( "CHapticsMsgHandler::NotifyShutdown - End" ) ) );    
       
   253     } 
       
   254 	
       
   255 // end of file