bluetoothmgmt/bluetoothclientlib/btlib/btphysicallinks.cpp
changeset 0 29b1cd4cb562
child 18 f8503e232b0c
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 "btphysicallinkshelpers.h"
       
    21 
       
    22 //----
       
    23 EXPORT_C CBluetoothPhysicalLinks* CBluetoothPhysicalLinks::NewL(MBluetoothPhysicalLinksNotifier& aNotifier,
       
    24 														        RSocketServ& aServer)
       
    25 /** Constructor
       
    26 
       
    27 Opens a Bluetooth socket.
       
    28 @pre A session on the socket server has been started.
       
    29 @leave No memory, or error on opening socket
       
    30 @param aNotifier The object which will receive asynchronous events.
       
    31 @param aServer The socket server.
       
    32 @return An object of type CBluetoothPhysicalLinks
       
    33 */
       
    34 	{
       
    35 	CBluetoothPhysicalLinks* self = NewLC(aNotifier, aServer);
       
    36 	CleanupStack::Pop();
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 EXPORT_C CBluetoothPhysicalLinks* CBluetoothPhysicalLinks::NewLC(MBluetoothPhysicalLinksNotifier& aNotifier,
       
    41 														         RSocketServ& aServer)
       
    42 /** Constructor
       
    43 
       
    44 Opens a Bluetooth socket.
       
    45 @pre A session on the socket server has been started.
       
    46 @leave No memory, or error on opening socket
       
    47 @param aNotifier The object which will receive asynchronous events.
       
    48 @param aServer The socket server.
       
    49 @return An object of type CBluetoothPhysicalLinks
       
    50 */
       
    51 	{
       
    52 	CBluetoothPhysicalLinks* self=new (ELeave) CBluetoothPhysicalLinks(aNotifier, aServer);
       
    53 	CleanupStack::PushL(self);
       
    54 	self->ConstructL();
       
    55 	return self;
       
    56 	}
       
    57 
       
    58 EXPORT_C CBluetoothPhysicalLinks::~CBluetoothPhysicalLinks()
       
    59 /**
       
    60 Destructor
       
    61 */
       
    62 	{
       
    63 	delete iBTBasebandConnecter;
       
    64 	delete iBTDisconnector;
       
    65 	BTBaseband().Close();
       
    66 	}
       
    67 
       
    68 CBluetoothPhysicalLinks::CBluetoothPhysicalLinks(MBluetoothPhysicalLinksNotifier& aNotifier,
       
    69 												 RSocketServ& aServer)
       
    70 : iNotifier(aNotifier), iSockServer(aServer)
       
    71 	{
       
    72 	}
       
    73 
       
    74 void CBluetoothPhysicalLinks::ConstructL()
       
    75 	{
       
    76 	User::LeaveIfError(BTBaseband().Open(SockServer()));
       
    77 	}
       
    78 
       
    79 EXPORT_C TInt CBluetoothPhysicalLinks::CreateConnection(const TBTDevAddr& aBDAddr)
       
    80 /**
       
    81 Bring in a new member to the piconet
       
    82 
       
    83 This can be useful for preparing for a logical link (service socket). 
       
    84 Firstly if the local device has not bonded with the specified remote device, 
       
    85 then this function can be used to allow the bonding procedure can take place in advance. 
       
    86 Secondly if a CBluetoothSocket call is made to create a logical link within an internally 
       
    87 specified time of this CreateConnection function being called, then that link will come 
       
    88 up much faster since the physical link will already be in place.
       
    89 Unless this connection is taken up by a logical link (or a synchronous link) within that
       
    90 internally specified time then it will be closed.
       
    91 
       
    92 This function can also be used to create a physical link ready for the setting up
       
    93 of a synchronous link (e.g. voice link).
       
    94 
       
    95 @param aBDAddr Bluetooth address specifying remote device (new member of piconet)
       
    96 @return Error code
       
    97 */
       
    98 	{
       
    99 	if(iBTBasebandConnecter)
       
   100 		{
       
   101 		return KErrInUse;
       
   102 		}
       
   103 	
       
   104 	//Close and re-open socket subsession
       
   105 	//Allow ESock to do multiple connects	
       
   106 	BTBaseband().Close(); 
       
   107 	TInt err = BTBaseband().Open(SockServer());
       
   108 	if(err != KErrNone)
       
   109 		{
       
   110 		return err;
       
   111 		}
       
   112 
       
   113 	//Create Connecting AO
       
   114 	TRAP(err, iBTBasebandConnecter = CBTBasebandConnecter::NewL(*this));
       
   115 	if(err != KErrNone)
       
   116 		{
       
   117 		//BTBaseband().Close();//no leave it open! 
       
   118 		return err;
       
   119 		}
       
   120 
       
   121 	//Try to make BT connection
       
   122 	iBTBasebandConnecter->BasebandConnect(aBDAddr);
       
   123 	return err;
       
   124 	}
       
   125 
       
   126 EXPORT_C void CBluetoothPhysicalLinks::CancelCreateConnection()
       
   127 /** Cancel bringing in a new member to the piconet
       
   128 */
       
   129 	{
       
   130 	if(iBTBasebandConnecter)
       
   131 		{
       
   132 		delete iBTBasebandConnecter;
       
   133 		iBTBasebandConnecter = 0;
       
   134 		}
       
   135 	}
       
   136 
       
   137 EXPORT_C TInt CBluetoothPhysicalLinks::Disconnect(const TBTDevAddr& aBDAddr)
       
   138 /** Disconnect a single member of piconet
       
   139 
       
   140 @param aBDAddr Bluetooth address specifying remote device (member of piconet)
       
   141 @return Error code
       
   142 @capability NetworkControl
       
   143 */
       
   144 	{
       
   145 	if(iBTDisconnector)
       
   146 		{
       
   147 		return KErrInUse;
       
   148 		}
       
   149 
       
   150 	//Allow ESock to do multiple disconnects	
       
   151 	BTBaseband().Close(); 
       
   152 	TInt ESockErr = BTBaseband().Open(SockServer());
       
   153 	if(ESockErr != KErrNone)
       
   154 		{
       
   155 		return ESockErr;
       
   156 		}
       
   157 	
       
   158 	TRAPD(err, iBTDisconnector = CBTDisconnector::NewL(*this));
       
   159 	if(err == KErrNone)
       
   160 		{
       
   161 		iBTDisconnector->Disconnect(aBDAddr);
       
   162 		}
       
   163 	return err;
       
   164 	}
       
   165 
       
   166 EXPORT_C TInt CBluetoothPhysicalLinks::DisconnectAll()
       
   167 /** Disconnect all members of piconet
       
   168 @return Error code
       
   169 @capability NetworkControl
       
   170 */
       
   171 	{
       
   172 	if(iBTDisconnector)
       
   173 		{
       
   174 		return KErrInUse;
       
   175 		}
       
   176 
       
   177 	//Allow ESock to do multiple disconnects	
       
   178 	BTBaseband().Close(); 
       
   179 	TInt ESockErr = BTBaseband().Open(SockServer());
       
   180 	if(ESockErr != KErrNone)
       
   181 		{
       
   182 		return ESockErr;
       
   183 		}
       
   184 		
       
   185 	TRAPD(err, iBTDisconnector = CBTDisconnector::NewL(*this));
       
   186 	if(err == KErrNone)
       
   187 		{
       
   188 		iBTDisconnector->DisconnectAll();
       
   189 		}
       
   190 	return err;
       
   191 	}
       
   192 
       
   193 EXPORT_C TInt CBluetoothPhysicalLinks::Broadcast(const TDesC8& aData)
       
   194 /** Write (raw) broadcast data
       
   195 
       
   196 Raw broadcast data is data that is not directed to any specified service.
       
   197 @param aData Contains data to be broadcast.
       
   198 @test
       
   199 @return Error code
       
   200 @capability NetworkControl
       
   201 */
       
   202 	{
       
   203 	return BTBaseband().Broadcast(aData);
       
   204 	}
       
   205 
       
   206 EXPORT_C TInt CBluetoothPhysicalLinks::ReadRaw(TDes8& aData)
       
   207 /** Receive (raw) broadcast data
       
   208 
       
   209 Raw broadcast data is data that is not directed to any specified service.
       
   210 @param aData Contains data received.
       
   211 @test
       
   212 @return Error code
       
   213 @capability NetworkControl
       
   214 */
       
   215 	{
       
   216 	return BTBaseband().ReadRaw(aData);
       
   217 	}
       
   218 
       
   219 EXPORT_C TInt CBluetoothPhysicalLinks::Enumerate(RBTDevAddrArray& aBTDevAddrArray, TUint aMaxNumber)
       
   220 /** Enumerate connected members of the piconet
       
   221 @param aBTDevAddrArray Bluetooth device address array to be filled with bluetooth addresses of connected members of 
       
   222 the piconet.
       
   223 @param aMaxNumber Upper limit on number of members to be returned.
       
   224 @return Error code
       
   225 */
       
   226 	{
       
   227 	return BTBaseband().Enumerate(aBTDevAddrArray, aMaxNumber);
       
   228 	}
       
   229 
       
   230 
       
   231 
       
   232 void CBluetoothPhysicalLinks::HandleCreateConnectionCompleteL(TInt aErr)
       
   233 	{
       
   234 	delete iBTBasebandConnecter;
       
   235 	iBTBasebandConnecter = 0;
       
   236 	iNotifier.HandleCreateConnectionCompleteL(aErr);
       
   237 	}
       
   238 
       
   239 void CBluetoothPhysicalLinks::HandleDisconnectCompleteL(TInt aErr)
       
   240 	{
       
   241 	delete iBTDisconnector;
       
   242 	iBTDisconnector = 0;
       
   243 	iNotifier.HandleDisconnectCompleteL(aErr);
       
   244 	}
       
   245 
       
   246 void CBluetoothPhysicalLinks::HandleDisconnectAllCompleteL(TInt aErr)
       
   247 	{
       
   248 	delete iBTDisconnector;
       
   249 	iBTDisconnector = 0;
       
   250 	Notifier().HandleDisconnectAllCompleteL(aErr);
       
   251 	}
       
   252 
       
   253 RSocketServ& CBluetoothPhysicalLinks::SockServer()
       
   254 	{
       
   255 	return iSockServer;
       
   256 	}
       
   257 
       
   258 RBTBaseband& CBluetoothPhysicalLinks::BTBaseband()
       
   259 	{
       
   260 	return iBTBaseband;
       
   261 	}
       
   262 
       
   263 MBluetoothPhysicalLinksNotifier& CBluetoothPhysicalLinks::Notifier()
       
   264 	{
       
   265 	return iNotifier;
       
   266 	}
       
   267 
       
   268 
       
   269 
       
   270 
       
   271 
       
   272 
       
   273 
       
   274 
       
   275 
       
   276 
       
   277 
       
   278 
       
   279 
       
   280 
       
   281 //==========================================================
       
   282 //Active Object Helpers
       
   283 //
       
   284 
       
   285 //
       
   286 //for CBluetoothPhysicalLinks
       
   287 //
       
   288 
       
   289 //--
       
   290 CBTBasebandConnecter* CBTBasebandConnecter::NewL(CBluetoothPhysicalLinks& aParent)
       
   291 {
       
   292 	CBTBasebandConnecter* self = new (ELeave) CBTBasebandConnecter(aParent);
       
   293 	CleanupStack::PushL(self);
       
   294 	self->ConstructL();
       
   295 	CleanupStack::Pop();//self
       
   296 	return self;
       
   297 }
       
   298 
       
   299 CBTBasebandConnecter::CBTBasebandConnecter(CBluetoothPhysicalLinks& aParent)
       
   300 : CActive(CActive::EPriorityStandard),iParent(aParent)
       
   301 //
       
   302 //Constructorthe RBluetoothSocket parent is provided. 
       
   303 //It contains all the ESocky stuff and has suitable "Getters".
       
   304 //
       
   305 	{
       
   306 	}
       
   307 
       
   308 void CBTBasebandConnecter::ConstructL()
       
   309 	{
       
   310 	CActiveScheduler::Add(this);
       
   311 	}
       
   312 
       
   313 CBTBasebandConnecter::~CBTBasebandConnecter()
       
   314 //
       
   315 //Destructor
       
   316 //
       
   317 	{
       
   318 	if(IsActive())
       
   319 		{
       
   320 		Cancel();
       
   321 		}
       
   322 	}
       
   323 
       
   324 
       
   325 void CBTBasebandConnecter::BasebandConnect(const TBTDevAddr& aBDAddr)
       
   326 //
       
   327 //Attempts to connect to the remote device. 
       
   328 //
       
   329 	{
       
   330 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
   331 	
       
   332 	iParent.BTBaseband().Connect(aBDAddr, iStatus);
       
   333 	SetActive();
       
   334 	}
       
   335 
       
   336 void CBTBasebandConnecter::RunL()
       
   337 //
       
   338 //When logical socket has connected (only async bit), 
       
   339 //opens baseband socket.
       
   340 //
       
   341 	{
       
   342 	//must come LAST .. deletes this active object
       
   343 	iParent.HandleCreateConnectionCompleteL(iStatus.Int()); 
       
   344 	}
       
   345 
       
   346 //
       
   347 // Swallow any error from RunL
       
   348 //
       
   349 TInt CBTBasebandConnecter::RunError(TInt /*aError*/)
       
   350 	{
       
   351 	return KErrNone;
       
   352 	}
       
   353 
       
   354 void CBTBasebandConnecter::DoCancel()
       
   355 //
       
   356 //Cancels current asynchronous requests.
       
   357 //
       
   358 	{
       
   359 	iParent.BTBaseband().TerminatePhysicalLink(0); //zam todo - check
       
   360 	}
       
   361 
       
   362 
       
   363 
       
   364 
       
   365 //----
       
   366 
       
   367 CBTDisconnector* CBTDisconnector::NewL(CBluetoothPhysicalLinks& aParent)
       
   368 //
       
   369 //NewL: the CBluetoothPhysicalLinkssManager parent is provided. 
       
   370 //It contains all the ESocky stuff and has suitable "Getters".
       
   371 //
       
   372 {
       
   373 	CBTDisconnector* self = new (ELeave) CBTDisconnector(aParent);
       
   374 	CleanupStack::PushL(self);
       
   375 	self->ConstructL();
       
   376 	CleanupStack::Pop();//self
       
   377 	return self;
       
   378 }
       
   379 
       
   380 
       
   381 CBTDisconnector::CBTDisconnector(CBluetoothPhysicalLinks& aParent)
       
   382 : CActive(CActive::EPriorityStandard),
       
   383  iParent(aParent)
       
   384 //
       
   385 //Constructor
       
   386 //
       
   387 	{
       
   388 	}
       
   389 
       
   390 void CBTDisconnector::ConstructL()
       
   391 	{
       
   392 	CActiveScheduler::Add(this);
       
   393 	}
       
   394 
       
   395 CBTDisconnector::~CBTDisconnector()
       
   396 //
       
   397 //Destructor
       
   398 //
       
   399 	{
       
   400 	if(IsActive())
       
   401 		{
       
   402 		Cancel();
       
   403 		}
       
   404 	}
       
   405 
       
   406 void CBTDisconnector::Disconnect(const TBTDevAddr& aBDAddr)
       
   407 	{
       
   408 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
   409 
       
   410 	iParent.BTBaseband().TerminatePhysicalLink(0, aBDAddr, iStatus);
       
   411 	iCurrentRequest = EDisconnect;
       
   412 	SetActive();
       
   413 	}
       
   414 
       
   415 void CBTDisconnector::DisconnectAll()
       
   416 
       
   417 	{
       
   418 	__ASSERT_ALWAYS(!IsActive(), Panic(EUnfinishedBusiness));
       
   419 
       
   420 	iParent.BTBaseband().TerminateAllPhysicalLinks(0, iStatus);
       
   421 	iCurrentRequest = EDisconnectAll;
       
   422 	SetActive();
       
   423 	}
       
   424 
       
   425 void CBTDisconnector::RunL()
       
   426 //
       
   427 //When logical socket has connected (only async bit), 
       
   428 //opens baseband socket.
       
   429 //
       
   430 	{
       
   431 	//must come LAST .. deletes this active object
       
   432 	switch (iCurrentRequest)
       
   433 		{
       
   434 		//zam todo do these cover all Read/Recv methods?
       
   435 		case EDisconnect:
       
   436 			//must come LAST .. deletes this active object
       
   437 			iParent.HandleDisconnectCompleteL(iStatus.Int());
       
   438 			break;
       
   439 		case EDisconnectAll:
       
   440 			//must come LAST .. deletes this active object
       
   441 			iParent.HandleDisconnectAllCompleteL(iStatus.Int());
       
   442 			break;
       
   443 		default:
       
   444 			Panic(EBadRequest);
       
   445 			iCurrentRequest = ENone;
       
   446 			break;
       
   447 		};
       
   448 	}
       
   449 
       
   450 //
       
   451 // Swallow any error from RunL
       
   452 //
       
   453 TInt CBTDisconnector::RunError(TInt /*aError*/)
       
   454 	{
       
   455 	return KErrNone;
       
   456 	}
       
   457 
       
   458 void CBTDisconnector::DoCancel()
       
   459 //
       
   460 //Cancels current asynchronous requests.
       
   461 //
       
   462 	{
       
   463 	//zam todo - more here...can we cancel a shutdown?
       
   464 	iCurrentRequest = ENone;
       
   465 	}
       
   466 
       
   467 //
       
   468 // Function Definitions for the M- Classes associated with CBluetoothPhysicalLinks 
       
   469 //
       
   470 
       
   471 /**
       
   472 This virtual function allows the M- class to be extended in future in a binary
       
   473 compatible way by providing a method that clients can override in future to
       
   474 allow extra callbacks to be made via aObject.
       
   475 */
       
   476 EXPORT_C void MBluetoothSynchronousLinkNotifier::MBSLN_ExtensionInterfaceL(TUid /*aInterface*/, void*& aObject)
       
   477 	{
       
   478 	aObject = NULL;
       
   479 	}
       
   480 
       
   481 /**
       
   482 This virtual function allows the M- class to be extended in future in a binary
       
   483 compatible way by providing a method that clients can override in future to
       
   484 allow extra callbacks to be made via aObject.
       
   485 */
       
   486 EXPORT_C void MBluetoothPhysicalLinksNotifier::MBPLN_ExtensionInterfaceL(TUid /*aInterface*/, void*& aObject)
       
   487 	{
       
   488 	aObject = NULL;
       
   489 	}
       
   490 	
       
   491 
       
   492 /**
       
   493 This virtual function allows the M- class to be extended in future in a binary
       
   494 compatible way by providing a method that clients can override in future to
       
   495 allow extra callbacks to be made via aObject.
       
   496 */
       
   497 EXPORT_C void MBluetoothSocketNotifier::MBSN_ExtensionInterfaceL(TUid /*aInterface*/, void*& aObject)
       
   498 	{
       
   499 	aObject = NULL;
       
   500 	}
       
   501 	
       
   502 // EOF