htiui/HtiAdminQt/chtiadminengine.cpp
changeset 0 39ab869ed429
equal deleted inserted replaced
-1:000000000000 0:39ab869ed429
       
     1 /*
       
     2 * Copyright (c) 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:  Implementation of HtiAdmin main.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <coecntrl.h>
       
    19 #include <w32std.h>
       
    20 #include <commdb.h>
       
    21 
       
    22 #include <HtiCfg.h>
       
    23 #include <HtiCommPluginInterface.h> // for KHTICommInterfaceUid
       
    24 
       
    25 
       
    26 #include "htienginewrapper.h"
       
    27 #include "chtiadminengine.h"
       
    28 
       
    29 
       
    30 #ifdef __ENABLE_LOGGING__
       
    31 
       
    32 #include <flogger.h>
       
    33 
       
    34 _LIT(KLogFolder, "hti");
       
    35 _LIT(KLogFile, "htiadmin.txt");
       
    36 #define HTI_LOG_TEXT(a1) {_LIT(temp, a1); RFileLogger::Write(KLogFolder, KLogFile, EFileLoggingModeAppend, temp);}
       
    37 #define HTI_LOG_FORMAT(a1,a2) {_LIT(temp, a1); RFileLogger::WriteFormat(KLogFolder, KLogFile, EFileLoggingModeAppend, temp, (a2));}
       
    38 #define HTI_LOG_DES(a1) {RFileLogger::Write(KLogFolder, KLogFile, EFileLoggingModeAppend, (a1));}
       
    39 
       
    40 #else // __ENABLE_LOGGING__
       
    41 
       
    42 #define HTI_LOG_TEXT(a1)
       
    43 #define HTI_LOG_FORMAT(a1,a2)
       
    44 #define HTI_LOG_DES(a1)
       
    45 
       
    46 #endif // __ENABLE_LOGGING__
       
    47 
       
    48 // CONSTANTS
       
    49 _LIT( KHtiWatchDogMatchPattern, "HtiWatchDog*" );
       
    50 _LIT( KHtiFrameworkExe, "HtiFramework.exe" );
       
    51 _LIT( KHtiMatchPattern, "HtiFramework*" );
       
    52 _LIT( KHtiFrameworkCaption, "HTI status: " );
       
    53 _LIT( KHtiAutoStartCaption, "AutoStart: " );
       
    54 _LIT( KHtiVersionCaption, "Version: " );
       
    55 _LIT( KHtiSelectedCommCaption, "Communication: " );
       
    56 _LIT( KHtiAdminStartParameter, "admin" );
       
    57 _LIT( KHtiAdminVersionFormat, "%u.%u.%u (%uwk%02u)" );
       
    58 _LIT( KHtiAdminAboutVersionFormat, "Version %u.%u.%u - " );
       
    59 const static TInt KTerminateReason = 1;
       
    60 
       
    61 const TInt32 KHTIImplUidIsaComm   = {0x1020DEBD};
       
    62 const TInt32 KHTIImplUidSerial    = {0x10210CCA};
       
    63 const TInt32 KHTIImplUidBluetooth = {0x200212CC};
       
    64 const TInt32 KHTIImplUidIPComm    = {0x200212CE};
       
    65 const TInt32 KHTIImplUidUsbSerialComm = {0x200212D0};
       
    66 const TInt32 KHTIImplUidTraceComm = {0x200212D8};
       
    67 
       
    68 // configuration file constants
       
    69 _LIT( KCfgFilePath,         "\\");
       
    70 _LIT( KHtiCfg,              "hti.cfg" );
       
    71 _LIT( KHtiBtCommCfg,        "HTIBtComm.cfg" );
       
    72 _LIT( KHtiSerialCommCfg ,   "HTISerialComm.cfg" );
       
    73 _LIT( KHtiIPCommCfg,        "HTIIPComm.cfg" );
       
    74 // hti.cfg
       
    75 _LIT8( KCommPlugin,         "CommPlugin" );
       
    76 _LIT8( KPriority,           "Priority" );
       
    77 _LIT8( KShowConsole,        "ShowConsole" );
       
    78 _LIT8( KEnableHtiWatchDog,  "EnableHtiWatchDog" );
       
    79 _LIT8( KEnableHtiAutoStart, "EnableHtiAutoStart" );
       
    80 _LIT8( KShowErrorDialogs,   "ShowErrorDialogs" );
       
    81 // HtiBtComm.cfg
       
    82 _LIT8( KBtDeviceAddress,    "BtDeviceAddress" );
       
    83 _LIT8( KBtDeviceName,       "BtDeviceName" );
       
    84 // HtiSerialComm.cfg
       
    85 _LIT8( KCommPortNumber,     "CommPort" );
       
    86 // HTIIPComm.cfg
       
    87 _LIT8( KLocalPort,          "LocalPort" );
       
    88 _LIT8( KRemoteHost,         "RemoteHost" );
       
    89 _LIT8( KRemotePort,         "RemotePort" );
       
    90 _LIT8( KIAPName,            "IAPName");
       
    91 
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 
       
    95 CHtiAdminEngine::CHtiAdminEngine()
       
    96 {
       
    97     iIAPsArray = NULL;
       
    98 }
       
    99 
       
   100 // ---------------------------------------------------------------------------
       
   101 CHtiAdminEngine* CHtiAdminEngine::NewLC(HtiEngineWrapper *aEngineWrapper)
       
   102     {
       
   103     CHtiAdminEngine* self = new (ELeave) CHtiAdminEngine();
       
   104     CleanupStack::PushL(self);
       
   105     self->ConstructL(aEngineWrapper);
       
   106     return self;
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------------------------
       
   110 CHtiAdminEngine* CHtiAdminEngine::NewL(HtiEngineWrapper *aEngineWrapper)
       
   111     {
       
   112     CHtiAdminEngine* self = CHtiAdminEngine::NewLC(aEngineWrapper);
       
   113     CleanupStack::Pop(); // self;
       
   114     return self;
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 void CHtiAdminEngine::ConstructL(HtiEngineWrapper *aEngineWrapper)
       
   119     {
       
   120     iEngineWrapper = aEngineWrapper;
       
   121     iHtiCfg = CHtiCfg::NewL();
       
   122     
       
   123     CheckStatusesL();
       
   124     StartTimer();
       
   125     }
       
   126 
       
   127 // ---------------------------------------------------------------------------
       
   128 void CHtiAdminEngine::CheckStatusesL()
       
   129     {
       
   130     UpdateConsoleStatus();
       
   131     UpdateWatchDogStatus();
       
   132     UpdateAutoStartStatus(); 
       
   133     UpdateSelectedComm();
       
   134     UpdateStatusL();
       
   135     }
       
   136 
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 CHtiAdminEngine::~CHtiAdminEngine()
       
   140 {
       
   141 	KillTimer();	
       
   142 	delete iHtiCfg;
       
   143 	delete iPluginNameArray;	
       
   144 	delete iPluginDataTypeArray;	
       
   145 }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 void CHtiAdminEngine::StartHtiL()
       
   149 {
       
   150 
       
   151 	RProcess prs;
       
   152 	TInt err;
       
   153 	TBool isRunning = EFalse;
       
   154 	if ( OpenHtiProcess( prs ) )
       
   155 		{
       
   156 		if ( prs.ExitType() == EExitPending )
       
   157 			{
       
   158 			isRunning = ETrue;
       
   159 			iEngineWrapper->updateStatus(HtiEngineWrapper::Running);
       
   160 			}
       
   161 		prs.Close();
       
   162 		}
       
   163 
       
   164 	if ( !isRunning )
       
   165 		{
       
   166 		HTI_LOG_TEXT( "create process" );
       
   167 		err = prs.Create( KHtiFrameworkExe, KHtiAdminStartParameter );
       
   168 		if ( err == KErrNone )
       
   169 			{
       
   170 			prs.Resume();
       
   171 			prs.Close();
       
   172 			UpdateStatusL();
       
   173 			HTI_LOG_TEXT("create done");
       
   174 			}
       
   175 		else
       
   176 			{
       
   177 			iEngineWrapper->updateStatus(HtiEngineWrapper::Error);
       
   178 			HTI_LOG_TEXT("create error");
       
   179 			}
       
   180 		}
       
   181 
       
   182 }
       
   183 
       
   184 
       
   185 // ---------------------------------------------------------------------------
       
   186 
       
   187 void CHtiAdminEngine::StopHtiL()
       
   188 {
       
   189 	HTI_LOG_TEXT("Stop");
       
   190 
       
   191 	// kill the watchdog to prevent it from restarting HTI again
       
   192 	KillHtiWatchDogL();
       
   193 
       
   194 	RProcess prs;
       
   195 	if ( OpenHtiProcess( prs ) )
       
   196 		{
       
   197 		if ( prs.ExitType() == EExitPending )
       
   198 			{
       
   199 			HTI_LOG_TEXT("try to kill");
       
   200 			prs.Kill( KTerminateReason );
       
   201 			UpdateStatusL();
       
   202 			}
       
   203 		else
       
   204 			{
       
   205 			iEngineWrapper->updateStatus(HtiEngineWrapper::Stopped);
       
   206 			}
       
   207 		prs.Close();
       
   208 		}
       
   209 
       
   210 }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 
       
   214 void CHtiAdminEngine::EnableBtByAddressL(const TDesC& aAddress)
       
   215 {
       
   216 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath,  KHtiCfg ) );
       
   217 	iHtiCfg->SetParameterL( KCommPlugin, _L8( "BTSERIAL" ) );
       
   218 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   219 
       
   220 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiBtCommCfg ) );
       
   221 	iHtiCfg->RemoveParameterL( KBtDeviceName );
       
   222 	iHtiCfg->RemoveParameterL( KBtDeviceAddress );
       
   223 	
       
   224 	TBuf8<KMaxParameterValueLength> value;
       
   225 	value.Copy(aAddress);
       
   226 	iHtiCfg->SetParameterL( KBtDeviceAddress, value);
       
   227 
       
   228 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiBtCommCfg);
       
   229 	UpdateSelectedComm();
       
   230 
       
   231 }
       
   232 
       
   233 // ---------------------------------------------------------------------------
       
   234 void CHtiAdminEngine::EnableBtByNameL(const TDesC& aAddress)
       
   235 {
       
   236 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   237 	iHtiCfg->SetParameterL( KCommPlugin, _L8( "BTSERIAL" ) );
       
   238 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   239 
       
   240 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiBtCommCfg ) );
       
   241 	iHtiCfg->RemoveParameterL( KBtDeviceName );
       
   242 	iHtiCfg->RemoveParameterL( KBtDeviceAddress );
       
   243 
       
   244 	TBuf8<KMaxParameterValueLength> value;
       
   245 	value.Copy(aAddress);
       
   246 	iHtiCfg->SetParameterL( KBtDeviceName, value);
       
   247 	
       
   248 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiBtCommCfg );
       
   249 	UpdateSelectedComm();
       
   250 }
       
   251 
       
   252 
       
   253 // ---------------------------------------------------------------------------
       
   254 void CHtiAdminEngine::BtSearchL()
       
   255 {
       
   256 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   257 	iHtiCfg->SetParameterL( KCommPlugin, _L8( "BTSERIAL" ) );
       
   258 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   259 
       
   260 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiBtCommCfg ) );
       
   261 	iHtiCfg->RemoveParameterL( KBtDeviceName );
       
   262 	iHtiCfg->RemoveParameterL( KBtDeviceAddress );
       
   263 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiBtCommCfg );
       
   264 	UpdateSelectedComm();
       
   265 }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 void CHtiAdminEngine::IPListenL(const TDesC& aPort, const TDesC& aIAP)
       
   269 {
       
   270 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   271 	iHtiCfg->SetParameterL( KCommPlugin, _L8("IPCOMM"));
       
   272 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   273 
       
   274 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiIPCommCfg ) );
       
   275 	
       
   276 	TBuf8<KMaxParameterValueLength> port;
       
   277 	port.Copy(aPort);
       
   278 	iHtiCfg->SetParameterL( KLocalPort, port);
       
   279 	
       
   280 	TBuf8<KMaxParameterValueLength> iap;
       
   281 	iap.Copy(aIAP);
       
   282 	iHtiCfg->SetParameterL(KIAPName, iap);
       
   283 
       
   284 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiIPCommCfg );
       
   285 	UpdateSelectedComm();
       
   286 
       
   287 }
       
   288 
       
   289 // ---------------------------------------------------------------------------
       
   290 void CHtiAdminEngine::IPConnectL(const TDesC& aHost, const TDesC& aPort, const TDesC& aIAP)
       
   291 {
       
   292 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   293 	iHtiCfg->SetParameterL( KCommPlugin, _L8( "IPCOMM" ) );
       
   294 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   295 
       
   296 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiIPCommCfg ) );
       
   297 	iHtiCfg->RemoveParameterL( KLocalPort );
       
   298 
       
   299 	TBuf8<KMaxParameterValueLength> value;
       
   300 	value.Copy(aHost);
       
   301 	iHtiCfg->SetParameterL( KRemoteHost, value);
       
   302 	
       
   303 	TBuf8<KMaxParameterValueLength> port;
       
   304     port.Copy(aPort);
       
   305     iHtiCfg->SetParameterL(KRemotePort, port);
       
   306 
       
   307     TBuf8<KMaxParameterValueLength> iap;
       
   308     iap.Copy(aIAP);
       
   309     iHtiCfg->SetParameterL(KIAPName, iap);
       
   310 
       
   311     iHtiCfg->SaveCfgL(KCfgFilePath, KHtiIPCommCfg);
       
   312     UpdateSelectedComm();
       
   313 }
       
   314 
       
   315 // ---------------------------------------------------------------------------
       
   316 void CHtiAdminEngine::EnableSerialL(const TDesC& aComPortNumber)
       
   317 {
       
   318 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   319 	iHtiCfg->SetParameterL( KCommPlugin, _L8( "SERIAL" ) );
       
   320 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   321 	
       
   322 	TBuf8<KMaxParameterValueLength> value;
       
   323 	value.Copy(aComPortNumber);
       
   324 	iHtiCfg->SetParameterL( KCommPortNumber, value );
       
   325 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiSerialCommCfg );
       
   326 	UpdateSelectedComm();
       
   327 }
       
   328 
       
   329 
       
   330 // ---------------------------------------------------------------------------
       
   331 void CHtiAdminEngine::EnableOtherCommL(const TDesC& aSelectedPluginName)
       
   332 {
       
   333 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   334 	
       
   335 	TInt namePosition;
       
   336 	iPluginNameArray->Find(aSelectedPluginName, namePosition);
       
   337 	
       
   338 	//Save correct data type for selected plugin
       
   339 	iHtiCfg->SetParameterL( KCommPlugin, iPluginDataTypeArray->MdcaPoint(namePosition) );
       
   340 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   341 	UpdateSelectedComm();
       
   342 }
       
   343 
       
   344 // ---------------------------------------------------------------------------
       
   345 void CHtiAdminEngine::SetPriorityBackgroundL()
       
   346 {
       
   347 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   348 	iHtiCfg->SetParameterL( KPriority, _L8("1"));
       
   349 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   350 
       
   351 }
       
   352 
       
   353 // ---------------------------------------------------------------------------
       
   354 void CHtiAdminEngine::SetPriorityForegroundL()
       
   355 {
       
   356 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   357 	iHtiCfg->SetParameterL( KPriority, _L8("2"));
       
   358 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   359 
       
   360 }
       
   361 
       
   362 // ---------------------------------------------------------------------------
       
   363 void CHtiAdminEngine::SetPriorityHighL()
       
   364 {
       
   365 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   366 	iHtiCfg->SetParameterL( KPriority, _L8("3"));
       
   367 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   368 
       
   369 }
       
   370 
       
   371 // ---------------------------------------------------------------------------
       
   372 void CHtiAdminEngine::SetPriorityAbsoluteHighL()
       
   373 {
       
   374 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   375 	iHtiCfg->SetParameterL( KPriority, _L8("4"));
       
   376 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   377 }
       
   378 
       
   379 // ---------------------------------------------------------------------------
       
   380 void CHtiAdminEngine::AutoStartEnableL(TBool aEnableAutoStart)
       
   381 {
       
   382 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   383 	if(aEnableAutoStart)
       
   384 		{
       
   385 		iHtiCfg->SetParameterL( KEnableHtiAutoStart, _L8("1"));
       
   386 		}
       
   387 	else
       
   388 		{
       
   389 		iHtiCfg->SetParameterL( KEnableHtiAutoStart, _L8("0"));
       
   390 		}
       
   391 	
       
   392 	iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   393 	UpdateAutoStartStatus();
       
   394 }
       
   395 
       
   396 // ---------------------------------------------------------------------------
       
   397 void CHtiAdminEngine::WatchDogEnableL(TBool aEnableWDog)
       
   398 {
       
   399 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   400     
       
   401 	if(aEnableWDog)
       
   402 		{
       
   403 		iHtiCfg->SetParameterL( KEnableHtiWatchDog, _L8("1"));
       
   404 		}
       
   405 	else
       
   406 		{
       
   407 		iHtiCfg->SetParameterL( KEnableHtiWatchDog, _L8("0"));
       
   408 		}
       
   409     
       
   410     iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   411     UpdateWatchDogStatus();
       
   412 }
       
   413 
       
   414 // ---------------------------------------------------------------------------
       
   415 void CHtiAdminEngine::ConsoleEnableL(TBool aEnableConsole)
       
   416 {
       
   417 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   418     
       
   419 	if(aEnableConsole)
       
   420 		{
       
   421 		iHtiCfg->SetParameterL( KShowConsole, _L8("1"));
       
   422 		}
       
   423 	else
       
   424 		{
       
   425 		iHtiCfg->SetParameterL( KShowConsole, _L8("0"));
       
   426 		}
       
   427     
       
   428     iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   429     UpdateConsoleStatus();
       
   430 }
       
   431 
       
   432 // ---------------------------------------------------------------------------
       
   433 void CHtiAdminEngine::ErrorDialogsEnableL(TBool aEnableErrorDialogs)
       
   434 {
       
   435 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   436     
       
   437 	if(aEnableErrorDialogs)
       
   438 		{
       
   439 		iHtiCfg->SetParameterL( KShowErrorDialogs, _L8("1"));
       
   440 		}
       
   441 	else
       
   442 		{
       
   443 		iHtiCfg->SetParameterL( KShowErrorDialogs, _L8("0"));
       
   444 		}
       
   445     
       
   446     iHtiCfg->SaveCfgL( KCfgFilePath, KHtiCfg );
       
   447 }
       
   448 
       
   449 // ---------------------------------------------------------------------------
       
   450 void CHtiAdminEngine::SetHtiCfgParamL(const TDesC& aParamName, const TDesC& aParamValue)
       
   451 {
       
   452 	SetCfgParamL(aParamName, aParamValue, KHtiCfg);
       
   453 }
       
   454 
       
   455 // ---------------------------------------------------------------------------
       
   456 void CHtiAdminEngine::SetBtCfgParamL(const TDesC& aParamName, const TDesC& aParamValue)
       
   457 {
       
   458 	SetCfgParamL(aParamName, aParamValue, KHtiBtCommCfg);
       
   459 }
       
   460 
       
   461 // ---------------------------------------------------------------------------
       
   462 void CHtiAdminEngine::SetSerialCfgParamL(const TDesC& aParamName, const TDesC& aParamValue)
       
   463 {
       
   464 	SetCfgParamL(aParamName, aParamValue, KHtiSerialCommCfg);
       
   465 }
       
   466 
       
   467 // ---------------------------------------------------------------------------
       
   468 void CHtiAdminEngine::SetIPCfgParamL(const TDesC& aParamName, const TDesC& aParamValue)
       
   469 {
       
   470 	SetCfgParamL(aParamName, aParamValue, KHtiIPCommCfg);
       
   471 }
       
   472 
       
   473 // ---------------------------------------------------------------------------
       
   474 void CHtiAdminEngine::SetCfgParamL(const TDesC& aParamName, const TDesC& aParamValue, const TDesC& aFileName)
       
   475 {
       
   476 	TBuf8<KMaxParameterNameLength> name;
       
   477 	name.Copy(aParamName);
       
   478 	
       
   479 	TBuf8<KMaxParameterValueLength> value;
       
   480 	value.Copy(aParamValue);
       
   481 	
       
   482 	TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, aFileName ) );
       
   483 	
       
   484 	iHtiCfg->SetParameterL( name, value );
       
   485 	iHtiCfg->SaveCfgL( KCfgFilePath, aFileName);
       
   486 }
       
   487 
       
   488 // ---------------------------------------------------------------------------
       
   489 void CHtiAdminEngine::GetHtiCfgParamL(const TDesC& aParamName, TDes& aParamValue)
       
   490     {
       
   491     GetCfgParamL(aParamName, aParamValue, KHtiCfg);
       
   492     }
       
   493 
       
   494 // ---------------------------------------------------------------------------
       
   495 void CHtiAdminEngine::GetBtCfgParamL(const TDesC& aParamName, TDes& aParamValue)
       
   496     {
       
   497     GetCfgParamL(aParamName, aParamValue, KHtiBtCommCfg);
       
   498     }
       
   499 
       
   500 // ---------------------------------------------------------------------------
       
   501 void CHtiAdminEngine::GetSerialCfgParamL(const TDesC& aParamName, TDes& aParamValue)
       
   502     {
       
   503     GetCfgParamL(aParamName, aParamValue, KHtiSerialCommCfg);
       
   504     }
       
   505 
       
   506 // ---------------------------------------------------------------------------
       
   507 void CHtiAdminEngine::GetIPCfgParamL(const TDesC& aParamName, TDes& aParamValue)
       
   508     {
       
   509     GetCfgParamL(aParamName, aParamValue, KHtiIPCommCfg);
       
   510     }
       
   511 
       
   512 // ---------------------------------------------------------------------------
       
   513 void CHtiAdminEngine::GetCfgParamL(const TDesC& aParamName, TDes& aParamValue, const TDesC& aFileName)
       
   514     {
       
   515     TBuf8<KMaxParameterNameLength> name;
       
   516     name.Copy(aParamName);
       
   517     
       
   518     TBuf<KMaxParameterValueLength> value;
       
   519 
       
   520     TRAP_IGNORE( iHtiCfg->LoadCfgL( KCfgFilePath, aFileName ) );
       
   521     aParamValue.Copy(iHtiCfg->GetParameterL(name));
       
   522     }
       
   523 
       
   524 // ---------------------------------------------------------------------------
       
   525 void CHtiAdminEngine::GetSelectedComm(TDes& aPluginName)
       
   526     {
       
   527     TBuf8<KMaxParameterValueLength> commValue;
       
   528     TRAPD( err, iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   529     if ( !err )
       
   530         {
       
   531         TRAP( err, commValue = iHtiCfg->GetParameterL( KCommPlugin ) );
       
   532         }
       
   533     // Use default if not found from config
       
   534     if ( commValue.Length() == 0 )
       
   535         {
       
   536         commValue.Copy(KCommDefaultImplementation  );        
       
   537         }
       
   538     TInt typePosition;
       
   539     iPluginDataTypeArray->Find(commValue, typePosition);
       
   540     aPluginName.Copy(iPluginNameArray->MdcaPoint(typePosition));
       
   541     }
       
   542 
       
   543 // ---------------------------------------------------------------------------
       
   544 TBool CHtiAdminEngine::OpenHtiProcess( RProcess& aPrs )
       
   545 {
       
   546     HTI_LOG_TEXT( "OpenHtiProcess" );
       
   547 
       
   548 	TFullName processName;
       
   549 	TInt err;
       
   550 
       
   551 	TFindProcess finder( KHtiMatchPattern );
       
   552 	err = finder.Next( processName );
       
   553 	if ( err == KErrNone )
       
   554 		{
       
   555 		HTI_LOG_TEXT( "try to open..." );
       
   556 		err = aPrs.Open( finder );
       
   557 		HTI_LOG_TEXT( "opened" );
       
   558 
       
   559 		if ( err == KErrNone )
       
   560 			{
       
   561 			HTI_LOG_TEXT( "OpenHtiProcess OK" );
       
   562 			return ETrue;
       
   563 			}
       
   564 		else
       
   565 			{
       
   566 			iEngineWrapper->updateStatus(HtiEngineWrapper::Error);
       
   567 			}
       
   568 		}
       
   569 	else if ( err == KErrNotFound )
       
   570 		{
       
   571 		iEngineWrapper->updateStatus(HtiEngineWrapper::Stopped);
       
   572 		}
       
   573 	else
       
   574 		{
       
   575 		iEngineWrapper->updateStatus(HtiEngineWrapper::Error);
       
   576 		}
       
   577 
       
   578 	HTI_LOG_TEXT( "OpenHtiProcess NOK" );
       
   579 
       
   580 	return EFalse;
       
   581 }
       
   582 
       
   583 // ---------------------------------------------------------------------------
       
   584 void CHtiAdminEngine::UpdateStatusL()
       
   585     {
       
   586     HTI_LOG_TEXT( "UpdateStatusL" );
       
   587     RProcess prs;
       
   588     if ( OpenHtiProcess( prs ) )
       
   589         {
       
   590         HTI_LOG_TEXT( "Update display" );
       
   591         TExitType exT = prs.ExitType();
       
   592 
       
   593         switch ( exT )
       
   594             {
       
   595             case EExitPending:
       
   596                 {
       
   597                 iEngineWrapper->updateStatus(HtiEngineWrapper::Running);
       
   598                 }
       
   599                 break;
       
   600 
       
   601             case EExitKill:
       
   602             case EExitTerminate:
       
   603                 {
       
   604                 iEngineWrapper->updateStatus(HtiEngineWrapper::Stopped);
       
   605                 }
       
   606                 break;
       
   607 
       
   608             case EExitPanic:
       
   609                 {
       
   610                 iEngineWrapper->updateStatus(HtiEngineWrapper::Panic);
       
   611                 }
       
   612                 break;
       
   613             };
       
   614         prs.Close();
       
   615         }
       
   616     }
       
   617 
       
   618 // ---------------------------------------------------------------------------
       
   619 void CHtiAdminEngine::KillHtiWatchDogL()
       
   620     {
       
   621     TFullName processName;
       
   622     TFindProcess finder( KHtiWatchDogMatchPattern );
       
   623     TInt err = finder.Next( processName );
       
   624     if ( err == KErrNone )
       
   625         {
       
   626         HTI_LOG_TEXT( "HTI watchdog process found. Trying to open and kill it..." );
       
   627         RProcess prs;
       
   628         User::LeaveIfError( prs.Open( finder ) );
       
   629         prs.Kill( KTerminateReason );
       
   630         prs.Close();
       
   631         HTI_LOG_TEXT( "HTI watchdog killed" );
       
   632         }
       
   633     }
       
   634 
       
   635 // ---------------------------------------------------------------------------
       
   636 void CHtiAdminEngine::StartTimer()
       
   637     {
       
   638     // start timer to watch the status
       
   639     if ( !iPeriodic )
       
   640         {
       
   641         iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );
       
   642         iPeriodic->Start( 1 * 1000 * 1000,
       
   643                           10 * 1000 * 1000,
       
   644                           TCallBack( TimerCallBackL, this ) );
       
   645         }
       
   646     }
       
   647 // ---------------------------------------------------------------------------
       
   648 void CHtiAdminEngine::KillTimer()
       
   649     {
       
   650     if ( iPeriodic )
       
   651         {
       
   652         iPeriodic->Cancel();
       
   653         delete iPeriodic;
       
   654         iPeriodic = NULL;
       
   655         }
       
   656     }
       
   657 
       
   658 // ---------------------------------------------------------------------------
       
   659 TInt CHtiAdminEngine::TimerCallBackL( TAny* aPtr )
       
   660     {
       
   661     ( (CHtiAdminEngine*)aPtr )->UpdateStatusL();
       
   662     return ETrue;
       
   663     }
       
   664 
       
   665 
       
   666 // ---------------------------------------------------------------------------
       
   667 void CHtiAdminEngine::UpdateAutoStartStatus()
       
   668     {
       
   669     HTI_LOG_TEXT( "UpdateAutoStartStatus" );
       
   670     TInt value = KErrNotFound;
       
   671     TRAPD( err, iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   672     if ( !err )
       
   673         {
       
   674         TRAP( err, value = iHtiCfg->GetParameterIntL(KEnableHtiAutoStart) );
       
   675         }
       
   676 
       
   677     if ( value == KErrNotFound )
       
   678         {
       
   679         // Use default values if not found from config.
       
   680 #ifdef __WINS__
       
   681         value = 0;
       
   682 #else
       
   683         value = 1;
       
   684 #endif
       
   685         }
       
   686 
       
   687     if ( value == 0 )
       
   688         {
       
   689         iEngineWrapper->updateAutostartStatus( HtiEngineWrapper::AutoStartDisabled );
       
   690         }
       
   691     else if ( value == 1 )
       
   692         {
       
   693         iEngineWrapper->updateAutostartStatus( HtiEngineWrapper::AutoStartEnabled );
       
   694         }
       
   695     else
       
   696         {
       
   697         iEngineWrapper->updateAutostartStatus( HtiEngineWrapper::AutoStartUnknown );
       
   698         }
       
   699     }
       
   700 
       
   701 // ---------------------------------------------------------------------------
       
   702 void CHtiAdminEngine::UpdateConsoleStatus()
       
   703     {
       
   704     HTI_LOG_TEXT( "UpdateConsoleStatus" );
       
   705     TInt value = KErrNotFound;
       
   706     TRAPD( err, iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   707     if(!err)
       
   708         {
       
   709         TRAP( err, value = iHtiCfg->GetParameterIntL(KShowConsole) );
       
   710         }
       
   711     
       
   712     if ( value == KErrNotFound )
       
   713         {
       
   714         value = 0;
       
   715         }
       
   716 
       
   717     iEngineWrapper->updateConsoleStatus(value);
       
   718     }
       
   719 
       
   720 // ---------------------------------------------------------------------------
       
   721 void CHtiAdminEngine::UpdateWatchDogStatus()
       
   722     {
       
   723     HTI_LOG_TEXT( "UpdateWatchDogStatus" );
       
   724     TInt value = KErrNotFound;
       
   725     TRAPD( err, iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   726     if(!err)
       
   727         {
       
   728         TRAP( err, value = iHtiCfg->GetParameterIntL(KEnableHtiWatchDog) );
       
   729         }
       
   730     
       
   731     if ( value == KErrNotFound )
       
   732         {
       
   733         value = 0;
       
   734         }
       
   735 
       
   736     iEngineWrapper->updateWatchDogStatus(value);    
       
   737     }
       
   738 
       
   739 // ---------------------------------------------------------------------------
       
   740 void CHtiAdminEngine::UpdateSelectedComm()
       
   741     {
       
   742     HTI_LOG_TEXT( "UpdateSelectedComm" );
       
   743     TBuf<64> commPlugin;
       
   744     TRAPD( err, iHtiCfg->LoadCfgL( KCfgFilePath, KHtiCfg ) );
       
   745     if ( !err )
       
   746         {
       
   747         TBuf8<KMaxParameterValueLength> commValue;
       
   748         TRAP( err, commValue = iHtiCfg->GetParameterL( KCommPlugin ) );
       
   749         if ( err == KErrNone )
       
   750             {
       
   751             commPlugin.Copy( commValue );
       
   752             }
       
   753         }
       
   754     // Use default if not found from config
       
   755     if ( commPlugin.Length() == 0 )
       
   756         {
       
   757         commPlugin.Copy(KCommDefaultImplementation  );        
       
   758         }
       
   759 		iEngineWrapper->updateSelectedComm( commPlugin );
       
   760 
       
   761     if ( commPlugin.Compare( _L( "SERIAL" ) ) == 0 )
       
   762         {
       
   763         TInt portNum = 0;
       
   764         TRAP( err, iHtiCfg->LoadCfgL( KCfgFilePath, KHtiSerialCommCfg ) );
       
   765         if ( err == KErrNone )
       
   766             {
       
   767             TRAP( err, portNum = iHtiCfg->GetParameterIntL( KCommPortNumber ) );
       
   768             }
       
   769         TBuf<16> portBuf;
       
   770         portBuf.Copy( _L( "Port: " ) );
       
   771         portBuf.AppendNum( portNum );
       
   772         iEngineWrapper->updateCommDetails( portBuf );
       
   773         }
       
   774 
       
   775     else if ( commPlugin.Compare( _L( "BTSERIAL" ) ) == 0 )
       
   776         {
       
   777         TBuf<64> hostBuf;
       
   778         TRAP( err, iHtiCfg->LoadCfgL( KCfgFilePath, KHtiBtCommCfg ) );
       
   779         if ( err == KErrNone )
       
   780             {
       
   781             TBuf8<KMaxParameterValueLength> hostValue;
       
   782             TRAP( err, hostValue = iHtiCfg->GetParameterL( KBtDeviceAddress ) );
       
   783             if ( err != KErrNone )
       
   784                 {
       
   785                 TRAP( err, hostValue = iHtiCfg->GetParameterL( KBtDeviceName ) );
       
   786                 }
       
   787             if ( err == KErrNone )
       
   788                 {
       
   789                 hostBuf.Copy( hostValue );
       
   790                 }
       
   791             }
       
   792         if ( hostBuf.Length() > 0 )
       
   793             {
       
   794             hostBuf.Insert( 0, _L( "Host: " ) );
       
   795             }
       
   796         iEngineWrapper->updateCommDetails(hostBuf);
       
   797         }
       
   798 
       
   799     else if ( commPlugin.Compare( _L( "IPCOMM" ) ) == 0 )
       
   800         {
       
   801         TBuf<128> hostBuf;
       
   802         TRAP( err, iHtiCfg->LoadCfgL( KCfgFilePath, KHtiIPCommCfg ) );
       
   803         if ( err == KErrNone )
       
   804             {
       
   805             TInt localPort = 0;
       
   806             TRAP( err, localPort = iHtiCfg->GetParameterIntL( KLocalPort ) );
       
   807             if ( err == KErrNone && localPort > 0 )
       
   808                 {
       
   809                 hostBuf.Copy( _L( "Listen: " ) );
       
   810                 hostBuf.AppendNum( localPort );
       
   811                 }
       
   812             else
       
   813                 {
       
   814                 TBuf8<KMaxParameterValueLength> remoteHost;
       
   815                 TInt remotePort = 0;
       
   816                 TRAP( err, remoteHost = iHtiCfg->GetParameterL( KRemoteHost ) );
       
   817                 if ( err == KErrNone )
       
   818                     {
       
   819                     TRAP( err, remotePort = iHtiCfg->GetParameterIntL(
       
   820                             KRemotePort ) );
       
   821                     }
       
   822                 if ( err == KErrNone )
       
   823                     {
       
   824                     hostBuf.Copy( remoteHost );
       
   825                     hostBuf.Insert( 0, _L("Connect: " ) );
       
   826                     hostBuf.Append( ':' );
       
   827                     hostBuf.AppendNum( remotePort );
       
   828                     }
       
   829                 else
       
   830                     {
       
   831                     hostBuf.Copy( _L( "Invalid config!" ) );
       
   832                     }
       
   833                 }
       
   834             }
       
   835         iEngineWrapper->updateCommDetails(hostBuf);
       
   836         }
       
   837 
       
   838     else
       
   839         {
       
   840         iEngineWrapper->updateCommDetails(KNullDesC);
       
   841         }
       
   842     }
       
   843 
       
   844 // ---------------------------------------------------------------------------
       
   845 void CleanupRArray( TAny* object )
       
   846     {
       
   847     ( ( RImplInfoPtrArray* ) object )->ResetAndDestroy();
       
   848     }
       
   849 
       
   850 // ---------------------------------------------------------------------------
       
   851 CDesCArray* CHtiAdminEngine::SearchCommPluginsL()
       
   852     {
       
   853     const TInt pluginNameArrayGranularity (15);
       
   854 	iPluginNameArray = new (ELeave) CDesCArrayFlat(pluginNameArrayGranularity);
       
   855 	iPluginDataTypeArray = new (ELeave) CDesC8ArrayFlat(pluginNameArrayGranularity);
       
   856 			
       
   857 	RImplInfoPtrArray implInfoArray;
       
   858 	CleanupStack::PushL( TCleanupItem( CleanupRArray, &implInfoArray ) );
       
   859 	
       
   860 	REComSession::ListImplementationsL( KHTICommInterfaceUid, implInfoArray );
       
   861 
       
   862 	for ( TInt i = 0; i < implInfoArray.Count(); ++i )
       
   863 		{
       
   864 		iPluginNameArray->AppendL(implInfoArray[i]->DisplayName());
       
   865 		iPluginDataTypeArray->AppendL(implInfoArray[i]->DataType());
       
   866 		}
       
   867     
       
   868 	CleanupStack::PopAndDestroy(); //implInfoArray
       
   869 		
       
   870 	return iPluginNameArray;
       
   871     }
       
   872 
       
   873 // ---------------------------------------------------------------------------
       
   874 CDesCArray* CHtiAdminEngine::SearchIAPsL()
       
   875     {
       
   876     if(iIAPsArray)
       
   877         {
       
   878         return iIAPsArray;
       
   879         }
       
   880     
       
   881     TBuf<KMaxParameterValueLength> name;
       
   882     const TInt arrayGranularity (10);
       
   883     iIAPsArray = new (ELeave) CDesCArrayFlat(arrayGranularity);
       
   884 
       
   885     // Get IAP names from the database
       
   886     CCommsDatabase* db = CCommsDatabase::NewL(EDatabaseTypeIAP);;
       
   887     CleanupStack::PushL(db);
       
   888     db->ShowHiddenRecords();
       
   889     CCommsDbTableView* view = db->OpenTableLC(TPtrC(IAP));
       
   890     TInt res = view->GotoFirstRecord();
       
   891 
       
   892     while(res == KErrNone)
       
   893         {
       
   894         view->ReadTextL(TPtrC(COMMDB_NAME), name);
       
   895         iIAPsArray->InsertL(0, name);
       
   896         res = view->GotoNextRecord();
       
   897         }
       
   898 
       
   899     CleanupStack::PopAndDestroy(view); //view
       
   900     CleanupStack::PopAndDestroy(db); //db   
       
   901     
       
   902     return iIAPsArray;
       
   903     }