btobexprofiles/obexsendservices/obexservicesendutils/src/BTSBPPController.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:  Basic printing profile implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "BTServiceUtils.h"
       
    22 #include "BTSBPPController.h"
       
    23 #include "BTSUDebug.h"
       
    24 
       
    25 #include <obexheaders.h>
       
    26 
       
    27 // CONSTANTS
       
    28 _LIT8( KBTSDirectPrintingUUID, "\x00\x00\x11\x18\x00\x00\x10\x00\x80\x00\x00\x80\x5F\x9B\x34\xFB" );
       
    29 _LIT8( KBTSXHTMLPrintType,     "application/vnd.pwg-xhtml-print+xml\0" );
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS ===============================
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CBTSBPPController::CBTSBPPController
       
    35 // C++ default constructor can NOT contain any code, that
       
    36 // might leave.
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CBTSBPPController::CBTSBPPController( MBTServiceObserver* aObserver,
       
    40                                       const CBTServiceParameterList* aList ) : 
       
    41                                       iServerState( EBTSBPPSrvIdle ), 
       
    42                                       iClientDone( EFalse ), 
       
    43                                       iObserverPtr( aObserver ), 
       
    44                                       iListPtr( aList )
       
    45     {
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CBTSBPPController::ConstructL
       
    50 // Symbian 2nd phase constructor can leave.
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 void CBTSBPPController::ConstructL( const TBTDevAddr& aRemoteDevice,
       
    54                                     const TUint aRemotePort,
       
    55                                     CBTEngDiscovery* aBTConnectionPtr )
       
    56     {
       
    57     FLOG(_L("[BTSU]\t CBTSBPPController::ConstructL()"));
       
    58 
       
    59     // Create an array of obex headers
       
    60     //
       
    61     RArray<CObexHeader*> headers;
       
    62     CleanupClosePushL( headers );
       
    63 
       
    64     CObexHeader* targetHdr = CObexHeader::NewL();
       
    65     CleanupStack::PushL( targetHdr );
       
    66     targetHdr->SetByteSeqL( KBTSUTargetHeader, KBTSDirectPrintingUUID );
       
    67     headers.Append( targetHdr );
       
    68 
       
    69     // Create obex client
       
    70     //    
       
    71     CreateClientL ( this, aRemoteDevice, aRemotePort, headers );        
       
    72 
       
    73     CleanupStack::Pop( 2 ); // targetHdr, headers
       
    74     headers.Close();
       
    75 
       
    76     // Start the object server if there were referenced objects
       
    77     //
       
    78     if ( iListPtr->HasAnyReferencedObjects() )
       
    79         {
       
    80         iServer = CBTSBPPObjectServer::NewL( this,
       
    81                                              aBTConnectionPtr,
       
    82                                              aRemoteDevice );
       
    83         }
       
    84 
       
    85     FLOG(_L("[BTSU]\t CBTSBPPController::ConstructL() completed"));
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CBTSBPPController::NewL
       
    90 // Two-phased constructor.
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 CBTSBPPController* CBTSBPPController::NewL( MBTServiceObserver* aObserver,                                                                              
       
    94                                             const TUint aRemotePort,
       
    95                                             const TBTDevAddr& aRemoteDevice,
       
    96                                             const CBTServiceParameterList* aList,
       
    97                                             CBTEngDiscovery* aBTConnectionPtr )
       
    98     {
       
    99     CBTSBPPController* self = new( ELeave ) CBTSBPPController( aObserver, aList );
       
   100     CleanupStack::PushL( self );
       
   101     self->ConstructL( aRemoteDevice, aRemotePort, aBTConnectionPtr );
       
   102     CleanupStack::Pop(self);
       
   103     return self;
       
   104     }
       
   105 
       
   106     
       
   107 // Destructor
       
   108 CBTSBPPController::~CBTSBPPController()
       
   109     {
       
   110     if ( iServer )
       
   111         {
       
   112         delete iServer;
       
   113         iServer = NULL;
       
   114         }
       
   115     if ( iServerWait )
       
   116         {            
       
   117         delete iServerWait;
       
   118         iServerWait = NULL;
       
   119         }
       
   120     }
       
   121 
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // CBTSBPPController::ConnectCompleted
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 void CBTSBPPController::ConnectCompleted( TInt aStatus )
       
   128     {
       
   129     FTRACE(FPrint(_L("[BTSU]\t CBTSBPPController::ConnectCompleted() %d"), aStatus ));
       
   130 
       
   131     if ( aStatus )
       
   132         {
       
   133         // The connect operation failed.
       
   134         //
       
   135         iObserverPtr->ControllerComplete( EBTSConnectingFailed );
       
   136         }
       
   137     else
       
   138         {
       
   139         TRAPD( error, SelectAndSendL() );
       
   140 
       
   141         if ( error )
       
   142             {
       
   143             FTRACE(FPrint(_L("[BTSU]\t CBTSBPPController::SelectAndSendL() leaved with %d"), error ));
       
   144             iObserverPtr->ControllerComplete( EBTSPuttingFailed );
       
   145             }
       
   146         }
       
   147 
       
   148     FLOG(_L("[BTSU]\t CBTSBPPController::ConnectCompleted() completed"));
       
   149     }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CBTSBPPController::PutCompleted
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void CBTSBPPController::PutCompleted( TInt aStatus, const CObexHeaderSet* /*aPutResponse*/ )
       
   156     {
       
   157     FLOG(_L("[BTSU]\t CBTSBPPController::PutCompleted()"));
       
   158 
       
   159 	if ( aStatus )
       
   160         {
       
   161         // The put operation failed.
       
   162         //
       
   163         iObserverPtr->ControllerComplete( aStatus );
       
   164         }
       
   165     else
       
   166         {
       
   167         iClientDone = ETrue;
       
   168 
       
   169         if ( iServer == NULL                  || // Server was not needed at all or
       
   170              !iServer->HasReferencedObjects() || // no referenced objects in current file or
       
   171              iServerState == EBTSBPPSrvDone )    // server is done.
       
   172             {
       
   173             // Object sent and server done, check if there are 
       
   174             // more files to send.
       
   175             //
       
   176             TRAPD( error, SelectAndSendL() );
       
   177 
       
   178             if ( error )
       
   179                 {
       
   180                 FTRACE(FPrint(_L("[BTSU]\t CBTSBPPController::SelectAndSendL() leaved with %d"), error ));
       
   181                 iObserverPtr->ControllerComplete( EBTSPuttingFailed );
       
   182                 }
       
   183             }
       
   184         else if ( iServerState == EBTSBPPSrvIdle )
       
   185             {
       
   186             __ASSERT_DEBUG( iServerWait == NULL, BTSUPanic( EBTSUPanicExistingObject ) );
       
   187 
       
   188             // Object sent but server isn't serving yet. 
       
   189             // Wait for a connection attempt.
       
   190             //
       
   191 
       
   192             TRAPD(error, iServerWait = CBTSBPPServerWait::NewL( this ));
       
   193 			
       
   194 			if ( error )
       
   195                 {
       
   196                 FTRACE(FPrint(_L("[BTSU]\t CBTSBPPServerWait::NewL() leaved with %d"), error ));
       
   197                 iObserverPtr->ControllerComplete( EBTSPuttingFailed );
       
   198                 }
       
   199 
       
   200             }
       
   201         }
       
   202 
       
   203     FLOG(_L("[BTSU]\t CBTSBPPController::PutCompleted() completed"));
       
   204     }
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // CBTSBPPController::GetCompleted
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 void CBTSBPPController::GetCompleted( TInt /*aStatus*/, CObexBufObject* /*aGetResponse*/ )
       
   211     {
       
   212     FLOG(_L("[BTSU]\t CBTSBPPController::GetCompleted() ERROR: unsolicited callback"));
       
   213     __ASSERT_DEBUG( EFalse, BTSUPanic( EBTSUPanicInternalError ) );
       
   214     }
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // CBTSBPPController::ClientConnectionClosed
       
   218 // -----------------------------------------------------------------------------
       
   219 //
       
   220 void CBTSBPPController::ClientConnectionClosed()
       
   221     {
       
   222     FLOG(_L("[BTSU]\t CBTSBPPController::ClientConnectionClosed()"));
       
   223 
       
   224     // Everything is now ready.
       
   225     //
       
   226     iObserverPtr->ControllerComplete( EBTSNoError );
       
   227 
       
   228     FLOG(_L("[BTSU]\t CBTSBPPController::ClientConnectionClosed() completed"));
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CBTSBPPController::ServerError
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 void CBTSBPPController::ServerError( TInt aError )
       
   236     {
       
   237     FTRACE(FPrint(_L("[BTSU]\t CBTSBPPController::ServerError() %d"), aError ) );
       
   238 
       
   239     // Mark server as ready and if client is ready, complete the controller.
       
   240     //
       
   241     iServerState = EBTSBPPSrvDone;
       
   242 
       
   243     if ( iClientDone )
       
   244         {
       
   245         iObserverPtr->ControllerComplete( aError );
       
   246         }
       
   247     else
       
   248         {
       
   249         // Server ready, no need to wait anymore
       
   250         //
       
   251         if ( iServerWait )
       
   252             {            
       
   253             delete iServerWait;
       
   254             iServerWait = NULL;
       
   255             }
       
   256         }
       
   257 
       
   258     FLOG(_L("[BTSU]\t CBTSBPPController::ServerError() completed"));
       
   259     }
       
   260 
       
   261 // -----------------------------------------------------------------------------
       
   262 // CBTSBPPController::ServerConnectionEstablished
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 void CBTSBPPController::ServerConnectionEstablished()
       
   266     {
       
   267     FLOG(_L("[BTSU]\t CBTSBPPController::ServerConnectionEstablished()"));
       
   268 
       
   269     iServerState = EBTSBPPSrvServing;
       
   270 
       
   271     if ( iServerWait )
       
   272         {
       
   273         // The wait for server connection can now be stopped.
       
   274         //
       
   275         delete iServerWait;
       
   276         iServerWait = NULL;
       
   277         }
       
   278 
       
   279     FLOG(_L("[BTSU]\t CBTSBPPController::ServerConnectionEstablished() completed"));
       
   280     }
       
   281 
       
   282 // -----------------------------------------------------------------------------
       
   283 // CBTSBPPController::ServerConnectionClosed
       
   284 // -----------------------------------------------------------------------------
       
   285 //
       
   286 void CBTSBPPController::ServerConnectionClosed()
       
   287     {
       
   288     FLOG(_L("[BTSU]\t CBTSBPPController::ServerConnectionClosed()"));
       
   289 
       
   290     iServerState = EBTSBPPSrvDone;
       
   291 
       
   292     if ( iClientDone )
       
   293         {
       
   294         // The client was waiting for server, but now another object 
       
   295         // can be sent.
       
   296         //
       
   297         TRAPD( error, SelectAndSendL() );
       
   298 
       
   299         if ( error )
       
   300             {
       
   301             iObserverPtr->ControllerComplete( error );
       
   302             }
       
   303         }
       
   304 
       
   305     FLOG(_L("[BTSU]\t CBTSBPPController::ServerConnectionClosed() completed"));
       
   306     }
       
   307 
       
   308 // -----------------------------------------------------------------------------
       
   309 // CBTSBPPController::WaitComplete
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 void CBTSBPPController::WaitComplete()
       
   313     {
       
   314     FLOG(_L("[BTSU]\t CBTSBPPController::WaitComplete()"));
       
   315 
       
   316     // For some reason the printer didn't establish server connection although
       
   317     // there were referenced objects
       
   318     //
       
   319     if ( iServerWait )
       
   320         {   
       
   321         delete iServerWait;
       
   322         iServerWait = NULL;
       
   323         }
       
   324 
       
   325     // The client was waiting for server, but now another object 
       
   326     // can be sent.
       
   327     //
       
   328     TRAPD( error, SelectAndSendL() );
       
   329 
       
   330     if ( error )
       
   331         {
       
   332         FTRACE(FPrint(_L("[BTSU]\t CBTSBPPController::SelectAndSendL() leaved with %d"), error ));
       
   333         iObserverPtr->ControllerComplete( EBTSPuttingFailed );
       
   334         }
       
   335 
       
   336     FLOG(_L("[BTSU]\t CBTSBPPController::WaitComplete() completed"));
       
   337     }
       
   338 
       
   339 // -----------------------------------------------------------------------------
       
   340 // CBTSBPPController::SelectAndSendL
       
   341 // -----------------------------------------------------------------------------
       
   342 //
       
   343 void CBTSBPPController::SelectAndSendL()
       
   344     {
       
   345     FLOG(_L("[BTSU]\t CBTSBPPController::SelectAndSendL()"));
       
   346 
       
   347     TBTSUXhtmlParam param;
       
   348     param.iFileName = NULL;
       
   349     param.iRefObjectList = NULL;
       
   350    
       
   351     if ( iListPtr->XhtmlCount() > 0 && iSendIndex < iListPtr->XhtmlCount() )
       
   352         {
       
   353         // Get next xhtml parameter
       
   354         //
       
   355         param = iListPtr->XhtmlAt( iSendIndex );
       
   356         iSendIndex++;
       
   357         }
       
   358 
       
   359     if ( param.iFileName != NULL )
       
   360         {
       
   361         FTRACE(FPrint(_L("[BTSU]\t CBTSBPPController::SelectAndSendL() fileName='%S'"), param.iFileName ));
       
   362 
       
   363         if ( iServer )
       
   364             {
       
   365             // Pass the list of referenced objects in the file to object server.
       
   366             //
       
   367             iServer->SetReferencedObjectList( param.iRefObjectList );
       
   368             }
       
   369 
       
   370         // Create an array of obex headers
       
   371         //
       
   372         RArray<CObexHeader*> headers;
       
   373         CleanupClosePushL( headers );
       
   374 
       
   375         CObexHeader* typeHdr = CObexHeader::NewL();
       
   376         CleanupStack::PushL( typeHdr );
       
   377         typeHdr->SetByteSeqL( KBTSUTypeHeader, KBTSXHTMLPrintType );
       
   378         headers.Append( typeHdr );
       
   379 
       
   380         // Send object.
       
   381         //
       
   382         iClient->PutObjectL( headers, *param.iFileName );
       
   383         
       
   384         CleanupStack::Pop( 2 ); // typeHdr, headers
       
   385         headers.Close();
       
   386 
       
   387         // A new object is being sent. Reset states.
       
   388         //
       
   389         iClientDone = EFalse;
       
   390         iServerState = EBTSBPPSrvIdle;
       
   391         }
       
   392     else
       
   393         {
       
   394         FLOG(_L("[BTSU]\t CBTSBPPController::SelectAndSendL() all objects sent, closing connection"));
       
   395 
       
   396         // All objects sent, close client connection.
       
   397         //
       
   398         iClient->CloseClientConnection();
       
   399         }
       
   400 
       
   401     FLOG(_L("[BTSU]\t CBTSBPPController::SelectAndSendL() completed"));
       
   402     }
       
   403 
       
   404 //-----------------------------------------------------------------------------
       
   405 // void CBTSBPPController::ResetFileHandleL
       
   406 // -----------------------------------------------------------------------------
       
   407 //    
       
   408 void CBTSBPPController::ResetFileHandleL()
       
   409     {    
       
   410     }
       
   411 
       
   412 //-----------------------------------------------------------------------------
       
   413 // void CBTSBPPController::ConnectTimedOut()
       
   414 // -----------------------------------------------------------------------------
       
   415 //        
       
   416 void CBTSBPPController::ConnectTimedOut()    
       
   417     {
       
   418     iObserverPtr->ConnectTimedOut();
       
   419     }
       
   420 //  End of File