obex/obexprotocol/obexbttransport/src/ObexRfcommConnector.cpp
changeset 0 d0791faffa3f
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     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 <obex/transport/mobextransportnotify.h>
       
    17 #include <obexbttransportinfo.h>
       
    18 #include "ObexRfcommConnector.h"
       
    19 #include "ObexActiveRfcommConnector.h"
       
    20 #include "ObexListenerRfcommConnector.h"
       
    21 #include "logger.h"
       
    22 #include "obexbtfaults.h"
       
    23 
       
    24 #ifdef __FLOG_ACTIVE
       
    25 _LIT8(KLogComponent, "RFCOMM");
       
    26 #endif
       
    27 
       
    28 #ifdef _DEBUG
       
    29 _LIT(KPanicCat, "RfcommConnector");
       
    30 #endif
       
    31 
       
    32 CObexRfcommConnector* CObexRfcommConnector::NewL(MObexTransportNotify& aOwner, 
       
    33 						 TBTSockAddr& aAddr)
       
    34 	{
       
    35 	LOG_STATIC_FUNC_ENTRY
       
    36 
       
    37 	CObexRfcommConnector* self = new(ELeave) CObexRfcommConnector(aOwner, aAddr);
       
    38 	CleanupStack::PushL(self);
       
    39 	self->ConstructL();
       
    40 	CleanupStack::Pop(self);
       
    41 	return self;
       
    42 	}
       
    43 
       
    44 CObexRfcommConnector::CObexRfcommConnector(MObexTransportNotify& aObserver, 
       
    45 										   TBTSockAddr& aAddr)
       
    46  :	CObexConnector(aObserver),
       
    47 	iAddr(aAddr)
       
    48 	{
       
    49 	LOG_FUNC
       
    50 	LOG(_L8("\taAddr:"));
       
    51 	LOGHEXDESC(aAddr);
       
    52 	}
       
    53 
       
    54 void CObexRfcommConnector::ConstructL()
       
    55 	{
       
    56 	LOG_FUNC
       
    57 
       
    58 	LEAVEIFERRORL(iSocketServ.Connect());
       
    59 	LEAVEIFERRORL(iSocketServ.FindProtocol(_L("RFCOMM"), iProtocolDesc));
       
    60 
       
    61 	iConnector = new(ELeave) CObexActiveRfcommConnector(*this, iSocketServ, iSocket, iProtocolDesc, iAddr);
       
    62 	iReceiver = new(ELeave) CObexListenerRfcommConnector(*this, iSocketServ, iSocket, iProtocolDesc, iAddr);
       
    63 
       
    64 	BaseConstructL();
       
    65 	}
       
    66 
       
    67 CObexRfcommConnector::~CObexRfcommConnector()
       
    68 	{
       
    69 	LOG_FUNC
       
    70 
       
    71 	delete iConnector;
       
    72 	delete iReceiver;
       
    73 	iSocket.Close();
       
    74 	iSocketServ.Close();
       
    75 	}
       
    76 
       
    77 void CObexRfcommConnector::ConnectL()
       
    78 	{
       
    79 	LOG_FUNC
       
    80 
       
    81 	__ASSERT_DEBUG(iConnector, PANIC(KPanicCat, ERfcommConnectorInternalError));
       
    82 	iConnector->ConnectL();
       
    83 	}
       
    84 
       
    85 void CObexRfcommConnector::CancelConnect()
       
    86 	{
       
    87 	LOG_FUNC
       
    88 
       
    89 	__ASSERT_DEBUG(iConnector, PANIC(KPanicCat, ERfcommConnectorInternalError));
       
    90 	iConnector->Cancel();
       
    91 	}
       
    92 
       
    93 void CObexRfcommConnector::AcceptL()
       
    94 	{
       
    95 	LOG_FUNC
       
    96 
       
    97 	__ASSERT_DEBUG(iReceiver, PANIC(KPanicCat, ERfcommConnectorInternalError));
       
    98 	iReceiver->AcceptL();
       
    99 	}
       
   100 
       
   101 void CObexRfcommConnector::CancelAccept()
       
   102 	{
       
   103 	LOG_FUNC
       
   104 
       
   105 	__ASSERT_DEBUG(iReceiver, PANIC(KPanicCat, ERfcommConnectorInternalError));
       
   106 	iReceiver->Cancel();
       
   107 	}
       
   108 
       
   109 TBool CObexRfcommConnector::BringTransportDown()
       
   110 	{
       
   111 	LOG_FUNC
       
   112 
       
   113 	// There's a nasty contract going on here- at most, only one of these AOs 
       
   114 	// should be active at any time. They each use this object's iSocket, 
       
   115 	// opening and closing it, and asserting its SubSessionHandle at various 
       
   116 	// points. They may be better designed as a single AO state machine.
       
   117 	__ASSERT_DEBUG(iConnector, PANIC(KPanicCat, ERfcommConnectorInternalError));
       
   118 	iConnector->Cancel();
       
   119 	__ASSERT_DEBUG(iReceiver, PANIC(KPanicCat, ERfcommConnectorInternalError));
       
   120 	iReceiver->Cancel();
       
   121 
       
   122 	// For cases where the underlying transport has gone we'll
       
   123 	// do an Immediate Shutdown to prevent Close() from blocking.
       
   124 	// This should stop dialogs from "freezing". 
       
   125 	//returns true as this transport proceeds with taking down the transport
       
   126 	if ( iTransportUp )
       
   127 		{
       
   128 		TRequestStatus status;
       
   129 		iSocket.Shutdown(RSocket::EImmediate, status);
       
   130 		User::WaitForRequest(status);
       
   131 		iTransportUp = EFalse;
       
   132 		}
       
   133 
       
   134 	iSocket.Close();
       
   135 	return ETrue;
       
   136 	}
       
   137 
       
   138 /** Used to set any link layer specific error condition flags. Bluetooth has 
       
   139 none, so the function is a stub. */
       
   140 void CObexRfcommConnector::SignalTransportError()
       
   141 	{
       
   142 	LOG_FUNC
       
   143 	}
       
   144 	
       
   145 /**
       
   146 Returns the socket instance iSocket
       
   147 @return RSocket
       
   148 */
       
   149 RSocket& CObexRfcommConnector::GetSocket()
       
   150 	{
       
   151 	LOG_LINE
       
   152 	LOG_FUNC
       
   153 
       
   154 	return iSocket;
       
   155 	}
       
   156 	
       
   157 /**
       
   158 This method is used to field completed connections from both the active 
       
   159 connector and the listen/acceptor.
       
   160 */
       
   161 void CObexRfcommConnector::ConnectComplete(TInt aError, TObexConnectionInfo& aSockinfo)
       
   162 	{
       
   163 	LOG_FUNC
       
   164 	LOG1(_L8("\taError = %d"), aError);
       
   165 
       
   166 	if ( aError == KErrNone )
       
   167 		{
       
   168 		Observer().TransportUp(aSockinfo);
       
   169 		iTransportUp = ETrue;
       
   170 		}
       
   171 	else
       
   172 		{
       
   173 		Observer().Error(aError);
       
   174 		__ASSERT_DEBUG(!iTransportUp, PANIC(KPanicCat, ERfcommConnectorInternalError));
       
   175 		}
       
   176 	}