bluetoothappprofiles/avrcp/remconbeareravrcp/src/avrcpbrowsingcommandhandler.cpp
changeset 70 f5508c13dfe0
parent 67 16e4b9007960
child 71 083fd884d7dd
equal deleted inserted replaced
67:16e4b9007960 70:f5508c13dfe0
     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 // avrcpincomingcommandhandler.cpp
       
    15 //
       
    16 
       
    17 
       
    18 
       
    19 /**
       
    20  @file
       
    21  @internalComponent
       
    22  @released
       
    23 */
       
    24 #include <e32base.h>
       
    25 #include <remcon/remconbearerbulkobserver.h>
       
    26 #include <remconaddress.h>
       
    27 
       
    28 #include "browsecommand.h"
       
    29 #include "avrcpbrowsingcommandhandler.h"
       
    30 #include "avrcpinternalinterface.h"
       
    31 #include "avrcplog.h"
       
    32 #include "avrcprouter.h"
       
    33 #include "avrcputils.h"
       
    34 #include "browsingframe.h"
       
    35 #include "bulkbearer.h"
       
    36 #include "mediabrowse.h"
       
    37 #include "remconcommandinterface.h"
       
    38 
       
    39 //------------------------------------------------------------------------------------
       
    40 // Construction/Destruction
       
    41 //------------------------------------------------------------------------------------
       
    42 
       
    43 /** Factory function.
       
    44 
       
    45 @param aCommandInterface	The interface for providing commands that have been handled.
       
    46 @param aRouter	A CRcpRouter to use for communication with remote devices.
       
    47 @param aPlayerInfoManager	The central manager for player information.
       
    48 @param aAddr	The Bluetooth device address for the remote device handled by this handler.
       
    49 @return A fully constructed CRcpBrowsingCommandHandler.
       
    50 @leave System wide error codes.
       
    51 */
       
    52 CRcpBrowsingCommandHandler* CRcpBrowsingCommandHandler::NewL(MRemConBulkCommandInterface& aCommandInterface,
       
    53 	CBulkRouter& aRouter,
       
    54 	CAvrcpPlayerInfoManager& aPlayerInfoManager,
       
    55 	const TBTDevAddr& aAddr)
       
    56 	{
       
    57 	LOG_STATIC_FUNC
       
    58 	CRcpBrowsingCommandHandler* handler = new(ELeave) CRcpBrowsingCommandHandler(aCommandInterface, aRouter, aPlayerInfoManager, aAddr);
       
    59 	return handler;
       
    60 	}
       
    61 
       
    62 /**
       
    63 @param aCommandInterface	The interface for providing commands that have been handled.
       
    64 @param aRouter	A CRcpRouter to use for communication with remote devices.
       
    65 @param aPlayerInfoManager	The central manager for player information.
       
    66 @param aAddr	The Bluetooth device address for the remote device handled by this handler.
       
    67 @return A partially constructed CRcpBrowsingCommandHandler.
       
    68 */	
       
    69 CRcpBrowsingCommandHandler::CRcpBrowsingCommandHandler(MRemConBulkCommandInterface& aCommandInterface,
       
    70 	CBulkRouter& aRouter,
       
    71 	CAvrcpPlayerInfoManager& aPlayerInfoManager,
       
    72 	const TBTDevAddr& aAddr) 
       
    73 	: iCommandQueue(_FOFF(CBrowseCommand, iHandlingLink))
       
    74 	, iInternalCommandQueue(_FOFF(CBrowseCommand, iHandlingLink))
       
    75 	, iCommandInterface(aCommandInterface)
       
    76 	, iRouter(aRouter)
       
    77 	, iMtu(335) // initialise to min for browse channel
       
    78 	, iPlayerInfoManager(aPlayerInfoManager)
       
    79 	, iAddr(aAddr)
       
    80 	{
       
    81 	LOG_FUNC
       
    82 	}
       
    83 	
       
    84 CRcpBrowsingCommandHandler::~CRcpBrowsingCommandHandler()
       
    85 	{
       
    86 	LOG_FUNC
       
    87 
       
    88 	while (!iCommandQueue.IsEmpty())
       
    89 		{
       
    90 		CBrowseCommand *command = iCommandQueue.First();
       
    91 		HandledCommand(*command);
       
    92 		}
       
    93 	
       
    94 	TRemConAddress remConAddr;
       
    95 	AvrcpUtils::BTToRemConAddr(iAddr, remConAddr);
       
    96 	iCommandInterface.MrcbciRemoveAddressing(remConAddr);
       
    97 	}
       
    98 
       
    99 //---------------------------------------------------------------------
       
   100 // Called from the bearer
       
   101 //---------------------------------------------------------------------
       
   102 
       
   103 /** Tell the handler to gracefully shutdown.
       
   104 
       
   105 */
       
   106 void CRcpBrowsingCommandHandler::Disconnect()
       
   107 	{
       
   108 	LOG_FUNC
       
   109 	
       
   110 	while (!iCommandQueue.IsEmpty())
       
   111 		{
       
   112 		CBrowseCommand* command = iCommandQueue.First();
       
   113 		iRouter.RemoveFromSendQueue(*command);
       
   114 		
       
   115 		HandledCommand(*command);
       
   116 		}
       
   117 	}
       
   118 
       
   119 //------------------------------------------------------------------------------------
       
   120 // Called by router
       
   121 //------------------------------------------------------------------------------------
       
   122 
       
   123 /** Receive an incoming AVRCP browse command.
       
   124 
       
   125 @param aMessageInformation	The command data from the AVCTP message.
       
   126 @param aTransactionLabel	AVCTP transaction label for this command.
       
   127 @param aAddr	The bluetooth device from which this command originated.
       
   128 @leave System Wide Error code
       
   129 */
       
   130 void CRcpBrowsingCommandHandler::ReceiveCommandL(const TDesC8& aMessageInformation, 
       
   131 	SymbianAvctp::TTransactionLabel aTransactionLabel, 
       
   132 	const TBTDevAddr& aAddr)
       
   133 	{
       
   134 	LOG_FUNC
       
   135 	
       
   136 	// If there's nothing beyond a header this is bobs.  Dump it now.
       
   137 	AvrcpBrowsing::BrowsingFrame::VerifyFrameL(aMessageInformation);
       
   138 	
       
   139 	TUint id = iCommandInterface.MrcciNewTransactionId();
       
   140 	CBrowseCommand* command = CBrowseCommand::NewL(aMessageInformation, id, aTransactionLabel, aAddr, &iPlayerInfoManager);
       
   141 	CleanupStack::PushL(command);
       
   142 	
       
   143 	TInt result = command->ProcessIncomingCommandL(iMtu);
       
   144 	CleanupStack::Pop(command);
       
   145 
       
   146 	command->IncrementUsers();
       
   147 	
       
   148 	switch(result)
       
   149 		{
       
   150 	case KErrAvrcpFurtherProcessingRequired: 
       
   151 		{
       
   152 		// The only command that we need to check out before sending on is
       
   153 		// SetBrowsedPlayer.  Although it's been parsed to verify that it's 
       
   154 		// a syntactically valid command we need to ensure that the selected
       
   155 		// player is available before sending it on.
       
   156 
       
   157 		__ASSERT_DEBUG(command->RemConInterfaceUid() == TUid::Uid(KRemConMediaBrowseApiUid) && command->RemConOperationId() == ESetBrowsedPlayerOperationId, AVRCP_PANIC(EFurtherProcessingRequiredForNonSetBrowsedPlayer));
       
   158 		TBool valid = HandleSetBrowsedPlayer(*command);
       
   159 		
       
   160 		if(!valid)
       
   161 			{
       
   162 			Respond(*command, result);
       
   163 			command->DecrementUsers();
       
   164 			break;
       
   165 			}
       
   166 		else
       
   167 			{
       
   168 			result = KErrNone;
       
   169 			}
       
   170 		// valid case fallsthrough to be handled as normal
       
   171 		}
       
   172 	case KErrAvrcpHandledInternallyInformRemCon:  // this case falls through
       
   173 	case KErrNone:
       
   174 		{
       
   175 		iCommandQueue.AddLast(*command);
       
   176 		iCommandInterface.MrcciNewCommand(*command);
       
   177 		
       
   178 		if (result == KErrNone)
       
   179 			{
       
   180 			break;
       
   181 			}
       
   182 		// KErrAvrcpHandledInternallyInformRemCon fallsthrough here
       
   183 		}
       
   184 	case KErrAvrcpHandledInternallyRespondNow:
       
   185 		{
       
   186 		// If the command has already set payload, just sent the command
       
   187 		iRouter.AddToSendQueue(*command);
       
   188 		command->DecrementUsers();
       
   189 		break;
       
   190 		}
       
   191 	case KErrAvrcpInternalCommand:
       
   192 		{
       
   193 		iInternalCommandQueue.AddLast(*command);
       
   194 		HandleInternalCommand(*command);
       
   195 		break;
       
   196 		}
       
   197 	default:
       
   198 		{
       
   199 		Respond(*command, result);
       
   200 		command->DecrementUsers();
       
   201 		break;
       
   202 		}
       
   203 		};
       
   204 	}
       
   205 
       
   206 /** Called from the router to indicate send completion.
       
   207 
       
   208 @param aCommand The command that has been sent.
       
   209 @param aSendResult KErrNone if the command was sent successfully.  System wide
       
   210 				   error code otherwise.
       
   211 */
       
   212 void CRcpBrowsingCommandHandler::MessageSent(CAvrcpCommand& /*aCommand*/, TInt /*aSendResult*/)
       
   213 	{
       
   214 	LOG_FUNC
       
   215 	// We try and send the response, but if we fail there's not a lot we can do about
       
   216 	// it.  Just let the remote assume the response.
       
   217 	}
       
   218 
       
   219 void CRcpBrowsingCommandHandler::MaxPacketSize(TInt aMtu)
       
   220 	{
       
   221 	iMtu = aMtu-AvrcpBrowsing::KHeaderLength;
       
   222 	}
       
   223 
       
   224 //------------------------------------------------------------------------------------
       
   225 // Called by bearer
       
   226 //------------------------------------------------------------------------------------
       
   227 
       
   228 /** Send a response.
       
   229 
       
   230 @param aInterfaceUid The RemCon interface this response is from.
       
   231 @param aId The RemCon transaction label of the command to respond to.
       
   232 @param aData The command response data.
       
   233 @return KErrNotFound if the command was not found on the queue.
       
   234 		System wide error codes.
       
   235 */
       
   236 TInt CRcpBrowsingCommandHandler::SendRemConResponse(TUid /*aInterfaceUid*/, TUint aId, RBuf8& aData)
       
   237 	{
       
   238 	LOG_FUNC
       
   239 
       
   240 	return SendResponse(iCommandQueue, aId, aData);
       
   241 	}
       
   242 
       
   243 //------------------------------------------------------------------------------------
       
   244 // Internal command handling functions
       
   245 //------------------------------------------------------------------------------------
       
   246 
       
   247 
       
   248 /** Sends a response to the remote device.
       
   249 
       
   250 @param aCommand The command to respond to.
       
   251 @param aErr The result of handling the command.
       
   252 */
       
   253 void CRcpBrowsingCommandHandler::Respond(CBrowseCommand& aCommand, TInt aErr)
       
   254 	{
       
   255 	LOG_FUNC
       
   256 	aCommand.SetResult(aErr);
       
   257 	iRouter.AddToSendQueue(aCommand);
       
   258 	}
       
   259 
       
   260 /** To be called on completion of command handling.
       
   261 
       
   262 This aggregates the handler's tidying up of a finished
       
   263 command.
       
   264 
       
   265 @param aCommand The command to tidy up.
       
   266 */	
       
   267 void CRcpBrowsingCommandHandler::HandledCommand(CBrowseCommand& aCommand)
       
   268 	{
       
   269 	LOG_FUNC
       
   270 
       
   271 	aCommand.iHandlingLink.Deque();
       
   272 	aCommand.DecrementUsers();
       
   273 	}
       
   274 
       
   275 void CRcpBrowsingCommandHandler::HandleInternalCommand(CBrowseCommand& aCommand)
       
   276 	{
       
   277 	LOG_FUNC
       
   278 
       
   279 	TUid interfaceUid;
       
   280 	TUint id;
       
   281 	TUint operationId;
       
   282 	RBuf8 commandData;
       
   283 	TBTDevAddr addr;
       
   284 	
       
   285 	aCommand.GetCommandInfo(interfaceUid, id, operationId, commandData, addr);
       
   286 	
       
   287 	__ASSERT_DEBUG(interfaceUid == TUid::Uid(KUidAvrcpInternalInterface), AvrcpUtils::Panic(EAvrcpInternalHandlingRequestedOnWrongInterface));
       
   288 	
       
   289 	TInt err = KErrNone;
       
   290 	switch(operationId)
       
   291 		{
       
   292 	case EAvrcpInternalGetFolderItems:
       
   293 		{
       
   294 		err = HandleGetFolderItems(id, commandData);
       
   295 		break;
       
   296 		}
       
   297 		};
       
   298 	
       
   299 	if(err)
       
   300 		{
       
   301 		HandledCommand(aCommand);
       
   302 		}
       
   303 	
       
   304 	commandData.Close();
       
   305 	}
       
   306 
       
   307 TInt CRcpBrowsingCommandHandler::HandleGetFolderItems(TUint aId, RBuf8& aCommandData)
       
   308 	{
       
   309 	LOG_FUNC
       
   310 
       
   311 	RBuf8 responseBuf;
       
   312 	TRAPD(err, DoHandleGetFolderItemsL(aCommandData, responseBuf));
       
   313 	
       
   314 	if(!err)
       
   315 		{
       
   316 		err = SendInternalResponse(aId, responseBuf);
       
   317 		}
       
   318 	
       
   319 	return err;
       
   320 	}
       
   321 
       
   322 void CRcpBrowsingCommandHandler::DoHandleGetFolderItemsL(RBuf8& aCommandData, RBuf8& aResponseData)
       
   323 	{
       
   324 	LOG_FUNC
       
   325 
       
   326 	__ASSERT_DEBUG( &iPlayerInfoManager != NULL, AvrcpUtils::Panic(EAvrcpNotFullyConstructed));
       
   327 	RAvrcpGetFolderItemsRequest request;
       
   328 	CleanupClosePushL(request);
       
   329 	request.ReadL(aCommandData);
       
   330 	
       
   331 	// Use 4 bytes even though player ids are 2 bytes becuase of
       
   332 	// restrictions on RArray preventing use of non-word aligned types
       
   333 	RArray<TUint> players;
       
   334 	TInt err = iPlayerInfoManager.PlayerListing(request.iStartItem, request.iEndItem, players);
       
   335 	CleanupStack::PopAndDestroy(&request);
       
   336 	
       
   337 	RRemConMediaErrorResponse errResponse;
       
   338 	if(err != KErrNone)
       
   339 		{
       
   340 		CleanupClosePushL(players);
       
   341 		errResponse.iPduId = AvrcpBrowsing::EGetFolderItems;
       
   342 		errResponse.iStatus = (err == KErrArgument) ? AvrcpStatus::ERangeOutOfBounds : AvrcpStatus::EInternalError;
       
   343 		aResponseData.CreateL(KBrowseResponseBaseLength);
       
   344 		CleanupClosePushL(aResponseData);
       
   345 		errResponse.WriteL(aResponseData);
       
   346 		CleanupStack::Pop(&aResponseData);
       
   347 		CleanupStack::PopAndDestroy(&players);
       
   348 		return;
       
   349 		}
       
   350 	
       
   351 	RAvrcpGetFolderItemsResponse response;
       
   352 	CleanupClosePushL(response);
       
   353 	CleanupClosePushL(players);
       
   354 	for(TInt i = 0; i < players.Count(); i++)
       
   355 		{
       
   356 		RMediaPlayerItem item;
       
   357 		CleanupClosePushL(item);
       
   358 		iPlayerInfoManager.MediaPlayerItemL(players[i], item);
       
   359 		response.iItems.AppendL(item);
       
   360 		CleanupStack::Pop(&item);
       
   361 		}
       
   362 	
       
   363 	response.iPduId = AvrcpBrowsing::EGetFolderItems;
       
   364 	response.iStatus = AvrcpStatus::ESuccess;
       
   365 	response.iUidCounter = KMediaPlayerListUidCounter;
       
   366 	response.iNumberItems = players.Count();
       
   367 	CleanupStack::PopAndDestroy(&players);
       
   368 	
       
   369 	//check this fits within MTU, Leave if the response size is bigger than max size 
       
   370 	CleanupClosePushL(aResponseData);
       
   371 	if(response.Size() > iMtu)
       
   372 		{
       
   373 		
       
   374 		errResponse.iPduId = AvrcpBrowsing::EGetFolderItems;
       
   375 		errResponse.iStatus = AvrcpStatus::EInternalError;
       
   376 		aResponseData.CreateL(KBrowseResponseBaseLength);
       
   377 		errResponse.WriteL(aResponseData);
       
   378 		}
       
   379 	else
       
   380 		{
       
   381 		aResponseData.CreateL(response.Size());
       
   382 		response.WriteL(aResponseData);
       
   383 		}
       
   384 	CleanupStack::Pop(&aResponseData);
       
   385 	CleanupStack::PopAndDestroy(&response);
       
   386 	}
       
   387 
       
   388 TInt CRcpBrowsingCommandHandler::SendInternalResponse(TUint aId, RBuf8& aData)
       
   389 	{
       
   390 	LOG_FUNC
       
   391 
       
   392 	return SendResponse(iInternalCommandQueue, aId, aData);
       
   393 	}
       
   394 
       
   395 TInt CRcpBrowsingCommandHandler::SendResponse(TDblQue<CBrowseCommand>& aCommandQueue, TUint aId, RBuf8& aData)
       
   396 	{
       
   397 	LOG_FUNC
       
   398 	
       
   399 	TInt err = KErrNotFound;
       
   400 	
       
   401 	TDblQueIter<CBrowseCommand> iter(aCommandQueue);
       
   402 	CBrowseCommand* command = NULL;
       
   403 
       
   404 	while (iter)
       
   405 		{
       
   406 		command = iter++;
       
   407 		if(command->RemConCommandId() == aId)
       
   408 			{
       
   409 			err = KErrNone;
       
   410 			command->ProcessOutgoingResponse(aData);
       
   411 			
       
   412 			Respond(*command, err);
       
   413 			aData.Close();
       
   414 			HandledCommand(*command);
       
   415 			
       
   416 			break;
       
   417 			}		
       
   418 		}
       
   419 
       
   420 	return err;
       
   421 	}
       
   422 
       
   423 void CRcpBrowsingCommandHandler::SendReject(TUid /*aInterfaceUid*/, TUint aTransactionId)
       
   424 	{
       
   425 	LOG_FUNC;
       
   426 
       
   427 	TDblQueIter<CBrowseCommand> iter(iCommandQueue);
       
   428 	CBrowseCommand* command = NULL;
       
   429 
       
   430 	while (iter)
       
   431 		{
       
   432 		command = iter++;
       
   433 		if(command->RemConCommandId() == aTransactionId)
       
   434 			{
       
   435 			Respond(*command, KErrAvrcpAirInternalError);
       
   436 			HandledCommand(*command);
       
   437 			}		
       
   438 		}
       
   439 	}
       
   440 
       
   441 const TBTDevAddr& CRcpBrowsingCommandHandler::BtAddr() const
       
   442 	{
       
   443 	return iAddr;
       
   444 	}
       
   445 
       
   446 TBool CRcpBrowsingCommandHandler::HandleSetBrowsedPlayer(CBrowseCommand& aCommand)
       
   447 	{
       
   448 	TInt err = KErrNone;
       
   449 	RRemConSetBrowsedPlayerRequest request;
       
   450 	
       
   451 	TRAP(err, request.ReadL(aCommand.CommandData()));
       
   452 	__ASSERT_DEBUG(err == KErrNone, AvrcpUtils::Panic(ESetBrowsePlayerRequestCorruptedLocally));
       
   453 
       
   454 	// Check if selected player exists
       
   455 	TUint16 playerId = request.iPlayerId;
       
   456 	TRemConClientId clientId;
       
   457 	TRAP(err, clientId = iPlayerInfoManager.ClientL(playerId));
       
   458 
       
   459 	if(err == KErrNone)
       
   460 		{
       
   461 		// Selected player exists, check with RemCon if we can use it
       
   462 		TRemConAddress remConAddr;
       
   463 		AvrcpUtils::BTToRemConAddr(iAddr, remConAddr);
       
   464 		
       
   465 		TInt err = iCommandInterface.MrcbciSetAddressedClient(remConAddr, clientId);
       
   466 		}
       
   467 	
       
   468 	if(err != KErrNone)
       
   469 		{
       
   470 		// Either the player was incorrect or is already in use, form a RemCon
       
   471 		// format response, then ask the command to process it for sending out
       
   472 		// on the air.
       
   473 		RBuf8 buf;
       
   474 		TInt bufErr = buf.Create(KMediaBrowseOutBufMaxLength);
       
   475 		
       
   476 		if(bufErr == KErrNone)
       
   477 			{
       
   478 			RRemConMediaErrorResponse response;
       
   479 			response.iPduId = 0x70;
       
   480 			response.iStatus = RAvrcpIPC::SymbianErrToStatus(KErrAvrcpAirInvalidPlayerId);
       
   481 			TRAP(bufErr, response.WriteL(buf));
       
   482 			aCommand.ProcessOutgoingResponse(buf);
       
   483 			buf.Close();
       
   484 			}
       
   485 		}
       
   486 	// else we will continue processing this command as normal
       
   487 	
       
   488 	return err == KErrNone ? ETrue : EFalse;
       
   489 	}