accessoryservices/remotecontrolfw/server/src/session.cpp
changeset 74 9d35fd98f273
parent 0 4e1aa6a622a0
equal deleted inserted replaced
69:dc67b94625c5 74:9d35fd98f273
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 2004-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    28 #include "utils.h"
    28 #include "utils.h"
    29 #include "server.h"
    29 #include "server.h"
    30 #include "bearermanager.h"
    30 #include "bearermanager.h"
    31 #include "remconmessage.h"
    31 #include "remconmessage.h"
    32 #include "connections.h"
    32 #include "connections.h"
    33 #include "activehelper.h"
       
    34 #include "session.h"
    33 #include "session.h"
    35 #include "messagequeue.h"
    34 #include "messagequeue.h"
    36 #include "remconserver.h"
    35 #include "remconserver.h"
    37 
    36 
    38 #ifdef __FLOG_ACTIVE
    37 #ifdef __FLOG_ACTIVE
    41 
    40 
    42 #ifdef _DEBUG
    41 #ifdef _DEBUG
    43 PANICCATEGORY("session");
    42 PANICCATEGORY("session");
    44 #endif
    43 #endif
    45 
    44 
    46 CRemConSession* CRemConSession::NewL(CRemConServer& aServer,
       
    47 		CBearerManager& aBearerManager,
       
    48 		const RMessage2& aMessage,
       
    49 		TUint aId)
       
    50 	{
       
    51 	LOG_STATIC_FUNC
       
    52 	CRemConSession* self = new(ELeave) CRemConSession(aServer, aBearerManager, aId);
       
    53 	CleanupStack::PushL(self);
       
    54 	self->ConstructL(aMessage);
       
    55 	CLEANUPSTACK_POP1(self);
       
    56 	return self;
       
    57 	}
       
    58 
    45 
    59 CRemConSession::CRemConSession(CRemConServer& aServer,
    46 CRemConSession::CRemConSession(CRemConServer& aServer,
    60 		CBearerManager& aBearerManager,
    47 		CBearerManager& aBearerManager,
    61 		TUint aId)
    48 		TUint aId)
    62  :	iServer(aServer),
    49  :	iServer(aServer),
    63 	iBearerManager(aBearerManager),
    50 	iBearerManager(aBearerManager),
    64 	iId(aId)
    51 	iId(aId)
    65 	{
    52 	{
    66 	LOG_FUNC
    53 	LOG_FUNC;
    67 	}
    54 	}
    68 
    55 
    69 void CRemConSession::ConstructL(const RMessage2& aMessage)
    56 void CRemConSession::BaseConstructL(const TClientInfo& aClientInfo)
    70 	{
    57 	{
    71 	LOG_FUNC;
    58 	LOG_FUNC;
    72 
    59 
    73 	// Get the client's process ID.
    60 	iClientInfo = aClientInfo;
    74 	RThread thread;
       
    75 	LEAVEIFERRORL(aMessage.Client(thread));
       
    76 	CleanupClosePushL(thread);
       
    77 	RProcess process;
       
    78 	LEAVEIFERRORL(thread.Process(process));
       
    79 	iClientInfo.ProcessId() = process.Id();
       
    80 	process.Close();
       
    81 	iClientInfo.SecureId() = thread.SecureId();
       
    82 	CleanupStack::PopAndDestroy(&thread);
       
    83 
    61 
    84 	iSendQueue = CMessageQueue::NewL();
    62 	iSendQueue = CMessageQueue::NewL();
    85 	
    63 
       
    64 	// The send callback is used by the base class to handle queued sends.
       
    65 	iSendNextCallBack = new(ELeave) CAsyncCallBack(CActive::EPriorityStandard);
    86 	TCallBack cb(SendNextCb, this);
    66 	TCallBack cb(SendNextCb, this);
    87 	
    67 	iSendNextCallBack->Set(cb);
    88 	iSendNextCallBack = new(ELeave) CAsyncCallBack(cb, CActive::EPriorityStandard);
    68 
    89 	
       
    90 	// Tell the server about us.
       
    91 	LEAVEIFERRORL(iServer.ClientOpened(*this));
       
    92 
       
    93 	// Set our pointer into the connection history at the current/'Last' item.
       
    94 	iServer.SetConnectionHistoryPointer(Id());
       
    95 	
       
    96 	iPendingMsgProcessor = new (ELeave) CActiveHelper(*this);
       
    97 	}
    69 	}
    98 
    70 
    99 CRemConSession::~CRemConSession()
    71 CRemConSession::~CRemConSession()
   100 	{
    72 	{
   101 	LOG(KNullDesC8);
    73 	LOG(KNullDesC8);
   102 	LOG_FUNC;
    74 	LOG_FUNC;
   103 	
    75 	
   104 	delete iPendingMsgProcessor;
       
   105 	delete iSendNextCallBack;
    76 	delete iSendNextCallBack;
   106 	delete iSendQueue;
    77 	delete iSendQueue;
   107 	// we will need to tell the server which bearer this used to be connected to
       
   108 	// this enables the server to not inform a bearer that is already connected
       
   109 	// that its been connected
       
   110 	// Tell the server we've gone away- it may start its shutdown timer.
       
   111 	iServer.ClientClosed(*this, iRemoteAddress.BearerUid());
       
   112 	delete iInterestedAPIs;
    78 	delete iInterestedAPIs;
   113 	iPlayerName.Close();
       
   114 	}
    79 	}
   115 
    80 
   116 void CRemConSession::ServiceL(const RMessage2& aMessage)
    81 void CRemConSession::ServiceL(const RMessage2& aMessage)
   117 	{
    82 	{
   118 	LOG(KNullDesC8);
    83 	LOG(KNullDesC8);
   119 	LOG_FUNC;
    84 	LOG_FUNC;
   120 	LOG1(_L("\taMessage.Function() = %d"), aMessage.Function());
    85 	LOG1(_L("\taMessage.Function() = %d"), aMessage.Function());
   121 
       
   122 	// Switch on the IPC number and call a 'message handler'. Message handlers 
    86 	// Switch on the IPC number and call a 'message handler'. Message handlers 
   123 	// complete aMessage (either with Complete or Panic), or make a note of 
    87 	// complete aMessage (either with Complete or Panic), or make a note of 
   124 	// the message for later asynchronous completion.
    88 	// the message for later asynchronous completion.
   125 	// Message handlers should not leave- the server does not have an Error 
    89 	// Message handlers should not leave- the server does not have an Error 
   126 	// function. 
    90 	// function. 
   167 		}
   131 		}
   168 #endif // _DEBUG
   132 #endif // _DEBUG
   169 		CompleteClient(aMessage, KErrNone);
   133 		CompleteClient(aMessage, KErrNone);
   170 		break;
   134 		break;
   171 
   135 
   172 	case ERemConSetClientType:
   136 	case ERemConSetPlayerType:
   173 		SetClientType(aMessage);
   137 		SetPlayerType(aMessage);
   174 		// This is a sync API- check that the message has been completed.
   138 		// This is a sync API- check that the message has been completed.
   175 		// (NB We don't check the converse for async APIs because the message 
   139 		// (NB We don't check the converse for async APIs because the message 
   176 		// may have been panicked synchronously.)
   140 		// may have been panicked synchronously.)
   177 		ASSERT_DEBUG(aMessage.IsNull());
   141 		ASSERT_DEBUG(aMessage.IsNull());
   178 		break;
   142 		break;
   179 
   143 
       
   144 	case ERemConSend:
       
   145 		Send(aMessage);
       
   146 		break;
       
   147 				
       
   148 	case ERemConSendUnreliable:
       
   149 		SendUnreliable(aMessage);
       
   150 		break;
       
   151 	
       
   152 	case ERemConSendCancel:
       
   153 		SendCancel(aMessage);
       
   154 		ASSERT_DEBUG(aMessage.IsNull());
       
   155 		break;
       
   156 
       
   157 	case ERemConReceive:
       
   158 		Receive(aMessage);
       
   159 		break;
       
   160 
       
   161 	case ERemConReceiveCancel:
       
   162 		ReceiveCancel(aMessage);
       
   163 		ASSERT_DEBUG(aMessage.IsNull());
       
   164 		break;
       
   165 
       
   166 	case ERemConGetConnectionCount:
       
   167 		GetConnectionCount(aMessage);
       
   168 		ASSERT_DEBUG(aMessage.IsNull());
       
   169 		break;
       
   170 
       
   171 	case ERemConGetConnections:
       
   172 		GetConnections(aMessage);
       
   173 		ASSERT_DEBUG(aMessage.IsNull());
       
   174 		break;
       
   175 
       
   176 	case ERemConNotifyConnectionsChange:
       
   177 		NotifyConnectionsChange(aMessage);
       
   178 		break;
       
   179 
       
   180 	case ERemConNotifyConnectionsChangeCancel:
       
   181 		NotifyConnectionsChangeCancel(aMessage);
       
   182 		ASSERT_DEBUG(aMessage.IsNull());
       
   183 		break;
       
   184 		
       
   185 	case ERemConRegisterInterestedAPIs:
       
   186 		RegisterInterestedAPIs(aMessage);
       
   187 		ASSERT_DEBUG(aMessage.IsNull());
       
   188 		break;
       
   189 		
   180 	case ERemConGoConnectionOriented:
   190 	case ERemConGoConnectionOriented:
   181 		GoConnectionOriented(aMessage);
   191 		GoConnectionOriented(aMessage);
   182 		ASSERT_DEBUG(aMessage.IsNull());
   192 		ASSERT_DEBUG(aMessage.IsNull());
   183 		break;
   193 		break;
   184 
   194 
   201 		break;
   211 		break;
   202 
   212 
   203 	case ERemConDisconnectBearerCancel:
   213 	case ERemConDisconnectBearerCancel:
   204 		DisconnectBearerCancel(aMessage);
   214 		DisconnectBearerCancel(aMessage);
   205 		ASSERT_DEBUG(aMessage.IsNull());
   215 		ASSERT_DEBUG(aMessage.IsNull());
   206 		break;
       
   207 
       
   208 	case ERemConSend:
       
   209 		Send(aMessage);
       
   210 		break;
   216 		break;
   211 		
   217 		
   212 	case ERemConSendNotify:
   218 	case ERemConSendNotify:
   213 		SendNotify(aMessage);
   219 		SendNotify(aMessage);
   214 		break;
       
   215 		
       
   216 	case ERemConSendUnreliable:
       
   217 		SendUnreliable(aMessage);
       
   218 		break;
       
   219 	
       
   220 	case ERemConSendCancel:
       
   221 		SendCancel(aMessage);
       
   222 		ASSERT_DEBUG(aMessage.IsNull());
       
   223 		break;
       
   224 
       
   225 	case ERemConReceive:
       
   226 		Receive(aMessage);
       
   227 		break;
       
   228 
       
   229 	case ERemConReceiveCancel:
       
   230 		ReceiveCancel(aMessage);
       
   231 		ASSERT_DEBUG(aMessage.IsNull());
       
   232 		break;
       
   233 
       
   234 	case ERemConGetConnectionCount:
       
   235 		GetConnectionCount(aMessage);
       
   236 		ASSERT_DEBUG(aMessage.IsNull());
       
   237 		break;
       
   238 
       
   239 	case ERemConGetConnections:
       
   240 		GetConnections(aMessage);
       
   241 		ASSERT_DEBUG(aMessage.IsNull());
       
   242 		break;
       
   243 
       
   244 	case ERemConNotifyConnectionsChange:
       
   245 		NotifyConnectionsChange(aMessage);
       
   246 		break;
       
   247 
       
   248 	case ERemConNotifyConnectionsChangeCancel:
       
   249 		NotifyConnectionsChangeCancel(aMessage);
       
   250 		ASSERT_DEBUG(aMessage.IsNull());
       
   251 		break;
       
   252 		
       
   253 	case ERemConRegisterInterestedAPIs:
       
   254 		RegisterInterestedAPIs(aMessage);
       
   255 		ASSERT_DEBUG(aMessage.IsNull());
       
   256 		break;
   220 		break;
   257 		
   221 		
   258 	default:
   222 	default:
   259 		// Unknown message
   223 		// Unknown message
   260 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicIllegalIpc);
   224 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicIllegalIpc);
   271 		{
   235 		{
   272 		iClientInfo.Message() = RMessage2();
   236 		iClientInfo.Message() = RMessage2();
   273 		}
   237 		}
   274 	}
   238 	}
   275 
   239 
   276 void CRemConSession::SetClientType(const RMessage2& aMessage)
   240 void CRemConSession::GetPlayerTypeAndNameL(const RMessage2& aMessage, TPlayerTypeInformation& aPlayerType, RBuf8& aPlayerName)
   277 	{
   241 	{
   278 	LOG_FUNC;
   242 	// check validity of descriptors
   279 
   243 	if (aMessage.GetDesLength(1) < 0 || aMessage.GetDesLength(2) < 0)
   280 	if ( iType != ERemConClientTypeUndefined )
   244 		{
   281 		{
   245 		LEAVEL(KErrBadDescriptor);
   282 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientTypeAlreadySet);
   246 		}
   283 		return;
   247 
   284 		}
   248 	// Retrieve and validate the client type information 
   285 
   249 	TPckg<TPlayerTypeInformation> pckg(aPlayerType);
   286 	const TRemConClientType type = static_cast<TRemConClientType>(aMessage.Int0());
   250 	aMessage.ReadL(1, pckg);
   287 	LOG1(_L("\trequested (TRemConClientType) type = %d"), type);
   251 	switch (aPlayerType.iPlayerType)
   288 	
   252 		{
   289 	TInt err = aMessage.GetDesLength(1);
   253 	case ERemConAudioPlayer:
   290 	if(err >= 0)
   254 		// Valid
   291 		{
   255 	case ERemConVideoPlayer:
   292 		TRAP(err, DoSetClientTypeL(aMessage));
   256 		// Valid
   293 		if(err == KErrBadDescriptor)
   257 	case ERemConBroadcastingAudioPlayer:
   294 			{
   258 		// Valid
   295 			PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
   259 	case ERemConBroadcastingVideoPlayer:
   296 			return;
   260 		// Valid
   297 			}
   261 		break;
   298 		}
   262 	default:
   299 	else if(err == KErrBadDescriptor)
   263 		// Invalid
   300 		{
   264 		LEAVEL(KErrArgument);
   301 		// The additional parameters are optional (i.e. old or controller clients won't provide them).
   265 		}
   302 		err = KErrNone;
   266 	switch (aPlayerType.iPlayerSubType)
   303 		}
   267 		{
   304 	
   268 	case ERemConNoSubType:
   305 	if(err != KErrNone)
   269 		// Valid
   306 		{
   270 	case ERemConAudioBook:
   307 		CompleteClient(aMessage, err);
   271 		// Valid
   308 		}
   272 	case ERemConPodcast:
   309 	else
   273 		// Valid
   310 		{
   274 		break;
   311 		switch ( type )
   275 	default:
   312 			{
   276 		// Invalid
   313 		case ERemConClientTypeController:
   277 		LEAVEL(KErrArgument);
   314 			iType = type;
   278 		}
   315 			CompleteClient(aMessage, KErrNone);
   279 
   316 			break;
       
   317 	
       
   318 		case ERemConClientTypeTarget:
       
   319 			// Check that there aren't already any target clients with the
       
   320 			// same process ID.
       
   321 			if ( !iServer.TargetClientWithSameProcessId(iClientInfo.ProcessId()) )
       
   322 				{
       
   323 				iType = type;
       
   324 				CompleteClient(aMessage, KErrNone);
       
   325 				}
       
   326 			else
       
   327 				{
       
   328 				CompleteClient(aMessage, KErrInUse);
       
   329 				}
       
   330 			break;
       
   331 	
       
   332 		default:
       
   333 			PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
       
   334 			break;
       
   335 			}
       
   336 		}
       
   337 
       
   338 	if ( iType != ERemConClientTypeUndefined )
       
   339 		{
       
   340 		// The type got set, so tell the server, so it can tell the bearers if 
       
   341 		// necessary. 
       
   342 		iServer.ClientTypeSet(*this);
       
   343 		}
       
   344 	}
       
   345 
       
   346 void CRemConSession::DoSetClientTypeL(const RMessage2& aMessage)
       
   347 	{
       
   348 	// Retrieve the client type information 
       
   349 	RBuf8 typeBuf;
       
   350 	typeBuf.CreateL(sizeof(TPlayerTypeInformation));
       
   351 	CleanupClosePushL(typeBuf);
       
   352 	aMessage.ReadL(1, typeBuf);
       
   353 			
       
   354 	const TPlayerTypeInformation* Ptr = reinterpret_cast<const TPlayerTypeInformation*> (typeBuf.Ptr());
       
   355 	iPlayerType.iPlayerType = (*Ptr).iPlayerType;
       
   356 	iPlayerType.iPlayerSubType = (*Ptr).iPlayerSubType;
       
   357 	// Retrieve the client player name inforamtion
   280 	// Retrieve the client player name inforamtion
   358 	iPlayerName.CreateL(aMessage.Int2());
   281 	aPlayerName.CreateL(aMessage.GetDesLengthL(2));
   359 	CleanupClosePushL(iPlayerName);
   282 	CleanupClosePushL(aPlayerName);
   360 	aMessage.ReadL(3, iPlayerName);	
   283 	aMessage.ReadL(2, aPlayerName);	
   361 	CleanupStack::Pop(&iPlayerName);
   284 	CleanupStack::Pop(&aPlayerName);
   362 	
       
   363 	CleanupStack::PopAndDestroy(&typeBuf); 
       
   364 	}
       
   365 void CRemConSession::GoConnectionOriented(const RMessage2& aMessage)
       
   366 	{
       
   367 	LOG_FUNC;
       
   368 
       
   369 	// Check we've had our type set...
       
   370 	if ( iType != ERemConClientTypeController )
       
   371 		{
       
   372 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
       
   373 		return;
       
   374 		}
       
   375 
       
   376 	if ( !iRemoteAddress.IsNull() )
       
   377 		{
       
   378 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicAlreadyConnectionOriented);
       
   379 		return;
       
   380 		}
       
   381 
       
   382 	if ( iConnectBearerMsg.Handle() || iDisconnectBearerMsg.Handle() || iSendMsg.Handle())
       
   383 		{
       
   384 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBearerControlOutstanding);
       
   385 		return;
       
   386 		}
       
   387 	if (iSending != ENotSending)
       
   388 		{
       
   389 		DoSendCancel();
       
   390 		}
       
   391 	EmptySendQueue();
       
   392 	
       
   393 	// Get the desired address from the message and check it.
       
   394 	const TUid uid = TUid::Uid(aMessage.Int0());
       
   395 	LOG1(_L("\tuid = 0x%08x"), uid);
       
   396 	// Check the requested bearer exists.
       
   397 	TBool bearerExists = iBearerManager.BearerExists(uid);
       
   398 	if ( !bearerExists)
       
   399 		{
       
   400 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBearerPluginIncorrectInterface);
       
   401 		return;
       
   402 		}
       
   403 	// Check the bearer-specific part of the address.
       
   404 	TBuf8<TRemConAddress::KMaxAddrSize> buf;
       
   405 	TInt err = aMessage.Read(1, buf);
       
   406 	if ( err != KErrNone )
       
   407 		{
       
   408 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
       
   409 		return;
       
   410 		}
       
   411 
       
   412 	// Do security check- if this client won't be allowed to use the bearer 
       
   413 	// then fail the request. 
       
   414 	// NB This security check (repeated in debug at ConnectBearer and 
       
   415 	// DisconnectBearer time) is all that stands between a connection-oriented 
       
   416 	// client and the bearer, and is all the caps checking that RemCon does!
       
   417 	err = KErrPermissionDenied;
       
   418 	if ( iBearerManager.CheckPolicy(uid, aMessage) )
       
   419 		{
       
   420 		err = KErrNone;
       
   421 		}
       
   422 		
       
   423 		
       
   424 	// if alls well and we're connection oriented then set up as such
       
   425 	if (KErrNone == err)
       
   426 		{
       
   427 		// The client has passed all our checks- set our data member.
       
   428 		iRemoteAddress.BearerUid() = uid;
       
   429 		iRemoteAddress.Addr() = buf;
       
   430 		// tell the server
       
   431 		iServer.ClientGoConnectionOriented(*this,uid);
       
   432 		}
       
   433 				
       
   434 	CompleteClient(aMessage, err);
       
   435 	}
       
   436 
       
   437 void CRemConSession::GoConnectionless(const RMessage2& aMessage)
       
   438 	{
       
   439 	LOG_FUNC;
       
   440 
       
   441 	// Check we've had our type set...
       
   442 	if ( iType != ERemConClientTypeController )
       
   443 		{
       
   444 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
       
   445 		return;
       
   446 		}
       
   447 
       
   448 	if ( iRemoteAddress.IsNull() )
       
   449 		{
       
   450 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicNotConnectionOriented);
       
   451 		return;
       
   452 		}
       
   453 
       
   454 	if ( iConnectBearerMsg.Handle() || iDisconnectBearerMsg.Handle() || iSendMsg.Handle())
       
   455 		{
       
   456 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBearerControlOutstanding);
       
   457 		return;
       
   458 		}
       
   459 	
       
   460 	if (iSending != ENotSending)
       
   461 		{
       
   462 		DoSendCancel();
       
   463 		}
       
   464 	EmptySendQueue();
       
   465 	
       
   466 	// we will need to tell the server which bearer this used to be connected to
       
   467 	// this enables the server to not inform a bearer that is already connected
       
   468 	// that its been connected
       
   469 	TUid oldUid = iRemoteAddress.BearerUid();
       
   470 	
       
   471 	iRemoteAddress.BearerUid() = KNullUid;	
       
   472 
       
   473 	// tell the server
       
   474 	iServer.ClientGoConnectionless(*this, oldUid);
       
   475 
       
   476 	CompleteClient(aMessage, KErrNone);
       
   477 	}
       
   478 
       
   479 void CRemConSession::ConnectBearer(const RMessage2& aMessage)
       
   480 	{
       
   481 	LOG_FUNC;
       
   482 
       
   483 	if ( iType != ERemConClientTypeController )
       
   484 		{
       
   485 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
       
   486 		return;
       
   487 		}
       
   488 
       
   489 	if ( iConnectBearerMsg.Handle() || iDisconnectBearerMsg.Handle() )
       
   490 		{
       
   491 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBearerControlOutstanding);
       
   492 		return;
       
   493 		}
       
   494 
       
   495 	if ( iRemoteAddress.IsNull() )
       
   496 		{
       
   497 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicNotConnectionOriented);
       
   498 		return;
       
   499 		}
       
   500 
       
   501 	// Check the requested bearer exists.
       
   502 	TBool bearerExists = iBearerManager.BearerExists(iRemoteAddress.BearerUid());
       
   503 	// This check was done at GoConnectionOriented time.
       
   504 	ASSERT_DEBUG(bearerExists);
       
   505 	// So was this one.
       
   506 	ASSERT_DEBUG(iBearerManager.CheckPolicy(iRemoteAddress.BearerUid(), aMessage));
       
   507 
       
   508 	// Check the state of our given connection at the bearer level. If it is: 
       
   509 	// -) disconnected request the connection to come up,
       
   510 	// -) connecting or disconnecting, add message to the queue of pending 
       
   511 	//		messages, and process it once connecting/disconnecting has been completed
       
   512 	// -) connected, complete the client's message,
       
   513 
       
   514 	TConnectionState conState;
       
   515 	conState = iServer.ConnectionState(iRemoteAddress);
       
   516 
       
   517 	if ( conState == EDisconnected )
       
   518 		{
       
   519 		// The bearer may indicate connection synchronously, so set this 
       
   520 		// message _before_ we ask them
       
   521 		iConnectBearerMsg = aMessage;
       
   522 		TInt err = iBearerManager.Connect(iRemoteAddress);
       
   523 		if ( err != KErrNone )
       
   524 			{
       
   525 			CompleteClient(iConnectBearerMsg, err);
       
   526 			}
       
   527 		}
       
   528 	else if ( conState == EDisconnecting ||  conState == EConnecting )
       
   529 		{
       
   530 		if ( iPendingMsg.Handle() )
       
   531 			{
       
   532 			PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBearerControlOutstanding);
       
   533 			return;
       
   534 			}
       
   535 		// Store the message, it will get processed later.
       
   536 		iPendingMsg = aMessage;
       
   537 		}
       
   538 	else // EConnected
       
   539 		{
       
   540 		CompleteClient(aMessage, KErrNone);
       
   541 		}
       
   542 		
       
   543 	}
       
   544 
       
   545 void CRemConSession::ConnectBearerCancel(const RMessage2& aMessage)
       
   546 	{
       
   547 	LOG_FUNC;
       
   548 
       
   549 	// Check we've had our type set...
       
   550 	if ( iType != ERemConClientTypeController )
       
   551 		{
       
   552 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
       
   553 		return;
       
   554 		}
       
   555 
       
   556 	if ( iConnectBearerMsg.Handle() )
       
   557 		{
       
   558 		CompleteClient(iConnectBearerMsg, KErrCancel);
       
   559 		}
       
   560 	else if ( iPendingMsg.Handle() && ( iPendingMsg.Function() == ERemConConnectBearer ))
       
   561 		{
       
   562 		CompleteClient(iPendingMsg, KErrCancel);
       
   563 		}
       
   564 		
       
   565 	CompleteClient(aMessage, KErrNone);
       
   566 	// At no point do we make any change to the processes going on underneath
       
   567 	// us- 'Cancel' APIs are just for cancelling interest in an async
       
   568 	// operation.
       
   569 	}
       
   570 
       
   571 void CRemConSession::DisconnectBearer(const RMessage2& aMessage)
       
   572 	{
       
   573 	LOG_FUNC;
       
   574 
       
   575 	if ( iType != ERemConClientTypeController )
       
   576 		{
       
   577 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
       
   578 		return;
       
   579 		}
       
   580 
       
   581 	if ( iDisconnectBearerMsg.Handle() || iConnectBearerMsg.Handle() || iSendMsg.Handle())
       
   582 		{
       
   583 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBearerControlOutstanding);
       
   584 		return;
       
   585 		}
       
   586 
       
   587 	if ( iRemoteAddress.IsNull() )
       
   588 		{
       
   589 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicNotConnectionOriented);
       
   590 		return;
       
   591 		}
       
   592 
       
   593 	if (iSending != ENotSending)
       
   594 		{
       
   595 		DoSendCancel();
       
   596 		}
       
   597 	EmptySendQueue();
       
   598 	
       
   599 	// Check the requested bearer exists.
       
   600 	TBool bearerExists = iBearerManager.BearerExists(iRemoteAddress.BearerUid());
       
   601 	// This check was done at GoConnectionOriented time.
       
   602 	ASSERT_DEBUG(bearerExists);
       
   603 	// So was this one.
       
   604 	ASSERT_DEBUG(iBearerManager.CheckPolicy(iRemoteAddress.BearerUid(), aMessage));
       
   605 
       
   606 	// Check the state of the given connection. If it is:
       
   607 	// -) connected, request connection to go away,
       
   608 	// -) disconnected, compete the client's message,
       
   609 	// -) connecting or disconnecting, add message to the queue of pending 
       
   610 	//		messages, and process it once connecting/disconnecting has been completed
       
   611 
       
   612 	TInt err;
       
   613 	TConnectionState conState;
       
   614 	conState = iServer.ConnectionState(iRemoteAddress);
       
   615 
       
   616 	if ( conState == EConnected )
       
   617 		{
       
   618 		// The bearer may indicate disconnection synchronously, so set this 
       
   619 		// message _before_ we ask them
       
   620 		iDisconnectBearerMsg = aMessage;
       
   621 		err = iBearerManager.Disconnect(iRemoteAddress);
       
   622 		if ( err != KErrNone )
       
   623 			{
       
   624 			CompleteClient(iDisconnectBearerMsg, err);
       
   625 			}
       
   626 		}
       
   627 	else if ( conState == EDisconnecting ||  conState == EConnecting )
       
   628 		{
       
   629 		if ( iPendingMsg.Handle() )
       
   630 			{
       
   631 			PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBearerControlOutstanding);
       
   632 			return;
       
   633 			}
       
   634 		// Store the message, it will get processed later.
       
   635 		iPendingMsg = aMessage;
       
   636 		}
       
   637 	else //disconnected
       
   638 		{
       
   639 		CompleteClient(aMessage, KErrNone);	
       
   640 		}
       
   641 	}
       
   642 
       
   643 void CRemConSession::DisconnectBearerCancel(const RMessage2& aMessage)
       
   644 	{
       
   645 	LOG_FUNC;
       
   646 
       
   647 	// Check we've had our type set...
       
   648 	if ( iType != ERemConClientTypeController )
       
   649 		{
       
   650 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
       
   651 		return;
       
   652 		}
       
   653 
       
   654 	if ( iDisconnectBearerMsg.Handle() )
       
   655 		{
       
   656 		CompleteClient(iDisconnectBearerMsg, KErrCancel);
       
   657 		}
       
   658 	else if ( iPendingMsg.Handle() && (iPendingMsg.Function() == ERemConDisconnectBearer ))
       
   659 		{
       
   660 		CompleteClient(iPendingMsg, KErrCancel);
       
   661 		}
       
   662 		
       
   663 	CompleteClient(aMessage, KErrNone);
       
   664 	}
   285 	}
   665 
   286 
   666 void CRemConSession::Send(const RMessage2& aMessage)
   287 void CRemConSession::Send(const RMessage2& aMessage)
   667 	{
   288 	{
   668 	LOG_FUNC;
   289 	LOG_FUNC;
   674 		return;
   295 		return;
   675 		}
   296 		}
   676 	
   297 	
   677 	iSendMsg = aMessage;
   298 	iSendMsg = aMessage;
   678 	
   299 	
   679 	// Check we've had our type set...
   300 	// Check we've had our features set...
   680 	if (	Type() != ERemConClientTypeController
   301 	if (!ClientAvailable())
   681 		&&	Type() != ERemConClientTypeTarget
   302 		{
   682 		)
   303 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientFeaturesNotSet);
   683 		{
   304 		return;
   684 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientTypeNotSet);
   305 		}
   685 		return;
       
   686 		}
       
   687 
       
   688 	// Check we don't have a disconnect outstanding- this makes no sense from 
       
   689 	// a client viewpoint (they should cancel the disconnect first).
       
   690 	// [The client is allowed to have a connect request outstanding- the 
       
   691 	// bearer manager makes sure a bearer-level connect is not posted on the 
       
   692 	// same address twice.]
       
   693 	if ( iDisconnectBearerMsg.Handle() )
       
   694 		{
       
   695 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBearerControlOutstanding);
       
   696 		return;
       
   697 		}
       
   698 
       
   699 	TRAPD(err, DoSendL(aMessage));
       
   700 	if ( err != KErrNone )
       
   701 		{
       
   702 		CompleteClient(aMessage, err);
       
   703 		}
       
   704 	}
       
   705 
       
   706 /**
       
   707 Sends a notify message to the remote device.
       
   708 
       
   709 This function is intended for the RemCon controller client to send a notify
       
   710 command to the remote device.
       
   711 */
       
   712 void CRemConSession::SendNotify(const RMessage2& aMessage)
       
   713 	{
       
   714 	LOG_FUNC;
       
   715 
       
   716 	// Check we're not already sending...
       
   717 	if ( iSendMsg.Handle())
       
   718 		{
       
   719 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicSendAlreadyOutstanding);
       
   720 		return;
       
   721 		}
       
   722 	
       
   723 	iSendMsg = aMessage;
       
   724 	
       
   725 	// Check we've had our type set...
       
   726 	if (Type() != ERemConClientTypeController)
       
   727 		{
       
   728 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientTypeNotSet);
       
   729 		return;
       
   730 		}
       
   731 
       
   732 	// Check we don't have a disconnect outstanding- this makes no sense from 
       
   733 	// a client viewpoint (they should cancel the disconnect first).
       
   734 	// [The client is allowed to have a connect request outstanding- the 
       
   735 	// bearer manager makes sure a bearer-level connect is not posted on the 
       
   736 	// same address twice.]
       
   737 	if ( iDisconnectBearerMsg.Handle() )
       
   738 		{
       
   739 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBearerControlOutstanding);
       
   740 		return;
       
   741 		}
       
   742 
       
   743 	TRAPD(err, DoSendNotifyL(aMessage));
       
   744 	if ( err != KErrNone )
       
   745 		{
       
   746 		CompleteClient(aMessage, err);
       
   747 		}
       
   748 	}
       
   749 
       
   750 void CRemConSession::DoSendL(const RMessage2& aMessage)
       
   751 	{
       
   752 	LOG_FUNC;
       
   753 
       
   754 	// Get the data the client wants to send.
       
   755 	const TUid interfaceUid = TUid::Uid(aMessage.Int0());
       
   756 	LOG1(_L("\tinterfaceUid = 0x%08x"), interfaceUid);
       
   757 
       
   758 	if (aMessage.GetDesLengthL(1) != sizeof(TOperationInformation))
       
   759 		{
       
   760 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
       
   761 		return;
       
   762 		}
       
   763 
       
   764 	TPckgBuf<TOperationInformation> opInfoPckg;
       
   765 	
       
   766 	TInt err= aMessage.Read(
       
   767 			1, // location of the descriptor in the client's message (as we expect them to have set it up)
       
   768 			opInfoPckg, // descriptor to write to from client memory space
       
   769 			0 // offset into our descriptor to put the client's data
       
   770 			);
       
   771 	
       
   772 	if ( err != KErrNone )
       
   773 		{
       
   774 		LOG1(_L("\taMessage.Read = %d"), err);
       
   775 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
       
   776 		return;
       
   777 		}	
       
   778 	
       
   779 	const TUint operationId = opInfoPckg().iOperationId;
       
   780 	LOG1(_L("\toperationId = 0x%02x"), operationId);
       
   781 	
       
   782 	const TRemConMessageSubType messageSubType = opInfoPckg().iMessageSubType;
       
   783 	LOG1(_L("\tmessageSubType = 0x%02x"), messageSubType);
       
   784 
       
   785 	
       
   786 	const TUint dataLength = (TUint)aMessage.GetDesLengthL(3);
       
   787 	LOG1(_L("\tdataLength = %d"), dataLength);
       
   788 	
       
   789 	// If the client wanted to send some operation-associated data, read it 
       
   790 	// from them.
       
   791 	RBuf8 sendDes;
       
   792 	if ( dataLength != 0 )
       
   793 		{
       
   794 		sendDes.CreateL(dataLength);
       
   795 		TInt err = aMessage.Read(
       
   796 			3, // location of the descriptor in the client's message (as we expect them to have set it up)
       
   797 			sendDes, // descriptor to write to from client memory space
       
   798 			0 // offset into our descriptor to put the client's data
       
   799 			);
       
   800 		// NB We don't do LEAVEIFERRORL(aMessage.Read) because a bad client 
       
   801 		// descriptor is a panicking offence for them, not an 'error the 
       
   802 		// request' offence.
       
   803 		if ( err != KErrNone )
       
   804 			{
       
   805 			LOG1(_L("\taMessage.Read = %d"), err);
       
   806 			sendDes.Close();
       
   807 			PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
       
   808 			return;
       
   809 			}
       
   810 		}
       
   811 	CleanupClosePushL(sendDes);
       
   812 
   306 
   813 	// Before we ask the server to send, we must set our ClientInfo 
   307 	// Before we ask the server to send, we must set our ClientInfo 
   814 	// correctly so the TSP can get information about the client. 
   308 	// correctly so the TSP can get information about the client. 
   815 	iClientInfo.Message() = aMessage;
   309 	iClientInfo.Message() = aMessage;
   816 
   310 
       
   311 	// Prepare the message for send. If DoPrepareSendMessageL() returns
       
   312 	// NULL, it panicked the client.
   817 	CRemConMessage* msg = NULL;
   313 	CRemConMessage* msg = NULL;
   818 	
   314 	TRAPD(err, msg = DoPrepareSendMessageL(aMessage));
   819 	// Examine the session type.
   315 
   820 	switch ( iType )
       
   821 		{
       
   822 	case ERemConClientTypeTarget:
       
   823 		{
       
   824 		LOG(_L("\tTARGET send"));
       
   825 
       
   826 		msg = CRemConMessage::NewL(
       
   827 			TRemConAddress(), // we don't know which remotes it's going to yet
       
   828 			ERemConResponse, // targets can only send responses
       
   829 			messageSubType,
       
   830 			interfaceUid,
       
   831 			operationId,
       
   832 			sendDes, // msg takes ownership
       
   833 			Id(), // session id to match this response against the originating command
       
   834 			0, // transaction id not yet known
       
   835 			ETrue);
       
   836 		CLEANUPSTACK_POP1(&sendDes); // now owned by msg
       
   837 		
       
   838 		}
       
   839 		break;
       
   840 
       
   841 	case ERemConClientTypeController:
       
   842 		{
       
   843 		LOG(_L("\tCONTROLLER send"));
       
   844         if (  (messageSubType == ERemConNotifyCommandAwaitingInterim)
       
   845            || (messageSubType == ERemConNotifyCommandAwaitingChanged)
       
   846             )
       
   847         	{
       
   848         	LOG(_L("\terror, not allowed to use Send() to send notify command"));
       
   849         	CleanupStack::PopAndDestroy(&sendDes);
       
   850         	PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicIllegalIpc);
       
   851         	return;
       
   852         	}
       
   853         else
       
   854         	{
       
   855         	msg = CRemConMessage::NewL(
       
   856         			iRemoteAddress, // either specified (if we're connection-oriented) or null (we're connectionless- this field will be filled in by the TSP)
       
   857         			ERemConCommand, 
       
   858         			messageSubType,
       
   859         			interfaceUid,
       
   860         			operationId,
       
   861         			sendDes, // msg takes ownership
       
   862         			Id(), // session id for when the response comes back
       
   863         			0, // we let the bearer manager invent a new transaction id when the message gets to it
       
   864         			ETrue);
       
   865         	}		
       
   866 		CLEANUPSTACK_POP1(&sendDes); // now owned by msg
       
   867 
       
   868 		}
       
   869 		break;
       
   870 
       
   871 	default:
       
   872 		DEBUG_PANIC_LINENUM;
       
   873 		break;
       
   874 		}
       
   875 	
       
   876 	ASSERT_DEBUG(iSendQueue);
       
   877 	// We know msg is valid at this stage as the code would leave or panic earlier if msg was 
       
   878 	// not set.
       
   879 	ASSERT_DEBUG(msg);
       
   880 	
       
   881 	if (iSending != ENotSending || !iSendQueue->IsEmpty())
       
   882 		{
       
   883 		iSendQueue->Append(*msg);
       
   884 		}
       
   885 	else
       
   886 		{
       
   887 		// we know msg cannot be null here as said above.
       
   888 		SendToServer(*msg);
       
   889 		}
       
   890 	}
       
   891 
       
   892 /**
       
   893 @see CRemConSession::SendNotify
       
   894 */
       
   895 void CRemConSession::DoSendNotifyL(const RMessage2& aMessage)
       
   896 	{
       
   897 	LOG_FUNC;
       
   898 
       
   899 	// Get the data the client wants to send.
       
   900 	const TUid interfaceUid = TUid::Uid(aMessage.Int0());
       
   901 	LOG1(_L("\tinterfaceUid = 0x%08x"), interfaceUid);
       
   902 
       
   903 	if (aMessage.GetDesLengthL(1) != sizeof(TOperationInformation))
       
   904 		{
       
   905 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
       
   906 		return;
       
   907 		}
       
   908 
       
   909 	TPckgBuf<TOperationInformation> opInfoPckg;	
       
   910 	TInt err= aMessage.Read(
       
   911 			1, // location of the descriptor in the client's message (as we expect them to have set it up)
       
   912 			opInfoPckg, // descriptor to write to from client memory space
       
   913 			0 // offset into our descriptor to put the client's data
       
   914 			);
       
   915 	
       
   916 	if ( err != KErrNone )
   316 	if ( err != KErrNone )
   917 		{
   317 		{
   918 		LOG1(_L("\taMessage.Read = %d"), err);
   318 		CompleteClient(aMessage, err);
   919 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
   319 		}
   920 		return;
   320 	else if (msg)
   921 		}	
       
   922 	
       
   923 	const TUint operationId = opInfoPckg().iOperationId;
       
   924 	LOG1(_L("\toperationId = 0x%02x"), operationId);
       
   925 	
       
   926 	const TRemConMessageSubType messageSubType = opInfoPckg().iMessageSubType;
       
   927 	LOG1(_L("\tmessageSubType = 0x%02x"), messageSubType);
       
   928 	
       
   929 	const TUint dataLength = (TUint)aMessage.GetDesLengthL(2);
       
   930 	LOG1(_L("\tdataLength = %d"), dataLength);
       
   931 	
       
   932 	// If the client wanted to send some operation-associated data, read it 
       
   933 	// from them.
       
   934 	RBuf8 sendDes;
       
   935 	if ( dataLength != 0 )
       
   936 		{
       
   937 		sendDes.CreateL(dataLength);
       
   938 		TInt err = aMessage.Read(
       
   939 			2, // location of the descriptor in the client's message (as we expect them to have set it up)
       
   940 			sendDes, // descriptor to write to from client memory space
       
   941 			0 // offset into our descriptor to put the client's data
       
   942 			);
       
   943 		// NB We don't do LEAVEIFERRORL(aMessage.Read) because a bad client 
       
   944 		// descriptor is a panicking offence for them, not an 'error the 
       
   945 		// request' offence.
       
   946 		if ( err != KErrNone )
       
   947 			{
       
   948 			LOG1(_L("\taMessage.Read = %d"), err);
       
   949 			sendDes.Close();
       
   950 			PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
       
   951 			return;
       
   952 			}
       
   953 		}
       
   954 
       
   955 	// Before we ask the server to send, we must set our ClientInfo 
       
   956 	// correctly so the TSP can get information about the client. 
       
   957 	iClientInfo.Message() = aMessage;
       
   958 
       
   959 	CRemConMessage* msg = NULL;
       
   960 	
       
   961 	if (messageSubType != ERemConNotifyCommandAwaitingInterim)
       
   962 		{
       
   963 		sendDes.Close();
       
   964 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicIllegalIpc);
       
   965 		return;
       
   966 		}
       
   967 	
       
   968 	CleanupClosePushL(sendDes);
       
   969 	msg = CRemConMessage::NewL(
       
   970 			iRemoteAddress, // either specified (if we're connection-oriented) or null (we're connectionless- this field will be filled in by the TSP)
       
   971 			ERemConNotifyCommand, 
       
   972 			messageSubType,
       
   973 			interfaceUid,
       
   974 			operationId,
       
   975 			sendDes, // msg takes ownership
       
   976 			Id(), // session id for when the response comes back
       
   977 			0, // we let the bearer manager invent a new transaction id when the message gets to it
       
   978 			ETrue);	
       
   979 	CLEANUPSTACK_POP1(&sendDes); // now owned by msg
       
   980 	
       
   981 	LOG(_L("\tCONTROLLER send"));
       
   982 	ASSERT_DEBUG(iSendQueue);
       
   983 	if (iSending != ENotSending || !iSendQueue->IsEmpty())
       
   984 		{
       
   985 		iSendQueue->Append(*msg);
       
   986 		}
       
   987 	else
       
   988 		{
       
   989 		SendToServer(*msg);
       
   990 		}
       
   991 	}
       
   992 
       
   993 void CRemConSession::SendUnreliable(const RMessage2& aMessage)
       
   994 	{
       
   995 	LOG_FUNC;
       
   996 
       
   997 	// Check we've had our type set...
       
   998 	if (	Type() != ERemConClientTypeController
       
   999 		&&	Type() != ERemConClientTypeTarget
       
  1000 		)
       
  1001 		{
       
  1002 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientTypeNotSet);
       
  1003 		return;
       
  1004 		}
       
  1005 
       
  1006 	// Check we don't have a disconnect outstanding- this makes no sense from 
       
  1007 	// a client viewpoint (they should cancel the disconnect first).
       
  1008 	// [The client is allowed to have a connect request outstanding- the 
       
  1009 	// bearer manager makes sure a bearer-level connect is not posted on the 
       
  1010 	// same address twice.]
       
  1011 	if ( iDisconnectBearerMsg.Handle() )
       
  1012 		{
       
  1013 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBearerControlOutstanding);
       
  1014 		return;
       
  1015 		}
       
  1016 
       
  1017 	CRemConMessage* msg = NULL;
       
  1018 	TRAPD(err, msg = DoCreateUnreliableMessageL(aMessage));
       
  1019 	CompleteClient(aMessage, err);
       
  1020 	if (err == KErrNone)
       
  1021 		{
   321 		{
  1022 		ASSERT_DEBUG(iSendQueue);
   322 		ASSERT_DEBUG(iSendQueue);
  1023 		if (iSending || !iSendQueue->IsEmpty())
   323 	
       
   324 		if (iSending != ENotSending || !iSendQueue->IsEmpty())
  1024 			{
   325 			{
  1025 			iSendQueue->Append(*msg);
   326 			iSendQueue->Append(*msg);
  1026 			}
   327 			}
  1027 		else
   328 		else
  1028 			{
   329 			{
       
   330 			// we know msg cannot be null here as said above.
  1029 			SendToServer(*msg);
   331 			SendToServer(*msg);
  1030 			}
   332 			}
  1031 		}
   333 		}
  1032 	}
   334 	}
  1033 
   335 
  1034 CRemConMessage* CRemConSession::DoCreateUnreliableMessageL(const RMessage2& aMessage)
       
  1035 	{
       
  1036 	LOG_FUNC;
       
  1037 
       
  1038 	// Get the data the client wants to send.
       
  1039 	const TUid interfaceUid = TUid::Uid(aMessage.Int0());
       
  1040 	LOG1(_L("\tinterfaceUid = 0x%08x"), interfaceUid);
       
  1041 
       
  1042 	if (aMessage.GetDesLengthL(1) != sizeof(TOperationInformation))
       
  1043 		{
       
  1044 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
       
  1045 		LEAVEL(KErrBadDescriptor);
       
  1046 		}
       
  1047 
       
  1048 	TPckgBuf<TOperationInformation> opInfoPckg;
       
  1049 	
       
  1050 	TInt err= aMessage.Read(
       
  1051 			1, // location of the descriptor in the client's message (as we expect them to have set it up)
       
  1052 			opInfoPckg, // descriptor to write to from client memory space
       
  1053 			0 // offset into our descriptor to put the client's data
       
  1054 			);
       
  1055 	
       
  1056 	if ( err != KErrNone )
       
  1057 		{
       
  1058 		LOG1(_L("\taMessage.Read = %d"), err);
       
  1059 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
       
  1060 		LEAVEL(KErrBadDescriptor);
       
  1061 		}	
       
  1062 	
       
  1063 	const TUint operationId = opInfoPckg().iOperationId;
       
  1064 	LOG1(_L("\toperationId = 0x%02x"), operationId);
       
  1065 	
       
  1066 	const TRemConMessageSubType messageSubType = opInfoPckg().iMessageSubType;
       
  1067 	LOG1(_L("\tmessageSubType = 0x%02x"), messageSubType);
       
  1068 
       
  1069 	
       
  1070 	const TUint dataLength = (TUint)aMessage.GetDesLengthL(2);
       
  1071 	LOG1(_L("\tdataLength = %d"), dataLength);
       
  1072 	
       
  1073 	// If the client wanted to send some operation-associated data, read it 
       
  1074 	// from them.
       
  1075 	RBuf8 sendDes;
       
  1076 	if ( dataLength != 0 )
       
  1077 		{
       
  1078 		sendDes.CreateL(dataLength);
       
  1079 		TInt err = aMessage.Read(
       
  1080 			2, // location of the descriptor in the client's message (as we expect them to have set it up)
       
  1081 			sendDes, // descriptor to write to from client memory space
       
  1082 			0 // offset into our descriptor to put the client's data
       
  1083 			);
       
  1084 		// NB We don't do LEAVEIFERRORL(aMessage.Read) because a bad client 
       
  1085 		// descriptor is a panicking offence for them, not an 'error the 
       
  1086 		// request' offence.
       
  1087 		if ( err != KErrNone )
       
  1088 			{
       
  1089 			LOG1(_L("\taMessage.Read = %d"), err);
       
  1090 			sendDes.Close();
       
  1091 			PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
       
  1092 			LEAVEL(KErrBadDescriptor);
       
  1093 			}
       
  1094 		}
       
  1095 	CleanupClosePushL(sendDes);
       
  1096 
       
  1097 	// Before we ask the server to send, we must set our ClientInfo 
       
  1098 	// correctly so the TSP can get information about the client. 
       
  1099 	iClientInfo.Message() = aMessage;
       
  1100 
       
  1101 	CRemConMessage* msg = NULL;
       
  1102 	
       
  1103 	// Examine the session type.
       
  1104 	switch ( iType )
       
  1105 		{
       
  1106 	case ERemConClientTypeTarget:
       
  1107 		{
       
  1108 		LOG(_L("\tTARGET send"));
       
  1109 
       
  1110 		msg = CRemConMessage::NewL(
       
  1111 			TRemConAddress(), // we don't know which remotes it's going to yet
       
  1112 			ERemConResponse, // targets can only send responses
       
  1113 			messageSubType,
       
  1114 			interfaceUid,
       
  1115 			operationId,
       
  1116 			sendDes, // msg takes ownership
       
  1117 			Id(), // session id to match this response against the originating command
       
  1118 			0, // transaction id not yet known
       
  1119 			EFalse);
       
  1120 		CLEANUPSTACK_POP1(&sendDes); // now owned by msg
       
  1121 		break;
       
  1122 		}
       
  1123 
       
  1124 	case ERemConClientTypeController:
       
  1125 		{
       
  1126 		LOG(_L("\tCONTROLLER send"));
       
  1127 		
       
  1128 		// A client is not allowed to send an unreliable notify command.
       
  1129 		if	(	(messageSubType == ERemConNotifyCommandAwaitingInterim)
       
  1130 			||	(messageSubType == ERemConNotifyCommandAwaitingChanged)
       
  1131 			)
       
  1132 			{
       
  1133 			LOG(_L8("\tNot allowed to send unreliable notify command"));
       
  1134 			CleanupStack::PopAndDestroy(&sendDes);
       
  1135 			PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicIllegalIpc);
       
  1136 			LEAVEL(KErrBadDescriptor);
       
  1137 			}
       
  1138 		
       
  1139 		msg = CRemConMessage::NewL(
       
  1140 			iRemoteAddress, // either specified (if we're connection-oriented) or null (we're connectionless- this field will be filled in by the TSP)
       
  1141 			ERemConCommand, // controllers can only send commands
       
  1142 			messageSubType,
       
  1143 			interfaceUid,
       
  1144 			operationId,
       
  1145 			sendDes, // msg takes ownership
       
  1146 			Id(), // session id for when the response comes back
       
  1147 			0, // we let the bearer manager invent a new transaction id when the message gets to it
       
  1148 			EFalse);
       
  1149 		CLEANUPSTACK_POP1(&sendDes); // now owned by msg
       
  1150 		
       
  1151 		}
       
  1152 		break;
       
  1153 
       
  1154 	default:
       
  1155 		DEBUG_PANIC_LINENUM;
       
  1156 		break;
       
  1157 		}
       
  1158 
       
  1159 	return msg;
       
  1160 	}
       
  1161 
       
  1162 void CRemConSession::SendToServer(CRemConMessage& aMsg)
       
  1163 	{
       
  1164 	LOG_FUNC;
       
  1165 	
       
  1166 	// Set our completion members.
       
  1167 	NumRemotes() = 0;
       
  1168 	NumRemotesToTry() = 0;
       
  1169 	SendError() = KErrNone;
       
  1170 
       
  1171 	
       
  1172 	iSending = (aMsg.IsReliableSend()) ? ESendingReliable: ESendingUnreliable;
       
  1173 	switch ( iType )
       
  1174 		{
       
  1175 	case ERemConClientTypeTarget:
       
  1176 		{
       
  1177 		iServer.SendResponse(aMsg, *this);
       
  1178 		break;
       
  1179 		}
       
  1180 	case ERemConClientTypeController:
       
  1181 		{
       
  1182 		iServer.SendCommand(aMsg);
       
  1183 		break;
       
  1184 		}
       
  1185 	default:
       
  1186 		DEBUG_PANIC_LINENUM;
       
  1187 		break;
       
  1188 		}
       
  1189 	}
       
  1190 
       
  1191 
       
  1192 void CRemConSession::SendCancel(const RMessage2& aMessage)
   336 void CRemConSession::SendCancel(const RMessage2& aMessage)
  1193 	{
   337 	{
  1194 	LOG_FUNC;
   338 	LOG_FUNC;
  1195 
   339 
  1196 	// Check we've had our type set...
   340 	// Check we've had our features set...
  1197 	if (	Type() != ERemConClientTypeController
   341 	if (!ClientAvailable())
  1198 		&&	Type() != ERemConClientTypeTarget
   342 		{
  1199 		)
   343 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientFeaturesNotSet);
  1200 		{
       
  1201 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientTypeNotSet);
       
  1202 		return;
   344 		return;
  1203 		}
   345 		}
  1204 
   346 
  1205 	// See comments in ConnectBearerCancel.
   347 	// See comments in ConnectBearerCancel.
  1206 	if ( iSendMsg.Handle() )
   348 	if ( iSendMsg.Handle() )
  1232 		}
   374 		}
  1233 	
   375 	
  1234 	CompleteClient(aMessage, KErrNone);
   376 	CompleteClient(aMessage, KErrNone);
  1235 	}
   377 	}
  1236 
   378 
  1237 void CRemConSession::DoSendCancel()
       
  1238 	{
       
  1239 	LOG_FUNC;
       
  1240 	// We must tell the server, and pull the CRemConMessage from the 
       
  1241 	// 'outgoing pending TSP' queue if it's on it. If the TSP is currently 
       
  1242 	// processing the CRemConMessage, we must tell it to stop before we 
       
  1243 	// can complete the RMessage2 iSendMsg- the TSP might still be 
       
  1244 	// dereferencing bits of it. (The TSP is given iSendMsg so it can 
       
  1245 	// access the client's secure ID and do a capability check.)
       
  1246 	// NB This only matters for commands- responses don't go through the 
       
  1247 	// TSP.
       
  1248 	// Not also that this processing *stops* this 
       
  1249 	// CRemConSession::SendCancel method from being the very simple 'I'm 
       
  1250 	// no longer interested in the completion of the asynchronous request' 
       
  1251 	// type of API it (and all cancels) should be. It actually does work 
       
  1252 	// as well. As long as this work is implemented _synchronously_, we 
       
  1253 	// should be OK.
       
  1254 	iServer.SendCancel(*this);
       
  1255 
       
  1256 	NumRemotesToTry() = 0;
       
  1257 	iSendError = KErrCancel;
       
  1258 	CompleteSend();
       
  1259 	
       
  1260 	}
       
  1261 
       
  1262 void CRemConSession::EmptySendQueue()
       
  1263 	{
       
  1264 	ASSERT_DEBUG(!iSendMsg.Handle())
       
  1265 	ASSERT_DEBUG(iSendNextCallBack);
       
  1266 	iSendNextCallBack->Cancel();
       
  1267 	CRemConMessage* msg;
       
  1268 	ASSERT_DEBUG(iSendQueue);
       
  1269 	TSglQueIter<CRemConMessage>& iter = iSendQueue->SetToFirst();
       
  1270 	while ((msg = iter++) != NULL)
       
  1271 		{
       
  1272 		iSendQueue->RemoveAndDestroy(*msg);
       
  1273 		}
       
  1274 	}
       
  1275 
       
  1276 void CRemConSession::Receive(const RMessage2& aMessage)
   379 void CRemConSession::Receive(const RMessage2& aMessage)
  1277 	{
   380 	{
  1278 	LOG_FUNC;
   381 	LOG_FUNC;
  1279 
   382 
  1280 	// Messages are pushed from bearers, so we 
   383 	// Messages are pushed from bearers, so we 
  1286 		{
   389 		{
  1287 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicReceiveAlreadyOutstanding);
   390 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicReceiveAlreadyOutstanding);
  1288 		return;
   391 		return;
  1289 		}
   392 		}
  1290 
   393 
  1291 	// Check we've had our type set...
   394 	// Check we've had our features set...
  1292 	if (	Type() != ERemConClientTypeController
   395 	if (!ClientAvailable())
  1293 		&&	Type() != ERemConClientTypeTarget
   396 		{
  1294 		)
   397 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientFeaturesNotSet);
  1295 		{
       
  1296 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientTypeNotSet);
       
  1297 		return;
   398 		return;
  1298 		}
   399 		}
  1299 
   400 
  1300 	iReceiveMsg = aMessage;
   401 	iReceiveMsg = aMessage;
  1301 	// If there's anything waiting to be given to us, ReceiveRequest will call 
   402 
  1302 	// back to us with it.
   403 	DoReceive();
  1303 	iServer.ReceiveRequest(*this);
       
  1304 	}
   404 	}
  1305 
   405 
  1306 void CRemConSession::ReceiveCancel(const RMessage2& aMessage)
   406 void CRemConSession::ReceiveCancel(const RMessage2& aMessage)
  1307 	{
   407 	{
  1308 	LOG_FUNC;
   408 	LOG_FUNC;
  1309 
   409 
  1310 	// Check we've had our type set...
   410 	// Check we've had our features set...
  1311 	if (	Type() != ERemConClientTypeController
   411 	if (!ClientAvailable())
  1312 		&&	Type() != ERemConClientTypeTarget
   412 		{
  1313 		)
   413 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientFeaturesNotSet);
  1314 		{
       
  1315 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientTypeNotSet);
       
  1316 		return;
   414 		return;
  1317 		}
   415 		}
  1318 
   416 
  1319 	// See comments in ConnectBearerCancel.
   417 	// See comments in ConnectBearerCancel.
  1320 	if ( iReceiveMsg.Handle() )
   418 	if ( iReceiveMsg.Handle() )
  1326 
   424 
  1327 void CRemConSession::GetConnectionCount(const RMessage2& aMessage)
   425 void CRemConSession::GetConnectionCount(const RMessage2& aMessage)
  1328 	{
   426 	{
  1329 	LOG_FUNC;
   427 	LOG_FUNC;
  1330 
   428 
  1331 	// Check we've had our type set...
   429 	// Check we've had our features set...
  1332 	if (	Type() != ERemConClientTypeController
   430 	if (!ClientAvailable())
  1333 		&&	Type() != ERemConClientTypeTarget
   431 		{
  1334 		)
   432 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientFeaturesNotSet);
  1335 		{
       
  1336 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientTypeNotSet);
       
  1337 		return;
   433 		return;
  1338 		}
   434 		}
  1339 
   435 
  1340 	// Get the answer to the question- the number of connections at the 
   436 	// Get the answer to the question- the number of connections at the 
  1341 	// current point in time (i.e. the latest entry in the connection 
   437 	// current point in time (i.e. the latest entry in the connection 
  1357 
   453 
  1358 void CRemConSession::GetConnections(const RMessage2& aMessage)
   454 void CRemConSession::GetConnections(const RMessage2& aMessage)
  1359 	{
   455 	{
  1360 	LOG_FUNC;
   456 	LOG_FUNC;
  1361 
   457 
  1362 	// Check we've had our type set...
   458 	// Check we've had our features set...
  1363 	if (	Type() != ERemConClientTypeController
   459 	if (!ClientAvailable())
  1364 		&&	Type() != ERemConClientTypeTarget
   460 		{
  1365 		)
   461 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientFeaturesNotSet);
  1366 		{
       
  1367 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientTypeNotSet);
       
  1368 		return;
   462 		return;
  1369 		}
   463 		}
  1370 
   464 
  1371 	// Get the array of connections at the point in the history we're 
   465 	// Get the array of connections at the point in the history we're 
  1372 	// interested in and write it back to the client. NB This is not 
   466 	// interested in and write it back to the client. NB This is not 
  1412 	{
   506 	{
  1413 	LOG_FUNC;
   507 	LOG_FUNC;
  1414 
   508 
  1415 	// Messages are pushed to us from bearers, so we don't need anything more 
   509 	// Messages are pushed to us from bearers, so we don't need anything more 
  1416 	// than some sanity checking here.
   510 	// than some sanity checking here.
  1417 	// Check we've had our type set...
   511 	// Check we've had our features set...
  1418 	if (	Type() != ERemConClientTypeController
   512 	if (!ClientAvailable())
  1419 		&&	Type() != ERemConClientTypeTarget
   513 		{
  1420 		)
   514 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientFeaturesNotSet);
  1421 		{
       
  1422 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientTypeNotSet);
       
  1423 		return;
   515 		return;
  1424 		}
   516 		}
  1425 
   517 
  1426 	if ( iNotifyConnectionsChangeMsg.Handle() )
   518 	if ( iNotifyConnectionsChangeMsg.Handle() )
  1427 		{
   519 		{
  1444 
   536 
  1445 void CRemConSession::NotifyConnectionsChangeCancel(const RMessage2& aMessage)
   537 void CRemConSession::NotifyConnectionsChangeCancel(const RMessage2& aMessage)
  1446 	{
   538 	{
  1447 	LOG_FUNC;
   539 	LOG_FUNC;
  1448 
   540 
  1449 	// Check we've had our type set...
   541 	// Check we've had our features set...
  1450 	if (	Type() != ERemConClientTypeController
   542 	if (!ClientAvailable())
  1451 		&&	Type() != ERemConClientTypeTarget
   543 		{
  1452 		)
   544 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientFeaturesNotSet);
  1453 		{
       
  1454 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicClientTypeNotSet);
       
  1455 		return;
   545 		return;
  1456 		}
   546 		}
  1457 
   547 
  1458 	// See comments in ConnectBearerCancel.
   548 	// See comments in ConnectBearerCancel.
  1459 	if ( iNotifyConnectionsChangeMsg.Handle() )
   549 	if ( iNotifyConnectionsChangeMsg.Handle() )
  1461 		CompleteClient(iNotifyConnectionsChangeMsg, KErrCancel);
   551 		CompleteClient(iNotifyConnectionsChangeMsg, KErrCancel);
  1462 		}
   552 		}
  1463 	CompleteClient(aMessage, KErrNone);
   553 	CompleteClient(aMessage, KErrNone);
  1464 	}
   554 	}
  1465 
   555 
  1466 void CRemConSession::RegisterInterestedAPIs(const RMessage2& aMessage)
   556 CRemConInterfaceDetailsArray* CRemConSession::ExtractInterestedAPIsL(const RMessage2& aMessage)
  1467 	{
   557 	{
  1468 	LOG_FUNC;
   558 	LOG_FUNC;
  1469 	
   559 	
  1470 	if(iType == ERemConClientTypeUndefined)
   560 	CRemConInterfaceDetailsArray* result;
  1471 		{
       
  1472 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicRegisterInterestedAPIsInNonTargetSession);
       
  1473 		}
       
  1474 	
       
  1475 	TRAPD(err, DoRegisterInterestedAPIsL(aMessage));
       
  1476 	
       
  1477 	if(iType == ERemConClientTypeController)
       
  1478 		{
       
  1479 		iServer.ControllerClientAvailable();
       
  1480 		}
       
  1481 	else if(err == KErrNone) // must be target
       
  1482 		{
       
  1483 		iServer.TargetClientAvailable(*this);
       
  1484 		
       
  1485 		TInt count = iInterestedAPIs->Array().Count();
       
  1486 		for(TInt ix=0; ix<count; ++ix)
       
  1487 			{
       
  1488 			CRemConInterfaceDetails* details = iInterestedAPIs->Array()[ix];
       
  1489 			ASSERT_DEBUG(details);
       
  1490 			if(details->IsBulk())
       
  1491 				{
       
  1492 				iServer.BulkServerRequired();
       
  1493 				break;
       
  1494 				}
       
  1495 			}
       
  1496 		}
       
  1497 	
       
  1498 	CompleteClient(aMessage, err);
       
  1499 	}
       
  1500 
       
  1501 void CRemConSession::DoRegisterInterestedAPIsL(const RMessage2& aMessage)
       
  1502 	{
       
  1503 	LOG_FUNC;
       
  1504 	
   561 	
  1505 	RBuf8 buf;
   562 	RBuf8 buf;
  1506 	buf.CreateL(aMessage.GetDesLengthL(0));
   563 	buf.CreateL(aMessage.GetDesLengthL(0));
  1507 	CleanupClosePushL(buf);
   564 	CleanupClosePushL(buf);
  1508 	
   565 	
  1509 	aMessage.ReadL(0, buf);
   566 	aMessage.ReadL(0, buf);
  1510 	RDesReadStream ipcStream(buf);
   567 	RDesReadStream ipcStream(buf);
  1511 	
   568 	
  1512 	iInterestedAPIs = CRemConInterfaceDetailsArray::InternalizeL(ipcStream);
   569 	result = CRemConInterfaceDetailsArray::InternalizeL(ipcStream);
  1513 	
   570 	
  1514 	ipcStream.Close(); 
   571 	ipcStream.Close(); 
  1515 	CleanupStack::PopAndDestroy(&buf); 
   572 	CleanupStack::PopAndDestroy(&buf);
  1516 	}
   573 	
  1517 
   574 	return result;
  1518 void CRemConSession::CompleteConnect(const TRemConAddress& aAddr, TInt aError)
   575 	}
  1519 	{
   576 
  1520 	LOG_FUNC;
   577 TBool CRemConSession::DoGetSendInfoLC(const RMessage2& aMessage, 
  1521 	LOG2(_L("\taError = %d, aAddr.BearerUid = 0x%08x"), aError, aAddr.BearerUid());
   578 		TUid& aInterfaceUid,
  1522 
   579 		TUint& aOperationId,
  1523 	LOG1(_L("\tiRemoteAddress.BearerUid = 0x%08x"), iRemoteAddress.BearerUid());
   580 		TRemConMessageSubType& aMessageSubType,
  1524 	LOG1(_L("\tiConnectBearerMsg.Handle = %d"), iConnectBearerMsg.Handle());
   581 		RBuf8& aSendDes)
  1525 
   582 	{
  1526 	if ( iRemoteAddress == aAddr )
   583 	// Get the data the client wants to send.
  1527 		{
   584 	aInterfaceUid = TUid::Uid(aMessage.Int0());
  1528 		if ( iConnectBearerMsg.Handle() )
   585 	LOG1(_L("\taInterfaceUid = 0x%08x"), aInterfaceUid);
  1529 			{
   586 
  1530 			// We are a session that has an outstanding request on this specific 
   587 	if (aMessage.GetDesLengthL(1) != sizeof(TOperationInformation))
  1531 			// connection address.
   588 		{
  1532 			CompleteClient(iConnectBearerMsg, aError);
   589 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
  1533 			}
   590 		return EFalse;
  1534 		else 
   591 		}
  1535 			{
   592 
  1536 			// Connect bearer message is not valid. 
   593 	TPckgBuf<TOperationInformation> opInfoPckg;
  1537 			// Check for pending messages.
   594 	
  1538 			CheckForPendingMsg();
   595 	TInt err= aMessage.Read(
  1539 			}
   596 			1, // location of the descriptor in the client's message (as we expect them to have set it up)
  1540 		}
   597 			opInfoPckg, // descriptor to write to from client memory space
  1541 	}
   598 			0 // offset into our descriptor to put the client's data
  1542 
   599 			);
  1543 void CRemConSession::CompleteDisconnect(const TRemConAddress& aAddr, TInt aError)
   600 	
  1544 	{
   601 	if ( err != KErrNone )
  1545 	LOG_FUNC;
   602 		{
  1546 	LOG2(_L("\taError = %d, aAddr.BearerUid = 0x%08x"), aError, aAddr.BearerUid());
   603 		LOG1(_L("\taMessage.Read = %d"), err);
  1547 
   604 		PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
  1548 	LOG1(_L("\tiRemoteAddress.BearerUid = 0x%08x"), iRemoteAddress.BearerUid());
   605 		return EFalse;
  1549 	LOG1(_L("\tiDisconnectBearerMsg.Handle = %d"), iDisconnectBearerMsg.Handle());
   606 		}	
  1550 
   607 	
  1551 	if ( iRemoteAddress == aAddr )
   608 	aOperationId = opInfoPckg().iOperationId;
  1552 		{
   609 	LOG1(_L("\taOperationId = 0x%02x"), aOperationId);
  1553 		if ( iDisconnectBearerMsg.Handle() )
   610 	
  1554 			{
   611 	aMessageSubType = opInfoPckg().iMessageSubType;
  1555 			// We are a session that has an outstanding request on this specific 
   612 	LOG1(_L("\taMessageSubType = 0x%02x"), aMessageSubType);
  1556 			// connection address.
   613 
  1557 			CompleteClient(iDisconnectBearerMsg, aError);
   614 	const TUint dataLength = (TUint)aMessage.GetDesLengthL(3);
  1558 			}
   615 	LOG1(_L("\tdataLength = %d"), dataLength);
  1559 		else 
   616 	
  1560 			{
   617 	// If the client wanted to send some operation-associated data, read it 
  1561 			// Diconnect bearer message is not valid. 
   618 	// from them.
  1562 			// Check for pending messages.
   619 	if ( dataLength != 0 )
  1563 			CheckForPendingMsg();
   620 		{
  1564 			}
   621 		aSendDes.CreateL(dataLength);
  1565 
   622 		TInt err = aMessage.Read(
  1566 		}
   623 			3, // location of the descriptor in the client's message (as we expect them to have set it up)
       
   624 			aSendDes, // descriptor to write to from client memory space
       
   625 			0 // offset into our descriptor to put the client's data
       
   626 			);
       
   627 		// NB We don't do LEAVEIFERRORL(aMessage.Read) because a bad client 
       
   628 		// descriptor is a panicking offence for them, not an 'error the 
       
   629 		// request' offence.
       
   630 		if ( err != KErrNone )
       
   631 			{
       
   632 			LOG1(_L("\taMessage.Read = %d"), err);
       
   633 			aSendDes.Close();
       
   634 			PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadDescriptor);
       
   635 			return EFalse;
       
   636 			}
       
   637 		}
       
   638 	CleanupClosePushL(aSendDes);
       
   639 	return ETrue;
       
   640 	}
       
   641 
       
   642 void CRemConSession::GoConnectionOriented(const RMessage2& aMessage)
       
   643 	{
       
   644 	PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
       
   645 	}
       
   646 
       
   647 void CRemConSession::GoConnectionless(const RMessage2& aMessage)
       
   648 	{
       
   649 	PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
       
   650 	}
       
   651 
       
   652 void CRemConSession::ConnectBearer(const RMessage2& aMessage)
       
   653 	{
       
   654 	PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
       
   655 	}
       
   656 
       
   657 void CRemConSession::ConnectBearerCancel(const RMessage2& aMessage)
       
   658 	{
       
   659 	PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
       
   660 	}
       
   661 
       
   662 void CRemConSession::DisconnectBearer(const RMessage2& aMessage)
       
   663 	{
       
   664 	PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
       
   665 	}
       
   666 
       
   667 void CRemConSession::DisconnectBearerCancel(const RMessage2& aMessage)
       
   668 	{
       
   669 	PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
       
   670 	}
       
   671 
       
   672 void CRemConSession::SendNotify(const RMessage2& aMessage)
       
   673 	{
       
   674 	PANIC_MSG(aMessage, KRemConClientPanicCat, ERemConClientPanicBadType);
  1567 	}
   675 	}
  1568 
   676 
  1569 void CRemConSession::ConnectionsChanged()
   677 void CRemConSession::ConnectionsChanged()
  1570 	{
   678 	{
  1571 	LOG_FUNC;
   679 	LOG_FUNC;
  1584 	{
   692 	{
  1585 	LOG_FUNC;
   693 	LOG_FUNC;
  1586 	LOG2(_L("\tiNumRemotes = %d, iSendError = %d"), iNumRemotes, iSendError);
   694 	LOG2(_L("\tiNumRemotes = %d, iSendError = %d"), iNumRemotes, iSendError);
  1587 
   695 
  1588 	ASSERT_DEBUG(NumRemotesToTry() == 0);
   696 	ASSERT_DEBUG(NumRemotesToTry() == 0);
  1589 	NumRemotesToTry() = -1;
       
  1590 
       
  1591 	
   697 	
  1592 	if (iSending == ESendingReliable)
   698 	if (iSending == ESendingReliable)
  1593 		{
   699 		{
  1594 		if ( iSendError == KErrNone )
   700 		if ( iSendError == KErrNone )
  1595 			{
   701 			{
  1617 
   723 
  1618 	if (iSending == ESendingReliable)
   724 	if (iSending == ESendingReliable)
  1619 		{
   725 		{
  1620 		CompleteClient(iSendMsg, iSendError);
   726 		CompleteClient(iSendMsg, iSendError);
  1621 		}
   727 		}
  1622 	
   728 
  1623 	ASSERT_DEBUG(iSendQueue);
   729 	ASSERT_DEBUG(iSendQueue);
  1624 	if (!iSendQueue->IsEmpty())
   730 	if (!iSendQueue->IsEmpty())
  1625 		{
   731 		{
  1626 		ASSERT_DEBUG(iSendNextCallBack);
   732 		ASSERT_DEBUG(iSendNextCallBack);
  1627 		iSendNextCallBack->CallBack();
   733 		iSendNextCallBack->CallBack();
  1628 		}
   734 		}
  1629 	iSending = ENotSending;
   735 	iSending = ENotSending;
  1630 	}
   736 	}
  1631 
   737 
  1632 TInt CRemConSession::SendNextCb(TAny* aThis)
       
  1633 	{
       
  1634 	static_cast<CRemConSession*>(aThis)->DoSendNext();
       
  1635 	return KErrNone;
       
  1636 	}
       
  1637 
       
  1638 void CRemConSession::DoSendNext()
       
  1639 	{
       
  1640 	ASSERT_DEBUG(iSendQueue);
       
  1641 	CRemConMessage& msg = iSendQueue->First();
       
  1642 	iSendQueue->Remove(msg);
       
  1643 	SendToServer(msg);
       
  1644 	}
       
  1645 
       
  1646 void CRemConSession::PanicSend(TRemConClientPanic aCode)
   738 void CRemConSession::PanicSend(TRemConClientPanic aCode)
  1647 	{
   739 	{
  1648 	LOG_FUNC;
   740 	LOG_FUNC;
  1649 	LOG1(_L("\taCode = %d"), aCode);
   741 	LOG1(_L("\taCode = %d"), aCode);
  1650 
   742 
  1651 	PANIC_MSG(iSendMsg, KRemConClientPanicCat, aCode);
   743 	PANIC_MSG(iSendMsg, KRemConClientPanicCat, aCode);
  1652 	}
   744 	}
  1653 
       
  1654 TBool CRemConSession::SupportedMessage(const CRemConMessage& aMsg)
       
  1655     {
       
  1656     // Return true unless this is a command for an unsupported interface
       
  1657     return !(aMsg.MsgType() == ERemConCommand && !FindInterfaceByUid(aMsg.InterfaceUid()));
       
  1658     }
       
  1659 
   745 
  1660 TInt CRemConSession::WriteMessageToClient(const CRemConMessage& aMsg)
   746 TInt CRemConSession::WriteMessageToClient(const CRemConMessage& aMsg)
  1661 	{
   747 	{
  1662 	LOG_FUNC;
   748 	LOG_FUNC;
  1663 
   749 
  1699 	// big enough to hold the operation-specific data. If we did, then 
   785 	// big enough to hold the operation-specific data. If we did, then 
  1700 	// a buggy remote could take down a client of RemCon. Just error 
   786 	// a buggy remote could take down a client of RemCon. Just error 
  1701 	// the client instead.
   787 	// the client instead.
  1702 	LEAVEIFERRORL(iReceiveMsg.Write(1, aMsg.OperationData()));
   788 	LEAVEIFERRORL(iReceiveMsg.Write(1, aMsg.OperationData()));
  1703 	}
   789 	}
  1704 	
       
  1705 void CRemConSession::CheckForPendingMsg() const
       
  1706 	{
       
  1707 	LOG_FUNC;
       
  1708 	if (iPendingMsg.Handle())
       
  1709 		{
       
  1710 		ASSERT_DEBUG(iPendingMsgProcessor);
       
  1711 		iPendingMsgProcessor->Complete();
       
  1712 		}
       
  1713 	}
       
  1714 
       
  1715 void CRemConSession::ProcessPendingMsgL()
       
  1716 	{
       
  1717 	LOG_FUNC;
       
  1718 	if (!iPendingMsg.Handle())
       
  1719 		{
       
  1720 		// This means that the pending connect or disconnect message,
       
  1721 		// has been cancelled by the time we got here.
       
  1722 		// (It was cancelled between two following calls:
       
  1723 		// iPendingMsgProcessor::Complete and iPendingMsgProcessor::RunL
       
  1724 		return;
       
  1725 		}
       
  1726 		
       
  1727 	ServiceL(iPendingMsg);
       
  1728 	if (iPendingMsg.Handle())
       
  1729 		{
       
  1730 		// This means that the pending msg has not been completed in ServiceL call.
       
  1731 		// It was stored either in iConnectBearerMsg or iDisconnectBearerMsg member.
       
  1732 		// This also means that this message is not "pending" any more 
       
  1733 		// (as processing of its copy has been started). 
       
  1734 		// However because the copy will get completed we need to 
       
  1735 		// clean iPendingMsg.iHandle here
       
  1736 		// To supress coverity error for uninitialized use of 'emptyMsg' coverity annotations
       
  1737 		// are used as the in-line default constructor of RMessage2 doesn't initialize all member variables.
       
  1738 		// coverity[var_decl]
       
  1739 		RMessage2 emptyMsg;
       
  1740 		iPendingMsg = emptyMsg;
       
  1741 		}
       
  1742 	}
       
  1743 
       
  1744 
   790 
  1745 TInt CRemConSession::SupportedInterfaces(RArray<TUid>& aUids)
   791 TInt CRemConSession::SupportedInterfaces(RArray<TUid>& aUids)
  1746 	{
   792 	{
  1747 	LOG_FUNC
   793 	LOG_FUNC;
       
   794 
       
   795 	aUids.Reset();
       
   796 	return AppendSupportedInterfaces(aUids);
       
   797 	}
       
   798 
       
   799 TInt CRemConSession::AppendSupportedInterfaces(RArray<TUid>& aUids)
       
   800 	{
       
   801 	LOG_FUNC;
  1748 	ASSERT_DEBUG(iInterestedAPIs);
   802 	ASSERT_DEBUG(iInterestedAPIs);
  1749 	TInt err = KErrNone;
   803 	TInt err = KErrNone;
  1750 	
   804 
  1751 	aUids.Reset();
       
  1752 	TInt count = iInterestedAPIs->Array().Count();
   805 	TInt count = iInterestedAPIs->Array().Count();
  1753 	for(TInt i=0; (i<count) && (err == KErrNone); i++)
   806 	for(TInt i=0; (i<count) && (err == KErrNone); i++)
  1754 		{
   807 		{
  1755 		CRemConInterfaceDetails* details = iInterestedAPIs->Array()[i];
   808 		CRemConInterfaceDetails* details = iInterestedAPIs->Array()[i];
  1756 		ASSERT_DEBUG(details);
   809 		ASSERT_DEBUG(details);
  1760 	return err;
   813 	return err;
  1761 	}
   814 	}
  1762 
   815 
  1763 TInt CRemConSession::SupportedBulkInterfaces(RArray<TUid>& aUids)
   816 TInt CRemConSession::SupportedBulkInterfaces(RArray<TUid>& aUids)
  1764 	{
   817 	{
  1765 	LOG_FUNC
   818 	LOG_FUNC;
       
   819 
       
   820     	aUids.Reset();
       
   821 	return AppendSupportedBulkInterfaces(aUids);
       
   822 	}
       
   823 
       
   824 TInt CRemConSession::AppendSupportedBulkInterfaces(RArray<TUid>& aUids)
       
   825 	{
       
   826 	LOG_FUNC;
  1766 	ASSERT_DEBUG(iInterestedAPIs);
   827 	ASSERT_DEBUG(iInterestedAPIs);
  1767 	TInt err = KErrNone;
   828 	TInt err = KErrNone;
  1768 	
   829 
  1769 	aUids.Reset();
       
  1770 	TInt count = iInterestedAPIs->Array().Count();
   830 	TInt count = iInterestedAPIs->Array().Count();
  1771 	for(TInt i=0; (i<count) && (err == KErrNone); i++)
   831 	for(TInt i=0; (i<count) && (err == KErrNone); i++)
  1772 		{
   832 		{
  1773 		CRemConInterfaceDetails* details = iInterestedAPIs->Array()[i];
   833 		CRemConInterfaceDetails* details = iInterestedAPIs->Array()[i];
  1774 		ASSERT_DEBUG(details);
   834 		ASSERT_DEBUG(details);
  1775 		if(details->IsBulk())
   835 		if(details->IsBulk())
  1776 			{
   836 			{
  1777 			err = aUids.Append(details->Uid());
   837 			err = aUids.Append(details->Uid());
  1778 			}
   838 			}
  1779 		}
   839 		}
  1780 	
   840 
  1781 	return err;
   841 	return err;
  1782 	}
   842 	}
  1783 
   843 
  1784 TInt CRemConSession::SupportedOperations(TUid aInterfaceUid, RArray<TUint>& aOperations)
   844 TInt CRemConSession::SupportedOperations(TUid aInterfaceUid, RArray<TUint>& aOperations)
  1785 	{
   845 	{
  1786 	LOG_FUNC
   846 	LOG_FUNC;
  1787 	TInt err = KErrNotSupported;
   847 	TInt err = KErrNotSupported;
  1788 	CRemConInterfaceDetails* details = FindInterfaceByUid(aInterfaceUid);
   848 	CRemConInterfaceDetails* details = FindInterfaceByUid(aInterfaceUid);
  1789 	
   849 	
  1790 	if(details)
   850 	if(details)
  1791 		{
   851 		{
  1794 	return err;
   854 	return err;
  1795 	}
   855 	}
  1796 
   856 
  1797 CRemConInterfaceDetails* CRemConSession::FindInterfaceByUid(TUid aUid) const
   857 CRemConInterfaceDetails* CRemConSession::FindInterfaceByUid(TUid aUid) const
  1798 	{
   858 	{
  1799 	LOG_FUNC
   859 	LOG_FUNC;
  1800 	ASSERT_DEBUG(iInterestedAPIs);
   860 	ASSERT_DEBUG(iInterestedAPIs);
  1801 	TInt count = iInterestedAPIs->Array().Count();
   861 	TInt count = iInterestedAPIs->Array().Count();
  1802 	for(TInt ix=0; ix<count; ++ix)
   862 	for(TInt ix=0; ix<count; ++ix)
  1803 		{
   863 		{
  1804 		CRemConInterfaceDetails* details = iInterestedAPIs->Array()[ix];
   864 		CRemConInterfaceDetails* details = iInterestedAPIs->Array()[ix];
  1809 			}
   869 			}
  1810 		}
   870 		}
  1811 	return NULL;
   871 	return NULL;
  1812 	}
   872 	}
  1813 
   873 
  1814 
   874 void CRemConSession::DoSendNext()
       
   875 	{
       
   876 	LOG_FUNC;
       
   877 
       
   878 	ASSERT_DEBUG(iSendQueue);
       
   879 	CRemConMessage& msg = iSendQueue->First();
       
   880 	iSendQueue->Remove(msg);
       
   881 	SendToServer(msg);
       
   882 	}
       
   883 
       
   884 TInt CRemConSession::SendNextCb(TAny *aThis)
       
   885 	{
       
   886 	LOG_STATIC_FUNC;
       
   887 
       
   888 	static_cast<CRemConSession*>(aThis)->DoSendNext();
       
   889 	return KErrNone;
       
   890 	}
       
   891