mmappcomponents/mmmtpdataprovider/mmmtpdprequestprocessor/src/cgetpartialobject.cpp
changeset 0 a2952bb97e68
child 9 bee149131e4b
child 25 d881023c13eb
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     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:  Implement the operation: GetPartialObject
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <mtp/cmtptypefile.h>
       
    20 #include <mtp/mmtpdataproviderframework.h>
       
    21 #include <mtp/cmtpobjectmetadata.h>
       
    22 
       
    23 #include "cgetpartialobject.h"
       
    24 #include "mmmtpdplogger.h"
       
    25 #include "tmmmtpdppanic.h"
       
    26 #include "ttypeflatbuf.h"
       
    27 #include "mmmtpdpconfig.h"
       
    28 
       
    29 /**
       
    30 * Verification data for the GetPartialObject request
       
    31 */
       
    32 const TMTPRequestElementInfo KMTPGetPartialObjectPolicy[] =
       
    33     {
       
    34         {TMTPTypeRequest::ERequestParameter1, EMTPElementTypeObjectHandle, EMTPElementAttrFile, 0, 0, 0}
       
    35     };
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CGetPartialObject::NewL
       
    39 // Two-phase construction method
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 EXPORT_C MMmRequestProcessor* CGetPartialObject::NewL( MMTPDataProviderFramework& aFramework,
       
    43     MMTPConnection& aConnection,
       
    44     MMmMtpDpConfig& aDpConfig )
       
    45     {
       
    46     CGetPartialObject* self = new (ELeave) CGetPartialObject( aFramework, aConnection );
       
    47     CleanupStack::PushL( self );
       
    48     self->ConstructL();
       
    49     CleanupStack::Pop( self );
       
    50     return self;
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CGetPartialObject::~CGetPartialObject()
       
    55 // Destructor
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 EXPORT_C CGetPartialObject::~CGetPartialObject()
       
    59     {
       
    60     delete iBuffer;
       
    61     delete iPartialData;
       
    62     delete iFileObject;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CGetPartialObject::CGetPartialObject
       
    67 // Standard c++ constructor
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 EXPORT_C CGetPartialObject::CGetPartialObject( MMTPDataProviderFramework& aFramework,
       
    71     MMTPConnection& aConnection ) :
       
    72     CRequestProcessor( aFramework,
       
    73         aConnection,
       
    74         sizeof( KMTPGetPartialObjectPolicy ) / sizeof( TMTPRequestElementInfo ),
       
    75         KMTPGetPartialObjectPolicy ),
       
    76     iFramework ( aFramework ),
       
    77     iFs( iFramework.Fs() ),
       
    78     iBufferPtr8( NULL, 0 )
       
    79     {
       
    80     PRINT( _L( "Operation: GetPartialObject(0x101B)" ) );
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CGetPartialObject::CheckRequestL
       
    85 // Check the GetPartialObject reqeust
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 EXPORT_C TMTPResponseCode CGetPartialObject::CheckRequestL()
       
    89     {
       
    90     PRINT( _L( "MM MTP => CGetPartialObject::CheckRequestL" ) );
       
    91     TMTPResponseCode result = CRequestProcessor::CheckRequestL();
       
    92     if( result == EMTPRespCodeOK && !VerifyParametersL() )
       
    93         {
       
    94         result = EMTPRespCodeInvalidParameter;
       
    95         }
       
    96 
       
    97     PRINT1( _L( "MM MTP <= CGetPartialObject::CheckRequestL result = ox%x" ), result );
       
    98     return result;
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CGetPartialObject::VerifyParametersL
       
   103 // Verify if the parameter of the request (i.e. offset) is good.
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 TBool CGetPartialObject::VerifyParametersL()
       
   107     {
       
   108     PRINT( _L( "MM MTP => CGetPartialObject::VerifyParametersL" ) );
       
   109     __ASSERT_DEBUG( iRequestChecker, Panic( EMmMTPDpRequestCheckNull ) );
       
   110     TBool result = EFalse;
       
   111     iObjectHandle = Request().Uint32( TMTPTypeRequest::ERequestParameter1 );
       
   112     PRINT1( _L( "MM MTP <> CGetPartialObject::VerifyParametersL iObjectHandle = 0x%x" ), iObjectHandle );
       
   113     iOffset = Request().Uint32( TMTPTypeRequest::ERequestParameter2 );
       
   114     PRINT1( _L( "MM MTP <> CGetPartialObject::VerifyParametersL iOffset = %d" ), iOffset );
       
   115     TUint32 maxLength = Request().Uint32( TMTPTypeRequest::ERequestParameter3 );
       
   116     PRINT1( _L( "MM MTP <> CGetPartialObject::VerifyParametersL maxLength = %d" ), maxLength );
       
   117 
       
   118     //get object info, but do not have the ownship of the object
       
   119     CMTPObjectMetaData* objectInfo = iRequestChecker->GetObjectInfo( iObjectHandle );
       
   120     __ASSERT_DEBUG( objectInfo, Panic( EMmMTPDpObjectNull ) );
       
   121 
       
   122     const TDesC& suid( objectInfo->DesC( CMTPObjectMetaData::ESuid ) );
       
   123     PRINT1( _L( "MM MTP <> CGetPartialObject::VerifyParametersL suid = %S" ), &suid );
       
   124 
       
   125     if ( objectInfo->Uint( CMTPObjectMetaData::EDataProviderId )
       
   126         == iFramework.DataProviderId() )
       
   127         result = ETrue;
       
   128 
       
   129     TEntry fileEntry;
       
   130     User::LeaveIfError( iFs.Entry( suid, fileEntry ) );
       
   131     TInt64 fileSize = fileEntry.iSize;
       
   132     if( ( iOffset < fileEntry.iSize ) && result )
       
   133         {
       
   134         if ( maxLength == fileSize )
       
   135             {
       
   136             iCompleteFile = ETrue;
       
   137             }
       
   138 
       
   139         if( iOffset + maxLength > fileSize )
       
   140             {
       
   141             maxLength = fileSize - iOffset;
       
   142             }
       
   143         iPartialDataLength = maxLength;
       
   144         result = ETrue;
       
   145         }
       
   146     PRINT1( _L( "MM MTP <> CGetPartialObject::VerifyParametersL iPartialDataLength = %d" ), iPartialDataLength );
       
   147 
       
   148     PRINT1( _L( "MM MTP <= CGetPartialObject::VerifyParametersL result = %d" ), result );
       
   149     return result;
       
   150     }
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CGetPartialObject::ServiceL
       
   154 // GetPartialObject request handler
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 EXPORT_C void CGetPartialObject::ServiceL()
       
   158     {
       
   159     PRINT( _L( "MM MTP => CGetPartialObject::ServiceL" ) );
       
   160     // Get file information
       
   161     CMTPObjectMetaData* objectInfo = iRequestChecker->GetObjectInfo( iObjectHandle );
       
   162     __ASSERT_DEBUG( objectInfo, Panic( EMmMTPDpObjectNull ) );
       
   163     iFileSuid.SetLength( 0 );
       
   164     iFileSuid.Append( objectInfo->DesC( CMTPObjectMetaData::ESuid ) );
       
   165 
       
   166     if ( iCompleteFile )
       
   167         {
       
   168         // Pass the complete file back to the host
       
   169         delete iFileObject;
       
   170         iFileObject = NULL;
       
   171         iFileObject = CMTPTypeFile::NewL( iFramework.Fs(), iFileSuid, EFileRead );
       
   172         SendDataL( *iFileObject );
       
   173         }
       
   174     else
       
   175         {
       
   176         // Send partial file fragment back.
       
   177         BuildPartialDataL();
       
   178         delete iPartialData;
       
   179         iPartialData = NULL;
       
   180         iPartialData  = new (ELeave) TMTPTypeFlatBuf( iBufferPtr8 );
       
   181         SendDataL( *iPartialData );
       
   182         }
       
   183     PRINT( _L( "MM MTP <= CGetPartialObject::ServiceL" ) );
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CGetPartialObject::DoHandleResponsePhaseL
       
   188 // Signal to the initiator how much data has been sent
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 EXPORT_C TBool CGetPartialObject::DoHandleResponsePhaseL()
       
   192     {
       
   193     PRINT( _L( "MM MTP => CGetPartialObject::DoHandleResponsePhaseL" ) );
       
   194     TUint32 dataLength = iPartialDataLength;
       
   195     PRINT1( _L( "MM MTP <> CGetPartialObject::DoHandleResponsePhaseL dataLength = %d" ), dataLength );
       
   196     SendResponseL( EMTPRespCodeOK, 1, &dataLength );
       
   197     PRINT( _L( "MM MTP <= CGetPartialObject::DoHandleResponsePhaseL" ) );
       
   198     return EFalse;
       
   199     }
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 // CGetPartialObject::ConstructL()
       
   203 // Second-phase construction
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 void CGetPartialObject::ConstructL()
       
   207     {
       
   208     SetPSStatus();
       
   209     }
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // CGetPartialObject::BuildPartialDataL()
       
   213 // Populate the partial data object
       
   214 // -----------------------------------------------------------------------------
       
   215 //
       
   216 void CGetPartialObject::BuildPartialDataL()
       
   217     {
       
   218     PRINT( _L( "MM MTP => CGetPartialObject::BuildPartialDataL" ) );
       
   219     __ASSERT_DEBUG( iRequestChecker, Panic( EMmMTPDpRequestCheckNull ) );
       
   220 
       
   221     if ( iBuffer )
       
   222         {
       
   223         delete iBuffer;
       
   224         iBuffer = NULL;
       
   225         }
       
   226 
       
   227     // We might fail if we have insufficient memory...
       
   228     iBuffer = HBufC8::NewL( iPartialDataLength );
       
   229     iBuffer->Des().Zero();
       
   230     iBufferPtr8.Set( iBuffer->Des() );
       
   231 
       
   232     RFile file;
       
   233     User::LeaveIfError( file.Open( iFs, iFileSuid, EFileRead ) );
       
   234     CleanupClosePushL( file ); // + file
       
   235     User::LeaveIfError( file.Read( iOffset, iBufferPtr8, iPartialDataLength ) );
       
   236     CleanupStack::PopAndDestroy( &file ); // - file
       
   237     PRINT( _L( "MM MTP <= CGetPartialObject::BuildPartialDataL" ) );
       
   238     }
       
   239 
       
   240 // end of file