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