remotecontrol/remotecontrolfw/client/intermediate/src/bulkreceiver.cpp
changeset 51 20ac952a623c
equal deleted inserted replaced
48:22de2e391156 51:20ac952a623c
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalComponent
       
    21 */
       
    22 
       
    23 #include <comms-infras/commsdebugutility.h>
       
    24 #include <e32atomics.h>
       
    25 #include "bulkreceiver.h"
       
    26 #include "remconclient.h"
       
    27 #include "remconbulkclient.h"
       
    28 #include "utils.h"
       
    29 
       
    30 #include <bluetooth/logger.h>
       
    31 
       
    32 #ifdef __FLOG_ACTIVE
       
    33 _LIT8(KLogComponent, LOG_COMPONENT_REMCON_IF_BASE);
       
    34 #endif
       
    35 
       
    36 #ifdef _DEBUG
       
    37 PANICCATEGORY("bulkrecv");
       
    38 #endif // _DEBUG
       
    39 
       
    40 CBulkReceiver* CBulkReceiver::NewL(CRemConInterfaceSelector& aObserver)
       
    41 	{
       
    42 	LOG_STATIC_FUNC;
       
    43 	CBulkReceiver* self = new(ELeave) CBulkReceiver(aObserver);
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL();
       
    46 	CleanupStack::Pop(self);
       
    47 	return self;
       
    48 	}
       
    49 
       
    50 CBulkReceiver::~CBulkReceiver()
       
    51 	{
       
    52 	LOG_FUNC;
       
    53 	Cancel();
       
    54 	iConnectCall.Close();
       
    55 	iData.Close();
       
    56 	}
       
    57 
       
    58 CBulkReceiver::CBulkReceiver(CRemConInterfaceSelector& aObserver)
       
    59 	: CActive(CActive::EPriorityStandard)
       
    60 	, iObserver(aObserver)
       
    61 	{
       
    62 	LOG_FUNC;
       
    63 	}
       
    64 
       
    65 void CBulkReceiver::ConstructL()
       
    66 	{
       
    67 	LOG_FUNC;
       
    68 	TCallBack connectCb(StaticConnect, this);
       
    69 	LEAVEIFERRORL(iConnectCall.Create(connectCb, EPriorityStandard));
       
    70 	BindToCurrentThreadL();
       
    71 	}
       
    72 
       
    73 void CBulkReceiver::BindToCurrentThreadL()
       
    74 	{
       
    75 	LOG_FUNC;
       
    76 	ASSERT_DEBUG(!IsAdded());
       
    77 	TRAPD(err, DoBindToCurrentThreadL());
       
    78 	if(err != KErrNone)
       
    79 		{
       
    80 		this->Deque();
       
    81 		LEAVEL(err);
       
    82 		}
       
    83 	}
       
    84 
       
    85 void CBulkReceiver::DoBindToCurrentThreadL()
       
    86 	{
       
    87 	LOG_FUNC;
       
    88 	CActiveScheduler::Add(this);
       
    89 	LEAVEIFERRORL(iConnectCall.Start());
       
    90 	iThreadId = RThread().Id();
       
    91 	}
       
    92 
       
    93 TBool CBulkReceiver::InitialisationRequired() const
       
    94 	{
       
    95 	LOG_FUNC;
       
    96 	return IsAdded();
       
    97 	}
       
    98 
       
    99 TInt CBulkReceiver::StaticConnect(TAny* aSelf)
       
   100 	{
       
   101 	LOG_STATIC_FUNC;
       
   102 	ASSERT_DEBUG(aSelf);
       
   103 	return reinterpret_cast<CBulkReceiver*>(aSelf)->Connect();
       
   104 	}
       
   105 
       
   106 TInt CBulkReceiver::Connect()
       
   107 	{
       
   108 	LOG_FUNC;
       
   109 	// issue the connect from the bulk thread.
       
   110 	TRAPD(err, iObserver.BulkSessionConnectL());
       
   111 	if(err == KErrNone)
       
   112 		{
       
   113 		// Start the perpetual receive cycle.
       
   114 		Receive();
       
   115 		}
       
   116 	return err;
       
   117 	}
       
   118 
       
   119 void CBulkReceiver::InitialiseL(RRemConBulk& aRemConBulk, TUint aMaxDataLength)
       
   120 	{
       
   121 	LOG_FUNC;
       
   122 	iData.CreateL(aMaxDataLength);
       
   123 	iRemConBulk = &aRemConBulk;
       
   124 	// NB If a remote sends longer data than we expect to receive here, our 
       
   125 	// receive will be errored and the message will effectively be dropped in 
       
   126 	// the intermediate layer. (It won't get passed to the outer layer.)
       
   127 	
       
   128 	// Receive is queued once we have a connected bulk server session
       
   129 	}
       
   130 
       
   131 void CBulkReceiver::Receive()
       
   132 	{
       
   133 	LOG_FUNC
       
   134 	ASSERT_DEBUG(iRemConBulk);
       
   135 	iRemConBulk->Receive(iStatus, iInterfaceUid, iOperationId, iData);
       
   136 	SetActive();
       
   137 	}
       
   138 
       
   139 void CBulkReceiver::RunL()
       
   140 	{
       
   141 	LOG_FUNC
       
   142 	LOG1(_L8("\tiStatus = %d"), iStatus.Int());
       
   143 
       
   144 	TInt err = iStatus.Int();
       
   145 	
       
   146 	if(err == KErrNone)
       
   147 		{
       
   148 		iObserver.BulkReceiveComplete(iInterfaceUid, iOperationId, iData);
       
   149 		}
       
   150 	else
       
   151 		{
       
   152 		iObserver.BulkError(err);
       
   153 		}
       
   154 	
       
   155 	if(err != KErrServerTerminated)
       
   156 		{
       
   157 		// Repost request.
       
   158 		Receive();
       
   159 		}
       
   160 	}
       
   161 
       
   162 void CBulkReceiver::DoCancel()
       
   163 	{
       
   164 	LOG_FUNC
       
   165 	
       
   166 	if(RThread().Id() == iThreadId)
       
   167 		{
       
   168 		// There's nothing we can do about any error here, and it probably 
       
   169 		// indicates that the server has gone away anyway for some reason.
       
   170 		ASSERT_DEBUG(iRemConBulk);
       
   171 		static_cast<void>(iRemConBulk->ReceiveCancel());
       
   172 		
       
   173 		// No need to cancel the "connecting" request as it is a synchronous kick
       
   174 		}
       
   175 	else
       
   176 		{
       
   177 		// being run in a different thread - this means that something has gone
       
   178 		// wrong and we merely want to tidy up the CActive state (i.e. not active).
       
   179 		TRequestStatus* status = &iStatus;
       
   180 		User::RequestComplete(status, KErrCancel);
       
   181 		}
       
   182 	}
       
   183 
       
   184 /**
       
   185 This function deals with connecting to the bulk server - it handles the
       
   186 cases where the bulk interfaces are running in the same thread as the control
       
   187 interfaces, and also when running in a different thread (which is as one might
       
   188 expect more complicated). 
       
   189 */
       
   190 void CBulkReceiver::WaitUntilConnectedL()
       
   191 	{
       
   192 	LOG_FUNC;
       
   193 	LEAVEIFERRORL(iConnectCall.CallBack());
       
   194 	}
       
   195 
       
   196 
       
   197