usbmgmt/usbmgrtest/t_ncm/src/simpancommand.cpp
changeset 28 f1fd07aa74c9
equal deleted inserted replaced
27:2fefb5a2b416 28:f1fd07aa74c9
       
     1 /*
       
     2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 /** @file
       
    19  @internalComponent
       
    20  @test
       
    21  */
       
    22 
       
    23 #include <e32property.h> 
       
    24 
       
    25 #include "simpancommand.h"
       
    26 #include "ncmtestconsole.h"
       
    27 
       
    28 static _LIT_SECURITY_POLICY_PASS(KAllowAllPolicy);
       
    29 static _LIT_SECURITY_POLICY_C1(KNetworkControlPolicy, ECapabilityNetworkControl); 
       
    30 
       
    31 _LIT(KSimPANCommandActive, "Simulate PAN connection is active");
       
    32 _LIT(KSimPANCommandNone, "Simulate PAN connection is not active");
       
    33 
       
    34 CSimPANCommand* CSimPANCommand::NewL(CUsbNcmConsole& aTestConsole, TUint aKey)
       
    35 	{
       
    36 	LOG_STATIC_FUNC_ENTRY
       
    37 	
       
    38 	CSimPANCommand* self = new(ELeave) CSimPANCommand(aTestConsole, aKey);
       
    39 	CleanupStack::PushL(self);
       
    40 	self->ConstructL();
       
    41 	CleanupStack::Pop(self);
       
    42 	return self;
       
    43 	}
       
    44 CSimPANCommand::~CSimPANCommand()
       
    45 	{
       
    46 	Cancel();
       
    47 	iNcmExclusiveProp.Close();
       
    48 	}
       
    49 
       
    50 CSimPANCommand::CSimPANCommand(CUsbNcmConsole& aTestConsole, TUint aKey)
       
    51 	: CNcmCommandBase(EPriorityStandard, aTestConsole, aKey)
       
    52 	{
       
    53 	}
       
    54 
       
    55 void CSimPANCommand::ConstructL()
       
    56 	{
       
    57 	//Attach the property of Exclusive state
       
    58 	User::LeaveIfError(iNcmExclusiveProp.Attach(KC32ExeSid, KIPBearerCoexistenceProperty));
       
    59 	CActiveScheduler::Add(this);
       
    60 	iNcmExclusiveProp.Subscribe(iStatus);
       
    61 	SetActive();
       
    62 	
       
    63 	//Get the current value of exclusive state
       
    64 	TInt state; 
       
    65 	TInt err = iNcmExclusiveProp.Get(state);
       
    66 	switch(err)
       
    67 		{
       
    68 		case KErrNotFound:
       
    69 			//The property is not defined. means BtPAN and Ncm didn't start.
       
    70 			DisplayExclusive(ENoneIsActive);
       
    71 			break;
       
    72 		case KErrNone:
       
    73 			//Display the exclusive state on main console
       
    74 			DisplayExclusive(state);
       
    75 			break;
       
    76 		default:
       
    77 			User::LeaveIfError(err);
       
    78 			break;
       
    79 		}
       
    80  
       
    81 	}
       
    82 
       
    83 void CSimPANCommand::RunL()
       
    84 /**
       
    85 Get the current exclusive state and update on main console when the state changed
       
    86 */	
       
    87 	{
       
    88 	LOG_FUNC
       
    89 	__FLOG_STATIC1(KSubSys, KLogComponent , _L8("CNcmExclusiveStateAO::RunL - iStatus = %d"), iStatus.Int());
       
    90 
       
    91 	User::LeaveIfError(iStatus.Int());
       
    92 
       
    93 	iNcmExclusiveProp.Subscribe(iStatus);
       
    94 	SetActive();
       
    95 	
       
    96 	TInt exclusiveState; 
       
    97 	TInt err = iNcmExclusiveProp.Get(exclusiveState);
       
    98 
       
    99 	__FLOG_STATIC2(KSubSys, KLogComponent , _L8("Get[IPBearerCoexistence]=%d, err=%d"), exclusiveState, err);
       
   100 
       
   101 	if(err == KErrNone)
       
   102 		{
       
   103 		//Display the exclusive state on main console
       
   104 		DisplayExclusive(exclusiveState);
       
   105 		}	
       
   106 	}
       
   107 
       
   108 void CSimPANCommand::DoCancel()
       
   109 	{
       
   110 	iNcmExclusiveProp.Cancel();
       
   111 	}
       
   112 
       
   113 TInt CSimPANCommand::RunError(TInt aError)
       
   114 	{
       
   115 	User::Panic(_L("CSimPANCommand"), aError);
       
   116 	return aError;
       
   117 	}
       
   118 
       
   119 void CSimPANCommand::DoCommandL()
       
   120 /**
       
   121 Main function of simualate Pan active/inactive. 
       
   122 It will try to get the exclusive state from P&S key. And set the value based on the iSetPAN.
       
   123 There's three value for this P&S key: EExclusiveNone,ENcmOwned,EBtPanOwned
       
   124 
       
   125 If now the command is 'active PAN'(iSetPAN=ETrue), then set the value of property to Pan active.
       
   126 If now the command is 'inactive PAN'(iSetPAN=EFalse), then set the value of property to Pan inactive.
       
   127 */	
       
   128 	{
       
   129 	LOG_FUNC
       
   130 		
       
   131 	//Get the mutex which used to protect the P&S key.
       
   132 	RMutex mtx;
       
   133 	TInt ret;
       
   134 	ret = mtx.CreateGlobal(KIPBearerCoexistenceMutex);
       
   135 	if (ret == KErrAlreadyExists)
       
   136 		{
       
   137 		ret  = mtx.OpenGlobal(KIPBearerCoexistenceMutex);
       
   138 		}	
       
   139 	User::LeaveIfError(ret);
       
   140 	__FLOG_STATIC0(KSubSys, KLogComponent , _L8("Get Mutex, wait()"));
       
   141 	CleanupClosePushL(mtx);
       
   142  	mtx.Wait();
       
   143  	
       
   144  	RProperty prop;
       
   145  	TInt state;
       
   146  	TBool bSet = ETrue;
       
   147  	
       
   148  	//Get the exclusive state.
       
   149  	ret = prop.Get(KC32ExeSid, KIPBearerCoexistenceProperty, state);
       
   150 	CleanupClosePushL(prop);
       
   151  	__FLOG_STATIC2(KSubSys, KLogComponent , _L8("Get Property[IPBearerCoexistence]: ret=%d, val=%d"), ret, state);
       
   152  	
       
   153  	switch(ret)
       
   154  		{
       
   155  		case KErrNotFound:
       
   156  			{
       
   157 	 		//The property not exists. (NCM not start.)
       
   158 	
       
   159 		 	__FLOG_STATIC0(KSubSys, KLogComponent , _L8("Not define property yet. define it"));		
       
   160 			CUsbNcmConsoleEvent* event = CUsbNcmConsoleEvent::NewL();
       
   161 			event->iEvent.AppendFormat(_L("Not define property yet. define it."));
       
   162 			iTestConsole.NotifyEvent(event);
       
   163 	
       
   164 			//Define the property
       
   165 			User::LeaveIfError(RProperty::Define(KC32ExeSid, KIPBearerCoexistenceProperty, RProperty::EInt, KAllowAllPolicy, KNetworkControlPolicy));
       
   166 		 	__FLOG_STATIC0(KSubSys, KLogComponent , _L8("Define Property[IPBearerCoexistence] ok"));
       
   167 		 	break;
       
   168  			}
       
   169  		case KErrNone:
       
   170  			{
       
   171 	 		//property exists.
       
   172 	 		if(state == ENcmIsActive)
       
   173 	 			{
       
   174 	 			//Ncm hold the key. PAN can't active. Do nothing in this command.
       
   175 	 			CUsbNcmConsoleEvent* event = CUsbNcmConsoleEvent::NewL();
       
   176 	 			event->iEvent.AppendFormat(_L("NCM hold the P&S key!"));
       
   177 	 			iTestConsole.NotifyEvent(event); 	
       
   178 	 			bSet = EFalse;
       
   179 	 			}
       
   180 	 		break;
       
   181  			}
       
   182  		default:
       
   183  			{
       
   184  			//error when get P&S key.
       
   185  			CUsbNcmConsoleEvent* event = CUsbNcmConsoleEvent::NewL();
       
   186  			event->iEvent.AppendFormat(_L("Get P&S key error! err = %d"), ret);
       
   187  			iTestConsole.NotifyEvent(event);
       
   188  			bSet = EFalse;
       
   189  			break;
       
   190  			}
       
   191  		}
       
   192  	
       
   193  	if(bSet)
       
   194  		{
       
   195  		//Set the property value
       
   196 		CUsbNcmConsoleEvent* event = CUsbNcmConsoleEvent::NewL();
       
   197 		CleanupStack::PushL(event);
       
   198  		if(this->iSetPAN)
       
   199  			{
       
   200  			User::LeaveIfError(prop.Set(KC32ExeSid, KIPBearerCoexistenceProperty, EBTPanIsActive));
       
   201  	 		__FLOG_STATIC1(KSubSys, KLogComponent , _L8("Set Property[IPBearerCoexistence]: val = %d"), EBTPanIsActive);		
       
   202  			event->iEvent.AppendFormat(_L("Set PAN active! prop=%d"), EBTPanIsActive);			
       
   203  			}
       
   204  		else
       
   205  			{
       
   206  			User::LeaveIfError(prop.Set(KC32ExeSid, KIPBearerCoexistenceProperty, ENoneIsActive));
       
   207  	 		__FLOG_STATIC1(KSubSys, KLogComponent , _L8("Set Property[IPBearerCoexistence]: val = %d"), ENoneIsActive);		
       
   208 			event->iEvent.AppendFormat(_L("Set PAN disactive! prop=%d"), ENoneIsActive);
       
   209 			}
       
   210  		CleanupStack::Pop(event);
       
   211 		iTestConsole.NotifyEvent(event);
       
   212 					
       
   213  		}
       
   214  	
       
   215  	CleanupStack::Pop(2, &mtx); //prop, mtx
       
   216  	mtx.Signal();
       
   217  	prop.Close();
       
   218  	mtx.Close();
       
   219  	
       
   220 	}
       
   221 
       
   222 void CSimPANCommand::ChangeDescription()
       
   223 	{
       
   224 	if(iSetPAN)
       
   225 		{
       
   226 		SetDescription(KSimPANCommandActive());
       
   227 		}
       
   228 	else
       
   229 		{
       
   230 		SetDescription(KSimPANCommandNone());
       
   231 		}
       
   232 	}
       
   233 
       
   234 void CSimPANCommand::DisplayExclusive(TInt aState)
       
   235 /**
       
   236 Display the exclusive state on main console and change the description of command by the
       
   237 value of aState
       
   238   @param  aState 	Exclusive state
       
   239 */
       
   240 	{
       
   241 	TBuf<DISPLAY_NCM_BTPAN_LEN>	exclusive;
       
   242 	iSetPAN = ETrue;
       
   243 	switch(aState)
       
   244 		{
       
   245 		case ENoneIsActive:
       
   246 			{
       
   247 			exclusive = _L("None    ");
       
   248 			}
       
   249 			break;
       
   250 		case ENcmIsActive:
       
   251 			{
       
   252 			exclusive = _L("NCM   ");
       
   253 			}
       
   254 			break;
       
   255 		case EBTPanIsActive:
       
   256 			{
       
   257 			exclusive = _L("BT PAN  ");
       
   258 			//If Bt Pan is active, the command description should be 'disactive BT PAN'
       
   259 			iSetPAN = EFalse;
       
   260 			}
       
   261 			break;
       
   262 		default:
       
   263 			exclusive = _L("        ");
       
   264 			break;
       
   265 		}
       
   266 	
       
   267 	ChangeDescription();
       
   268 	iTestConsole.SetDisplayItem(ENcmBtPanItem, exclusive);
       
   269 	}