obex/obexprotocol/obextransport/src/obextransportcontrollerbase.cpp
changeset 54 4dc88a4ac6f4
parent 52 866b4af7ffbe
child 57 f6055a57ae18
equal deleted inserted replaced
52:866b4af7ffbe 54:4dc88a4ac6f4
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <ecom/ecom.h>
       
    17 #include <obex/transport/obextransportcontrollerbase.h>
       
    18 #include <obex/internal/mobexnotifyextend.h>
       
    19 #include <obexirtransportinfo.h>
       
    20 #include <obex/internal/obexactiverw.h>
       
    21 #include <obextransportinfo.h>
       
    22 #include <obex/internal/obexpacket.h>
       
    23 #include <obexpanics.h>
       
    24 #include "ObexTransportUtil.h" 
       
    25 #include "obexconnectdata.h" 
       
    26 #include "logger.h"
       
    27 #include "obextransportfaults.h"
       
    28 
       
    29 #ifdef __FLOG_ACTIVE
       
    30 _LIT8(KLogComponent, "OBEXCT");
       
    31 #endif
       
    32 
       
    33 //Category used for internal panics
       
    34 _LIT(KPanicCat, "TransCTBase");
       
    35 
       
    36 // The minimum allowed number of 'transport controller' implementations. If 
       
    37 // NewL doesn't find at least this many, then in debug NewL panics, and in 
       
    38 // release it leaves.
       
    39 const TUint KMinimumNumImplementations = 1;
       
    40 
       
    41 /**
       
    42 Constructs a CObexTransportControllerBase PlugIn object.
       
    43  
       
    44 @internalTechnology
       
    45 @return	A new CObexTransportControllerBase PlugIn object	
       
    46 @panic TransCTBase EInvalidNumberOfTransportImplementations if there is more than one implementation for a given interface uid and transport name (equal to the default data in the plugin resource file
       
    47 */
       
    48 EXPORT_C CObexTransportControllerBase* CObexTransportControllerBase::NewL(TObexTransportInfo& aTransportInfo)
       
    49 	{
       
    50 	LOG_LINE
       
    51 	LOG_STATIC_FUNC_ENTRY
       
    52 
       
    53 	//Making a copy as Collapse modifies the original iTransportName
       
    54 	TBuf<60> transportName = aTransportInfo.iTransportName;
       
    55 	TPtrC8 params = transportName.Collapse();	
       
    56 	TEComResolverParams resolverParams; 
       
    57 	resolverParams.SetDataType(*reinterpret_cast<TDesC8*>(&params));
       
    58 	
       
    59 	RImplInfoPtrArray implInfoArray;
       
    60 	REComSession::ListImplementationsL(TUid::Uid(KObexTransportControllerInterfaceUid), resolverParams, KRomOnlyResolverUid,  implInfoArray);
       
    61 	
       
    62 	CleanupResetAndDestroyPushL(implInfoArray);
       
    63 	__ASSERT_DEBUG(implInfoArray.Count() <= KMinimumNumImplementations, PANIC(KObexTransportPanicCat, EInvalidNumberOfTransportImplementations) ); 
       
    64 	if (implInfoArray.Count() <  KMinimumNumImplementations)
       
    65 		{
       
    66 		User::Leave(KErrNotFound);
       
    67 		}
       
    68 
       
    69 	const TUid uid = implInfoArray[KMinimumNumImplementations - 1]->ImplementationUid();
       
    70 	CleanupStack::PopAndDestroy(&implInfoArray);
       
    71 	
       
    72 	CObexTransportControllerBase* ptr = reinterpret_cast<CObexTransportControllerBase*> 
       
    73 		(REComSession::CreateImplementationL(uid, _FOFF(CObexTransportControllerBase, iPrivateEComUID), 
       
    74 			reinterpret_cast<TAny*>(&aTransportInfo)));
       
    75 			
       
    76 	CleanupStack::PushL(ptr);
       
    77 	// Do any base construction here. This may in future include allocation of 
       
    78 	// iFuture1.
       
    79 	ptr->BaseConstructL();
       
    80 	CleanupStack::Pop(ptr);
       
    81 	return ptr;
       
    82 	}
       
    83 	
       
    84 /**
       
    85 Constructor.
       
    86  
       
    87 @publishedPartner
       
    88 @released
       
    89 */
       
    90 EXPORT_C CObexTransportControllerBase::CObexTransportControllerBase()
       
    91 	{
       
    92 	LOG_LINE
       
    93 	LOG_FUNC
       
    94 	}
       
    95 
       
    96 /**
       
    97 This function is a place holder for future use. If iFuture1 variable is used 
       
    98 it will need this function for any allocation required.
       
    99 This function is called from CObexTransportControllerBase::NewL. 
       
   100 */
       
   101 void CObexTransportControllerBase::BaseConstructL()
       
   102 	{
       
   103 	LOG_LINE
       
   104 	LOG_FUNC
       
   105 	}
       
   106 
       
   107 /**
       
   108 Sets the owner information from the received aOwner
       
   109 
       
   110 @internalTechnology
       
   111 */
       
   112 EXPORT_C void CObexTransportControllerBase::SetOwner(MObexNotifyExtend& aOwner)
       
   113 	{
       
   114 	LOG_LINE
       
   115 	LOG_FUNC
       
   116 
       
   117 	__ASSERT_ALWAYS(iOwner==NULL, PANIC(KPanicCat, EOwnerAlreadySet));
       
   118 	iOwner = &aOwner;
       
   119 	}
       
   120 
       
   121 /**
       
   122 Destructor.
       
   123  
       
   124 @publishedPartner
       
   125 @released
       
   126 */
       
   127 EXPORT_C CObexTransportControllerBase::~CObexTransportControllerBase()
       
   128 	{
       
   129 	LOG_LINE
       
   130 	LOG_FUNC
       
   131 
       
   132 	delete iConnector;
       
   133 	DeleteTransport();
       
   134 	delete iTransportInfo;
       
   135 
       
   136 	REComSession::DestroyedImplementation(iPrivateEComUID);
       
   137 	}
       
   138 /**
       
   139 Cancels outstanding reads or writes
       
   140 
       
   141 @internalTechnology
       
   142 */
       
   143 EXPORT_C void CObexTransportControllerBase::CancelTransfers()
       
   144 	{
       
   145 	LOG_LINE
       
   146 	LOG_FUNC
       
   147 
       
   148 	if (iActiveReader)
       
   149 		{
       
   150 		iActiveReader->Cancel();
       
   151 		}
       
   152 	if (iActiveWriter)
       
   153 		{
       
   154 		iActiveWriter->Cancel();
       
   155 		}
       
   156 	}
       
   157 /**
       
   158 Calls on the constructor to take down the transport
       
   159 
       
   160 @internalTechnology
       
   161 @return TBool ETrue if the transport was taken down
       
   162 */
       
   163 EXPORT_C TBool CObexTransportControllerBase::BringTransportDown()
       
   164 	{
       
   165 	LOG_LINE
       
   166 	LOG_FUNC
       
   167 
       
   168 	DeleteTransport();
       
   169 	return iConnector->BringTransportDown();
       
   170 	}
       
   171 
       
   172 /**
       
   173 Calls ConnectL on the connector
       
   174 
       
   175 @internalTechnology
       
   176 */	
       
   177 EXPORT_C void CObexTransportControllerBase::ConnectL ()
       
   178 	{
       
   179 	LOG_LINE
       
   180 	LOG_FUNC
       
   181 
       
   182 	iConnector->ConnectL();
       
   183 	}
       
   184 
       
   185 /**
       
   186 Ask Connector to cancel connect
       
   187 
       
   188 @see CObexTransportControllerBase::ConnectL
       
   189 @internalTechnology
       
   190 */	
       
   191 EXPORT_C void CObexTransportControllerBase::CancelConnect()
       
   192 	{
       
   193 	LOG_LINE
       
   194 	LOG_FUNC
       
   195 
       
   196 	iConnector->CancelConnect();
       
   197 	}
       
   198 
       
   199 /**
       
   200 Calls accept connection on the connector
       
   201 @internalTechnology
       
   202 */
       
   203 EXPORT_C void CObexTransportControllerBase::AcceptConnectionL()
       
   204 	{
       
   205 	LOG_LINE
       
   206 	LOG_FUNC
       
   207 
       
   208 	iConnector->AcceptL();
       
   209 	}
       
   210 
       
   211 /**
       
   212 Calls Cancel Accept on he connector
       
   213 
       
   214 @see CObexTransportControllerBase::AcceptConnectionL
       
   215 @internalTechnology
       
   216 */	
       
   217 EXPORT_C void CObexTransportControllerBase::CancelAccept()
       
   218 	{
       
   219 	LOG_LINE
       
   220 	LOG_FUNC
       
   221 
       
   222 	if ( iConnector)
       
   223 		{
       
   224 		iConnector->CancelAccept();
       
   225 		}
       
   226 	}
       
   227 
       
   228 /**
       
   229 Calls signal transport error on the connector
       
   230 
       
   231 @internalTechnology
       
   232 */
       
   233 EXPORT_C void CObexTransportControllerBase::SignalTransportError()
       
   234 	{
       
   235 	LOG_LINE
       
   236 	LOG_FUNC
       
   237 
       
   238 	// If this is called during construction, iConnector could be NULL
       
   239 	if(iConnector)
       
   240 		{
       
   241 		iConnector->SignalTransportError();
       
   242 		}
       
   243 	}
       
   244 
       
   245 /**
       
   246 returns the send packet
       
   247 
       
   248 @return CObexPacket the packet that will be sent
       
   249 @internalTechnology
       
   250 */	
       
   251 EXPORT_C CObexPacket& CObexTransportControllerBase::SendPacket ()
       
   252 	{
       
   253 	LOG_LINE
       
   254 	LOG_FUNC
       
   255 
       
   256 	return *iSendPacket;
       
   257 	}
       
   258 
       
   259 /**
       
   260 Returns the packet received
       
   261 
       
   262 @return CObexPacket the packet that will be sent
       
   263 @internalTechnology
       
   264 */
       
   265 EXPORT_C CObexPacket& CObexTransportControllerBase::ReceivePacket ()
       
   266 	{
       
   267 	LOG_LINE
       
   268 	LOG_FUNC
       
   269 
       
   270 	return *iReceivePacket;
       
   271 	}
       
   272 
       
   273 /**
       
   274 Ask active writer to do a send
       
   275 
       
   276 @internalTechnology
       
   277 */
       
   278 EXPORT_C void CObexTransportControllerBase::Send ()
       
   279 	{
       
   280 	LOG_LINE
       
   281 	LOG_FUNC
       
   282 
       
   283 	LOG3(_L8("Packet Sent, Opcode 0x%2x (0x%2x with final bit cleared), Length %d"), (iSendPacket->IsFinal() ? (iSendPacket->Opcode() | KObexPacketFinalBit) : iSendPacket->Opcode()), iSendPacket->Opcode(), iSendPacket->PacketSize());
       
   284 	FTRACE(iSendPacket->Dump ());
       
   285 
       
   286 	iActiveWriter->Transfer (*iSendPacket);
       
   287 	}
       
   288 
       
   289 /**
       
   290 Init the send packet wtih the received, aOpcode, set final to true,  and send the packet
       
   291 
       
   292 @internalTechnology
       
   293 */
       
   294 EXPORT_C void CObexTransportControllerBase::Send (TObexOpcode aOpcode)
       
   295 	{
       
   296 	LOG_LINE
       
   297 	LOG_FUNC
       
   298 	LOG1(_L8("\taOpcode = %d"), aOpcode);
       
   299 
       
   300 	iSendPacket->Init (aOpcode);
       
   301 	iSendPacket->SetFinal (ETrue);
       
   302 	Send ();
       
   303 	}
       
   304 
       
   305 /**
       
   306 Ask activer reader to do a read
       
   307 
       
   308 @internalTechnology
       
   309 */
       
   310 EXPORT_C void CObexTransportControllerBase::Receive ()
       
   311 	{
       
   312 	LOG_LINE
       
   313 	LOG_FUNC
       
   314 
       
   315 	iActiveReader->Transfer (*iReceivePacket);
       
   316 	}
       
   317 
       
   318 /**
       
   319 Is there a write outstanding
       
   320 
       
   321 @return TBool  return ETrue is there is a write activer otherwise EFalse
       
   322 @internalTechnology
       
   323 */		
       
   324 EXPORT_C TBool CObexTransportControllerBase::IsWriteActive ()  const
       
   325 	{
       
   326 	LOG_LINE
       
   327 	LOG_FUNC
       
   328 
       
   329 	if (iActiveWriter)
       
   330 		{
       
   331 		return (iActiveWriter->IsActive());
       
   332 		}
       
   333 	else // this has been added incase of null iActiveWriter
       
   334 		{
       
   335 		return EFalse;
       
   336 		}
       
   337 	}
       
   338 
       
   339 /**
       
   340 Insert local Connection info with the received, aVersion, aFlags and from iReceivePacket into the receive packet, aPacket
       
   341 
       
   342 @return TInt KErrNone or a symbian error
       
   343 @param aPacket the packet that is updated with local connection info
       
   344 @param aVersion 
       
   345 @param aFlags
       
   346 @internalTechnology
       
   347 */
       
   348 EXPORT_C TInt CObexTransportControllerBase::InsertLocalConnectInfo (CObexPacket &aPacket, TUint8 aVersion, TUint8 aFlags)
       
   349 	{
       
   350 	LOG_LINE
       
   351 	LOG_FUNC
       
   352 
       
   353 	if (iReceivePacket)
       
   354 		{
       
   355 		TObexConnectData localinfo;
       
   356 		localinfo.iVersion = aVersion;
       
   357 		localinfo.iFlags = aFlags;
       
   358 		localinfo.iMaxPacketLength = iReceivePacket->DataLimit();	// maximum packet length we can receive
       
   359 		return (aPacket.InsertData (localinfo));	
       
   360 		}
       
   361 	else
       
   362 		{
       
   363 		return KErrGeneral;
       
   364 		}
       
   365 	}
       
   366 
       
   367 /**
       
   368 Extract the local information from the received packet 
       
   369 
       
   370 @return EFalse- failed to extract the information. ETrue- extraction succeeded.
       
   371 @param aPacket The packet from which  local connection info is extracted
       
   372 @param aVersion
       
   373 @param aFlags
       
   374 @internalTechnology
       
   375 */
       
   376 EXPORT_C TBool CObexTransportControllerBase::ExtractRemoteConnectInfo(CObexPacket &aPacket, TUint8& aVersion, TUint8& aFlags)
       
   377 	{
       
   378 	LOG_LINE
       
   379 	LOG_FUNC
       
   380 
       
   381 	if (iSendPacket)
       
   382 		{
       
   383 		TObexConnectData remoteinfo;
       
   384 		if (!aPacket.ExtractData (remoteinfo))
       
   385 			{
       
   386 			return EFalse;
       
   387 			}
       
   388 		aVersion = remoteinfo.iVersion;
       
   389 		aFlags = remoteinfo.iFlags;
       
   390 
       
   391 		// Set the send buffer data limit (i.e. how much OBEX can use) to match the size of the
       
   392 		// remote receive buffer (the setter function ensures it's a legal size)
       
   393 		(void)iSendPacket->SetLegalDataLimit (remoteinfo.iMaxPacketLength);
       
   394 		return ETrue;
       
   395 		}
       
   396 	else
       
   397 		{
       
   398 		return EFalse;
       
   399 		}
       
   400 	}
       
   401 
       
   402 /**
       
   403 @internalTechnology
       
   404 */
       
   405 void CObexTransportControllerBase::ConstructPacketsL() 
       
   406 	{
       
   407 	LOG_FUNC
       
   408 
       
   409 	// Set actual packet sizes
       
   410 	// Receive must be at least KObexPacketDefaultSize to maintain functional compatibility with previous implentations
       
   411 	// (to ensure that remote ends that ignore how big we say our receive buffer is get that same grace on buffer overflow)
       
   412 	__ASSERT_DEBUG(iTransportInfo, PANIC(KPanicCat, ETransportNullPointer)); 
       
   413 	TUint16 receivePacketBufferSize = Max ( iTransportInfo->iReceiveMtu, KObexPacketDefaultSize );
       
   414 	TUint16 sendPacketBufferSize = iTransportInfo->iTransmitMtu;
       
   415 
       
   416 	// Set initial "software throttle" for packets (how big OBEX says they are)
       
   417 	// Send packet is set to miniumum, so initial transmit cannot overflow remote end
       
   418 	TUint16 receivePacketDataLimit = GetReceivePacketDataLimit();
       
   419 	TUint16 sendPacketDataLimit = KObexPacketMinSize;
       
   420 		
       
   421 	LOG2(_L8("CObexTransportControllerBase::ConstructPacketsL send buffer %d send data limit %d"), sendPacketBufferSize, sendPacketDataLimit);
       
   422 	LOG2(_L8("CObexTransportControllerBase::ConstructPacketsL receive buffer %d receive data limit %d"), receivePacketBufferSize, receivePacketDataLimit);
       
   423 
       
   424 	// Create the packets
       
   425 	iSendPacket = CObexPacket::NewL(sendPacketBufferSize, sendPacketDataLimit);
       
   426 	iReceivePacket = CObexPacket::NewL(receivePacketBufferSize, receivePacketDataLimit);
       
   427 	}
       
   428 
       
   429 /**
       
   430 Gets the socket associated with the connector and asks the socket for its  remote name. 
       
   431 This can be cast to the appropriate TSockAddr-derived class TIrdaSockAddr for IrDA. 
       
   432 
       
   433 @publishedPartner
       
   434 @released
       
   435 */
       
   436 EXPORT_C void CObexTransportControllerBase::RemoteAddr(TSockAddr& aAddr)
       
   437 	{
       
   438 	LOG_LINE
       
   439 	LOG_FUNC
       
   440 
       
   441 	DoRemoteAddr(aAddr);
       
   442 	}
       
   443 
       
   444 /**
       
   445 Returns true if the derived transport can re-connect when an obex connection is re-connected
       
   446 This is used to determine whether to take the transport down when an obex connection has been disconnected
       
   447 
       
   448 @see DoIsTransportRestartable
       
   449 @return TBool return ETrue if the derived transport can support re-connection when obex re-connects
       
   450 @publishedPartner
       
   451 @released
       
   452 */	
       
   453 EXPORT_C TBool CObexTransportControllerBase::IsTransportRestartable() const	
       
   454 	{
       
   455 	LOG_LINE
       
   456 	LOG_FUNC
       
   457 
       
   458 	return DoIsTransportRestartable();
       
   459 	}
       
   460 
       
   461 //MObexTransport Notify Protected 
       
   462 /**
       
   463 Process a Packet.  Upcall to the owner
       
   464 
       
   465 @publishedPartner
       
   466 @released
       
   467 */
       
   468 EXPORT_C void CObexTransportControllerBase::DoProcess(CObexPacket &aPacket)
       
   469 	{
       
   470 	LOG_LINE
       
   471 	LOG_FUNC
       
   472 
       
   473 	__ASSERT_DEBUG(iOwner, PANIC(KPanicCat, ETransportNullPointer)); 
       
   474 	iOwner->Process(aPacket);
       
   475 	}
       
   476 
       
   477 /**
       
   478 Reports and error to the owner
       
   479 
       
   480 @publishedPartner
       
   481 @released
       
   482 */
       
   483 EXPORT_C void CObexTransportControllerBase::DoError(TInt aError) 
       
   484 	{
       
   485 	LOG_LINE
       
   486 	LOG_FUNC
       
   487 	LOG1(_L8("\taError = %d"), aError);
       
   488 
       
   489 	__ASSERT_DEBUG(iOwner, PANIC(KPanicCat, ETransportNullPointer)); 
       
   490 	iOwner->Error(aError);
       
   491 	}
       
   492 
       
   493 /**
       
   494 Create the transport objects and reports that the transport is up to the owner
       
   495 
       
   496 @publishedPartner
       
   497 @released
       
   498 */	
       
   499 EXPORT_C void CObexTransportControllerBase::DoTransportUp(TObexConnectionInfo& aInfo)
       
   500 	{
       
   501 	LOG_LINE
       
   502 	LOG_FUNC
       
   503 
       
   504 	DeleteTransport();
       
   505 	TRAPD(err, InitialiseTransportL(aInfo));
       
   506 	if(err != KErrNone)
       
   507 		{
       
   508 		TransportUpError(err);
       
   509 		}
       
   510 	else
       
   511 		{
       
   512 		__ASSERT_DEBUG(iOwner, PANIC(KPanicCat, ETransportNullPointer));
       
   513 		iOwner->TransportUp();
       
   514 		Receive();
       
   515 		}
       
   516 	}
       
   517 
       
   518 void CObexTransportControllerBase::InitialiseTransportL(TObexConnectionInfo& aInfo)
       
   519 	{
       
   520 	NewTransportL(aInfo);
       
   521 	ConstructPacketsL();
       
   522 	}
       
   523 
       
   524 void CObexTransportControllerBase::TransportUpError(TInt aError)
       
   525 	{
       
   526 	// At this stage the transport controller will have a connection set-up.
       
   527 	// As a result if we get an error while setting up Obex we should bring
       
   528 	// the transport down (the owner has alrady been informed of the error,
       
   529 	// but we shouldn't expect them to have to bring the transport down).
       
   530 	TBool broughtDown = BringTransportDown();
       
   531 	// If we failed to bring down the transport there isn't much
       
   532 	// more we can do.
       
   533 	(void)broughtDown;
       
   534 
       
   535 	Error(aError); // Signal error indication
       
   536 	}
       
   537 
       
   538 /**
       
   539 Signals that an event related to processing the packet has occurred.
       
   540 @param aEvent The event that has occurred.
       
   541 */
       
   542 EXPORT_C void CObexTransportControllerBase::DoSignalPacketProcessEvent(TObexPacketProcessEvent aEvent)
       
   543 	{
       
   544 	LOG_FUNC
       
   545 	
       
   546 	__ASSERT_DEBUG(iOwner, PANIC(KPanicCat, ETransportNullPointer)); 
       
   547 	iOwner->SignalPacketProcessEvent(aEvent);
       
   548 	}
       
   549 
       
   550 /**
       
   551 Returns a pointer to the transport info being used by the transport. This may 
       
   552 be cast to the transport info type associated with the known concrete 
       
   553 transport controller.
       
   554 */
       
   555 EXPORT_C const TObexTransportInfo* CObexTransportControllerBase::TransportInfo() const
       
   556 	{
       
   557 	LOG_FUNC
       
   558 
       
   559 	return iTransportInfo;
       
   560 	}
       
   561 
       
   562 /**
       
   563 This function is part of the extension pattern and must be implemented by all derived instantiable classes.
       
   564 By default this returns null.  Any derived class that is required to extend its interface and that of this base 
       
   565 class returns its new interface in the form of an M class, that it extends, if and only if  the corresponding TUid, 
       
   566 aUid, is received. Otherwise the function calls the base class implementation, returning NULL.
       
   567 
       
   568 @return The M Class representing the extension to the interface otherwise NULL
       
   569 @param aUid The uid associated with the M Class that is being implemented
       
   570 @publishedPartner
       
   571 @released
       
   572 */
       
   573 EXPORT_C TAny* CObexTransportControllerBase::GetInterface(TUid /*aUid*/)
       
   574 	{
       
   575 	LOG_FUNC
       
   576 	
       
   577 	return NULL;
       
   578 	}
       
   579 
       
   580 //private
       
   581 void CObexTransportControllerBase::DeleteTransport()
       
   582 	{
       
   583 	LOG_LINE
       
   584 	LOG_FUNC
       
   585 
       
   586 	delete iActiveWriter;
       
   587 	delete iActiveReader;
       
   588 	delete iReceivePacket;
       
   589 	delete iSendPacket;
       
   590 	iActiveWriter = NULL;
       
   591 	iActiveReader = NULL;
       
   592 	iReceivePacket = NULL;
       
   593 	iSendPacket = NULL;
       
   594 	}