bluetoothmgmt/bluetoothclientlib/avctpservices/avctpremotedevices.cpp
changeset 0 29b1cd4cb562
child 51 20ac952a623c
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include <bluetooth/logger.h>
       
    22 #include <bt_sock.h>
       
    23 
       
    24 #include "avctpremotedevices.h"
       
    25 #include "avctpbody.h"
       
    26 #include "avctpserviceutils.h"
       
    27 #include "avctpcommon.h"
       
    28 #include "avctpPriorities.h"
       
    29 #include "avctpserviceutils.h"
       
    30 #include "channelcontrollers.h"
       
    31 
       
    32 using namespace SymbianAvctp;
       
    33 
       
    34 #ifdef __FLOG_ACTIVE
       
    35 _LIT8(KLogComponent, LOG_COMPONENT_AVCTP_SERVICES);
       
    36 #endif
       
    37 
       
    38 RNestableLock::RNestableLock()
       
    39 	: iRefCount(0)
       
    40 	, iThreadId(KInvalidThreadId)	// set to an invalid id
       
    41 	{
       
    42 	}
       
    43 
       
    44 TInt RNestableLock::CreateLocal()
       
    45 	{
       
    46 	TInt err = iLock.CreateLocal();
       
    47 	if(err == KErrNone)
       
    48 		{
       
    49 		err = iMetaLock.CreateLocal();
       
    50 		}
       
    51 	if(err != KErrNone)
       
    52 		{
       
    53 		Close();
       
    54 		}
       
    55 	return err;
       
    56 	}
       
    57 
       
    58 void RNestableLock::Close()
       
    59 	{
       
    60 	iLock.Close();
       
    61 	iMetaLock.Close();
       
    62 	iRefCount = 0;
       
    63 	}
       
    64 
       
    65 void RNestableLock::Wait()
       
    66 	{
       
    67 	iMetaLock.Wait();
       
    68 	TThreadId currentThreadId = RThread().Id();
       
    69 	if(iThreadId == TThreadId(KInvalidThreadId) || currentThreadId != iThreadId)
       
    70 		{
       
    71 		iMetaLock.Signal();
       
    72 		iLock.Wait();
       
    73 		iMetaLock.Wait();
       
    74 		iThreadId = currentThreadId;
       
    75 		}
       
    76 	++iRefCount;
       
    77 	iMetaLock.Signal();
       
    78 	}
       
    79 
       
    80 void RNestableLock::Signal()
       
    81 	{
       
    82 	iMetaLock.Wait();
       
    83 	// Assert if current thread is stored current thread?
       
    84 	if(--iRefCount == 0)
       
    85 		{
       
    86 		iLock.Signal();
       
    87 		iThreadId = TThreadId(KInvalidThreadId);
       
    88 		}
       
    89 	iMetaLock.Signal();
       
    90 	}
       
    91 
       
    92 TAvctpRemoteDeviceInfo::TAvctpRemoteDeviceInfo(const TBTDevAddr& aAddr) :
       
    93 	iAddr(aAddr),
       
    94 	iHasSecondChannel(EFalse)
       
    95 	{
       
    96 	LOG_FUNC
       
    97 	}
       
    98 
       
    99 const TBTDevAddr& TAvctpRemoteDeviceInfo::RemoteAddress() const
       
   100 	{
       
   101 	LOG_FUNC
       
   102 	return iAddr;
       
   103 	}
       
   104 
       
   105 TBool TAvctpRemoteDeviceInfo::HasSecondChannel() const
       
   106 	{
       
   107 	LOG_FUNC
       
   108 	return iHasSecondChannel;
       
   109 	}
       
   110 
       
   111 void TAvctpRemoteDeviceInfo::SetHasSecondChannel(TBool aHasSecondChannel)
       
   112 	{
       
   113 	LOG_FUNC
       
   114 	iHasSecondChannel = aHasSecondChannel;
       
   115 	}
       
   116 
       
   117 /**
       
   118 Two phase constructor of CAvctpRemoteDevices which deals with connection and disconnection 
       
   119 of a remote device. 
       
   120 @param aNotify provides access to the MAvctpEventNotify
       
   121 @param aSocketServ a connected comms session
       
   122 @internalComponent
       
   123 */
       
   124 CAvctpRemoteDevices* CAvctpRemoteDevices::NewL(MAvctpEventNotify& aNotify, RSocketServ& aSocketServ, SymbianAvctp::TPid aPid)
       
   125 	{
       
   126 	LOG_STATIC_FUNC
       
   127 
       
   128 	CAvctpRemoteDevices* self = CAvctpRemoteDevices::NewLC(aNotify, aSocketServ, aPid);
       
   129 	CleanupStack::Pop(self);
       
   130 	return self;
       
   131 	}
       
   132 	
       
   133 /**
       
   134 Two phase constructor of CAvctpRemoteDevices which deals with connection and disconnection 
       
   135 of a remote device. 
       
   136 This leaves newly created object on cleanupstack
       
   137 @param aAvctpBody provides access to the MAvctpEventNotify
       
   138 @param aSocketServ a connected comms session
       
   139 @internalComponent
       
   140 */
       
   141 CAvctpRemoteDevices* CAvctpRemoteDevices::NewLC(MAvctpEventNotify& aNotify, RSocketServ& aSocketServ, SymbianAvctp::TPid aPid)
       
   142 	{
       
   143 	LOG_STATIC_FUNC
       
   144 
       
   145 	CAvctpRemoteDevices* self = new(ELeave) CAvctpRemoteDevices(aNotify, aSocketServ);
       
   146 	CleanupStack::PushL(self);
       
   147 	self->ConstructL(aPid);
       
   148 	return self;	
       
   149 	}
       
   150 
       
   151 /**
       
   152 c'tor
       
   153 @internalComponent
       
   154 */
       
   155 CAvctpRemoteDevices::CAvctpRemoteDevices(MAvctpEventNotify& aNotify, RSocketServ& aSocketServ):
       
   156  	iNotify(aNotify),
       
   157 	iSocketServ(aSocketServ)
       
   158 	{
       
   159 	LOG_FUNC
       
   160 	}
       
   161 	
       
   162 
       
   163 /** 
       
   164 @internalComponent
       
   165 */
       
   166 void CAvctpRemoteDevices::ConstructL(SymbianAvctp::TPid aPid)
       
   167 	{
       
   168 	LOG_FUNC
       
   169 
       
   170 	User::LeaveIfError(iLock.CreateLocal()); 
       
   171 	
       
   172 	iPid = aPid;
       
   173 	iState = EListening;
       
   174 	
       
   175 	iPrimaryChannelController = CPrimaryChannelController::NewL(*this, iSocketServ, aPid);	
       
   176 	iPrimaryChannelController->Listen();
       
   177 	}
       
   178 
       
   179 	
       
   180 /**
       
   181 d'tor
       
   182 @internalComponent
       
   183 */	
       
   184 CAvctpRemoteDevices::~CAvctpRemoteDevices()
       
   185 	{
       
   186 	LOG_FUNC
       
   187 	iLock.Wait();
       
   188 	iLock.Close();
       
   189 
       
   190 	iRemoteAddrs.Close();
       
   191 	delete iSecondaryChannelController;
       
   192 	delete iPrimaryChannelController;
       
   193 	}
       
   194 
       
   195 /**
       
   196 Starts a connection to a remote device
       
   197 @param aBTDevice the remote device to connect to
       
   198 @internalComponent
       
   199 */
       
   200 TInt CAvctpRemoteDevices::PrimaryChannelAttachRequest(const TBTDevAddr& aBTDevice)
       
   201 	{
       
   202 	LOG_FUNC
       
   203 	
       
   204 	iLock.Wait();
       
   205 
       
   206 	TInt err = KErrRepeatConnectionAttempt;
       
   207 
       
   208 	// Start the connection attempt
       
   209 	
       
   210 	if (iState == EListening)
       
   211 		{
       
   212 		if (!IsAttached(aBTDevice))
       
   213 			{
       
   214 			iState = ECreatingControlLink;
       
   215 			iPrimaryChannelController->AttachRequest(aBTDevice);
       
   216 			err = KErrNone;
       
   217 			}
       
   218 		}
       
   219 	
       
   220 	if (err != KErrNone)
       
   221 		{
       
   222 		iState = EListening;
       
   223 		}
       
   224 	
       
   225 	iLock.Signal();
       
   226 	return err;
       
   227 	}
       
   228 
       
   229 /**
       
   230 Called after a Attach Indicate. If the connection hasn't been accepted it send back to the server
       
   231 a refuse attach ioctl, and the client won't be notified through the controlling channel
       
   232 @param aBTDevice the remote device to add
       
   233 @param aClientsAccepts ETrue if the client has accepted the Attach Indication or
       
   234 					   EFalse if the client has denied the Attach Indication
       
   235 @param aChannel the controlling channel through the indication has been received
       
   236 @internalComponent
       
   237 */
       
   238 TInt CAvctpRemoteDevices::PropagateAttachRsp(const TBTDevAddr& aBTDevice, 
       
   239 							  			      TBool aClientsAccepts,
       
   240 							  			      TInt aChannel)
       
   241 	{
       
   242 	LOG_FUNC
       
   243 
       
   244 	TInt err = KErrNone;
       
   245 
       
   246 	if (aClientsAccepts) 
       
   247 		{
       
   248 		if (err == KErrNone && !IsAttached(aBTDevice) && aChannel == KAvctpPrimaryChannel)
       
   249 			{
       
   250 			//only add device on primary control channel connection
       
   251 			TAvctpRemoteDeviceInfo info(aBTDevice);
       
   252 			err = iRemoteAddrs.Append(info);
       
   253 			}
       
   254 		}
       
   255 	else
       
   256 		{
       
   257 		__ASSERT_DEBUG(aChannel == KAvctpPrimaryChannel || aChannel == KAvctpSecondaryChannel, Panic(EAvctpInvalidChannel));
       
   258 		
       
   259 		CBaseChController* pointer = (aChannel == KAvctpPrimaryChannel) ? ((CBaseChController*)iPrimaryChannelController) : ((CBaseChController*)iSecondaryChannelController);
       
   260 		
       
   261 		__ASSERT_DEBUG(pointer, Panic(EAvctpNullControllerChannel));
       
   262 		pointer->RefuseAttach(aBTDevice);	// won't receive a confirmation
       
   263 		}	
       
   264 	
       
   265 	return err;
       
   266 	}
       
   267 	
       
   268 /**
       
   269 Explicitly disconnect from the give remote device.
       
   270 @internalComponent
       
   271 */
       
   272 TInt CAvctpRemoteDevices::PrimaryChannelDetachRequest(const TBTDevAddr& aBTDevice)
       
   273 	{
       
   274 	LOG_FUNC
       
   275 	
       
   276 	iLock.Wait();
       
   277 	
       
   278 	TInt err = KErrFirstChannelNotAttached;
       
   279 	const TAvctpRemoteDeviceInfo* info = RemoteDeviceInfo(aBTDevice);
       
   280 	if (info)
       
   281 		{
       
   282 		iPrimaryChannelController->DetachRequest(aBTDevice);
       
   283 		err = KErrNone;
       
   284 		}
       
   285 	
       
   286 	iLock.Signal();
       
   287 	
       
   288 	// else covered by ret
       
   289 	return err;
       
   290 	}
       
   291 
       
   292 void CAvctpRemoteDevices::PrimaryChannelCancelAttach(const TBTDevAddr& aBTDevice)
       
   293 	{
       
   294 	LOG_FUNC
       
   295 	
       
   296 	iLock.Wait();
       
   297 	
       
   298 	if (!IsAttached(aBTDevice) && iState == ECreatingControlLink)
       
   299 		{
       
   300 		// the notifications are only propagated if the state is indicating an
       
   301 		// outstanding request. So, simply changing the state to listening will avoid
       
   302 		// the notification to be propagated. 
       
   303 		// We also need to notify the stack that we are not interested so to have a consistent state
       
   304 		// It is safe to call RefuseAttach that deal with this scenario server side.
       
   305 		iPrimaryChannelController->RefuseAttach(aBTDevice);
       
   306 		iState = EListening;	
       
   307 		}
       
   308 	
       
   309 	iLock.Signal();
       
   310 	}
       
   311 
       
   312 TInt CAvctpRemoteDevices::SecondaryChannelAttachRequest(const TBTDevAddr& aBTDevice)
       
   313 	{
       
   314 	LOG_FUNC
       
   315 	
       
   316 	iLock.Wait();
       
   317 	TInt err = KErrFirstChannelNotAttached;
       
   318 	const TAvctpRemoteDeviceInfo* info = RemoteDeviceInfo(aBTDevice);
       
   319 	if (info)
       
   320 		{
       
   321 		err = KErrRepeatConnectionAttempt;
       
   322 		if (!info->HasSecondChannel())
       
   323 			{
       
   324 			__ASSERT_DEBUG(iSecondaryChannelNotify, Panic(EAvctpSecondChannelNotPresent));
       
   325 			if (iState == EListening)
       
   326 				{
       
   327 				iState = ECreatingSecondLink;
       
   328 				iSecondaryChannelController->AttachRequest(aBTDevice);
       
   329 				err = KErrNone;
       
   330 				}
       
   331 			}
       
   332 		}
       
   333 	iLock.Signal();
       
   334 	return err;
       
   335 	}
       
   336 
       
   337 TInt CAvctpRemoteDevices::SecondaryChannelDetachRequest(const TBTDevAddr& aBTDevice)
       
   338 	{
       
   339 	LOG_FUNC
       
   340 	
       
   341 	iLock.Wait();
       
   342 	TInt err = KErrSecondChannelNotAttached;
       
   343 	const TAvctpRemoteDeviceInfo* info = RemoteDeviceInfo(aBTDevice);
       
   344 	if (info)
       
   345 		{
       
   346 		if (info->HasSecondChannel())
       
   347 			{
       
   348 			__ASSERT_DEBUG(iSecondaryChannelNotify, Panic(EAvctpSecondChannelNotPresent));
       
   349 			iSecondaryChannelController->DetachRequest(aBTDevice);
       
   350 			err = KErrNone;
       
   351 			}
       
   352 		}
       
   353 	iLock.Signal();
       
   354 	return err;
       
   355 	}
       
   356 
       
   357 void CAvctpRemoteDevices::SecondaryChannelCancelAttach(const TBTDevAddr& aBTDevice)
       
   358 	{
       
   359 	LOG_FUNC
       
   360 	
       
   361 	iLock.Wait();
       
   362 	
       
   363 	if (IsAttached(aBTDevice) && iState == ECreatingSecondLink)
       
   364 		{
       
   365 		// the notifications are only propagated if the state is indicating an
       
   366 		// outstanding request. So, simply changing the state to listening will avoid
       
   367 		// the notification to be propagated.
       
   368 		iState = EListening;	
       
   369 		}
       
   370 	
       
   371 	iLock.Signal();
       
   372 	}
       
   373 
       
   374 void CAvctpRemoteDevices::RemoveRemoteDevice(const TBTDevAddr& aBTDevice)
       
   375 	{
       
   376 	LOG_FUNC
       
   377 	
       
   378 	__DEBUG_ONLY
       
   379 		(
       
   380 		TBuf<KBTAddressLength> address;
       
   381 		aBTDevice.GetReadable(address);
       
   382 		)
       
   383 	
       
   384 	LOG1(_L("BT Device 0x%S"), &address);
       
   385 
       
   386 	TInt index;	  
       
   387 	for(index= 0; index < iRemoteAddrs.Count(); index++)
       
   388 		{
       
   389 		if (iRemoteAddrs[index].RemoteAddress() == aBTDevice)
       
   390 			{
       
   391 			iRemoteAddrs.Remove(index);
       
   392 			break; // since there should only be one remote device per TBTDevAddr
       
   393 			}
       
   394 		}
       
   395 	}
       
   396 
       
   397 const TAvctpRemoteDeviceInfo* CAvctpRemoteDevices::RemoteDeviceInfo(const TBTDevAddr& aBTDevice) const
       
   398 	{
       
   399 	LOG_FUNC
       
   400 
       
   401 	const TAvctpRemoteDeviceInfo* deviceInfo = NULL;
       
   402 	
       
   403 	TInt index;
       
   404 	for (index = 0; index < iRemoteAddrs.Count(); index++)
       
   405 		{
       
   406 		if (iRemoteAddrs[index].RemoteAddress() == aBTDevice)
       
   407 			{
       
   408 			deviceInfo = &iRemoteAddrs[index];
       
   409 			break; // there should only be one remote device on each TBTDevAddr
       
   410 			}
       
   411 		}
       
   412 	
       
   413 	return deviceInfo;
       
   414 	}
       
   415 
       
   416 TBool CAvctpRemoteDevices::IsAttached(const TBTDevAddr& aBTDevice) const
       
   417 	{
       
   418 	LOG_FUNC
       
   419 	
       
   420 	iLock.Wait();
       
   421 	
       
   422 	TBool ans = RemoteDeviceInfo(aBTDevice) ? ETrue : EFalse;
       
   423 	
       
   424 #ifdef _DEBUG
       
   425 //	// Check there is only one remote device on this TBTDevAddr in the array
       
   426 	TInt numDevices = 0;
       
   427 	TInt index;
       
   428 	for (index = 0; index < iRemoteAddrs.Count(); index++)
       
   429 		{
       
   430 		if (iRemoteAddrs[index].RemoteAddress() == aBTDevice)
       
   431 			{
       
   432 			numDevices++;
       
   433 			}
       
   434 		}
       
   435 	__ASSERT_DEBUG(((ans && numDevices == 1) || (!ans && numDevices == 0)), Panic(EAvctpRemoteAddressOccursMultipleTimes));
       
   436 #endif // _DEBUG
       
   437 	
       
   438 	LOG1(_L("IsAttached() result is: %d"), ans)
       
   439 	
       
   440 	iLock.Signal();
       
   441 	
       
   442 	return ans;
       
   443 	}
       
   444 
       
   445 /**
       
   446 All Errors to do with a remote device will result in a disconnect Ind happening
       
   447 because all device errors are currently fatal. NB since the actual error doesn't
       
   448 matter to the client they don't get informed of it.
       
   449 */
       
   450 void CAvctpRemoteDevices::NotifyError(const TBTDevAddr& aBTDevice, TInt aError, TInt aChannel)
       
   451 	{
       
   452 	LOG_FUNC
       
   453 	
       
   454 	MAvctpEventNotify* notify = aChannel == KAvctpPrimaryChannel ? &iNotify : iSecondaryChannelNotify; 
       
   455 	notify->MaenErrorNotify(aBTDevice, aError);	
       
   456 	// don't do anything here as the client may have closed the RAvctp object after the NotifyError
       
   457 	}
       
   458 
       
   459 void CAvctpRemoteDevices::SetSecondaryChannelNotifyL(MAvctpEventNotify* aSecondChannelNotify)
       
   460 	{
       
   461 	LOG_FUNC
       
   462 	
       
   463 	iLock.Wait();
       
   464 	
       
   465 	__ASSERT_DEBUG(!aSecondChannelNotify || !iSecondaryChannelNotify, Panic(EAvctpSecondaryChannelNotifyAlreadyAssigned));
       
   466 
       
   467 	if(aSecondChannelNotify)
       
   468 		{
       
   469 		iSecondaryChannelNotify = aSecondChannelNotify;
       
   470 		
       
   471 		iSecondaryChannelController = CSecondaryChannelController::NewL(*this, iSocketServ, iPid);
       
   472 		iSecondaryChannelController->Listen();
       
   473 		}
       
   474 	else
       
   475 		{
       
   476 		delete iSecondaryChannelController;
       
   477 		iSecondaryChannelController = NULL;
       
   478 		}
       
   479 	
       
   480 	iLock.Signal();
       
   481 	}
       
   482 
       
   483 void CAvctpRemoteDevices::PrimaryChannelAttachConfirm(const TBTDevAddr& aAddr, TInt aMtu, TInt aError)
       
   484 	{
       
   485 	LOG_FUNC
       
   486 	
       
   487 	iLock.Wait();
       
   488 	
       
   489 	// state must be:
       
   490 	// ECreatingControlLink because we have requested to be attached.
       
   491 	// EListening because we had asked to be attached but then we canceled the request.
       
   492 	__ASSERT_DEBUG(iState == ECreatingControlLink || iState == EListening, Panic(EAvctpInvalidChannelState));
       
   493 	
       
   494 	if (iState == ECreatingControlLink)
       
   495 		{
       
   496 		iNotify.MaenAttachConfirm(aAddr, aMtu, aError);
       
   497 		if (aError == KErrNone && !IsAttached(aAddr))
       
   498 			{
       
   499 			TAvctpRemoteDeviceInfo info(aAddr);
       
   500 			aError = iRemoteAddrs.Append(info);
       
   501 			}
       
   502 		}
       
   503 	
       
   504 	iState = EListening;
       
   505 	iPrimaryChannelController->Listen();
       
   506 	
       
   507 	iLock.Signal();
       
   508 	}
       
   509 
       
   510 void CAvctpRemoteDevices::PrimaryChannelAttachIndicate(const TBTDevAddr& aAddr, TInt aMtu)
       
   511 	{
       
   512 	LOG_FUNC
       
   513 	
       
   514 	iLock.Wait();
       
   515 	
       
   516 	TBool clientAccepts = EFalse;
       
   517 	iNotify.MaenAttachIndicate(aAddr, aMtu, clientAccepts);
       
   518 	TInt err = PropagateAttachRsp(aAddr, clientAccepts, KAvctpPrimaryChannel);
       
   519 	// we can have an error if the array append fails 
       
   520 	if (err == KErrNone)
       
   521 		{
       
   522 		if (clientAccepts)
       
   523 			{
       
   524 			iPrimaryChannelController->AgreeAttachment(aAddr);
       
   525 			// iSecondaryChannelController->AttachPassively(aAddr);
       
   526 			}
       
   527 		}
       
   528 	else 
       
   529 		{
       
   530 		NotifyError(aAddr, err, KAvctpPrimaryChannel);
       
   531 		}
       
   532 
       
   533 	// whatever happen (even an error) we need to listen anyway.
       
   534 	iState = EListening;
       
   535 	iPrimaryChannelController->Listen();
       
   536 	
       
   537 	iLock.Signal();
       
   538 	}
       
   539 
       
   540 void CAvctpRemoteDevices::PrimaryChannelDetachConfirm(const TBTDevAddr& aAddr, TInt aError)
       
   541 	{
       
   542 	LOG_FUNC
       
   543 	
       
   544 	iLock.Wait();
       
   545 	
       
   546 	if (aError == KErrNone)
       
   547 		{
       
   548 		const TAvctpRemoteDeviceInfo* info = RemoteDeviceInfo(aAddr);
       
   549 		__ASSERT_DEBUG(info, Panic(EAvctpRemoteDeviceNotConnected));
       
   550 		
       
   551 		RemoveRemoteDevice(aAddr);
       
   552 		iNotify.MaenDetachConfirm(aAddr, KErrNone);
       
   553 		}
       
   554 	else
       
   555 		{
       
   556 		NotifyError(aAddr, aError, KAvctpPrimaryChannel);
       
   557 		}
       
   558 	
       
   559 	// whatever happens we need to listen
       
   560 	iState = EListening;
       
   561 	iPrimaryChannelController->Listen();
       
   562 	
       
   563 	iLock.Signal();
       
   564 	}
       
   565 
       
   566 void CAvctpRemoteDevices::PrimaryChannelDetachIndicate(const TBTDevAddr& aAddr)
       
   567 	{
       
   568 	LOG_FUNC
       
   569 	
       
   570 	iLock.Wait();
       
   571 	
       
   572 	// it can happen that we are in ECreatingSecondLink and the remote disconnects
       
   573 	// the first channel.
       
   574 	// server side we first notify the secondary channel, then the first one. 
       
   575 	// But we are not guarantee that the secondary channel will be served first
       
   576 	// so we need to save the oldstate to update after this process.
       
   577 	// in this case, when we'll receive the secondary channel event we are in the 
       
   578 	// correct state.
       
   579 	// even if the state at the end won't be listening, we have to listen on the 
       
   580 	// primary channel anyway.
       
   581 	
       
   582 	TState oldState = iState;
       
   583 	
       
   584     const TAvctpRemoteDeviceInfo* info = RemoteDeviceInfo(aAddr);
       
   585     __ASSERT_DEBUG(info, Panic(EAvctpRemoteDeviceNotConnected));
       
   586 
       
   587     RemoveRemoteDevice(aAddr);
       
   588     iNotify.MaenDetachIndicate(aAddr);
       
   589 	
       
   590 	// we go to the previous state (before detaching the primary channel). see the comment above
       
   591 	// for more info
       
   592 	iState = oldState;
       
   593 	// but we need to listen on the primary channel anyway.
       
   594 	iPrimaryChannelController->Listen();
       
   595 	
       
   596 	iLock.Signal();
       
   597 	}
       
   598 
       
   599 void CAvctpRemoteDevices::PrimaryChannelIoctlError(const TBTDevAddr& aAddr, TInt aError)
       
   600 	{
       
   601 	LOG_FUNC
       
   602 	
       
   603 	iLock.Wait();
       
   604 	
       
   605 	iNotify.MaenErrorNotify(aAddr, aError);
       
   606 	
       
   607 	// after having notified the error we must back to listen, despite the state we were before.
       
   608 	iState = EListening;
       
   609 	iPrimaryChannelController->Listen();
       
   610 	
       
   611 	iLock.Signal();
       
   612 	}
       
   613 
       
   614 void CAvctpRemoteDevices::PrimaryChannelAgreementError(const TBTDevAddr& aAddr, TInt __DEBUG_ONLY(aError))
       
   615 	{
       
   616 	LOG_FUNC
       
   617 	
       
   618 	iLock.Wait();
       
   619 	
       
   620 	const TAvctpRemoteDeviceInfo* info = RemoteDeviceInfo(aAddr);
       
   621 	__ASSERT_DEBUG(info, Panic(EAvctpRemoteDeviceNotConnected));
       
   622 	__ASSERT_DEBUG(!info->HasSecondChannel(), Panic(EAvctpSecondaryChannelUnexpected));
       
   623 	__ASSERT_DEBUG(aError == KErrMuxerNotFound || aError == KErrNoMemory, Panic(EAvctpUnexpectedErrorCode));
       
   624 	
       
   625 	RemoveRemoteDevice(aAddr);
       
   626 	iNotify.MaenDetachIndicate(aAddr);
       
   627 	iState = EListening;
       
   628 	iPrimaryChannelController->Listen();
       
   629 	
       
   630 	iLock.Signal();
       
   631 	}
       
   632 
       
   633 void CAvctpRemoteDevices::SecondaryChannelAttachConfirm(const TBTDevAddr& aAddr, TInt aMtu, TInt aError)
       
   634 	{
       
   635 	LOG_FUNC
       
   636 	
       
   637 	iLock.Wait();
       
   638 	
       
   639 	// state must be:
       
   640 	// ECreatingControlLink because we have requested to be attached.
       
   641 	// EListening because we had asked to be attached but then we canceled the request.
       
   642 	__ASSERT_DEBUG(iState == ECreatingSecondLink || iState == EListening, Panic(EAvctpInvalidChannelState));
       
   643 		
       
   644 	if (iState == ECreatingSecondLink)
       
   645 		{
       
   646 		iSecondaryChannelNotify->MaenAttachConfirm(aAddr, aMtu, aError);
       
   647 		if (aError == KErrNone)
       
   648 			{
       
   649 			TAvctpRemoteDeviceInfo* info = const_cast<TAvctpRemoteDeviceInfo*>(RemoteDeviceInfo(aAddr));
       
   650 			__ASSERT_DEBUG(info, Panic(EAvctpRemoteDeviceNotConnected));
       
   651 			info->SetHasSecondChannel(ETrue);
       
   652 			}
       
   653 		else
       
   654 			{
       
   655 			if (aError == KErrMuxerShutDown)
       
   656 				{
       
   657 				RemoveRemoteDevice(aAddr);
       
   658 				}
       
   659 			}
       
   660 		}
       
   661 	iState = EListening;
       
   662 	iSecondaryChannelController->Listen();
       
   663 	
       
   664 	iLock.Signal();
       
   665 	}
       
   666 
       
   667 void CAvctpRemoteDevices::SecondaryChannelAttachIndicate(const TBTDevAddr& aAddr, TInt aMtu)
       
   668 	{
       
   669 	LOG_FUNC
       
   670 	
       
   671 	iLock.Wait();
       
   672 	
       
   673 	TAvctpRemoteDeviceInfo* info = const_cast<TAvctpRemoteDeviceInfo*>(RemoteDeviceInfo(aAddr));
       
   674 
       
   675 	// if info is NULL it probably means that the first channel attach indication was refused or
       
   676 	// the attach request was cancelled. However, in that case, we do nothing.
       
   677 	if (info)
       
   678 		{
       
   679 		TBool clientAccepts = EFalse;
       
   680 		if(iSecondaryChannelNotify)
       
   681 			{
       
   682 			iSecondaryChannelNotify->MaenAttachIndicate(aAddr, aMtu, clientAccepts);
       
   683 	#ifdef _DEBUG		
       
   684 			TInt err = 
       
   685 	#endif
       
   686 			PropagateAttachRsp(aAddr, clientAccepts, KAvctpSecondaryChannel);	
       
   687 			
       
   688 			// err must always be KErrNone when PropagateConnectRsp is called on the secondary channel
       
   689 			__ASSERT_DEBUG(err == KErrNone, Panic(EAvctpUnexpectedErrorCode));
       
   690 			
       
   691 			if (clientAccepts)
       
   692 				{
       
   693 				iSecondaryChannelController->AgreeAttachment(aAddr);
       
   694 				info->SetHasSecondChannel(ETrue);
       
   695 				}
       
   696 			}
       
   697 		else // No-one's interested
       
   698 			{
       
   699 			iSecondaryChannelController->RefuseAttach(aAddr);
       
   700 			}
       
   701 		}
       
   702 	iState = EListening;
       
   703 	iSecondaryChannelController->Listen();
       
   704 	
       
   705 	iLock.Signal();
       
   706 	}
       
   707 
       
   708 void CAvctpRemoteDevices::SecondaryChannelDetachConfirm(const TBTDevAddr& aAddr, TInt aError)
       
   709 	{
       
   710 	LOG_FUNC
       
   711 	
       
   712 	iLock.Wait();
       
   713 	
       
   714 	if (aError == KErrNone)
       
   715 		{
       
   716 		__ASSERT_DEBUG(iSecondaryChannelNotify, Panic(EAvctpSecondChannelNotPresent));
       
   717 		
       
   718 		TAvctpRemoteDeviceInfo* info = const_cast<TAvctpRemoteDeviceInfo*>(RemoteDeviceInfo(aAddr));
       
   719 		__ASSERT_DEBUG(info, Panic(EAvctpRemoteDeviceNotConnected));
       
   720 		__ASSERT_DEBUG(info->HasSecondChannel(), Panic(EAvctpSecondChannelNotPresent));
       
   721 		iSecondaryChannelNotify->MaenDetachConfirm(aAddr, KErrNone);
       
   722 		info->SetHasSecondChannel(EFalse);
       
   723 		}
       
   724 	else
       
   725 		{
       
   726 		NotifyError(aAddr, aError, KAvctpSecondaryChannel);
       
   727 		}
       
   728 
       
   729 	iState = EListening;
       
   730 	iSecondaryChannelController->Listen();
       
   731 	
       
   732 	iLock.Signal();
       
   733 	}
       
   734 
       
   735 void CAvctpRemoteDevices::SecondaryChannelDetachIndicate(const TBTDevAddr& aAddr)
       
   736 	{
       
   737 	LOG_FUNC
       
   738 	
       
   739 	iLock.Wait();
       
   740 	
       
   741 	// In the case where we haven't got a secondary channel notify we may still get
       
   742 	// a detach indicate if the refusal for the attach indicate has not been 
       
   743 	// processed before the remote disconnects.  
       
   744 	if(iSecondaryChannelNotify)
       
   745 		{
       
   746 		iSecondaryChannelNotify->MaenDetachIndicate(aAddr);
       
   747 		TAvctpRemoteDeviceInfo* info = const_cast<TAvctpRemoteDeviceInfo*>(RemoteDeviceInfo(aAddr));
       
   748         if (info && info->HasSecondChannel())
       
   749             {
       
   750             info->SetHasSecondChannel(EFalse);
       
   751             }
       
   752 		}
       
   753 #ifdef _DEBUG
       
   754 	else
       
   755 		{
       
   756 		// in this case we should not have any remote device info - check
       
   757 		TAvctpRemoteDeviceInfo* info = const_cast<TAvctpRemoteDeviceInfo*>(RemoteDeviceInfo(aAddr));
       
   758 		__ASSERT_ALWAYS(!info || !info->HasSecondChannel(), Panic(EDetachIndicateForSecondChannelWithNoConsumer));
       
   759 		}
       
   760 #endif
       
   761 	
       
   762 	iState = EListening;
       
   763 	iSecondaryChannelController->Listen();
       
   764 	
       
   765 	iLock.Signal();
       
   766 	}
       
   767 
       
   768 void CAvctpRemoteDevices::SecondaryChannelIoctlError(const TBTDevAddr& aAddr, TInt aError)
       
   769 	{
       
   770 	LOG_FUNC
       
   771 	
       
   772 	iLock.Wait();
       
   773 	
       
   774 	__ASSERT_DEBUG(iSecondaryChannelNotify, Panic(EAvctpSecondChannelNotPresent));
       
   775 	iSecondaryChannelNotify->MaenErrorNotify(aAddr, aError);
       
   776 	// after having notified the error we must back to listen, despite the state we were before.
       
   777 	iState = EListening;
       
   778 	iSecondaryChannelController->Listen();
       
   779 	
       
   780 	iLock.Signal();
       
   781 	}
       
   782 
       
   783 void CAvctpRemoteDevices::SecondaryChannelAgreementError(const TBTDevAddr& aAddr, TInt __DEBUG_ONLY(aError))
       
   784 	{
       
   785 	LOG_FUNC
       
   786 	
       
   787 	iLock.Wait();
       
   788 	
       
   789 	TAvctpRemoteDeviceInfo* info = const_cast<TAvctpRemoteDeviceInfo*>(RemoteDeviceInfo(aAddr));
       
   790 	__ASSERT_DEBUG(info, Panic(EAvctpRemoteDeviceNotConnected));
       
   791 	__ASSERT_DEBUG(info->HasSecondChannel(), Panic(EAvctpSecondaryChannelUnexpected));
       
   792 	__ASSERT_DEBUG(aError == KErrMuxerNotFound, Panic(EAvctpUnexpectedErrorCode));
       
   793 	
       
   794 	RemoveRemoteDevice(aAddr);
       
   795 	iSecondaryChannelNotify->MaenDetachIndicate(aAddr);
       
   796 	iState = EListening;
       
   797 	iSecondaryChannelController->Listen();
       
   798 	
       
   799 	iLock.Signal();
       
   800 	}