bluetoothappprofiles/avrcp/remconbeareravrcp/src/playerstatewatcher.cpp
changeset 70 f5508c13dfe0
parent 67 16e4b9007960
child 71 083fd884d7dd
equal deleted inserted replaced
67:16e4b9007960 70:f5508c13dfe0
     1 // Copyright (c) 2008-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 #include "mediabrowse.h"
       
    17 #include "avrcputils.h"
       
    18 #include "commandhandlerinterface.h"
       
    19 #include "internalcommand.h"
       
    20 #include "mediabrowse.h"
       
    21 #include "playerstatewatcher.h"
       
    22 #include "remconcommandinterface.h"
       
    23 #include "avrcplog.h"
       
    24 #include "playerinformation.h"
       
    25 
       
    26 CPlayerWatcherBase::CPlayerWatcherBase(MRemConCommandInterface& aCommandInterface)
       
    27 	: iCommandInterface(aCommandInterface), iCommands()
       
    28 	{
       
    29 	LOG_FUNC;
       
    30 	}
       
    31 
       
    32 CPlayerWatcherBase::~CPlayerWatcherBase()
       
    33 	{
       
    34 	LOG_FUNC;
       
    35 	}
       
    36 
       
    37 void CPlayerWatcherBase::StopWatchingPlayer(TRemConClientId aClientId)
       
    38 	{
       
    39 	LOG_FUNC;
       
    40 	CInternalCommand** cmdPtr = iCommands.Find(aClientId);
       
    41 	if (cmdPtr) // This may be NULL if we've already stopped watching the player as a result of reciving a reject
       
    42 	    {
       
    43         CInternalCommand* cmd = *cmdPtr;
       
    44         
       
    45         TUint transId = cmd->RemConCommandId();
       
    46         iCommandInterface.MrcciCommandExpired(transId);
       
    47     
       
    48         iCommands.Remove(aClientId);
       
    49         cmd->DecrementUsers();
       
    50 	    }
       
    51 	}
       
    52 
       
    53 void CPlayerWatcherBase::MessageSent(CAvrcpCommand& /*aCommand*/, TInt /*aSendResult*/)
       
    54 	{
       
    55 	LOG_FUNC;
       
    56 	__DEBUG_ONLY(AVRCP_PANIC(ELowerInterfaceUsedOnInternalHandler));
       
    57 	}
       
    58 
       
    59 void CPlayerWatcherBase::MaxPacketSize(TInt /*aMtu*/)
       
    60 	{
       
    61 	LOG_FUNC;
       
    62 	__DEBUG_ONLY(AVRCP_PANIC(ELowerInterfaceUsedOnInternalHandler));
       
    63 	}
       
    64 
       
    65 void CPlayerWatcherBase::ReceiveCommandL(const TDesC8& /*aMessageInformation*/, SymbianAvctp::TTransactionLabel /*aTransLabel*/, const TBTDevAddr& /*aAddr*/)
       
    66 	{
       
    67 	LOG_FUNC;
       
    68 	__DEBUG_ONLY(AVRCP_PANIC(ELowerInterfaceUsedOnInternalHandler));
       
    69 	}
       
    70 
       
    71 CInternalCommand& CPlayerWatcherBase::FindCommand(TUid __DEBUG_ONLY(aInterfaceUid),
       
    72 		TUint aTransactionId, TRemConClientId& aFoundClientId)
       
    73 	{
       
    74 	__ASSERT_DEBUG(aInterfaceUid == TUid::Uid(KRemConPlayerInformationUid) || aInterfaceUid == TUid::Uid(KRemConMediaBrowseApiUid), AVRCP_PANIC(EResponseForWrongInterface));
       
    75 	THashMapIter<TRemConClientId, CInternalCommand*> commandIter(iCommands);
       
    76 
       
    77 	CInternalCommand* command = NULL;
       
    78 	while(commandIter.NextValue())
       
    79 		{
       
    80 		command = *commandIter.CurrentValue();
       
    81 		if(command->RemConCommandId() == aTransactionId)
       
    82 			{
       
    83 			aFoundClientId = *commandIter.CurrentKey();
       
    84 			break;
       
    85 			}
       
    86 		}
       
    87 
       
    88 	// If command is NULL we reached the end of our iter without finding the match
       
    89 	__ASSERT_DEBUG(command, AVRCP_PANIC(EUnmatchedResponseFromRemCon));
       
    90 
       
    91 	return *command;
       
    92 	}
       
    93 
       
    94 TInt CPlayerWatcherBase::SendRemConResponse(TUid aInterfaceUid, TUint aTransactionId, RBuf8& aData)
       
    95 	{
       
    96 	LOG_FUNC;
       
    97 
       
    98 	TRemConClientId clientId;
       
    99 	CInternalCommand& command = FindCommand(aInterfaceUid, aTransactionId, clientId);
       
   100 
       
   101 	ReceiveUpdate(command, clientId, aData);
       
   102 
       
   103 	return KErrNone;
       
   104 	}
       
   105 
       
   106 void CPlayerWatcherBase::SendReject(TUid aInterfaceUid, TUint aTransactionId)
       
   107 	{
       
   108 	TRemConClientId clientId;
       
   109 	(void)FindCommand(aInterfaceUid, aTransactionId, clientId);
       
   110 
       
   111 	ReceiveReject(clientId);
       
   112 	}
       
   113 
       
   114 void CPlayerWatcherBase::Disconnect()
       
   115 	{
       
   116 	LOG_FUNC;
       
   117 	}
       
   118 
       
   119 CPlayStatusWatcher* CPlayStatusWatcher::NewL(MPlayStatusObserver& aObserver, MRemConCommandInterface& aCommandInterface)
       
   120 	{
       
   121 	LOG_STATIC_FUNC;
       
   122 	CPlayStatusWatcher* watcher = new(ELeave)CPlayStatusWatcher(aObserver, aCommandInterface);
       
   123 	return watcher;
       
   124 	}
       
   125 
       
   126 CPlayStatusWatcher::CPlayStatusWatcher(MPlayStatusObserver& aObserver,
       
   127 		MRemConCommandInterface& aCommandInterface)
       
   128 	: CPlayerWatcherBase(aCommandInterface), iObserver(aObserver)
       
   129 	{
       
   130 	LOG_FUNC;
       
   131 	}
       
   132 
       
   133 CPlayStatusWatcher::~CPlayStatusWatcher()
       
   134 	{
       
   135 	LOG_FUNC;
       
   136 	}
       
   137 
       
   138 void CPlayStatusWatcher::StartWatchingPlayerL(TRemConClientId aClientId)
       
   139 	{
       
   140 	LOG_FUNC;
       
   141 	__ASSERT_DEBUG(!iCommands.Find(aClientId), AVRCP_PANIC(EAlreadyWatchingPlayer));
       
   142 
       
   143 	CInternalCommand* command = CInternalCommand::NewL(TUid::Uid(KRemConPlayerInformationUid),
       
   144 			0,
       
   145 			EGetPlayStatusUpdate,
       
   146 			KNullDesC8);
       
   147 
       
   148 	CleanupStack::PushL(command);
       
   149 	iCommands.InsertL(aClientId, command);
       
   150 	CleanupStack::Pop(command);
       
   151 	command->IncrementUsers();
       
   152 
       
   153 	// Initially request uid notification relative to stopped
       
   154 	SendPlayStatusUpdateRequest(*command, aClientId, MPlayerEventsObserver::EStopped);
       
   155 	}
       
   156 
       
   157 void CPlayStatusWatcher::SendPlayStatusUpdateRequest(CInternalCommand& aCommand, TRemConClientId& aClientId, MPlayerEventsObserver::TPlaybackStatus aPlaybackStatus)
       
   158 	{
       
   159 	LOG_FUNC;
       
   160 
       
   161 	RRemConPlayerInformationGetPlayStatusUpdateRequest request;
       
   162 	request.iStatus = aPlaybackStatus;
       
   163 
       
   164 	TBuf8<sizeof(MPlayerEventsObserver::TPlaybackStatus)> buf;
       
   165 	TRAPD(err, request.WriteL(buf));
       
   166 
       
   167 	// We know how big the request is so this should never fail
       
   168 	__ASSERT_DEBUG(err == KErrNone, AVRCP_PANIC(EUidUpdateRequestWriteFailure));
       
   169 
       
   170 	TUint transId = iCommandInterface.MrcciNewTransactionId();
       
   171 	TRAP(err, aCommand.ResetL(transId, buf));
       
   172 
       
   173 	if(err == KErrNone)
       
   174 		{
       
   175 		iCommandInterface.MrcciNewCommand(aCommand, aClientId);
       
   176 		}
       
   177 	else
       
   178 		{
       
   179 		// Doom
       
   180 		iObserver.MpsoError(aClientId);
       
   181 		}
       
   182 	}
       
   183 
       
   184 void CPlayStatusWatcher::ReceiveUpdate(CInternalCommand& aCommand, TRemConClientId aClientId, RBuf8& aData)
       
   185 	{
       
   186 	LOG_FUNC;
       
   187 
       
   188 	// Read 4 byte Big-Endian error code before the payload
       
   189 	RAvrcpIPCError errorResponse;
       
   190 	TRAPD(err, errorResponse.ReadL(aData));
       
   191 	err = err ? err : errorResponse.iError;
       
   192 
       
   193 	RRemConPlayerInformationGetPlayStatusUpdateResponse response;
       
   194 	if(!err)
       
   195 		{
       
   196 		// Parse the rest of the response (minus error code)
       
   197 		TRAP(err, response.ReadL(aData.RightTPtr(aData.Length() - KLengthErrorResponse)));
       
   198 		}
       
   199 
       
   200 	aData.Close(); // data has been used now
       
   201 
       
   202 	if(!err)
       
   203 		{
       
   204 		iObserver.MpsoPlayStatusChanged(aClientId, response.iStatus);
       
   205 		SendPlayStatusUpdateRequest(aCommand, aClientId, response.iStatus);
       
   206 		}
       
   207 	else
       
   208 		{
       
   209 		// Should never get here with a valid player.  This client is
       
   210 		// sending us junk.
       
   211 		iObserver.MpsoError(aClientId);
       
   212 		}
       
   213 	}
       
   214 
       
   215 void CPlayStatusWatcher::ReceiveReject(TRemConClientId aClientId)
       
   216 	{
       
   217 	LOG_FUNC;
       
   218 	iObserver.MpsoError(aClientId);
       
   219 	}
       
   220 
       
   221 CUidWatcher* CUidWatcher::NewL(MUidObserver& aObserver,
       
   222 		MRemConCommandInterface& aCommandInterface)
       
   223 	{
       
   224 	LOG_STATIC_FUNC;
       
   225 	CUidWatcher* watcher = new(ELeave)CUidWatcher(aObserver, aCommandInterface);
       
   226 	return watcher;
       
   227 	}
       
   228 
       
   229 CUidWatcher::CUidWatcher(MUidObserver& aObserver,
       
   230 		MRemConCommandInterface& aCommandInterface)
       
   231 	: CPlayerWatcherBase(aCommandInterface), iObserver(aObserver)
       
   232 	{
       
   233 	LOG_FUNC;
       
   234 	}
       
   235 
       
   236 CUidWatcher::~CUidWatcher()
       
   237 	{
       
   238 	LOG_FUNC;
       
   239 	}
       
   240 
       
   241 void CUidWatcher::StartWatchingPlayerL(TRemConClientId aClientId)
       
   242 	{
       
   243 	LOG_FUNC;
       
   244 	__ASSERT_DEBUG(!iCommands.Find(aClientId), AVRCP_PANIC(EAlreadyWatchingPlayer));
       
   245 
       
   246 	CInternalCommand* command = CInternalCommand::NewL(TUid::Uid(KRemConMediaBrowseApiUid),
       
   247 			0,
       
   248 			EMediaLibraryStateCookieUpdateOperationId,
       
   249 			KNullDesC8);
       
   250 
       
   251 	CleanupStack::PushL(command);
       
   252 	iCommands.InsertL(aClientId, command);
       
   253 	CleanupStack::Pop(command);
       
   254 	command->IncrementUsers();
       
   255 
       
   256 	// Initially request uid notification relative to 0 uid counter
       
   257 	SendUidUpdateRequest(*command, aClientId, 0);
       
   258 	}
       
   259 
       
   260 void CUidWatcher::SendUidUpdateRequest(CInternalCommand& aCommand, TRemConClientId& aClientId, TUint16 aUidCounter)
       
   261 	{
       
   262 	LOG_FUNC;
       
   263 	RRemConUidsChangedRequest request;
       
   264 	request.iInitialUidCounter = aUidCounter;
       
   265 	TBuf8<sizeof(TUint16)> buf;
       
   266 	TRAPD(err, request.WriteL(buf));
       
   267 	// We know how big the request is so this should never fail
       
   268 	__ASSERT_DEBUG(err == KErrNone, AVRCP_PANIC(EUidUpdateRequestWriteFailure));
       
   269 
       
   270 	TUint transId = iCommandInterface.MrcciNewTransactionId();
       
   271 	TRAP(err, aCommand.ResetL(transId, buf));
       
   272 
       
   273 	if(err == KErrNone)
       
   274 		{
       
   275 		iCommandInterface.MrcciNewCommand(aCommand, aClientId);
       
   276 		}
       
   277 	else
       
   278 		{
       
   279 		// Doom
       
   280 		iObserver.MuoError(aClientId);
       
   281 		}
       
   282 	}
       
   283 
       
   284 void CUidWatcher::ReceiveUpdate(CInternalCommand& aCommand, TRemConClientId aClientId, RBuf8& aData)
       
   285 	{
       
   286 	LOG_FUNC;
       
   287 	RRemConUidsChangedResponse response;
       
   288 	TRAPD(err, response.ReadL(aData));
       
   289 	aData.Close(); // data has been used now
       
   290 
       
   291 	if(!err)
       
   292 		{
       
   293 		iObserver.MuoUidChanged(aClientId, response.iUidCounter);
       
   294 		SendUidUpdateRequest(aCommand, aClientId, response.iUidCounter);
       
   295 		}
       
   296 	else
       
   297 		{
       
   298 		// Should never get here with a valid player.  This client is
       
   299 		// sending us junk.
       
   300 		iObserver.MuoError(aClientId);
       
   301 		}
       
   302 	}
       
   303 
       
   304 void CUidWatcher::ReceiveReject(TRemConClientId aClientId)
       
   305 	{
       
   306 	LOG_FUNC;
       
   307 	iObserver.MuoError(aClientId);
       
   308 	}
       
   309 
       
   310 
       
   311