dbgagents/trkagent/tcbserver/TrkTcbServer.cpp
changeset 0 c6b0df440bee
equal deleted inserted replaced
-1:000000000000 0:c6b0df440bee
       
     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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "TrkTcbServer.h"
       
    20 
       
    21 static CMetroTrkIO* TrkIO = NULL;
       
    22 
       
    23 #define SafeDelete(x) { if (x) delete x; x = NULL; }
       
    24 
       
    25 
       
    26 //
       
    27 //
       
    28 // CKeyboardInput implementation
       
    29 //
       
    30 //
       
    31 
       
    32 //
       
    33 // CKeyboardInput constructor
       
    34 //
       
    35 CKeyboardInput::CKeyboardInput(CConsoleBase* aConsole)
       
    36 	: CActive(EPriorityUserInput),
       
    37 	  iConsole(aConsole)
       
    38 {
       
    39 	CActiveScheduler::Add(this);
       
    40 }
       
    41 
       
    42 //
       
    43 // CKeyboardInput destructor
       
    44 //
       
    45 CKeyboardInput::~CKeyboardInput()
       
    46 {
       
    47 	Cancel();
       
    48 	Deque();
       
    49 }
       
    50 
       
    51 //
       
    52 // CKeyboardInput::ListenToKeyboard
       
    53 //
       
    54 // Wait for a key to be pressed
       
    55 //
       
    56 void CKeyboardInput::ListenToKeyboard()
       
    57 {
       
    58 	iConsole->Read(iStatus);
       
    59 	SetActive();
       
    60 }
       
    61 
       
    62 //
       
    63 // CKeyboardInput::RunL
       
    64 //
       
    65 // Called when a key is pressed
       
    66 //
       
    67 void CKeyboardInput::RunL()
       
    68 {
       
    69 	TKeyCode key = iConsole->KeyCode();
       
    70 	if (key == 'q' || key == 'Q')
       
    71 		CActiveScheduler::Stop();
       
    72 	else
       
    73 		ListenToKeyboard();
       
    74 }
       
    75 
       
    76 //
       
    77 // CKeyboardInput::DoCancel
       
    78 //
       
    79 // Stop waiting for keyboard input
       
    80 //
       
    81 void CKeyboardInput::DoCancel()
       
    82 {
       
    83 	iConsole->ReadCancel();
       
    84 }
       
    85 
       
    86 
       
    87 //
       
    88 //
       
    89 // CMetroTrkIO implementation
       
    90 //
       
    91 //
       
    92 
       
    93 //
       
    94 // CMetroTrkIO constructor
       
    95 //
       
    96 CMetroTrkIO::CMetroTrkIO()
       
    97 	: iConsole(0),
       
    98 	  iKeyboardInput(0)
       
    99 {
       
   100 	TRAPD(error, iConsole = Console::NewL(_L("MetroTrk"), TSize(KConsFullScreen, KConsFullScreen)));
       
   101 	if (error != KErrNone)
       
   102 		User::Panic(_L("TrkTcbServer failed to allocate CConsoleBase"), __LINE__);
       
   103 
       
   104 	iKeyboardInput = new CKeyboardInput(iConsole);
       
   105 	if (iKeyboardInput == 0)
       
   106 		User::Panic(_L("TrkTcbServer failed to allocate CKeyboardInput"), __LINE__);
       
   107 
       
   108 	iKeyboardInput->ListenToKeyboard();
       
   109 }
       
   110 
       
   111 //
       
   112 // CMetroTrkIO destructor
       
   113 //
       
   114 CMetroTrkIO::~CMetroTrkIO()
       
   115 {
       
   116 	SafeDelete(iKeyboardInput);
       
   117 	SafeDelete(iConsole);
       
   118 }
       
   119 
       
   120 //
       
   121 // CMetroTrkIO::PrintToScreen
       
   122 //
       
   123 //	Print a message to the screen
       
   124 //
       
   125 void CMetroTrkIO::PrintToScreen(TRefByValue<const TDesC> aFmt, ...)
       
   126 {
       
   127 	VA_LIST(l);
       
   128 	VA_START(l, aFmt);
       
   129 	iBuffer.FormatList(aFmt, l);
       
   130 
       
   131 	iConsole->Printf(iBuffer);
       
   132 }
       
   133 
       
   134 //
       
   135 // CMetroTrkIO::Terminating
       
   136 //
       
   137 //	Notify user of termination
       
   138 //
       
   139 void CMetroTrkIO::Terminating()
       
   140 {
       
   141 	iConsole->Printf(_L("\r\nPress any key to exit."));
       
   142 	iKeyboardInput->Cancel();
       
   143 	iConsole->Getch();
       
   144 }
       
   145 
       
   146 //
       
   147 // MainL
       
   148 //
       
   149 // Create the engine and the communications interface and start the active scheduler
       
   150 //
       
   151 void MainL()
       
   152 {
       
   153 	RProcess().SetPriority(EPriorityHigh);
       
   154 	RThread().SetPriority(EPriorityAbsoluteHigh);
       
   155 	
       
   156 	TInt sid = RProcess().SecureId();
       
   157 		
       
   158 	//TrkIO->PrintToScreen(_L("Welcome to TrkTcbServer for MetroTrk\r\n"));
       
   159 	//TrkIO->PrintToScreen(_L("Press 'Q' to quit.\r\n\r\n"));
       
   160 
       
   161 	CActiveScheduler::Start();
       
   162 }
       
   163 
       
   164 //
       
   165 // EXE entrypoint
       
   166 //
       
   167 TInt E32Main()
       
   168 {	
       
   169 	__UHEAP_MARK;
       
   170 
       
   171 	CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   172 	if (cleanupStack == 0)
       
   173 		User::Panic(_L("MetroTrk failed to allocate CTrapCleanup"), __LINE__);
       
   174 	
       
   175 	CActiveScheduler* scheduler = new CActiveScheduler;
       
   176 	if (scheduler == 0)
       
   177 		User::Panic(_L("MetroTrk failed to allocate CActiveScheduler"), __LINE__);
       
   178 
       
   179 	CActiveScheduler::Install(scheduler);
       
   180 
       
   181 //	TrkIO = new CMetroTrkIO;
       
   182 	
       
   183 //	if (!TrkIO)
       
   184 //		User::Panic(_L("TrkTcbServer failed to allocate CMetroTrkIO"), __LINE__);
       
   185 	
       
   186 	TRAPD(error, MainL());
       
   187 
       
   188 //	if (error != KErrNone)
       
   189 //	{
       
   190 //		TrkIO->PrintToScreen(_L("\r\nUnexpected error %d"), error);
       
   191 //		TrkIO->Terminating();
       
   192 //	}
       
   193 	
       
   194 //	SafeDelete(TrkIO);
       
   195 	SafeDelete(scheduler);
       
   196 	SafeDelete(cleanupStack);
       
   197 
       
   198 	__UHEAP_MARKEND;
       
   199 
       
   200 	User::Exit(0);
       
   201 	return 0;
       
   202 }