ImagePrint/ImagePrintEngine/DeviceProtocols/btprotocol/src/cbtchannelbase.cpp
branchRCL_3
changeset 28 d59c248c9d36
parent 0 d11fb78c4374
equal deleted inserted replaced
27:159fc2f68139 28:d59c248c9d36
       
     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:  Defines the CBtChannelBase class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <obex.h>
       
    20 
       
    21 #include "crsbtdevice.h"
       
    22 #include "cbtchannelbase.h"
       
    23 #include "cbtsoapcodec.h"
       
    24 #include "clog.h"
       
    25 #include "tbtmapper.h"
       
    26 #include "cbtdiscover.h"
       
    27 #include "printmessagecodes.h"
       
    28 #include "rsutils.h"
       
    29 
       
    30 //--------------------------------------------------------------------------------------------
       
    31 //
       
    32 // CBtChannelBase::CBtChannelBase
       
    33 //
       
    34 //--------------------------------------------------------------------------------------------
       
    35 CBtChannelBase::CBtChannelBase(MBtPCObserver& aObs):
       
    36 	CActive(CActive::EPriorityStandard),
       
    37 	iObs( aObs ),
       
    38 	iState( EStateNone ),
       
    39 	iJobId( KErrNotFound ),
       
    40 	iChannel( NULL ),
       
    41 	iSoapCodec( NULL ),
       
    42 	iObexBufObject( NULL ),
       
    43 	iObexNullObject( NULL ),
       
    44 	iObexBody( NULL ),
       
    45 	iDevice( NULL ),
       
    46 	iWait( NULL ),
       
    47 	iResponseError(KErrNone),
       
    48 	iStop(EFalse)
       
    49 {
       
    50 	LOG("[CBtChannelBase::CBtChannelBase]\t");
       
    51 	CActiveScheduler::Add(this);
       
    52 }
       
    53 
       
    54 //--------------------------------------------------------------------------------------------
       
    55 //
       
    56 // CBtChannelBase::~CBtChannelBase
       
    57 //
       
    58 //--------------------------------------------------------------------------------------------
       
    59 CBtChannelBase::~CBtChannelBase()
       
    60 {
       
    61 	LOG("[CBtChannelBase::~CBtChannelBase]\t");
       
    62 
       
    63 	if(iWait)
       
    64 	{
       
    65 		StopWaiting();
       
    66 		delete iWait;
       
    67 	}
       
    68 
       
    69 	ClearConnection();
       
    70 
       
    71 	if(iSoapCodec)
       
    72 		delete iSoapCodec;
       
    73 
       
    74 	if(iObexNullObject)
       
    75 		delete iObexNullObject;
       
    76 
       
    77 	if(iObexBufObject)
       
    78 		delete iObexBufObject;
       
    79 
       
    80 	if(iObexBody)
       
    81 		delete iObexBody;
       
    82 
       
    83 	if(iChannel)
       
    84 	{
       
    85 		delete iChannel;
       
    86 		iChannel = NULL;
       
    87 	}
       
    88 	if (iPasswordQuery)
       
    89 	{
       
    90 		delete iPasswordQuery;
       
    91 		iPasswordQuery = NULL;
       
    92 	}
       
    93 }
       
    94 
       
    95 //--------------------------------------------------------------------------------------------
       
    96 //
       
    97 // CBtChannelBase::ConstructL
       
    98 //
       
    99 //--------------------------------------------------------------------------------------------
       
   100 void CBtChannelBase::ConstructL()
       
   101 {
       
   102 	LOG("[CBtChannelBase::ConstructL]\t begin");
       
   103 
       
   104 	iWait = new (ELeave) CActiveSchedulerWait;
       
   105 
       
   106 	LOG("[CBtChannelBase::ConstructL]\t iSoapCodec");
       
   107 	iSoapCodec = CBtSoapCodec::NewL();
       
   108 
       
   109 	// Create obex objects here: do not NEVER delete them but in destructor
       
   110 	iObexBody = CBufFlat::NewL(8);
       
   111 	iObexBufObject = CObexBufObject::NewL(iObexBody);
       
   112 	iObexNullObject = CObexNullObject::NewL();
       
   113 }
       
   114 
       
   115 //--------------------------------------------------------------------------------------------
       
   116 //
       
   117 // CBtChannelBase::SetDevice
       
   118 //
       
   119 //--------------------------------------------------------------------------------------------
       
   120 void CBtChannelBase::SetJobId( TInt aId )
       
   121 {
       
   122 	iJobId = aId;
       
   123 }
       
   124 
       
   125 //--------------------------------------------------------------------------------------------
       
   126 //
       
   127 // CBtChannelBase::Device
       
   128 //
       
   129 //--------------------------------------------------------------------------------------------
       
   130 TInt CBtChannelBase::JobId() const
       
   131 {
       
   132 	return iJobId;
       
   133 }
       
   134 
       
   135 //--------------------------------------------------------------------------------------------
       
   136 //
       
   137 // CBtChannelBase::SetDevice
       
   138 //
       
   139 //--------------------------------------------------------------------------------------------
       
   140 void CBtChannelBase::SetDevice( CRsBtDevice& aDevice )
       
   141 {
       
   142 	iDevice = &aDevice;
       
   143 	LOG1("[CBtChannelBase::SetDevice]\t iDevice %d", iDevice);
       
   144 }
       
   145 
       
   146 //--------------------------------------------------------------------------------------------
       
   147 //
       
   148 // CBtChannelBase::Device
       
   149 //
       
   150 //--------------------------------------------------------------------------------------------
       
   151 const CRsBtDevice& CBtChannelBase::Device() const
       
   152 {
       
   153 	return *iDevice;
       
   154 }
       
   155 
       
   156 
       
   157 //--------------------------------------------------------------------------------------------
       
   158 //
       
   159 // CBtChannelBase::StartL
       
   160 //
       
   161 //--------------------------------------------------------------------------------------------
       
   162 void CBtChannelBase::StartL( CRsBtDevice& aDevice )
       
   163 {
       
   164 	LOG1("[CBtChannelBase::StartL]\t state %d", iState);
       
   165 
       
   166 	User::LeaveIfNull(&aDevice);
       
   167 	
       
   168 	Stop();
       
   169 
       
   170 	iStop = EFalse;
       
   171 
       
   172 	TBool btOn = RsUtils::IsBluetoothOn();
       
   173 	LOG1("[CBtChannelBase::StartL]\t btOn: %d", btOn);
       
   174 	if( !btOn )
       
   175 		User::Leave( EObexConnectError );
       
   176 
       
   177 	SetDevice( aDevice );
       
   178 	iState = EStateNone;
       
   179 	ConnectL();
       
   180 	WaitL();
       
   181 
       
   182 	LOG("[CBtChannelBase::StartL]\t out");
       
   183 }
       
   184 
       
   185 //--------------------------------------------------------------------------------------------
       
   186 //
       
   187 // CBtChannelBase::Stop
       
   188 //
       
   189 //--------------------------------------------------------------------------------------------
       
   190 void CBtChannelBase::Stop()
       
   191 {
       
   192 	LOG("[CBtChannelBase::Stop]\t");
       
   193 
       
   194 	iStop = ETrue;
       
   195 
       
   196 	if( IsActive() )
       
   197 	{
       
   198 		Cancel();
       
   199 	}
       
   200 	else
       
   201 		StopWaiting();
       
   202 	
       
   203 	ClearConnection();
       
   204 	LOG("[CBtChannelBase::Stop]\t out");
       
   205 }
       
   206 
       
   207 //--------------------------------------------------------------------------------------------
       
   208 //
       
   209 // CBtChannelBase::DoCancel
       
   210 //
       
   211 //--------------------------------------------------------------------------------------------
       
   212 void CBtChannelBase::DoCancel()
       
   213 {
       
   214 	LOG("[CBtChannelBase::DoCancel]\t");
       
   215 
       
   216 	StopWaiting();
       
   217 	
       
   218 	LOG1("[CBtChannelBase::DoCancel]\t iChannel %d", iChannel);
       
   219 	if(iChannel)
       
   220 	{
       
   221 		LOG1("[CBtChannelBase::DoCancel]\t iStatus.Int(): 0x%X", iStatus.Int());
       
   222 		if(KRequestPending == iStatus.Int())
       
   223 		{
       
   224 			DeleteChannel();
       
   225 		}
       
   226 	}
       
   227 	
       
   228 	LOG1("[CBtChannelBase::DoCancel]\t out iStatus 0x%X", iStatus.Int());
       
   229 }
       
   230 
       
   231 //------------------------------
       
   232 // InCalls
       
   233 //------------------------------
       
   234 
       
   235 //--------------------------------------------------------------------------------------------
       
   236 //
       
   237 // CBtChannelBase::IsConnected
       
   238 //
       
   239 //--------------------------------------------------------------------------------------------
       
   240 TBool CBtChannelBase::IsConnected()
       
   241 {
       
   242 	TBool connected = EFalse;
       
   243 
       
   244 	if(iChannel && iChannel->IsConnected())
       
   245 	{
       
   246 		connected = ETrue;
       
   247 	}
       
   248 
       
   249 	LOG2("[CBtChannelBase::IsConnected]\t iChannel: %d, connected: %d", iChannel, connected);
       
   250 
       
   251 	return connected;
       
   252 }
       
   253 
       
   254 //--------------------------------------------------------------------------------------------
       
   255 //
       
   256 // CBtChannelBase::Disconnect
       
   257 //
       
   258 //--------------------------------------------------------------------------------------------
       
   259 void CBtChannelBase::Disconnect()
       
   260 {
       
   261 	TBool conn = IsConnected();
       
   262 	LOG1("[CBtChannelBase::Disconnect]\t IsConnected(): %d", conn);
       
   263 	
       
   264 	if( conn )
       
   265 	{
       
   266 		if(IsActive())
       
   267 		{
       
   268 			LOG("[CBtChannelBase::Disconnect]\t iChannel abort");
       
   269 			iChannel->Abort();
       
   270 		}
       
   271 		else
       
   272 		{
       
   273 			LOG("[CBtChannelBase::Disconnect]\t iChannel disconnect");
       
   274 			iState = EStateDisconnecting;
       
   275 			iChannel->Disconnect(iStatus);
       
   276 			Activate();
       
   277 		}
       
   278 		iStop = ETrue;
       
   279 	}
       
   280 	else
       
   281 	{
       
   282 		DeleteChannel();
       
   283 		iState = EStateNone;
       
   284 	}
       
   285 
       
   286 	LOG1("[CBtChannelBase::Disconnect]\t out. iState: %d", iState);
       
   287 	return;
       
   288 }
       
   289 
       
   290 //--------------------------------------------------------------------------------------------
       
   291 //
       
   292 // CBtChannelBase::FinishPrinting
       
   293 //
       
   294 //--------------------------------------------------------------------------------------------
       
   295 void CBtChannelBase::FinishPrinting()
       
   296 {
       
   297 	LOG("[CBtChannelBase::FinishPrintingL]\t begin");
       
   298 
       
   299 	// Already finishing
       
   300 	if(EStateFinish == iState)
       
   301 	{
       
   302 		LOG("[CBtChannelBase::FinishPrintingL]\t already finishing");
       
   303 		return;
       
   304 	}
       
   305 
       
   306 	iState = EStateDisconnecting;
       
   307 
       
   308 	LOG1("[CBtChannelBase::FinishPrintingL]\t IsActive() %d", IsActive());
       
   309 	if(IsActive())
       
   310 	{
       
   311 		//return: the iState causes disconnection printing when RunL called.
       
   312 		return;
       
   313 	}
       
   314 
       
   315 	IsConnected();
       
   316 		Disconnect();
       
   317 
       
   318 	LOG("[CBtChannelBase::FinishPrintingL]\t end");
       
   319     return;
       
   320 }
       
   321 
       
   322 //--------------------------------------------------------------------------------------------
       
   323 //
       
   324 // CBtChannelBase::SendSoapRequestL
       
   325 //
       
   326 //--------------------------------------------------------------------------------------------
       
   327 void CBtChannelBase::SendSoapRequestL(const TInt aRequest, const TInt aJobId)
       
   328 {
       
   329 	SendSoapRequestL(aRequest, iStatus, aJobId);
       
   330 }
       
   331 
       
   332 //--------------------------------------------------------------------------------------------
       
   333 //
       
   334 // CBtChannelBase::SendSoapRequestL
       
   335 //
       
   336 //--------------------------------------------------------------------------------------------
       
   337 void CBtChannelBase::SendSoapRequestL(const TInt aRequest,
       
   338 								  TRequestStatus& aStatus,
       
   339 								  const TInt aJobId)
       
   340 {
       
   341 	LOG1("[CBtChannelBase::SendSoapRequestL]\t begin for %d", aRequest);
       
   342 	User::LeaveIfNull(iChannel);
       
   343 	User::LeaveIfNull(iObexBufObject); // panics if iObexBody == NULL
       
   344 
       
   345 	/* Create soap from file */
       
   346 	if(!iSoapCodec)
       
   347 	{
       
   348 		iSoapCodec = CBtSoapCodec::NewL();
       
   349 	}
       
   350     TPtrC8 soap = iSoapCodec->CreateSoapL(aRequest, aJobId);
       
   351 
       
   352 	LOG2("[CBtChannelBase::SendSoapRequestL]\t Soap created for %d. len: %d",aRequest, soap.Length());
       
   353 
       
   354 	iObexBufObject->Reset();
       
   355 
       
   356 	iObexBody->InsertL(0, soap);
       
   357 	LOG1("[CBtChannelBase::SendSoapRequestL]\t data size %d", iObexBody->Size());
       
   358 
       
   359 	iObexBufObject->SetTypeL(KBtMimeTypeSoap());
       
   360 
       
   361 	LOG("[CBtChannelBase::SendSoapRequestL]\t Issuing get.");
       
   362 	iChannel->Get(*iObexBufObject, aStatus);
       
   363 
       
   364 	Activate();
       
   365 	LOG("[CBtChannelBase::SendSoapRequestL]\t end");
       
   366 }
       
   367 
       
   368 //--------------------------------------------------------------------------------------------
       
   369 //
       
   370 // CBtChannelBase::GetSoapResponseL
       
   371 //
       
   372 //--------------------------------------------------------------------------------------------
       
   373 TInt CBtChannelBase::GetSoapResponseL()
       
   374 {
       
   375 	LOG("[CBtChannelBase::GetSoapResponseL]\t");
       
   376 
       
   377 	if( 0 >= iObexBody->Size() )
       
   378 		User::Leave(KErrInvalidData);
       
   379 
       
   380 	LOG("[CBtChannelBase::GetSoapResponseL]\t Data in obex body.");
       
   381 	LOG1("[CBtChannelBase::GetSoapResponseL]\t iDevice: %d", iDevice);
       
   382 	TInt action = EBtUnknownAction;
       
   383 
       
   384 	//give obexbody to parser
       
   385 	if(!iSoapCodec)
       
   386 	{
       
   387 		iSoapCodec = CBtSoapCodec::NewL();
       
   388 	}
       
   389 
       
   390    	//read
       
   391 	iSoapCodec->ReadSoapL(action, *iObexBody);
       
   392 	LOG1("[CBtChannelBase::GetSoapResponseL]\t soap read. Action %d", action);
       
   393 
       
   394 	// Reset object: data is stored in iSoapCodec.
       
   395 	// NOTE: reset resets also iObexBody
       
   396 	iObexBufObject->Reset();
       
   397 
       
   398 	LOG("[CBtChannelBase::GetSoapResponseL]\t done");
       
   399 	return action;
       
   400 }
       
   401 
       
   402 //--------------------------------------------------------------------------------------------
       
   403 //
       
   404 // CBtChannelBase::Activate
       
   405 //
       
   406 //--------------------------------------------------------------------------------------------
       
   407 void CBtChannelBase::Activate()
       
   408 {
       
   409 	// If already active do not activate again
       
   410 	LOG2("[CBtChannelBase::Activate]\t for %d, IsActive %d",iState, IsActive());
       
   411 	if( IsActive() )
       
   412 		return;
       
   413 
       
   414 	SetActive();
       
   415 }
       
   416 
       
   417 //--------------------------------------------------------------------------------------------
       
   418 //
       
   419 // CBtChannelBase::CompleteRequest
       
   420 //
       
   421 //--------------------------------------------------------------------------------------------
       
   422 void CBtChannelBase::CompleteRequest(TControlState aStateCompleted, TInt aError)
       
   423 {
       
   424 	LOG2("[CBtChannelBase::CompleteRequest]\t aStateCompleted %d, aError %d",aStateCompleted, aError);
       
   425 	iState = aStateCompleted;
       
   426 
       
   427 	LOG2("[CBtChannelBase::CompleteRequest]\t IsActive() %d, iStatus %d", IsActive(), iStatus.Int());
       
   428 	if(IsActive() && KRequestPending == iStatus.Int())
       
   429 	{
       
   430 		TRequestStatus* statusPtr = &iStatus;
       
   431 		User::RequestComplete(statusPtr, aError);
       
   432 	}
       
   433 	if(EStateNone != aStateCompleted)
       
   434 	{
       
   435 		Activate();
       
   436 	}
       
   437 }
       
   438 
       
   439 //--------------------------------------------------------------------------------------------
       
   440 //
       
   441 // CBtChannelBase::StartWaiting
       
   442 //
       
   443 //--------------------------------------------------------------------------------------------
       
   444 void CBtChannelBase::WaitL()
       
   445 {
       
   446 	if( !(iWait->IsStarted()) && KRequestPending == iStatus.Int())
       
   447 	{
       
   448 		LOG("[CBtChannelBase::WaitL]\t");
       
   449 		iWait->Start();
       
   450 
       
   451 		LOG2("[CBtChannelBase::WaitL]\t Continue, iStatus: %d at iState: %d", iStatus.Int(), iState);
       
   452 		LOG1("[CBtChannelBase::WaitL]\t iStop: %d",iStop);
       
   453 		if(KErrNone != iStatus.Int() && KRequestPending != iStatus.Int() && !iStop)
       
   454 			User::Leave(iStatus.Int());
       
   455 		LOG("[CBtChannelBase::WaitL]\t out");
       
   456 	}
       
   457 }
       
   458 
       
   459 //--------------------------------------------------------------------------------------------
       
   460 //
       
   461 // CBtChannelBase::StopWaiting
       
   462 //
       
   463 //--------------------------------------------------------------------------------------------
       
   464 void CBtChannelBase::StopWaiting()
       
   465 {
       
   466 	if( (iWait->IsStarted()) )
       
   467 	{
       
   468 		LOG("[CBtChannelBase::StopWaiting]\t");
       
   469 		iWait->AsyncStop();
       
   470 	}
       
   471 }
       
   472 
       
   473 //--------------------------------------------------------------------------------------------
       
   474 //
       
   475 // CBtChannelBase::ClearConnection
       
   476 //
       
   477 //--------------------------------------------------------------------------------------------
       
   478 void CBtChannelBase::ClearConnection()
       
   479 {
       
   480 	LOG1("[CBtChannelBase::ClearConnection]\t iState %d", iState);
       
   481 
       
   482 	Cancel();
       
   483 
       
   484 	iObexBufObject->Reset();
       
   485 	iObexNullObject->Reset();
       
   486 	iDevice = NULL;
       
   487 
       
   488     if(iChannel)
       
   489     {
       
   490 		LOG("[CBtChannelBase::ClearConnection]\t iChannel clear");
       
   491 		//Sends the abort operation to receiver and completes request with KErrAbort
       
   492 		Disconnect();	
       
   493     }
       
   494 	LOG("[CBtChannelBase::ClearConnection]\t out");
       
   495 }
       
   496 
       
   497 //--------------------------------------------------------------------------------------------
       
   498 //
       
   499 // CBtChannelBase::DeleteChannel
       
   500 //
       
   501 //--------------------------------------------------------------------------------------------
       
   502 void CBtChannelBase::DeleteChannel()
       
   503 {
       
   504 	LOG1("[CBtChannelBase::DeleteChannel]\t iChannel %d delete", iChannel);
       
   505     if(iChannel)
       
   506     {
       
   507 		delete iChannel;
       
   508 		iChannel = NULL;
       
   509     }
       
   510 	iStop = EFalse;
       
   511 
       
   512 	LOG1("[CBtChannelBase::DeleteChannel]\t iChannel %d out", iChannel);
       
   513 }
       
   514 
       
   515 //--------------------------------------------------------------------------------------------
       
   516 //
       
   517 // CBtChannelBase::GetUserPasswordL
       
   518 //
       
   519 //--------------------------------------------------------------------------------------------
       
   520 
       
   521 void CBtChannelBase::GetUserPasswordL( const TDesC &aRealm )
       
   522 {
       
   523     LOG("[CBtChannelBase::GetUserPasswordL begin]\t");
       
   524 
       
   525     iPasswordQuery = CBtPasswordQuery::NewL( *this );
       
   526     
       
   527     iPasswordQuery->StartActiveObjectL();
       
   528   
       
   529     LOG("[CBtChannelBase::GetUserPasswordL end]\t");
       
   530 }
       
   531 
       
   532 void CBtChannelBase::PasswordQueryCompleteL ( TObexPasskeyBuf aResultPckg )
       
   533 	{     
       
   534 	    iPckg = aResultPckg(); 
       
   535 	    
       
   536 	    TBuf<256> data;
       
   537 	    data.Copy(iPckg);
       
   538 	    
       
   539 	    // Send user password
       
   540 	    iChannel->UserPasswordL(data);
       
   541 	}
       
   542 
       
   543 //  End of File