linklayercontrol/networkinterfacemgr/src/CStateMachineAgentBase.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2002-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 // The CStateMachineAgentBase base class implementation
       
    15 // CStateMachineAgentBase is an Agent that owns a State Machine
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  @file CStateMachineAgentBase.cpp
       
    21 */
       
    22 
       
    23 #include "CStateMachineAgentBase.h"
       
    24 #include "AgentPanic.h"
       
    25 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    26 #include <nifman_internal.h>
       
    27 #endif
       
    28 
       
    29 /**
       
    30 State Machine Agent Panic
       
    31 @internalComponent
       
    32 */
       
    33 _LIT(KStateMachineAgentPanic, "StateMachineAgent");
       
    34 
       
    35 /**
       
    36 Panic - programming error!
       
    37 
       
    38 @internalComponent
       
    39 @param aPanic the reason for panicking
       
    40 */
       
    41 GLDEF_C void StateMachineAgentPanic(Agent::TStateMachineAgentPanic aPanic)
       
    42 	{
       
    43 	LOG( NifmanLog::Printf(_L("CStateMachineAgentBase Panic %d"), aPanic); )
       
    44 	User::Panic(KStateMachineAgentPanic, aPanic);
       
    45 	}
       
    46 
       
    47 
       
    48 //
       
    49 //                                //
       
    50 //  Construction and Destruction  //
       
    51 //                                //
       
    52 //
       
    53 
       
    54 EXPORT_C CStateMachineAgentBase::~CStateMachineAgentBase()
       
    55 /**
       
    56 Destructor.
       
    57 Deletes objects used by CStateMachineAgentBase
       
    58 */
       
    59 	{
       
    60 
       
    61 	if (iStateMachine)
       
    62 		delete iStateMachine;
       
    63 	
       
    64 	if (iNotifyCb)
       
    65 		delete iNotifyCb;
       
    66 	}
       
    67 
       
    68 EXPORT_C CStateMachineAgentBase::CStateMachineAgentBase()
       
    69 : iNotifyCbOp(EUndefined), iNotifyCbError(KErrNone), iDisconnectReason(KErrNone)
       
    70 /**
       
    71 Default Constructor
       
    72 */
       
    73 	{ }
       
    74 
       
    75 
       
    76 EXPORT_C void CStateMachineAgentBase::ConstructL()
       
    77 /**
       
    78 2nd Phase of construction. Calls CAgentBase::ConstructL().
       
    79 
       
    80 iStateMachine should be instantiated by the derived class.
       
    81 
       
    82 Leaves if memory allocation fails or the base class constructor leaves.
       
    83 
       
    84 @see CAgentBase::ConstructL()
       
    85 */
       
    86 	{
       
    87 
       
    88 	// construct the database and dialog processor
       
    89 	CAgentBase::ConstructL();
       
    90 
       
    91 	// create the callback for completing the connect
       
    92 	TCallBack callback(NotifyCbComplete, this);
       
    93 	iNotifyCb = new (ELeave) CAsyncCallBack(callback, CActive::EPriorityStandard);
       
    94 	}
       
    95 
       
    96 
       
    97 //
       
    98 //                                                     //
       
    99 //  Partial implementation of CNifAgentBase interface  //
       
   100 //                                                     //
       
   101 //
       
   102 
       
   103 EXPORT_C void CStateMachineAgentBase::Connect(TAgentConnectType aType)
       
   104 /**
       
   105 Request that the State Machine establish a connection.
       
   106 
       
   107 @param aType the type of connection to make 
       
   108 (Outgoing, Reconnect, Callback, None, Incoming)
       
   109 */
       
   110 	{
       
   111 	LOG_DETAILED( NifmanLog::Printf(_L("StateMachAgent %x:\tConnect(%d)"), this, aType); )
       
   112 
       
   113 	switch(aType)
       
   114 		{
       
   115 		case EAgentStartDialOut:
       
   116 		case EAgentStartDialIn:
       
   117 			TRAPD(err, CreateAndStartStateMachineL());
       
   118 			if (err!=KErrNone)
       
   119 				{
       
   120 				ConnectionComplete(err);
       
   121 				}
       
   122 			break;
       
   123 
       
   124 		case EAgentReconnect:
       
   125 			__ASSERT_DEBUG(iStateMachine, StateMachineAgentPanic(Agent::ENullStateMachineOnReconnect));
       
   126 			iStateMachine->ConnectionContinuation(CAgentSMBase::EReconnect);
       
   127 			break;
       
   128 
       
   129 		case EAgentStartCallBack:
       
   130 			__ASSERT_DEBUG(iStateMachine, StateMachineAgentPanic(Agent::ENullStateMachineOnCallBack));
       
   131 			iStateMachine->ConnectionContinuation(CAgentSMBase::ECallBack);
       
   132 			break;
       
   133 
       
   134 		default:
       
   135 			StateMachineAgentPanic(Agent::EUnknownStartType);
       
   136 			break;
       
   137 		}
       
   138 	}
       
   139 
       
   140 void CStateMachineAgentBase::CreateAndStartStateMachineL()
       
   141 /**
       
   142 Create the appropriate state machine and start the connection
       
   143 
       
   144 @exception leaves if one of the database access or 
       
   145 the creation of the state machine leaves
       
   146 */
       
   147 	{
       
   148 
       
   149 	__ASSERT_DEBUG(!iStateMachine, StateMachineAgentPanic(Agent::ENonNullStateMachineOnCreate));
       
   150 	__ASSERT_DEBUG(iDatabase, AgentPanic(Agent::ENullDatabase));
       
   151 	iStateMachine = CreateAgentSML(*this, iDlgPrc, *iDatabase, iDatabase->GetConnectionDirection());
       
   152 
       
   153 	__ASSERT_DEBUG(iStateMachine, StateMachineAgentPanic(Agent::ENullStateMachineOnConnect));
       
   154 	iStateMachine->StartConnect();
       
   155 	}
       
   156 
       
   157 EXPORT_C void CStateMachineAgentBase::Connect(TAgentConnectType aType, CStoreableOverrideSettings* aOverrideSettings)
       
   158 /**
       
   159 Request that the State Machine establish a connection using Overrides.
       
   160 
       
   161 @param aType the type of connection to make (Outgoing, Reconnect, Callback, None, Incoming)
       
   162 @param aOverrideSettings the database overrides to use for this connection
       
   163 */
       
   164 	{
       
   165 	LOG_DETAILED( NifmanLog::Printf(_L("StateMachAgent %x:\tConnect(%d) with overrides(%x)"), this, aType, aOverrideSettings); )
       
   166 
       
   167 	__ASSERT_DEBUG(iDatabase, AgentPanic(Agent::ENullDatabase));
       
   168 
       
   169 	TRAPD(err, SetOverridesL(aOverrideSettings));
       
   170 	if (err!=KErrNone)
       
   171 		{
       
   172 		ConnectionComplete(err);
       
   173 		}
       
   174 	else
       
   175 		{
       
   176 		Connect(aType);
       
   177 		}
       
   178 	}
       
   179 
       
   180 EXPORT_C void CStateMachineAgentBase::CancelConnect()
       
   181 /**
       
   182 Cancel a previous request to Connect.
       
   183 */
       
   184 	{
       
   185 	LOG_DETAILED( NifmanLog::Printf(_L("StateMachAgent %x:\tCancelConnect()"), this); )
       
   186 
       
   187 	__ASSERT_DEBUG(iStateMachine, StateMachineAgentPanic(Agent::ENullStateMachineOnCancelConnect));
       
   188 
       
   189 	// cancel any pending ServiceStarted() or ConnectComplete() callbacks
       
   190 	if(iNotifyCb->IsActive() && (iNotifyCbOp==EConnectComplete || iNotifyCbOp==EServiceStarted))
       
   191 		{
       
   192 		iNotifyCb->Cancel();
       
   193 
       
   194 		LOG_DETAILED( NifmanLog::Printf(_L("StateMachAgent %x:\tCancelled NotifyCb() for op=%d"), this, iNotifyCbOp); )
       
   195 		}
       
   196 
       
   197 	iStateMachine->CancelConnect();
       
   198 	}
       
   199 
       
   200 EXPORT_C void CStateMachineAgentBase::Disconnect(TInt aReason)
       
   201 /**
       
   202 Request that the State Machine tears down a connection.
       
   203 
       
   204 @param aReason the reason for the request to disconnect
       
   205 */
       
   206 	{
       
   207 	LOG_DETAILED( NifmanLog::Printf(_L("StateMachAgent %x:\tDisconnect(%d)"), this, aReason); )
       
   208 
       
   209 	__ASSERT_DEBUG(iStateMachine, StateMachineAgentPanic(Agent::ENullStateMachineOnDisconnect));
       
   210 	
       
   211 	CAgentBase::CancelAuthenticate();
       
   212 
       
   213 	iDisconnectReason = aReason;
       
   214 	iStateMachine->ConnectionContinuation(CAgentSMBase::EDisconnect);
       
   215 	}
       
   216 
       
   217 EXPORT_C TInt CStateMachineAgentBase::GetExcessData(TDes8& aBuffer)
       
   218 /**
       
   219 Get Excess Data from the State Machine.
       
   220 
       
   221 @param aBuffer the buffer in which to store the excess data
       
   222 @todo Is this used?
       
   223 @return
       
   224 */
       
   225 	{
       
   226 
       
   227 	__ASSERT_DEBUG(iStateMachine, StateMachineAgentPanic(Agent::ENullStateMachineOnGetExcessData));
       
   228 	
       
   229 	return iStateMachine->GetExcessData(aBuffer);
       
   230 	}
       
   231 
       
   232 EXPORT_C TInt CStateMachineAgentBase::Notification(TNifToAgentEventType aEvent, TAny* aInfo)
       
   233 /**
       
   234 Notification from the NIF to the State Machine.
       
   235 
       
   236 NIFMAN does not interpret this message
       
   237 
       
   238 @param aEvent 
       
   239 @param aInfo 
       
   240 */
       
   241 	{
       
   242 
       
   243 	__ASSERT_DEBUG(iStateMachine, StateMachineAgentPanic(Agent::ENullStateMachineOnNotification));
       
   244 
       
   245 	return iStateMachine->Notification(aEvent, aInfo);
       
   246 	}
       
   247 
       
   248 EXPORT_C void CStateMachineAgentBase::GetLastError(TInt& aError)
       
   249 /**
       
   250 Return the last error from the State Machine.
       
   251 
       
   252 @param aError on return contains the error
       
   253 */
       
   254 	{
       
   255 
       
   256 	__ASSERT_DEBUG(iStateMachine, StateMachineAgentPanic(Agent::ENullStateMachineOnGetLastError));
       
   257 
       
   258 	iStateMachine->GetLastError(aError);
       
   259 	}
       
   260 
       
   261 EXPORT_C TBool CStateMachineAgentBase::IsReconnect() const
       
   262 /**
       
   263 Is this a reconnection? - required for Authenticate()
       
   264 
       
   265 @return ETrue if this is a reconnection,
       
   266          EFalse otherwise
       
   267 */
       
   268 	{
       
   269 
       
   270 	__ASSERT_DEBUG(iStateMachine, StateMachineAgentPanic(Agent::ENullStateMachineOnIsReconnect));
       
   271 
       
   272 	return iStateMachine->IsReconnect();
       
   273 	}
       
   274 
       
   275 //
       
   276 // Implementation of MAgentNotify interface
       
   277 //
       
   278 
       
   279 EXPORT_C void CStateMachineAgentBase::PreventConnectionRetries()
       
   280 /**
       
   281 @todo we need to work out how to do this & whether it should still be supported
       
   282 */
       
   283 	{
       
   284 	LOG_DETAILED( NifmanLog::Printf(_L("StateMachAgent %x:\tPreventConnectionRetries()"), this); )
       
   285 	}
       
   286 
       
   287 EXPORT_C void CStateMachineAgentBase::ServiceStarted()
       
   288 /**
       
   289 ServiceStarted.
       
   290 */
       
   291 	{
       
   292 	LOG_DETAILED( NifmanLog::Printf(_L("StateMachAgent %x:\tServiceStarted()"), this); )
       
   293 
       
   294 	CallNotifyCb(EServiceStarted, KErrNone);
       
   295 	}
       
   296 
       
   297 EXPORT_C void CStateMachineAgentBase::ConnectionComplete(TInt aProgress, TInt aError)
       
   298 /**
       
   299 ConnectionComplete upcall from the state machine
       
   300 
       
   301 @param aProgress
       
   302 @param aError on return contains the error
       
   303 */
       
   304 	{
       
   305 	LOG_DETAILED( NifmanLog::Printf(_L("StateMachAgent %x:\tConnectionComplete(%d, %d)"), this, aProgress, aError); )
       
   306 
       
   307 	UpdateProgress(aProgress,aError);
       
   308 	ConnectionComplete(aError);
       
   309 	}
       
   310 
       
   311 EXPORT_C void CStateMachineAgentBase::ConnectionComplete(TInt aError)
       
   312 /**
       
   313 ConnectionComplete upcall from the state machine
       
   314 
       
   315 @param aError on return contains the error
       
   316 */
       
   317 	{
       
   318 	LOG_DETAILED( NifmanLog::Printf(_L("StateMachAgent %x:\tConnectionComplete()"), this); )
       
   319 
       
   320 	CallNotifyCb(EConnectComplete, aError);
       
   321 	}
       
   322 
       
   323 EXPORT_C void CStateMachineAgentBase::DisconnectComplete()
       
   324 /**
       
   325 DisconnectComplete upcall from the state machine
       
   326 */
       
   327 	{
       
   328 	LOG_DETAILED( NifmanLog::Printf(_L("StateMachAgent %x:\tDisconnectComplete()"), this); )
       
   329 
       
   330 	delete iStateMachine;
       
   331 	iStateMachine = NULL;
       
   332 
       
   333 	CallNotifyCb(EDisconnectComplete, KErrNone);
       
   334 	}
       
   335 
       
   336 EXPORT_C void CStateMachineAgentBase::UpdateProgress(TInt aProgress, TInt aError)
       
   337 /**
       
   338 UpdateProgress upcall from the state machine
       
   339 
       
   340 @param aProgress
       
   341 @param aError on return contains the error
       
   342 */
       
   343 	{
       
   344 	__ASSERT_DEBUG(iNotify, AgentPanic(Agent::ENullNifmanNotifyPointer));
       
   345 
       
   346 	iNotify->AgentProgress(aProgress, aError);
       
   347 	}
       
   348 
       
   349 EXPORT_C TInt CStateMachineAgentBase::Notification(TAgentToNifEventType aEvent, TAny* aInfo)
       
   350 /**
       
   351 Notification upcall from the state machine
       
   352 
       
   353 @param aEvent
       
   354 @param aInfo
       
   355 */
       
   356 	{
       
   357 	__ASSERT_DEBUG(iNotify, AgentPanic(Agent::ENullNifmanNotifyPointer));
       
   358 
       
   359 	return iNotify->Notification(aEvent, aInfo);
       
   360 	}
       
   361 
       
   362 EXPORT_C TInt CStateMachineAgentBase::IncomingConnectionReceived()
       
   363 /**
       
   364 IncomingConnectionReceived upcall from the state machine
       
   365 
       
   366 @return
       
   367 */
       
   368 	{
       
   369 	__ASSERT_DEBUG(iNotify, AgentPanic(Agent::ENullNifmanNotifyPointer));
       
   370 
       
   371 	return iNotify->IncomingConnectionReceived();
       
   372 	}
       
   373 
       
   374 
       
   375 //
       
   376 //                                                 //
       
   377 //  Callback used for notifying NIFMAN             //
       
   378 //                                                 //
       
   379 //
       
   380 
       
   381 void CStateMachineAgentBase::CallNotifyCb(TNotifyOperation aOperation, TInt aError)
       
   382 /**
       
   383 CallNotifyCb
       
   384 
       
   385 @param aOperation the MNifAgtNotify function to call
       
   386 @param aError the error to pass to Nifman
       
   387 */
       
   388 	{
       
   389 	__ASSERT_DEBUG(!iNotifyCb->IsActive(), StateMachineAgentPanic(Agent::ENotifyCallbackAlreadyPending));
       
   390 
       
   391 	LOG_DETAILED( NifmanLog::Printf(_L("StateMachAgent %x:\tCallNotifyCb() op=%d, err=%d"), this, aOperation, aError); )
       
   392 
       
   393 	iNotifyCbOp = aOperation;
       
   394 	iNotifyCbError = aError;
       
   395 	iNotifyCb->CallBack();
       
   396 	}
       
   397 
       
   398 TInt CStateMachineAgentBase::NotifyCbComplete(TAny* aThisPtr)
       
   399 /**
       
   400 NotifyCbComplete
       
   401 
       
   402 @param aThisPtr pointer to the instance object that triggered the callback
       
   403 @return KErrNone
       
   404 */
       
   405 	{
       
   406 	LOG_DETAILED( NifmanLog::Printf(_L("StateMachAgent:\t\tNotifyCbComplete(%x)"), aThisPtr); )
       
   407 
       
   408 	((CStateMachineAgentBase*)aThisPtr)->DoNotify();
       
   409 	return KErrNone;
       
   410 	}
       
   411 
       
   412 void CStateMachineAgentBase::DoNotify()
       
   413 /**
       
   414 Notify NIFMAN that part of the connection process has completed
       
   415 
       
   416 */
       
   417 	{
       
   418 	LOG_DETAILED( NifmanLog::Printf(_L("StateMachAgent %x:\tDoNotify() op=%d, err=%d"), this, iNotifyCbOp, iNotifyCbError); )
       
   419 
       
   420 	__ASSERT_DEBUG(iNotify, AgentPanic(Agent::ENullNifmanNotifyPointer));
       
   421 
       
   422 	TNotifyOperation notifyCbOp = iNotifyCbOp;
       
   423 	TInt notifyCbError = iNotifyCbError;
       
   424 
       
   425 	iNotifyCbOp = EUndefined;
       
   426 	iNotifyCbError = KErrNone;
       
   427 
       
   428 	switch(notifyCbOp)
       
   429 		{
       
   430 		case EConnectComplete:
       
   431 			{
       
   432 			iNotify->ConnectComplete(notifyCbError);
       
   433 			break;
       
   434 			}
       
   435 		case EServiceStarted:
       
   436 			{
       
   437 			iNotify->ServiceStarted();
       
   438 			break;
       
   439 			}
       
   440 		case EDisconnectComplete:
       
   441 			{
       
   442 			iNotify->DisconnectComplete();
       
   443 			break;
       
   444 			}
       
   445 		default:
       
   446 			StateMachineAgentPanic(Agent::EUndefinedNotifyOperation);
       
   447 			break;
       
   448 		}
       
   449 	}
       
   450