bluetoothengine/btsap/src/BTSapSocketHandler.cpp
changeset 0 f63038272f30
child 70 f5508c13dfe0
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2004-2008 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 *	  This class handles data exchange between BTSap server and client
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <bt_sock.h>
       
    22 #include <btextnotifiers.h> // Needed to check the NOTIFIERS_SUPPORT_PASSKEY_MIN_LENGTH flag
       
    23 #include <btengdiscovery.h>
       
    24 #include <btengconstants.h>
       
    25 #include "BTSapSocketHandler.h"
       
    26 #include "BTSapServerState.h"
       
    27 #include "BTSapSecurityHandler.h"
       
    28 #include "debug.h"
       
    29 
       
    30 // Old versions of RAN always try to connect to the same channel after pairing
       
    31 // To solve this problem, we try to use always the same channel
       
    32 const TUint KBTSapProposedChannel = 21; // 21 is big enough and my lucky number ;)
       
    33 const TInt KListenQueSize = 1;
       
    34 
       
    35 CBTSapSocketHandler::CBTSapSocketHandler(CBTSapServerState& aServerState, CBTSapRequestHandler& aRequestHandler)
       
    36 	: CActive(CActive::EPriorityHigh), 
       
    37 	  iServerState(aServerState),
       
    38 	  iChannel(0),
       
    39 	  iStateListen(iSockServ, iSocket, iListener, iChannel),
       
    40 	  iStateRecv(iSocket, aRequestHandler, &iSniffHandler),
       
    41 	  iStateSend(iSocket),
       
    42 	  iStateCheckSecurity(iSocket, iSecurityHandler),
       
    43 	  iCurrentState(EBTSapSocketStateListen),
       
    44 	  iNextState(EBTSapSocketStateRecv)
       
    45 	{
       
    46 	iStateArray.Append(&iStateListen);
       
    47 	iStateArray.Append(&iStateRecv);
       
    48 	iStateArray.Append(&iStateSend);
       
    49 	iStateArray.Append(&iStateCheckSecurity);
       
    50 	
       
    51 	CActiveScheduler::Add(this);
       
    52 	}
       
    53 
       
    54 // Destructor.
       
    55 CBTSapSocketHandler::~CBTSapSocketHandler()
       
    56 	{
       
    57 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  ~CBTSapSocketHandler")));
       
    58 
       
    59 	Cancel();
       
    60 	iStateArray.Close();
       
    61 
       
    62 	TRAPD(err, UnregisterBTSapServiceL());
       
    63     if(err != KErrNone)
       
    64         {
       
    65         BTSAP_TRACE_OPT(KBTSAP_TRACE_ERROR, BTSapPrintTrace(_L("[BTSap]  ~CBTSapSocketHandler - Couldn't unregister SAP Service!!!")));
       
    66         }
       
    67 
       
    68 	delete iSecurityHandler;
       
    69 	iSocket.Close();
       
    70 	iListener.Close();
       
    71 	iSockServ.Close();
       
    72 	delete iBtDiscovery;
       
    73     delete iSniffHandler;
       
    74 	}
       
    75 
       
    76 // ---------------------------------------------------------
       
    77 // CBTSapSocketHandler::NewL()
       
    78 // ---------------------------------------------------------
       
    79 //
       
    80 CBTSapSocketHandler* CBTSapSocketHandler::NewL(CBTSapServerState& aServerState, CBTSapRequestHandler& aRequestHandler)
       
    81 	{
       
    82 	CBTSapSocketHandler* self = new (ELeave) CBTSapSocketHandler(aServerState, aRequestHandler);
       
    83 	CleanupStack::PushL(self);
       
    84 	self->ConstructL();
       
    85 	CleanupStack::Pop();
       
    86 	return self;
       
    87 	}
       
    88 
       
    89 // ---------------------------------------------------------
       
    90 // CBTSapSocketHandler::ConstructL
       
    91 // ---------------------------------------------------------
       
    92 //
       
    93 void CBTSapSocketHandler::ConstructL()
       
    94 	{
       
    95 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSocketHandler: ConstructL")));
       
    96 
       
    97 	RegisterBTSapServiceL();
       
    98 
       
    99 	iSecurityHandler = CBTSapSecurityHandler::NewL();
       
   100     // Now create the Sniff Handler
       
   101     iSniffHandler = CBTSapSniffHandler::NewL(iSocket, iSockServ);
       
   102 	}
       
   103 
       
   104 // ---------------------------------------------------------
       
   105 // CBTSapSocketHandler::DoCancel
       
   106 // ---------------------------------------------------------
       
   107 //
       
   108 void CBTSapSocketHandler::DoCancel()
       
   109 	{
       
   110 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSocketHandler: DoCancel")));
       
   111 	iStateArray[iCurrentState]->Cancel();
       
   112 	}
       
   113 
       
   114 // ---------------------------------------------------------
       
   115 // RunL
       
   116 // ---------------------------------------------------------
       
   117 //
       
   118 void CBTSapSocketHandler::RunL()
       
   119 	{
       
   120 	TInt status = iStatus.Int();
       
   121 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSocketHandler: RunL: %d"), status));
       
   122 	TBool disconnecting = EFalse;
       
   123 	
       
   124 	if (iNextState == EBTSapSocketStateListen)
       
   125         {
       
   126 		// In case we are here, we are disconnecting existing connection
       
   127 		disconnecting = ETrue;
       
   128         }
       
   129 
       
   130 	if (status >= KErrNone)
       
   131 		{
       
   132 		TBTSapSocketState nextState = iStateArray[iCurrentState]->Complete(status);
       
   133 		
       
   134 		if (iCurrentState == EBTSapSocketStateListen)
       
   135 		    {
       
   136 		    // The remote side has created SAP connection
       
   137 		    // In order to save RAM, the SAP states are not created until here
       
   138 		    iServerState.CreateStatesL();
       
   139 		    }
       
   140 
       
   141 		if (iNextState == EBTSapSocketStateRecv)
       
   142 			{
       
   143 			iNextState = nextState;
       
   144 			}
       
   145 
       
   146 		ChangeState();
       
   147 		}
       
   148 	else
       
   149 		{
       
   150 		// BT link loss or other socket errors
       
   151         iCurrentState = EBTSapSocketStateRecvWrong;
       
   152 		iServerState.ChangeState(EStateNotConnected);
       
   153 		}
       
   154 		
       
   155     if (disconnecting)
       
   156         {
       
   157         // Disconnect has been completed, notify caller
       
   158         iServerState.DisconnectCompleteL(status);
       
   159         }
       
   160 	}
       
   161 
       
   162 // ---------------------------------------------------------
       
   163 // CBTSapSocketHandler::Listen
       
   164 // ---------------------------------------------------------
       
   165 void CBTSapSocketHandler::Listen()
       
   166 	{
       
   167 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSocketHandler: Listen")));
       
   168 	
       
   169 	iNextState = EBTSapSocketStateListen;
       
   170 
       
   171 	if (iCurrentState != EBTSapSocketStateSend)
       
   172 		{
       
   173 		Cancel();
       
   174 		ChangeState();
       
   175 		}
       
   176 		
       
   177     // In order to save RAM, release the SAP states here and create
       
   178     // them when the connection has been created.
       
   179     TRAP_IGNORE( iServerState.ReleaseStatesL() );
       
   180 
       
   181     // Make sure the sniffer is disabled when we're listening for an incoming connection
       
   182     iSniffHandler->Disable();
       
   183 	}
       
   184 
       
   185 // ---------------------------------------------------------
       
   186 // CBTSapSocketHandler::Send
       
   187 // ---------------------------------------------------------
       
   188 void CBTSapSocketHandler::Send(const TDes8& aResponseData)
       
   189 	{
       
   190 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSocketHandler: Send")));
       
   191 
       
   192 	if (iCurrentState == EBTSapSocketStateRecv ||
       
   193 		iCurrentState == EBTSapSocketStateSend)
       
   194 		{
       
   195 		iStateSend.SetResponseData(aResponseData);
       
   196 		iNextState = EBTSapSocketStateSend;
       
   197 
       
   198 		if (iCurrentState == EBTSapSocketStateRecv)
       
   199 			{
       
   200 			Cancel();
       
   201 			ChangeState();
       
   202 			}
       
   203 		}
       
   204 	}
       
   205 
       
   206 // ---------------------------------------------------------
       
   207 // CBTSapSocketHandler::GetRemoteBTAddress
       
   208 // ---------------------------------------------------------
       
   209 TInt CBTSapSocketHandler::GetRemoteBTAddress(TBTDevAddr& aBTDevAddr)
       
   210 	{
       
   211 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSocketHandler: GetRemoteBTAddress >>")));
       
   212 	TInt retVal = KErrDisconnected;
       
   213 
       
   214 	if (iCurrentState == EBTSapSocketStateRecv ||
       
   215 		iCurrentState == EBTSapSocketStateSend)
       
   216 		{
       
   217 		TBTSockAddr addr;
       
   218 		iSocket.RemoteName(addr);
       
   219 		aBTDevAddr = addr.BTAddr();
       
   220 		retVal = KErrNone;
       
   221 		}
       
   222 
       
   223 	BTSAP_TRACE_OPT(KBTSAP_TRACE_INFO, BTSapPrintTrace(_L("[BTSap]  CBTSapSocketHandler: GetRemoteBTAddress: %d"), retVal));
       
   224 	return retVal;
       
   225 	}
       
   226 
       
   227 // ---------------------------------------------------------
       
   228 // CBTSapSocketHandler::IsSapConnected
       
   229 // ---------------------------------------------------------
       
   230 TBool CBTSapSocketHandler::IsSapConnected()
       
   231     {
       
   232 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSocketHandler::IsSapConnected")));
       
   233     return (iCurrentState != EBTSapSocketStateListen);
       
   234     }
       
   235 
       
   236 // ---------------------------------------------------------
       
   237 // CBTSapSocketHandler::ChangeState
       
   238 // ---------------------------------------------------------
       
   239 void CBTSapSocketHandler::ChangeState()
       
   240 	{
       
   241 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSocketHandler: ChangeState: %d"), iNextState));
       
   242 
       
   243 	iCurrentState = iNextState;
       
   244 	iNextState = EBTSapSocketStateRecv;
       
   245 	iStateArray[iCurrentState]->Enter(iStatus);
       
   246 	SetActive();
       
   247 	}
       
   248 
       
   249 // ---------------------------------------------------------
       
   250 // CBTSapSocketHandler::RegisterBTSapServiceL
       
   251 // ---------------------------------------------------------
       
   252 void CBTSapSocketHandler::RegisterBTSapServiceL()
       
   253 	{
       
   254 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSocketHandler::RegisterBTSapServiceL")));
       
   255 	
       
   256 	User::LeaveIfError(iSockServ.Connect());
       
   257 	TProtocolDesc pInfo;
       
   258 	iBtDiscovery = CBTEngDiscovery::NewL();
       
   259 	TInt err = KErrNone;
       
   260 
       
   261 	// Old version of RAN always use the same channel, so try to get a fixed one
       
   262 	iChannel = KBTSapProposedChannel;
       
   263 
       
   264     User::LeaveIfError(iSockServ.FindProtocol(TProtocolName(KRFCOMMDesC), pInfo));  // Or other protocol
       
   265     User::LeaveIfError(iListener.Open( iSockServ, pInfo.iAddrFamily, pInfo.iSockType, pInfo.iProtocol));
       
   266 
       
   267     TRfcommSockAddr addr;
       
   268     TBTServiceSecurity sec;
       
   269 
       
   270     sec.SetAuthentication( EMitmRequired );
       
   271     sec.SetAuthorisation(ETrue);
       
   272     sec.SetEncryption(ETrue);
       
   273     sec.SetPasskeyMinLength(KRequiredPassKeyLen);
       
   274     addr.SetSecurity(sec);
       
   275     addr.SetPort(iChannel);
       
   276    
       
   277     // On return, _should_ contain a free RFCOMM port
       
   278     err = iListener.Bind(addr);
       
   279     
       
   280     if(err)
       
   281         {
       
   282         TInt freeChnl;
       
   283         err = iListener.GetOpt(KRFCOMMGetAvailableServerChannel, KSolBtRFCOMM, freeChnl);
       
   284         if (err == KErrNone)
       
   285             {
       
   286             err = iListener.SetLocalPort(freeChnl);
       
   287             iChannel = freeChnl;
       
   288             }
       
   289         }
       
   290     if(err)
       
   291         {
       
   292         // Try to bind using KRfcommPassiveAutoBind
       
   293         iChannel = KRfcommPassiveAutoBind;
       
   294         addr.SetPort(iChannel);
       
   295         err = iListener.Bind(addr);
       
   296         }
       
   297     
       
   298     if (!err)
       
   299         {
       
   300 		// Update with the actual value
       
   301 		iChannel = iListener.LocalPort();
       
   302 		
       
   303         // Set the socket to listening
       
   304         User::LeaveIfError(iListener.Listen(KListenQueSize));
       
   305         }
       
   306     else
       
   307         {
       
   308 		BTSAP_TRACE_OPT(KBTSAP_TRACE_ERROR, BTSapPrintTrace(_L("[BTSap]  CBTSapSocketHandler: RegisterBTSapServiceL: Couldn't register dynamic channel!!! (err = %d)"), err));
       
   309 		User::Leave( err );		
       
   310         }
       
   311 
       
   312 	// register BTSap service profile
       
   313 	TUUID uuid(EBTProfileSAP);
       
   314 	User::LeaveIfError( iBtDiscovery->RegisterSdpRecord(uuid, iChannel, iSdpHandle) );
       
   315 	}
       
   316 	
       
   317 // ---------------------------------------------------------
       
   318 // CBTSapSocketHandler::UnregisterBTSapServiceL
       
   319 // ---------------------------------------------------------
       
   320 void CBTSapSocketHandler::UnregisterBTSapServiceL()
       
   321 	{
       
   322 	if(iSdpHandle)
       
   323 		{
       
   324 		User::LeaveIfError(iBtDiscovery->DeleteSdpRecord(iSdpHandle));
       
   325 		}
       
   326 	}
       
   327 
       
   328 // ---------------------------------------------------------
       
   329 // CBTSapSocketHandler::TState::TState
       
   330 // ---------------------------------------------------------
       
   331 CBTSapSocketHandler::TState::TState(RSocket& aSocket)
       
   332 	: iSocket(aSocket)
       
   333 	{
       
   334 	}
       
   335 
       
   336 //	End of File