mtpfws/mtpfw/src/cmtpsession.cpp
changeset 0 d0791faffa3f
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include <mtp/mtpprotocolconstants.h>
       
    22 
       
    23 #include "cmtpsession.h"
       
    24 
       
    25 // Class constants.
       
    26 __FLOG_STMT(_LIT8(KComponent,"Session");)
       
    27 
       
    28 #ifdef _DEBUG
       
    29 
       
    30 /**
       
    31 CMTPSession panics
       
    32 */
       
    33 _LIT(KMTPPanicCategory, "CMTPSession");
       
    34 enum TMTPPanicReasons
       
    35     {
       
    36     EMTPPanicBusy = 0,
       
    37     EMTPPanicStraySignal = 1,
       
    38     };
       
    39     
       
    40 LOCAL_C void Panic(TInt aReason)
       
    41     {
       
    42     User::Panic(KMTPPanicCategory, aReason);
       
    43     }
       
    44 
       
    45 #endif //_DEBUG
       
    46 
       
    47 /**
       
    48 CMTPSession factory method. A pointer to the new CMTPSession instance is placed
       
    49 on the cleanup stack.
       
    50 @param aMTPId The session identifier assigned by the MTP connection on which 
       
    51 the session resides. 
       
    52 @param aUniqueId The session identifier assigned by the MTP data provider framework that 
       
    53 is unique across all active connections.
       
    54 @return Pointer to the new CMTPSession instance. Ownership IS transfered.
       
    55 @leave One of the system wide error codes.
       
    56 */
       
    57 CMTPSession* CMTPSession::NewLC(TUint32 aMTPId, TUint aUniqueId)
       
    58     {
       
    59     CMTPSession* self = new(ELeave) CMTPSession(aMTPId, aUniqueId);
       
    60     CleanupStack::PushL(self);
       
    61     self->ConstructL();
       
    62     return self;
       
    63     }
       
    64 
       
    65 /**
       
    66 Destructor.
       
    67 */ 
       
    68 CMTPSession::~CMTPSession()
       
    69     {
       
    70     __FLOG(_L8("~CMTPSession - Entry"));
       
    71     iRoutingRegistrations.Close();
       
    72     __FLOG(_L8("~CMTPSession - Exit"));
       
    73     __FLOG_CLOSE;
       
    74     }
       
    75     
       
    76 /**
       
    77 Provides the next expected TransactionID. Transaction IDs are assigned 
       
    78 in incremental sequence by the MTP initiator in the range 0x00000001 to
       
    79 0xFFFFFFFE.
       
    80 @return The next TransactionID expected on the session.
       
    81 */
       
    82 TUint32 CMTPSession::ExpectedTransactionId() const
       
    83     {
       
    84     __FLOG(_L8("ExpectedTransactionId - Entry"));
       
    85     __FLOG_VA((_L8("iExpectedTransactionId = 0x%08X"), iExpectedTransactionId));
       
    86     __FLOG(_L8("ExpectedTransactionId - Exit")); 
       
    87     return iExpectedTransactionId; 
       
    88     }
       
    89 
       
    90 /**
       
    91 Increments the next expected TransactionID to the next value in the sequence.
       
    92 TransactionIDs are assigned by the MTP initiator starting from 0x00000001. 
       
    93 When the TransactionID increments to 0xFFFFFFFF it wraps back to 0x00000001.
       
    94 */
       
    95 void CMTPSession::IncrementExpectedTransactionId()
       
    96     {
       
    97     __FLOG(_L8("IncrementExpectedTransactionId - Entry"));
       
    98     if (++iExpectedTransactionId == KMTPTransactionIdLast)
       
    99         {
       
   100         iExpectedTransactionId = KMTPTransactionIdFirst;
       
   101         }
       
   102     __FLOG_VA((_L8("iExpectedTransactionId = 0x%08X"), iExpectedTransactionId));
       
   103     __FLOG(_L8("IncrementExpectedTransactionId - Exit"));
       
   104     }
       
   105 
       
   106 /**
       
   107 Sets or resets the session's active transaction request dataset. The active 
       
   108 transaction request dataset should only be set at the start of the transaction 
       
   109 (ERequestPhase), and reset and the end of the transaction (ECompletingPhase).
       
   110 @param aRequest The active transaction request dataset.
       
   111 */
       
   112 void CMTPSession::SetActiveRequestL(const TMTPTypeRequest& aRequest)
       
   113     {
       
   114     __FLOG(_L8("SetActiveRequestL - Entry"));
       
   115     MMTPType::CopyL(aRequest, iActiveRequest);    
       
   116     __FLOG(_L8("SetActiveRequestL - Exit"));
       
   117     }
       
   118 
       
   119 /**
       
   120 Sets the session's transaction phase state variable.
       
   121 @param aPhase The new transaction phase state value.
       
   122 */
       
   123 void CMTPSession::SetTransactionPhase(TMTPTransactionPhase aPhase)
       
   124     {
       
   125     __FLOG(_L8("SetTransactionPhase - Entry"));
       
   126     iTransactionPhase = aPhase;
       
   127     __FLOG_VA((_L8("iTransactionPhase = 0x%08X"), iTransactionPhase));
       
   128     __FLOG(_L8("SetTransactionPhase - Exit"));
       
   129     }
       
   130 
       
   131     
       
   132 /**
       
   133 Provides the current MTP transaction state for the session.
       
   134 @return The MTP transaction state for the session.
       
   135 */
       
   136 TMTPTransactionPhase CMTPSession::TransactionPhase() const
       
   137     {
       
   138     __FLOG(_L8("TransactionPhase - Entry"));
       
   139     __FLOG_VA((_L8("iTransactionPhase = 0x%08X"), iTransactionPhase));
       
   140     __FLOG(_L8("TransactionPhase - Exit"));
       
   141 	return  iTransactionPhase;
       
   142     }
       
   143     
       
   144 TInt CMTPSession::RouteRequest(const TMTPTypeRequest& aRequest)
       
   145     {
       
   146     __FLOG(_L8("RouteRequest - Entry"));
       
   147     TInt ret(KErrNotFound);
       
   148     
       
   149     // Attempt to match the request to existing registrations.
       
   150     TInt idx(iRoutingRegistrations.FindInOrder(aRequest, CMTPSession::RouteRequestOrder));
       
   151     if (idx != KErrNotFound)
       
   152         {
       
   153         // Retrieve the request registration.
       
   154         const TMTPTypeRequest& registration(iRoutingRegistrations[idx]);
       
   155             
       
   156         /*
       
   157         Extract the registered DP ID. For convenience the DP ID is saved in 
       
   158         the registered request, in the TransactionID element (which is unused 
       
   159         for routing).
       
   160         */  
       
   161         ret = registration.Uint32(TMTPTypeRequest::ERequestTransactionID);
       
   162         
       
   163         /* 
       
   164         Recognised follow-on request types match one request occurence 
       
   165         and are then deleted.
       
   166         */
       
   167         TUint16 op(aRequest.Uint16(TMTPTypeRequest::ERequestOperationCode));
       
   168         if ((op == EMTPOpCodeSendObject) ||
       
   169             (op == EMTPOpCodeTerminateOpenCapture))
       
   170             {
       
   171             __FLOG_VA((_L8("Unregistering follow-on request 0x%08X"), op));
       
   172             iRoutingRegistrations.Remove(idx);
       
   173             }
       
   174         }
       
   175         
       
   176     __FLOG_VA((_L8("DP ID = %d"), ret));
       
   177     __FLOG(_L8("RouteRequest - Exit"));
       
   178     return ret;
       
   179     }
       
   180   
       
   181 void CMTPSession::RouteRequestRegisterL(const TMTPTypeRequest& aRequest, TInt aDpId)
       
   182     {
       
   183     __FLOG(_L8("RouteRequestRegisterL - Entry"));
       
   184     // Locate any pre-existing registration (which if found, will be overwritten).
       
   185     TInt idx(iRoutingRegistrations.FindInOrder(aRequest, CMTPSession::RouteRequestOrder));
       
   186     if (idx == KErrNotFound)
       
   187         {
       
   188         iRoutingRegistrations.InsertInOrderL(aRequest, CMTPSession::RouteRequestOrder);
       
   189         User::LeaveIfError(idx = iRoutingRegistrations.FindInOrder(aRequest, CMTPSession::RouteRequestOrder));
       
   190         }
       
   191     
       
   192     /*
       
   193     For convenience the DP ID is saved in the registered request, in the 
       
   194     TransactionID element (which is unused for routing).
       
   195     */
       
   196     iRoutingRegistrations[idx].SetUint32(TMTPTypeRequest::ERequestTransactionID, aDpId);
       
   197     __FLOG(_L8("RouteRequestRegisterL - Exit"));
       
   198     }
       
   199 
       
   200 /**
       
   201 Indicates if a routing request is registered on the session with the 
       
   202 specified MTP operation code.
       
   203 @param aOpCode The MTP operation code.
       
   204 @return ETrue if a routing request with the specified MTP operation code is 
       
   205 registered on the session, otherwise EFalse.
       
   206 */
       
   207 TBool CMTPSession::RouteRequestRegistered(TUint16 aOpCode) const
       
   208     {
       
   209     __FLOG(_L8("RouteRequestPending - Entry"));
       
   210     __FLOG(_L8("RouteRequestPending - Entry"));
       
   211     return (iRoutingRegistrations.Find(aOpCode, CMTPSession::RouteRequestMatchOpCode) != KErrNotFound);
       
   212     }
       
   213 
       
   214 void CMTPSession::RouteRequestUnregister(const TMTPTypeRequest& aRequest)
       
   215     {
       
   216     __FLOG(_L8("RouteRequestUnregister - Entry"));
       
   217     TInt idx(iRoutingRegistrations.FindInOrder(aRequest, CMTPSession::RouteRequestOrder));
       
   218     if (idx != KErrNotFound)
       
   219         {
       
   220         iRoutingRegistrations.Remove(idx);
       
   221         }
       
   222     __FLOG(_L8("RouteRequestUnregister - Exit"));
       
   223     }
       
   224     
       
   225 void CMTPSession::StorePendingEventL(const TMTPTypeEvent& aEvent)
       
   226     {
       
   227     MMTPType::CopyL(aEvent, iPendingEvent);
       
   228     }
       
   229     
       
   230 TBool CMTPSession::CheckPendingEvent(const TMTPTypeRequest& aRequest) const
       
   231     {
       
   232     TBool ret = EFalse;
       
   233     
       
   234     // Compare transaction ID in the request and any pending event
       
   235     if ( aRequest.Uint32(TMTPTypeRequest::ERequestTransactionID)  
       
   236          == iPendingEvent.Uint32(TMTPTypeEvent::EEventTransactionID) )
       
   237         {
       
   238         ret = ETrue;
       
   239         }
       
   240     
       
   241     return ret;
       
   242     }
       
   243     
       
   244 const TMTPTypeEvent& CMTPSession::PendingEvent() const
       
   245     {
       
   246     return iPendingEvent;
       
   247     }
       
   248     
       
   249 /**
       
   250 Completes the currently pending asynchronous request status with the specified
       
   251 completion code.
       
   252 @param aErr The asynchronous request completion request.
       
   253 */
       
   254 void CMTPSession::CompletePendingRequest(TInt aErr)
       
   255     {
       
   256     __FLOG(_L8("CompletePendingRequest - Entry"));
       
   257     
       
   258     if (iRequestStatus != NULL)
       
   259         {
       
   260         __ASSERT_DEBUG(*iRequestStatus == KRequestPending, Panic(EMTPPanicStraySignal));
       
   261         User::RequestComplete(iRequestStatus, aErr);
       
   262         }
       
   263     
       
   264     __FLOG(_L8("CompletePendingRequest - Exit"));
       
   265     }
       
   266     
       
   267 
       
   268 /**
       
   269 Indicates if an asynchronous request is currently pending.
       
   270 @return ETrue if an asynchronous request is currently pending, otherwise 
       
   271 EFalse.
       
   272 */
       
   273 TBool CMTPSession::RequestPending() const
       
   274     {
       
   275     return (iRequestStatus != NULL);        
       
   276     }
       
   277 
       
   278 /**
       
   279 Set the status to complete for the currently pending asynchronous request.
       
   280 @param aStatus The asynchronous request status to complete.
       
   281 */
       
   282 void CMTPSession::SetRequestPending(TRequestStatus& aStatus)
       
   283     {
       
   284     __FLOG(_L8("SetRequestPending - Entry"));
       
   285     __ASSERT_DEBUG(!iRequestStatus, Panic(EMTPPanicBusy));
       
   286     iRequestStatus = &aStatus;
       
   287     *iRequestStatus = KRequestPending;
       
   288     __FLOG(_L8("SetRequestPending - Exit"));
       
   289     }
       
   290 
       
   291 const TMTPTypeRequest& CMTPSession::ActiveRequestL() const
       
   292     {
       
   293     __FLOG(_L8("ActiveRequestL - Entry"));
       
   294     
       
   295     if (iTransactionPhase == EIdlePhase)
       
   296         {
       
   297         User::Leave(KErrNotFound);            
       
   298         }
       
   299     
       
   300     __FLOG(_L8("ActiveRequestL - Exit"));
       
   301     return iActiveRequest;  
       
   302     }
       
   303 
       
   304 TUint32 CMTPSession::SessionMTPId() const
       
   305     {
       
   306     __FLOG(_L8("SessionMTPId - Entry"));
       
   307     __FLOG_VA( (_L8("Session MTP ID = %d"), iIdMTP) );
       
   308     __FLOG(_L8("SessionMTPId - Exit"));
       
   309     return iIdMTP;        
       
   310     }
       
   311 
       
   312 TUint CMTPSession::SessionUniqueId() const
       
   313     {
       
   314     __FLOG(_L8("SessionUniqueId - Entry"));
       
   315     __FLOG(_L8("SessionUniqueId - Exit"));
       
   316     return iIdUnique;        
       
   317     }
       
   318     
       
   319 TAny* CMTPSession::GetExtendedInterface(TUid /*aInterfaceUid*/)
       
   320     {
       
   321     __FLOG(_L8("GetExtendedInterface - Entry"));
       
   322     __FLOG(_L8("GetExtendedInterface - Exit"));
       
   323     return NULL;        
       
   324     }
       
   325     
       
   326 /**
       
   327 Constructor.
       
   328 */
       
   329 CMTPSession::CMTPSession(TUint32 aMTPId, TUint aUniqueId) :
       
   330     iExpectedTransactionId(KMTPTransactionIdFirst),
       
   331     iIdMTP(aMTPId),
       
   332     iIdUnique(aUniqueId),
       
   333     iRequestStatus(NULL)
       
   334     {
       
   335     
       
   336     }
       
   337 
       
   338 void CMTPSession::ConstructL()
       
   339     {
       
   340     __FLOG_OPEN(KMTPSubsystem, KComponent);
       
   341     __FLOG(_L8("ConstructL - Entry"));
       
   342     __FLOG(_L8("ConstructL - Exit"));
       
   343     }
       
   344     
       
   345 TBool CMTPSession::RouteRequestMatchOpCode(const TUint16* aOpCode, const TMTPTypeRequest& aRequest)
       
   346     {
       
   347     return (aRequest.Uint16(TMTPTypeRequest::ERequestOperationCode) == *aOpCode);
       
   348     }
       
   349 
       
   350 TInt CMTPSession::RouteRequestOrder(const TMTPTypeRequest& aLeft, const TMTPTypeRequest& aRight)
       
   351     {
       
   352     TInt unequal(aLeft.Uint16(TMTPTypeRequest::ERequestOperationCode) - aRight.Uint16(TMTPTypeRequest::ERequestOperationCode));
       
   353     if (!unequal)
       
   354         {
       
   355         for (TUint i(TMTPTypeRequest::ERequestParameter1); ((i <= TMTPTypeRequest::ERequestParameter5) && (!unequal)); i++)
       
   356             {
       
   357             unequal = aLeft.Uint32(i) - aRight.Uint32(i);
       
   358             }
       
   359         }
       
   360     return unequal;
       
   361     }