usbmgmt/usbmgrtest/t_ncm/src/setncmiapcommand.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 "setncmiapcommand.h"
       
    24 #include "ncmtestconsole.h"
       
    25 
       
    26 _LIT(KSetNcmIapCommandDescription, "Set Ncm Iap ID");
       
    27 
       
    28 CSetNcmIapCommand* CSetNcmIapCommand::NewL(CUsbNcmConsole& aTestConsole, TUint aKey)
       
    29 	{
       
    30 	LOG_STATIC_FUNC_ENTRY
       
    31 	
       
    32 	CSetNcmIapCommand* self = new(ELeave) CSetNcmIapCommand(aTestConsole, aKey);
       
    33 	CleanupStack::PushL(self);
       
    34 	self->ConstructL();
       
    35 	CleanupStack::Pop(self);
       
    36 	return self;
       
    37 	}
       
    38 CSetNcmIapCommand::~CSetNcmIapCommand()
       
    39 	{
       
    40 	EndWork();
       
    41 	}
       
    42 
       
    43 CSetNcmIapCommand::CSetNcmIapCommand(CUsbNcmConsole& aTestConsole, TUint aKey)
       
    44 	: CNcmCommandBase(EPriorityStandard, aTestConsole, aKey)
       
    45 	{
       
    46 	CActiveScheduler::Add(this);
       
    47 	}
       
    48 
       
    49 void CSetNcmIapCommand::ConstructL()
       
    50 	{
       
    51 	SetDescription(KSetNcmIapCommandDescription());
       
    52 	}
       
    53 
       
    54 void CSetNcmIapCommand::DoCommandL()
       
    55 /**
       
    56 Main function of set ncm iap. 
       
    57 It will start a new console to display all Iaps in commsdb and current setting for ncm. 
       
    58 Then prompt user to input ncm iap id and the index of this iap will be placed in central 
       
    59 repository. Press 'ESC' for give up this function. After the setting is successful, the 
       
    60 console will be destroyed and return to main console
       
    61 */	
       
    62 	{
       
    63 	LOG_FUNC
       
    64 
       
    65 	if(!IsActive())
       
    66 		{
       
    67 		//If the console has existed, close the old console. The work on old console will be 
       
    68 		//gived up.
       
    69 		if(iConsole)
       
    70 			{
       
    71 			__FLOG_STATIC0(KSubSys, KLogComponent , _L8("Close old console"));
       
    72 			EndWork();
       
    73 			}
       
    74 		InitWorkL();
       
    75 		
       
    76 		iConsole->Printf(_L("Press \'ESC\' to exit without change\n"));
       
    77 		
       
    78 		//Display all iaps in commsdb
       
    79 		ListAllNcmIapL();
       
    80 	
       
    81 		//wait user input iap id
       
    82 		iConsole->Printf(KSetIapMemo());
       
    83 		iCommandState = EIapId;
       
    84 		iConsole->Read(iStatus);
       
    85 		SetActive();
       
    86 		}				
       
    87 	}
       
    88 
       
    89 void CSetNcmIapCommand::ListAllNcmIapL()
       
    90 /**
       
    91 Display all iaps in commsdb on console and get current config in cental repoistory
       
    92 */
       
    93 	{
       
    94 	LOG_FUNC
       
    95 	
       
    96 	//Display commsdb's iaps
       
    97 	TInt iapcount = iTestConsole.DrawAvailableIapsL();
       
    98 
       
    99 	TInt iapId = 0;
       
   100 
       
   101 	//Get iap config from central repository
       
   102 	TInt err = iRepository->FindL(KPartialKey, KMask, iRows);
       
   103 	if(err == KErrNone)
       
   104 		{
       
   105 		TUint count = iRows.Count();
       
   106 		for (TInt row = 0; row < count; ++row)
       
   107 		    {
       
   108 		    // Read each IAP ID
       
   109 		    User::LeaveIfError(iRepository->Get(iRows[row], iapId));
       
   110 			__FLOG_STATIC2(KSubSys, KLogComponent , _L8("index[%d]: iap = %d"), row, iapId);		    
       
   111 		    iConsole->Printf(_L("index[%d]: iap = %d\n"), row, iapId);
       
   112 		    }  
       
   113 		}
       
   114 	else
       
   115 		{
       
   116 		__FLOG_STATIC1(KSubSys, KLogComponent , _L8("FindL() err[%d]"), err);		    		
       
   117 		}
       
   118 	}
       
   119 
       
   120 void CSetNcmIapCommand::GetDefaultIapL(TInt& aDefaultIap)
       
   121 /**
       
   122 Get the default iap id (The first one) stored in central repository. 
       
   123 @return the default iap id
       
   124 */
       
   125 	{
       
   126 	LOG_FUNC
       
   127 	aDefaultIap = 13;
       
   128 	return ;
       
   129 	}
       
   130 
       
   131 void CSetNcmIapCommand::SetNcmIapL()
       
   132 /**
       
   133 Set iap id into central repository. The iap id is iIapId. The place of iap id in central
       
   134 repository is iIndex. iIndex is started from 0. If the iIndex is not exist in central 
       
   135 repository, iIapId will be placed at last place. 
       
   136 */
       
   137 	{
       
   138 	LOG_FUNC
       
   139 	
       
   140 	TUint count = iRows.Count();
       
   141 	__FLOG_STATIC1(KSubSys, KLogComponent , _L8("now iap count = %d"), count);
       
   142 
       
   143 	TInt idx;
       
   144 	if(iIndex >= count)
       
   145 		{
       
   146 		//iIndex is not exist in central repository, create a new entry in central repository
       
   147 		idx = iRows[count-1]+1;
       
   148 		User::LeaveIfError(iRepository->Create(idx, iIapId));
       
   149 		__FLOG_STATIC2(KSubSys, KLogComponent , _L8("add iap: idx = %d, iIapId"), idx, iIapId);		
       
   150 		}
       
   151 	else
       
   152 		{
       
   153 		//iIndex exists, just set in the new value
       
   154 		idx = iRows[iIndex];
       
   155 		User::LeaveIfError(iRepository->Set(idx, iIapId));
       
   156 		__FLOG_STATIC2(KSubSys, KLogComponent , _L8("set iap: aIdx = %d, iIapId"), idx, iIapId);		
       
   157 		}
       
   158 
       
   159 	}
       
   160 
       
   161 void CSetNcmIapCommand::RunL()
       
   162 	{
       
   163 	LOG_FUNC
       
   164 	__FLOG_STATIC1(KSubSys, KLogComponent , _L8("CSetNcmIapCommand::RunL - iStatus = %d"), iStatus.Int());
       
   165 
       
   166 	User::LeaveIfError(iStatus.Int());
       
   167 
       
   168 	TKeyCode code = iConsole->KeyCode();
       
   169 	switch(code)
       
   170 		{
       
   171 		case EKeyEnter:
       
   172 			{
       
   173 			iConsole->Printf(_L("\n"));
       
   174 
       
   175 			//get value
       
   176 			TInt val;
       
   177 			TBool typeErr = EFalse;
       
   178 			if(iChars.Length() != 0)
       
   179 				{					
       
   180 				TLex lex(iChars);				
       
   181 				lex.SkipSpaceAndMark();
       
   182 
       
   183 				if(KErrNone != lex.Val(val))
       
   184 					{
       
   185 					typeErr = ETrue;
       
   186 					}
       
   187 				}
       
   188 			else
       
   189 				{
       
   190 				typeErr = ETrue;
       
   191 				}
       
   192 			
       
   193 			if(typeErr)
       
   194 				{
       
   195 				//Prompt user input again
       
   196 				iConsole->Printf(KErrInput());
       
   197 				iConsole->Printf(KSetIapMemo());
       
   198 				iChars.SetLength(0);
       
   199 				__FLOG_STATIC0(KSubSys, KLogComponent , _L8("Input error"));
       
   200 				break;					
       
   201 				}
       
   202 			
       
   203 			//The input is correct
       
   204 			__FLOG_STATIC1(KSubSys, KLogComponent , _L8("Get val = %d"), val);
       
   205 			
       
   206 			if(iCommandState == EIapId)
       
   207 				{
       
   208 				//if the command state is wait iap id, now get the iap id, wait for iap index
       
   209 				iIapId = val;
       
   210 				iChars.SetLength(0);
       
   211 				iConsole->Printf(KSetIapIndexMemo());
       
   212 				iCommandState = EIapPriority;
       
   213 				}
       
   214 			else //(iCommandState == EIapPriority)
       
   215 				{
       
   216 				//if the command state is wait iap index, now the index. set into central repoistory.
       
   217 				//Then destroy the current console and update the value displayed on main console if the
       
   218 				//setting value is ncm default value.
       
   219 				iIndex = val;
       
   220 				SetNcmIapL();
       
   221 				EndWork();
       
   222 				if(iIndex == 0)
       
   223 					{
       
   224 					iTestConsole.SetLocalIapId(iIapId);
       
   225 					}
       
   226 				__FLOG_STATIC2(KSubSys, KLogComponent , _L8("Set Ncm Iap, idx = %d, id = %d"),
       
   227 						iIndex, iIapId);
       
   228 
       
   229 				return;
       
   230 				}				
       
   231 			}
       
   232 			break;
       
   233 		case EKeyEscape:
       
   234 			//delete this;
       
   235 			EndWork();
       
   236 			return;
       
   237 		case EKeyBackspace:
       
   238 			if(iChars.Length() > 0)
       
   239 				{
       
   240 				iConsole->SetCursorPosRel(TPoint(-1, 0));
       
   241 				iConsole->ClearToEndOfLine();
       
   242 				iChars.SetLength(iChars.Length()-1);
       
   243 				}
       
   244 			break;
       
   245 		default:
       
   246 			iChars.Append(code);
       
   247 			iConsole->Printf(_L("%c"), code);
       
   248 			break;
       
   249 		}
       
   250 	iConsole->Read(iStatus);
       
   251 	SetActive();
       
   252 	
       
   253 	}
       
   254 
       
   255 void CSetNcmIapCommand::DoCancel()
       
   256 	{
       
   257 	iConsole->ReadCancel();
       
   258 	}
       
   259 
       
   260 TInt CSetNcmIapCommand::RunError(TInt aError)
       
   261 	{
       
   262 	User::Panic(_L("CSetNcmIapCommand"), aError);
       
   263 	return aError;
       
   264 	}
       
   265 
       
   266 void CSetNcmIapCommand::InitWorkL()
       
   267 /**
       
   268 Initialize work. Including start a new console, create buf for getting user input, get repoistory handle.
       
   269 */
       
   270 	{
       
   271 	LOG_FUNC
       
   272 
       
   273 	if(!iConsole)
       
   274 		{		
       
   275 		iConsole = Console::NewL(_L("Set Ncm IAP Console"), TSize(-1,-1));
       
   276 		__FLOG_STATIC0(KSubSys, KLogComponent , _L8("Create Console ok!"));
       
   277 
       
   278 		User::LeaveIfError(iChars.Create(KMaxNumOfChars));
       
   279 		__FLOG_STATIC0(KSubSys, KLogComponent , _L8("Create iChars ok!"));
       
   280 
       
   281 		iRepository = CRepository::NewL(KNcmIapIdRepositoryUID);	
       
   282 		__FLOG_STATIC0(KSubSys, KLogComponent , _L8("Get CRepository ok!"));
       
   283 		
       
   284 		iIapId = 1;
       
   285 		iIndex = 0;
       
   286 		}
       
   287 	}
       
   288 
       
   289 void CSetNcmIapCommand::EndWork()
       
   290 /**
       
   291 Finalize work. Including delete console, delete the buf used for getting user input, close repoistory handle.
       
   292 */
       
   293 	{
       
   294 	LOG_FUNC
       
   295 	
       
   296 	if(iConsole)
       
   297 		{
       
   298 		__FLOG_STATIC0(KSubSys, KLogComponent , _L8("Destroy console..."));
       
   299 		Cancel();
       
   300 		iChars.Close();
       
   301 		iRows.Close();
       
   302 		delete iRepository;
       
   303 		delete iConsole;
       
   304 		iConsole = NULL;
       
   305 		__FLOG_STATIC0(KSubSys, KLogComponent , _L8("Destroy console ok!"));
       
   306 		}
       
   307 	}
       
   308