remotecontrol/remotecontrolfw/server/src/targetclientprocess.cpp
changeset 51 20ac952a623c
equal deleted inserted replaced
48:22de2e391156 51:20ac952a623c
       
     1 // Copyright (c) 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 // Server-side representation of a target client, which may have multiple 
       
    15 // sessions associated with it.
       
    16 // 
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <bluetooth/logger.h>
       
    21 
       
    22 #include "server.h"
       
    23 #include "bearermanager.h"
       
    24 #include "remconmessage.h"
       
    25 #include "targetsession.h"
       
    26 
       
    27 #include "targetclientprocess.h"
       
    28 
       
    29 #ifdef __FLOG_ACTIVE
       
    30 _LIT8(KLogComponent, LOG_COMPONENT_REMCON_SERVER);
       
    31 #endif
       
    32 
       
    33 #ifdef _DEBUG
       
    34 PANICCATEGORY("tgclient");
       
    35 #endif
       
    36 
       
    37 TBool TargetSessionCompareUsingSupportedInterface(const TUid* aInterfaceUid, const CRemConTargetSession& aSession)
       
    38 	{
       
    39 	return aSession.InterfaceSupported(*aInterfaceUid);
       
    40 	}
       
    41 
       
    42 TBool TargetSessionCompareUsingSupportedMessage(const CRemConMessage* aMessage, const CRemConTargetSession& aSession)
       
    43 	{
       
    44 	return aSession.SupportedMessage(*aMessage);
       
    45 	}
       
    46 
       
    47 CRemConTargetClientProcess* CRemConTargetClientProcess::NewLC(TClientInfo& aClientInfo, TRemConClientId aClientId, CRemConServer& aServer, CBearerManager& aBearerManager)
       
    48 	{
       
    49 	LOG_STATIC_FUNC
       
    50 
       
    51 	CRemConTargetClientProcess* result = new (ELeave) CRemConTargetClientProcess(aClientInfo, aClientId, aServer, aBearerManager);
       
    52 	CleanupStack::PushL(result);
       
    53 	return result;
       
    54 	}
       
    55 
       
    56 CRemConTargetClientProcess::CRemConTargetClientProcess(TClientInfo& aClientInfo, TRemConClientId aClientId, CRemConServer& aServer, CBearerManager& aBearerManager)
       
    57 	: iClientInfo(aClientInfo),
       
    58 	iClientId(aClientId),
       
    59 	iServer(aServer),
       
    60 	iBearerManager(aBearerManager),
       
    61 	iPlayerInfoSet(EFalse),
       
    62 	iTargetSessions(KMaxNumberTargetSessions)
       
    63 	{
       
    64 	LOG_FUNC
       
    65 	}
       
    66 
       
    67 CRemConTargetClientProcess::~CRemConTargetClientProcess()
       
    68 	{
       
    69 	LOG_FUNC
       
    70 
       
    71 	iPlayerName.Close();
       
    72 	iTargetSessions.Close();
       
    73 	iServer.TargetClientClosed(*this);
       
    74 	}
       
    75 
       
    76 CRemConTargetSession* CRemConTargetClientProcess::NewSessionL(TUint aSessionId)
       
    77 	{
       
    78 	LOG_FUNC
       
    79 	CRemConTargetSession* sess = NULL;
       
    80 
       
    81 	// Only create a session if we havent reached our maximum.
       
    82 	if (TargetSessionCount() < KMaxNumberTargetSessions)
       
    83 		{
       
    84 		sess = CRemConTargetSession::NewL(*this, iServer, iBearerManager, aSessionId);
       
    85 		}
       
    86 	else
       
    87 		{
       
    88 		LEAVEIFERRORL(KErrOverflow);
       
    89 		}
       
    90 
       
    91 	return sess;
       
    92 	}
       
    93 
       
    94 
       
    95 TInt CRemConTargetClientProcess::TargetSessionOpened(CRemConTargetSession& aSession)
       
    96 	{
       
    97 	LOG_FUNC
       
    98 	LOG1(_L("\t&aSession = 0x%08x"), &aSession)
       
    99 
       
   100 	// Register the session by appending it to our array and asking the server to
       
   101 	// make an item for it in the record of which points in the connection history 
       
   102 	// sessions are interested in.
       
   103 	ASSERT(iTargetSessions.Count() < KMaxNumberTargetSessions);	// Should have been caught by now.
       
   104 
       
   105 	// The append should never fail because the RPointerArray was constructed with a granularity of 
       
   106 	// KMaxNumberTargetSessions items.
       
   107 #ifdef _DEBUG	
       
   108 	TInt err = iTargetSessions.Append(&aSession);
       
   109 	ASSERT(err == KErrNone);
       
   110 #else
       
   111 	static_cast<void>(iTargetSessions.Append(&aSession));
       
   112 #endif
       
   113 
       
   114 	TInt ret = iServer.RegisterTargetSessionPointerToConnHistory(aSession);
       
   115 			
       
   116 	if ( ret != KErrNone )
       
   117 		{
       
   118 		iTargetSessions.Remove(iTargetSessions.Count() - 1);
       
   119 		}
       
   120 	else
       
   121 		{
       
   122 		// Session successfully registered.
       
   123 		iServer.CancelShutdownTimer();
       
   124 		}
       
   125 	
       
   126 	LOG1(_L("\tret = %d"), ret)
       
   127 	return ret;
       
   128 	}
       
   129 
       
   130 void CRemConTargetClientProcess::TargetSessionClosed(CRemConTargetSession& aSession)
       
   131 	{
       
   132 	LOG_FUNC
       
   133 	LOG1(_L("\t&aSession = 0x%08x"), &aSession)
       
   134 
       
   135 	// Remove session from our list
       
   136 	TInt sessionIndex = iTargetSessions.Find(&aSession);
       
   137 
       
   138 	if (sessionIndex > KErrNotFound)
       
   139 		{
       
   140 		iTargetSessions.Remove(sessionIndex);
       
   141 
       
   142 		// Inform server that session has dropped. 			
       
   143 		iServer.TargetSessionClosed(*this, aSession);
       
   144 		
       
   145 		if (iTargetSessions.Count() > 0)
       
   146 		    {
       
   147             iServer.TargetFeaturesUpdated(*this);
       
   148 		    }
       
   149 		
       
   150 		}
       
   151 	}
       
   152 
       
   153 void CRemConTargetClientProcess::SetPlayerInformationL(const TPlayerTypeInformation& aPlayerType, const TDesC8& aPlayerName)
       
   154 	{
       
   155 	LOG_FUNC
       
   156 	ASSERT_DEBUG(!HasPlayerInformation());
       
   157 
       
   158 	if (!HasPlayerInformation())
       
   159 		{
       
   160 		iPlayerName.CreateL(aPlayerName);
       
   161 		iPlayerType = aPlayerType;
       
   162 		iPlayerInfoSet = ETrue;
       
   163 		}
       
   164 	}
       
   165 
       
   166 void CRemConTargetClientProcess::InterfacesRegistered()
       
   167 	{
       
   168 	LOG_FUNC
       
   169 
       
   170 	// If this client has not been made available yet, do so now.
       
   171 	// Otherwise, notify the server that new interfaces have been registered. 
       
   172 	if (!iClientAvailable)
       
   173 		{
       
   174 		iServer.TargetClientAvailable(*this);	
       
   175 		iClientAvailable = ETrue;
       
   176 		}
       
   177 	else
       
   178 		{
       
   179 		iServer.TargetFeaturesUpdated(*this);
       
   180 		}
       
   181 
       
   182 	}
       
   183 
       
   184 TBool CRemConTargetClientProcess::IsInterfaceTypeRegisteredByAnotherSession(CRemConTargetSession& aSession, TUid aInterfaceUid) const
       
   185 	{
       
   186 	LOG_FUNC
       
   187 
       
   188 	TUint sessionCount = iTargetSessions.Count();
       
   189 	for (TUint i=0; i < sessionCount; ++i)
       
   190 		{
       
   191 		if (aSession.Id() != iTargetSessions[i]->Id())
       
   192 			{
       
   193 			if (iTargetSessions[i]->InterfaceSupported(aInterfaceUid))
       
   194 				{
       
   195 				return ETrue;
       
   196 				}
       
   197 			}
       
   198 		}
       
   199 
       
   200 	return EFalse;
       
   201 	}
       
   202 
       
   203 TInt CRemConTargetClientProcess::ReceiveMessage(CRemConMessage& aMessage)
       
   204 	{
       
   205 	LOG_FUNC
       
   206 
       
   207 	// Find the session supporting this message
       
   208 	CRemConTargetSession* sess = FindSessionForMessage(aMessage);
       
   209 
       
   210 	if (sess)
       
   211 		{
       
   212 		// Session found. Check session is able to handle this message.
       
   213 		if (sess->CurrentReceiveMessage().Handle())
       
   214 			{
       
   215 			// Pass message to session and return the error code obtained.
       
   216 			return sess->WriteMessageToClient(aMessage);
       
   217 			}
       
   218 		else
       
   219 			{
       
   220 			// Session not able to handle message at this time.
       
   221 			return KErrNotReady;
       
   222 			}
       
   223 		}
       
   224 	else
       
   225 		{
       
   226 		// No session supports this message
       
   227 		return KErrArgument;
       
   228 		}
       
   229 	}
       
   230 
       
   231 void CRemConTargetClientProcess::ConnectionsChanged()
       
   232 	{
       
   233 	LOG_FUNC
       
   234 
       
   235 	// Notify each session
       
   236 	TUint sessionCount = iTargetSessions.Count();
       
   237 	for (TUint i = 0; i < sessionCount; ++i)
       
   238 		{
       
   239 		iTargetSessions[i]->ConnectionsChanged();
       
   240 		}
       
   241 	}
       
   242 
       
   243 TInt CRemConTargetClientProcess::SupportedInterfaces(RArray<TUid>& aUids)
       
   244 	{
       
   245 	LOG_FUNC
       
   246 	
       
   247 	TInt err = KErrNone;
       
   248 	aUids.Reset();
       
   249 
       
   250 	// Gather the list of supported interfaces from each session.
       
   251 	TUint sessionCount = iTargetSessions.Count();
       
   252 	for (TUint i = 0 ; i < sessionCount && err == KErrNone; ++i)
       
   253 		{
       
   254 		err = iTargetSessions[i]->AppendSupportedInterfaces(aUids);
       
   255 		}
       
   256 
       
   257 	return err;
       
   258 	}
       
   259 
       
   260 TInt CRemConTargetClientProcess::SupportedBulkInterfaces(RArray<TUid>& aUids)
       
   261 	{
       
   262 	LOG_FUNC
       
   263 	
       
   264 	TInt err = KErrNone;
       
   265 	aUids.Reset();
       
   266 
       
   267 	// Gather the list of supported bulk interfaces from each session.
       
   268 	TUint sessionCount = iTargetSessions.Count();
       
   269 	for (TUint i = 0 ; i < sessionCount && err == KErrNone; ++i)
       
   270 		{
       
   271 		err = iTargetSessions[i]->AppendSupportedBulkInterfaces(aUids);
       
   272 		}
       
   273 
       
   274 	return err;
       
   275 	}
       
   276 
       
   277 TInt CRemConTargetClientProcess::SupportedOperations(TUid aInterfaceUid, RArray<TUint>& aOperations)
       
   278 	{
       
   279 	LOG_FUNC
       
   280 
       
   281 	// Find the session supporting this interface.
       
   282 	TInt sessIndex = FindSessionForInterface(aInterfaceUid);
       
   283 
       
   284 	if (sessIndex >= KErrNone)
       
   285 		{
       
   286 		return iTargetSessions[sessIndex]->SupportedOperations(aInterfaceUid, aOperations);
       
   287 		}
       
   288 	else
       
   289 		{
       
   290 		// Interface not found, so operation not supported.
       
   291 		return KErrNotSupported;
       
   292 		}
       
   293 	}
       
   294 
       
   295 CRemConTargetSession* CRemConTargetClientProcess::FindSessionForMessage(const CRemConMessage& aMessage)
       
   296 	{
       
   297 	LOG_FUNC
       
   298 
       
   299 	TInt result = iTargetSessions.Find(aMessage, TargetSessionCompareUsingSupportedMessage);
       
   300 	if (result >= KErrNone)
       
   301 		{
       
   302 		return iTargetSessions[result];
       
   303 		}
       
   304 	else
       
   305 		{
       
   306 		return NULL;
       
   307 		}
       
   308 
       
   309 	}
       
   310 
       
   311 TInt CRemConTargetClientProcess::FindSessionForInterface(TUid aInterfaceUid) const
       
   312 	{
       
   313 	LOG_FUNC
       
   314 
       
   315 	return iTargetSessions.Find(aInterfaceUid, TargetSessionCompareUsingSupportedInterface);
       
   316 	}
       
   317 
       
   318 void CRemConTargetClientProcess::CompleteMessageForSession(const CRemConMessage& aMessage, CRemConTargetSession& aSession)
       
   319 	{
       
   320 	LOG_FUNC
       
   321 
       
   322 	// Targets can only send responses or rejects.
       
   323 	switch (aMessage.MsgType())
       
   324 		{
       
   325 	case ERemConResponse:
       
   326 	case ERemConReject:
       
   327 		aSession.CompleteSend();
       
   328 		break;
       
   329 	default:
       
   330 		ASSERT_DEBUG(EFalse);
       
   331 		break;
       
   332 		}
       
   333 
       
   334 	}
       
   335 
       
   336 void CRemConTargetClientProcess::MrcmsoMessageSendResult(const CRemConMessage& /*aMessage*/, TInt /*aError*/)
       
   337 	{
       
   338 	LOG_FUNC
       
   339 	
       
   340 	// This method should never be called, as it is not required to support target client processes.
       
   341 	ASSERT_DEBUG(EFalse);
       
   342 	}
       
   343 
       
   344 void CRemConTargetClientProcess::MrcmsoMessageSendOneOrMoreAttempt(const CRemConMessage& aMessage, TUint aNumRemotes)
       
   345 	{
       
   346 	LOG_FUNC
       
   347 
       
   348 	// Notifications should not be received for reject messages, as the client did not request these be sent.
       
   349 	ASSERT_DEBUG(aMessage.MsgType() != ERemConReject);
       
   350 	
       
   351 	// Find session and notify
       
   352 	CRemConTargetSession* sess = FindSessionForMessage(aMessage);
       
   353 	ASSERT_DEBUG(sess);
       
   354 	
       
   355 	// Session should not already be sending a message to n remotes
       
   356 	ASSERT_DEBUG(sess->NumRemotesToTry() == 0);
       
   357 
       
   358 	sess->NumRemotes() = 0;
       
   359 	sess->NumRemotesToTry() = aNumRemotes;
       
   360 	sess->SendError() = KErrNone;
       
   361 	}
       
   362 
       
   363 void CRemConTargetClientProcess::MrcmsoMessageSendOneOrMoreIncremental(const CRemConMessage& aMessage, TUint aNumRemotes)
       
   364 	{
       
   365 	LOG_FUNC
       
   366 
       
   367 	// Notifications should not be received for reject messages, as the client did not request these be sent.
       
   368 	ASSERT_DEBUG(aMessage.MsgType() != ERemConReject);
       
   369 	
       
   370 	// Find session and notify
       
   371 	CRemConTargetSession* sess = FindSessionForMessage(aMessage);
       
   372 	ASSERT_DEBUG(sess);
       
   373 
       
   374 	if (sess->NumRemotesToTry() == 0)
       
   375 		{
       
   376 		MrcmsoMessageSendOneOrMoreAttempt(aMessage,aNumRemotes);
       
   377 		}
       
   378 	else
       
   379 		{
       
   380 		// No send should have yet been attempted
       
   381 		ASSERT_DEBUG(sess->NumRemotes() == 0);
       
   382 
       
   383 		sess->NumRemotesToTry() += aNumRemotes;
       
   384 		}
       
   385 	}
       
   386 	
       
   387 void CRemConTargetClientProcess::MrcmsoMessageSendOneOrMoreAttemptFailed(const CRemConMessage& aMessage, TInt aError)
       
   388 	{
       
   389 	LOG_FUNC
       
   390 
       
   391 	// Notifications should not be received for reject messages, as the client did not request these be sent.
       
   392 	ASSERT_DEBUG(aMessage.MsgType() != ERemConReject);
       
   393 	
       
   394 	// Find session and notify
       
   395 	CRemConTargetSession* sess = FindSessionForMessage(aMessage);
       
   396 	ASSERT_DEBUG(sess);
       
   397 	
       
   398 	// Session should not already be sending a message to n remotes
       
   399 	ASSERT_DEBUG(sess->NumRemotesToTry() == 0);
       
   400 
       
   401 	sess->NumRemotes() = 0;
       
   402 	sess->SendError() = aError;
       
   403 	CompleteMessageForSession(aMessage, *sess);
       
   404 	}
       
   405 
       
   406 void CRemConTargetClientProcess::MrcmsoMessageSendOneOrMoreResult(const CRemConMessage& aMessage, TInt aError)
       
   407 	{
       
   408 	LOG_FUNC
       
   409 
       
   410 	// Notifications should not be received for reject messages, as the client did not request these be sent.
       
   411 	ASSERT_DEBUG(aMessage.MsgType() != ERemConReject);
       
   412 	
       
   413 	// Find session and notify
       
   414 	CRemConTargetSession* sess = FindSessionForMessage(aMessage);
       
   415 	ASSERT_DEBUG(sess);
       
   416 	
       
   417 	// Ignore notification if client has been completed
       
   418 	if (sess->NumRemotesToTry() > 0)
       
   419 		{
       
   420 		// Only set error if different from KErrNone
       
   421 		if (aError == KErrNone)
       
   422 			{
       
   423 			++sess->NumRemotes();
       
   424 			}
       
   425 		else
       
   426 			{
       
   427 			sess->SendError() = aError;
       
   428 			}
       
   429 
       
   430 		--sess->NumRemotesToTry();
       
   431 		if (sess->NumRemotesToTry() == 0)
       
   432 			{
       
   433 			CompleteMessageForSession(aMessage, *sess);
       
   434 			}
       
   435 		}
       
   436 	}
       
   437 
       
   438 void CRemConTargetClientProcess::MrcmsoMessageSendOneOrMoreAbandoned(const CRemConMessage& aMessage)
       
   439 	{
       
   440 	LOG_FUNC
       
   441 	
       
   442 	// Notifications should not be received for reject messages, as the client did not request these be sent.
       
   443 	ASSERT_DEBUG(aMessage.MsgType() != ERemConReject);
       
   444 
       
   445 	// Find session and notify
       
   446 	CRemConTargetSession* sess = FindSessionForMessage(aMessage);
       
   447 	ASSERT_DEBUG(sess);
       
   448 	
       
   449 	// Ignore notification if client has been completed
       
   450 	if (sess->NumRemotesToTry() > 0)
       
   451 		{
       
   452 		// Do not adjust NumRemotes() as the message was not sent (but we still don't error the client!)
       
   453 		--sess->NumRemotesToTry();
       
   454 		if (sess->NumRemotesToTry() == 0)
       
   455 			{
       
   456 			CompleteMessageForSession(aMessage, *sess);
       
   457 			}
       
   458 		}
       
   459 	}