btobexprofiles/obexsendservices/obexservicesendutils/src/BTServiceClient.cpp
branchRCL_3
changeset 56 9386f31cc85b
parent 55 613943a21004
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
     1 /*
       
     2 * Copyright (c) 2002-2010 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:  Obex client implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <apgcli.h> // RApaLSession
       
    22 
       
    23 #include "BTServiceClient.h"
       
    24 #include "BTServiceUtils.h"
       
    25 #include "BTConnectionTimer.h"
       
    26 #include "BTSUDebug.h"
       
    27 
       
    28 const TUint16 KMtuSizeReceiv    = 0xFFFF;	// 64kB - 1 (65535)
       
    29 const TUint16 KMtuSizeTrans     = 0x8000;	// 32kB
       
    30 const TInt    KBufferSize       = 0x8000;	// 32kB
       
    31 
       
    32 const TInt KBTConnectionTimeout = 20 * 1000 * 1000;	// 20 seconds
       
    33 const TInt KBTAbortTimeout      = 2 * 1000 * 1000;	// 2 seconds
       
    34 
       
    35 // CONSTANTS
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ===============================
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CBTServiceClient::CBTServiceClient
       
    41 // C++ default constructor can NOT contain any code, that
       
    42 // might leave.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CBTServiceClient::CBTServiceClient( MBTServiceClientObserver* aObserver ) 
       
    46     : CActive( EPriorityStandard ), 
       
    47       iClientState( EBTSCliIdle ), 
       
    48       iObserver( aObserver )                           
       
    49     {
       
    50     CActiveScheduler::Add( this );
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CBTServiceClient::ConstructL
       
    55 // Symbian 2nd phase constructor can leave.
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 void CBTServiceClient::ConstructL( const TBTDevAddr& aRemoteDevice,
       
    59                                    const TUint aRemotePort,
       
    60                                    RArray<CObexHeader*> aHeaderList )
       
    61     {
       
    62     FLOG(_L("[BTSU]\t CBTServiceClient::ConstructL()"));
       
    63 
       
    64     iTotalBytesSent = 0;
       
    65     // Create Obex Client
       
    66     //
       
    67     TObexBluetoothProtocolInfo info;
       
    68     info.iTransport = KBTSProtocol;
       
    69     info.iAddr.SetBTAddr( aRemoteDevice );
       
    70     info.iAddr.SetPort( aRemotePort );
       
    71     
       
    72     TObexProtocolPolicy obexProtocolPolicy;
       
    73     obexProtocolPolicy.SetReceiveMtu( KMtuSizeReceiv  );
       
    74     obexProtocolPolicy.SetTransmitMtu( KMtuSizeTrans  );
       
    75 	
       
    76     iClient = CObexClient::NewL( info, obexProtocolPolicy );
       
    77     iClient->SetCallBack( *this );
       
    78     iPasskeyRequest = new (ELeave) CBTSUPasskeyRequest();
       
    79 
       
    80     // Create Connect-object
       
    81     //
       
    82     iConnectObject = CObexNullObject::NewL();
       
    83 
       
    84     if ( aHeaderList.Count() > 0 )
       
    85         {
       
    86         for ( TInt index = 0; index < aHeaderList.Count(); index++ )
       
    87             {
       
    88             iConnectObject->AddHeaderL( *aHeaderList[index] );
       
    89             }
       
    90         }
       
    91 
       
    92     // Establish client connection
       
    93     //
       
    94     iClient->Connect( *iConnectObject, iStatus );
       
    95     SetActive();
       
    96     iClientState = EBTSCliConnecting;
       
    97     iConnectionTimer = CBTConnectionTimer::NewL(this);
       
    98     iConnectionTimer -> SetTimeOut ( TTimeIntervalMicroSeconds32( KBTConnectionTimeout ) );    
       
    99     iConnectionTimer -> Start();
       
   100 
       
   101     FLOG(_L("[BTSU]\t CBTServiceClient::ConstructL() completed"));
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CBTServiceClient::NewL
       
   106 // Two-phased constructor.
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 CBTServiceClient* CBTServiceClient::NewL( MBTServiceClientObserver* aObserver,
       
   110                                           const TBTDevAddr& aRemoteDevice,
       
   111                                           const TUint aRemotePort,
       
   112                                           RArray<CObexHeader*> aHeaderList )
       
   113     {
       
   114     CBTServiceClient* self = new( ELeave ) CBTServiceClient( aObserver );
       
   115     CleanupStack::PushL( self );
       
   116     self->ConstructL( aRemoteDevice, aRemotePort, aHeaderList );
       
   117     CleanupStack::Pop();
       
   118     return self;
       
   119     }
       
   120 
       
   121     
       
   122 // Destructor
       
   123 CBTServiceClient::~CBTServiceClient()
       
   124     {
       
   125     FLOG(_L("[BTSU]\t CBTServiceClient::~CBTServiceClient()"));
       
   126     
       
   127     if(iConnectionTimer)
       
   128         {
       
   129         iConnectionTimer->Cancel();
       
   130         delete iConnectionTimer;
       
   131         iConnectionTimer=NULL;
       
   132         }    
       
   133     Cancel();
       
   134     if(iClient)
       
   135         {
       
   136         delete iClient;
       
   137         iClient = NULL;
       
   138         }    
       
   139     delete iPasskeyRequest;
       
   140     iPasskeyRequest = NULL;
       
   141 
       
   142     if ( iConnectObject )
       
   143         {
       
   144         iConnectObject->Reset();
       
   145         delete iConnectObject;
       
   146         iConnectObject = NULL;
       
   147         }
       
   148     if ( iObjectBuffer )
       
   149         {
       
   150         iObjectBuffer->Reset();
       
   151         delete iObjectBuffer;
       
   152         iObjectBuffer = NULL;
       
   153         }
       
   154     if ( iGetObject )
       
   155         {
       
   156         delete iGetObject;
       
   157         iGetObject = NULL;
       
   158         }
       
   159     if ( iPutObject )
       
   160         {
       
   161         iPutObject->Reset();
       
   162         delete iPutObject;
       
   163         iPutObject = NULL;
       
   164         }
       
   165      if ( iPutBufObject )
       
   166         {
       
   167         iPutBufObject->Reset();
       
   168         delete iPutBufObject;
       
   169         iPutBufObject = NULL;
       
   170         }
       
   171            
       
   172     if(iBuffer)
       
   173         {
       
   174         delete iBuffer;
       
   175         iBuffer = NULL;
       
   176         }
       
   177 
       
   178     FLOG(_L("[BTSU]\t CBTServiceClient::~CBTServiceClient() completed"));
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CBTServiceClient::GetObjectL
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 void CBTServiceClient::GetObjectL( RArray<CObexHeader*>& aHeaderList,
       
   186                                    const TDesC& aFileName )
       
   187     {
       
   188     FLOG(_L("[BTSU]\t CBTServiceClient::GetObjectL()"));
       
   189 
       
   190     if ( iGetObject )
       
   191         {
       
   192         iGetObject->Reset();
       
   193         delete iGetObject;
       
   194         iGetObject = NULL;
       
   195         }
       
   196 
       
   197     // Create object
       
   198     //
       
   199     if ( aFileName == KNullDesC )
       
   200         {
       
   201         iObjectBuffer = CBufFlat::NewL( KBTSUDataBufferExpandSize );
       
   202         iGetObject = CObexBufObject::NewL( iObjectBuffer );
       
   203         }
       
   204     else
       
   205         {        
       
   206         iGetObject = CObexBufObject::NewL( NULL );
       
   207         iGetObject->SetDataBufL( aFileName );
       
   208         }
       
   209 
       
   210     // Set headers
       
   211     //
       
   212     if ( aHeaderList.Count() > 0 )
       
   213         {
       
   214         for ( TInt index = 0; index < aHeaderList.Count(); index++ )
       
   215             {
       
   216             iGetObject->AddHeaderL( *aHeaderList[index] );
       
   217             }
       
   218         }
       
   219 
       
   220     // Send get request
       
   221     //
       
   222     iClient->Get( *iGetObject, iStatus );
       
   223     SetActive();
       
   224     iClientState = EBTSCliGetting;
       
   225 
       
   226     FLOG(_L("[BTSU]\t CBTServiceClient::GetObjectL() completed"));
       
   227     }
       
   228 
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CBTServiceClient::PutObjectL
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 void CBTServiceClient::PutObjectL( RArray<CObexHeader*>& aHeaderList,
       
   235                                    const TDesC& aFileName )
       
   236     {
       
   237     FLOG(_L("[BTSU]\t CBTServiceClient::PutObjectL()"));
       
   238 
       
   239     if ( iPutObject )
       
   240         {
       
   241         iPutObject->Reset();
       
   242         delete iPutObject;
       
   243         iPutObject = NULL;
       
   244         }
       
   245 
       
   246     // Create object
       
   247     //
       
   248     iPutObject = CObexFileObject::NewL();
       
   249 
       
   250     // Set body
       
   251     //
       
   252     
       
   253     if ( aFileName != KNullDesC )
       
   254         {
       
   255         iPutObject->InitFromFileL ( aFileName );
       
   256         }
       
   257     
       
   258     // Set headers
       
   259     //
       
   260     if ( aHeaderList.Count() > 0 )
       
   261         {
       
   262         for ( TInt index = 0; index < aHeaderList.Count(); index++ )
       
   263             {
       
   264             iPutObject->AddHeaderL( *aHeaderList[index] );
       
   265             }
       
   266         }
       
   267 
       
   268     // Send object
       
   269     //
       
   270     iClient->Put( *iPutObject, iStatus );
       
   271 	SetActive();
       
   272     iClientState = EBTSCliPutting;
       
   273 
       
   274     FLOG(_L("[BTSU]\t CBTServiceClient::PutObjectL() completed"));
       
   275     }
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // CBTServiceClient::PutObjectL
       
   279 // -----------------------------------------------------------------------------
       
   280 //
       
   281 void CBTServiceClient::PutObjectL( RArray<CObexHeader*>& aHeaderList,
       
   282                                    RFile& aFile )
       
   283     {
       
   284     FLOG(_L("[BTSU]\t CBTServiceClient::PutObjectL()"));
       
   285 
       
   286     if ( iPutObject )
       
   287         {
       
   288         iPutObject->Reset();
       
   289         delete iPutObject;
       
   290         iPutObject = NULL;
       
   291         }
       
   292 
       
   293     if ( iPutBufObject )
       
   294         {
       
   295         iPutBufObject->Reset();
       
   296         delete iPutBufObject;
       
   297         iPutBufObject = NULL;
       
   298         }
       
   299     // Create object
       
   300     //
       
   301     iPutBufObject = CObexBufObject::NewL(NULL);
       
   302 
       
   303     // Resolve MIME type
       
   304     //
       
   305     
       
   306     RApaLsSession session;
       
   307     HBufC8* mimeType = NULL;
       
   308     TDataType type;
       
   309     
       
   310     TUid uid;
       
   311     // Set headers
       
   312     //
       
   313     if ( aHeaderList.Count() > 0 )
       
   314         {
       
   315         for ( TInt index = 0; index < aHeaderList.Count(); index++ )
       
   316             {
       
   317             iPutBufObject->AddHeaderL( *aHeaderList[index] );
       
   318             }
       
   319         }
       
   320         
       
   321     TDesC8 typeheader=iPutBufObject->Type();   
       
   322     if ( typeheader == KNullDesC8 )    
       
   323         {        
       
   324         User::LeaveIfError( session.Connect() );
       
   325         CleanupClosePushL( session );
       
   326         TInt error = session.AppForDocument( aFile, uid, type );    
       
   327         if ( error == KErrNone )
       
   328             {
       
   329             mimeType = type.Des8().AllocLC();
       
   330             iPutBufObject->SetTypeL(*mimeType);    
       
   331             CleanupStack::PopAndDestroy();
       
   332             }          
       
   333         CleanupStack::Pop(); // session
       
   334         session.Close();       
       
   335         }    
       
   336     
       
   337     //Set object information
       
   338     //
       
   339     TFileName filename;    
       
   340     aFile.Name(filename);
       
   341     
       
   342     TInt size;
       
   343     aFile.Size(size);
       
   344     iPutBufObject->SetLengthL(size);			
       
   345     iPutBufObject->SetNameL(filename);
       
   346     
       
   347     TTime time;
       
   348 	if ( aFile.Modified(time) == KErrNone )
       
   349 	    {
       
   350 		iPutBufObject->SetTimeL(time);
       
   351 	    }
       
   352 	    
       
   353     RFile file;    
       
   354     file.Duplicate(aFile);
       
   355     
       
   356     iBuffer = CBufFlat::NewL(KBufferSize);
       
   357     iBuffer ->ResizeL(KBufferSize);
       
   358     
       
   359     TObexRFileBackedBuffer bufferdetails(*iBuffer,file,CObexBufObject::ESingleBuffering);  
       
   360     iPutBufObject->SetDataBufL(bufferdetails);         
       
   361     
       
   362     // Send object
       
   363     //        
       
   364     iClient->Put( *iPutBufObject, iStatus );
       
   365 	SetActive();
       
   366     iClientState = EBTSCliPutting;
       
   367 
       
   368     FLOG(_L("[BTSU]\t CBTServiceClient::PutObjectL() completed"));
       
   369     }
       
   370 
       
   371 // -----------------------------------------------------------------------------
       
   372 // CBTServiceClient::CloseClientConnection
       
   373 // -----------------------------------------------------------------------------
       
   374 //
       
   375 void CBTServiceClient::CloseClientConnection()
       
   376     {
       
   377     FLOG(_L("[BTSU]\t CBTServiceClient::CloseClientConnection()"));
       
   378 
       
   379     iClient->Disconnect( iStatus );
       
   380    	SetActive();
       
   381     iClientState = EBTSCliDisconnecting;
       
   382 
       
   383     FLOG(_L("[BTSU]\t CBTServiceClient::CloseClientConnection() completed"));
       
   384     }
       
   385 
       
   386 // -----------------------------------------------------------------------------
       
   387 // CBTServiceClient::GetProgressStatus
       
   388 // -----------------------------------------------------------------------------
       
   389 //
       
   390 TInt CBTServiceClient::GetProgressStatus()
       
   391     {
       
   392     FLOG(_L("[BTSU]\t CBTServiceClient::GetProgressStatus()"));
       
   393 
       
   394     TInt bytesSent = 0;
       
   395     if ( iPutBufObject )
       
   396         {
       
   397         bytesSent = iPutBufObject->BytesSent();
       
   398         }
       
   399     if ( iPutObject )
       
   400         {
       
   401         bytesSent = iPutObject->BytesSent();
       
   402         }
       
   403 
       
   404     FTRACE(FPrint(_L("[BTSU]\t CBTServiceClient::GetProgressStatus() completed, bytes sent %d"), iTotalBytesSent + bytesSent ) );
       
   405 
       
   406    // return iTotalBytesSent + bytesSent;
       
   407     return bytesSent;
       
   408     }
       
   409 
       
   410 // -----------------------------------------------------------------------------
       
   411 // CBTServiceClient::GetUserPasswordL
       
   412 // -----------------------------------------------------------------------------
       
   413 //
       
   414 void CBTServiceClient::GetUserPasswordL( const TDesC& /*aRealm*/ )
       
   415     {
       
   416     FLOG(_L("[BTSU]\t CBTServiceClient::GetUserPasswordL()"));
       
   417 
       
   418     iPasskeyRequest->StartPassKeyRequestL( iClient );
       
   419 
       
   420     FLOG(_L("[BTSU]\t CBTServiceClient::GetUserPasswordL() completed"));
       
   421     }
       
   422 
       
   423 // -----------------------------------------------------------------------------
       
   424 // CBTServiceClient::DoCancel
       
   425 // -----------------------------------------------------------------------------
       
   426 //
       
   427 void CBTServiceClient::DoCancel()
       
   428     {
       
   429     FLOG(_L("[BTSU]\t CBTServiceClient::DoCancel()"));
       
   430     
       
   431     if ( iConnectionTimer )
       
   432         {
       
   433         iConnectionTimer->Cancel();
       
   434         delete iConnectionTimer;
       
   435         iConnectionTimer=NULL;
       
   436         }    
       
   437     // Deleting obexclient is the only way to cancel active requests
       
   438     //                
       
   439     if ( iClient )
       
   440         {
       
   441         delete iClient;
       
   442         iClient = NULL;
       
   443         }    
       
   444     
       
   445     FLOG(_L("[BTSU]\t CBTServiceClient::DoCancel() completed"));
       
   446     }
       
   447 
       
   448 // -----------------------------------------------------------------------------
       
   449 // CBTServiceClient::RunL
       
   450 // -----------------------------------------------------------------------------
       
   451 //
       
   452 void CBTServiceClient::RunL()
       
   453     {
       
   454     FTRACE(FPrint(_L("[BTSU]\t CBTServiceClient::RunL() status %d"), iStatus.Int() ) );
       
   455     if ( !iObserver )
       
   456         {
       
   457         return;    
       
   458         }
       
   459     switch ( iClientState )
       
   460         {
       
   461         case EBTSCliConnecting:
       
   462             {
       
   463             FLOG(_L("[BTSU]\t CBTServiceClient::RunL() EBTSCliConnecting"));
       
   464             iConnectObject->Reset();
       
   465             if(iConnectionTimer)
       
   466                 {
       
   467                 iConnectionTimer->Cancel();
       
   468                 delete iConnectionTimer;
       
   469                 iConnectionTimer=NULL;
       
   470                 }    
       
   471             iObserver->ConnectCompleted( iStatus.Int() );
       
   472             break;
       
   473             }
       
   474 
       
   475         case EBTSCliPutting:
       
   476             {
       
   477             FLOG(_L("[BTSU]\t CBTServiceClient::RunL() EBTSCliPutting"));
       
   478             if(iPutBufObject)
       
   479                 {
       
   480                 iTotalBytesSent += iPutBufObject->BytesSent();    
       
   481                 }
       
   482             else
       
   483                 {
       
   484                  iTotalBytesSent += iPutObject->BytesSent();         
       
   485                 }
       
   486 			   
       
   487 			const CObexHeaderSet& response=iClient->GetPutFinalResponseHeaders();		
       
   488 			if ( iPutBufObject )
       
   489 				{
       
   490 				iPutBufObject->Reset();
       
   491 				delete iPutBufObject;
       
   492 				iPutBufObject = NULL;
       
   493 				}
       
   494 				
       
   495 			if ( iPutObject )
       
   496 				{
       
   497 				iPutObject->Reset();
       
   498 				delete iPutObject;
       
   499 				iPutObject = NULL;
       
   500 				}
       
   501 			if(iBuffer)
       
   502                 {
       
   503                 delete iBuffer;
       
   504                 iBuffer = NULL;
       
   505                 }
       
   506 			//put there call getEnv
       
   507             iObserver->PutCompleted( iStatus.Int(), &response);
       
   508             FLOG(_L("[BTSU]\t CBTServiceClient::RunL() EBTSCliPutting done"));
       
   509             break;
       
   510             }
       
   511 
       
   512         case EBTSCliGetting:
       
   513             {
       
   514             iObserver->GetCompleted( iStatus.Int(), iGetObject );
       
   515             break;
       
   516             }
       
   517 
       
   518         case EBTSCliDisconnecting:
       
   519             {
       
   520             // Any errors are ignored
       
   521             //
       
   522             iObserver->ClientConnectionClosed();
       
   523             break;
       
   524             }
       
   525 
       
   526         case EBTSCliIdle:
       
   527         default:
       
   528            {
       
   529            FLOG(_L("[BTSU]\t CBTServiceClient::RunL() ERROR, unhandled case"));           
       
   530            break;
       
   531            }
       
   532         }
       
   533 
       
   534     FLOG(_L("[BTSU]\t CBTServiceClient::RunL() completed"));
       
   535     }
       
   536     
       
   537 // -----------------------------------------------------------------------------
       
   538 // CBTServiceClient::ConnectionTimedOut
       
   539 // -----------------------------------------------------------------------------
       
   540 //	
       
   541 void CBTServiceClient::ConnectionTimedOut()
       
   542     {
       
   543     FLOG(_L("[BTSU]\t CBTServiceClient::ConnectionTimedOut"));    
       
   544     switch ( iClientState )
       
   545         {
       
   546         case EBTSCliConnecting:
       
   547             {
       
   548             iObserver->ConnectTimedOut();      
       
   549             break;
       
   550             }
       
   551         case EBTSCliGetting:
       
   552             {
       
   553             iObserver->GetCompleted( KErrAbort, iGetObject );    
       
   554             break;
       
   555             }    
       
   556         case EBTSCliPutting:
       
   557             {
       
   558             const CObexHeaderSet& response=iClient->GetPutFinalResponseHeaders();	    
       
   559             iObserver->PutCompleted( KErrAbort, &response );    
       
   560 			break;
       
   561             }        
       
   562         default:    
       
   563         FLOG(_L("[BTSU]\t CBTServiceClient::ConnectionTimedOut unhandled client state "));
       
   564         }
       
   565           
       
   566     FLOG(_L("[BTSU]\t CBTServiceClient::ConnectionTimedOut"));    
       
   567     }
       
   568 // -----------------------------------------------------------------------------
       
   569 // CBTServiceClient::Abort
       
   570 // -----------------------------------------------------------------------------
       
   571 //	    
       
   572 void CBTServiceClient::Abort()
       
   573     {
       
   574     FLOG(_L("[BTSU]\t CBTServiceClient::Abort"));        
       
   575     if ( iClient && ( iClientState == EBTSCliPutting || iClientState == EBTSCliGetting )  )
       
   576         {    
       
   577         if ( iConnectionTimer )    
       
   578             {
       
   579             delete iConnectionTimer;
       
   580             iConnectionTimer = NULL;    
       
   581             }
       
   582         TRAPD(trapErr, iConnectionTimer = CBTConnectionTimer::NewL(this) );
       
   583         if ( trapErr  != KErrNone)
       
   584             {
       
   585             iObserver->ConnectCompleted( KErrAbort );        
       
   586             return;
       
   587             }        
       
   588         iConnectionTimer -> SetTimeOut ( TTimeIntervalMicroSeconds32( KBTAbortTimeout ) );    
       
   589         iConnectionTimer -> Start();    
       
   590         iClient->Abort();    
       
   591         }
       
   592     else if ( iClient && iClientState == EBTSCliConnecting)
       
   593         {
       
   594         iObserver->ConnectCompleted( KErrAbort );    
       
   595         }
       
   596     FLOG(_L("[BTSU]\t CBTServiceClient::Abort"));            
       
   597     
       
   598     }
       
   599 
       
   600 //  End of File