ImagePrint/ImagePrintEngine/DeviceProtocols/btprotocol/src/cbtobjectserver.cpp
branchGCC_SURGE
changeset 25 59ea2209bb67
parent 23 08cc4cc059d4
parent 15 a92d00fca574
equal deleted inserted replaced
23:08cc4cc059d4 25:59ea2209bb67
     1 /*
       
     2 * Copyright (c) 2004-2007 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <avkon.hrh>
       
    20 #include <aknnotewrappers.h>
       
    21 
       
    22 #include "cbtprintingdevice.h"
       
    23 #include "cbtobjectserver.h"
       
    24 #include "btprotocolconsts.h"
       
    25 #include "clog.h"
       
    26 #include "printmessagecodes.h"
       
    27 #include "tbtmapper.h"
       
    28 #include "xhtmlfilecomposerconst.h"
       
    29 #include "rsutils.h"
       
    30 
       
    31 
       
    32 // ----------------------------------------------------------------------------
       
    33 // CBtObjectServer::NewL()
       
    34 // Creates a new instance of the class
       
    35 // ----------------------------------------------------------------------------
       
    36 
       
    37 CBtObjectServer* CBtObjectServer::NewL(const TBTDevAddr& aDeviceAddress,
       
    38                                                RArray<CImageInfo> &aImgArray,
       
    39                                                MBtObjectServerObserver &aObs)
       
    40 {
       
    41 
       
    42     CBtObjectServer* self = CBtObjectServer::NewLC( aDeviceAddress, aImgArray, aObs );
       
    43     CleanupStack::Pop( self );
       
    44     return self;
       
    45 }
       
    46 
       
    47 // ----------------------------------------------------------------------------
       
    48 // CBtObjectServer::NewLC()
       
    49 // Creates a new instance of the class
       
    50 // ----------------------------------------------------------------------------
       
    51 
       
    52 CBtObjectServer* CBtObjectServer::NewLC(const TBTDevAddr& aDeviceAddress,
       
    53                                                 RArray<CImageInfo> &aImgArray,
       
    54                                                 MBtObjectServerObserver &aObs)
       
    55 {
       
    56 
       
    57     CBtObjectServer* self = new ( ELeave ) CBtObjectServer( aDeviceAddress, aObs );
       
    58     CleanupStack::PushL( self );
       
    59     self->ConstructL(aImgArray);
       
    60     return self;
       
    61 
       
    62 }
       
    63 
       
    64 // ----------------------------------------------------------------------------
       
    65 // CBtObjectServer::CBtObjectServer
       
    66 // The actual constructor of the class
       
    67 // ----------------------------------------------------------------------------
       
    68 
       
    69 CBtObjectServer::CBtObjectServer(const TBTDevAddr& aDeviceAddress, MBtObjectServerObserver& aObs )
       
    70 :  	iAllowedAddress ( aDeviceAddress ),
       
    71 	iObs ( aObs ),
       
    72 	iStarted(EFalse),
       
    73 	iConnected(EFalse),
       
    74 	iTransportUp(EFalse)
       
    75 {
       
    76     LOG("CBtObjectServer::CBtObjectServer]\t default constructor");
       
    77 
       
    78    iImgArray.Reset();
       
    79    iImgArray.Close();
       
    80 }
       
    81 
       
    82 // ----------------------------------------------------------------------------
       
    83 // CBtObjectServer::~CBtObjectServer()
       
    84 // Destructor.
       
    85 // ----------------------------------------------------------------------------
       
    86 //
       
    87 CBtObjectServer::~CBtObjectServer()
       
    88 {
       
    89     LOG("CBtObjectServer::~CBtObjectServer]\t");
       
    90 
       
    91 	iStarted = EFalse;
       
    92 	iOperation = EIdle;
       
    93 
       
    94     if ( iObexServer && iObexServer->IsStarted() )
       
    95     {
       
    96         iObexServer->Stop();
       
    97     }
       
    98 
       
    99 	iFs.Close();
       
   100 
       
   101     if (iObexServer)
       
   102     	delete iObexServer;
       
   103     iObexServer = NULL;
       
   104 
       
   105 	if(iObexBufObject)
       
   106     	delete iObexBufObject;
       
   107     iObexBufObject = NULL;
       
   108 
       
   109 	// This must be deleted after the iObexBufObject who uses it.
       
   110     if (iObexBody)
       
   111 	    delete iObexBody;
       
   112     iObexBody = NULL;
       
   113 
       
   114 	if(iAdvertiser)
       
   115     	delete iAdvertiser;
       
   116     iAdvertiser = NULL;
       
   117 
       
   118     iImgArray.Close();
       
   119 }
       
   120 
       
   121 // ----------------------------------------------------------------------------
       
   122 // CBtObjectServer::Cancel()
       
   123 // Sends current file to the server and then stops the server
       
   124 // ----------------------------------------------------------------------------
       
   125 void CBtObjectServer::Cancel()
       
   126 {
       
   127     LOG("[CBtObjectServer::Cancel]\t");
       
   128 
       
   129     if (!IsSendingData())
       
   130     {
       
   131         LOG("[CBtObjectServer::Cancel]\t - not sending data - stopping server now! \n");
       
   132         Stop();
       
   133     }
       
   134     else
       
   135     {
       
   136     	iOperation = EIdle;
       
   137     }
       
   138 }
       
   139 
       
   140 // ----------------------------------------------------------------------------
       
   141 // CBtObjectServer::ConstructL()
       
   142 // Constucts everything that can leave in the class
       
   143 // ----------------------------------------------------------------------------
       
   144 void CBtObjectServer::ConstructL(RArray<CImageInfo> &aImgArray)
       
   145 {
       
   146     LOG("[CBtObjectServer::ConstructL]\t");
       
   147 
       
   148     LOG1("[CBtObjectServer::ConstructL]\t aImgArray.Count(): %d", aImgArray.Count());
       
   149 	for(TInt i = 0; i < aImgArray.Count(); ++i)
       
   150 	{
       
   151 		iImgArray.Append(aImgArray[i]);
       
   152 
       
   153 /* For debug		
       
   154 #ifdef ENABLE_LOGGING		
       
   155 		TFileName8 uri;
       
   156 		aImgArray[i].GetUri(uri);
       
   157 		TFileName8 log;
       
   158 		CImageInfo::UriLog(uri, log);
       
   159     	LOG1("[CBtObjectServer::ConstructL]\t uri: %d", log);
       
   160 #endif // ENABLE_LOGGING
       
   161 */
       
   162 	}
       
   163 
       
   164     iAdvertiser = CBtDprServiceAdvertiser::NewL();
       
   165 
       
   166     // create OBEX object to receive obex transfer. 8 for reserving data in 8 bytes segments
       
   167     iObexBody = CBufFlat::NewL( 8 );
       
   168 
       
   169     User::LeaveIfError(iFs.Connect());
       
   170 
       
   171 	InitTransferData();
       
   172 	iRemoteAddress.Reset();
       
   173 }
       
   174 
       
   175 // ----------------------------------------------------------------------------
       
   176 // CBtObjectServer::Stop()
       
   177 // Disconnects the server.
       
   178 // ----------------------------------------------------------------------------
       
   179 //
       
   180 void CBtObjectServer::Stop()
       
   181 {
       
   182     LOG("[CBtObjectServer::Stop]\t");
       
   183     
       
   184     // already stopping
       
   185     if(!iStarted)
       
   186     	return;
       
   187     
       
   188 	iOperation = ETerminating;
       
   189 
       
   190     LOG("[CBtObjectServer::Stop]\t cont...");
       
   191     iRemoteAddress.Reset();
       
   192     
       
   193     //cannot delete here. Stop called from iObexServer Callback function.
       
   194     if ( iObexServer && iObexServer->IsStarted())
       
   195     {
       
   196 	    LOG("[CBtObjectServer::Stop]\t stopping iObexServer...");
       
   197     	iObexServer->Stop();
       
   198 	}
       
   199 
       
   200     // Trap this: if it fails, we cannot do anything.
       
   201     TInt leave; // for remove compiling warnings
       
   202     TRAP(leave, iAdvertiser->StopAdvertisingL());
       
   203     LOG1("[CBtObjectServer::Stop]\t iAdvertiser->StopAdvertising() leaves with %d", leave);
       
   204 
       
   205     LOG1("[CBtObjectServer::Stop]\t iObexServer: %d", iObexServer);
       
   206 
       
   207 	iOperation = EIdle; 
       
   208 	iStarted = EFalse;
       
   209 	iConnected = EFalse;
       
   210 	iTransportUp = EFalse;
       
   211 
       
   212     /* Sending 'stopped' status */
       
   213     iObs.HandleObjectServerEvent(MBtObjectServerObserver::KObjectServerStopped);
       
   214 }
       
   215 
       
   216 // ----------------------------------------------------------------------------
       
   217 // CBtObjectServer::StartL()
       
   218 // Starts the server.
       
   219 // ----------------------------------------------------------------------------
       
   220 //
       
   221 void CBtObjectServer::StartL()
       
   222 {
       
   223     LOG("[CBtObjectServer::StartL]\t");
       
   224 
       
   225 	TBool btOn = RsUtils::IsBluetoothOn();
       
   226 	LOG1("[CBtObjectServer::StartL]\t btOn: %d", btOn);
       
   227 	if( !btOn )
       
   228 		User::Leave(KErrCancel);
       
   229 
       
   230     TRAPD( err, InitialiseServerL() );
       
   231     LOG1("CBtObjectServer::StartL]\t InitialiseServerL leaves with %d", err);
       
   232 
       
   233     if ( err != KErrNone )
       
   234     {
       
   235         Stop();
       
   236     	User::Leave(err);
       
   237     }
       
   238 
       
   239     iStarted = ETrue;
       
   240 
       
   241     /* Send 'server started' status */
       
   242     iObs.HandleObjectServerEvent(MBtObjectServerObserver::KObjectServerStarted);
       
   243 }
       
   244 
       
   245 // ----------------------------------------------------------------------------
       
   246 // CBtObjectServer::InitialiseServerL()
       
   247 // Initialises the server.
       
   248 // ----------------------------------------------------------------------------
       
   249 //
       
   250 void CBtObjectServer::InitialiseServerL()
       
   251 {
       
   252     LOG("CBtObjectServer::InitialiseServerL]\t");
       
   253 
       
   254     if ( iObexServer )
       
   255     {
       
   256         ASSERT( IsConnected() ); // server already running
       
   257         return;
       
   258     }
       
   259 
       
   260     LOG("[CBtObjectServer::InitialiseServerL]\t - getting channel");
       
   261 
       
   262 	// Set the Socket's security with parameters,
       
   263 	// Authentication, Encryption, Authorisation and Denied
       
   264 	// Method also return the channel available to listen to.
       
   265 	TInt channel = SetSecurityWithChannelL( EFalse, EFalse, EFalse, EFalse );
       
   266 
       
   267     // start the OBEX server
       
   268     TObexBluetoothProtocolInfo obexProtocolInfo;
       
   269 
       
   270     LOG1("[CBtObjectServer::InitialiseServerL]\t setting %S ", &KRFCOMMDesC);
       
   271     obexProtocolInfo.iTransport.Copy( KRFCOMMDesC() );
       
   272 
       
   273     LOG1("[CBtObjectServer::InitialiseServerL]\t setting port %d", channel);
       
   274     obexProtocolInfo.iAddr.SetPort( channel );
       
   275 
       
   276     LOG("[CBtObjectServer::InitialiseServerL]\t CObexServer::NewL()");
       
   277     if(iObexServer)
       
   278     {
       
   279     	delete iObexServer;
       
   280     	iObexServer = NULL;
       
   281     }
       
   282     iObexServer = CObexServer::NewL( obexProtocolInfo );
       
   283 
       
   284     LOG("[CBtObjectServer::InitialiseServerL]\t iObexServer->Start()");
       
   285     User::LeaveIfError(iObexServer->Start( this ) );
       
   286 
       
   287     TUUID who(KBTSDPDPROService);
       
   288 	User::LeaveIfError(iObexServer->SetLocalWho(who.LongForm()));
       
   289 
       
   290     // advertise this service
       
   291     LOG("[CBtObjectServer::InitialiseServerL]\t iAdvertiser->StartAdvertisingL()");
       
   292     iAdvertiser->StartAdvertisingL( channel );
       
   293     LOG("[CBtObjectServer::InitialiseServerL]\t iAdvertiser->UpdateAvailabilityL()");
       
   294     iAdvertiser->UpdateAvailabilityL( ETrue );
       
   295 
       
   296 }
       
   297 
       
   298 // ----------------------------------------------------------------------------
       
   299 // CBtObjectServer::InitTransferData()
       
   300 // Initialises the variables needed for progressing and processing the printing
       
   301 // ----------------------------------------------------------------------------
       
   302 //
       
   303 void CBtObjectServer::InitTransferData()
       
   304 {
       
   305     LOG("CBtObjectServer::InitTransferData]\t");
       
   306 
       
   307 	InitHeaderVariables();
       
   308 	iProgress = 0;
       
   309 
       
   310 	// delete this to be ready to send NULL object.
       
   311     if(iObexBufObject)
       
   312     	delete iObexBufObject;
       
   313     iObexBufObject = NULL;
       
   314 
       
   315     // no need to delete. Reset is enough.
       
   316     iObexBody->Reset();
       
   317 }
       
   318 
       
   319 // ----------------------------------------------------------------------------
       
   320 // CBtObjectServer::InitHeaderVariables()
       
   321 // Initialises the variables needed for progressing and processing the printing
       
   322 // ----------------------------------------------------------------------------
       
   323 //
       
   324 void CBtObjectServer::InitHeaderVariables()
       
   325 {
       
   326     LOG("CBtObjectServer::InitHeaderVariables]\t");
       
   327 
       
   328 	iOffset = 0;
       
   329 	iCount = 0; // KErrNotFound indicates "the rest"
       
   330 	iSize = KErrNotFound;
       
   331 }
       
   332 
       
   333 // ----------------------------------------------------------------------------
       
   334 // CBtObjectServer::SetSecurityWithChannelL()
       
   335 // Sets the security on the channel port and returns the available port.
       
   336 // ----------------------------------------------------------------------------
       
   337 //
       
   338 TInt CBtObjectServer::SetSecurityWithChannelL( TBool aAuthentication,
       
   339                                                   TBool aEncryption,
       
   340                                                   TBool aAuthorisation,
       
   341 												  TBool aDenied )
       
   342 
       
   343 {
       
   344 
       
   345     LOG("CBtObjectServer::SetSecurityWithChannelL]\t");
       
   346 
       
   347 	// Local variable to channel to listen to.
       
   348 	TInt channel;
       
   349 
       
   350 	RSocketServ socketServer;
       
   351 
       
   352 	// Connect to SocetServer
       
   353     LOG("[CBtObjectServer::SetSecurityWithChannelL]\t - connecting to socketServer");
       
   354 	User::LeaveIfError( socketServer.Connect() );
       
   355 	CleanupClosePushL( socketServer );
       
   356 
       
   357 	RSocket socket;
       
   358 
       
   359 	// Open the Socket connection
       
   360     LOG("[CBtObjectServer::SetSecurityWithChannelL]\t - opening a socket");
       
   361 	User::LeaveIfError( socket.Open( socketServer, KRFCOMMDesC() ) );
       
   362 	CleanupClosePushL( socket );
       
   363 
       
   364 	// Retreive to one channel that is available.
       
   365     LOG("[CBtObjectServer::SetSecurityWithChannelL]\t - get one channel");
       
   366 	User::LeaveIfError( socket.GetOpt( KRFCOMMGetAvailableServerChannel,KSolBtRFCOMM, channel ) );
       
   367 
       
   368 	// Set the Socket's Port.
       
   369 	TBTSockAddr sockaddr;
       
   370 	sockaddr.SetPort( channel );
       
   371 
       
   372     LOG("[CBtObjectServer::SetSecurityWithChannelL]\t - setting security settings");
       
   373 
       
   374 	// Set the security according to.
       
   375 	TBTServiceSecurity serviceSecurity;
       
   376 
       
   377 	serviceSecurity.SetUid ( KBtProtocolUid ); // UID for _this_ security service; internal inside the phone
       
   378 	serviceSecurity.SetAuthentication ( aAuthentication );
       
   379 	serviceSecurity.SetEncryption ( aEncryption );
       
   380 	serviceSecurity.SetAuthorisation ( aAuthorisation );
       
   381 	serviceSecurity.SetDenied( aDenied );
       
   382 
       
   383 	// Attach the security settings.
       
   384 	sockaddr.SetSecurity(serviceSecurity);
       
   385 
       
   386     LOG("[CBtObjectServer::SetSecurityWithChannelL]\t - binding socket to a address");
       
   387 
       
   388 	// Bind and start listeing the port with security set,
       
   389 	User::LeaveIfError(socket.Bind(sockaddr));
       
   390 	User::LeaveIfError(socket.Listen(KSimultainousSocketsOpen ) );
       
   391 
       
   392 	// now close the socket and the socket server
       
   393 	CleanupStack::PopAndDestroy(2);  //  socket, socketServer
       
   394 
       
   395     LOG("[CBtObjectServer::SetSecurityWithChannelL]\t - returning");
       
   396 
       
   397 	return channel;
       
   398 }
       
   399 
       
   400 // ----------------------------------------------------------------------------
       
   401 // CBtObjectServer::IsConnected()
       
   402 // Results true if the server is connected.
       
   403 // ----------------------------------------------------------------------------
       
   404 //
       
   405 TBool CBtObjectServer::IsConnected()
       
   406 {
       
   407     LOG1("CBtObjectServer::IsConnected]\t %d", iConnected);
       
   408     return iConnected;
       
   409 }
       
   410 
       
   411 
       
   412 // ----------------------------------------------------------------------------
       
   413 // CBtObjectServer::IsTransporting()
       
   414 // Results true if the transport connection is up.
       
   415 // ----------------------------------------------------------------------------
       
   416 //
       
   417 TBool CBtObjectServer::IsTransporting()
       
   418 {
       
   419     LOG1("CBtObjectServer::IsTransporting]\t %d", iTransportUp);
       
   420     return iTransportUp;
       
   421 }
       
   422 // ----------------------------------------------------------------------------
       
   423 // IsSendingData()
       
   424 // @return ETrue if the server is sending data to a Bluetooth device
       
   425 // ----------------------------------------------------------------------------
       
   426 TBool CBtObjectServer::IsSendingData()
       
   427 {
       
   428     LOG("CBtObjectServer::IsSendingData]\t");
       
   429 
       
   430 	if(ESending == iOperation)
       
   431 		return ETrue;
       
   432 	
       
   433 	return EFalse;
       
   434 }
       
   435 
       
   436 // ----------------------------------------------------------------------------
       
   437 // IsSendingData()
       
   438 // @return ETrue if the server is finished and can be deleted
       
   439 // ----------------------------------------------------------------------------
       
   440 TBool CBtObjectServer::IsFinished()
       
   441 {
       
   442     LOG("CBtObjectServer::IsFinished]\t");
       
   443 
       
   444 	if(ESending != iOperation)
       
   445 		return ETrue;
       
   446 	
       
   447 	return EFalse;
       
   448 }
       
   449 
       
   450 /*****************************************************************************/
       
   451 /* Following methods implement the Obex Observer callbacks in this class     */
       
   452 /*****************************************************************************/
       
   453 
       
   454 // ----------------------------------------------------------------------------
       
   455 // CBtObjectServer::ErrorIndication()
       
   456 // Receive error indication.
       
   457 // ----------------------------------------------------------------------------
       
   458 //
       
   459 void CBtObjectServer::ErrorIndication( TInt aError )
       
   460 {
       
   461     LOG2("CBtObjectServer::ErrorIndication]\t iOperation: %d, aError: %d", iOperation, aError);
       
   462     iObs.HandleObjectServerEvent(MBtObjectServerObserver::KObjectServerError, aError);
       
   463 }
       
   464 
       
   465 // ----------------------------------------------------------------------------
       
   466 // CBtObjectServer::TransportUpIndication()
       
   467 // Called when the underlying socket transport connection is made from
       
   468 // a remote client to the server
       
   469 // ----------------------------------------------------------------------------
       
   470 //
       
   471 void CBtObjectServer::TransportUpIndication()
       
   472 {
       
   473     LOG1("CBtObjectServer::TransportUpIndication]\t iOperation: %d", iOperation);
       
   474 
       
   475 	iTransportUp = ETrue;
       
   476 
       
   477     iObs.HandleObjectServerEvent(MBtObjectServerObserver::KObjectServerTransportUp);
       
   478 }
       
   479 
       
   480 // ----------------------------------------------------------------------------
       
   481 // CBtObjectServer::TransportDownIndication()
       
   482 // Transport connection is dropped.
       
   483 // ----------------------------------------------------------------------------
       
   484 //
       
   485 void CBtObjectServer::TransportDownIndication()
       
   486 {
       
   487     LOG1("[CBtObjectServer::TransportDownIndication]\t iOperation: %d", iOperation);
       
   488 
       
   489 	iTransportUp = EFalse;
       
   490 	
       
   491 	if(ETerminating != iOperation)
       
   492 		iObs.HandleObjectServerEvent(MBtObjectServerObserver::KObjectServerTransportDown);
       
   493 }
       
   494 
       
   495 // ----------------------------------------------------------------------------
       
   496 // CBtObjectServer::ObexConnectIndication()
       
   497 // Invoked when an OBEX connection is made from a remote client.
       
   498 // ----------------------------------------------------------------------------
       
   499 //
       
   500 TInt CBtObjectServer::ObexConnectIndication(
       
   501     const TObexConnectInfo& /*aRemoteInfo*/, const TDesC8& /*aInfo*/ )
       
   502 {
       
   503     LOG("CBtObjectServer::ObexConnectIndication]\t");
       
   504 
       
   505 	TInt message = KErrAccessDenied;
       
   506 
       
   507     /* Check the address of the remote device */
       
   508     if(AllowConnection())
       
   509     {
       
   510     	message = KErrNone;
       
   511     	iConnected = ETrue;
       
   512 	    iObs.HandleObjectServerEvent(MBtObjectServerObserver::KObjectServerConnected);
       
   513     }
       
   514 
       
   515     LOG1("CBtObjectServer::ObexConnectIndication]\t returns %d", message);
       
   516 	return message;
       
   517 }
       
   518 
       
   519 // ----------------------------------------------------------------------------
       
   520 // CBtObjectServer::ObexDisconnectIndication()
       
   521 // OBEX server has been disconnected.
       
   522 // ----------------------------------------------------------------------------
       
   523 //
       
   524 void CBtObjectServer::ObexDisconnectIndication( const TDesC8& /*aInfo*/ )
       
   525 {
       
   526     LOG("CBtObjectServer::ObexDisconnectIndication]\t");
       
   527 
       
   528 	if(ETerminating != iOperation)
       
   529 	{
       
   530 	    iObs.HandleObjectServerEvent(MBtObjectServerObserver::KObjectServerDisconnected);
       
   531 	}
       
   532    	iConnected = EFalse;
       
   533 }
       
   534 
       
   535 // ----------------------------------------------------------------------------
       
   536 // CBtObjectServer::PutRequestIndication()
       
   537 // ----------------------------------------------------------------------------
       
   538 //
       
   539 CObexBufObject* CBtObjectServer::PutRequestIndication()
       
   540 {
       
   541     LOG("CBtObjectServer::PutRequestIndication]\t");
       
   542 
       
   543     /* This is the object where server receives the client request over OBEX */
       
   544     return iObexBufObject;
       
   545 }
       
   546 
       
   547 // ----------------------------------------------------------------------------
       
   548 // CBtObjectServer::PutPacketIndication()
       
   549 // ----------------------------------------------------------------------------
       
   550 //
       
   551 TInt CBtObjectServer::PutPacketIndication()
       
   552 {
       
   553     LOG("CBtObjectServer::PutPacketIndication]\t");
       
   554 
       
   555     return KErrNone;
       
   556 }
       
   557 
       
   558 // ----------------------------------------------------------------------------
       
   559 // CBtObjectServer::PutCompleteIndication()
       
   560 // ----------------------------------------------------------------------------
       
   561 //
       
   562 TInt CBtObjectServer::PutCompleteIndication()
       
   563 {
       
   564     LOG("CBtObjectServer::PutCompleteIndication]\t");
       
   565 
       
   566     return KErrNone;
       
   567 }
       
   568 
       
   569 // ----------------------------------------------------------------------------
       
   570 // CBtObjectServer::GetRequestIndication()
       
   571 // Called when a full get request has been received from the client.
       
   572 // ----------------------------------------------------------------------------
       
   573 //
       
   574 CObexBufObject* CBtObjectServer::GetRequestIndication( CObexBaseObject* aRequestedObject )
       
   575 {
       
   576     LOG("[CBtObjectServer::GetRequestIndication]\t");
       
   577 
       
   578 	CObexBufObject* object = NULL;
       
   579 
       
   580     /* Check if connection allowed for the remote device */
       
   581     if(AllowConnection())
       
   582     {
       
   583 	    iOperation = ESending;
       
   584 	    TRAPD(leave, object = HandleGetRequestL(aRequestedObject));
       
   585         if(KErrNone != leave)
       
   586 		    LOG1("[CBtObjectServer::GetRequestIndication]\t HandleGetRequestL leaves with %d", leave );
       
   587 
       
   588 		// Stop advertising if still advertisig...
       
   589         TRAP(leave, iAdvertiser->StopAdvertisingL());
       
   590         if(KErrNone != leave)
       
   591 	    	LOG1("[CBtObjectServer::GetRequestIndication]\t StopAdvertisingL leaves with %d", leave);
       
   592     }
       
   593 
       
   594     LOG1("[CBtObjectServer::GetRequestIndication]\t return object pointer %d", object );
       
   595     return object;
       
   596 }
       
   597 
       
   598 // ----------------------------------------------------------------------------
       
   599 // CBtObjectServer::GetPacketIndication()
       
   600 // ----------------------------------------------------------------------------
       
   601 //
       
   602 TInt CBtObjectServer::GetPacketIndication()
       
   603 {
       
   604     LOG("[CBtObjectServer::GetPacketIndication]\t");
       
   605 
       
   606     /* Setting that we are now sending data */
       
   607     iOperation = ESending;
       
   608 
       
   609     return KErrNone;
       
   610 }
       
   611 
       
   612 // ----------------------------------------------------------------------------
       
   613 // CBtObjectServer::GetCompleteIndication()
       
   614 // ----------------------------------------------------------------------------
       
   615 //
       
   616 TInt CBtObjectServer::GetCompleteIndication()
       
   617 {
       
   618     LOG("[CBtObjectServer::GetCompleteIndication]\t");
       
   619 
       
   620     if(iProgress >= 100)
       
   621         // Sending status that the whole file sent to BT-device 
       
   622 	    iObs.HandleObjectServerEvent(MBtObjectServerObserver::KObjectServerFileSent, iProgress);
       
   623 	else
       
   624 	    // Sending status that one package sent to BT-device 
       
   625 	    iObs.HandleObjectServerEvent(MBtObjectServerObserver::KObjectServerPackageSent, iProgress);
       
   626 
       
   627     // We are finished for one package - init
       
   628     SetCompleted();
       
   629 
       
   630     return KErrNone;
       
   631 }
       
   632 
       
   633 // ----------------------------------------------------------------------------
       
   634 // CBtObjectServer::SetPathIndication()
       
   635 // ----------------------------------------------------------------------------
       
   636 //
       
   637 TInt CBtObjectServer::SetPathIndication( const CObex::TSetPathInfo& /*aPathInfo*/,
       
   638                                          const TDesC8& /*aInfo*/ )
       
   639 {
       
   640     LOG("CBtObjectServer::SetPathIndication]\t");
       
   641     return KErrNone;
       
   642 }
       
   643 
       
   644 // ----------------------------------------------------------------------------
       
   645 // CBtObjectServer::AbortIndication()
       
   646 // ----------------------------------------------------------------------------
       
   647 //
       
   648 void CBtObjectServer::AbortIndication()
       
   649 {
       
   650     LOG("CBtObjectServer::AbortIndication]\t");
       
   651 
       
   652     SetCompleted();
       
   653 
       
   654     /* Sending status that one file sent to BT-device */
       
   655     iObs.HandleObjectServerEvent(KErrAbort);
       
   656 
       
   657 }
       
   658 
       
   659 /*****************************************************************************/
       
   660 /* Own privates	*/
       
   661 /*****************************************************************************/
       
   662 
       
   663 // ----------------------------------------------------------------------------
       
   664 // CBtObjectServer::AllowConnection()
       
   665 // Checks if the connection should allow.
       
   666 // ----------------------------------------------------------------------------
       
   667 //
       
   668 TBool CBtObjectServer::AllowConnection()
       
   669 {
       
   670     LOG("CBtObjectServer::AllowConnection]\t");
       
   671 
       
   672     // Return immediatedly if addresses macth
       
   673     if( iRemoteAddress != 0 && iRemoteAddress == iAllowedAddress)
       
   674     	return ETrue;
       
   675     
       
   676     TBool allow = EFalse;
       
   677 
       
   678     if( iRemoteAddress == 0 ) 
       
   679     {
       
   680 	    /* Getting address of the remote device */
       
   681 	    TBTSockAddr remoteAddress;
       
   682 	    iObexServer->RemoteAddr(remoteAddress);
       
   683 
       
   684 	    /* Getting the actual bluetooth address */
       
   685 	    iRemoteAddress = remoteAddress.BTAddr();
       
   686 
       
   687 	    /* Comparing the two bluetooth device addresses */
       
   688 	    allow = (iRemoteAddress == iAllowedAddress);
       
   689 
       
   690 #ifdef _DEBUG
       
   691 
       
   692 	    TBuf<100> origName;
       
   693 	    iAllowedAddress.GetReadable(origName);
       
   694 	    TBuf<100> remoteName;
       
   695 	    iRemoteAddress.GetReadable(remoteName);
       
   696 
       
   697 		LOG1("[CBtObjectServer::AllowConnection]\t btAddressOrig: %S", &origName);
       
   698 	    LOG1("[CBtObjectServer::AllowConnection]\t btAddressRemote: %S", &remoteName);
       
   699 #endif
       
   700     }
       
   701     
       
   702 
       
   703     if (!allow)
       
   704     {
       
   705 	    LOG("[CBtObjectServer::AllowConnection]\t BT addresses DON'T match!");
       
   706         iObs.HandleObjectServerEvent(MBtObjectServerObserver::KObjectServerBTDeviceMismatch);
       
   707     }
       
   708 	else
       
   709 	{
       
   710 	    LOG("[CBtObjectServer::AllowConnection]\t BT addresses match!");
       
   711 	}
       
   712 
       
   713 	return allow;
       
   714 }
       
   715 
       
   716 // ----------------------------------------------------------------------------
       
   717 // CBtObjectServer::HandleGetRequestL()
       
   718 // Called when a full get request has been received from the client.
       
   719 // ----------------------------------------------------------------------------
       
   720 //
       
   721 CObexBufObject* CBtObjectServer::HandleGetRequestL( CObexBaseObject* aRequestedObject )
       
   722 {
       
   723 	User::LeaveIfNull(aRequestedObject);
       
   724     
       
   725     TFileName name(aRequestedObject->Name());
       
   726 
       
   727 	TFileName log;
       
   728 	CImageInfo::UriLog(name, log);
       
   729     LOG1("[CBtObjectServer::HandleGetRequestL]\t file requested: \"%S\"", &log);
       
   730 
       
   731 	/* get application parameters header */
       
   732     GetAppHeaderL(aRequestedObject);
       
   733 
       
   734 	//Append obex header start in uri
       
   735 	_LIT(KObex, "obex:");
       
   736 	_LIT(KSlash, "/");
       
   737 	TFileName scheme(KObex());
       
   738 	// Note here: the correct format would be uuid.LongForm(), 
       
   739 	// but in currently supported services it is not required.
       
   740 	scheme.AppendNum(KBTSDPDPROService, EHex);
       
   741 	scheme.Append(KSlash());
       
   742 	
       
   743 	TInt pos = name.Find(scheme);
       
   744 	if(KErrNotFound == pos)
       
   745 		name.Insert(0, scheme);
       
   746 
       
   747     return CreateObexObjectL(name);
       
   748 }
       
   749 
       
   750 // ----------------------------------------------------------------------------
       
   751 // CBtObjectServer::GetAppHeaderL()
       
   752 // ----------------------------------------------------------------------------
       
   753 //
       
   754 void CBtObjectServer::GetAppHeaderL(CObexBaseObject* aRequestedObject)
       
   755 {
       
   756     LOG("CBtObjectServer::GetAppHeaderL]\t");
       
   757 
       
   758 	/* get header parameters */
       
   759 	InitHeaderVariables();
       
   760 
       
   761 	TInt indx = 0; // grows cumulatively during GetTriplet
       
   762 	TInt tmp = 0;
       
   763 
       
   764 	// offset
       
   765 	TUint tag = 0;
       
   766 	TInt len = KBtTripletLength;
       
   767 	HBufC8* params = (aRequestedObject->AppParam()).AllocLC();
       
   768 
       
   769 	if(len <= params->Des().Length())
       
   770 	{
       
   771 		tmp = TBtMapper::TripletValue(params->Des(), indx, tag);
       
   772 	}
       
   773 	if(KBtObexTagOffset == tag)
       
   774 		iOffset = tmp;
       
   775 
       
   776     LOG1("CBtObjectServer::GetAppHeaderL]\t offset: %d", iOffset);
       
   777 
       
   778 	// count
       
   779 	tag = 0;
       
   780 	len += KBtTripletLength;
       
   781 	if(len <= params->Des().Length())
       
   782 	{
       
   783 		tmp = TBtMapper::TripletValue(params->Des(), indx, tag);
       
   784 	}
       
   785 	if(KBtObexTagCount == tag)
       
   786 		iCount = tmp;
       
   787 
       
   788     LOG1("CBtObjectServer::GetAppHeaderL]\t count: %d", iCount);
       
   789 
       
   790 	// size
       
   791 	tag = 0;
       
   792 	len += KBtTripletLength;
       
   793 	if(len <= params->Des().Length())
       
   794 	{
       
   795 		tmp = TBtMapper::TripletValue(params->Des(), indx, tag);
       
   796 	}
       
   797 	if(KBtObexTagSize == tag)
       
   798 		iSize = tmp;
       
   799 
       
   800     LOG1("CBtObjectServer::GetAppHeaderL]\t size: %d", iSize);
       
   801 
       
   802 	CleanupStack::PopAndDestroy(params);
       
   803 }
       
   804 
       
   805 // ----------------------------------------------------------------------------
       
   806 // CBtObjectServer
       
   807 //
       
   808 // ----------------------------------------------------------------------------
       
   809 CObexBufObject* CBtObjectServer::CreateObexObjectL(const TDesC& aUri)
       
   810 {
       
   811 	TFileName log;
       
   812 	CImageInfo::UriLog(aUri, log);
       
   813     LOG1("[CBtObjectServer::CreateObexObjectL]\t file requested: \"%S\"", &log);
       
   814     
       
   815     if(iObexBufObject)
       
   816     	delete iObexBufObject;
       
   817     iObexBufObject = NULL;
       
   818 
       
   819 	TInt size = KErrNotFound;
       
   820 
       
   821 	for(TInt i = 0; i < iImgArray.Count(); ++i)
       
   822 	{
       
   823 		if(iImgArray[i].CompareUri(aUri))
       
   824 		{
       
   825 			TFileName file;
       
   826 			iImgArray[i].GetFilePathL(file);
       
   827 	    	LOG1("[CBtObjectServer::CreateObexObject]\t file = \"%S\"", &file );
       
   828 
       
   829 			GetDataSequenceL(file, size);
       
   830 			if(iObexBody)
       
   831                 {
       
   832                 LOG2("[CBtObjectServer::CreateObexObject]\t data len: %d, file size: %d", iObexBody->Size(), size);
       
   833     			iObexBufObject = CObexBufObject::NewL(iObexBody);
       
   834                 }
       
   835 
       
   836 			break;
       
   837 		}
       
   838 	}
       
   839     if(!iObexBufObject)
       
   840     {
       
   841 	    LOG1("[CBtObjectServer::CreateObexObject]\t return NULL: %d", iObexBufObject);
       
   842     	return iObexBufObject;
       
   843     }
       
   844 
       
   845     // Fill headers
       
   846 	// File size (application parameters)
       
   847 
       
   848 	TBuf8<20> triplet;
       
   849 
       
   850 	triplet.Zero();
       
   851 	triplet.Append((TChar)KBtObexTagSize);
       
   852 	triplet.Append((TChar)KBtDataLength4);
       
   853 	//append value
       
   854 	triplet.Append((TChar) ((size & 0xFF000000) >> 24) );
       
   855 	triplet.Append((TChar) ((size & 0xFF0000) >> 16) );
       
   856 	triplet.Append((TChar) ((size & 0xFF00) >> 8) );
       
   857 	triplet.Append((TChar) (size & 0xFF) );
       
   858 	triplet.ZeroTerminate();
       
   859 
       
   860 	iObexBufObject->SetAppParamL(triplet);
       
   861 
       
   862    	return iObexBufObject;
       
   863 }
       
   864 
       
   865 // ----------------------------------------------------------------------------
       
   866 // CBtObjectServer::GetDataSequenceL()
       
   867 // ----------------------------------------------------------------------------
       
   868 //
       
   869 void CBtObjectServer::GetDataSequenceL(const TFileName& aFileName, TInt& aFileSize)
       
   870 {
       
   871     //LOG1("[CBtObjectServer::GetDataSequenceL]\t for file \"%S\"", aFileName);
       
   872 
       
   873 	RFile file;
       
   874 	CleanupClosePushL(file);
       
   875 	TInt err = file.Open(iFs, aFileName, EFileRead|EFileShareReadersOnly);
       
   876 	if(KErrNone != err)
       
   877 		User::LeaveIfError(file.Open(iFs, aFileName, EFileRead|EFileShareAny));
       
   878 
       
   879 	// Get file size
       
   880 	aFileSize = KErrNotFound;
       
   881 	file.Size(aFileSize);
       
   882     
       
   883 	if(!iObexBody)
       
   884 	{
       
   885 		iObexBody = static_cast<CBufFlat*>(User::LeaveIfNull(CBufFlat::NewL(8)));
       
   886 	}
       
   887 	iObexBody->Reset();
       
   888 
       
   889 	// if iCount is zero, return empty body...
       
   890 	if(0 == iCount)
       
   891 		return;
       
   892 
       
   893 	// count to send
       
   894 	TInt len = iCount;
       
   895 
       
   896 	// ...if iCount is -1, return the rest of the file
       
   897 	// (http://www.bluetooth.com/NR/rdonlyres/276DE16A-BDB9-48BF-8123-AF01E3730E5F/925/BPP_SPEC_V10.pdf)
       
   898 	if(KErrNotFound == len)
       
   899 	{
       
   900 		len = aFileSize - iOffset;
       
   901 	}
       
   902     //LOG2("[CBtObjectServer::GetDataSequenceL]\t iOffset: %d, len: %d", iOffset, len);
       
   903 
       
   904 	HBufC8* data = static_cast<HBufC8*>(User::LeaveIfNull(HBufC8::NewLC(len)));
       
   905 	TPtr8 dataPtr( data->Des() );
       
   906 
       
   907 	dataPtr.Zero();
       
   908 	User::LeaveIfError( file.Read(iOffset, dataPtr, len) );
       
   909     //LOG1("[CBtObjectServer::GetDataSequenceL]\t read data->Des().Length(): %d", data->Des().Length());
       
   910 
       
   911 	iObexBody->InsertL(0, data->Des());
       
   912 
       
   913 	CleanupStack::PopAndDestroy(2); // data, file
       
   914 	
       
   915 	//count proggress of this file
       
   916 	if(!iOffset)
       
   917 		iProgress = iOffset;
       
   918 	else
       
   919 		iProgress = (((iOffset << 8) / aFileSize) * 100) >> 8;
       
   920 	
       
   921     LOG1("[CBtObjectServer::GetDataSequenceL]\t Progress: %d", iProgress);
       
   922 	return;
       
   923 }
       
   924 
       
   925 // ----------------------------------------------------------------------------
       
   926 // CBtObjectServer::SetCompleted()
       
   927 // ----------------------------------------------------------------------------
       
   928 //
       
   929 void CBtObjectServer::SetCompleted()
       
   930 {
       
   931     // method to indicate a package is sent
       
   932     LOG("[CBtObjectServer::SetCompleted]\t");
       
   933 
       
   934 	// init requested parameters
       
   935 	InitTransferData();
       
   936 
       
   937     /* If the user of the server called Cancel() */
       
   938     if(ETerminating == iOperation)
       
   939     {
       
   940 	    LOG("[CBtObjectServer::SetCompleted]\t to stop...");
       
   941     	Stop();
       
   942     }
       
   943     else
       
   944     {
       
   945 	    LOG("[CBtObjectServer::SetCompleted]\t package sent...");
       
   946     	iOperation = EIdle;
       
   947     }
       
   948 
       
   949 	return;
       
   950 }
       
   951 
       
   952 //  End of File