bluetoothmgmt/bluetoothclientlib/btlib/btsocket.cpp
changeset 0 29b1cd4cb562
child 11 20fda83a6398
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2003-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 // Generic functions associated with all Bluetooth socket addresses
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <bt_sock.h>
       
    19 #include "btsocketpanic.h"
       
    20 #include "btsockethelpers.h"
       
    21 #include "e32cmn.h" 
       
    22 
       
    23 #include <bluetooth/logger.h>
       
    24 
       
    25 
       
    26 
       
    27 void Panic(TBTSocketPanics aCode)
       
    28 	{
       
    29 	_LIT(KPanicName, "BTSocket");
       
    30 	User::Panic(KPanicName, aCode);
       
    31 	}
       
    32 
       
    33 
       
    34 
       
    35 
       
    36 
       
    37 //*****************//
       
    38 
       
    39 //----
       
    40 EXPORT_C CBluetoothSocket* CBluetoothSocket::NewL(MBluetoothSocketNotifier& aNotifier, 
       
    41 												  RSocketServ& aServer, 
       
    42 												  TUint aSockType,
       
    43 												  TUint aProtocol)
       
    44 /** Standard Bluetooth socket NewL constructor.
       
    45 
       
    46 Opens a Bluetooth socket. The constructor identifies the server, socket type, and bluetooth 
       
    47 protocol to be used for the socket, as well as an asynchronous notifier.
       
    48 @pre A session on the socket server has been started.
       
    49 @leave No memory, or error on opening socket.
       
    50 @param aNotifier The object which will receive asynchronous events.
       
    51 @param aServer A handle to an existing session on the socket server (ESock).
       
    52 @param aSockType One of a set of values defined in es_sock.h, for example KSockStream.
       
    53 @param aProtocol An entry point into the Bluetooth stack, for example KL2CAP.
       
    54 @return The Bluetooth socket created with the constructor.
       
    55 @capability LocalServices
       
    56 */
       
    57 	{
       
    58 	CBluetoothSocket* self = NewLC(aNotifier, aServer, aSockType,aProtocol);
       
    59 	CleanupStack::Pop();
       
    60 	return self;
       
    61 	}
       
    62 
       
    63 EXPORT_C CBluetoothSocket* CBluetoothSocket::NewLC(MBluetoothSocketNotifier& aNotifier, 
       
    64 												   RSocketServ& aServer,
       
    65 												   TUint aSockType,
       
    66 												   TUint aProtocol)
       
    67 /** Standard Bluetooth socket NewLC constructor.
       
    68 
       
    69 Opens a Bluetooth socket as with the NewL() above, but leaves the returned CBluetoothSocket
       
    70 object on the cleanup stack.
       
    71 
       
    72 @param aNotifier The object which will receive asynchronous events.
       
    73 @param aServer A handle to an existing session on the socket server (ESock)
       
    74 @param aSockType One of a set of values defined in es_sock.h, for example KSockStream
       
    75 @param aProtocol An entry point into the Bluetooth stack, for example KL2CAP
       
    76 @return The Bluetooth socket created with the constructor.
       
    77 @capability LocalServices
       
    78 */
       
    79 	{
       
    80 	CBluetoothSocket* self=new (ELeave) CBluetoothSocket(aNotifier, aServer);
       
    81 	CleanupStack::PushL(self);
       
    82 	self->ConstructL(aSockType, aProtocol);
       
    83 	return self;
       
    84 	}
       
    85 
       
    86 
       
    87 EXPORT_C CBluetoothSocket* CBluetoothSocket::NewL(MBluetoothSocketNotifier& aNotifier, 
       
    88 												  RSocketServ& aServer, 
       
    89 												  TUint aSockType,
       
    90 												  TUint aProtocol,
       
    91 												  RConnection& aConnection)
       
    92 /** Opens a Bluetooth socket on a specified connection.
       
    93 @pre A session on the socket server has been started.
       
    94 @leave No memory, or error on opening socket
       
    95 @param aNotifier The object which will receive asynchronous events.
       
    96 @param aServer A handle to an existing session on the socket server (ESock)
       
    97 @param aSockType One of a set of values defined in es_sock.h, for example KSockStream
       
    98 @param aProtocol An entry point into the Bluetooth stack, for example KL2CAP
       
    99 @param aConnection A socket server management interface for a connection
       
   100 @return The Bluetooth socket created with the constructor.
       
   101 @capability LocalServices
       
   102 */
       
   103 	{
       
   104 	CBluetoothSocket* self = NewLC(aNotifier, aServer, aSockType, aProtocol, aConnection);
       
   105 	CleanupStack::Pop();
       
   106 	return self;
       
   107 	}
       
   108 
       
   109 
       
   110 EXPORT_C CBluetoothSocket* CBluetoothSocket::NewLC(MBluetoothSocketNotifier& aNotifier, 
       
   111 												   RSocketServ& aServer,
       
   112 												   TUint aSockType,
       
   113 												   TUint aProtocol,
       
   114 												   RConnection& aConnection)
       
   115 /** This constructor opens a Bluetooth socket on a specified connection.
       
   116 
       
   117 Leaves the socket on the cleanup stack.
       
   118 
       
   119 @param aNotifier The object which will receive asynchronous events.
       
   120 @param aServer A handle to an existing session on the socket server (ESock)
       
   121 @param aSockType One of a set of values defined in es_sock.h, for example KSockStream
       
   122 @param aProtocol An entry point into the Bluetooth stack, for example KL2CAP
       
   123 @param aConnection A socket server management interface for a connection
       
   124 @return The Bluetooth socket created with the constructor.
       
   125 @capability LocalServices
       
   126 */
       
   127 	{
       
   128 	CBluetoothSocket* self=new (ELeave) CBluetoothSocket(aNotifier, aServer);
       
   129 	CleanupStack::PushL(self);
       
   130 	self->ConstructL(aSockType, aProtocol, aConnection);
       
   131 	return self;
       
   132 	}
       
   133 
       
   134 
       
   135 EXPORT_C CBluetoothSocket* CBluetoothSocket::NewL(MBluetoothSocketNotifier& aNotifier, 
       
   136 												  RSocketServ& aServer,
       
   137 												  const TDesC& aName)
       
   138 /** Bluetooth socket named protocol NewL() constructor.
       
   139 
       
   140 Opens a Bluetooth socket over a specified named protocol, such as "L2CAP".
       
   141 @pre A session on the socket server has been started.
       
   142 @leave No memory, or error on opening socket
       
   143 @param aNotifier The object which will receive asynchronous events.
       
   144 @param aServer A handle to an existing session on the socket server (ESock)
       
   145 @param aName A descriptor containing the name of the protocol wanted for
       
   146 making a Bluetooth connection, for example _L("L2CAP")
       
   147 @return The Bluetooth socket created with the constructor.
       
   148 @capability LocalServices
       
   149 */
       
   150 	{
       
   151 	CBluetoothSocket* self = NewLC(aNotifier, aServer, aName);
       
   152 	CleanupStack::Pop();
       
   153 	return self;
       
   154 	}
       
   155 
       
   156 EXPORT_C CBluetoothSocket* CBluetoothSocket::NewLC(MBluetoothSocketNotifier& aNotifier, 
       
   157 												   RSocketServ& aServer,
       
   158 												   const TDesC& aName)
       
   159 /** Bluetooth socket named protocol NewLC() constructor.
       
   160 
       
   161 Opens a Bluetooth socket over a specified named protocol, such as "L2CAP".
       
   162 Leaves returned CBluetoothSocket object on the cleanup stack.
       
   163 
       
   164 @param aNotifier The object which will receive asynchronous events.
       
   165 @param aServer A handle to an existing session on the socket server (ESock)
       
   166 @param aName A descriptor containing the name of the protocol wanted for
       
   167 making a Bluetooth connection, for example _L("L2CAP")
       
   168 @return The Bluetooth socket created with the constructor.
       
   169 @capability LocalServices
       
   170 */
       
   171 	{
       
   172 	CBluetoothSocket* self=new (ELeave) CBluetoothSocket(aNotifier, aServer);
       
   173 	CleanupStack::PushL(self);
       
   174 	self->ConstructL(aName);
       
   175 	return self;
       
   176 	}
       
   177 
       
   178 
       
   179 EXPORT_C CBluetoothSocket* CBluetoothSocket::NewL(MBluetoothSocketNotifier& aNotifier, RSocketServ& aServer)
       
   180 /** Bluetooth blank socket NewL() constructor.
       
   181 
       
   182 Opens a blank socket to be used when accepting an incoming connection.
       
   183 This socket should be used as the parameter when a listening socket
       
   184 calls CBluetoothSocket::Accept(CBluetoothSocket& aBlankSocket). When 
       
   185 that connection completes this blank socket becomes the Bluetooth 
       
   186 socket for that connection.
       
   187 
       
   188 @pre A session on the socket server has been started.
       
   189 @leave No memory, or error on opening socket
       
   190 @param aNotifier The object which will receive asynchronous events.
       
   191 @param aServer A handle to an existing session on the socket server (ESock)
       
   192 @return The Bluetooth socket created with the constructor.
       
   193 */
       
   194 	{
       
   195 	CBluetoothSocket* self = NewLC(aNotifier, aServer);
       
   196 	CleanupStack::Pop();
       
   197 	return self;
       
   198 	}
       
   199 
       
   200 EXPORT_C CBluetoothSocket* CBluetoothSocket::NewLC(MBluetoothSocketNotifier& aNotifier, RSocketServ& aServer)
       
   201 /** Bluetooth blank socket NewLC() constructor
       
   202 
       
   203 Opens a blank socket to be used when accepting an incoming connection.
       
   204 This socket should be used as the parameter when a listening socket
       
   205 calls CBluetoothSocket::Accept(CBluetoothSocket& aBlankSocket). When 
       
   206 that connection completes this blank socket becomes the Bluetooth 
       
   207 socket for that connection.
       
   208 
       
   209 Leaves returned CBluetoothSocket object on the cleanup stack.
       
   210 
       
   211 @pre A session on the socket server has been started.
       
   212 @leave No memory, or error on opening socket
       
   213 @param aNotifier The object which will receive asynchronous events.
       
   214 @param aServer A handle to an existing session on the socket server (ESock)
       
   215 @return The Bluetooth socket created with the constructor.
       
   216 */
       
   217 	{
       
   218 	CBluetoothSocket* self=new (ELeave) CBluetoothSocket(aNotifier, aServer);
       
   219 	CleanupStack::PushL(self);
       
   220 	self->ConstructL();
       
   221 	return self;
       
   222 	}
       
   223 
       
   224 EXPORT_C CBluetoothSocket* CBluetoothSocket::NewL(MBluetoothSocketNotifier& aNotifier,
       
   225 												  RSocketServ& aServer,
       
   226 												  RSocket& aSocket)
       
   227 /** Bluetooth socket using an existing RSocket instance constructor.
       
   228 
       
   229 Takes ownership of an existing socket to be used as the underlying
       
   230 connection for this Socket wrapper.  This should be used where an
       
   231 existing API returned a RSocket representing a Bluetooth connection.
       
   232 
       
   233 @pre A session on the socket server has been started, and aSocket is open and is
       
   234 set as "Transferable" through the KSOEnableTransfer SetOpt. 
       
   235 @leave No memory
       
   236 @param aNotifier The object which will receive asynchronous events.
       
   237 @param aServer A handle to an existing session on the socket server (ESock)
       
   238 @param aSocket A handle to an existing socket.
       
   239 @return The Bluetooth socket created with the constructor.
       
   240 */
       
   241 	{
       
   242 	CBluetoothSocket* self = NewLC(aNotifier, aServer, aSocket);
       
   243 	CleanupStack::Pop();
       
   244 	return self;
       
   245 	}
       
   246 
       
   247 EXPORT_C CBluetoothSocket* CBluetoothSocket::NewLC(MBluetoothSocketNotifier& aNotifier, 
       
   248 												   RSocketServ& aServer,
       
   249 												   RSocket& aSocket)
       
   250 /** Bluetooth socket using an existing RSocket instance constructor.
       
   251 
       
   252 Takes ownership of an existing socket to be used as the underlying
       
   253 connection for this Socket wrapper.  This should be used where an
       
   254 existing API returned a RSocket representing a Bluetooth connection.
       
   255 
       
   256 Leaves returned CBluetoothSocket object on the cleanup stack.
       
   257 
       
   258 @pre A session on the socket server has been started, and aSocket is open and is
       
   259 set as "Transferable" through the KSOEnableTransfer SetOpt.
       
   260 @leave No memory
       
   261 @param aNotifier The object which will receive asynchronous events.
       
   262 @param aServer A handle to an existing session on the socket server (ESock)
       
   263 @param aSocket A handle to an existing socket.
       
   264 @return The Bluetooth socket created with the constructor.
       
   265 */
       
   266 	{
       
   267 	CBluetoothSocket* self=new (ELeave) CBluetoothSocket(aNotifier, aServer);
       
   268 	CleanupStack::PushL(self);
       
   269 	self->ConstructL(aSocket);
       
   270 	return self;
       
   271 	}
       
   272 	
       
   273 EXPORT_C CBluetoothSocket::~CBluetoothSocket()
       
   274 /** Destructor
       
   275 
       
   276 Cancels any outstanding Bluetooth socket functions and closes the socket.
       
   277 Note sockets should be closed or shutdown before destructor is called.
       
   278 */
       
   279 	{
       
   280 	delete iAutoSniffDelayTimer;
       
   281 	delete iBTConnecter;
       
   282 	delete iBTAccepter;
       
   283 	delete iBTShutdowner;
       
   284 	delete iBTReceiver;
       
   285 	delete iBTSender;
       
   286 	delete iBTIoctler;
       
   287 	delete iBTBasebandChangeEventNotifier;
       
   288 	if(Socket().SubSessionHandle())
       
   289 		{
       
   290 		Socket().Close();
       
   291 		}
       
   292 	delete iBTBasebandManager; // Unregisters any basebands it has registered
       
   293 	delete iBTBasebandChangeEventDelegate;
       
   294 	delete iAsyncDestroyer;
       
   295 	}
       
   296 
       
   297 
       
   298 //RSocket functions
       
   299 EXPORT_C TInt CBluetoothSocket::Send(const TDesC8& aDesc,TUint someFlags)
       
   300 /** Sends data to a remote bluetooth device.
       
   301 
       
   302 The Send() operation checks that another Send() operation isn't already in
       
   303 progress before sending.
       
   304 
       
   305 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   306 object	supplied when this CBluetoothSocket was constructed.
       
   307 This is done in the function MBluetoothSocketNotifier::HandleSendCompleteL(TInt aErr).
       
   308 
       
   309 If Automatic Sniff Mode is active, using this function will not automatically put the link
       
   310 into active mode. If it is required that the link be taken out of sniff mode automatically
       
   311 when data is to be sent then the CBluetoothSocket::Write(const TDesC8& aDesc) function
       
   312 should be used instead.
       
   313 
       
   314 @param aDesc A descriptor for the data being sent to the remote device.
       
   315 @param someFlags Bluetooth specific flags.
       
   316 @return KErrNone meaning the operation was successful and data is being sent to the remote device,
       
   317         or KErrInUse meaning a previous Send() is not yet finished.
       
   318 @see RSocket::Send(const TDesC8& aDesc,TUint someFlags,TRequestStatus& aStatus)
       
   319 */
       
   320 	{
       
   321 	TInt rerr = KErrNone;
       
   322 	if(iSending)
       
   323 		{
       
   324 		rerr = KErrInUse;
       
   325 		}
       
   326 	else
       
   327 		{
       
   328 		iBTSender->Send(aDesc, someFlags);
       
   329 		iSending = ETrue;
       
   330 		}
       
   331 	return rerr;
       
   332 	}
       
   333 
       
   334 EXPORT_C TInt CBluetoothSocket::Send(const TDesC8& aDesc,TUint someFlags,TSockXfrLength& aLen)
       
   335 /**  Sends data to a remote bluetooth device.
       
   336 
       
   337 The Send() operation checks that another Send() operation isn't already in
       
   338 progress before sending.
       
   339 
       
   340 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   341 object	supplied when this CBluetoothSocket was constructed. 
       
   342 This is done in the function
       
   343 MBluetoothSocketNotifier::HandleSendCompleteL(TInt aErr)
       
   344 
       
   345 If Automatic Sniff Mode is active, using this function will not automatically put the link
       
   346 into active mode. If it is required that the link be taken out of sniff mode automatically
       
   347 when data is to be sent then the CBluetoothSocket::Write(const TDesC8& aDesc) function
       
   348 should be used instead.
       
   349 
       
   350 @param aDesc A descriptor for the data being sent to the remote device.
       
   351 @param someFlags Bluetooth specific flags.
       
   352 @param aLen Amount of data being sent.
       
   353 @return KErrNone meaning the operation was successful and data is being sent to the remote device,
       
   354         or KErrInUse meaning a previous Send() is not yet finished.
       
   355 @see RSocket::Send(const TDesC8& aDesc,TUint someFlags,TSockXfrLength& aLen)
       
   356 */
       
   357 	{
       
   358 	TInt rerr = KErrNone;
       
   359 	if(iSending)
       
   360 		{
       
   361 		rerr = KErrInUse;
       
   362 		}
       
   363 	else
       
   364 		{
       
   365 		iBTSender->Send(aDesc, someFlags, aLen);
       
   366 		iSending = ETrue;
       
   367 		}
       
   368 	return rerr;
       
   369 	}
       
   370 
       
   371 EXPORT_C void CBluetoothSocket::CancelSend()
       
   372 /** Cancels an outstanding Bluetooth Send() operation.
       
   373 
       
   374 Calling the function will cause an outstanding Bluetooth Send() 
       
   375 operation to complete prematurely.
       
   376 
       
   377 @see RSocket::CancelSend()
       
   378 */
       
   379 	{
       
   380 	if(iSending)
       
   381 		{
       
   382 		iBTSender->Cancel(); //cancels 'Write' or 'Send' as appropriate
       
   383 		iSending = EFalse;
       
   384 		}
       
   385 	}
       
   386 
       
   387 EXPORT_C TInt CBluetoothSocket::Recv(TDes8& aDesc,TUint flags)
       
   388 /** Receives data from a remote Bluetooth device.
       
   389 
       
   390 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   391 object	supplied when this CBluetoothSocket was constructed. 
       
   392 This is done in the function 
       
   393 MBluetoothSocketNotifier::HandleReceiveCompleteL(TInt aErr)
       
   394 @param aDesc A descriptor for the information being sent to the remote Bluetooth device.
       
   395 @param flags Bluetooth information flags.
       
   396 @return KErrNone meaning the operation was successful and data is being received from the remote device,
       
   397         or KErrInUse meaning a previous Recv() is not yet finished.
       
   398 @see RSocket::Recv(TDes8& aDesc,TUint flags)
       
   399 */
       
   400 	{
       
   401 	TInt rerr = KErrNone;
       
   402 	if(iReceiving)
       
   403 		{
       
   404 		rerr = KErrInUse;
       
   405 		}
       
   406 	else
       
   407 		{
       
   408 		iBTReceiver->Recv(aDesc, flags);
       
   409 		iReceiving = ETrue;
       
   410 		}
       
   411 	return rerr;
       
   412 	}
       
   413 
       
   414 EXPORT_C TInt CBluetoothSocket::Recv(TDes8& aDesc,TUint flags, TSockXfrLength& aLen)
       
   415 /** Receives data from a remote Bluetooth device.
       
   416 
       
   417 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   418 object	supplied when this CBluetoothSocket was constructed. 
       
   419 This is done in the function 
       
   420 MBluetoothSocketNotifier::HandleReceiveCompleteL(TInt aErr)
       
   421 @param aDesc A descriptor for the information being sent to the remote Bluetooth device.
       
   422 @param flags Bluetooth information flags.
       
   423 @param aLen A length indicating how much data was read. This is the same as length of the returned aDesc.
       
   424 @return KErrNone meaning the operation was successful and data is being received from the remote device,
       
   425         or KErrInUse meaning a previous Recv() is not yet finished.
       
   426 @see RSocket::Recv(TDes8& aDesc,TUint flags, TSockXfrLength& aLen)
       
   427 */
       
   428 	{
       
   429 	TInt rerr = KErrNone;
       
   430 	if(iReceiving)
       
   431 		{
       
   432 		rerr = KErrInUse;
       
   433 		}
       
   434 	else
       
   435 		{
       
   436 		iBTReceiver->Recv(aDesc, flags, aLen);
       
   437 		iReceiving = ETrue;	
       
   438 		}
       
   439 	return rerr;
       
   440 	}
       
   441 
       
   442 EXPORT_C TInt CBluetoothSocket::RecvOneOrMore(TDes8& aDesc,TUint flags,TSockXfrLength& aLen)
       
   443 /** Receives data from a remote Bluetooth device, completing when data is available.
       
   444 
       
   445 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   446 object	supplied when this CBluetoothSocket was constructed. 
       
   447 This is done in the function MBluetoothSocketNotifier::HandleReceiveCompleteL(TInt aErr).
       
   448 @param aDesc A descriptor for the information being sent to the remote Bluetooth device.
       
   449 @param flags Bluetooth information flags.
       
   450 @param aLen A length indicating how much data was read. This is the same as length of the returned aDesc.
       
   451 @return KErrNone meaning the operation was successful and data is being received from the remote device,
       
   452         or KErrInUse meaning a previous Recv() is not yet finished.
       
   453 @see RSocket::RecvOneOrMore(TDes8& aDesc,TUint flags,TSockXfrLength& aLen)
       
   454 */
       
   455 	{
       
   456 	TInt rerr = KErrNone;
       
   457 	if(iReceiving)
       
   458 		{
       
   459 		rerr = KErrInUse;
       
   460 		}
       
   461 	else
       
   462 		{
       
   463 		iBTReceiver->RecvOneOrMore(aDesc, flags, aLen);
       
   464 		iReceiving = ETrue;
       
   465 		}
       
   466 	return rerr;
       
   467 	}
       
   468 
       
   469 EXPORT_C void CBluetoothSocket::CancelRecv()
       
   470 /** Cancels an outstanding Recv() operation. 
       
   471 
       
   472 Calling this function will cause any outstanding receive operation to cancel.
       
   473 
       
   474 @see RSocket::CancelRecv()
       
   475 */
       
   476 	{
       
   477 	if(iReceiving)
       
   478 		{
       
   479 		iBTReceiver->Cancel(); //cancels 'Read' or 'Recv' as appropriate
       
   480 		iReceiving = EFalse;
       
   481 		}
       
   482 	}
       
   483 
       
   484 EXPORT_C TInt CBluetoothSocket::Read(TDes8& aDesc)
       
   485 /** Receives data from a remote Bluetooth host.
       
   486 
       
   487 Read() is only used with a connected Bluetooth host.
       
   488 
       
   489 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   490 object	supplied when this CBluetoothSocket was constructed. 
       
   491 This is done in the function MBluetoothSocketNotifier::HandleReceiveCompleteL(TInt aErr)
       
   492 @param aDesc A descriptor for the data being read.
       
   493 @return KErrNone meaning the operation was successful and data is being received from the remote device,
       
   494         or KErrInUse meaning a previous Recv() is not yet finished.
       
   495 @see RSocket::Read(TDes8& aDesc)
       
   496 */
       
   497 	{
       
   498 	TInt rerr = KErrNone;
       
   499 	if(iReceiving)
       
   500 		{
       
   501 		rerr = KErrInUse;
       
   502 		}
       
   503 	else
       
   504 		{
       
   505 		iBTReceiver->Read(aDesc);
       
   506 		iReceiving = ETrue;
       
   507 		}
       
   508 	return rerr;
       
   509 	}
       
   510 
       
   511 EXPORT_C void CBluetoothSocket::CancelRead()
       
   512 /** Cancels an outstanding Read() operation. 
       
   513 
       
   514 Calling this function will cause any outstanding Read() operation to cancel.
       
   515 @see RSocket::CancelRead()
       
   516 */
       
   517 	{
       
   518 	if(iReceiving)
       
   519 		{
       
   520 		iBTReceiver->Cancel(); //cancels 'Read' or 'Recv' as appropriate
       
   521 		iReceiving = EFalse;
       
   522 		}
       
   523 	}
       
   524 
       
   525 EXPORT_C TInt CBluetoothSocket::Write(const TDesC8& aDesc)
       
   526 /** Writes to a remote Bluetooth device
       
   527 
       
   528 @see RSocket::Write(const TDesC8& aDesc)
       
   529 
       
   530 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   531 object	supplied when this CBluetoothSocket was constructed. 
       
   532 This is done in the function MBluetoothSocketNotifier::HandleSendCompleteL(TInt aErr)
       
   533 
       
   534 If Automatic Sniff Mode is active on this socket instance, then upon calling this function
       
   535 the link will be put into active mode.  If no CBluetoothSocket::Write() calls are made
       
   536 for a period of the timeout specified during activation of the automatic sniffing then
       
   537 the link will be put back into sniff mode.
       
   538 @see CBluetoothSocket::SetAutomaticSniffMode(TBool aAutoSniffMode, TInt aIdleSecondsBeforeSniffRequest)
       
   539 @see CBluetoothSocket::SetAutomaticSniffMode(TBool aAutoSniffMode)
       
   540 
       
   541 @param aDesc A descriptor for the data being sent to the remote Bluetooth device.
       
   542 @return KErrNone meaning the operation was successful and data is being sent to the remote device,
       
   543         or KErrInUse meaning a previous Write() is not yet finished.
       
   544 */
       
   545 	{
       
   546 	TInt rerr = KErrNone;
       
   547 	if(iSending)
       
   548 		{
       
   549 		rerr = KErrInUse;
       
   550 		}
       
   551 	else
       
   552 		{
       
   553 		if(iAutoSniffDelayTimer)
       
   554 			{
       
   555 			iAutoSniffDelayTimer->StartActive();
       
   556 			}
       
   557 		iBTSender->Write(aDesc);
       
   558 		iSending = ETrue;
       
   559 		}
       
   560 	return rerr;
       
   561 	}
       
   562 
       
   563 EXPORT_C void CBluetoothSocket::CancelWrite()
       
   564 /** Cancels an outstanding Write() operation.
       
   565 
       
   566 Calling this operation will cause any outstanding Write() operation to cancel.
       
   567 @see RSocket::CancelWrite()
       
   568 */
       
   569 	{
       
   570 	if(iSending)
       
   571 		{
       
   572 		iBTSender->Cancel(); //cancels 'Write' or 'Send' as appropriate
       
   573 		iSending = EFalse;
       
   574 		}
       
   575 	}
       
   576 
       
   577 EXPORT_C TInt CBluetoothSocket::SendTo(const TDesC8& aDesc,TSockAddr& aAddr,TUint flags)
       
   578 /** Sends the aDesc data to the aAddr specified and applies the flags indicated to the operation.
       
   579 
       
   580 @see RSocket::SendTo(const TDesC8& aDesc,TSockAddr& aAddr,TUint flags)
       
   581 
       
   582 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   583 object	supplied when this CBluetoothSocket was constructed. 
       
   584 This is done in the function MBluetoothSocketNotifier::HandleSendCompleteL(TInt aErr)
       
   585 
       
   586 If Automatic Sniff Mode is active, using this function will not automatically put the link
       
   587 into active mode. If it is required that the link be taken out of sniff mode automatically
       
   588 when data is to be sent then the CBluetoothSocket::Write(const TDesC8& aDesc) function
       
   589 should be used instead.
       
   590 
       
   591 @param aDesc A descriptor for the data being sent.
       
   592 @param aAddr The address of the Bluetooth device.
       
   593 @param flags The Bluetooth flags.
       
   594 @return KErrNone meaning the operation was successful and data is being sent to the specified 
       
   595         and addressed remote device, or KErrInUse meaning a previous SendTo() is not yet finished.
       
   596 */
       
   597 	{
       
   598 	TInt rerr = KErrNone;
       
   599 	if(iSending)
       
   600 		{
       
   601 		rerr = KErrInUse;
       
   602 		}
       
   603 	else
       
   604 		{
       
   605 		iBTSender->SendTo(aDesc, aAddr, flags);
       
   606 		iSending = ETrue;
       
   607 		}
       
   608 	return rerr;
       
   609 	}
       
   610 
       
   611 EXPORT_C TInt CBluetoothSocket::SendTo(const TDesC8& aDesc,TSockAddr& aAddr,TUint flags,TSockXfrLength& aLen)
       
   612 /**  Sends the aDesc data to the aAddr specified and applies the flags indicated to the operation.
       
   613 
       
   614 @see RSocket::SendTo(const TDesC8& aDesc,TSockAddr& aAddr,TUint flags,TSockXfrLength& aLen)
       
   615 
       
   616 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   617 object	supplied when this CBluetoothSocket was constructed. 
       
   618 This is done in the function MBluetoothSocketNotifier::HandleSendCompleteL(TInt aErr)
       
   619 
       
   620 If Automatic Sniff Mode is active, using this function will not automatically put the link
       
   621 into active mode. If it is required that the link be taken out of sniff mode automatically
       
   622 when data is to be sent then the CBluetoothSocket::Write(const TDesC8& aDesc) function
       
   623 should be used instead.
       
   624 
       
   625 @param aDesc A descriptor for the data being sent.
       
   626 @param aAddr The address of the Bluetooth device.
       
   627 @param flags The Bluetooth flags.
       
   628 @param aLen An integer representing the length of the message.
       
   629 @return KErrNone meaning the operation was successful and data is being sent to the specified 
       
   630         and addressed remote device, or KErrInUse meaning a previous SendTo() is not yet finished.
       
   631 */
       
   632 	{
       
   633 	TInt rerr = KErrNone;
       
   634 	if(iSending)
       
   635 		{
       
   636 		rerr = KErrInUse;
       
   637 		}
       
   638 	else
       
   639 		{
       
   640 		iBTSender->SendTo(aDesc, aAddr, flags, aLen);
       
   641 		iSending = ETrue;
       
   642 		}
       
   643 	return rerr;
       
   644 	}
       
   645 
       
   646 EXPORT_C TInt CBluetoothSocket::RecvFrom(TDes8& aDesc,TSockAddr& aAddr,TUint flags)
       
   647 /** Receives aDesc data from the named remote aAddr Bluetooth device using the flags indicated. 
       
   648 
       
   649 @see RSocket::RecvFrom(TDes8& aDesc,TSockAddr& aAddr,TUint flags)
       
   650 
       
   651 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   652 object	supplied when this CBluetoothSocket was constructed. 
       
   653 This is done in the function MBluetoothSocketNotifier::HandleReceiveCompleteL(TInt aErr)
       
   654 @param aDesc A descriptor for the data being received.
       
   655 @param aAddr The address of the Bluetooth device.
       
   656 @param flags The Bluetooth flags.
       
   657 @return KErrNone meaning the operation was successful and data is being received from the specified 
       
   658         and addressed remote device, or KErrInUse meaning a previous RecvFrom() is not yet finished.
       
   659 */
       
   660 	{
       
   661 	TInt rerr = KErrNone;
       
   662 	if(iReceiving)
       
   663 		{
       
   664 		rerr = KErrInUse;
       
   665 		}
       
   666 	else
       
   667 		{
       
   668 		iBTReceiver->RecvFrom(aDesc, aAddr, flags);
       
   669 		iReceiving = ETrue;
       
   670 		}
       
   671 	return rerr;
       
   672 	}
       
   673 
       
   674 EXPORT_C TInt CBluetoothSocket::RecvFrom(TDes8& aDesc,TSockAddr& aAddr,TUint flags,TSockXfrLength& aLen)
       
   675 /** Receives aDesc data from the named remote aAddr Bluetooth device using the flags indicated. 
       
   676 
       
   677 @see RSocket::RecvFrom(TDes8& aDesc,TSockAddr& aAddr,TUint flags,TSockXfrLength& aLen)
       
   678 
       
   679 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   680 object	supplied when this CBluetoothSocket was constructed. 
       
   681 This is done in the function 
       
   682 MBluetoothSocketNotifier::HandleReceiveCompleteL(TInt aErr)
       
   683 @param aDesc A descriptor for the data being received.
       
   684 @param aAddr The address of the Bluetooth device.
       
   685 @param flags The Bluetooth flags.
       
   686 @param aLen An integer representing the length of the message.
       
   687 @return KErrNone meaning the operation was successful and data is being received from the specified 
       
   688         and addressed remote device, or KErrInUse meaning a previous RecvFrom() is not yet finished.
       
   689 */
       
   690 	{
       
   691 	TInt rerr = KErrNone;
       
   692 	if(iReceiving)
       
   693 		{
       
   694 		rerr = KErrInUse;
       
   695 		}
       
   696 	else
       
   697 		{
       
   698 		iBTReceiver->RecvFrom(aDesc, aAddr, flags, aLen);
       
   699 		iReceiving = ETrue;
       
   700 		}
       
   701 	return rerr;
       
   702 	}
       
   703 
       
   704 EXPORT_C TInt CBluetoothSocket::Connect(TBTSockAddr& aAddr)
       
   705 /** Opens a connection to a specific Bluetooth device by its address.
       
   706 
       
   707 The address represents the address of the remote Bluetooth device. A socket may
       
   708 only have one connect operation outstanding at any one time. Once the connect 
       
   709 is completed, the socket is ready to send or receive data. If a socket is 
       
   710 unbound - i.e. Bind() has not been called yet - then it will automatically have
       
   711 a local address allocated.
       
   712 
       
   713 @see RSocket::Connect(TBTSockAddr& aAddr)
       
   714 
       
   715 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   716 object	supplied when this CBluetoothSocket was constructed. 
       
   717 This is done in the function MBluetoothSocketNotifier::HandleConnectCompleteL(TInt aErr)
       
   718 @param aAddr The address of the bluetooth device to which the connection is being made.
       
   719 @return KErrInUse if the device is already being used, else KErrNone is the connection is successful.
       
   720 */
       
   721 	{
       
   722 	if(iBTConnecter)
       
   723 		{
       
   724 		return KErrInUse;
       
   725 		}
       
   726 
       
   727 	TRAPD(err, iBTConnecter = CBTConnecter::NewL(*this));
       
   728 	if(err == KErrNone)
       
   729 		{
       
   730 		iBTConnecter->Connect(aAddr);
       
   731 		}
       
   732 	return err;
       
   733 	}
       
   734 
       
   735 EXPORT_C TInt CBluetoothSocket::Connect(TBTSockAddr& aAddr,const TDesC8& aConnectDataOut,TDes8& aConnectDataIn)
       
   736 /** Connects to a remote Bluetooth device by address with a defined data-out and data-in descriptor.
       
   737 
       
   738 The address provided specifies the address of the remote Bluetooth host. Data can be sent in 
       
   739 connect request packets, which may be provided in the data-out descriptor, or connect responses, which may be collected in the data-in descriptor.
       
   740 
       
   741 A socket may only have one connect operation outstanding at any one time. Once the connect is completed, the socket is ready to send or receive data. If a socket is unbound - i.e. Bind() has not been called yet - then it will automatically have a local address allocated.
       
   742 
       
   743 @see RSocket::Connect(TBTSockAddr& aAddr,const TDesC8& aConnectDataOut,TDes8& aConnectDataIn)
       
   744 
       
   745 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   746 object	supplied when this CBluetoothSocket was constructed.
       
   747 This is done in the function MBluetoothSocketNotifier::HandleConnectCompleteL(TInt aErr).
       
   748 @param aAddr The address of the remote Bluetooth device. 
       
   749 @param aConnectDataOut A descriptor containing data to be sent.
       
   750 @param aConnectDataIn A descriptor to receive data.
       
   751 @return KErrInUse if the device is already being used, else KErrNone is the connection is successful.
       
   752 */
       
   753 	{
       
   754 	if(iBTConnecter)
       
   755 		{
       
   756 		return KErrInUse;
       
   757 		}
       
   758 
       
   759 	TRAPD(err, iBTConnecter = CBTConnecter::NewL(*this));
       
   760 	if(err == KErrNone)
       
   761 		{
       
   762 		iBTConnecter->Connect(aAddr, aConnectDataOut, aConnectDataIn);
       
   763 		}
       
   764 	return err;
       
   765 	}
       
   766 
       
   767 EXPORT_C TInt CBluetoothSocket::Connect(TBTSockAddr& aAddr, TUint16 aServiceBits)
       
   768 /** Sets the Service bits in the Class Of Device. If this is successful, the socket then opens
       
   769 a connection to a specific Bluetooth device by its address.
       
   770 
       
   771 The address represents the address of the remote Bluetooth device. A socket may
       
   772 only have one connect operation outstanding at any one time. Once the connect 
       
   773 is completed, the socket is ready to send or receive data. If a socket is 
       
   774 unbound - i.e. Bind() has not been called yet - then it will automatically have
       
   775 a local address allocated.
       
   776 
       
   777 @see RSocket::Connect(TBTSockAddr& aAddr)
       
   778 
       
   779 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   780 object	supplied when this CBluetoothSocket was constructed. 
       
   781 This is done in the function MBluetoothSocketNotifier::HandleConnectCompleteL(TInt aErr)
       
   782 @param aAddr The address of the bluetooth device to which the connection is being made.
       
   783 @return KErrInUse if the device is already being used, else KErrNone is the connection is successful.
       
   784 */
       
   785 	{
       
   786 	TInt err = KErrNone;
       
   787 	err = iSocket.SetOpt(KBTRegisterCodService, KSolBtSAPBase, aServiceBits);
       
   788 	if(err == KErrNone)
       
   789 		{
       
   790 		if(iBTConnecter)
       
   791 			{
       
   792 			return KErrInUse;
       
   793 			}
       
   794 
       
   795 		TRAPD(err, iBTConnecter = CBTConnecter::NewL(*this));
       
   796 		if(err == KErrNone)
       
   797 			{
       
   798 			iBTConnecter->Connect(aAddr);
       
   799 			}
       
   800 		}
       
   801 	return err;
       
   802 	}
       
   803 
       
   804 EXPORT_C void CBluetoothSocket::CancelConnect()
       
   805 /** Cancel an attempted connection.
       
   806 
       
   807 @see RSocket::CancelConnect()
       
   808 */
       
   809 	{
       
   810 	if(iBTConnecter)
       
   811 		{
       
   812 		delete iBTConnecter;
       
   813 		iBTConnecter = NULL;
       
   814 		}
       
   815 	}
       
   816 
       
   817 EXPORT_C TInt CBluetoothSocket::Bind(TSockAddr& aAddr)
       
   818 /** Sets a Bluetooth socket address.
       
   819 
       
   820 Sets the local address of a socket. When a socket is opened it has no name associated
       
   821 with it, and binding is required so data can be routed to the socket. Bind() should be
       
   822 called before Listen() or Connect().
       
   823 
       
   824 @param aAddr The address of the socket.
       
   825 @see RSocket::Bind(TSockAddr& aAddr)
       
   826 @return aAddr The address of the socket.
       
   827 */
       
   828 	{
       
   829 	return iSocket.Bind(aAddr);
       
   830 	}
       
   831 
       
   832 EXPORT_C TInt CBluetoothSocket::SetLocalPort(TInt aPort)
       
   833 /** Sets the local port of a Bluetooth socket.
       
   834 
       
   835 Setting the local port is equivalent to calling Bind() with only the port set in the address.
       
   836 
       
   837 @see RSocket::SetLocalPort(TInt aPort)
       
   838 @param aPort The socket port.
       
   839 @return aPort The socket port.
       
   840 */
       
   841 	{
       
   842 	return iSocket.SetLocalPort(aPort);
       
   843 	}
       
   844 
       
   845 EXPORT_C TInt CBluetoothSocket::Accept(CBluetoothSocket& aBlankSocket)
       
   846 /** Accepts a connection from a remote Bluetooth socket.
       
   847 
       
   848 The call extracts the first pending connection on a queue of sockets, the queue
       
   849 size being previously specified by Listen(). On successful completion the blank
       
   850 socket is given the handle of the new socket and it may then be used to transfer
       
   851 data. After completion the accept socket may be used to make further connections
       
   852 with new blank sockets (see Open() on how to open a blank socket).
       
   853 
       
   854 @see RSocket::Accept(CBluetoothSocket& aBlankSocket)
       
   855 
       
   856 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
   857 object	supplied when this CBluetoothSocket was constructed. 
       
   858 This is done in the function MBluetoothSocketNotifier::HandleAcceptCompleteL(TInt aErr)
       
   859 @param aBlankSocket A socket opened as a blank socket.
       
   860 @return KErrNone if the connection is established. KErrInUse if a connection already exists.
       
   861 */
       
   862 	{
       
   863 	if(iBTAccepter)
       
   864 		{
       
   865 		return KErrInUse;
       
   866 		}
       
   867 
       
   868 	TRAPD(err, iBTAccepter = CBTAccepter::NewL(*this, aBlankSocket));
       
   869 	if(err == KErrNone)
       
   870 		{
       
   871 		iBTAccepter->Accept();
       
   872 		}
       
   873 	return err;
       
   874 	}
       
   875 
       
   876 EXPORT_C TInt CBluetoothSocket::Accept(CBluetoothSocket& aBlankSocket,TDes8& aConnectData)
       
   877 /** Reserved for future.
       
   878 
       
   879 @param aBlankSocket a socket opened as a blank socket.
       
   880 @param aConnectData Data that may be received in connection.
       
   881 @return KErrNone if the connection is established. KErrInUse if a connection already exists.
       
   882 */
       
   883 	{
       
   884 	if(iBTAccepter)
       
   885 		{
       
   886 		return KErrInUse;
       
   887 		}
       
   888 
       
   889 	TRAPD(err, iBTAccepter = CBTAccepter::NewL(*this, aBlankSocket));
       
   890 	if(err == KErrNone)
       
   891 		{
       
   892 		iBTAccepter->Accept(aConnectData);
       
   893 		}
       
   894 	return err;
       
   895 	}
       
   896 
       
   897 EXPORT_C void CBluetoothSocket::CancelAccept()
       
   898 /** Cancels the Accept() operation.
       
   899 @see RSocket::CancelAccept()
       
   900 */
       
   901 	{
       
   902 	if(iBTAccepter)
       
   903 		{
       
   904 		delete iBTAccepter;
       
   905 		iBTAccepter = NULL;
       
   906 		}
       
   907 	}
       
   908 
       
   909 EXPORT_C TInt CBluetoothSocket::Listen(TUint qSize)
       
   910 /** Sets up a socket to listen for incoming connections.
       
   911 
       
   912 Before calling this procedure a socket should be opened using Open() and be bound 
       
   913 to a local address using Bind().
       
   914 
       
   915 Listen() creates a queue to hold incoming connections which can be married with
       
   916 blank sockets using Accept().
       
   917 
       
   918 Once a listen queue has been created it will continue to allow peers to connect until
       
   919 it is full, at which point it will reject any incoming connections as specified by
       
   920 protocol behaviour. When a socket is accepted by the client a space is made
       
   921 available in the queue.
       
   922 
       
   923 @see RSocket::Listen(TUint qSize)
       
   924 @param qSize The number of connections allowed in the queue.
       
   925 @return The 'listen' queue.
       
   926 */
       
   927 	{
       
   928 	return iSocket.Listen(qSize);
       
   929 	}
       
   930 
       
   931 EXPORT_C TInt CBluetoothSocket::Listen(TUint qSize,const TDesC8& aConnectData)
       
   932 /** Reserved for future.
       
   933 
       
   934 @see RSocket::Listen(TUint qSize,const TDesC8& aConnectData)
       
   935 @param qSize The size of the 'listen' queue.
       
   936 @param aConnectData The descriptor for data sent in connection respoonses.
       
   937 @return The 'listen' queue.
       
   938 */
       
   939 	{
       
   940 	return iSocket.Listen(qSize,aConnectData);
       
   941 	}
       
   942 
       
   943 EXPORT_C TInt CBluetoothSocket::Listen(TUint qSize, TUint16 aServiceBits)
       
   944 /** Sets the Service bits in the Class Of Device. If this is successful, the socket
       
   945 will then be set to Listen for incoming connections.
       
   946 
       
   947 Before calling this procedure a socket should be opened using Open() and be bound 
       
   948 to a local address using Bind().
       
   949 
       
   950 Listen() creates a queue to hold incoming connections which can be married with
       
   951 blank sockets using Accept().
       
   952 
       
   953 Once a listen queue has been created it will continue to allow peers to connect until
       
   954 it is full, at which point it will reject any incoming connections as specified by
       
   955 protocol behaviour. When a socket is accepted by the client a space is made
       
   956 available in the queue.
       
   957 
       
   958 @see RSocket::Listen(TUint qSize)
       
   959 @param qSize The number of connections allowed in the queue.
       
   960 @return The 'listen' queue.
       
   961 */
       
   962 	{
       
   963 	TInt err = KErrNone;
       
   964 	err = iSocket.SetOpt(KBTRegisterCodService, KSolBtSAPBase, aServiceBits);
       
   965 	if(err == KErrNone)
       
   966 		{
       
   967 		return iSocket.Listen(qSize);
       
   968 		}
       
   969 	return err;
       
   970 	}
       
   971 
       
   972 EXPORT_C TInt CBluetoothSocket::SetOption(TUint aOptionName, TUint aOptionLevel,const TDesC8& aOption)
       
   973 /** Sets a socket option.
       
   974 
       
   975 The socket server has options which are generic to all sockets and protocols may add specific options.
       
   976 
       
   977 Options available for all protocols can be set with anOptionLevel set to KSOLSocket. See individual protocol notes for other socket options. 
       
   978 
       
   979 @see RSocket::SetOpt(TUint aOptionName, TUint aOptionLevel,const TDesC8& aOption)
       
   980 @param aOptionName An option identifier.
       
   981 @param aOptionLevel An option level. Option levels 'group' related options.
       
   982 @param aOption The option value as a descriptor.
       
   983 @return The socket options set.
       
   984 */
       
   985 	{
       
   986 	return iSocket.SetOpt(aOptionName, aOptionLevel,aOption);
       
   987 	}
       
   988 	
       
   989 EXPORT_C TInt CBluetoothSocket::SetOpt(TUint aOptionName, TUint aOptionLevel,const TDesC8& aOption)
       
   990 /** Sets a socket option.
       
   991 
       
   992 The socket server has options which are generic to all sockets and protocols may add specific options.
       
   993 
       
   994 Options available for all protocols can be set with anOptionLevel set to KSOLSocket. See individual protocol notes for other socket options. 
       
   995 
       
   996 @see RSocket::SetOpt(TUint aOptionName, TUint aOptionLevel,const TDesC8& aOption)
       
   997 @param aOptionName An option identifier.
       
   998 @param aOptionLevel An option level. Option levels 'group' related options.
       
   999 @param aOption The option value as a descriptor.
       
  1000 @return The socket options set.
       
  1001 @deprecated
       
  1002 */
       
  1003 	{
       
  1004 	return iSocket.SetOpt(aOptionName, aOptionLevel,aOption);
       
  1005 	}
       
  1006 
       
  1007 EXPORT_C TInt CBluetoothSocket::SetOpt(TUint aOptionName,TUint aOptionLevel,TInt aOption)
       
  1008 /**  Sets a socket option.
       
  1009 
       
  1010 The socket server has options which are generic to all sockets and protocols may add specific options.
       
  1011 
       
  1012 Options available for all protocols can be set with anOptionLevel set to KSOLSocket. See individual protocol notes for other socket options. 
       
  1013 
       
  1014 @see RSocket::SetOpt(TUint aOptionName, TUint aOptionLevel,const TDesC8& aOption)
       
  1015 @param aOptionName An option identifier.
       
  1016 @param aOptionLevel An option level. Option levels 'group' related options.
       
  1017 @param aOption The option value as an integer.
       
  1018 @return The socket options set.
       
  1019 */
       
  1020 	{
       
  1021 	return iSocket.SetOpt(aOptionName, aOptionLevel,aOption);
       
  1022 	}
       
  1023 
       
  1024 EXPORT_C TInt CBluetoothSocket::GetOpt(TUint aOptionName,TUint aOptionLevel,TDes8& aOption)
       
  1025 /** Gets a socket option.
       
  1026 
       
  1027 The socket server has options which are generic to all sockets and protocols may add specific options.
       
  1028 
       
  1029 Options available for all protocols can be got with anOptionLevel set to KSOLSocket. See individual protocol notes for other socket options. 
       
  1030 
       
  1031 @see RSocket::GetOpt(TUint aOptionName,TUint aOptionLevel,TDes8& aOption)
       
  1032 @param aOptionName An option identifier.
       
  1033 @param aOptionLevel An option level. Option levels 'group' related options.
       
  1034 @param aOption The option value as a descriptor.
       
  1035 @return The socket options set.
       
  1036 */
       
  1037 	{
       
  1038 	return iSocket.GetOpt(aOptionName, aOptionLevel, aOption);
       
  1039 	}
       
  1040 
       
  1041 EXPORT_C TInt CBluetoothSocket::GetOpt(TUint aOptionName,TUint aOptionLevel,TInt &aOption)
       
  1042 /** Gets a socket option.
       
  1043 
       
  1044 The socket server has options which are generic to all sockets and protocols may add specific options.
       
  1045 
       
  1046 Options available for all protocols can be got with anOptionLevel set to KSOLSocket. See individual protocol notes for other socket options. 
       
  1047 
       
  1048 @see RSocket::GetOpt(TUint aOptionName,TUint aOptionLevel,TDes8& aOption)
       
  1049 @param aOptionName An option identifier.
       
  1050 @param aOptionLevel An option level. Option levels 'group' related options.
       
  1051 @param aOption The option value as an integer.
       
  1052 @return The socket options set.
       
  1053 */
       
  1054 	{
       
  1055 	return iSocket.GetOpt(aOptionName, aOptionLevel, aOption);
       
  1056 	}
       
  1057 
       
  1058 EXPORT_C TInt CBluetoothSocket::Ioctl(TUint aLevel, TUint aCommand,TDes8* aDesc)
       
  1059 /** Applies an asynchronous control operation on a socket.
       
  1060 
       
  1061 Data may be passed and received if a descriptor address is provided as an argument.
       
  1062 Only one Ioctl() operation may be outstanding for each socket.
       
  1063 
       
  1064 @see RSocket::Ioctl(TUint aCommand,TDes8* aDesc,TUint aLevel)
       
  1065 
       
  1066 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
  1067 object	supplied when this CBluetoothSocket was constructed. 
       
  1068 This is done in the function 
       
  1069 MBluetoothSocketNotifier::HandleIoctlCompleteL(TInt aErr)
       
  1070 @param aCommand ioctl command.
       
  1071 @param aDesc Pointer to a descriptor in which data may be sent and received on completion.
       
  1072 @param aLevel Control operation level.
       
  1073 @return KErrNone if the ioctl command is applied or 
       
  1074         KErrInUse if another ioctl command is still being used.
       
  1075 */
       
  1076 	{
       
  1077 	if(iBTIoctler)
       
  1078 		{
       
  1079 		return KErrInUse;
       
  1080 		}
       
  1081 
       
  1082 	TRAPD(err, iBTIoctler = CBTIoctler::NewL(*this));
       
  1083 	if(err == KErrNone)
       
  1084 		{
       
  1085 		iBTIoctler->Ioctl(aCommand, aDesc, aLevel);
       
  1086 		}
       
  1087 	return err;
       
  1088 	}
       
  1089 	
       
  1090 EXPORT_C TInt CBluetoothSocket::Ioctl(TUint aCommand,TDes8* aDesc,TUint aLevel)
       
  1091 /** Applies an asynchronous control operation on a socket.
       
  1092 
       
  1093 Data may be passed and received if a descriptor address is provided as an argument.
       
  1094 Only one Ioctl() operation may be outstanding for each socket.
       
  1095 
       
  1096 @see RSocket::Ioctl(TUint aCommand,TDes8* aDesc,TUint aLevel)
       
  1097 
       
  1098 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
  1099 object	supplied when this CBluetoothSocket was constructed. 
       
  1100 This is done in the function 
       
  1101 MBluetoothSocketNotifier::HandleIoctlCompleteL(TInt aErr)
       
  1102 @param aCommand ioctl command.
       
  1103 @param aDesc Pointer to a descriptor in which data may be sent and received on completion.
       
  1104 @param aLevel Control operation level.
       
  1105 @return KErrNone if the ioctl command is applied or 
       
  1106         KErrInUse if another ioctl command is still being used.
       
  1107 @deprecated
       
  1108 */
       
  1109 	{
       
  1110 	if(iBTIoctler)
       
  1111 		{
       
  1112 		return KErrInUse;
       
  1113 		}
       
  1114 
       
  1115 	TRAPD(err, iBTIoctler = CBTIoctler::NewL(*this));
       
  1116 	if(err == KErrNone)
       
  1117 		{
       
  1118 		iBTIoctler->Ioctl(aCommand, aDesc, aLevel);
       
  1119 		}
       
  1120 	return err;
       
  1121 	}
       
  1122 
       
  1123 EXPORT_C void CBluetoothSocket::CancelIoctl()
       
  1124 /** Cancels the Ioctl() asynchronous control operation.
       
  1125 @see RSocket::CancelIoctl()
       
  1126 */
       
  1127 	{
       
  1128 	if(iBTIoctler)
       
  1129 		{
       
  1130 		delete iBTIoctler;
       
  1131 		iBTIoctler = NULL;
       
  1132 		}
       
  1133 	}
       
  1134 
       
  1135 EXPORT_C TInt CBluetoothSocket::GetDisconnectData(TDes8& aDesc)
       
  1136 /** 
       
  1137 @see RSocket::GetDisconnectData(TDes8& aDesc)
       
  1138 @param aDesc A descriptor for the disconnect data.
       
  1139 @return KErrNone if successful.
       
  1140 */
       
  1141 	{
       
  1142 	return iSocket.GetDisconnectData(aDesc);
       
  1143 	}
       
  1144 
       
  1145 EXPORT_C void CBluetoothSocket::LocalName(TSockAddr& aAddr)
       
  1146 /** Gets the local address of a socket.
       
  1147 
       
  1148 The local address is set either by calling Bind() or it is automatically set when Connect()
       
  1149 is called. If a socket is created through Accept() then a socket will inherit the port of
       
  1150 its parent unless otherwise specified by a protocol's behaviour.
       
  1151 @see RSocket::LocalName(TSockAddr& aAddr)
       
  1152 @param aAddr The socket address.
       
  1153 */
       
  1154 	{
       
  1155 	iSocket.LocalName(aAddr);
       
  1156 	}
       
  1157 
       
  1158 EXPORT_C TUint CBluetoothSocket::LocalPort()
       
  1159 /** Gets the local port of a socket.
       
  1160 
       
  1161 Similar to LocalName().
       
  1162 
       
  1163 @see RSocket::LocalPort()
       
  1164 @return The local port of the socket.
       
  1165 */
       
  1166 	{
       
  1167 	return iSocket.LocalPort();
       
  1168 	}
       
  1169 
       
  1170 EXPORT_C void CBluetoothSocket::RemoteName(TSockAddr& aAddr)
       
  1171 /** Gets the remote address of a socket.
       
  1172 
       
  1173 The remote name (address) of a socket is associated with the remote host the
       
  1174 socket is connected to. The remote name is only valid for a connected socket.
       
  1175 A socket is either connected through calling Connect() or Accept().
       
  1176 
       
  1177 @see RSocket::RemoteName(TSockAddr& aAddr)
       
  1178 @param aAddr The remote socket address.
       
  1179 */
       
  1180 	{
       
  1181 	iSocket.RemoteName(aAddr);
       
  1182 	}
       
  1183 
       
  1184 EXPORT_C TInt CBluetoothSocket::Shutdown(RSocket::TShutdown aHow)
       
  1185 /** Shuts down a connected socket. (The socket cannot be null. @see 
       
  1186 CBluetoothSocket::Info.)
       
  1187 
       
  1188 The shutdown method allows input and output to be individually stopped for
       
  1189 a protocol endpoint.
       
  1190 
       
  1191 @see RSocket::Shutdown(RSocket::TShutdown aHow)
       
  1192 
       
  1193 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
  1194 object	supplied when this CBluetoothSocket was constructed. 
       
  1195 This is done in the function 
       
  1196 MBluetoothSocketNotifier::HandleShutdownCompleteL(TInt aErr)
       
  1197 @param aHow Shutdown option. All variants complete when a socket is disconnected.
       
  1198 @return KErrNone if successful or 
       
  1199         KErrInUse if the system is already trying to shutdown a connected socket.
       
  1200 */
       
  1201 	{
       
  1202 	if(iBTShutdowner)
       
  1203 		{
       
  1204 		return KErrInUse;
       
  1205 		}
       
  1206 
       
  1207 	TRAPD(err, iBTShutdowner = CBTShutdowner::NewL(*this));
       
  1208 	if(err == KErrNone)
       
  1209 		{
       
  1210 		// AutomaticSniff mode should be unset, to avoid situation 
       
  1211 		// when sniff request is sent while we are in the middle of disconnection.
       
  1212 		if ( AutomaticSniffMode() )
       
  1213 			{
       
  1214 			(void) SetAutomaticSniffMode(EFalse);
       
  1215 			}
       
  1216 		// If we are in one of LMP, in order to disconnect, link needs 
       
  1217 		// to be put into acitve mode. It can happen that before link 
       
  1218 		// disconnects LMP requester acknowledges that link is in active mode, 
       
  1219 		// and will send LMP request. Therefore all LMP requesters should 
       
  1220 		// be cancelled before disconnecting socket.		
       
  1221 		else
       
  1222 			{
       
  1223 			(void) CancelLowPowerModeRequester();
       
  1224 			}
       
  1225 		iBTShutdowner->Shutdown(aHow);
       
  1226 		}
       
  1227 	return err;
       
  1228 	}
       
  1229 
       
  1230 EXPORT_C TInt CBluetoothSocket::Shutdown(RSocket::TShutdown aHow,const TDesC8& aDisconnectDataOut,TDes8& aDisconnectDataIn)
       
  1231 /** Reserved for future.
       
  1232 The socket must not be null. (@see CBluetoothSocket::Info.)
       
  1233 @see RSocket::Shutdown(RSocket::TShutdown aHow,const TDesC8& aDisconnectDataOut,TDes8& aDisconnectDataIn)
       
  1234 
       
  1235 When this request completes, notification is sent to the MBluetoothSocketNotifier
       
  1236 object	supplied when this CBluetoothSocket was constructed. 
       
  1237 This is done in the function 
       
  1238 MBluetoothSocketNotifier::HandleShutdownCompleteL(TInt aErr)
       
  1239 @param aHow Shutdown option. All variants complete when a socket is disconnected.
       
  1240 @param aDisconnectDataOut A descriptor to send data.
       
  1241 @param aDisconnectDataIn A descriptor to recive data.
       
  1242 @return KErrNone if successful or 
       
  1243         KErrInUse if the system is already trying to shutdown a connected socket.
       
  1244 */
       
  1245 	{
       
  1246 	if(iBTShutdowner)
       
  1247 		{
       
  1248 		return KErrInUse;
       
  1249 		}
       
  1250 
       
  1251 	TRAPD(err, iBTShutdowner = CBTShutdowner::NewL(*this));
       
  1252 	if(err == KErrNone)
       
  1253 		{
       
  1254 		// AutomaticSniff mode should be unset, to avoid situation 
       
  1255 		// when sniff request is sent while we are in the middle of disconnection.
       
  1256 		if ( AutomaticSniffMode() )
       
  1257 			{
       
  1258 			(void) SetAutomaticSniffMode(EFalse);
       
  1259 			}
       
  1260 		// If we are in one of LMP, in order to disconnect, link needs 
       
  1261 		// to be put into acitve mode. It can happen that before link 
       
  1262 		// disconnects LMP requester acknowledges that link is in active mode, 
       
  1263 		// and will send LMP request. Therefore all LMP requesters should 
       
  1264 		// be cancelled before disconnecting socket.		
       
  1265 		else
       
  1266 			{
       
  1267 			(void) CancelLowPowerModeRequester();
       
  1268 			}
       
  1269 		iBTShutdowner->Shutdown(aHow, aDisconnectDataOut, aDisconnectDataIn);
       
  1270 		}
       
  1271 	return err;
       
  1272 	}
       
  1273 
       
  1274 EXPORT_C void CBluetoothSocket::CancelAll()
       
  1275 /** Cancels all outstanding operations. 
       
  1276 
       
  1277 Calling it will cause all outstanding operations to cancel. Outstanding operations
       
  1278 for a socket include: read, write, Ioctl, connect, accept, shutdown and the Baseband 
       
  1279 event notifier. All of these operations will be completed by this call.
       
  1280 */
       
  1281 	{
       
  1282 	//Sort any active objects
       
  1283 	delete iBTConnecter;
       
  1284 	iBTConnecter = NULL;
       
  1285 	delete iBTAccepter;
       
  1286 	iBTAccepter = NULL;
       
  1287 	delete iBTIoctler;
       
  1288 	iBTIoctler = NULL;
       
  1289 	delete iBTBasebandChangeEventNotifier;
       
  1290 	iBTBasebandChangeEventNotifier = NULL;
       
  1291 	CancelSend(); //ao not deleted
       
  1292 	CancelRecv(); //ao not deleted
       
  1293 	// The shutdowner may need to be canceled.
       
  1294 //	if(iBTShutdowner)
       
  1295 //		{
       
  1296 //		iBTShutdowner->Cancel();
       
  1297 //		}
       
  1298 
       
  1299 	iSocket.CancelAll(); //belt and braces
       
  1300 	}
       
  1301 
       
  1302 EXPORT_C TInt CBluetoothSocket::Info(TProtocolDesc& aProtocol)
       
  1303 /** Gets information about the protocol the socket is opened on.
       
  1304 @see RSocket::Info(TProtocolDesc& aProtocol)
       
  1305 @param aProtocol The protocol on which the socket is opened. Note that if the 
       
  1306 socket is a null socket, and aProtocol is a null TProtocolDesc when it is 
       
  1307 passed in, if will be null on return as well.
       
  1308 @return KErrNone if successful.
       
  1309 */
       
  1310 	{
       
  1311 	return iSocket.Info(aProtocol);
       
  1312 	}
       
  1313 
       
  1314 EXPORT_C TInt CBluetoothSocket::Name(TName& aName)
       
  1315 /** Used to get a unique name of a socket.
       
  1316 
       
  1317 The name is needed when Transfer() is called.
       
  1318 
       
  1319 @see RSocket::Name(TName& aName)
       
  1320 @param aName The system name of a socket.
       
  1321 @return KErrNone if successful.
       
  1322 */
       
  1323 	{
       
  1324 	return iSocket.Name(aName);
       
  1325 	}
       
  1326 
       
  1327 EXPORT_C TInt CBluetoothSocket::Transfer(RSocketServ& aServer, const TDesC& aName)
       
  1328 /** Transfers a socket from one socket server session to another.
       
  1329 
       
  1330 It creates the socket in the target session, and removes the socket from the source
       
  1331 session. The call is made on an uninitialised RSocket object. The socket system name
       
  1332 'Name()' is used to identify the socket to transfer. 
       
  1333 
       
  1334 If the call fails, the socket that is being transferred remains with the original session. 
       
  1335 Success or failure can be checked on the originating socket by calling Info(), which returns
       
  1336 KErrNone if the transfer failed, and KErrBadHandle if it succeeded.
       
  1337 
       
  1338 @see RSocket::Transfer(RSocketServ& aServer, const TDesC& aName)
       
  1339 @param aServer The session to which the socket is being transfered
       
  1340 @param aName The system name of the socket being transfered.
       
  1341 @return KErrNone if successful.
       
  1342 */
       
  1343 	{
       
  1344 	return iSocket.Transfer(aServer,  aName);
       
  1345 	}
       
  1346 
       
  1347 
       
  1348 	
       
  1349 	
       
  1350 
       
  1351 	
       
  1352 //RBTBaseband functions
       
  1353 //Getter
       
  1354 EXPORT_C TInt CBluetoothSocket::PhysicalLinkState(TUint32& aState)
       
  1355 /** Get the state of the physical link.
       
  1356 @see RBTPhysicalLinkAdapter::PhysicalLinkState(TUint32& aState)
       
  1357 @param aState The state of the physical link.
       
  1358 @return The state of the physical link.
       
  1359 */
       
  1360 	{
       
  1361 	return iBTBaseband.PhysicalLinkState(aState);
       
  1362 	}
       
  1363 
       
  1364 
       
  1365 
       
  1366 //Role change functions
       
  1367 EXPORT_C TInt CBluetoothSocket::PreventRoleSwitch()
       
  1368 /** Role switching is not allowed.
       
  1369 @see RBTPhysicalLinkAdapter::PreventRoleSwitch()
       
  1370 @return KErrNone if successful.
       
  1371 */
       
  1372 	{
       
  1373 	return iBTBaseband.PreventRoleSwitch();
       
  1374 	}
       
  1375 
       
  1376 EXPORT_C TInt CBluetoothSocket::AllowRoleSwitch()
       
  1377 /** Allow the socket to switch roles.
       
  1378 @see RBTPhysicalLinkAdapter::AllowRoleSwitch()
       
  1379 @return KErrNone if successful.
       
  1380 */
       
  1381 	{
       
  1382 	return iBTBaseband.AllowRoleSwitch();
       
  1383 	}
       
  1384 
       
  1385 EXPORT_C TInt CBluetoothSocket::RequestMasterRole()
       
  1386 /** Request the socket take on the master role.
       
  1387 @see RBTPhysicalLinkAdapter::RequestMasterRole()
       
  1388 @return KErrNone if successful.
       
  1389 */
       
  1390 	{
       
  1391 	return iBTBaseband.RequestMasterRole();
       
  1392 	}
       
  1393 
       
  1394 EXPORT_C TInt CBluetoothSocket::RequestSlaveRole()
       
  1395 /** Request the socket take on a slave role.
       
  1396 @see RBTPhysicalLinkAdapter::RequestSlaveRole()
       
  1397 @return KErrNone if successful.
       
  1398 */
       
  1399 	{
       
  1400 	return iBTBaseband.RequestSlaveRole();
       
  1401 	}
       
  1402 
       
  1403 
       
  1404 //Low power mode functions
       
  1405 EXPORT_C TInt CBluetoothSocket::PreventLowPowerModes(TUint32 aLowPowerModes)
       
  1406 /** Prevents low power modes from being activated.
       
  1407 @see RBTPhysicalLinkAdapter::PreventLowPowerModes(TUint32 aLowPowerModes)
       
  1408 @param aLowPowerModes The low power mode being prevented.
       
  1409 @return KErrNone if successful.
       
  1410 		KErrInUse if the socket is using automatic sniff mode.
       
  1411 @panic	EInvalidOpWhenAutoSniffOn (10) This API is not allowed when AutoSniff is On
       
  1412 */
       
  1413 	{
       
  1414 	TInt err(KErrNone);
       
  1415 	__ASSERT_ALWAYS(!AutomaticSniffMode(), Panic(EInvalidOpWhenAutoSniffOn));
       
  1416 	err = iBTBaseband.PreventLowPowerModes(aLowPowerModes);
       
  1417 	return err;
       
  1418 	}
       
  1419 
       
  1420 EXPORT_C TInt CBluetoothSocket::AllowLowPowerModes(TUint32 aLowPowerModes)
       
  1421 /** Allow low power modes.
       
  1422 
       
  1423 The power modes are indicated by aLowPowerModes.
       
  1424 
       
  1425 @see RBTPhysicalLinkAdapter::AllowLowPowerModes(TUint32 aLowPowerModes)
       
  1426 @param aLowPowerModes The low power mode.
       
  1427 @return KErrNone if successful.
       
  1428 		KErrInUse if the socket is using automatic sniff mode.
       
  1429 @panic	EInvalidOpWhenAutoSniffOn (10) This API is not allowed when AutoSniff is On
       
  1430 */
       
  1431 	{
       
  1432 	TInt err(KErrNone);
       
  1433 	__ASSERT_ALWAYS(!AutomaticSniffMode(), Panic(EInvalidOpWhenAutoSniffOn));
       
  1434 	err = iBTBaseband.AllowLowPowerModes(aLowPowerModes);
       
  1435 	return err;
       
  1436 	}
       
  1437 
       
  1438 
       
  1439 //Low power mode functions
       
  1440 EXPORT_C TInt CBluetoothSocket::ActivateSniffRequester()
       
  1441 /**
       
  1442 @see RBTPhysicalLinkAdapter::ActivateSniffRequester()
       
  1443 @return KErrNone if successful.
       
  1444 		KErrInUse if the socket is using automatic sniff mode.
       
  1445 @panic	EInvalidOpWhenAutoSniffOn (10) This API is not allowed when AutoSniff is On
       
  1446 */
       
  1447 	{
       
  1448 	TInt err(KErrNone);
       
  1449 	__ASSERT_ALWAYS(!AutomaticSniffMode(), Panic(EInvalidOpWhenAutoSniffOn));
       
  1450 	err = iBTBaseband.ActivateSniffRequester();
       
  1451 	return err;
       
  1452 	}
       
  1453 
       
  1454 EXPORT_C TInt CBluetoothSocket::ActivateParkRequester()
       
  1455 /** Activate park requestor.
       
  1456 @see RBTPhysicalLinkAdapter::ActivateParkRequester()
       
  1457 @return KErrNone if successful.
       
  1458 		KErrInUse if the socket is using automatic sniff mode.
       
  1459 @panic	EInvalidOpWhenAutoSniffOn (10) This API is not allowed when AutoSniff is On
       
  1460 */
       
  1461 	{
       
  1462 	TInt err(KErrNone);
       
  1463 	__ASSERT_ALWAYS(!AutomaticSniffMode(), Panic(EInvalidOpWhenAutoSniffOn));
       
  1464 	err = iBTBaseband.ActivateParkRequester();
       
  1465 	return err;
       
  1466 	}
       
  1467 
       
  1468 EXPORT_C TInt CBluetoothSocket::CancelLowPowerModeRequester()
       
  1469 /** Cancels the low power mode request.
       
  1470 @see RBTPhysicalLinkAdapter::CancelLowPowerModeRequester()
       
  1471 @return KErrNone if successful.
       
  1472 		KErrInUse if the socket is using automatic sniff mode.
       
  1473 @panic	EInvalidOpWhenAutoSniffOn (10) This API is not allowed when AutoSniff is On
       
  1474 */
       
  1475 	{
       
  1476 	TInt err(KErrNone);
       
  1477 	__ASSERT_ALWAYS(!AutomaticSniffMode(), Panic(EInvalidOpWhenAutoSniffOn));
       
  1478 	err = iBTBaseband.CancelLowPowerModeRequester();
       
  1479 	return err;
       
  1480 	}
       
  1481 
       
  1482 
       
  1483 //Packet function
       
  1484 EXPORT_C TInt CBluetoothSocket::RequestChangeSupportedPacketTypes(TUint16 aPacketTypes)
       
  1485 /** Request to change the supported packet types.
       
  1486 @see RBTPhysicalLinkAdapter::RequestChangeSupportedPacketTypes(TUint16 aPacketTypes)
       
  1487 @param Integer representing the packet types.
       
  1488 @return KErrNone if successful.
       
  1489 */
       
  1490 	{
       
  1491 	return iBTBaseband.RequestChangeSupportedPacketTypes(aPacketTypes);
       
  1492 	}
       
  1493 
       
  1494 
       
  1495 //Notification functions
       
  1496 EXPORT_C TInt CBluetoothSocket::ActivateBasebandEventNotifier(TUint32 aEventTypes)
       
  1497 /** Set up notification of baseband events
       
  1498 
       
  1499 Elect to be notified whenever one of a user specified selection
       
  1500 of baseband events occurs.
       
  1501 
       
  1502 The notification takes place in the pure virtual function 
       
  1503 MBluetoothSocketNotifier::HandleActivateBasebandEventNotifierCompleteL(TInt aErr, TBTBasebandEventNotification& aEventNotification)
       
  1504 
       
  1505 The FIRST notification will take place ALMOST INSTANTANEOUSLY and will be 
       
  1506 A FULL DESCRIPTION (OR SNAPSHOT) OF THE CURRENT STATE OF THE PHYSICAL LINK 
       
  1507 irrespective of the specified selection of events wanted. This first notification or
       
  1508 snapshot has NOTHING to do with a baseband event occurring. Once this snapshot has 
       
  1509 taken place, all occurrences of the specified events from the instant of the snapshot 
       
  1510 onwards will be notified until CBluetoothSocket::CancelBasebandEventNotifier() is called.
       
  1511 
       
  1512 @see PhysicalLinkState(TUint32& aState) if only a single full 
       
  1513 description of the physical link state is required.
       
  1514 
       
  1515 @param aEventTypes bitmask for those events for which notification is being requested 
       
  1516 (Combine elements of TBTPhysicalLinkStateNotifier and TBTPhysicalLinkStateNotifierCombinations)
       
  1517 @see TBTPhysicalLinkStateNotifier
       
  1518 @see TBTPhysicalLinkStateNotifierCombinations
       
  1519 @return KErrNone if activated or KErrInUse if the baseband event notifier is already being used.
       
  1520 */
       
  1521 	{
       
  1522 	if(iBTBasebandChangeEventNotifier)
       
  1523 		{
       
  1524 		return KErrInUse;
       
  1525 		}
       
  1526 
       
  1527 	TRAPD(err, iBTBasebandChangeEventNotifier = CBTBasebandChangeEventNotifier::NewL(*iBTBasebandChangeEventDelegate));
       
  1528 	if(err == KErrNone)
       
  1529 		{
       
  1530 		iNotifierEventMask = aEventTypes;
       
  1531 		iBTBasebandChangeEventNotifier->SetNotify(iNotifierEventMask);
       
  1532 		}
       
  1533 	return err;
       
  1534 	}
       
  1535 
       
  1536 EXPORT_C void CBluetoothSocket::CancelBasebandEventNotifier()
       
  1537 /** Cancels an active baseband notifier.
       
  1538 
       
  1539 No further baseband events will be notified.
       
  1540 
       
  1541 If this function is called when there is no active baseband notifier, 
       
  1542 it will do nothing.
       
  1543 */
       
  1544 	{
       
  1545 	if(iBTBasebandChangeEventNotifier)
       
  1546 		{
       
  1547 		delete iBTBasebandChangeEventNotifier;//calls Cancel()
       
  1548 		iBTBasebandChangeEventNotifier = NULL;
       
  1549 		}
       
  1550 	}
       
  1551 	
       
  1552 EXPORT_C void CBluetoothSocket::SetNotifier(MBluetoothSocketNotifier& aNewNotifier)
       
  1553 /** Changes the owner (receiver of notifications) of the socket.
       
  1554 
       
  1555 The previous notifier will not be deleted after the change has been made.
       
  1556 
       
  1557 @param aNewNotifier The object which will receive asynchronous events.
       
  1558 */
       
  1559 	{
       
  1560 	// We do not delete the current notifier as although it is stored as a
       
  1561 	// pointer we do not own it. (it is always passed to us by reference).
       
  1562 	iNotifier = &aNewNotifier;
       
  1563 	}
       
  1564 
       
  1565 EXPORT_C TInt CBluetoothSocket::SetAutomaticSniffMode(TBool aAutoSniffMode)
       
  1566 /** Sets up automatic sniff/unsniff requesting for the socket. 
       
  1567 
       
  1568 If Automatic Sniff requesting is being selected, a timer with default delay in seconds before the idle socket will
       
  1569 request the link to go into sniff mode. The CBluetoothSocket::Write() method will clear an outstanding sniff
       
  1570 requester and start the CAutoSniffDelayTimer active object which will issue another request for sniff mode once it
       
  1571 times out.
       
  1572 
       
  1573 If Automatic Sniff requesting is being deselected, a previous sniff requester will be cancelled.
       
  1574 
       
  1575 If Automatic Sniff requesting is selected when it is already active, no change will be performed and KErrInUse will
       
  1576 be returned
       
  1577 
       
  1578 @param aAutoSniffMode ETrue if Automatic Sniff/Unsniff requests are required.
       
  1579 @return KErrNone if the automatic sniff mode has been successfully changed.
       
  1580 		KErrInUse if automatic sniff mode is requested, when it is already active.
       
  1581 		KErrArgument if an invalid idle timer value for the sniff request is provided.
       
  1582 		A Symbian wide error code for another other error that might have occurred.
       
  1583 */
       
  1584 	{
       
  1585 	const TInt KDefaultSecondsBeforeSniffRequest = 5;
       
  1586 	return SetAutomaticSniffMode(aAutoSniffMode, KDefaultSecondsBeforeSniffRequest);
       
  1587 	}
       
  1588 
       
  1589 EXPORT_C TInt CBluetoothSocket::SetAutomaticSniffMode(TBool aAutoSniffMode, TInt aIdleSecondsBeforeSniffRequest)
       
  1590 /** Sets up automatic sniff/unsniff requesting for the socket. 
       
  1591 
       
  1592 If Automatic Sniff requesting is being selected, supply the delay in seconds required before the idle socket will 
       
  1593 request the link to go into sniff mode. The CBluetoothSocket::Write() method will clear an outstanding sniff 
       
  1594 requester and start the CAutoSniffDelayTimer active object which will issue another request for sniff mode once 
       
  1595 it times out.
       
  1596 
       
  1597 If Automatic Sniff requesting is being deselected, a previous sniff requester will be cancelled.
       
  1598 
       
  1599 If Automatic Sniff requesting is selected when it is already active, no change will be performed and KErrInUse will
       
  1600 be returned
       
  1601 
       
  1602 @param aAutoSniffMode ETrue if Automatic Sniff/Unsniff requests are required.
       
  1603 @param aIdleSecondsBeforeSniffRequest A delay after the socket becomes idle before it will request sniff mode.
       
  1604 @return KErrNone if the automatic sniff mode has been successfully changed.
       
  1605 		KErrInUse if automatic sniff mode is requested, when it is already active.
       
  1606 		KErrArgument if an invalid idle timer value for the sniff request is provided.
       
  1607 		A Symbian wide error code for another other error that might have occurred.
       
  1608 */
       
  1609 	{
       
  1610 	TInt err(KErrNone);
       
  1611 	TBool usingAutoSniffMode = AutomaticSniffMode();
       
  1612 	
       
  1613 	if(aAutoSniffMode && usingAutoSniffMode)
       
  1614 		{
       
  1615 		// if we request auto sniff requesting when it is already active we error.
       
  1616 		err = KErrInUse;
       
  1617 		}
       
  1618 	else if(aAutoSniffMode)
       
  1619 		{
       
  1620 		if(aIdleSecondsBeforeSniffRequest > 0)
       
  1621 			{
       
  1622 			// if here we know iAutoSniffDelayTimer is NULL, so there is no need to delete
       
  1623 			__ASSERT_DEBUG(!iAutoSniffDelayTimer, Panic(EUnfinishedBusiness));
       
  1624 			TRAP(err, iAutoSniffDelayTimer = CAutoSniffDelayTimer::NewL(*this, aIdleSecondsBeforeSniffRequest));
       
  1625 			if(err == KErrNone)
       
  1626 				{
       
  1627 				err = iBTBaseband.CancelLowPowerModeRequester();
       
  1628 				}
       
  1629 			}
       
  1630 		else
       
  1631 			{
       
  1632 			err = KErrArgument;
       
  1633 			}
       
  1634 		}
       
  1635 	else if(usingAutoSniffMode)
       
  1636 		{
       
  1637 		delete iAutoSniffDelayTimer;
       
  1638         iAutoSniffDelayTimer = NULL;
       
  1639 		}
       
  1640 
       
  1641 	return err;
       
  1642 	}
       
  1643 
       
  1644 
       
  1645 EXPORT_C TBool CBluetoothSocket::AutomaticSniffMode() const
       
  1646 /** Reports the automatic sniff/unsniff requesting status of the socket.
       
  1647 
       
  1648 @return ETrue if automatic sniff/unsniff has been selected for this socket.
       
  1649 */
       
  1650 	{
       
  1651 	return iAutoSniffDelayTimer ? ETrue : EFalse;
       
  1652 	}
       
  1653 
       
  1654 EXPORT_C void CBluetoothSocket::AsyncDelete()
       
  1655 /** Asynchronous deletion of the CBluetoothSocket object.
       
  1656 
       
  1657 Function will cause asynchronous deletion of CBluetoothSocket object. 
       
  1658 It should be called if associated MBluetoothSocketNotifier needs to delete 
       
  1659 CBluetoothSocket from any of its callback functions.
       
  1660 */
       
  1661 	{
       
  1662 	iAsyncDestroyer->CallBack();
       
  1663 	}
       
  1664 
       
  1665 
       
  1666 MBluetoothSocketNotifier& CBluetoothSocket::Notifier()
       
  1667 	{
       
  1668 	// iNotifier should never be null, so a deref. is always valid.
       
  1669 	return static_cast<MBluetoothSocketNotifier&> (*iNotifier);
       
  1670 	}
       
  1671 
       
  1672 void CBluetoothSocket::ConfigureSocket()
       
  1673 	{
       
  1674 	// This is done as a best effort in order to make more efficient usage
       
  1675 	// of the baseband packets being created. As a result we don't bother 
       
  1676 	// checking the results as if they fail it won't cause the socket to 
       
  1677 	// behave incorrectly	
       
  1678 	TPckgBuf<TUint> optBuf(65535); //If all else fails we should get a max 64K buffer
       
  1679 	TProtocolDesc info;
       
  1680 	
       
  1681 	TInt err = iSocket.Info(info);
       
  1682 	
       
  1683 	if (err == KErrNone && info.iProtocol == KRFCOMM)
       
  1684 		{
       
  1685 		// For RFCOMM we set the buffers based on the optimal MTU for RFCOMM	
       
  1686 		iSocket.GetOpt(KRFCOMMMaximumMTU, KSolBtRFCOMM, optBuf);
       
  1687 		iSocket.SetOpt(KSOSendBuf, KSOLSocket, optBuf);
       
  1688 		iSocket.SetOpt(KSORecvBuf, KSOLSocket, optBuf);		
       
  1689 		}
       
  1690 	else
       
  1691 		{
       
  1692 		// For L2CAP, or others, we just ensure that we can accomodate the negotiated MTU
       
  1693 		iSocket.GetOpt(KL2CAPNegotiatedOutboundMTU, KSolBtL2CAP, optBuf);
       
  1694 		iSocket.SetOpt(KSOSendBuf, KSOLSocket, optBuf);
       
  1695 			
       
  1696 		iSocket.GetOpt(KL2CAPInboundMTU, KSolBtL2CAP, optBuf);
       
  1697 		iSocket.SetOpt(KSORecvBuf, KSOLSocket, optBuf);
       
  1698 		}
       
  1699 	}
       
  1700 
       
  1701 void CBluetoothSocket::HandleConnectCompleteL(TInt aErr)
       
  1702 	{
       
  1703 	FTRACE(FPrint(_L("CBluetoothSocket::HandleConnectCompleteL(aErr %d)"), aErr));
       
  1704 	delete iBTConnecter;
       
  1705 	iBTConnecter = NULL;
       
  1706 
       
  1707 	if (aErr == KErrNone)
       
  1708 		{
       
  1709 		ConfigureSocket();
       
  1710 		}
       
  1711 	
       
  1712 	// If AsyncDestroyer was called, client could delete associated MBluetoothSocketNotifier
       
  1713 	// therefore no further calls to iNotifier should be made.
       
  1714 	if (!iAsyncDestroyer->IsActive())
       
  1715 		{
       
  1716 #ifdef __FLOGGING__
       
  1717 	TRAPD(err, Notifier().HandleConnectCompleteL(aErr));
       
  1718 	FTRACE(FPrint(_L("Connect upcall to link owner returned %d"), err));
       
  1719 	User::LeaveIfError(err);
       
  1720 #else
       
  1721 	Notifier().HandleConnectCompleteL(aErr);
       
  1722 #endif
       
  1723 		}
       
  1724 		
       
  1725 	// Third parties intend to delete CBluetoothSocket object from upcall to
       
  1726 	// associated MBluetoothSocketNotifier, therefore we should make sure that 
       
  1727 	// there is no code to execute after upcall Notifier().HandleConnectCompleteL(aErr);
       
  1728 	}
       
  1729 
       
  1730 void CBluetoothSocket::HandleAcceptCompleteL(TInt aErr)
       
  1731 	{
       
  1732 	FTRACE(FPrint(_L("CBluetoothSocket::HandleAcceptCompleteL(aErr %d)"), aErr));
       
  1733 	delete iBTAccepter;
       
  1734 	iBTAccepter = NULL;
       
  1735 
       
  1736 	// If AsyncDestroyer was called, client could delete associated MBluetoothSocketNotifier
       
  1737 	// therefore no further calls to iNotifier should be made.
       
  1738 	if (!iAsyncDestroyer->IsActive())
       
  1739 		{
       
  1740 #ifdef __FLOGGING__
       
  1741 	TRAPD(err, Notifier().HandleAcceptCompleteL(aErr));
       
  1742 	FTRACE(FPrint(_L("Accept upcall to link owner returned %d"), err));
       
  1743 	User::LeaveIfError(err);
       
  1744 #else
       
  1745 	Notifier().HandleAcceptCompleteL(aErr);
       
  1746 #endif
       
  1747 		}
       
  1748 		
       
  1749 	// Third parties intend to delete CBluetoothSocket object from upcall to
       
  1750 	// associated MBluetoothSocketNotifier, therefore we should make sure that 
       
  1751 	// there is no code to execute after upcall Notifier().HandleAcceptCompleteL(aErr);	
       
  1752 	}
       
  1753 
       
  1754 void CBluetoothSocket::HandleShutdownCompleteL(TInt aErr)
       
  1755 	{
       
  1756 	FTRACE(FPrint(_L("CBluetoothSocket::HandleShutdownCompleteL(aErr %d)"), aErr));
       
  1757 
       
  1758 	CancelAll();
       
  1759 	BTBasebandManager().HandleConnectionDown();
       
  1760 
       
  1761 	delete iBTShutdowner;
       
  1762 	iBTShutdowner = NULL;
       
  1763 
       
  1764 	// If AsyncDestroyer was called, client could delete associated MBluetoothSocketNotifier
       
  1765 	// therefore no further calls to iNotifier should be made.
       
  1766 	if (!iAsyncDestroyer->IsActive())
       
  1767 		{
       
  1768 #ifdef __FLOGGING__
       
  1769 	TRAPD(err, Notifier().HandleShutdownCompleteL(aErr));
       
  1770 	FTRACE(FPrint(_L("Shutdown upcall to link owner returned %d"), err));
       
  1771 	User::LeaveIfError(err);
       
  1772 #else
       
  1773 	Notifier().HandleShutdownCompleteL(aErr);
       
  1774 #endif
       
  1775 		}
       
  1776 		
       
  1777 	// Third parties intend to delete CBluetoothSocket object from upcall to
       
  1778 	// associated MBluetoothSocketNotifier, therefore we should make sure that 
       
  1779 	// there is no code to execute after upcall Notifier().HandleShutdownCompleteL(aErr);	
       
  1780 	}
       
  1781 
       
  1782 void CBluetoothSocket::HandleSendCompleteL(TInt aErr)
       
  1783 	{
       
  1784 	FTRACE(FPrint(_L("CBluetoothSocket::HandleSendCompleteL(aErr %d)"), aErr));
       
  1785 	iSending = EFalse;
       
  1786 
       
  1787 	// If AsyncDestroyer was called, client could delete associated MBluetoothSocketNotifier
       
  1788 	// therefore no further calls to iNotifier should be made.
       
  1789 	if (!iAsyncDestroyer->IsActive())
       
  1790 		{
       
  1791 		Notifier().HandleSendCompleteL(aErr);
       
  1792 		}
       
  1793 		
       
  1794 	FLOG(_L("Send complete upcall complete"));
       
  1795 	// Third parties intend to delete CBluetoothSocket object from upcall to
       
  1796 	// associated MBluetoothSocketNotifier, therefore we should make sure that 
       
  1797 	// there is no code to execute after upcall Notifier().HandleSendCompleteL(aErr);	
       
  1798 	}
       
  1799 
       
  1800 void CBluetoothSocket::HandleReceiveCompleteL(TInt aErr)
       
  1801 	{
       
  1802 	FTRACE(FPrint(_L("CBluetoothSocket::HandleReceiveCompleteL(aErr %d)"), aErr));
       
  1803 	iReceiving = EFalse;
       
  1804 	if(iAutoSniffDelayTimer)
       
  1805 		{
       
  1806 		iAutoSniffDelayTimer->Start();
       
  1807 		}
       
  1808 
       
  1809 	// If AsyncDestroyer was called, client could delete associated MBluetoothSocketNotifier
       
  1810 	// therefore no further calls to iNotifier should be made.
       
  1811 	if (!iAsyncDestroyer->IsActive())
       
  1812 		{
       
  1813 		Notifier().HandleReceiveCompleteL(aErr);
       
  1814 		}
       
  1815 	
       
  1816 	FLOG(_L("Receive complete upcall complete"));
       
  1817 	// Third parties intend to delete CBluetoothSocket object from upcall to
       
  1818 	// associated MBluetoothSocketNotifier, therefore we should make sure that 
       
  1819 	// there is no code to execute after upcall Notifier().HandleReceiveCompleteL(aErr);	
       
  1820 	}
       
  1821 
       
  1822 void CBluetoothSocket::HandleIoctlCompleteL(TInt aErr)
       
  1823 	{
       
  1824 	FTRACE(FPrint(_L("CBluetoothSocket::HandleIoctlCompleteL(aErr %d)"), aErr));
       
  1825 	delete iBTIoctler;
       
  1826 	iBTIoctler = NULL;
       
  1827 
       
  1828 	// If AsyncDestroyer was called, client could delete associated MBluetoothSocketNotifier
       
  1829 	// therefore no further calls to iNotifier should be made.
       
  1830 	if (!iAsyncDestroyer->IsActive())
       
  1831 		{	
       
  1832 #ifdef __FLOGGING__
       
  1833 		TRAPD(err, Notifier().HandleIoctlCompleteL(aErr));
       
  1834 		FTRACE(FPrint(_L("Ioctl upcall to link owner returned %d"), err));
       
  1835 		User::LeaveIfError(err);
       
  1836 #else
       
  1837 		Notifier().HandleIoctlCompleteL(aErr);
       
  1838 #endif
       
  1839 		}
       
  1840 	// Third parties intend to delete CBluetoothSocket object from upcall to
       
  1841 	// associated MBluetoothSocketNotifier, therefore we should make sure that 
       
  1842 	// there is no code to execute after upcall Notifier().HandleIoctlCompleteL(aErr);	
       
  1843 	}
       
  1844 
       
  1845 void CBluetoothSocket::HandleActivateBasebandEventNotifierCompleteL(TInt aErr, TBTBasebandEventNotification aEventNotification)
       
  1846 	{
       
  1847 	iBTBasebandChangeEventNotifier->SetNotify(iNotifierEventMask);
       
  1848 	
       
  1849 	// If AsyncDestroyer was called, client could delete associated MBluetoothSocketNotifier
       
  1850 	// therefore no further calls to iNotifier should be made.
       
  1851 	if (!iAsyncDestroyer->IsActive())
       
  1852 		{
       
  1853 		Notifier().HandleActivateBasebandEventNotifierCompleteL(aErr, aEventNotification);	
       
  1854 		}
       
  1855 		
       
  1856 	// Third parties intend to delete CBluetoothSocket object from upcall to
       
  1857 	// associated MBluetoothSocketNotifier, therefore we should make sure that 
       
  1858 	// there is no code to execute after upcall Notifier().HandleActivateBasebandEventNotifierCompleteL(aErr, aEventNotification);	
       
  1859 	}
       
  1860 
       
  1861 //
       
  1862 //Getters
       
  1863 //
       
  1864 
       
  1865 RSocket& CBluetoothSocket::Socket()
       
  1866 	{
       
  1867 	return iSocket;
       
  1868 	}
       
  1869 
       
  1870 RSocketServ& CBluetoothSocket::SocketServer() 
       
  1871 	{
       
  1872 	return iSockServer;
       
  1873 	}
       
  1874 
       
  1875 RBTBaseband& CBluetoothSocket::BTBaseband() 
       
  1876 	{
       
  1877 	return iBTBaseband;
       
  1878 	}
       
  1879 
       
  1880 CBTBasebandManager& CBluetoothSocket::BTBasebandManager()
       
  1881 	{
       
  1882 	return *iBTBasebandManager;
       
  1883 	}
       
  1884 
       
  1885 
       
  1886 
       
  1887 
       
  1888 //
       
  1889 //Private Functions
       
  1890 //
       
  1891 
       
  1892 CBluetoothSocket::CBluetoothSocket(MBluetoothSocketNotifier& aNotifier, RSocketServ& aServer)
       
  1893 : iSockServer(aServer), iNotifier(&aNotifier), iNotifierEventMask(0), iSending(EFalse), iReceiving(EFalse)
       
  1894 //
       
  1895 //Constructor
       
  1896 //
       
  1897 	{
       
  1898 	}
       
  1899 
       
  1900 //RSocket 'Open' functions now called in NewL overloads
       
  1901 void CBluetoothSocket::ConstructL(TUint sockType, TUint protocol)
       
  1902 	{
       
  1903 	User::LeaveIfError(Socket().Open(iSockServer, KBTAddrFamily, sockType, protocol));
       
  1904 	InitialiseL();
       
  1905 	}
       
  1906 
       
  1907 void CBluetoothSocket::ConstructL(TUint sockType,TUint protocol, RConnection& aConnection)
       
  1908 	{
       
  1909 	User::LeaveIfError(Socket().Open(iSockServer, KBTAddrFamily, sockType, protocol, aConnection));
       
  1910 	InitialiseL();
       
  1911 	}
       
  1912 
       
  1913 void CBluetoothSocket::ConstructL(const TDesC& aName)
       
  1914 	{
       
  1915 	User::LeaveIfError(Socket().Open(iSockServer, aName));
       
  1916 	InitialiseL();
       
  1917 	}
       
  1918 
       
  1919 void CBluetoothSocket::ConstructL()
       
  1920 	{
       
  1921 	User::LeaveIfError(Socket().Open(iSockServer));
       
  1922 	InitialiseL();
       
  1923 	}
       
  1924 
       
  1925 void CBluetoothSocket::ConstructL(RSocket& aSocket)
       
  1926 	{
       
  1927 	// We create the Sender and Receiver first, so that if either
       
  1928 	// leave during construction the original RSocket handle will
       
  1929 	// still be valid (as it has not yet been transferred.)
       
  1930 	InitialiseL();
       
  1931 	
       
  1932 	// If we leave from now on, the original RSocket should still
       
  1933 	// be valid (as it won't have successfully transferred.)	
       
  1934 	TName name;
       
  1935 	aSocket.Name(name);
       
  1936 	User::LeaveIfError(Socket().Transfer(SocketServer(), name));
       
  1937 	
       
  1938 	TProtocolDesc protocolDesc;
       
  1939 	// We expect a KErrBadHandle from the original socket.
       
  1940 	if (KErrBadHandle != aSocket.Info(protocolDesc))
       
  1941 		{
       
  1942 		// Otherwise we leave with the status of the internal RSocket.
       
  1943 		User::Leave(Socket().Info(protocolDesc));
       
  1944 		}
       
  1945 	}
       
  1946 
       
  1947 void CBluetoothSocket::InitialiseL()
       
  1948 	{
       
  1949 	iBTSender = CBTSender::NewL(*this);
       
  1950 	iBTReceiver = CBTReceiver::NewL(*this);
       
  1951 	iBTBasebandManager = CBTBasebandManager::NewL();
       
  1952 	iBTBasebandChangeEventDelegate = CBTBasebandChangeEventDelegate::NewL(*this);
       
  1953 	User::LeaveIfError(BTBasebandManager().RegisterBTBaseband(*iBTBasebandChangeEventDelegate));
       
  1954 	
       
  1955 	TCallBack asyncDelete(AsyncDeleteCallBack, this);
       
  1956 	iAsyncDestroyer = new (ELeave) CAsyncCallBack(asyncDelete, CActive::EPriorityStandard);
       
  1957 	}
       
  1958 	
       
  1959 TInt CBluetoothSocket::AsyncDeleteCallBack(TAny *aThisPtr)
       
  1960 	{
       
  1961 	__ASSERT_DEBUG(aThisPtr, Panic(ENullPointerInAsyncDelete));
       
  1962 	CBluetoothSocket* self = static_cast<CBluetoothSocket*>(aThisPtr);
       
  1963 	delete self;
       
  1964 	return KErrNone;
       
  1965 	}
       
  1966 	
       
  1967 
       
  1968 //==========================================================
       
  1969 //Active Object Helpers
       
  1970 //
       
  1971 
       
  1972 //
       
  1973 //for CBluetoothSocket
       
  1974 //
       
  1975 
       
  1976 //--
       
  1977 CBTConnecter* CBTConnecter::NewL(CBluetoothSocket& aParent)
       
  1978 	{
       
  1979 	CBTConnecter* self = new (ELeave) CBTConnecter(aParent);
       
  1980 	CleanupStack::PushL(self);
       
  1981 	self->ConstructL();
       
  1982 	CleanupStack::Pop();//self
       
  1983 	return self;
       
  1984 	}
       
  1985 
       
  1986 CBTConnecter::CBTConnecter(CBluetoothSocket& aParent)
       
  1987 : CActive(CActive::EPriorityStandard),iParent(aParent)
       
  1988 //
       
  1989 //Constructor - the CBluetoothSocket parent is provided. 
       
  1990 //It contains all the ESocky stuff and has suitable "Getters".
       
  1991 //
       
  1992 	{
       
  1993 	}
       
  1994 
       
  1995 void CBTConnecter::ConstructL()
       
  1996 	{
       
  1997 	CActiveScheduler::Add(this);
       
  1998 	}
       
  1999 
       
  2000 CBTConnecter::~CBTConnecter()
       
  2001 //
       
  2002 //Destructor
       
  2003 //
       
  2004 	{
       
  2005 	Cancel();
       
  2006 	iActiveModeRequester.Close();
       
  2007 	}
       
  2008 
       
  2009 void CBTConnecter::Connect(TBTSockAddr& aSockAddr)
       
  2010 //
       
  2011 //Attempts to connect to the remote device. 
       
  2012 //
       
  2013 	{
       
  2014 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2015 	
       
  2016 	iParent.Socket().Connect(aSockAddr, iStatus);
       
  2017 	
       
  2018 	
       
  2019 	if (iParent.AutomaticSniffMode())
       
  2020 		{
       
  2021 		TBTDevAddr devAddr = aSockAddr.BTAddr();
       
  2022 		TInt err = iActiveModeRequester.Open(iParent.SocketServer(), devAddr);
       
  2023 		if(err == KErrNone)
       
  2024 			{
       
  2025 			// If there is an error, there is nothing we can do about it. This is safe to
       
  2026 			// do as the only effect is that we will fail to ensure connection is done in active
       
  2027 			// mode.
       
  2028 			(void)iActiveModeRequester.RequestExplicitActiveMode(ETrue);
       
  2029 			}
       
  2030 		}
       
  2031 	
       
  2032 	SetActive();
       
  2033 	}
       
  2034 
       
  2035 void CBTConnecter::Connect(TBTSockAddr& aSockAddr, const TDesC8& aConnectDataOut, TDes8& aConnectDataIn)
       
  2036 //
       
  2037 //Attempts to connect to the remote device. 
       
  2038 //
       
  2039 	{
       
  2040 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2041 	
       
  2042 	iParent.Socket().Connect(aSockAddr, aConnectDataOut, aConnectDataIn, iStatus);
       
  2043 	
       
  2044 	if (iParent.AutomaticSniffMode())
       
  2045 		{
       
  2046 		TBTDevAddr devAddr = aSockAddr.BTAddr();
       
  2047 		TInt err = iActiveModeRequester.Open(iParent.SocketServer(), devAddr);
       
  2048 		if(err == KErrNone)
       
  2049 			{
       
  2050 			// If there is an error, there is nothing we can do about it. This is safe to
       
  2051 			// do as the only effect is that we will fail to ensure connection is done in active
       
  2052 			// mode.
       
  2053 			(void)iActiveModeRequester.RequestExplicitActiveMode(ETrue);
       
  2054 			}
       
  2055 		}
       
  2056 		
       
  2057 	SetActive();
       
  2058 	}
       
  2059 
       
  2060 void CBTConnecter::RunL()
       
  2061 //
       
  2062 //When logical socket has connected (only async bit), 
       
  2063 //opens baseband socket.
       
  2064 //
       
  2065 	{
       
  2066 	TInt err = iStatus.Int();
       
  2067 	if(iStatus == KErrNone)
       
  2068 		{
       
  2069 		err = BasebandAccessor();
       
  2070 		}
       
  2071 
       
  2072 	// We have now finished with this temporary SAP.
       
  2073 	iActiveModeRequester.Close();
       
  2074 
       
  2075 	//must come LAST .. deletes this active object
       
  2076 	iParent.HandleConnectCompleteL(err); 
       
  2077 	}
       
  2078 
       
  2079 TInt CBTConnecter::RunError(TInt aError)
       
  2080 	{
       
  2081 	#ifdef __FLOGGING__
       
  2082 	FTRACE(FPrint(_L("CBluetoothSocket::HandleConnectCompleteL left with error %d"), aError));
       
  2083 	#else
       
  2084 	(void) aError;
       
  2085 	#endif
       
  2086 	return KErrNone;
       
  2087 	}
       
  2088 
       
  2089 void CBTConnecter::DoCancel()
       
  2090 //
       
  2091 //Cancels current asynchronous requests.
       
  2092 //
       
  2093 	{
       
  2094 	iParent.Socket().CancelConnect();
       
  2095 	iActiveModeRequester.Close();
       
  2096 	}
       
  2097 	
       
  2098 //Call HandleConnectionUp() to open registered basebands
       
  2099 TInt CBTConnecter::BasebandAccessor()
       
  2100 	{
       
  2101 	TInt err = iParent.BTBasebandManager().HandleConnectionUp(iParent.SocketServer(), iParent.Socket());
       
  2102 	return err;
       
  2103 	}
       
  2104 
       
  2105 
       
  2106 
       
  2107 
       
  2108 
       
  2109 
       
  2110 
       
  2111 //--
       
  2112 CBTAccepter* CBTAccepter::NewL(CBluetoothSocket& aParent, CBluetoothSocket& aBlankSocket)
       
  2113 	{
       
  2114 	CBTAccepter* self = new (ELeave) CBTAccepter(aParent, aBlankSocket);
       
  2115 	CleanupStack::PushL(self);
       
  2116 	self->ConstructL();
       
  2117 	CleanupStack::Pop();//self
       
  2118 	return self;
       
  2119 	}
       
  2120 
       
  2121 CBTAccepter::CBTAccepter(CBluetoothSocket& aParent, CBluetoothSocket& aBlankSocket)
       
  2122 : CActive(CActive::EPriorityStandard),iParent(aParent),iBlankSocket(aBlankSocket)
       
  2123 //
       
  2124 //Constructorthe CBluetoothSocket parent is provided.
       
  2125 //It contains all the ESocky stuff and has suitable "Getters".
       
  2126 //
       
  2127 	{
       
  2128 	}
       
  2129 
       
  2130 void CBTAccepter::ConstructL()
       
  2131 	{
       
  2132 	CActiveScheduler::Add(this);
       
  2133 	}
       
  2134 
       
  2135 CBTAccepter::~CBTAccepter()
       
  2136 //
       
  2137 //Destructor
       
  2138 //
       
  2139 	{
       
  2140 	Cancel();
       
  2141 	}
       
  2142 
       
  2143 void CBTAccepter::Accept()
       
  2144 //
       
  2145 //Attempts to accept from the remote device. 
       
  2146 //
       
  2147 	{
       
  2148 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2149 	iParent.Socket().Accept(iBlankSocket.Socket(), iStatus);
       
  2150 	SetActive();
       
  2151 	}
       
  2152 
       
  2153 void CBTAccepter::Accept(TDes8& aConnectData)
       
  2154 //
       
  2155 //Attempts to accept from the remote device. 
       
  2156 //
       
  2157 	{
       
  2158 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2159 	
       
  2160 	iParent.Socket().Accept(iBlankSocket.Socket(), aConnectData, iStatus);
       
  2161 	SetActive();
       
  2162 	}
       
  2163 
       
  2164 void CBTAccepter::RunL()
       
  2165 //
       
  2166 //When logical socket has connected (only async bit), 
       
  2167 //opens baseband socket.
       
  2168 //
       
  2169 	{
       
  2170 	if(iStatus == KErrNone)
       
  2171 		{
       
  2172 		BasebandAccessor();
       
  2173 		iBlankSocket.ConfigureSocket();
       
  2174 		}
       
  2175 
       
  2176 	//must come LAST .. deletes this active object
       
  2177 	iParent.HandleAcceptCompleteL(iStatus.Int()); 
       
  2178 	}
       
  2179 
       
  2180 TInt CBTAccepter::RunError(TInt aError)
       
  2181 	{
       
  2182 	#ifdef __FLOGGING__
       
  2183 	FTRACE(FPrint(_L("CBluetoothSocket::HandleAcceptCompleteL left with error %d"), aError));
       
  2184 	#else
       
  2185 	(void) aError;
       
  2186 	#endif
       
  2187 	return KErrNone;
       
  2188 	}
       
  2189 
       
  2190 void CBTAccepter::DoCancel()
       
  2191 //
       
  2192 //Cancels current asynchronous requests.
       
  2193 //
       
  2194 	{
       
  2195 	iParent.Socket().CancelAccept();
       
  2196 	}
       
  2197 
       
  2198 void CBTAccepter::BasebandAccessor()
       
  2199 	{
       
  2200 	// iBlankSocket
       
  2201 	iBlankSocket.BTBasebandManager().HandleConnectionUp(iBlankSocket.SocketServer(), iBlankSocket.Socket());
       
  2202 	}
       
  2203 
       
  2204 
       
  2205 
       
  2206 
       
  2207 
       
  2208 
       
  2209 
       
  2210 //--
       
  2211 CBTShutdowner* CBTShutdowner::NewL(CBluetoothSocket& aParent)
       
  2212 //
       
  2213 //NewL: the CBluetoothSocket parent is provided. 
       
  2214 //It contains all the ESocky stuff and has suitable "Getters".
       
  2215 //
       
  2216 	{
       
  2217 	CBTShutdowner* self = new (ELeave) CBTShutdowner(aParent);
       
  2218 	CleanupStack::PushL(self);
       
  2219 	self->ConstructL();
       
  2220 	CleanupStack::Pop();//self
       
  2221 	return self;
       
  2222 	}
       
  2223 
       
  2224 
       
  2225 CBTShutdowner::CBTShutdowner(CBluetoothSocket& aParent)
       
  2226 : CActive(CActive::EPriorityStandard),
       
  2227  iParent(aParent)
       
  2228 //
       
  2229 //Constructor
       
  2230 //
       
  2231 	{
       
  2232 	}
       
  2233 
       
  2234 void CBTShutdowner::ConstructL()
       
  2235 	{
       
  2236 	CActiveScheduler::Add(this);
       
  2237 	}
       
  2238 
       
  2239 CBTShutdowner::~CBTShutdowner()
       
  2240 //
       
  2241 //Destructor
       
  2242 //
       
  2243 	{
       
  2244 	Cancel();
       
  2245 	}
       
  2246 
       
  2247 void CBTShutdowner::Shutdown(RSocket::TShutdown aHow)
       
  2248 //
       
  2249 //Attempts to connect to the remote device. 
       
  2250 //
       
  2251 	{
       
  2252 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2253 	
       
  2254 	iParent.Socket().Shutdown(aHow, iStatus);
       
  2255 	SetActive();
       
  2256 	}
       
  2257 
       
  2258 void CBTShutdowner::Shutdown(RSocket::TShutdown aHow,const TDesC8& aDisconnectDataOut,TDes8& aDisconnectDataIn)
       
  2259 	{
       
  2260 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2261 	iParent.BTBasebandManager().HandleConnectionDown();
       
  2262 	iParent.Socket().Shutdown(aHow, aDisconnectDataOut, aDisconnectDataIn, iStatus);
       
  2263 
       
  2264 	SetActive();
       
  2265 	}
       
  2266 
       
  2267 void CBTShutdowner::RunL()
       
  2268 //
       
  2269 //When logical socket has connected (only async bit), 
       
  2270 //opens baseband socket.
       
  2271 //
       
  2272 	{
       
  2273 	//must come LAST .. deletes this active object
       
  2274 	iParent.HandleShutdownCompleteL(iStatus.Int());
       
  2275 	}
       
  2276 
       
  2277 TInt CBTShutdowner::RunError(TInt aError)
       
  2278 	{
       
  2279 	#ifdef __FLOGGING__
       
  2280 	FTRACE(FPrint(_L("CBluetoothSocket::HandleShutdownCompleteL left with error %d"), aError));
       
  2281 	#else
       
  2282 	(void) aError;
       
  2283 	#endif
       
  2284 	return KErrNone;
       
  2285 	}
       
  2286 
       
  2287 void CBTShutdowner::DoCancel()
       
  2288 //
       
  2289 //Cancels current asynchronous requests.
       
  2290 //
       
  2291 	{
       
  2292 	//iParent.Socket().CancelAll(); 
       
  2293 	//what do we do here if anything: no CancelShutdown exists in RSocket?
       
  2294 	}
       
  2295 
       
  2296 
       
  2297 
       
  2298 
       
  2299 
       
  2300 
       
  2301 
       
  2302 //--
       
  2303 CBTSender* CBTSender::NewL(CBluetoothSocket& aParent)
       
  2304 //
       
  2305 //NewL: the CBluetoothSocket parent is provided. 
       
  2306 //It contains all the ESocky stuff and has suitable "Getters".
       
  2307 //
       
  2308 	{
       
  2309 	CBTSender* self = new (ELeave) CBTSender(aParent);
       
  2310 	CleanupStack::PushL(self);
       
  2311 	self->ConstructL();
       
  2312 	CleanupStack::Pop();//self
       
  2313 	return self;
       
  2314 	}
       
  2315 
       
  2316 
       
  2317 CBTSender::CBTSender(CBluetoothSocket& aParent)
       
  2318 : CActive(CActive::EPriorityStandard),
       
  2319  iParent(aParent)
       
  2320 //
       
  2321 //Constructor
       
  2322 //
       
  2323 	{
       
  2324 	}
       
  2325 
       
  2326 void CBTSender::ConstructL()
       
  2327 	{
       
  2328 	CActiveScheduler::Add(this);
       
  2329 	}
       
  2330 
       
  2331 CBTSender::~CBTSender()
       
  2332 //
       
  2333 //Destructor
       
  2334 //
       
  2335 	{
       
  2336 	Cancel();
       
  2337 	}
       
  2338 
       
  2339 void CBTSender::Write(const TDesC8& aDesc)
       
  2340 	{
       
  2341 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2342 
       
  2343 	iParent.Socket().Write(aDesc, iStatus);
       
  2344 	iCurrentRequest = EWrite;
       
  2345 	SetActive();
       
  2346 	}
       
  2347 
       
  2348 void CBTSender::Send(const TDesC8& aDesc, TUint someFlags)
       
  2349 	{
       
  2350 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2351 
       
  2352 	iParent.Socket().Send(aDesc, someFlags, iStatus);
       
  2353 	iCurrentRequest = ESend;
       
  2354 	SetActive();
       
  2355 	}
       
  2356 
       
  2357 void CBTSender::Send(const TDesC8& aDesc, TUint someFlags, TSockXfrLength& aLen)
       
  2358 	{
       
  2359 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2360 
       
  2361 	iParent.Socket().Send(aDesc, someFlags, iStatus, aLen);
       
  2362 	iCurrentRequest = ESend;
       
  2363 	SetActive();
       
  2364 	}
       
  2365 
       
  2366 void CBTSender::SendTo(const TDesC8& aDesc, TSockAddr& aAddr, TUint flags)
       
  2367 	{
       
  2368 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2369 
       
  2370 	iParent.Socket().SendTo(aDesc, aAddr, flags, iStatus);
       
  2371 	iCurrentRequest = ESend;
       
  2372 	SetActive();
       
  2373 	}
       
  2374 
       
  2375 void CBTSender::SendTo(const TDesC8& aDesc, TSockAddr& aAddr, TUint flags, TSockXfrLength& aLen)
       
  2376 	{
       
  2377 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2378 
       
  2379 	iParent.Socket().SendTo(aDesc, aAddr, flags, iStatus, aLen);
       
  2380 	iCurrentRequest = ESend;
       
  2381 	SetActive();
       
  2382 	}
       
  2383 
       
  2384 void CBTSender::RunL()
       
  2385 //
       
  2386 //When logical socket has connected (only async bit), 
       
  2387 //opens baseband socket.
       
  2388 //
       
  2389 	{
       
  2390 	//must come LAST .. deletes this active object
       
  2391 	iParent.HandleSendCompleteL(iStatus.Int());
       
  2392 	}
       
  2393 
       
  2394 TInt CBTSender::RunError(TInt aError)
       
  2395 	{
       
  2396 	#ifdef __FLOGGING__
       
  2397 	FTRACE(FPrint(_L("CBluetoothSocket::HandleSendCompleteL left with error %d"), aError));
       
  2398 	#else
       
  2399 	(void) aError;
       
  2400 	#endif
       
  2401 	return KErrNone;
       
  2402 	}
       
  2403 
       
  2404 void CBTSender::DoCancel()
       
  2405 //
       
  2406 //Cancels current asynchronous requests.
       
  2407 //
       
  2408 	{
       
  2409 	switch (iCurrentRequest)
       
  2410 		{
       
  2411 		//zam todo do these cover all Write/Send functions?
       
  2412 		case EWrite:
       
  2413 			iCurrentRequest = ENone;
       
  2414 			iParent.Socket().CancelWrite(); 
       
  2415 			break;
       
  2416 		case ESend:
       
  2417 			iCurrentRequest = ENone;
       
  2418 			iParent.Socket().CancelSend(); 
       
  2419 			break;
       
  2420 		default:
       
  2421 			iCurrentRequest = ENone;
       
  2422 			break;
       
  2423 		};
       
  2424 	}
       
  2425 
       
  2426 
       
  2427 
       
  2428 
       
  2429 
       
  2430 
       
  2431 
       
  2432 //--
       
  2433 CBTReceiver* CBTReceiver::NewL(CBluetoothSocket& aParent)
       
  2434 //
       
  2435 //NewL: the CBluetoothSocket parent is provided. 
       
  2436 //It contains all the ESocky stuff and has suitable "Getters".
       
  2437 //
       
  2438 	{
       
  2439 	CBTReceiver* self = new (ELeave) CBTReceiver(aParent);
       
  2440 	CleanupStack::PushL(self);
       
  2441 	self->ConstructL();
       
  2442 	CleanupStack::Pop();//self
       
  2443 	return self;
       
  2444 	}
       
  2445 
       
  2446 
       
  2447 CBTReceiver::CBTReceiver(CBluetoothSocket& aParent)
       
  2448 : CActive(CActive::EPriorityStandard),
       
  2449  iParent(aParent)
       
  2450 //
       
  2451 //Constructor
       
  2452 //
       
  2453 	{
       
  2454 	}
       
  2455 
       
  2456 void CBTReceiver::ConstructL()
       
  2457 	{
       
  2458 	CActiveScheduler::Add(this);
       
  2459 	}
       
  2460 
       
  2461 CBTReceiver::~CBTReceiver()
       
  2462 //
       
  2463 //Destructor
       
  2464 //
       
  2465 	{
       
  2466 	Cancel();
       
  2467 	}
       
  2468 
       
  2469 void CBTReceiver::Read(TDes8& aDesc)
       
  2470 	{
       
  2471 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2472 
       
  2473 	iParent.Socket().Read(aDesc, iStatus);
       
  2474 	iCurrentRequest = ERead;
       
  2475 	SetActive();
       
  2476 	}
       
  2477 
       
  2478 void CBTReceiver::Recv(TDes8& aDesc,TUint flags)
       
  2479 	{
       
  2480 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2481 
       
  2482 	iParent.Socket().Recv(aDesc, flags, iStatus);
       
  2483 	iCurrentRequest = ERecv;
       
  2484 	SetActive();
       
  2485 	}
       
  2486 
       
  2487 void CBTReceiver::Recv(TDes8& aDesc,TUint flags,TSockXfrLength& aLen)
       
  2488 	{
       
  2489 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2490 
       
  2491 	iParent.Socket().Recv(aDesc, flags, iStatus, aLen);
       
  2492 	iCurrentRequest = ERecv;
       
  2493 	SetActive();
       
  2494 	}
       
  2495 
       
  2496 void CBTReceiver::RecvOneOrMore(TDes8& aDesc,TUint flags,TSockXfrLength& aLen)
       
  2497 	{
       
  2498 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2499 
       
  2500 	iParent.Socket().RecvOneOrMore(aDesc,flags, iStatus, aLen);
       
  2501 	iCurrentRequest = ERecv;
       
  2502 	SetActive();
       
  2503 	}
       
  2504 
       
  2505 void CBTReceiver::RecvFrom(TDes8& aDesc,TSockAddr& aAddr,TUint flags)
       
  2506 	{
       
  2507 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2508 
       
  2509 	iParent.Socket().RecvFrom(aDesc, aAddr, flags, iStatus);
       
  2510 	iCurrentRequest = ERecv;
       
  2511 	SetActive();
       
  2512 	}
       
  2513 
       
  2514 void CBTReceiver::RecvFrom(TDes8& aDesc,TSockAddr& aAddr,TUint flags,TSockXfrLength& aLen)
       
  2515 	{
       
  2516 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2517 
       
  2518 	iParent.Socket().RecvFrom(aDesc, aAddr, flags, iStatus, aLen);
       
  2519 	iCurrentRequest = ERecv;
       
  2520 	SetActive();
       
  2521 	}
       
  2522 
       
  2523 
       
  2524 
       
  2525 void CBTReceiver::RunL()
       
  2526 //
       
  2527 //When logical socket has connected (only async bit), 
       
  2528 //opens baseband socket.
       
  2529 //
       
  2530 	{
       
  2531 	//must come LAST .. deletes this active object
       
  2532 	iParent.HandleReceiveCompleteL(iStatus.Int());
       
  2533 	}
       
  2534 
       
  2535 TInt CBTReceiver::RunError(TInt aError)
       
  2536 	{
       
  2537 	#ifdef __FLOGGING__
       
  2538 	FTRACE(FPrint(_L("CBluetoothSocket::HandleReceiveCompleteL left with error %d"), aError));
       
  2539 	#else
       
  2540 	(void) aError;
       
  2541 	#endif
       
  2542 	return KErrNone;
       
  2543 	}
       
  2544 
       
  2545 void CBTReceiver::DoCancel()
       
  2546 //
       
  2547 //Cancels current asynchronous requests.
       
  2548 //
       
  2549 	{
       
  2550 	switch (iCurrentRequest)
       
  2551 		{
       
  2552 		//zam todo do these cover all Read/Recv functions?
       
  2553 		case ERead:
       
  2554 			iCurrentRequest = ENone;
       
  2555 			iParent.Socket().CancelRead(); 
       
  2556 			break;
       
  2557 		case ERecv:
       
  2558 			iCurrentRequest = ENone;
       
  2559 			iParent.Socket().CancelRecv(); 
       
  2560 			break;
       
  2561 		default:
       
  2562 			iCurrentRequest = ENone;
       
  2563 			break;
       
  2564 		};
       
  2565 
       
  2566 	iParent.Socket().CancelRead(); 
       
  2567 	}
       
  2568 
       
  2569 
       
  2570 
       
  2571 
       
  2572 
       
  2573 
       
  2574 
       
  2575 //--
       
  2576 CBTIoctler* CBTIoctler::NewL(CBluetoothSocket& aParent)
       
  2577 //
       
  2578 //NewL: the CBluetoothSocket parent is provided. 
       
  2579 //It contains all the ESocky stuff and has suitable "Getters".
       
  2580 //
       
  2581 	{
       
  2582 	CBTIoctler* self = new (ELeave) CBTIoctler(aParent);
       
  2583 	CleanupStack::PushL(self);
       
  2584 	self->ConstructL();
       
  2585 	CleanupStack::Pop();//self
       
  2586 	return self;
       
  2587 	}
       
  2588 
       
  2589 
       
  2590 CBTIoctler::CBTIoctler(CBluetoothSocket& aParent)
       
  2591 : CActive(CActive::EPriorityStandard),
       
  2592  iParent(aParent)
       
  2593 //
       
  2594 //Constructor
       
  2595 //
       
  2596 	{
       
  2597 	}
       
  2598 
       
  2599 void CBTIoctler::ConstructL()
       
  2600 	{
       
  2601 	CActiveScheduler::Add(this);
       
  2602 	}
       
  2603 
       
  2604 CBTIoctler::~CBTIoctler()
       
  2605 //
       
  2606 //Destructor
       
  2607 //
       
  2608 	{
       
  2609 	Cancel();
       
  2610 	}
       
  2611 
       
  2612 void CBTIoctler::Ioctl(TUint aCommand, TDes8* aDesc, TUint aLevel)
       
  2613 	{
       
  2614 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2615 
       
  2616 	iParent.Socket().Ioctl(aCommand, iStatus, aDesc, aLevel);
       
  2617 	SetActive();
       
  2618 	}
       
  2619 
       
  2620 void CBTIoctler::RunL()
       
  2621 //
       
  2622 //When logical socket has connected (only async bit), 
       
  2623 //opens baseband socket.
       
  2624 //
       
  2625 	{
       
  2626 	//must come LAST .. deletes this active object
       
  2627 	iParent.HandleIoctlCompleteL(iStatus.Int());
       
  2628 	}
       
  2629 
       
  2630 
       
  2631 TInt CBTIoctler::RunError(TInt aError)
       
  2632 	{
       
  2633 	#ifdef __FLOGGING__
       
  2634 	FTRACE(FPrint(_L("CBluetoothSocket::HandleIoctlCompleteL left with error %d"), aError));
       
  2635 	#else
       
  2636 	(void) aError;
       
  2637 	#endif
       
  2638 	return KErrNone;
       
  2639 	}
       
  2640 
       
  2641 void CBTIoctler::DoCancel()
       
  2642 //
       
  2643 //Cancels current asynchronous requests.
       
  2644 //
       
  2645 	{
       
  2646 	iParent.Socket().CancelIoctl();
       
  2647 	}
       
  2648 
       
  2649 //--
       
  2650 CBTBasebandChangeEventNotifier* CBTBasebandChangeEventNotifier::NewL(MBTBasebandHandler& aUser)
       
  2651 //
       
  2652 //NewL: the RBluetoothPhysicalLinksManager parent is provided. 
       
  2653 //It contains all the ESocky stuff and has suitable "Getters".
       
  2654 //
       
  2655 	{
       
  2656 	CBTBasebandChangeEventNotifier* self = new (ELeave) CBTBasebandChangeEventNotifier(aUser);
       
  2657 	CleanupStack::PushL(self);
       
  2658 	self->ConstructL();
       
  2659 	CleanupStack::Pop();//self
       
  2660 	return self;
       
  2661 	}
       
  2662 
       
  2663 
       
  2664 CBTBasebandChangeEventNotifier::CBTBasebandChangeEventNotifier(MBTBasebandHandler& aUser)
       
  2665 : CActive(CActive::EPriorityStandard),
       
  2666  iUser(aUser)
       
  2667 //
       
  2668 //Constructor
       
  2669 //
       
  2670 	{
       
  2671 	}
       
  2672 
       
  2673 void CBTBasebandChangeEventNotifier::ConstructL()
       
  2674 	{
       
  2675 	CActiveScheduler::Add(this);
       
  2676 	}
       
  2677 
       
  2678 CBTBasebandChangeEventNotifier::~CBTBasebandChangeEventNotifier()
       
  2679 //
       
  2680 //Destructor
       
  2681 //
       
  2682 	{
       
  2683 	Cancel();
       
  2684 	}
       
  2685 
       
  2686 void CBTBasebandChangeEventNotifier::SetNotifyNextModeChange()
       
  2687 	{
       
  2688 	iCurrentRequest = ENotifyAnyPowerMode;
       
  2689 	SetNotify(iCurrentRequest);
       
  2690 	}
       
  2691 
       
  2692 void CBTBasebandChangeEventNotifier::SetNotify(TUint32 aNotifications)
       
  2693 	{
       
  2694 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
  2695 	iEvent = TBTBasebandEventNotification();
       
  2696 	iUser.MbbhBTBaseband().ActivateNotifierForRecall(iEvent, iStatus, aNotifications);
       
  2697 	SetActive();
       
  2698 	}
       
  2699 
       
  2700 void CBTBasebandChangeEventNotifier::RunL()
       
  2701 //
       
  2702 //When logical socket has connected (only async bit), 
       
  2703 //opens baseband socket.
       
  2704 //
       
  2705 	{
       
  2706 	iCurrentRequest = 0;
       
  2707 	TBTBasebandEventNotification notification = iEvent();
       
  2708 	iUser.MbbhHandleActivateBasebandEventNotifierCompleteL(iStatus.Int(), notification);
       
  2709 	}
       
  2710 
       
  2711 TInt CBTBasebandChangeEventNotifier::RunError(TInt aError)
       
  2712 	{
       
  2713 	#ifdef __FLOGGING__
       
  2714 	FTRACE(FPrint(_L("CBluetoothSocket::HandleActivateBasebandEventNotifierCompleteL left with error %d"), aError));
       
  2715 	#else
       
  2716 	(void) aError;
       
  2717 	#endif
       
  2718 	return KErrNone;
       
  2719 	}
       
  2720 
       
  2721 void CBTBasebandChangeEventNotifier::DoCancel()
       
  2722 //
       
  2723 //Cancels current asynchronous requests.
       
  2724 //
       
  2725 	{
       
  2726 	iUser.MbbhBTBaseband().CancelNextBasebandChangeEventNotifier();
       
  2727 	iCurrentRequest = 0;
       
  2728 	}
       
  2729 
       
  2730 
       
  2731 // CAutoSniffDelayTimer
       
  2732 
       
  2733 CAutoSniffDelayTimer::CAutoSniffDelayTimer(CBluetoothSocket& aParent, TInt aIdleSecondsBeforeSniffRequest)
       
  2734 	:CTimer(CActive::EPriorityStandard),
       
  2735 	iParent(aParent),
       
  2736 	iSniffModeRequested(EFalse)
       
  2737 	{
       
  2738 	const TInt KMicroSecondsInASecond = 1000000;
       
  2739 	iAutoSniffDelay = aIdleSecondsBeforeSniffRequest * KMicroSecondsInASecond;
       
  2740 	}
       
  2741 
       
  2742 CAutoSniffDelayTimer::~CAutoSniffDelayTimer()
       
  2743 	{
       
  2744 	Cancel();
       
  2745 	delete iBTAutoSniffBasebandChangeEventNotifier;
       
  2746 	// Disable explicit active mode and cancel any low power request if we
       
  2747 	// have done an auto sniff, there is nothing we do if we error in this process.
       
  2748 	(void)MbbhBTBaseband().RequestExplicitActiveMode(EFalse);
       
  2749 	if(iSniffModeRequested)
       
  2750 		{
       
  2751 		(void)MbbhBTBaseband().CancelLowPowerModeRequester();
       
  2752 		}
       
  2753 	(void)iParent.BTBasebandManager().UnRegisterBTBaseband(*this);
       
  2754 	}
       
  2755 
       
  2756 void CAutoSniffDelayTimer::ConstructL()
       
  2757 	{
       
  2758 	CTimer::ConstructL();
       
  2759 	User::LeaveIfError(iParent.BTBasebandManager().RegisterBTBaseband(*this));
       
  2760 	iBTAutoSniffBasebandChangeEventNotifier = CBTBasebandChangeEventNotifier::NewL(*this);
       
  2761 	// Issue a sniff request as soon as we enable auto sniffing.
       
  2762 	User::LeaveIfError(MakeSniffRequest());
       
  2763 	iBTAutoSniffBasebandChangeEventNotifier->SetNotify(ENotifyActiveMode);
       
  2764 	CActiveScheduler::Add(this);
       
  2765 	}
       
  2766 	
       
  2767 CAutoSniffDelayTimer* CAutoSniffDelayTimer::NewL(CBluetoothSocket& aParent, TInt aIdleSecondsBeforeSniffRequest)
       
  2768 	{
       
  2769 	CAutoSniffDelayTimer* self = new (ELeave) CAutoSniffDelayTimer(aParent, aIdleSecondsBeforeSniffRequest);
       
  2770 	CleanupStack::PushL(self);
       
  2771 	self->ConstructL();
       
  2772 	CleanupStack::Pop();
       
  2773 	return self;
       
  2774 	}
       
  2775 
       
  2776 void CAutoSniffDelayTimer::Start()
       
  2777 	{
       
  2778 	Cancel();
       
  2779 	if(iSniffModeRequested)
       
  2780 		{
       
  2781 		// Try to cancel the low power modes, if we fail to do that
       
  2782 		// then we just continue anyway as there isn't much else
       
  2783 		// we can do.
       
  2784 		(void)MbbhBTBaseband().CancelLowPowerModeRequester();
       
  2785 		}
       
  2786 
       
  2787 	iSniffModeRequested = EFalse;
       
  2788 	After(iAutoSniffDelay);
       
  2789 	}
       
  2790 
       
  2791 //Start a 5-minute window for active mode
       
  2792 void CAutoSniffDelayTimer::StartActive()
       
  2793 	{
       
  2794 	Start();
       
  2795 	if(iSniffModeRequested)
       
  2796 		{
       
  2797 		// When enabling auto sniffing, we make use of Explicit active mode.
       
  2798 		(void)MbbhBTBaseband().RequestExplicitActiveMode(ETrue);
       
  2799 		}
       
  2800 	}
       
  2801 
       
  2802 RBTBaseband& CAutoSniffDelayTimer::MbbhBTBaseband() 
       
  2803 	{
       
  2804 	return iBTAutoSniffBaseband;
       
  2805 	}
       
  2806 
       
  2807 void CAutoSniffDelayTimer::MbbhHandleActivateBasebandEventNotifierCompleteL(TInt /*aErr*/, TBTBasebandEventNotification& aEventNotification)
       
  2808 	{
       
  2809 	if(aEventNotification.EventType() == ENotifyActiveMode)
       
  2810 		{
       
  2811 		// Link has gone into active mode - if we are requesting
       
  2812 		// sniff mode, then it is up to us to cancel the sniff
       
  2813 		// requester to prevent the ping-pong effect.
       
  2814 		if(iSniffModeRequested)
       
  2815 			{
       
  2816 			MbbhBTBaseband().CancelLowPowerModeRequester();
       
  2817 			StartActive();
       
  2818 			}
       
  2819 		}
       
  2820 	__ASSERT_ALWAYS(iBTAutoSniffBasebandChangeEventNotifier != NULL, Panic(EBBInvalidAddress));
       
  2821 	iBTAutoSniffBasebandChangeEventNotifier->SetNotify(ENotifyActiveMode);
       
  2822 	}
       
  2823 	
       
  2824 TInt CAutoSniffDelayTimer::MbbhOpen(RSocketServ& aSocketServ, RSocket& aSocket)
       
  2825 	{
       
  2826 	TInt err;
       
  2827 	err = iBTAutoSniffBaseband.Open(aSocketServ, aSocket);
       
  2828 	if(err == KErrNone)
       
  2829 		{
       
  2830 		//On sucessful opening, preventing Park Mode
       
  2831 		err = iParent.PreventLowPowerModes(EParkMode);
       
  2832 		}
       
  2833 	return err;
       
  2834 	}
       
  2835 	
       
  2836 void CAutoSniffDelayTimer::MbbhClose()
       
  2837 	{
       
  2838 	iParent.BTBaseband().AllowLowPowerModes(EParkMode);
       
  2839 	iBTAutoSniffBaseband.Close();
       
  2840 	}
       
  2841 
       
  2842 void CAutoSniffDelayTimer::RunL()
       
  2843 /**
       
  2844 Request Sniff Mode when the timer expires
       
  2845 **/
       
  2846 	{
       
  2847 	(void)MbbhBTBaseband().RequestExplicitActiveMode(EFalse);
       
  2848 	(void)MakeSniffRequest();
       
  2849 	}
       
  2850 
       
  2851 TInt CAutoSniffDelayTimer::MakeSniffRequest()
       
  2852 	{
       
  2853 	TInt err = MbbhBTBaseband().ActivateSniffRequester();
       
  2854 	if(err == KErrNone)
       
  2855 		{
       
  2856 		iSniffModeRequested = ETrue;
       
  2857 		}
       
  2858 	return err;
       
  2859 	}
       
  2860 
       
  2861 
       
  2862 
       
  2863 
       
  2864 // Class CBTBasebandManager 
       
  2865 
       
  2866 CBTBasebandManager* CBTBasebandManager::NewL()
       
  2867 	{
       
  2868 	CBTBasebandManager* self = new (ELeave) CBTBasebandManager();
       
  2869 	CleanupStack::PushL(self);
       
  2870 	self->ConstructL();
       
  2871 	CleanupStack::Pop();
       
  2872 	return self;
       
  2873 	}
       
  2874 	
       
  2875 CBTBasebandManager::~CBTBasebandManager()
       
  2876 	{
       
  2877 	CloseAllBasebands();
       
  2878 	iBasebandHandlers.Close();
       
  2879 	}
       
  2880 	
       
  2881 CBTBasebandManager::CBTBasebandManager()
       
  2882 //	: iConnected(EFalse) // By default we are not connected.
       
  2883 	{
       
  2884 	}
       
  2885 	
       
  2886 void CBTBasebandManager::ConstructL()
       
  2887 	{
       
  2888 	}
       
  2889 
       
  2890 //Register a RBTBaseband as specified by aBasebandRegisterMask.
       
  2891 //After registering, Open the RBTBaseband if the connection is up.
       
  2892 TInt CBTBasebandManager::RegisterBTBaseband(MBTBasebandHandler& aBasebandHandler)
       
  2893 	{
       
  2894 	TInt err = KErrNone;
       
  2895 	//Trying to register too many basebands
       
  2896 	if(KErrNotFound != iBasebandHandlers.Find(&aBasebandHandler))
       
  2897 		{
       
  2898 		// Trying to register an existing handler again
       
  2899 		err = KErrArgument;
       
  2900 		}
       
  2901 	else
       
  2902 		{
       
  2903 		// We know we have a baseband "slot" available from the check above
       
  2904 		// so just find somewhere to put it.
       
  2905 		err = iBasebandHandlers.Append(&aBasebandHandler);
       
  2906 		 
       
  2907 		if(err != KErrNone)
       
  2908 			{
       
  2909 			//Most likely KErrNoMemory
       
  2910 			return err;
       
  2911 			}
       
  2912 
       
  2913 		// Might already be open, so to be sure we close it now.
       
  2914 	 	aBasebandHandler.MbbhClose();
       
  2915 		// Now bring the baseband up to the current state
       
  2916 		if(iConnected)
       
  2917 			{
       
  2918 		 	err = aBasebandHandler.MbbhOpen(*iSocketServ, *iSocket);
       
  2919 			}
       
  2920 		if(err != KErrNone)
       
  2921 			{
       
  2922 			// On error - roll back the managers state
       
  2923 			TInt handlerIndex = iBasebandHandlers.Find(&aBasebandHandler);
       
  2924 			__ASSERT_DEBUG((handlerIndex != KErrNotFound),Panic(EBadArgument));
       
  2925 			iBasebandHandlers.Remove(handlerIndex);
       
  2926 			}
       
  2927 		}
       
  2928 	return err;
       
  2929 	}
       
  2930 	
       
  2931 //Un-register a RBTBaseband
       
  2932 TInt CBTBasebandManager::UnRegisterBTBaseband(MBTBasebandHandler& aBasebandHandler)
       
  2933 	{
       
  2934 	// Check that the baseband is actually registered in the manager
       
  2935 	// We check to see if it is the same instance of MBTBasebandHandler through
       
  2936 	// their pointer addresses.
       
  2937 	TInt handlerIndex = iBasebandHandlers.Find(&aBasebandHandler);
       
  2938 	if (handlerIndex == KErrNotFound)
       
  2939 		{
       
  2940 		return KErrNotFound;		// send error condition back to API
       
  2941 		}
       
  2942 	iBasebandHandlers.Remove(handlerIndex);
       
  2943 		
       
  2944 	// Close the baseband if we successfully unregistered it.
       
  2945 	aBasebandHandler.MbbhClose();
       
  2946 
       
  2947 	return KErrNone;
       
  2948 	}
       
  2949 	
       
  2950 //Open all registered basebands when connection is up
       
  2951 TInt CBTBasebandManager::HandleConnectionUp(RSocketServ& aSocketServ, RSocket& aSocket)
       
  2952 	{
       
  2953 	TInt err = KErrNone;
       
  2954 	TInt handlerCount = iBasebandHandlers.Count();
       
  2955 	
       
  2956 	if(iConnected)
       
  2957 		{
       
  2958 		err = KErrInUse;
       
  2959 		}
       
  2960 	else
       
  2961 		{
       
  2962 		// Cache the information for basebands that register after the connection has come up
       
  2963 		iSocketServ = &aSocketServ;
       
  2964 		iSocket = &aSocket;
       
  2965 		
       
  2966 		for(TInt i = 0; i < handlerCount; i++)
       
  2967 			{
       
  2968 			err = iBasebandHandlers[i]->MbbhOpen(aSocketServ, aSocket);
       
  2969 			if(err != KErrNone)
       
  2970 				{
       
  2971 				// There was an error opening one of the sockets, we aren't connected
       
  2972 				// so make sure we roll all RBTBasebands back to being closed.
       
  2973 				CloseAllBasebands();
       
  2974 				break;			
       
  2975 				}
       
  2976 			}
       
  2977 			
       
  2978 		if(err == KErrNone)
       
  2979 			{
       
  2980 			iConnected = ETrue;
       
  2981 			}
       
  2982 		}
       
  2983 
       
  2984 	return err;
       
  2985 	}
       
  2986 	
       
  2987 //Close all opened basebands when connection is down
       
  2988 void CBTBasebandManager::HandleConnectionDown()
       
  2989 	{
       
  2990 	CloseAllBasebands();
       
  2991 	iConnected = EFalse;
       
  2992 	}
       
  2993 
       
  2994 TBool CBTBasebandManager::Connected() const
       
  2995 	{
       
  2996 	return iConnected;
       
  2997 	}
       
  2998 
       
  2999 void CBTBasebandManager::CloseAllBasebands()
       
  3000 	{
       
  3001 	TInt handlerCount = iBasebandHandlers.Count();
       
  3002 	for(TInt i = 0; i < handlerCount; i++)
       
  3003 		{
       
  3004 		iBasebandHandlers[i]->MbbhClose();
       
  3005 		}
       
  3006 	}
       
  3007 
       
  3008 
       
  3009 //class CBTBasebandChangeEventDelegate
       
  3010 CBTBasebandChangeEventDelegate* CBTBasebandChangeEventDelegate::NewL(CBluetoothSocket& aParent)
       
  3011 	{
       
  3012 	CBTBasebandChangeEventDelegate* self = new (ELeave) CBTBasebandChangeEventDelegate(aParent);
       
  3013 	CleanupStack::PushL(self);
       
  3014 	self->ConstructL();
       
  3015 	CleanupStack::Pop();
       
  3016 	return self;
       
  3017 	}
       
  3018 	
       
  3019 CBTBasebandChangeEventDelegate::CBTBasebandChangeEventDelegate(CBluetoothSocket& aParent)
       
  3020 :iParent(aParent)
       
  3021 	{	
       
  3022 	}
       
  3023 
       
  3024 void CBTBasebandChangeEventDelegate::ConstructL()
       
  3025 	{
       
  3026 	}
       
  3027 	
       
  3028 RBTBaseband& CBTBasebandChangeEventDelegate::MbbhBTBaseband()
       
  3029 	{
       
  3030 	return iParent.BTBaseband();
       
  3031 	}
       
  3032 	
       
  3033 void CBTBasebandChangeEventDelegate::MbbhHandleActivateBasebandEventNotifierCompleteL(TInt aErr, TBTBasebandEventNotification& aEventNotification)
       
  3034 	{
       
  3035 	iParent.HandleActivateBasebandEventNotifierCompleteL(aErr, aEventNotification);
       
  3036 	}
       
  3037 
       
  3038 TInt CBTBasebandChangeEventDelegate::MbbhOpen(RSocketServ& aSocketServ, RSocket& aSocket)
       
  3039 	{
       
  3040 	TInt err = iParent.BTBaseband().Open(aSocketServ, aSocket);
       
  3041 	return err;
       
  3042 	}
       
  3043 	
       
  3044 void CBTBasebandChangeEventDelegate::MbbhClose()
       
  3045 	{
       
  3046 	iParent.BTBaseband().Close();
       
  3047 	}