bluetoothappprofiles/avrcp/remconbeareravrcp/src/avrcpincomingcommandhandler.cpp
changeset 0 f63038272f30
child 20 2f88a7d66f50
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 // Copyright (c) 2004-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 
       
    18 /**
       
    19  @file
       
    20  @internalComponent
       
    21  @released
       
    22 */
       
    23 
       
    24 #include <remcon/avrcpspec.h>
       
    25 #include <remcon/remconbearerobserver.h>
       
    26 #include <remconcoreapi.h>
       
    27 
       
    28 #include "controlcommand.h"
       
    29 #include "avrcpcommandframer.h"
       
    30 #include "avrcpincomingcommandhandler.h"
       
    31 #include "avrcpinternalinterface.h"
       
    32 #include "avrcplog.h"
       
    33 #include "avrcprouter.h"
       
    34 #include "avrcptimer.h"
       
    35 #include "avrcputils.h"
       
    36 #include "controlbearer.h"
       
    37 #include "passthroughhelper.h"
       
    38 #include "mediainformation.h"
       
    39 #include "nowplaying.h"
       
    40 #include "playerinformation.h"
       
    41 #include "remconbattery.h"
       
    42 #include "remcongroupnavigation.h"
       
    43 
       
    44 //------------------------------------------------------------------------------------
       
    45 // Construction/Destruction
       
    46 //------------------------------------------------------------------------------------
       
    47 
       
    48 /** Factory function.
       
    49 
       
    50 @param aBearer	The CRemConBearerAvrcp this is to handle commands for.
       
    51 @param aObserver The observer of the bearer. Used to acquire converters.
       
    52 @param aRouter	A CRcpRouter to use for communication with remote devices.
       
    53 @param aTimer	CDeltaTimer to use for queuing timed events.
       
    54 @return A fully constructed CRcpIncomingCommandHandler.
       
    55 @leave System wide error codes.
       
    56 */
       
    57 CRcpIncomingCommandHandler* CRcpIncomingCommandHandler::NewL(CRemConBearerAvrcp& aBearer,
       
    58 	MRemConBearerObserver& aObserver,
       
    59 	CRcpRouter& aRouter,
       
    60 	CDeltaTimer& aTimer,
       
    61 	CAvrcpPlayerInfoManager& aPlayerInfoManager,
       
    62 	TBTDevAddr& aDevice) 
       
    63 	{
       
    64 	LOG_STATIC_FUNC
       
    65 	CRcpIncomingCommandHandler* handler = new(ELeave)CRcpIncomingCommandHandler(aBearer, aObserver, aRouter, aTimer, aPlayerInfoManager, aDevice);
       
    66 	CleanupStack::PushL(handler);
       
    67 	handler->ConstructL();
       
    68 	CleanupStack::Pop(handler);
       
    69 	return handler;
       
    70 	}
       
    71 
       
    72 /** Constructor.
       
    73 
       
    74 @param aBearer	The CRemConBearerAvrcp this is to handle commands for.
       
    75 @param aObserver The observer of the bearer. Used to aquire converters.
       
    76 @param aRouter	A CRcpRouter to use for communication with remote devices.
       
    77 @param aTimer	CDeltaTimer to use for queuing timed events.
       
    78 @return A partially constructed CRcpIncomingCommandHandler.
       
    79 @leave System wide error codes.
       
    80 */	
       
    81 CRcpIncomingCommandHandler::CRcpIncomingCommandHandler(CRemConBearerAvrcp& aBearer, 
       
    82 	MRemConBearerObserver& aObserver,
       
    83 	CRcpRouter& aRouter,
       
    84 	CDeltaTimer& aTimer,
       
    85 	CAvrcpPlayerInfoManager& aPlayerInfoManager,
       
    86 	TBTDevAddr& aDevice) 
       
    87 	: iCommandQueue(_FOFF(CControlCommand, iHandlingLink))
       
    88 	, iInternalCommandQueue(_FOFF(CControlCommand, iHandlingLink))
       
    89 	, iFragmenter(NULL)
       
    90 	, iBearer(aBearer)
       
    91 	, iObserver(aObserver)
       
    92 	, iRouter(aRouter)
       
    93 	, iTimer(aTimer)
       
    94 	, iAddressedMode(EFalse)
       
    95 	, iPlayerInfoManager(aPlayerInfoManager)
       
    96 	, iDevice(aDevice)
       
    97 	{
       
    98 	LOG_FUNC
       
    99 	}
       
   100 
       
   101 void CRcpIncomingCommandHandler::ConstructL()
       
   102 	{
       
   103 	LOG_FUNC
       
   104 	
       
   105 	iFragmenter = CAVRCPFragmenter::NewL();
       
   106 	iPlayerInfoManager.AddObserverL(*this);
       
   107 
       
   108 	RArray<TUint> players;
       
   109 	iPlayerInfoManager.PlayerListing(players);
       
   110 	CleanupClosePushL(players);
       
   111 	
       
   112 	if(players.Count())
       
   113 		{
       
   114 		// Start out with the first player as default.  TSP will need to update
       
   115 		// us if it wants to set a different one.  Note that this wont be used 
       
   116 		// unless we enter addressed mode anyway.
       
   117 		iClientId = iPlayerInfoManager.ClientL(0);
       
   118 		}
       
   119 	// if no player have yet been registered we'll set the default when the
       
   120 	// first one is
       
   121 	
       
   122 	CleanupStack::PopAndDestroy(&players);
       
   123 	
       
   124 	iPassthroughHelper = CPassthroughHelper::NewL(iRouter, iBearer, iTimer);
       
   125 	}
       
   126 
       
   127 /** Destructor.
       
   128 */	
       
   129 CRcpIncomingCommandHandler::~CRcpIncomingCommandHandler()
       
   130 	{
       
   131 	LOG_FUNC
       
   132 
       
   133 	iPlayerInfoManager.RemoveObserver( *this );
       
   134 	delete iFragmenter;
       
   135 	delete iPassthroughHelper;
       
   136 
       
   137 	// All commands should have been handled by the time we reach here.
       
   138 	__ASSERT_ALWAYS(iCommandQueue.IsEmpty(), AvrcpUtils::Panic(EAvrcpIncomingCommandsNotHandled));
       
   139 	__ASSERT_ALWAYS(iInternalCommandQueue.IsEmpty(), AvrcpUtils::Panic(EAvrcpIncomingCommandsNotHandled));
       
   140 	}
       
   141 
       
   142 //---------------------------------------------------------------------
       
   143 // Called from the bearer
       
   144 //---------------------------------------------------------------------
       
   145 
       
   146 /** Tell the handler to gracefully shutdown.
       
   147 
       
   148 */
       
   149 void CRcpIncomingCommandHandler::Disconnect()
       
   150 	{
       
   151 	LOG_FUNC
       
   152 	
       
   153 	iPassthroughHelper->Disconnect();
       
   154 
       
   155 	if(!iCommandQueue.IsEmpty())
       
   156 		{
       
   157 		// Cleanup remaining commands
       
   158 		while(!iCommandQueue.IsEmpty())
       
   159 			{
       
   160 			CControlCommand* command = iCommandQueue.First();
       
   161 			// Tell RemCon the command is finished with
       
   162 			iObserver.CommandExpired(command->RemConCommandId());
       
   163 			// And now remove the command from the bearer
       
   164 			iRouter.RemoveFromSendQueue(*command);
       
   165 			HandledCommand(*command);
       
   166 			}
       
   167 		}
       
   168 	if(!iInternalCommandQueue.IsEmpty())
       
   169 		{
       
   170 				
       
   171 		// Cleanup remaining commands
       
   172 		while(!iInternalCommandQueue.IsEmpty())
       
   173 			{
       
   174 			CControlCommand* command = iInternalCommandQueue.First();
       
   175 			// Tell RemCon the command is finished with
       
   176 			iObserver.CommandExpired(command->RemConCommandId());
       
   177 			// And now remove the command from the bearer
       
   178 			iRouter.RemoveFromSendQueue(*command);
       
   179 			HandledCommand(*command);
       
   180 			}
       
   181 		}
       
   182 	}
       
   183 
       
   184 //------------------------------------------------------------------------------------
       
   185 // Called by router
       
   186 //------------------------------------------------------------------------------------
       
   187 
       
   188 void CRcpIncomingCommandHandler::MaxPacketSize(TInt /*aMtu*/)
       
   189 	{
       
   190 	// ignore - we don't care if we use AVCTP fragmentation on the
       
   191 	// control channel
       
   192 	}
       
   193 
       
   194 /** Receive an incoming AVRCP command.
       
   195 
       
   196 The command is parsed from a CAVCFrame into a CControlCommand owned by the
       
   197 command handler.
       
   198 
       
   199 @param aFrame	The AVC frame contained within the AVCTP message.
       
   200 @param aTransactionLabel	AVCTP transaction label for this command.
       
   201 @param aAddr	The bluetooth device from which this command originated.
       
   202 @leave System Wide Error code
       
   203 */
       
   204 void CRcpIncomingCommandHandler::ReceiveCommandL(const TDesC8& aMessageInformation, 
       
   205 	SymbianAvctp::TTransactionLabel aTransactionLabel, 
       
   206 	const TBTDevAddr& aAddr)
       
   207 	{
       
   208 	LOG_FUNC
       
   209 		
       
   210 	TUint id = iBearer.MrcciNewTransactionId();
       
   211 	
       
   212 	// If there's nothing beyond a header this is bobs.  Dump it now.
       
   213 	if(aMessageInformation.Length() <= KAVCFrameHeaderLength)
       
   214 		{
       
   215 		User::Leave(KErrCorrupt);
       
   216 		}
       
   217 
       
   218 	// Parse it out into an AV/C frame
       
   219 	CAVCFrame* frame = CAVCFrame::NewL(aMessageInformation, AVC::ECommand);
       
   220 	
       
   221 	CControlCommand* command = CControlCommand::NewL(frame, id, aTransactionLabel, aAddr, iAddressedMode ? iClientId : KNullClientId, &AvrcpPlayerInfoManager());
       
   222 	CleanupStack::PushL(command);
       
   223 
       
   224 	TInt result = command->ParseIncomingCommandL(iObserver, *iFragmenter);
       
   225 	CleanupStack::Pop(command);
       
   226 
       
   227 	command->IncrementUsers();
       
   228 	
       
   229 	switch(result)
       
   230 		{
       
   231 		case KErrAvrcpHandledInternallyInformRemCon:
       
   232 			{
       
   233 			HandleRemConCommand(*command);
       
   234 			iRouter.AddToSendQueue(*command);
       
   235 			command->DecrementUsers();
       
   236 			break;
       
   237 			}
       
   238 		case KErrNone:
       
   239 			{
       
   240 			if(! command->IsPassthrough())
       
   241 				{
       
   242 				// add to iCommandQueue for non-passthrough commands
       
   243 				iCommandQueue.AddLast(*command);
       
   244 				}
       
   245 			HandleRemConCommand(*command);
       
   246 			break;
       
   247 			}
       
   248 		case KErrAvrcpInternalCommand:
       
   249 			{
       
   250 			iInternalCommandQueue.AddLast(*command);
       
   251 			HandleInternalCommand(*command);
       
   252 			break;
       
   253 			}
       
   254 		case KErrAvrcpInvalidCType:
       
   255 			{
       
   256 			// We should ignore commands with invalid ctype (AV/C v4.0 8.3.1).
       
   257 			command->DecrementUsers();
       
   258 			break;
       
   259 			}
       
   260 		default:
       
   261 			{
       
   262 			Respond(*command, result);
       
   263 			command->DecrementUsers();
       
   264 			break;
       
   265 			}
       
   266 		}
       
   267 	}
       
   268 
       
   269 /** Called from the router to indicate send completion.
       
   270 
       
   271 @param aCommand The command that has been sent.
       
   272 @param aSendResult KErrNone if the command was sent successfully.  System wide
       
   273 				   error code otherwise.
       
   274 */
       
   275 void CRcpIncomingCommandHandler::MessageSent(CAvrcpCommand& /*aCommand*/, TInt /*aSendResult*/)
       
   276 	{
       
   277 	LOG_FUNC
       
   278 	// We try and send the response, but if we fail there's not a lot we can do about
       
   279 	// it.  Just let the remote assume the response.
       
   280 	}
       
   281 
       
   282 //------------------------------------------------------------------------------------
       
   283 // Called by bearer
       
   284 //------------------------------------------------------------------------------------
       
   285 
       
   286 /** Send a response.
       
   287 
       
   288 @param aInterfaceUid The RemCon interface this response is from.
       
   289 @param aId The RemCon transaction label of the command to respond to.
       
   290 @param aData The command response data.
       
   291 @return KErrNone.
       
   292 */
       
   293 TInt CRcpIncomingCommandHandler::SendRemConResponse(TUid /*aInterfaceUid*/, TUint aId, RBuf8& aData)
       
   294 	{
       
   295 	LOG_FUNC
       
   296 
       
   297 	// We always take ownership of the response data in SendResponse, so we 
       
   298 	// always return KErrNone.
       
   299 	SendResponse(iCommandQueue, aId, aData);
       
   300 	return KErrNone;
       
   301 	}
       
   302 
       
   303 void CRcpIncomingCommandHandler::SendReject(TUid aInterfaceUid, TUint aId)
       
   304 	{
       
   305 	LOG_FUNC
       
   306 
       
   307 	TDblQueIter<CControlCommand> iter(iCommandQueue);
       
   308 	CControlCommand* command = NULL;
       
   309 
       
   310 	while (iter)
       
   311 		{
       
   312 		command = iter++;
       
   313 		if(command->RemConCommandId() == aId)
       
   314 			{
       
   315 			TInt err = KErrGeneral;
       
   316 			if (aInterfaceUid.iUid == KRemConMediaInformationApiUid || aInterfaceUid.iUid == KRemConPlayerInformationUid)
       
   317 				{
       
   318 				err = KErrAvrcpMetadataInternalError;
       
   319 				}
       
   320 			
       
   321 			Respond(*command, err);
       
   322 			
       
   323 			HandledCommand(*command);
       
   324 			}		
       
   325 		}
       
   326 	}
       
   327 
       
   328 //------------------------------------------------------------------------------------
       
   329 // MPlayerChangeObserver functions
       
   330 //------------------------------------------------------------------------------------
       
   331 void CRcpIncomingCommandHandler::MpcoAvailablePlayersChanged()
       
   332 	{
       
   333 	LOG_FUNC 
       
   334 	
       
   335 	TDblQueIter<CControlCommand> internalIter(iInternalCommandQueue);
       
   336 	CControlCommand* command = NULL;
       
   337 	TInt err = KErrNone;
       
   338 	
       
   339 	while(internalIter)
       
   340 		{
       
   341 		command = internalIter++;
       
   342 		if((command->RemConInterfaceUid() == TUid::Uid(KUidAvrcpInternalInterface)) && (command->RemConOperationId() == EAvrcpInternalAvailablePlayersNotification))
       
   343 			{
       
   344 			// If this fails we cann't send the notification - just give up
       
   345 			err = HandleRegisterAvailablePlayersNotification(*command);
       
   346 			static_cast<void>(err = err); // ignore this error (i.e. give up).
       
   347 			}
       
   348 		}
       
   349 	//register internal notification
       
   350 	//still need to optimize for avoid re-registering notification
       
   351 	RArray<TUint> players;
       
   352 	err = iPlayerInfoManager.PlayerListing(players);
       
   353 	
       
   354 	if(err == KErrNone)
       
   355 		{
       
   356 		TBool currentClientAvailable = EFalse;
       
   357 
       
   358 		for(TInt i = 0 ; i < players.Count() ; i++)
       
   359 			{
       
   360 			TRemConClientId clientId = 0;
       
   361 			clientId = iPlayerInfoManager.Client(players[i]);
       
   362 			if(clientId == iClientId)
       
   363 				{
       
   364 				currentClientAvailable = ETrue;
       
   365 				}
       
   366 			}
       
   367 		
       
   368 		if((iClientId == KNullClientId || !currentClientAvailable) && players.Count())
       
   369 			{
       
   370 			// If this is the first target client we set our default client id
       
   371 			// to this
       
   372 			iClientId = iPlayerInfoManager.Client(players[0]);
       
   373 			}
       
   374 		}
       
   375 
       
   376 	players.Close();
       
   377 	}
       
   378 
       
   379 void CRcpIncomingCommandHandler::MpcoAddressedPlayerChangedLocally(TRemConClientId aClientId)
       
   380 	{
       
   381 	LOG_FUNC
       
   382 	
       
   383 	TRAP_IGNORE(AddressedPlayerChangedL(aClientId));
       
   384 	}
       
   385 
       
   386 void CRcpIncomingCommandHandler::MpcoUidCounterChanged(TRemConClientId aClientId)
       
   387 	{
       
   388 	LOG_FUNC
       
   389 	
       
   390 	CControlCommand* command;
       
   391 	if(aClientId == iClientId)
       
   392 		{
       
   393 		command = FindNotify(iInternalCommandQueue, TUid::Uid(KUidAvrcpInternalInterface),EAvrcpInternalUidChangedNotification);
       
   394 		if(command)
       
   395 			{
       
   396 			// if we fail to send an update it is effectively the same condition
       
   397 			// as when the notification arrives after the controller uses the
       
   398 			// old UID counter
       
   399 			TInt err = HandleUidChangedNotification(*command);
       
   400 			
       
   401 			if(err != KErrNone)
       
   402 				{
       
   403 				Respond(*command, KErrAvrcpAirInternalError);
       
   404 				HandledCommand(*command);
       
   405 				}
       
   406 			}
       
   407 		}
       
   408 	}
       
   409 
       
   410 //------------------------------------------------------------------------------------
       
   411 // Internal command handling functions
       
   412 //------------------------------------------------------------------------------------
       
   413 
       
   414 
       
   415 /** Sends a response to the remote device.
       
   416 
       
   417 Because of the 100ms timeout for responses we send a response to
       
   418 passthrough commands as soon as we receive them.  This means the real
       
   419 response from RemCon is currently ignored.  A real response is
       
   420 only sent if this is a vendor dependent command.
       
   421 
       
   422 @param aCommand The command to respond to.
       
   423 @param aErr The result of handling the command.  KErrNone if successful.
       
   424 			KErrNotSupported if this operation is not supported.
       
   425 */		
       
   426 void CRcpIncomingCommandHandler::Respond(CControlCommand& aCommand, TInt aErr)
       
   427 	{
       
   428 	LOG_FUNC
       
   429 	aCommand.SetResponseType(aErr);
       
   430 	iRouter.AddToSendQueue(aCommand);
       
   431 	}
       
   432 
       
   433 /** To be called on completion of command handling.
       
   434 
       
   435 This aggregates the handler's tidying up of a finished
       
   436 command.
       
   437 
       
   438 @param aCommand The command to tidy up.
       
   439 */	
       
   440 void CRcpIncomingCommandHandler::HandledCommand(CControlCommand& aCommand)
       
   441 	{
       
   442 	LOG_FUNC
       
   443 	aCommand.CancelTimer(iTimer);
       
   444 	aCommand.iHandlingLink.Deque();
       
   445 	aCommand.DecrementUsers();
       
   446 	}
       
   447 
       
   448 void CRcpIncomingCommandHandler::HandleInternalCommand(CControlCommand& aCommand)
       
   449 	{
       
   450 	LOG_FUNC
       
   451 	
       
   452 	TUid interfaceUid;
       
   453 	TUint id;
       
   454 	TUint operationId;
       
   455 	RBuf8 commandData;
       
   456 	TBTDevAddr addr;
       
   457 	
       
   458 	aCommand.GetCommandInfo(interfaceUid, id, operationId, commandData, addr);
       
   459 	
       
   460 	__ASSERT_DEBUG(interfaceUid == TUid::Uid(KUidAvrcpInternalInterface), AvrcpUtils::Panic(EAvrcpInternalHandlingRequestedOnWrongInterface));
       
   461 	
       
   462 	TInt err = KErrNone;
       
   463 	switch(operationId)
       
   464 		{
       
   465 	case EAvrcpInternalSetAddressedPlayer:
       
   466 		{
       
   467 		err = HandleSetAddressedPlayer(id, commandData);
       
   468 		break;
       
   469 		}
       
   470 	case EAvrcpInternalAvailablePlayersNotification:
       
   471 		{
       
   472 		err = HandleRegisterAvailablePlayersNotification(aCommand);
       
   473 		break;
       
   474 		}
       
   475 	case EAvrcpInternalAddressedPlayerNotification:
       
   476 		{
       
   477 		err = HandleRegisterAddressedPlayerNotification(aCommand);
       
   478 		break;
       
   479 		}
       
   480 	case EAvrcpInternalUidChangedNotification:
       
   481 		{
       
   482 		err = HandleUidChangedNotification(aCommand);
       
   483 		break;
       
   484 		}
       
   485 		};
       
   486 
       
   487 	if(err != KErrNone)
       
   488 		{
       
   489 		Respond(aCommand, KErrAvrcpAirInternalError);
       
   490 		HandledCommand(aCommand);
       
   491 		}
       
   492 
       
   493 	commandData.Close();
       
   494 	}
       
   495 
       
   496 void CRcpIncomingCommandHandler::HandleRemConCommand(CControlCommand& aCommand)
       
   497 	{
       
   498 	LOG_FUNC
       
   499 	
       
   500 	if(aCommand.IsPassthrough())
       
   501 		{
       
   502 		// This deals with button press/release stuff,
       
   503 		// queues the command and responds
       
   504 		iPassthroughHelper->HandlePassthrough(aCommand);
       
   505 		}
       
   506 	else 
       
   507 		{
       
   508 		// can go directly to client (unlike passthrough which may need to map 2 commands to 1 click
       
   509 		if (aCommand.Frame().Type() == AVC::ENotify)
       
   510 			{
       
   511 			iBearer.MrccciNewNotifyCommand(aCommand, aCommand.ClientId());
       
   512 			}
       
   513 		else
       
   514 			{
       
   515 			iBearer.MrcciNewCommand(aCommand, aCommand.ClientId());
       
   516 			}
       
   517 		}
       
   518 	}
       
   519 
       
   520 TInt CRcpIncomingCommandHandler::HandleSetAddressedPlayer(TUint aId, RBuf8& aCommandData)
       
   521 	{
       
   522 	LOG_FUNC
       
   523 	
       
   524 	// Once we respond to this we've told the remote that we're using a particular player
       
   525 	iAddressedMode = ETrue;
       
   526 	
       
   527 	// SetAddressedPlayer involves not just responding to this command but
       
   528 	// also rejecting a bunch of notifies and completing the addressed player
       
   529 	// changed notify.  We try as hard as we can to ensure our state remains
       
   530 	// consistent with the view of the remote.
       
   531 	
       
   532 	// Allocate the response buffer for the SetAddressedPlayer command first
       
   533 	RBuf8 responseBuf;
       
   534 	TInt err = responseBuf.Create(KSetAddressedPlayerResponseSize);
       
   535 	if(err == KErrNone)
       
   536 		{
       
   537 		// Now we know we can at least try and send a response to the remote 
       
   538 		// do the other gubbins, which we can cope with failing
       
   539 		TRAPD(setResult, DoHandleSetAddressedPlayerL(aCommandData));
       
   540 	
       
   541 		switch(setResult)
       
   542 			{
       
   543 		case KErrNotFound:
       
   544 			{
       
   545 			RAvrcpIPCError errorResponse;
       
   546 			errorResponse.iError = KErrAvrcpAirInvalidPlayerId;
       
   547 	
       
   548 			// Can ignore this as we know we have allocated a big enough buffer
       
   549 			TRAP_IGNORE(errorResponse.WriteL(responseBuf));
       
   550 			break;
       
   551 			}
       
   552 		case KErrNone:
       
   553 		default:
       
   554 			// For any result other than KErrNotFound we managed to set the 
       
   555 			// addressed client.  Other errors indicate a failure in sending
       
   556 			// a changed response to the AddressedPlayerChanged notify.  Even
       
   557 			// if we failed sending a changed response we should have been
       
   558 			// able to reject the outstanding notify so we are in a consistent
       
   559 			// state
       
   560 			{
       
   561 			RAvrcpSetAddressedPlayerResponse response;
       
   562 			response.iStatus = AvrcpStatus::ESuccess;
       
   563 			
       
   564 			// Can ignore this as we know we have allocated a big enough buffer
       
   565 			TRAP_IGNORE(response.WriteL(responseBuf));
       
   566 			break;
       
   567 			}
       
   568 			};
       
   569 	
       
   570 		SendInternalResponse(aId, responseBuf);
       
   571 		iBearer.MrccciSetAddressedClient(iClientId);
       
   572 	
       
   573 		responseBuf.Close();
       
   574 		}
       
   575 
       
   576 	return err;
       
   577 	}
       
   578 
       
   579 void CRcpIncomingCommandHandler::DoHandleSetAddressedPlayerL(RBuf8& aCommandData)
       
   580 	{
       
   581 	LOG_FUNC
       
   582 
       
   583 	RAvrcpSetAddressedPlayerRequest request;
       
   584 	request.ReadL(aCommandData);
       
   585 	
       
   586 	TRemConClientId clientId = iPlayerInfoManager.ClientL(request.iPlayerId);
       
   587 	AddressedPlayerChangedL(clientId);
       
   588 	}
       
   589 
       
   590 void CRcpIncomingCommandHandler::AddressedPlayerChangedL(TRemConClientId aClientId)
       
   591 	{
       
   592 	LOG_FUNC
       
   593 
       
   594 	if(aClientId == iClientId)
       
   595 		{
       
   596 		return;
       
   597 		}
       
   598 
       
   599 	iClientId = aClientId;
       
   600 	
       
   601 	TDblQueIter<CControlCommand> iter(iCommandQueue);
       
   602 	CControlCommand* command = NULL;
       
   603 	
       
   604 	// Complete player specific notifications
       
   605 	while(iter)
       
   606 		{
       
   607 		command = iter++;
       
   608 		if(command->PlayerSpecificNotify())
       
   609 			{
       
   610 			iObserver.CommandExpired(command->RemConCommandId());
       
   611 
       
   612 			Respond(*command, KErrAvrcpAirAddressedPlayerChanged);
       
   613 			
       
   614 			HandledCommand(*command);
       
   615 			}
       
   616 		}
       
   617 
       
   618 	command = FindNotify(iInternalCommandQueue, TUid::Uid(KUidAvrcpInternalInterface),EAvrcpInternalAddressedPlayerNotification);
       
   619 	if(command)
       
   620 		{
       
   621 		User::LeaveIfError(HandleRegisterAddressedPlayerNotification(*command));
       
   622 		}
       
   623 	}
       
   624 
       
   625 TInt CRcpIncomingCommandHandler::HandleRegisterAvailablePlayersNotification(CControlCommand& aCommand)
       
   626 	{
       
   627 	LOG_FUNC
       
   628 
       
   629 	RBuf8 responseBuf;
       
   630 	TRAPD(err, DoHandleRegisterAvailablePlayersNotificationL(responseBuf, aCommand));
       
   631 	
       
   632 	if(err == KErrNone)
       
   633 		{
       
   634 		SendInternalResponse(aCommand.RemConCommandId(), responseBuf);
       
   635 		}
       
   636 
       
   637 	responseBuf.Close();
       
   638 	return err;
       
   639 	}
       
   640 
       
   641 void CRcpIncomingCommandHandler::DoHandleRegisterAvailablePlayersNotificationL(RBuf8& aResponseData, CControlCommand& aCommand)
       
   642 	{
       
   643 	LOG_FUNC
       
   644 
       
   645 	RAvrcpIPCError response;
       
   646 	response.iError = KErrNone;
       
   647 
       
   648 	if(DuplicateNotify(iInternalCommandQueue, aCommand))
       
   649 		{
       
   650 		response.iError = KErrAvrcpAirInvalidCommand;
       
   651 		}
       
   652 	
       
   653 	aResponseData.CreateL(KRegisterNotificationEmptyResponseSize);
       
   654 	CleanupClosePushL(aResponseData);
       
   655 	
       
   656 	response.WriteL(aResponseData);
       
   657 	CleanupStack::Pop();
       
   658 	}
       
   659 
       
   660 TInt CRcpIncomingCommandHandler::HandleRegisterAddressedPlayerNotification(CControlCommand& aCommand)
       
   661 	{
       
   662 	LOG_FUNC
       
   663 
       
   664 	// Once we respond to this we've told the remote that we're using a particular player
       
   665 	iAddressedMode = ETrue;
       
   666 	
       
   667 	RBuf8 responseBuf;
       
   668 	TRAPD(err, DoHandleRegisterAddressedPlayerNotificationL(responseBuf, aCommand));
       
   669 	
       
   670 	if(!err)
       
   671 		{
       
   672 		SendInternalResponse(aCommand.RemConCommandId(), responseBuf);
       
   673 		}
       
   674 
       
   675 	responseBuf.Close();
       
   676 	return err;
       
   677 	}
       
   678 
       
   679 void CRcpIncomingCommandHandler::DoHandleRegisterAddressedPlayerNotificationL(RBuf8& aResponseData, CControlCommand& aCommand)
       
   680 	{
       
   681 	LOG_FUNC
       
   682 
       
   683 	RAvrcpAddressedPlayerNotificationResponse response;
       
   684 	RAvrcpIPCError rejectResponse;
       
   685 	
       
   686 	if(DuplicateNotify(iInternalCommandQueue, aCommand))
       
   687 		{
       
   688 		aResponseData.CreateL(KRegisterNotificationEmptyResponseSize);
       
   689 		rejectResponse.iError = KErrAvrcpAirInvalidCommand;
       
   690 		CleanupClosePushL(aResponseData);
       
   691 		rejectResponse.WriteL(aResponseData);
       
   692 		}
       
   693 	else
       
   694 		{
       
   695 		// Tricky situation thinking.Reject if at this moment the client just be shut down
       
   696 		TRAPD(err, response.iPlayerId = iPlayerInfoManager.PlayerL(iClientId));
       
   697 		if(err != KErrNone)
       
   698 			{
       
   699 			aResponseData.CreateL(KRegisterNotificationEmptyResponseSize);
       
   700 			rejectResponse.iError = KErrAvrcpAirInvalidCommand;
       
   701 			CleanupClosePushL(aResponseData);
       
   702 			rejectResponse.WriteL(aResponseData);
       
   703 			}
       
   704 		else
       
   705 			{
       
   706 			// This line will never leave once the previous line pass 
       
   707 			response.iUidCounter = iPlayerInfoManager.UidCounterL(iClientId);
       
   708 			aResponseData.CreateL(KRegisterNotificationAddressedPlayerResponseSize);
       
   709 			CleanupClosePushL(aResponseData);
       
   710 			response.WriteL(aResponseData);
       
   711 			}
       
   712 		}
       
   713 	
       
   714 	CleanupStack::Pop();
       
   715 	}
       
   716 
       
   717 TInt CRcpIncomingCommandHandler::HandleUidChangedNotification(CControlCommand& aCommand)
       
   718 	{
       
   719 	LOG_FUNC
       
   720 
       
   721 	// Although we haven't strictly told the remote which player we're using this is 
       
   722 	// a 1.4 command, and implies use of a specific player so switch into addressed mode
       
   723 	iAddressedMode = ETrue;
       
   724 
       
   725 	RBuf8 responseBuf;
       
   726 	TUint16 uidCounter = 0;
       
   727 	TRAPD(err, uidCounter = iPlayerInfoManager.UidCounterL(iClientId));
       
   728 	
       
   729 	if(err == KErrNone && !DuplicateNotify(iInternalCommandQueue, aCommand))
       
   730 		{
       
   731 		TRAP(err, DoHandleUidChangedNotificationL(responseBuf, uidCounter));
       
   732 		}
       
   733 
       
   734 	if(err == KErrNone)
       
   735 		{
       
   736 		SendInternalResponse(aCommand.RemConCommandId(), responseBuf);
       
   737 		}
       
   738 
       
   739 	responseBuf.Close();
       
   740 	return err;
       
   741 	}
       
   742 
       
   743 void CRcpIncomingCommandHandler::DoHandleUidChangedNotificationL(RBuf8& aResponseData, TUint16 aUidCounter)
       
   744 	{
       
   745 	LOG_FUNC
       
   746 
       
   747 	RAvrcpUidCounterNotificationResponse response;
       
   748 	response.iUidCounter = aUidCounter;
       
   749 	aResponseData.CreateL(KRegisterNotificationUidChangedResponseSize);
       
   750 	CleanupClosePushL(aResponseData);
       
   751 	response.WriteL(aResponseData);
       
   752 	CleanupStack::Pop();
       
   753 	}
       
   754 
       
   755 void CRcpIncomingCommandHandler::SendInternalResponse(TUint aId, RBuf8& aData)
       
   756 	{
       
   757 	LOG_FUNC 
       
   758 
       
   759 	SendResponse(iInternalCommandQueue, aId, aData);
       
   760 	}
       
   761 
       
   762 /**
       
   763 This function always takes responsibility for the response.
       
   764 */
       
   765 void CRcpIncomingCommandHandler::SendResponse(TDblQue<CControlCommand>& aCommandQueue, TUint aId, RBuf8& aData)
       
   766 	{
       
   767 	LOG_FUNC
       
   768 	
       
   769 	TDblQueIter<CControlCommand> iter(aCommandQueue);
       
   770 	CControlCommand* command = NULL;
       
   771 
       
   772 	while (iter)
       
   773 		{
       
   774 		command = iter++;
       
   775 		if(command->RemConCommandId() == aId)
       
   776 			{
       
   777 			TInt err = command->ProcessOutgoingResponse(iObserver, aData, *iFragmenter);
       
   778 			
       
   779 			if(command->Frame().Type() == AVC::ENotify)
       
   780 				{
       
   781 				// If the interim response was successful then create a new command 
       
   782 				// to contain the final response when it arrives.  The same command
       
   783 				// cannot be re-used as we may not have finished sending this before
       
   784 				// we get the final response.  We won't have both the commands on 
       
   785 				// the handling queue at one time though, so there is no ambiguity
       
   786 				// about which is which.  We finish handling the interim response here.
       
   787 				if(err == KErrNone)
       
   788 					{
       
   789 					// Try creating the CControlCommand which will be used for the
       
   790 					// final response to the Notify.  If it fails then we will just
       
   791 					// reject the notify straight away.
       
   792 					//
       
   793 					// To start with we set its AV/C frame to be an Interim response, 
       
   794 					// since this will match the AV/C frame of the original CControlCommand
       
   795 					// once we send the interim response later in this function.
       
   796 					CControlCommand* finalResponse = NULL;
       
   797 					TRAP(err, finalResponse = command->InterimResponseL());
       
   798 				
       
   799 					if(err == KErrNone)
       
   800 						{
       
   801 						finalResponse->IncrementUsers();
       
   802 						aCommandQueue.AddLast(*finalResponse);
       
   803 						}
       
   804 					}
       
   805 				
       
   806 				if(err != KErrNone && command->NormalCommand())
       
   807 					{
       
   808 					// If we had an unsuucessful interim response, we need to remove the command from remcon
       
   809 					iObserver.CommandExpired(aId);
       
   810 					}
       
   811 				}
       
   812 
       
   813 			Respond(*command, err);
       
   814 			HandledCommand(*command);
       
   815 			break;  // Exit while (iter) loop
       
   816 			}
       
   817 		}
       
   818 	
       
   819 	// Either we have created a response which took ownership of aData, or
       
   820 	// we didn't match a command, so this was a response to something we've
       
   821 	// already removed from our queue.  We're telling RemCon that we dealt ok
       
   822 	// with this so we have resonsibility for tidying up the data.
       
   823 	aData.Close();
       
   824 	}
       
   825 
       
   826 TBool CRcpIncomingCommandHandler::DuplicateNotify(TDblQue<CControlCommand>& aCommandQueue, const CControlCommand& aCommand) const
       
   827 	{
       
   828 	TUid interfaceUid = aCommand.RemConInterfaceUid();
       
   829 	TUint operationId = aCommand.RemConOperationId();
       
   830 	
       
   831 	CControlCommand* command = NULL;
       
   832 	TDblQueIter<CControlCommand> iter(aCommandQueue);
       
   833 	TInt count = 0;
       
   834 	TBool duplicate = EFalse;
       
   835 	
       
   836 	while(iter)
       
   837 		{
       
   838 		command = iter++;
       
   839 		if((interfaceUid == command->RemConInterfaceUid())&&(operationId == command->RemConOperationId()))
       
   840 			{
       
   841 			count++;
       
   842 			// this should be a reject if we've already got a notification outstanding
       
   843 			if(count > 1)
       
   844 				{
       
   845 				duplicate = ETrue;
       
   846 				break;
       
   847 				}
       
   848 			}
       
   849 		}
       
   850 	
       
   851 	return duplicate;
       
   852 	}
       
   853 
       
   854 CControlCommand* CRcpIncomingCommandHandler::FindNotify(TDblQue<CControlCommand>& aCommandQueue, TUid aInterfaceUid, TUint aOperationId)
       
   855 	{
       
   856 	CControlCommand* command = NULL;
       
   857 	TDblQueIter<CControlCommand> iter(aCommandQueue);
       
   858 	TBool found = EFalse;
       
   859 	
       
   860 	while(iter)
       
   861 		{
       
   862 		command = iter++;
       
   863 
       
   864 		if((command->RemConInterfaceUid() == aInterfaceUid)&&(command->RemConOperationId() == aOperationId))
       
   865 			{
       
   866 			found = ETrue;
       
   867 			break;
       
   868 			}
       
   869 		}
       
   870 	
       
   871 	return found ? command : NULL;
       
   872 	}