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