usbmgmt/usbmgrtest/usbcontrolapp/testusbawareapp/testusbawareapp.cpp
changeset 0 c9bc50fca66e
equal deleted inserted replaced
-1:000000000000 0:c9bc50fca66e
       
     1 /*
       
     2 * Copyright (c) 2008-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 #include "testusbawareapp.h"
       
    19 
       
    20 #include <e32cons.h>
       
    21 #include <e32debug.h>
       
    22 #include "e32svr.h"
       
    23 #include <e32keys.h>
       
    24 
       
    25 #include "usbcontrolappshared.h"
       
    26 #include "e32property.h" 
       
    27 
       
    28 #define LOG(A,B) RDebug::Print(_L("UsbAwareApp: " L##A),B)
       
    29 #define PNT(A)	 RDebug::Print(_L("UsbAwareApp: " L##A))
       
    30 #define PANIC Panic(__LINE__)
       
    31 
       
    32 void Panic(TInt aLine)
       
    33 	{
       
    34 	RDebug::Printf("UsbAwareApp: PANIC line=%d", aLine);
       
    35 	User::Panic(_L("USBAWAREAPP"), aLine);
       
    36 	}
       
    37 
       
    38 void RunAppL()
       
    39 	{
       
    40 	CUsbAwareAppConsole* awareapp = CUsbAwareAppConsole::NewLC();
       
    41 	awareapp->Start();
       
    42 	CleanupStack::PopAndDestroy(awareapp);
       
    43 	}
       
    44 
       
    45 TInt E32Main()
       
    46 	{
       
    47 	__UHEAP_MARK;
       
    48 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
    49 	CActiveScheduler* activeScheduler = new CActiveScheduler;
       
    50 	TInt err = KErrNoMemory;
       
    51 	if(cleanup && activeScheduler)
       
    52 		{
       
    53 		CActiveScheduler::Install(activeScheduler);
       
    54 		PNT("*** UsbAwareApp E32Main ***\n");
       
    55 		TRAP(err, RunAppL());
       
    56 		}
       
    57 	delete activeScheduler;
       
    58 	delete cleanup;
       
    59 	__UHEAP_MARKEND;
       
    60 	return err;
       
    61 	}
       
    62 
       
    63 
       
    64 
       
    65 CUsbAwareAppConsole* CUsbAwareAppConsole::NewLC()
       
    66 	{
       
    67 	CUsbAwareAppConsole* self = new(ELeave) CUsbAwareAppConsole;
       
    68 	CleanupStack::PushL(self);
       
    69 	self->ConstructL();
       
    70 	PNT("\nConstructed Aware App\n");
       
    71 	return self;
       
    72 	}
       
    73 	
       
    74 CUsbAwareAppConsole::~CUsbAwareAppConsole()
       
    75 	{
       
    76 	PNT("\nClosing Aware App\n");
       
    77 	delete iKeys;
       
    78 	delete iShutdownMonitor;
       
    79 	delete iConsole;
       
    80 	}
       
    81 
       
    82 CUsbAwareAppConsole::CUsbAwareAppConsole()
       
    83 	{
       
    84 	}
       
    85 
       
    86 void CUsbAwareAppConsole::ConstructL()
       
    87 	{
       
    88 	iConsole = Console::NewL(KUsbAwareAppTitle, TSize(KViewerNumCharactersOnLine, KNumLinesInWindow));
       
    89 	Move(-3, 9);
       
    90 	iShutdownMonitor = CShutdownMonitor::NewL(*this);
       
    91 	// After everything else, enable interactivity.
       
    92 	iKeys = CUsbAwareAppKeys::NewL(*this);
       
    93 	Draw();
       
    94 	}
       
    95 
       
    96 void CUsbAwareAppConsole::Move(TInt aX, TInt aY)
       
    97 	{
       
    98 	TRawEvent event;
       
    99 
       
   100 	event.Set(TRawEvent::EKeyDown, EStdKeyLeftShift);
       
   101 	UserSvr::AddEvent(event);
       
   102 
       
   103 	if (aX)
       
   104 		{
       
   105 		if ( aX > 0 )
       
   106 			{
       
   107 			// Move to the right...
       
   108 			for(TInt i=aX; i; i--)
       
   109 				{
       
   110 				event.Set(TRawEvent::EKeyDown, EStdKeyRightArrow);
       
   111 				UserSvr::AddEvent(event);
       
   112 				event.Set(TRawEvent::EKeyUp, EStdKeyRightArrow);
       
   113 				UserSvr::AddEvent(event);
       
   114 				}
       
   115 			}
       
   116 		else
       
   117 			{
       
   118 			// Move to the Left...
       
   119 			for(TInt i=aX; i; i++)
       
   120 				{
       
   121 				event.Set(TRawEvent::EKeyDown, EStdKeyLeftArrow);
       
   122 				UserSvr::AddEvent(event);
       
   123 				event.Set(TRawEvent::EKeyUp, EStdKeyLeftArrow);
       
   124 				UserSvr::AddEvent(event);
       
   125 				}
       
   126 			}
       
   127 		}
       
   128 	
       
   129 	if (aY)
       
   130 		{
       
   131 		if ( aY > 0 )
       
   132 			{
       
   133 			// Move downwards...
       
   134 			for(TInt i=aY; i; i--)
       
   135 				{
       
   136 				event.Set(TRawEvent::EKeyDown, EStdKeyDownArrow);
       
   137 				UserSvr::AddEvent(event);
       
   138 				event.Set(TRawEvent::EKeyUp, EStdKeyDownArrow);
       
   139 				UserSvr::AddEvent(event);
       
   140 				}
       
   141 			}
       
   142 		else
       
   143 			{
       
   144 			// Move upwards...
       
   145 			for(TInt i=aY; i; i++)
       
   146 				{
       
   147 				event.Set(TRawEvent::EKeyDown, EStdKeyUpArrow);
       
   148 				UserSvr::AddEvent(event);
       
   149 				event.Set(TRawEvent::EKeyUp, EStdKeyUpArrow);
       
   150 				UserSvr::AddEvent(event);
       
   151 				}
       
   152 			}
       
   153 		}
       
   154 
       
   155 	event.Set(TRawEvent::EKeyUp, EStdKeyLeftShift);
       
   156 	UserSvr::AddEvent(event);
       
   157 	}
       
   158 
       
   159 void CUsbAwareAppConsole::Start()
       
   160 	{
       
   161 	// Get everything running
       
   162 	CActiveScheduler::Start();
       
   163 	}
       
   164 	
       
   165 void CUsbAwareAppConsole::Stop() const
       
   166 	{
       
   167 	CActiveScheduler::Stop();
       
   168 	}
       
   169 
       
   170 void CUsbAwareAppConsole::Draw()
       
   171 	{
       
   172 	iConsole->ClearScreen();
       
   173 	
       
   174 	iConsole->Printf(_L(
       
   175 //          1         2         3         4         5 \r\n
       
   176 // 12345678901234567890123456789012345678901234567890123
       
   177   "Test USB Aware App:           \r\n"
       
   178  L"\r\n"
       
   179  L"Press 'R' to RequestSession   \r\n"
       
   180 	)
       
   181 	);
       
   182 
       
   183 	}
       
   184 
       
   185 
       
   186 
       
   187 CUsbAwareAppKeys* CUsbAwareAppKeys::NewL(CUsbAwareAppConsole& aUsbAwareAppConsole)
       
   188 	{
       
   189 	CUsbAwareAppKeys* self = new(ELeave) CUsbAwareAppKeys(aUsbAwareAppConsole);
       
   190 	CleanupStack::PushL(self);
       
   191 	self->ConstructL();
       
   192 	CleanupStack::Pop(self);
       
   193 	return self;
       
   194 	}
       
   195 
       
   196 CUsbAwareAppKeys::~CUsbAwareAppKeys()
       
   197 	{
       
   198 	iUsb.Close();
       
   199 	Cancel();
       
   200 	}
       
   201 
       
   202 CUsbAwareAppKeys::CUsbAwareAppKeys(CUsbAwareAppConsole& aUsbAwareAppConsole)
       
   203 	: CActive(EPriorityStandard)
       
   204 	, iUsbAwareAppConsole(aUsbAwareAppConsole)
       
   205 	{
       
   206 	CActiveScheduler::Add(this);
       
   207 	}
       
   208 
       
   209 void CUsbAwareAppKeys::ConstructL()
       
   210 	{
       
   211 	// Connect USB session
       
   212 	TInt err = iUsb.Connect();
       
   213 	LOG("CUsbAwareAppKeys::ConstructL()   iUsb.Connect()   err=%d", err);
       
   214 	User::LeaveIfError(err);
       
   215 	// Allow keyboard input
       
   216 	iUsbAwareAppConsole.iConsole->Read(iStatus);
       
   217 	SetActive();
       
   218 	}
       
   219 
       
   220 void CUsbAwareAppKeys::RunL()
       
   221 	{
       
   222 	if (!IsActive())
       
   223 		{
       
   224 		User::LeaveIfError(iStatus.Int());
       
   225 		
       
   226 		switch(iUsbAwareAppConsole.iConsole->KeyCode())
       
   227 			{
       
   228 		case 'r': case 'R':
       
   229 			{
       
   230 			TInt err = iUsb.RequestSession();
       
   231 			LOG("CUsbAwareAppKeys::RunL()   RequestSession()	err=%d", err);
       
   232 			}
       
   233 			break;
       
   234 			} // switch
       
   235 		iUsbAwareAppConsole.iConsole->Read(iStatus);
       
   236 		SetActive();
       
   237 		}
       
   238 	}
       
   239 
       
   240 void CUsbAwareAppKeys::DoCancel()
       
   241 	{
       
   242 	iUsbAwareAppConsole.iConsole->ReadCancel();
       
   243 	}
       
   244 
       
   245 
       
   246 
       
   247 
       
   248 CShutdownMonitor* CShutdownMonitor::NewL(MShutdownInterface& aUsbAwareAppConsole)
       
   249 	{
       
   250 	CShutdownMonitor* self = new(ELeave) CShutdownMonitor(aUsbAwareAppConsole);
       
   251 	CleanupStack::PushL(self);
       
   252 	self->ConstructL();
       
   253 	CleanupStack::Pop(self);
       
   254 	return self;
       
   255 	}
       
   256 
       
   257 CShutdownMonitor::CShutdownMonitor(MShutdownInterface& aUsbAwareAppConsole)
       
   258 	: CActive(EPriorityLow) // Low so all notifications that want to be serviced will be done first
       
   259 	, iUsbAwareAppConsole(aUsbAwareAppConsole)
       
   260 	{
       
   261 	CActiveScheduler::Add(this);
       
   262 	}
       
   263 
       
   264 void CShutdownMonitor::ConstructL()
       
   265 	{
       
   266 	// Monitor the KUsbControlAppShutdownKey property to tell us when to shut down
       
   267 	TInt err = iShutdownProp.Attach(KUidUsbControlAppCategory, KUsbControlAppShutdownKey);
       
   268 	LOG("CShutdownMonitor::ConstructL	 iShutdownProp.Attach() => %d", err);
       
   269 	User::LeaveIfError(err);
       
   270 	iShutdownProp.Subscribe(iStatus);
       
   271 	SetActive();
       
   272 	TInt val;
       
   273 	// Make sure the cuurent value is 0 - shut down when this changes to 1
       
   274 	err = iShutdownProp.Get(val);
       
   275 	LOG("CShutdownMonitor::ConstructL()	 iShutdownProp.Get(val)		val => %d", val);
       
   276 	LOG("CShutdownMonitor::ConstructL()	 iShutdownProp.Get(val)		err => %d", err);
       
   277 	User::LeaveIfError(err);
       
   278 	__ASSERT_ALWAYS(val==0, PANIC);
       
   279 	}
       
   280 
       
   281 CShutdownMonitor::~CShutdownMonitor()
       
   282 	{
       
   283 	iShutdownProp.Close();
       
   284 	}
       
   285 
       
   286 void CShutdownMonitor::RunL()
       
   287 	{
       
   288 	// Request to shut everything down made in USB Aware App
       
   289 	TInt val;
       
   290 	TInt err = iShutdownProp.Get(val);
       
   291 	LOG("CShutdownMonitor::RunL	 iShutdownProp.Get(val) err => %d", err);
       
   292 	LOG("CShutdownMonitor::RunL	 iShutdownProp.Get(val) val => %d", val);
       
   293 	Cancel(); // Not interested in any more notifications
       
   294 	iUsbAwareAppConsole.Stop(); // Stopping Active Scheduler will results in the destructor getting called
       
   295 	}
       
   296 
       
   297 void CShutdownMonitor::DoCancel()
       
   298 	{
       
   299 	iShutdownProp.Cancel();
       
   300 	}